Add panning to ScrollPanel when TouchUI is enabled

This commit is contained in:
Tamás Bálint Misius 2024-02-06 14:14:00 +01:00
parent 69e0a8b0aa
commit 588fe293ec
No known key found for this signature in database
GPG Key ID: 5B472A12F6ECA9F2
4 changed files with 68 additions and 3 deletions

View File

@ -55,6 +55,7 @@ lldb_server=${LLDB_SERVER:-$default_lldb_server}
lldb_server_port=${LLDB_SERVER_PORT:-9998} lldb_server_port=${LLDB_SERVER_PORT:-9998}
jdb_port=${JDB_PORT:-13456} jdb_port=${JDB_PORT:-13456}
lldb_client=${LLDB_CLIENT:-$default_lldb_client} lldb_client=${LLDB_CLIENT:-$default_lldb_client}
meson=${MESON:-meson}
adb=${ADB:-adb} adb=${ADB:-adb}
jdb=${JDB:-jdb} jdb=${JDB:-jdb}
@ -113,6 +114,11 @@ Naturally, replace bagelsbagels with an appropriate password.
HELP HELP
exit 1 exit 1
fi fi
>&2 echo "[+] meson compiling android/$app_exe.apk"
if ! $meson compile sign-apk; then
>&2 echo "[-] failed"
return 1
fi
>&2 echo "[+] adb installing android/$app_exe.apk" >&2 echo "[+] adb installing android/$app_exe.apk"
if ! $adb install android/$app_exe.apk; then if ! $adb install android/$app_exe.apk; then
>&2 echo "[-] failed" >&2 echo "[-] failed"

View File

@ -51,6 +51,11 @@ inline int isign(float i)
return 0; return 0;
} }
inline int iabs(int i)
{
return i * isign(i);
}
inline unsigned clamp_flt(float f, float min, float max) inline unsigned clamp_flt(float f, float min, float max)
{ {
if (f<min) if (f<min)

View File

@ -1,8 +1,9 @@
#include "ScrollPanel.h" #include "ScrollPanel.h"
#include "Engine.h" #include "Engine.h"
#include "graphics/Graphics.h" #include "graphics/Graphics.h"
#include "Misc.h"
#include "PowderToySDL.h"
#include "Window.h"
#include <algorithm> #include <algorithm>
using namespace ui; using namespace ui;
@ -79,6 +80,7 @@ void ScrollPanel::XOnMouseDown(int x, int y, unsigned int button)
scrollbarSelected = true; scrollbarSelected = true;
scrollbarInitialYOffset = int(offsetY); scrollbarInitialYOffset = int(offsetY);
} }
initialOffsetY = offsetY;
scrollbarInitialYClick = y - Position.Y; scrollbarInitialYClick = y - Position.Y;
scrollbarClickLocation = 100; scrollbarClickLocation = 100;
} }
@ -87,6 +89,21 @@ void ScrollPanel::XOnMouseDown(int x, int y, unsigned int button)
void ScrollPanel::XOnMouseUp(int x, int y, unsigned int button) void ScrollPanel::XOnMouseUp(int x, int y, unsigned int button)
{ {
scrollbarSelected = false; scrollbarSelected = false;
panning = false;
{
auto it = panHistory.end();
while (it != panHistory.begin() && *(it - 1))
{
--it;
}
if (it < panHistory.end())
{
auto offsetYDiff = panHistory.back()->offsetY - (*it)->offsetY;
auto tickDiff = panHistory.back()->ticks - (*it)->ticks;
yScrollVel += offsetYDiff / tickDiff * (1000.f / Engine::Ref().GetFps());
}
}
panHistory = {};
isMouseInsideScrollbarArea = false; isMouseInsideScrollbarArea = false;
scrollbarClickLocation = 0; scrollbarClickLocation = 0;
} }
@ -116,6 +133,19 @@ void ScrollPanel::XOnMouseMoved(int x, int y, int dx, int dy)
offsetY = float(scrollbarInitialYOffset); offsetY = float(scrollbarInitialYOffset);
} }
} }
else if (MouseDownInside)
{
Vec2<int> mouseAt{ x, y };
if (Engine::Ref().TouchUI && iabs(scrollbarInitialYClick - mouseAt.Y) > PanOffsetThreshold)
{
panning = true;
for (auto *child : children)
{
child->MouseDownInside = false;
}
GetParentWindow()->FocusComponent(NULL);
}
}
if (x > (Size.X-scrollBarWidth) && x < (Size.X-scrollBarWidth)+scrollBarWidth) if (x > (Size.X-scrollBarWidth) && x < (Size.X-scrollBarWidth)+scrollBarWidth)
{ {
@ -130,6 +160,19 @@ void ScrollPanel::XOnMouseMoved(int x, int y, int dx, int dy)
void ScrollPanel::XTick(float dt) void ScrollPanel::XTick(float dt)
{ {
if (panning)
{
auto scrollY = initialOffsetY + scrollbarInitialYClick - (Engine::Ref().GetMouseY() - GetScreenPos().Y);
ViewportPosition.Y = -scrollY;
offsetY = float(scrollY);
PanPoint p{ offsetY, GetTicks() };
if (!(panHistory.back() && panHistory.back()->ticks == p.ticks))
{
std::copy(panHistory.begin() + 1, panHistory.end(), panHistory.begin());
panHistory.back() = p;
}
}
if (xScrollVel > 7.0f) xScrollVel = 7.0f; if (xScrollVel > 7.0f) xScrollVel = 7.0f;
if (xScrollVel < -7.0f) xScrollVel = -7.0f; if (xScrollVel < -7.0f) xScrollVel = -7.0f;
if (xScrollVel > -0.5f && xScrollVel < 0.5) if (xScrollVel > -0.5f && xScrollVel < 0.5)

View File

@ -1,6 +1,7 @@
#pragma once #pragma once
#include "Panel.h" #include "Panel.h"
#include <optional>
#include <array>
namespace ui namespace ui
{ {
@ -19,6 +20,16 @@ namespace ui
int scrollbarInitialYOffset; int scrollbarInitialYOffset;
int scrollbarInitialYClick; int scrollbarInitialYClick;
int scrollbarClickLocation; int scrollbarClickLocation;
int initialOffsetY;
bool panning = false;
static constexpr int PanOffsetThreshold = 10;
static constexpr int PanHistorySize = 5;
struct PanPoint
{
float offsetY;
unsigned int ticks;
};
std::array<std::optional<PanPoint>, PanHistorySize> panHistory;
public: public:
ScrollPanel(Point position, Point size); ScrollPanel(Point position, Point size);