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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
|
/*
* Copyright (C) 2005-2008 Team XBMC
* http://www.xbmc.org
*
* This Program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This Program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with XBMC; see the file COPYING. If not, write to
* the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
* http://www.gnu.org/copyleft/gpl.html
*
*/
#include "GUISelectButtonControl.h"
#include "GUIWindowManager.h"
#include "utils/CharsetConverter.h"
#include "utils/TimeUtils.h"
#include "Key.h"
CGUISelectButtonControl::CGUISelectButtonControl(int parentID, int controlID,
float posX, float posY,
float width, float height,
const CTextureInfo& buttonFocus,
const CTextureInfo& button,
const CLabelInfo& labelInfo,
const CTextureInfo& selectBackground,
const CTextureInfo& selectArrowLeft,
const CTextureInfo& selectArrowLeftFocus,
const CTextureInfo& selectArrowRight,
const CTextureInfo& selectArrowRightFocus
)
: CGUIButtonControl(parentID, controlID, posX, posY, width, height, buttonFocus, button, labelInfo)
, m_imgBackground(posX, posY, width, height, selectBackground)
, m_imgLeft(posX, posY, 16, 16, selectArrowLeft)
, m_imgLeftFocus(posX, posY, 16, 16, selectArrowLeftFocus)
, m_imgRight(posX, posY, 16, 16, selectArrowRight)
, m_imgRightFocus(posX, posY, 16, 16, selectArrowRightFocus)
{
m_bShowSelect = false;
m_iCurrentItem = -1;
m_iDefaultItem = -1;
m_iStartFrame = 0;
m_bLeftSelected = false;
m_bRightSelected = false;
m_bMovedLeft = false;
m_bMovedRight = false;
m_ticks = 0;
m_label.SetAlign(m_label.GetLabelInfo().align | XBFONT_CENTER_X);
ControlType = GUICONTROL_SELECTBUTTON;
}
CGUISelectButtonControl::~CGUISelectButtonControl(void)
{}
void CGUISelectButtonControl::Render()
{
if (m_bInvalidated)
{
m_imgBackground.SetWidth(m_width);
m_imgBackground.SetHeight(m_height);
}
// Are we in selection mode
if (m_bShowSelect)
{
// render background, left and right arrow
m_imgBackground.Render();
CGUILabel::COLOR color = CGUILabel::COLOR_TEXT;
// User has moved left...
if (m_bMovedLeft)
{
m_iStartFrame++;
if (m_iStartFrame >= 10)
{
m_iStartFrame = 0;
m_bMovedLeft = false;
}
// If we are moving left
// render item text as disabled
color = CGUILabel::COLOR_DISABLED;
}
// Render arrow
if (m_bLeftSelected || m_bMovedLeft)
m_imgLeftFocus.Render();
else
m_imgLeft.Render();
// User has moved right...
if (m_bMovedRight)
{
m_iStartFrame++;
if (m_iStartFrame >= 10)
{
m_iStartFrame = 0;
m_bMovedRight = false;
}
// If we are moving right
// render item text as disabled
color = CGUILabel::COLOR_DISABLED;
}
// Render arrow
if (m_bRightSelected || m_bMovedRight)
m_imgRightFocus.Render();
else
m_imgRight.Render();
// Render text if a current item is available
if (m_iCurrentItem >= 0 && (unsigned)m_iCurrentItem < m_vecItems.size())
{
m_label.SetMaxRect(m_posX, m_posY, m_width, m_height);
m_label.SetText(m_vecItems[m_iCurrentItem]);
m_label.SetColor(color);
m_label.Render();
}
// Select current item, if user doesn't
// move left or right for 1.5 sec.
unsigned int ticksSpan = CTimeUtils::GetFrameTime() - m_ticks;
if (ticksSpan > 1500)
{
// User hasn't moved disable selection mode...
m_bShowSelect = false;
// ...and send a thread message.
// (Sending a message with SendMessage
// can result in a GPF.)
CGUIMessage message(GUI_MSG_CLICKED, GetID(), GetParentID() );
g_windowManager.SendThreadMessage(message);
}
} // if (m_bShowSelect)
else
{
// No, render a normal button
CGUIButtonControl::Render();
}
CGUIControl::Render();
}
bool CGUISelectButtonControl::OnMessage(CGUIMessage& message)
{
if ( message.GetControlId() == GetID() )
{
if (message.GetMessage() == GUI_MSG_LABEL_ADD)
{
if (m_vecItems.size() <= 0)
{
m_iCurrentItem = 0;
m_iDefaultItem = 0;
}
m_vecItems.push_back(message.GetLabel());
return true;
}
else if (message.GetMessage() == GUI_MSG_LABEL_RESET)
{
m_vecItems.erase(m_vecItems.begin(), m_vecItems.end());
m_iCurrentItem = -1;
m_iDefaultItem = -1;
return true;
}
else if (message.GetMessage() == GUI_MSG_ITEM_SELECTED)
{
message.SetParam1(m_iCurrentItem);
if (m_iCurrentItem >= 0 && m_iCurrentItem < (int)m_vecItems.size())
message.SetLabel(m_vecItems[m_iCurrentItem]);
return true;
}
else if (message.GetMessage() == GUI_MSG_ITEM_SELECT)
{
m_iDefaultItem = m_iCurrentItem = message.GetParam1();
return true;
}
}
return CGUIButtonControl::OnMessage(message);
}
bool CGUISelectButtonControl::OnAction(const CAction &action)
{
if (!m_bShowSelect)
{
if (action.GetID() == ACTION_SELECT_ITEM)
{
// Enter selection mode
m_bShowSelect = true;
// Start timer, if user doesn't select an item
// or moves left/right. The control will
// automatically select the current item.
m_ticks = CTimeUtils::GetFrameTime();
return true;
}
else
return CGUIButtonControl::OnAction(action);
}
else
{
if (action.GetID() == ACTION_SELECT_ITEM)
{
// User has selected an item, disable selection mode...
m_bShowSelect = false;
// ...and send a message.
CGUIMessage message(GUI_MSG_CLICKED, GetID(), GetParentID() );
SendWindowMessage(message);
return true;
}
if (action.GetID() == ACTION_MOVE_UP || action.GetID() == ACTION_MOVE_DOWN )
{
// Disable selection mode when moving up or down
m_bShowSelect = false;
m_iCurrentItem = m_iDefaultItem;
}
// call the base class
return CGUIButtonControl::OnAction(action);
}
}
void CGUISelectButtonControl::FreeResources()
{
CGUIButtonControl::FreeResources();
m_imgBackground.FreeResources();
m_imgLeft.FreeResources();
m_imgLeftFocus.FreeResources();
m_imgRight.FreeResources();
m_imgRightFocus.FreeResources();
m_bShowSelect = false;
}
void CGUISelectButtonControl::DynamicResourceAlloc(bool bOnOff)
{
CGUIControl::DynamicResourceAlloc(bOnOff);
m_imgBackground.DynamicResourceAlloc(bOnOff);
m_imgLeft.DynamicResourceAlloc(bOnOff);
m_imgLeftFocus.DynamicResourceAlloc(bOnOff);
m_imgRight.DynamicResourceAlloc(bOnOff);
m_imgRightFocus.DynamicResourceAlloc(bOnOff);
}
void CGUISelectButtonControl::AllocResources()
{
CGUIButtonControl::AllocResources();
m_imgBackground.AllocResources();
m_imgLeft.AllocResources();
m_imgLeftFocus.AllocResources();
m_imgRight.AllocResources();
m_imgRightFocus.AllocResources();
// Position right arrow
float posX = (m_posX + m_width - 8) - 16;
float posY = m_posY + (m_height - 16) / 2;
m_imgRight.SetPosition(posX, posY);
m_imgRightFocus.SetPosition(posX, posY);
// Position left arrow
posX = m_posX + 8;
m_imgLeft.SetPosition(posX, posY);
m_imgLeftFocus.SetPosition(posX, posY);
}
void CGUISelectButtonControl::SetInvalid()
{
CGUIControl::SetInvalid();
m_imgBackground.SetInvalid();
m_imgLeft.SetInvalid();
m_imgLeftFocus.SetInvalid();
m_imgRight.SetInvalid();
m_imgRightFocus.SetInvalid();
}
void CGUISelectButtonControl::OnLeft()
{
if (m_bShowSelect)
{
// Set for visual feedback
m_bMovedLeft = true;
m_iStartFrame = 0;
// Reset timer for automatically selecting
// the current item.
m_ticks = CTimeUtils::GetFrameTime();
// Switch to previous item
if (m_vecItems.size() > 0)
{
m_iCurrentItem--;
if (m_iCurrentItem < 0)
m_iCurrentItem = (int)m_vecItems.size() - 1;
}
}
else
{ // use the base class
CGUIButtonControl::OnLeft();
}
}
void CGUISelectButtonControl::OnRight()
{
if (m_bShowSelect)
{
// Set for visual feedback
m_bMovedRight = true;
m_iStartFrame = 0;
// Reset timer for automatically selecting
// the current item.
m_ticks = CTimeUtils::GetFrameTime();
// Switch to next item
if (m_vecItems.size() > 0)
{
m_iCurrentItem++;
if (m_iCurrentItem >= (int)m_vecItems.size())
m_iCurrentItem = 0;
}
}
else
{ // use the base class
CGUIButtonControl::OnRight();
}
}
bool CGUISelectButtonControl::OnMouseOver(const CPoint &point)
{
bool ret = CGUIControl::OnMouseOver(point);
m_bLeftSelected = false;
m_bRightSelected = false;
if (m_imgLeft.HitTest(point))
{ // highlight the left control, but don't start moving until we have clicked
m_bLeftSelected = true;
}
if (m_imgRight.HitTest(point))
{ // highlight the right control, but don't start moving until we have clicked
m_bRightSelected = true;
}
// reset ticks
m_ticks = CTimeUtils::GetFrameTime();
return ret;
}
bool CGUISelectButtonControl::OnMouseEvent(const CPoint &point, const CMouseEvent &event)
{
if (event.m_id == ACTION_MOUSE_LEFT_CLICK)
{
if (m_bShowSelect && m_imgLeft.HitTest(point))
OnLeft();
else if (m_bShowSelect && m_imgRight.HitTest(point))
OnRight();
else // normal select
CGUIButtonControl::OnMouseEvent(point, event);
return true;
}
else if (event.m_id == ACTION_MOUSE_WHEEL_UP)
{
OnLeft();
return true;
}
else if (event.m_id == ACTION_MOUSE_WHEEL_DOWN)
{
OnRight();
return true;
}
return false;
}
void CGUISelectButtonControl::SetPosition(float posX, float posY)
{
float leftOffX = m_imgLeft.GetXPosition() - m_posX;
float leftOffY = m_imgLeft.GetYPosition() - m_posY;
float rightOffX = m_imgRight.GetXPosition() - m_posX;
float rightOffY = m_imgRight.GetYPosition() - m_posY;
float backOffX = m_imgBackground.GetXPosition() - m_posX;
float backOffY = m_imgBackground.GetYPosition() - m_posY;
CGUIButtonControl::SetPosition(posX, posY);
m_imgLeft.SetPosition(posX + leftOffX, posY + leftOffY);
m_imgLeftFocus.SetPosition(posX + leftOffX, posY + leftOffY);
m_imgRight.SetPosition(posX + rightOffX, posY + rightOffY);
m_imgRightFocus.SetPosition(posX + rightOffX, posY + rightOffY);
m_imgBackground.SetPosition(posX + backOffX, posY + backOffY);
}
void CGUISelectButtonControl::UpdateColors()
{
CGUIButtonControl::UpdateColors();
m_imgLeft.SetDiffuseColor(m_diffuseColor);
m_imgLeftFocus.SetDiffuseColor(m_diffuseColor);
m_imgRight.SetDiffuseColor(m_diffuseColor);
m_imgRightFocus.SetDiffuseColor(m_diffuseColor);
m_imgBackground.SetDiffuseColor(m_diffuseColor);
}
|