tibia-client/src/framework/ui/uicheckbox.cpp

80 lines
2.9 KiB
C++
Raw Normal View History

2011-11-16 00:47:32 +01:00
/*
2012-01-02 17:58:37 +01:00
* Copyright (c) 2010-2012 OTClient <https://github.com/edubart/otclient>
2011-11-16 00:47:32 +01:00
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include "uicheckbox.h"
#include "uitranslator.h"
2011-11-16 00:47:32 +01:00
#include <framework/otml/otmlnode.h>
#include <framework/graphics/image.h>
#include <framework/graphics/font.h>
#include <framework/graphics/graphics.h>
#include <framework/core/eventdispatcher.h>
2011-11-17 18:43:41 +01:00
UICheckBox::UICheckBox()
{
2011-11-17 18:43:41 +01:00
m_focusable = false;
m_textAlign = Fw::AlignLeft;
}
2011-11-16 00:47:32 +01:00
void UICheckBox::render()
{
2011-11-16 03:50:48 +01:00
if(m_image) {
Rect boxRect;
2011-12-07 01:31:55 +01:00
boxRect.resize(m_boxSize);
2011-11-16 03:50:48 +01:00
boxRect.moveLeft(m_rect.left());
boxRect.moveVerticalCenter(m_rect.verticalCenter());
2011-12-07 01:31:55 +01:00
g_painter.setColor(m_backgroundColor);
2011-11-16 03:50:48 +01:00
m_image->draw(boxRect);
}
2011-11-16 00:47:32 +01:00
2011-11-16 03:50:48 +01:00
if(m_text.length()) {
Rect textRect(m_rect);
textRect.setTopLeft(textRect.topLeft() + m_textOffset);
m_font->renderText(m_text, textRect, m_textAlign, m_foregroundColor);
2011-11-16 03:50:48 +01:00
}
2011-11-16 00:47:32 +01:00
}
void UICheckBox::onMouseRelease(const Point& mousePos, Fw::MouseButton button)
{
if(isPressed() && getRect().contains(mousePos))
setChecked(!isChecked());
2011-11-16 00:47:32 +01:00
}
void UICheckBox::onStyleApply(const std::string& styleName, const OTMLNodePtr& styleNode)
2011-11-16 00:47:32 +01:00
{
UIWidget::onStyleApply(styleName, styleNode);
2011-11-16 00:47:32 +01:00
for(OTMLNodePtr node : styleNode->children()) {
if(node->tag() == "text-offset")
m_textOffset = node->value<Point>();
else if(node->tag() == "text")
m_text = node->value();
else if(node->tag() == "box-size")
m_boxSize = node->value<Size>();
else if(node->tag() == "text-align")
m_textAlign = Fw::translateAlignment(node->value());
2011-11-16 00:47:32 +01:00
else if(node->tag() == "checked") {
// must be scheduled because setChecked can change the style again
g_dispatcher.addEvent(std::bind(&UICheckBox::setChecked, asUICheckBox(), node->value<bool>()));
}
}
}