changed comments from old to new style (one-line comments only)
[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_CALLBACK_ACTION_ALWAYS      51
116
117 // gadget deactivation hack
118 #define GDI_ACTIVE_POS(a)               ((a) < 0 ? POS_OFFSCREEN : (a))
119
120
121 typedef void (*gadget_function)(void *);
122
123 struct GadgetBorder
124 {
125   int xsize, ysize;                     // size of gadget border
126   int xsize_selectbutton;               // for selectbox gadgets
127   int width;                            // for selectbox/text input gadgets
128 };
129
130 struct GadgetDesign
131 {
132   Bitmap *bitmap;                       // Bitmap with gadget surface
133   int x, y;                             // position of rectangle in Bitmap
134 };
135
136 struct GadgetDecoration
137 {
138   struct GadgetDesign design;           // decoration design structure
139   int x, y;                             // position of deco on the gadget
140   int width, height;                    // width and height of decoration
141   int xshift, yshift;                   // deco shifting when gadget pressed
142   boolean masked;                       // draw decoration masked over button
143 };
144
145 struct GadgetEvent
146 {
147   unsigned int type;                    // event type
148   int button;                           // button number for button events
149   int mx, my;                           // raw gadget position at event time
150   int x, y;                             // gadget position at event time
151   boolean off_borders;                  // mouse pointer outside gadget?
152   int item_x, item_y, item_position;    // new item position
153 };
154
155 struct GadgetDrawingArea
156 {
157   int area_xsize, area_ysize;           // size of drawing area (in items)
158   int item_xsize, item_ysize;           // size of each item in drawing area
159 };
160
161 struct GadgetTextButton
162 {
163   char value[MAX_GADGET_TEXTSIZE + 1];  // text written on the button
164   int size;                             // maximal size of button text
165 };
166
167 struct GadgetTextInput
168 {
169   char value[MAX_GADGET_TEXTSIZE + 1];  // text string in input field
170   char last_value[MAX_GADGET_TEXTSIZE + 1];// last text string in input field
171   int cursor_position;                  // actual text cursor position
172   int number_value;                     // integer value, if numeric
173   int number_min;                       // minimal allowed numeric value
174   int number_max;                       // maximal allowed numeric value
175   int size;                             // maximal size of input text
176 };
177
178 struct GadgetTextArea
179 {
180   char value[MAX_GADGET_TEXTSIZE + 1];  // text string in input field
181   char last_value[MAX_GADGET_TEXTSIZE + 1];// last text string in input field
182   int cursor_position;                  // actual text cursor position
183   int cursor_x;                         // actual x cursor position
184   int cursor_y;                         // actual y cursor position
185   int cursor_x_preferred;               // "preferred" x cursor position
186   int size;                             // maximal size of input text
187   int xsize, ysize;                     // size of text area (in chars)
188 };
189
190 struct GadgetSelectbox
191 {
192   struct ValueTextInfo *options;        // pointer to text/value array
193   int index;                            // index of actual text string
194   int size;                             // maximal size of text strings
195   char char_unselectable;               // first char of unselectable options
196
197   // automatically determined values
198   int x, y;                             // open selectbox position
199   int width, height;                    // open selectbox size
200   int num_values;                       // number of text strings
201   Pixel inverse_color;                  // color for highlighting
202
203   // runtime values
204   boolean open;                         // opening state of selectbox
205   boolean stay_open;                    // open after button release
206   int current_index;                    // index of text while selecting
207 };
208
209 struct GadgetScrollbar
210 {
211   int items_max;                        // number of items to access
212   int items_visible;                    // number of visible items
213   int item_position;                    // actual item position
214   int size_min;                         // minimal scrollbar size
215   int size_max;                         // this is either width or height
216   int size_max_cmp;                     // needed for minimal scrollbar size
217   int size;                             // scrollbar size on screen
218   int position;                         // scrollbar position on screen
219   int position_max;                     // bottom/right scrollbar position
220   int drag_position;                    // drag position on scrollbar
221   int correction;                       // scrollbar position correction
222 };
223
224 struct GadgetWheelArea
225 {
226   int x, y;                             // active area for wheel (start)
227   int width, height;                    // active area for wheel (size)
228 };
229
230 struct GadgetInfo
231 {
232   boolean deactivated;                  // flag to deactivate gadget
233
234   int id;                               // internal gadget identifier
235   int image_id;                         // internal gadget image identifier
236   int custom_id;                        // custom gadget identifier
237   int custom_type_id;                   // custom gadget type identifier
238   char info_text[MAX_INFO_TEXTSIZE + 1];// short popup info text
239   int x, y;                             // gadget position
240   int width, height;                    // gadget size
241   unsigned int type;                    // type (button, text input, ...)
242   unsigned int state;                   // state (pressed, released, ...)
243   boolean checked;                      // check/radio button state
244   int radio_nr;                         // number of radio button series
245   boolean mapped;                       // gadget is mapped on the screen
246   boolean active;                       // gadget is active
247   boolean direct_draw;                  // directly draw to frontbuffer
248   boolean callback_action_always;       // also callback if gadget unchanged
249   int font;                             // font to use when inactive
250   int font_active;                      // font to use when active
251   int font_unselectable;                // font to use when unselectable
252   struct GadgetBorder border;           // gadget border design
253   struct GadgetDesign design[2];        // 0: normal; 1: pressed
254   struct GadgetDesign alt_design[2];    // alternative design
255   struct GadgetDecoration deco;         // decoration on top of gadget
256   unsigned int event_mask;              // possible events for this gadget
257   struct GadgetEvent event;             // actual gadget event
258   gadget_function callback_info;        // function for pop-up info text
259   gadget_function callback_action;      // function for gadget action
260   struct GadgetDrawingArea drawing;     // fields for drawing area gadget
261   struct GadgetTextButton textbutton;   // fields for text button gadget
262   struct GadgetTextInput textinput;     // fields for text input gadget
263   struct GadgetTextArea textarea;       // fields for text area gadget
264   struct GadgetSelectbox selectbox;     // fields for selectbox gadget
265   struct GadgetScrollbar scrollbar;     // fields for scrollbar gadget
266   struct GadgetWheelArea wheelarea;     // fields for scroll wheel area
267   struct GadgetInfo *next;              // next list entry
268 };
269
270
271 void InitGadgetsSoundCallback(void (*activating_function)(void),
272                               void (*selecting_function)(void));
273
274 struct GadgetInfo *CreateGadget(int, ...);
275 void FreeGadget(struct GadgetInfo *);
276
277 void ModifyGadget(struct GadgetInfo *, int, ...);
278 void RedrawGadget(struct GadgetInfo *);
279
280 void MapGadget(struct GadgetInfo *);
281 void UnmapGadget(struct GadgetInfo *);
282 void UnmapAllGadgets(void);
283 void RemapAllGadgets(void);
284
285 boolean anyTextInputGadgetActive(void);
286 boolean anyTextAreaGadgetActive(void);
287 boolean anySelectboxGadgetActive(void);
288 boolean anyScrollbarGadgetActive(void);
289 boolean anyTextGadgetActive(void);
290
291 void ClickOnGadget(struct GadgetInfo *, int);
292
293 boolean HandleGadgets(int, int, int);
294 boolean HandleGadgetsKeyInput(Key);
295
296 void DumpGadgetIdentifiers(void);
297 boolean DoGadgetAction(int);
298
299 #endif  // GADGETS_H