2 * Copyright (C) 2005-2013 Team XBMC
5 * This Program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2, or (at your option)
10 * This Program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with XBMC; see the file COPYING. If not, see
17 * <http://www.gnu.org/licenses/>.
21 #include "GUIProgressControl.h"
22 #include "GUIInfoManager.h"
23 #include "GUIListItem.h"
24 #include "GUIWindowManager.h"
26 #include "utils/StringUtils.h"
28 CGUIProgressControl::CGUIProgressControl(int parentID, int controlID,
29 float posX, float posY, float width,
30 float height, const CTextureInfo& backGroundTexture,
31 const CTextureInfo& leftTexture,
32 const CTextureInfo& midTexture,
33 const CTextureInfo& rightTexture,
34 const CTextureInfo& overlayTexture,
36 : CGUIControl(parentID, controlID, posX, posY, width, height)
37 , m_guiBackground(posX, posY, width, height, backGroundTexture)
38 , m_guiLeft(posX, posY, width, height, leftTexture)
39 , m_guiMid(posX, posY, width, height, midTexture)
40 , m_guiRight(posX, posY, width, height, rightTexture)
41 , m_guiOverlay(posX, posY, width, height, overlayTexture)
45 ControlType = GUICONTROL_PROGRESS;
50 CGUIProgressControl::~CGUIProgressControl(void)
54 void CGUIProgressControl::SetPosition(float posX, float posY)
56 // everything is positioned based on the background image position
57 CGUIControl::SetPosition(posX, posY);
58 m_guiBackground.SetPosition(posX, posY);
61 void CGUIProgressControl::Process(unsigned int currentTime, CDirtyRegionList &dirtyregions)
66 changed |= UpdateLayout();
67 changed |= m_guiBackground.Process(currentTime);
68 changed |= m_guiMid.Process(currentTime);
69 changed |= m_guiLeft.Process(currentTime);
70 changed |= m_guiRight.Process(currentTime);
71 changed |= m_guiOverlay.Process(currentTime);
76 CGUIControl::Process(currentTime, dirtyregions);
79 void CGUIProgressControl::Render()
83 m_guiBackground.Render();
85 if (m_guiLeft.GetFileName().IsEmpty() && m_guiRight.GetFileName().IsEmpty())
87 if (m_bReveal && !m_guiMidClipRect.IsEmpty())
89 bool restore = g_graphicsContext.SetClipRegion(m_guiMidClipRect.x1, m_guiMidClipRect.y1, m_guiMidClipRect.Width(), m_guiMidClipRect.Height());
92 g_graphicsContext.RestoreClipRegion();
94 else if (!m_bReveal && m_guiMid.GetWidth() > 0)
101 if (m_bReveal && !m_guiMidClipRect.IsEmpty())
103 bool restore = g_graphicsContext.SetClipRegion(m_guiMidClipRect.x1, m_guiMidClipRect.y1, m_guiMidClipRect.Width(), m_guiMidClipRect.Height());
106 g_graphicsContext.RestoreClipRegion();
108 else if (!m_bReveal && m_guiMid.GetWidth() > 0)
114 m_guiOverlay.Render();
117 CGUIControl::Render();
121 bool CGUIProgressControl::CanFocus() const
127 bool CGUIProgressControl::OnMessage(CGUIMessage& message)
129 return CGUIControl::OnMessage(message);
132 void CGUIProgressControl::SetPercentage(float fPercent)
134 fPercent = std::max(0.0f, std::min(fPercent, 100.0f));
135 if (m_fPercent != fPercent)
137 m_fPercent = fPercent;
140 float CGUIProgressControl::GetPercentage() const
144 void CGUIProgressControl::FreeResources(bool immediately)
146 CGUIControl::FreeResources(immediately);
147 m_guiBackground.FreeResources(immediately);
148 m_guiMid.FreeResources(immediately);
149 m_guiRight.FreeResources(immediately);
150 m_guiLeft.FreeResources(immediately);
151 m_guiOverlay.FreeResources(immediately);
154 void CGUIProgressControl::DynamicResourceAlloc(bool bOnOff)
156 CGUIControl::DynamicResourceAlloc(bOnOff);
157 m_guiBackground.DynamicResourceAlloc(bOnOff);
158 m_guiMid.DynamicResourceAlloc(bOnOff);
159 m_guiRight.DynamicResourceAlloc(bOnOff);
160 m_guiLeft.DynamicResourceAlloc(bOnOff);
161 m_guiOverlay.DynamicResourceAlloc(bOnOff);
164 void CGUIProgressControl::AllocResources()
166 CGUIControl::AllocResources();
167 m_guiBackground.AllocResources();
168 m_guiMid.AllocResources();
169 m_guiRight.AllocResources();
170 m_guiLeft.AllocResources();
171 m_guiOverlay.AllocResources();
173 m_guiBackground.SetHeight(25);
174 m_guiRight.SetHeight(20);
175 m_guiLeft.SetHeight(20);
176 m_guiMid.SetHeight(20);
177 m_guiOverlay.SetHeight(20);
180 void CGUIProgressControl::SetInvalid()
182 CGUIControl::SetInvalid();
183 m_guiBackground.SetInvalid();
184 m_guiMid.SetInvalid();
185 m_guiRight.SetInvalid();
186 m_guiLeft.SetInvalid();
187 m_guiOverlay.SetInvalid();
190 void CGUIProgressControl::SetInfo(int iInfo)
195 bool CGUIProgressControl::UpdateColors()
197 bool changed = CGUIControl::UpdateColors();
198 changed |= m_guiBackground.SetDiffuseColor(m_diffuseColor);
199 changed |= m_guiRight.SetDiffuseColor(m_diffuseColor);
200 changed |= m_guiLeft.SetDiffuseColor(m_diffuseColor);
201 changed |= m_guiMid.SetDiffuseColor(m_diffuseColor);
202 changed |= m_guiOverlay.SetDiffuseColor(m_diffuseColor);
207 CStdString CGUIProgressControl::GetDescription() const
209 return StringUtils::Format("%2.f", m_fPercent);
212 bool CGUIProgressControl::UpdateLayout(void)
214 bool bChanged(false);
217 m_width = m_guiBackground.GetTextureWidth();
219 m_height = m_guiBackground.GetTextureHeight();
221 bChanged |= m_guiBackground.SetHeight(m_height);
222 bChanged |= m_guiBackground.SetWidth(m_width);
224 float fScaleX, fScaleY;
225 fScaleY = m_guiBackground.GetTextureHeight() ? m_height / m_guiBackground.GetTextureHeight() : 1.0f;
226 fScaleX = m_guiBackground.GetTextureWidth() ? m_width / m_guiBackground.GetTextureWidth() : 1.0f;
228 float posX = m_guiBackground.GetXPosition();
229 float posY = m_guiBackground.GetYPosition();
231 if (m_guiLeft.GetFileName().IsEmpty() && m_guiRight.GetFileName().IsEmpty())
232 { // rendering without left and right image - fill the mid image completely
233 float width = m_fPercent * m_width * 0.01f;
234 float offset = fabs(fScaleY * 0.5f * (m_guiMid.GetTextureHeight() - m_guiBackground.GetTextureHeight()));
235 if (offset > 0) // Center texture to the background if necessary
236 bChanged |= m_guiMid.SetPosition(posX, posY + offset);
238 bChanged |= m_guiMid.SetPosition(posX, posY);
239 bChanged |= m_guiMid.SetHeight(fScaleY * m_guiMid.GetTextureHeight());
242 bChanged |= m_guiMid.SetWidth(m_width);
243 float x = posX, y = posY + offset, w = width, h = fScaleY * m_guiMid.GetTextureHeight();
244 CRect rect(x, y, x + w, y + h);
245 if (rect != m_guiMidClipRect)
247 m_guiMidClipRect = rect;
253 bChanged |= m_guiMid.SetWidth(width);
254 m_guiMidClipRect = CRect();
259 float fWidth = m_fPercent;
260 float fFullWidth = m_guiBackground.GetTextureWidth() - m_guiLeft.GetTextureWidth() - m_guiRight.GetTextureWidth();
262 fWidth *= fFullWidth;
264 float offset = fabs(fScaleY * 0.5f * (m_guiLeft.GetTextureHeight() - m_guiBackground.GetTextureHeight()));
265 if (offset > 0) // Center texture to the background if necessary
266 bChanged |= m_guiLeft.SetPosition(posX, posY + offset);
268 bChanged |= m_guiLeft.SetPosition(posX, posY);
269 bChanged |= m_guiLeft.SetHeight(fScaleY * m_guiLeft.GetTextureHeight());
270 bChanged |= m_guiLeft.SetWidth(fScaleX * m_guiLeft.GetTextureWidth());
272 posX += fScaleX * m_guiLeft.GetTextureWidth();
273 offset = fabs(fScaleY * 0.5f * (m_guiMid.GetTextureHeight() - m_guiBackground.GetTextureHeight()));
274 if (offset > 0) // Center texture to the background if necessary
275 bChanged |= m_guiMid.SetPosition(posX, posY + offset);
277 bChanged |= m_guiMid.SetPosition(posX, posY);
278 bChanged |= m_guiMid.SetHeight(fScaleY * m_guiMid.GetTextureHeight());
281 bChanged |= m_guiMid.SetWidth(fScaleX * fFullWidth);
282 float x = posX, y = posY + offset, w = fScaleX * fWidth, h = fScaleY * m_guiMid.GetTextureHeight();
283 CRect rect(x, y, x + w, y + h);
284 if (rect != m_guiMidClipRect)
286 m_guiMidClipRect = rect;
292 bChanged |= m_guiMid.SetWidth(fScaleX * fWidth);
293 m_guiMidClipRect = CRect();
296 posX += fWidth * fScaleX;
298 offset = fabs(fScaleY * 0.5f * (m_guiRight.GetTextureHeight() - m_guiBackground.GetTextureHeight()));
299 if (offset > 0) // Center texture to the background if necessary
300 bChanged |= m_guiRight.SetPosition(posX, posY + offset);
302 bChanged |= m_guiRight.SetPosition(posX, posY);
303 bChanged |= m_guiRight.SetHeight(fScaleY * m_guiRight.GetTextureHeight());
304 bChanged |= m_guiRight.SetWidth(fScaleX * m_guiRight.GetTextureWidth());
306 float offset = fabs(fScaleY * 0.5f * (m_guiOverlay.GetTextureHeight() - m_guiBackground.GetTextureHeight()));
307 if (offset > 0) // Center texture to the background if necessary
308 bChanged |= m_guiOverlay.SetPosition(m_guiBackground.GetXPosition(), m_guiBackground.GetYPosition() + offset);
310 bChanged |= m_guiOverlay.SetPosition(m_guiBackground.GetXPosition(), m_guiBackground.GetYPosition());
311 bChanged |= m_guiOverlay.SetHeight(fScaleY * m_guiOverlay.GetTextureHeight());
312 bChanged |= m_guiOverlay.SetWidth(fScaleX * m_guiOverlay.GetTextureWidth());
317 void CGUIProgressControl::UpdateInfo(const CGUIListItem *item)
324 if (g_infoManager.GetInt(value, m_iInfoCode, m_parentID, item))
325 m_fPercent = (float)value;
327 if (m_fPercent < 0.0f) m_fPercent = 0.0f;
328 if (m_fPercent > 100.0f) m_fPercent = 100.0f;