1/**************************************************************************/
2/* nine_patch_rect.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 "nine_patch_rect.h"
32
33#include "scene/scene_string_names.h"
34#include "servers/rendering_server.h"
35
36void NinePatchRect::_notification(int p_what) {
37 switch (p_what) {
38 case NOTIFICATION_DRAW: {
39 if (texture.is_null()) {
40 return;
41 }
42
43 Rect2 rect = Rect2(Point2(), get_size());
44 Rect2 src_rect = region_rect;
45
46 texture->get_rect_region(rect, src_rect, rect, src_rect);
47
48 RID ci = get_canvas_item();
49 RS::get_singleton()->canvas_item_add_nine_patch(ci, rect, src_rect, texture->get_rid(), Vector2(margin[SIDE_LEFT], margin[SIDE_TOP]), Vector2(margin[SIDE_RIGHT], margin[SIDE_BOTTOM]), RS::NinePatchAxisMode(axis_h), RS::NinePatchAxisMode(axis_v), draw_center);
50 } break;
51 }
52}
53
54Size2 NinePatchRect::get_minimum_size() const {
55 return Size2(margin[SIDE_LEFT] + margin[SIDE_RIGHT], margin[SIDE_TOP] + margin[SIDE_BOTTOM]);
56}
57
58void NinePatchRect::_bind_methods() {
59 ClassDB::bind_method(D_METHOD("set_texture", "texture"), &NinePatchRect::set_texture);
60 ClassDB::bind_method(D_METHOD("get_texture"), &NinePatchRect::get_texture);
61 ClassDB::bind_method(D_METHOD("set_patch_margin", "margin", "value"), &NinePatchRect::set_patch_margin);
62 ClassDB::bind_method(D_METHOD("get_patch_margin", "margin"), &NinePatchRect::get_patch_margin);
63 ClassDB::bind_method(D_METHOD("set_region_rect", "rect"), &NinePatchRect::set_region_rect);
64 ClassDB::bind_method(D_METHOD("get_region_rect"), &NinePatchRect::get_region_rect);
65 ClassDB::bind_method(D_METHOD("set_draw_center", "draw_center"), &NinePatchRect::set_draw_center);
66 ClassDB::bind_method(D_METHOD("is_draw_center_enabled"), &NinePatchRect::is_draw_center_enabled);
67 ClassDB::bind_method(D_METHOD("set_h_axis_stretch_mode", "mode"), &NinePatchRect::set_h_axis_stretch_mode);
68 ClassDB::bind_method(D_METHOD("get_h_axis_stretch_mode"), &NinePatchRect::get_h_axis_stretch_mode);
69 ClassDB::bind_method(D_METHOD("set_v_axis_stretch_mode", "mode"), &NinePatchRect::set_v_axis_stretch_mode);
70 ClassDB::bind_method(D_METHOD("get_v_axis_stretch_mode"), &NinePatchRect::get_v_axis_stretch_mode);
71
72 ADD_SIGNAL(MethodInfo("texture_changed"));
73
74 ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "texture", PROPERTY_HINT_RESOURCE_TYPE, "Texture2D"), "set_texture", "get_texture");
75 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "draw_center"), "set_draw_center", "is_draw_center_enabled");
76 ADD_PROPERTY(PropertyInfo(Variant::RECT2, "region_rect", PROPERTY_HINT_NONE, "suffix:px"), "set_region_rect", "get_region_rect");
77
78 ADD_GROUP("Patch Margin", "patch_margin_");
79 ADD_PROPERTYI(PropertyInfo(Variant::INT, "patch_margin_left", PROPERTY_HINT_RANGE, "0,16384,1,suffix:px"), "set_patch_margin", "get_patch_margin", SIDE_LEFT);
80 ADD_PROPERTYI(PropertyInfo(Variant::INT, "patch_margin_top", PROPERTY_HINT_RANGE, "0,16384,1,suffix:px"), "set_patch_margin", "get_patch_margin", SIDE_TOP);
81 ADD_PROPERTYI(PropertyInfo(Variant::INT, "patch_margin_right", PROPERTY_HINT_RANGE, "0,16384,1,suffix:px"), "set_patch_margin", "get_patch_margin", SIDE_RIGHT);
82 ADD_PROPERTYI(PropertyInfo(Variant::INT, "patch_margin_bottom", PROPERTY_HINT_RANGE, "0,16384,1,suffix:px"), "set_patch_margin", "get_patch_margin", SIDE_BOTTOM);
83 ADD_GROUP("Axis Stretch", "axis_stretch_");
84 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");
85 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");
86
87 BIND_ENUM_CONSTANT(AXIS_STRETCH_MODE_STRETCH);
88 BIND_ENUM_CONSTANT(AXIS_STRETCH_MODE_TILE);
89 BIND_ENUM_CONSTANT(AXIS_STRETCH_MODE_TILE_FIT);
90}
91
92void NinePatchRect::_texture_changed() {
93 queue_redraw();
94 update_minimum_size();
95}
96
97void NinePatchRect::set_texture(const Ref<Texture2D> &p_tex) {
98 if (texture == p_tex) {
99 return;
100 }
101
102 if (texture.is_valid()) {
103 texture->disconnect_changed(callable_mp(this, &NinePatchRect::_texture_changed));
104 }
105
106 texture = p_tex;
107
108 if (texture.is_valid()) {
109 texture->connect_changed(callable_mp(this, &NinePatchRect::_texture_changed));
110 }
111
112 queue_redraw();
113 update_minimum_size();
114 emit_signal(SceneStringNames::get_singleton()->texture_changed);
115}
116
117Ref<Texture2D> NinePatchRect::get_texture() const {
118 return texture;
119}
120
121void NinePatchRect::set_patch_margin(Side p_side, int p_size) {
122 ERR_FAIL_INDEX((int)p_side, 4);
123
124 if (margin[p_side] == p_size) {
125 return;
126 }
127
128 margin[p_side] = p_size;
129 queue_redraw();
130 update_minimum_size();
131}
132
133int NinePatchRect::get_patch_margin(Side p_side) const {
134 ERR_FAIL_INDEX_V((int)p_side, 4, 0);
135 return margin[p_side];
136}
137
138void NinePatchRect::set_region_rect(const Rect2 &p_region_rect) {
139 if (region_rect == p_region_rect) {
140 return;
141 }
142
143 region_rect = p_region_rect;
144
145 item_rect_changed();
146}
147
148Rect2 NinePatchRect::get_region_rect() const {
149 return region_rect;
150}
151
152void NinePatchRect::set_draw_center(bool p_enabled) {
153 if (draw_center == p_enabled) {
154 return;
155 }
156
157 draw_center = p_enabled;
158 queue_redraw();
159}
160
161bool NinePatchRect::is_draw_center_enabled() const {
162 return draw_center;
163}
164
165void NinePatchRect::set_h_axis_stretch_mode(AxisStretchMode p_mode) {
166 if (axis_h == p_mode) {
167 return;
168 }
169
170 axis_h = p_mode;
171 queue_redraw();
172}
173
174NinePatchRect::AxisStretchMode NinePatchRect::get_h_axis_stretch_mode() const {
175 return axis_h;
176}
177
178void NinePatchRect::set_v_axis_stretch_mode(AxisStretchMode p_mode) {
179 if (axis_v == p_mode) {
180 return;
181 }
182
183 axis_v = p_mode;
184 queue_redraw();
185}
186
187NinePatchRect::AxisStretchMode NinePatchRect::get_v_axis_stretch_mode() const {
188 return axis_v;
189}
190
191NinePatchRect::NinePatchRect() {
192 set_mouse_filter(MOUSE_FILTER_IGNORE);
193}
194
195NinePatchRect::~NinePatchRect() {
196}
197