Improve drag input detection
This commit is contained in:
21
game/input.h
21
game/input.h
@@ -3,6 +3,7 @@
|
||||
|
||||
#include <breeze.h>
|
||||
#include <flecs.h>
|
||||
#include <raymath.h>
|
||||
|
||||
#include "components.h"
|
||||
|
||||
@@ -17,7 +18,7 @@ typedef enum InputType {
|
||||
|
||||
#define MOUSE_BUTTON_COUNT (MOUSE_BUTTON_BACK + 1)
|
||||
|
||||
#define CLICK_LIMIT 0.12f
|
||||
#define DRAG_THRESHOLD 5
|
||||
#define BUTTON_COUNT (MOUSE_BUTTON_BACK + 1)
|
||||
|
||||
typedef struct InputMapping {
|
||||
@@ -105,18 +106,26 @@ static void updateInputState(InputState *state, const Camera2D camera, const f32
|
||||
state->mouseWorld = mouseWorld;
|
||||
}
|
||||
|
||||
static f32 inputMouseDelta(const InputState *state) {
|
||||
Vector2 start = state->mouseDown;
|
||||
Vector2 end = state->mouse;
|
||||
f32 dst = Vector2DistanceSqr(start, end);
|
||||
return dst;
|
||||
}
|
||||
|
||||
static bool isInputBtnDragged(const InputState *state, const MouseButton btn) {
|
||||
return IsMouseButtonDown(btn) && state->mouseDownElapsed[btn] > CLICK_LIMIT;
|
||||
if (!IsMouseButtonDown(btn)) return false;
|
||||
return inputMouseDelta(state) >= DRAG_THRESHOLD;
|
||||
}
|
||||
static bool isInputBtnJustDragged(const InputState *state, const MouseButton btn) {
|
||||
return IsMouseButtonReleased(btn) && state->mouseDownElapsed[btn] > CLICK_LIMIT;
|
||||
if (!IsMouseButtonReleased(btn)) return false;
|
||||
return inputMouseDelta(state) >= DRAG_THRESHOLD;
|
||||
}
|
||||
static bool isInputBtnJustDown(const InputState *state, const MouseButton btn) {
|
||||
BZ_UNUSED(state);
|
||||
return IsMouseButtonPressed(btn);
|
||||
return IsMouseButtonPressed(btn) && inputMouseDelta(state) < DRAG_THRESHOLD;
|
||||
}
|
||||
static bool isInputBtnJustUp(const InputState *state, const MouseButton btn) {
|
||||
return IsMouseButtonReleased(btn) && state->mouseDownElapsed[btn] <= CLICK_LIMIT;
|
||||
return IsMouseButtonReleased(btn) && inputMouseDelta(state) < DRAG_THRESHOLD;
|
||||
}
|
||||
static bool isInputBtnDown(const InputState *state, const MouseButton btn) {
|
||||
BZ_UNUSED(state);
|
||||
|
||||
Reference in New Issue
Block a user