dialogs.cpp 12 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-2016 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::_input_event(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. Size2 size = get_size();
  54. pos+=rel;
  55. if (pos.y<0)
  56. pos.y=0;
  57. set_pos(pos);
  58. }
  59. }
  60. void WindowDialog::_notification(int p_what) {
  61. switch(p_what) {
  62. case NOTIFICATION_DRAW: {
  63. RID ci = get_canvas_item();
  64. Size2 s = get_size();
  65. Ref<StyleBox> st = get_stylebox("panel","WindowDialog");
  66. st->draw(ci,Rect2(Point2(),s));
  67. int th = get_constant("title_height","WindowDialog");
  68. Color tc = get_color("title_color","WindowDialog");
  69. Ref<Font> font = get_font("title_font","WindowDialog");
  70. int ofs = (s.width-font->get_string_size(title).width)/2;
  71. //int ofs = st->get_margin(MARGIN_LEFT);
  72. draw_string(font,Point2(ofs,-th+font->get_ascent()),title,tc,s.width - st->get_minimum_size().width);
  73. } break;
  74. case NOTIFICATION_THEME_CHANGED:
  75. case NOTIFICATION_ENTER_TREE: {
  76. close_button->set_normal_texture( get_icon("close","WindowDialog"));
  77. close_button->set_pressed_texture( get_icon("close","WindowDialog"));
  78. close_button->set_hover_texture( get_icon("close_hilite","WindowDialog"));
  79. close_button->set_anchor(MARGIN_LEFT,ANCHOR_END);
  80. close_button->set_begin( Point2( get_constant("close_h_ofs","WindowDialog"), -get_constant("close_v_ofs","WindowDialog") ));
  81. } break;
  82. }
  83. }
  84. void WindowDialog::_closed() {
  85. _close_pressed();
  86. hide();
  87. }
  88. void WindowDialog::set_title(const String& p_title) {
  89. title=XL_MESSAGE(p_title);
  90. update();
  91. }
  92. Size2 WindowDialog::get_minimum_size() const {
  93. Ref<Font> font = get_font("title_font","WindowDialog");
  94. int msx=close_button->get_combined_minimum_size().x;
  95. msx+=font->get_string_size(title).x;
  96. return Size2(msx,1);
  97. }
  98. String WindowDialog::get_title() const {
  99. return title;
  100. }
  101. TextureButton *WindowDialog::get_close_button() {
  102. return close_button;
  103. }
  104. void WindowDialog::_bind_methods() {
  105. ObjectTypeDB::bind_method( _MD("_input_event"),&WindowDialog::_input_event);
  106. ObjectTypeDB::bind_method( _MD("set_title","title"),&WindowDialog::set_title);
  107. ObjectTypeDB::bind_method( _MD("get_title"),&WindowDialog::get_title);
  108. ObjectTypeDB::bind_method( _MD("_closed"),&WindowDialog::_closed);
  109. ObjectTypeDB::bind_method( _MD("get_close_button:TextureButton"),&WindowDialog::get_close_button);
  110. ADD_PROPERTY( PropertyInfo(Variant::STRING,"window/title",PROPERTY_HINT_NONE,"",PROPERTY_USAGE_DEFAULT_INTL),_SCS("set_title"),_SCS("get_title"));
  111. }
  112. WindowDialog::WindowDialog() {
  113. //title="Hello!";
  114. dragging=false;
  115. close_button = memnew( TextureButton );
  116. add_child(close_button);
  117. close_button->connect("pressed",this,"_closed");
  118. }
  119. WindowDialog::~WindowDialog(){
  120. }
  121. void PopupDialog::_notification(int p_what) {
  122. if (p_what==NOTIFICATION_DRAW) {
  123. RID ci = get_canvas_item();
  124. get_stylebox("panel","PopupMenu")->draw(ci,Rect2(Point2(),get_size()));
  125. }
  126. }
  127. PopupDialog::PopupDialog() {
  128. }
  129. PopupDialog::~PopupDialog() {
  130. }
  131. //
  132. void AcceptDialog::_post_popup() {
  133. WindowDialog::_post_popup();
  134. get_ok()->grab_focus();
  135. }
  136. void AcceptDialog::_notification(int p_what) {
  137. if (p_what==NOTIFICATION_MODAL_CLOSE) {
  138. cancel_pressed();
  139. } if (p_what==NOTIFICATION_RESIZED) {
  140. _update_child_rect();
  141. }
  142. }
  143. void AcceptDialog::_builtin_text_entered(const String& p_text) {
  144. _ok_pressed();
  145. }
  146. void AcceptDialog::_ok_pressed() {
  147. if (hide_on_ok)
  148. hide();
  149. ok_pressed();
  150. emit_signal("confirmed");
  151. }
  152. void AcceptDialog::_close_pressed() {
  153. cancel_pressed();
  154. }
  155. String AcceptDialog::get_text() const {
  156. return label->get_text();
  157. }
  158. void AcceptDialog::set_text(String p_text) {
  159. label->set_text(p_text);
  160. }
  161. void AcceptDialog::set_hide_on_ok(bool p_hide) {
  162. hide_on_ok=p_hide;
  163. }
  164. bool AcceptDialog::get_hide_on_ok() const {
  165. return hide_on_ok;
  166. }
  167. void AcceptDialog::register_text_enter(Node *p_line_edit) {
  168. ERR_FAIL_NULL(p_line_edit);
  169. p_line_edit->connect("text_entered", this,"_builtin_text_entered");
  170. }
  171. void AcceptDialog::_update_child_rect() {
  172. int margin = get_constant("margin","Dialogs");
  173. Size2 size = get_size();
  174. Size2 hminsize = hbc->get_combined_minimum_size();
  175. Vector2 cpos(margin,margin);
  176. Vector2 csize(size.x-margin*2,size.y-margin*3-hminsize.y);
  177. label->set_pos(cpos);
  178. label->set_size(csize);
  179. if (child) {
  180. child->set_pos(cpos);
  181. child->set_size(csize);
  182. }
  183. cpos.y+=csize.y+margin;
  184. csize.y=hminsize.y;
  185. hbc->set_pos(cpos);
  186. hbc->set_size(csize);
  187. }
  188. Size2 AcceptDialog::get_minimum_size() const {
  189. int margin = get_constant("margin","Dialogs");
  190. Size2 minsize = label->get_combined_minimum_size();
  191. if (child) {
  192. Size2 cminsize = child->get_combined_minimum_size();
  193. minsize.x=MAX(cminsize.x,minsize.x);
  194. minsize.y=MAX(cminsize.y,minsize.y);
  195. }
  196. Size2 hminsize = hbc->get_combined_minimum_size();
  197. minsize.x = MAX(hminsize.x,minsize.x);
  198. minsize.y+=hminsize.y;
  199. minsize.x+=margin*2;
  200. minsize.y+=margin*3; //one as separation between hbc and child
  201. Size2 wmsize = WindowDialog::get_minimum_size();
  202. minsize.x=MAX(wmsize.x,minsize.x);
  203. return minsize;
  204. }
  205. void AcceptDialog::set_child_rect(Control *p_child) {
  206. ERR_FAIL_COND(p_child->get_parent()!=this);
  207. //p_child->set_area_as_parent_rect(get_constant("margin","Dialogs"));
  208. child=p_child;
  209. minimum_size_changed();
  210. _update_child_rect();
  211. }
  212. void AcceptDialog::remove_child_notify(Node *p_child) {
  213. if (p_child==child) {
  214. child=NULL;
  215. }
  216. }
  217. void AcceptDialog::_custom_action(const String& p_action) {
  218. emit_signal("custom_action",p_action);
  219. custom_action(p_action);
  220. }
  221. Button* AcceptDialog::add_button(const String& p_text,bool p_right,const String& p_action) {
  222. Button *button = memnew( Button );
  223. button->set_text(p_text);
  224. if (p_right) {
  225. hbc->add_child(button);
  226. hbc->add_spacer();
  227. } else {
  228. hbc->add_child(button);
  229. hbc->move_child(button,0);
  230. hbc->add_spacer(true);
  231. }
  232. if (p_action!="") {
  233. button->connect("pressed",this,"_custom_action",varray(p_action));
  234. }
  235. return button;
  236. }
  237. Button* AcceptDialog::add_cancel(const String &p_cancel) {
  238. String c = p_cancel;
  239. if (p_cancel=="")
  240. c=RTR("Cancel");
  241. Button *b = swap_ok_cancel ? add_button(c,true) : add_button(c);
  242. b->connect("pressed",this,"_closed");
  243. return b;
  244. }
  245. void AcceptDialog::_bind_methods() {
  246. ObjectTypeDB::bind_method(_MD("_ok"),&AcceptDialog::_ok_pressed);
  247. ObjectTypeDB::bind_method(_MD("get_ok"),&AcceptDialog::get_ok);
  248. ObjectTypeDB::bind_method(_MD("get_label"),&AcceptDialog::get_label);
  249. ObjectTypeDB::bind_method(_MD("set_hide_on_ok","enabled"),&AcceptDialog::set_hide_on_ok);
  250. ObjectTypeDB::bind_method(_MD("get_hide_on_ok"),&AcceptDialog::get_hide_on_ok);
  251. ObjectTypeDB::bind_method(_MD("add_button:Button","text","right","action"),&AcceptDialog::add_button,DEFVAL(false),DEFVAL(""));
  252. ObjectTypeDB::bind_method(_MD("add_cancel:Button","name"),&AcceptDialog::add_cancel);
  253. ObjectTypeDB::bind_method(_MD("_builtin_text_entered"),&AcceptDialog::_builtin_text_entered);
  254. ObjectTypeDB::bind_method(_MD("register_text_enter:LineEdit","line_edit"),&AcceptDialog::register_text_enter);
  255. ObjectTypeDB::bind_method(_MD("_custom_action"),&AcceptDialog::_custom_action);
  256. ObjectTypeDB::bind_method(_MD("set_text","text"),&AcceptDialog::set_text);
  257. ObjectTypeDB::bind_method(_MD("get_text"),&AcceptDialog::get_text);
  258. ADD_SIGNAL( MethodInfo("confirmed") );
  259. ADD_SIGNAL( MethodInfo("custom_action",PropertyInfo(Variant::STRING,"action")) );
  260. ADD_PROPERTYNZ( PropertyInfo(Variant::STRING,"dialog/text",PROPERTY_HINT_MULTILINE_TEXT,"",PROPERTY_USAGE_DEFAULT_INTL),_SCS("set_text"),_SCS("get_text"));
  261. ADD_PROPERTY( PropertyInfo(Variant::BOOL, "dialog/hide_on_ok"),_SCS("set_hide_on_ok"),_SCS("get_hide_on_ok") );
  262. }
  263. bool AcceptDialog::swap_ok_cancel=false;
  264. void AcceptDialog::set_swap_ok_cancel(bool p_swap) {
  265. swap_ok_cancel=p_swap;
  266. }
  267. AcceptDialog::AcceptDialog() {
  268. int margin = get_constant("margin","Dialogs");
  269. int button_margin = get_constant("button_margin","Dialogs");
  270. label = memnew( Label );
  271. label->set_anchor(MARGIN_RIGHT,ANCHOR_END);
  272. label->set_anchor(MARGIN_BOTTOM,ANCHOR_END);
  273. label->set_begin( Point2( margin, margin) );
  274. label->set_end( Point2( margin, button_margin+10) );
  275. //label->set_autowrap(true);
  276. add_child(label);
  277. hbc = memnew( HBoxContainer );
  278. hbc->set_area_as_parent_rect(margin);
  279. hbc->set_anchor_and_margin(MARGIN_TOP,ANCHOR_END,button_margin);
  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. //add_child(ok);
  287. ok->connect("pressed", this,"_ok");
  288. set_as_toplevel(true);
  289. hide_on_ok=true;
  290. set_title(RTR("Alert!"));
  291. child=NULL;
  292. }
  293. AcceptDialog::~AcceptDialog()
  294. {
  295. }
  296. void ConfirmationDialog::_bind_methods() {
  297. ObjectTypeDB::bind_method(_MD("get_cancel:Button"),&ConfirmationDialog::get_cancel);
  298. }
  299. Button *ConfirmationDialog::get_cancel() {
  300. return cancel;
  301. }
  302. ConfirmationDialog::ConfirmationDialog() {
  303. set_title(RTR("Please Confirm..."));
  304. cancel = add_cancel();
  305. }