openclonk/src/gui/C4GuiCheckBox.cpp

160 lines
4.6 KiB
C++
Raw Permalink Normal View History

2009-05-08 13:28:41 +00:00
/*
* OpenClonk, http://www.openclonk.org
*
* Copyright (c) 2005-2009, RedWolf Design GmbH, http://www.clonk.de/
2016-04-03 18:18:29 +00:00
* Copyright (c) 2010-2016, The OpenClonk Team and contributors
2009-05-08 13:28:41 +00:00
*
* Distributed under the terms of the ISC license; see accompanying file
* "COPYING" for details.
2009-05-08 13:28:41 +00:00
*
* "Clonk" is a registered trademark of Matthes Bender, used with permission.
* See accompanying file "TRADEMARK" for details.
2009-05-08 13:28:41 +00:00
*
* To redistribute this file separately, substitute the full license texts
* for the above references.
2009-05-08 13:28:41 +00:00
*/
// generic user interface
// checkbox
#include "C4Include.h"
#include "gui/C4Gui.h"
#include "graphics/C4Draw.h"
#include "graphics/C4FacetEx.h"
#include "graphics/C4GraphicsResource.h"
#include "gui/C4MouseControl.h"
2009-05-08 13:28:41 +00:00
2010-03-28 18:58:01 +00:00
namespace C4GUI
{
2009-05-08 13:28:41 +00:00
// ----------------------------------------------------
// CheckBox
2010-03-28 18:58:01 +00:00
CheckBox::CheckBox(const C4Rect &rtBounds, const char *szCaption, bool fChecked)
: Control(rtBounds), fChecked(fChecked), fMouseOn(false), fEnabled(true), pFont(nullptr)
2010-03-28 18:58:01 +00:00
, dwEnabledClr(C4GUI_CheckboxFontClr), dwDisabledClr(C4GUI_CheckboxDisabledFontClr), cHotkey(0)
2009-05-08 13:28:41 +00:00
{
2010-03-28 18:58:01 +00:00
if (szCaption)
2009-05-08 13:28:41 +00:00
{
2010-03-28 18:58:01 +00:00
sCaption.Copy(szCaption);
ExpandHotkeyMarkup(sCaption, cHotkey);
2009-05-08 13:28:41 +00:00
}
2010-03-28 18:58:01 +00:00
// key callbacks: Check/Uncheck on space and primary joy button
C4CustomKey::CodeList Keys;
Keys.emplace_back(K_SPACE);
2010-03-28 18:58:01 +00:00
if (Config.Controls.GamepadGuiControl)
2009-05-08 13:28:41 +00:00
{
ControllerKeys::Ok(Keys);
2009-05-08 13:28:41 +00:00
}
2010-03-28 18:58:01 +00:00
pKeyCheck = new C4KeyBinding(Keys, "GUICheckboxToggle", KEYSCOPE_Gui,
new ControlKeyCB<CheckBox>(*this, &CheckBox::KeyCheck), C4CustomKey::PRIO_Ctrl);
pCBHandler = nullptr;
2009-05-08 13:28:41 +00:00
}
2010-03-28 18:58:01 +00:00
CheckBox::~CheckBox()
2009-05-08 13:28:41 +00:00
{
2010-03-28 18:58:01 +00:00
delete pKeyCheck;
if (pCBHandler) pCBHandler->DeRef();
2009-05-08 13:28:41 +00:00
}
2010-03-28 18:58:01 +00:00
void CheckBox::UpdateOwnPos()
2009-05-08 13:28:41 +00:00
{
}
bool CheckBox::OnHotkey(uint32_t cHotkey)
2009-05-08 13:28:41 +00:00
{
2010-03-28 18:58:01 +00:00
if (cHotkey != this->cHotkey) return false;
ToggleCheck(true);
return true;
2009-05-08 13:28:41 +00:00
}
2010-03-28 18:58:01 +00:00
void CheckBox::ToggleCheck(bool fByUser)
2009-05-08 13:28:41 +00:00
{
2010-03-28 18:58:01 +00:00
// user can't toggle if disabled
if (fByUser && !fEnabled) return;
// sound
if (fByUser) GUISound("UI::Tick");
2010-03-28 18:58:01 +00:00
// toggle state
fChecked = !fChecked;
// callback (last call; may destroy element)
if (pCBHandler) pCBHandler->DoCall(this);
2009-05-08 13:28:41 +00:00
}
2010-03-28 18:58:01 +00:00
void CheckBox::MouseInput(CMouse &rMouse, int32_t iButton, int32_t iX, int32_t iY, DWORD dwKeyParam)
2009-05-08 13:28:41 +00:00
{
2010-03-28 18:58:01 +00:00
if (fEnabled)
2009-05-08 13:28:41 +00:00
{
2010-03-28 18:58:01 +00:00
// set mouse-on flag depending on whether mouse is over box area
fMouseOn = Inside<int32_t>(iX, 0, rcBounds.Hgt) && Inside<int32_t>(iY, 0, rcBounds.Hgt);
// left-click within checkbox toggles it
if (iButton == C4MC_Button_LeftDown && fMouseOn)
2009-05-08 13:28:41 +00:00
{
2010-03-28 18:58:01 +00:00
ToggleCheck(true);
return;
2009-05-08 13:28:41 +00:00
}
}
2010-03-28 18:58:01 +00:00
// not recognized; base call
Control::MouseInput(rMouse, iButton, iX, iY, dwKeyParam);
2009-05-08 13:28:41 +00:00
}
2010-03-28 18:58:01 +00:00
void CheckBox::MouseEnter(CMouse &rMouse)
2009-05-08 13:28:41 +00:00
{
2010-03-28 18:58:01 +00:00
Control::MouseEnter(rMouse);
2009-05-08 13:28:41 +00:00
}
2010-03-28 18:58:01 +00:00
void CheckBox::MouseLeave(CMouse &rMouse)
2009-05-08 13:28:41 +00:00
{
2010-03-28 18:58:01 +00:00
fMouseOn = false;
Control::MouseLeave(rMouse);
2009-05-08 13:28:41 +00:00
}
2010-03-28 18:58:01 +00:00
void CheckBox::DrawElement(C4TargetFacet &cgo)
2009-05-08 13:28:41 +00:00
{
2010-03-28 18:58:01 +00:00
// left side: check facet (squared)
int x0 = rcBounds.x + cgo.TargetX;
int y0 = rcBounds.y + cgo.TargetY;
::GraphicsResource.fctCheckbox.GetPhase(fChecked + 2*!fEnabled).DrawX(cgo.Surface, x0, y0, rcBounds.Hgt, rcBounds.Hgt);
2010-03-28 18:58:01 +00:00
// right of it: checkbox text
CStdFont *pUseFont = pFont ? pFont : &(::GraphicsResource.TextFont);
2010-03-28 18:58:01 +00:00
int32_t yOff; float fZoom;
if (pUseFont->GetLineHeight() <= rcBounds.Hgt)
2009-05-08 13:28:41 +00:00
{
yOff = std::max<int32_t>(rcBounds.Hgt - pUseFont->GetLineHeight(), 0)/2;
2010-03-28 18:58:01 +00:00
fZoom = 1.0f;
2009-05-08 13:28:41 +00:00
}
2010-03-28 18:58:01 +00:00
else
2009-05-08 13:28:41 +00:00
{
2010-03-28 18:58:01 +00:00
yOff = 0;
fZoom = (float) rcBounds.Hgt / std::max(pUseFont->GetLineHeight(), 1);
2009-05-08 13:28:41 +00:00
}
2011-10-03 14:30:18 +00:00
pDraw->TextOut(sCaption.getData(), *pUseFont, fZoom, cgo.Surface, x0 + rcBounds.Hgt + C4GUI_CheckBoxLabelSpacing, y0 + yOff, fEnabled ? dwEnabledClr : dwDisabledClr, ALeft, true);
2010-03-28 18:58:01 +00:00
// selection marker
if ((fMouseOn && IsInActiveDlg(false)) || HasDrawFocus())
2009-05-08 13:28:41 +00:00
{
2011-10-03 14:30:18 +00:00
pDraw->SetBlitMode(C4GFXBLIT_ADDITIVE);
::GraphicsResource.fctButtonHighlightRound.DrawX(cgo.Surface, x0+rcBounds.Hgt*1/4, y0+rcBounds.Hgt*1/4, rcBounds.Hgt*1/2, rcBounds.Hgt*1/2);
2011-10-03 14:30:18 +00:00
pDraw->ResetBlitMode();
2009-05-08 13:28:41 +00:00
}
}
2010-03-28 18:58:01 +00:00
void CheckBox::SetOnChecked(BaseCallbackHandler *pCB)
2009-05-08 13:28:41 +00:00
{
2010-03-28 18:58:01 +00:00
if (pCBHandler) pCBHandler->DeRef();
if ((pCBHandler = pCB)) pCB->Ref();
2009-05-08 13:28:41 +00:00
}
2010-03-28 18:58:01 +00:00
bool CheckBox::GetStandardCheckBoxSize(int *piWdt, int *piHgt, const char *szForCaptionText, CStdFont *pUseFont)
2009-05-08 13:28:41 +00:00
{
2010-03-28 18:58:01 +00:00
// get needed text size
if (!pUseFont) pUseFont = &(::GraphicsResource.TextFont);
2010-03-28 18:58:01 +00:00
int32_t iWdt=100, iHgt=32;
pUseFont->GetTextExtent(szForCaptionText, iWdt, iHgt, true);
// check box height equals text height
// add check box plus indent
if (piWdt) *piWdt = iWdt + iHgt + C4GUI_CheckBoxLabelSpacing;
if (piHgt) *piHgt = iHgt;
return true;
2009-05-08 13:28:41 +00:00
}
2010-01-25 04:00:59 +00:00
} // namespace C4GUI