Subversion Repository Public Repository

Divide-Framework

This repository has no backups
This repository's network speed is throttled to 100KB/sec

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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
#include "Headers/Scene.h"
#include "Headers/SceneInput.h"

#include "Rendering/PostFX/Headers/PostFX.h"
#include "Managers/Headers/SceneManager.h"

namespace Divide {

namespace {
    const bool g_recordInput = true;
};

void PressReleaseActionCbks::from(const PressReleaseActions& actions, const InputActionList& actionList) {
    _onPressAction = actionList.getInputAction(actions._onPressAction)._action;
    _onReleaseAction = actionList.getInputAction(actions._onReleaseAction)._action;
    _onLCtrlPressAction = actionList.getInputAction(actions._onLCtrlPressAction)._action;
    _onLCtrlReleaseAction = actionList.getInputAction(actions._onLCtrlReleaseAction)._action;
    _onRCtrlPressAction = actionList.getInputAction(actions._onRCtrlPressAction)._action;
    _onRCtrlReleaseAction = actionList.getInputAction(actions._onRCtrlReleaseAction)._action;
    _onLAltPressAction = actionList.getInputAction(actions._onLAltPressAction)._action;
    _onLAltReleaseAction = actionList.getInputAction(actions._onLAltReleaseAction)._action;
    _onRAltPressAction = actionList.getInputAction(actions._onRAltPressAction)._action;
    _onRAltReleaseAction = actionList.getInputAction(actions._onRAltReleaseAction)._action;
}

SceneInput::SceneInput(Scene& parentScene, Input::InputInterface& context) 
    : _parentScene(parentScene),
      _context(context)
{
}

bool SceneInput::isDeviceInUse(I32 deviceID) {
    return std::find_if(std::cbegin(_usedInputDevices),
                        std::cend(_usedInputDevices),
                        [deviceID](I32 device) {
                            return device == deviceID;
                        }) != std::cend(_usedInputDevices);
}

void SceneInput::onSetActive() {
    _playerControlDevices.clear();
    _usedInputDevices.clear();
}

void SceneInput::onRemoveActive() {

}

void SceneInput::onPlayerAdd(U8 index) {
    _keyLog.insert(std::make_pair(index, KeyLog()));
    _mouseBtnLog.insert(std::make_pair(index, MouseBtnLog()));

    std::pair<I32, I32>& devices = _playerControlDevices[index];
    
    // Keyboard + Mouse pairs have priority
    I32 numKBMousePairs = _context.kbMousePairCount();
    for (I32 i = 0; i < numKBMousePairs; ++i) {
        Input::InputInterface::KBMousePair pair = _context.getKeyboardMousePair(to_ubyte(i));

        std::pair<I32, I32> deviceIndices(pair.first ? pair.first->getID() : -1,
                                          pair.second ? pair.second->getID() : -1);

        if (!isDeviceInUse(deviceIndices.first) && !isDeviceInUse(deviceIndices.second)) {
            devices = deviceIndices;
            _usedInputDevices.push_back(devices.first);
            _usedInputDevices.push_back(devices.second);
            return;
        }
    }

    // Fallback to gamepads
    I32 numJoysticks = _context.joystickCount();
    for (I32 i = 0; i < numJoysticks; ++i) {
        OIS::JoyStick* joy = _context.getJoystick(static_cast<Input::Joystick>(i));
        I32 deviceIndex = joy ? joy->getID() : -1;
        if (!isDeviceInUse(deviceIndex)) {
            devices.first = deviceIndex;
            devices.second = -1;
            _usedInputDevices.push_back(deviceIndex);
            return;
        }
    }
}

void SceneInput::onPlayerRemove(U8 index) {
    _keyLog.find(index)->second.clear();
    _mouseBtnLog.find(index)->second.clear();

    std::pair<I32, I32>& devices = _playerControlDevices[index];
    _usedInputDevices.erase(std::remove_if(std::begin(_usedInputDevices),
                                           std::end(_usedInputDevices),
                                           [devices](I32 device) {
                                                return device == devices.first ||
                                                       device == devices.second;
                                           }),
                            std::end(_usedInputDevices));

    devices.first = devices.second = -1;
}

U8 SceneInput::getPlayerIndexForDevice(U8 deviceIndex) const {
    for (hashMapImpl<U8 /*player index*/, std::pair<I32, I32>>::value_type it : _playerControlDevices) {
        if (it.second.first == deviceIndex || it.second.second == deviceIndex) {
            return it.first;
        }
    }

    DIVIDE_UNEXPECTED_CALL("No player for current device");
    return 0;
}

Input::InputState SceneInput::getKeyState(U8 deviceIndex, Input::KeyCode key) const {
    return _context.getKeyState(deviceIndex, key);
}

Input::InputState SceneInput::getMouseButtonState(U8 deviceIndex, Input::MouseButton button) const {
   return _context.getMouseButtonState(deviceIndex, button);
}

Input::InputState SceneInput::getJoystickButtonState(Input::Joystick deviceIndex,
                                                     Input::JoystickButton button) const {
    return _context.getJoystickeButtonState(deviceIndex, button);
}

bool SceneInput::handleCallbacks(const PressReleaseActionCbks& cbks,
                                 const InputParams& params,
                                 bool onPress)
{
    const DELEGATE_CBK<void, InputParams>& lAlt  = (onPress ? cbks._onLAltPressAction  : cbks._onLAltReleaseAction);
    const DELEGATE_CBK<void, InputParams>& rAlt  = (onPress ? cbks._onRAltPressAction  : cbks._onRAltReleaseAction);
    const DELEGATE_CBK<void, InputParams>& lCtrl = (onPress ? cbks._onLCtrlPressAction : cbks._onLCtrlReleaseAction);
    const DELEGATE_CBK<void, InputParams>& rCtrl = (onPress ? cbks._onRCtrlPressAction : cbks._onRCtrlReleaseAction);
    const DELEGATE_CBK<void, InputParams>& noMod = (onPress ? cbks._onPressAction      : cbks._onReleaseAction);

    if (getKeyState(params._deviceIndex, Input::KeyCode::KC_LMENU) == Input::InputState::PRESSED && lAlt) {
        lAlt(params);
        return true;
    }

    if (getKeyState(params._deviceIndex, Input::KeyCode::KC_RMENU) == Input::InputState::PRESSED && rAlt) {
        rAlt(params);
        return true;
    }

    if (getKeyState(params._deviceIndex, Input::KeyCode::KC_LCONTROL) == Input::InputState::PRESSED && lCtrl) {
        lCtrl(params);
        return true;
    }

    if (getKeyState(params._deviceIndex, Input::KeyCode::KC_RCONTROL) == Input::InputState::PRESSED && rCtrl) {
        rCtrl(params);
        return true;
    }

    if (noMod) {
        noMod(params);
        return true;
    }

    return false;
}

bool SceneInput::onKeyDown(const Input::KeyEvent& arg) {
    if (g_recordInput) {
        vectorAlg::emplace_back(
            _keyLog[arg._deviceIndex], std::make_pair(arg._key, Input::InputState::PRESSED));
    }

    PressReleaseActionCbks cbks;
    if (getKeyMapping(arg._key, cbks)) {
        return handleCallbacks(cbks, InputParams(arg._deviceIndex, arg._deviceIndex, to_int(arg._key)), true);
    }

    return false;
}

bool SceneInput::onKeyUp(const Input::KeyEvent& arg) {
    if (g_recordInput) {
        vectorAlg::emplace_back(
            _keyLog[arg._deviceIndex], std::make_pair(arg._key, Input::InputState::RELEASED));
    }

    PressReleaseActionCbks cbks;
    if (getKeyMapping(arg._key, cbks)) {
        return handleCallbacks(cbks, InputParams(arg._deviceIndex, to_int(arg._key)), false);
    }

    return false;
}

bool SceneInput::joystickButtonPressed(const Input::JoystickEvent& arg,
                                       Input::JoystickButton button) {

    Input::Joystick joy = _context.joystick(arg._deviceIndex);
    PressReleaseActionCbks cbks;
    if (getJoystickMapping(joy, Input::JoystickElement(Input::JoystickElementType::BUTTON_PRESS, button), cbks)) {
        return handleCallbacks(cbks, InputParams(arg._deviceIndex, to_int(button)), true);
    }

    return false;
}

bool SceneInput::joystickButtonReleased(const Input::JoystickEvent& arg,
                                        Input::JoystickButton button) {
    
    Input::Joystick joy = _context.joystick(arg._deviceIndex);

    PressReleaseActionCbks cbks;
    if (getJoystickMapping(joy, Input::JoystickElement(Input::JoystickElementType::BUTTON_PRESS, button), cbks)) {
        return handleCallbacks(cbks, InputParams(arg._deviceIndex, to_int(button)), false);
    }

    return false;
}

bool SceneInput::joystickAxisMoved(const Input::JoystickEvent& arg, I8 axis) {
    Input::Joystick joy = _context.joystick(arg._deviceIndex);

    PressReleaseActionCbks cbks;
    if (getJoystickMapping(joy, Input::JoystickElement(Input::JoystickElementType::AXIS_MOVE, axis), cbks)) {
        InputParams params(arg._deviceIndex,
                           arg._event.state.mAxes[axis].abs,
                           arg._event.state.mAxes[axis].rel,
                           to_int(axis),
                           to_int(joy));
        return handleCallbacks(cbks, params, true);
    }

    return false;
}

bool SceneInput::joystickPovMoved(const Input::JoystickEvent& arg, I8 pov) {

    Input::Joystick joy = _context.joystick(arg._deviceIndex);

    PressReleaseActionCbks cbks;
    if (getJoystickMapping(joy, Input::JoystickElement(Input::JoystickElementType::POV_MOVE, pov), cbks)) {
        InputParams params(arg._deviceIndex, arg._event.state.mPOV[pov].direction);
        return handleCallbacks(cbks, params, true);
    }

    return false;
}

bool SceneInput::joystickSliderMoved(const Input::JoystickEvent& arg, I8 index) {
    Input::Joystick joy = _context.joystick(arg._deviceIndex);

    PressReleaseActionCbks cbks;
    if (getJoystickMapping(joy, Input::JoystickElement(Input::JoystickElementType::SLIDER_MOVE, index), cbks)) {
        InputParams params(arg._deviceIndex,
                           arg._event.state.mSliders[index].abX,
                           arg._event.state.mSliders[index].abY);
        return handleCallbacks(cbks, params, true);
    }

    return false;
}

bool SceneInput::joystickVector3DMoved(const Input::JoystickEvent& arg, I8 index) {
    Input::Joystick joy = _context.joystick(arg._deviceIndex);

    PressReleaseActionCbks cbks;
    if (getJoystickMapping(joy, Input::JoystickElement(Input::JoystickElementType::VECTOR_MOVE, index), cbks)) {
        InputParams params(arg._deviceIndex,
                           to_int(arg._event.state.mVectors[index].x),
                           to_int(arg._event.state.mVectors[index].y),
                           to_int(arg._event.state.mVectors[index].z));
        return handleCallbacks(cbks, params, true);
    }

    return false;
}

bool SceneInput::mouseMoved(const Input::MouseEvent& arg) {
    SceneStatePerPlayer& state = _parentScene.state().playerState(arg._deviceIndex);
    state.aimPos(vec2<I32>(arg._event.state.X.abs, arg._event.state.Y.abs));

    if (state.cameraLockedToMouse()) {
        state.angleLR(state.aimDelta().x < 0
                        ? MoveDirection::POSITIVE
                        : MoveDirection::NEGATIVE);
        state.angleUD(state.aimDelta().x < 0
                        ? MoveDirection::POSITIVE
                        : MoveDirection::NEGATIVE);
    }

    return Attorney::SceneInput::mouseMoved(_parentScene, arg);
}

bool SceneInput::mouseButtonPressed(const Input::MouseEvent& arg,
                                    Input::MouseButton id) {

    if (g_recordInput) {
        vectorAlg::emplace_back(_mouseBtnLog[arg._deviceIndex],
            vectorAlg::make_tuple(id, Input::InputState::PRESSED, vec2<I32>(arg._event.state.X.abs, arg._event.state.Y.abs)));
    }

    PressReleaseActionCbks cbks;
    if (getMouseMapping(id, cbks)) {
        return handleCallbacks(cbks, InputParams(arg._deviceIndex, to_int(id)), true);
    }

    return false;
}

bool SceneInput::mouseButtonReleased(const Input::MouseEvent& arg,
                                     Input::MouseButton id) {
    if (g_recordInput) {
        vectorAlg::emplace_back(_mouseBtnLog[arg._deviceIndex],
                                vectorAlg::make_tuple(id, Input::InputState::RELEASED, vec2<I32>(arg._event.state.X.abs, arg._event.state.Y.abs)));
    }

    PressReleaseActionCbks cbks;
    if (getMouseMapping(id, cbks)) {
        return handleCallbacks(cbks, InputParams(arg._deviceIndex, to_int(id)), false);
    }

    return false;
}

bool SceneInput::addKeyMapping(Input::KeyCode key, PressReleaseActions keyCbks) {
    std::pair<KeyMap::iterator, bool> result = hashAlg::emplace(_keyMap, key, keyCbks);

    return result.second;
}

bool SceneInput::removeKeyMapping(Input::KeyCode key) {
    KeyMap::iterator it = _keyMap.find(key);
    if (it != std::end(_keyMap)) {
        _keyMap.erase(it);
        return false;
    }

    return false;
}

bool SceneInput::getKeyMapping(Input::KeyCode key, PressReleaseActionCbks& keyCbksOut) {
    KeyMapCache::const_iterator itCache = _keyMapCache.find(key);
    if (itCache != std::cend(_keyMapCache)) {
        keyCbksOut = itCache->second;
        return true;
    }

    KeyMap::const_iterator it = _keyMap.find(key);
    if (it != std::cend(_keyMap)) {
        const PressReleaseActions& actions = it->second;
        keyCbksOut.from(actions, _actionList);
        hashAlg::emplace(_keyMapCache, key, keyCbksOut);

        return true;
    }

    return false;
}

bool SceneInput::addMouseMapping(Input::MouseButton button, PressReleaseActions btnCbks) {
    std::pair<MouseMap::iterator, bool> result =
        hashAlg::emplace(_mouseMap, button, btnCbks);

    return result.second;
}

bool SceneInput::removeMouseMapping(Input::MouseButton button) {
    MouseMap::iterator it = _mouseMap.find(button);
    if (it != std::end(_mouseMap)) {
        _mouseMap.erase(it);
        return false;
    }

    return false;
}

bool SceneInput::getMouseMapping(Input::MouseButton button, PressReleaseActionCbks& btnCbksOut) {

    MouseMapCache::const_iterator itCache = _mouseMapCache.find(button);
    if (itCache != std::cend(_mouseMapCache)) {
        btnCbksOut = itCache->second;
        return true;
    }


    MouseMap::const_iterator it = _mouseMap.find(button);
    if (it != std::cend(_mouseMap)) {
        const PressReleaseActions& actions = it->second;
        btnCbksOut.from(actions, _actionList);
        hashAlg::emplace(_mouseMapCache, button, btnCbksOut);
        return true;
    }

    return false;
}

bool SceneInput::addJoystickMapping(Input::Joystick device, Input::JoystickElement element, PressReleaseActions btnCbks) {
    if (element._type == Input::JoystickElementType::POV_MOVE ||
        element._type == Input::JoystickElementType::AXIS_MOVE ||
        element._type == Input::JoystickElementType::SLIDER_MOVE ||
        element._type == Input::JoystickElementType::VECTOR_MOVE)
    {
        // non-buttons have no pressed/release states so map on release to on press's action as well
        if (btnCbks._onPressAction){ 
            btnCbks._onReleaseAction = btnCbks._onPressAction;
        } else {
            btnCbks._onPressAction = btnCbks._onReleaseAction;
        }

        if (btnCbks._onLCtrlPressAction) {
            btnCbks._onLCtrlReleaseAction = btnCbks._onLCtrlPressAction;
        } else {
            btnCbks._onLCtrlPressAction = btnCbks._onLCtrlReleaseAction;
        }

        if (btnCbks._onRCtrlPressAction) {
            btnCbks._onRCtrlReleaseAction = btnCbks._onRCtrlPressAction;
        } else {
            btnCbks._onRCtrlPressAction = btnCbks._onRCtrlReleaseAction;
        }

        if (btnCbks._onLAltPressAction) {
            btnCbks._onLAltReleaseAction = btnCbks._onLAltPressAction;
        } else {
            btnCbks._onLAltPressAction = btnCbks._onLAltReleaseAction;
        }

        if (btnCbks._onRAltPressAction) {
            btnCbks._onRAltReleaseAction = btnCbks._onRAltPressAction;
        } else {
            btnCbks._onRAltPressAction = btnCbks._onRAltReleaseAction;
        }
    }

    return hashAlg::emplace(_joystickMap[device], element, btnCbks).second;
}

bool SceneInput::removeJoystickMapping(Input::Joystick device, Input::JoystickElement element) {
    JoystickMapEntry& entry = _joystickMap[device];

    JoystickMapEntry::iterator it = entry.find(element);
    if (it != std::end(entry)) {
        entry.erase(it);
        return false;
    }

    return false;
}

bool SceneInput::getJoystickMapping(Input::Joystick device, Input::JoystickElement element, PressReleaseActionCbks& btnCbksOut) {
    JoystickMapCacheEntry& entry = _joystickMapCache[device];

    JoystickMapCacheEntry::const_iterator itCache = entry.find(element);
    if (itCache != std::cend(entry)) {
        btnCbksOut = itCache->second;
        return true;
    }

    JoystickMapEntry& entry2 = _joystickMap[device];
    JoystickMapEntry::const_iterator it = entry2.find(element);
    if (it != std::cend(entry2)) {
        const PressReleaseActions& actions = it->second;
        btnCbksOut.from(actions, _actionList);
        hashAlg::emplace(entry, element, btnCbksOut);
        return true;
    }

    return false;
}

InputActionList& SceneInput::actionList() {
    return _actionList;
}

void SceneInput::flushCache() {
    _keyMapCache.clear();
    _mouseMapCache.clear();
    _joystickMapCache.clear();
}

};

