OpenClonk
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros
C4GuiCheckBox.cpp
Go to the documentation of this file.
1 /*
2  * OpenClonk, http://www.openclonk.org
3  *
4  * Copyright (c) 2005-2009, RedWolf Design GmbH, http://www.clonk.de/
5  * Copyright (c) 2010-2016, The OpenClonk Team and contributors
6  *
7  * Distributed under the terms of the ISC license; see accompanying file
8  * "COPYING" for details.
9  *
10  * "Clonk" is a registered trademark of Matthes Bender, used with permission.
11  * See accompanying file "TRADEMARK" for details.
12  *
13  * To redistribute this file separately, substitute the full license texts
14  * for the above references.
15  */
16 // generic user interface
17 // checkbox
18 
19 #include "C4Include.h"
20 #include "gui/C4Gui.h"
21 
22 #include "graphics/C4FacetEx.h"
23 #include "gui/C4MouseControl.h"
25 #include "graphics/C4Draw.h"
26 
27 #include "platform/C4Window.h"
28 
29 namespace C4GUI
30 {
31 
32 // ----------------------------------------------------
33 // CheckBox
34 
35  CheckBox::CheckBox(const C4Rect &rtBounds, const char *szCaption, bool fChecked)
36  : Control(rtBounds), fChecked(fChecked), fMouseOn(false), fEnabled(true), pFont(nullptr)
37  , dwEnabledClr(C4GUI_CheckboxFontClr), dwDisabledClr(C4GUI_CheckboxDisabledFontClr), cHotkey(0)
38  {
39  if (szCaption)
40  {
41  sCaption.Copy(szCaption);
42  ExpandHotkeyMarkup(sCaption, cHotkey);
43  }
44  // key callbacks: Check/Uncheck on space and primary joy button
46  Keys.push_back(C4KeyCodeEx(K_SPACE));
48  {
49  ControllerKeys::Ok(Keys);
50  }
51  pKeyCheck = new C4KeyBinding(Keys, "GUICheckboxToggle", KEYSCOPE_Gui,
52  new ControlKeyCB<CheckBox>(*this, &CheckBox::KeyCheck), C4CustomKey::PRIO_Ctrl);
53  pCBHandler = nullptr;
54  }
55 
57  {
58  delete pKeyCheck;
59  if (pCBHandler) pCBHandler->DeRef();
60  }
61 
63  {
64  }
65 
66  bool CheckBox::OnHotkey(uint32_t cHotkey)
67  {
68  if (cHotkey != this->cHotkey) return false;
69  ToggleCheck(true);
70  return true;
71  }
72 
73  void CheckBox::ToggleCheck(bool fByUser)
74  {
75  // user can't toggle if disabled
76  if (fByUser && !fEnabled) return;
77  // sound
78  if (fByUser) GUISound("UI::Tick");
79  // toggle state
80  fChecked = !fChecked;
81  // callback (last call; may destroy element)
82  if (pCBHandler) pCBHandler->DoCall(this);
83  }
84 
85  void CheckBox::MouseInput(CMouse &rMouse, int32_t iButton, int32_t iX, int32_t iY, DWORD dwKeyParam)
86  {
87  if (fEnabled)
88  {
89  // set mouse-on flag depending on whether mouse is over box area
90  fMouseOn = Inside<int32_t>(iX, 0, rcBounds.Hgt) && Inside<int32_t>(iY, 0, rcBounds.Hgt);
91  // left-click within checkbox toggles it
92  if (iButton == C4MC_Button_LeftDown && fMouseOn)
93  {
94  ToggleCheck(true);
95  return;
96  }
97  }
98  // not recognized; base call
99  Control::MouseInput(rMouse, iButton, iX, iY, dwKeyParam);
100  }
101 
103  {
104  Control::MouseEnter(rMouse);
105  }
106 
108  {
109  fMouseOn = false;
110  Control::MouseLeave(rMouse);
111  }
112 
114  {
115  // left side: check facet (squared)
116  int x0 = rcBounds.x + cgo.TargetX;
117  int y0 = rcBounds.y + cgo.TargetY;
118  ::GraphicsResource.fctCheckbox.GetPhase(fChecked + 2*!fEnabled).DrawX(cgo.Surface, x0, y0, rcBounds.Hgt, rcBounds.Hgt);
119  // right of it: checkbox text
120  CStdFont *pUseFont = pFont ? pFont : &(::GraphicsResource.TextFont);
121  int32_t yOff; float fZoom;
122  if (pUseFont->GetLineHeight() <= rcBounds.Hgt)
123  {
124  yOff = std::max<int32_t>(rcBounds.Hgt - pUseFont->GetLineHeight(), 0)/2;
125  fZoom = 1.0f;
126  }
127  else
128  {
129  yOff = 0;
130  fZoom = (float) rcBounds.Hgt / std::max(pUseFont->GetLineHeight(), 1);
131  }
132  pDraw->TextOut(sCaption.getData(), *pUseFont, fZoom, cgo.Surface, x0 + rcBounds.Hgt + C4GUI_CheckBoxLabelSpacing, y0 + yOff, fEnabled ? dwEnabledClr : dwDisabledClr, ALeft, true);
133  // selection marker
134  if ((fMouseOn && IsInActiveDlg(false)) || HasDrawFocus())
135  {
138  pDraw->ResetBlitMode();
139  }
140  }
141 
143  {
144  if (pCBHandler) pCBHandler->DeRef();
145  if ((pCBHandler = pCB)) pCB->Ref();
146  }
147 
148  bool CheckBox::GetStandardCheckBoxSize(int *piWdt, int *piHgt, const char *szForCaptionText, CStdFont *pUseFont)
149  {
150  // get needed text size
151  if (!pUseFont) pUseFont = &(::GraphicsResource.TextFont);
152  int32_t iWdt=100, iHgt=32;
153  pUseFont->GetTextExtent(szForCaptionText, iWdt, iHgt, true);
154  // check box height equals text height
155  // add check box plus indent
156  if (piWdt) *piWdt = iWdt + iHgt + C4GUI_CheckBoxLabelSpacing;
157  if (piHgt) *piHgt = iHgt;
158  return true;
159  }
160 
161 } // namespace C4GUI
const char * getData() const
Definition: StdBuf.h:450
virtual void DoCall(class Element *pElement)=0
C4Config Config
Definition: C4Config.cpp:837
const int32_t C4MC_Button_LeftDown
int GetLineHeight() const
Definition: C4FontLoader.h:132
void GUISound(const char *szSound)
Definition: C4Gui.cpp:1178
#define C4GUI_CheckBoxLabelSpacing
Definition: C4Gui.h:120
void ResetBlitMode()
Definition: C4Draw.h:192
#define C4GUI_CheckboxFontClr
Definition: C4Gui.h:48
bool TextOut(const char *szText, CStdFont &rFont, float fZoom, C4Surface *sfcDest, float iTx, float iTy, DWORD dwFCol=0xffffffff, BYTE byForm=ALeft, bool fDoMarkup=true)
Definition: C4Draw.cpp:570
#define C4GFXBLIT_ADDITIVE
Definition: C4Surface.h:28
virtual void UpdateOwnPos()
Definition: C4Rect.h:29
bool GetTextExtent(const char *szText, int32_t &rsx, int32_t &rsy, bool fCheckMarkup=true)
C4GraphicsResource GraphicsResource
virtual void MouseInput(CMouse &rMouse, int32_t iButton, int32_t iX, int32_t iY, DWORD dwKeyParam)
void SetBlitMode(DWORD dwBlitMode)
Definition: C4Draw.h:191
bool ExpandHotkeyMarkup(StdStrBuf &sText, uint32_t &rcHotkey, bool for_tooltip)
Definition: C4Gui.cpp:39
std::vector< C4KeyCodeEx > CodeList
int32_t y
Definition: C4Rect.h:32
void DrawX(C4Surface *sfcTarget, float iX, float iY, float iWdt, float iHgt, int32_t iPhaseX=0, int32_t iPhaseY=0) const
Definition: C4Facet.cpp:358
C4Rect rcBounds
Definition: C4Gui.h:385
virtual void MouseInput(CMouse &rMouse, int32_t iButton, int32_t iX, int32_t iY, DWORD dwKeyParam)
C4FacetID fctButtonHighlightRound
C4Draw * pDraw
Definition: C4Draw.cpp:45
virtual void MouseLeave(CMouse &rMouse)
static bool GetStandardCheckBoxSize(int *piWdt, int *piHgt, const char *szForCaptionText, CStdFont *pUseFont)
virtual bool OnHotkey(uint32_t cHotkey)
int32_t x
Definition: C4Rect.h:32
int32_t GamepadGuiControl
Definition: C4Config.h:230
void SetOnChecked(BaseCallbackHandler *pCB)
float TargetX
Definition: C4Facet.h:167
void Ok(T &keys)
C4ConfigControls Controls
Definition: C4Config.h:260
const int ALeft
Definition: C4Surface.h:43
virtual void DrawElement(C4TargetFacet &cgo)
int32_t Hgt
Definition: C4Rect.h:32
C4Surface * Surface
Definition: C4Facet.h:119
float TargetY
Definition: C4Facet.h:167
uint32_t DWORD
void Copy()
Definition: StdBuf.h:475
C4Facet GetPhase(int iPhaseX=0, int iPhaseY=0)
Definition: C4Facet.cpp:59
virtual void MouseEnter(CMouse &rMouse)
virtual void MouseEnter(CMouse &rMouse)
Definition: C4Gui.h:413
void ToggleCheck(bool fByUser)
CheckBox(const C4Rect &rtBounds, const char *szCaption, bool fChecked)
bool IsInActiveDlg(bool fForKeyboard)
Definition: C4Gui.cpp:436
virtual void MouseLeave(CMouse &rMouse)
Definition: C4Gui.h:414
#define C4GUI_CheckboxDisabledFontClr
Definition: C4Gui.h:50