-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbutton.cpp
More file actions
executable file
·56 lines (44 loc) · 931 Bytes
/
button.cpp
File metadata and controls
executable file
·56 lines (44 loc) · 931 Bytes
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
44
45
46
47
48
49
50
51
52
53
54
55
56
#include "button.h"
#include "ofMain.h"
Button::Button(float px, float py, float width, float height)
: x(px), y(py), w(width), h(height), isDown(false)
{
}
void Button::setup()
{
}
void Button::draw()
{
ofSetColor(isDown ? 220 : 255);
ofDrawRectangle(x, y, w, h);
}
bool Button::isHovered()
{
float mx = ofGetMouseX();
float my = ofGetMouseY();
return (mx >= x && mx <= x + w &&
my >= y && my <= y + h);
}
void Button::update()
{
if (isDown && !ofGetMousePressed(OF_MOUSE_BUTTON_1))
{
fireButtonClicked();
isDown = false;
return;
}
if (!isDown && isHovered() && ofGetMousePressed(OF_MOUSE_BUTTON_1))
{
isDown = true;
}
}
void Button::fireButtonClicked()
{
if (onClick == nullptr) {
return;
}
onClick(*this);
}
void Button::setObserver(std::function<void(const Button&)> onClick) {
this->onClick = onClick;
}