texture_button.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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-2017 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 = Control::get_minimum_size();
  32. if (!expand) {
  33. if (normal.is_null()) {
  34. if (pressed.is_null()) {
  35. if (hover.is_null())
  36. if (click_mask.is_null())
  37. rscale= Size2();
  38. else
  39. rscale= click_mask->get_size();
  40. else
  41. rscale= hover->get_size();
  42. } else
  43. rscale=pressed->get_size();
  44. } else
  45. rscale= normal->get_size();
  46. }
  47. return rscale.abs();
  48. }
  49. bool TextureButton::has_point(const Point2& p_point) const {
  50. if (click_mask.is_valid()) {
  51. Point2i p =p_point;
  52. if (p.x<0 || p.x>=click_mask->get_size().width || p.y<0 || p.y>=click_mask->get_size().height)
  53. return false;
  54. return click_mask->get_bit(p);
  55. }
  56. return Control::has_point(p_point);
  57. }
  58. void TextureButton::_notification(int p_what) {
  59. switch( p_what ) {
  60. case NOTIFICATION_DRAW: {
  61. DrawMode draw_mode = get_draw_mode();
  62. Ref<Texture> texdraw;
  63. switch (draw_mode) {
  64. case DRAW_NORMAL: {
  65. if (normal.is_valid())
  66. texdraw=normal;
  67. } break;
  68. case DRAW_PRESSED: {
  69. if (pressed.is_null()) {
  70. if (hover.is_null()) {
  71. if (normal.is_valid())
  72. texdraw=normal;
  73. } else
  74. texdraw=hover;
  75. } else
  76. texdraw=pressed;
  77. } break;
  78. case DRAW_HOVER: {
  79. if (hover.is_null()) {
  80. if (pressed.is_valid() && is_pressed())
  81. texdraw=pressed;
  82. else if (normal.is_valid())
  83. texdraw=normal;
  84. } else
  85. texdraw=hover;
  86. } break;
  87. case DRAW_DISABLED: {
  88. if (disabled.is_null()) {
  89. if (normal.is_valid())
  90. texdraw=normal;
  91. } else
  92. texdraw=disabled;
  93. } break;
  94. }
  95. if (texdraw.is_valid()) {
  96. Point2 ofs;
  97. Size2 size = texdraw->get_size();
  98. Rect2 tex_regin = Rect2(Point2(), texdraw->get_size());
  99. bool tile = false;
  100. if (expand) {
  101. switch (stretch_mode) {
  102. case STRETCH_KEEP:
  103. size = texdraw->get_size();
  104. break;
  105. case STRETCH_SCALE:
  106. size = get_size();
  107. break;
  108. case STRETCH_TILE:
  109. size = get_size();
  110. tile = true;
  111. break;
  112. case STRETCH_KEEP_CENTERED:
  113. ofs = (get_size() - texdraw->get_size())/2;
  114. size = texdraw->get_size();
  115. break;
  116. case STRETCH_KEEP_ASPECT_CENTERED:
  117. case STRETCH_KEEP_ASPECT: {
  118. Size2 _size=get_size();
  119. float tex_width = texdraw->get_width() * _size.height / texdraw->get_height();
  120. float tex_height = _size.height;
  121. if (tex_width > _size.width) {
  122. tex_width = _size.width;
  123. tex_height = texdraw->get_height() * tex_width / texdraw->get_width();
  124. }
  125. if (stretch_mode==STRETCH_KEEP_ASPECT_CENTERED) {
  126. ofs.x = (_size.width - tex_width)/2;
  127. ofs.y = (_size.height - tex_height)/2;
  128. }
  129. size.width = tex_width;
  130. size.height = tex_height;
  131. } break;
  132. case STRETCH_KEEP_ASPECT_COVERED:{
  133. size = get_size();
  134. Size2 tex_size = texdraw->get_size();
  135. Size2 scaleSize(size.width/tex_size.width, size.height/tex_size.height);
  136. float scale = scaleSize.width > scaleSize.height? scaleSize.width : scaleSize.height;
  137. Size2 scaledTexSize = tex_size * scale;
  138. Point2 ofs = ((scaledTexSize - size) / scale).abs() / 2.0f;
  139. tex_regin = Rect2(ofs, size/scale);
  140. } break;
  141. }
  142. }
  143. if (tile)
  144. draw_texture_rect(texdraw,Rect2(ofs,size),tile);
  145. else
  146. draw_texture_rect_region(texdraw, Rect2(ofs, size), tex_regin);
  147. }
  148. if (has_focus() && focused.is_valid()) {
  149. Rect2 drect(Point2(), get_size());
  150. draw_texture_rect(focused,drect,false);
  151. };
  152. } break;
  153. }
  154. }
  155. void TextureButton::_bind_methods() {
  156. ClassDB::bind_method(_MD("set_normal_texture","texture:Texture"),&TextureButton::set_normal_texture);
  157. ClassDB::bind_method(_MD("set_pressed_texture","texture:Texture"),&TextureButton::set_pressed_texture);
  158. ClassDB::bind_method(_MD("set_hover_texture","texture:Texture"),&TextureButton::set_hover_texture);
  159. ClassDB::bind_method(_MD("set_disabled_texture","texture:Texture"),&TextureButton::set_disabled_texture);
  160. ClassDB::bind_method(_MD("set_focused_texture","texture:Texture"),&TextureButton::set_focused_texture);
  161. ClassDB::bind_method(_MD("set_click_mask","mask:BitMap"),&TextureButton::set_click_mask);
  162. ClassDB::bind_method(_MD("set_expand","p_expand"),&TextureButton::set_expand);
  163. ClassDB::bind_method(_MD("set_stretch_mode","p_mode"),&TextureButton::set_stretch_mode);
  164. ClassDB::bind_method(_MD("get_normal_texture:Texture"),&TextureButton::get_normal_texture);
  165. ClassDB::bind_method(_MD("get_pressed_texture:Texture"),&TextureButton::get_pressed_texture);
  166. ClassDB::bind_method(_MD("get_hover_texture:Texture"),&TextureButton::get_hover_texture);
  167. ClassDB::bind_method(_MD("get_disabled_texture:Texture"),&TextureButton::get_disabled_texture);
  168. ClassDB::bind_method(_MD("get_focused_texture:Texture"),&TextureButton::get_focused_texture);
  169. ClassDB::bind_method(_MD("get_click_mask:BitMap"),&TextureButton::get_click_mask);
  170. ClassDB::bind_method(_MD("get_expand"),&TextureButton::get_expand);
  171. ClassDB::bind_method(_MD("get_stretch_mode"),&TextureButton::get_stretch_mode);
  172. ADD_GROUP("Textures","texture_");
  173. ADD_PROPERTYNZ(PropertyInfo(Variant::OBJECT,"texture_normal",PROPERTY_HINT_RESOURCE_TYPE,"Texture"), "set_normal_texture", "get_normal_texture");
  174. ADD_PROPERTYNZ(PropertyInfo(Variant::OBJECT,"texture_pressed",PROPERTY_HINT_RESOURCE_TYPE,"Texture"), "set_pressed_texture", "get_pressed_texture");
  175. ADD_PROPERTYNZ(PropertyInfo(Variant::OBJECT,"texture_hover",PROPERTY_HINT_RESOURCE_TYPE,"Texture"), "set_hover_texture", "get_hover_texture");
  176. ADD_PROPERTYNZ(PropertyInfo(Variant::OBJECT,"texture_disabled",PROPERTY_HINT_RESOURCE_TYPE,"Texture"), "set_disabled_texture", "get_disabled_texture");
  177. ADD_PROPERTYNZ(PropertyInfo(Variant::OBJECT,"texture_focused",PROPERTY_HINT_RESOURCE_TYPE,"Texture"), "set_focused_texture", "get_focused_texture");
  178. ADD_PROPERTYNZ(PropertyInfo(Variant::OBJECT,"texture_click_mask",PROPERTY_HINT_RESOURCE_TYPE,"BitMap"), "set_click_mask", "get_click_mask") ;
  179. ADD_PROPERTYNZ(PropertyInfo(Variant::BOOL,"expand",PROPERTY_HINT_RESOURCE_TYPE,"bool"), "set_expand", "get_expand") ;
  180. ADD_PROPERTYNO(PropertyInfo(Variant::INT,"stretch_mode",PROPERTY_HINT_ENUM,"Scale,Tile,Keep,Keep Centered,Keep Aspect,Keep Aspect Centered,Keep Aspect Covered"), "set_stretch_mode", "get_stretch_mode");
  181. BIND_CONSTANT(STRETCH_SCALE);
  182. BIND_CONSTANT(STRETCH_TILE);
  183. BIND_CONSTANT(STRETCH_KEEP);
  184. BIND_CONSTANT(STRETCH_KEEP_CENTERED);
  185. BIND_CONSTANT(STRETCH_KEEP_ASPECT);
  186. BIND_CONSTANT(STRETCH_KEEP_ASPECT_CENTERED);
  187. BIND_CONSTANT(STRETCH_KEEP_ASPECT_COVERED);
  188. }
  189. void TextureButton::set_normal_texture(const Ref<Texture>& p_normal) {
  190. normal=p_normal;
  191. update();
  192. minimum_size_changed();
  193. }
  194. void TextureButton::set_pressed_texture(const Ref<Texture>& p_pressed) {
  195. pressed=p_pressed;
  196. update();
  197. }
  198. void TextureButton::set_hover_texture(const Ref<Texture>& p_hover) {
  199. hover=p_hover;
  200. update();
  201. }
  202. void TextureButton::set_disabled_texture(const Ref<Texture>& p_disabled) {
  203. disabled=p_disabled;
  204. update();
  205. }
  206. void TextureButton::set_click_mask(const Ref<BitMap>& p_click_mask) {
  207. click_mask=p_click_mask;
  208. update();
  209. }
  210. Ref<Texture> TextureButton::get_normal_texture() const {
  211. return normal;
  212. }
  213. Ref<Texture> TextureButton::get_pressed_texture() const {
  214. return pressed;
  215. }
  216. Ref<Texture> TextureButton::get_hover_texture() const {
  217. return hover;
  218. }
  219. Ref<Texture> TextureButton::get_disabled_texture() const {
  220. return disabled;
  221. }
  222. Ref<BitMap> TextureButton::get_click_mask() const {
  223. return click_mask;
  224. }
  225. Ref<Texture> TextureButton::get_focused_texture() const {
  226. return focused;
  227. };
  228. void TextureButton::set_focused_texture(const Ref<Texture>& p_focused) {
  229. focused = p_focused;
  230. };
  231. bool TextureButton::get_expand() const {
  232. return expand;
  233. }
  234. void TextureButton::set_expand(bool p_expand) {
  235. expand = p_expand;
  236. minimum_size_changed();
  237. update();
  238. }
  239. void TextureButton::set_stretch_mode(StretchMode p_mode) {
  240. stretch_mode = p_mode;
  241. update();
  242. }
  243. TextureButton::StretchMode TextureButton::get_stretch_mode() const {
  244. return stretch_mode;
  245. }
  246. TextureButton::TextureButton() {
  247. expand = false;
  248. stretch_mode = STRETCH_SCALE;
  249. }