godot/scene/gui/dialogs.cpp

441 lines
11 KiB
C++
Raw Normal View History

2014-02-10 01:10:30 +00:00
/*************************************************************************/
/* dialogs.cpp */
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* http://www.godotengine.org */
/*************************************************************************/
2016-01-01 13:50:53 +00:00
/* Copyright (c) 2007-2016 Juan Linietsky, Ariel Manzur. */
2014-02-10 01:10:30 +00:00
/* */
/* 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: {
2014-02-10 01:10:30 +00:00
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);
}
2014-02-10 01:10:30 +00:00
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) {
2016-03-08 23:00:52 +00:00
2014-02-10 01:10:30 +00:00
if (p_what==NOTIFICATION_MODAL_CLOSE) {
cancel_pressed();
} if (p_what==NOTIFICATION_RESIZED) {
2014-02-10 01:10:30 +00:00
_update_child_rect();
2016-03-08 23:00:52 +00:00
}
2014-02-10 01:10:30 +00:00
}
void AcceptDialog::_builtin_text_entered(const String& p_text) {
_ok_pressed();
}
void AcceptDialog::_ok_pressed() {
2016-03-08 23:00:52 +00:00
2014-02-10 01:10:30 +00:00
if (hide_on_ok)
hide();
ok_pressed();
emit_signal("confirmed");
}
void AcceptDialog::_close_pressed() {
2016-03-08 23:00:52 +00:00
2014-02-10 01:10:30 +00:00
cancel_pressed();
}
String AcceptDialog::get_text() const {
return label->get_text();
}
void AcceptDialog::set_text(String p_text) {
2016-03-08 23:00:52 +00:00
2014-02-10 01:10:30 +00:00
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;
}
2014-02-10 01:10:30 +00:00
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;
}
2014-02-10 01:10:30 +00:00
}
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));
2014-02-10 01:10:30 +00:00
}
return button;
}
Button* AcceptDialog::add_cancel(const String &p_cancel) {
String c = p_cancel;
if (p_cancel=="")
2016-05-30 09:01:56 +00:00
c=RTR("Cancel");
Button *b = swap_ok_cancel ? add_button(c,true) : add_button(c);
2014-02-10 01:10:30 +00:00
b->connect("pressed",this,"_closed");
return b;
}
void AcceptDialog::_bind_methods() {
2016-03-08 23:00:52 +00:00
2014-02-10 01:10:30 +00:00
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);
2014-10-26 14:07:54 +00:00
ObjectTypeDB::bind_method(_MD("add_button:Button","text","right","action"),&AcceptDialog::add_button,DEFVAL(false),DEFVAL(""));
2014-02-10 01:10:30 +00:00
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")) );
2015-11-20 21:25:01 +00:00
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") );
2014-02-10 01:10:30 +00:00
}
bool AcceptDialog::swap_ok_cancel=false;
void AcceptDialog::set_swap_ok_cancel(bool p_swap) {
swap_ok_cancel=p_swap;
}
AcceptDialog::AcceptDialog() {
2016-03-08 23:00:52 +00:00
2014-02-10 01:10:30 +00:00
int margin = get_constant("margin","Dialogs");
int button_margin = get_constant("button_margin","Dialogs");
2016-03-08 23:00:52 +00:00
2014-02-10 01:10:30 +00:00
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);
2014-02-10 01:10:30 +00:00
add_child(label);
hbc = memnew( HBoxContainer );
add_child(hbc);
hbc->add_spacer();
ok = memnew( Button );
2016-05-30 09:01:56 +00:00
ok->set_text(RTR("OK"));
2014-02-10 01:10:30 +00:00
hbc->add_child(ok);
hbc->add_spacer();
2016-03-08 23:00:52 +00:00
2014-02-10 01:10:30 +00:00
ok->connect("pressed", this,"_ok");
set_as_toplevel(true);
hide_on_ok=true;
2016-05-30 09:01:56 +00:00
set_title(RTR("Alert!"));
child=NULL;
2014-02-10 01:10:30 +00:00
}
AcceptDialog::~AcceptDialog()
{
}
void ConfirmationDialog::_bind_methods() {
ObjectTypeDB::bind_method(_MD("get_cancel:Button"),&ConfirmationDialog::get_cancel);
}
Button *ConfirmationDialog::get_cancel() {
return cancel;
}
ConfirmationDialog::ConfirmationDialog() {
2016-05-30 09:01:56 +00:00
set_title(RTR("Please Confirm..."));
2014-02-10 01:10:30 +00:00
cancel = add_cancel();
}