diff --git a/library/include/borealis/core/geometry.hpp b/library/include/borealis/core/geometry.hpp index 170eaba3..393411ae 100644 --- a/library/include/borealis/core/geometry.hpp +++ b/library/include/borealis/core/geometry.hpp @@ -16,7 +16,7 @@ #pragma once -#include +#include namespace brls { diff --git a/library/include/borealis/core/gesture.hpp b/library/include/borealis/core/gesture.hpp index e2e38039..944d1c5b 100644 --- a/library/include/borealis/core/gesture.hpp +++ b/library/include/borealis/core/gesture.hpp @@ -39,8 +39,10 @@ enum class GestureState class GestureRecognizer { public: + virtual ~GestureRecognizer() { } + // Main recognition loop, for internal usage only, should not be called anywhere, but Application - virtual GestureState recognitionLoop(Touch touch, View* view, bool* shouldPlayDefaultSound); + virtual GestureState recognitionLoop(TouchState touch, View* view, bool* shouldPlayDefaultSound); // Interrupt this recognizer // If onlyIfUnsureState == true recognizer will be interupted diff --git a/library/include/borealis/core/input.hpp b/library/include/borealis/core/input.hpp index d50c9a99..1244fb24 100644 --- a/library/include/borealis/core/input.hpp +++ b/library/include/borealis/core/input.hpp @@ -85,14 +85,14 @@ enum class TouchPhase }; // Contains raw touch data -struct RawTouch +struct RawTouchState { bool pressed; Point position; }; // Contains touch data filled with it's current phase -struct Touch +struct TouchState { TouchPhase phase; Point position; @@ -111,14 +111,14 @@ class InputManager virtual void updateControllerState(ControllerState* state) = 0; /** - * Called once every frame to fill the given Touch struct with the touch state. + * Called once every frame to fill the given RawTouchState struct with the raw touch data. */ - virtual void updateTouchState(RawTouch* state) = 0; + virtual void updateTouchState(RawTouchState* state) = 0; /** * Calculate current touch phase based on it's previous state */ - static Touch computeTouchState(RawTouch currentTouch, Touch lastFrameState); + static TouchState computeTouchState(RawTouchState currentTouch, TouchState lastFrameState); }; }; // namespace brls diff --git a/library/include/borealis/core/touch/pan_gesture.hpp b/library/include/borealis/core/touch/pan_gesture.hpp index acad3be5..47414ac8 100644 --- a/library/include/borealis/core/touch/pan_gesture.hpp +++ b/library/include/borealis/core/touch/pan_gesture.hpp @@ -16,6 +16,7 @@ #pragma once +#include #include namespace brls @@ -44,7 +45,7 @@ struct PanGestureStatus PanAcceleration acceleration; }; -typedef std::function PanGestureRespond; +typedef Event PanGestureEvent; // Axis of pan recognition start conditions enum class PanAxis @@ -63,8 +64,8 @@ enum class PanAxis class PanGestureRecognizer : public GestureRecognizer { public: - PanGestureRecognizer(PanGestureRespond respond, PanAxis axis); - GestureState recognitionLoop(Touch touch, View* view, bool* shouldPlayDefaultSound) override; + PanGestureRecognizer(PanGestureEvent::Callback respond, PanAxis axis); + GestureState recognitionLoop(TouchState touch, View* view, bool* shouldPlayDefaultSound) override; // Get pan gesture axis PanAxis getAxis() const { return this->axis; } @@ -72,8 +73,11 @@ class PanGestureRecognizer : public GestureRecognizer // Get current state of recognizer PanGestureStatus getCurrentStatus(); + // Get pan gesture event + PanGestureEvent getPanGestureEvent() const { return panEvent; } + private: - PanGestureRespond respond; + PanGestureEvent panEvent; Point position; Point startPosition; Point delta; diff --git a/library/include/borealis/core/touch/tap_gesture.hpp b/library/include/borealis/core/touch/tap_gesture.hpp index 5c8e997f..be86d934 100644 --- a/library/include/borealis/core/touch/tap_gesture.hpp +++ b/library/include/borealis/core/touch/tap_gesture.hpp @@ -38,7 +38,7 @@ class TapGestureRecognizer : public GestureRecognizer { public: TapGestureRecognizer(TapGestureRespond respond, bool callbackOnEndOnly = true); - GestureState recognitionLoop(Touch touch, View* view, bool* shouldPlayDefaultSound) override; + GestureState recognitionLoop(TouchState touch, View* view, bool* shouldPlayDefaultSound) override; // Get current state of recognizer TapGestureStatus getCurrentStatus(); diff --git a/library/include/borealis/core/view.hpp b/library/include/borealis/core/view.hpp index aad06dd5..7fa2144f 100644 --- a/library/include/borealis/core/view.hpp +++ b/library/include/borealis/core/view.hpp @@ -1061,7 +1061,7 @@ class View * that needs to play it's own click sound, * so play default is allowed */ - bool gestureRecognizerRequest(Touch touch, View* firstResponder); + bool gestureRecognizerRequest(TouchState touch, View* firstResponder); /** * Called each frame diff --git a/library/include/borealis/platforms/glfw/glfw_input.hpp b/library/include/borealis/platforms/glfw/glfw_input.hpp index 3c3d01d8..9a52c607 100644 --- a/library/include/borealis/platforms/glfw/glfw_input.hpp +++ b/library/include/borealis/platforms/glfw/glfw_input.hpp @@ -32,7 +32,7 @@ class GLFWInputManager : public InputManager void updateControllerState(ControllerState* state) override; - void updateTouchState(RawTouch* state) override; + void updateTouchState(RawTouchState* state) override; private: GLFWwindow* window; diff --git a/library/include/borealis/platforms/switch/switch_input.hpp b/library/include/borealis/platforms/switch/switch_input.hpp index 429ca003..d3a78bf2 100644 --- a/library/include/borealis/platforms/switch/switch_input.hpp +++ b/library/include/borealis/platforms/switch/switch_input.hpp @@ -1,6 +1,6 @@ /* Copyright 2021 natinusala - Copyright (C) 2021 XITRIX + Copyright (C) 2021 XITRIX Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. @@ -32,11 +32,11 @@ class SwitchInputManager : public InputManager void updateControllerState(ControllerState* state); - void updateTouchState(RawTouch* state); + void updateTouchState(RawTouchState* state); private: PadState padState; - Touch oldTouch; + TouchState oldTouch; }; } // namespace brls diff --git a/library/lib/core/application.cpp b/library/lib/core/application.cpp index 4a7bde44..f40f42f6 100644 --- a/library/lib/core/application.cpp +++ b/library/lib/core/application.cpp @@ -163,15 +163,15 @@ bool Application::mainLoop() // Input ControllerState controllerState = {}; - RawTouch rawTouch = {}; + RawTouchState rawTouch = {}; InputManager* inputManager = Application::platform->getInputManager(); inputManager->updateTouchState(&rawTouch); inputManager->updateControllerState(&controllerState); - static Touch oldTouch; - Touch touchState = InputManager::computeTouchState(rawTouch, oldTouch); - oldTouch = touchState; + static TouchState oldTouch; + TouchState touchState = InputManager::computeTouchState(rawTouch, oldTouch); + oldTouch = touchState; // Touch controller events switch (touchState.phase) @@ -181,9 +181,10 @@ bool Application::mainLoop() Application::setInputType(InputType::TOUCH); // Search for first responder, which will be the root of recognition tree - firstResponder = Application::activitiesStack[Application::activitiesStack.size() - 1] - ->getContentView() - ->hitTest(touchState.position); + if (Application::activitiesStack.size() > 0) + firstResponder = Application::activitiesStack[Application::activitiesStack.size() - 1] + ->getContentView() + ->hitTest(touchState.position); break; case TouchPhase::NONE: firstResponder = nullptr; @@ -366,9 +367,7 @@ bool Application::setInputType(InputType type) Application::inputType = type; if (type == InputType::GAMEPAD) - { Application::currentFocus->onFocusGained(); - } return true; } @@ -405,7 +404,6 @@ bool Application::handleAction(char button) if (action.available) { - if (action.actionListener(hintParent)) { if (button == BUTTON_A) diff --git a/library/lib/core/box.cpp b/library/lib/core/box.cpp index 1e5b2c77..2cfa7937 100644 --- a/library/lib/core/box.cpp +++ b/library/lib/core/box.cpp @@ -319,7 +319,7 @@ View* Box::getDefaultFocus() View* Box::hitTest(Point point) { // Check if touch fits in view frame - if (getFrame().pointInside(point)) + if (this->getFrame().pointInside(point)) { Logger::debug(describe() + ": --- X: " + std::to_string((int)getX()) + ", Y: " + std::to_string((int)getY()) + ", W: " + std::to_string((int)getWidth()) + ", H: " + std::to_string((int)getHeight())); for (View* child : this->children) diff --git a/library/lib/core/geometry.cpp b/library/lib/core/geometry.cpp index 74cc26ff..21d0bb20 100644 --- a/library/lib/core/geometry.cpp +++ b/library/lib/core/geometry.cpp @@ -1,5 +1,5 @@ /* - Copyright 2021 natinusala + Copyright 2021 XITRIX Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. diff --git a/library/lib/core/gesture.cpp b/library/lib/core/gesture.cpp index 7a1ae37d..48fc7498 100644 --- a/library/lib/core/gesture.cpp +++ b/library/lib/core/gesture.cpp @@ -19,7 +19,7 @@ namespace brls { -GestureState GestureRecognizer::recognitionLoop(Touch touch, View* view, bool* shouldPlayDefaultSound) +GestureState GestureRecognizer::recognitionLoop(TouchState touch, View* view, bool* shouldPlayDefaultSound) { return GestureState::FAILED; } diff --git a/library/lib/core/input.cpp b/library/lib/core/input.cpp index 281bdbb8..190a81d4 100644 --- a/library/lib/core/input.cpp +++ b/library/lib/core/input.cpp @@ -19,30 +19,22 @@ namespace brls { -Touch InputManager::computeTouchState(RawTouch currentTouch, Touch lastFrameState) +TouchState InputManager::computeTouchState(RawTouchState currentTouch, TouchState lastFrameState) { if (currentTouch.pressed) { lastFrameState.position = currentTouch.position; if (lastFrameState.phase == TouchPhase::START || lastFrameState.phase == TouchPhase::STAY) - { lastFrameState.phase = TouchPhase::STAY; - } else - { lastFrameState.phase = TouchPhase::START; - } } else { if (lastFrameState.phase == TouchPhase::END || lastFrameState.phase == TouchPhase::NONE) - { lastFrameState.phase = TouchPhase::NONE; - } else - { lastFrameState.phase = TouchPhase::END; - } } return lastFrameState; diff --git a/library/lib/core/touch/pan_gesture.cpp b/library/lib/core/touch/pan_gesture.cpp index f424f97a..c732bd82 100644 --- a/library/lib/core/touch/pan_gesture.cpp +++ b/library/lib/core/touch/pan_gesture.cpp @@ -24,13 +24,13 @@ namespace brls { -PanGestureRecognizer::PanGestureRecognizer(PanGestureRespond respond, PanAxis axis) - : respond(respond) - , axis(axis) +PanGestureRecognizer::PanGestureRecognizer(PanGestureEvent::Callback respond, PanAxis axis) + : axis(axis) { + panEvent.subscribe(respond); } -GestureState PanGestureRecognizer::recognitionLoop(Touch touch, View* view, bool* shouldPlayDefaultSound) +GestureState PanGestureRecognizer::recognitionLoop(TouchState touch, View* view, bool* shouldPlayDefaultSound) { if (!enabled) return GestureState::FAILED; @@ -41,8 +41,8 @@ GestureState PanGestureRecognizer::recognitionLoop(Touch touch, View* view, bool { if (this->state == GestureState::INTERRUPTED || this->state == GestureState::FAILED) { - if (respond && this->state != lastState) - this->respond(getCurrentStatus()); + if (this->state != lastState) + this->panEvent.fire(getCurrentStatus()); lastState = this->state; return this->state; @@ -117,7 +117,7 @@ GestureState PanGestureRecognizer::recognitionLoop(Touch touch, View* view, bool { PanGestureStatus state = getCurrentStatus(); state.acceleration = acceleration; - this->respond(state); + this->panEvent.fire(state); } break; diff --git a/library/lib/core/touch/tap_gesture.cpp b/library/lib/core/touch/tap_gesture.cpp index 6a9b64f0..e8d776fd 100644 --- a/library/lib/core/touch/tap_gesture.cpp +++ b/library/lib/core/touch/tap_gesture.cpp @@ -25,7 +25,7 @@ TapGestureRecognizer::TapGestureRecognizer(TapGestureRespond respond, bool callb { } -GestureState TapGestureRecognizer::recognitionLoop(Touch touch, View* view, bool* shouldPlayDefaultSound) +GestureState TapGestureRecognizer::recognitionLoop(TouchState touch, View* view, bool* shouldPlayDefaultSound) { if (!enabled) return GestureState::FAILED; diff --git a/library/lib/core/view.cpp b/library/lib/core/view.cpp index faaf0827..5f0263c9 100644 --- a/library/lib/core/view.cpp +++ b/library/lib/core/view.cpp @@ -110,7 +110,7 @@ void View::addGestureRecognizer(GestureRecognizer* recognizer) this->gestureRecognizers.push_back(recognizer); } -bool View::gestureRecognizerRequest(Touch touch, View* firstResponder) +bool View::gestureRecognizerRequest(TouchState touch, View* firstResponder) { bool shouldPlayDefaultSound = touch.phase == TouchPhase::START; diff --git a/library/lib/platforms/glfw/glfw_input.cpp b/library/lib/platforms/glfw/glfw_input.cpp index 5f1b4c57..024e3421 100644 --- a/library/lib/platforms/glfw/glfw_input.cpp +++ b/library/lib/platforms/glfw/glfw_input.cpp @@ -112,7 +112,7 @@ void GLFWInputManager::updateControllerState(ControllerState* state) } } -void GLFWInputManager::updateTouchState(RawTouch* state) +void GLFWInputManager::updateTouchState(RawTouchState* state) { // Get touchscreen state double x, y; diff --git a/library/lib/platforms/switch/switch_input.cpp b/library/lib/platforms/switch/switch_input.cpp index 06327241..9017f01c 100644 --- a/library/lib/platforms/switch/switch_input.cpp +++ b/library/lib/platforms/switch/switch_input.cpp @@ -66,7 +66,7 @@ void SwitchInputManager::updateControllerState(ControllerState* state) } } -void SwitchInputManager::updateTouchState(RawTouch* state) +void SwitchInputManager::updateTouchState(RawTouchState* state) { // Get touchscreen state static HidTouchScreenState hidState = { 0 };