summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndrei Karas <akaras@inbox.ru>2012-04-22 17:48:18 +0300
committerAndrei Karas <akaras@inbox.ru>2012-04-22 17:53:56 +0300
commitd32d2c9b6555201aeda94e657357803f2f9a9a70 (patch)
tree744d19e9ad4108a6e51b3651ea9644e2b792141e
parent49ab517a6c51a14ab7ae72143f4364b1e1416a2f (diff)
downloadplus-d32d2c9b6555201aeda94e657357803f2f9a9a70.tar.gz
plus-d32d2c9b6555201aeda94e657357803f2f9a9a70.tar.bz2
plus-d32d2c9b6555201aeda94e657357803f2f9a9a70.tar.xz
plus-d32d2c9b6555201aeda94e657357803f2f9a9a70.zip
Improve keyboard handling in character selection dialog.
-rw-r--r--src/gui/charselectdialog.cpp156
-rw-r--r--src/gui/charselectdialog.h7
2 files changed, 157 insertions, 6 deletions
diff --git a/src/gui/charselectdialog.cpp b/src/gui/charselectdialog.cpp
index a148376fb..f924cd259 100644
--- a/src/gui/charselectdialog.cpp
+++ b/src/gui/charselectdialog.cpp
@@ -59,6 +59,7 @@
#include "utils/gettext.h"
#include "utils/stringutils.h"
+#include <guichan/focushandler.hpp>
#include <guichan/font.hpp>
#include <string>
@@ -110,6 +111,18 @@ class CharacterDisplay : public Container
void setActive(bool active);
+ bool isSelectFocused()
+ { return mButton->isFocused(); }
+
+ bool isDeleteFocused()
+ { return mDelete->isFocused(); }
+
+ void focusSelect()
+ { mFocusHandler->requestFocus(mButton); }
+
+ void focusDelete()
+ { mFocusHandler->requestFocus(mDelete); }
+
private:
void update();
@@ -263,11 +276,144 @@ void CharSelectDialog::action(const gcn::ActionEvent &event)
void CharSelectDialog::keyPressed(gcn::KeyEvent &keyEvent)
{
- if (static_cast<KeyEvent*>(&keyEvent)->getActionId()
- == Input::KEY_GUI_CANCEL)
+ int actionId = static_cast<KeyEvent*>(&keyEvent)->getActionId();
+ switch (actionId)
{
- action(gcn::ActionEvent(mSwitchLoginButton,
- mSwitchLoginButton->getActionEventId()));
+ case Input::KEY_GUI_CANCEL:
+ keyEvent.consume();
+ action(gcn::ActionEvent(mSwitchLoginButton,
+ mSwitchLoginButton->getActionEventId()));
+ break;
+
+ case Input::KEY_GUI_RIGHT:
+ {
+ keyEvent.consume();
+ int idx;
+ int idx2;
+ if (getFocusedContainer(idx, idx2))
+ {
+ idx ++;
+ if (idx == SLOTS_PER_ROW)
+ break;
+ setFocusedContainer(idx, idx2);
+ }
+ break;
+ }
+
+ case Input::KEY_GUI_LEFT:
+ {
+ keyEvent.consume();
+ int idx;
+ int idx2;
+ if (getFocusedContainer(idx, idx2))
+ {
+ if (!idx || idx == SLOTS_PER_ROW)
+ break;
+ idx --;
+ setFocusedContainer(idx, idx2);
+ }
+ break;
+ }
+
+ case Input::KEY_GUI_UP:
+ {
+ keyEvent.consume();
+ int idx;
+ int idx2;
+ if (getFocusedContainer(idx, idx2))
+ {
+ if (idx < SLOTS_PER_ROW && !idx2)
+ break;
+ if (idx2 > 0)
+ {
+ idx2 = 0;
+ }
+ else
+ {
+ idx -= SLOTS_PER_ROW;
+ if (mCharacterEntries[idx]->getCharacter())
+ idx2 = 1;
+ else
+ idx2 = 0;
+ }
+ setFocusedContainer(idx, idx2);
+ }
+ break;
+ }
+
+ case Input::KEY_GUI_DOWN:
+ {
+ keyEvent.consume();
+ int idx;
+ int idx2;
+ if (getFocusedContainer(idx, idx2))
+ {
+ if (idx >= SLOTS_PER_ROW && idx2)
+ break;
+ if (idx2 > 0)
+ {
+ idx += SLOTS_PER_ROW;
+ idx2 = 0;
+ }
+ else
+ {
+ if (mCharacterEntries[idx]->getCharacter())
+ idx2 = 1;
+ else
+ idx += SLOTS_PER_ROW;
+ }
+ setFocusedContainer(idx, idx2);
+ }
+ break;
+ }
+
+ case Input::KEY_GUI_DELETE:
+ {
+ keyEvent.consume();
+ int idx;
+ int idx2;
+ if (getFocusedContainer(idx, idx2)
+ && mCharacterEntries[idx]->getCharacter())
+ {
+ new CharDeleteConfirm(this, idx);
+ }
+ break;
+ }
+
+ default:
+ break;
+ }
+}
+
+bool CharSelectDialog::getFocusedContainer(int &container, int &idx)
+{
+ for (int f = 0; f < static_cast<int>(mLoginData->characterSlots); f ++)
+ {
+ if (mCharacterEntries[f]->isSelectFocused())
+ {
+ container = f;
+ idx = 0;
+ return true;
+ }
+ else if (mCharacterEntries[f]->isDeleteFocused())
+ {
+ container = f;
+ idx = 1;
+ return true;
+ }
+ }
+ return false;
+}
+
+void CharSelectDialog::setFocusedContainer(int i, int button)
+{
+ if (i >= 0 && i < static_cast<int>(mLoginData->characterSlots))
+ {
+ CharacterDisplay *container = mCharacterEntries[i];
+ if (button)
+ container->focusDelete();
+ else
+ container->focusSelect();
}
}
@@ -387,7 +533,7 @@ bool CharSelectDialog::selectByName(const std::string &name,
Net::Character *character = mCharacterEntries[i]->getCharacter();
if (mCharacterEntries[i] && character)
{
- if ( character->dummy && character->dummy->getName() == name)
+ if (character->dummy && character->dummy->getName() == name)
{
if (mCharacterEntries[i])
mCharacterEntries[i]->requestFocus();
diff --git a/src/gui/charselectdialog.h b/src/gui/charselectdialog.h
index 1e32fb311..fe65f3ce1 100644
--- a/src/gui/charselectdialog.h
+++ b/src/gui/charselectdialog.h
@@ -50,7 +50,8 @@ namespace Net
*
* \ingroup Interface
*/
-class CharSelectDialog : public Window, public gcn::ActionListener,
+class CharSelectDialog : public Window,
+ public gcn::ActionListener,
public gcn::KeyListener
{
public:
@@ -97,6 +98,10 @@ class CharSelectDialog : public Window, public gcn::ActionListener,
void unlock();
void setLocked(bool locked);
+ bool getFocusedContainer(int &container, int &idx);
+
+ void setFocusedContainer(int i, int button);
+
bool mLocked;
gcn::Label *mAccountNameLabel;