/** * @brief * Click on a menu item */ void AbstractMenu::ClickItem(int nItem) { // Get currently selected item MenuItem *pItem = m_pMenu->GetItems().Get(nItem); bool bCloseOnly = (m_pSelectedItem != nullptr && m_pSelectedItem == pItem); // Close currently open item if (m_pSelectedItem) { // Close submenu if (m_pSelectedItem->GetType() == TypeMenu) { CloseSubmenu(); } // Close submenu m_pSelectedItem = nullptr; } // Check clicked item if (pItem) { // Open new submenu? if (pItem->GetType() == TypeMenu && !bCloseOnly) { // Set open item m_pSelectedItem = pItem; // Open submenu OpenSubmenu(m_pSelectedItem, nItem); } else if (pItem->GetType() == TypeMenuItem) { // Item clicked m_pController->OnItemClicked(pItem); m_pController->SetSelection(-1); } } // Reset selection? if (nItem == -1) { SetSelection(-1); } // Redraw menu Redraw(); }
/** * @brief * Set currently selected menu item */ void AbstractMenu::SetSelection(int nItem) { // Has the selection changed? int nNumOfItems = (m_pMenu ? m_pMenu->GetItems().GetNumOfElements() : 0); if (m_nSelection != nItem && nItem >= -1 && nItem < nNumOfItems) { // Get currently selected item MenuItem *pItem = m_pMenu->GetItems().Get(nItem); bool bOpenSubmenu = (m_pSelectedItem != nullptr); // Close currently open item if (m_pSelectedItem) { // Close submenu if (m_pSelectedItem->GetType() == TypeMenu) { CloseSubmenu(); } // Close submenu m_pSelectedItem = nullptr; } // Set selection m_nSelection = nItem; // Emit signal SignalItemSelected(m_nSelection); // Check selected item if (pItem && bOpenSubmenu) { // Set new open submenu (or item) m_pSelectedItem = pItem; // Open new submenu? if (pItem->GetType() == TypeMenu) { // Open submenu OpenSubmenu(m_pSelectedItem, nItem); } } // Redraw menu Redraw(); } }
void AbstractMenu::OnKeyDown(uint32 nKey, uint32 nModifiers) { // Check pointer if (m_pControlMenu) { // Previous item if ( m_pControlMenu->GetSelection() > -1 && ((m_pControlMenu->GetOrientation() == Horizontal && nKey == PLGUIKEY_LEFT) || (m_pControlMenu->GetOrientation() == Vertical && nKey == PLGUIKEY_UP)) ) { // Select previous item int nSelection = m_pControlMenu->GetSelection() - 1; if (nSelection < 0) nSelection = m_pControlMenu->m_nItems-1; // Ignore separators MenuItem *pItem = (m_pMenu ? m_pMenu->GetItems().Get(nSelection) : nullptr); if (pItem && pItem->GetType() == TypeSeparator) { nSelection--; if (nSelection < 0) nSelection = m_pControlMenu->m_nItems-1; } // Update selection m_pControlMenu->SetSelection(nSelection); m_pControlMenu->m_nIgnoreMouse = 5; } // Next item if ( m_pControlMenu->GetSelection() > -1 && ((m_pControlMenu->GetOrientation() == Horizontal && nKey == PLGUIKEY_RIGHT) || (m_pControlMenu->GetOrientation() == Vertical && nKey == PLGUIKEY_DOWN)) ) { // Select next item int nSelection = m_pControlMenu->GetSelection() + 1; if (nSelection >= m_pControlMenu->m_nItems) nSelection = 0; // Ignore separators MenuItem *pItem = (m_pMenu ? m_pMenu->GetItems().Get(nSelection) : nullptr); if (pItem && pItem->GetType() == TypeSeparator) { nSelection++; if (nSelection >= m_pControlMenu->m_nItems) nSelection = 0; } // Update selection m_pControlMenu->SetSelection(nSelection); m_pControlMenu->m_nIgnoreMouse = 5; } // Select current item if ((nKey == PLGUIKEY_SPACE || nKey == PLGUIKEY_RETURN) && m_pControlMenu->GetSelection() > -1) { m_pControlMenu->ClickItem(m_pControlMenu->GetSelection()); } // Open next popup-menu if ( !m_pControlMenu->m_pSelectedItem && ((m_pControlMenu->GetOrientation() == Horizontal && nKey == PLGUIKEY_DOWN) || (m_pControlMenu->GetOrientation() == Vertical && nKey == PLGUIKEY_RIGHT)) ) { // Must be a sub-menu MenuItem *pItem = (m_pControlMenu->m_pMenu ? m_pControlMenu->m_pMenu->GetItems().Get(m_pControlMenu->GetSelection()) : nullptr); if (pItem && pItem->GetType() == TypeMenu) { m_pControlMenu->ClickItem(m_pControlMenu->GetSelection()); } } // Close current popup-menu if (m_pControlMenu->GetOrientation() == Vertical && nKey == PLGUIKEY_LEFT && m_pControlMenu->m_pParentMenu) { m_pControlMenu = m_pControlMenu->m_pParentMenu; m_pControlMenu->ClickItem(m_pControlMenu->GetSelection()); } // Keyboard shortcut if (nKey >= PLGUIKEY_A && nKey <= PLGUIKEY_Z) { // Get character char nChar = static_cast<char>('a' + (nKey - PLGUIKEY_A)); m_pControlMenu->PressShortcut(nChar); m_pControlMenu->m_nIgnoreMouse = 5; } } // Close entire menu if (nKey == PLGUIKEY_ESCAPE) { if (m_pSelectedItem) ClickItem(GetSelection()); else ClickItem(-1); } }