diff options
author | Ira Rice <irarice@gmail.com> | 2009-02-25 00:50:33 -0700 |
---|---|---|
committer | Ira Rice <irarice@gmail.com> | 2009-02-25 00:50:33 -0700 |
commit | d8bc3a6418c5027d7f6c42ce8f8dceca8dee8971 (patch) | |
tree | fbb0b461bd151cbf3ad5aeedb80257140253f57c /src/gui/listbox.cpp | |
parent | a1e483913672e55704e8fbafeff5ea0ccc0c9b07 (diff) | |
download | mana-d8bc3a6418c5027d7f6c42ce8f8dceca8dee8971.tar.gz mana-d8bc3a6418c5027d7f6c42ce8f8dceca8dee8971.tar.bz2 mana-d8bc3a6418c5027d7f6c42ce8f8dceca8dee8971.tar.xz mana-d8bc3a6418c5027d7f6c42ce8f8dceca8dee8971.zip |
Fixed up NPC list dialogs to be navigatable by keyboard (scrolling
through the list requires the use of the mouse wheel at the moment),
fixed wrapping behavior for wrapping around lists to actually wrap
around lists properly, and placed a few checks for current_npc where
they were assumed before which could cause the client to hang or crash
in case the NPC is no longer around.
Signed-off-by: Ira Rice <irarice@gmail.com>
Diffstat (limited to 'src/gui/listbox.cpp')
-rw-r--r-- | src/gui/listbox.cpp | 85 |
1 files changed, 85 insertions, 0 deletions
diff --git a/src/gui/listbox.cpp b/src/gui/listbox.cpp index f16857ce..8ae68e09 100644 --- a/src/gui/listbox.cpp +++ b/src/gui/listbox.cpp @@ -22,6 +22,7 @@ #include <guichan/font.hpp> #include <guichan/graphics.hpp> +#include <guichan/key.hpp> #include <guichan/listmodel.hpp> #include "color.h" @@ -69,6 +70,90 @@ void ListBox::draw(gcn::Graphics *graphics) } } +void ListBox::setSelected(int selected) +{ + if (!mListModel) + { + mSelected = -1; + } + else + { + if (selected < 0 && !mWrappingEnabled) + { + mSelected = -1; + } + else if (selected >= mListModel->getNumberOfElements() && + mWrappingEnabled) + { + mSelected = 0; + } + else if ((selected >= mListModel->getNumberOfElements() && + !mWrappingEnabled) || (selected < 0 && mWrappingEnabled)) + { + mSelected = mListModel->getNumberOfElements() - 1; + } + else + { + mSelected = selected; + } + } +} + +// -- KeyListener notifications +void ListBox::keyPressed(gcn::KeyEvent& keyEvent) +{ + gcn::Key key = keyEvent.getKey(); + + if (key.getValue() == gcn::Key::ENTER || key.getValue() == gcn::Key::SPACE) + { + distributeActionEvent(); + keyEvent.consume(); + } + else if (key.getValue() == gcn::Key::UP) + { + setSelected(mSelected - 1); + keyEvent.consume(); + } + else if (key.getValue() == gcn::Key::DOWN) + { + setSelected(mSelected + 1); + keyEvent.consume(); + } + else if (key.getValue() == gcn::Key::HOME) + { + setSelected(0); + keyEvent.consume(); + } + else if (key.getValue() == gcn::Key::END) + { + setSelected(getListModel()->getNumberOfElements() - 1); + keyEvent.consume(); + } +} + +void ListBox::mouseWheelMovedUp(gcn::MouseEvent& mouseEvent) +{ + if (isFocused()) + { + if (getSelected() > 0 || (getSelected() == 0 && mWrappingEnabled)) + { + setSelected(getSelected() - 1); + } + + mouseEvent.consume(); + } +} + +void ListBox::mouseWheelMovedDown(gcn::MouseEvent& mouseEvent) +{ + if (isFocused()) + { + setSelected(getSelected() + 1); + + mouseEvent.consume(); + } +} + void ListBox::mouseDragged(gcn::MouseEvent &event) { // Pretend mouse is pressed continuously while dragged. Causes list |