void Menu::onMouseButton(vec2 point, MouseButton button, Action action, uint mods) { if (action == RELEASED) { const vec2 local = transformToLocal(point); float itemTop = height() - 2.f; uint index = 0; for (auto i : m_items) { const float itemHeight = i->height(); if (itemTop - itemHeight < 0.f) break; if (itemTop - itemHeight <= local.y) { m_itemSelectedSignal(*this, index); hide(); break; } itemTop -= itemHeight; index++; } } Widget::onMouseButton(point, button, action, mods); }
void Menu::onKey(Key key, Action action, uint mods) { switch (key) { case KEY_UP: { if (action == PRESSED || action == REPEATED) { if (m_selection == NO_ITEM) { if (not m_items.empty()) m_selection = 0; } else m_selection = (m_selection + m_items.size() - 1) % m_items.size(); invalidate(); } break; } case KEY_DOWN: { if (action == PRESSED || action == REPEATED) { if (m_selection == NO_ITEM) { if (!m_items.empty()) m_selection = int(m_items.size() - 1); } else m_selection = (m_selection + 1) % m_items.size(); invalidate(); } break; } case KEY_ENTER: { if (action == PRESSED) { m_itemSelectedSignal(*this, m_selection); hide(); } break; } default: break; } Widget::onKey(key, action, mods); }
void List::setSelection(uint newSelection, bool notify) { if (m_selection == newSelection) return; m_selection = newSelection; if (isSelectionVisible()) invalidate(); else setOffset(m_selection); if (notify) m_itemSelectedSignal(*this); }
void Popup::onItemSelected(Menu& menu, uint index) { m_selection = index; m_itemSelectedSignal(*this, m_selection); activate(); }