summaryrefslogtreecommitdiffstats
path: root/menu_demo/menu.c
diff options
context:
space:
mode:
Diffstat (limited to 'menu_demo/menu.c')
-rw-r--r--menu_demo/menu.c260
1 files changed, 196 insertions, 64 deletions
diff --git a/menu_demo/menu.c b/menu_demo/menu.c
index e6a15ee..b4d98cf 100644
--- a/menu_demo/menu.c
+++ b/menu_demo/menu.c
@@ -20,7 +20,7 @@ enum MenuState {
};
struct MenuStruct {
- enum MenuState m_eMenuId;
+ enum MenuState m_iMenuId;
int m_iButtonCount;
struct ButtonStruct m_sButtons[MENU_MAX_BUTTONS];
};
@@ -34,54 +34,146 @@ int g_iWinWidth;
int g_iWinHeight;
+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)
+void MenuAddButton(int f_dXPos, int f_dYPos, int f_dWidth, int f_dHeight, char *f_pcTitle, int f_iType, int f_iGroup)
{
- int i;
+ int i = g_pCurMenu->m_iButtonCount;;
- i = g_sMenuMain.m_iButtonCount;
- g_sMenuMain.m_sButtons[i] = ButtonCreate(f_dXPos, f_dYPos, f_dWidth, f_dHeight, f_pcTitle, f_iType);
-
- i++;
- g_sMenuMain.m_iButtonCount = i;
+ // 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);
+ g_pCurMenu->m_iButtonCount = i + 1;
} // MenuAddButton
+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_MAIN:
+ MenuAddButton(50, 50, 250, 64, "Button 1", BUTTON_CLICK, BUTTON_NO_GROUP);
+ MenuAddButton(50, 150, 250, 64, "Button 2", BUTTON_CLICK, BUTTON_NO_GROUP);
+ MenuAddButton(350, 50, 64, 64, "Radio 1", BUTTON_RADIO, BUTTON_GROUP1);
+ MenuAddButton(350, 150, 64, 64, "Radio 2", BUTTON_RADIO, BUTTON_GROUP1);
+ MenuEnableGroup(BUTTON_GROUP1);
+
+ MenuAddButton(50, 250, 256, 64, "Button 3", BUTTON_CLICK, BUTTON_NO_GROUP);
+ MenuAddButton(50, 350, 256, 64, "Button 4", BUTTON_CLICK, BUTTON_NO_GROUP);
+ MenuAddButton(350, 250, 64, 64, "Radio A", BUTTON_RADIO, BUTTON_GROUP2);
+ MenuAddButton(350, 350, 64, 64, "Radio B", BUTTON_RADIO, BUTTON_GROUP2);
+ MenuEnableGroup(BUTTON_GROUP2);
+ break;
+
+ case MENU_START:
+ MenuAddButton(50, 150, 250, 64, "Start", BUTTON_CLICK, BUTTON_NO_GROUP);
+ MenuAddButton(50, 250, 250, 64, "Back", BUTTON_CLICK, BUTTON_NO_GROUP);
+ MenuAddButton(350, 150, 64, 64, "Try first!", BUTTON_RADIO, BUTTON_GROUP1);
+ MenuAddButton(350, 250, 64, 64, "10 games", BUTTON_RADIO, BUTTON_GROUP1);
+ MenuEnableGroup(BUTTON_GROUP1);
+ break;
+
+ case MENU_OPTIONS:
+ MenuAddButton(50, 50, 64, 64, "Headtracking", BUTTON_RADIO, BUTTON_GROUP1);
+ MenuAddButton(50, 150, 64, 64, "Stereo vision", BUTTON_RADIO, BUTTON_GROUP1);
+ MenuEnableGroup(BUTTON_GROUP1);
+ break;
+ }
+
+} // MenuBuild
+
+
+
+void MenuRebuild(void)
+{
+ MenuClear();
+ MenuBuild();
+
+} // MenuRebuild
+
+
+
+void MenuNext(void)
+{
+ int l_iMenuId = g_pCurMenu->m_iMenuId;
+
+ l_iMenuId = (l_iMenuId + 1) % 3;
+ g_pCurMenu->m_iMenuId = l_iMenuId;
+ MenuRebuild();
+
+} // MenuNext
+
+
+
void MenuInit(int f_iWinWidth, int f_iWinHeight)
{
// init menu props
g_iWinWidth = f_iWinWidth;
g_iWinHeight = f_iWinHeight;
+ // set pointer to the current menu
+ g_pCurMenu = &g_sMenuStart;
+
// init main menu
- g_sMenuMain.m_eMenuId = MENU_MAIN;
- MenuAddButton(32, 16, 256, 128, "Button 1", BUTTON_CLICK);
- MenuAddButton(32, 128, 256, 128, "Button 2", BUTTON_CLICK);
- MenuAddButton(312, 16, 110, 110, "Radio 1", BUTTON_RADIO);
+ g_sMenuMain.m_iMenuId = MENU_MAIN;
// init start menu
- g_sMenuStart.m_eMenuId = MENU_START;
- g_sMenuStart.m_iButtonCount = 0;
+ g_sMenuStart.m_iMenuId = MENU_START;
// init options menu
- g_sMenuOptions.m_eMenuId = MENU_OPTIONS;
- g_sMenuOptions.m_iButtonCount = 0;
-
- // set pointer to the current menu
- g_pCurMenu = &g_sMenuMain;
+ g_sMenuOptions.m_iMenuId = MENU_OPTIONS;
// init font
FontInit(f_iWinWidth, f_iWinHeight);
+ // build the current menu (main)
+ MenuBuild();
+
} // MenuInit
void MenuRender(void)
{
- int i;
+ int i = 0;
+
+ glClearColor(0.9, 0.9, 0.9, 1);
glDisable(GL_DEPTH_TEST);
glEnable(GL_TEXTURE_2D);
@@ -96,24 +188,12 @@ void MenuRender(void)
glMatrixMode(GL_MODELVIEW);
glEnable(GL_BLEND);
- //glBlendFunc(GL_ONE, GL_ZERO);
-
- //glColor4d(0.25, 0.5, 1, 0.1);
- //glBegin(GL_TRIANGLE_STRIP);
- // glVertex2i(0, 0);
- // glVertex2i(g_iWinWidth, 0);
- // glVertex2i(0, g_iWinHeight);
- // glVertex2i(g_iWinWidth, g_iWinHeight);
- //glEnd();
-
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
// render the buttons of the current menu
- i = 0;
- while (i < g_pCurMenu->m_iButtonCount)
+ for (i; i < g_pCurMenu->m_iButtonCount; i++)
{
ButtonRender(&g_pCurMenu->m_sButtons[i]);
- i++;
}
glDisable(GL_BLEND);
@@ -127,61 +207,113 @@ void MenuRender(void)
-void MenuMouseHandle(int f_iGlutButton, int f_iGlutState, int f_iXPos, int f_iYPos)
+int MenuCollision(struct ButtonStruct *f_sButton, int f_iXPos, int f_iYPos)
{
- int i = 0;
- double l_dXPos, l_dYPos;
- double l_iWidth, l_iHeight;
+ int l_iCollision;
+ int l_iButtonX, l_iButtonY;
+ int l_iButtonWidth, l_iButtonHeight;
+
+ l_iButtonX = f_sButton->m_dXPos;
+ l_iButtonY = f_sButton->m_dYPos;
+ l_iButtonWidth = f_sButton->m_dWidth;
+ l_iButtonHeight = f_sButton->m_dHeight;
- // f_iGlutButton:
- // # GLUT_LEFT_BUTTON
- // # GLUT_MIDDLE_BUTTON
- // # GLUT_RIGHT_BUTTON
+ l_iCollision = f_iXPos >= l_iButtonX;
+ l_iCollision &= f_iYPos >= l_iButtonY;
+ l_iCollision &= f_iXPos <= l_iButtonX + l_iButtonWidth;
+ l_iCollision &= f_iYPos <= l_iButtonY + l_iButtonHeight;
- // f_iGlutState
- // # GLUT_DOWN
- // # GLUT_UP
+
+ if (l_iCollision) return TRUE;
+ return FALSE;
+
+} // MenuCollision
+
+
+
+void MenuMouseClick(int f_iGlutButton, int f_iGlutState, int f_iXPos, int f_iYPos)
+{
+ int i = 0;
+ int l_iXPos, l_iYPos;
+ int l_iWidth, l_iHeight;
+ int l_iType, l_iId, l_iGroup;
+ struct ButtonStruct l_sButton;
// button released
if (f_iGlutButton == GLUT_LEFT_BUTTON && f_iGlutState == GLUT_UP)
{
- i = 0;
- while (i < g_pCurMenu->m_iButtonCount)
+ // post a button released event to all buttons
+ for (i; i < g_pCurMenu->m_iButtonCount; i++)
{
- if (ButtonGetType(&g_pCurMenu->m_sButtons[i]) == BUTTON_CLICK)
- {
- ButtonRelease(&g_pCurMenu->m_sButtons[i]);
- }
- i++;
+ l_iType = g_pCurMenu->m_sButtons[i].m_iType;
+ ButtonRelease(&g_pCurMenu->m_sButtons[i]);
}
}
// check if any button needs attention from the mouse
- i = 0;
- while (i < g_pCurMenu->m_iButtonCount)
+ i = g_pCurMenu->m_iButtonCount;
+ for (i; i >= 0; i--)
{
- l_dXPos = g_pCurMenu->m_sButtons[i].m_dXPos;
- l_dYPos = g_pCurMenu->m_sButtons[i].m_dYPos;
- l_iWidth = g_pCurMenu->m_sButtons[i].m_dWidth;
- l_iHeight = g_pCurMenu->m_sButtons[i].m_dHeight;
-
- if (l_dXPos <= f_iXPos && l_dXPos + l_iWidth >= f_iXPos &&l_dYPos <= f_iYPos && l_dYPos + l_iHeight >= f_iYPos)
+ l_sButton = g_pCurMenu->m_sButtons[i];
+ l_iXPos = l_sButton.m_dXPos;
+ l_iYPos = l_sButton.m_dYPos;
+ l_iWidth = l_sButton.m_dWidth;
+ l_iHeight = l_sButton.m_dHeight;
+ 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))
{
- ButtonEnter(&g_pCurMenu->m_sButtons[i]);
-
// button pressed?
- if (f_iGlutButton == GLUT_LEFT_BUTTON && f_iGlutState == GLUT_DOWN)
+ 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;
+
+ // 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]);
}
- i++;
}
-} // MenuMouseHandle
+} // MenuMouseMove