From f079bc76fb7152a2cdb41850a780ca2ab2bf29da Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Thu, 30 Jul 2015 04:43:33 +1000 Subject: [PATCH] Perfected button responses --- src/common/button.cpp | 40 +++++++++++++++++++++++++++++----------- src/common/button.hpp | 4 ++-- src/example_scene.cpp | 24 +++++++++++++++++++++--- 3 files changed, 52 insertions(+), 16 deletions(-) diff --git a/src/common/button.cpp b/src/common/button.cpp index 454feeb..42a5f80 100644 --- a/src/common/button.cpp +++ b/src/common/button.cpp @@ -100,13 +100,14 @@ void Button::SetY(int y) { Button::State Button::MouseMotion(SDL_MouseMotionEvent const& event) { //if out of bounds, exit - if (CheckBounds(event.x, event.y)) { + if (!CheckBounds(event.x, event.y)) { return state = State::IDLE; } //if in bounds, check button - if (event.state & SDL_BUTTON_LMASK) { - state = State::PRESSED; + if (event.state & SDL_BUTTON_LMASK && state == State::PRESSED) { + //stay pressed +// state = State::PRESSED; } else { state = State::HOVER; @@ -117,22 +118,37 @@ Button::State Button::MouseMotion(SDL_MouseMotionEvent const& event) { Button::State Button::MouseButtonDown(SDL_MouseButtonEvent const& event) { //if out of bounds, exit - if (CheckBounds(event.x, event.y)) { + if (!CheckBounds(event.x, event.y)) { return state = State::IDLE; } - //if in bounds, set button - return state = State::PRESSED; + //if in bounds, check button + if (event.button == SDL_BUTTON_LEFT) { + return state = State::PRESSED; + } + + //NOTE: if not left button down, ignore + return State::HOVER; } Button::State Button::MouseButtonUp(SDL_MouseButtonEvent const& event) { //if out of bounds, exit - if (CheckBounds(event.x, event.y)) { + if (!CheckBounds(event.x, event.y)) { return state = State::IDLE; } - //if in bounds, set button - return state = State::HOVER; + //if not left button up, ignore + if (event.button != SDL_BUTTON_LEFT) { + return state; + } + + //if in bounds and left button up, send release signal + if (state == State::PRESSED) { + state = State::HOVER; + return State::RELEASED; + } + + return state; } void Button::SetState(State s) { @@ -144,9 +160,11 @@ Button::State Button::GetState() { } bool Button::CheckBounds(int x, int y) { - return + //return if true (x, y) is within bounds, otherwise return false + return !( x < posX || y < posY || x > posX + image.GetClipW() || - y > posY + image.GetClipH(); + y > posY + image.GetClipH() + ); } \ No newline at end of file diff --git a/src/common/button.hpp b/src/common/button.hpp index c6fd91f..ab34c96 100644 --- a/src/common/button.hpp +++ b/src/common/button.hpp @@ -30,7 +30,7 @@ class Button { public: enum State { - IDLE = 0, HOVER = 1, PRESSED = 2 + IDLE = 0, HOVER = 1, PRESSED = 2, RELEASED = 3 }; //methods @@ -59,5 +59,5 @@ protected: Image image; int posX = 0, posY = 0; - State state; + State state = State::IDLE; }; diff --git a/src/example_scene.cpp b/src/example_scene.cpp index cd62893..2a9e270 100644 --- a/src/example_scene.cpp +++ b/src/example_scene.cpp @@ -96,7 +96,12 @@ void ExampleScene::RenderFrame(SDL_Renderer* renderer) { //------------------------- void ExampleScene::MouseMotion(SDL_MouseMotionEvent const& event) { - button.MouseMotion(event); + Button::State state = button.MouseMotion(event); + + if (state == Button::State::PRESSED) { + //motion while pressed + return; + } //right mouse button moves the camera if (event.state & SDL_BUTTON_RMASK) { @@ -106,7 +111,14 @@ void ExampleScene::MouseMotion(SDL_MouseMotionEvent const& event) { } void ExampleScene::MouseButtonDown(SDL_MouseButtonEvent const& event) { - button.MouseButtonDown(event); + Button::State state = button.MouseButtonDown(event); + + //catch button input + if (state == Button::State::PRESSED) { + //TODO: do stuff + std::cout << "pressed" << std::endl; + return; + } switch(event.button) { case SDL_BUTTON_LEFT: { @@ -129,7 +141,13 @@ void ExampleScene::MouseButtonDown(SDL_MouseButtonEvent const& event) { } void ExampleScene::MouseButtonUp(SDL_MouseButtonEvent const& event) { - button.MouseButtonUp(event); + Button::State state = button.MouseButtonUp(event); + + //catch button input + if (state == Button::State::RELEASED) { + std::cout << "released" << std::endl; + return; + } } void ExampleScene::MouseWheel(SDL_MouseWheelEvent const& event) {