-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathimage-button-widget.cc
103 lines (85 loc) · 3.37 KB
/
image-button-widget.cc
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#include "image-button-widget.hh"
#include "screen.hh"
#include "helpers.hh"
ImageButtonWidget::ImageButtonWidget(Screen *screen,
ResourceCache::Texture texture,
float x,
float y,
float height,
TextAnchor xanchor,
TextAnchor yanchor,
const SDL_Color &activeColor,
const SDL_Color &inactiveColor) :
ImageWidget(screen, texture, x, y, height, xanchor, yanchor),
activeColorR((float) activeColor.r / 255),
activeColorG((float) activeColor.g / 255),
activeColorB((float) activeColor.b / 255),
activeColorA((float) activeColor.a / 255),
inactiveColorR((float) inactiveColor.r / 255),
inactiveColorG((float) inactiveColor.g / 255),
inactiveColorB((float) inactiveColor.b / 255),
inactiveColorA((float) inactiveColor.a / 255),
isActive(false),
mouseDown(false)
{
}
void ImageButtonWidget::HandleEvent(const SDL_Event &e) {
int mousex, mousey;
float xp, yp;
switch (e.type) {
case SDL_MOUSEBUTTONDOWN:
this->mouseDown = this->isActive;
break;
case SDL_MOUSEBUTTONUP:
if (this->isActive && this->mouseDown)
if (this->visible)
this->screen->HandleWidgetEvent(BUTTON_CLICK, this);
this->mouseDown = false;
break;
case SDL_MOUSEMOTION:
SDL_GetMouseState(&mousex, &mousey);
GetRelativeCoords(mousex, mousey, this->screen->window, this->xanchor, this->yanchor, xp, yp);
bool xInRange = false;
bool yInRange = false;
// this->width is expressed as a ratio of screen
// _height_. Calculate the width as a ratio of screen width for
// the following calculations.
int winw, winh;
SDL_GetWindowSize(this->screen->window, &winw, &winh);
float screenRatio = (float) winh / winw;
float w = this->width * screenRatio;
float nx = this->x * screenRatio;
if (this->xanchor == TextAnchor::CENTER)
xInRange = xp >= (nx - w / 2.0) && xp <= (nx + w / 2.0);
else
xInRange = xp >= nx && xp <= (nx + w);
if (this->yanchor == TextAnchor::CENTER)
yInRange = yp >= (this->y - this->height / 2.0) && yp <= (this->y + this->height / 2.0);
else
yInRange = yp >= this->y && yp <= (this->y + this->height);
bool prevIsActive = this->isActive;
this->isActive = xInRange && yInRange;
if (this->isActive)
this->SetColor(this->activeColorR, this->activeColorG, this->activeColorB, this->activeColorA);
else
this->SetColor(this->inactiveColorR, this->inactiveColorG, this->inactiveColorB, this->inactiveColorA);
if (!prevIsActive && this->isActive)
this->screen->HandleWidgetEvent(BUTTON_MOUSE_ENTER, this);
else if (prevIsActive && !this->isActive)
this->screen->HandleWidgetEvent(BUTTON_MOUSE_LEAVE, this);
break;
} // switch (e.type)
this->ImageWidget::HandleEvent(e);
}
void ImageButtonWidget::Advance(float dt) {
this->ImageWidget::Advance(dt);
}
void ImageButtonWidget::Render(Renderer *renderer) {
this->ImageWidget::Render(renderer);
}
void ImageButtonWidget::Reset() {
this->isActive = false;
this->SetColor(this->inactiveColorR, this->inactiveColorG, this->inactiveColorB, this->inactiveColorA);
this->mouseDown = false;
this->ImageWidget::Reset();
}