Reduce needless copying of KeyPress

This commit is contained in:
sfan5 2024-04-20 18:52:52 +02:00
parent e7f6e7d7b6
commit e39e47b21f
3 changed files with 38 additions and 34 deletions

@ -149,45 +149,39 @@ bool MyEventReceiver::OnEvent(const SEvent &event)
return joystick && joystick->handleEvent(event.JoystickEvent); return joystick && joystick->handleEvent(event.JoystickEvent);
} else if (event.EventType == irr::EET_MOUSE_INPUT_EVENT) { } else if (event.EventType == irr::EET_MOUSE_INPUT_EVENT) {
// Handle mouse events // Handle mouse events
KeyPress key;
switch (event.MouseInput.Event) { switch (event.MouseInput.Event) {
case EMIE_LMOUSE_PRESSED_DOWN: case EMIE_LMOUSE_PRESSED_DOWN:
key = "KEY_LBUTTON"; keyIsDown.set(LMBKey);
keyIsDown.set(key); keyWasDown.set(LMBKey);
keyWasDown.set(key); keyWasPressed.set(LMBKey);
keyWasPressed.set(key);
break; break;
case EMIE_MMOUSE_PRESSED_DOWN: case EMIE_MMOUSE_PRESSED_DOWN:
key = "KEY_MBUTTON"; keyIsDown.set(MMBKey);
keyIsDown.set(key); keyWasDown.set(MMBKey);
keyWasDown.set(key); keyWasPressed.set(MMBKey);
keyWasPressed.set(key);
break; break;
case EMIE_RMOUSE_PRESSED_DOWN: case EMIE_RMOUSE_PRESSED_DOWN:
key = "KEY_RBUTTON"; keyIsDown.set(RMBKey);
keyIsDown.set(key); keyWasDown.set(RMBKey);
keyWasDown.set(key); keyWasPressed.set(RMBKey);
keyWasPressed.set(key);
break; break;
case EMIE_LMOUSE_LEFT_UP: case EMIE_LMOUSE_LEFT_UP:
key = "KEY_LBUTTON"; keyIsDown.unset(LMBKey);
keyIsDown.unset(key); keyWasReleased.set(LMBKey);
keyWasReleased.set(key);
break; break;
case EMIE_MMOUSE_LEFT_UP: case EMIE_MMOUSE_LEFT_UP:
key = "KEY_MBUTTON"; keyIsDown.unset(MMBKey);
keyIsDown.unset(key); keyWasReleased.set(MMBKey);
keyWasReleased.set(key);
break; break;
case EMIE_RMOUSE_LEFT_UP: case EMIE_RMOUSE_LEFT_UP:
key = "KEY_RBUTTON"; keyIsDown.unset(RMBKey);
keyIsDown.unset(key); keyWasReleased.set(RMBKey);
keyWasReleased.set(key);
break; break;
case EMIE_MOUSE_WHEEL: case EMIE_MOUSE_WHEEL:
mouse_wheel += event.MouseInput.Wheel; mouse_wheel += event.MouseInput.Wheel;
break; break;
default: break; default:
break;
} }
} }

@ -239,7 +239,7 @@ static const struct table_key table[] = {
#undef N_ #undef N_
struct table_key lookup_keyname(const char *name) static const table_key &lookup_keyname(const char *name)
{ {
for (const auto &table_key : table) { for (const auto &table_key : table) {
if (strcmp(table_key.Name, name) == 0) if (strcmp(table_key.Name, name) == 0)
@ -249,7 +249,7 @@ struct table_key lookup_keyname(const char *name)
throw UnknownKeycode(name); throw UnknownKeycode(name);
} }
struct table_key lookup_keykey(irr::EKEY_CODE key) static const table_key &lookup_keykey(irr::EKEY_CODE key)
{ {
for (const auto &table_key : table) { for (const auto &table_key : table) {
if (table_key.Key == key) if (table_key.Key == key)
@ -261,7 +261,7 @@ struct table_key lookup_keykey(irr::EKEY_CODE key)
throw UnknownKeycode(os.str().c_str()); throw UnknownKeycode(os.str().c_str());
} }
struct table_key lookup_keychar(wchar_t Char) static const table_key &lookup_keychar(wchar_t Char)
{ {
for (const auto &table_key : table) { for (const auto &table_key : table) {
if (table_key.Char == Char) if (table_key.Char == Char)
@ -287,7 +287,7 @@ KeyPress::KeyPress(const char *name)
int chars_read = mbtowc(&Char, name, 1); int chars_read = mbtowc(&Char, name, 1);
FATAL_ERROR_IF(chars_read != 1, "Unexpected multibyte character"); FATAL_ERROR_IF(chars_read != 1, "Unexpected multibyte character");
try { try {
struct table_key k = lookup_keychar(Char); auto &k = lookup_keychar(Char);
m_name = k.Name; m_name = k.Name;
Key = k.Key; Key = k.Key;
return; return;
@ -296,7 +296,7 @@ KeyPress::KeyPress(const char *name)
// Lookup by name // Lookup by name
m_name = name; m_name = name;
try { try {
struct table_key k = lookup_keyname(name); auto &k = lookup_keyname(name);
Key = k.Key; Key = k.Key;
Char = k.Char; Char = k.Char;
return; return;
@ -350,6 +350,10 @@ const char *KeyPress::name() const
const KeyPress EscapeKey("KEY_ESCAPE"); const KeyPress EscapeKey("KEY_ESCAPE");
const KeyPress CancelKey("KEY_CANCEL"); const KeyPress CancelKey("KEY_CANCEL");
const KeyPress LMBKey("KEY_LBUTTON");
const KeyPress MMBKey("KEY_MBUTTON");
const KeyPress RMBKey("KEY_RBUTTON");
/* /*
Key config Key config
*/ */
@ -357,15 +361,15 @@ const KeyPress CancelKey("KEY_CANCEL");
// A simple cache for quicker lookup // A simple cache for quicker lookup
static std::unordered_map<std::string, KeyPress> g_key_setting_cache; static std::unordered_map<std::string, KeyPress> g_key_setting_cache;
KeyPress getKeySetting(const char *settingname) const KeyPress &getKeySetting(const char *settingname)
{ {
auto n = g_key_setting_cache.find(settingname); auto n = g_key_setting_cache.find(settingname);
if (n != g_key_setting_cache.end()) if (n != g_key_setting_cache.end())
return n->second; return n->second;
KeyPress k(g_settings->get(settingname).c_str()); auto &ref = g_key_setting_cache[settingname];
g_key_setting_cache[settingname] = k; ref = g_settings->get(settingname).c_str();
return k; return ref;
} }
void clearKeyCache() void clearKeyCache()

@ -21,7 +21,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "exceptions.h" #include "exceptions.h"
#include "irrlichttypes.h" #include "irrlichttypes.h"
#include "Keycodes.h" #include <Keycodes.h>
#include <IEventReceiver.h> #include <IEventReceiver.h>
#include <string> #include <string>
@ -63,11 +63,17 @@ protected:
std::string m_name = ""; std::string m_name = "";
}; };
// Global defines for convenience
extern const KeyPress EscapeKey; extern const KeyPress EscapeKey;
extern const KeyPress CancelKey; extern const KeyPress CancelKey;
extern const KeyPress LMBKey;
extern const KeyPress MMBKey; // Middle Mouse Button
extern const KeyPress RMBKey;
// Key configuration getter // Key configuration getter
KeyPress getKeySetting(const char *settingname); const KeyPress &getKeySetting(const char *settingname);
// Clear fast lookup cache // Clear fast lookup cache
void clearKeyCache(); void clearKeyCache();