123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441 |
- /*************************************************************************/
- /* dialogs.cpp */
- /*************************************************************************/
- /* This file is part of: */
- /* GODOT ENGINE */
- /* http://www.godotengine.org */
- /*************************************************************************/
- /* Copyright (c) 2007-2016 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 "dialogs.h"
- #include "print_string.h"
- #include "line_edit.h"
- #include "translation.h"
- void WindowDialog::_post_popup() {
- dragging=false; //just in case
- }
- 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;
- return r.has_point(p_point);
- }
- 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.mouse_button.pressed && p_event.mouse_button.y < 0)
- dragging=true;
- else if (dragging && !p_event.mouse_button.pressed)
- 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 pos = get_pos();
- pos+=rel;
- if (pos.y<0)
- pos.y=0;
- set_pos(pos);
- }
- }
- void WindowDialog::_notification(int 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;
- //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);
- } 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") ));
- } break;
- }
- }
- void WindowDialog::_closed() {
- _close_pressed();
- hide();
- }
- void WindowDialog::set_title(const String& 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;
- 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);
- 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 );
- add_child(close_button);
- close_button->connect("pressed",this,"_closed");
- }
- WindowDialog::~WindowDialog(){
- }
- void PopupDialog::_notification(int p_what) {
- if (p_what==NOTIFICATION_DRAW) {
- RID ci = get_canvas_item();
- 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) {
- cancel_pressed();
- } if (p_what==NOTIFICATION_RESIZED) {
- _update_child_rect();
- }
- }
- void AcceptDialog::_builtin_text_entered(const String& p_text) {
- _ok_pressed();
- }
- void AcceptDialog::_ok_pressed() {
- if (hide_on_ok)
- hide();
- ok_pressed();
- emit_signal("confirmed");
- }
- void AcceptDialog::_close_pressed() {
- cancel_pressed();
- }
- String AcceptDialog::get_text() const {
- return label->get_text();
- }
- void AcceptDialog::set_text(String p_text) {
- label->set_text(p_text);
- }
- void AcceptDialog::set_hide_on_ok(bool p_hide) {
- hide_on_ok=p_hide;
- }
- 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");
- }
- void AcceptDialog::_update_child_rect() {
- int margin = get_constant("margin","Dialogs");
- Size2 size = get_size();
- Size2 hminsize = hbc->get_combined_minimum_size();
- Vector2 cpos(margin,margin);
- Vector2 csize(size.x-margin*2,size.y-margin*3-hminsize.y);
- label->set_pos(cpos);
- label->set_size(csize);
- if (child) {
- child->set_pos(cpos);
- child->set_size(csize);
- }
- 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");
- 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);
- }
- 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
- Size2 wmsize = WindowDialog::get_minimum_size();
- 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);
- //p_child->set_area_as_parent_rect(get_constant("margin","Dialogs"));
- child=p_child;
- minimum_size_changed();
- _update_child_rect();
- }
- void AcceptDialog::remove_child_notify(Node *p_child) {
- if (p_child==child) {
- child=NULL;
- }
- }
- void AcceptDialog::_custom_action(const String& 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 *button = memnew( Button );
- button->set_text(p_text);
- if (p_right) {
- hbc->add_child(button);
- hbc->add_spacer();
- } else {
- hbc->add_child(button);
- hbc->move_child(button,0);
- hbc->add_spacer(true);
- }
- if (p_action!="") {
- button->connect("pressed",this,"_custom_action",varray(p_action));
- }
- return button;
- }
- 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");
- 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("set_child_rect","child:Control"),&AcceptDialog::set_child_rect);
- 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;
- void AcceptDialog::set_swap_ok_cancel(bool p_swap) {
- swap_ok_cancel=p_swap;
- }
- AcceptDialog::AcceptDialog() {
- 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->set_autowrap(true);
- add_child(label);
- hbc = memnew( HBoxContainer );
- add_child(hbc);
- hbc->add_spacer();
- ok = memnew( Button );
- ok->set_text(RTR("OK"));
- hbc->add_child(ok);
- hbc->add_spacer();
- ok->connect("pressed", this,"_ok");
- set_as_toplevel(true);
- hide_on_ok=true;
- set_title(RTR("Alert!"));
- child=NULL;
- }
- AcceptDialog::~AcceptDialog()
- {
- }
- void ConfirmationDialog::_bind_methods() {
- ObjectTypeDB::bind_method(_MD("get_cancel:Button"),&ConfirmationDialog::get_cancel);
- }
- Button *ConfirmationDialog::get_cancel() {
- return cancel;
- }
- ConfirmationDialog::ConfirmationDialog() {
- set_title(RTR("Please Confirm..."));
- cancel = add_cancel();
- }
|