diff options
Diffstat (limited to '')
| -rw-r--r-- | scene/gui/dialogs.cpp | 258 |
1 files changed, 114 insertions, 144 deletions
diff --git a/scene/gui/dialogs.cpp b/scene/gui/dialogs.cpp index 923d2ec56..c887ae9cd 100644 --- a/scene/gui/dialogs.cpp +++ b/scene/gui/dialogs.cpp @@ -27,46 +27,43 @@ /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /*************************************************************************/ #include "dialogs.h" -#include "print_string.h" #include "line_edit.h" +#include "print_string.h" #include "translation.h" void WindowDialog::_post_popup() { - dragging=false; //just in case + dragging = false; //just in case } -bool WindowDialog::has_point(const Point2& p_point) const { - +bool WindowDialog::has_point(const Point2 &p_point) const { - int extra = get_constant("titlebar_height","WindowDialog"); - Rect2 r( Point2(), get_size() ); - r.pos.y-=extra; - r.size.y+=extra; + int extra = get_constant("titlebar_height", "WindowDialog"); + Rect2 r(Point2(), get_size()); + r.pos.y -= extra; + r.size.y += extra; return r.has_point(p_point); - } -void WindowDialog::_input_event(const InputEvent& p_event) { +void WindowDialog::_input_event(const InputEvent &p_event) { - if (p_event.type == InputEvent::MOUSE_BUTTON && p_event.mouse_button.button_index==BUTTON_LEFT) { + if (p_event.type == InputEvent::MOUSE_BUTTON && p_event.mouse_button.button_index == BUTTON_LEFT) { if (p_event.mouse_button.pressed && p_event.mouse_button.y < 0) - dragging=true; + dragging = true; else if (dragging && !p_event.mouse_button.pressed) - dragging=false; + dragging = false; } - if (p_event.type == InputEvent::MOUSE_MOTION && dragging) { - Point2 rel( p_event.mouse_motion.relative_x, p_event.mouse_motion.relative_y ); + Point2 rel(p_event.mouse_motion.relative_x, p_event.mouse_motion.relative_y); Point2 pos = get_pos(); - pos+=rel; + pos += rel; - if (pos.y<0) - pos.y=0; + if (pos.y < 0) + pos.y = 0; set_pos(pos); } @@ -74,35 +71,33 @@ void WindowDialog::_input_event(const InputEvent& p_event) { void WindowDialog::_notification(int p_what) { - switch(p_what) { + switch (p_what) { case NOTIFICATION_DRAW: { RID ci = get_canvas_item(); Size2 s = get_size(); - Ref<StyleBox> st = get_stylebox("panel","WindowDialog"); - st->draw(ci,Rect2(Point2(),s)); - int th = get_constant("title_height","WindowDialog"); - Color tc = get_color("title_color","WindowDialog"); - Ref<Font> font = get_font("title_font","WindowDialog"); - int ofs = (s.width-font->get_string_size(title).width)/2; + Ref<StyleBox> st = get_stylebox("panel", "WindowDialog"); + st->draw(ci, Rect2(Point2(), s)); + int th = get_constant("title_height", "WindowDialog"); + Color tc = get_color("title_color", "WindowDialog"); + Ref<Font> font = get_font("title_font", "WindowDialog"); + int ofs = (s.width - font->get_string_size(title).width) / 2; //int ofs = st->get_margin(MARGIN_LEFT); - draw_string(font,Point2(ofs,-th+font->get_ascent()),title,tc,s.width - st->get_minimum_size().width); - + draw_string(font, Point2(ofs, -th + font->get_ascent()), title, tc, s.width - st->get_minimum_size().width); } break; case NOTIFICATION_THEME_CHANGED: case NOTIFICATION_ENTER_TREE: { - close_button->set_normal_texture( get_icon("close","WindowDialog")); - close_button->set_pressed_texture( get_icon("close","WindowDialog")); - close_button->set_hover_texture( get_icon("close_hilite","WindowDialog")); - close_button->set_anchor(MARGIN_LEFT,ANCHOR_END); - close_button->set_begin( Point2( get_constant("close_h_ofs","WindowDialog"), -get_constant("close_v_ofs","WindowDialog") )); + close_button->set_normal_texture(get_icon("close", "WindowDialog")); + close_button->set_pressed_texture(get_icon("close", "WindowDialog")); + close_button->set_hover_texture(get_icon("close_hilite", "WindowDialog")); + close_button->set_anchor(MARGIN_LEFT, ANCHOR_END); + close_button->set_begin(Point2(get_constant("close_h_ofs", "WindowDialog"), -get_constant("close_v_ofs", "WindowDialog"))); } break; } - } void WindowDialog::_closed() { @@ -111,103 +106,90 @@ void WindowDialog::_closed() { hide(); } -void WindowDialog::set_title(const String& p_title) { +void WindowDialog::set_title(const String &p_title) { - title=XL_MESSAGE(p_title); + title = XL_MESSAGE(p_title); update(); } Size2 WindowDialog::get_minimum_size() const { - Ref<Font> font = get_font("title_font","WindowDialog"); - int msx=close_button->get_combined_minimum_size().x; - msx+=font->get_string_size(title).x; + Ref<Font> font = get_font("title_font", "WindowDialog"); + int msx = close_button->get_combined_minimum_size().x; + msx += font->get_string_size(title).x; - return Size2(msx,1); + return Size2(msx, 1); } - String WindowDialog::get_title() const { return title; } - TextureButton *WindowDialog::get_close_button() { - return close_button; } void WindowDialog::_bind_methods() { - ObjectTypeDB::bind_method( _MD("_input_event"),&WindowDialog::_input_event); - ObjectTypeDB::bind_method( _MD("set_title","title"),&WindowDialog::set_title); - ObjectTypeDB::bind_method( _MD("get_title"),&WindowDialog::get_title); - ObjectTypeDB::bind_method( _MD("_closed"),&WindowDialog::_closed); - ObjectTypeDB::bind_method( _MD("get_close_button:TextureButton"),&WindowDialog::get_close_button); + ObjectTypeDB::bind_method(_MD("_input_event"), &WindowDialog::_input_event); + ObjectTypeDB::bind_method(_MD("set_title", "title"), &WindowDialog::set_title); + ObjectTypeDB::bind_method(_MD("get_title"), &WindowDialog::get_title); + ObjectTypeDB::bind_method(_MD("_closed"), &WindowDialog::_closed); + ObjectTypeDB::bind_method(_MD("get_close_button:TextureButton"), &WindowDialog::get_close_button); - ADD_PROPERTY( PropertyInfo(Variant::STRING,"window/title",PROPERTY_HINT_NONE,"",PROPERTY_USAGE_DEFAULT_INTL),_SCS("set_title"),_SCS("get_title")); + ADD_PROPERTY(PropertyInfo(Variant::STRING, "window/title", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT_INTL), _SCS("set_title"), _SCS("get_title")); } WindowDialog::WindowDialog() { //title="Hello!"; - dragging=false; - close_button = memnew( TextureButton ); + dragging = false; + close_button = memnew(TextureButton); add_child(close_button); - close_button->connect("pressed",this,"_closed"); - + close_button->connect("pressed", this, "_closed"); } -WindowDialog::~WindowDialog(){ - - +WindowDialog::~WindowDialog() { } - void PopupDialog::_notification(int p_what) { - if (p_what==NOTIFICATION_DRAW) { + if (p_what == NOTIFICATION_DRAW) { RID ci = get_canvas_item(); - get_stylebox("panel","PopupMenu")->draw(ci,Rect2(Point2(),get_size())); + get_stylebox("panel", "PopupMenu")->draw(ci, Rect2(Point2(), get_size())); } } PopupDialog::PopupDialog() { - - } PopupDialog::~PopupDialog() { - - } - // - void AcceptDialog::_post_popup() { WindowDialog::_post_popup(); get_ok()->grab_focus(); - } void AcceptDialog::_notification(int p_what) { - if (p_what==NOTIFICATION_MODAL_CLOSE) { + if (p_what == NOTIFICATION_MODAL_CLOSE) { cancel_pressed(); - } if (p_what==NOTIFICATION_RESIZED) { + } + if (p_what == NOTIFICATION_RESIZED) { _update_child_rect(); } } -void AcceptDialog::_builtin_text_entered(const String& p_text) { +void AcceptDialog::_builtin_text_entered(const String &p_text) { _ok_pressed(); } @@ -218,7 +200,6 @@ void AcceptDialog::_ok_pressed() { hide(); ok_pressed(); emit_signal("confirmed"); - } void AcceptDialog::_close_pressed() { @@ -238,7 +219,7 @@ void AcceptDialog::set_text(String p_text) { void AcceptDialog::set_hide_on_ok(bool p_hide) { - hide_on_ok=p_hide; + hide_on_ok = p_hide; } bool AcceptDialog::get_hide_on_ok() const { @@ -246,24 +227,23 @@ bool AcceptDialog::get_hide_on_ok() const { return hide_on_ok; } - void AcceptDialog::register_text_enter(Node *p_line_edit) { ERR_FAIL_NULL(p_line_edit); - p_line_edit->connect("text_entered", this,"_builtin_text_entered"); + p_line_edit->connect("text_entered", this, "_builtin_text_entered"); } void AcceptDialog::_update_child_rect() { - Size2 label_size=label->get_minimum_size(); + Size2 label_size = label->get_minimum_size(); if (label->get_text().empty()) { label_size.height = 0; } - int margin = get_constant("margin","Dialogs"); + int margin = get_constant("margin", "Dialogs"); Size2 size = get_size(); Size2 hminsize = hbc->get_combined_minimum_size(); - Vector2 cpos(margin,margin+label_size.height); - Vector2 csize(size.x-margin*2,size.y-margin*3-hminsize.y-label_size.height); + Vector2 cpos(margin, margin + label_size.height); + Vector2 csize(size.x - margin * 2, size.y - margin * 3 - hminsize.y - label_size.height); if (child) { @@ -271,64 +251,61 @@ void AcceptDialog::_update_child_rect() { child->set_size(csize); } - cpos.y+=csize.y+margin; - csize.y=hminsize.y; + cpos.y += csize.y + margin; + csize.y = hminsize.y; hbc->set_pos(cpos); hbc->set_size(csize); - } Size2 AcceptDialog::get_minimum_size() const { - int margin = get_constant("margin","Dialogs"); + int margin = get_constant("margin", "Dialogs"); Size2 minsize = label->get_combined_minimum_size(); if (child) { Size2 cminsize = child->get_combined_minimum_size(); - minsize.x=MAX(cminsize.x,minsize.x); - minsize.y=MAX(cminsize.y,minsize.y); + minsize.x = MAX(cminsize.x, minsize.x); + minsize.y = MAX(cminsize.y, minsize.y); } Size2 hminsize = hbc->get_combined_minimum_size(); - minsize.x = MAX(hminsize.x,minsize.x); - minsize.y+=hminsize.y; - minsize.x+=margin*2; - minsize.y+=margin*3; //one as separation between hbc and child + minsize.x = MAX(hminsize.x, minsize.x); + minsize.y += hminsize.y; + minsize.x += margin * 2; + minsize.y += margin * 3; //one as separation between hbc and child Size2 wmsize = WindowDialog::get_minimum_size(); - minsize.x=MAX(wmsize.x,minsize.x); + minsize.x = MAX(wmsize.x, minsize.x); return minsize; } - void AcceptDialog::set_child_rect(Control *p_child) { - ERR_FAIL_COND(p_child->get_parent()!=this); + ERR_FAIL_COND(p_child->get_parent() != this); //p_child->set_area_as_parent_rect(get_constant("margin","Dialogs")); - child=p_child; + child = p_child; minimum_size_changed(); _update_child_rect(); } void AcceptDialog::remove_child_notify(Node *p_child) { - if (p_child==child) { - child=NULL; + if (p_child == child) { + child = NULL; } } -void AcceptDialog::_custom_action(const String& p_action) { +void AcceptDialog::_custom_action(const String &p_action) { - emit_signal("custom_action",p_action); + emit_signal("custom_action", p_action); custom_action(p_action); } -Button* AcceptDialog::add_button(const String& p_text,bool p_right,const String& p_action) { - +Button *AcceptDialog::add_button(const String &p_text, bool p_right, const String &p_action) { - Button *button = memnew( Button ); + Button *button = memnew(Button); button->set_text(p_text); if (p_right) { hbc->add_child(button); @@ -336,99 +313,92 @@ Button* AcceptDialog::add_button(const String& p_text,bool p_right,const String& } else { hbc->add_child(button); - hbc->move_child(button,0); + hbc->move_child(button, 0); hbc->add_spacer(true); } - if (p_action!="") { - button->connect("pressed",this,"_custom_action",varray(p_action)); + if (p_action != "") { + button->connect("pressed", this, "_custom_action", varray(p_action)); } return button; } -Button* AcceptDialog::add_cancel(const String &p_cancel) { +Button *AcceptDialog::add_cancel(const String &p_cancel) { String c = p_cancel; - if (p_cancel=="") - c=RTR("Cancel"); - Button *b = swap_ok_cancel ? add_button(c,true) : add_button(c); - b->connect("pressed",this,"_closed"); + if (p_cancel == "") + c = RTR("Cancel"); + Button *b = swap_ok_cancel ? add_button(c, true) : add_button(c); + b->connect("pressed", this, "_closed"); return b; } void AcceptDialog::_bind_methods() { - ObjectTypeDB::bind_method(_MD("_ok"),&AcceptDialog::_ok_pressed); - ObjectTypeDB::bind_method(_MD("get_ok"),&AcceptDialog::get_ok); - ObjectTypeDB::bind_method(_MD("get_label"),&AcceptDialog::get_label); - ObjectTypeDB::bind_method(_MD("set_hide_on_ok","enabled"),&AcceptDialog::set_hide_on_ok); - ObjectTypeDB::bind_method(_MD("get_hide_on_ok"),&AcceptDialog::get_hide_on_ok); - ObjectTypeDB::bind_method(_MD("add_button:Button","text","right","action"),&AcceptDialog::add_button,DEFVAL(false),DEFVAL("")); - ObjectTypeDB::bind_method(_MD("add_cancel:Button","name"),&AcceptDialog::add_cancel); - ObjectTypeDB::bind_method(_MD("_builtin_text_entered"),&AcceptDialog::_builtin_text_entered); - ObjectTypeDB::bind_method(_MD("register_text_enter:LineEdit","line_edit"),&AcceptDialog::register_text_enter); - ObjectTypeDB::bind_method(_MD("_custom_action"),&AcceptDialog::_custom_action); - ObjectTypeDB::bind_method(_MD("set_text","text"),&AcceptDialog::set_text); - ObjectTypeDB::bind_method(_MD("get_text"),&AcceptDialog::get_text); + ObjectTypeDB::bind_method(_MD("_ok"), &AcceptDialog::_ok_pressed); + ObjectTypeDB::bind_method(_MD("get_ok"), &AcceptDialog::get_ok); + ObjectTypeDB::bind_method(_MD("get_label"), &AcceptDialog::get_label); + ObjectTypeDB::bind_method(_MD("set_hide_on_ok", "enabled"), &AcceptDialog::set_hide_on_ok); + ObjectTypeDB::bind_method(_MD("get_hide_on_ok"), &AcceptDialog::get_hide_on_ok); + ObjectTypeDB::bind_method(_MD("add_button:Button", "text", "right", "action"), &AcceptDialog::add_button, DEFVAL(false), DEFVAL("")); + ObjectTypeDB::bind_method(_MD("add_cancel:Button", "name"), &AcceptDialog::add_cancel); + ObjectTypeDB::bind_method(_MD("_builtin_text_entered"), &AcceptDialog::_builtin_text_entered); + ObjectTypeDB::bind_method(_MD("register_text_enter:LineEdit", "line_edit"), &AcceptDialog::register_text_enter); + ObjectTypeDB::bind_method(_MD("_custom_action"), &AcceptDialog::_custom_action); + ObjectTypeDB::bind_method(_MD("set_text", "text"), &AcceptDialog::set_text); + ObjectTypeDB::bind_method(_MD("get_text"), &AcceptDialog::get_text); - ADD_SIGNAL( MethodInfo("confirmed") ); - ADD_SIGNAL( MethodInfo("custom_action",PropertyInfo(Variant::STRING,"action")) ); - - ADD_PROPERTYNZ( PropertyInfo(Variant::STRING,"dialog/text",PROPERTY_HINT_MULTILINE_TEXT,"",PROPERTY_USAGE_DEFAULT_INTL),_SCS("set_text"),_SCS("get_text")); - ADD_PROPERTY( PropertyInfo(Variant::BOOL, "dialog/hide_on_ok"),_SCS("set_hide_on_ok"),_SCS("get_hide_on_ok") ); + ADD_SIGNAL(MethodInfo("confirmed")); + ADD_SIGNAL(MethodInfo("custom_action", PropertyInfo(Variant::STRING, "action"))); + ADD_PROPERTYNZ(PropertyInfo(Variant::STRING, "dialog/text", PROPERTY_HINT_MULTILINE_TEXT, "", PROPERTY_USAGE_DEFAULT_INTL), _SCS("set_text"), _SCS("get_text")); + ADD_PROPERTY(PropertyInfo(Variant::BOOL, "dialog/hide_on_ok"), _SCS("set_hide_on_ok"), _SCS("get_hide_on_ok")); } - -bool AcceptDialog::swap_ok_cancel=false; +bool AcceptDialog::swap_ok_cancel = false; void AcceptDialog::set_swap_ok_cancel(bool p_swap) { - swap_ok_cancel=p_swap; + swap_ok_cancel = p_swap; } AcceptDialog::AcceptDialog() { - int margin = get_constant("margin","Dialogs"); - int button_margin = get_constant("button_margin","Dialogs"); + int margin = get_constant("margin", "Dialogs"); + int button_margin = get_constant("button_margin", "Dialogs"); - - label = memnew( Label ); - label->set_anchor(MARGIN_RIGHT,ANCHOR_END); - label->set_anchor(MARGIN_BOTTOM,ANCHOR_END); - label->set_begin( Point2( margin, margin) ); - label->set_end( Point2( margin, button_margin+10) ); + label = memnew(Label); + label->set_anchor(MARGIN_RIGHT, ANCHOR_END); + label->set_anchor(MARGIN_BOTTOM, ANCHOR_END); + label->set_begin(Point2(margin, margin)); + label->set_end(Point2(margin, button_margin + 10)); //label->set_autowrap(true); add_child(label); - hbc = memnew( HBoxContainer ); + hbc = memnew(HBoxContainer); add_child(hbc); hbc->add_spacer(); - ok = memnew( Button ); + ok = memnew(Button); ok->set_text(RTR("OK")); hbc->add_child(ok); hbc->add_spacer(); - - ok->connect("pressed", this,"_ok"); + ok->connect("pressed", this, "_ok"); set_as_toplevel(true); - hide_on_ok=true; + hide_on_ok = true; set_title(RTR("Alert!")); - child=NULL; + child = NULL; } - -AcceptDialog::~AcceptDialog() -{ +AcceptDialog::~AcceptDialog() { } - void ConfirmationDialog::_bind_methods() { - ObjectTypeDB::bind_method(_MD("get_cancel:Button"),&ConfirmationDialog::get_cancel); + ObjectTypeDB::bind_method(_MD("get_cancel:Button"), &ConfirmationDialog::get_cancel); } Button *ConfirmationDialog::get_cancel() { |
