summaryrefslogtreecommitdiffstats
path: root/matchblox/menu/button.c
blob: c38bbfe16df05928e24a026a947be0045210fe81 (plain)
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
#ifdef G_OS_WIN32
  #define WIN32_LEAN_AND_MEAN 1
  #include <windows.h>
#else
  #define FALSE 0
  #define TRUE !FALSE
  #define max(a, b)  (((a) > (b)) ? (a) : (b))

#endif

#include <string.h>

#include <GL/glut.h>
#include <string.h>
#include <stdio.h>
#include <math.h>

#include "button.h"
#include "font.h"


struct ButtonStruct ButtonCreate(int f_iXPos, int f_iYPos, int f_iWidth, int f_iHeight, char *f_pcTitle, int f_iType, int f_iGroup, int f_iId, void (*f_fpFunc)(void))
{
  struct ButtonStruct l_sButton;

  // copy parameters
  l_sButton.m_iXPos   = f_iXPos;
  l_sButton.m_iYPos   = f_iYPos;
  l_sButton.m_iType   = f_iType;
  l_sButton.m_iHeight = f_iHeight;
  l_sButton.m_iWidth  = f_iWidth;
  l_sButton.m_iGroup  = f_iGroup;
  l_sButton.m_iId     = f_iId;
  l_sButton.m_fpFunc  = f_fpFunc;

  // copy title
  memset(&l_sButton.m_pcTitle, 0, sizeof(l_sButton.m_pcTitle));
  memcpy(l_sButton.m_pcTitle, f_pcTitle, strlen(f_pcTitle));

  switch (f_iType)
  {
    case BUTTON_CLICK:
      l_sButton.m_psImgNormal[0] = BitmapLoad("img/button_click_left.bmp");
      l_sButton.m_psImgNormal[1] = BitmapLoad("img/button_click_center.bmp");
      l_sButton.m_psImgNormal[2] = BitmapLoad("img/button_click_right.bmp");

      l_sButton.m_psImgHover[0] = BitmapLoad("img/button_click_left_hover.bmp");
      l_sButton.m_psImgHover[1] = BitmapLoad("img/button_click_center_hover.bmp");
      l_sButton.m_psImgHover[2] = BitmapLoad("img/button_click_right_hover.bmp");

      BitmapConvertWidth(&l_sButton.m_psImgNormal[0], f_iHeight);
      BitmapConvertWidth(&l_sButton.m_psImgNormal[1], f_iHeight);
      BitmapConvertWidth(&l_sButton.m_psImgNormal[2], f_iHeight);

      BitmapConvertWidth(&l_sButton.m_psImgHover[0], f_iHeight);
      BitmapConvertWidth(&l_sButton.m_psImgHover[1], f_iHeight);
      BitmapConvertWidth(&l_sButton.m_psImgHover[2], f_iHeight);

      // calculate (center)width of image
      l_sButton.m_iCenterWidth = max(f_iWidth - l_sButton.m_psImgNormal[0].m_iWidth - l_sButton.m_psImgNormal[2].m_iWidth, 0);
      l_sButton.m_iWidth = l_sButton.m_iCenterWidth + l_sButton.m_psImgNormal[0].m_iWidth + l_sButton.m_psImgNormal[2].m_iWidth;
      break;

    case BUTTON_RADIO:
      l_sButton.m_psImgNormal[0]  = BitmapLoad("img/button_radio.bmp");
      l_sButton.m_psImgHover[0]   = BitmapLoad("img/button_radio_hover.bmp");
      l_sButton.m_psImgPressed[0] = BitmapLoad("img/button_radio_pressed.bmp");

      BitmapConvertWidth(&l_sButton.m_psImgNormal[0],  f_iHeight);
      BitmapConvertWidth(&l_sButton.m_psImgHover[0],   f_iHeight);
      BitmapConvertWidth(&l_sButton.m_psImgPressed[0], f_iHeight);

      l_sButton.m_iCenterWidth = 0;
      l_sButton.m_iHeight = f_iHeight;
      l_sButton.m_iWidth  = f_iWidth;
      break;

    default:
      break;
  }

