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);
} else if (event.EventType == irr::EET_MOUSE_INPUT_EVENT) {
// Handle mouse events
KeyPress key;
switch (event.MouseInput.Event) {
case EMIE_LMOUSE_PRESSED_DOWN:
key = "KEY_LBUTTON";
keyIsDown.set(key);
keyWasDown.set(key);
keyWasPressed.set(key);
keyIsDown.set(LMBKey);
keyWasDown.set(LMBKey);
keyWasPressed.set(LMBKey);
break;
case EMIE_MMOUSE_PRESSED_DOWN:
key = "KEY_MBUTTON";
keyIsDown.set(key);
keyWasDown.set(key);
keyWasPressed.set(key);
keyIsDown.set(MMBKey);
keyWasDown.set(MMBKey);
keyWasPressed.set(MMBKey);
break;
case EMIE_RMOUSE_PRESSED_DOWN:
key = "KEY_RBUTTON";
keyIsDown.set(key);
keyWasDown.set(key);
keyWasPressed.set(key);
keyIsDown.set(RMBKey);
keyWasDown.set(RMBKey);
keyWasPressed.set(RMBKey);
break;
case EMIE_LMOUSE_LEFT_UP:
key = "KEY_LBUTTON";
keyIsDown.unset(key);
keyWasReleased.set(key);
keyIsDown.unset(LMBKey);
keyWasReleased.set(LMBKey);
break;
case EMIE_MMOUSE_LEFT_UP:
key = "KEY_MBUTTON";
keyIsDown.unset(key);
keyWasReleased.set(key);
keyIsDown.unset(MMBKey);
keyWasReleased.set(MMBKey);
break;
case EMIE_RMOUSE_LEFT_UP:
key = "KEY_RBUTTON";
keyIsDown.unset(key);
keyWasReleased.set(key);
keyIsDown.unset(RMBKey);
keyWasReleased.set(RMBKey);
break;
case EMIE_MOUSE_WHEEL:
mouse_wheel += event.MouseInput.Wheel;
break;
default: break;
default:
break;
}
}

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

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