summaryrefslogtreecommitdiffstats
path: root/matchblox/menu/menu.c
blob: c87d369d0fa8afdcf4f6d59e809e543caff7c941 (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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
#ifdef G_OS_WIN32
  #define WIN32_LEAN_AND_MEAN 1
  #include <windows.h>
#else
  #define FALSE 0
  #define TRUE !FALSE
#endif

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

#include "message_queue.h"
#include "menu_msg.h"

#include "message_input.h"
#include "menu.h"
#include "button.h"
#include "font.h"

#define MENU_MAX_BUTTONS 16

enum MenuState {
  MENU_OFF,
  MENU_MAIN,
  MENU_START,
  MENU_OPTIONS
};

struct MenuStruct {
  enum MenuState m_iMenuId;
  int m_iButtonCount;
  struct ButtonStruct m_sButtons[MENU_MAX_BUTTONS];
};

struct MenuStruct  g_sMenuOff;     // definition of the menu when it's disabled (empty menu)
struct MenuStruct  g_sMenuMain;    // definition of the main menu
struct MenuStruct  g_sMenuStart;   // definition of the start menu
struct MenuStruct  g_sMenuOptions; // definition of the options menu
struct MenuStruct *g_pCurMenu;     // current menu

struct BitmapStruct g_sCursorImage; // bitmap struct of the cursor

int g_iWinWidth;
int g_iWinHeight;

int g_iXPos; // last cursor pos
int g_iYPos;


void MenuEnableGroup(int f_iGroup)
{
  struct ButtonStruct l_sButton;
  int l_iType, l_iGroup;
  int i = 0;

  // enable first radio button in this group
  while (i < g_pCurMenu->m_iButtonCount)
  {
    l_sButton = g_pCurMenu->m_sButtons[i];
    l_iType   = l_sButton.m_iType;
    l_iGroup  = l_sButton.m_iGroup;

    if (l_iType == BUTTON_RADIO && l_iGroup == f_iGroup)
    {
      ButtonEnable(&g_pCurMenu->m_sButtons[i]);
      return;
    }

    i++;
  }

} // MenuEnableGroup


void MenuAddButton(int f_dXPos, int f_dYPos, int f_dWidth, int f_dHeight, char *f_pcTitle, int f_iType, int f_iGroup, void (*f_fpFunc)(void))
{
  int i = g_pCurMenu->m_iButtonCount;

  if (i > MENU_MAX_BUTTONS - 1)
  {
    // return
  }
  else
  {
    // create the button with the right parameters
    g_pCurMenu->m_sButtons[i] = ButtonCreate(f_dXPos, f_dYPos, f_dWidth, f_dHeight, f_pcTitle, f_iType, f_iGroup, i, f_fpFunc);
    g_pCurMenu->m_iButtonCount = i + 1;
  }
} // MenuAddButton


void MenuAddLabel(int f_dXPos, int f_dYPos, char *f_pcTitle)
{
  MenuAddButton(f_dXPos, f_dYPos, 0, 0, f_pcTitle, BUTTON_LABEL, BUTTON_NO_GROUP, NULL);
} //MenuAddLabel


void MenuClear(void)
{
  g_pCurMenu->m_iButtonCount = 0;
  memset(&g_pCurMenu->m_sButtons, 0, sizeof(g_pCurMenu->m_sButtons));

} // MenuClear


void MenuBuild (void)
{
  int l_iMenuId = g_pCurMenu->m_iMenuId;

  switch (l_iMenuId)
  {
    default:
    case MENU_OFF:
      // no buttons
      break;

    case MENU_MAIN:
      MenuAddLabel(200, 150, "Press A+B to begin");
      break;

    case MENU_START:
      MenuAddLabel(50,  50, "Headtracking");
      MenuAddButton(150, 100, 64, 64, "On",  BUTTON_RADIO, BUTTON_GROUP1, NULL);
      MenuAddButton(350, 100, 64, 64, "Off", BUTTON_RADIO, BUTTON_GROUP1, NULL);
      MenuEnableGroup(BUTTON_GROUP1);

      MenuAddLabel(50,  250, "Stereo vision");
      MenuAddButton(150, 300, 64,  64, "On",   BUTTON_RADIO, BUTTON_GROUP2,   NULL);
      MenuAddButton(350, 300, 64,  64, "Off",  BUTTON_RADIO, BUTTON_GROUP2,   NULL);
      MenuAddButton(350, 400, 256, 64, "Next", BUTTON_CLICK, BUTTON_NO_GROUP, MenuPostMessageStart);
      MenuEnableGroup(BUTTON_GROUP2);
      break;

    case MENU_OPTIONS:
      MenuAddButton(200, 150, 250, 64, "Abort",       BUTTON_CLICK, BUTTON_NO_GROUP, MenuPostMessageAbort);
      MenuAddButton(200, 250, 250, 64, "Recalibrate", BUTTON_CLICK, BUTTON_NO_GROUP, MenuPostMessageAbort);
      MenuEnableGroup(BUTTON_GROUP1);
      break;
  }

} // MenuBuild


void MenuNext(void)
{
  int l_iMenuId = g_pCurMenu->m_iMenuId;

  l_iMenuId = (l_iMenuId + 1) % 4;

  switch (l_iMenuId)
  {
    default:
    case MENU_OFF:     g_pCurMenu = &g_sMenuOff; break;
    case MENU_MAIN:    g_pCurMenu = &g_sMenuMain; break;
    case MENU_START:   g_pCurMenu = &g_sMenuStart; break;
    case MENU_OPTIONS: g_pCurMenu = &g_sMenuOptions; break;
  }

} // MenuNext


void MenuInit(int f_iWinWidth, int f_iWinHeight)
{
  // init menu props
  g_iWinWidth  = f_iWinWidth;
  g_iWinHeight = f_iWinHeight;
  g_iXPos = -100;
  g_iYPos = -100;

  // init main off
  g_sMenuOff.m_iMenuId = MENU_OFF;
  g_pCurMenu = &g_sMenuOff;
  MenuBuild();

  // init main menu
  g_sMenuMain.m_iMenuId = MENU_MAIN;
  g_pCurMenu = &g_sMenuMain;
  MenuBuild();

  // init start menu
  g_sMenuStart.m_iMenuId = MENU_START;
  g_pCurMenu = &g_sMenuStart;
  MenuBuild();

  // init options menu
  g_sMenuOptions.m_iMenuId = MENU_OPTIONS;
  g_pCurMenu = &g_sMenuOptions;
  MenuBuild();

  // init font
  FontInit(f_iWinWidth, f_iWinHeight);

  //g_pCurMenu = &g_sMenuOff;
  g_pCurMenu = &g_sMenuMain;

  // load cursor image
  g_sCursorImage = BitmapLoad("img/cursor.bmp");

} // MenuInit


void MenuRender(void)
{
  int i = 0;

  glClearColor(0.9, 0.9, 0.9, 1);

  glPushAttrib(GL_ENABLE_BIT | GL_TEXTURE_BIT | GL_CURRENT_BIT | GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
  glDisable(GL_DEPTH_TEST);
  glEnable(GL_TEXTURE_2D);

  glMatrixMode(GL_PROJECTION);
  glPushMatrix();

    glLoadIdentity();
    glOrtho(0, g_iWinWidth, g_iWinHeight, 0, 0, 1);
    glMatrixMode(GL_MODELVIEW);

    glEnable(GL_BLEND);

    if (g_pCurMenu->m_iMenuId != MENU_OFF)
    {
      glBlendFunc(GL_SRC_COLOR, GL_ONE_MINUS_SRC_COLOR);

      glColor3f(0.6, 0.6, 0.7);
      glBegin(GL_QUADS);
        glVertex2i(0,           0);
        glVertex2i(0,           g_iWinHeight);
        glVertex2i(g_iWinWidth, g_iWinHeight);
        glVertex2i(g_iWinWidth, 0);
      glEnd();
    }

    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

    // render the buttons of the current menu
    for (i; i < g_pCurMenu->m_iButtonCount; i++)
    {
      glColor4d(1, 1, 1, 1);
      ButtonRender(&g_pCurMenu->m_sButtons[i]);
    }
    
    if (g_pCurMenu->m_iMenuId != MENU_OFF)
    {
      // render cursor
      glColor4d(1, 1, 1, 1);
      BitmapRender(g_iXPos,
                   g_iYPos,
                   64, //g_sCursorImage.m_iWidth,
                   64, //g_sCursorImage.m_iHeight,
                   g_sCursorImage.m_iImageId);
    }

    glDisable(GL_BLEND);

    //restore the previous projection matrix
    glMatrixMode(GL_PROJECTION);

  glPopMatrix();

  glPopAttrib();
} // MenuRender


void MenuProcessMessage(void)
{
  struct messageq_s *message;
  struct input_payload_wiimote *l_pMsg;
  int l_iXPos, l_iYPos;
  int l_iWidth, l_iHeight;
  int i = 0;

  message = messageq_get(MESSAGE_MENU);
  if (message)
  {
    switch(message->sender)
    {
      case MESSAGE_INPUT_KEYBOARD:
        break;
      case MESSAGE_INPUT_MOUSE:
        break;
      case MESSAGE_INPUT_WIIMOTE:
        l_pMsg = (struct input_payload_wiimote*)message->payload;

        switch (g_pCurMenu->m_iMenuId)
        {
          case MENU_MAIN:
            if (l_pMsg->btns & WIIMOTE_BUTTON_A && l_pMsg->btns & WIIMOTE_BUTTON_B)
            {
              MenuNext();
            }
            break;

          default:
            if (l_pMsg->posDataValid)
            {
              l_iWidth  = glutGet(GLUT_WINDOW_WIDTH);
              l_iHeight = glutGet(GLUT_WINDOW_HEIGHT);
              l_iXPos = l_pMsg->relX * l_iWidth;
              l_iYPos = l_iHeight - (l_pMsg->relY * l_iHeight);

              if (l_pMsg->btns & WIIMOTE_BUTTON_A)
              {
                MenuMouseClick(GLUT_LEFT_BUTTON, GLUT_DOWN, l_iXPos, l_iYPos);
              }
              else if (l_pMsg->btnsUp & WIIMOTE_BUTTON_A)
              {
                MenuMouseClick(GLUT_LEFT_BUTTON, GLUT_UP, l_iXPos, l_iYPos);
              }
              else
              {
                MenuMouseMove(l_iXPos, l_iYPos);
              }
            }
            break;
            break;
        }
        break;
    }
  }
}


int MenuCollision(struct ButtonStruct *f_sButton, int f_iXPos, int f_iYPos)
{
  int l_bCollision;
  int l_iButtonX, l_iButtonY;
  int l_iButtonWidth, l_iButtonHeight;

  l_iButtonX      = f_sButton->m_iXPos;
  l_iButtonY      = f_sButton->m_iYPos;
  l_iButtonWidth  = f_sButton->m_iWidth;
  l_iButtonHeight = f_sButton->m_iHeight;

  l_bCollision  = f_iXPos >= l_iButtonX;
  l_bCollision &= f_iYPos >= l_iButtonY;
  l_bCollision &= f_iXPos <= l_iButtonX + l_iButtonWidth;
  l_bCollision &= f_iYPos <= l_iButtonY + l_iButtonHeight;

  if (l_bCollision) return TRUE;
  return FALSE;

} // MenuCollision


void MenuMouseClick(int f_iGlutButton, int f_iGlutState, int f_iXPos, int f_iYPos)
{
  int i = 0;
  int l_iType, l_iId, l_iGroup;
  struct ButtonStruct l_sButton;
  
  // button released
  if (f_iGlutButton == GLUT_LEFT_BUTTON  && f_iGlutState == GLUT_UP)
  {
    // post a button released event to all buttons
    for (i; i < g_pCurMenu->m_iButtonCount; i++)
    {
      l_iType = g_pCurMenu->m_sButtons[i].m_iType;
      ButtonRelease(&g_pCurMenu->m_sButtons[i]);

      if (MenuCollision(&g_pCurMenu->m_sButtons[i], f_iXPos, f_iYPos))
      {
        ButtonExecute(&g_pCurMenu->m_sButtons[i]);
      }
    }
  }

  // check if any button needs attention from the mouse
  i = g_pCurMenu->m_iButtonCount;
  for (i; i >= 0; i--)
  {
    l_sButton = g_pCurMenu->m_sButtons[i];
    l_iType   = l_sButton.m_iType;
    l_iId     = l_sButton.m_iId;
    l_iGroup  = l_sButton.m_iGroup;

    if (MenuCollision(&l_sButton, f_iXPos, f_iYPos))
    {
      // button pressed?
      if (f_iGlutButton == GLUT_LEFT_BUTTON && f_iGlutState == GLUT_DOWN)
      {
        ButtonPress(&g_pCurMenu->m_sButtons[i]);

        if (l_iType == BUTTON_RADIO)
        {
          i = 0;
          while (i < g_pCurMenu->m_iButtonCount)
          {
            if (l_iId    != g_pCurMenu->m_sButtons[i].m_iId   &&
                l_iType  == g_pCurMenu->m_sButtons[i].m_iType &&
                l_iGroup == g_pCurMenu->m_sButtons[i].m_iGroup)
            {
              ButtonDisable(&g_pCurMenu->m_sButtons[i]);
            }
            i++;
          }
        }

        return;
      }
    }
  }

} // MenuMouseClick


void MenuMouseMove(int f_iXPos, int f_iYPos)
{
  int i = 0;
  struct ButtonStruct l_sButton;

  g_iXPos = f_iXPos;
  g_iYPos = f_iYPos;

  // check if any button needs attention from the mouse
  for (i; i < g_pCurMenu->m_iButtonCount; i++)
  {
    l_sButton = g_pCurMenu->m_sButtons[i];

    if (MenuCollision(&l_sButton, f_iXPos, f_iYPos))
    {
      ButtonEnter(&g_pCurMenu->m_sButtons[i]);
    }
    else // mouse leaves button
    {
      ButtonExit(&g_pCurMenu->m_sButtons[i]);
    }
  }

} // MenuMouseMove

void MenuOff(void)
{
  g_pCurMenu = &g_sMenuOff;

} // MenuOff