Commits for Divide-Framework/trunk/Source Code/Scenes/SceneInput.cpp

Diff revisions: vs.
Revision Author Commited Message
842 Diff Diff IonutCava picture IonutCava Wed 01 Feb, 2017 17:25:15 +0000

[IonutCava]
- Start to implement scripting support via ChaiScript: http://chaiscript.com/
- Cleanup DELEGATE_CBK alias

830 Diff Diff IonutCava picture IonutCava Wed 25 Jan, 2017 17:29:44 +0000

[IonutCava]
- Add a new Platform element: File
— Move all predefined paths to FileManagement System
- Split MathHelper into MathHelper and StringHelper

817 Diff Diff IonutCava picture IonutCava Sun 15 Jan, 2017 21:29:59 +0000

[IonutCava]
- Add a new Unit component to SGNs that holds NPC/Player/etc. objects
- Add support for multiple keyboard+mouse combos and joysticks and add mapping between these and players

816 Diff Diff IonutCava picture IonutCava Fri 13 Jan, 2017 17:05:20 +0000

[IonutCava]
- Allow per player scene state and input source
— Add SceneStatePerPlayer class
— Add device index to InputParams objects.
— ToDo: map players to devide indices

801 Diff Diff IonutCava picture IonutCava Sun 27 Nov, 2016 21:28:01 +0000

