X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Finput%2Fkeys.cpp;h=d5687ae2c9e1142a92bc8b6d8b4844a2c0b3a1d6;hb=b99a9eb342d0f6ba5509c6d9f8ab0b0b5d5d2979;hp=4a6df371776a843ab159c9dc4493e92ee9c586f8;hpb=51d4984883ae44002fd33665b6ce3c3249210a90;p=libs%2Fgui.git diff --git a/source/input/keys.cpp b/source/input/keys.cpp index 4a6df37..d5687ae 100644 --- a/source/input/keys.cpp +++ b/source/input/keys.cpp @@ -1,7 +1,8 @@ -#include +#include "keys.h" #include +#include +#include #include -#include "keys.h" using namespace std; @@ -12,21 +13,25 @@ extern unsigned sys_keymap[]; unsigned key_from_sys(unsigned code) { - static bool init_done = false; - static map reverse_map; + struct MappedKey + { + unsigned code; + Key key; + }; - if(!init_done) + static vector reverse_map; + + if(reverse_map.empty()) { for(unsigned i=0; i(i) }); + sort_member(reverse_map, &MappedKey::code); } - map::const_iterator i = reverse_map.find(code); - if(i!=reverse_map.end()) - return i->second; + auto i = lower_bound_member(reverse_map, code, &MappedKey::code); + if(i!=reverse_map.end() && i->code==code) + return i->key; return 0; } @@ -333,5 +338,54 @@ void operator<<(LexicalConverter &conv, Key key) } } +void operator>>(const LexicalConverter &conv, MouseAxis &axis) +{ + if(conv.get()=="X") + axis = MOUSE_X_AXIS; + else if(conv.get()=="Y") + axis = MOUSE_Y_AXIS; + else + throw lexical_error(format("conversion of '%s' to MouseAxis", conv.get())); +} + +void operator<<(LexicalConverter &conv, MouseAxis axis) +{ + switch(axis) + { + case MOUSE_X_AXIS: conv.result("X"); break; + case MOUSE_Y_AXIS: conv.result("Y"); break; + default: conv.result(format("MouseAxis(%#x)", static_cast(axis))); + } +} + +void operator>>(const LexicalConverter &conv, MouseButton &btn) +{ + if(conv.get()=="LEFT") + btn = MOUSE_LEFT; + else if(conv.get()=="MIDDLE") + btn = MOUSE_MIDDLE; + else if(conv.get()=="RIGHT") + btn = MOUSE_RIGHT; + else if(conv.get()=="WHEEL_UP") + btn = MOUSE_WHEEL_UP; + else if(conv.get()=="WHEEL_DOWN") + btn = MOUSE_WHEEL_DOWN; + else + throw lexical_error(format("conversion of '%s' to MouseButton", conv.get())); +} + +void operator<<(LexicalConverter &conv, MouseButton btn) +{ + switch(btn) + { + case MOUSE_LEFT: conv.result("LEFT"); break; + case MOUSE_MIDDLE: conv.result("MIDDLE"); break; + case MOUSE_RIGHT: conv.result("RIGHT"); break; + case MOUSE_WHEEL_UP: conv.result("WHEEL_UP"); break; + case MOUSE_WHEEL_DOWN: conv.result("WHEEL_DOWN"); break; + default: conv.result(format("MouseButton(%#x)", static_cast(btn))); + } +} + } // namespace Input } // namespace Msp