[cstdstring] demise Format, replacing with StringUtils::Format
[vuplus_xbmc] / xbmc / guilib / GUIProgressControl.cpp
1 /*
2  *      Copyright (C) 2005-2013 Team XBMC
3  *      http://xbmc.org
4  *
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)
8  *  any later version.
9  *
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.
14  *
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/>.
18  *
19  */
20
21 #include "GUIProgressControl.h"
22 #include "GUIInfoManager.h"
23 #include "GUIListItem.h"
24 #include "GUIWindowManager.h"
25 #include "FileItem.h"
26 #include "utils/StringUtils.h"
27
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,
35                                          bool reveal)
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)
42 {
43   m_fPercent = 0;
44   m_iInfoCode = 0;
45   ControlType = GUICONTROL_PROGRESS;
46   m_bReveal = reveal;
47   m_bChanged = false;
48 }
49
50 CGUIProgressControl::~CGUIProgressControl(void)
51 {
52 }
53
54 void CGUIProgressControl::SetPosition(float posX, float posY)
55 {
56   // everything is positioned based on the background image position
57   CGUIControl::SetPosition(posX, posY);
58   m_guiBackground.SetPosition(posX, posY);
59 }
60
61 void CGUIProgressControl::Process(unsigned int currentTime, CDirtyRegionList &dirtyregions)
62 {
63   bool changed = false;
64
65   if (!IsDisabled())
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);
72
73   if (changed)
74     MarkDirtyRegion();
75
76   CGUIControl::Process(currentTime, dirtyregions);
77 }
78
79 void CGUIProgressControl::Render()
80 {
81   if (!IsDisabled())
82   {
83     m_guiBackground.Render();
84
85     if (m_guiLeft.GetFileName().IsEmpty() && m_guiRight.GetFileName().IsEmpty())
86     {
87       if (m_bReveal && !m_guiMidClipRect.IsEmpty())
88       {
89         bool restore = g_graphicsContext.SetClipRegion(m_guiMidClipRect.x1, m_guiMidClipRect.y1, m_guiMidClipRect.Width(), m_guiMidClipRect.Height());
90         m_guiMid.Render();
91         if (restore)
92           g_graphicsContext.RestoreClipRegion();
93       }
94       else if (!m_bReveal && m_guiMid.GetWidth() > 0)
95         m_guiMid.Render();
96     }
97     else
98     {
99       m_guiLeft.Render();
100
101       if (m_bReveal && !m_guiMidClipRect.IsEmpty())
102       {
103         bool restore = g_graphicsContext.SetClipRegion(m_guiMidClipRect.x1, m_guiMidClipRect.y1, m_guiMidClipRect.Width(), m_guiMidClipRect.Height());
104         m_guiMid.Render();
105         if (restore)
106           g_graphicsContext.RestoreClipRegion();
107       }
108       else if (!m_bReveal && m_guiMid.GetWidth() > 0)
109         m_guiMid.Render();
110
111       m_guiRight.Render();
112     }
113
114     m_guiOverlay.Render();
115   }
116
117   CGUIControl::Render();
118 }
119
120
121 bool CGUIProgressControl::CanFocus() const
122 {
123   return false;
124 }
125
126
127 bool CGUIProgressControl::OnMessage(CGUIMessage& message)
128 {
129   return CGUIControl::OnMessage(message);
130 }
131
132 void CGUIProgressControl::SetPercentage(float fPercent)
133 {
134   fPercent = std::max(0.0f, std::min(fPercent, 100.0f));
135   if (m_fPercent != fPercent)
136     SetInvalid();
137   m_fPercent = fPercent;
138 }
139
140 float CGUIProgressControl::GetPercentage() const
141 {
142   return m_fPercent;
143 }
144 void CGUIProgressControl::FreeResources(bool immediately)
145 {
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);
152 }
153
154 void CGUIProgressControl::DynamicResourceAlloc(bool bOnOff)
155 {
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);
162 }
163
164 void CGUIProgressControl::AllocResources()
165 {
166   CGUIControl::AllocResources();
167   m_guiBackground.AllocResources();
168   m_guiMid.AllocResources();
169   m_guiRight.AllocResources();
170   m_guiLeft.AllocResources();
171   m_guiOverlay.AllocResources();
172
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);
178 }
179
180 void CGUIProgressControl::SetInvalid()
181 {
182   CGUIControl::SetInvalid();
183   m_guiBackground.SetInvalid();
184   m_guiMid.SetInvalid();
185   m_guiRight.SetInvalid();
186   m_guiLeft.SetInvalid();
187   m_guiOverlay.SetInvalid();
188 }
189
190 void CGUIProgressControl::SetInfo(int iInfo)
191 {
192   m_iInfoCode = iInfo;
193 }
194
195 bool CGUIProgressControl::UpdateColors()
196 {
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);
203
204   return changed;
205 }
206
207 CStdString CGUIProgressControl::GetDescription() const
208 {
209   return StringUtils::Format("%2.f", m_fPercent);
210 }
211
212 bool CGUIProgressControl::UpdateLayout(void)
213 {
214   bool bChanged(false);
215
216   if (m_width == 0)
217     m_width = m_guiBackground.GetTextureWidth();
218   if (m_height == 0)
219     m_height = m_guiBackground.GetTextureHeight();
220
221   bChanged |= m_guiBackground.SetHeight(m_height);
222   bChanged |= m_guiBackground.SetWidth(m_width);
223
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;
227
228   float posX = m_guiBackground.GetXPosition();
229   float posY = m_guiBackground.GetYPosition();
230
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);
237     else
238       bChanged |= m_guiMid.SetPosition(posX, posY);
239     bChanged |= m_guiMid.SetHeight(fScaleY * m_guiMid.GetTextureHeight());
240     if (m_bReveal)
241     {
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)
246       {
247         m_guiMidClipRect = rect;
248         bChanged = true;
249       }
250     }
251     else
252     {
253       bChanged |= m_guiMid.SetWidth(width);
254       m_guiMidClipRect = CRect();
255     }
256   }
257   else
258   {
259     float fWidth = m_fPercent;
260     float fFullWidth = m_guiBackground.GetTextureWidth() - m_guiLeft.GetTextureWidth() - m_guiRight.GetTextureWidth();
261     fWidth /= 100.0f;
262     fWidth *= fFullWidth;
263
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);
267     else
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());
271
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);
276     else
277       bChanged |= m_guiMid.SetPosition(posX, posY);
278     bChanged |= m_guiMid.SetHeight(fScaleY * m_guiMid.GetTextureHeight());
279     if (m_bReveal)
280     {
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)
285       {
286         m_guiMidClipRect = rect;
287         bChanged = true;
288       }
289     }
290     else
291     {
292       bChanged |= m_guiMid.SetWidth(fScaleX * fWidth);
293       m_guiMidClipRect = CRect();
294     }
295
296     posX += fWidth * fScaleX;
297
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);
301     else
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());
305   }
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);
309   else
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());
313
314   return bChanged;
315 }
316
317 void CGUIProgressControl::UpdateInfo(const CGUIListItem *item)
318 {
319   if (!IsDisabled())
320   {
321     if (m_iInfoCode)
322     {
323       int value;
324       if (g_infoManager.GetInt(value, m_iInfoCode, m_parentID, item))
325         m_fPercent = (float)value;
326
327       if (m_fPercent < 0.0f) m_fPercent = 0.0f;
328       if (m_fPercent > 100.0f) m_fPercent = 100.0f;
329     }
330   }
331 }