texture_button.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /*************************************************************************/
  2. /* texture_button.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 "texture_button.h"
  30. Size2 TextureButton::get_minimum_size() const {
  31. Size2 rscale;
  32. if (normal.is_null()) {
  33. if (pressed.is_null()) {
  34. if (hover.is_null())
  35. if (click_mask.is_null())
  36. rscale= Size2();
  37. else
  38. rscale= click_mask->get_size();
  39. else
  40. rscale= hover->get_size();
  41. } else
  42. rscale=pressed->get_size();
  43. } else
  44. rscale= normal->get_size();
  45. return rscale*scale.abs();
  46. }
  47. bool TextureButton::has_point(const Point2& p_point) const {
  48. if (scale[0] == 0 || scale[1] == 0) {
  49. return false;
  50. }
  51. Point2 ppos = p_point/scale.abs();
  52. if (click_mask.is_valid()) {
  53. Point2i p =ppos;
  54. if (p.x<0 || p.x>=click_mask->get_size().width || p.y<0 || p.y>=click_mask->get_size().height)
  55. return false;
  56. return click_mask->get_bit(p);
  57. }
  58. return Control::has_point(p_point);
  59. }
  60. void TextureButton::_notification(int p_what) {
  61. switch( p_what ) {
  62. case NOTIFICATION_DRAW: {
  63. RID canvas_item = get_canvas_item();
  64. DrawMode draw_mode = get_draw_mode();
  65. // if (normal.is_null())
  66. // break;
  67. Ref<Texture> texdraw;
  68. switch (draw_mode) {
  69. case DRAW_NORMAL: {
  70. if (normal.is_valid())
  71. texdraw=normal;
  72. } break;
  73. case DRAW_PRESSED: {
  74. if (pressed.is_null()) {
  75. if (hover.is_null()) {
  76. if (normal.is_valid())
  77. texdraw=normal;
  78. } else
  79. texdraw=hover;
  80. } else
  81. texdraw=pressed;
  82. } break;
  83. case DRAW_HOVER: {
  84. if (hover.is_null()) {
  85. if (pressed.is_valid() && is_pressed())
  86. texdraw=pressed;
  87. else if (normal.is_valid())
  88. texdraw=normal;
  89. } else
  90. texdraw=hover;
  91. } break;
  92. case DRAW_DISABLED: {
  93. if (disabled.is_null()) {
  94. if (normal.is_valid())
  95. texdraw=normal;
  96. } else
  97. texdraw=disabled;
  98. } break;
  99. }
  100. if (texdraw.is_valid()) {
  101. Rect2 drect(Point2(),texdraw->get_size()*scale);
  102. draw_texture_rect(texdraw,drect,false,modulate);
  103. }
  104. if (has_focus() && focused.is_valid()) {
  105. Rect2 drect(Point2(),focused->get_size()*scale);
  106. draw_texture_rect(focused,drect,false,modulate);
  107. };
  108. } break;
  109. }
  110. }
  111. void TextureButton::_bind_methods() {
  112. ObjectTypeDB::bind_method(_MD("set_normal_texture","texture:Texture"),&TextureButton::set_normal_texture);
  113. ObjectTypeDB::bind_method(_MD("set_pressed_texture","texture:Texture"),&TextureButton::set_pressed_texture);
  114. ObjectTypeDB::bind_method(_MD("set_hover_texture","texture:Texture"),&TextureButton::set_hover_texture);
  115. ObjectTypeDB::bind_method(_MD("set_disabled_texture","texture:Texture"),&TextureButton::set_disabled_texture);
  116. ObjectTypeDB::bind_method(_MD("set_focused_texture","texture:Texture"),&TextureButton::set_focused_texture);
  117. ObjectTypeDB::bind_method(_MD("set_click_mask","mask:BitMap"),&TextureButton::set_click_mask);
  118. ObjectTypeDB::bind_method(_MD("set_texture_scale","scale"),&TextureButton::set_texture_scale);
  119. ObjectTypeDB::bind_method(_MD("set_modulate","color"),&TextureButton::set_modulate);
  120. ObjectTypeDB::bind_method(_MD("get_normal_texture:Texture"),&TextureButton::get_normal_texture);
  121. ObjectTypeDB::bind_method(_MD("get_pressed_texture:Texture"),&TextureButton::get_pressed_texture);
  122. ObjectTypeDB::bind_method(_MD("get_hover_texture:Texture"),&TextureButton::get_hover_texture);
  123. ObjectTypeDB::bind_method(_MD("get_disabled_texture:Texture"),&TextureButton::get_disabled_texture);
  124. ObjectTypeDB::bind_method(_MD("get_focused_texture:Texture"),&TextureButton::get_focused_texture);
  125. ObjectTypeDB::bind_method(_MD("get_click_mask:BitMap"),&TextureButton::get_click_mask);
  126. ObjectTypeDB::bind_method(_MD("get_texture_scale"),&TextureButton::get_texture_scale);
  127. ObjectTypeDB::bind_method(_MD("get_modulate"),&TextureButton::get_modulate);
  128. ADD_PROPERTYNZ(PropertyInfo(Variant::OBJECT,"textures/normal",PROPERTY_HINT_RESOURCE_TYPE,"Texture"), _SCS("set_normal_texture"), _SCS("get_normal_texture"));
  129. ADD_PROPERTYNZ(PropertyInfo(Variant::OBJECT,"textures/pressed",PROPERTY_HINT_RESOURCE_TYPE,"Texture"), _SCS("set_pressed_texture"), _SCS("get_pressed_texture"));
  130. ADD_PROPERTYNZ(PropertyInfo(Variant::OBJECT,"textures/hover",PROPERTY_HINT_RESOURCE_TYPE,"Texture"), _SCS("set_hover_texture"), _SCS("get_hover_texture"));
  131. ADD_PROPERTYNZ(PropertyInfo(Variant::OBJECT,"textures/disabled",PROPERTY_HINT_RESOURCE_TYPE,"Texture"), _SCS("set_disabled_texture"), _SCS("get_disabled_texture"));
  132. ADD_PROPERTYNZ(PropertyInfo(Variant::OBJECT,"textures/focused",PROPERTY_HINT_RESOURCE_TYPE,"Texture"), _SCS("set_focused_texture"), _SCS("get_focused_texture"));
  133. ADD_PROPERTYNZ(PropertyInfo(Variant::OBJECT,"textures/click_mask",PROPERTY_HINT_RESOURCE_TYPE,"BitMap"), _SCS("set_click_mask"), _SCS("get_click_mask")) ;
  134. ADD_PROPERTYNO(PropertyInfo(Variant::VECTOR2,"params/scale",PROPERTY_HINT_RANGE,"0.01,1024,0.01"), _SCS("set_texture_scale"), _SCS("get_texture_scale"));
  135. ADD_PROPERTYNO(PropertyInfo(Variant::COLOR,"params/modulate"), _SCS("set_modulate"), _SCS("get_modulate"));
  136. }
  137. void TextureButton::set_normal_texture(const Ref<Texture>& p_normal) {
  138. normal=p_normal;
  139. update();
  140. minimum_size_changed();
  141. }
  142. void TextureButton::set_pressed_texture(const Ref<Texture>& p_pressed) {
  143. pressed=p_pressed;
  144. update();
  145. }
  146. void TextureButton::set_hover_texture(const Ref<Texture>& p_hover) {
  147. hover=p_hover;
  148. update();
  149. }
  150. void TextureButton::set_disabled_texture(const Ref<Texture>& p_disabled) {
  151. disabled=p_disabled;
  152. update();
  153. }
  154. void TextureButton::set_click_mask(const Ref<BitMap>& p_click_mask) {
  155. click_mask=p_click_mask;
  156. update();
  157. }
  158. Ref<Texture> TextureButton::get_normal_texture() const {
  159. return normal;
  160. }
  161. Ref<Texture> TextureButton::get_pressed_texture() const {
  162. return pressed;
  163. }
  164. Ref<Texture> TextureButton::get_hover_texture() const {
  165. return hover;
  166. }
  167. Ref<Texture> TextureButton::get_disabled_texture() const {
  168. return disabled;
  169. }
  170. Ref<BitMap> TextureButton::get_click_mask() const {
  171. return click_mask;
  172. }
  173. Ref<Texture> TextureButton::get_focused_texture() const {
  174. return focused;
  175. };
  176. void TextureButton::set_focused_texture(const Ref<Texture>& p_focused) {
  177. focused = p_focused;
  178. };
  179. void TextureButton::set_texture_scale(Size2 p_scale) {
  180. scale=p_scale;
  181. minimum_size_changed();
  182. update();
  183. }
  184. Size2 TextureButton::get_texture_scale() const{
  185. return scale;
  186. }
  187. void TextureButton::set_modulate(const Color& p_modulate) {
  188. modulate=p_modulate;
  189. update();
  190. }
  191. Color TextureButton::get_modulate() const {
  192. return modulate;
  193. }
  194. TextureButton::TextureButton() {
  195. scale=Size2(1.0, 1.0);
  196. modulate=Color(1,1,1);
  197. }