lotrointerface.com
Search Downloads

LoTROInterface SVN KragenBars

[/] [branches/] [3.21/] [UI/] [Slider.lua] - Blame information for rev 72

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 72 Kragenwar-3044
import "Turbine.UI";
2 Kragenwar-3044
import "Turbine";
3 Kragenwar-3044
 
4 Kragenwar-3044
Slider = class(Turbine.UI.Control);
5 Kragenwar-3044
 
6 Kragenwar-3044
-- colors
7 Kragenwar-3044
Slider.ItemColor = Turbine.UI.Color(245/255, 222/255, 147/255);
8 Kragenwar-3044
Slider.DisabledColor = Turbine.UI.Color(162/255, 162/255, 162/255);
9 Kragenwar-3044
 
10 Kragenwar-3044
function Slider:Constructor()
11 Kragenwar-3044
    Turbine.UI.Control.Constructor(self);
12 Kragenwar-3044
 
13 Kragenwar-3044
    self.step = 1;
14 Kragenwar-3044
    self.min = 0;
15 Kragenwar-3044
    self.max = 0;
16 Kragenwar-3044
    self.value = 0;
17 Kragenwar-3044
    self.format = "%d";
18 Kragenwar-3044
 
19 Kragenwar-3044
    -- text label
20 Kragenwar-3044
    self.label = Turbine.UI.Label();
21 Kragenwar-3044
    self.label:SetParent(self);
22 Kragenwar-3044
    self.label:SetPosition(0, 0);
23 Kragenwar-3044
    self.label:SetFont(Turbine.UI.Lotro.Font.TrajanPro14);
24 Kragenwar-3044
    self.label:SetForeColor(Slider.ItemColor);
25 Kragenwar-3044
    self.label:SetTextAlignment(Turbine.UI.ContentAlignment.TopLeft);
26 Kragenwar-3044
    --self.label:SetMouseVisible(false);
27 Kragenwar-3044
 
28 Kragenwar-3044
    -- value label
29 Kragenwar-3044
    self.valueLabel = Turbine.UI.Label();
30 Kragenwar-3044
    self.valueLabel:SetParent(self);
31 Kragenwar-3044
    self.valueLabel:SetFont(Turbine.UI.Lotro.Font.TrajanPro14);
32 Kragenwar-3044
    self.valueLabel:SetForeColor(Slider.ItemColor);
33 Kragenwar-3044
    self.valueLabel:SetTextAlignment(Turbine.UI.ContentAlignment.TopRight);
34 Kragenwar-3044
    self.valueLabel:SetMouseVisible(false);
35 Kragenwar-3044
 
36 Kragenwar-3044
    -- left arrow
37 Kragenwar-3044
    self.leftArrow = Turbine.UI.Control();
38 Kragenwar-3044
    self.leftArrow:SetParent(self);
39 Kragenwar-3044
    self.leftArrow:SetBackground("KragenPlugs/UI/Resources/slider_leftarrow.tga");
40 Kragenwar-3044
    self.leftArrow:SetBlendMode(Turbine.UI.BlendMode.AlphaBlend);
41 Kragenwar-3044
    self.leftArrow:SetSize(16,16);
42 Kragenwar-3044
    --[[ turbine's slider doesn't do a highlight for some reason
43 Kragenwar-3044
    self.leftArrow.MouseEnter = function(sender, args)
44 Kragenwar-3044
        self.leftArrow:SetBackground("KragenPlugs/UI/Resources/slider_leftarrow_highlight.tga");
45 Kragenwar-3044
    end
46 Kragenwar-3044
    self.leftArrow.MouseLeave = function(sender, args)
47 Kragenwar-3044
        self.leftArrow:SetBackground("KragenPlugs/UI/Resources/slider_leftarrow.tga");
48 Kragenwar-3044
    end
49 Kragenwar-3044
    --]]
50 Kragenwar-3044
    self.leftArrow.MouseDown = function(sender, args)
51 Kragenwar-3044
        if (not self:IsEnabled()) then
52 Kragenwar-3044
            return;
53 Kragenwar-3044
        end
54 Kragenwar-3044
 
