-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSlider.cpp
79 lines (64 loc) · 1.72 KB
/
Slider.cpp
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
#include <stdio.h>
#include <string.h>
#include "Slider.h"
#include "global.h"
const int widthOftarget = 15;
const int heightOftarget = 30;
Slider::Slider(int pos_x = 0, int pos_y = 0)
{
track_x = pos_x;
track_y = pos_y;
target_x = track_x + lengthOftrack/2;
target_y = track_y;
target_color = al_map_rgb(150, 150, 150);
degree = 0.5;
font = al_load_ttf_font("Caviar_Dreams_Bold.ttf", font_size, 0); // load font
strncpy(label, "label", 20);
}
Slider::~Slider()
{
al_destroy_font(font);
}
void
Slider::Draw()
{
int draw_x = target_x - widthOftarget/2;
int draw_y = target_y - heightOftarget/2;
al_draw_line(track_x, track_y, track_x + lengthOftrack, track_y, al_map_rgb(255, 255, 255), 2);
al_draw_filled_rectangle(draw_x, draw_y, draw_x + widthOftarget, draw_y + heightOftarget, target_color);
al_draw_text(font, al_map_rgb(255, 255, 255), field_width+12, track_y - 37.5, ALLEGRO_ALIGN_LEFT, label);
}
void
Slider::set_color(ALLEGRO_COLOR color)
{
target_color = color;
}
void
Slider::set_label_content(const char *content)
{
strncpy(label, content, 20);
}
float
Slider::Drag(int update_x, int update_y)
{
if(update_x < target_x)
target_x = max(update_x, track_x);
else
target_x = min(update_x, track_x + lengthOftrack);
degree = ((float)(target_x - track_x) / (float)lengthOftrack);
return degree;
}
bool
Slider::isClicked(int mouse_x, int mouse_y)
{
int real_x = target_x - widthOftarget/2;
int real_y = target_y - heightOftarget/2;
if(mouse_x >= real_x && mouse_x <= real_x + widthOftarget)
{
if(mouse_y >= real_y && mouse_y <= real_y + heightOftarget)
{
return true;
}
}
return false;
}