dialogs.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  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. 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. ObjectTypeDB::bind_method( _MD("_input_event"),&WindowDialog::_input_event);
  105. ObjectTypeDB::bind_method( _MD("set_title","title"),&WindowDialog::set_title);
  106. ObjectTypeDB::bind_method( _MD("get_title"),&WindowDialog::get_title);
  107. ObjectTypeDB::bind_method( _MD("_closed"),&WindowDialog::_closed);
  108. ObjectTypeDB::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),_SCS("set_title"),_SCS("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_rect();
  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. }
  160. void AcceptDialog::set_hide_on_ok(bool p_hide) {
  161. hide_on_ok=p_hide;
  162. }
  163. bool AcceptDialog::get_hide_on_ok() const {
  164. return hide_on_ok;
  165. }
  166. void AcceptDialog::register_text_enter(Node *p_line_edit) {
  167. ERR_FAIL_NULL(p_line_edit);
  168. p_line_edit->connect("text_entered", this,"_builtin_text_entered");
  169. }
  170. void AcceptDialog::_update_child_rect() {
  171. int margin = get_constant("margin","Dialogs");
  172. Size2 size = get_size();
  173. Size2 hminsize = hbc->get_combined_minimum_size();
  174. Vector2 cpos(margin,margin);
  175. Vector2 csize(size.x-margin*2,size.y-margin*3-hminsize.y);
  176. label->set_pos(cpos);
  177. label->set_size(csize);
  178. if (child) {
  179. child->set_pos(cpos);
  180. child->set_size(csize);
  181. }
  182. cpos.y+=csize.y+margin;
  183. csize.y=hminsize.y;
  184. hbc->set_pos(cpos);
  185. hbc->set_size(csize);
  186. }
  187. Size2 AcceptDialog::get_minimum_size() const {
  188. int margin = get_constant("margin","Dialogs");
  189. Size2 minsize = label->get_combined_minimum_size();
  190. if (child) {
  191. Size2 cminsize = child->get_combined_minimum_size();
  192. minsize.x=MAX(cminsize.x,minsize.x);
  193. minsize.y=MAX(cminsize.y,minsize.y);
  194. }
  195. Size2 hminsize = hbc->get_combined_minimum_size();
  196. minsize.x = MAX(hminsize.x,minsize.x);
  197. minsize.y+=hminsize.y;
  198. minsize.x+=margin*2;
  199. minsize.y+=margin*3; //one as separation between hbc and child
  200. Size2 wmsize = WindowDialog::get_minimum_size();
  201. minsize.x=MAX(wmsize.x,minsize.x);
  202. return minsize;
  203. }
  204. void AcceptDialog::set_child_rect(Control *p_child) {
  205. ERR_FAIL_COND(p_child->get_parent()!=this);
  206. //p_child->set_area_as_parent_rect(get_constant("margin","Dialogs"));
  207. child=p_child;
  208. minimum_size_changed();
  209. _update_child_rect();
  210. }
  211. void AcceptDialog::remove_child_notify(Node *p_child) {
  212. if (p_child==child) {
  213. child=NULL;
  214. }
  215. }
  216. void AcceptDialog::_custom_action(const String& p_action) {
  217. emit_signal("custom_action",p_action);
  218. custom_action(p_action);
  219. }
  220. Button* AcceptDialog::add_button(const String& p_text,bool p_right,const String& p_action) {
  221. Button *button = memnew( Button );
  222. button->set_text(p_text);
  223. if (p_right) {
  224. hbc->add_child(button);
  225. hbc->add_spacer();
  226. } else {
  227. hbc->add_child(button);
  228. hbc->move_child(button,0);
  229. hbc->add_spacer(true);
  230. }
  231. if (p_action!="") {
  232. button->connect("pressed",this,"_custom_action",varray(p_action));
  233. }
  234. return button;
  235. }
  236. Button* AcceptDialog::add_cancel(const String &p_cancel) {
  237. String c = p_cancel;
  238. if (p_cancel=="")
  239. c=RTR("Cancel");
  240. Button *b = swap_ok_cancel ? add_button(c,true) : add_button(c);
  241. b->connect("pressed",this,"_closed");
  242. return b;
  243. }
  244. void AcceptDialog::_bind_methods() {
  245. ObjectTypeDB::bind_method(_MD("_ok"),&AcceptDialog::_ok_pressed);
  246. ObjectTypeDB::bind_method(_MD("get_ok"),&AcceptDialog::get_ok);
  247. ObjectTypeDB::bind_method(_MD("get_label"),&AcceptDialog::get_label);
  248. ObjectTypeDB::bind_method(_MD("set_hide_on_ok","enabled"),&AcceptDialog::set_hide_on_ok);
  249. ObjectTypeDB::bind_method(_MD("get_hide_on_ok"),&AcceptDialog::get_hide_on_ok);
  250. ObjectTypeDB::bind_method(_MD("add_button:Button","text","right","action"),&AcceptDialog::add_button,DEFVAL(false),DEFVAL(""));
  251. ObjectTypeDB::bind_method(_MD("add_cancel:Button","name"),&AcceptDialog::add_cancel);
  252. ObjectTypeDB::bind_method(_MD("_builtin_text_entered"),&AcceptDialog::_builtin_text_entered);
  253. ObjectTypeDB::bind_method(_MD("register_text_enter:LineEdit","line_edit"),&AcceptDialog::register_text_enter);
  254. ObjectTypeDB::bind_method(_MD("_custom_action"),&AcceptDialog::_custom_action);
  255. ObjectTypeDB::bind_method(_MD("set_text","text"),&AcceptDialog::set_text);
  256. ObjectTypeDB::bind_method(_MD("get_text"),&AcceptDialog::get_text);
  257. ObjectTypeDB::bind_method(_MD("set_child_rect","child:Control"),&AcceptDialog::set_child_rect);
  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. add_child(hbc);
  279. hbc->add_spacer();
  280. ok = memnew( Button );
  281. ok->set_text(RTR("OK"));
  282. hbc->add_child(ok);
  283. hbc->add_spacer();
  284. ok->connect("pressed", this,"_ok");
  285. set_as_toplevel(true);
  286. hide_on_ok=true;
  287. set_title(RTR("Alert!"));
  288. child=NULL;
  289. }
  290. AcceptDialog::~AcceptDialog()
  291. {
  292. }
  293. void ConfirmationDialog::_bind_methods() {
  294. ObjectTypeDB::bind_method(_MD("get_cancel:Button"),&ConfirmationDialog::get_cancel);
  295. }
  296. Button *ConfirmationDialog::get_cancel() {
  297. return cancel;
  298. }
  299. ConfirmationDialog::ConfirmationDialog() {
  300. set_title(RTR("Please Confirm..."));
  301. cancel = add_cancel();
  302. }