RTS2D
include/gui/nihgui.h
00001 /*
00002  *  Este codigo foi retirado da pasta /examples do Allegro 5.0.10 e foram feitas algumas modificacoes
00003  *  http://sourceforge.net/projects/alleg/files/allegro/5.0.10/allegro-5.0.10.tar.gz/download
00004  */
00005 
00006 #ifndef __included_nihgui_hpp
00007 #define __included_nihgui_hpp
00008 
00009 #include <list>
00010 #include <string>
00011 #include <vector>
00012 #include <cstring>
00013 
00014 #include "allegro5/allegro.h"
00015 #include "allegro5/allegro_font.h"
00016 #include "UnitProperty.h"
00017 #include "BuildingProperty.h"
00018 #include "CommandProperty.h"
00019 
00020 class Theme;
00021 class Dialog;
00022 class Widget;
00023 
00024 class Theme {
00025 public:
00026     ALLEGRO_COLOR        bg;
00027     ALLEGRO_COLOR        fg;
00028     ALLEGRO_COLOR        highlight;
00029     const ALLEGRO_FONT   *font;
00030 
00031     // Null font is fine if you don't use a widget that requires text.
00032     explicit Theme(const ALLEGRO_FONT *font=NULL);
00033 };
00034 
00035 class Widget {
00036 private:
00037     int      grid_x;
00038     int      grid_y;
00039     int      grid_w;
00040     int      grid_h;
00041 
00042 protected:
00043     Dialog   *dialog;
00044     int      x1;
00045     int      y1;
00046     int      x2;
00047     int      y2;
00048 
00049 public:
00050     Widget();
00051     virtual ~Widget() {}
00052 
00053     void           configure(int xsize, int ysize,
00054                              int x_padding, int y_padding);
00055     virtual bool   contains(int x, int y);
00056     unsigned int   width()  {
00057         return x2 - x1 + 1;
00058     }
00059     unsigned int   height() {
00060         return y2 - y1 + 1;
00061     }
00062 
00063     virtual bool   want_mouse_focus() {
00064         return true;
00065     }
00066     virtual void   got_mouse_focus() {}
00067     virtual void   lost_mouse_focus() {}
00068     virtual void   on_mouse_button_down(int mx, int my) {
00069         (void)mx;
00070         (void)my;
00071     }
00072     virtual void   on_mouse_button_hold(int mx, int my) {
00073         (void)mx;
00074         (void)my;
00075     }
00076     virtual void   on_mouse_button_up(int mx, int my) {
00077         (void)mx;
00078         (void)my;
00079     }
00080     virtual void   on_click(int mx, int my) {
00081         (void)mx;
00082         (void)my;
00083     }
00084 
00085     virtual bool   want_key_focus() {
00086         return false;
00087     }
00088     virtual void   got_key_focus() {}
00089     virtual void   lost_key_focus() {}
00090     virtual void   on_key_down(const ALLEGRO_KEYBOARD_EVENT & event) {
00091         (void)event;
00092     }
00093 
00094     virtual void   draw() = 0;
00095 
00096     friend class Dialog;
00097 };
00098 
00099 class EventHandler {
00100 public:
00101     virtual ~EventHandler() {}
00102     virtual void   handle_event(const ALLEGRO_EVENT & event) = 0;
00103 };
00104 
00105 class Dialog {
00106 private:
00107     const Theme &        theme;
00108     ALLEGRO_DISPLAY *    display;
00109     ALLEGRO_EVENT_QUEUE *event_queue;
00110     int                  grid_m;
00111     int                  grid_n;
00112     int                  x_padding;
00113     int                  y_padding;
00114 
00115     bool                 draw_requested;
00116     bool                 quit_requested;
00117     std::list<Widget *>  all_widgets;
00118     Widget *             mouse_over_widget;
00119     Widget *             mouse_down_widget;
00120     Widget *             key_widget;
00121 
00122     EventHandler *       event_handler;
00123 
00124 public:
00125     Dialog(const Theme & theme, ALLEGRO_DISPLAY *display,
00126            int grid_m, int grid_n);
00127     ~Dialog();
00128 
00129     void           set_padding(int x_padding, int y_padding);
00130     void           add(Widget & widget, int grid_x, int grid_y,
00131                        int grid_w, int grid_h);
00132     void           prepare();
00133     void           run_step(bool block);
00134     void           request_quit();
00135     bool           is_quit_requested() const;
00136     void           request_draw();
00137     bool           is_draw_requested() const;
00138     void           draw();
00139     const Theme &  get_theme() const;
00140 
00141     void           register_event_source(ALLEGRO_EVENT_SOURCE *source);
00142     void           set_event_handler(EventHandler *handler);
00143 
00144 private:
00145     void           configure_all();
00146     void           on_key_down(const ALLEGRO_KEYBOARD_EVENT & event);
00147     void           on_mouse_axes(const ALLEGRO_MOUSE_EVENT & event);
00148     void           check_mouse_over(int mx, int my);
00149     void           on_mouse_button_down(const ALLEGRO_MOUSE_EVENT & event);
00150     void           on_mouse_button_up(const ALLEGRO_MOUSE_EVENT & event);
00151 };
00152 
00153 /*---------------------------------------------------------------------------*/
00154 
00155 class Label : public Widget {
00156 private:
00157     std::string    text;
00158     bool           centred;
00159 
00160 public:
00161     Label(std::string text="", bool centred=true);
00162     void set_text(std::string text);
00163     virtual void   draw();
00164     virtual bool   want_mouse_focus();
00165 };
00166 
00167 class Button : public Widget {
00168 protected:
00169     std::string    text;
00170     bool           pushed;
00171 
00172 public:
00173     explicit Button(std::string text);
00174     virtual void   on_mouse_button_down(int mx, int my);
00175     virtual void   on_mouse_button_up(int mx, int my);
00176     virtual void   draw();
00177     void set_text(std::string text);
00178     std::string get_text();
00179     bool           get_pushed();
00180 };
00181 
00182 class ToggleButton : public Button {
00183 public:
00184     explicit ToggleButton(std::string text);
00185     virtual void   on_mouse_button_down(int mx, int my);
00186     virtual void   on_mouse_button_up(int mx, int my);
00187 
00188     void           set_pushed(bool pushed);
00189 };
00190 
00191 class List : public Widget {
00192 private:
00193     static const   std::string empty_string;
00194 
00195     std::vector<std::string> items;
00196     unsigned int   selected_item;
00197 
00198 public:
00199     List(int initial_selection = 0);
00200     virtual bool   want_key_focus();
00201     virtual void   on_key_down(const ALLEGRO_KEYBOARD_EVENT & event);
00202     virtual void   on_click(int mx, int my);
00203     virtual void   draw();
00204 
00205     void           clear_items();
00206     void           append_item(std::string text);
00207     const std::string & get_selected_item_text() const;
00208     int            get_cur_value() const;
00209 };
00210 
00211 class VSlider : public Widget {
00212 private:
00213     int   cur_value;
00214     int   max_value;
00215 
00216 public:
00217     VSlider(int cur_value = 0, int max_value = 1);
00218 
00219     virtual void   on_mouse_button_down(int mx, int my);
00220     virtual void   on_mouse_button_hold(int mx, int my);
00221     virtual void   draw();
00222 
00223     int            get_cur_value() const;
00224     void           set_cur_value(int v);
00225 };
00226 
00227 class HSlider : public Widget {
00228 private:
00229     int   cur_value;
00230     int   max_value;
00231 
00232 public:
00233     HSlider(int cur_value = 0, int max_value = 1);
00234 
00235     virtual void   on_mouse_button_down(int mx, int my);
00236     virtual void   on_mouse_button_hold(int mx, int my);
00237     virtual void   draw();
00238 
00239     int            get_cur_value() const;
00240     void           set_cur_value(int v);
00241 };
00242 
00243 class TextEntry : public Widget {
00244 private:
00245     static const int CURSOR_WIDTH = 8;
00246 
00247     ALLEGRO_USTR   *text;
00248     bool           focused;
00249     int            cursor_pos;
00250     int            left_pos;
00251 
00252 public:
00253     explicit TextEntry(const char *initial_text="");
00254     ~TextEntry();
00255 
00256     virtual bool   want_key_focus();
00257     virtual void   got_key_focus();
00258     virtual void   lost_key_focus();
00259     virtual void   on_key_down(const ALLEGRO_KEYBOARD_EVENT & event);
00260     virtual void   draw();
00261 
00262     const char *   get_text();
00263 
00264 private:
00265     void maybe_scroll();
00266 };
00267 
00268 #endif
00269 
00270 /* vim: set sts=3 sw=3 et: */
 Todos Classes Funções