X-Git-Url: http://shamusworld.gotdns.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=src%2Fgui%2Fmenu.cpp;fp=src%2Fgui%2Fmenu.cpp;h=f0339775a5cdfc3e4d13a13b197c81015d49a03a;hb=f9098d0570ae6462781e8189518085cb1c8c00ef;hp=0000000000000000000000000000000000000000;hpb=836c7fa1f3e2dc3ec9849cac2584d4544bf2fba4;p=virtualjaguar diff --git a/src/gui/menu.cpp b/src/gui/menu.cpp new file mode 100644 index 0000000..f033977 --- /dev/null +++ b/src/gui/menu.cpp @@ -0,0 +1,177 @@ +// +// Menu class & supporting structs/classes +// +// by James L. Hammons +// + +#include "menu.h" + +// Local variables +static const char separator[] = "--------------------------------------------------------"; + +// Implementation + +Menu::Menu(uint32 x/*= 0*/, uint32 y/*= 0*/, uint32 w/*= 0*/, uint32 h/*= FONT_HEIGHT*/, + uint32 fgc/*= 0xFF7F0000*/, uint32 bgc/*= 0xFFFF3F3F*/, uint32 fgch/*= 0xFFFF3F3F*/, + uint32 bgch/*= 0xFFFF8787*/): + Element(x, y, w, h), activated(false), clicked(false), inside(0), insidePopup(0), + fgColor(fgc), bgColor(bgc), fgColorHL(fgch), bgColorHL(bgch), menuChosen(-1), + menuItemChosen(-1) +{ +} + +void Menu::HandleKey(SDLKey key) +{ + for(uint32 i=0; i= xpos && x < xpos + width) + { + inside = i + 1; + menuChosen = i; + break; + } + + xpos += width; + } + } + + if (!Inside(x, y) && !clicked) + { + menuChosen = -1; + } + + if (itemList[menuChosen].Inside(x, y) && clicked) + { + insidePopup = ((y - itemList[menuChosen].extents.y) / FONT_HEIGHT) + 1; + menuItemChosen = insidePopup - 1; + } +} + +void Menu::HandleMouseButton(uint32 x, uint32 y, bool mouseDown) +{ + if (!clicked) + { + if (mouseDown) + { + if (inside) + clicked = true; + else + menuChosen = -1; // clicked is already false...! + } + } + else // clicked == true + { + if (insidePopup && !mouseDown) // I.e., mouse-button-up + { + activated = true; + if (itemList[menuChosen].item[menuItemChosen].action != NULL) + { +// itemList[menuChosen].item[menuItemChosen].action(); + SDL_Event event; + event.type = SDL_USEREVENT; + event.user.code = MENU_ITEM_CHOSEN; + event.user.data1 = (void *)itemList[menuChosen].item[menuItemChosen].action; + SDL_PushEvent(&event); + + clicked = false, menuChosen = menuItemChosen = -1; + +/* SDL_Event event; + while (SDL_PollEvent(&event)); // Flush the event queue... + event.type = SDL_MOUSEMOTION; + int mx, my; + SDL_GetMouseState(&mx, &my); + event.motion.x = mx, event.motion.y = my; + SDL_PushEvent(&event); // & update mouse position...! +*/ } + } + + if (!inside && !insidePopup && mouseDown) + clicked = false, menuChosen = menuItemChosen = -1; + } +} + +void Menu::Draw(uint32 offsetX/*= 0*/, uint32 offsetY/*= 0*/) +{ + uint32 xpos = extents.x + offsetX; + + for(uint32 i=0; i 0) + DrawStringOpaque(screenBuffer, itemList[menuChosen].extents.x, ypos, + color1, color2, " %-*.*s ", itemList[menuChosen].charLength, + itemList[menuChosen].charLength, itemList[menuChosen].item[i].name.c_str()); + else + DrawStringOpaque(screenBuffer, itemList[menuChosen].extents.x, ypos, + fgColor, bgColor, "%.*s", itemList[menuChosen].charLength + 2, separator); + + ypos += FONT_HEIGHT; + } + } +} + +void Menu::Add(MenuItems mi) +{ + for(uint32 i=0; i mi.charLength) + mi.charLength = mi.item[i].name.length(); + + // Set extents here as well... + mi.extents.x = extents.x + extents.w, mi.extents.y = extents.y + FONT_HEIGHT + 1; + mi.extents.w = (mi.charLength + 2) * FONT_WIDTH, mi.extents.h = mi.item.size() * FONT_HEIGHT; + + itemList.push_back(mi); + extents.w += (mi.title.length() + 2) * FONT_WIDTH; +}