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 "servers/visual_server.h"
#include "button_group.h" #include "button_group.h"
void CheckBox::_bind_methods()
{ void CheckBox::_notification(int p_what) {
ObjectTypeDB::bind_method(_MD("set_pressed","pressed"),&CheckBox::toggled);
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() bool CheckBox::is_radio()
@ -48,27 +66,12 @@ bool CheckBox::is_radio()
return (parent != nullptr); return (parent != nullptr);
} }
void CheckBox::update_icon(bool p_pressed) CheckBox::CheckBox(const String &p_text):
{ Button(p_text)
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_toggle_mode(true);
set_text_align(ALIGN_LEFT); set_text_align(ALIGN_LEFT);
update_icon(is_pressed());
} }
CheckBox::~CheckBox() CheckBox::~CheckBox()

View File

@ -40,17 +40,14 @@ class CheckBox : public Button {
protected: protected:
static void _bind_methods(); void _notification(int p_what);
bool is_radio(); bool is_radio();
void update_icon(bool p_pressed);
public: public:
void toggled(bool p_pressed);
CheckBox(); CheckBox(const String& p_text=String());
~CheckBox(); ~CheckBox();
}; };

View File

@ -303,9 +303,13 @@ void make_default_theme() {
// CheckBox // CheckBox
Ref<StyleBox> cbx_empty = memnew( StyleBoxEmpty ); 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; Ref<StyleBox> cbx_focus = focus;
cbx_focus->set_default_margin(MARGIN_LEFT,4); 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_TOP,4);
cbx_focus->set_default_margin(MARGIN_BOTTOM,5); cbx_focus->set_default_margin(MARGIN_BOTTOM,5);