Perfected button responses
This commit is contained in:
+28
-10
@@ -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
|
||||
//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()
|
||||
);
|
||||
}
|
||||
@@ -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;
|
||||
};
|
||||
|
||||
+21
-3
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user