texture_button.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. /**************************************************************************/
  2. /* texture_button.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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 (!ignore_texture_size) {
  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;
  60. Size2 mask_size = click_mask->get_size();
  61. if (!_position_rect.has_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 = _texture_region.position.maxf(0);
  95. rect.size = mask_size.min(_texture_region.size);
  96. }
  97. if (!rect.has_point(point)) {
  98. return false;
  99. }
  100. Point2i p = point;
  101. return click_mask->get_bitv(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. bool draw_focus = (has_focus() && focused.is_valid());
  154. // If no other texture is valid, try using focused texture.
  155. bool draw_focus_only = draw_focus && !texdraw.is_valid();
  156. if (draw_focus_only) {
  157. texdraw = focused;
  158. }
  159. if (texdraw.is_valid() || click_mask.is_valid()) {
  160. const Size2 texdraw_size = texdraw.is_valid() ? texdraw->get_size() : Size2(click_mask->get_size());
  161. size = texdraw_size;
  162. _texture_region = Rect2(Point2(), texdraw_size);
  163. _tile = false;
  164. switch (stretch_mode) {
  165. case STRETCH_KEEP:
  166. break;
  167. case STRETCH_SCALE:
  168. size = get_size();
  169. break;
  170. case STRETCH_TILE:
  171. size = get_size();
  172. _tile = true;
  173. break;
  174. case STRETCH_KEEP_CENTERED:
  175. ofs = (get_size() - texdraw_size) / 2;
  176. break;
  177. case STRETCH_KEEP_ASPECT_CENTERED:
  178. case STRETCH_KEEP_ASPECT: {
  179. Size2 _size = get_size();
  180. float tex_width = texdraw_size.width * _size.height / texdraw_size.height;
  181. float tex_height = _size.height;
  182. if (tex_width > _size.width) {
  183. tex_width = _size.width;
  184. tex_height = texdraw_size.height * tex_width / texdraw_size.width;
  185. }
  186. if (stretch_mode == STRETCH_KEEP_ASPECT_CENTERED) {
  187. ofs.x = (_size.width - tex_width) / 2;
  188. ofs.y = (_size.height - tex_height) / 2;
  189. }
  190. size.width = tex_width;
  191. size.height = tex_height;
  192. } break;
  193. case STRETCH_KEEP_ASPECT_COVERED: {
  194. size = get_size();
  195. Size2 scale_size = size / texdraw_size;
  196. float scale = scale_size.width > scale_size.height ? scale_size.width : scale_size.height;
  197. Size2 scaled_tex_size = texdraw_size * scale;
  198. Point2 ofs2 = ((scaled_tex_size - size) / scale).abs() / 2.0f;
  199. _texture_region = Rect2(ofs2, size / scale);
  200. } break;
  201. }
  202. _position_rect = Rect2(ofs, size);
  203. size.width *= hflip ? -1.0f : 1.0f;
  204. size.height *= vflip ? -1.0f : 1.0f;
  205. if (draw_focus_only) {
  206. // Do nothing, we only needed to calculate the rectangle.
  207. } else if (texdraw.is_valid()) {
  208. if (_tile) {
  209. draw_texture_rect(texdraw, Rect2(ofs, size), _tile);
  210. } else {
  211. draw_texture_rect_region(texdraw, Rect2(ofs, size), _texture_region);
  212. }
  213. }
  214. } else {
  215. _position_rect = Rect2();
  216. }
  217. if (draw_focus) {
  218. draw_texture_rect(focused, Rect2(ofs, size), false);
  219. };
  220. } break;
  221. }
  222. }
  223. void TextureButton::_bind_methods() {
  224. ClassDB::bind_method(D_METHOD("set_texture_normal", "texture"), &TextureButton::set_texture_normal);
  225. ClassDB::bind_method(D_METHOD("set_texture_pressed", "texture"), &TextureButton::set_texture_pressed);
  226. ClassDB::bind_method(D_METHOD("set_texture_hover", "texture"), &TextureButton::set_texture_hover);
  227. ClassDB::bind_method(D_METHOD("set_texture_disabled", "texture"), &TextureButton::set_texture_disabled);
  228. ClassDB::bind_method(D_METHOD("set_texture_focused", "texture"), &TextureButton::set_texture_focused);
  229. ClassDB::bind_method(D_METHOD("set_click_mask", "mask"), &TextureButton::set_click_mask);
  230. ClassDB::bind_method(D_METHOD("set_ignore_texture_size", "ignore"), &TextureButton::set_ignore_texture_size);
  231. ClassDB::bind_method(D_METHOD("set_stretch_mode", "mode"), &TextureButton::set_stretch_mode);
  232. ClassDB::bind_method(D_METHOD("set_flip_h", "enable"), &TextureButton::set_flip_h);
  233. ClassDB::bind_method(D_METHOD("is_flipped_h"), &TextureButton::is_flipped_h);
  234. ClassDB::bind_method(D_METHOD("set_flip_v", "enable"), &TextureButton::set_flip_v);
  235. ClassDB::bind_method(D_METHOD("is_flipped_v"), &TextureButton::is_flipped_v);
  236. ClassDB::bind_method(D_METHOD("get_texture_normal"), &TextureButton::get_texture_normal);
  237. ClassDB::bind_method(D_METHOD("get_texture_pressed"), &TextureButton::get_texture_pressed);
  238. ClassDB::bind_method(D_METHOD("get_texture_hover"), &TextureButton::get_texture_hover);
  239. ClassDB::bind_method(D_METHOD("get_texture_disabled"), &TextureButton::get_texture_disabled);
  240. ClassDB::bind_method(D_METHOD("get_texture_focused"), &TextureButton::get_texture_focused);
  241. ClassDB::bind_method(D_METHOD("get_click_mask"), &TextureButton::get_click_mask);
  242. ClassDB::bind_method(D_METHOD("get_ignore_texture_size"), &TextureButton::get_ignore_texture_size);
  243. ClassDB::bind_method(D_METHOD("get_stretch_mode"), &TextureButton::get_stretch_mode);
  244. ADD_GROUP("Textures", "texture_");
  245. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "texture_normal", PROPERTY_HINT_RESOURCE_TYPE, "Texture2D"), "set_texture_normal", "get_texture_normal");
  246. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "texture_pressed", PROPERTY_HINT_RESOURCE_TYPE, "Texture2D"), "set_texture_pressed", "get_texture_pressed");
  247. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "texture_hover", PROPERTY_HINT_RESOURCE_TYPE, "Texture2D"), "set_texture_hover", "get_texture_hover");
  248. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "texture_disabled", PROPERTY_HINT_RESOURCE_TYPE, "Texture2D"), "set_texture_disabled", "get_texture_disabled");
  249. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "texture_focused", PROPERTY_HINT_RESOURCE_TYPE, "Texture2D"), "set_texture_focused", "get_texture_focused");
  250. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "texture_click_mask", PROPERTY_HINT_RESOURCE_TYPE, "BitMap"), "set_click_mask", "get_click_mask");
  251. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "ignore_texture_size", PROPERTY_HINT_RESOURCE_TYPE, "bool"), "set_ignore_texture_size", "get_ignore_texture_size");
  252. 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");
  253. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "flip_h", PROPERTY_HINT_RESOURCE_TYPE, "bool"), "set_flip_h", "is_flipped_h");
  254. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "flip_v", PROPERTY_HINT_RESOURCE_TYPE, "bool"), "set_flip_v", "is_flipped_v");
  255. BIND_ENUM_CONSTANT(STRETCH_SCALE);
  256. BIND_ENUM_CONSTANT(STRETCH_TILE);
  257. BIND_ENUM_CONSTANT(STRETCH_KEEP);
  258. BIND_ENUM_CONSTANT(STRETCH_KEEP_CENTERED);
  259. BIND_ENUM_CONSTANT(STRETCH_KEEP_ASPECT);
  260. BIND_ENUM_CONSTANT(STRETCH_KEEP_ASPECT_CENTERED);
  261. BIND_ENUM_CONSTANT(STRETCH_KEEP_ASPECT_COVERED);
  262. }
  263. void TextureButton::set_texture_normal(const Ref<Texture2D> &p_normal) {
  264. _set_texture(&normal, p_normal);
  265. }
  266. void TextureButton::set_texture_pressed(const Ref<Texture2D> &p_pressed) {
  267. _set_texture(&pressed, p_pressed);
  268. }
  269. void TextureButton::set_texture_hover(const Ref<Texture2D> &p_hover) {
  270. _set_texture(&hover, p_hover);
  271. }
  272. void TextureButton::set_texture_disabled(const Ref<Texture2D> &p_disabled) {
  273. _set_texture(&disabled, p_disabled);
  274. }
  275. void TextureButton::set_click_mask(const Ref<BitMap> &p_click_mask) {
  276. if (click_mask == p_click_mask) {
  277. return;
  278. }
  279. click_mask = p_click_mask;
  280. _texture_changed();
  281. }
  282. Ref<Texture2D> TextureButton::get_texture_normal() const {
  283. return normal;
  284. }
  285. Ref<Texture2D> TextureButton::get_texture_pressed() const {
  286. return pressed;
  287. }
  288. Ref<Texture2D> TextureButton::get_texture_hover() const {
  289. return hover;
  290. }
  291. Ref<Texture2D> TextureButton::get_texture_disabled() const {
  292. return disabled;
  293. }
  294. Ref<BitMap> TextureButton::get_click_mask() const {
  295. return click_mask;
  296. }
  297. Ref<Texture2D> TextureButton::get_texture_focused() const {
  298. return focused;
  299. }
  300. void TextureButton::set_texture_focused(const Ref<Texture2D> &p_focused) {
  301. focused = p_focused;
  302. }
  303. void TextureButton::_set_texture(Ref<Texture2D> *p_destination, const Ref<Texture2D> &p_texture) {
  304. DEV_ASSERT(p_destination);
  305. Ref<Texture2D> &destination = *p_destination;
  306. if (destination == p_texture) {
  307. return;
  308. }
  309. if (destination.is_valid()) {
  310. destination->disconnect_changed(callable_mp(this, &TextureButton::_texture_changed));
  311. }
  312. destination = p_texture;
  313. if (destination.is_valid()) {
  314. // Pass `CONNECT_REFERENCE_COUNTED` to avoid early disconnect in case the same texture is assigned to different "slots".
  315. destination->connect_changed(callable_mp(this, &TextureButton::_texture_changed), CONNECT_REFERENCE_COUNTED);
  316. }
  317. _texture_changed();
  318. }
  319. void TextureButton::_texture_changed() {
  320. queue_redraw();
  321. update_minimum_size();
  322. }
  323. bool TextureButton::get_ignore_texture_size() const {
  324. return ignore_texture_size;
  325. }
  326. void TextureButton::set_ignore_texture_size(bool p_ignore) {
  327. if (ignore_texture_size == p_ignore) {
  328. return;
  329. }
  330. ignore_texture_size = p_ignore;
  331. update_minimum_size();
  332. queue_redraw();
  333. }
  334. void TextureButton::set_stretch_mode(StretchMode p_stretch_mode) {
  335. if (stretch_mode == p_stretch_mode) {
  336. return;
  337. }
  338. stretch_mode = p_stretch_mode;
  339. queue_redraw();
  340. }
  341. TextureButton::StretchMode TextureButton::get_stretch_mode() const {
  342. return stretch_mode;
  343. }
  344. void TextureButton::set_flip_h(bool p_flip) {
  345. if (hflip == p_flip) {
  346. return;
  347. }
  348. hflip = p_flip;
  349. queue_redraw();
  350. }
  351. bool TextureButton::is_flipped_h() const {
  352. return hflip;
  353. }
  354. void TextureButton::set_flip_v(bool p_flip) {
  355. if (vflip == p_flip) {
  356. return;
  357. }
  358. vflip = p_flip;
  359. queue_redraw();
  360. }
  361. bool TextureButton::is_flipped_v() const {
  362. return vflip;
  363. }
  364. TextureButton::TextureButton() {}