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