dialogs.cpp 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. /*************************************************************************/
  2. /* dialogs.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2014 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_SCENE: {
  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. String WindowDialog::get_title() const {
  93. return title;
  94. }
  95. TextureButton *WindowDialog::get_close_button() {
  96. return close_button;
  97. }
  98. void WindowDialog::_bind_methods() {
  99. ObjectTypeDB::bind_method( _MD("_input_event"),&WindowDialog::_input_event);
  100. ObjectTypeDB::bind_method( _MD("set_title","title"),&WindowDialog::set_title);
  101. ObjectTypeDB::bind_method( _MD("get_title"),&WindowDialog::get_title);
  102. ObjectTypeDB::bind_method( _MD("_closed"),&WindowDialog::_closed);
  103. ObjectTypeDB::bind_method( _MD("get_close_button:TextureButton"),&WindowDialog::get_close_button);
  104. ADD_PROPERTY( PropertyInfo(Variant::STRING,"window/title",PROPERTY_HINT_NONE,"",PROPERTY_USAGE_DEFAULT_INTL),_SCS("set_title"),_SCS("get_title"));
  105. }
  106. WindowDialog::WindowDialog() {
  107. //title="Hello!";
  108. dragging=false;
  109. close_button = memnew( TextureButton );
  110. add_child(close_button);
  111. close_button->connect("pressed",this,"_closed");
  112. }
  113. WindowDialog::~WindowDialog(){
  114. }
  115. void PopupDialog::_notification(int p_what) {
  116. if (p_what==NOTIFICATION_DRAW) {
  117. RID ci = get_canvas_item();
  118. get_stylebox("panel","PopupMenu")->draw(ci,Rect2(Point2(),get_size()));
  119. }
  120. }
  121. PopupDialog::PopupDialog() {
  122. }
  123. PopupDialog::~PopupDialog() {
  124. }
  125. //
  126. void AcceptDialog::_post_popup() {
  127. WindowDialog::_post_popup();
  128. get_ok()->grab_focus();
  129. }
  130. void AcceptDialog::_notification(int p_what) {
  131. if (p_what==NOTIFICATION_MODAL_CLOSE) {
  132. cancel_pressed();
  133. } if (p_what==NOTIFICATION_DRAW) {
  134. }
  135. }
  136. void AcceptDialog::_builtin_text_entered(const String& p_text) {
  137. _ok_pressed();
  138. }
  139. void AcceptDialog::_ok_pressed() {
  140. if (hide_on_ok)
  141. hide();
  142. ok_pressed();
  143. emit_signal("confirmed");
  144. }
  145. void AcceptDialog::_close_pressed() {
  146. cancel_pressed();
  147. }
  148. String AcceptDialog::get_text() const {
  149. return label->get_text();
  150. }
  151. void AcceptDialog::set_text(String p_text) {
  152. label->set_text(p_text);
  153. }
  154. void AcceptDialog::set_hide_on_ok(bool p_hide) {
  155. hide_on_ok=p_hide;
  156. }
  157. bool AcceptDialog::get_hide_on_ok() const {
  158. return hide_on_ok;
  159. }
  160. void AcceptDialog::register_text_enter(Node *p_line_edit) {
  161. ERR_FAIL_NULL(p_line_edit);
  162. p_line_edit->connect("text_entered", this,"_builtin_text_entered");
  163. }
  164. void AcceptDialog::set_child_rect(Control *p_child) {
  165. ERR_FAIL_COND(p_child->get_parent()!=this);
  166. p_child->set_area_as_parent_rect(get_constant("margin","Dialogs"));
  167. p_child->set_margin(MARGIN_BOTTOM, get_constant("button_margin","Dialogs")+10);
  168. }
  169. void AcceptDialog::_custom_action(const String& p_action) {
  170. emit_signal("custom_action",p_action);
  171. custom_action(p_action);
  172. }
  173. Button* AcceptDialog::add_button(const String& p_text,bool p_right,const String& p_action) {
  174. Button *button = memnew( Button );
  175. button->set_text(p_text);
  176. if (p_right) {
  177. hbc->add_child(button);
  178. hbc->add_spacer();
  179. } else {
  180. hbc->add_child(button);
  181. hbc->move_child(button,0);
  182. hbc->add_spacer(true);
  183. }
  184. if (p_action!="") {
  185. button->connect("pressed",this,"_custom_action",make_binds(p_action));
  186. }
  187. return button;
  188. }
  189. Button* AcceptDialog::add_cancel(const String &p_cancel) {
  190. String c = p_cancel;
  191. if (p_cancel=="")
  192. c="Cancel";
  193. Button *b = swap_ok_cancel ? add_button("Cancel",true) : add_button("Cancel");
  194. b->connect("pressed",this,"_closed");
  195. return b;
  196. }
  197. void AcceptDialog::_bind_methods() {
  198. ObjectTypeDB::bind_method(_MD("_ok"),&AcceptDialog::_ok_pressed);
  199. ObjectTypeDB::bind_method(_MD("get_ok"),&AcceptDialog::get_ok);
  200. ObjectTypeDB::bind_method(_MD("get_label"),&AcceptDialog::get_label);
  201. ObjectTypeDB::bind_method(_MD("set_hide_on_ok","enabled"),&AcceptDialog::set_hide_on_ok);
  202. ObjectTypeDB::bind_method(_MD("get_hide_on_ok"),&AcceptDialog::get_hide_on_ok);
  203. ObjectTypeDB::bind_method(_MD("add_button:Button","text","right","action"),&AcceptDialog::add_cancel,DEFVAL(false),DEFVAL(""));
  204. ObjectTypeDB::bind_method(_MD("add_cancel:Button","name"),&AcceptDialog::add_cancel);
  205. ObjectTypeDB::bind_method(_MD("_builtin_text_entered"),&AcceptDialog::_builtin_text_entered);
  206. ObjectTypeDB::bind_method(_MD("register_text_enter:LineEdit","line_edit"),&AcceptDialog::register_text_enter);
  207. ObjectTypeDB::bind_method(_MD("_custom_action"),&AcceptDialog::_custom_action);
  208. ObjectTypeDB::bind_method(_MD("set_text","text"),&AcceptDialog::set_text);
  209. ObjectTypeDB::bind_method(_MD("get_text"),&AcceptDialog::get_text);
  210. ADD_SIGNAL( MethodInfo("confirmed") );
  211. ADD_SIGNAL( MethodInfo("custom_action",PropertyInfo(Variant::STRING,"action")) );
  212. }
  213. bool AcceptDialog::swap_ok_cancel=false;
  214. void AcceptDialog::set_swap_ok_cancel(bool p_swap) {
  215. swap_ok_cancel=p_swap;
  216. }
  217. AcceptDialog::AcceptDialog() {
  218. int margin = get_constant("margin","Dialogs");
  219. int button_margin = get_constant("button_margin","Dialogs");
  220. label = memnew( Label );
  221. label->set_anchor(MARGIN_RIGHT,ANCHOR_END);
  222. label->set_anchor(MARGIN_BOTTOM,ANCHOR_END);
  223. label->set_begin( Point2( margin, margin) );
  224. label->set_end( Point2( margin, button_margin) );
  225. label->set_autowrap(true);
  226. add_child(label);
  227. hbc = memnew( HBoxContainer );
  228. hbc->set_area_as_parent_rect(margin);
  229. hbc->set_anchor_and_margin(MARGIN_TOP,ANCHOR_END,button_margin);
  230. add_child(hbc);
  231. hbc->add_spacer();
  232. ok = memnew( Button );
  233. ok->set_text("OK");
  234. hbc->add_child(ok);
  235. hbc->add_spacer();
  236. //add_child(ok);
  237. ok->connect("pressed", this,"_ok");
  238. set_as_toplevel(true);
  239. hide_on_ok=true;
  240. set_title("Alert!");
  241. }
  242. AcceptDialog::~AcceptDialog()
  243. {
  244. }
  245. void ConfirmationDialog::_bind_methods() {
  246. ObjectTypeDB::bind_method(_MD("get_cancel:Button"),&ConfirmationDialog::get_cancel);
  247. }
  248. Button *ConfirmationDialog::get_cancel() {
  249. return cancel;
  250. }
  251. ConfirmationDialog::ConfirmationDialog() {
  252. set_title("Please Confirm...");
  253. cancel = add_cancel();
  254. }