55 Kragenwar-3044
        if (args.Button == Turbine.UI.MouseButton.Left) then
56 Kragenwar-3044
            self.leftArrow:SetWantsUpdates(true);
57 Kragenwar-3044
            self.leftArrow:SetBackground("KragenPlugs/UI/Resources/slider_leftarrow_pressed.tga");
58 Kragenwar-3044
            self.leftArrow.tick = Turbine.Engine.GetGameTime();
59 Kragenwar-3044
            self.leftArrow.wait = true;
60 Kragenwar-3044
        end
61 Kragenwar-3044
    end
62 Kragenwar-3044
    self.leftArrow.MouseUp = function(sender, args)
63 Kragenwar-3044
        if (not self:IsEnabled()) then
64 Kragenwar-3044
            return;
65 Kragenwar-3044
        end
66 Kragenwar-3044
 
67 Kragenwar-3044
        if (args.Button == Turbine.UI.MouseButton.Left) then
68 Kragenwar-3044
            self.leftArrow:SetWantsUpdates(false);
69 Kragenwar-3044
            self.leftArrow:SetBackground("KragenPlugs/UI/Resources/slider_leftarrow.tga");
70 Kragenwar-3044
            self:Decrement();
71 Kragenwar-3044
        end
72 Kragenwar-3044
    end
73 Kragenwar-3044
    self.leftArrow.Update = function(sender, args)
74 Kragenwar-3044
        local gameTime = Turbine.Engine.GetGameTime();
75 Kragenwar-3044
        if (self.leftArrow.wait) then
76 Kragenwar-3044
            if ((gameTime - self.leftArrow.tick) > .5) then
77 Kragenwar-3044
                self.leftArrow.wait = false;
78 Kragenwar-3044
            end
79 Kragenwar-3044
        else
80 Kragenwar-3044
            if ((gameTime - self.leftArrow.tick) > .05) then
81 Kragenwar-3044
                self:Decrement();
82 Kragenwar-3044
                self.leftArrow.tick = gameTime;
83 Kragenwar-3044
            end
84 Kragenwar-3044
        end
85 Kragenwar-3044
    end
86 Kragenwar-3044
 
87 Kragenwar-3044
    -- right arrow
88 Kragenwar-3044
    self.rightArrow = Turbine.UI.Control();
89 Kragenwar-3044
    self.rightArrow:SetParent(self);
90 Kragenwar-3044
    self.rightArrow:SetBackground("KragenPlugs/UI/Resources/slider_rightarrow.tga");
91 Kragenwar-3044
    self.rightArrow:SetBlendMode(Turbine.UI.BlendMode.AlphaBlend);
92 Kragenwar-3044
    self.rightArrow:SetSize(16,16);
93 Kragenwar-3044
    --[[ turbine's slider doesn't do a highlight for some reason
94 Kragenwar-3044
    self.rightArrow.MouseEnter = function(sender, args)
95 Kragenwar-3044
        self.rightArrow:SetBackground("KragenPlugs/UI/Resources/slider_rightarrow_highlight.tga");
96 Kragenwar-3044
    end
97 Kragenwar-3044
    self.rightArrow.MouseLeave = function(sender, args)
98 Kragenwar-3044
        self.rightArrow:SetBackground("KragenPlugs/UI/Resources/slider_rightarrow.tga");
99 Kragenwar-3044
    end
100 Kragenwar-3044
    --]]
101 Kragenwar-3044
    self.rightArrow.MouseDown = function(sender, args)
102 Kragenwar-3044
        if (not self:IsEnabled()) then
103 Kragenwar-3044
            return;
104 Kragenwar-3044
        end
105 Kragenwar-3044
 
106 Kragenwar-3044
        if (args.Button == Turbine.UI.MouseButton.Left) then
107 Kragenwar-3044
            self.rightArrow:SetWantsUpdates(true);
108 Kragenwar-3044
            self.rightArrow:SetBackground("KragenPlugs/UI/Resources/slider_rightarrow_pressed.tga");
109 Kragenwar-3044
            self.rightArrow.tick = Turbine.Engine.GetGameTime();
