blob: 0a61f359be885e0f8b946fc0b5d927228c60eb7e (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
#pragma once
#include "../globals.hpp"
#include "rectangle.hpp"
namespace Framework {
class Button : public Rectangle {
public:
Button(int x, int y, int w, int h)
: Rectangle{x, y, w, h} {
SetColor(Config::BUTTON_COLOR);
}
virtual void HandleEvent(const SDL_Event& E) {
if (isDisabled) return;
if (E.type == SDL_MOUSEMOTION) {
HandleMouseMotion(E.motion);
} else if (E.type == SDL_MOUSEBUTTONDOWN) {
if (IsWithinBounds(E.button.x, E.button.y)) {
E.button.button == SDL_BUTTON_LEFT
? HandleLeftClick()
: HandleRightClick();
}
}
}
void SetIsDisabled(bool newVal) { isDisabled = newVal; }
protected:
virtual void HandleLeftClick(){}
virtual void HandleRightClick(){}
virtual void HandleMouseMotion(const SDL_MouseMotionEvent& E) {
if (IsWithinBounds(E.x, E.y)) {
SetColor(Config::BUTTON_HOVER_COLOR);
} else {
SetColor(Config::BUTTON_COLOR);
}
}
private:
bool isDisabled{ false };
};
}
|