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
|
/*
* 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 "GUIProgressControl.h"
#include "GUIInfoManager.h"
#include "GUIListItem.h"
#include "GUIMessage.h"
#include "utils/StringUtils.h"
CGUIProgressControl::CGUIProgressControl(int parentID,
int controlID,
float posX,
float posY,
float width,
float height,
const CTextureInfo& backGroundTexture,
const CTextureInfo& leftTexture,
const CTextureInfo& midTexture,
const CTextureInfo& rightTexture,
const CTextureInfo& overlayTexture,
bool reveal)
: CGUIControl(parentID, controlID, posX, posY, width, height),
m_guiBackground(CGUITexture::CreateTexture(posX, posY, width, height, backGroundTexture)),
m_guiLeft(CGUITexture::CreateTexture(posX, posY, width, height, leftTexture)),
m_guiMid(CGUITexture::CreateTexture(posX, posY, width, height, midTexture)),
m_guiRight(CGUITexture::CreateTexture(posX, posY, width, height, rightTexture)),
m_guiOverlay(CGUITexture::CreateTexture(posX, posY, width, height, overlayTexture))
{
m_fPercent = 0;
m_iInfoCode = 0;
ControlType = GUICONTROL_PROGRESS;
m_bReveal = reveal;
m_bChanged = false;
}
CGUIProgressControl::CGUIProgressControl(const CGUIProgressControl& control)
: CGUIControl(control),
m_guiBackground(control.m_guiBackground->Clone()),
m_guiLeft(control.m_guiLeft->Clone()),
m_guiMid(control.m_guiMid->Clone()),
m_guiRight(control.m_guiRight->Clone()),
m_guiOverlay(control.m_guiOverlay->Clone()),
m_guiMidClipRect(control.m_guiMidClipRect),
m_iInfoCode(control.m_iInfoCode),
m_iInfoCode2(control.m_iInfoCode2),
m_fPercent(control.m_fPercent),
m_fPercent2(control.m_fPercent2),
m_bReveal(control.m_bReveal),
m_bChanged(control.m_bChanged)
{
}
void CGUIProgressControl::SetPosition(float posX, float posY)
{
// everything is positioned based on the background image position
CGUIControl::SetPosition(posX, posY);
m_guiBackground->SetPosition(posX, posY);
}
void CGUIProgressControl::Process(unsigned int currentTime, CDirtyRegionList &dirtyregions)
{
bool changed = false;
if (!IsDisabled())
changed |= UpdateLayout();
changed |= m_guiBackground->Process(currentTime);
changed |= m_guiMid->Process(currentTime);
changed |= m_guiLeft->Process(currentTime);
changed |= m_guiRight->Process(currentTime);
changed |= m_guiOverlay->Process(currentTime);
if (changed)
MarkDirtyRegion();
CGUIControl::Process(currentTime, dirtyregions);
}
void CGUIProgressControl::Render()
{
if (!IsDisabled())
{
m_guiBackground->Render();
if (m_guiLeft->GetFileName().empty() && m_guiRight->GetFileName().empty())
{
if (m_bReveal && !m_guiMidClipRect.IsEmpty())
{
bool restore = CServiceBroker::GetWinSystem()->GetGfxContext().SetClipRegion(m_guiMidClipRect.x1, m_guiMidClipRect.y1, m_guiMidClipRect.Width(), m_guiMidClipRect.Height());
m_guiMid->Render();
if (restore)
CServiceBroker::GetWinSystem()->GetGfxContext().RestoreClipRegion();
}
else if (!m_bReveal && m_guiMid->GetWidth() > 0)
m_guiMid->Render();
}
else
{
m_guiLeft->Render();
if (m_bReveal && !m_guiMidClipRect.IsEmpty())
{
bool restore = CServiceBroker::GetWinSystem()->GetGfxContext().SetClipRegion(m_guiMidClipRect.x1, m_guiMidClipRect.y1, m_guiMidClipRect.Width(), m_guiMidClipRect.Height());
m_guiMid->Render();
if (restore)
CServiceBroker::GetWinSystem()->GetGfxContext().RestoreClipRegion();
}
else if (!m_bReveal && m_guiMid->GetWidth() > 0)
m_guiMid->Render();
m_guiRight->Render();
}
m_guiOverlay->Render();
}
CGUIControl::Render();
}
bool CGUIProgressControl::CanFocus() const
{
return false;
}
bool CGUIProgressControl::OnMessage(CGUIMessage& message)
{
if (message.GetMessage() == GUI_MSG_ITEM_SELECT)
{
SetPercentage((float)message.GetParam1());
return true;
}
return CGUIControl::OnMessage(message);
}
void CGUIProgressControl::SetPercentage(float fPercent)
{
fPercent = std::max(0.0f, std::min(fPercent, 100.0f));
if (m_fPercent != fPercent)
SetInvalid();
m_fPercent = fPercent;
}
float CGUIProgressControl::GetPercentage() const
{
return m_fPercent;
}
void CGUIProgressControl::FreeResources(bool immediately)
{
CGUIControl::FreeResources(immediately);
m_guiBackground->FreeResources(immediately);
m_guiMid->FreeResources(immediately);
m_guiRight->FreeResources(immediately);
m_guiLeft->FreeResources(immediately);
m_guiOverlay->FreeResources(immediately);
}
void CGUIProgressControl::DynamicResourceAlloc(bool bOnOff)
{
CGUIControl::DynamicResourceAlloc(bOnOff);
m_guiBackground->DynamicResourceAlloc(bOnOff);
m_guiMid->DynamicResourceAlloc(bOnOff);
m_guiRight->DynamicResourceAlloc(bOnOff);
m_guiLeft->DynamicResourceAlloc(bOnOff);
m_guiOverlay->DynamicResourceAlloc(bOnOff);
}
void CGUIProgressControl::AllocResources()
{
CGUIControl::AllocResources();
m_guiBackground->AllocResources();
m_guiMid->AllocResources();
m_guiRight->AllocResources();
m_guiLeft->AllocResources();
m_guiOverlay->AllocResources();
m_guiBackground->SetHeight(25);
m_guiRight->SetHeight(20);
m_guiLeft->SetHeight(20);
m_guiMid->SetHeight(20);
m_guiOverlay->SetHeight(20);
}
void CGUIProgressControl::SetInvalid()
{
CGUIControl::SetInvalid();
m_guiBackground->SetInvalid();
m_guiMid->SetInvalid();
m_guiRight->SetInvalid();
m_guiLeft->SetInvalid();
m_guiOverlay->SetInvalid();
}
void CGUIProgressControl::SetInfo(int iInfo, int iInfo2)
{
m_iInfoCode = iInfo;
m_iInfoCode2 = iInfo2;
}
bool CGUIProgressControl::UpdateColors(const CGUIListItem* item)
{
bool changed = CGUIControl::UpdateColors(nullptr);
changed |= m_guiBackground->SetDiffuseColor(m_diffuseColor);
changed |= m_guiRight->SetDiffuseColor(m_diffuseColor);
changed |= m_guiLeft->SetDiffuseColor(m_diffuseColor);
changed |= m_guiMid->SetDiffuseColor(m_diffuseColor);
changed |= m_guiOverlay->SetDiffuseColor(m_diffuseColor);
return changed;
}
std::string CGUIProgressControl::GetDescription() const
{
return StringUtils::Format("{:1.0f}", m_fPercent);
}
bool CGUIProgressControl::UpdateLayout(void)
{
bool bChanged(false);
if (m_width == 0)
m_width = m_guiBackground->GetTextureWidth();
if (m_height == 0)
m_height = m_guiBackground->GetTextureHeight();
bChanged |= m_guiBackground->SetHeight(m_height);
bChanged |= m_guiBackground->SetWidth(m_width);
float fScaleX, fScaleY;
fScaleY =
m_guiBackground->GetTextureHeight() ? m_height / m_guiBackground->GetTextureHeight() : 1.0f;
fScaleX =
m_guiBackground->GetTextureWidth() ? m_width / m_guiBackground->GetTextureWidth() : 1.0f;
float posX = m_guiBackground->GetXPosition();
float posY = m_guiBackground->GetYPosition();
if (m_guiLeft->GetFileName().empty() && m_guiRight->GetFileName().empty())
{ // rendering without left and right image - fill the mid image completely
float width = (m_fPercent - m_fPercent2) * m_width * 0.01f;
float offsetX = m_fPercent2 * m_width * 0.01f;;
float offsetY =
fabs(fScaleY * 0.5f * (m_guiMid->GetTextureHeight() - m_guiBackground->GetTextureHeight()));
bChanged |= m_guiMid->SetPosition(posX + (offsetX > 0 ? offsetX : 0),
posY + (offsetY > 0 ? offsetY : 0));
bChanged |= m_guiMid->SetHeight(fScaleY * m_guiMid->GetTextureHeight());
if (m_bReveal)
{
bChanged |= m_guiMid->SetWidth(m_width);
float x = posX + offsetX, y = posY + offsetY, w = width,
h = fScaleY * m_guiMid->GetTextureHeight();
CRect rect(x, y, x + w, y + h);
if (rect != m_guiMidClipRect)
{
m_guiMidClipRect = rect;
bChanged = true;
}
}
else
{
bChanged |= m_guiMid->SetWidth(width);
m_guiMidClipRect = CRect();
}
}
else
{
float fWidth = m_fPercent - m_fPercent2;
float fFullWidth = m_guiBackground->GetTextureWidth() - m_guiLeft->GetTextureWidth() -
m_guiRight->GetTextureWidth();
fWidth /= 100.0f;
fWidth *= fFullWidth;
float offsetY = fabs(fScaleY * 0.5f *
(m_guiLeft->GetTextureHeight() - m_guiBackground->GetTextureHeight()));
bChanged |= m_guiLeft->SetPosition(posX, posY + (offsetY > 0 ? offsetY : 0));
bChanged |= m_guiLeft->SetHeight(fScaleY * m_guiLeft->GetTextureHeight());
bChanged |= m_guiLeft->SetWidth(fScaleX * m_guiLeft->GetTextureWidth());
posX += fScaleX * m_guiLeft->GetTextureWidth();
float offsetX = m_fPercent2 * fWidth * 0.01f;;
offsetY =
fabs(fScaleY * 0.5f * (m_guiMid->GetTextureHeight() - m_guiBackground->GetTextureHeight()));
bChanged |= m_guiMid->SetPosition(posX + offsetX, posY + (offsetY > 0 ? offsetY : 0));
bChanged |= m_guiMid->SetHeight(fScaleY * m_guiMid->GetTextureHeight());
if (m_bReveal)
{
bChanged |= m_guiMid->SetWidth(fScaleX * fFullWidth);
float x = posX + offsetX, y = posY + offsetY, w = fScaleX * fWidth,
h = fScaleY * m_guiMid->GetTextureHeight();
CRect rect(x, y, x + w, y + h);
if (rect != m_guiMidClipRect)
{
m_guiMidClipRect = rect;
bChanged = true;
}
}
else
{
bChanged |= m_guiMid->SetWidth(fScaleX * fWidth);
m_guiMidClipRect = CRect();
}
posX += fWidth * fScaleX;
offsetY = fabs(fScaleY * 0.5f *
(m_guiRight->GetTextureHeight() - m_guiBackground->GetTextureHeight()));
bChanged |= m_guiRight->SetPosition(posX, posY + (offsetY > 0 ? offsetY : 0));
bChanged |= m_guiRight->SetHeight(fScaleY * m_guiRight->GetTextureHeight());
bChanged |= m_guiRight->SetWidth(fScaleX * m_guiRight->GetTextureWidth());
}
float offset = fabs(fScaleY * 0.5f *
(m_guiOverlay->GetTextureHeight() - m_guiBackground->GetTextureHeight()));
if (offset > 0) // Center texture to the background if necessary
bChanged |= m_guiOverlay->SetPosition(m_guiBackground->GetXPosition(),
m_guiBackground->GetYPosition() + offset);
else
bChanged |=
m_guiOverlay->SetPosition(m_guiBackground->GetXPosition(), m_guiBackground->GetYPosition());
bChanged |= m_guiOverlay->SetHeight(fScaleY * m_guiOverlay->GetTextureHeight());
bChanged |= m_guiOverlay->SetWidth(fScaleX * m_guiOverlay->GetTextureWidth());
return bChanged;
}
void CGUIProgressControl::UpdateInfo(const CGUIListItem *item)
{
if (!IsDisabled())
{
if (m_iInfoCode)
{
int value;
if (CServiceBroker::GetGUI()->GetInfoManager().GetInt(value, m_iInfoCode, m_parentID, item))
m_fPercent = std::max(0.0f, std::min(static_cast<float>(value), 100.0f));
}
if (m_iInfoCode2)
{
int value;
if (CServiceBroker::GetGUI()->GetInfoManager().GetInt(value, m_iInfoCode2, m_parentID, item))
m_fPercent2 = std::max(0.0f, std::min(static_cast<float>(value), 100.0f));
}
}
}
|