bool ListBox::Update()

in ID@XboxSDK/Social/UWP/Cpp/SampleGUI.cpp [3066:3231]


bool ListBox::Update(float elapsedTime, const DirectX::Mouse::State& mstate, const DirectX::Keyboard::State& kbstate)
{
    UNREFERENCED_PARAMETER(elapsedTime);

    auto mgr = UIManager::Impl::s_uiManager;
    if (!mgr)
    {
        throw std::exception("UIManager");
    }

	assert(m_focusItem >= 0 && m_focusItem < static_cast<int>(m_items.size()));

    // Handle mouse
    if (mstate.x >= m_itemRect.left && mstate.x < m_itemRect.right
        && mstate.y >= m_itemRect.top && mstate.y < m_itemRect.bottom)
    {
        if (m_lastHeight > 0)
        {
            int maxl = static_cast<int>(float(m_itemRect.bottom - m_itemRect.top - c_MarginSize * 2) / float(m_lastHeight));
            int item = static_cast<int>(float(mstate.y - m_itemRect.top - c_MarginSize) / float(m_lastHeight));
            if (item >= 0 && item < maxl)
            {
                item = m_topItem + item;
                if (item >= 0 && item < static_cast<int>(m_items.size()))
                {
                    m_focusItem = item;

                    if (mgr->m_mouseButtonState.leftButton == Mouse::ButtonStateTracker::PRESSED)
                    {
                        SelectItem(m_focusItem);
                    }
                }
            }
        }
        return true;
    }

    if ((m_style & c_StyleScrollBar) && (m_thumbRect.top != m_thumbRect.bottom))
    {
        if (mstate.x >= m_scrollRect.left && mstate.x < m_scrollRect.right
            && mstate.y >= m_scrollRect.top && mstate.y < m_scrollRect.bottom)
        {
            if (mgr->m_mouseButtonState.leftButton == Mouse::ButtonStateTracker::PRESSED)
            {
                if (mstate.y < m_thumbRect.top)
                {
                    if (!m_items.empty() && (m_lastHeight > 0))
                    {
                        int maxl = static_cast<int>(float(m_itemRect.bottom - m_itemRect.top - c_MarginSize * 2) / float(m_lastHeight));
                        m_focusItem -= maxl;
                        if (m_focusItem < 0)
                            m_focusItem = 0;
                    }
                }
                else if (mstate.y > m_thumbRect.bottom)
                {
                    if (!m_items.empty() && (m_lastHeight > 0))
                    {
                        int maxl = static_cast<int>(float(m_itemRect.bottom - m_itemRect.top - c_MarginSize * 2) / float(m_lastHeight));
                        m_focusItem += maxl;
                        if (m_focusItem >= static_cast<int>(m_items.size()))
                            m_focusItem = static_cast<int>(m_items.size() - 1);
                    }
                }
            }
            return true;
        }
    }

    // Handle keyboard

    if (kbstate.IsKeyDown(Keyboard::Space))
    {
        if (mgr->m_keyboardState.IsKeyPressed(Keyboard::Space))
        {
            if (!m_items.empty())
            {
                SelectItem(m_focusItem);
            }
        }
        return true;
    }
    else if (kbstate.IsKeyDown(Keyboard::W))
    {
        if (mgr->m_keyboardState.IsKeyPressed(Keyboard::W))
        {
            if (!m_items.empty())
            {
                --m_focusItem;
                if (m_focusItem < 0)
                {
                    m_focusItem = static_cast<int>(m_items.size() - 1);
                }
            }
        }
        return true;
    }
    else if (kbstate.IsKeyDown(Keyboard::S))
    {
        if (mgr->m_keyboardState.IsKeyPressed(Keyboard::S))
        {
            if (!m_items.empty())
            {
                ++m_focusItem;
                if (m_focusItem >= static_cast<int>(m_items.size()))
                {
                    m_focusItem = 0;
                }
            }
        }
        return true;
    }
    else if (kbstate.IsKeyDown(Keyboard::Home))
    {
        if (mgr->m_keyboardState.IsKeyPressed(Keyboard::Home))
        {
            m_focusItem = 0;
        }

        return true;
    }
    else if (kbstate.IsKeyDown(Keyboard::End))
    {
        if (mgr->m_keyboardState.IsKeyPressed(Keyboard::End))
        {
            if (!m_items.empty())
            {
                m_focusItem = static_cast<int>(m_items.size() - 1);
            }
        }

        return true;
    }
    else if (kbstate.IsKeyDown(Keyboard::PageUp))
    {
        if (mgr->m_keyboardState.IsKeyPressed(Keyboard::PageUp))
        {
            if (!m_items.empty() && (m_lastHeight > 0))
            {
                int maxl = static_cast<int>(float(m_itemRect.bottom - m_itemRect.top - c_MarginSize * 2) / float(m_lastHeight));
                m_focusItem -= maxl;
                if (m_focusItem < 0)
                    m_focusItem = 0;
            }
        }

        return true;
    }
    else if (kbstate.IsKeyDown(Keyboard::PageDown))
    {
        if (mgr->m_keyboardState.IsKeyPressed(Keyboard::PageDown))
        {
            if (!m_items.empty() && (m_lastHeight > 0))
            {
                int maxl = static_cast<int>(float(m_itemRect.bottom - m_itemRect.top - c_MarginSize * 2) / float(m_lastHeight));
                m_focusItem += maxl;
                if (m_focusItem >= static_cast<int>(m_items.size()))
                    m_focusItem = static_cast<int>(m_items.size() - 1);
            }
        }

        return true;
    }

    return false;
}