[IonutCava]
- More performance analysis guided optimizations.
- Some refactoring to allow later removal of Singletons status for: GFXDevice, SFXDevice, PXDevice, GUI and Input.
— Pass a PlatformContext around objects to access these classes.

703 Diff Diff IonutCava picture IonutCava Wed 11 May, 2016 15:44:57 +0000

[IonutCava]
- Finish XML-based control bindings:
— Allow per bind input params (key id, mouse button index, joystick control data, etc)
— Allow mouse binding (with up to 7 buttons)
— Allow up to 4 joysticks (for now) with 120+ button, POV, Axis, Slider and Vector binding support for each
- Change all hashes in code from U32 to size_t for compatibility with STL/Boost

700 Diff Diff IonutCava picture IonutCava Tue 10 May, 2016 22:13:05 +0000

[IonutCava]
- More work on rebindable keys:
— Key events loaded from XML finished
— Mouse and Joystick control binding from XML still not finished

699 Diff Diff IonutCava picture IonutCava Tue 10 May, 2016 16:16:57 +0000

[IonutCava]
- Initial key binding code with external XML support
— Allow separation of actions and keys
— Specify actions in code by id
— Specify action id for each Key state
— Allow key modifiers: Left/Right Alt, Left/Right Ctrl

- Initial default scene code

674 Diff Diff IonutCava picture IonutCava Mon 11 Apr, 2016 16:17:10 +0000

[IonutCava]
- Some profile guided optimizations:
— Remove the task state hashmap and replace with flat array
— Allow PhysX to load collision meshes from a RAM cache first -> then file -> then recompute
- Improve memory logger output

670 IonutCava picture IonutCava Wed 06 Apr, 2016 16:21:35 +0000

[IonutCava]
- getInstance() renamed to instance() for simplicity (should really go away altogether, but that’s a different issue)
- some particle emitter threading updates