dialogs.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. /*************************************************************************/
  2. /* dialogs.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #include "dialogs.h"
  30. #include "print_string.h"
  31. #include "line_edit.h"
  32. #include "translation.h"
  33. void WindowDialog::_post_popup() {
  34. dragging=false; //just in case
  35. }
  36. bool WindowDialog::has_point(const Point2& p_point) const {
  37. int extra = get_constant("titlebar_height","WindowDialog");
  38. Rect2 r( Point2(), get_size() );
  39. r.pos.y-=extra;
  40. r.size.y+=extra;
  41. return r.has_point(p_point);
  42. }
  43. void WindowDialog::_gui_input(const InputEvent& p_event) {
  44. if (p_event.type == InputEvent::MOUSE_BUTTON && p_event.mouse_button.button_index==BUTTON_LEFT) {
  45. if (p_event.mouse_button.pressed && p_event.mouse_button.y < 0)
  46. dragging=true;
  47. else if (dragging && !p_event.mouse_button.pressed)
  48. dragging=false;
  49. }
  50. if (p_event.type == InputEvent::MOUSE_MOTION && dragging) {
  51. Point2 rel( p_event.mouse_motion.relative_x, p_event.mouse_motion.relative_y );
  52. Point2 pos = get_pos();
  53. pos+=rel;
  54. if (pos.y<0)
  55. pos.y=0;
  56. set_pos(pos);
  57. }
  58. }
  59. void WindowDialog::_notification(int p_what) {
  60. switch(p_what) {
  61. case NOTIFICATION_DRAW: {
  62. RID ci = get_canvas_item();
  63. Size2 s = get_size();
  64. Ref<StyleBox> st = get_stylebox("panel","WindowDialog");
  65. st->draw(ci,Rect2(Point2(),s));
  66. int th = get_constant("title_height","WindowDialog");
  67. Color tc = get_color("title_color","WindowDialog");
  68. Ref<Font> font = get_font("title_font","WindowDialog");
  69. int ofs = (s.width-font->get_string_size(title).width)/2;
  70. //int ofs = st->get_margin(MARGIN_LEFT);
  71. draw_string(font,Point2(ofs,-th+font->get_ascent()),title,tc,s.width - st->get_minimum_size().width);
  72. } break;
  73. case NOTIFICATION_THEME_CHANGED:
  74. case NOTIFICATION_ENTER_TREE: {
  75. close_button->set_normal_texture( get_icon("close","WindowDialog"));
  76. close_button->set_pressed_texture( get_icon("close","WindowDialog"));
  77. close_button->set_hover_texture( get_icon("close_hilite","WindowDialog"));
  78. close_button->set_anchor(MARGIN_LEFT,ANCHOR_END);
  79. close_button->set_begin( Point2( get_constant("close_h_ofs","WindowDialog"), -get_constant("close_v_ofs","WindowDialog") ));
  80. } break;
  81. }
  82. }
  83. void WindowDialog::_closed() {
  84. _close_pressed();
  85. hide();
  86. }
  87. void WindowDialog::set_title(const String& p_title) {
  88. title=XL_MESSAGE(p_title);
  89. update();
  90. }
  91. Size2 WindowDialog::get_minimum_size() const {
  92. Ref<Font> font = get_font("title_font","WindowDialog");
  93. int msx=close_button->get_combined_minimum_size().x;
  94. msx+=font->get_string_size(title).x;
  95. return Size2(msx,1);
  96. }
  97. String WindowDialog::get_title() const {
  98. return title;
  99. }
  100. TextureButton *WindowDialog::get_close_button() {
  101. return close_button;
  102. }
  103. void WindowDialog::_bind_methods() {
  104. ClassDB::bind_method( _MD("_gui_input"),&WindowDialog::_gui_input);
  105. ClassDB::bind_method( _MD("set_title","title"),&WindowDialog::set_title);
  106. ClassDB::bind_method( _MD("get_title"),&WindowDialog::get_title);
  107. ClassDB::bind_method( _MD("_closed"),&WindowDialog::_closed);
  108. ClassDB::bind_method( _MD("get_close_button:TextureButton"),&WindowDialog::get_close_button);
  109. ADD_PROPERTY( PropertyInfo(Variant::STRING,"window_title",PROPERTY_HINT_NONE,"",PROPERTY_USAGE_DEFAULT_INTL),"set_title","get_title");
  110. }
  111. WindowDialog::WindowDialog() {
  112. //title="Hello!";
  113. dragging=false;
  114. close_button = memnew( TextureButton );
  115. add_child(close_button);
  116. close_button->connect("pressed",this,"_closed");
  117. }
  118. WindowDialog::~WindowDialog(){
  119. }
  120. void PopupDialog::_notification(int p_what) {
  121. if (p_what==NOTIFICATION_DRAW) {
  122. RID ci = get_canvas_item();
  123. get_stylebox("panel","PopupMenu")->draw(ci,Rect2(Point2(),get_size()));
  124. }
  125. }
  126. PopupDialog::PopupDialog() {
  127. }
  128. PopupDialog::~PopupDialog() {
  129. }
  130. //
  131. void AcceptDialog::_post_popup() {
  132. WindowDialog::_post_popup();
  133. get_ok()->grab_focus();
  134. }
  135. void AcceptDialog::_notification(int p_what) {
  136. if (p_what==NOTIFICATION_MODAL_CLOSE) {
  137. cancel_pressed();
  138. } if (p_what==NOTIFICATION_RESIZED) {
  139. _update_child_rects();
  140. }
  141. }
  142. void AcceptDialog::_builtin_text_entered(const String& p_text) {
  143. _ok_pressed();
  144. }
  145. void AcceptDialog::_ok_pressed() {
  146. if (hide_on_ok)
  147. hide();
  148. ok_pressed();
  149. emit_signal("confirmed");
  150. }
  151. void AcceptDialog::_close_pressed() {
  152. cancel_pressed();
  153. }
  154. String AcceptDialog::get_text() const {
  155. return label->get_text();
  156. }
  157. void AcceptDialog::set_text(String p_text) {
  158. label->set_text(p_text);
  159. minimum_size_changed();
  160. _update_child_rects();
  161. }
  162. void AcceptDialog::set_hide_on_ok(bool p_hide) {
  163. hide_on_ok=p_hide;
  164. }
  165. bool AcceptDialog::get_hide_on_ok() const {
  166. return hide_on_ok;
  167. }
  168. void AcceptDialog::register_text_enter(Node *p_line_edit) {
  169. ERR_FAIL_NULL(p_line_edit);
  170. p_line_edit->connect("text_entered", this,"_builtin_text_entered");
  171. }
  172. void AcceptDialog::_update_child_rects() {
  173. Size2 label_size=label->get_minimum_size();
  174. if (label->get_text().empty()) {
  175. label_size.height = 0;
  176. }
  177. int margin = get_constant("margin","Dialogs");
  178. Size2 size = get_size();
  179. Size2 hminsize = hbc->get_combined_minimum_size();
  180. Vector2 cpos(margin,margin+label_size.height);
  181. Vector2 csize(size.x-margin*2,size.y-margin*3-hminsize.y-label_size.height);
  182. for(int i=0;i<get_child_count();i++) {
  183. Control *c = get_child(i)->cast_to<Control>();
  184. if (!c)
  185. continue;
  186. if (c==hbc || c==label || c==get_close_button())
  187. continue;
  188. c->set_pos(cpos);
  189. c->set_size(csize);
  190. }
  191. cpos.y+=csize.y+margin;
  192. csize.y=hminsize.y;
  193. hbc->set_pos(cpos);
  194. hbc->set_size(csize);
  195. }
  196. Size2 AcceptDialog::get_minimum_size() const {
  197. int margin = get_constant("margin","Dialogs");
  198. Size2 minsize = label->get_combined_minimum_size();
  199. for(int i=0;i<get_child_count();i++) {
  200. Control *c = get_child(i)->cast_to<Control>();
  201. if (!c)
  202. continue;
  203. if (c==hbc || c==label || c==const_cast<AcceptDialog*>(this)->get_close_button())
  204. continue;
  205. Size2 cminsize = c->get_combined_minimum_size();
  206. minsize.x=MAX(cminsize.x,minsize.x);
  207. minsize.y=MAX(cminsize.y,minsize.y);
  208. }
  209. Size2 hminsize = hbc->get_combined_minimum_size();
  210. minsize.x = MAX(hminsize.x,minsize.x);
  211. minsize.y+=hminsize.y;
  212. minsize.x+=margin*2;
  213. minsize.y+=margin*3; //one as separation between hbc and child
  214. Size2 wmsize = WindowDialog::get_minimum_size();
  215. minsize.x=MAX(wmsize.x,minsize.x);
  216. return minsize;
  217. }
  218. void AcceptDialog::_custom_action(const String& p_action) {
  219. emit_signal("custom_action",p_action);
  220. custom_action(p_action);
  221. }
  222. Button* AcceptDialog::add_button(const String& p_text,bool p_right,const String& p_action) {
  223. Button *button = memnew( Button );
  224. button->set_text(p_text);
  225. if (p_right) {
  226. hbc->add_child(button);
  227. hbc->add_spacer();
  228. } else {
  229. hbc->add_child(button);
  230. hbc->move_child(button,0);
  231. hbc->add_spacer(true);
  232. }
  233. if (p_action!="") {
  234. button->connect("pressed",this,"_custom_action",varray(p_action));
  235. }
  236. return button;
  237. }
  238. Button* AcceptDialog::add_cancel(const String &p_cancel) {
  239. String c = p_cancel;
  240. if (p_cancel=="")
  241. c=RTR("Cancel");
  242. Button *b = swap_ok_cancel ? add_button(c,true) : add_button(c);
  243. b->connect("pressed",this,"_closed");
  244. return b;
  245. }
  246. void AcceptDialog::_bind_methods() {
  247. ClassDB::bind_method(_MD("_ok"),&AcceptDialog::_ok_pressed);
  248. ClassDB::bind_method(_MD("get_ok"),&AcceptDialog::get_ok);
  249. ClassDB::bind_method(_MD("get_label"),&AcceptDialog::get_label);
  250. ClassDB::bind_method(_MD("set_hide_on_ok","enabled"),&AcceptDialog::set_hide_on_ok);
  251. ClassDB::bind_method(_MD("get_hide_on_ok"),&AcceptDialog::get_hide_on_ok);
  252. ClassDB::bind_method(_MD("add_button:Button","text","right","action"),&AcceptDialog::add_button,DEFVAL(false),DEFVAL(""));
  253. ClassDB::bind_method(_MD("add_cancel:Button","name"),&AcceptDialog::add_cancel);
  254. ClassDB::bind_method(_MD("_builtin_text_entered"),&AcceptDialog::_builtin_text_entered);
  255. ClassDB::bind_method(_MD("register_text_enter:LineEdit","line_edit"),&AcceptDialog::register_text_enter);
  256. ClassDB::bind_method(_MD("_custom_action"),&AcceptDialog::_custom_action);
  257. ClassDB::bind_method(_MD("set_text","text"),&AcceptDialog::set_text);
  258. ClassDB::bind_method(_MD("get_text"),&AcceptDialog::get_text);
  259. ADD_SIGNAL( MethodInfo("confirmed") );
  260. ADD_SIGNAL( MethodInfo("custom_action",PropertyInfo(Variant::STRING,"action")) );
  261. ADD_GROUP("Dialog","dialog");
  262. ADD_PROPERTYNZ( PropertyInfo(Variant::STRING,"dialog_text",PROPERTY_HINT_MULTILINE_TEXT,"",PROPERTY_USAGE_DEFAULT_INTL),"set_text","get_text");
  263. ADD_PROPERTY( PropertyInfo(Variant::BOOL, "dialog_hide_on_ok"),"set_hide_on_ok","get_hide_on_ok") ;
  264. }
  265. bool AcceptDialog::swap_ok_cancel=false;
  266. void AcceptDialog::set_swap_ok_cancel(bool p_swap) {
  267. swap_ok_cancel=p_swap;
  268. }
  269. AcceptDialog::AcceptDialog() {
  270. int margin = get_constant("margin","Dialogs");
  271. int button_margin = get_constant("button_margin","Dialogs");
  272. label = memnew( Label );
  273. label->set_anchor(MARGIN_RIGHT,ANCHOR_END);
  274. label->set_anchor(MARGIN_BOTTOM,ANCHOR_END);
  275. label->set_begin( Point2( margin, margin) );
  276. label->set_end( Point2( margin, button_margin+10) );
  277. //label->set_autowrap(true);
  278. add_child(label);
  279. hbc = memnew( HBoxContainer );
  280. add_child(hbc);
  281. hbc->add_spacer();
  282. ok = memnew( Button );
  283. ok->set_text(RTR("OK"));
  284. hbc->add_child(ok);
  285. hbc->add_spacer();
  286. ok->connect("pressed", this,"_ok");
  287. set_as_toplevel(true);
  288. hide_on_ok=true;
  289. set_title(RTR("Alert!"));
  290. }
  291. AcceptDialog::~AcceptDialog()
  292. {
  293. }
  294. void ConfirmationDialog::_bind_methods() {
  295. ClassDB::bind_method(_MD("get_cancel:Button"),&ConfirmationDialog::get_cancel);
  296. }
  297. Button *ConfirmationDialog::get_cancel() {
  298. return cancel;
  299. }
  300. ConfirmationDialog::ConfirmationDialog() {
  301. set_title(RTR("Please Confirm..."));
  302. cancel = add_cancel();
  303. }