110 Kragenwar-3044
            self.rightArrow.wait = true;
111 Kragenwar-3044
        end
112 Kragenwar-3044
    end
113 Kragenwar-3044
    self.rightArrow.MouseUp = function(sender, args)
114 Kragenwar-3044
        if (not self:IsEnabled()) then
115 Kragenwar-3044
            return;
116 Kragenwar-3044
        end
117 Kragenwar-3044
 
118 Kragenwar-3044
        if (args.Button == Turbine.UI.MouseButton.Left) then
119 Kragenwar-3044
            self.rightArrow:SetWantsUpdates(false);
120 Kragenwar-3044
            self.rightArrow:SetBackground("KragenPlugs/UI/Resources/slider_rightarrow.tga");
121 Kragenwar-3044
            self:Increment();
122 Kragenwar-3044
        end
123 Kragenwar-3044
    end
124 Kragenwar-3044
    self.rightArrow.Update = function(sender, args)
125 Kragenwar-3044
        local gameTime = Turbine.Engine.GetGameTime();
126 Kragenwar-3044
        if (self.rightArrow.wait) then
127 Kragenwar-3044
            if ((gameTime - self.rightArrow.tick) > .5) then
128 Kragenwar-3044
                self.rightArrow.wait = false;
129 Kragenwar-3044
            end
130 Kragenwar-3044
        else
131 Kragenwar-3044
            if ((gameTime - self.rightArrow.tick) > .05) then
132 Kragenwar-3044
                self:Increment();
133 Kragenwar-3044
                self.rightArrow.tick = gameTime;
134 Kragenwar-3044
            end
135 Kragenwar-3044
        end
136 Kragenwar-3044
    end
137 Kragenwar-3044
 
138 Kragenwar-3044
    -- slider area
139 Kragenwar-3044
    self.sliderBox = Turbine.UI.Control();
140 Kragenwar-3044
    self.sliderBox:SetParent(self);
141 Kragenwar-3044
    self.sliderBox:SetBackground("KragenPlugs/UI/Resources/slider_background.tga");
142 Kragenwar-3044
    self.sliderBox:SetBlendMode(Turbine.UI.BlendMode.AlphaBlend);
143 Kragenwar-3044
    self.sliderBox.MouseClick = function(sender, args)
144 Kragenwar-3044
        if (not self:IsEnabled()) then
145 Kragenwar-3044
            return;
146 Kragenwar-3044
        end
147 Kragenwar-3044
 
148 Kragenwar-3044
        if (args.Button == Turbine.UI.MouseButton.Left) then
149 Kragenwar-3044
            local width = self.sliderBox:GetWidth() - 16;
150 Kragenwar-3044
            local x = args.X - 8;
151 Kragenwar-3044
            if (x < 0) then
152 Kragenwar-3044
                x = 0;
153 Kragenwar-3044
            end
154 Kragenwar-3044
            if(x > width) then
155 Kragenwar-3044
                x = width;
156 Kragenwar-3044
            end
157 Kragenwar-3044
            self.slider:SetPosition(x, 0);
158 Kragenwar-3044
            self:UpdateValueFromPosition();
159 Kragenwar-3044
        end
160 Kragenwar-3044
    end
161 Kragenwar-3044
 
162 Kragenwar-3044
    -- slider widget
163 Kragenwar-3044
    self.slider = Turbine.UI.Control();
164 Kragenwar-3044
    self.slider:SetParent(self.sliderBox);
165 Kragenwar-3044
    self.slider:SetBackground("KragenPlugs/UI/Resources/slider_widget.tga");
166 Kragenwar-3044
    self.slider:SetBlendMode(Turbine.UI.BlendMode.AlphaBlend);
167 Kragenwar-3044
    self.slider:SetSize(16,16)
168 Kragenwar-3044
    self.slider.MouseDown = function(sender, args)
169 Kragenwar-3044
        if (not self:IsEnabled()) then
170 Kragenwar-3044
            return;
171 Kragenwar-3044
        end
172 Kragenwar-3044
 
173 Kragenwar-3044
        if (args.Button == Turbine.UI.MouseButton.Left) then
