1 /*************************************************************************/
2 /* nine_patch_rect.cpp */
3 /*************************************************************************/
4 /* This file is part of: */
5 /* GODOT ENGINE */
6 /* https://godotengine.org */
7 /*************************************************************************/
8 /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
9 /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
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 "nine_patch_rect.h"
32
33 #include "servers/visual_server.h"
34
_notification(int p_what)35 void NinePatchRect::_notification(int p_what) {
36
37 if (p_what == NOTIFICATION_DRAW) {
38
39 if (texture.is_null())
40 return;
41
42 Rect2 rect = Rect2(Point2(), get_size());
43 Rect2 src_rect = region_rect;
44
45 texture->get_rect_region(rect, src_rect, rect, src_rect);
46
47 RID ci = get_canvas_item();
48 VS::get_singleton()->canvas_item_add_nine_patch(ci, rect, src_rect, texture->get_rid(), Vector2(margin[MARGIN_LEFT], margin[MARGIN_TOP]), Vector2(margin[MARGIN_RIGHT], margin[MARGIN_BOTTOM]), VS::NinePatchAxisMode(axis_h), VS::NinePatchAxisMode(axis_v), draw_center);
49 }
50 }
51
get_minimum_size() const52 Size2 NinePatchRect::get_minimum_size() const {
53
54 return Size2(margin[MARGIN_LEFT] + margin[MARGIN_RIGHT], margin[MARGIN_TOP] + margin[MARGIN_BOTTOM]);
55 }
_bind_methods()56 void NinePatchRect::_bind_methods() {
57
58 ClassDB::bind_method(D_METHOD("set_texture", "texture"), &NinePatchRect::set_texture);
59 ClassDB::bind_method(D_METHOD("get_texture"), &NinePatchRect::get_texture);
60 ClassDB::bind_method(D_METHOD("set_patch_margin", "margin", "value"), &NinePatchRect::set_patch_margin);
61 ClassDB::bind_method(D_METHOD("get_patch_margin", "margin"), &NinePatchRect::get_patch_margin);
62 ClassDB::bind_method(D_METHOD("set_region_rect", "rect"), &NinePatchRect::set_region_rect);
63 ClassDB::bind_method(D_METHOD("get_region_rect"), &NinePatchRect::get_region_rect);
64 ClassDB::bind_method(D_METHOD("set_draw_center", "draw_center"), &NinePatchRect::set_draw_center);
65 ClassDB::bind_method(D_METHOD("is_draw_center_enabled"), &NinePatchRect::is_draw_center_enabled);
66 ClassDB::bind_method(D_METHOD("set_h_axis_stretch_mode", "mode"), &NinePatchRect::set_h_axis_stretch_mode);
67 ClassDB::bind_method(D_METHOD("get_h_axis_stretch_mode"), &NinePatchRect::get_h_axis_stretch_mode);
68 ClassDB::bind_method(D_METHOD("set_v_axis_stretch_mode", "mode"), &NinePatchRect::set_v_axis_stretch_mode);
69 ClassDB::bind_method(D_METHOD("get_v_axis_stretch_mode"), &NinePatchRect::get_v_axis_stretch_mode);
70
71 ADD_SIGNAL(MethodInfo("texture_changed"));
72
73 ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "texture", PROPERTY_HINT_RESOURCE_TYPE, "Texture"), "set_texture", "get_texture");
74 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "draw_center"), "set_draw_center", "is_draw_center_enabled");
75 ADD_PROPERTY(PropertyInfo(Variant::RECT2, "region_rect"), "set_region_rect", "get_region_rect");
76
77 ADD_GROUP("Patch Margin", "patch_margin_");
78 ADD_PROPERTYI(PropertyInfo(Variant::INT, "patch_margin_left", PROPERTY_HINT_RANGE, "0,16384,1"), "set_patch_margin", "get_patch_margin", MARGIN_LEFT);
79 ADD_PROPERTYI(PropertyInfo(Variant::INT, "patch_margin_top", PROPERTY_HINT_RANGE, "0,16384,1"), "set_patch_margin", "get_patch_margin", MARGIN_TOP);
80 ADD_PROPERTYI(PropertyInfo(Variant::INT, "patch_margin_right", PROPERTY_HINT_RANGE, "0,16384,1"), "set_patch_margin", "get_patch_margin", MARGIN_RIGHT);
81 ADD_PROPERTYI(PropertyInfo(Variant::INT, "patch_margin_bottom", PROPERTY_HINT_RANGE, "0,16384,1"), "set_patch_margin", "get_patch_margin", MARGIN_BOTTOM);
82 ADD_GROUP("Axis Stretch", "axis_stretch_");
83 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");
84 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");
85
86 BIND_ENUM_CONSTANT(AXIS_STRETCH_MODE_STRETCH);
87 BIND_ENUM_CONSTANT(AXIS_STRETCH_MODE_TILE);
88 BIND_ENUM_CONSTANT(AXIS_STRETCH_MODE_TILE_FIT);
89 }
90
set_texture(const Ref<Texture> & p_tex)91 void NinePatchRect::set_texture(const Ref<Texture> &p_tex) {
92
93 if (texture == p_tex)
94 return;
95 texture = p_tex;
96 update();
97 /*
98 if (texture.is_valid())
99 texture->set_flags(texture->get_flags()&(~Texture::FLAG_REPEAT)); //remove repeat from texture, it looks bad in sprites
100 */
101 minimum_size_changed();
102 emit_signal("texture_changed");
103 _change_notify("texture");
104 }
105
get_texture() const106 Ref<Texture> NinePatchRect::get_texture() const {
107
108 return texture;
109 }
110
set_patch_margin(Margin p_margin,int p_size)111 void NinePatchRect::set_patch_margin(Margin p_margin, int p_size) {
112
113 ERR_FAIL_INDEX((int)p_margin, 4);
114 margin[p_margin] = p_size;
115 update();
116 minimum_size_changed();
117 switch (p_margin) {
118 case MARGIN_LEFT:
119 _change_notify("patch_margin_left");
120 break;
121 case MARGIN_TOP:
122 _change_notify("patch_margin_top");
123 break;
124 case MARGIN_RIGHT:
125 _change_notify("patch_margin_right");
126 break;
127 case MARGIN_BOTTOM:
128 _change_notify("patch_margin_bottom");
129 break;
130 }
131 }
132
get_patch_margin(Margin p_margin) const133 int NinePatchRect::get_patch_margin(Margin p_margin) const {
134
135 ERR_FAIL_INDEX_V((int)p_margin, 4, 0);
136 return margin[p_margin];
137 }
138
set_region_rect(const Rect2 & p_region_rect)139 void NinePatchRect::set_region_rect(const Rect2 &p_region_rect) {
140
141 if (region_rect == p_region_rect)
142 return;
143
144 region_rect = p_region_rect;
145
146 item_rect_changed();
147 _change_notify("region_rect");
148 }
149
get_region_rect() const150 Rect2 NinePatchRect::get_region_rect() const {
151
152 return region_rect;
153 }
154
set_draw_center(bool p_enabled)155 void NinePatchRect::set_draw_center(bool p_enabled) {
156
157 draw_center = p_enabled;
158 update();
159 }
160
is_draw_center_enabled() const161 bool NinePatchRect::is_draw_center_enabled() const {
162
163 return draw_center;
164 }
165
set_h_axis_stretch_mode(AxisStretchMode p_mode)166 void NinePatchRect::set_h_axis_stretch_mode(AxisStretchMode p_mode) {
167 axis_h = p_mode;
168 update();
169 }
170
get_h_axis_stretch_mode() const171 NinePatchRect::AxisStretchMode NinePatchRect::get_h_axis_stretch_mode() const {
172 return axis_h;
173 }
174
set_v_axis_stretch_mode(AxisStretchMode p_mode)175 void NinePatchRect::set_v_axis_stretch_mode(AxisStretchMode p_mode) {
176
177 axis_v = p_mode;
178 update();
179 }
180
get_v_axis_stretch_mode() const181 NinePatchRect::AxisStretchMode NinePatchRect::get_v_axis_stretch_mode() const {
182
183 return axis_v;
184 }
185
NinePatchRect()186 NinePatchRect::NinePatchRect() {
187
188 margin[MARGIN_LEFT] = 0;
189 margin[MARGIN_RIGHT] = 0;
190 margin[MARGIN_BOTTOM] = 0;
191 margin[MARGIN_TOP] = 0;
192
193 set_mouse_filter(MOUSE_FILTER_IGNORE);
194 draw_center = true;
195
196 axis_h = AXIS_STRETCH_MODE_STRETCH;
197 axis_v = AXIS_STRETCH_MODE_STRETCH;
198 }
199
~NinePatchRect()200 NinePatchRect::~NinePatchRect() {
201 }
202