texture_button.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. /*************************************************************************/
  2. /* texture_button.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md) */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "texture_button.h"
  31. Size2 TextureButton::get_minimum_size() const {
  32. Size2 rscale = Control::get_minimum_size();
  33. if (!expand) {
  34. if (normal.is_null()) {
  35. if (pressed.is_null()) {
  36. if (hover.is_null())
  37. if (click_mask.is_null())
  38. rscale = Size2();
  39. else
  40. rscale = click_mask->get_size();
  41. else
  42. rscale = hover->get_size();
  43. } else
  44. rscale = pressed->get_size();
  45. } else
  46. rscale = normal->get_size();
  47. }
  48. return rscale.abs();
  49. }
  50. bool TextureButton::has_point(const Point2 &p_point) const {
  51. if (click_mask.is_valid()) {
  52. Point2i p = p_point;
  53. if (p.x < 0 || p.x >= click_mask->get_size().width || p.y < 0 || p.y >= click_mask->get_size().height)
  54. return false;
  55. return click_mask->get_bit(p);
  56. }
  57. return Control::has_point(p_point);
  58. }
  59. void TextureButton::_notification(int p_what) {
  60. switch (p_what) {
  61. case NOTIFICATION_DRAW: {
  62. DrawMode draw_mode = get_draw_mode();
  63. Ref<Texture> texdraw;
  64. switch (draw_mode) {
  65. case DRAW_NORMAL: {
  66. if (normal.is_valid())
  67. texdraw = normal;
  68. } break;
  69. case DRAW_PRESSED: {
  70. if (pressed.is_null()) {
  71. if (hover.is_null()) {
  72. if (normal.is_valid())
  73. texdraw = normal;
  74. } else
  75. texdraw = hover;
  76. } else
  77. texdraw = pressed;
  78. } break;
  79. case DRAW_HOVER: {
  80. if (hover.is_null()) {
  81. if (pressed.is_valid() && is_pressed())
  82. texdraw = pressed;
  83. else if (normal.is_valid())
  84. texdraw = normal;
  85. } else
  86. texdraw = hover;
  87. } break;
  88. case DRAW_DISABLED: {
  89. if (disabled.is_null()) {
  90. if (normal.is_valid())
  91. texdraw = normal;
  92. } else
  93. texdraw = disabled;
  94. } break;
  95. }
  96. if (texdraw.is_valid()) {
  97. Point2 ofs;
  98. Size2 size = texdraw->get_size();
  99. Rect2 tex_regin = Rect2(Point2(), texdraw->get_size());
  100. bool tile = false;
  101. if (expand) {
  102. switch (stretch_mode) {
  103. case STRETCH_KEEP:
  104. size = texdraw->get_size();
  105. break;
  106. case STRETCH_SCALE:
  107. size = get_size();
  108. break;
  109. case STRETCH_TILE:
  110. size = get_size();
  111. tile = true;
  112. break;
  113. case STRETCH_KEEP_CENTERED:
  114. ofs = (get_size() - texdraw->get_size()) / 2;
  115. size = texdraw->get_size();
  116. break;
  117. case STRETCH_KEEP_ASPECT_CENTERED:
  118. case STRETCH_KEEP_ASPECT: {
  119. Size2 _size = get_size();
  120. float tex_width = texdraw->get_width() * _size.height / texdraw->get_height();
  121. float tex_height = _size.height;
  122. if (tex_width > _size.width) {
  123. tex_width = _size.width;
  124. tex_height = texdraw->get_height() * tex_width / texdraw->get_width();
  125. }
  126. if (stretch_mode == STRETCH_KEEP_ASPECT_CENTERED) {
  127. ofs.x = (_size.width - tex_width) / 2;
  128. ofs.y = (_size.height - tex_height) / 2;
  129. }
  130. size.width = tex_width;
  131. size.height = tex_height;
  132. } break;
  133. case STRETCH_KEEP_ASPECT_COVERED: {
  134. size = get_size();
  135. Size2 tex_size = texdraw->get_size();
  136. Size2 scaleSize(size.width / tex_size.width, size.height / tex_size.height);
  137. float scale = scaleSize.width > scaleSize.height ? scaleSize.width : scaleSize.height;
  138. Size2 scaledTexSize = tex_size * scale;
  139. Point2 ofs = ((scaledTexSize - size) / scale).abs() / 2.0f;
  140. tex_regin = Rect2(ofs, size / scale);
  141. } break;
  142. }
  143. }
  144. if (tile)
  145. draw_texture_rect(texdraw, Rect2(ofs, size), tile);
  146. else
  147. draw_texture_rect_region(texdraw, Rect2(ofs, size), tex_regin);
  148. }
  149. if (has_focus() && focused.is_valid()) {
  150. Rect2 drect(Point2(), get_size());
  151. draw_texture_rect(focused, drect, false);
  152. };
  153. } break;
  154. }
  155. }
  156. void TextureButton::_bind_methods() {
  157. ClassDB::bind_method(D_METHOD("set_normal_texture", "texture"), &TextureButton::set_normal_texture);
  158. ClassDB::bind_method(D_METHOD("set_pressed_texture", "texture"), &TextureButton::set_pressed_texture);
  159. ClassDB::bind_method(D_METHOD("set_hover_texture", "texture"), &TextureButton::set_hover_texture);
  160. ClassDB::bind_method(D_METHOD("set_disabled_texture", "texture"), &TextureButton::set_disabled_texture);
  161. ClassDB::bind_method(D_METHOD("set_focused_texture", "texture"), &TextureButton::set_focused_texture);
  162. ClassDB::bind_method(D_METHOD("set_click_mask", "mask"), &TextureButton::set_click_mask);
  163. ClassDB::bind_method(D_METHOD("set_expand", "p_expand"), &TextureButton::set_expand);
  164. ClassDB::bind_method(D_METHOD("set_stretch_mode", "p_mode"), &TextureButton::set_stretch_mode);
  165. ClassDB::bind_method(D_METHOD("get_normal_texture"), &TextureButton::get_normal_texture);
  166. ClassDB::bind_method(D_METHOD("get_pressed_texture"), &TextureButton::get_pressed_texture);
  167. ClassDB::bind_method(D_METHOD("get_hover_texture"), &TextureButton::get_hover_texture);
  168. ClassDB::bind_method(D_METHOD("get_disabled_texture"), &TextureButton::get_disabled_texture);
  169. ClassDB::bind_method(D_METHOD("get_focused_texture"), &TextureButton::get_focused_texture);
  170. ClassDB::bind_method(D_METHOD("get_click_mask"), &TextureButton::get_click_mask);
  171. ClassDB::bind_method(D_METHOD("get_expand"), &TextureButton::get_expand);
  172. ClassDB::bind_method(D_METHOD("get_stretch_mode"), &TextureButton::get_stretch_mode);
  173. ADD_GROUP("Textures", "texture_");
  174. ADD_PROPERTYNZ(PropertyInfo(Variant::OBJECT, "texture_normal", PROPERTY_HINT_RESOURCE_TYPE, "Texture"), "set_normal_texture", "get_normal_texture");
  175. ADD_PROPERTYNZ(PropertyInfo(Variant::OBJECT, "texture_pressed", PROPERTY_HINT_RESOURCE_TYPE, "Texture"), "set_pressed_texture", "get_pressed_texture");
  176. ADD_PROPERTYNZ(PropertyInfo(Variant::OBJECT, "texture_hover", PROPERTY_HINT_RESOURCE_TYPE, "Texture"), "set_hover_texture", "get_hover_texture");
  177. ADD_PROPERTYNZ(PropertyInfo(Variant::OBJECT, "texture_disabled", PROPERTY_HINT_RESOURCE_TYPE, "Texture"), "set_disabled_texture", "get_disabled_texture");
  178. ADD_PROPERTYNZ(PropertyInfo(Variant::OBJECT, "texture_focused", PROPERTY_HINT_RESOURCE_TYPE, "Texture"), "set_focused_texture", "get_focused_texture");
  179. ADD_PROPERTYNZ(PropertyInfo(Variant::OBJECT, "texture_click_mask", PROPERTY_HINT_RESOURCE_TYPE, "BitMap"), "set_click_mask", "get_click_mask");
  180. ADD_PROPERTYNZ(PropertyInfo(Variant::BOOL, "expand", PROPERTY_HINT_RESOURCE_TYPE, "bool"), "set_expand", "get_expand");
  181. ADD_PROPERTYNZ(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");
  182. BIND_ENUM_CONSTANT(STRETCH_SCALE);
  183. BIND_ENUM_CONSTANT(STRETCH_TILE);
  184. BIND_ENUM_CONSTANT(STRETCH_KEEP);
  185. BIND_ENUM_CONSTANT(STRETCH_KEEP_CENTERED);
  186. BIND_ENUM_CONSTANT(STRETCH_KEEP_ASPECT);
  187. BIND_ENUM_CONSTANT(STRETCH_KEEP_ASPECT_CENTERED);
  188. BIND_ENUM_CONSTANT(STRETCH_KEEP_ASPECT_COVERED);
  189. }
  190. void TextureButton::set_normal_texture(const Ref<Texture> &p_normal) {
  191. normal = p_normal;
  192. update();
  193. minimum_size_changed();
  194. }
  195. void TextureButton::set_pressed_texture(const Ref<Texture> &p_pressed) {
  196. pressed = p_pressed;
  197. update();
  198. }
  199. void TextureButton::set_hover_texture(const Ref<Texture> &p_hover) {
  200. hover = p_hover;
  201. update();
  202. }
  203. void TextureButton::set_disabled_texture(const Ref<Texture> &p_disabled) {
  204. disabled = p_disabled;
  205. update();
  206. }
  207. void TextureButton::set_click_mask(const Ref<BitMap> &p_click_mask) {
  208. click_mask = p_click_mask;
  209. update();
  210. }
  211. Ref<Texture> TextureButton::get_normal_texture() const {
  212. return normal;
  213. }
  214. Ref<Texture> TextureButton::get_pressed_texture() const {
  215. return pressed;
  216. }
  217. Ref<Texture> TextureButton::get_hover_texture() const {
  218. return hover;
  219. }
  220. Ref<Texture> TextureButton::get_disabled_texture() const {
  221. return disabled;
  222. }
  223. Ref<BitMap> TextureButton::get_click_mask() const {
  224. return click_mask;
  225. }
  226. Ref<Texture> TextureButton::get_focused_texture() const {
  227. return focused;
  228. };
  229. void TextureButton::set_focused_texture(const Ref<Texture> &p_focused) {
  230. focused = p_focused;
  231. };
  232. bool TextureButton::get_expand() const {
  233. return expand;
  234. }
  235. void TextureButton::set_expand(bool p_expand) {
  236. expand = p_expand;
  237. minimum_size_changed();
  238. update();
  239. }
  240. void TextureButton::set_stretch_mode(StretchMode p_stretch_mode) {
  241. stretch_mode = p_stretch_mode;
  242. update();
  243. }
  244. TextureButton::StretchMode TextureButton::get_stretch_mode() const {
  245. return stretch_mode;
  246. }
  247. TextureButton::TextureButton() {
  248. expand = false;
  249. stretch_mode = STRETCH_SCALE;
  250. }