File: nihgui.hpp

package info (click to toggle)
allegro5 2%3A5.2.10.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 10,820 kB
  • sloc: ansic: 109,795; cpp: 12,976; objc: 4,592; java: 2,845; python: 2,595; javascript: 1,238; sh: 1,008; makefile: 40; xml: 27; pascal: 24
file content (245 lines) | stat: -rw-r--r-- 6,456 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
#ifndef __included_nihgui_hpp
#define __included_nihgui_hpp

#include <list>
#include <string>
#include <vector>

#include "allegro5/allegro.h"
#include "allegro5/allegro_font.h"

class Theme;
class Dialog;
class Widget;

class Theme {
public:
   ALLEGRO_COLOR        bg;
   ALLEGRO_COLOR        fg;
   ALLEGRO_COLOR        highlight;
   const ALLEGRO_FONT   *font;

   // Null font is fine if you don't use a widget that requires text.
   explicit Theme(const ALLEGRO_FONT *font=NULL);
};

class Widget {
private:
   int      grid_x;
   int      grid_y;
   int      grid_w;
   int      grid_h;

protected:
   Dialog   *dialog;
   int      x1;
   int      y1;
   int      x2;
   int      y2;
   bool     disabled;

public:
   Widget();
   virtual ~Widget() {}

   void           configure(int xsize, int ysize,
                     int x_padding, int y_padding);
   virtual bool   contains(int x, int y);
   unsigned int   width()  { return x2 - x1 + 1; }
   unsigned int   height() { return y2 - y1 + 1; }

   virtual bool   want_mouse_focus() { return true; }
   virtual void   got_mouse_focus() {}
   virtual void   lost_mouse_focus() {}
   virtual void   on_mouse_button_down(int mx, int my) { (void)mx; (void)my; }
   virtual void   on_mouse_button_hold(int mx, int my) { (void)mx; (void)my; }
   virtual void   on_mouse_button_up(int mx, int my) { (void)mx; (void)my; }
   virtual void   on_click(int mx, int my) { (void)mx; (void)my; }

   virtual bool   want_key_focus() { return false; }
   virtual void   got_key_focus() {}
   virtual void   lost_key_focus() {}
   virtual void   on_key_down(const ALLEGRO_KEYBOARD_EVENT & event) {
                     (void)event; }

   virtual void   draw() = 0;
   virtual void   set_disabled(bool value) { disabled = value;  }
   virtual bool   is_disabled() { return disabled;   }

   friend class Dialog;
};

class EventHandler {
public:
   virtual ~EventHandler() {}
   virtual void   handle_event(const ALLEGRO_EVENT & event) = 0;
};

class Dialog {
private:
   const Theme &        theme;
   ALLEGRO_DISPLAY *    display;
   ALLEGRO_EVENT_QUEUE *event_queue;
   int                  grid_m;
   int                  grid_n;
   int                  x_padding;
   int                  y_padding;

   bool                 draw_requested;
   bool                 quit_requested;

   std::list<Widget *>  all_widgets;
   Widget *             mouse_over_widget;
   Widget *             mouse_down_widget;
   Widget *             key_widget;

   EventHandler *       event_handler;

public:
   Dialog(const Theme & theme, ALLEGRO_DISPLAY *display,
      int grid_m, int grid_n);
   ~Dialog();

   void           set_padding(int x_padding, int y_padding);
   void           add(Widget & widget, int grid_x, int grid_y,
                     int grid_w, int grid_h);
   void           prepare();
   void           run_step(bool block);
   void           request_quit();
   bool           is_quit_requested() const;
   void           request_draw();
   bool           is_draw_requested() const;
   void           draw();
   const Theme &  get_theme() const;

   void           register_event_source(ALLEGRO_EVENT_SOURCE *source);
   void           set_event_handler(EventHandler *handler);

private:
   void           configure_all();
   void           on_key_down(const ALLEGRO_KEYBOARD_EVENT & event);
   void           on_mouse_axes(const ALLEGRO_MOUSE_EVENT & event);
   void           check_mouse_over(int mx, int my);
   void           on_mouse_button_down(const ALLEGRO_MOUSE_EVENT & event);
   void           on_mouse_button_up(const ALLEGRO_MOUSE_EVENT & event);
};

/*---------------------------------------------------------------------------*/

class Label : public Widget {
private:
   std::string    text;
   bool           centred;

public:
   Label(std::string text="", bool centred=true);
   void set_text(std::string text);
   virtual void   draw();
   virtual bool   want_mouse_focus();
};

class Button : public Widget {
protected:
   std::string    text;
   bool           pushed;

public:
   explicit Button(std::string text);
   virtual void   on_mouse_button_down(int mx, int my);
   virtual void   on_mouse_button_up(int mx, int my);
   virtual void   draw();

   bool           get_pushed();
};

class ToggleButton : public Button {
public:
   explicit ToggleButton(std::string text);
   virtual void   on_mouse_button_down(int mx, int my);
   virtual void   on_mouse_button_up(int mx, int my);

   void           set_pushed(bool pushed);
};

class List : public Widget {
private:
   static const   std::string empty_string;

   std::vector<std::string> items;
   unsigned int   selected_item;

public:
   List(int initial_selection = 0);
   virtual bool   want_key_focus();
   virtual void   on_key_down(const ALLEGRO_KEYBOARD_EVENT & event);
   virtual void   on_click(int mx, int my);
   virtual void   draw();

   void           clear_items();
   void           append_item(std::string text);
   const std::string & get_selected_item_text() const;
   int            get_cur_value() const;
};

class VSlider : public Widget {
private:
   int   cur_value;
   int   max_value;

public:
   VSlider(int cur_value = 0, int max_value = 1);

   virtual void   on_mouse_button_down(int mx, int my);
   virtual void   on_mouse_button_hold(int mx, int my);
   virtual void   draw();

   int            get_max_value() const;
   int            get_cur_value() const;
   void           set_cur_value(int v);
};

class HSlider : public Widget {
private:
   int   cur_value;
   int   max_value;

public:
   HSlider(int cur_value = 0, int max_value = 1);

   virtual void   on_mouse_button_down(int mx, int my);
   virtual void   on_mouse_button_hold(int mx, int my);
   virtual void   draw();

   int            get_max_value() const;
   int            get_cur_value() const;
   void           set_cur_value(int v);
};

class TextEntry : public Widget {
private:
   static const int CURSOR_WIDTH = 8;

   ALLEGRO_USTR   *text;
   bool           focused;
   int            cursor_pos;
   int            left_pos;

public:
   explicit TextEntry(const char *initial_text="");
   ~TextEntry();

   virtual bool   want_key_focus();
   virtual void   got_key_focus();
   virtual void   lost_key_focus();
   virtual void   on_key_down(const ALLEGRO_KEYBOARD_EVENT & event);
   virtual void   draw();

   const char *   get_text();

private:
   void maybe_scroll();
};

#endif

/* vim: set sts=3 sw=3 et: */