Nebula
Toggle main menu visibility
Loading...
Searching...
No Matches
modifier.h
Go to the documentation of this file.
1
#pragma once
2
//------------------------------------------------------------------------------
11
//------------------------------------------------------------------------------
12
#include "
core/types.h
"
13
#include "
input/key.h
"
14
#include "
util/string.h
"
15
16
namespace
Input
17
{
18
namespace
Modifier
19
{
20
21
using
Code
= uint32_t;
22
23
constexpr
Code
NoModifier
= 0;
24
25
constexpr
Code
LeftControlModifier
= 1 << 0;
26
constexpr
Code
RightControlModifier
= 1 << 1;
27
constexpr
Code
LeftShiftModifier
= 1 << 2;
28
constexpr
Code
RightShiftModifier
= 1 << 3;
29
constexpr
Code
LeftAltModifier
= 1 << 4;
30
constexpr
Code
RightAltModifier
= 1 << 5;
31
constexpr
Code
LeftSuperModifier
= 1 << 6;
32
constexpr
Code
RightSuperModifier
= 1 << 7;
33
34
constexpr
Code
ControlModifier
=
LeftControlModifier
|
RightControlModifier
;
35
constexpr
Code
ShiftModifier
=
LeftShiftModifier
|
RightShiftModifier
;
36
constexpr
Code
AltModifier
=
LeftAltModifier
|
RightAltModifier
;
37
constexpr
Code
SuperModifier
=
LeftSuperModifier
|
RightSuperModifier
;
38
39
constexpr
Code
GroupMasks
[] = {
ControlModifier
,
ShiftModifier
,
AltModifier
,
SuperModifier
};
40
constexpr
const
char
*
GroupNames
[] = {
"ctrl"
,
"shift"
,
"alt"
,
"super"
};
41
constexpr
IndexT
NumGroups
= 4;
42
43
constexpr
Key::Code
Keys
[] = {
44
Key::LeftControl
,
Key::RightControl
,
45
Key::LeftShift
,
Key::RightShift
,
46
Key::LeftMenu
,
Key::RightMenu
,
47
Key::LeftWindows
,
Key::RightWindows
,
48
Key::Control
,
Key::Shift
,
Key::Menu
,
49
};
50
constexpr
IndexT
NumKeys
= 11;
51
52
//------------------------------------------------------------------------------
56
inline
Code
57
FromKey
(
Key::Code
key)
58
{
59
switch
(key)
60
{
61
case
Key::LeftControl
:
return
LeftControlModifier
;
62
case
Key::RightControl
:
return
RightControlModifier
;
63
case
Key::LeftShift
:
return
LeftShiftModifier
;
64
case
Key::RightShift
:
return
RightShiftModifier
;
65
case
Key::LeftMenu
:
return
LeftAltModifier
;
66
case
Key::RightMenu
:
return
RightAltModifier
;
67
case
Key::LeftWindows
:
return
LeftSuperModifier
;
68
case
Key::RightWindows
:
return
RightSuperModifier
;
69
70
case
Key::Control
:
return
ControlModifier
;
71
case
Key::Shift
:
return
ShiftModifier
;
72
case
Key::Menu
:
return
AltModifier
;
73
74
default
:
75
return
NoModifier
;
76
}
77
}
78
79
//------------------------------------------------------------------------------
83
inline
bool
84
IsModifier
(
Key::Code
key)
85
{
86
return
FromKey
(key) !=
NoModifier
;
87
}
88
89
//------------------------------------------------------------------------------
93
inline
Code
94
Logical
(
Code
bit)
95
{
96
for
(
IndexT
i = 0; i <
NumGroups
; i++)
97
{
98
if
(bit &
GroupMasks
[i])
99
return
GroupMasks
[i];
100
}
101
return
NoModifier
;
102
}
103
104
//------------------------------------------------------------------------------
108
inline
bool
109
Matches
(
Code
required,
Code
held)
110
{
111
for
(
IndexT
i = 0; i <
NumGroups
; i++)
112
{
113
const
Code
want = required &
GroupMasks
[i];
114
if
(want == 0)
115
continue
;
116
117
if
(want ==
GroupMasks
[i])
118
{
119
if
((held &
GroupMasks
[i]) == 0)
120
return
false
;
121
}
122
else
if
((held & want) != want)
123
return
false
;
124
}
125
return
true
;
126
}
127
128
//------------------------------------------------------------------------------
132
inline
IndexT
133
Count
(
Code
required)
134
{
135
IndexT
count = 0;
136
for
(
IndexT
i = 0; i <
NumGroups
; i++)
137
{
138
if
(required &
GroupMasks
[i])
139
count++;
140
}
141
return
count;
142
}
143
145
Util::String
ToString
(
Code
mods);
147
Code
FromString
(
const
Util::String
& token);
148
149
}
// namespace Modifier
150
}
// namespace Input
151
//------------------------------------------------------------------------------
Input::Key::Code
Code
key codes
Definition
key.h:25
Input::Key::RightShift
@ RightShift
Definition
key.h:103
Input::Key::LeftMenu
@ LeftMenu
Definition
key.h:106
Input::Key::RightControl
@ RightControl
Definition
key.h:105
Input::Key::Control
@ Control
Definition
key.h:31
Input::Key::LeftShift
@ LeftShift
Definition
key.h:102
Input::Key::Shift
@ Shift
Definition
key.h:30
Input::Key::RightWindows
@ RightWindows
Definition
key.h:57
Input::Key::LeftWindows
@ LeftWindows
Definition
key.h:56
Input::Key::Menu
@ Menu
Definition
key.h:32
Input::Key::RightMenu
@ RightMenu
Definition
key.h:107
Input::Key::LeftControl
@ LeftControl
Definition
key.h:104
Util::String
Nebula's universal string class.
Definition
string.h:50
key.h
Input::Modifier
The modifier keys as a bitmask.
Definition
modifier.cc:11
Input::Modifier::Keys
constexpr Key::Code Keys[]
Definition
modifier.h:43
Input::Modifier::ControlModifier
constexpr Code ControlModifier
Definition
modifier.h:34
Input::Modifier::GroupNames
constexpr const char * GroupNames[]
Definition
modifier.h:40
Input::Modifier::SuperModifier
constexpr Code SuperModifier
Definition
modifier.h:37
Input::Modifier::IsModifier
bool IsModifier(Key::Code key)
Returns true when the key code is one of the modifier keys.
Definition
modifier.h:84
Input::Modifier::LeftAltModifier
constexpr Code LeftAltModifier
Definition
modifier.h:29
Input::Modifier::FromString
Code FromString(const Util::String &token)
read one combo token as a modifier. Returns NoModifier when the token is not a modifier
Definition
modifier.cc:38
Input::Modifier::GroupMasks
constexpr Code GroupMasks[]
Definition
modifier.h:39
Input::Modifier::Count
IndexT Count(Code required)
The number of groups a requirement has.
Definition
modifier.h:133
Input::Modifier::LeftShiftModifier
constexpr Code LeftShiftModifier
Definition
modifier.h:27
Input::Modifier::Code
uint32_t Code
Definition
modifier.h:21
Input::Modifier::LeftControlModifier
constexpr Code LeftControlModifier
Definition
modifier.h:25
Input::Modifier::ToString
Util::String ToString(Code mods)
The modifiers as the combo string writes them: the groups, in the order a combo is written in.
Definition
modifier.cc:19
Input::Modifier::RightAltModifier
constexpr Code RightAltModifier
Definition
modifier.h:30
Input::Modifier::NumKeys
constexpr IndexT NumKeys
Definition
modifier.h:50
Input::Modifier::LeftSuperModifier
constexpr Code LeftSuperModifier
Definition
modifier.h:31
Input::Modifier::Logical
Code Logical(Code bit)
The group a bit belongs to.
Definition
modifier.h:94
Input::Modifier::FromKey
Code FromKey(Key::Code key)
The bit a key code has, or NoModifier when the key is not a modifier key.
Definition
modifier.h:57
Input::Modifier::AltModifier
constexpr Code AltModifier
Definition
modifier.h:36
Input::Modifier::RightSuperModifier
constexpr Code RightSuperModifier
Definition
modifier.h:32
Input::Modifier::RightControlModifier
constexpr Code RightControlModifier
Definition
modifier.h:26
Input::Modifier::NumGroups
constexpr IndexT NumGroups
Definition
modifier.h:41
Input::Modifier::NoModifier
constexpr Code NoModifier
Definition
modifier.h:23
Input::Modifier::Matches
bool Matches(Code required, Code held)
Return true when the held bits satisfy the requirement.
Definition
modifier.h:109
Input::Modifier::RightShiftModifier
constexpr Code RightShiftModifier
Definition
modifier.h:28
Input::Modifier::ShiftModifier
constexpr Code ShiftModifier
Definition
modifier.h:35
Input
FIXME!
string.h
types.h
IndexT
int IndexT
Definition
types.h:49
code
input
input
modifier.h
Generated on
for Nebula. Dark theme by
Tilen Majerle
. All rights reserved.