diff options
author | Joshua Langley <joshlangley[at]optusnet.com.au> | 2007-07-16 15:23:34 +0000 |
---|---|---|
committer | Joshua Langley <joshlangley[at]optusnet.com.au> | 2007-07-16 15:23:34 +0000 |
commit | 71532a23200b246f63ed1e0dc3d563bd1184b593 (patch) | |
tree | 2ce28dd04573bb40b31e367d7d980b32f113edab | |
parent | bb5f430e6f7e1471aefc1de22e423e6015d0236c (diff) | |
download | mana-71532a23200b246f63ed1e0dc3d563bd1184b593.tar.gz mana-71532a23200b246f63ed1e0dc3d563bd1184b593.tar.bz2 mana-71532a23200b246f63ed1e0dc3d563bd1184b593.tar.xz mana-71532a23200b246f63ed1e0dc3d563bd1184b593.zip |
-rw-r--r-- | ChangeLog | 14 | ||||
-rw-r--r-- | The Mana World.dev | 60 | ||||
-rw-r--r-- | src/game.cpp | 148 | ||||
-rw-r--r-- | src/gui/button.cpp | 20 | ||||
-rw-r--r-- | src/gui/button.h | 3 | ||||
-rw-r--r-- | src/gui/setup.cpp | 11 | ||||
-rw-r--r-- | src/gui/setup_keyboard.cpp | 145 | ||||
-rw-r--r-- | src/gui/setup_keyboard.h | 74 | ||||
-rw-r--r-- | src/keyboardconfig.cpp | 129 | ||||
-rw-r--r-- | src/keyboardconfig.h | 208 | ||||
-rw-r--r-- | src/main.cpp | 10 |
11 files changed, 729 insertions, 93 deletions
@@ -1,4 +1,16 @@ -2007-07-16 Eugenio Favalli <elvenprogrammer@gmail.com> +2007-07-17 Joshua Langley <joshlangley[at]optusnet.com.au> + + * src/game.cpp: only one key per function. + * src/main.cpp: keyboard configuration included. + * src/gui/button.cpp, src/gui/button.h: default constructor and + init function added. + * src/gui/setup.cpp: keyboard setup tab added. + * src/gui/setup_keyboard.cpp, src/gui/setup_keyboard.h: + Add to project file, it is the keyboard setup tab. + * src/keyboardconfig.cpp, src/keyboardconfig.h: Add to project file, + the main keyboard config operations. + +2007-07-16 Eugenio Favalli <elvenprogrammer@gmail.com> * tmw.cbp: Fixed description of silk headband. diff --git a/The Mana World.dev b/The Mana World.dev index 963e5c7c..b2fa556a 100644 --- a/The Mana World.dev +++ b/The Mana World.dev @@ -1,7 +1,7 @@ [Project] FileName=The Mana World.dev Name=tmw -UnitCount=278 +UnitCount=281 Type=0 Ver=1 ObjFiles= @@ -2550,7 +2550,7 @@ BuildCmd= [Unit250] FileName=src\animationparticle.cpp CompileCpp=1 -Folder=tmw +Folder= Compile=1 Link=1 Priority=1000 @@ -2560,7 +2560,7 @@ BuildCmd= [Unit251] FileName=src\animationparticle.h CompileCpp=1 -Folder=tmw +Folder= Compile=1 Link=1 Priority=1000 @@ -2570,7 +2570,7 @@ BuildCmd= [Unit252] FileName=src\imageparticle.cpp CompileCpp=1 -Folder=tmw +Folder= Compile=1 Link=1 Priority=1000 @@ -2580,7 +2580,7 @@ BuildCmd= [Unit253] FileName=src\imageparticle.h CompileCpp=1 -Folder=tmw +Folder= Compile=1 Link=1 Priority=1000 @@ -2590,7 +2590,7 @@ BuildCmd= [Unit254] FileName=src\particle.cpp CompileCpp=1 -Folder=tmw +Folder= Compile=1 Link=1 Priority=1000 @@ -2600,7 +2600,7 @@ BuildCmd= [Unit255] FileName=src\particle.h CompileCpp=1 -Folder=tmw +Folder= Compile=1 Link=1 Priority=1000 @@ -2610,7 +2610,7 @@ BuildCmd= [Unit256] FileName=src\particleemitter.cpp CompileCpp=1 -Folder=tmw +Folder= Compile=1 Link=1 Priority=1000 @@ -2620,7 +2620,7 @@ BuildCmd= [Unit257] FileName=src\particleemitter.h CompileCpp=1 -Folder=tmw +Folder= Compile=1 Link=1 Priority=1000 @@ -2630,7 +2630,7 @@ BuildCmd= [Unit258] FileName=src\simpleanimation.cpp CompileCpp=1 -Folder=tmw +Folder= Compile=1 Link=1 Priority=1000 @@ -2640,7 +2640,7 @@ BuildCmd= [Unit259] FileName=src\simpleanimation.h CompileCpp=1 -Folder=tmw +Folder= Compile=1 Link=1 Priority=1000 @@ -2650,7 +2650,7 @@ BuildCmd= [Unit260] FileName=src\textparticle.cpp CompileCpp=1 -Folder=tmw +Folder= Compile=1 Link=1 Priority=1000 @@ -2660,7 +2660,7 @@ BuildCmd= [Unit261] FileName=src\textparticle.h CompileCpp=1 -Folder=tmw +Folder= Compile=1 Link=1 Priority=1000 @@ -2828,9 +2828,39 @@ OverrideBuildCmd=0 BuildCmd= [Unit278] -FileName=src\resources\equipmentinfo.cpp +FileName=..\tmw_3367_CR0000016\src\keyboardconfig.cpp CompileCpp=1 -Folder=tmw +Folder= +Compile=1 +Link=1 +Priority=1000 +OverrideBuildCmd=0 +BuildCmd= + +[Unit279] +FileName=..\tmw_3367_CR0000016\src\keyboardconfig.h +CompileCpp=1 +Folder=header +Compile=1 +Link=1 +Priority=1000 +OverrideBuildCmd=0 +BuildCmd= + +[Unit280] +FileName=..\tmw_3367_CR0000016\src\gui\setup_keyboard.cpp +CompileCpp=1 +Folder=gui/source +Compile=1 +Link=1 +Priority=1000 +OverrideBuildCmd=0 +BuildCmd= + +[Unit281] +FileName=..\tmw_3367_CR0000016\src\gui\setup_keyboard.h +CompileCpp=1 +Folder=gui/header Compile=1 Link=1 Priority=1000 diff --git a/src/game.cpp b/src/game.cpp index 54474b1c..eb990e1b 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -37,6 +37,7 @@ #include "flooritemmanager.h" #include "graphics.h" #include "joystick.h" +#include "keyboardconfig.h" #include "localplayer.h" #include "log.h" #include "npc.h" @@ -426,6 +427,14 @@ void Game::handleInput() { gcn::Window *requestedWindow = NULL; + if (//setupWindow->isVisible() && + keyboard.getNewKeyIndex() > keyboard.KEY_NO_VALUE) + { + keyboard.setNewKey((int) event.key.keysym.sym); + keyboard.callbackNewKey(); + keyboard.setNewKeyIndex(keyboard.KEY_NO_VALUE); + return; + } switch (event.key.keysym.sym) { case SDLK_F1: @@ -484,54 +493,7 @@ void Game::handleInput() used = true; } break; - - // Hide certain windows - case SDLK_h: - if (!chatWindow->isFocused()) - { - statusWindow->setVisible(false); - inventoryWindow->setVisible(false); - skillDialog->setVisible(false); - setupWindow->setVisible(false); - equipmentWindow->setVisible(false); - helpWindow->setVisible(false); - debugWindow->setVisible(false); - } - break; - - // Picking up items on the floor - case SDLK_g: - case SDLK_z: - if (!chatWindow->isFocused()) - { - FloorItem *item = floorItemManager->findByCoordinates( - player_node->mX, player_node->mY); - - // If none below the player, try the tile in front of - // the player - if (!item) { - Uint16 x = player_node->mX; - Uint16 y = player_node->mY; - if (player_node->getDirection() & Being::UP) - y--; - if (player_node->getDirection() & Being::DOWN) - y++; - if (player_node->getDirection() & Being::LEFT) - x--; - if (player_node->getDirection() & Being::RIGHT) - x++; - - item = floorItemManager->findByCoordinates(x, y); - } - - if (item) - player_node->pickUp(item); - - used = true; - } - break; - - // Quitting confirmation dialog + // Quitting confirmation dialog case SDLK_ESCAPE: if (!exitConfirm) { exitConfirm = new ConfirmDialog( @@ -545,6 +507,58 @@ void Game::handleInput() break; } + if (keyboard.isEnabled() && !chatWindow->isFocused()) + { + switch (keyboard.getKeyIndex(event.key.keysym.sym)) { + case KeyboardConfig::KEY_PICKUP: + { + FloorItem *item = floorItemManager->findByCoordinates( + player_node->mX, player_node->mY); + + // If none below the player, try the tile in front of + // the player + if (!item) { + Uint16 x = player_node->mX; + Uint16 y = player_node->mY; + if (player_node->getDirection() & Being::UP) + y--; + if (player_node->getDirection() & Being::DOWN) + y++; + if (player_node->getDirection() & Being::LEFT) + x--; + if (player_node->getDirection() & Being::RIGHT) + x++; + + item = floorItemManager->findByCoordinates(x, y); + } + + if (item) + player_node->pickUp(item); + + used = true; + } + break; + case KeyboardConfig::KEY_SIT: + // Player sit action + player_node->toggleSit(); + used = true; + break; + case KeyboardConfig::KEY_HIDE_WINDOWS: + // Hide certain windows + if (!chatWindow->isFocused()) + { + statusWindow->setVisible(false); + inventoryWindow->setVisible(false); + skillDialog->setVisible(false); + setupWindow->setVisible(false); + equipmentWindow->setVisible(false); + helpWindow->setVisible(false); + debugWindow->setVisible(false); + } + break; + } + } + if (requestedWindow) { requestedWindow->setVisible(!requestedWindow->isVisible()); @@ -555,18 +569,13 @@ void Game::handleInput() used = true; } + // Keys pressed together with Alt/Meta // Emotions and some internal gui windows if (event.key.keysym.mod & KMOD_ALT) { switch (event.key.keysym.sym) { - case SDLK_s: - // Player sit action - player_node->toggleSit(); - used = true; - break; - case SDLK_p: // Screenshot (picture, hence the p) { @@ -636,41 +645,42 @@ void Game::handleInput() } } // End while - + // If the user is configuring the keys then don't respond. + if (!keyboard.isEnabled()) + { + return; + } // Moving player around if (player_node->mAction != Being::DEAD && current_npc == 0 && !chatWindow->isFocused()) { // Get the state of the keyboard keys - Uint8* keys; - keys = SDL_GetKeyState(NULL); + keyboard.refreshActiveKeys(); Uint16 x = player_node->mX; Uint16 y = player_node->mY; unsigned char direction = 0; // Translate pressed keys to movement and direction - if (keys[SDLK_UP] || keys[SDLK_KP8] || - keys[SDLK_KP7] || keys[SDLK_KP9] || + if ( keyboard.isKeyActive(keyboard.KEY_MOVE_UP) || joystick && joystick->isUp()) { direction |= Being::UP; } - else if (keys[SDLK_DOWN] || keys[SDLK_KP2] || - keys[SDLK_KP1] || keys[SDLK_KP3] || + + else if ( keyboard.isKeyActive(keyboard.KEY_MOVE_DOWN) || joystick && joystick->isDown()) { direction |= Being::DOWN; } - if (keys[SDLK_LEFT] || keys[SDLK_KP4] || - keys[SDLK_KP1] || keys[SDLK_KP7] || + + if ( keyboard.isKeyActive(keyboard.KEY_MOVE_LEFT) || joystick && joystick->isLeft()) { direction |= Being::LEFT; } - else if (keys[SDLK_RIGHT] || keys[SDLK_KP6] || - keys[SDLK_KP3] || keys[SDLK_KP9] || + else if ( keyboard.isKeyActive(keyboard.KEY_MOVE_RIGHT) || joystick && joystick->isRight()) { direction |= Being::RIGHT; @@ -679,12 +689,11 @@ void Game::handleInput() player_node->setWalkingDir(direction); // Attacking monsters - if (keys[SDLK_LCTRL] || keys[SDLK_RCTRL] || + if ( keyboard.isKeyActive(keyboard.KEY_ATTACK) || joystick && joystick->buttonPressed(0)) { Being *target = NULL; - bool newTarget = keys[SDLK_LSHIFT] || keys[SDLK_RSHIFT]; - + bool newTarget = keyboard.isKeyActive(keyboard.KEY_TARGET); // A set target has highest priority if (newTarget || !player_node->getTarget()) { @@ -711,7 +720,8 @@ void Game::handleInput() } // Target the nearest monster if 'a' pressed - if (keys[SDLK_a]) + if ( keyboard.isKeyActive(keyboard.KEY_TARGET_CLOSEST) ) + //if (keys[SDLK_a]) { Being *target = beingManager->findNearestLivingBeing(x, y, 20, Being::MONSTER); diff --git a/src/gui/button.cpp b/src/gui/button.cpp index e607b66a..a9212e83 100644 --- a/src/gui/button.cpp +++ b/src/gui/button.cpp @@ -37,11 +37,26 @@ ImageRect Button::button[4]; int Button::mInstances = 0; +Button::Button(): + mIsLogged(false) +{ + init(); +} + Button::Button(const std::string& caption, const std::string &actionEventId, gcn::ActionListener *listener): gcn::Button(caption), mIsLogged(false) { + init(); + setActionEventId(actionEventId); + if (listener) { + addActionListener(listener); + } +} + +void Button::init() +{ setBorderSize(0); if (mInstances == 0) @@ -72,12 +87,7 @@ Button::Button(const std::string& caption, const std::string &actionEventId, btn[mode]->decRef(); } } - mInstances++; - setActionEventId(actionEventId); - if (listener) { - addActionListener(listener); - } } Button::~Button() diff --git a/src/gui/button.h b/src/gui/button.h index eb73e311..68831d5a 100644 --- a/src/gui/button.h +++ b/src/gui/button.h @@ -37,6 +37,7 @@ class ImageRect; */ class Button : public gcn::Button { public: + Button(); /** * Constructor, sets the caption of the button to the given string. */ @@ -60,6 +61,8 @@ class Button : public gcn::Button { { mIsLogged = enable; } private: + void init(); + static ImageRect button[4]; /**< Button state graphics */ static int mInstances; /**< Number of button instances */ bool mIsLogged; /**< Makes the button appear pressed all the time */ diff --git a/src/gui/setup.cpp b/src/gui/setup.cpp index 3add3a18..b936f688 100644 --- a/src/gui/setup.cpp +++ b/src/gui/setup.cpp @@ -27,6 +27,7 @@ #include "setup_audio.h" #include "setup_joystick.h" #include "setup_video.h" +#include "setup_keyboard.h" #include "tabbedcontainer.h" #include "../utils/dtor.h" @@ -42,8 +43,8 @@ extern Window *skillDialog; Setup::Setup(): Window("Setup") { - int width = 230; - int height = 245; + int width = 260; + int height = 305; setContentSize(width, height); const char *buttonNames[] = { @@ -58,7 +59,7 @@ Setup::Setup(): } TabbedContainer *panel = new TabbedContainer(); - panel->setDimension(gcn::Rectangle(5, 5, 220, 205)); + panel->setDimension(gcn::Rectangle(5, 5, 250, 265)); panel->setOpaque(false); SetupTab *tab; @@ -75,6 +76,10 @@ Setup::Setup(): panel->addTab(tab, "Joystick"); mTabs.push_back(tab); + tab = new Setup_Keyboard(); + panel->addTab(tab, "Keyboard"); + mTabs.push_back(tab); + add(panel); setLocationRelativeTo(getParent()); diff --git a/src/gui/setup_keyboard.cpp b/src/gui/setup_keyboard.cpp new file mode 100644 index 00000000..6ca149a5 --- /dev/null +++ b/src/gui/setup_keyboard.cpp @@ -0,0 +1,145 @@ +/*
+ * The Mana World
+ * Copyright 2007 The Mana World Development Team
+ *
+ * This file is part of The Mana World.
+ *
+ * The Mana World is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * any later version.
+ *
+ * The Mana World is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with The Mana World; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ *
+ */
+
+#include "setup_keyboard.h"
+
+#include <guichan/widgets/label.hpp>
+
+#include "button.h"
+#include "ok_dialog.h"
+
+#include "../configuration.h"
+#include "../keyboardconfig.h"
+#include "../log.h"
+
+#include "../utils/tostring.h"
+
+#include <SDL_keyboard.h>
+
+Setup_Keyboard::Setup_Keyboard()
+{
+ setOpaque(false);
+
+ keyboard.setSetupKeyboard(this);
+
+ mKeyLabel = new gcn::Label[keyboard.KEY_TOTAL];
+ mKeyButton = new Button[keyboard.KEY_TOTAL];
+
+ 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->addActionListener(this);
+ add(mMakeDefaultButton);
+}
+
+Setup_Keyboard::~Setup_Keyboard()
+{
+ delete [] mKeyLabel;
+ delete [] mKeyButton;
+ delete mMakeDefaultButton;
+}
+
+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");
+ }
+ keyboard.setEnabled(true);
+ keyboard.store();
+}
+
+void Setup_Keyboard::cancel()
+{
+ keyboard.retrieve();
+ keyboard.setEnabled(true);
+ refreshKeys();
+}
+
+void Setup_Keyboard::action(const gcn::ActionEvent &event)
+{
+ if (event.getId() == "makeDefault")
+ {
+ keyboard.makeDefault();
+ refreshKeys();
+ return;
+ }
+ for (int i=0; i < keyboard.KEY_TOTAL; i++)
+ {
+ if (event.getId() == "sk"+toString(i))
+ {
+ keyboard.setEnabled(false);
+ keyboard.setNewKeyIndex(i);
+ enableButtons(false);
+ mKeyLabel[i].setCaption(keyboard.getKeyCaption(i) + ": ?");
+ }
+ }
+}
+
+
+void Setup_Keyboard::enableButtons(bool bValue)
+{
+ for (int i=0; i < keyboard.KEY_TOTAL; i++)
+ {
+ mKeyButton[i].setEnabled(bValue);
+ }
+}
+
+void Setup_Keyboard::refreshAssignedKey(const int index)
+{
+ char *temp = SDL_GetKeyName(
+ (SDLKey) keyboard.getKeyValue(index));
+ mKeyLabel[index].setCaption(
+ keyboard.getKeyCaption(index) + ": " + toString(temp));
+ mKeyLabel[index].adjustSize();
+}
+
+void Setup_Keyboard::newKeyCallback(const int index)
+{
+ refreshAssignedKey(index);
+ enableButtons(true);
+}
+
+void Setup_Keyboard::refreshKeys()
+{
+ 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 new file mode 100644 index 00000000..0430b040 --- /dev/null +++ b/src/gui/setup_keyboard.h @@ -0,0 +1,74 @@ +/*
+ * The Mana World
+ * Copyright 2007 The Mana World Development Team
+ *
+ * This file is part of The Mana World.
+ *
+ * The Mana World is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * any later version.
+ *
+ * The Mana World is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with The Mana World; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ *
+ */
+
+#ifndef _TMW_GUI_SETUP_KEYBOARD_H
+#define _TMW_GUI_SETUP_KEYBOARD_H
+
+#include "setuptab.h"
+#include "button.h"
+#include "../guichanfwd.h"
+
+#include <guichan/actionlistener.hpp>
+
+
+#include <string>
+
+class Setup_Keyboard : public SetupTab, public gcn::ActionListener
+{
+ public:
+ Setup_Keyboard();
+ ~Setup_Keyboard();
+
+ void apply();
+ void cancel();
+
+ void action(const gcn::ActionEvent &event);
+
+ /**
+ * easy way to disable/enable all the set buttons.
+ */
+ void enableButtons(bool bValue);
+
+ /**
+ * get an update on the assigned key.
+ */
+ void refreshAssignedKey(const int index);
+
+ /**
+ * the callback function when a new key has been pressed.
+ */
+ void newKeyCallback(const int index);
+
+ /**
+ * shorthand method to update all the keys.
+ */
+ void refreshKeys();
+
+ private:
+ gcn::Label *mKeyLabel;
+
+ Button *mKeyButton;
+
+ gcn::Button *mMakeDefaultButton;
+};
+
+#endif
diff --git a/src/keyboardconfig.cpp b/src/keyboardconfig.cpp new file mode 100644 index 00000000..3185a58a --- /dev/null +++ b/src/keyboardconfig.cpp @@ -0,0 +1,129 @@ +/*
+ * The Mana World
+ * Copyright 2007 The Mana World Development Team
+ *
+ * This file is part of The Mana World.
+ *
+ * The Mana World is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * any later version.
+ *
+ * The Mana World is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with The Mana World; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ *
+ */
+
+#include "keyboardconfig.h"
+#include "configuration.h"
+#include "log.h"
+
+#include <guichan/sdl/sdlinput.hpp>
+
+#include "gui/setup_keyboard.h"
+
+void KeyboardConfig::destroy()
+{
+
+}
+
+void KeyboardConfig::init()
+{
+ mKey[KEY_MOVE_UP] = KeyFunction("keyMoveUp", SDLK_UP, "Move Up");
+ mKey[KEY_MOVE_DOWN] = KeyFunction("keyMoveDown", SDLK_DOWN, "Move Down");
+ mKey[KEY_MOVE_LEFT] = KeyFunction("keyMoveLeft", SDLK_LEFT, "Move Left");
+ mKey[KEY_MOVE_RIGHT] =
+ KeyFunction("keyMoveRight", SDLK_RIGHT, "Move Right");
+
+ mKey[KEY_ATTACK] = KeyFunction("keyAttack", SDLK_LCTRL, "Attack");
+ mKey[KEY_TARGET] = KeyFunction("keyTarget", SDLK_LSHIFT, "Target");
+ mKey[KEY_TARGET_CLOSEST] =
+ KeyFunction("mKeyTargetClosest", SDLK_a, "Target Closest");
+ mKey[KEY_PICKUP] = KeyFunction("keyPickup", SDLK_z, "Pickup");
+ mKey[KEY_HIDE_WINDOWS] =
+ KeyFunction("keyHideWindows", SDLK_h, "Hide Windows");
+ mKey[KEY_SIT] = KeyFunction("keyBeingSit", SDLK_g, "Sit");
+
+ for (int i = 0; i < KEY_TOTAL; i++)
+ {
+ mKey[i].value = KEY_NO_VALUE;
+ }
+ mNewKeyIndex = KEY_NO_VALUE;
+ mEnabled = true;
+
+ retrieve();
+}
+
+void KeyboardConfig::retrieve()
+{
+ for (int i = 0; i < KEY_TOTAL; i++)
+ {
+ mKey[i].value = (int) config.getValue(
+ mKey[i].configField, mKey[i].defaultValue);
+ }
+}
+
+void KeyboardConfig::store()
+{
+ for (int i = 0; i < KEY_TOTAL; i++)
+ {
+ config.setValue(mKey[i].configField, mKey[i].value);
+ }
+}
+
+void KeyboardConfig::makeDefault()
+{
+ for (int i = 0; i < KEY_TOTAL; i++)
+ {
+ mKey[i].value = mKey[i].defaultValue;
+ }
+}
+
+bool KeyboardConfig::hasConflicts()
+{
+ int i, j;
+ for (i = 0; i < KEY_TOTAL; i++)
+ {
+ for (j = 0; j < KEY_TOTAL; j++)
+ {
+ if (i != j && mKey[i].value == mKey[j].value)
+ {
+ return true;
+ }
+ }
+ }
+ return false;
+}
+
+void KeyboardConfig::callbackNewKey()
+{
+ mSetupKey->newKeyCallback((const int) mNewKeyIndex);
+}
+
+int KeyboardConfig::getKeyIndex(const int keyValue) const
+{
+ for (int i = 0; i < KEY_TOTAL; i++)
+ {
+ if(keyValue == mKey[i].value)
+ {
+ return i;
+ }
+ }
+ return KEY_NO_VALUE;
+}
+
+bool KeyboardConfig::isKeyActive(int index)
+{
+ return mActiveKeys[mKey[index].value];
+}
+
+void KeyboardConfig::refreshActiveKeys()
+{
+ mActiveKeys = SDL_GetKeyState(NULL);
+}
diff --git a/src/keyboardconfig.h b/src/keyboardconfig.h new file mode 100644 index 00000000..73e08730 --- /dev/null +++ b/src/keyboardconfig.h @@ -0,0 +1,208 @@ +/*
+ * The Mana World
+ * Copyright 2007 The Mana World Development Team
+ *
+ * This file is part of The Mana World.
+ *
+ * The Mana World is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * any later version.
+ *
+ * The Mana World is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with The Mana World; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ *
+ */
+
+#ifndef _TMW_KEYBOARDCONFIG_H
+#define _TMW_KEYBOARDCONFIG_H
+
+#include <string>
+
+#include "gui/setup_keyboard.h"
+
+#include <guichan/sdl/sdlinput.hpp>
+
+#define CALL_MEMBER_FN(object,ptrToMember) ((object).*(ptrToMember))
+
+/**
+ * Each key represents a key function. Such as 'Move up', 'Attack' etc.
+ */
+struct KeyFunction
+{
+ KeyFunction(){}
+ KeyFunction(std::string configField, int defaultValue, std::string caption):
+ configField(configField),
+ defaultValue(defaultValue),
+ caption(caption)
+ { }
+
+ std::string configField;
+ std::string caption;
+ int defaultValue;
+ int value;
+};
+
+class KeyboardConfig
+{
+ public:
+ /**
+ * initialize the keyboard config.
+ */
+ void init();
+
+ /**
+ * destroy the object by releasing any memory.
+ */
+ void destroy();
+
+ /**
+ * retrieve the key values from config file.
+ */
+ void retrieve();
+
+ /**
+ * store the key values to config file.
+ */
+ void store();
+
+ /**
+ * make the keys their default values.
+ */
+ void makeDefault();
+
+ /**
+ * determines if any key assignments are the same as each other.
+ */
+ bool hasConflicts();
+
+ /**
+ * calls a function back so the key re-assignment(s) can be seen.
+ */
+ void callbackNewKey();
+
+ /**
+ * obtain the value stored in memory.
+ */
+ int getKeyValue(int index) const
+ {
+ return mKey[index].value;
+ };
+
+ /**
+ * get the index of the new key to be assigned.
+ */
+ int getNewKeyIndex() const
+ {
+ return mNewKeyIndex;
+ };
+
+ /**
+ * get the enable flag, which will stop the user from doing actions.
+ */
+ bool isEnabled() const
+ {
+ return mEnabled;
+ };
+
+ /**
+ * get the key caption, providing more meaning to the user.
+ */
+ std::string& getKeyCaption(int index)
+ {
+ return mKey[index].caption;
+ };
+
+ /**
+ * get the key function index by providing the keys value.
+ */
+ int getKeyIndex(int keyValue) const;
+
+ /**
+ * set the enable flag, which will stop the user from doing actions.
+ */
+ void setEnabled(bool flag)
+ {
+ mEnabled = flag;
+ };
+
+ /**
+ * set the index of the new key to be assigned.
+ */
+ void setNewKeyIndex(int value)
+ {
+ mNewKeyIndex = value;
+ };
+
+ /**
+ * set the value of the new key.
+ */
+ void setNewKey(int value)
+ {
+ mKey[mNewKeyIndex].value = value;
+ };
+
+ /**
+ * set a reference to the key setup window.
+ */
+ void setSetupKeyboard(Setup_Keyboard *setupKey)
+ {
+ mSetupKey = setupKey;
+ };
+
+ /**
+ * checks if the key is active, by providing the key function index.
+ */
+ bool isKeyActive(const int index);
+
+ /**
+ * takes a snapshot of all the active keys.
+ */
+ void refreshActiveKeys();
+
+ /**
+ * All the key functions.
+ * KEY_NO_VALUE is used in initialization, and should be unchanged.
+ * KEY_MIN and KEY_TOTAL should always be first and last respectively,
+ * the bare used in control loops.
+ * The third element (after KEY_NO_VALUE and KEY_MIN),
+ * should always equal KEY_MIN.
+ * The key assignment view gets arranged according to the order of
+ * these values.
+ */
+ enum KeyAction {
+ KEY_NO_VALUE = -1,
+ KEY_MOVE_UP,
+ KEY_MOVE_DOWN,
+ KEY_MOVE_LEFT,
+ KEY_MOVE_RIGHT,
+ KEY_ATTACK,
+ KEY_TARGET,
+ KEY_TARGET_CLOSEST,
+ KEY_PICKUP,
+ KEY_SIT,
+ KEY_HIDE_WINDOWS,
+ KEY_TOTAL
+ };
+ private:
+ int mNewKeyIndex; /** index of new key to be assigned */
+ bool mEnabled; /** flag to determine respond to key input */
+
+ Setup_Keyboard *mSetupKey; /** reference to setup window */
+
+ KeyFunction mKey[KEY_TOTAL]; /** pointer to all the key data */
+
+ Uint8 *mActiveKeys; /** stores a list of all the keys */
+
+};
+extern KeyboardConfig keyboard;
+
+#endif
+
+
diff --git a/src/main.cpp b/src/main.cpp index 1a6aea4c..e147f760 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -45,6 +45,7 @@ #endif #include "configuration.h" +#include "keyboardconfig.h" #include "game.h" #include "graphics.h" #include "lockedarray.h" @@ -103,6 +104,7 @@ Music *bgm; Configuration config; /**< XML file configuration reader */ Logger *logger; /**< Log object */ +KeyboardConfig keyboard; /** * A structure holding the values of various options that can be passed from @@ -318,12 +320,20 @@ void init_engine(const Options &options) errorMessage = err; logger->log("Warning: %s", err); } + + //Initialize keyboard + keyboard.init(); } /** Clear the engine */ void exit_engine() { + // Store keys and Remove Keyboard configuration. + keyboard.store(); + keyboard.destroy(); + config.write(); + delete gui; delete graphics; |