From 205ed6c9f5d6680608765dde17cbcabedfbbffa9 Mon Sep 17 00:00:00 2001 From: Mariano Javier Suligoy Date: Sun, 1 Mar 2015 16:45:13 -0300 Subject: [PATCH] Improve mechanism and fix radio icon rendering. --- scene/gui/check_box.cpp | 45 ++++++++++--------- scene/gui/check_box.h | 7 +-- .../resources/default_theme/default_theme.cpp | 8 +++- 3 files changed, 32 insertions(+), 28 deletions(-) diff --git a/scene/gui/check_box.cpp b/scene/gui/check_box.cpp index 1f378336186..ac156f51443 100644 --- a/scene/gui/check_box.cpp +++ b/scene/gui/check_box.cpp @@ -31,9 +31,27 @@ #include "servers/visual_server.h" #include "button_group.h" -void CheckBox::_bind_methods() -{ - ObjectTypeDB::bind_method(_MD("set_pressed","pressed"),&CheckBox::toggled); + +void CheckBox::_notification(int p_what) { + + if (p_what==NOTIFICATION_DRAW) { + + RID ci = get_canvas_item(); + + Ref on=Control::get_icon(is_radio() ? "radio_checked" : "checked"); + Ref off=Control::get_icon(is_radio() ? "radio_unchecked" : "unchecked"); + + Vector2 ofs; + ofs.x = 0; + ofs.y = int((get_size().height - on->get_height())/2); + + if (is_pressed()) + on->draw(ci,ofs); + else + off->draw(ci,ofs); + + + } } bool CheckBox::is_radio() @@ -41,34 +59,19 @@ bool CheckBox::is_radio() Node* parent = this; do { parent = parent->get_parent(); - if (dynamic_cast< ButtonGroup* >( parent)) + if (dynamic_cast< ButtonGroup* >(parent)) break; } while (parent != nullptr); return (parent != nullptr); } -void CheckBox::update_icon(bool p_pressed) -{ - if (is_radio()) - set_icon(Control::get_icon(p_pressed ? "radio_checked" : "radio_unchecked")); - else - set_icon(Control::get_icon(p_pressed ? "checked" : "unchecked")); -} - -void CheckBox::toggled(bool p_pressed) -{ - update_icon(); - BaseButton::toggled(p_pressed); -} - -CheckBox::CheckBox() +CheckBox::CheckBox(const String &p_text): + Button(p_text) { set_toggle_mode(true); set_text_align(ALIGN_LEFT); - update_icon(is_pressed()); - } CheckBox::~CheckBox() diff --git a/scene/gui/check_box.h b/scene/gui/check_box.h index 1f0d1ed7ed4..171fd55351a 100644 --- a/scene/gui/check_box.h +++ b/scene/gui/check_box.h @@ -40,17 +40,14 @@ class CheckBox : public Button { protected: - static void _bind_methods(); + void _notification(int p_what); bool is_radio(); - void update_icon(bool p_pressed); - public: - void toggled(bool p_pressed); - CheckBox(); + CheckBox(const String& p_text=String()); ~CheckBox(); }; diff --git a/scene/resources/default_theme/default_theme.cpp b/scene/resources/default_theme/default_theme.cpp index ab0688af810..7d5981522e0 100644 --- a/scene/resources/default_theme/default_theme.cpp +++ b/scene/resources/default_theme/default_theme.cpp @@ -303,9 +303,13 @@ void make_default_theme() { // CheckBox Ref cbx_empty = memnew( StyleBoxEmpty ); + cbx_empty->set_default_margin(MARGIN_LEFT,22); + cbx_empty->set_default_margin(MARGIN_RIGHT,4); + cbx_empty->set_default_margin(MARGIN_TOP,4); + cbx_empty->set_default_margin(MARGIN_BOTTOM,5); Ref cbx_focus = focus; cbx_focus->set_default_margin(MARGIN_LEFT,4); - cbx_focus->set_default_margin(MARGIN_RIGHT,4); + cbx_focus->set_default_margin(MARGIN_RIGHT,22); cbx_focus->set_default_margin(MARGIN_TOP,4); cbx_focus->set_default_margin(MARGIN_BOTTOM,5); @@ -570,7 +574,7 @@ void make_default_theme() { // Tree Ref tree_selected = make_stylebox( selection_png,4,4,4,4,8,0,8,0); - Ref tree_selected_oof = make_stylebox( selection_oof_png,4,4,4,4,8,0,8,0); + Ref tree_selected_oof = make_stylebox( selection_oof_png,4,4,4,4,8,0,8,0); t->set_stylebox("bg","Tree", make_stylebox( tree_bg_png,4,4,4,5) ); t->set_stylebox("bg_focus","Tree", focus );