174 Kragenwar-3044
            self.slider.dragStartX = args.X;
175 Kragenwar-3044
            self.slider.dragging = true;
176 Kragenwar-3044
        end
177 Kragenwar-3044
    end
178 Kragenwar-3044
    self.slider.MouseUp = function(sender, args)
179 Kragenwar-3044
        if (not self:IsEnabled()) then
180 Kragenwar-3044
            return;
181 Kragenwar-3044
        end
182 Kragenwar-3044
 
183 Kragenwar-3044
        if (args.Button == Turbine.UI.MouseButton.Left) then
184 Kragenwar-3044
            self.slider.dragging = false;
185 Kragenwar-3044
        end
186 Kragenwar-3044
    end
187 Kragenwar-3044
    self.slider.MouseMove = function(sender, args)
188 Kragenwar-3044
        if (not self:IsEnabled()) then
189 Kragenwar-3044
            return;
190 Kragenwar-3044
        end
191 Kragenwar-3044
 
192 Kragenwar-3044
        if (self.slider.dragging) then
193 Kragenwar-3044
            local left, top = self.slider:GetPosition();
194 Kragenwar-3044
            local width = self.sliderBox:GetWidth() - 16;
195 Kragenwar-3044
 
196 Kragenwar-3044
            local x = left - self.slider.dragStartX + args.X;
197 Kragenwar-3044
            if (x < 0) then
198 Kragenwar-3044
                x = 0;
199 Kragenwar-3044
            end
200 Kragenwar-3044
            if(x > width) then
201 Kragenwar-3044
                x = width;
202 Kragenwar-3044
            end
203 Kragenwar-3044
            self.slider:SetPosition(x, 0);
204 Kragenwar-3044
            self:UpdateValueFromPosition();
205 Kragenwar-3044
        end
206 Kragenwar-3044
    end
207 Kragenwar-3044
end
208 Kragenwar-3044
 
209 Kragenwar-3044
function Slider:SetText(text)
210 Kragenwar-3044
    self.label:SetText(text);
211 Kragenwar-3044
end
212 Kragenwar-3044
 
213 Kragenwar-3044
function Slider:SetSize(width, height)
214 Kragenwar-3044
    self.base.SetSize(self, width, height);
215 Kragenwar-3044
    self:Layout();
216 Kragenwar-3044
end
217 Kragenwar-3044
 
218 Kragenwar-3044
function Slider:SetEnabled(enabled)
219 Kragenwar-3044
    Turbine.UI.Control.SetEnabled(self, enabled);
220 Kragenwar-3044
    if (enabled) then
221 Kragenwar-3044
        self.label:SetForeColor(ComboBox.ItemColor);
222 Kragenwar-3044
        self.valueLabel:SetForeColor(ComboBox.ItemColor);
223 Kragenwar-3044
        self.slider:SetBackground("KragenPlugs/UI/Resources/slider_widget.tga");
224 Kragenwar-3044
    else
225 Kragenwar-3044
        self.label:SetForeColor(Slider.DisabledColor);
226 Kragenwar-3044
        self.valueLabel:SetForeColor(Slider.DisabledColor);
227 Kragenwar-3044
        self.slider:SetBackground("KragenPlugs/UI/Resources/slider_widget_ghosted.tga");
228 Kragenwar-3044
    end
229 Kragenwar-3044
end
230 Kragenwar-3044
 
231 Kragenwar-3044
function Slider:Layout()
232 Kragenwar-3044
    local width, height = self:GetSize();
233 Kragenwar-3044
 
234 Kragenwar-3044
    self.label:SetSize(width * .75, 20);
235 Kragenwar-3044
    self.valueLabel:SetSize(width * .25, 20);
236 Kragenwar-3044
    self.valueLabel:SetPosition(width - (width * .25), 0);
237 Kragenwar-3044
 
238 Kragenwar-3044
    self.sliderBox:SetSize(width - 56, 16);
239 Kragenwar-3044
    self.sliderBox:SetPosition(28, 21);
240 Kragenwar-3044
 
241 Kragenwar-3044
    self.leftArrow:SetPosition(12, 21);
