added a different mouse sens for menus/editor
[twcon.git] / src / game / client / ui.cpp
blob00a30c150c8d25a6ccf3e7c73a53dc86caf8ae95
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>
8 #include "ui.h"
10 /********************************************************
12 *********************************************************/
14 CUI::CUI()
16 m_pHotItem = 0;
17 m_pActiveItem = 0;
18 m_pLastActiveItem = 0;
19 m_pBecommingHotItem = 0;
21 m_MouseX = 0;
22 m_MouseY = 0;
23 m_MouseWorldX = 0;
24 m_MouseWorldY = 0;
25 m_MouseButtons = 0;
26 m_LastMouseButtons = 0;
28 m_Screen.x = 0;
29 m_Screen.y = 0;
30 m_Screen.w = 848.0f;
31 m_Screen.h = 480.0f;
34 int CUI::Update(float Mx, float My, float Mwx, float Mwy, int Buttons)
36 m_MouseX = Mx;
37 m_MouseY = My;
38 m_MouseWorldX = Mwx;
39 m_MouseWorldY = Mwy;
40 m_LastMouseButtons = m_MouseButtons;
41 m_MouseButtons = Buttons;
42 m_pHotItem = m_pBecommingHotItem;
43 if(m_pActiveItem)
44 m_pHotItem = m_pActiveItem;
45 m_pBecommingHotItem = 0;
46 return 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)
52 return 1;
53 return 0;
56 void CUI::ConvertMouseMove(float *x, float *y)
58 float Fac = (float)(g_Config.m_UiMousesens)/g_Config.m_InpMousesens;
59 *x = *x*Fac;
60 *y = *y*Fac;
63 CUIRect *CUI::Screen()
65 float Aspect = Graphics()->ScreenAspect();
66 float w, h;
68 h = 600;
69 w = Aspect*h;
71 m_Screen.w = w;
72 m_Screen.h = h;
74 return &m_Screen;
77 void CUI::SetScale(float s)
79 g_Config.m_UiScale = (int)(s*100.0f);
82 float CUI::Scale()
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
106 CUIRect r = *this;
107 float Cut = r.h/2;
109 if(pTop)
111 pTop->x = r.x;
112 pTop->y = r.y;
113 pTop->w = r.w;
114 pTop->h = Cut;
117 if(pBottom)
119 pBottom->x = r.x;
120 pBottom->y = r.y + Cut;
121 pBottom->w = r.w;
122 pBottom->h = r.h - Cut;
126 void CUIRect::HSplitTop(float Cut, CUIRect *pTop, CUIRect *pBottom) const
128 CUIRect r = *this;
129 Cut *= Scale();
131 if (pTop)
133 pTop->x = r.x;
134 pTop->y = r.y;
135 pTop->w = r.w;
136 pTop->h = Cut;
139 if (pBottom)
141 pBottom->x = r.x;
142 pBottom->y = r.y + Cut;
143 pBottom->w = r.w;
144 pBottom->h = r.h - Cut;
148 void CUIRect::HSplitBottom(float Cut, CUIRect *pTop, CUIRect *pBottom) const
150 CUIRect r = *this;
151 Cut *= Scale();
153 if (pTop)
155 pTop->x = r.x;
156 pTop->y = r.y;
157 pTop->w = r.w;
158 pTop->h = r.h - Cut;
161 if (pBottom)
163 pBottom->x = r.x;
164 pBottom->y = r.y + r.h - Cut;
165 pBottom->w = r.w;
166 pBottom->h = Cut;
171 void CUIRect::VSplitMid(CUIRect *pLeft, CUIRect *pRight) const
173 CUIRect r = *this;
174 float Cut = r.w/2;
175 // Cut *= Scale();
177 if (pLeft)
179 pLeft->x = r.x;
180 pLeft->y = r.y;
181 pLeft->w = Cut;
182 pLeft->h = r.h;
185 if (pRight)
187 pRight->x = r.x + Cut;
188 pRight->y = r.y;
189 pRight->w = r.w - Cut;
190 pRight->h = r.h;
194 void CUIRect::VSplitLeft(float Cut, CUIRect *pLeft, CUIRect *pRight) const
196 CUIRect r = *this;
197 Cut *= Scale();
199 if (pLeft)
201 pLeft->x = r.x;
202 pLeft->y = r.y;
203 pLeft->w = Cut;
204 pLeft->h = r.h;
207 if (pRight)
209 pRight->x = r.x + Cut;
210 pRight->y = r.y;
211 pRight->w = r.w - Cut;
212 pRight->h = r.h;
216 void CUIRect::VSplitRight(float Cut, CUIRect *pLeft, CUIRect *pRight) const
218 CUIRect r = *this;
219 Cut *= Scale();
221 if (pLeft)
223 pLeft->x = r.x;
224 pLeft->y = r.y;
225 pLeft->w = r.w - Cut;
226 pLeft->h = r.h;
229 if (pRight)
231 pRight->x = r.x + r.w - Cut;
232 pRight->y = r.y;
233 pRight->w = Cut;
234 pRight->h = r.h;
238 void CUIRect::Margin(float Cut, CUIRect *pOtherRect) const
240 CUIRect r = *this;
241 Cut *= Scale();
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
251 CUIRect r = *this;
252 Cut *= Scale();
254 pOtherRect->x = r.x + Cut;
255 pOtherRect->y = r.y;
256 pOtherRect->w = r.w - 2*Cut;
257 pOtherRect->h = r.h;
260 void CUIRect::HMargin(float Cut, CUIRect *pOtherRect) const
262 CUIRect r = *this;
263 Cut *= Scale();
265 pOtherRect->x = r.x;
266 pOtherRect->y = r.y + Cut;
267 pOtherRect->w = r.w;
268 pOtherRect->h = r.h - 2*Cut;
271 int CUI::DoButtonLogic(const void *pID, const char *pText, int Checked, const CUIRect *pRect)
273 // logic
274 int ReturnValue = 0;
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;
284 SetActiveItem(0);
287 else if(HotItem() == pID)
289 if(MouseButton(0))
291 SetActiveItem(pID);
292 ButtonUsed = 0;
295 if(MouseButton(1))
297 SetActiveItem(pID);
298 ButtonUsed = 1;
302 if(Inside)
303 SetHotItem(pID);
305 return ReturnValue;
308 int CUI::DoButton(const void *id, const char *text, int checked, const CUIRect *r, ui_draw_button_func draw_func, const void *extra)
310 // logic
311 int ret = 0;
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)
320 ret = 1+button_used;
321 ui_SetActiveItem(0);
324 else if(ui_HotItem() == id)
326 if(ui_MouseButton(0))
328 ui_SetActiveItem(id);
329 button_used = 0;
332 if(ui_MouseButton(1))
334 ui_SetActiveItem(id);
335 button_used = 1;
339 if(inside)
340 ui_SetHotItem(id);
342 if(draw_func)
343 draw_func(id, text, checked, r, extra);
344 return ret;
347 void CUI::DoLabel(const CUIRect *r, const char *pText, float Size, int Align, int MaxWidth)
349 // TODO: FIX ME!!!!
350 //Graphics()->BlendNormal();
351 if(Align == 0)
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);
356 else if(Align < 0)
357 TextRender()->Text(0, r->x, r->y - Size/10, Size, pText, MaxWidth);
358 else if(Align > 0)
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);