[ExecString] combine SplitParameters with identical function of CUtil
[xbmc.git] / xbmc / guilib / GUIMoverControl.cpp
blob4299fc0c5f7cf04bbde3909961e887c08afe2ffa
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 "GUIMoverControl.h"
11 #include "GUIMessage.h"
12 #include "input/actions/Action.h"
13 #include "input/actions/ActionIDs.h"
14 #include "input/mouse/MouseEvent.h"
15 #include "input/mouse/MouseStat.h"
16 #include "utils/TimeUtils.h"
18 using namespace KODI;
19 using namespace UTILS;
21 CGUIMoverControl::CGUIMoverControl(int parentID,
22 int controlID,
23 float posX,
24 float posY,
25 float width,
26 float height,
27 const CTextureInfo& textureFocus,
28 const CTextureInfo& textureNoFocus,
29 UTILS::MOVING_SPEED::MapEventConfig& movingSpeedCfg)
30 : CGUIControl(parentID, controlID, posX, posY, width, height),
31 m_imgFocus(CGUITexture::CreateTexture(posX, posY, width, height, textureFocus)),
32 m_imgNoFocus(CGUITexture::CreateTexture(posX, posY, width, height, textureNoFocus))
34 m_frameCounter = 0;
35 m_movingSpeed.AddEventMapConfig(movingSpeedCfg);
36 m_fAnalogSpeed = 2.0f; //! @todo implement correct analog speed
37 ControlType = GUICONTROL_MOVER;
38 SetLimits(0, 0, 720, 576); // defaults
39 SetLocation(0, 0, false); // defaults
42 CGUIMoverControl::CGUIMoverControl(const CGUIMoverControl& control)
43 : CGUIControl(control),
44 m_imgFocus(control.m_imgFocus->Clone()),
45 m_imgNoFocus(control.m_imgNoFocus->Clone()),
46 m_frameCounter(control.m_frameCounter),
47 m_movingSpeed(control.m_movingSpeed),
48 m_fAnalogSpeed(control.m_fAnalogSpeed),
49 m_iX1(control.m_iX1),
50 m_iX2(control.m_iX2),
51 m_iY1(control.m_iY1),
52 m_iY2(control.m_iY2),
53 m_iLocationX(control.m_iLocationX),
54 m_iLocationY(control.m_iLocationY)
58 void CGUIMoverControl::Process(unsigned int currentTime, CDirtyRegionList &dirtyregions)
60 if (m_bInvalidated)
62 m_imgFocus->SetWidth(m_width);
63 m_imgFocus->SetHeight(m_height);
65 m_imgNoFocus->SetWidth(m_width);
66 m_imgNoFocus->SetHeight(m_height);
68 if (HasFocus())
70 unsigned int alphaCounter = m_frameCounter + 2;
71 unsigned int alphaChannel;
72 if ((alphaCounter % 128) >= 64)
73 alphaChannel = alphaCounter % 64;
74 else
75 alphaChannel = 63 - (alphaCounter % 64);
77 alphaChannel += 192;
78 if (SetAlpha( (unsigned char)alphaChannel ))
79 MarkDirtyRegion();
80 m_imgFocus->SetVisible(true);
81 m_imgNoFocus->SetVisible(false);
82 m_frameCounter++;
84 else
86 if (SetAlpha(0xff))
87 MarkDirtyRegion();
88 m_imgFocus->SetVisible(false);
89 m_imgNoFocus->SetVisible(true);
91 m_imgFocus->Process(currentTime);
92 m_imgNoFocus->Process(currentTime);
94 CGUIControl::Process(currentTime, dirtyregions);
97 void CGUIMoverControl::Render()
99 // render both so the visibility settings cause the frame counter to resetcorrectly
100 m_imgFocus->Render();
101 m_imgNoFocus->Render();
102 CGUIControl::Render();
105 bool CGUIMoverControl::OnAction(const CAction &action)
107 if (action.GetID() == ACTION_SELECT_ITEM)
109 // button selected - send message to parent
110 CGUIMessage message(GUI_MSG_CLICKED, GetID(), GetParentID());
111 SendWindowMessage(message);
112 return true;
114 if (action.GetID() == ACTION_ANALOG_MOVE)
116 // if (m_dwAllowedDirections == ALLOWED_DIRECTIONS_UPDOWN)
117 // Move(0, (int)(-m_fAnalogSpeed*action.GetAmount(1)));
118 // else if (m_dwAllowedDirections == ALLOWED_DIRECTIONS_LEFTRIGHT)
119 // Move((int)(m_fAnalogSpeed*action.GetAmount()), 0);
120 // else // ALLOWED_DIRECTIONS_ALL
121 Move((int)(m_fAnalogSpeed*action.GetAmount()), (int)( -m_fAnalogSpeed*action.GetAmount(1)));
122 return true;
124 // base class
125 return CGUIControl::OnAction(action);
128 void CGUIMoverControl::OnUp()
130 // if (m_dwAllowedDirections == ALLOWED_DIRECTIONS_LEFTRIGHT) return;
131 Move(0, -static_cast<int>(m_movingSpeed.GetUpdatedDistance(MOVING_SPEED::EventType::UP)));
134 void CGUIMoverControl::OnDown()
136 // if (m_dwAllowedDirections == ALLOWED_DIRECTIONS_LEFTRIGHT) return;
137 Move(0, static_cast<int>(m_movingSpeed.GetUpdatedDistance(MOVING_SPEED::EventType::DOWN)));
140 void CGUIMoverControl::OnLeft()
142 // if (m_dwAllowedDirections == ALLOWED_DIRECTIONS_UPDOWN) return;
143 Move(-static_cast<int>(m_movingSpeed.GetUpdatedDistance(MOVING_SPEED::EventType::LEFT)), 0);
146 void CGUIMoverControl::OnRight()
148 // if (m_dwAllowedDirections == ALLOWED_DIRECTIONS_UPDOWN) return;
149 Move(static_cast<int>(m_movingSpeed.GetUpdatedDistance(MOVING_SPEED::EventType::RIGHT)), 0);
152 EVENT_RESULT CGUIMoverControl::OnMouseEvent(const CPoint& point, const MOUSE::CMouseEvent& event)
154 if (event.m_id == ACTION_MOUSE_DRAG || event.m_id == ACTION_MOUSE_DRAG_END)
156 if (static_cast<HoldAction>(event.m_state) == HoldAction::DRAG)
157 { // grab exclusive access
158 CGUIMessage msg(GUI_MSG_EXCLUSIVE_MOUSE, GetID(), GetParentID());
159 SendWindowMessage(msg);
161 else if (static_cast<HoldAction>(event.m_state) == HoldAction::DRAG_END)
162 { // release exclusive access
163 CGUIMessage msg(GUI_MSG_EXCLUSIVE_MOUSE, 0, GetParentID());
164 SendWindowMessage(msg);
166 Move((int)event.m_offsetX, (int)event.m_offsetY);
167 return EVENT_RESULT_HANDLED;
169 return EVENT_RESULT_UNHANDLED;
172 void CGUIMoverControl::AllocResources()
174 CGUIControl::AllocResources();
175 m_frameCounter = 0;
176 m_imgFocus->AllocResources();
177 m_imgNoFocus->AllocResources();
178 float width = m_width ? m_width : m_imgFocus->GetWidth();
179 float height = m_height ? m_height : m_imgFocus->GetHeight();
180 SetWidth(width);
181 SetHeight(height);
184 void CGUIMoverControl::FreeResources(bool immediately)
186 CGUIControl::FreeResources(immediately);
187 m_imgFocus->FreeResources(immediately);
188 m_imgNoFocus->FreeResources(immediately);
191 void CGUIMoverControl::DynamicResourceAlloc(bool bOnOff)
193 CGUIControl::DynamicResourceAlloc(bOnOff);
194 m_imgFocus->DynamicResourceAlloc(bOnOff);
195 m_imgNoFocus->DynamicResourceAlloc(bOnOff);
198 void CGUIMoverControl::SetInvalid()
200 CGUIControl::SetInvalid();
201 m_imgFocus->SetInvalid();
202 m_imgNoFocus->SetInvalid();
205 void CGUIMoverControl::Move(int iX, int iY)
207 if (!m_enabled)
208 return;
210 int iLocX = m_iLocationX + iX;
211 int iLocY = m_iLocationY + iY;
212 // check if we are within the bounds
213 if (iLocX < m_iX1) iLocX = m_iX1;
214 if (iLocY < m_iY1) iLocY = m_iY1;
215 if (iLocX > m_iX2) iLocX = m_iX2;
216 if (iLocY > m_iY2) iLocY = m_iY2;
217 // ok, now set the location of the mover
218 SetLocation(iLocX, iLocY);
221 void CGUIMoverControl::SetLocation(int iLocX, int iLocY, bool bSetPosition)
223 if (bSetPosition) SetPosition(GetXPosition() + iLocX - m_iLocationX, GetYPosition() + iLocY - m_iLocationY);
224 m_iLocationX = iLocX;
225 m_iLocationY = iLocY;
228 void CGUIMoverControl::SetPosition(float posX, float posY)
230 CGUIControl::SetPosition(posX, posY);
231 m_imgFocus->SetPosition(posX, posY);
232 m_imgNoFocus->SetPosition(posX, posY);
235 bool CGUIMoverControl::SetAlpha(unsigned char alpha)
237 bool changed = m_imgFocus->SetAlpha(alpha);
238 changed |= m_imgNoFocus->SetAlpha(alpha);
239 return changed;
242 bool CGUIMoverControl::UpdateColors(const CGUIListItem* item)
244 bool changed = CGUIControl::UpdateColors(nullptr);
245 changed |= m_imgFocus->SetDiffuseColor(m_diffuseColor);
246 changed |= m_imgNoFocus->SetDiffuseColor(m_diffuseColor);
248 return changed;
251 void CGUIMoverControl::SetLimits(int iX1, int iY1, int iX2, int iY2)
253 m_iX1 = iX1;
254 m_iY1 = iY1;
255 m_iX2 = iX2;
256 m_iY2 = iY2;