OpenBarnyard
 
Loading...
Searching...
No Matches
AInputMapManager.cpp
Go to the documentation of this file.
1#include "pch.h"
2#include "AInputMapManager.h"
3#include "Memory/AMemory.h"
4
5//-----------------------------------------------------------------------------
6// Enables memory debugging.
7// Note: Should be the last include!
8//-----------------------------------------------------------------------------
10
12
14
16 : m_pActiveInputMap( TNULL ), m_iNumPushedInputMaps( 0 ), m_oDoodadToNameMap( AMemory::GetAllocator( AMemory::POOL_Misc ) ), m_oKeyMap( AMemory::GetAllocator( AMemory::POOL_Misc ) ), m_oCommandMap( AMemory::GetAllocator( AMemory::POOL_Misc ) ), m_UnkMap( AMemory::GetAllocator( AMemory::POOL_Misc ) )
17{
18 InitMouseDoodads();
19 InitKeyboardDoodads();
20 InitGamepadDoodads();
21 InitCommandMap();
22}
23
25{
26 TString8 controlsFilePath;
27 controlsFilePath.Format( "Data/Options/%s.trb", "Controls_Win" );
28
29 TTRB trb;
30 auto eLoadResult = trb.Load( controlsFilePath );
31
32 if ( eLoadResult != TTRB::ERROR_OK )
33 {
34 return TFALSE;
35 }
36
37 auto properties = PBProperties::LoadFromTRB( trb );
38
39 while ( m_InputMaps.Size() != 0 )
40 {
41 auto pInputMap = m_InputMaps.Back();
42 m_InputMaps.PopBack();
43
44 if ( pInputMap )
45 {
46 delete pInputMap;
47 }
48 }
49
50 for ( auto it = properties->Begin(); it != properties->End(); it++ )
51 {
52 auto pInputMap = new AInputMap( TPString8( it->GetName().GetString() ) );
53
54 pInputMap->LoadFromProperties( *it->GetValue()->GetProperties() );
55 m_InputMaps.PushBack( pInputMap );
56 }
57
58 return TTRUE;
59}
60
62{
63 TASSERT( m_iNumPushedInputMaps < MAX_NUM_INPUT_MAPS );
64 m_iNumPushedInputMaps++;
65
66 if ( m_iNumPushedInputMaps < 32 )
67 {
68 m_apPushedInputMaps[ m_iNumPushedInputMaps - 1 ] = a_pInputMap;
69 }
70
71 m_pActiveInputMap = a_pInputMap;
72}
73
75{
76 TASSERT( m_iNumPushedInputMaps > 0 );
77 m_iNumPushedInputMaps = Toshi::TMath::Max( m_iNumPushedInputMaps - 1, 0 );
78
79 m_pActiveInputMap = ( m_iNumPushedInputMaps != 0 ) ? m_apPushedInputMaps[ m_iNumPushedInputMaps - 1 ] : TNULL;
80 return m_apPushedInputMaps[ m_iNumPushedInputMaps - 1 ];
81}
82
83AInputMap* AInputMapManager::FindMap( const Toshi::TPString8& a_rMapName )
84{
85 for ( auto it = m_InputMaps.Begin(); it != m_InputMaps.End(); it++ )
86 {
87 if ( ( *it )->GetName() == a_rMapName )
88 {
89 return *it;
90 }
91 }
92
93 return TNULL;
94}
95
96TBOOL AInputMapManager::SetMap( const Toshi::TPString8& a_rMapName )
97{
98 auto pMap = FindMap( a_rMapName );
99 if ( !pMap ) return TFALSE;
100
101 m_pActiveInputMap = pMap;
102 return TTRUE;
103}
104
105void AInputMapManager::GetEventCommands( const Toshi::TInputInterface::InputEvent* a_pEvent, AInputCommandArray& a_rCommandArray )
106{
107 a_rCommandArray.Clear();
108
109 if ( m_pActiveInputMap )
110 {
111 auto pCommandMap = &m_pActiveInputMap->GetCommandMap();
112 auto pCommandIndexRes = pCommandMap->Find( a_pEvent->GetDoodad() );
113
114 while ( pCommandMap->IsValid( pCommandIndexRes ) )
115 {
116 auto pCommandRes = m_oCommandMap.Find( pCommandIndexRes->GetSecond() );
117 TBOOL bCommandValid;
118
119 if ( m_oCommandMap.IsValid( pCommandRes ) )
120 {
121 bCommandValid = TTRUE;
122 auto command = pCommandRes->GetSecond();
123
124 if ( !command.IsEventTypeAllowed( a_pEvent->GetEventType() ) )
125 {
126 bCommandValid = TFALSE;
127 }
128 }
129 else
130 {
131 bCommandValid = TFALSE;
132 }
133
134 if ( bCommandValid )
135 {
136 a_rCommandArray.AddCommand( pCommandIndexRes->GetSecond() );
137 }
138
139 // Look if there are any other commands binded to this doodad
140 pCommandIndexRes = pCommandMap->FindNext( pCommandIndexRes, a_pEvent->GetDoodad() );
141 }
142 }
143}
144
145AInputCommand AInputMapManager::GetCommandCode( const Toshi::TPString8& a_rCommandName )
146{
147 for ( auto it = m_oCommandMap.Begin(); it != m_oCommandMap.End(); it++ )
148 {
149 if ( it->GetSecond().Name == a_rCommandName )
150 {
151 return it->GetFirst();
152 }
153 }
154
156}
157
158Toshi::TInputDevice::Doodad AInputMapManager::GetDoodadFromKey( const Toshi::TPString8& a_rKey )
159{
160 for ( auto it = m_oDoodadToNameMap.Begin(); it != m_oDoodadToNameMap.End(); it++ )
161 {
162 if ( it->GetSecond() == a_rKey )
163 {
164 return it->GetFirst();
165 }
166 }
167
168 return -1;
169}
170
171void AInputMapManager::InitMouseDoodads()
172{
173 BindDoodad( 0x30001, TPString8( "Button1" ), 0x582 );
174 BindDoodad( 0x30002, TPString8( "Button2" ), 0x583 );
175 BindDoodad( 0x30003, TPString8( "Button3" ), 0x584 );
176 BindDoodad( 0x30004, TPString8( "Button4" ), 0x585 );
177 BindDoodad( 0x30005, TPString8( "Button5" ), 0x586 );
178 BindDoodad( 0x30006, TPString8( "Button6" ), 0x587 );
179 BindDoodad( 0x30007, TPString8( "Button7" ), 0x588 );
180 BindDoodad( 0x30008, TPString8( "Button8" ), 0x589 );
181 BindDoodad( 0x30009, TPString8( "Wheel Fwd" ), 0x58A );
182 BindDoodad( 0x3000A, TPString8( "Wheel Back" ), 0x58B );
183 BindDoodad( 0x3000B, TPString8( "Cursor Axis" ), 0x58C );
184 BindDoodad( 0x3000C, TPString8( "Wheel Axis" ), 0x58D );
185}
186
187void AInputMapManager::InitKeyboardDoodads()
188{
189 BindDoodad( 0x20001, TPString8( "Virtual Stick" ), 0x506 );
190 BindDoodad( 0x2002e, TPString8( "Left Shift" ), 0x530 );
191 BindDoodad( 0x2003a, TPString8( "Right Shift" ), 0x53c );
192 BindDoodad( 0x20021, TPString8( "Left Control" ), 0x523 );
193 BindDoodad( 0x2006b, TPString8( "Right Control" ), 0x56d );
194 BindDoodad( 0x2003c, TPString8( "Left Alt" ), 0x53e );
195 BindDoodad( 0x2006f, TPString8( "Right Alt" ), 0x571 );
196 BindDoodad( 0x2007b, TPString8( "Left Windows" ), 0x57d );
197 BindDoodad( 0x2007c, TPString8( "Right Windows" ), 0x57e );
198 BindDoodad( 0x20005, TPString8( "Escape" ), 0x507 );
199 BindDoodad( 0x20006, TPString8( "1" ), 0x508 );
200 BindDoodad( 0x20007, TPString8( "2" ), 0x509 );
201 BindDoodad( 0x20008, TPString8( "3" ), 0x50a );
202 BindDoodad( 0x20009, TPString8( "4" ), 0x50b );
203 BindDoodad( 0x2000a, TPString8( "5" ), 0x50c );
204 BindDoodad( 0x2000b, TPString8( "6" ), 0x50d );
205 BindDoodad( 0x2000c, TPString8( "7" ), 0x50e );
206 BindDoodad( 0x2000d, TPString8( "8" ), 0x50f );
207 BindDoodad( 0x2000e, TPString8( "9" ), 0x510 );
208 BindDoodad( 0x2000f, TPString8( "0" ), 0x511 );
209 BindDoodad( 0x20010, TPString8( "Minus" ), 0x512 );
210 BindDoodad( 0x20011, TPString8( "Equals" ), 0x513 );
211 BindDoodad( 0x20012, TPString8( "Backspace" ), 0x514 );
212 BindDoodad( 0x20013, TPString8( "Tab" ), 0x515 );
213 BindDoodad( 0x20014, TPString8( "Q" ), 0x516 );
214 BindDoodad( 0x20015, TPString8( "W" ), 0x517 );
215 BindDoodad( 0x20016, TPString8( "E" ), 0x518 );
216 BindDoodad( 0x20017, TPString8( "R" ), 0x519 );
217 BindDoodad( 0x20018, TPString8( "T" ), 0x51a );
218 BindDoodad( 0x20019, TPString8( "Y" ), 0x51b );
219 BindDoodad( 0x2001a, TPString8( "U" ), 0x51c );
220 BindDoodad( 0x2001b, TPString8( "I" ), 0x51d );
221 BindDoodad( 0x2001c, TPString8( "O" ), 0x51e );
222 BindDoodad( 0x2001d, TPString8( "P" ), 0x51f );
223 BindDoodad( 0x2001e, TPString8( "Left Bracket" ), 0x520 );
224 BindDoodad( 0x2001f, TPString8( "Right Bracket" ), 0x521 );
225 BindDoodad( 0x20020, TPString8( "Return" ), 0x522 );
226 BindDoodad( 0x20022, TPString8( "A" ), 0x524 );
227 BindDoodad( 0x20023, TPString8( "S" ), 0x525 );
228 BindDoodad( 0x20024, TPString8( "D" ), 0x526 );
229 BindDoodad( 0x20025, TPString8( "F" ), 0x527 );
230 BindDoodad( 0x20026, TPString8( "G" ), 0x528 );
231 BindDoodad( 0x20027, TPString8( "H" ), 0x529 );
232 BindDoodad( 0x20028, TPString8( "J" ), 0x52a );
233 BindDoodad( 0x20029, TPString8( "K" ), 0x52b );
234 BindDoodad( 0x2002a, TPString8( "L" ), 0x52c );
235 BindDoodad( 0x2002b, TPString8( "Semicolon" ), 0x52d );
236 BindDoodad( 0x2002c, TPString8( "Apostrophe" ), 0x52e );
237 BindDoodad( 0x2002d, TPString8( "Grave" ), 0x52f );
238 BindDoodad( 0x2002f, TPString8( "Backslash" ), 0x531 );
239 BindDoodad( 0x20030, TPString8( "Z" ), 0x532 );
240 BindDoodad( 0x20031, TPString8( "X" ), 0x533 );
241 BindDoodad( 0x20032, TPString8( "C" ), 0x534 );
242 BindDoodad( 0x20033, TPString8( "V" ), 0x535 );
243 BindDoodad( 0x20034, TPString8( "B" ), 0x536 );
244 BindDoodad( 0x20035, TPString8( "N" ), 0x537 );
245 BindDoodad( 0x20036, TPString8( "M" ), 0x538 );
246 BindDoodad( 0x20037, TPString8( "Comma" ), 0x539 );
247 BindDoodad( 0x20038, TPString8( "Period" ), 0x53a );
248 BindDoodad( 0x20039, TPString8( "Slash" ), 0x53b );
249 BindDoodad( 0x2003b, TPString8( "Multiply" ), 0x53d );
250 BindDoodad( 0x2003d, TPString8( "Space" ), 0x53f );
251 BindDoodad( 0x2003e, TPString8( "Capslock" ), 0x540 );
252 BindDoodad( 0x2003f, TPString8( "F1" ), 0x541 );
253 BindDoodad( 0x20040, TPString8( "F2" ), 0x542 );
254 BindDoodad( 0x20041, TPString8( "F3" ), 0x543 );
255 BindDoodad( 0x20042, TPString8( "F4" ), 0x544 );
256 BindDoodad( 0x20043, TPString8( "F5" ), 0x545 );
257 BindDoodad( 0x20044, TPString8( "F6" ), 0x546 );
258 BindDoodad( 0x20045, TPString8( "F7" ), 0x547 );
259 BindDoodad( 0x20046, TPString8( "F8" ), 0x548 );
260 BindDoodad( 0x20047, TPString8( "F9" ), 0x549 );
261 BindDoodad( 0x20048, TPString8( "F10" ), 0x54a );
262 BindDoodad( 0x20049, TPString8( "NumLock" ), 0x54b );
263 BindDoodad( 0x2004a, TPString8( "ScrollLock" ), 0x54c );
264 BindDoodad( 0x2004b, TPString8( "Numpad 7" ), 0x54d );
265 BindDoodad( 0x2004c, TPString8( "Numpad 8" ), 0x54e );
266 BindDoodad( 0x2004d, TPString8( "Numpad 9" ), 0x54f );
267 BindDoodad( 0x2004e, TPString8( "Subtract" ), 0x550 );
268 BindDoodad( 0x2004f, TPString8( "Numpad 4" ), 0x551 );
269 BindDoodad( 0x20050, TPString8( "Numpad 5" ), 0x552 );
270 BindDoodad( 0x20051, TPString8( "Numpad 6" ), 0x553 );
271 BindDoodad( 0x20052, TPString8( "Add" ), 0x554 );
272 BindDoodad( 0x20053, TPString8( "Numpad 1" ), 0x555 );
273 BindDoodad( 0x20054, TPString8( "Numpad 2" ), 0x556 );
274 BindDoodad( 0x20055, TPString8( "Numpad 3" ), 0x557 );
275 BindDoodad( 0x20056, TPString8( "Numpad 0" ), 0x558 );
276 BindDoodad( 0x20057, TPString8( "Decimal" ), 0x559 );
277 BindDoodad( 0x20058, TPString8( "F11" ), 0x55a );
278 BindDoodad( 0x20059, TPString8( "F12" ), 0x55b );
279 BindDoodad( 0x2005a, TPString8( "F13" ), 0x55c );
280 BindDoodad( 0x2005b, TPString8( "F14" ), 0x55d );
281 BindDoodad( 0x2005c, TPString8( "F15" ), 0x55e );
282 BindDoodad( 0x2005d, TPString8( "Kana" ), 0x55f );
283 BindDoodad( 0x2005e, TPString8( "Convert" ), 0x560 );
284 BindDoodad( 0x2005f, TPString8( "No Convert" ), 0x561 );
285 BindDoodad( 0x20060, TPString8( "Yen" ), 0x562 );
286 BindDoodad( 0x20061, TPString8( "Numpad Equals" ), 0x563 );
287 BindDoodad( 0x20062, TPString8( "Circumflex" ), 0x564 );
288 BindDoodad( 0x20063, TPString8( "At" ), 0x565 );
289 BindDoodad( 0x20064, TPString8( "Colon" ), 0x566 );
290 BindDoodad( 0x20065, TPString8( "Underline" ), 0x567 );
291 BindDoodad( 0x20066, TPString8( "Kanji" ), 0x568 );
292 BindDoodad( 0x20067, TPString8( "Stop" ), 0x569 );
293 BindDoodad( 0x20068, TPString8( "Ax" ), 0x56a );
294 BindDoodad( 0x20069, TPString8( "Unlabeled" ), 0x56b );
295 BindDoodad( 0x2006a, TPString8( "Numpad Enter" ), 0x56c );
296 BindDoodad( 0x2006c, TPString8( "Numpad Comma" ), 0x56e );
297 BindDoodad( 0x2006d, TPString8( "Divide" ), 0x56f );
298 BindDoodad( 0x2006e, TPString8( "SysRq" ), 0x570 );
299 BindDoodad( 0x20071, TPString8( "Pause" ), 0x572 );
300 BindDoodad( 0x20070, TPString8( "Home" ), 0x573 );
301 BindDoodad( 0x20072, TPString8( "Up" ), 0x574 );
302 BindDoodad( 0x20073, TPString8( "Prior" ), 0x575 );
303 BindDoodad( 0x20074, TPString8( "Left" ), 0x576 );
304 BindDoodad( 0x20075, TPString8( "Right" ), 0x577 );
305 BindDoodad( 0x20076, TPString8( "End" ), 0x578 );
306 BindDoodad( 0x20077, TPString8( "Down" ), 0x579 );
307 BindDoodad( 0x20078, TPString8( "Next" ), 0x57a );
308 BindDoodad( 0x20079, TPString8( "Insert" ), 0x57b );
309 BindDoodad( 0x2007a, TPString8( "Delete" ), 0x57c );
310 BindDoodad( 0x2007d, TPString8( "Apps" ), 0x57f );
311 BindDoodad( 0x2007e, TPString8( "Power" ), 0x580 );
312 BindDoodad( 0x2007f, TPString8( "Sleep" ), 0x581 );
313}
314
315void AInputMapManager::InitGamepadDoodads()
316{
317 BindDoodad( 0x10001, TPString8( "DPadUp" ), 0x58e );
318 BindDoodad( 0x10002, TPString8( "DPadRight" ), 0x58f );
319 BindDoodad( 0x10003, TPString8( "DPadDown" ), 0x590 );
320 BindDoodad( 0x10004, TPString8( "DPadLeft" ), 0x591 );
321 BindDoodad( 0x10005, TPString8( "Select" ), 0x592 );
322 BindDoodad( 0x10006, TPString8( "Start" ), 0x593 );
323 BindDoodad( 0x10007, TPString8( "Left Stick Click" ), 0x594 );
324 BindDoodad( 0x10008, TPString8( "Right Stick Click" ), 0x595 );
325 BindDoodad( 0x10009, TPString8( "Context1" ), 0x596 );
326 BindDoodad( 0x1000a, TPString8( "Context2" ), 0x597 );
327 BindDoodad( 0x1000b, TPString8( "Context3" ), 0x598 );
328 BindDoodad( 0x1000c, TPString8( "Context4" ), 0x599 );
329 BindDoodad( 0x1000d, TPString8( "L1" ), 0x59a );
330 BindDoodad( 0x1000e, TPString8( "L2" ), 0x59b );
331 BindDoodad( 0x1000f, TPString8( "R1" ), 0x59c );
332 BindDoodad( 0x10010, TPString8( "R2" ), 0x59d );
333 BindDoodad( 0x10011, TPString8( "Left Stick Up" ), 0x59e );
334 BindDoodad( 0x10012, TPString8( "Left Stick Right" ), 0x59f );
335 BindDoodad( 0x10013, TPString8( "Left Stick Down" ), 0x5a0 );
336 BindDoodad( 0x10014, TPString8( "Left Stick Left" ), 0x5a1 );
337 BindDoodad( 0x10015, TPString8( "Right Stick Up" ), 0x5a2 );
338 BindDoodad( 0x10016, TPString8( "Right Stick Right" ), 0x5a3 );
339 BindDoodad( 0x10017, TPString8( "Right Stick Down" ), 0x5a4 );
340 BindDoodad( 0x10018, TPString8( "Right Stick Left" ), 0x5a5 );
341 BindDoodad( 0x1001b, TPString8( "Virtual DPad Stick" ), 0x5a8 );
342 BindDoodad( 0x10019, TPString8( "Left Stick" ), 0x5a6 );
343 BindDoodad( 0x1001a, TPString8( "Right Stick" ), 0x5a7 );
344}
345
346void AInputMapManager::InitCommandMap()
347{
348#define ADD_COMMAND( NAME, UNK, EVENT_TYPE ) m_oCommandMap.Insert( AInputCommand_##NAME, { TPString8( #NAME ), UNK, EVENT_TYPE } )
349#define ADD_COMMAND_EX( COMMAND, NAME, UNK, EVENT_TYPE ) m_oCommandMap.Insert( COMMAND, { TPString8( NAME ), UNK, EVENT_TYPE } )
350
351 ADD_COMMAND( Unknown, -1, 0 );
352 ADD_COMMAND_EX( AInputCommand_Empty, Toshi::TPString8(), -1, 0 );
432 ADD_COMMAND_EX( AInputCommand_CUSTOM_COMMAND, "CUSTOM COMMAND", -1, 0 );
433}
434
435void AInputMapManager::BindDoodad( Toshi::TInputDevice::Doodad a_iDoodad, const Toshi::TPString8& a_ButtonName, ActionId a_uiAction )
436{
437 m_oDoodadToNameMap.Insert( a_iDoodad, a_ButtonName );
438 m_oKeyMap.Insert( a_iDoodad, a_uiAction );
439}
#define TASSERT(X,...)
Definition Defines.h:138
#define TOSHI_NAMESPACE_USING
Definition Defines.h:46
#define TDEFINE_CLASS(...)
Definition TObject.h:120
#define TNULL
Definition Typedefs.h:23
#define TFALSE
Definition Typedefs.h:24
#define TTRUE
Definition Typedefs.h:25
bool TBOOL
Definition Typedefs.h:6
AInputCommand
Definition AInputMap.h:12
@ AInputCommand_CUSTOM_COMMAND
Definition AInputMap.h:93
@ AInputCommand_Empty
Definition AInputMap.h:14
@ AInputCommand_Unknown
Definition AInputMap.h:13
#define ADD_COMMAND_EX(COMMAND, NAME, UNK, EVENT_TYPE)
#define ADD_COMMAND(NAME, UNK, EVENT_TYPE)
Definition TTRB.h:253
ERROR Load(const TCHAR *a_szFilePath, TUINT32 a_uiUnknown=0)
Definition TTRB.cpp:38
@ ERROR_OK
Definition TTRB.h:258
TString8 & Format(const TCHAR *a_pcFormat,...)
Definition TString8.cpp:176
void AddCommand(AInputCommand a_eCommand)
AInputMap * PopInputMap()
void PushInputMap(AInputMap *a_pInputMap)
void GetEventCommands(const Toshi::TInputInterface::InputEvent *a_pEvent, AInputCommandArray &a_rCommandArray)
static constexpr TUINT MAX_NUM_INPUT_MAPS
AInputCommand GetCommandCode(const Toshi::TPString8 &a_rCommandName)
Toshi::TInputDevice::Doodad GetDoodadFromKey(const Toshi::TPString8 &a_rKey)
AInputMap * FindMap(const Toshi::TPString8 &a_rMapName)
TBOOL SetMap(const Toshi::TPString8 &a_rMapName)