From c94e93337b778db7e024483f3f261dcef4bd19c5 Mon Sep 17 00:00:00 2001 From: Stijn Buys Date: Fri, 29 Jul 2016 18:32:19 +0200 Subject: Added checkbox widget. --- src/ui/Makefile.am | 2 + src/ui/checkbox.cc | 135 +++++++++++++++++++++++++++++++++++++++++++++++++++++ src/ui/checkbox.h | 86 ++++++++++++++++++++++++++++++++++ 3 files changed, 223 insertions(+) create mode 100644 src/ui/checkbox.cc create mode 100644 src/ui/checkbox.h (limited to 'src/ui') diff --git a/src/ui/Makefile.am b/src/ui/Makefile.am index 340f8fb..1485565 100644 --- a/src/ui/Makefile.am +++ b/src/ui/Makefile.am @@ -11,6 +11,7 @@ endif noinst_HEADERS = \ bitmap.h \ button.h \ + checkbox.h \ colorpicker.h \ console.h \ definitions.h \ @@ -35,6 +36,7 @@ noinst_HEADERS = \ libui_la_SOURCES = \ bitmap.cc \ button.cc \ + checkbox.cc \ colorpicker.cc \ console.cc \ font.cc \ diff --git a/src/ui/checkbox.cc b/src/ui/checkbox.cc new file mode 100644 index 0000000..575b3d4 --- /dev/null +++ b/src/ui/checkbox.cc @@ -0,0 +1,135 @@ +/* + 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 diff --git a/src/ui/checkbox.h b/src/ui/checkbox.h new file mode 100644 index 0000000..aab42cf --- /dev/null +++ b/src/ui/checkbox.h @@ -0,0 +1,86 @@ +/* + ui/checkbox.h + This file is part of the Osirion project and is distributed under + the terms of the GNU General Public License version 2 +*/ + +#ifndef __INCLUDED_UI_CHECKBOX_H__ +#define __INCLUDED_UI_CHECKBOX_H__ + +#include "ui/widget.h" + +namespace ui +{ + +/** + * @brief a checkbox widget + * */ +class Checkbox : public Widget +{ +public: + Checkbox(Widget *parent, const char *text = 0); + ~Checkbox(); + + /** + * @brief set the text displayed next to the checkbox + * */ + inline const std::string & text() const { + return _text; + } + + /** + * @brief current value indicated by the checkbox + * */ + inline const bool value() const { + return _value; + } + + /** + * @brief set the text displayed next to the checkbox + * */ + void set_text(const std::string &text); + + /** + * @brief set the text displayed next to the checkbox + * If text is NULL, the displayed text will be cleared. + * */ + void set_text(const char *text); + + /** + * @brief set the checkbox value + * */ + void set_value(const bool value); + + /** + * @brief print checkbox description + * */ + virtual void print(const size_t indent) const; + + +protected: + + /** + * @brief mouse over event handler + * */ + virtual void on_mouseover(const math::Vector2f &cursor); + + /** + * @brief mouse button press event handler + * */ + virtual bool on_mousepress(const unsigned int button); + + /// draw the button border + virtual void draw_border(); + + /// draw the button + virtual void draw(); + +private: + bool _value; + std::string _text; +}; + +} + +#endif // __INCLUDED_UI_CHECKBOX_H__ + -- cgit v1.2.3