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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
|
/*
* Slider.cpp
*
* Created on: Mar 3, 2012
* Author: Simon
*/
#include <iostream>
#include "Slider.h"
#include "Colour.h"
namespace ui {
Slider::Slider(Point position, Point size, int steps):
Component(position, size),
sliderSteps(steps),
sliderPosition(0),
isMouseDown(false),
bgGradient(NULL),
col1(0, 0, 0, 0),
col2(0, 0, 0, 0)
{
// TODO Auto-generated constructor stub
}
void Slider::updatePosition(int position)
{
if(position < 3)
position = 3;
if(position > Size.X-3)
position = Size.X-3;
float fPosition = position-3;
float fSize = Size.X-6;
float fSteps = sliderSteps;
float fSliderPosition = (fPosition/fSize)*sliderSteps;//position;//((x-3)/(Size.X-6))*sliderSteps;
int newSliderPosition = fSliderPosition;
if(newSliderPosition == sliderPosition)
return;
sliderPosition = newSliderPosition;
if(actionCallback)
{
actionCallback->ValueChangedCallback(this);
}
}
void Slider::OnMouseMoved(int x, int y, int dx, int dy)
{
if(isMouseDown)
{
updatePosition(x);
}
}
void Slider::OnMouseClick(int x, int y, unsigned button)
{
isMouseDown = true;
updatePosition(x);
}
void Slider::OnMouseUp(int x, int y, unsigned button)
{
if(isMouseDown)
{
isMouseDown = false;
}
}
int Slider::GetValue()
{
return sliderPosition;
}
void Slider::SetColour(Colour col1, Colour col2)
{
if(bgGradient)
free(bgGradient);
this->col1 = col1;
this->col2 = col2;
bgGradient = (unsigned char*)Graphics::GenerateGradient(
(pixel[2]){PIXRGB(col1.Red, col1.Green, col1.Blue), PIXRGB(col2.Red, col2.Green, col2.Blue)},
(float[2]){0.0f, 1.0f}, 2, Size.X-7);
}
void Slider::SetValue(int value)
{
if(value < 0)
value = 0;
if(value > sliderSteps)
value = sliderSteps;
sliderPosition = value;
}
void Slider::Draw(const Point& screenPos)
{
Graphics * g = Engine::Ref().g;
//g->drawrect(screenPos.X, screenPos.Y, Size.X, Size.Y, 255, 255, 255, 255);
if(bgGradient)
{
#ifndef OGLI
for (int j = 3; j < Size.Y-7; j++)
for (int i = 3; i < Size.X-7; i++)
g->blendpixel(screenPos.X+i+2, screenPos.Y+j+2, bgGradient[(i-3)*3], bgGradient[(i-3)*3+1], bgGradient[(i-3)*3+2], 255);
#else
g->gradientrect(screenPos.X+5, screenPos.Y+5, Size.X-10, Size.Y-10, col1.Red, col1.Green, col1.Blue, col1.Alpha, col2.Red, col2.Green, col2.Blue, col2.Alpha);
#endif
}
g->drawrect(screenPos.X+3, screenPos.Y+3, Size.X-6, Size.Y-6, 255, 255, 255, 255);
float fPosition = sliderPosition;
float fSize = Size.X-6;
float fSteps = sliderSteps;
float fSliderX = (fSize/fSteps)*fPosition;//sliderPosition;//((Size.X-6)/sliderSteps)*sliderPosition;
int sliderX = fSliderX;
sliderX += 3;
g->fillrect(screenPos.X+sliderX-2, screenPos.Y+1, 4, Size.Y-2, 20, 20, 20, 255);
g->drawrect(screenPos.X+sliderX-2, screenPos.Y+1, 4, Size.Y-2, 200, 200, 200, 255);
}
Slider::~Slider()
{
}
} /* namespace ui */
|