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 "GUIResizeControl.h"
22 #include "GUIWindowManager.h"
24 #include "utils/TimeUtils.h"
26 // time to reset accelerated cursors (digital movement)
27 #define MOVE_TIME_OUT 500L
29 CGUIResizeControl::CGUIResizeControl(int parentID, int controlID, float posX, float posY, float width, float height, const CTextureInfo& textureFocus, const CTextureInfo& textureNoFocus)
30 : CGUIControl(parentID, controlID, posX, posY, width, height)
31 , m_imgFocus(posX, posY, width, height, textureFocus)
32 , m_imgNoFocus(posX, posY, width, height, textureNoFocus)
37 m_fAnalogSpeed = 2.0f; // TODO: implement correct analog speed
38 m_fAcceleration = 0.2f; // TODO: implement correct computation of acceleration
39 m_fMaxSpeed = 10.0; // TODO: implement correct computation of maxspeed
40 ControlType = GUICONTROL_RESIZE;
41 SetLimits(0, 0, 720, 576); // defaults
42 m_nDirection = DIRECTION_NONE;
45 CGUIResizeControl::~CGUIResizeControl(void)
48 void CGUIResizeControl::Process(unsigned int currentTime, CDirtyRegionList &dirtyregions)
52 m_imgFocus.SetWidth(m_width);
53 m_imgFocus.SetHeight(m_height);
55 m_imgNoFocus.SetWidth(m_width);
56 m_imgNoFocus.SetHeight(m_height);
60 unsigned int alphaCounter = m_frameCounter + 2;
61 unsigned int alphaChannel;
62 if ((alphaCounter % 128) >= 64)
63 alphaChannel = alphaCounter % 64;
65 alphaChannel = 63 - (alphaCounter % 64);
68 if (SetAlpha( (unsigned char)alphaChannel ))
70 m_imgFocus.SetVisible(true);
71 m_imgNoFocus.SetVisible(false);
78 m_imgFocus.SetVisible(false);
79 m_imgNoFocus.SetVisible(true);
81 m_imgFocus.Process(currentTime);
82 m_imgNoFocus.Process(currentTime);
83 CGUIControl::Process(currentTime, dirtyregions);
86 void CGUIResizeControl::Render()
89 m_imgNoFocus.Render();
90 CGUIControl::Render();
93 bool CGUIResizeControl::OnAction(const CAction &action)
95 if (action.GetID() == ACTION_SELECT_ITEM)
97 // button selected - send message to parent
98 CGUIMessage message(GUI_MSG_CLICKED, GetID(), GetParentID());
99 SendWindowMessage(message);
102 if (action.GetID() == ACTION_ANALOG_MOVE)
104 Resize(m_fAnalogSpeed*action.GetAmount(), -m_fAnalogSpeed*action.GetAmount(1));
107 return CGUIControl::OnAction(action);
110 void CGUIResizeControl::OnUp()
112 UpdateSpeed(DIRECTION_UP);
113 Resize(0, -m_fSpeed);
116 void CGUIResizeControl::OnDown()
118 UpdateSpeed(DIRECTION_DOWN);
122 void CGUIResizeControl::OnLeft()
124 UpdateSpeed(DIRECTION_LEFT);
125 Resize(-m_fSpeed, 0);
128 void CGUIResizeControl::OnRight()
130 UpdateSpeed(DIRECTION_RIGHT);
134 EVENT_RESULT CGUIResizeControl::OnMouseEvent(const CPoint &point, const CMouseEvent &event)
136 if (event.m_id == ACTION_MOUSE_DRAG)
138 if (event.m_state == 1)
139 { // grab exclusive access
140 CGUIMessage msg(GUI_MSG_EXCLUSIVE_MOUSE, GetID(), GetParentID());
141 SendWindowMessage(msg);
143 else if (event.m_state == 3)
144 { // release exclusive access
145 CGUIMessage msg(GUI_MSG_EXCLUSIVE_MOUSE, 0, GetParentID());
146 SendWindowMessage(msg);
148 Resize(event.m_offsetX, event.m_offsetY);
149 return EVENT_RESULT_HANDLED;
151 return EVENT_RESULT_UNHANDLED;
154 void CGUIResizeControl::UpdateSpeed(int nDirection)
156 if (CTimeUtils::GetFrameTime() - m_lastMoveTime > MOVE_TIME_OUT)
159 m_nDirection = DIRECTION_NONE;
161 m_lastMoveTime = CTimeUtils::GetFrameTime();
162 if (nDirection == m_nDirection)
164 m_fSpeed += m_fAcceleration;
165 if (m_fSpeed > m_fMaxSpeed) m_fSpeed = m_fMaxSpeed;
168 { // reset direction and speed
170 m_nDirection = nDirection;
174 void CGUIResizeControl::AllocResources()
176 CGUIControl::AllocResources();
178 m_imgFocus.AllocResources();
179 m_imgNoFocus.AllocResources();
180 m_width = m_imgFocus.GetWidth();
181 m_height = m_imgFocus.GetHeight();
184 void CGUIResizeControl::FreeResources(bool immediately)
186 CGUIControl::FreeResources(immediately);
187 m_imgFocus.FreeResources(immediately);
188 m_imgNoFocus.FreeResources(immediately);
191 void CGUIResizeControl::DynamicResourceAlloc(bool bOnOff)
193 CGUIControl::DynamicResourceAlloc(bOnOff);
194 m_imgFocus.DynamicResourceAlloc(bOnOff);
195 m_imgNoFocus.DynamicResourceAlloc(bOnOff);
198 void CGUIResizeControl::SetInvalid()
200 CGUIControl::SetInvalid();
201 m_imgFocus.SetInvalid();
202 m_imgNoFocus.SetInvalid();
205 void CGUIResizeControl::Resize(float x, float y)
207 float width = m_width + x;
208 float height = m_height + y;
209 // check if we are within the bounds
210 if (width < m_x1) width = m_x1;
211 if (height < m_y1) height = m_y1;
212 if (width > m_x2) width = m_x2;
213 if (height > m_y2) height = m_y2;
214 // ok, now set the default size of the resize control
219 void CGUIResizeControl::SetPosition(float posX, float posY)
221 CGUIControl::SetPosition(posX, posY);
222 m_imgFocus.SetPosition(posX, posY);
223 m_imgNoFocus.SetPosition(posX, posY);
226 bool CGUIResizeControl::SetAlpha(unsigned char alpha)
228 return m_imgFocus.SetAlpha(alpha) |
229 m_imgNoFocus.SetAlpha(alpha);
232 bool CGUIResizeControl::UpdateColors()
234 bool changed = CGUIControl::UpdateColors();
235 changed |= m_imgFocus.SetDiffuseColor(m_diffuseColor);
236 changed |= m_imgNoFocus.SetDiffuseColor(m_diffuseColor);
241 void CGUIResizeControl::SetLimits(float x1, float y1, float x2, float y2)