screen_button.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. /*************************************************************************/
  2. /* screen_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 "screen_button.h"
  30. #include "os/os.h"
  31. #include "input_map.h"
  32. #include "os/input.h"
  33. void TouchScreenButton::set_texture(const Ref<Texture>& p_texture) {
  34. texture=p_texture;
  35. update();
  36. }
  37. Ref<Texture> TouchScreenButton::get_texture() const{
  38. return texture;
  39. }
  40. void TouchScreenButton::set_texture_pressed(const Ref<Texture>& p_texture_pressed) {
  41. texture_pressed=p_texture_pressed;
  42. update();
  43. }
  44. Ref<Texture> TouchScreenButton::get_texture_pressed() const{
  45. return texture_pressed;
  46. }
  47. void TouchScreenButton::set_bitmask(const Ref<BitMap>& p_bitmask){
  48. bitmask=p_bitmask;
  49. }
  50. Ref<BitMap> TouchScreenButton::get_bitmask() const{
  51. return bitmask;
  52. }
  53. void TouchScreenButton::_notification(int p_what) {
  54. switch(p_what) {
  55. case NOTIFICATION_DRAW: {
  56. if (!is_inside_scene())
  57. return;
  58. if (!get_scene()->is_editor_hint() && !OS::get_singleton()->has_touchscreen_ui_hint() && visibility==VISIBILITY_TOUCHSCREEN_ONLY)
  59. return;
  60. if (finger_pressed!=-1) {
  61. if (texture_pressed.is_valid())
  62. draw_texture(texture_pressed,Point2());
  63. else if (texture.is_valid())
  64. draw_texture(texture,Point2());
  65. } else {
  66. if (texture.is_valid())
  67. draw_texture(texture,Point2());
  68. }
  69. } break;
  70. case NOTIFICATION_ENTER_SCENE: {
  71. if (!get_scene()->is_editor_hint() && !OS::get_singleton()->has_touchscreen_ui_hint() && visibility==VISIBILITY_TOUCHSCREEN_ONLY)
  72. return;
  73. update();
  74. set_process_input(true);
  75. if (action.operator String()!="" && InputMap::get_singleton()->has_action(action)) {
  76. action_id=InputMap::get_singleton()->get_action_id(action);
  77. } else {
  78. action_id=-1;
  79. }
  80. } break;
  81. }
  82. }
  83. bool TouchScreenButton::is_pressed() const{
  84. return finger_pressed!=-1;
  85. }
  86. void TouchScreenButton::set_action(const String& p_action) {
  87. action=p_action;
  88. if (action.operator String()!="" && InputMap::get_singleton()->has_action(action)) {
  89. action_id=InputMap::get_singleton()->get_action_id(action);
  90. } else {
  91. action_id=-1;
  92. }
  93. }
  94. String TouchScreenButton::get_action() const {
  95. return action;
  96. }
  97. void TouchScreenButton::_input(const InputEvent& p_event) {
  98. if (!get_scene())
  99. return;
  100. if (p_event.device != 0)
  101. return;
  102. if (passby_press) {
  103. if (p_event.type==InputEvent::SCREEN_TOUCH && !p_event.screen_touch.pressed && finger_pressed==p_event.screen_touch.index) {
  104. emit_signal("released");
  105. if (action_id!=-1) {
  106. Input::get_singleton()->action_release(action);
  107. InputEvent ie;
  108. ie.type=InputEvent::ACTION;
  109. ie.ID=0;
  110. ie.action.action=action_id;
  111. ie.action.pressed=false;
  112. get_scene()->input_event(ie);
  113. }
  114. finger_pressed=-1;
  115. update();
  116. }
  117. if ((p_event.type==InputEvent::SCREEN_TOUCH && p_event.screen_touch.pressed)|| p_event.type==InputEvent::SCREEN_DRAG) {
  118. if (finger_pressed==-1 || p_event.screen_touch.index==finger_pressed) {
  119. Point2 coord = (get_global_transform()).affine_inverse().xform(Point2(p_event.screen_touch.x,p_event.screen_touch.y));
  120. bool touched=false;
  121. if (bitmask.is_valid()) {
  122. if (Rect2(Point2(),bitmask->get_size()).has_point(coord)) {
  123. if (bitmask->get_bit(coord))
  124. touched=true;
  125. }
  126. } else {
  127. touched=Rect2(Point2(),texture->get_size()).has_point(coord);
  128. }
  129. if (touched) {
  130. if (finger_pressed==-1) {
  131. finger_pressed=p_event.screen_touch.index;
  132. //emit change stuff
  133. emit_signal("pressed");
  134. if (action_id!=-1) {
  135. Input::get_singleton()->action_press(action);
  136. InputEvent ie;
  137. ie.type=InputEvent::ACTION;
  138. ie.ID=0;
  139. ie.action.action=action_id;
  140. ie.action.pressed=true;
  141. get_scene()->input_event(ie);
  142. }
  143. update();
  144. }
  145. } else {
  146. if (finger_pressed!=-1) {
  147. emit_signal("released");
  148. if (action_id!=-1) {
  149. Input::get_singleton()->action_release(action);
  150. InputEvent ie;
  151. ie.type=InputEvent::ACTION;
  152. ie.ID=0;
  153. ie.action.action=action_id;
  154. ie.action.pressed=false;
  155. get_scene()->input_event(ie);
  156. }
  157. finger_pressed=-1;
  158. update();
  159. }
  160. }
  161. }
  162. }
  163. } else {
  164. if (p_event.type==InputEvent::SCREEN_TOUCH) {
  165. if (p_event.screen_touch.pressed) {
  166. if (!is_visible())
  167. return;
  168. if (finger_pressed!=-1)
  169. return; //already fingering
  170. Point2 coord = (get_global_transform()).affine_inverse().xform(Point2(p_event.screen_touch.x,p_event.screen_touch.y));
  171. bool touched=false;
  172. if (bitmask.is_valid()) {
  173. if (Rect2(Point2(),bitmask->get_size()).has_point(coord)) {
  174. if (bitmask->get_bit(coord))
  175. touched=true;
  176. }
  177. } else {
  178. if (!texture.is_null())
  179. touched=Rect2(Point2(),texture->get_size()).has_point(coord);
  180. }
  181. if (touched) {
  182. finger_pressed=p_event.screen_touch.index;
  183. //emit change stuff
  184. emit_signal("pressed");
  185. if (action_id!=-1) {
  186. Input::get_singleton()->action_press(action);
  187. InputEvent ie;
  188. ie.type=InputEvent::ACTION;
  189. ie.ID=0;
  190. ie.action.action=action_id;
  191. ie.action.pressed=true;
  192. get_scene()->input_event(ie);
  193. }
  194. update();
  195. }
  196. } else {
  197. if (p_event.screen_touch.index==finger_pressed) {
  198. //untouch
  199. emit_signal("released");
  200. if (action_id!=-1) {
  201. Input::get_singleton()->action_release(action);
  202. InputEvent ie;
  203. ie.type=InputEvent::ACTION;
  204. ie.ID=0;
  205. ie.action.action=action_id;
  206. ie.action.pressed=false;
  207. get_scene()->input_event(ie);
  208. }
  209. finger_pressed=-1;
  210. update();
  211. }
  212. }
  213. }
  214. }
  215. }
  216. Rect2 TouchScreenButton::get_item_rect() const {
  217. if (texture.is_null())
  218. return Rect2(0,0,1,1);
  219. //if (texture.is_null())
  220. // return CanvasItem::get_item_rect();
  221. return Rect2(Size2(),texture->get_size());
  222. }
  223. void TouchScreenButton::set_visibility_mode(VisibilityMode p_mode) {
  224. visibility=p_mode;
  225. update();
  226. }
  227. TouchScreenButton::VisibilityMode TouchScreenButton::get_visibility_mode() const {
  228. return visibility;
  229. }
  230. void TouchScreenButton::set_passby_press(bool p_enable) {
  231. passby_press=p_enable;
  232. }
  233. bool TouchScreenButton::is_passby_press_enabled() const{
  234. return passby_press;
  235. }
  236. void TouchScreenButton::_bind_methods() {
  237. ObjectTypeDB::bind_method(_MD("set_texture","texture"),&TouchScreenButton::set_texture);
  238. ObjectTypeDB::bind_method(_MD("get_texture"),&TouchScreenButton::get_texture);
  239. ObjectTypeDB::bind_method(_MD("set_texture_pressed","texture_pressed"),&TouchScreenButton::set_texture_pressed);
  240. ObjectTypeDB::bind_method(_MD("get_texture_pressed"),&TouchScreenButton::get_texture_pressed);
  241. ObjectTypeDB::bind_method(_MD("set_bitmask","bitmask"),&TouchScreenButton::set_bitmask);
  242. ObjectTypeDB::bind_method(_MD("get_bitmask"),&TouchScreenButton::get_bitmask);
  243. ObjectTypeDB::bind_method(_MD("set_action","action"),&TouchScreenButton::set_action);
  244. ObjectTypeDB::bind_method(_MD("get_action"),&TouchScreenButton::get_action);
  245. ObjectTypeDB::bind_method(_MD("set_visibility_mode","mode"),&TouchScreenButton::set_visibility_mode);
  246. ObjectTypeDB::bind_method(_MD("get_visibility_mode"),&TouchScreenButton::get_visibility_mode);
  247. ObjectTypeDB::bind_method(_MD("set_passby_press","enabled"),&TouchScreenButton::set_passby_press);
  248. ObjectTypeDB::bind_method(_MD("is_passby_press_enabled"),&TouchScreenButton::is_passby_press_enabled);
  249. ObjectTypeDB::bind_method(_MD("is_pressed"),&TouchScreenButton::is_pressed);
  250. ObjectTypeDB::bind_method(_MD("_input"),&TouchScreenButton::_input);
  251. ADD_PROPERTY( PropertyInfo(Variant::OBJECT,"normal",PROPERTY_HINT_RESOURCE_TYPE,"Texture"),_SCS("set_texture"),_SCS("get_texture"));
  252. ADD_PROPERTY( PropertyInfo(Variant::OBJECT,"pressed",PROPERTY_HINT_RESOURCE_TYPE,"Texture"),_SCS("set_texture_pressed"),_SCS("get_texture_pressed"));
  253. ADD_PROPERTY( PropertyInfo(Variant::OBJECT,"bitmask",PROPERTY_HINT_RESOURCE_TYPE,"BitMap"),_SCS("set_bitmask"),_SCS("get_bitmask"));
  254. ADD_PROPERTY( PropertyInfo(Variant::BOOL,"passby_press"),_SCS("set_passby_press"),_SCS("is_passby_press_enabled"));
  255. ADD_PROPERTY( PropertyInfo(Variant::STRING,"action"),_SCS("set_action"),_SCS("get_action"));
  256. ADD_PROPERTY( PropertyInfo(Variant::INT,"visibility_mode",PROPERTY_HINT_ENUM,"Always,TouchScreen Only"),_SCS("set_visibility_mode"),_SCS("get_visibility_mode"));
  257. ADD_SIGNAL( MethodInfo("pressed" ) );
  258. ADD_SIGNAL( MethodInfo("released" ) );
  259. }
  260. TouchScreenButton::TouchScreenButton() {
  261. finger_pressed=-1;
  262. action_id=-1;
  263. passby_press=false;
  264. visibility=VISIBILITY_ALWAYS;
  265. }