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