texture_button.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  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. Size2 TextureButton::get_minimum_size() const {
  33. Size2 rscale = Control::get_minimum_size();
  34. if (!ignore_texture_size) {
  35. if (normal.is_null()) {
  36. if (pressed.is_null()) {
  37. if (hover.is_null()) {
  38. if (click_mask.is_null()) {
  39. rscale = Size2();
  40. } else {
  41. rscale = click_mask->get_size();
  42. }
  43. } else {
  44. rscale = hover->get_size();
  45. }
  46. } else {
  47. rscale = pressed->get_size();
  48. }
  49. } else {
  50. rscale = normal->get_size();
  51. }
  52. }
  53. return rscale.abs();
  54. }
  55. bool TextureButton::has_point(const Point2 &p_point) const {
  56. if (click_mask.is_valid()) {
  57. Point2 point = p_point;
  58. Rect2 rect;
  59. Size2 mask_size = click_mask->get_size();
  60. if (!_position_rect.has_area()) {
  61. rect.size = mask_size;
  62. } else if (_tile) {
  63. // if the stretch mode is tile we offset the point to keep it inside the mask size
  64. rect.size = mask_size;
  65. if (_position_rect.has_point(point)) {
  66. int cols = (int)Math::ceil(_position_rect.size.x / mask_size.x);
  67. int rows = (int)Math::ceil(_position_rect.size.y / mask_size.y);
  68. int col = (int)(point.x / mask_size.x) % cols;
  69. int row = (int)(point.y / mask_size.y) % rows;
  70. point.x -= mask_size.x * col;
  71. point.y -= mask_size.y * row;
  72. }
  73. } else {
  74. // we need to transform the point from our scaled / translated image back to our mask image
  75. Point2 ofs = _position_rect.position;
  76. Size2 scale = mask_size / _position_rect.size;
  77. switch (stretch_mode) {
  78. case STRETCH_KEEP_ASPECT_COVERED: {
  79. // if the stretch mode is aspect covered the image uses a texture region so we need to take that into account
  80. float min = MIN(scale.x, scale.y);
  81. scale.x = min;
  82. scale.y = min;
  83. ofs -= _texture_region.position / min;
  84. } break;
  85. default: {
  86. // FIXME: Why a switch if we only handle one enum value?
  87. }
  88. }
  89. // offset and scale the new point position to adjust it to the bitmask size
  90. point -= ofs;
  91. point *= scale;
  92. // finally, we need to check if the point is inside a rectangle with a position >= 0,0 and a size <= mask_size
  93. rect.position = _texture_region.position.maxf(0);
  94. rect.size = mask_size.min(_texture_region.size);
  95. }
  96. if (!rect.has_point(point)) {
  97. return false;
  98. }
  99. Point2i p = point;
  100. return click_mask->get_bitv(p);
  101. }
  102. return Control::has_point(p_point);
  103. }
  104. void TextureButton::_notification(int p_what) {
  105. switch (p_what) {
  106. case NOTIFICATION_DRAW: {
  107. DrawMode draw_mode = get_draw_mode();
  108. Ref<Texture2D> texdraw;
  109. switch (draw_mode) {
  110. case DRAW_NORMAL: {
  111. if (normal.is_valid()) {
  112. texdraw = normal;
  113. }
  114. } break;
  115. case DRAW_HOVER_PRESSED:
  116. case DRAW_PRESSED: {
  117. if (pressed.is_null()) {
  118. if (hover.is_null()) {
  119. if (normal.is_valid()) {
  120. texdraw = normal;
  121. }
  122. } else {
  123. texdraw = hover;
  124. }
  125. } else {
  126. texdraw = pressed;
  127. }
  128. } break;
  129. case DRAW_HOVER: {
  130. if (hover.is_null()) {
  131. if (pressed.is_valid() && is_pressed()) {
  132. texdraw = pressed;
  133. } else if (normal.is_valid()) {
  134. texdraw = normal;
  135. }
  136. } else {
  137. texdraw = hover;
  138. }
  139. } break;
  140. case DRAW_DISABLED: {
  141. if (disabled.is_null()) {
  142. if (normal.is_valid()) {
  143. texdraw = normal;
  144. }
  145. } else {
  146. texdraw = disabled;
  147. }
  148. } break;
  149. }
  150. Point2 ofs;
  151. Size2 size;
  152. bool draw_focus = (has_focus() && focused.is_valid());
  153. // If no other texture is valid, try using focused texture.
  154. bool draw_focus_only = draw_focus && texdraw.is_null();
  155. if (draw_focus_only) {
  156. texdraw = focused;
  157. }
  158. if (texdraw.is_valid() || click_mask.is_valid()) {
  159. const Size2 texdraw_size = texdraw.is_valid() ? texdraw->get_size() : Size2(click_mask->get_size());
  160. size = texdraw_size;
  161. _texture_region = Rect2(Point2(), texdraw_size);
  162. _tile = false;
  163. switch (stretch_mode) {
  164. case STRETCH_KEEP:
  165. break;
  166. case STRETCH_SCALE:
  167. size = get_size();
  168. break;
  169. case STRETCH_TILE:
  170. size = get_size();
  171. _tile = true;
  172. break;
  173. case STRETCH_KEEP_CENTERED:
  174. ofs = (get_size() - texdraw_size) / 2;
  175. break;
  176. case STRETCH_KEEP_ASPECT_CENTERED:
  177. case STRETCH_KEEP_ASPECT: {
  178. Size2 _size = get_size();
  179. float tex_width = texdraw_size.width * _size.height / texdraw_size.height;
  180. float tex_height = _size.height;
  181. if (tex_width > _size.width) {
  182. tex_width = _size.width;
  183. tex_height = texdraw_size.height * tex_width / texdraw_size.width;
  184. }
  185. if (stretch_mode == STRETCH_KEEP_ASPECT_CENTERED) {
  186. ofs.x = (_size.width - tex_width) / 2;
  187. ofs.y = (_size.height - tex_height) / 2;
  188. }
  189. size.width = tex_width;
  190. size.height = tex_height;
  191. } break;
  192. case STRETCH_KEEP_ASPECT_COVERED: {
  193. size = get_size();
  194. Size2 scale_size = size / texdraw_size;
  195. float scale = scale_size.width > scale_size.height ? scale_size.width : scale_size.height;
  196. Size2 scaled_tex_size = texdraw_size * scale;
  197. Point2 ofs2 = ((scaled_tex_size - size) / scale).abs() / 2.0f;
  198. _texture_region = Rect2(ofs2, size / scale);
  199. } break;
  200. }
  201. _position_rect = Rect2(ofs, size);
  202. size.width *= hflip ? -1.0f : 1.0f;
  203. size.height *= vflip ? -1.0f : 1.0f;
  204. if (draw_focus_only) {
  205. // Do nothing, we only needed to calculate the rectangle.
  206. } else if (texdraw.is_valid()) {
  207. if (_tile) {
  208. draw_texture_rect(texdraw, Rect2(ofs, size), _tile);
  209. } else {
  210. draw_texture_rect_region(texdraw, Rect2(ofs, size), _texture_region);
  211. }
  212. }
  213. } else {
  214. _position_rect = Rect2();
  215. }
  216. if (draw_focus) {
  217. draw_texture_rect(focused, Rect2(ofs, size), false);
  218. };
  219. } break;
  220. }
  221. }
  222. void TextureButton::_bind_methods() {
  223. ClassDB::bind_method(D_METHOD("set_texture_normal", "texture"), &TextureButton::set_texture_normal);
  224. ClassDB::bind_method(D_METHOD("set_texture_pressed", "texture"), &TextureButton::set_texture_pressed);
  225. ClassDB::bind_method(D_METHOD("set_texture_hover", "texture"), &TextureButton::set_texture_hover);
  226. ClassDB::bind_method(D_METHOD("set_texture_disabled", "texture"), &TextureButton::set_texture_disabled);
  227. ClassDB::bind_method(D_METHOD("set_texture_focused", "texture"), &TextureButton::set_texture_focused);
  228. ClassDB::bind_method(D_METHOD("set_click_mask", "mask"), &TextureButton::set_click_mask);
  229. ClassDB::bind_method(D_METHOD("set_ignore_texture_size", "ignore"), &TextureButton::set_ignore_texture_size);
  230. ClassDB::bind_method(D_METHOD("set_stretch_mode", "mode"), &TextureButton::set_stretch_mode);
  231. ClassDB::bind_method(D_METHOD("set_flip_h", "enable"), &TextureButton::set_flip_h);
  232. ClassDB::bind_method(D_METHOD("is_flipped_h"), &TextureButton::is_flipped_h);
  233. ClassDB::bind_method(D_METHOD("set_flip_v", "enable"), &TextureButton::set_flip_v);
  234. ClassDB::bind_method(D_METHOD("is_flipped_v"), &TextureButton::is_flipped_v);
  235. ClassDB::bind_method(D_METHOD("get_texture_normal"), &TextureButton::get_texture_normal);
  236. ClassDB::bind_method(D_METHOD("get_texture_pressed"), &TextureButton::get_texture_pressed);
  237. ClassDB::bind_method(D_METHOD("get_texture_hover"), &TextureButton::get_texture_hover);
  238. ClassDB::bind_method(D_METHOD("get_texture_disabled"), &TextureButton::get_texture_disabled);
  239. ClassDB::bind_method(D_METHOD("get_texture_focused"), &TextureButton::get_texture_focused);
  240. ClassDB::bind_method(D_METHOD("get_click_mask"), &TextureButton::get_click_mask);
  241. ClassDB::bind_method(D_METHOD("get_ignore_texture_size"), &TextureButton::get_ignore_texture_size);
  242. ClassDB::bind_method(D_METHOD("get_stretch_mode"), &TextureButton::get_stretch_mode);
  243. ADD_GROUP("Textures", "texture_");
  244. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "texture_normal", PROPERTY_HINT_RESOURCE_TYPE, "Texture2D"), "set_texture_normal", "get_texture_normal");
  245. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "texture_pressed", PROPERTY_HINT_RESOURCE_TYPE, "Texture2D"), "set_texture_pressed", "get_texture_pressed");
  246. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "texture_hover", PROPERTY_HINT_RESOURCE_TYPE, "Texture2D"), "set_texture_hover", "get_texture_hover");
  247. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "texture_disabled", PROPERTY_HINT_RESOURCE_TYPE, "Texture2D"), "set_texture_disabled", "get_texture_disabled");
  248. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "texture_focused", PROPERTY_HINT_RESOURCE_TYPE, "Texture2D"), "set_texture_focused", "get_texture_focused");
  249. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "texture_click_mask", PROPERTY_HINT_RESOURCE_TYPE, "BitMap"), "set_click_mask", "get_click_mask");
  250. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "ignore_texture_size", PROPERTY_HINT_RESOURCE_TYPE, "bool"), "set_ignore_texture_size", "get_ignore_texture_size");
  251. 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");
  252. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "flip_h", PROPERTY_HINT_RESOURCE_TYPE, "bool"), "set_flip_h", "is_flipped_h");
  253. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "flip_v", PROPERTY_HINT_RESOURCE_TYPE, "bool"), "set_flip_v", "is_flipped_v");
  254. BIND_ENUM_CONSTANT(STRETCH_SCALE);
  255. BIND_ENUM_CONSTANT(STRETCH_TILE);
  256. BIND_ENUM_CONSTANT(STRETCH_KEEP);
  257. BIND_ENUM_CONSTANT(STRETCH_KEEP_CENTERED);
  258. BIND_ENUM_CONSTANT(STRETCH_KEEP_ASPECT);
  259. BIND_ENUM_CONSTANT(STRETCH_KEEP_ASPECT_CENTERED);
  260. BIND_ENUM_CONSTANT(STRETCH_KEEP_ASPECT_COVERED);
  261. }
  262. void TextureButton::set_texture_normal(const Ref<Texture2D> &p_normal) {
  263. _set_texture(&normal, p_normal);
  264. }
  265. void TextureButton::set_texture_pressed(const Ref<Texture2D> &p_pressed) {
  266. _set_texture(&pressed, p_pressed);
  267. }
  268. void TextureButton::set_texture_hover(const Ref<Texture2D> &p_hover) {
  269. _set_texture(&hover, p_hover);
  270. }
  271. void TextureButton::set_texture_disabled(const Ref<Texture2D> &p_disabled) {
  272. _set_texture(&disabled, p_disabled);
  273. }
  274. void TextureButton::set_click_mask(const Ref<BitMap> &p_click_mask) {
  275. if (click_mask == p_click_mask) {
  276. return;
  277. }
  278. click_mask = p_click_mask;
  279. _texture_changed();
  280. }
  281. Ref<Texture2D> TextureButton::get_texture_normal() const {
  282. return normal;
  283. }
  284. Ref<Texture2D> TextureButton::get_texture_pressed() const {
  285. return pressed;
  286. }
  287. Ref<Texture2D> TextureButton::get_texture_hover() const {
  288. return hover;
  289. }
  290. Ref<Texture2D> TextureButton::get_texture_disabled() const {
  291. return disabled;
  292. }
  293. Ref<BitMap> TextureButton::get_click_mask() const {
  294. return click_mask;
  295. }
  296. Ref<Texture2D> TextureButton::get_texture_focused() const {
  297. return focused;
  298. }
  299. void TextureButton::set_texture_focused(const Ref<Texture2D> &p_focused) {
  300. _set_texture(&focused, p_focused);
  301. }
  302. void TextureButton::_set_texture(Ref<Texture2D> *p_destination, const Ref<Texture2D> &p_texture) {
  303. DEV_ASSERT(p_destination);
  304. Ref<Texture2D> &destination = *p_destination;
  305. if (destination == p_texture) {
  306. return;
  307. }
  308. if (destination.is_valid()) {
  309. destination->disconnect_changed(callable_mp(this, &TextureButton::_texture_changed));
  310. }
  311. destination = p_texture;
  312. if (destination.is_valid()) {
  313. // Pass `CONNECT_REFERENCE_COUNTED` to avoid early disconnect in case the same texture is assigned to different "slots".
  314. destination->connect_changed(callable_mp(this, &TextureButton::_texture_changed), CONNECT_REFERENCE_COUNTED);
  315. }
  316. _texture_changed();
  317. }
  318. void TextureButton::_texture_changed() {
  319. queue_redraw();
  320. update_minimum_size();
  321. }
  322. bool TextureButton::get_ignore_texture_size() const {
  323. return ignore_texture_size;
  324. }
  325. void TextureButton::set_ignore_texture_size(bool p_ignore) {
  326. if (ignore_texture_size == p_ignore) {
  327. return;
  328. }
  329. ignore_texture_size = p_ignore;
  330. update_minimum_size();
  331. queue_redraw();
  332. }
  333. void TextureButton::set_stretch_mode(StretchMode p_stretch_mode) {
  334. if (stretch_mode == p_stretch_mode) {
  335. return;
  336. }
  337. stretch_mode = p_stretch_mode;
  338. queue_redraw();
  339. }
  340. TextureButton::StretchMode TextureButton::get_stretch_mode() const {
  341. return stretch_mode;
  342. }
  343. void TextureButton::set_flip_h(bool p_flip) {
  344. if (hflip == p_flip) {
  345. return;
  346. }
  347. hflip = p_flip;
  348. queue_redraw();
  349. }
  350. bool TextureButton::is_flipped_h() const {
  351. return hflip;
  352. }
  353. void TextureButton::set_flip_v(bool p_flip) {
  354. if (vflip == p_flip) {
  355. return;
  356. }
  357. vflip = p_flip;
  358. queue_redraw();
  359. }
  360. bool TextureButton::is_flipped_v() const {
  361. return vflip;
  362. }