242 Kragenwar-3044
    self.rightArrow:SetPosition(width - 28, 21);
243 Kragenwar-3044
 
244 Kragenwar-3044
    -- update the slider position from the value now that our size has changed
245 Kragenwar-3044
    self:UpdatePositionFromValue();
246 Kragenwar-3044
end
247 Kragenwar-3044
 
248 Kragenwar-3044
function Slider:UpdateValueFromPosition()
249 Kragenwar-3044
    local x, y = self.slider:GetPosition();
250 Kragenwar-3044
    local width = self.sliderBox:GetWidth() - 16;
251 Kragenwar-3044
    local ppv = width / ((self.max - self.min) / self.step);
252 Kragenwar-3044
 
253 Kragenwar-3044
    self.value = (math.floor(x / ppv) * self.step) + self.min;
254 Kragenwar-3044
    self.valueLabel:SetText(string.format(self.format, self.value));
255 Kragenwar-3044
 
256 Kragenwar-3044
    if (type(self.ValueChanged) == "function") then
257 Kragenwar-3044
        self:ValueChanged({value=self.value});
258 Kragenwar-3044
    end
259 Kragenwar-3044
end
260 Kragenwar-3044
 
261 Kragenwar-3044
function Slider:UpdatePositionFromValue()
262 Kragenwar-3044
    local width = self.sliderBox:GetWidth() - 16;
263 Kragenwar-3044
    local ppv = width / ((self.max - self.min) / self.step);
264 Kragenwar-3044
 
265 Kragenwar-3044
    local x = (self.value - self.min) * ppv / self.step;
266 Kragenwar-3044
    self.slider:SetPosition(x, 0);
267 Kragenwar-3044
end
268 Kragenwar-3044
 
269 Kragenwar-3044
function Slider:SetValue(value)
270 Kragenwar-3044
    self.value = value;
271 Kragenwar-3044
    self.valueLabel:SetText(string.format(self.format, self.value));
272 Kragenwar-3044
    self:UpdatePositionFromValue();
273 Kragenwar-3044
 
274 Kragenwar-3044
    if (type(self.ValueChanged) == "function") then
275 Kragenwar-3044
        self:ValueChanged({value=self.value});
276 Kragenwar-3044
    end
277 Kragenwar-3044
end
278 Kragenwar-3044
 
279 Kragenwar-3044
function Slider:GetValue()
280 Kragenwar-3044
    return self.value;
281 Kragenwar-3044
end
282 Kragenwar-3044
 
283 Kragenwar-3044
function Slider:SetStep(step)
284 Kragenwar-3044
    self.step = step;
285 Kragenwar-3044
end
286 Kragenwar-3044
 
287 Kragenwar-3044
function Slider:SetMin(min)
288 Kragenwar-3044
    self.min = min;
289 Kragenwar-3044
end
290 Kragenwar-3044
 
291 Kragenwar-3044
function Slider:SetMax(max)
292 Kragenwar-3044
    self.max = max;
293 Kragenwar-3044
end
294 Kragenwar-3044
 
295 Kragenwar-3044
function Slider:SetFormat(format)
296 Kragenwar-3044
    self.format = format;
297 Kragenwar-3044
end
298 Kragenwar-3044
 
299 Kragenwar-3044
function Slider:Increment()
300 Kragenwar-3044
    local value = self.value + self.step;
301 Kragenwar-3044
    if (value > self.max) then
302 Kragenwar-3044
        value = self.max;
303 Kragenwar-3044
    end
304 Kragenwar-3044
    self:SetValue(value);
305 Kragenwar-3044
end
306 Kragenwar-3044
 
307 Kragenwar-3044
function Slider:Decrement()
308 Kragenwar-3044
    local value = self.value - self.step;
309 Kragenwar-3044
    if (value < self.min) then
310 Kragenwar-3044
        value = self.min;
311 Kragenwar-3044
    end
312 Kragenwar-3044
    self:SetValue(value);
313 Kragenwar-3044
end

All times are GMT -5. The time now is 06:43 PM.


Our Network
EQInterface | EQ2Interface | Minion | WoWInterface | ESOUI | LoTROInterface | MMOUI | Swtorui