@@ -56,32 +56,20 @@ public BsnesControllers(BsnesCore.SnesSyncSettings ss)
56
56
Definition . MakeImmutable ( ) ;
57
57
}
58
58
59
- public void CoreInputPoll ( IController controller )
59
+ public short CoreInputPoll ( IController controller , int port , int index , int id )
60
60
{
61
- // i hope this is correct lol
62
- for ( int i = 0 ; i < 2 ; i ++ )
63
- {
64
- _ports [ i ] . UpdateState ( _mergers [ i ] . UnMerge ( controller ) ) ;
65
- }
66
- }
67
-
68
- public short CoreInputState ( int port , int index , int id )
69
- {
70
- return _ports [ port ] . GetState ( index , id ) ;
61
+ return _ports [ port ] . GetState ( _mergers [ port ] . UnMerge ( controller ) , index , id ) ;
71
62
}
72
63
}
73
64
74
65
public interface IBsnesController
75
66
{
76
- // Updates the internal state; gets called once per frame from the core
77
- void UpdateState ( IController controller ) ;
78
-
79
67
/// <summary>
80
- /// Returns the internal state ; gets called potentially many times per frame
68
+ /// Corresponds to an InputPoll call from the core ; gets called potentially many times per frame
81
69
/// </summary>
82
70
/// <param name="index">bsnes specific value, sometimes multitap number</param>
83
71
/// <param name="id">bsnes specific value, sometimes button number</param>
84
- short GetState ( int index , int id ) ;
72
+ short GetState ( IController controller , int index , int id ) ;
85
73
86
74
ControllerDefinition Definition { get ; }
87
75
}
@@ -92,15 +80,11 @@ internal class BsnesUnpluggedController : IBsnesController
92
80
93
81
public ControllerDefinition Definition => _definition ;
94
82
95
- public void UpdateState ( IController controller ) { }
96
-
97
- public short GetState ( int index , int id ) => 0 ;
83
+ public short GetState ( IController controller , int index , int id ) => 0 ;
98
84
}
99
85
100
86
internal class BsnesController : IBsnesController
101
87
{
102
- private readonly bool [ ] _state = new bool [ 12 ] ;
103
-
104
88
private static readonly string [ ] Buttons =
105
89
{
106
90
"0Up" , "0Down" , "0Left" , "0Right" , "0B" , "0A" , "0Y" , "0X" , "0L" , "0R" , "0Select" , "0Start"
@@ -129,67 +113,54 @@ internal class BsnesController : IBsnesController
129
113
130
114
public ControllerDefinition Definition => _definition ;
131
115
132
- public void UpdateState ( IController controller )
133
- {
134
- for ( int i = 0 ; i < 12 ; i ++ )
135
- {
136
- _state [ i ] = controller . IsPressed ( Buttons [ i ] ) ;
137
- }
138
- }
139
-
140
- public short GetState ( int index , int id )
116
+ public short GetState ( IController controller , int index , int id )
141
117
{
142
118
if ( id >= 12 )
143
119
return 0 ;
144
120
145
- return ( short ) ( _state [ id ] ? 1 : 0 ) ;
121
+ return ( short ) ( controller . IsPressed ( Buttons [ id ] ) ? 1 : 0 ) ;
146
122
}
147
123
}
148
124
149
125
internal class BsnesMouseController : IBsnesController
150
126
{
151
- private readonly short [ ] _state = new short [ 4 ] ;
152
-
153
127
private static readonly ControllerDefinition _definition = new ControllerDefinition ( "(SNES Controller fragment)" )
154
128
{ BoolButtons = { "0Mouse Left" , "0Mouse Right" } }
155
129
. AddXYPair ( "0Mouse {0}" , AxisPairOrientation . RightAndDown , ( - 127 ) . RangeTo ( 127 ) , 0 ) ; //TODO verify direction against hardware, R+D inferred from behaviour in Mario Paint
156
130
157
131
public ControllerDefinition Definition => _definition ;
158
132
public bool LimitAnalogChangeSensitivity { get ; init ; } = true ;
159
133
160
- public void UpdateState ( IController controller )
134
+ public short GetState ( IController controller , int index , int id )
161
135
{
162
- int x = controller . AxisValue ( "0Mouse X" ) ;
163
- if ( LimitAnalogChangeSensitivity )
164
- {
165
- x = x . Clamp ( - 10 , 10 ) ;
166
- }
167
- _state [ 0 ] = ( short ) x ;
168
-
169
- int y = controller . AxisValue ( "0Mouse Y" ) ;
170
- if ( LimitAnalogChangeSensitivity )
136
+ switch ( id )
171
137
{
172
- y = y . Clamp ( - 10 , 10 ) ;
138
+ case 0 :
139
+ int x = controller . AxisValue ( "0Mouse X" ) ;
140
+ if ( LimitAnalogChangeSensitivity )
141
+ {
142
+ x = x . Clamp ( - 10 , 10 ) ;
143
+ }
144
+ return ( short ) x ;
145
+ case 1 :
146
+ int y = controller . AxisValue ( "0Mouse Y" ) ;
147
+ if ( LimitAnalogChangeSensitivity )
148
+ {
149
+ y = y . Clamp ( - 10 , 10 ) ;
150
+ }
151
+ return ( short ) y ;
152
+ case 2 :
153
+ return ( short ) ( controller . IsPressed ( "0Mouse Left" ) ? 1 : 0 ) ;
154
+ case 3 :
155
+ return ( short ) ( controller . IsPressed ( "0Mouse Right" ) ? 1 : 0 ) ;
156
+ default :
157
+ return 0 ;
173
158
}
174
- _state [ 1 ] = ( short ) y ;
175
-
176
- _state [ 2 ] = ( short ) ( controller . IsPressed ( "0Mouse Left" ) ? 1 : 0 ) ;
177
- _state [ 3 ] = ( short ) ( controller . IsPressed ( "0Mouse Right" ) ? 1 : 0 ) ;
178
- }
179
-
180
- public short GetState ( int index , int id )
181
- {
182
- if ( id >= 4 )
183
- return 0 ;
184
-
185
- return _state [ id ] ;
186
159
}
187
160
}
188
161
189
162
internal class BsnesMultitapController : IBsnesController
190
163
{
191
- private readonly bool [ , ] _state = new bool [ 4 , 12 ] ;
192
-
193
164
private static readonly string [ ] Buttons =
194
165
{
195
166
"Up" , "Down" , "Left" , "Right" , "B" , "A" , "Y" , "X" , "L" , "R" , "Select" , "Start"
@@ -221,28 +192,17 @@ internal class BsnesMultitapController : IBsnesController
221
192
222
193
public ControllerDefinition Definition => _definition ;
223
194
224
- public void UpdateState ( IController controller )
225
- {
226
- for ( int port = 0 ; port < 4 ; port ++ )
227
- for ( int i = 0 ; i < 12 ; i ++ )
228
- {
229
- _state [ port , i ] = controller . IsPressed ( port + Buttons [ i ] ) ;
230
- }
231
- }
232
-
233
- public short GetState ( int index , int id )
195
+ public short GetState ( IController controller , int index , int id )
234
196
{
235
197
if ( id >= 12 || index >= 4 )
236
198
return 0 ;
237
199
238
- return ( short ) ( _state [ index , id ] ? 1 : 0 ) ;
200
+ return ( short ) ( controller . IsPressed ( index + Buttons [ id ] ) ? 1 : 0 ) ;
239
201
}
240
202
}
241
203
242
204
internal class BsnesPayloadController : IBsnesController
243
205
{
244
- private readonly bool [ , ] _state = new bool [ 2 , 16 ] ;
245
-
246
206
private readonly int [ ] _buttonsOrder = { 4 , 5 , 6 , 7 , 0 , 8 , 1 , 9 , 10 , 11 , 2 , 3 , 12 , 13 , 14 , 15 } ;
247
207
248
208
private static readonly ControllerDefinition _definition = new ( "(SNES Controller fragment)" )
@@ -252,50 +212,32 @@ internal class BsnesPayloadController : IBsnesController
252
212
253
213
public ControllerDefinition Definition => _definition ;
254
214
255
- public void UpdateState ( IController controller )
256
- {
257
- for ( int index = 0 ; index < 2 ; index ++ )
258
- for ( int i = 0 ; i < 16 ; i ++ )
259
- {
260
- _state [ index , i ] = controller . IsPressed ( Definition . BoolButtons [ index * 16 + _buttonsOrder [ i ] ] ) ;
261
- }
262
- }
263
-
264
- public short GetState ( int index , int id )
215
+ public short GetState ( IController controller , int index , int id )
265
216
{
266
217
if ( index >= 2 || id >= 16 )
267
218
return 0 ;
268
219
269
- return ( short ) ( _state [ index , id ] ? 1 : 0 ) ;
220
+ return ( short ) ( controller . IsPressed ( Definition . BoolButtons [ index * 16 + _buttonsOrder [ id ] ] ) ? 1 : 0 ) ;
270
221
}
271
222
}
272
223
273
224
internal class BsnesSuperScopeController : IBsnesController
274
225
{
275
- private readonly short [ ] _state = new short [ 6 ] ;
276
-
277
226
private static readonly ControllerDefinition _definition = new ControllerDefinition ( "(SNES Controller fragment)" )
278
227
{ BoolButtons = { "0Trigger" , "0Cursor" , "0Turbo" , "0Pause" } }
279
228
. AddLightGun ( "0Scope {0}" ) ;
280
229
281
230
public ControllerDefinition Definition => _definition ;
282
231
283
- public void UpdateState ( IController controller )
232
+ public short GetState ( IController controller , int index , int id )
284
233
{
285
- _state [ 0 ] = ( short ) controller . AxisValue ( "0Scope X" ) ;
286
- _state [ 1 ] = ( short ) controller . AxisValue ( "0Scope Y" ) ;
287
- for ( int i = 0 ; i < 4 ; i ++ )
234
+ return id switch
288
235
{
289
- _state [ i + 2 ] = ( short ) ( controller . IsPressed ( _definition . BoolButtons [ i ] ) ? 1 : 0 ) ;
290
- }
291
- }
292
-
293
- public short GetState ( int index , int id )
294
- {
295
- if ( id >= 6 )
296
- return 0 ;
297
-
298
- return _state [ id ] ;
236
+ 0 => ( short ) controller . AxisValue ( "0Scope X" ) ,
237
+ 1 => ( short ) controller . AxisValue ( "0Scope Y" ) ,
238
+ 2 or 3 or 4 or 5 => ( short ) ( controller . IsPressed ( _definition . BoolButtons [ id - 2 ] ) ? 1 : 0 ) ,
239
+ _ => 0
240
+ } ;
299
241
}
300
242
}
301
243
@@ -311,35 +253,25 @@ public BsnesJustifierController(bool chained)
311
253
: new ControllerDefinition ( "(SNES Controller fragment)" )
312
254
{ BoolButtons = { "0Trigger" , "0Start" } }
313
255
. AddLightGun ( "0Justifier {0}" ) ;
314
- _state = new short [ chained ? 8 : 4 ] ;
315
256
_chained = chained ;
316
257
}
317
258
318
259
private readonly bool _chained ;
319
- private readonly short [ ] _state ;
320
260
321
261
public ControllerDefinition Definition { get ; }
322
262
323
- public void UpdateState ( IController controller )
263
+ public short GetState ( IController controller , int index , int id )
324
264
{
325
- _state [ 0 ] = ( short ) controller . AxisValue ( "0Justifier X" ) ;
326
- _state [ 1 ] = ( short ) controller . AxisValue ( "0Justifier Y" ) ;
327
- _state [ 2 ] = ( short ) ( controller . IsPressed ( Definition . BoolButtons [ 0 ] ) ? 1 : 0 ) ;
328
- _state [ 3 ] = ( short ) ( controller . IsPressed ( Definition . BoolButtons [ 1 ] ) ? 1 : 0 ) ;
329
- if ( _chained )
330
- {
331
- _state [ 4 ] = ( short ) controller . AxisValue ( "1Justifier X" ) ;
332
- _state [ 5 ] = ( short ) controller . AxisValue ( "1Justifier Y" ) ;
333
- _state [ 6 ] = ( short ) ( controller . IsPressed ( Definition . BoolButtons [ 2 ] ) ? 1 : 0 ) ;
334
- _state [ 7 ] = ( short ) ( controller . IsPressed ( Definition . BoolButtons [ 3 ] ) ? 1 : 0 ) ;
335
- }
336
- }
337
- public short GetState ( int index , int id )
338
- {
339
- if ( index >= 2 || id >= 4 || ( index == 1 && ! _chained ) )
265
+ if ( index == 1 && ! _chained )
340
266
return 0 ;
341
267
342
- return _state [ index * 4 + id ] ;
268
+ return id switch
269
+ {
270
+ 0 => ( short ) controller . AxisValue ( $ "{ index } Justifier X") ,
271
+ 1 => ( short ) controller . AxisValue ( $ "{ index } Justifier Y") ,
272
+ 2 or 3 => ( short ) ( controller . IsPressed ( Definition . BoolButtons [ index * 2 + id ] ) ? 1 : 0 ) ,
273
+ _ => 0
274
+ } ;
343
275
}
344
276
}
345
277
}
0 commit comments