mirror of
https://github.com/LNH-team/pico-launcher.git
synced 2026-06-02 09:06:54 +02:00
Add touch input support, add fast scrolling support for coverflow display mode, fix use after free bug in banner list mode
This commit is contained in:
@@ -2,10 +2,10 @@
|
||||
#include "InputKey.h"
|
||||
|
||||
/// @brief Interface for a source of key input.
|
||||
class IInputSource
|
||||
class IKeyInputSource
|
||||
{
|
||||
public:
|
||||
virtual ~IInputSource() { }
|
||||
virtual ~IKeyInputSource() { }
|
||||
|
||||
virtual InputKey Sample() const = 0;
|
||||
};
|
||||
14
arm9/source/gui/input/ITouchInputSource.h
Normal file
14
arm9/source/gui/input/ITouchInputSource.h
Normal file
@@ -0,0 +1,14 @@
|
||||
#pragma once
|
||||
#include "core/math/Point.h"
|
||||
|
||||
/// @brief Interface for a source of touch input.
|
||||
class ITouchInputSource
|
||||
{
|
||||
public:
|
||||
virtual ~ITouchInputSource() { }
|
||||
|
||||
/// @brief Samples the touch input.
|
||||
/// @param touchPosition When the pen is down, the current touch position.
|
||||
/// @return \c true when the pen is down, or \c false otherwise.
|
||||
virtual bool Sample(Point& touchPosition) const = 0;
|
||||
};
|
||||
@@ -15,7 +15,9 @@ enum class InputKey : u16
|
||||
L = 1 << 9,
|
||||
X = 1 << 10,
|
||||
Y = 1 << 11,
|
||||
Debug = 1 << 13
|
||||
Debug = 1 << 13,
|
||||
Touch = 1 << 14,
|
||||
Lid = 1 << 15
|
||||
};
|
||||
|
||||
inline InputKey operator&(InputKey lhs, InputKey rhs)
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
#pragma once
|
||||
|
||||
#include "core/math/Point.h"
|
||||
#include "InputKey.h"
|
||||
|
||||
class InputProvider
|
||||
@@ -14,6 +14,23 @@ public:
|
||||
/// @return \c true if any of the keys in the \p mask is being held, or \c false otherwise.
|
||||
bool Current(InputKey mask) const { return static_cast<bool>(_currentKeys & mask); }
|
||||
|
||||
/// @brief Returns the current touch point if the screen is being touched.
|
||||
/// @param touchPoint The current touch point if the screen is being touched.
|
||||
/// @return \c true if the screen is being touched, or \c false otherwise.
|
||||
bool GetCurrentTouchPoint(Point& touchPoint)
|
||||
{
|
||||
if (Current(InputKey::Touch))
|
||||
{
|
||||
touchPoint = _currentTouchPoint;
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
touchPoint = Point(0, 0);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/// @brief Returns a bitmask of the keys that went from unpressed to pressed in the latest update.
|
||||
/// @return A bitmask of the keys that went from unpressed to pressed in the latest update.
|
||||
InputKey GetTriggeredKeys() const
|
||||
@@ -46,13 +63,16 @@ public:
|
||||
_currentKeys = InputKey::None;
|
||||
_triggeredKeys = InputKey::None;
|
||||
_releasedKeys = InputKey::None;
|
||||
_currentTouchPoint = Point(0, 0);
|
||||
}
|
||||
|
||||
protected:
|
||||
InputKey _currentKeys;
|
||||
InputKey _triggeredKeys;
|
||||
InputKey _releasedKeys;
|
||||
Point _currentTouchPoint;
|
||||
|
||||
InputProvider()
|
||||
: _currentKeys(InputKey::None), _triggeredKeys(InputKey::None), _releasedKeys(InputKey::None) { }
|
||||
: _currentKeys(InputKey::None), _triggeredKeys(InputKey::None), _releasedKeys(InputKey::None)
|
||||
, _currentTouchPoint(0, 0) { }
|
||||
};
|
||||
|
||||
@@ -27,7 +27,9 @@ void InputRepeater::Update()
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
_state = State::Idle;
|
||||
}
|
||||
}
|
||||
else if (_state == State::NextRepeat)
|
||||
{
|
||||
@@ -40,13 +42,24 @@ void InputRepeater::Update()
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
_state = State::Idle;
|
||||
}
|
||||
}
|
||||
|
||||
InputKey lastRepKeys = _currentKeys & _repeatMask;
|
||||
_currentKeys = curKeys | repKeys;
|
||||
_triggeredKeys = _inputProvider->GetTriggeredKeys() | repKeys;
|
||||
_releasedKeys = _inputProvider->GetReleasedKeys() | (lastRepKeys & (lastRepKeys ^ repKeys));
|
||||
Point touchPoint;
|
||||
if (_inputProvider->GetCurrentTouchPoint(touchPoint))
|
||||
{
|
||||
_currentTouchPoint = touchPoint;
|
||||
}
|
||||
else
|
||||
{
|
||||
_currentTouchPoint = Point(0, 0);
|
||||
}
|
||||
}
|
||||
|
||||
void InputRepeater::Reset()
|
||||
|
||||
@@ -7,7 +7,7 @@ class InputRepeater : public InputProvider
|
||||
public:
|
||||
InputRepeater(InputProvider* inputProvider, InputKey repeatMask, u16 firstRepeatDelay, u16 nextRepeatDelay)
|
||||
: _inputProvider(inputProvider), _state(State::Idle)
|
||||
, _frameCounter(0), _repeatMask(repeatMask)
|
||||
, _frameCounter(0), _repeatMask(repeatMask & ~InputKey::Touch)
|
||||
, _firstRepeatDelayFrames(firstRepeatDelay)
|
||||
, _nextRepeatDelayFrames(nextRepeatDelay) { }
|
||||
|
||||
|
||||
@@ -1,17 +1,18 @@
|
||||
#pragma once
|
||||
#include "common.h"
|
||||
#include <nds/input.h>
|
||||
#include "IInputSource.h"
|
||||
#include "IKeyInputSource.h"
|
||||
#include "sharedMemory.h"
|
||||
|
||||
/// @brief Input source from the physical DS buttons.
|
||||
class PadInputSource : public IInputSource
|
||||
class PadInputSource : public IKeyInputSource
|
||||
{
|
||||
public:
|
||||
InputKey Sample() const override
|
||||
{
|
||||
u16 arm9Mask = (~REG_KEYINPUT & 0x3FF);
|
||||
u16 arm7Mask = (~SHARED_KEY_XY & 0xB);
|
||||
u16 arm7Mask = (~SHARED_KEY_XY & 0xCB);
|
||||
arm7Mask = (arm7Mask & 0xB) | ((arm7Mask & 0xC0) >> 2);
|
||||
return static_cast<InputKey>((arm9Mask | (arm7Mask << 10)));
|
||||
}
|
||||
};
|
||||
|
||||
@@ -7,16 +7,44 @@ void SampledInputProvider::Update()
|
||||
InputKey trig = InputKey::None;
|
||||
InputKey rel = InputKey::None;
|
||||
|
||||
Point touchPoint(0, 0);
|
||||
int touchPointCount = 0;
|
||||
while (_inputBufferReadPtr != _inputBufferWritePtr)
|
||||
{
|
||||
InputKey nextKeys = _inputBuffer[_inputBufferReadPtr];
|
||||
InputKey nextKeys = _keyInputBuffer[_inputBufferReadPtr];
|
||||
trig |= (nextKeys ^ curKeys) & nextKeys;
|
||||
rel |= (nextKeys ^ curKeys) & curKeys;
|
||||
curKeys = nextKeys;
|
||||
if ((nextKeys & InputKey::Touch) != InputKey::None)
|
||||
{
|
||||
touchPoint.x += _touchInputBuffer[_inputBufferReadPtr].x;
|
||||
touchPoint.y += _touchInputBuffer[_inputBufferReadPtr].y;
|
||||
touchPointCount++;
|
||||
}
|
||||
else
|
||||
{
|
||||
touchPoint = Point(0, 0);
|
||||
touchPointCount = 0;
|
||||
}
|
||||
_inputBufferReadPtr = (_inputBufferReadPtr + 1) & 3;
|
||||
}
|
||||
|
||||
_triggeredKeys = trig;
|
||||
_releasedKeys = rel;
|
||||
_currentKeys = curKeys;
|
||||
if (touchPointCount == 0)
|
||||
{
|
||||
if ((_triggeredKeys & InputKey::Touch) != InputKey::None ||
|
||||
(_currentKeys & InputKey::Touch) != InputKey::None)
|
||||
{
|
||||
_releasedKeys = _releasedKeys | InputKey::Touch;
|
||||
}
|
||||
_triggeredKeys = _triggeredKeys & ~InputKey::Touch;
|
||||
_currentKeys = _currentKeys & ~InputKey::Touch;
|
||||
}
|
||||
else
|
||||
{
|
||||
_currentTouchPoint.x = touchPoint.x / touchPointCount;
|
||||
_currentTouchPoint.y = touchPoint.y / touchPointCount;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,20 +1,23 @@
|
||||
#pragma once
|
||||
#include "InputProvider.h"
|
||||
#include "IInputSource.h"
|
||||
#include "IKeyInputSource.h"
|
||||
#include "ITouchInputSource.h"
|
||||
|
||||
/// @brief Input provider providing input from an \see IInputSource.
|
||||
/// @brief Input provider providing input from an \see IKeyInputSource.
|
||||
class SampledInputProvider : public InputProvider
|
||||
{
|
||||
public:
|
||||
explicit SampledInputProvider(const IInputSource* inputSource)
|
||||
: _inputSource(inputSource), _inputBufferReadPtr(0), _inputBufferWritePtr(0) { }
|
||||
explicit SampledInputProvider(const IKeyInputSource* keyInputSource, const ITouchInputSource* touchInputSource)
|
||||
: _keyInputSource(keyInputSource), _touchInputSource(touchInputSource)
|
||||
, _inputBufferReadPtr(0), _inputBufferWritePtr(0) { }
|
||||
|
||||
void Update() override;
|
||||
|
||||
/// @brief Samples the input source.
|
||||
void Sample()
|
||||
{
|
||||
_inputBuffer[_inputBufferWritePtr] = _inputSource->Sample();
|
||||
_keyInputBuffer[_inputBufferWritePtr] = _keyInputSource->Sample();
|
||||
_touchInputSource->Sample(_touchInputBuffer[_inputBufferWritePtr]);
|
||||
_inputBufferWritePtr = (_inputBufferWritePtr + 1) & 3;
|
||||
}
|
||||
|
||||
@@ -26,9 +29,11 @@ public:
|
||||
}
|
||||
|
||||
private:
|
||||
const IInputSource* _inputSource;
|
||||
const IKeyInputSource* _keyInputSource;
|
||||
const ITouchInputSource* _touchInputSource;
|
||||
|
||||
InputKey _inputBuffer[4];
|
||||
InputKey _keyInputBuffer[4];
|
||||
Point _touchInputBuffer[4];
|
||||
u8 _inputBufferReadPtr;
|
||||
u8 _inputBufferWritePtr;
|
||||
};
|
||||
|
||||
24
arm9/source/gui/input/TouchInputSource.h
Normal file
24
arm9/source/gui/input/TouchInputSource.h
Normal file
@@ -0,0 +1,24 @@
|
||||
#pragma once
|
||||
#include "common.h"
|
||||
#include <nds/input.h>
|
||||
#include "ITouchInputSource.h"
|
||||
#include "sharedMemory.h"
|
||||
|
||||
/// @brief Input source from the physical DS touch screen.
|
||||
class TouchInputSource : public ITouchInputSource
|
||||
{
|
||||
public:
|
||||
bool Sample(Point& touchPosition) const override
|
||||
{
|
||||
if (SHARED_KEY_XY & (1 << 6))
|
||||
{
|
||||
touchPosition = Point(0, 0);
|
||||
return false;
|
||||
}
|
||||
else
|
||||
{
|
||||
touchPosition = Point(SHARED_TOUCH_X, SHARED_TOUCH_Y);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user