1//************************************ bs::framework - Copyright 2018 Marko Pintera **************************************//
2//*********** Licensed under the MIT license. See LICENSE.md for full terms. This notice is not to be removed. ***********//
3#include "GUI/BsShortcutKey.h"
4#include "Platform/BsPlatform.h"
5#include "Localization/BsHEString.h"
6#include "Localization/BsHString.h"
7
8namespace bs
9{
10 const ShortcutKey ShortcutKey::NONE = ShortcutKey();
11
12 size_t ShortcutKey::Hash::operator()(const ShortcutKey& x) const
13 {
14 size_t seed = 0;
15 bs_hash_combine(seed, (UINT32)x.button);
16 bs_hash_combine(seed, (UINT32)x.modifier);
17
18 return seed;
19 }
20
21 bool ShortcutKey::Equals::operator()(const ShortcutKey& a, const ShortcutKey& b) const
22 {
23 return a.button == b.button && a.modifier == b.modifier;
24 }
25
26 ShortcutKey::ShortcutKey(ButtonModifier modifier, ButtonCode code)
27 : modifier(modifier), button(code)
28 { }
29
30 String ShortcutKey::getName() const
31 {
32 static const String MODIFIER_TO_NAME[8] =
33 {
34 u8"", u8"Shift + ", u8"Ctrl + ", u8"Shift + Ctrl + ",
35 u8"Alt + ", u8"Shift + Alt + ", u8"Ctrl + Alt + ",
36 u8"Shift + Ctrl + Alt + "
37 };
38
39 static const UnorderedMap<ButtonCode, HEString> FUNCTION_KEY_TO_NAME =
40 {
41 {BC_ESCAPE, HEString(u8"Escape")},
42 {BC_BACK, HEString(u8"Backspace")},
43 {BC_TAB, HEString(u8"Tab")},
44 {BC_RETURN, HEString(u8"Return")},
45 {BC_SPACE, HEString(u8"Space")},
46 {BC_CAPITAL, HEString(u8"Caps Lock")},
47 {BC_F1, HEString(u8"F1")},
48 {BC_F2, HEString(u8"F2")},
49 {BC_F3, HEString(u8"F3")},
50 {BC_F4, HEString(u8"F4")},
51 {BC_F5, HEString(u8"F5")},
52 {BC_F6, HEString(u8"F6")},
53 {BC_F7, HEString(u8"F7")},
54 {BC_F8, HEString(u8"F8")},
55 {BC_F9, HEString(u8"F9")},
56 {BC_F10, HEString(u8"F10")},
57 {BC_F11, HEString(u8"F11")},
58 {BC_F12, HEString(u8"F12")},
59 {BC_DELETE, HEString(u8"Delete")},
60 {BC_INSERT, HEString(u8"Insert")},
61 {BC_UP, HEString(u8"Up")},
62 {BC_LEFT, HEString(u8"Left")},
63 {BC_RIGHT, HEString(u8"Right")},
64 {BC_DOWN, HEString(u8"Down")},
65 {BC_PGUP, HEString(u8"Page Up")},
66 {BC_PGDOWN, HEString(u8"Page Down")},
67 {BC_END, HEString(u8"End")},
68 {BC_HOME, HEString(u8"Home")},
69 {BC_PAUSE, HEString(u8"Pause")},
70 };
71
72 if (button == BC_UNASSIGNED)
73 return u8"";
74
75 String charStr;
76
77 auto iterFind = FUNCTION_KEY_TO_NAME.find(button);
78 if (iterFind != FUNCTION_KEY_TO_NAME.end())
79 {
80 charStr = ((HString)iterFind->second);
81 }
82 else
83 {
84 charStr = Platform::keyCodeToUnicode((UINT32)button);
85 StringUtil::toUpperCase(charStr);
86 }
87
88 return MODIFIER_TO_NAME[(UINT32)modifier] + charStr;
89 }
90}