Browse Source

Keep MenuItem active when its child menu is open

tags/v0.4.0
Andrew Belt 7 years ago
parent
commit
e8eddc4463
2 changed files with 18 additions and 12 deletions
  1. +4
    -3
      include/widgets.hpp
  2. +14
    -9
      src/widgets/MenuItem.cpp

+ 4
- 3
include/widgets.hpp View File

@@ -273,9 +273,13 @@ struct MenuOverlay : OpaqueWidget {
Widget *onHoverKey(Vec pos, int key); Widget *onHoverKey(Vec pos, int key);
}; };


struct MenuEntry;

struct Menu : OpaqueWidget { struct Menu : OpaqueWidget {
Menu *parentMenu = NULL; Menu *parentMenu = NULL;
Menu *childMenu = NULL; Menu *childMenu = NULL;
/** The entry which created the child menu */
MenuEntry *activeEntry = NULL;


Menu() { Menu() {
box.size = Vec(0, 0); box.size = Vec(0, 0);
@@ -304,14 +308,11 @@ struct MenuLabel : MenuEntry {
}; };


struct MenuItem : MenuEntry { struct MenuItem : MenuEntry {
BNDwidgetState state = BND_DEFAULT;

float computeMinWidth(NVGcontext *vg); float computeMinWidth(NVGcontext *vg);
void draw(NVGcontext *vg); void draw(NVGcontext *vg);


virtual Menu *createChildMenu() {return NULL;} virtual Menu *createChildMenu() {return NULL;}
void onMouseEnter(); void onMouseEnter();
void onMouseLeave();
void onDragDrop(Widget *origin); void onDragDrop(Widget *origin);
}; };




+ 14
- 9
src/widgets/MenuItem.cpp View File

@@ -12,31 +12,36 @@ float MenuItem::computeMinWidth(NVGcontext *vg) {
} }


void MenuItem::draw(NVGcontext *vg) { void MenuItem::draw(NVGcontext *vg) {
// Get state
BNDwidgetState state = (gHoveredWidget == this) ? BND_HOVER : BND_DEFAULT;
Menu *parentMenu = dynamic_cast<Menu*>(parent);
if (parentMenu && parentMenu->activeEntry == this) {
state = BND_ACTIVE;
}

bndMenuItem(vg, 0.0, 0.0, box.size.x, box.size.y, state, -1, text.c_str()); bndMenuItem(vg, 0.0, 0.0, box.size.x, box.size.y, state, -1, text.c_str());


float x = box.size.x - bndLabelWidth(vg, -1, rightText.c_str()); float x = box.size.x - bndLabelWidth(vg, -1, rightText.c_str());
NVGcolor rightColor = (state == BND_DEFAULT) ? bndGetTheme()->menuTheme.textColor : bndGetTheme()->menuTheme.textSelectedColor; NVGcolor rightColor = (state == BND_DEFAULT) ? bndGetTheme()->menuTheme.textColor : bndGetTheme()->menuTheme.textSelectedColor;
bndIconLabelValue(vg, x, 0.0, box.size.x, box.size.y, -1,
rightColor, BND_LEFT, BND_LABEL_FONT_SIZE, rightText.c_str(), NULL);
bndIconLabelValue(vg, x, 0.0, box.size.x, box.size.y, -1, rightColor, BND_LEFT, BND_LABEL_FONT_SIZE, rightText.c_str(), NULL);
} }


void MenuItem::onMouseEnter() { void MenuItem::onMouseEnter() {
state = BND_HOVER;
Menu *parentMenu = dynamic_cast<Menu*>(parent);
if (!parentMenu)
return;

parentMenu->activeEntry = NULL;


// Try to create child menu // Try to create child menu
Menu *childMenu = createChildMenu(); Menu *childMenu = createChildMenu();
if (childMenu) { if (childMenu) {
childMenu->box.pos = parent->box.pos.plus(box.getTopRight()); childMenu->box.pos = parent->box.pos.plus(box.getTopRight());
Menu *parentMenu = dynamic_cast<Menu*>(parent);
assert(parentMenu);
parentMenu->setChildMenu(childMenu); parentMenu->setChildMenu(childMenu);
parentMenu->activeEntry = this;
} }
} }


void MenuItem::onMouseLeave() {
state = BND_DEFAULT;
}

void MenuItem::onDragDrop(Widget *origin) { void MenuItem::onDragDrop(Widget *origin) {
if (origin != this) if (origin != this)
return; return;


Loading…
Cancel
Save