base_button.cpp 10 KB

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