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
#include "fsCCursorInertia.h"

#include <algorithm>
#include <unordered_map>

#include <fsCore/src/fsNMaths.h>
#include <fsCore/fsVec2d.h>
#include <fsCore/src/fsNCoreUtils.h>

#include <fsAppCore/fsIDisplay.h>

#include "fsIInputManager.h"


#define _AX_DEBUG
void fsCCursorInertia::adjustPointerAcceleration()
{
    mPointerPos += mPointerAcc;

    const fsVec2d max = {static_cast<fsF32>(fsIDisplay::instanceGet()->visibleCanvasWidthGet()),
                         static_cast<fsF32>(fsIDisplay::instanceGet()->visibleCanvasHeightGet()) };

    fsNCoreUtils::clamp(mPointerPos.x, 0.0f, max.x);
    fsNCoreUtils::clamp(mPointerPos.y, 0.0f, max.y);

    if (!mKeyPressed)
    {
        const float decelerationRate = 0.8f;
        mPointerAcc.x *= decelerationRate;
        mPointerAcc.y *= decelerationRate;

        if (fsNMaths::fabsF32(mPointerAcc.x) < 0.01f) mPointerAcc.x = 0;
        if (fsNMaths::fabsF32(mPointerAcc.y) < 0.01f) mPointerAcc.y = 0;
    }

    mKeyPressed = false;
}

void fsCCursorInertia::applyDPadMovement(fsS32 pKeyCode)
{
    static const std::unordered_map<fsS32, std::pair<fsF32, fsF32>> dpadMappings = {
            {static_cast<fsS32>(fsIInputManager::eKEY_DPAD_LEFT), {-1, 0}},
            {static_cast<fsS32>(fsIInputManager::eKEY_DPAD_RIGHT), {1, 0}},
            {static_cast<fsS32>(fsIInputManager::eKEY_DPAD_UP), {0, 1}},
            {static_cast<fsS32>(fsIInputManager::eKEY_DPAD_DOWN), {0, -1}}
    };

    auto it = dpadMappings.find(pKeyCode);
    if (it != dpadMappings.end()) {
        // Non-linear acceleration
        float incrementX = (MAX_POINTER_ACC - fsNMaths::fabsF32(mPointerAcc.x)) / MAX_POINTER_ACC;
        float incrementY = (MAX_POINTER_ACC - fsNMaths::fabsF32(mPointerAcc.y)) / MAX_POINTER_ACC;

        incrementX = std::max(incrementX, 0.1f);
        incrementY = std::max(incrementY, 0.1f);

        mPointerAcc.x += it->second.first * incrementX * 0.5f;
        mPointerAcc.y += it->second.second * incrementY * 0.5f;

        fsNCoreUtils::clamp(mPointerAcc.x, -MAX_POINTER_ACC, MAX_POINTER_ACC);
        fsNCoreUtils::clamp(mPointerAcc.y, -MAX_POINTER_ACC, MAX_POINTER_ACC);

        mKeyPressed = true;
    }
}


fsBool fsCCursorInertia::messageProcessDo(const fsCUniqueId& pMessageType,
                                               const fsIMessageDispatcher::sMessageParameters& pParam)
{
    auto typedChar = static_cast<const fsIInputManager::sKeyboardMsgParam&>(pParam);
    applyDPadMovement(typedChar.mKey);
    if (fsIInputManager::mDPADUpMsg == pMessageType)
    {
        if (fsIInputManager::eKEY_DPAD_CENTRE == typedChar.mKey)
        {
            fsIInputManager::instanceGet()->ondDPADClickEvent(mPointerPos, typedChar.mPressed);
        }
        return true;
    }
    if (fsIInputManager::mDPADDownMsg == pMessageType)
    {
        if (fsIInputManager::eKEY_DPAD_CENTRE == typedChar.mKey)
        {
            fsIInputManager::instanceGet()->ondDPADClickEvent(mPointerPos, typedChar.mPressed);
            return true;
        }
    }
    return false;
}

void fsCCursorInertia::typedCharacterHandle(fsS32 pTypedChar)<--- Unused private function: 'fsCCursorInertia::typedCharacterHandle'<--- The member function 'fsCCursorInertia::typedCharacterHandle' can be static.
{
}

void fsCCursorInertia::listenersDeregisterDo()
{
    fsIInputManager::instanceGet()->listenerDeregister(fsIInputManager::mDPADDownMsg, this);
    fsIInputManager::instanceGet()->listenerDeregister(fsIInputManager::mDPADUpMsg, this);
}

void fsCCursorInertia::listenersRegisterDo()
{
    fsIInputManager::instanceGet()->listenerRegister(fsIInputManager::mDPADDownMsg, this);
    fsIInputManager::instanceGet()->listenerRegister(fsIInputManager::mDPADUpMsg, this);
}

fsCCursorInertia::~fsCCursorInertia()
{
}

fsCCursorInertia::fsCCursorInertia(const fsIComponent* const pParent):
mParent(pParent),
fsIMessageListener(fsIMessageListener::ePRIORITY_SOFTWARE_CURSOR),
mPointerPos(fsIDisplay::instanceGet()->visibleCanvasWidthGet() / 2,fsIDisplay::instanceGet()->visibleCanvasHeightGet() / 2),
mPointerAcc(0,0),
mKeyPressed(false)
{
}