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
|
/*
* 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 "GUIFixedListContainer.h"
#include "GUIListItemLayout.h"
#include "input/actions/Action.h"
#include "input/actions/ActionIDs.h"
CGUIFixedListContainer::CGUIFixedListContainer(int parentID, int controlID, float posX, float posY, float width, float height, ORIENTATION orientation, const CScroller& scroller, int preloadItems, int fixedPosition, int cursorRange)
: CGUIBaseContainer(parentID, controlID, posX, posY, width, height, orientation, scroller, preloadItems)
{
ControlType = GUICONTAINER_FIXEDLIST;
m_type = VIEW_TYPE_LIST;
m_fixedCursor = fixedPosition;
m_cursorRange = std::max(0, cursorRange);
SetCursor(m_fixedCursor);
}
CGUIFixedListContainer::~CGUIFixedListContainer(void) = default;
bool CGUIFixedListContainer::OnAction(const CAction &action)
{
switch (action.GetID())
{
case ACTION_PAGE_UP:
{
Scroll(-m_itemsPerPage);
return true;
}
break;
case ACTION_PAGE_DOWN:
{
Scroll(m_itemsPerPage);
return true;
}
break;
// smooth scrolling (for analog controls)
case ACTION_SCROLL_UP:
{
m_analogScrollCount += action.GetAmount() * action.GetAmount();
bool handled = false;
while (m_analogScrollCount > 0.4f)
{
handled = true;
m_analogScrollCount -= 0.4f;
Scroll(-1);
}
return handled;
}
break;
case ACTION_SCROLL_DOWN:
{
m_analogScrollCount += action.GetAmount() * action.GetAmount();
bool handled = false;
while (m_analogScrollCount > 0.4f)
{
handled = true;
m_analogScrollCount -= 0.4f;
Scroll(1);
}
return handled;
}
break;
}
return CGUIBaseContainer::OnAction(action);
}
bool CGUIFixedListContainer::MoveUp(bool wrapAround)
{
int item = GetSelectedItem();
if (item > 0)
SelectItem(item - 1);
else if (wrapAround)
{
SelectItem((int)m_items.size() - 1);
SetContainerMoving(-1);
}
else
return false;
return true;
}
bool CGUIFixedListContainer::MoveDown(bool wrapAround)
{
int item = GetSelectedItem();
if (item < (int)m_items.size() - 1)
SelectItem(item + 1);
else if (wrapAround)
{ // move first item in list
SelectItem(0);
SetContainerMoving(1);
}
else
return false;
return true;
}
void CGUIFixedListContainer::Scroll(int amount)
{
// increase or decrease the offset within [-minCursor, m_items.size() - maxCursor]
int minCursor, maxCursor;
GetCursorRange(minCursor, maxCursor);
const int nextCursor = GetCursor() + amount;
int offset = GetOffset() + amount;
if (offset < -minCursor)
{
offset = -minCursor;
SetCursor(nextCursor < minCursor ? minCursor : nextCursor);
}
if (offset > (int)m_items.size() - 1 - maxCursor)
{
offset = m_items.size() - 1 - maxCursor;
SetCursor(nextCursor > maxCursor ? maxCursor : nextCursor);
}
ScrollToOffset(offset);
}
bool CGUIFixedListContainer::GetOffsetRange(int &minOffset, int &maxOffset) const
{
GetCursorRange(minOffset, maxOffset);
minOffset = -minOffset;
maxOffset = m_items.size() - maxOffset - 1;
return true;
}
void CGUIFixedListContainer::ValidateOffset()
{
if (!m_layout) return;
// ensure our fixed cursor position is valid
if (m_fixedCursor >= m_itemsPerPage)
m_fixedCursor = m_itemsPerPage - 1;
if (m_fixedCursor < 0)
m_fixedCursor = 0;
// compute our minimum and maximum cursor positions
int minCursor, maxCursor;
GetCursorRange(minCursor, maxCursor);
// assure our cursor is between these limits
SetCursor(std::max(GetCursor(), minCursor));
SetCursor(std::min(GetCursor(), maxCursor));
int minOffset, maxOffset;
GetOffsetRange(minOffset, maxOffset);
// and finally ensure our offset is valid
// don't validate offset if we are scrolling in case the tween image exceed <0, 1> range
if (GetOffset() > maxOffset || (!m_scroller.IsScrolling() && m_scroller.GetValue() > maxOffset * m_layout->Size(m_orientation)))
{
SetOffset(std::max(-minCursor, maxOffset));
m_scroller.SetValue(GetOffset() * m_layout->Size(m_orientation));
}
if (GetOffset() < minOffset || (!m_scroller.IsScrolling() && m_scroller.GetValue() < minOffset * m_layout->Size(m_orientation)))
{
SetOffset(minOffset);
m_scroller.SetValue(GetOffset() * m_layout->Size(m_orientation));
}
}
int CGUIFixedListContainer::GetCursorFromPoint(const CPoint &point, CPoint *itemPoint) const
{
if (!m_focusedLayout || !m_layout)
return -1;
int minCursor, maxCursor;
GetCursorRange(minCursor, maxCursor);
// see if the point is either side of our focus range
float start = (minCursor + 0.2f) * m_layout->Size(m_orientation);
float end = (maxCursor - 0.2f) * m_layout->Size(m_orientation) + m_focusedLayout->Size(m_orientation);
float pos = (m_orientation == VERTICAL) ? point.y : point.x;
if (pos >= start && pos <= end)
{ // select the appropriate item
pos -= minCursor * m_layout->Size(m_orientation);
for (int row = minCursor; row <= maxCursor; row++)
{
const CGUIListItemLayout *layout = (row == GetCursor()) ? m_focusedLayout : m_layout;
if (pos < layout->Size(m_orientation))
{
if (!InsideLayout(layout, point))
return -1;
return row;
}
pos -= layout->Size(m_orientation);
}
}
return -1;
}
bool CGUIFixedListContainer::SelectItemFromPoint(const CPoint &point)
{
if (!m_focusedLayout || !m_layout)
return false;
MarkDirtyRegion();
const float mouse_scroll_speed = 0.25f;
const float mouse_max_amount = 1.5f;
float sizeOfItem = m_layout->Size(m_orientation);
int minCursor, maxCursor;
GetCursorRange(minCursor, maxCursor);
// see if the point is either side of our focus range
float start = (minCursor + 0.2f) * sizeOfItem;
float end = (maxCursor - 0.2f) * sizeOfItem + m_focusedLayout->Size(m_orientation);
float pos = (m_orientation == VERTICAL) ? point.y : point.x;
if (pos < start && GetOffset() > -minCursor)
{ // scroll backward
if (!InsideLayout(m_layout, point))
return false;
float amount = std::min((start - pos) / sizeOfItem, mouse_max_amount);
m_analogScrollCount += amount * amount * mouse_scroll_speed;
if (m_analogScrollCount > 1)
{
ScrollToOffset(GetOffset() - 1);
m_analogScrollCount = 0;
}
return true;
}
else if (pos > end && GetOffset() + maxCursor < (int)m_items.size() - 1)
{
if (!InsideLayout(m_layout, point))
return false;
// scroll forward
float amount = std::min((pos - end) / sizeOfItem, mouse_max_amount);
m_analogScrollCount += amount * amount * mouse_scroll_speed;
if (m_analogScrollCount > 1)
{
ScrollToOffset(GetOffset() + 1);
m_analogScrollCount = 0;
}
return true;
}
else
{ // select the appropriate item
int cursor = GetCursorFromPoint(point);
if (cursor < 0)
return false;
// calling SelectItem() here will focus the item and scroll, which isn't really what we're after
SetCursor(cursor);
return true;
}
}
void CGUIFixedListContainer::SelectItem(int item)
{
// Check that GetOffset() is valid
ValidateOffset();
// only select an item if it's in a valid range
if (item >= 0 && item < (int)m_items.size())
{
// Select the item requested - we first set the cursor position
// which may be different at either end of the list, then the offset
int minCursor, maxCursor;
GetCursorRange(minCursor, maxCursor);
int cursor;
if ((int)m_items.size() - 1 - item <= maxCursor - m_fixedCursor)
cursor = std::max(m_fixedCursor, maxCursor + item - (int)m_items.size() + 1);
else if (item <= m_fixedCursor - minCursor)
cursor = std::min(m_fixedCursor, minCursor + item);
else
cursor = m_fixedCursor;
if (cursor != GetCursor())
SetContainerMoving(cursor - GetCursor());
SetCursor(cursor);
ScrollToOffset(item - GetCursor());
MarkDirtyRegion();
}
}
bool CGUIFixedListContainer::HasPreviousPage() const
{
return (GetOffset() > 0);
}
bool CGUIFixedListContainer::HasNextPage() const
{
return (GetOffset() < (int)m_items.size() - m_itemsPerPage && (int)m_items.size() >= m_itemsPerPage);
}
int CGUIFixedListContainer::GetCurrentPage() const
{
int offset = CorrectOffset(GetOffset(), GetCursor());
if (offset + m_itemsPerPage - GetCursor() >= (int)GetRows()) // last page
return (GetRows() + m_itemsPerPage - 1) / m_itemsPerPage;
return offset / m_itemsPerPage + 1;
}
void CGUIFixedListContainer::GetCursorRange(int &minCursor, int &maxCursor) const
{
minCursor = std::max(m_fixedCursor - m_cursorRange, 0);
maxCursor = std::min(m_fixedCursor + m_cursorRange, m_itemsPerPage);
if (!m_items.size())
{
minCursor = m_fixedCursor;
maxCursor = m_fixedCursor;
return;
}
while (maxCursor - minCursor > (int)m_items.size() - 1)
{
if (maxCursor - m_fixedCursor > m_fixedCursor - minCursor)
maxCursor--;
else
minCursor++;
}
}
|