/* ui/checkbox.cc This file is part of the Osirion project and is distributed under the terms of the GNU General Public License version 2 */ #include "ui/checkbox.h" #include "ui/paint.h" #include "math/functions.h" #include "audio/audio.h" #include "core/application.h" namespace ui { Checkbox::Checkbox(Widget *parent, const char *text) : Widget(parent) { set_label("checkbox"); set_background(false); set_border(false); if (text) { _text.assign(text); } } Checkbox::~Checkbox() { } void Checkbox::set_text(const std::string &text) { _text.assign(text); } void Checkbox::set_text(const char *text) { if (text) { _text.assign(text); } else { _text.clear(); } } void Checkbox::set_value(const bool value) { _value = value; } void Checkbox::print(const size_t indent) const { std::string marker(""); con_print << aux::pad_left(marker, indent*2) << label() << " \"" << (value() ? "true" : "false") << "\"" << std::endl; } void Checkbox::draw() { const float iconwidth = math::min(width(), height()); math::Vector2f s(iconwidth, iconwidth); math::Vector2f l(global_location()); math::Color color; if (disabled()) { color.assign(palette()->disabled()); } else if (has_mouse_focus()) { color.assign(palette()->highlight()); } else { color.assign(palette()->foreground()); } std::string iconname(value() ? "bitmaps/ui/checkbox_on" : "bitmaps/ui/checkbox_off"); Paint::draw_bitmap(l, s, color, iconname); l.get_x() += iconwidth; s.get_x() = width() - iconwidth; Paint::set_color(color); Paint::draw_label(l, s, font(), text(), AlignLeft | AlignVCenter); // if (enabled() && has_mouse_focus()) { // root()->set_pointer("action", Palette::Highlight); // } } void Checkbox::draw_border() { if (disabled()) { Paint::set_color(palette()->disabled()); } else if (has_mouse_focus()) { math::Color color(palette()->foreground()); float t = core::application()->time(); t = t - floorf(t); if (t > 0.5) t = 1 - t; color.a = 0.5f + t; Paint::set_color(color); } else { Paint::set_color(palette()->border()); } Paint::draw_border(global_location(), size()); } bool Checkbox::on_mousepress(const unsigned int button) { if (button == SDL_BUTTON_LEFT) { if (enabled()) { audio::play("ui/clicked"); emit(EventClicked); _value = !_value; } return true; } return false; } void Checkbox::on_mouseover(const math::Vector2f &cursor) { if (enabled()) { //audio::play("ui/select"); } } } // namespace ui