9f2d6e2408aa36cb5e0d204ac98bd24a30b01454
[rocksndiamonds.git] / src / libgame / gadgets.h
1 // ============================================================================
2 // Artsoft Retro-Game Library
3 // ----------------------------------------------------------------------------
4 // (c) 1995-2014 by Artsoft Entertainment
5 //                  Holger Schemel
6 //                  info@artsoft.org
7 //                  http://www.artsoft.org/
8 // ----------------------------------------------------------------------------
9 // gadgets.h
10 // ============================================================================
11
12 #ifndef GADGETS_H
13 #define GADGETS_H
14
15 #include "system.h"
16
17
18 #define GADGET_FRAME_DELAY_FIRST        250     // delay after first click
19 #define GADGET_FRAME_DELAY              100     // delay for pressed butten
20
21 // gadget types
22 #define GD_TYPE_NORMAL_BUTTON           (1 << 0)
23 #define GD_TYPE_TEXT_BUTTON             (1 << 1)
24 #define GD_TYPE_CHECK_BUTTON            (1 << 2)
25 #define GD_TYPE_RADIO_BUTTON            (1 << 3)
26 #define GD_TYPE_DRAWING_AREA            (1 << 4)
27 #define GD_TYPE_TEXT_INPUT_ALPHANUMERIC (1 << 5)
28 #define GD_TYPE_TEXT_INPUT_NUMERIC      (1 << 6)
29 #define GD_TYPE_TEXT_AREA               (1 << 7)
30 #define GD_TYPE_SELECTBOX               (1 << 8)
31 #define GD_TYPE_SCROLLBAR_VERTICAL      (1 << 9)
32 #define GD_TYPE_SCROLLBAR_HORIZONTAL    (1 << 10)
33
34 #define GD_TYPE_BUTTON                  (GD_TYPE_NORMAL_BUTTON | \
35                                          GD_TYPE_TEXT_BUTTON | \
36                                          GD_TYPE_CHECK_BUTTON | \
37                                          GD_TYPE_RADIO_BUTTON)
38 #define GD_TYPE_SCROLLBAR               (GD_TYPE_SCROLLBAR_VERTICAL | \
39                                          GD_TYPE_SCROLLBAR_HORIZONTAL)
40 #define GD_TYPE_TEXT_INPUT              (GD_TYPE_TEXT_INPUT_ALPHANUMERIC | \
41                                          GD_TYPE_TEXT_INPUT_NUMERIC)
42
43 // gadget events
44 #define GD_EVENT_PRESSED                (1 << 0)
45 #define GD_EVENT_RELEASED               (1 << 1)
46 #define GD_EVENT_MOVING                 (1 << 2)
47 #define GD_EVENT_REPEATED               (1 << 3)
48 #define GD_EVENT_OFF_BORDERS            (1 << 4)
49 #define GD_EVENT_TEXT_RETURN            (1 << 5)
50 #define GD_EVENT_TEXT_LEAVING           (1 << 6)
51 #define GD_EVENT_INFO_ENTERING          (1 << 7)
52 #define GD_EVENT_INFO_LEAVING           (1 << 8)
53 #define GD_EVENT_PIXEL_PRECISE          (1 << 9)
54
55 // gadget button states
56 #define GD_BUTTON_UNPRESSED             0
57 #define GD_BUTTON_PRESSED               1
58
59 // gadget structure constants
60 #define MAX_GADGET_TEXTSIZE             1024
61 #define MAX_INFO_TEXTSIZE               1024
62
63 // gadget creation tags
64 #define GDI_END                         0
65 #define GDI_IMAGE_ID                    1
66 #define GDI_CUSTOM_ID                   2
67 #define GDI_CUSTOM_TYPE_ID              3
68 #define GDI_X                           4
69 #define GDI_Y                           5
70 #define GDI_WIDTH                       6
71 #define GDI_HEIGHT                      7
72 #define GDI_TYPE                        8
73 #define GDI_STATE                       9
74 #define GDI_CHECKED                     10
75 #define GDI_RADIO_NR                    11
76 #define GDI_NUMBER_VALUE                12
77 #define GDI_NUMBER_MIN                  13
78 #define GDI_NUMBER_MAX                  14
79 #define GDI_TEXT_VALUE                  15
80 #define GDI_TEXT_SIZE                   16
81 #define GDI_TEXT_FONT                   17
82 #define GDI_TEXT_FONT_ACTIVE            18
83 #define GDI_TEXT_FONT_UNSELECTABLE      19
84 #define GDI_SELECTBOX_OPTIONS           20
85 #define GDI_SELECTBOX_INDEX             21
86 #define GDI_SELECTBOX_CHAR_UNSELECTABLE 22
87 #define GDI_DESIGN_UNPRESSED            23
88 #define GDI_DESIGN_PRESSED              24
89 #define GDI_ALT_DESIGN_UNPRESSED        25
90 #define GDI_ALT_DESIGN_PRESSED          26
91 #define GDI_BORDER_SIZE                 27
92 #define GDI_BORDER_SIZE_SELECTBUTTON    28
93 #define GDI_DESIGN_WIDTH                29
94 #define GDI_DECORATION_DESIGN           30
95 #define GDI_DECORATION_POSITION         31
96 #define GDI_DECORATION_SIZE             32
97 #define GDI_DECORATION_SHIFTING         33
98 #define GDI_DECORATION_MASKED           34
99 #define GDI_EVENT_MASK                  35
100 #define GDI_EVENT                       36
101 #define GDI_CALLBACK_INFO               37
102 #define GDI_CALLBACK_ACTION             38
103 #define GDI_AREA_SIZE                   39
104 #define GDI_ITEM_SIZE                   40
105 #define GDI_SCROLLBAR_ITEMS_MAX         41
106 #define GDI_SCROLLBAR_ITEMS_VISIBLE     42
107 #define GDI_SCROLLBAR_ITEM_POSITION     43
108 #define GDI_WHEEL_AREA_X                44
109 #define GDI_WHEEL_AREA_Y                45
110 #define GDI_WHEEL_AREA_WIDTH            46
111 #define GDI_WHEEL_AREA_HEIGHT           47
112 #define GDI_INFO_TEXT                   48
113 #define GDI_ACTIVE                      49
114 #define GDI_DIRECT_DRAW                 50
115 #define GDI_OVERLAY_TOUCH_BUTTON        51
116 #define GDI_CALLBACK_ACTION_ALWAYS      52
117
118 // gadget deactivation hack
119 #define GDI_ACTIVE_POS(a)               ((a) < 0 ? POS_OFFSCREEN : (a))
120
121
122 typedef void (*gadget_function)(void *);
123
124 struct GadgetBorder
125 {
126   int xsize, ysize;                     // size of gadget border
127   int xsize_selectbutton;               // for selectbox gadgets
128   int width;                            // for selectbox/text input gadgets
129 };
130
131 struct GadgetDesign
132 {
133   Bitmap *bitmap;                       // Bitmap with gadget surface
134   int x, y;                             // position of rectangle in Bitmap
135 };
136
137 struct GadgetDecoration
138 {
139   struct GadgetDesign design;           // decoration design structure
140   int x, y;                             // position of deco on the gadget
141   int width, height;                    // width and height of decoration
142   int xshift, yshift;                   // deco shifting when gadget pressed
143   boolean masked;                       // draw decoration masked over button
144 };
145
146 struct GadgetEvent
147 {
148   unsigned int type;                    // event type
149   int button;                           // button number for button events
150   int mx, my;                           // raw gadget position at event time
151   int x, y;                             // gadget position at event time
152   boolean off_borders;                  // mouse pointer outside gadget?
153   int item_x, item_y, item_position;    // new item position
154 };
155
156 struct GadgetDrawingArea
157 {
158   int area_xsize, area_ysize;           // size of drawing area (in items)
159   int item_xsize, item_ysize;           // size of each item in drawing area
160 };
161
162 struct GadgetTextButton
163 {
164   char value[MAX_GADGET_TEXTSIZE + 1];  // text written on the button
165   int size;                             // maximal size of button text
166 };
167
168 struct GadgetTextInput
169 {
170   char value[MAX_GADGET_TEXTSIZE + 1];  // text string in input field
171   char last_value[MAX_GADGET_TEXTSIZE + 1];// last text string in input field
172   int cursor_position;                  // actual text cursor position
173   int number_value;                     // integer value, if numeric
174   int number_min;                       // minimal allowed numeric value
175   int number_max;                       // maximal allowed numeric value
176   int size;                             // maximal size of input text
177 };
178
179 struct GadgetTextArea
180 {
181   char value[MAX_GADGET_TEXTSIZE + 1];  // text string in input field
182   char last_value[MAX_GADGET_TEXTSIZE + 1];// last text string in input field
183   int cursor_position;                  // actual text cursor position
184   int cursor_x;                         // actual x cursor position
185   int cursor_y;                         // actual y cursor position
186   int cursor_x_preferred;               // "preferred" x cursor position
187   int size;                             // maximal size of input text
188   int xsize, ysize;                     // size of text area (in chars)
189 };
190
191 struct GadgetSelectbox
192 {
193   struct ValueTextInfo *options;        // pointer to text/value array
194   int index;                            // index of actual text string
195   int size;                             // maximal size of text strings
196   char char_unselectable;               // first char of unselectable options
197
198   // automatically determined values
199   int x, y;                             // open selectbox position
200   int width, height;                    // open selectbox size
201   int num_values;                       // number of text strings
202   Pixel inverse_color;                  // color for highlighting
203
204   // runtime values
205   boolean open;                         // opening state of selectbox
206   boolean stay_open;                    // open after button release
207   int current_index;                    // index of text while selecting
208 };
209
210 struct GadgetScrollbar
211 {
212   int items_max;                        // number of items to access
213   int items_visible;                    // number of visible items
214   int item_position;                    // actual item position
215   int size_min;                         // minimal scrollbar size
216   int size_max;                         // this is either width or height
217   int size_max_cmp;                     // needed for minimal scrollbar size
218   int size;                             // scrollbar size on screen
219   int position;                         // scrollbar position on screen
220   int position_max;                     // bottom/right scrollbar position
221   int drag_position;                    // drag position on scrollbar
222   int correction;                       // scrollbar position correction
223 };
224
225 struct GadgetWheelArea
226 {
227   int x, y;                             // active area for wheel (start)
228   int width, height;                    // active area for wheel (size)
229 };
230
231 struct GadgetInfo
232 {
233   boolean deactivated;                  // flag to deactivate gadget
234
235   int id;                               // internal gadget identifier
236   int image_id;                         // internal gadget image identifier
237   int custom_id;                        // custom gadget identifier
238   int custom_type_id;                   // custom gadget type identifier
239   char info_text[MAX_INFO_TEXTSIZE + 1];// short popup info text
240   int x, y;                             // gadget position
241   int orig_x, orig_y;                   // gadget position (original)
242   int width, height;                    // gadget size
243   unsigned int type;                    // type (button, text input, ...)
244   unsigned int state;                   // state (pressed, released, ...)
245   boolean checked;                      // check/radio button state
246   int radio_nr;                         // number of radio button series
247   boolean mapped;                       // gadget is mapped on the screen
248   boolean active;                       // gadget is active
249   boolean direct_draw;                  // directly draw to frontbuffer
250   boolean overlay_touch_button;         // gadget is overlay touch button
251   int overlay_touch_button_alpha;       // overlay touch button alpha value
252   boolean callback_action_always;       // also callback if gadget unchanged
253   int font;                             // font to use when inactive
254   int font_active;                      // font to use when active
255   int font_unselectable;                // font to use when unselectable
256   struct GadgetBorder border;           // gadget border design
257   struct GadgetDesign design[2];        // 0: normal; 1: pressed
258   struct GadgetDesign alt_design[2];    // alternative design
259   struct GadgetDecoration deco;         // decoration on top of gadget
260   unsigned int event_mask;              // possible events for this gadget
261   struct GadgetEvent event;             // actual gadget event
262   gadget_function callback_info;        // function for pop-up info text
263   gadget_function callback_action;      // function for gadget action
264   struct GadgetDrawingArea drawing;     // fields for drawing area gadget
265   struct GadgetTextButton textbutton;   // fields for text button gadget
266   struct GadgetTextInput textinput;     // fields for text input gadget
267   struct GadgetTextArea textarea;       // fields for text area gadget
268   struct GadgetSelectbox selectbox;     // fields for selectbox gadget
269   struct GadgetScrollbar scrollbar;     // fields for scrollbar gadget
270   struct GadgetWheelArea wheelarea;     // fields for scroll wheel area
271   struct GadgetInfo *next;              // next list entry
272 };
273
274
275 void InitGadgetsSoundCallback(void (*activating_function)(void),
276                               void (*selecting_function)(void));
277
278 struct GadgetInfo *CreateGadget(int, ...);
279 void FreeGadget(struct GadgetInfo *);
280
281 void ModifyGadget(struct GadgetInfo *, int, ...);
282 void RedrawGadget(struct GadgetInfo *);
283
284 void MapGadget(struct GadgetInfo *);
285 void UnmapGadget(struct GadgetInfo *);
286 void UnmapAllGadgets(void);
287 void RemapAllGadgets(void);
288
289 void SetGadgetsPosition_OverlayTouchButtons(void);
290 void DrawGadgets_OverlayTouchButtons(void);
291 boolean CheckPosition_OverlayTouchButtons(int, int, int);
292
293 boolean anyTextInputGadgetActive(void);
294 boolean anyTextAreaGadgetActive(void);
295 boolean anySelectboxGadgetActive(void);
296 boolean anyScrollbarGadgetActive(void);
297 boolean anyTextGadgetActive(void);
298
299 void ClickOnGadget(struct GadgetInfo *, int);
300
301 boolean HandleGadgets(int, int, int);
302 boolean HandleGadgetsKeyInput(Key);
303
304 void DumpGadgetIdentifiers(void);
305 boolean DoGadgetAction(int);
306
307 #endif  // GADGETS_H