base_button.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. /*************************************************************************/
  2. /* base_button.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 "base_button.h"
  30. #include "os/keyboard.h"
  31. #include "print_string.h"
  32. #include "button_group.h"
  33. void BaseButton::_input_event(InputEvent p_event) {
  34. if (status.disabled) // no interaction with disabled button
  35. return;
  36. switch(p_event.type) {
  37. case InputEvent::MOUSE_BUTTON: {
  38. const InputEventMouseButton &b=p_event.mouse_button;
  39. if ( status.disabled || b.button_index!=1 )
  40. return;
  41. if (status.pressing_button)
  42. break;
  43. if (status.click_on_press) {
  44. if (b.pressed) {
  45. if (!toggle_mode) { //mouse press attempt
  46. pressed();
  47. emit_signal("pressed");
  48. } else {
  49. status.pressed=!status.pressed;
  50. pressed();
  51. emit_signal("pressed");
  52. toggled(status.pressed);
  53. emit_signal("toggled",status.pressed);
  54. }
  55. }
  56. break;
  57. }
  58. if (b.pressed) {
  59. status.press_attempt=true;
  60. status.pressing_inside=true;
  61. } else {
  62. if (status.press_attempt &&status.pressing_inside) {
  63. if (!toggle_mode) { //mouse press attempt
  64. pressed();
  65. emit_signal("pressed");
  66. } else {
  67. status.pressed=!status.pressed;
  68. pressed();
  69. emit_signal("pressed");
  70. toggled(status.pressed);
  71. emit_signal("toggled",status.pressed);
  72. }
  73. }
  74. status.press_attempt=false;
  75. }
  76. update();
  77. } break;
  78. case InputEvent::MOUSE_MOTION: {
  79. if (status.press_attempt && status.pressing_button==0) {
  80. bool last_press_inside=status.pressing_inside;
  81. status.pressing_inside=has_point(Point2(p_event.mouse_motion.x,p_event.mouse_motion.y));
  82. if (last_press_inside!=status.pressing_inside)
  83. update();
  84. }
  85. } break;
  86. case InputEvent::JOYSTICK_BUTTON:
  87. case InputEvent::KEY: {
  88. if (p_event.is_echo()) {
  89. break;
  90. }
  91. if (status.disabled) {
  92. break;
  93. }
  94. if (status.press_attempt && status.pressing_button==0) {
  95. break;
  96. }
  97. if (p_event.is_action("ui_accept")) {
  98. if (p_event.is_pressed()) {
  99. status.pressing_button++;
  100. status.press_attempt=true;
  101. status.pressing_inside=true;
  102. } else if (status.press_attempt) {
  103. if (status.pressing_button)
  104. status.pressing_button--;
  105. if (status.pressing_button)
  106. break;
  107. status.press_attempt=false;
  108. status.pressing_inside=false;
  109. if (!toggle_mode) { //mouse press attempt
  110. pressed();
  111. emit_signal("pressed");
  112. } else {
  113. status.pressed=!status.pressed;
  114. pressed();
  115. emit_signal("pressed");
  116. toggled(status.pressed);
  117. emit_signal("toggled",status.pressed);
  118. }
  119. }
  120. accept_event();
  121. update();
  122. }
  123. }
  124. }
  125. }
  126. void BaseButton::_notification(int p_what) {
  127. if (p_what==NOTIFICATION_MOUSE_ENTER) {
  128. status.hovering=true;
  129. update();
  130. }
  131. if (p_what==NOTIFICATION_MOUSE_EXIT) {
  132. status.hovering=false;
  133. update();
  134. }
  135. if (p_what==NOTIFICATION_FOCUS_EXIT) {
  136. if (status.pressing_button && status.press_attempt) {
  137. status.press_attempt=false;
  138. status.pressing_button=0;
  139. }
  140. }
  141. if (p_what==NOTIFICATION_ENTER_SCENE) {
  142. CanvasItem *ci=this;
  143. while(ci) {
  144. ButtonGroup *bg = ci->cast_to<ButtonGroup>();
  145. if (bg) {
  146. group=bg;
  147. group->_add_button(this);
  148. }
  149. ci=ci->get_parent_item();
  150. }
  151. }
  152. if (p_what==NOTIFICATION_EXIT_SCENE) {
  153. if (group)
  154. group->_remove_button(this);
  155. }
  156. }
  157. void BaseButton::pressed() {
  158. if (get_script_instance())
  159. get_script_instance()->call("pressed");
  160. }
  161. void BaseButton::toggled(bool p_pressed) {
  162. if (get_script_instance())
  163. get_script_instance()->call("toggled",p_pressed);
  164. }
  165. void BaseButton::set_disabled(bool p_disabled) {
  166. status.disabled = p_disabled;
  167. update();
  168. _change_notify("disabled");
  169. if (p_disabled)
  170. set_focus_mode(FOCUS_NONE);
  171. else
  172. set_focus_mode(FOCUS_ALL);
  173. };
  174. bool BaseButton::is_disabled() const {
  175. return status.disabled;
  176. };
  177. void BaseButton::set_pressed(bool p_pressed) {
  178. if (!toggle_mode)
  179. return;
  180. if (status.pressed==p_pressed)
  181. return;
  182. _change_notify("pressed");
  183. status.pressed=p_pressed;
  184. update();
  185. }
  186. bool BaseButton::is_pressing() const{
  187. return status.press_attempt;
  188. }
  189. bool BaseButton::is_pressed() const {
  190. return toggle_mode?status.pressed:status.press_attempt;
  191. }
  192. bool BaseButton::is_hovered() const {
  193. return status.hovering;
  194. }
  195. BaseButton::DrawMode BaseButton::get_draw_mode() const {
  196. if (status.disabled) {
  197. return DRAW_DISABLED;
  198. };
  199. //print_line("press attempt: "+itos(status.press_attempt)+" hover: "+itos(status.hovering)+" pressed: "+itos(status.pressed));
  200. if (status.press_attempt==false && status.hovering && !status.pressed) {
  201. return DRAW_HOVER;
  202. } else {
  203. /* determine if pressed or not */
  204. bool pressing;
  205. if (status.press_attempt) {
  206. pressing=status.pressing_inside;
  207. if (status.pressed)
  208. pressing=!pressing;
  209. } else {
  210. pressing=status.pressed;
  211. }
  212. if (pressing)
  213. return DRAW_PRESSED;
  214. else
  215. return DRAW_NORMAL;
  216. }
  217. return DRAW_NORMAL;
  218. }
  219. void BaseButton::set_toggle_mode(bool p_on) {
  220. toggle_mode=p_on;
  221. }
  222. bool BaseButton::is_toggle_mode() const {
  223. return toggle_mode;
  224. }
  225. void BaseButton::set_click_on_press(bool p_click_on_press) {
  226. status.click_on_press=p_click_on_press;
  227. }
  228. bool BaseButton::get_click_on_press() const {
  229. return status.click_on_press;
  230. }
  231. void BaseButton::_bind_methods() {
  232. ObjectTypeDB::bind_method(_MD("_input_event"),&BaseButton::_input_event);
  233. ObjectTypeDB::bind_method(_MD("set_pressed","pressed"),&BaseButton::set_pressed);
  234. ObjectTypeDB::bind_method(_MD("is_pressed"),&BaseButton::is_pressed);
  235. ObjectTypeDB::bind_method(_MD("is_hovered"),&BaseButton::is_hovered);
  236. ObjectTypeDB::bind_method(_MD("set_toggle_mode","enabled"),&BaseButton::set_toggle_mode);
  237. ObjectTypeDB::bind_method(_MD("is_toggle_mode"),&BaseButton::is_toggle_mode);
  238. ObjectTypeDB::bind_method(_MD("set_disabled","disabled"),&BaseButton::set_disabled);
  239. ObjectTypeDB::bind_method(_MD("is_disabled"),&BaseButton::is_disabled);
  240. ObjectTypeDB::bind_method(_MD("set_click_on_press","enable"),&BaseButton::set_click_on_press);
  241. ObjectTypeDB::bind_method(_MD("get_click_on_press"),&BaseButton::get_click_on_press);
  242. ADD_SIGNAL( MethodInfo("pressed" ) );
  243. ADD_SIGNAL( MethodInfo("toggled", PropertyInfo( Variant::BOOL,"pressed") ) );
  244. ADD_PROPERTY( PropertyInfo( Variant::BOOL, "disabled"), _SCS("set_disabled"), _SCS("is_disabled"));
  245. ADD_PROPERTY( PropertyInfo( Variant::BOOL, "toggle_mode"), _SCS("set_toggle_mode"), _SCS("is_toggle_mode"));
  246. ADD_PROPERTY( PropertyInfo( Variant::BOOL, "click_on_press"), _SCS("set_click_on_press"), _SCS("get_click_on_press"));
  247. }
  248. BaseButton::BaseButton() {
  249. toggle_mode=false;
  250. status.pressed=false;
  251. status.press_attempt=false;
  252. status.hovering=false;
  253. status.pressing_inside=false;
  254. status.disabled = false;
  255. status.click_on_press=false;
  256. status.pressing_button=0;
  257. set_focus_mode( FOCUS_ALL );
  258. group=NULL;
  259. }
  260. BaseButton::~BaseButton()
  261. {
  262. }