From fbb717acfc2bd1f2a3cedf8465c817e8a5abec57 Mon Sep 17 00:00:00 2001 From: Joshua Langley Date: Wed, 25 Jul 2007 13:06:47 +0000 Subject: keyboard config - keyboard setup gui re-designed, fixed errors in keyboard config. m_id:16 --- ChangeLog | 9 +++- src/gui/setup.cpp | 4 +- src/gui/setup_keyboard.cpp | 123 +++++++++++++++++++++++++++------------------ src/gui/setup_keyboard.h | 15 ++---- src/keyboardconfig.cpp | 9 +--- src/keyboardconfig.h | 19 +++---- src/main.cpp | 3 -- 7 files changed, 98 insertions(+), 84 deletions(-) diff --git a/ChangeLog b/ChangeLog index 0edfeebb..14f67c33 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,4 +1,11 @@ -2007-07-24 Bjørn Lindeijer +2007-07-25 Joshua Langley + + * src/gui/setup.cpp, src/gui/setup_keyboard.cpp, + src/gui/setup_keyboard.h, src/keyboardconfig.cpp, + src/keyboardconfig.h, src/main.cpp: minor changes to + keyboard config, keyboard setup gui re-designed. + +2007-07-24 Bjørn Lindeijer * src/CMakeLists.txt, src/Makefile.am: Added setup_keyboard.* and keyboardconfig.* to the list of source files. diff --git a/src/gui/setup.cpp b/src/gui/setup.cpp index b936f688..1c17c50e 100644 --- a/src/gui/setup.cpp +++ b/src/gui/setup.cpp @@ -44,7 +44,7 @@ Setup::Setup(): Window("Setup") { int width = 260; - int height = 305; + int height = 245; setContentSize(width, height); const char *buttonNames[] = { @@ -59,7 +59,7 @@ Setup::Setup(): } TabbedContainer *panel = new TabbedContainer(); - panel->setDimension(gcn::Rectangle(5, 5, 250, 265)); + panel->setDimension(gcn::Rectangle(5, 5, 250, 205)); panel->setOpaque(false); SetupTab *tab; diff --git a/src/gui/setup_keyboard.cpp b/src/gui/setup_keyboard.cpp index 6a89ce8f..2929cab6 100644 --- a/src/gui/setup_keyboard.cpp +++ b/src/gui/setup_keyboard.cpp @@ -24,9 +24,12 @@ #include "setup_keyboard.h" #include +#include #include "button.h" +#include "listbox.h" #include "ok_dialog.h" +#include "scrollarea.h" #include "../configuration.h" #include "../keyboardconfig.h" @@ -35,38 +38,71 @@ #include -Setup_Keyboard::Setup_Keyboard() +/** + * The list model for key function list. + * + * \ingroup Interface + */ +class KeyListModel : public gcn::ListModel { - setOpaque(false); + public: + /** + * Returns the number of elements in container. + */ + int getNumberOfElements() { return keyboard.KEY_TOTAL; } + + /** + * Returns element from container. + */ + std::string getElementAt(int i) { return mKeyFunctions[i]; } + + /** + * Sets element from container. + */ + void setElementAt(int i, std::string caption) + { + mKeyFunctions[i] = caption; + } + + private: + std::string mKeyFunctions[keyboard.KEY_TOTAL]; +}; +Setup_Keyboard::Setup_Keyboard(): + mKeyListModel(new KeyListModel()), + mKeyList(new ListBox(mKeyListModel)) +{ keyboard.setSetupKeyboard(this); + setOpaque(false); - mKeyLabel = new gcn::Label[keyboard.KEY_TOTAL]; - mKeyButton = new Button[keyboard.KEY_TOTAL]; + refreshKeys(); + + mKeyList->setDimension(gcn::Rectangle(0, 0, 180, 140)); + mKeyList->addActionListener(this); + mKeyList->setSelected(-1); + + ScrollArea *scrollArea = new ScrollArea(mKeyList); + scrollArea->setDimension(gcn::Rectangle(10, 10, 180, 140)); + add(scrollArea); + + mAssignKeyButton = new Button("Assign", "assign", this); + mAssignKeyButton->setPosition(145, 155); + mAssignKeyButton->addActionListener(this); + mAssignKeyButton->setEnabled(false); + add(mAssignKeyButton); - for (int i = 0; i < keyboard.KEY_TOTAL; i++) - { - refreshAssignedKey(i); - mKeyLabel[i].setPosition(10, 10+(i*20)); - add(&mKeyLabel[i]); - - mKeyButton[i].setCaption("Set"); - mKeyButton[i].adjustSize(); - mKeyButton[i].addActionListener(this); - mKeyButton[i].setActionEventId("sk"+toString(i)); - mKeyButton[i].setPosition(150,5+(i*20)); - add(&mKeyButton[i]); - } mMakeDefaultButton = new Button("Default", "makeDefault", this); - mMakeDefaultButton->setPosition(200, 5); + mMakeDefaultButton->setPosition(10, 155); mMakeDefaultButton->addActionListener(this); add(mMakeDefaultButton); } Setup_Keyboard::~Setup_Keyboard() { - delete [] mKeyLabel; - delete [] mKeyButton; + delete mKeyList; + delete mKeyListModel; + + delete mAssignKeyButton; delete mMakeDefaultButton; } @@ -75,9 +111,7 @@ void Setup_Keyboard::apply() if (keyboard.hasConflicts()) { new OkDialog("Key Conflict(s) Detected.", - "One or more key conflicts has been detected. " - "Resolve them immediately, " - "or gameplay might result in unpredictable behaviour"); + "Resolve them, or gameplay may result in strange behaviour."); } keyboard.setEnabled(true); keyboard.store(); @@ -92,50 +126,43 @@ void Setup_Keyboard::cancel() void Setup_Keyboard::action(const gcn::ActionEvent &event) { - if (event.getId() == "makeDefault") + if (event.getSource() == mKeyList) { - keyboard.makeDefault(); - refreshKeys(); - return; + mAssignKeyButton->setEnabled(true); } - for (int i = 0; i < keyboard.KEY_TOTAL; i++) + else if (event.getId() == "assign") { - if (event.getId() == "sk"+toString(i)) - { - keyboard.setEnabled(false); - keyboard.setNewKeyIndex(i); - enableSetButtons(false); - mKeyLabel[i].setCaption(keyboard.getKeyCaption(i) + ": ?"); - } + int i(mKeyList->getSelected()); + mAssignKeyButton->setEnabled(false); + keyboard.setEnabled(false); + keyboard.setNewKeyIndex(i); + mKeyListModel->setElementAt(i, keyboard.getKeyCaption(i) + ": ?"); } -} - -void Setup_Keyboard::enableSetButtons(bool bValue) -{ - for (int i = 0; i < keyboard.KEY_TOTAL; i++) + else if (event.getId() == "makeDefault") { - mKeyButton[i].setEnabled(bValue); + keyboard.makeDefault(); + refreshKeys(); } } -void Setup_Keyboard::refreshAssignedKey(const int index) +void Setup_Keyboard::refreshAssignedKey(int index) { + std::string caption; char *temp = SDL_GetKeyName( (SDLKey) keyboard.getKeyValue(index)); - mKeyLabel[index].setCaption( - keyboard.getKeyCaption(index) + ": " + toString(temp)); - mKeyLabel[index].adjustSize(); + caption = keyboard.getKeyCaption(index) + ": " + toString(temp); + mKeyListModel->setElementAt(index, caption); } -void Setup_Keyboard::newKeyCallback(const int index) +void Setup_Keyboard::newKeyCallback(int index) { refreshAssignedKey(index); - enableSetButtons(true); + mAssignKeyButton->setEnabled(true); } void Setup_Keyboard::refreshKeys() { - for (int i = 0; i < keyboard.KEY_TOTAL; i++) + for(int i = 0; i < keyboard.KEY_TOTAL; i++) { refreshAssignedKey(i); } diff --git a/src/gui/setup_keyboard.h b/src/gui/setup_keyboard.h index 4701487b..ccc2558f 100644 --- a/src/gui/setup_keyboard.h +++ b/src/gui/setup_keyboard.h @@ -51,20 +51,15 @@ class Setup_Keyboard : public SetupTab, public gcn::ActionListener void action(const gcn::ActionEvent &event); - /** - * Easy way to disable/enable all the set buttons. - */ - void enableSetButtons(bool bValue); - /** * Get an update on the assigned key. */ - void refreshAssignedKey(const int index); + void refreshAssignedKey(int index); /** * The callback function when a new key has been pressed. */ - void newKeyCallback(const int index); + void newKeyCallback(int index); /** * Shorthand method to update all the keys. @@ -72,10 +67,10 @@ class Setup_Keyboard : public SetupTab, public gcn::ActionListener void refreshKeys(); private: - gcn::Label *mKeyLabel; - - Button *mKeyButton; + class KeyListModel *mKeyListModel; + gcn::ListBox *mKeyList; + gcn::Button *mAssignKeyButton; gcn::Button *mMakeDefaultButton; }; diff --git a/src/keyboardconfig.cpp b/src/keyboardconfig.cpp index 277fbcd3..73912000 100644 --- a/src/keyboardconfig.cpp +++ b/src/keyboardconfig.cpp @@ -56,13 +56,6 @@ void KeyboardConfig::init() retrieve(); } -void KeyboardConfig::destroy() -{ - store(); - - delete [] mActiveKeys; -} - void KeyboardConfig::retrieve() { for (int i = 0; i < KEY_TOTAL; i++) @@ -109,7 +102,7 @@ void KeyboardConfig::callbackNewKey() mSetupKey->newKeyCallback(mNewKeyIndex); } -int KeyboardConfig::getKeyIndex(const int keyValue) const +int KeyboardConfig::getKeyIndex(int keyValue) const { for (int i = 0; i < KEY_TOTAL; i++) { diff --git a/src/keyboardconfig.h b/src/keyboardconfig.h index e2eef385..64d80629 100644 --- a/src/keyboardconfig.h +++ b/src/keyboardconfig.h @@ -59,11 +59,6 @@ class KeyboardConfig */ void init(); - /** - * Destroys the keyboard config explicitly. - */ - void destroy(); - /** * Retrieve the key values from config file. */ @@ -110,7 +105,7 @@ class KeyboardConfig /** * Get the key caption, providing more meaning to the user. */ - std::string& getKeyCaption(int index) + std::string const &getKeyCaption(int index) const { return mKey[index].caption; } /** @@ -145,7 +140,7 @@ class KeyboardConfig /** * Checks if the key is active, by providing the key function index. */ - bool isKeyActive(const int index); + bool isKeyActive(int index); /** * Takes a snapshot of all the active keys. @@ -178,14 +173,14 @@ class KeyboardConfig }; private: - int mNewKeyIndex; /** Index of new key to be assigned */ - bool mEnabled; /** Flag to determine respond to key input */ + int mNewKeyIndex; /**< Index of new key to be assigned */ + bool mEnabled; /**< Flag to respond to key input */ - Setup_Keyboard *mSetupKey; /** Reference to setup window */ + Setup_Keyboard *mSetupKey; /**< Reference to setup window */ - KeyFunction mKey[KEY_TOTAL]; /** Pointer to all the key data */ + KeyFunction mKey[KEY_TOTAL]; /**< Pointer to all the key data */ - Uint8 *mActiveKeys; /** Stores a list of all the keys */ + Uint8 *mActiveKeys; /**< Stores a list of all the keys */ }; extern KeyboardConfig keyboard; diff --git a/src/main.cpp b/src/main.cpp index 112c5b6e..613d78a7 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -328,9 +328,6 @@ void init_engine(const Options &options) /** Clear the engine */ void exit_engine() { - // Remove Keyboard configuration. - keyboard.destroy(); - config.write(); delete gui; -- cgit v1.2.3-60-g2f50