texture_button.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. /*************************************************************************/
  2. /* texture_button.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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. #include "core/typedefs.h"
  32. #include <stdlib.h>
  33. Size2 TextureButton::get_minimum_size() const {
  34. Size2 rscale = Control::get_minimum_size();
  35. if (!expand) {
  36. if (normal.is_null()) {
  37. if (pressed.is_null()) {
  38. if (hover.is_null()) {
  39. if (click_mask.is_null()) {
  40. rscale = Size2();
  41. } else {
  42. rscale = click_mask->get_size();
  43. }
  44. } else {
  45. rscale = hover->get_size();
  46. }
  47. } else {
  48. rscale = pressed->get_size();
  49. }
  50. } else {
  51. rscale = normal->get_size();
  52. }
  53. }
  54. return rscale.abs();
  55. }
  56. bool TextureButton::has_point(const Point2 &p_point) const {
  57. if (click_mask.is_valid()) {
  58. Point2 point = p_point;
  59. Rect2 rect = Rect2();
  60. Size2 mask_size = click_mask->get_size();
  61. if (_position_rect.has_no_area()) {
  62. rect.size = mask_size;
  63. } else if (_tile) {
  64. // if the stretch mode is tile we offset the point to keep it inside the mask size
  65. rect.size = mask_size;
  66. if (_position_rect.has_point(point)) {
  67. int cols = (int)Math::ceil(_position_rect.size.x / mask_size.x);
  68. int rows = (int)Math::ceil(_position_rect.size.y / mask_size.y);
  69. int col = (int)(point.x / mask_size.x) % cols;
  70. int row = (int)(point.y / mask_size.y) % rows;
  71. point.x -= mask_size.x * col;
  72. point.y -= mask_size.y * row;
  73. }
  74. } else {
  75. // we need to transform the point from our scaled / translated image back to our mask image
  76. Point2 ofs = _position_rect.position;
  77. Size2 scale = mask_size / _position_rect.size;
  78. switch (stretch_mode) {
  79. case STRETCH_KEEP_ASPECT_COVERED: {
  80. // if the stretch mode is aspect covered the image uses a texture region so we need to take that into account
  81. float min = MIN(scale.x, scale.y);
  82. scale.x = min;
  83. scale.y = min;
  84. ofs -= _texture_region.position / min;
  85. } break;
  86. default: {
  87. // FIXME: Why a switch if we only handle one enum value?
  88. }
  89. }
  90. // offset and scale the new point position to adjust it to the bitmask size
  91. point -= ofs;
  92. point *= scale;
  93. // finally, we need to check if the point is inside a rectangle with a position >= 0,0 and a size <= mask_size
  94. rect.position = Point2(MAX(0, _texture_region.position.x), MAX(0, _texture_region.position.y));
  95. rect.size = Size2(MIN(mask_size.x, _texture_region.size.x), MIN(mask_size.y, _texture_region.size.y));
  96. }
  97. if (!rect.has_point(point)) {
  98. return false;
  99. }
  100. Point2i p = point;
  101. return click_mask->get_bit(p);
  102. }
  103. return Control::has_point(p_point);
  104. }
  105. void TextureButton::_notification(int p_what) {
  106. switch (p_what) {
  107. case NOTIFICATION_DRAW: {
  108. DrawMode draw_mode = get_draw_mode();
  109. Ref<Texture2D> texdraw;
  110. switch (draw_mode) {
  111. case DRAW_NORMAL: {
  112. if (normal.is_valid()) {
  113. texdraw = normal;
  114. }
  115. } break;
  116. case DRAW_HOVER_PRESSED:
  117. case DRAW_PRESSED: {
  118. if (pressed.is_null()) {
  119. if (hover.is_null()) {
  120. if (normal.is_valid()) {
  121. texdraw = normal;
  122. }
  123. } else {
  124. texdraw = hover;
  125. }
  126. } else {
  127. texdraw = pressed;
  128. }
  129. } break;
  130. case DRAW_HOVER: {
  131. if (hover.is_null()) {
  132. if (pressed.is_valid() && is_pressed()) {
  133. texdraw = pressed;
  134. } else if (normal.is_valid()) {
  135. texdraw = normal;
  136. }
  137. } else {
  138. texdraw = hover;
  139. }
  140. } break;
  141. case DRAW_DISABLED: {
  142. if (disabled.is_null()) {
  143. if (normal.is_valid()) {
  144. texdraw = normal;
  145. }
  146. } else {
  147. texdraw = disabled;
  148. }
  149. } break;
  150. }
  151. Point2 ofs;
  152. Size2 size;
  153. if (texdraw.is_valid()) {
  154. size = texdraw->get_size();
  155. _texture_region = Rect2(Point2(), texdraw->get_size());
  156. _tile = false;
  157. if (expand) {
  158. switch (stretch_mode) {
  159. case STRETCH_KEEP:
  160. size = texdraw->get_size();
  161. break;
  162. case STRETCH_SCALE:
  163. size = get_size();
  164. break;
  165. case STRETCH_TILE:
  166. size = get_size();
  167. _tile = true;
  168. break;
  169. case STRETCH_KEEP_CENTERED:
  170. ofs = (get_size() - texdraw->get_size()) / 2;
  171. size = texdraw->get_size();
  172. break;
  173. case STRETCH_KEEP_ASPECT_CENTERED:
  174. case STRETCH_KEEP_ASPECT: {
  175. Size2 _size = get_size();
  176. float tex_width = texdraw->get_width() * _size.height / texdraw->get_height();
  177. float tex_height = _size.height;
  178. if (tex_width > _size.width) {
  179. tex_width = _size.width;
  180. tex_height = texdraw->get_height() * tex_width / texdraw->get_width();
  181. }
  182. if (stretch_mode == STRETCH_KEEP_ASPECT_CENTERED) {
  183. ofs.x = (_size.width - tex_width) / 2;
  184. ofs.y = (_size.height - tex_height) / 2;
  185. }
  186. size.width = tex_width;
  187. size.height = tex_height;
  188. } break;
  189. case STRETCH_KEEP_ASPECT_COVERED: {
  190. size = get_size();
  191. Size2 tex_size = texdraw->get_size();
  192. Size2 scale_size(size.width / tex_size.width, size.height / tex_size.height);
  193. float scale = scale_size.width > scale_size.height ? scale_size.width : scale_size.height;
  194. Size2 scaled_tex_size = tex_size * scale;
  195. Point2 ofs2 = ((scaled_tex_size - size) / scale).abs() / 2.0f;
  196. _texture_region = Rect2(ofs2, size / scale);
  197. } break;
  198. }
  199. }
  200. _position_rect = Rect2(ofs, size);
  201. size.width *= hflip ? -1.0f : 1.0f;
  202. size.height *= vflip ? -1.0f : 1.0f;
  203. if (_tile) {
  204. draw_texture_rect(texdraw, Rect2(ofs, size), _tile);
  205. } else {
  206. draw_texture_rect_region(texdraw, Rect2(ofs, size), _texture_region);
  207. }
  208. } else {
  209. _position_rect = Rect2();
  210. }
  211. if (has_focus() && focused.is_valid()) {
  212. draw_texture_rect(focused, Rect2(ofs, size), false);
  213. };
  214. } break;
  215. }
  216. }
  217. void TextureButton::_bind_methods() {
  218. ClassDB::bind_method(D_METHOD("set_normal_texture", "texture"), &TextureButton::set_normal_texture);
  219. ClassDB::bind_method(D_METHOD("set_pressed_texture", "texture"), &TextureButton::set_pressed_texture);
  220. ClassDB::bind_method(D_METHOD("set_hover_texture", "texture"), &TextureButton::set_hover_texture);
  221. ClassDB::bind_method(D_METHOD("set_disabled_texture", "texture"), &TextureButton::set_disabled_texture);
  222. ClassDB::bind_method(D_METHOD("set_focused_texture", "texture"), &TextureButton::set_focused_texture);
  223. ClassDB::bind_method(D_METHOD("set_click_mask", "mask"), &TextureButton::set_click_mask);
  224. ClassDB::bind_method(D_METHOD("set_expand", "expand"), &TextureButton::set_expand);
  225. ClassDB::bind_method(D_METHOD("set_stretch_mode", "mode"), &TextureButton::set_stretch_mode);
  226. ClassDB::bind_method(D_METHOD("set_flip_h", "enable"), &TextureButton::set_flip_h);
  227. ClassDB::bind_method(D_METHOD("is_flipped_h"), &TextureButton::is_flipped_h);
  228. ClassDB::bind_method(D_METHOD("set_flip_v", "enable"), &TextureButton::set_flip_v);
  229. ClassDB::bind_method(D_METHOD("is_flipped_v"), &TextureButton::is_flipped_v);
  230. ClassDB::bind_method(D_METHOD("get_normal_texture"), &TextureButton::get_normal_texture);
  231. ClassDB::bind_method(D_METHOD("get_pressed_texture"), &TextureButton::get_pressed_texture);
  232. ClassDB::bind_method(D_METHOD("get_hover_texture"), &TextureButton::get_hover_texture);
  233. ClassDB::bind_method(D_METHOD("get_disabled_texture"), &TextureButton::get_disabled_texture);
  234. ClassDB::bind_method(D_METHOD("get_focused_texture"), &TextureButton::get_focused_texture);
  235. ClassDB::bind_method(D_METHOD("get_click_mask"), &TextureButton::get_click_mask);
  236. ClassDB::bind_method(D_METHOD("get_expand"), &TextureButton::get_expand);
  237. ClassDB::bind_method(D_METHOD("get_stretch_mode"), &TextureButton::get_stretch_mode);
  238. ADD_GROUP("Textures", "texture_");
  239. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "texture_normal", PROPERTY_HINT_RESOURCE_TYPE, "Texture2D"), "set_normal_texture", "get_normal_texture");
  240. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "texture_pressed", PROPERTY_HINT_RESOURCE_TYPE, "Texture2D"), "set_pressed_texture", "get_pressed_texture");
  241. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "texture_hover", PROPERTY_HINT_RESOURCE_TYPE, "Texture2D"), "set_hover_texture", "get_hover_texture");
  242. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "texture_disabled", PROPERTY_HINT_RESOURCE_TYPE, "Texture2D"), "set_disabled_texture", "get_disabled_texture");
  243. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "texture_focused", PROPERTY_HINT_RESOURCE_TYPE, "Texture2D"), "set_focused_texture", "get_focused_texture");
  244. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "texture_click_mask", PROPERTY_HINT_RESOURCE_TYPE, "BitMap"), "set_click_mask", "get_click_mask");
  245. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "expand", PROPERTY_HINT_RESOURCE_TYPE, "bool"), "set_expand", "get_expand");
  246. ADD_PROPERTY(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");
  247. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "flip_h", PROPERTY_HINT_RESOURCE_TYPE, "bool"), "set_flip_h", "is_flipped_h");
  248. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "flip_v", PROPERTY_HINT_RESOURCE_TYPE, "bool"), "set_flip_v", "is_flipped_v");
  249. BIND_ENUM_CONSTANT(STRETCH_SCALE);
  250. BIND_ENUM_CONSTANT(STRETCH_TILE);
  251. BIND_ENUM_CONSTANT(STRETCH_KEEP);
  252. BIND_ENUM_CONSTANT(STRETCH_KEEP_CENTERED);
  253. BIND_ENUM_CONSTANT(STRETCH_KEEP_ASPECT);
  254. BIND_ENUM_CONSTANT(STRETCH_KEEP_ASPECT_CENTERED);
  255. BIND_ENUM_CONSTANT(STRETCH_KEEP_ASPECT_COVERED);
  256. }
  257. void TextureButton::set_normal_texture(const Ref<Texture2D> &p_normal) {
  258. normal = p_normal;
  259. update();
  260. minimum_size_changed();
  261. }
  262. void TextureButton::set_pressed_texture(const Ref<Texture2D> &p_pressed) {
  263. pressed = p_pressed;
  264. update();
  265. minimum_size_changed();
  266. }
  267. void TextureButton::set_hover_texture(const Ref<Texture2D> &p_hover) {
  268. hover = p_hover;
  269. update();
  270. minimum_size_changed();
  271. }
  272. void TextureButton::set_disabled_texture(const Ref<Texture2D> &p_disabled) {
  273. disabled = p_disabled;
  274. update();
  275. }
  276. void TextureButton::set_click_mask(const Ref<BitMap> &p_click_mask) {
  277. click_mask = p_click_mask;
  278. update();
  279. minimum_size_changed();
  280. }
  281. Ref<Texture2D> TextureButton::get_normal_texture() const {
  282. return normal;
  283. }
  284. Ref<Texture2D> TextureButton::get_pressed_texture() const {
  285. return pressed;
  286. }
  287. Ref<Texture2D> TextureButton::get_hover_texture() const {
  288. return hover;
  289. }
  290. Ref<Texture2D> TextureButton::get_disabled_texture() const {
  291. return disabled;
  292. }
  293. Ref<BitMap> TextureButton::get_click_mask() const {
  294. return click_mask;
  295. }
  296. Ref<Texture2D> TextureButton::get_focused_texture() const {
  297. return focused;
  298. };
  299. void TextureButton::set_focused_texture(const Ref<Texture2D> &p_focused) {
  300. focused = p_focused;
  301. };
  302. bool TextureButton::get_expand() const {
  303. return expand;
  304. }
  305. void TextureButton::set_expand(bool p_expand) {
  306. expand = p_expand;
  307. minimum_size_changed();
  308. update();
  309. }
  310. void TextureButton::set_stretch_mode(StretchMode p_stretch_mode) {
  311. stretch_mode = p_stretch_mode;
  312. update();
  313. }
  314. TextureButton::StretchMode TextureButton::get_stretch_mode() const {
  315. return stretch_mode;
  316. }
  317. void TextureButton::set_flip_h(bool p_flip) {
  318. hflip = p_flip;
  319. update();
  320. }
  321. bool TextureButton::is_flipped_h() const {
  322. return hflip;
  323. }
  324. void TextureButton::set_flip_v(bool p_flip) {
  325. vflip = p_flip;
  326. update();
  327. }
  328. bool TextureButton::is_flipped_v() const {
  329. return vflip;
  330. }
  331. TextureButton::TextureButton() {}