forked from sigatrev/Stepmania-3.95
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ScreenMapControllers.cpp
411 lines (354 loc) · 12.4 KB
/
ScreenMapControllers.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
#include "global.h"
#include "ScreenMapControllers.h"
#include "PrefsManager.h"
#include "ScreenManager.h"
#include "GameConstantsAndTypes.h"
#include "PrefsManager.h"
#include "RageLog.h"
#include "InputMapper.h"
#include "GameManager.h"
#include "GameState.h"
#include "GameSoundManager.h"
#include "ThemeManager.h"
#include "Game.h"
#include "HelpDisplay.h"
#include "ScreenDimensions.h"
#include "ThemeMetric.h"
#include "Command.h"
static const ThemeMetric<apActorCommands> EVEN_LINE_IN ("ScreenMapControllers","EvenLineIn");
static const ThemeMetric<apActorCommands> EVEN_LINE_OUT ("ScreenMapControllers","EvenLineOut");
static const ThemeMetric<apActorCommands> ODD_LINE_IN ("ScreenMapControllers","OddLineIn");
static const ThemeMetric<apActorCommands> ODD_LINE_OUT ("ScreenMapControllers","OddLineOut");
const int FramesToWaitForInput = 2;
// reserve the 3rd slot for hard-coded keys
const int NUM_CHANGABLE_SLOTS = NUM_SHOWN_GAME_TO_DEVICE_SLOTS-1;
const float LINE_START_Y = 64;
const float LINE_GAP_Y = 28;
const float BUTTON_COLUMN_X[NUM_SHOWN_GAME_TO_DEVICE_SLOTS*MAX_GAME_CONTROLLERS] =
{
50, 125, 200, 440, 515, 590
};
REGISTER_SCREEN_CLASS( ScreenMapControllers );
ScreenMapControllers::ScreenMapControllers( CString sClassName ) : ScreenWithMenuElements( sClassName )
{
LOG->Trace( "ScreenMapControllers::ScreenMapControllers()" );
}
void ScreenMapControllers::Init()
{
ScreenWithMenuElements::Init();
#ifdef _XBOX
CStringArray strArray;
CString text("Use joypad to navigate, START to assign, A, B, X or Y to clear, BACK when done.");
strArray.push_back(text);
m_textHelp->SetTips(strArray);
#endif
for( int b=0; b<GAMESTATE->GetCurrentGame()->m_iButtonsPerController; b++ )
{
CString sName = GAMESTATE->GetCurrentGame()->m_szButtonNames[b];
CString sSecondary = GAMEMAN->GetMenuButtonSecondaryFunction( GAMESTATE->GetCurrentGame(), b );
m_textName[b].LoadFromFont( THEME->GetPathF("Common","title") );
m_textName[b].SetXY( SCREEN_CENTER_X, -6 );
m_textName[b].SetText( sName );
m_textName[b].SetZoom( 0.7f );
m_textName[b].SetShadowLength( 2 );
m_Line[b].AddChild( &m_textName[b] );
m_textName2[b].LoadFromFont( THEME->GetPathF("Common","title") );
m_textName2[b].SetXY( SCREEN_CENTER_X, +6 );
m_textName2[b].SetText( sSecondary );
m_textName2[b].SetZoom( 0.5f );
m_textName2[b].SetShadowLength( 2 );
m_Line[b].AddChild( &m_textName2[b] );
for( int p=0; p<MAX_GAME_CONTROLLERS; p++ )
{
for( int s=0; s<NUM_SHOWN_GAME_TO_DEVICE_SLOTS; s++ )
{
m_textMappedTo[p][b][s].LoadFromFont( THEME->GetPathF("ScreenMapControllers","entry") );
m_textMappedTo[p][b][s].SetXY( BUTTON_COLUMN_X[p*NUM_SHOWN_GAME_TO_DEVICE_SLOTS+s], 0 );
m_textMappedTo[p][b][s].SetZoom( 0.5f );
m_textMappedTo[p][b][s].SetShadowLength( 0 );
m_Line[b].AddChild( &m_textMappedTo[p][b][s] );
}
}
m_Line[b].SetY( LINE_START_Y + b*LINE_GAP_Y );
this->AddChild( &m_Line[b] );
m_Line[b].RunCommands( (b%2)? ODD_LINE_IN : EVEN_LINE_IN );
}
m_textError.LoadFromFont( THEME->GetPathF("Common","normal") );
m_textError.SetText( "" );
m_textError.SetXY( SCREEN_CENTER_X, SCREEN_CENTER_Y );
m_textError.SetDiffuse( RageColor(0,1,0,0) );
m_textError.SetZoom( 0.8f );
this->AddChild( &m_textError );
m_iCurController = 0;
m_iCurButton = 0;
m_iCurSlot = 0;
m_iWaitingForPress = 0;
SOUND->PlayMusic( THEME->GetPathS("ScreenMapControllers","music") );
Refresh();
}
ScreenMapControllers::~ScreenMapControllers()
{
LOG->Trace( "ScreenMapControllers::~ScreenMapControllers()" );
}
void ScreenMapControllers::Update( float fDeltaTime )
{
Screen::Update( fDeltaTime );
if( m_iWaitingForPress && m_DeviceIToMap.IsValid() ) // we're going to map an input
{
--m_iWaitingForPress;
if( m_iWaitingForPress )
return; /* keep waiting */
GameInput curGameI( (GameController)m_iCurController,
(GameButton)m_iCurButton );
INPUTMAPPER->SetInputMap( m_DeviceIToMap, curGameI, m_iCurSlot );
INPUTMAPPER->AddDefaultMappingsForCurrentGameIfUnmapped();
// commit to disk after each change
INPUTMAPPER->SaveMappingsToDisk();
Refresh();
}
}
void ScreenMapControllers::DrawPrimitives()
{
Screen::DrawPrimitives();
}
/* Note that this isn't necessarily correct. For example, JOY_LEFT might actually be
* a D-pad and not an axis. All this is actually doing is giving priority to some
* inputs over others; this function is unsuitable for other use. */
static bool IsAxis( const DeviceInput& DeviceI )
{
if( !DeviceI.IsJoystick() )
return false;
static int axes[] =
{
JOY_LEFT, JOY_RIGHT, JOY_UP, JOY_DOWN,
JOY_LEFT_2, JOY_RIGHT_2, JOY_UP_2, JOY_DOWN_2,
JOY_Z_UP, JOY_Z_DOWN,
JOY_ROT_UP, JOY_ROT_DOWN, JOY_ROT_LEFT, JOY_ROT_RIGHT, JOY_ROT_Z_UP, JOY_ROT_Z_DOWN,
JOY_HAT_LEFT, JOY_HAT_RIGHT, JOY_HAT_UP, JOY_HAT_DOWN,
JOY_AUX_1, JOY_AUX_2, JOY_AUX_3, JOY_AUX_4,
-1
};
for( int ax = 0; axes[ax] != -1; ++ax )
if( DeviceI.button == axes[ax] )
return true;
return false;
}
void ScreenMapControllers::Input( const DeviceInput& DeviceI, const InputEventType type, const GameInput &GameI, const MenuInput &MenuI, const StyleInput &StyleI )
{
if( type != IET_FIRST_PRESS && type != IET_SLOW_REPEAT )
return; // ignore
LOG->Trace( "ScreenMapControllers::Input(): device: %d, button: %d",
DeviceI.device, DeviceI.button );
int button = DeviceI.button;
#ifdef _XBOX
if(!m_iWaitingForPress && DeviceI.device == DEVICE_JOY1)
{
// map the xbox controller buttons to the keyboard equivalents
if(DeviceI.button == JOY_HAT_LEFT)
button = KEY_LEFT;
else if(DeviceI.button == JOY_HAT_RIGHT)
button = KEY_RIGHT;
else if(DeviceI.button == JOY_HAT_UP)
button = KEY_UP;
else if(DeviceI.button == JOY_HAT_DOWN)
button = KEY_DOWN;
else if(DeviceI.button == JOY_AUX_1)
button = KEY_ENTER;
else if(DeviceI.button == JOY_AUX_2)
button = KEY_ESC;
else if(DeviceI.button == JOY_1 || DeviceI.button == JOY_2 ||
DeviceI.button == JOY_3 || DeviceI.button == JOY_4)
button = KEY_DEL;
}
#endif
//
// TRICKY: Some adapters map the PlayStation digital d-pad to both axes and
// buttons. We want buttons to be used for any mappings where possible because
// presses of buttons aren't mutually exclusive and presses of axes are (e.g.
// can't read presses of both Left and Right simultaneously). So, when the user
// presses a button, we'll wait until the next Update before adding a mapping so
// that we get a chance to see all input events the user's press of a panel.
// Prefer non-axis events over axis events.
//
if( m_iWaitingForPress )
{
/* Don't allow function keys to be mapped. */
if ( DeviceI.device == DEVICE_KEYBOARD && (DeviceI.button >= KEY_F1 && DeviceI.button <= KEY_F12) )
{
m_textError.SetText( "That key can not be mapped." );
SCREENMAN->PlayInvalidSound();
m_textError.StopTweening();
m_textError.SetDiffuse( RageColor(0,1,0,1) );
m_textError.BeginTweening( 3 );
m_textError.BeginTweening( 1 );
m_textError.SetDiffuse( RageColor(0,1,0,0) );
}
else
{
if( m_DeviceIToMap.IsValid() &&
!IsAxis(m_DeviceIToMap) &&
IsAxis(DeviceI) )
{
LOG->Trace("Ignored input; non-axis event already received");
return; // ignore this press
}
m_DeviceIToMap = DeviceI;
}
}
#ifdef _XBOX
else if( DeviceI.device == DEVICE_JOY1 )
#else
else if( DeviceI.device == DEVICE_KEYBOARD )
#endif
{
switch( button )
{
/* We only advertise space as doing this, but most games
* use either backspace or delete, and I find them more
* intuitive, so allow them, too. -gm */
/* XXX: For some reason that eludes me, this function gets sent an
* KEY_SPACE button press every time the JOY_HAT_UP button is pressed.
* Had to put this in to prevent mappings being erased everytime the user
* pressed up on the joypad. */
case KEY_DEL:
#ifndef _XBOX
case KEY_SPACE:
case KEY_BACK: /* Clear the selected input mapping. */
#endif
{
GameInput curGameI( (GameController)m_iCurController, (GameButton)m_iCurButton );
INPUTMAPPER->ClearFromInputMap( curGameI, m_iCurSlot );
INPUTMAPPER->AddDefaultMappingsForCurrentGameIfUnmapped();
// commit to disk after each change
INPUTMAPPER->SaveMappingsToDisk();
}
break;
case KEY_LEFT: /* Move the selection left, wrapping up. */
if( m_iCurSlot == 0 && m_iCurController == 0 )
break; // can't go left any more
m_iCurSlot--;
if( m_iCurSlot < 0 )
{
m_iCurSlot = NUM_CHANGABLE_SLOTS-1;
m_iCurController--;
}
break;
case KEY_RIGHT: /* Move the selection right, wrapping down. */
if( m_iCurSlot == NUM_CHANGABLE_SLOTS-1 && m_iCurController == MAX_GAME_CONTROLLERS-1 )
break; // can't go right any more
m_iCurSlot++;
if( m_iCurSlot > NUM_CHANGABLE_SLOTS-1 )
{
m_iCurSlot = 0;
m_iCurController++;
}
break;
case KEY_UP: /* Move the selection up. */
if( m_iCurButton == 0 )
break; // can't go up any more
m_iCurButton--;
break;
case KEY_DOWN: /* Move the selection down. */
if( m_iCurButton == GAMESTATE->GetCurrentGame()->m_iButtonsPerController-1 )
break; // can't go down any more
m_iCurButton++;
break;
case KEY_ESC: /* Quit the screen. */
if( !IsTransitioning() )
{
SCREENMAN->PlayStartSound();
INPUTMAPPER->SaveMappingsToDisk(); // save changes
StartTransitioning( SM_GoToNextScreen );
for( int b=0; b<GAMESTATE->GetCurrentGame()->m_iButtonsPerController; b++ )
m_Line[b].RunCommands( (b%2)? ODD_LINE_OUT:EVEN_LINE_OUT );
}
break;
case KEY_ENTER: /* Change the selection. */
case KEY_KP_ENTER:
m_iWaitingForPress = FramesToWaitForInput;
m_DeviceIToMap.MakeInvalid();
break;
}
}
// Screen::Input( DeviceI, type, GameI, MenuI, StyleI ); // default handler
LOG->Trace( "m_iCurSlot: %d m_iCurController: %d m_iCurButton: %d", m_iCurSlot, m_iCurController, m_iCurButton );
Refresh();
}
void ScreenMapControllers::HandleScreenMessage( const ScreenMessage SM )
{
switch( SM )
{
case SM_GoToNextScreen:
SCREENMAN->SetNewScreen( "ScreenOptionsMenu" );
break;
}
}
void ScreenMapControllers::Refresh()
{
for( int p=0; p<MAX_GAME_CONTROLLERS; p++ )
{
for( int b=0; b<GAMESTATE->GetCurrentGame()->m_iButtonsPerController; b++ )
{
for( int s=0; s<NUM_SHOWN_GAME_TO_DEVICE_SLOTS; s++ )
{
bool bSelected = p == m_iCurController && b == m_iCurButton && s == m_iCurSlot;
GameInput cur_gi( (GameController)p, (GameButton)b );
DeviceInput di;
if( INPUTMAPPER->GameToDevice( cur_gi, s, di ) )
m_textMappedTo[p][b][s].SetText( di.toString() );
else
m_textMappedTo[p][b][s].SetText( "-----------" );
// highlight the currently selected pad button
RageColor color;
bool bPulse;
if( bSelected )
{
if( m_iWaitingForPress )
{
color = RageColor(1,0.5,0.5,1); // red
bPulse = true;
}
else
{
color = RageColor(1,1,1,1); // white
bPulse = false;
}
}
else
{
color = RageColor(0.5,0.5,0.5,1); // gray
bPulse = false;
}
m_textMappedTo[p][b][s].SetDiffuse( color );
if( bPulse )
m_textMappedTo[p][b][s].SetEffectPulse( .5f, .5f, .6f );
else
m_textMappedTo[p][b][s].SetEffectNone();
}
}
}
}
/*
* (c) 2001-2004 Chris Danford
* All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, and/or sell copies of the Software, and to permit persons to
* whom the Software is furnished to do so, provided that the above
* copyright notice(s) and this permission notice appear in all copies of
* the Software and that both the above copyright notice(s) and this
* permission notice appear in supporting documentation.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF
* THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR HOLDERS
* INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL INDIRECT
* OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
* OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
* OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
* PERFORMANCE OF THIS SOFTWARE.
*/