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
|
/*
* Copyright (C) 2005-2018 Team Kodi
* This file is part of Kodi - https://kodi.tv
*
* SPDX-License-Identifier: GPL-2.0-or-later
* See LICENSES/README.md for more information.
*/
#include "GUIScrollBarControl.h"
#include "GUIMessage.h"
#include "input/Key.h"
#include "input/mouse/MouseStat.h"
#include "utils/StringUtils.h"
#define MIN_NIB_SIZE 4.0f
GUIScrollBarControl::GUIScrollBarControl(int parentID,
int controlID,
float posX,
float posY,
float width,
float height,
const CTextureInfo& backGroundTexture,
const CTextureInfo& barTexture,
const CTextureInfo& barTextureFocus,
const CTextureInfo& nibTexture,
const CTextureInfo& nibTextureFocus,
ORIENTATION orientation,
bool showOnePage)
: CGUIControl(parentID, controlID, posX, posY, width, height),
m_guiBackground(CGUITexture::CreateTexture(posX, posY, width, height, backGroundTexture)),
m_guiBarNoFocus(CGUITexture::CreateTexture(posX, posY, width, height, barTexture)),
m_guiBarFocus(CGUITexture::CreateTexture(posX, posY, width, height, barTextureFocus)),
m_guiNibNoFocus(CGUITexture::CreateTexture(posX, posY, width, height, nibTexture)),
m_guiNibFocus(CGUITexture::CreateTexture(posX, posY, width, height, nibTextureFocus))
{
m_guiNibNoFocus->SetAspectRatio(CAspectRatio::AR_CENTER);
m_guiNibFocus->SetAspectRatio(CAspectRatio::AR_CENTER);
m_numItems = 100;
m_offset = 0;
m_pageSize = 10;
ControlType = GUICONTROL_SCROLLBAR;
m_orientation = orientation;
m_showOnePage = showOnePage;
}
GUIScrollBarControl::GUIScrollBarControl(const GUIScrollBarControl& control)
: CGUIControl(control),
m_guiBackground(control.m_guiBackground->Clone()),
m_guiBarNoFocus(control.m_guiBarNoFocus->Clone()),
m_guiBarFocus(control.m_guiBarFocus->Clone()),
m_guiNibNoFocus(control.m_guiNibNoFocus->Clone()),
m_guiNibFocus(control.m_guiNibFocus->Clone()),
m_numItems(control.m_numItems),
m_pageSize(control.m_pageSize),
m_offset(control.m_offset),
m_showOnePage(control.m_showOnePage),
m_orientation(control.m_orientation)
{
}
void GUIScrollBarControl::Process(unsigned int currentTime, CDirtyRegionList &dirtyregions)
{
bool changed = false;
if (m_bInvalidated)
changed |= UpdateBarSize();
changed |= m_guiBackground->Process(currentTime);
changed |= m_guiBarNoFocus->Process(currentTime);
changed |= m_guiBarFocus->Process(currentTime);
changed |= m_guiNibNoFocus->Process(currentTime);
changed |= m_guiNibFocus->Process(currentTime);
if (changed)
MarkDirtyRegion();
CGUIControl::Process(currentTime, dirtyregions);
}
void GUIScrollBarControl::Render()
{
m_guiBackground->Render();
if (m_bHasFocus)
{
m_guiBarFocus->Render();
m_guiNibFocus->Render();
}
else
{
m_guiBarNoFocus->Render();
m_guiNibNoFocus->Render();
}
CGUIControl::Render();
}
bool GUIScrollBarControl::OnMessage(CGUIMessage& message)
{
switch (message.GetMessage())
{
case GUI_MSG_ITEM_SELECT:
SetValue(message.GetParam1());
return true;
case GUI_MSG_LABEL_RESET:
SetRange(message.GetParam1(), message.GetParam2());
return true;
case GUI_MSG_PAGE_UP:
Move(-1);
return true;
case GUI_MSG_PAGE_DOWN:
Move(1);
return true;
}
return CGUIControl::OnMessage(message);
}
bool GUIScrollBarControl::OnAction(const CAction &action)
{
switch ( action.GetID() )
{
case ACTION_MOVE_LEFT:
if (m_orientation == HORIZONTAL)
{
if(Move( -1))
return true;
}
break;
case ACTION_MOVE_RIGHT:
if (m_orientation == HORIZONTAL)
{
if(Move(1))
return true;
}
break;
case ACTION_MOVE_UP:
if (m_orientation == VERTICAL)
{
if(Move(-1))
return true;
}
break;
case ACTION_MOVE_DOWN:
if (m_orientation == VERTICAL)
{
if(Move(1))
return true;
}
break;
}
return CGUIControl::OnAction(action);
}
bool GUIScrollBarControl::Move(int numSteps)
{
if (numSteps < 0 && m_offset == 0) // we are at the beginning - can't scroll up/left anymore
return false;
if (numSteps > 0 && m_offset == std::max(m_numItems - m_pageSize, 0)) // we are at the end - we can't scroll down/right anymore
return false;
m_offset += numSteps * m_pageSize;
if (m_offset > m_numItems - m_pageSize) m_offset = m_numItems - m_pageSize;
if (m_offset < 0) m_offset = 0;
CGUIMessage message(GUI_MSG_NOTIFY_ALL, GetParentID(), GetID(), GUI_MSG_PAGE_CHANGE, m_offset);
SendWindowMessage(message);
SetInvalid();
return true;
}
void GUIScrollBarControl::SetRange(int pageSize, int numItems)
{
if (m_pageSize != pageSize || m_numItems != numItems)
{
m_pageSize = pageSize;
m_numItems = numItems;
m_offset = 0;
SetInvalid();
}
}
void GUIScrollBarControl::SetValue(int value)
{
if (m_offset != value)
{
m_offset = value;
SetInvalid();
}
}
void GUIScrollBarControl::FreeResources(bool immediately)
{
CGUIControl::FreeResources(immediately);
m_guiBackground->FreeResources(immediately);
m_guiBarNoFocus->FreeResources(immediately);
m_guiBarFocus->FreeResources(immediately);
m_guiNibNoFocus->FreeResources(immediately);
m_guiNibFocus->FreeResources(immediately);
}
void GUIScrollBarControl::DynamicResourceAlloc(bool bOnOff)
{
CGUIControl::DynamicResourceAlloc(bOnOff);
m_guiBackground->DynamicResourceAlloc(bOnOff);
m_guiBarNoFocus->DynamicResourceAlloc(bOnOff);
m_guiBarFocus->DynamicResourceAlloc(bOnOff);
m_guiNibNoFocus->DynamicResourceAlloc(bOnOff);
m_guiNibFocus->DynamicResourceAlloc(bOnOff);
}
void GUIScrollBarControl::AllocResources()
{
CGUIControl::AllocResources();
m_guiBackground->AllocResources();
m_guiBarNoFocus->AllocResources();
m_guiBarFocus->AllocResources();
m_guiNibNoFocus->AllocResources();
m_guiNibFocus->AllocResources();
}
void GUIScrollBarControl::SetInvalid()
{
CGUIControl::SetInvalid();
m_guiBackground->SetInvalid();
m_guiBarFocus->SetInvalid();
m_guiBarFocus->SetInvalid();
m_guiNibNoFocus->SetInvalid();
m_guiNibFocus->SetInvalid();
}
bool GUIScrollBarControl::UpdateBarSize()
{
bool changed = false;
// scale our textures to suit
if (m_orientation == VERTICAL)
{
// calculate the height to display the nib at
float percent = (m_numItems == 0) ? 0 : (float)m_pageSize / m_numItems;
float nibSize = GetHeight() * percent;
if (nibSize < m_guiNibFocus->GetTextureHeight() + 2 * MIN_NIB_SIZE)
nibSize = m_guiNibFocus->GetTextureHeight() + 2 * MIN_NIB_SIZE;
if (nibSize > GetHeight()) nibSize = GetHeight();
changed |= m_guiBarNoFocus->SetHeight(nibSize);
changed |= m_guiBarFocus->SetHeight(nibSize);
changed |= m_guiNibNoFocus->SetHeight(nibSize);
changed |= m_guiNibFocus->SetHeight(nibSize);
// nibSize may be altered by the border size of the nib (and bar).
nibSize = std::max(m_guiBarFocus->GetHeight(), m_guiNibFocus->GetHeight());
// and the position
percent = (m_numItems == m_pageSize) ? 0 : (float)m_offset / (m_numItems - m_pageSize);
float nibPos = (GetHeight() - nibSize) * percent;
if (nibPos < 0) nibPos = 0;
if (nibPos > GetHeight() - nibSize) nibPos = GetHeight() - nibSize;
changed |= m_guiBarNoFocus->SetPosition(GetXPosition(), GetYPosition() + nibPos);
changed |= m_guiBarFocus->SetPosition(GetXPosition(), GetYPosition() + nibPos);
changed |= m_guiNibNoFocus->SetPosition(GetXPosition(), GetYPosition() + nibPos);
changed |= m_guiNibFocus->SetPosition(GetXPosition(), GetYPosition() + nibPos);
}
else
{
// calculate the height to display the nib at
float percent = (m_numItems == 0) ? 0 : (float)m_pageSize / m_numItems;
float nibSize = GetWidth() * percent + 0.5f;
if (nibSize < m_guiNibFocus->GetTextureWidth() + 2 * MIN_NIB_SIZE)
nibSize = m_guiNibFocus->GetTextureWidth() + 2 * MIN_NIB_SIZE;
if (nibSize > GetWidth()) nibSize = GetWidth();
changed |= m_guiBarNoFocus->SetWidth(nibSize);
changed |= m_guiBarFocus->SetWidth(nibSize);
changed |= m_guiNibNoFocus->SetWidth(nibSize);
changed |= m_guiNibFocus->SetWidth(nibSize);
// and the position
percent = (m_numItems == m_pageSize) ? 0 : (float)m_offset / (m_numItems - m_pageSize);
float nibPos = (GetWidth() - nibSize) * percent;
if (nibPos < 0) nibPos = 0;
if (nibPos > GetWidth() - nibSize) nibPos = GetWidth() - nibSize;
changed |= m_guiBarNoFocus->SetPosition(GetXPosition() + nibPos, GetYPosition());
changed |= m_guiBarFocus->SetPosition(GetXPosition() + nibPos, GetYPosition());
changed |= m_guiNibNoFocus->SetPosition(GetXPosition() + nibPos, GetYPosition());
changed |= m_guiNibFocus->SetPosition(GetXPosition() + nibPos, GetYPosition());
}
return changed;
}
void GUIScrollBarControl::SetFromPosition(const CPoint &point)
{
float fPercent;
if (m_orientation == VERTICAL)
fPercent = (point.y - m_guiBackground->GetYPosition() - 0.5f * m_guiBarFocus->GetHeight()) /
(m_guiBackground->GetHeight() - m_guiBarFocus->GetHeight());
else
fPercent = (point.x - m_guiBackground->GetXPosition() - 0.5f * m_guiBarFocus->GetWidth()) /
(m_guiBackground->GetWidth() - m_guiBarFocus->GetWidth());
if (fPercent < 0) fPercent = 0;
if (fPercent > 1) fPercent = 1;
int offset = (int)(floor(fPercent * (m_numItems - m_pageSize) + 0.5f));
if (m_offset != offset)
{
m_offset = offset;
CGUIMessage message(GUI_MSG_NOTIFY_ALL, GetParentID(), GetID(), GUI_MSG_PAGE_CHANGE, m_offset);
SendWindowMessage(message);
SetInvalid();
}
}
EVENT_RESULT GUIScrollBarControl::OnMouseEvent(const CPoint &point, const CMouseEvent &event)
{
if (event.m_id == ACTION_MOUSE_DRAG || event.m_id == ACTION_MOUSE_DRAG_END)
{
if (static_cast<HoldAction>(event.m_state) == HoldAction::DRAG)
{ // we want exclusive access
CGUIMessage msg(GUI_MSG_EXCLUSIVE_MOUSE, GetID(), GetParentID());
SendWindowMessage(msg);
}
else if (static_cast<HoldAction>(event.m_state) == HoldAction::DRAG_END)
{ // we're done with exclusive access
CGUIMessage msg(GUI_MSG_EXCLUSIVE_MOUSE, 0, GetParentID());
SendWindowMessage(msg);
}
SetFromPosition(point);
return EVENT_RESULT_HANDLED;
}
else if (event.m_id == ACTION_MOUSE_LEFT_CLICK && m_guiBackground->HitTest(point))
{
SetFromPosition(point);
return EVENT_RESULT_HANDLED;
}
else if (event.m_id == ACTION_MOUSE_WHEEL_UP)
{
Move(-1);
return EVENT_RESULT_HANDLED;
}
else if (event.m_id == ACTION_MOUSE_WHEEL_DOWN)
{
Move(1);
return EVENT_RESULT_HANDLED;
}
else if (event.m_id == ACTION_GESTURE_NOTIFY)
{
return (m_orientation == HORIZONTAL) ? EVENT_RESULT_PAN_HORIZONTAL_WITHOUT_INERTIA : EVENT_RESULT_PAN_VERTICAL_WITHOUT_INERTIA;
}
else if (event.m_id == ACTION_GESTURE_BEGIN)
{ // grab exclusive access
CGUIMessage msg(GUI_MSG_EXCLUSIVE_MOUSE, GetID(), GetParentID());
SendWindowMessage(msg);
return EVENT_RESULT_HANDLED;
}
else if (event.m_id == ACTION_GESTURE_PAN)
{ // do the drag
SetFromPosition(point);
return EVENT_RESULT_HANDLED;
}
else if (event.m_id == ACTION_GESTURE_END || event.m_id == ACTION_GESTURE_ABORT)
{ // release exclusive access
CGUIMessage msg(GUI_MSG_EXCLUSIVE_MOUSE, 0, GetParentID());
SendWindowMessage(msg);
return EVENT_RESULT_HANDLED;
}
return EVENT_RESULT_UNHANDLED;
}
std::string GUIScrollBarControl::GetDescription() const
{
return StringUtils::Format("{}/{}", m_offset, m_numItems);
}
bool GUIScrollBarControl::UpdateColors(const CGUIListItem* item)
{
bool changed = CGUIControl::UpdateColors(nullptr);
changed |= m_guiBackground->SetDiffuseColor(m_diffuseColor);
changed |= m_guiBarNoFocus->SetDiffuseColor(m_diffuseColor);
changed |= m_guiBarFocus->SetDiffuseColor(m_diffuseColor);
changed |= m_guiNibNoFocus->SetDiffuseColor(m_diffuseColor);
changed |= m_guiNibFocus->SetDiffuseColor(m_diffuseColor);
return changed;
}
bool GUIScrollBarControl::IsVisible() const
{
// page controls can be optionally disabled if the number of pages is 1
if (m_numItems <= m_pageSize && !m_showOnePage)
return false;
return CGUIControl::IsVisible();
}
|