Mana
Loading...
Searching...
No Matches
setup_keyboard.cpp
Go to the documentation of this file.
1/*
2 * Custom keyboard shortcuts configuration
3 * Copyright (C) 2007 Joshua Langley <joshlangley@optusnet.com.au>
4 * Copyright (C) 2009 The Mana World Development Team
5 * Copyright (C) 2009-2012 The Mana Developers
6 *
7 * This file is part of The Mana Client.
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 */
22
23#include "gui/setup_keyboard.h"
24
25#include "game.h"
26#include "keyboardconfig.h"
27
28#include "gui/gui.h"
29#include "gui/okdialog.h"
30
31#include "gui/widgets/button.h"
32#include "gui/widgets/layout.h"
33#include "gui/widgets/listbox.h"
35
36#include "utils/gettext.h"
37#include "utils/stringutils.h"
38
39#include <guichan/listmodel.hpp>
40
41#include <SDL_keyboard.h>
42
48class KeyListModel : public gcn::ListModel
49{
50 public:
55
59 std::string getElementAt(int i) override { return mKeyFunctions[i]; }
60
64 void setElementAt(int i, const std::string &caption)
65 {
66 mKeyFunctions[i] = caption;
67 }
68
69 private:
71};
72
74 mKeyListModel(new KeyListModel),
75 mKeyList(new ListBox(mKeyListModel)),
76 mKeySetting(false)
77{
79 setName(_("Keyboard"));
80
82
83 mKeyList->addActionListener(this);
84 mKeyList->setFont(monoFont);
85
86 auto *scrollArea = new ScrollArea(mKeyList);
87 scrollArea->setHorizontalScrollPolicy(gcn::ScrollArea::SHOW_NEVER);
88
89 mAssignKeyButton = new Button(_("Assign"), "assign", this);
90 mAssignKeyButton->addActionListener(this);
91 mAssignKeyButton->setEnabled(false);
92
93 mUnassignKeyButton = new Button(_("Unassign"), "unassign", this);
94 mUnassignKeyButton->addActionListener(this);
95 mUnassignKeyButton->setEnabled(false);
96
97 mMakeDefaultButton = new Button(_("Default"), "makeDefault", this);
98 mMakeDefaultButton->addActionListener(this);
99
100 // Do the layout
101 place(0, 0, scrollArea, 4, 6).setPadding(2);
103 place(2, 6, mAssignKeyButton);
105}
106
108{
109 delete mKeyList;
110 delete mKeyListModel;
111
112 delete mAssignKeyButton;
113 delete mUnassignKeyButton;
114 delete mMakeDefaultButton;
115}
116
118{
120
122 {
123 new OkDialog(_("Key Conflict(s) Detected."),
125 }
126 keyboard.setEnabled(true);
127 keyboard.store();
128}
129
131{
133
135 keyboard.setEnabled(true);
136
137 refreshKeys();
138}
139
140void Setup_Keyboard::action(const gcn::ActionEvent &event)
141{
142 if (event.getSource() == mKeyList)
143 {
144 if (!mKeySetting)
145 {
146 mAssignKeyButton->setEnabled(true);
147 mUnassignKeyButton->setEnabled(true);
148 }
149 }
150 else if (event.getId() == "assign")
151 {
152 mKeySetting = true;
153 mAssignKeyButton->setEnabled(false);
154 keyboard.setEnabled(false);
155 int i(mKeyList->getSelected());
158 }
159 else if (event.getId() == "unassign")
160 {
161 int i(mKeyList->getSelected());
163 refreshAssignedKey(mKeyList->getSelected());
165 mAssignKeyButton->setEnabled(true);
166 }
167 else if (event.getId() == "makeDefault")
168 {
170 refreshKeys();
171 }
172}
173
175{
176 std::string caption;
178 caption = keyboard.getKeyCaption(index) + ": ";
179 else
180 {
181 const char *temp = SDL_GetKeyName(keyboard.getKeyValue(index));
182
183 caption = strprintf("%-25s",
184 (keyboard.getKeyCaption(index) + ": ").c_str()) + toString(temp);
185
186 }
187 mKeyListModel->setElementAt(index, caption);
188 if (Game *game = Game::instance())
189 game->updateWindowMenuCaptions();
190}
191
193{
194 mKeySetting = false;
195 refreshAssignedKey(index);
196 mAssignKeyButton->setEnabled(true);
197}
198
200{
201 for (int i = 0; i < KeyboardConfig::KEY_TOTAL; i++)
202 {
204 }
205}
206
Button widget.
Definition button.h:38
LayoutCell & place(int x, int y, gcn::Widget *wg, int w=1, int h=1)
Adds a widget to the container and sets it at given cell.
Definition container.cpp:46
The main class responsible for running the game.
Definition game.h:37
static Game * instance()
Provides access to the game instance.
Definition game.h:53
The list model for key function list.
int getNumberOfElements() override
Returns the number of elements in container.
std::string getElementAt(int i) override
Returns element from container.
void setElementAt(int i, const std::string &caption)
Sets element from container.
std::string mKeyFunctions[KeyboardConfig::KEY_TOTAL]
std::string getBindError() const
int getKeyValue(int index) const
Obtain the value stored in memory.
void store()
Store the key values to config file.
void setNewKey(SDL_Keycode value)
Set the value of the new key.
int getNewKeyIndex() const
Get the index of the new key to be assigned.
void makeDefault()
Make the keys their default values.
bool hasConflicts()
Determines if any key assignments are the same as each other.
void retrieve()
Retrieve the key values from config file.
void setEnabled(bool flag)
Set the enable flag, which will stop the user from doing actions.
const std::string & getKeyCaption(int index) const
Get the key caption, providing more meaning to the user.
void setNewKeyIndex(int value)
Set the index of the new key to be assigned.
void setSetupKeyboard(Setup_Keyboard *setupKey)
Set a reference to the key setup window.
LayoutCell & setPadding(int p)
Sets the padding around the cell content.
Definition layout.h:179
A list box, meant to be used inside a scroll area.
Definition listbox.h:36
An 'Ok' button dialog.
Definition okdialog.h:34
A scroll area.
Definition scrollarea.h:38
void setName(const std::string &name)
Sets the name displayed on the tab.
Definition setuptab.h:54
void cancel() override
Called when the Cancel button is pressed in the setup window.
void keyUnresolved()
If a key function is unresolved, then this reverts it.
void newKeyCallback(int index)
The callback function when a new key has been pressed.
class KeyListModel * mKeyListModel
void refreshAssignedKey(int index)
Get an update on the assigned key.
gcn::Button * mMakeDefaultButton
void refreshKeys()
Shorthand method to update all the keys.
~Setup_Keyboard() override
gcn::ListBox * mKeyList
gcn::Button * mAssignKeyButton
bool mKeySetting
flag to check if key being set.
void action(const gcn::ActionEvent &event) override
void apply() override
Called when the Apply button is pressed in the setup window.
gcn::Button * mUnassignKeyButton
KeyboardConfig keyboard
Definition client.cpp:101
#define _(s)
Definition gettext.h:38
gcn::Font * monoFont
Monospaced text font.
Definition gui.cpp:57
std::string strprintf(char const *format,...)
A safe version of sprintf that returns a std::string of the result.
std::string toString(const T &arg)
Converts the given value to a string using std::stringstream.
Definition stringutils.h:68