Merge pull request #22816 from CastagnaIT/fix_tx3g
[xbmc.git] / xbmc / guilib / GUIResizeControl.cpp
blob83f79d4310e05c674f0cb0db888fea123c3f0f57
1 /*
2 * Copyright (C) 2005-2018 Team Kodi
3 * This file is part of Kodi - https://kodi.tv
5 * SPDX-License-Identifier: GPL-2.0-or-later
6 * See LICENSES/README.md for more information.
7 */
9 #include "GUIResizeControl.h"
11 #include "GUIMessage.h"
12 #include "input/Key.h"
13 #include "input/mouse/MouseStat.h"
15 using namespace UTILS;
17 CGUIResizeControl::CGUIResizeControl(int parentID,
18 int controlID,
19 float posX,
20 float posY,
21 float width,
22 float height,
23 const CTextureInfo& textureFocus,
24 const CTextureInfo& textureNoFocus,
25 UTILS::MOVING_SPEED::MapEventConfig& movingSpeedCfg)
26 : CGUIControl(parentID, controlID, posX, posY, width, height),
27 m_imgFocus(CGUITexture::CreateTexture(posX, posY, width, height, textureFocus)),
28 m_imgNoFocus(CGUITexture::CreateTexture(posX, posY, width, height, textureNoFocus))
30 m_frameCounter = 0;
31 m_movingSpeed.AddEventMapConfig(movingSpeedCfg);
32 m_fAnalogSpeed = 2.0f; //! @todo implement correct analog speed
33 ControlType = GUICONTROL_RESIZE;
34 SetLimits(0, 0, 720, 576); // defaults
37 CGUIResizeControl::CGUIResizeControl(const CGUIResizeControl& control)
38 : CGUIControl(control),
39 m_imgFocus(control.m_imgFocus->Clone()),
40 m_imgNoFocus(control.m_imgNoFocus->Clone()),
41 m_frameCounter(control.m_frameCounter),
42 m_movingSpeed(control.m_movingSpeed),
43 m_fAnalogSpeed(control.m_fAnalogSpeed),
44 m_x1(control.m_x1),
45 m_x2(control.m_x2),
46 m_y1(control.m_y1),
47 m_y2(control.m_y2)
51 void CGUIResizeControl::Process(unsigned int currentTime, CDirtyRegionList &dirtyregions)
53 if (m_bInvalidated)
55 m_imgFocus->SetWidth(m_width);
56 m_imgFocus->SetHeight(m_height);
58 m_imgNoFocus->SetWidth(m_width);
59 m_imgNoFocus->SetHeight(m_height);
61 if (HasFocus())
63 unsigned int alphaCounter = m_frameCounter + 2;
64 unsigned int alphaChannel;
65 if ((alphaCounter % 128) >= 64)
66 alphaChannel = alphaCounter % 64;
67 else
68 alphaChannel = 63 - (alphaCounter % 64);
70 alphaChannel += 192;
71 if (SetAlpha( (unsigned char)alphaChannel ))
72 MarkDirtyRegion();
73 m_imgFocus->SetVisible(true);
74 m_imgNoFocus->SetVisible(false);
75 m_frameCounter++;
77 else
79 if (SetAlpha(0xff))
80 MarkDirtyRegion();
81 m_imgFocus->SetVisible(false);
82 m_imgNoFocus->SetVisible(true);
84 m_imgFocus->Process(currentTime);
85 m_imgNoFocus->Process(currentTime);
86 CGUIControl::Process(currentTime, dirtyregions);
89 void CGUIResizeControl::Render()
91 m_imgFocus->Render();
92 m_imgNoFocus->Render();
93 CGUIControl::Render();
96 bool CGUIResizeControl::OnAction(const CAction &action)
98 if (action.GetID() == ACTION_SELECT_ITEM)
100 // button selected - send message to parent
101 CGUIMessage message(GUI_MSG_CLICKED, GetID(), GetParentID());
102 SendWindowMessage(message);
103 return true;
105 if (action.GetID() == ACTION_ANALOG_MOVE)
107 Resize(m_fAnalogSpeed*action.GetAmount(), -m_fAnalogSpeed*action.GetAmount(1));
108 return true;
110 return CGUIControl::OnAction(action);
113 void CGUIResizeControl::OnUp()
115 Resize(0, -m_movingSpeed.GetUpdatedDistance(MOVING_SPEED::EventType::UP));
118 void CGUIResizeControl::OnDown()
120 Resize(0, m_movingSpeed.GetUpdatedDistance(MOVING_SPEED::EventType::DOWN));
123 void CGUIResizeControl::OnLeft()
125 Resize(-m_movingSpeed.GetUpdatedDistance(MOVING_SPEED::EventType::LEFT), 0);
128 void CGUIResizeControl::OnRight()
130 Resize(m_movingSpeed.GetUpdatedDistance(MOVING_SPEED::EventType::RIGHT), 0);
133 EVENT_RESULT CGUIResizeControl::OnMouseEvent(const CPoint &point, const CMouseEvent &event)
135 if (event.m_id == ACTION_MOUSE_DRAG || event.m_id == ACTION_MOUSE_DRAG_END)
137 if (static_cast<HoldAction>(event.m_state) == HoldAction::DRAG)
138 { // grab exclusive access
139 CGUIMessage msg(GUI_MSG_EXCLUSIVE_MOUSE, GetID(), GetParentID());
140 SendWindowMessage(msg);
142 else if (static_cast<HoldAction>(event.m_state) == HoldAction::DRAG_END)
143 { // release exclusive access
144 CGUIMessage msg(GUI_MSG_EXCLUSIVE_MOUSE, 0, GetParentID());
145 SendWindowMessage(msg);
147 Resize(event.m_offsetX, event.m_offsetY);
148 return EVENT_RESULT_HANDLED;
150 return EVENT_RESULT_UNHANDLED;
153 void CGUIResizeControl::AllocResources()
155 CGUIControl::AllocResources();
156 m_frameCounter = 0;
157 m_imgFocus->AllocResources();
158 m_imgNoFocus->AllocResources();
159 m_width = m_imgFocus->GetWidth();
160 m_height = m_imgFocus->GetHeight();
163 void CGUIResizeControl::FreeResources(bool immediately)
165 CGUIControl::FreeResources(immediately);
166 m_imgFocus->FreeResources(immediately);
167 m_imgNoFocus->FreeResources(immediately);
170 void CGUIResizeControl::DynamicResourceAlloc(bool bOnOff)
172 CGUIControl::DynamicResourceAlloc(bOnOff);
173 m_imgFocus->DynamicResourceAlloc(bOnOff);
174 m_imgNoFocus->DynamicResourceAlloc(bOnOff);
177 void CGUIResizeControl::SetInvalid()
179 CGUIControl::SetInvalid();
180 m_imgFocus->SetInvalid();
181 m_imgNoFocus->SetInvalid();
184 void CGUIResizeControl::Resize(float x, float y)
186 float width = m_width + x;
187 float height = m_height + y;
188 // check if we are within the bounds
189 if (width < m_x1) width = m_x1;
190 if (height < m_y1) height = m_y1;
191 if (width > m_x2) width = m_x2;
192 if (height > m_y2) height = m_y2;
193 // ok, now set the default size of the resize control
194 SetWidth(width);
195 SetHeight(height);
198 void CGUIResizeControl::SetPosition(float posX, float posY)
200 CGUIControl::SetPosition(posX, posY);
201 m_imgFocus->SetPosition(posX, posY);
202 m_imgNoFocus->SetPosition(posX, posY);
205 bool CGUIResizeControl::SetAlpha(unsigned char alpha)
207 bool changed = m_imgFocus->SetAlpha(alpha);
208 changed |= m_imgNoFocus->SetAlpha(alpha);
209 return changed;
212 bool CGUIResizeControl::UpdateColors(const CGUIListItem* item)
214 bool changed = CGUIControl::UpdateColors(nullptr);
215 changed |= m_imgFocus->SetDiffuseColor(m_diffuseColor);
216 changed |= m_imgNoFocus->SetDiffuseColor(m_diffuseColor);
218 return changed;
221 void CGUIResizeControl::SetLimits(float x1, float y1, float x2, float y2)
223 m_x1 = x1;
224 m_y1 = y1;
225 m_x2 = x2;
226 m_y2 = y2;