~ubuntu-branches/ubuntu/oneiric/blobandconquer/oneiric

« back to all changes in this revision

Viewing changes to src/gui/CSlider.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Guus Sliepen
  • Date: 2008-06-15 12:04:29 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20080615120429-5ss7cbb4z9mpywj5
Tags: 0.95-1
New upstream release. Closes: #486310

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
Copyright (C) 2006 Parallel Realities
3
 
 
4
 
This program is free software; you can redistribute it and/or
5
 
modify it under the terms of the GNU General Public License
6
 
as published by the Free Software Foundation; either version 2
7
 
of the License, or (at your option) any later version.
8
 
 
9
 
This program is distributed in the hope that it will be useful,
10
 
but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12
 
 
13
 
See the GNU General Public License for more details.
14
 
 
15
 
You should have received a copy of the GNU General Public License
16
 
along with this program; if not, write to the Free Software
17
 
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18
 
 
19
 
*/
20
 
 
21
 
#include "../headers.h"
22
 
 
23
 
Slider::Slider()
24
 
{
25
 
        widgetType = SLIDER;
26
 
        
27
 
        value = 0;
28
 
        
29
 
        min = 0;
30
 
        max = 0;
31
 
        
32
 
        step = 1;
33
 
        
34
 
        dir = 0;
35
 
        
36
 
        smooth = true;
37
 
        
38
 
        changed = false;
39
 
}
40
 
 
41
 
Slider::~Slider()
42
 
{
43
 
}
44
 
 
45
 
void Slider::setProperties(Properties *properties)
46
 
{
47
 
        setBaseValues(properties);
48
 
 
49
 
        min = properties->getInt("min", 0);
50
 
        max = properties->getInt("max", 100);
51
 
        
52
 
        step = properties->getInt("step", 1);
53
 
 
54
 
        value = properties->getInt("value", 0);
55
 
        
56
 
        smooth = properties->getInt("smooth", 1);
57
 
}
58
 
 
59
 
void Slider::use(int mx, int my)
60
 
{
61
 
        if (Collision::collision(mx, my, 1, 1, x + 24, y, width - 48, 16))
62
 
        {
63
 
                float percent;
64
 
                percent = mx - (x + 24);
65
 
                percent /= (width - 48);
66
 
                
67
 
                value = percent * max;
68
 
                
69
 
                changed = true;
70
 
        }
71
 
}
72
 
 
73
 
bool Slider::hasChanged()
74
 
{
75
 
        if (changed)
76
 
        {
77
 
                changed = false;
78
 
                return true;
79
 
        }
80
 
        
81
 
        return false;
82
 
}
83
 
 
84
 
void Slider::update()
85
 
{
86
 
        value += (dir * step * Engine::getInstance()->getTimeDifference(TD_ANIMATION));
87
 
        Math::limit(&value, min, max);
88
 
        
89
 
        if (!smooth)
90
 
        {
91
 
                dir = 0;
92
 
        }
93
 
        
94
 
        if ((dir == -1) || (dir == 1))
95
 
        {
96
 
                changed = true;
97
 
        }
98
 
}
99
 
 
100
 
void Slider::mouseMoved(int mx, int my)
101
 
{
102
 
        if (!mouseHeld)
103
 
        {
104
 
                return;
105
 
        }
106
 
        
107
 
        if ((mx >= x + 24) && (mx <= x + width - 16))
108
 
        {
109
 
                float percent;
110
 
                percent = mx - (x + 24);
111
 
                percent /= (width - 48);
112
 
                
113
 
                value = percent * max;
114
 
                
115
 
                Math::limit(&value, min, max);
116
 
                
117
 
                changed = true;
118
 
        }
119
 
        
120
 
        if (mx < x)
121
 
        {
122
 
                value = 0;
123
 
                
124
 
                changed = true;
125
 
        }
126
 
        
127
 
        if (mx > x + width)
128
 
        {
129
 
                value = max;
130
 
                
131
 
                changed = true;
132
 
        }
133
 
}
134
 
 
135
 
void Slider::mousePressed(SDL_MouseButtonEvent mouse)
136
 
{
137
 
        if (Collision::collision(mouse.x, mouse.y, 1, 1, x, y, 16, 16))
138
 
        {
139
 
                dir = -1;
140
 
        }
141
 
        
142
 
        if (Collision::collision(mouse.x, mouse.y, 1, 1, x + width - 16, y, 16, 16))
143
 
        {
144
 
                dir = 1;
145
 
        }
146
 
        
147
 
        if (Collision::collision(mouse.x, mouse.y, 1, 1, x + 24, y, width - 48, 16))
148
 
        {
149
 
                float percent;
150
 
                percent = mouse.x - (x + 24);
151
 
                percent /= (width - 48);
152
 
                
153
 
                value = percent * max;
154
 
                
155
 
                changed = true;
156
 
        }
157
 
        
158
 
        mouseHeld = true;
159
 
}
160
 
 
161
 
void Slider::mouseReleased(SDL_MouseButtonEvent mouse)
162
 
{
163
 
        dir = 0;
164
 
        mouseHeld = false;
165
 
}
166
 
 
167
 
void Slider::change(int x)
168
 
{
169
 
        value += (x * step);
170
 
        changed = true;
171
 
        Math::limit(&value, min, max);
172
 
}