Perfected button responses
This commit is contained in:
+29
-11
@@ -100,13 +100,14 @@ void Button::SetY(int y) {
|
|||||||
|
|
||||||
Button::State Button::MouseMotion(SDL_MouseMotionEvent const& event) {
|
Button::State Button::MouseMotion(SDL_MouseMotionEvent const& event) {
|
||||||
//if out of bounds, exit
|
//if out of bounds, exit
|
||||||
if (CheckBounds(event.x, event.y)) {
|
if (!CheckBounds(event.x, event.y)) {
|
||||||
return state = State::IDLE;
|
return state = State::IDLE;
|
||||||
}
|
}
|
||||||
|
|
||||||
//if in bounds, check button
|
//if in bounds, check button
|
||||||
if (event.state & SDL_BUTTON_LMASK) {
|
if (event.state & SDL_BUTTON_LMASK && state == State::PRESSED) {
|
||||||
state = State::PRESSED;
|
//stay pressed
|
||||||
|
// state = State::PRESSED;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
state = State::HOVER;
|
state = State::HOVER;
|
||||||
@@ -117,22 +118,37 @@ Button::State Button::MouseMotion(SDL_MouseMotionEvent const& event) {
|
|||||||
|
|
||||||
Button::State Button::MouseButtonDown(SDL_MouseButtonEvent const& event) {
|
Button::State Button::MouseButtonDown(SDL_MouseButtonEvent const& event) {
|
||||||
//if out of bounds, exit
|
//if out of bounds, exit
|
||||||
if (CheckBounds(event.x, event.y)) {
|
if (!CheckBounds(event.x, event.y)) {
|
||||||
return state = State::IDLE;
|
return state = State::IDLE;
|
||||||
}
|
}
|
||||||
|
|
||||||
//if in bounds, set button
|
//if in bounds, check button
|
||||||
return state = State::PRESSED;
|
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) {
|
Button::State Button::MouseButtonUp(SDL_MouseButtonEvent const& event) {
|
||||||
//if out of bounds, exit
|
//if out of bounds, exit
|
||||||
if (CheckBounds(event.x, event.y)) {
|
if (!CheckBounds(event.x, event.y)) {
|
||||||
return state = State::IDLE;
|
return state = State::IDLE;
|
||||||
}
|
}
|
||||||
|
|
||||||
//if in bounds, set button
|
//if not left button up, ignore
|
||||||
return state = State::HOVER;
|
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) {
|
void Button::SetState(State s) {
|
||||||
@@ -144,9 +160,11 @@ Button::State Button::GetState() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
bool Button::CheckBounds(int x, int y) {
|
bool Button::CheckBounds(int x, int y) {
|
||||||
return
|
//return if true (x, y) is within bounds, otherwise return false
|
||||||
|
return !(
|
||||||
x < posX ||
|
x < posX ||
|
||||||
y < posY ||
|
y < posY ||
|
||||||
x > posX + image.GetClipW() ||
|
x > posX + image.GetClipW() ||
|
||||||
y > posY + image.GetClipH();
|
y > posY + image.GetClipH()
|
||||||
|
);
|
||||||
}
|
}
|
||||||
@@ -30,7 +30,7 @@
|
|||||||
class Button {
|
class Button {
|
||||||
public:
|
public:
|
||||||
enum State {
|
enum State {
|
||||||
IDLE = 0, HOVER = 1, PRESSED = 2
|
IDLE = 0, HOVER = 1, PRESSED = 2, RELEASED = 3
|
||||||
};
|
};
|
||||||
|
|
||||||
//methods
|
//methods
|
||||||
@@ -59,5 +59,5 @@ protected:
|
|||||||
|
|
||||||
Image image;
|
Image image;
|
||||||
int posX = 0, posY = 0;
|
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) {
|
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
|
//right mouse button moves the camera
|
||||||
if (event.state & SDL_BUTTON_RMASK) {
|
if (event.state & SDL_BUTTON_RMASK) {
|
||||||
@@ -106,7 +111,14 @@ void ExampleScene::MouseMotion(SDL_MouseMotionEvent const& event) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void ExampleScene::MouseButtonDown(SDL_MouseButtonEvent 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) {
|
switch(event.button) {
|
||||||
case SDL_BUTTON_LEFT: {
|
case SDL_BUTTON_LEFT: {
|
||||||
@@ -129,7 +141,13 @@ void ExampleScene::MouseButtonDown(SDL_MouseButtonEvent const& event) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void ExampleScene::MouseButtonUp(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) {
|
void ExampleScene::MouseWheel(SDL_MouseWheelEvent const& event) {
|
||||||
|
|||||||
Reference in New Issue
Block a user