  l_sButton.m_bPressed = FALSE;
  l_sButton.m_bHover   = FALSE;

  return l_sButton;

} // ButtonCreate


void ButtonClickRender(struct ButtonStruct *f_sButton)
{
  char   l_pcTitle[BUTTON_MAX_TITLE];
  struct BitmapStruct *l_sImgButton;
  int    l_iImageIdLeft, l_iImageIdCenter, l_iImageIdRight;
  int    l_iLen, l_iXOffset, l_iYOffset, l_iWidth;
  struct ColorStruct l_sColor = {0, 0, 0};

  // copy button attributes
  int l_iCnWdt  = f_sButton->m_iCenterWidth;
  int l_iHeight = f_sButton->m_iHeight;
  int l_iXPos   = f_sButton->m_iXPos;
  int l_iYPos   = f_sButton->m_iYPos;

  if (f_sButton->m_bPressed)
  {
    glColor3d(0.8, 0.9, 1);
  }
  else
  {
    glColor3d(1, 1, 1);
  }

  // get the right image struct
  if (f_sButton->m_bHover)
  {
    l_sImgButton = f_sButton->m_psImgHover;
  }
  else
  {
    l_sImgButton = f_sButton->m_psImgNormal;
  }

  // copy buttom images
  l_iImageIdLeft   = l_sImgButton[0].m_iImageId;
  l_iImageIdCenter = l_sImgButton[1].m_iImageId;
  l_iImageIdRight  = l_sImgButton[2].m_iImageId;

  // copy button title
  memcpy(&l_pcTitle, f_sButton->m_pcTitle, strlen(f_sButton->m_pcTitle));

  // render left side of button
  l_iWidth   = l_sImgButton[0].m_iWidth;
  BitmapRender(l_iXPos,
               l_iYPos,
               l_iWidth + 1,
               l_iHeight,
               l_iImageIdLeft);

  // render right side of button
  l_iWidth = l_sImgButton[2].m_iWidth;
  BitmapRender(l_iXPos + l_iCnWdt + l_iWidth - 1,
               l_iYPos,
               l_iWidth,
               l_iHeight,
               l_iImageIdRight);

  // render center of button
  if (l_iCnWdt > 0)
  {
    BitmapRender(l_iXPos + l_iWidth - 1,
                 l_iYPos,
                 l_iCnWdt + 1,
                 l_iHeight,
                 l_iImageIdCenter);
  }

  l_iLen = (int)strlen(f_sButton->m_pcTitle);
  l_iXOffset = (int)(0.5 * f_sButton->m_iWidth - 0.5 * l_iLen * FONT_SPACING);
  l_iYOffset = (int)(0.5 * f_sButton->m_iHeight - 0.5 * FONT_HEIGHT);

  glPrint(f_sButton->m_iXPos + l_iXOffset,
          f_sButton->m_iYPos + l_iYOffset,
          f_sButton->m_pcTitle,
          l_sColor);

} // ButtonClickRender


