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.
9 #include "GUIResizeControl.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"
18 using namespace UTILS
;
20 CGUIResizeControl::CGUIResizeControl(int parentID
,
26 const CTextureInfo
& textureFocus
,
27 const CTextureInfo
& textureNoFocus
,
28 UTILS::MOVING_SPEED::MapEventConfig
& movingSpeedCfg
)
29 : CGUIControl(parentID
, controlID
, posX
, posY
, width
, height
),
30 m_imgFocus(CGUITexture::CreateTexture(posX
, posY
, width
, height
, textureFocus
)),
31 m_imgNoFocus(CGUITexture::CreateTexture(posX
, posY
, width
, height
, textureNoFocus
))
34 m_movingSpeed
.AddEventMapConfig(movingSpeedCfg
);
35 m_fAnalogSpeed
= 2.0f
; //! @todo implement correct analog speed
36 ControlType
= GUICONTROL_RESIZE
;
37 SetLimits(0, 0, 720, 576); // defaults
40 CGUIResizeControl::CGUIResizeControl(const CGUIResizeControl
& control
)
41 : CGUIControl(control
),
42 m_imgFocus(control
.m_imgFocus
->Clone()),
43 m_imgNoFocus(control
.m_imgNoFocus
->Clone()),
44 m_frameCounter(control
.m_frameCounter
),
45 m_movingSpeed(control
.m_movingSpeed
),
46 m_fAnalogSpeed(control
.m_fAnalogSpeed
),
54 void CGUIResizeControl::Process(unsigned int currentTime
, CDirtyRegionList
&dirtyregions
)
58 m_imgFocus
->SetWidth(m_width
);
59 m_imgFocus
->SetHeight(m_height
);
61 m_imgNoFocus
->SetWidth(m_width
);
62 m_imgNoFocus
->SetHeight(m_height
);
66 unsigned int alphaCounter
= m_frameCounter
+ 2;
67 unsigned int alphaChannel
;
68 if ((alphaCounter
% 128) >= 64)
69 alphaChannel
= alphaCounter
% 64;
71 alphaChannel
= 63 - (alphaCounter
% 64);
74 if (SetAlpha( (unsigned char)alphaChannel
))
76 m_imgFocus
->SetVisible(true);
77 m_imgNoFocus
->SetVisible(false);
84 m_imgFocus
->SetVisible(false);
85 m_imgNoFocus
->SetVisible(true);
87 m_imgFocus
->Process(currentTime
);
88 m_imgNoFocus
->Process(currentTime
);
89 CGUIControl::Process(currentTime
, dirtyregions
);
92 void CGUIResizeControl::Render()
95 m_imgNoFocus
->Render();
96 CGUIControl::Render();
99 bool CGUIResizeControl::OnAction(const CAction
&action
)
101 if (action
.GetID() == ACTION_SELECT_ITEM
)
103 // button selected - send message to parent
104 CGUIMessage
message(GUI_MSG_CLICKED
, GetID(), GetParentID());
105 SendWindowMessage(message
);
108 if (action
.GetID() == ACTION_ANALOG_MOVE
)
110 Resize(m_fAnalogSpeed
*action
.GetAmount(), -m_fAnalogSpeed
*action
.GetAmount(1));
113 return CGUIControl::OnAction(action
);
116 void CGUIResizeControl::OnUp()
118 Resize(0, -m_movingSpeed
.GetUpdatedDistance(MOVING_SPEED::EventType::UP
));
121 void CGUIResizeControl::OnDown()
123 Resize(0, m_movingSpeed
.GetUpdatedDistance(MOVING_SPEED::EventType::DOWN
));
126 void CGUIResizeControl::OnLeft()
128 Resize(-m_movingSpeed
.GetUpdatedDistance(MOVING_SPEED::EventType::LEFT
), 0);
131 void CGUIResizeControl::OnRight()
133 Resize(m_movingSpeed
.GetUpdatedDistance(MOVING_SPEED::EventType::RIGHT
), 0);
136 EVENT_RESULT
CGUIResizeControl::OnMouseEvent(const CPoint
& point
, const MOUSE::CMouseEvent
& event
)
138 if (event
.m_id
== ACTION_MOUSE_DRAG
|| event
.m_id
== ACTION_MOUSE_DRAG_END
)
140 if (static_cast<HoldAction
>(event
.m_state
) == HoldAction::DRAG
)
141 { // grab exclusive access
142 CGUIMessage
msg(GUI_MSG_EXCLUSIVE_MOUSE
, GetID(), GetParentID());
143 SendWindowMessage(msg
);
145 else if (static_cast<HoldAction
>(event
.m_state
) == HoldAction::DRAG_END
)
146 { // release exclusive access
147 CGUIMessage
msg(GUI_MSG_EXCLUSIVE_MOUSE
, 0, GetParentID());
148 SendWindowMessage(msg
);
150 Resize(event
.m_offsetX
, event
.m_offsetY
);
151 return EVENT_RESULT_HANDLED
;
153 return EVENT_RESULT_UNHANDLED
;
156 void CGUIResizeControl::AllocResources()
158 CGUIControl::AllocResources();
160 m_imgFocus
->AllocResources();
161 m_imgNoFocus
->AllocResources();
162 m_width
= m_imgFocus
->GetWidth();
163 m_height
= m_imgFocus
->GetHeight();
166 void CGUIResizeControl::FreeResources(bool immediately
)
168 CGUIControl::FreeResources(immediately
);
169 m_imgFocus
->FreeResources(immediately
);
170 m_imgNoFocus
->FreeResources(immediately
);
173 void CGUIResizeControl::DynamicResourceAlloc(bool bOnOff
)
175 CGUIControl::DynamicResourceAlloc(bOnOff
);
176 m_imgFocus
->DynamicResourceAlloc(bOnOff
);
177 m_imgNoFocus
->DynamicResourceAlloc(bOnOff
);
180 void CGUIResizeControl::SetInvalid()
182 CGUIControl::SetInvalid();
183 m_imgFocus
->SetInvalid();
184 m_imgNoFocus
->SetInvalid();
187 void CGUIResizeControl::Resize(float x
, float y
)
189 float width
= m_width
+ x
;
190 float height
= m_height
+ y
;
191 // check if we are within the bounds
192 if (width
< m_x1
) width
= m_x1
;
193 if (height
< m_y1
) height
= m_y1
;
194 if (width
> m_x2
) width
= m_x2
;
195 if (height
> m_y2
) height
= m_y2
;
196 // ok, now set the default size of the resize control
201 void CGUIResizeControl::SetPosition(float posX
, float posY
)
203 CGUIControl::SetPosition(posX
, posY
);
204 m_imgFocus
->SetPosition(posX
, posY
);
205 m_imgNoFocus
->SetPosition(posX
, posY
);
208 bool CGUIResizeControl::SetAlpha(unsigned char alpha
)
210 bool changed
= m_imgFocus
->SetAlpha(alpha
);
211 changed
|= m_imgNoFocus
->SetAlpha(alpha
);
215 bool CGUIResizeControl::UpdateColors(const CGUIListItem
* item
)
217 bool changed
= CGUIControl::UpdateColors(nullptr);
218 changed
|= m_imgFocus
->SetDiffuseColor(m_diffuseColor
);
219 changed
|= m_imgNoFocus
->SetDiffuseColor(m_diffuseColor
);
224 void CGUIResizeControl::SetLimits(float x1
, float y1
, float x2
, float y2
)