Improve mechanism and fix radio icon rendering.

This commit is contained in:
Mariano Javier Suligoy 2015-03-01 16:45:13 -03:00
parent e8e9f100e5
commit 205ed6c9f5
3 changed files with 32 additions and 28 deletions

View File

@ -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<Texture> on=Control::get_icon(is_radio() ? "radio_checked" : "checked");
Ref<Texture> 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()

View File

@ -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();
};

View File

@ -303,9 +303,13 @@ void make_default_theme() {
// CheckBox
Ref<StyleBox> 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<StyleBox> 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<StyleBoxTexture> tree_selected = make_stylebox( selection_png,4,4,4,4,8,0,8,0);
Ref<StyleBoxTexture> tree_selected_oof = make_stylebox( selection_oof_png,4,4,4,4,8,0,8,0);
Ref<StyleBoxTexture> 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 );