void ButtonRadioRender(struct ButtonStruct *f_sButton)
{
  struct BitmapStruct *l_sImgButton;
  int l_iXOffset, l_iYOffset, l_iImageRadioId, l_iImageEnabledId;

  // copy button attributes
  int l_iWidth  = f_sButton->m_iWidth;
  int l_iHeight = f_sButton->m_iHeight;
  int l_iXPos   = f_sButton->m_iXPos;
  int l_iYPos   = f_sButton->m_iYPos;
  struct ColorStruct l_sColor = {0.2, 0.4, 0.8};

  // get the right image struct
  if (f_sButton->m_bHover)
  {
    l_sImgButton = f_sButton->m_psImgHover;
  }
  else
  {
    l_sImgButton = f_sButton->m_psImgNormal;
  }

  l_iImageRadioId   = l_sImgButton[0].m_iImageId;
  l_iImageEnabledId = f_sButton->m_psImgPressed[0].m_iImageId;

  glColor3d(1, 1, 1);

  // render the radio button
  BitmapRender(l_iXPos, l_iYPos, l_iWidth, l_iHeight, l_iImageRadioId);

  // render the enable-dot if button is pressed
  if (f_sButton->m_bPressed)
  {
    BitmapRender(l_iXPos, l_iYPos, l_iWidth, l_iHeight, l_iImageEnabledId);
  }

  l_iXOffset = (int)f_sButton->m_iWidth;
  l_iYOffset = (int)(0.5 * f_sButton->m_iHeight - 0.5 * FONT_HEIGHT);

  glPrint(f_sButton->m_iXPos + l_iXOffset,
          f_sButton->m_iYPos + l_iYOffset,
          f_sButton->m_pcTitle,
          l_sColor);

} // ButtonRadioRender


void ButtonLabelRender(struct ButtonStruct *f_sButton)
{
  struct ColorStruct l_sColor = {0, 0, 0}; //{0.2, 0.4, 0.8};

  glPrint(f_sButton->m_iXPos,
          f_sButton->m_iYPos,
          f_sButton->m_pcTitle,
          l_sColor);

} // ButtonLabelRender


void ButtonRender(struct ButtonStruct *f_sButton)
{
  int l_iType = f_sButton->m_iType;

  switch (l_iType)
  {
    case BUTTON_CLICK:
      ButtonClickRender(f_sButton);
      break;

    case BUTTON_RADIO:
      ButtonRadioRender(f_sButton);
        break;

    case BUTTON_LABEL:
      ButtonLabelRender(f_sButton);
        break;

    default:
      break;
  }

} // ButtonRender


void ButtonEnter(struct ButtonStruct *f_sButton)
{
  f_sButton->m_bHover = TRUE;

} // ButtonHover


void ButtonExit(struct ButtonStruct *f_sButton)
{
  f_sButton->m_bHover = FALSE;

} // ButtonHover


void ButtonClickPress(struct ButtonStruct *f_sButton)
{
  if (!f_sButton->m_bPressed) {
    ButtonEnable(f_sButton);
  }

} // ButtonClickPress


void ButtonRadioPress(struct ButtonStruct *f_sButton)
{
  int i = 0;

  if (!f_sButton->m_bPressed)
  {
    ButtonEnable(f_sButton);
  }

} // ButtonClickPress


void ButtonPress(struct ButtonStruct *f_sButton)
{
  int l_iType = f_sButton->m_iType;

  switch (l_iType)
  {
    case BUTTON_CLICK:
      ButtonClickPress(f_sButton);
      break;

    case BUTTON_RADIO:
      ButtonRadioPress(f_sButton);
        break;

    default:
      break;
  }

} // ButtonPress


void ButtonClickRelease(struct ButtonStruct *f_sButton)
{
  ButtonDisable(f_sButton);

} // ButtonClickRelease


void ButtonRelease(struct ButtonStruct *f_sButton)
{
  int l_iType = f_sButton->m_iType;

  switch (l_iType)
  {
    case BUTTON_CLICK:
      ButtonClickRelease(f_sButton);
      break;

    case BUTTON_RADIO:
    default:
      break;
  }

} // ButtonRelease


void ButtonEnable(struct ButtonStruct *f_sButton)
{
  f_sButton->m_bPressed = TRUE;

} // ButtonEnable


void ButtonDisable(struct ButtonStruct *f_sButton)
{
  f_sButton->m_bPressed = FALSE;

} // ButtonDisable

void ButtonExecute(struct ButtonStruct *f_sButton)
{
  if (f_sButton->m_fpFunc)
  {
    f_sButton->m_fpFunc();
  }
} // ButtonExecute