From e8e9f100e57d6b68915fc6df5ed153384b026371 Mon Sep 17 00:00:00 2001 From: Mariano Javier Suligoy Date: Sun, 1 Mar 2015 11:23:05 -0300 Subject: Add CheckBox control with theme edition and radio icon avaible. --- scene/gui/check_box.cpp | 76 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 scene/gui/check_box.cpp (limited to 'scene/gui/check_box.cpp') diff --git a/scene/gui/check_box.cpp b/scene/gui/check_box.cpp new file mode 100644 index 000000000..1f3783361 --- /dev/null +++ b/scene/gui/check_box.cpp @@ -0,0 +1,76 @@ +/*************************************************************************/ +/* check_button.cpp */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* http://www.godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ +/* */ +/* 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 "check_box.h" + +#include "servers/visual_server.h" +#include "button_group.h" + +void CheckBox::_bind_methods() +{ + ObjectTypeDB::bind_method(_MD("set_pressed","pressed"),&CheckBox::toggled); +} + +bool CheckBox::is_radio() +{ + Node* parent = this; + do { + parent = parent->get_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() +{ + set_toggle_mode(true); + set_text_align(ALIGN_LEFT); + + update_icon(is_pressed()); + +} + +CheckBox::~CheckBox() +{ +} -- cgit v1.2.3-70-g09d2 From 205ed6c9f5d6680608765dde17cbcabedfbbffa9 Mon Sep 17 00:00:00 2001 From: Mariano Javier Suligoy Date: Sun, 1 Mar 2015 16:45:13 -0300 Subject: Improve mechanism and fix radio icon rendering. --- scene/gui/check_box.cpp | 45 +++++++++++++------------ scene/gui/check_box.h | 7 ++-- scene/resources/default_theme/default_theme.cpp | 8 +++-- 3 files changed, 32 insertions(+), 28 deletions(-) (limited to 'scene/gui/check_box.cpp') diff --git a/scene/gui/check_box.cpp b/scene/gui/check_box.cpp index 1f3783361..ac156f514 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 1f0d1ed7e..171fd5535 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 ab0688af8..7d5981522 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 ); -- cgit v1.2.3-70-g09d2 From 0bc7eb1d91212e5fcc799d1b790430a4c2468751 Mon Sep 17 00:00:00 2001 From: Mariano Javier Suligoy Date: Sun, 8 Mar 2015 10:39:27 -0300 Subject: Fix C++11 compilation --- scene/gui/check_box.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'scene/gui/check_box.cpp') diff --git a/scene/gui/check_box.cpp b/scene/gui/check_box.cpp index ac156f514..309152ba8 100644 --- a/scene/gui/check_box.cpp +++ b/scene/gui/check_box.cpp @@ -61,9 +61,9 @@ bool CheckBox::is_radio() parent = parent->get_parent(); if (dynamic_cast< ButtonGroup* >(parent)) break; - } while (parent != nullptr); + } while (parent); - return (parent != nullptr); + return (parent != 0); } CheckBox::CheckBox(const String &p_text): -- cgit v1.2.3-70-g09d2