style_box_texture.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /**************************************************************************/
  2. /* style_box_texture.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 "style_box_texture.h"
  31. float StyleBoxTexture::get_style_margin(Side p_side) const {
  32. ERR_FAIL_INDEX_V((int)p_side, 4, 0.0);
  33. return texture_margin[p_side];
  34. }
  35. void StyleBoxTexture::set_texture(Ref<Texture2D> p_texture) {
  36. if (texture == p_texture) {
  37. return;
  38. }
  39. texture = p_texture;
  40. emit_changed();
  41. }
  42. Ref<Texture2D> StyleBoxTexture::get_texture() const {
  43. return texture;
  44. }
  45. void StyleBoxTexture::set_texture_margin(Side p_side, float p_size) {
  46. ERR_FAIL_INDEX((int)p_side, 4);
  47. texture_margin[p_side] = p_size;
  48. emit_changed();
  49. }
  50. void StyleBoxTexture::set_texture_margin_all(float p_size) {
  51. for (int i = 0; i < 4; i++) {
  52. texture_margin[i] = p_size;
  53. }
  54. emit_changed();
  55. }
  56. void StyleBoxTexture::set_texture_margin_individual(float p_left, float p_top, float p_right, float p_bottom) {
  57. texture_margin[SIDE_LEFT] = p_left;
  58. texture_margin[SIDE_TOP] = p_top;
  59. texture_margin[SIDE_RIGHT] = p_right;
  60. texture_margin[SIDE_BOTTOM] = p_bottom;
  61. emit_changed();
  62. }
  63. float StyleBoxTexture::get_texture_margin(Side p_side) const {
  64. ERR_FAIL_INDEX_V((int)p_side, 4, 0.0);
  65. return texture_margin[p_side];
  66. }
  67. void StyleBoxTexture::set_expand_margin(Side p_side, float p_size) {
  68. ERR_FAIL_INDEX((int)p_side, 4);
  69. expand_margin[p_side] = p_size;
  70. emit_changed();
  71. }
  72. void StyleBoxTexture::set_expand_margin_all(float p_expand_margin_size) {
  73. for (int i = 0; i < 4; i++) {
  74. expand_margin[i] = p_expand_margin_size;
  75. }
  76. emit_changed();
  77. }
  78. void StyleBoxTexture::set_expand_margin_individual(float p_left, float p_top, float p_right, float p_bottom) {
  79. expand_margin[SIDE_LEFT] = p_left;
  80. expand_margin[SIDE_TOP] = p_top;
  81. expand_margin[SIDE_RIGHT] = p_right;
  82. expand_margin[SIDE_BOTTOM] = p_bottom;
  83. emit_changed();
  84. }
  85. float StyleBoxTexture::get_expand_margin(Side p_side) const {
  86. ERR_FAIL_INDEX_V((int)p_side, 4, 0);
  87. return expand_margin[p_side];
  88. }
  89. void StyleBoxTexture::set_region_rect(const Rect2 &p_region_rect) {
  90. if (region_rect == p_region_rect) {
  91. return;
  92. }
  93. region_rect = p_region_rect;
  94. emit_changed();
  95. }
  96. Rect2 StyleBoxTexture::get_region_rect() const {
  97. return region_rect;
  98. }
  99. void StyleBoxTexture::set_draw_center(bool p_enabled) {
  100. draw_center = p_enabled;
  101. emit_changed();
  102. }
  103. bool StyleBoxTexture::is_draw_center_enabled() const {
  104. return draw_center;
  105. }
  106. void StyleBoxTexture::set_h_axis_stretch_mode(AxisStretchMode p_mode) {
  107. ERR_FAIL_INDEX((int)p_mode, 3);
  108. axis_h = p_mode;
  109. emit_changed();
  110. }
  111. StyleBoxTexture::AxisStretchMode StyleBoxTexture::get_h_axis_stretch_mode() const {
  112. return axis_h;
  113. }
  114. void StyleBoxTexture::set_v_axis_stretch_mode(AxisStretchMode p_mode) {
  115. ERR_FAIL_INDEX((int)p_mode, 3);
  116. axis_v = p_mode;
  117. emit_changed();
  118. }
  119. StyleBoxTexture::AxisStretchMode StyleBoxTexture::get_v_axis_stretch_mode() const {
  120. return axis_v;
  121. }
  122. void StyleBoxTexture::set_modulate(const Color &p_modulate) {
  123. if (modulate == p_modulate) {
  124. return;
  125. }
  126. modulate = p_modulate;
  127. emit_changed();
  128. }
  129. Color StyleBoxTexture::get_modulate() const {
  130. return modulate;
  131. }
  132. Rect2 StyleBoxTexture::get_draw_rect(const Rect2 &p_rect) const {
  133. return p_rect.grow_individual(expand_margin[SIDE_LEFT], expand_margin[SIDE_TOP], expand_margin[SIDE_RIGHT], expand_margin[SIDE_BOTTOM]);
  134. }
  135. void StyleBoxTexture::draw(RID p_canvas_item, const Rect2 &p_rect) const {
  136. if (texture.is_null()) {
  137. return;
  138. }
  139. Rect2 rect = p_rect;
  140. Rect2 src_rect = region_rect;
  141. texture->get_rect_region(rect, src_rect, rect, src_rect);
  142. rect.position.x -= expand_margin[SIDE_LEFT];
  143. rect.position.y -= expand_margin[SIDE_TOP];
  144. rect.size.x += expand_margin[SIDE_LEFT] + expand_margin[SIDE_RIGHT];
  145. rect.size.y += expand_margin[SIDE_TOP] + expand_margin[SIDE_BOTTOM];
  146. Vector2 start_offset = Vector2(texture_margin[SIDE_LEFT], texture_margin[SIDE_TOP]);
  147. Vector2 end_offset = Vector2(texture_margin[SIDE_RIGHT], texture_margin[SIDE_BOTTOM]);
  148. RenderingServer::get_singleton()->canvas_item_add_nine_patch(p_canvas_item, rect, src_rect, texture->get_rid(), start_offset, end_offset, RS::NinePatchAxisMode(axis_h), RS::NinePatchAxisMode(axis_v), draw_center, modulate);
  149. }
  150. void StyleBoxTexture::_bind_methods() {
  151. ClassDB::bind_method(D_METHOD("set_texture", "texture"), &StyleBoxTexture::set_texture);
  152. ClassDB::bind_method(D_METHOD("get_texture"), &StyleBoxTexture::get_texture);
  153. ClassDB::bind_method(D_METHOD("set_texture_margin", "margin", "size"), &StyleBoxTexture::set_texture_margin);
  154. ClassDB::bind_method(D_METHOD("set_texture_margin_all", "size"), &StyleBoxTexture::set_texture_margin_all);
  155. ClassDB::bind_method(D_METHOD("get_texture_margin", "margin"), &StyleBoxTexture::get_texture_margin);
  156. ClassDB::bind_method(D_METHOD("set_expand_margin", "margin", "size"), &StyleBoxTexture::set_expand_margin);
  157. ClassDB::bind_method(D_METHOD("set_expand_margin_all", "size"), &StyleBoxTexture::set_expand_margin_all);
  158. ClassDB::bind_method(D_METHOD("get_expand_margin", "margin"), &StyleBoxTexture::get_expand_margin);
  159. ClassDB::bind_method(D_METHOD("set_region_rect", "region"), &StyleBoxTexture::set_region_rect);
  160. ClassDB::bind_method(D_METHOD("get_region_rect"), &StyleBoxTexture::get_region_rect);
  161. ClassDB::bind_method(D_METHOD("set_draw_center", "enable"), &StyleBoxTexture::set_draw_center);
  162. ClassDB::bind_method(D_METHOD("is_draw_center_enabled"), &StyleBoxTexture::is_draw_center_enabled);
  163. ClassDB::bind_method(D_METHOD("set_modulate", "color"), &StyleBoxTexture::set_modulate);
  164. ClassDB::bind_method(D_METHOD("get_modulate"), &StyleBoxTexture::get_modulate);
  165. ClassDB::bind_method(D_METHOD("set_h_axis_stretch_mode", "mode"), &StyleBoxTexture::set_h_axis_stretch_mode);
  166. ClassDB::bind_method(D_METHOD("get_h_axis_stretch_mode"), &StyleBoxTexture::get_h_axis_stretch_mode);
  167. ClassDB::bind_method(D_METHOD("set_v_axis_stretch_mode", "mode"), &StyleBoxTexture::set_v_axis_stretch_mode);
  168. ClassDB::bind_method(D_METHOD("get_v_axis_stretch_mode"), &StyleBoxTexture::get_v_axis_stretch_mode);
  169. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "texture", PROPERTY_HINT_RESOURCE_TYPE, "Texture2D"), "set_texture", "get_texture");
  170. ADD_GROUP("Texture Margins", "texture_margin_");
  171. ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "texture_margin_left", PROPERTY_HINT_RANGE, "0,2048,1,suffix:px"), "set_texture_margin", "get_texture_margin", SIDE_LEFT);
  172. ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "texture_margin_top", PROPERTY_HINT_RANGE, "0,2048,1,suffix:px"), "set_texture_margin", "get_texture_margin", SIDE_TOP);
  173. ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "texture_margin_right", PROPERTY_HINT_RANGE, "0,2048,1,suffix:px"), "set_texture_margin", "get_texture_margin", SIDE_RIGHT);
  174. ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "texture_margin_bottom", PROPERTY_HINT_RANGE, "0,2048,1,suffix:px"), "set_texture_margin", "get_texture_margin", SIDE_BOTTOM);
  175. ADD_GROUP("Expand Margins", "expand_margin_");
  176. ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "expand_margin_left", PROPERTY_HINT_RANGE, "0,2048,1,suffix:px"), "set_expand_margin", "get_expand_margin", SIDE_LEFT);
  177. ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "expand_margin_top", PROPERTY_HINT_RANGE, "0,2048,1,suffix:px"), "set_expand_margin", "get_expand_margin", SIDE_TOP);
  178. ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "expand_margin_right", PROPERTY_HINT_RANGE, "0,2048,1,suffix:px"), "set_expand_margin", "get_expand_margin", SIDE_RIGHT);
  179. ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "expand_margin_bottom", PROPERTY_HINT_RANGE, "0,2048,1,suffix:px"), "set_expand_margin", "get_expand_margin", SIDE_BOTTOM);
  180. ADD_GROUP("Axis Stretch", "axis_stretch_");
  181. ADD_PROPERTY(PropertyInfo(Variant::INT, "axis_stretch_horizontal", PROPERTY_HINT_ENUM, "Stretch,Tile,Tile Fit"), "set_h_axis_stretch_mode", "get_h_axis_stretch_mode");
  182. ADD_PROPERTY(PropertyInfo(Variant::INT, "axis_stretch_vertical", PROPERTY_HINT_ENUM, "Stretch,Tile,Tile Fit"), "set_v_axis_stretch_mode", "get_v_axis_stretch_mode");
  183. ADD_GROUP("Sub-Region", "region_");
  184. ADD_PROPERTY(PropertyInfo(Variant::RECT2, "region_rect", PROPERTY_HINT_NONE, "suffix:px"), "set_region_rect", "get_region_rect");
  185. ADD_GROUP("Modulate", "modulate_");
  186. ADD_PROPERTY(PropertyInfo(Variant::COLOR, "modulate_color"), "set_modulate", "get_modulate");
  187. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "draw_center"), "set_draw_center", "is_draw_center_enabled");
  188. BIND_ENUM_CONSTANT(AXIS_STRETCH_MODE_STRETCH);
  189. BIND_ENUM_CONSTANT(AXIS_STRETCH_MODE_TILE);
  190. BIND_ENUM_CONSTANT(AXIS_STRETCH_MODE_TILE_FIT);
  191. }
  192. StyleBoxTexture::StyleBoxTexture() {}
  193. StyleBoxTexture::~StyleBoxTexture() {}