1 /* (c) Magnus Auvinen. See licence.txt in the root of the distribution for more information. */
2 /* If you are missing that file, acquire a complete release at teeworlds.com. */
3 #include <base/system.h>
5 #include <engine/shared/config.h>
6 #include <engine/graphics.h>
7 #include <engine/textrender.h>
10 /********************************************************
12 *********************************************************/
18 m_pLastActiveItem
= 0;
19 m_pBecommingHotItem
= 0;
26 m_LastMouseButtons
= 0;
34 int CUI::Update(float Mx
, float My
, float Mwx
, float Mwy
, int Buttons
)
40 m_LastMouseButtons
= m_MouseButtons
;
41 m_MouseButtons
= Buttons
;
42 m_pHotItem
= m_pBecommingHotItem
;
44 m_pHotItem
= m_pActiveItem
;
45 m_pBecommingHotItem
= 0;
49 int CUI::MouseInside(const CUIRect
*r
)
51 if(m_MouseX
>= r
->x
&& m_MouseX
<= r
->x
+r
->w
&& m_MouseY
>= r
->y
&& m_MouseY
<= r
->y
+r
->h
)
56 void CUI::ConvertMouseMove(float *x
, float *y
)
58 float Fac
= (float)(g_Config
.m_UiMousesens
)/g_Config
.m_InpMousesens
;
63 CUIRect
*CUI::Screen()
65 float Aspect
= Graphics()->ScreenAspect();
77 void CUI::SetScale(float s
)
79 g_Config
.m_UiScale
= (int)(s
*100.0f
);
84 return g_Config
.m_UiScale
/100.0f
;
87 float CUIRect::Scale() const
89 return g_Config
.m_UiScale
/100.0f
;
92 void CUI::ClipEnable(const CUIRect
*r
)
94 float XScale
= Graphics()->ScreenWidth()/Screen()->w
;
95 float YScale
= Graphics()->ScreenHeight()/Screen()->h
;
96 Graphics()->ClipEnable((int)(r
->x
*XScale
), (int)(r
->y
*YScale
), (int)(r
->w
*XScale
), (int)(r
->h
*YScale
));
99 void CUI::ClipDisable()
101 Graphics()->ClipDisable();
104 void CUIRect::HSplitMid(CUIRect
*pTop
, CUIRect
*pBottom
) const
120 pBottom
->y
= r
.y
+ Cut
;
122 pBottom
->h
= r
.h
- Cut
;
126 void CUIRect::HSplitTop(float Cut
, CUIRect
*pTop
, CUIRect
*pBottom
) const
142 pBottom
->y
= r
.y
+ Cut
;
144 pBottom
->h
= r
.h
- Cut
;
148 void CUIRect::HSplitBottom(float Cut
, CUIRect
*pTop
, CUIRect
*pBottom
) const
164 pBottom
->y
= r
.y
+ r
.h
- Cut
;
171 void CUIRect::VSplitMid(CUIRect
*pLeft
, CUIRect
*pRight
) const
187 pRight
->x
= r
.x
+ Cut
;
189 pRight
->w
= r
.w
- Cut
;
194 void CUIRect::VSplitLeft(float Cut
, CUIRect
*pLeft
, CUIRect
*pRight
) const
209 pRight
->x
= r
.x
+ Cut
;
211 pRight
->w
= r
.w
- Cut
;
216 void CUIRect::VSplitRight(float Cut
, CUIRect
*pLeft
, CUIRect
*pRight
) const
225 pLeft
->w
= r
.w
- Cut
;
231 pRight
->x
= r
.x
+ r
.w
- Cut
;
238 void CUIRect::Margin(float Cut
, CUIRect
*pOtherRect
) const
243 pOtherRect
->x
= r
.x
+ Cut
;
244 pOtherRect
->y
= r
.y
+ Cut
;
245 pOtherRect
->w
= r
.w
- 2*Cut
;
246 pOtherRect
->h
= r
.h
- 2*Cut
;
249 void CUIRect::VMargin(float Cut
, CUIRect
*pOtherRect
) const
254 pOtherRect
->x
= r
.x
+ Cut
;
256 pOtherRect
->w
= r
.w
- 2*Cut
;
260 void CUIRect::HMargin(float Cut
, CUIRect
*pOtherRect
) const
266 pOtherRect
->y
= r
.y
+ Cut
;
268 pOtherRect
->h
= r
.h
- 2*Cut
;
271 int CUI::DoButtonLogic(const void *pID
, const char *pText
, int Checked
, const CUIRect
*pRect
)
275 int Inside
= MouseInside(pRect
);
276 static int ButtonUsed
= 0;
278 if(ActiveItem() == pID
)
280 if(!MouseButton(ButtonUsed
))
282 if(Inside
&& Checked
>= 0)
283 ReturnValue
= 1+ButtonUsed
;
287 else if(HotItem() == pID
)
308 int CUI::DoButton(const void *id, const char *text, int checked, const CUIRect *r, ui_draw_button_func draw_func, const void *extra)
312 int inside = ui_MouseInside(r);
313 static int button_used = 0;
315 if(ui_ActiveItem() == id)
317 if(!ui_MouseButton(button_used))
319 if(inside && checked >= 0)
324 else if(ui_HotItem() == id)
326 if(ui_MouseButton(0))
328 ui_SetActiveItem(id);
332 if(ui_MouseButton(1))
334 ui_SetActiveItem(id);
343 draw_func(id, text, checked, r, extra);
347 void CUI::DoLabel(const CUIRect
*r
, const char *pText
, float Size
, int Align
, int MaxWidth
)
350 //Graphics()->BlendNormal();
353 float tw
= TextRender()->TextWidth(0, Size
, pText
, MaxWidth
);
354 TextRender()->Text(0, r
->x
+ r
->w
/2-tw
/2, r
->y
- Size
/10, Size
, pText
, MaxWidth
);
357 TextRender()->Text(0, r
->x
, r
->y
- Size
/10, Size
, pText
, MaxWidth
);
360 float tw
= TextRender()->TextWidth(0, Size
, pText
, MaxWidth
);
361 TextRender()->Text(0, r
->x
+ r
->w
-tw
, r
->y
- Size
/10, Size
, pText
, MaxWidth
);
365 void CUI::DoLabelScaled(const CUIRect
*r
, const char *pText
, float Size
, int Align
, int MaxWidth
)
367 DoLabel(r
, pText
, Size
*Scale(), Align
, MaxWidth
);