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