OpenClonk
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/C4Draw.h"
23 #include "graphics/C4FacetEx.h"
25 #include "gui/C4MouseControl.h"
26 
27 namespace C4GUI
28 {
29 
30 // ----------------------------------------------------
31 // CheckBox
32 
33  CheckBox::CheckBox(const C4Rect &rtBounds, const char *szCaption, bool fChecked)
34  : Control(rtBounds), fChecked(fChecked), fMouseOn(false), fEnabled(true), pFont(nullptr)
35  , dwEnabledClr(C4GUI_CheckboxFontClr), dwDisabledClr(C4GUI_CheckboxDisabledFontClr), cHotkey(0)
36  {
37  if (szCaption)
38  {
39  sCaption.Copy(szCaption);
40  ExpandHotkeyMarkup(sCaption, cHotkey);
41  }
42  // key callbacks: Check/Uncheck on space and primary joy button
44  Keys.emplace_back(K_SPACE);
46  {
47  ControllerKeys::Ok(Keys);
48  }
49  pKeyCheck = new C4KeyBinding(Keys, "GUICheckboxToggle", KEYSCOPE_Gui,
50  new ControlKeyCB<CheckBox>(*this, &CheckBox::KeyCheck), C4CustomKey::PRIO_Ctrl);
51  pCBHandler = nullptr;
52  }
53 
55  {
56  delete pKeyCheck;
57  if (pCBHandler) pCBHandler->DeRef();
58  }
59 
61  {
62  }
63 
64  bool CheckBox::OnHotkey(uint32_t cHotkey)
65  {
66  if (cHotkey != this->cHotkey) return false;
67  ToggleCheck(true);
68  return true;
69  }
70 
71  void CheckBox::ToggleCheck(bool fByUser)
72  {
73  // user can't toggle if disabled
74  if (fByUser && !fEnabled) return;
75  // sound
76  if (fByUser) GUISound("UI::Tick");
77  // toggle state
78  fChecked = !fChecked;
79  // callback (last call; may destroy element)
80  if (pCBHandler) pCBHandler->DoCall(this);
81  }
82 
83  void CheckBox::MouseInput(CMouse &rMouse, int32_t iButton, int32_t iX, int32_t iY, DWORD dwKeyParam)
84  {
85  if (fEnabled)
86  {
87  // set mouse-on flag depending on whether mouse is over box area
88  fMouseOn = Inside<int32_t>(iX, 0, rcBounds.Hgt) && Inside<int32_t>(iY, 0, rcBounds.Hgt);
89  // left-click within checkbox toggles it
90  if (iButton == C4MC_Button_LeftDown && fMouseOn)
91  {
92  ToggleCheck(true);
93  return;
94  }
95  }
96  // not recognized; base call
97  Control::MouseInput(rMouse, iButton, iX, iY, dwKeyParam);
98  }
99 
101  {
102  Control::MouseEnter(rMouse);
103  }
104 
106  {
107  fMouseOn = false;
108  Control::MouseLeave(rMouse);
109  }
110 
112  {
113  // left side: check facet (squared)
114  int x0 = rcBounds.x + cgo.TargetX;
115  int y0 = rcBounds.y + cgo.TargetY;
116  ::GraphicsResource.fctCheckbox.GetPhase(fChecked + 2*!fEnabled).DrawX(cgo.Surface, x0, y0, rcBounds.Hgt, rcBounds.Hgt);
117  // right of it: checkbox text
118  CStdFont *pUseFont = pFont ? pFont : &(::GraphicsResource.TextFont);
119  int32_t yOff; float fZoom;
120  if (pUseFont->GetLineHeight() <= rcBounds.Hgt)
121  {
122  yOff = std::max<int32_t>(rcBounds.Hgt - pUseFont->GetLineHeight(), 0)/2;
123  fZoom = 1.0f;
124  }
125  else
126  {
127  yOff = 0;
128  fZoom = (float) rcBounds.Hgt / std::max(pUseFont->GetLineHeight(), 1);
129  }
130  pDraw->TextOut(sCaption.getData(), *pUseFont, fZoom, cgo.Surface, x0 + rcBounds.Hgt + C4GUI_CheckBoxLabelSpacing, y0 + yOff, fEnabled ? dwEnabledClr : dwDisabledClr, ALeft, true);
131  // selection marker
132  if ((fMouseOn && IsInActiveDlg(false)) || HasDrawFocus())
133  {
136  pDraw->ResetBlitMode();
137  }
138  }
139 
141  {
142  if (pCBHandler) pCBHandler->DeRef();
143  if ((pCBHandler = pCB)) pCB->Ref();
144  }
145 
146  bool CheckBox::GetStandardCheckBoxSize(int *piWdt, int *piHgt, const char *szForCaptionText, CStdFont *pUseFont)
147  {
148  // get needed text size
149  if (!pUseFont) pUseFont = &(::GraphicsResource.TextFont);
150  int32_t iWdt=100, iHgt=32;
151  pUseFont->GetTextExtent(szForCaptionText, iWdt, iHgt, true);
152  // check box height equals text height
153  // add check box plus indent
154  if (piWdt) *piWdt = iWdt + iHgt + C4GUI_CheckBoxLabelSpacing;
155  if (piHgt) *piHgt = iHgt;
156  return true;
157  }
158 
159 } // namespace C4GUI
C4Config Config
Definition: C4Config.cpp:930
C4Draw * pDraw
Definition: C4Draw.cpp:42
C4GraphicsResource GraphicsResource
#define C4GUI_CheckboxFontClr
Definition: C4Gui.h:48
#define C4GUI_CheckboxDisabledFontClr
Definition: C4Gui.h:50
#define C4GUI_CheckBoxLabelSpacing
Definition: C4Gui.h:120
@ KEYSCOPE_Gui
const int32_t C4MC_Button_LeftDown
const int ALeft
Definition: C4Surface.h:41
#define C4GFXBLIT_ADDITIVE
Definition: C4Surface.h:26
uint32_t DWORD
int32_t GamepadGuiControl
Definition: C4Config.h:233
C4ConfigControls Controls
Definition: C4Config.h:263
std::vector< C4KeyCodeEx > CodeList
void SetBlitMode(DWORD dwBlitMode)
Definition: C4Draw.h:191
void ResetBlitMode()
Definition: C4Draw.h:192
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:561
C4Surface * Surface
Definition: C4Facet.h:117
C4Facet GetPhase(int iPhaseX=0, int iPhaseY=0)
Definition: C4Facet.cpp:59
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
virtual void DoCall(class Element *pElement)=0
void DrawElement(C4TargetFacet &cgo) override
~CheckBox() override
void MouseInput(CMouse &rMouse, int32_t iButton, int32_t iX, int32_t iY, DWORD dwKeyParam) override
void SetOnChecked(BaseCallbackHandler *pCB)
void MouseLeave(CMouse &rMouse) override
CheckBox(const C4Rect &rtBounds, const char *szCaption, bool fChecked)
bool OnHotkey(uint32_t cHotkey) override
void MouseEnter(CMouse &rMouse) override
static bool GetStandardCheckBoxSize(int *piWdt, int *piHgt, const char *szForCaptionText, CStdFont *pUseFont)
void ToggleCheck(bool fByUser)
void UpdateOwnPos() override
void MouseInput(CMouse &rMouse, int32_t iButton, int32_t iX, int32_t iY, DWORD dwKeyParam) override
C4Rect rcBounds
Definition: C4Gui.h:385
bool IsInActiveDlg(bool fForKeyboard)
Definition: C4Gui.cpp:435
virtual void MouseEnter(CMouse &rMouse)
Definition: C4Gui.h:413
virtual void MouseLeave(CMouse &rMouse)
Definition: C4Gui.h:414
C4FacetID fctButtonHighlightRound
Definition: C4Rect.h:28
int32_t y
Definition: C4Rect.h:30
int32_t Hgt
Definition: C4Rect.h:30
int32_t x
Definition: C4Rect.h:30
float TargetY
Definition: C4Facet.h:165
float TargetX
Definition: C4Facet.h:165
int GetLineHeight() const
Definition: C4FontLoader.h:125
bool GetTextExtent(const char *szText, int32_t &rsx, int32_t &rsy, bool fCheckMarkup=true)
const char * getData() const
Definition: StdBuf.h:442
void Copy()
Definition: StdBuf.h:467
bool ExpandHotkeyMarkup(StdStrBuf &sText, uint32_t &rcHotkey, bool for_tooltip)
Definition: C4Gui.cpp:38
void GUISound(const char *szSound)
Definition: C4Gui.cpp:1175
void Ok(T &keys)