summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoryexo <yexo@openttd.org>2010-07-03 13:27:32 +0000
committeryexo <yexo@openttd.org>2010-07-03 13:27:32 +0000
commitd1044c036a1321d377dfe0a79ed5d39888bfd3d1 (patch)
treebbf4795620090d37cc1445ab47b83173455d52d1
parenteb19d4b440a6b02c7c7f5f4d6c37e26d2b8b36ae (diff)
downloadopenttd-d1044c036a1321d377dfe0a79ed5d39888bfd3d1.tar.xz
(svn r20054) -Add: framework to check for hotkeys
-rw-r--r--projects/openttd_vs100.vcxproj2
-rw-r--r--projects/openttd_vs100.vcxproj.filters6
-rw-r--r--projects/openttd_vs80.vcproj8
-rw-r--r--projects/openttd_vs90.vcproj8
-rw-r--r--source.list2
-rw-r--r--src/hotkeys.cpp15
-rw-r--r--src/hotkeys.h125
7 files changed, 166 insertions, 0 deletions
diff --git a/projects/openttd_vs100.vcxproj b/projects/openttd_vs100.vcxproj
index 5198ee4be..6fdf426ac 100644
--- a/projects/openttd_vs100.vcxproj
+++ b/projects/openttd_vs100.vcxproj
@@ -330,6 +330,7 @@
<ClCompile Include="..\src\ground_vehicle.cpp" />
<ClCompile Include="..\src\heightmap.cpp" />
<ClCompile Include="..\src\highscore.cpp" />
+ <ClCompile Include="..\src\hotkeys.cpp" />
<ClCompile Include="..\src\ini.cpp" />
<ClCompile Include="..\src\landscape.cpp" />
<ClCompile Include="..\src\map.cpp" />
@@ -448,6 +449,7 @@
<ClInclude Include="..\src\gui.h" />
<ClInclude Include="..\src\heightmap.h" />
<ClInclude Include="..\src\highscore.h" />
+ <ClInclude Include="..\src\hotkeys.h" />
<ClInclude Include="..\src\house.h" />
<ClInclude Include="..\src\house_type.h" />
<ClInclude Include="..\src\industry.h" />
diff --git a/projects/openttd_vs100.vcxproj.filters b/projects/openttd_vs100.vcxproj.filters
index bccaedab8..e063b737a 100644
--- a/projects/openttd_vs100.vcxproj.filters
+++ b/projects/openttd_vs100.vcxproj.filters
@@ -193,6 +193,9 @@
<ClCompile Include="..\src\highscore.cpp">
<Filter>Source Files</Filter>
</ClCompile>
+ <ClCompile Include="..\src\hotkeys.cpp">
+ <Filter>Source Files</Filter>
+ </ClCompile>
<ClCompile Include="..\src\ini.cpp">
<Filter>Source Files</Filter>
</ClCompile>
@@ -547,6 +550,9 @@
<ClInclude Include="..\src\highscore.h">
<Filter>Header Files</Filter>
</ClInclude>
+ <ClInclude Include="..\src\hotkeys.h">
+ <Filter>Header Files</Filter>
+ </ClInclude>
<ClInclude Include="..\src\house.h">
<Filter>Header Files</Filter>
</ClInclude>
diff --git a/projects/openttd_vs80.vcproj b/projects/openttd_vs80.vcproj
index e145e1c15..febd17297 100644
--- a/projects/openttd_vs80.vcproj
+++ b/projects/openttd_vs80.vcproj
@@ -584,6 +584,10 @@
>
</File>
<File
+ RelativePath=".\..\src\hotkeys.cpp"
+ >
+ </File>
+ <File
RelativePath=".\..\src\ini.cpp"
>
</File>
@@ -1060,6 +1064,10 @@
>
</File>
<File
+ RelativePath=".\..\src\hotkeys.h"
+ >
+ </File>
+ <File
RelativePath=".\..\src\house.h"
>
</File>
diff --git a/projects/openttd_vs90.vcproj b/projects/openttd_vs90.vcproj
index 0d1c69c88..e21534b4f 100644
--- a/projects/openttd_vs90.vcproj
+++ b/projects/openttd_vs90.vcproj
@@ -581,6 +581,10 @@
>
</File>
<File
+ RelativePath=".\..\src\hotkeys.cpp"
+ >
+ </File>
+ <File
RelativePath=".\..\src\ini.cpp"
>
</File>
@@ -1057,6 +1061,10 @@
>
</File>
<File
+ RelativePath=".\..\src\hotkeys.h"
+ >
+ </File>
+ <File
RelativePath=".\..\src\house.h"
>
</File>
diff --git a/source.list b/source.list
index 48bbf2fc3..412a2c1a4 100644
--- a/source.list
+++ b/source.list
@@ -31,6 +31,7 @@ gfxinit.cpp
ground_vehicle.cpp
heightmap.cpp
highscore.cpp
+hotkeys.cpp
ini.cpp
landscape.cpp
map.cpp
@@ -174,6 +175,7 @@ group_type.h
gui.h
heightmap.h
highscore.h
+hotkeys.h
house.h
house_type.h
industry.h
diff --git a/src/hotkeys.cpp b/src/hotkeys.cpp
new file mode 100644
index 000000000..9e52cbaa0
--- /dev/null
+++ b/src/hotkeys.cpp
@@ -0,0 +1,15 @@
+/* $Id$ */
+
+/*
+ * This file is part of OpenTTD.
+ * OpenTTD 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, version 2.
+ * OpenTTD 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 OpenTTD. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+/** @file hotkeys.cpp Implementation of hotkey related functions */
+
+#include "stdafx.h"
+#include "hotkeys.h"
+
+
diff --git a/src/hotkeys.h b/src/hotkeys.h
new file mode 100644
index 000000000..0b82f0c5f
--- /dev/null
+++ b/src/hotkeys.h
@@ -0,0 +1,125 @@
+/* $Id$ */
+
+/*
+ * This file is part of OpenTTD.
+ * OpenTTD 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, version 2.
+ * OpenTTD 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 OpenTTD. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+/** @file hotkeys.h Hotkey related functions. */
+
+#ifndef HOTKEYS_H
+#define HOTKEYS_H
+
+#include "core/smallvec_type.hpp"
+
+/**
+ * All data for a single hotkey. The name (for saving/loading a configfile),
+ * a list of keycodes and a number to help identifying this hotkey.
+ */
+template<class T>
+struct Hotkey {
+ typedef void (T::*hotkey_callback)(int);
+
+ /**
+ * A wrapper around the callback function. This wrapper is needed because
+ * the size of a pointer to a member function depends on the class
+ * definition. The possible solutions are to either wrap the callback
+ * pointer in a class and dynamically allocate memory for it like we do
+ * now or making all class definitions available in hotkeys.cpp.
+ */
+ struct CallbackWrapper {
+ CallbackWrapper(hotkey_callback callback) :
+ callback(callback)
+ {}
+ hotkey_callback callback;
+ };
+
+ /**
+ * Create a new Hotkey object with a single default keycode.
+ * @param default_keycode The default keycode for this hotkey.
+ * @param name The name of this hotkey.
+ * @param num Number of this hotkey, should be unique within the hotkey list.
+ * @param callback The function to call if the hotkey is pressed.
+ */
+ Hotkey(uint16 default_keycode, const char *name, int num, hotkey_callback callback = NULL) :
+ name(name),
+ num(num)
+ {
+ if (callback == NULL) {
+ this->callback = NULL;
+ } else {
+ this->callback = new CallbackWrapper(callback);
+ }
+ if (default_keycode != 0) *this->keycodes.Append() = default_keycode;
+ }
+
+ /**
+ * Create a new Hotkey object with multiple default keycodes.
+ * @param default_keycodes An array of default keycodes terminated with 0.
+ * @param name The name of this hotkey.
+ * @param num Number of this hotkey, should be unique within the hotkey list.
+ * @param callback The function to call if the hotkey is pressed.
+ */
+ Hotkey(const uint16 *default_keycodes, const char *name, int num, hotkey_callback callback = NULL) :
+ name(name),
+ num(num)
+ {
+ if (callback == NULL) {
+ this->callback = NULL;
+ } else {
+ this->callback = new CallbackWrapper(callback);
+ }
+
+ const uint16 *keycode = default_keycodes;
+ while (*keycode != 0) {
+ this->keycodes.Include(keycode);
+ keycode++;
+ }
+ }
+
+ ~Hotkey()
+ {
+ delete this->callback;
+ }
+
+ /**
+ * Add a keycode to this hotkey, from now that keycode will be matched
+ * in addition to any previously added keycodes.
+ * @param keycode The keycode to add.
+ */
+ void AddKeycode(uint16 keycode)
+ {
+ this->keycodes.Include(keycode);
+ }
+
+ const char *name;
+ int num;
+ SmallVector<uint16, 1> keycodes;
+ CallbackWrapper *callback;
+};
+
+#define HOTKEY_LIST_END(window_class) Hotkey<window_class>((uint16)0, NULL, -1)
+
+/**
+ * Check if a keycode is bound to something.
+ * @param list The list with hotkeys to check
+ * @param keycode The keycode that was pressed
+ * @param w The window-pointer to give to the callback function (if any).
+ * @return The number of the matching hotkey or -1.
+ */
+template<class T>
+int CheckHotkeyMatch(Hotkey<T> *list, uint16 keycode, T *w)
+{
+ while (list->num != -1) {
+ if (list->keycodes.Contains(keycode)) {
+ if (list->callback != NULL) (w->*(list->callback->callback))(-1);
+ return list->num;
+ }
+ list++;
+ }
+ return -1;
+}
+
+#endif /* HOTKEYS_H */