From 79dda1350da919f86ccde0e1421a675745cf651a Mon Sep 17 00:00:00 2001 From: Indah Sylvia Date: Tue, 24 Oct 2017 12:59:04 +0700 Subject: Finished transition of Patch9Rect to NinePatchRect --- editor/plugins/canvas_item_editor_plugin.cpp | 2 +- editor/plugins/texture_region_editor_plugin.cpp | 90 +++++------ editor/plugins/texture_region_editor_plugin.h | 4 +- scene/gui/nine_patch_rect.cpp | 200 ++++++++++++++++++++++++ scene/gui/nine_patch_rect.h | 84 ++++++++++ scene/gui/patch_9_rect.cpp | 200 ------------------------ scene/gui/patch_9_rect.h | 84 ---------- scene/register_scene_types.cpp | 2 +- 8 files changed, 333 insertions(+), 333 deletions(-) create mode 100644 scene/gui/nine_patch_rect.cpp create mode 100644 scene/gui/nine_patch_rect.h delete mode 100644 scene/gui/patch_9_rect.cpp delete mode 100644 scene/gui/patch_9_rect.h diff --git a/editor/plugins/canvas_item_editor_plugin.cpp b/editor/plugins/canvas_item_editor_plugin.cpp index 70a6954fb5..5782edd321 100644 --- a/editor/plugins/canvas_item_editor_plugin.cpp +++ b/editor/plugins/canvas_item_editor_plugin.cpp @@ -45,7 +45,7 @@ #include "scene/2d/screen_button.h" #include "scene/2d/sprite.h" #include "scene/gui/grid_container.h" -#include "scene/gui/patch_9_rect.h" +#include "scene/gui/nine_patch_rect.h" #include "scene/main/canvas_layer.h" #include "scene/main/viewport.h" #include "scene/resources/packed_scene.h" diff --git a/editor/plugins/texture_region_editor_plugin.cpp b/editor/plugins/texture_region_editor_plugin.cpp index 740772e204..8870166dba 100644 --- a/editor/plugins/texture_region_editor_plugin.cpp +++ b/editor/plugins/texture_region_editor_plugin.cpp @@ -48,8 +48,8 @@ void TextureRegionEditor::_region_draw() { Ref base_tex = NULL; if (node_sprite) base_tex = node_sprite->get_texture(); - else if (node_patch9) - base_tex = node_patch9->get_texture(); + else if (node_ninepatch) + base_tex = node_ninepatch->get_texture(); else if (obj_styleBox.is_valid()) base_tex = obj_styleBox->get_texture(); else if (atlas_tex.is_valid()) @@ -177,12 +177,12 @@ void TextureRegionEditor::_region_draw() { updating_scroll = false; float margins[4]; - if (node_patch9 || obj_styleBox.is_valid()) { - if (node_patch9) { - margins[0] = node_patch9->get_patch_margin(MARGIN_TOP); - margins[1] = node_patch9->get_patch_margin(MARGIN_BOTTOM); - margins[2] = node_patch9->get_patch_margin(MARGIN_LEFT); - margins[3] = node_patch9->get_patch_margin(MARGIN_RIGHT); + if (node_ninepatch || obj_styleBox.is_valid()) { + if (node_ninepatch) { + margins[0] = node_ninepatch->get_patch_margin(MARGIN_TOP); + margins[1] = node_ninepatch->get_patch_margin(MARGIN_BOTTOM); + margins[2] = node_ninepatch->get_patch_margin(MARGIN_LEFT); + margins[3] = node_ninepatch->get_patch_margin(MARGIN_RIGHT); } else if (obj_styleBox.is_valid()) { margins[0] = obj_styleBox->get_margin_size(MARGIN_TOP); margins[1] = obj_styleBox->get_margin_size(MARGIN_BOTTOM); @@ -225,14 +225,14 @@ void TextureRegionEditor::_region_input(const Ref &p_input) { if (mb->get_button_index() == BUTTON_LEFT) { if (mb->is_pressed()) { - if (node_patch9 || obj_styleBox.is_valid()) { + if (node_ninepatch || obj_styleBox.is_valid()) { edited_margin = -1; float margins[4]; - if (node_patch9) { - margins[0] = node_patch9->get_patch_margin(MARGIN_TOP); - margins[1] = node_patch9->get_patch_margin(MARGIN_BOTTOM); - margins[2] = node_patch9->get_patch_margin(MARGIN_LEFT); - margins[3] = node_patch9->get_patch_margin(MARGIN_RIGHT); + if (node_ninepatch) { + margins[0] = node_ninepatch->get_patch_margin(MARGIN_TOP); + margins[1] = node_ninepatch->get_patch_margin(MARGIN_BOTTOM); + margins[2] = node_ninepatch->get_patch_margin(MARGIN_LEFT); + margins[3] = node_ninepatch->get_patch_margin(MARGIN_RIGHT); } else if (obj_styleBox.is_valid()) { margins[0] = obj_styleBox->get_margin_size(MARGIN_TOP); margins[1] = obj_styleBox->get_margin_size(MARGIN_BOTTOM); @@ -272,8 +272,8 @@ void TextureRegionEditor::_region_input(const Ref &p_input) { Rect2 r; if (node_sprite) r = node_sprite->get_region_rect(); - else if (node_patch9) - r = node_patch9->get_region_rect(); + else if (node_ninepatch) + r = node_ninepatch->get_region_rect(); else if (obj_styleBox.is_valid()) r = obj_styleBox->get_region_rect(); else if (atlas_tex.is_valid()) @@ -285,9 +285,9 @@ void TextureRegionEditor::_region_input(const Ref &p_input) { if (node_sprite) { undo_redo->add_do_method(node_sprite, "set_region_rect", rect); undo_redo->add_undo_method(node_sprite, "set_region_rect", node_sprite->get_region_rect()); - } else if (node_patch9) { - undo_redo->add_do_method(node_patch9, "set_region_rect", rect); - undo_redo->add_undo_method(node_patch9, "set_region_rect", node_patch9->get_region_rect()); + } else if (node_ninepatch) { + undo_redo->add_do_method(node_ninepatch, "set_region_rect", rect); + undo_redo->add_undo_method(node_ninepatch, "set_region_rect", node_ninepatch->get_region_rect()); } else if (obj_styleBox.is_valid()) { undo_redo->add_do_method(obj_styleBox.ptr(), "set_region_rect", rect); undo_redo->add_undo_method(obj_styleBox.ptr(), "set_region_rect", obj_styleBox->get_region_rect()); @@ -310,8 +310,8 @@ void TextureRegionEditor::_region_input(const Ref &p_input) { drag = true; if (node_sprite) rect_prev = node_sprite->get_region_rect(); - else if (node_patch9) - rect_prev = node_patch9->get_region_rect(); + else if (node_ninepatch) + rect_prev = node_ninepatch->get_region_rect(); else if (obj_styleBox.is_valid()) rect_prev = obj_styleBox->get_region_rect(); else if (atlas_tex.is_valid()) @@ -334,9 +334,9 @@ void TextureRegionEditor::_region_input(const Ref &p_input) { if (edited_margin >= 0) { undo_redo->create_action("Set Margin"); static Margin m[4] = { MARGIN_TOP, MARGIN_BOTTOM, MARGIN_LEFT, MARGIN_RIGHT }; - if (node_patch9) { - undo_redo->add_do_method(node_patch9, "set_patch_margin", m[edited_margin], node_patch9->get_patch_margin(m[edited_margin])); - undo_redo->add_undo_method(node_patch9, "set_patch_margin", m[edited_margin], prev_margin); + if (node_ninepatch) { + undo_redo->add_do_method(node_ninepatch, "set_patch_margin", m[edited_margin], node_ninepatch->get_patch_margin(m[edited_margin])); + undo_redo->add_undo_method(node_ninepatch, "set_patch_margin", m[edited_margin], prev_margin); } else if (obj_styleBox.is_valid()) { undo_redo->add_do_method(obj_styleBox.ptr(), "set_margin_size", m[edited_margin], obj_styleBox->get_margin_size(m[edited_margin])); undo_redo->add_undo_method(obj_styleBox.ptr(), "set_margin_size", m[edited_margin], prev_margin); @@ -351,11 +351,11 @@ void TextureRegionEditor::_region_input(const Ref &p_input) { } else if (atlas_tex.is_valid()) { undo_redo->add_do_method(atlas_tex.ptr(), "set_region", atlas_tex->get_region()); undo_redo->add_undo_method(atlas_tex.ptr(), "set_region", rect_prev); - } else if (node_patch9) { + } else if (node_ninepatch) { // FIXME: Is this intentional? - } else if (node_patch9) { - undo_redo->add_do_method(node_patch9, "set_region_rect", node_patch9->get_region_rect()); - undo_redo->add_undo_method(node_patch9, "set_region_rect", rect_prev); + } else if (node_ninepatch) { + undo_redo->add_do_method(node_ninepatch, "set_region_rect", node_ninepatch->get_region_rect()); + undo_redo->add_undo_method(node_ninepatch, "set_region_rect", rect_prev); } else if (obj_styleBox.is_valid()) { undo_redo->add_do_method(obj_styleBox.ptr(), "set_region_rect", obj_styleBox->get_region_rect()); undo_redo->add_undo_method(obj_styleBox.ptr(), "set_region_rect", rect_prev); @@ -375,8 +375,8 @@ void TextureRegionEditor::_region_input(const Ref &p_input) { drag = false; if (edited_margin >= 0) { static Margin m[4] = { MARGIN_TOP, MARGIN_BOTTOM, MARGIN_LEFT, MARGIN_RIGHT }; - if (node_patch9) - node_patch9->set_patch_margin(m[edited_margin], prev_margin); + if (node_ninepatch) + node_ninepatch->set_patch_margin(m[edited_margin], prev_margin); if (obj_styleBox.is_valid()) obj_styleBox->set_margin_size(m[edited_margin], prev_margin); edited_margin = -1; @@ -422,8 +422,8 @@ void TextureRegionEditor::_region_input(const Ref &p_input) { if (new_margin < 0) new_margin = 0; static Margin m[4] = { MARGIN_TOP, MARGIN_BOTTOM, MARGIN_LEFT, MARGIN_RIGHT }; - if (node_patch9) - node_patch9->set_patch_margin(m[edited_margin], new_margin); + if (node_ninepatch) + node_ninepatch->set_patch_margin(m[edited_margin], new_margin); if (obj_styleBox.is_valid()) obj_styleBox->set_margin_size(m[edited_margin], new_margin); } else { @@ -573,8 +573,8 @@ void TextureRegionEditor::_zoom_out() { void TextureRegionEditor::apply_rect(const Rect2 &rect) { if (node_sprite) node_sprite->set_region_rect(rect); - else if (node_patch9) - node_patch9->set_region_rect(rect); + else if (node_ninepatch) + node_ninepatch->set_region_rect(rect); else if (obj_styleBox.is_valid()) obj_styleBox->set_region_rect(rect); else if (atlas_tex.is_valid()) @@ -593,8 +593,8 @@ void TextureRegionEditor::_notification(int p_what) { } void TextureRegionEditor::_node_removed(Object *p_obj) { - if (p_obj == node_sprite || p_obj == node_patch9 || p_obj == obj_styleBox.ptr() || p_obj == atlas_tex.ptr()) { - node_patch9 = NULL; + if (p_obj == node_sprite || p_obj == node_ninepatch || p_obj == obj_styleBox.ptr() || p_obj == atlas_tex.ptr()) { + node_ninepatch = NULL; node_sprite = NULL; obj_styleBox = Ref(NULL); atlas_tex = Ref(NULL); @@ -623,15 +623,15 @@ void TextureRegionEditor::_bind_methods() { void TextureRegionEditor::edit(Object *p_obj) { if (node_sprite) node_sprite->remove_change_receptor(this); - if (node_patch9) - node_patch9->remove_change_receptor(this); + if (node_ninepatch) + node_ninepatch->remove_change_receptor(this); if (obj_styleBox.is_valid()) obj_styleBox->remove_change_receptor(this); if (atlas_tex.is_valid()) atlas_tex->remove_change_receptor(this); if (p_obj) { node_sprite = Object::cast_to(p_obj); - node_patch9 = Object::cast_to(p_obj); + node_ninepatch = Object::cast_to(p_obj); if (Object::cast_to(p_obj)) obj_styleBox = Ref(Object::cast_to(p_obj)); if (Object::cast_to(p_obj)) @@ -640,7 +640,7 @@ void TextureRegionEditor::edit(Object *p_obj) { _edit_region(); } else { node_sprite = NULL; - node_patch9 = NULL; + node_ninepatch = NULL; obj_styleBox = Ref(NULL); atlas_tex = Ref(NULL); } @@ -659,8 +659,8 @@ void TextureRegionEditor::_edit_region() { Ref texture = NULL; if (node_sprite) texture = node_sprite->get_texture(); - else if (node_patch9) - texture = node_patch9->get_texture(); + else if (node_ninepatch) + texture = node_ninepatch->get_texture(); else if (obj_styleBox.is_valid()) texture = obj_styleBox->get_texture(); else if (atlas_tex.is_valid()) @@ -726,8 +726,8 @@ void TextureRegionEditor::_edit_region() { if (node_sprite) rect = node_sprite->get_region_rect(); - else if (node_patch9) - rect = node_patch9->get_region_rect(); + else if (node_ninepatch) + rect = node_ninepatch->get_region_rect(); else if (obj_styleBox.is_valid()) rect = obj_styleBox->get_region_rect(); else if (atlas_tex.is_valid()) @@ -747,7 +747,7 @@ Vector2 TextureRegionEditor::snap_point(Vector2 p_target) const { TextureRegionEditor::TextureRegionEditor(EditorNode *p_editor) { node_sprite = NULL; - node_patch9 = NULL; + node_ninepatch = NULL; obj_styleBox = Ref(NULL); atlas_tex = Ref(NULL); editor = p_editor; diff --git a/editor/plugins/texture_region_editor_plugin.h b/editor/plugins/texture_region_editor_plugin.h index 0789dde1c1..2058dad791 100644 --- a/editor/plugins/texture_region_editor_plugin.h +++ b/editor/plugins/texture_region_editor_plugin.h @@ -37,7 +37,7 @@ #include "editor/editor_node.h" #include "editor/editor_plugin.h" #include "scene/2d/sprite.h" -#include "scene/gui/patch_9_rect.h" +#include "scene/gui/nine_patch_rect.h" #include "scene/resources/style_box.h" #include "scene/resources/texture.h" @@ -82,7 +82,7 @@ class TextureRegionEditor : public Control { Vector2 snap_step; Vector2 snap_separation; - NinePatchRect *node_patch9; + NinePatchRect *node_ninepatch; Sprite *node_sprite; Ref obj_styleBox; Ref atlas_tex; diff --git a/scene/gui/nine_patch_rect.cpp b/scene/gui/nine_patch_rect.cpp new file mode 100644 index 0000000000..c02d80bba8 --- /dev/null +++ b/scene/gui/nine_patch_rect.cpp @@ -0,0 +1,200 @@ +/*************************************************************************/ +/* nine_patch_rect.cpp */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md) */ +/* */ +/* Permission is hereby granted, free of charge, to any person obtaining */ +/* a copy of this software and associated documentation files (the */ +/* "Software"), to deal in the Software without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of the Software, and to */ +/* permit persons to whom the Software is furnished to do so, subject to */ +/* the following conditions: */ +/* */ +/* The above copyright notice and this permission notice shall be */ +/* included in all copies or substantial portions of the Software. */ +/* */ +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ +/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ +/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ +/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ +/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/*************************************************************************/ +#include "nine_patch_rect.h" + +#include "servers/visual_server.h" + +void NinePatchRect::_notification(int p_what) { + + if (p_what == NOTIFICATION_DRAW) { + + if (texture.is_null()) + return; + + Rect2 rect = Rect2(Point2(), get_size()); + Rect2 src_rect = region_rect; + + texture->get_rect_region(rect, src_rect, rect, src_rect); + + RID ci = get_canvas_item(); + 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); + } +} + +Size2 NinePatchRect::get_minimum_size() const { + + return Size2(margin[MARGIN_LEFT] + margin[MARGIN_RIGHT], margin[MARGIN_TOP] + margin[MARGIN_BOTTOM]); +} +void NinePatchRect::_bind_methods() { + + ClassDB::bind_method(D_METHOD("set_texture", "texture"), &NinePatchRect::set_texture); + ClassDB::bind_method(D_METHOD("get_texture"), &NinePatchRect::get_texture); + ClassDB::bind_method(D_METHOD("set_patch_margin", "margin", "value"), &NinePatchRect::set_patch_margin); + ClassDB::bind_method(D_METHOD("get_patch_margin", "margin"), &NinePatchRect::get_patch_margin); + ClassDB::bind_method(D_METHOD("set_region_rect", "rect"), &NinePatchRect::set_region_rect); + ClassDB::bind_method(D_METHOD("get_region_rect"), &NinePatchRect::get_region_rect); + ClassDB::bind_method(D_METHOD("set_draw_center", "draw_center"), &NinePatchRect::set_draw_center); + ClassDB::bind_method(D_METHOD("is_draw_center_enabled"), &NinePatchRect::is_draw_center_enabled); + ClassDB::bind_method(D_METHOD("set_h_axis_stretch_mode", "mode"), &NinePatchRect::set_h_axis_stretch_mode); + ClassDB::bind_method(D_METHOD("get_h_axis_stretch_mode"), &NinePatchRect::get_h_axis_stretch_mode); + ClassDB::bind_method(D_METHOD("set_v_axis_stretch_mode", "mode"), &NinePatchRect::set_v_axis_stretch_mode); + ClassDB::bind_method(D_METHOD("get_v_axis_stretch_mode"), &NinePatchRect::get_v_axis_stretch_mode); + + ADD_SIGNAL(MethodInfo("texture_changed")); + + ADD_PROPERTYNZ(PropertyInfo(Variant::OBJECT, "texture", PROPERTY_HINT_RESOURCE_TYPE, "Texture"), "set_texture", "get_texture"); + ADD_PROPERTYNO(PropertyInfo(Variant::BOOL, "draw_center"), "set_draw_center", "is_draw_center_enabled"); + ADD_PROPERTYNZ(PropertyInfo(Variant::RECT2, "region_rect"), "set_region_rect", "get_region_rect"); + + ADD_GROUP("Patch Margin", "patch_margin_"); + ADD_PROPERTYINZ(PropertyInfo(Variant::INT, "patch_margin_left", PROPERTY_HINT_RANGE, "0,16384,1"), "set_patch_margin", "get_patch_margin", MARGIN_LEFT); + ADD_PROPERTYINZ(PropertyInfo(Variant::INT, "patch_margin_top", PROPERTY_HINT_RANGE, "0,16384,1"), "set_patch_margin", "get_patch_margin", MARGIN_TOP); + ADD_PROPERTYINZ(PropertyInfo(Variant::INT, "patch_margin_right", PROPERTY_HINT_RANGE, "0,16384,1"), "set_patch_margin", "get_patch_margin", MARGIN_RIGHT); + ADD_PROPERTYINZ(PropertyInfo(Variant::INT, "patch_margin_bottom", PROPERTY_HINT_RANGE, "0,16384,1"), "set_patch_margin", "get_patch_margin", MARGIN_BOTTOM); + ADD_GROUP("Axis Stretch", "axis_stretch_"); + ADD_PROPERTYNZ(PropertyInfo(Variant::INT, "axis_stretch_horizontal", PROPERTY_HINT_ENUM, "Stretch,Tile,Tile Fit"), "set_h_axis_stretch_mode", "get_h_axis_stretch_mode"); + ADD_PROPERTYNZ(PropertyInfo(Variant::INT, "axis_stretch_vertical", PROPERTY_HINT_ENUM, "Stretch,Tile,Tile Fit"), "set_v_axis_stretch_mode", "get_v_axis_stretch_mode"); + + BIND_ENUM_CONSTANT(AXIS_STRETCH_MODE_STRETCH); + BIND_ENUM_CONSTANT(AXIS_STRETCH_MODE_TILE); + BIND_ENUM_CONSTANT(AXIS_STRETCH_MODE_TILE_FIT); +} + +void NinePatchRect::set_texture(const Ref &p_tex) { + + if (texture == p_tex) + return; + texture = p_tex; + update(); + /* + if (texture.is_valid()) + texture->set_flags(texture->get_flags()&(~Texture::FLAG_REPEAT)); //remove repeat from texture, it looks bad in sprites + */ + minimum_size_changed(); + emit_signal("texture_changed"); + _change_notify("texture"); +} + +Ref NinePatchRect::get_texture() const { + + return texture; +} + +void NinePatchRect::set_patch_margin(Margin p_margin, int p_size) { + + ERR_FAIL_INDEX(p_margin, 4); + margin[p_margin] = p_size; + update(); + minimum_size_changed(); + switch (p_margin) { + case MARGIN_LEFT: + _change_notify("patch_margin_left"); + break; + case MARGIN_TOP: + _change_notify("patch_margin_top"); + break; + case MARGIN_RIGHT: + _change_notify("patch_margin_right"); + break; + case MARGIN_BOTTOM: + _change_notify("patch_margin_bottom"); + break; + } +} + +int NinePatchRect::get_patch_margin(Margin p_margin) const { + + ERR_FAIL_INDEX_V(p_margin, 4, 0); + return margin[p_margin]; +} + +void NinePatchRect::set_region_rect(const Rect2 &p_region_rect) { + + if (region_rect == p_region_rect) + return; + + region_rect = p_region_rect; + + item_rect_changed(); + _change_notify("region_rect"); +} + +Rect2 NinePatchRect::get_region_rect() const { + + return region_rect; +} + +void NinePatchRect::set_draw_center(bool p_enabled) { + + draw_center = p_enabled; + update(); +} + +bool NinePatchRect::is_draw_center_enabled() const { + + return draw_center; +} + +void NinePatchRect::set_h_axis_stretch_mode(AxisStretchMode p_mode) { + axis_h = p_mode; + update(); +} + +NinePatchRect::AxisStretchMode NinePatchRect::get_h_axis_stretch_mode() const { + return axis_h; +} + +void NinePatchRect::set_v_axis_stretch_mode(AxisStretchMode p_mode) { + + axis_v = p_mode; + update(); +} + +NinePatchRect::AxisStretchMode NinePatchRect::get_v_axis_stretch_mode() const { + + return axis_v; +} + +NinePatchRect::NinePatchRect() { + + margin[MARGIN_LEFT] = 0; + margin[MARGIN_RIGHT] = 0; + margin[MARGIN_BOTTOM] = 0; + margin[MARGIN_TOP] = 0; + + set_mouse_filter(MOUSE_FILTER_IGNORE); + draw_center = true; + + axis_h = AXIS_STRETCH_MODE_STRETCH; + axis_v = AXIS_STRETCH_MODE_STRETCH; +} + +NinePatchRect::~NinePatchRect() { +} diff --git a/scene/gui/nine_patch_rect.h b/scene/gui/nine_patch_rect.h new file mode 100644 index 0000000000..809daf9db3 --- /dev/null +++ b/scene/gui/nine_patch_rect.h @@ -0,0 +1,84 @@ +/*************************************************************************/ +/* nine_patch_rect.h */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md) */ +/* */ +/* Permission is hereby granted, free of charge, to any person obtaining */ +/* a copy of this software and associated documentation files (the */ +/* "Software"), to deal in the Software without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of the Software, and to */ +/* permit persons to whom the Software is furnished to do so, subject to */ +/* the following conditions: */ +/* */ +/* The above copyright notice and this permission notice shall be */ +/* included in all copies or substantial portions of the Software. */ +/* */ +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ +/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ +/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ +/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ +/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/*************************************************************************/ +#ifndef NINE_PATCH_RECT_H +#define NINE_PATCH_RECT_H + +#include "scene/gui/control.h" +/** + @author Juan Linietsky +*/ +class NinePatchRect : public Control { + + GDCLASS(NinePatchRect, Control); + +public: + enum AxisStretchMode { + AXIS_STRETCH_MODE_STRETCH, + AXIS_STRETCH_MODE_TILE, + AXIS_STRETCH_MODE_TILE_FIT, + }; + + bool draw_center; + int margin[4]; + Rect2 region_rect; + Ref texture; + + AxisStretchMode axis_h, axis_v; + +protected: + void _notification(int p_what); + virtual Size2 get_minimum_size() const; + static void _bind_methods(); + +public: + void set_texture(const Ref &p_tex); + Ref get_texture() const; + + void set_patch_margin(Margin p_margin, int p_size); + int get_patch_margin(Margin p_margin) const; + + void set_region_rect(const Rect2 &p_region_rect); + Rect2 get_region_rect() const; + + void set_draw_center(bool p_enabled); + bool is_draw_center_enabled() const; + + void set_h_axis_stretch_mode(AxisStretchMode p_mode); + AxisStretchMode get_h_axis_stretch_mode() const; + + void set_v_axis_stretch_mode(AxisStretchMode p_mode); + AxisStretchMode get_v_axis_stretch_mode() const; + + NinePatchRect(); + ~NinePatchRect(); +}; + +VARIANT_ENUM_CAST(NinePatchRect::AxisStretchMode) +#endif // NINE_PATCH_RECT_H diff --git a/scene/gui/patch_9_rect.cpp b/scene/gui/patch_9_rect.cpp deleted file mode 100644 index 92c34dd3f9..0000000000 --- a/scene/gui/patch_9_rect.cpp +++ /dev/null @@ -1,200 +0,0 @@ -/*************************************************************************/ -/* patch_9_rect.cpp */ -/*************************************************************************/ -/* This file is part of: */ -/* GODOT ENGINE */ -/* https://godotengine.org */ -/*************************************************************************/ -/* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md) */ -/* */ -/* Permission is hereby granted, free of charge, to any person obtaining */ -/* a copy of this software and associated documentation files (the */ -/* "Software"), to deal in the Software without restriction, including */ -/* without limitation the rights to use, copy, modify, merge, publish, */ -/* distribute, sublicense, and/or sell copies of the Software, and to */ -/* permit persons to whom the Software is furnished to do so, subject to */ -/* the following conditions: */ -/* */ -/* The above copyright notice and this permission notice shall be */ -/* included in all copies or substantial portions of the Software. */ -/* */ -/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ -/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ -/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ -/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ -/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ -/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ -/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -/*************************************************************************/ -#include "patch_9_rect.h" - -#include "servers/visual_server.h" - -void NinePatchRect::_notification(int p_what) { - - if (p_what == NOTIFICATION_DRAW) { - - if (texture.is_null()) - return; - - Rect2 rect = Rect2(Point2(), get_size()); - Rect2 src_rect = region_rect; - - texture->get_rect_region(rect, src_rect, rect, src_rect); - - RID ci = get_canvas_item(); - 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); - } -} - -Size2 NinePatchRect::get_minimum_size() const { - - return Size2(margin[MARGIN_LEFT] + margin[MARGIN_RIGHT], margin[MARGIN_TOP] + margin[MARGIN_BOTTOM]); -} -void NinePatchRect::_bind_methods() { - - ClassDB::bind_method(D_METHOD("set_texture", "texture"), &NinePatchRect::set_texture); - ClassDB::bind_method(D_METHOD("get_texture"), &NinePatchRect::get_texture); - ClassDB::bind_method(D_METHOD("set_patch_margin", "margin", "value"), &NinePatchRect::set_patch_margin); - ClassDB::bind_method(D_METHOD("get_patch_margin", "margin"), &NinePatchRect::get_patch_margin); - ClassDB::bind_method(D_METHOD("set_region_rect", "rect"), &NinePatchRect::set_region_rect); - ClassDB::bind_method(D_METHOD("get_region_rect"), &NinePatchRect::get_region_rect); - ClassDB::bind_method(D_METHOD("set_draw_center", "draw_center"), &NinePatchRect::set_draw_center); - ClassDB::bind_method(D_METHOD("is_draw_center_enabled"), &NinePatchRect::is_draw_center_enabled); - ClassDB::bind_method(D_METHOD("set_h_axis_stretch_mode", "mode"), &NinePatchRect::set_h_axis_stretch_mode); - ClassDB::bind_method(D_METHOD("get_h_axis_stretch_mode"), &NinePatchRect::get_h_axis_stretch_mode); - ClassDB::bind_method(D_METHOD("set_v_axis_stretch_mode", "mode"), &NinePatchRect::set_v_axis_stretch_mode); - ClassDB::bind_method(D_METHOD("get_v_axis_stretch_mode"), &NinePatchRect::get_v_axis_stretch_mode); - - ADD_SIGNAL(MethodInfo("texture_changed")); - - ADD_PROPERTYNZ(PropertyInfo(Variant::OBJECT, "texture", PROPERTY_HINT_RESOURCE_TYPE, "Texture"), "set_texture", "get_texture"); - ADD_PROPERTYNO(PropertyInfo(Variant::BOOL, "draw_center"), "set_draw_center", "is_draw_center_enabled"); - ADD_PROPERTYNZ(PropertyInfo(Variant::RECT2, "region_rect"), "set_region_rect", "get_region_rect"); - - ADD_GROUP("Patch Margin", "patch_margin_"); - ADD_PROPERTYINZ(PropertyInfo(Variant::INT, "patch_margin_left", PROPERTY_HINT_RANGE, "0,16384,1"), "set_patch_margin", "get_patch_margin", MARGIN_LEFT); - ADD_PROPERTYINZ(PropertyInfo(Variant::INT, "patch_margin_top", PROPERTY_HINT_RANGE, "0,16384,1"), "set_patch_margin", "get_patch_margin", MARGIN_TOP); - ADD_PROPERTYINZ(PropertyInfo(Variant::INT, "patch_margin_right", PROPERTY_HINT_RANGE, "0,16384,1"), "set_patch_margin", "get_patch_margin", MARGIN_RIGHT); - ADD_PROPERTYINZ(PropertyInfo(Variant::INT, "patch_margin_bottom", PROPERTY_HINT_RANGE, "0,16384,1"), "set_patch_margin", "get_patch_margin", MARGIN_BOTTOM); - ADD_GROUP("Axis Stretch", "axis_stretch_"); - ADD_PROPERTYNZ(PropertyInfo(Variant::INT, "axis_stretch_horizontal", PROPERTY_HINT_ENUM, "Stretch,Tile,Tile Fit"), "set_h_axis_stretch_mode", "get_h_axis_stretch_mode"); - ADD_PROPERTYNZ(PropertyInfo(Variant::INT, "axis_stretch_vertical", PROPERTY_HINT_ENUM, "Stretch,Tile,Tile Fit"), "set_v_axis_stretch_mode", "get_v_axis_stretch_mode"); - - BIND_ENUM_CONSTANT(AXIS_STRETCH_MODE_STRETCH); - BIND_ENUM_CONSTANT(AXIS_STRETCH_MODE_TILE); - BIND_ENUM_CONSTANT(AXIS_STRETCH_MODE_TILE_FIT); -} - -void NinePatchRect::set_texture(const Ref &p_tex) { - - if (texture == p_tex) - return; - texture = p_tex; - update(); - /* - if (texture.is_valid()) - texture->set_flags(texture->get_flags()&(~Texture::FLAG_REPEAT)); //remove repeat from texture, it looks bad in sprites - */ - minimum_size_changed(); - emit_signal("texture_changed"); - _change_notify("texture"); -} - -Ref NinePatchRect::get_texture() const { - - return texture; -} - -void NinePatchRect::set_patch_margin(Margin p_margin, int p_size) { - - ERR_FAIL_INDEX(p_margin, 4); - margin[p_margin] = p_size; - update(); - minimum_size_changed(); - switch (p_margin) { - case MARGIN_LEFT: - _change_notify("patch_margin_left"); - break; - case MARGIN_TOP: - _change_notify("patch_margin_top"); - break; - case MARGIN_RIGHT: - _change_notify("patch_margin_right"); - break; - case MARGIN_BOTTOM: - _change_notify("patch_margin_bottom"); - break; - } -} - -int NinePatchRect::get_patch_margin(Margin p_margin) const { - - ERR_FAIL_INDEX_V(p_margin, 4, 0); - return margin[p_margin]; -} - -void NinePatchRect::set_region_rect(const Rect2 &p_region_rect) { - - if (region_rect == p_region_rect) - return; - - region_rect = p_region_rect; - - item_rect_changed(); - _change_notify("region_rect"); -} - -Rect2 NinePatchRect::get_region_rect() const { - - return region_rect; -} - -void NinePatchRect::set_draw_center(bool p_enabled) { - - draw_center = p_enabled; - update(); -} - -bool NinePatchRect::is_draw_center_enabled() const { - - return draw_center; -} - -void NinePatchRect::set_h_axis_stretch_mode(AxisStretchMode p_mode) { - axis_h = p_mode; - update(); -} - -NinePatchRect::AxisStretchMode NinePatchRect::get_h_axis_stretch_mode() const { - return axis_h; -} - -void NinePatchRect::set_v_axis_stretch_mode(AxisStretchMode p_mode) { - - axis_v = p_mode; - update(); -} - -NinePatchRect::AxisStretchMode NinePatchRect::get_v_axis_stretch_mode() const { - - return axis_v; -} - -NinePatchRect::NinePatchRect() { - - margin[MARGIN_LEFT] = 0; - margin[MARGIN_RIGHT] = 0; - margin[MARGIN_BOTTOM] = 0; - margin[MARGIN_TOP] = 0; - - set_mouse_filter(MOUSE_FILTER_IGNORE); - draw_center = true; - - axis_h = AXIS_STRETCH_MODE_STRETCH; - axis_v = AXIS_STRETCH_MODE_STRETCH; -} - -NinePatchRect::~NinePatchRect() { -} diff --git a/scene/gui/patch_9_rect.h b/scene/gui/patch_9_rect.h deleted file mode 100644 index 808b7a1f5d..0000000000 --- a/scene/gui/patch_9_rect.h +++ /dev/null @@ -1,84 +0,0 @@ -/*************************************************************************/ -/* patch_9_rect.h */ -/*************************************************************************/ -/* This file is part of: */ -/* GODOT ENGINE */ -/* https://godotengine.org */ -/*************************************************************************/ -/* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md) */ -/* */ -/* Permission is hereby granted, free of charge, to any person obtaining */ -/* a copy of this software and associated documentation files (the */ -/* "Software"), to deal in the Software without restriction, including */ -/* without limitation the rights to use, copy, modify, merge, publish, */ -/* distribute, sublicense, and/or sell copies of the Software, and to */ -/* permit persons to whom the Software is furnished to do so, subject to */ -/* the following conditions: */ -/* */ -/* The above copyright notice and this permission notice shall be */ -/* included in all copies or substantial portions of the Software. */ -/* */ -/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ -/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ -/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ -/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ -/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ -/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ -/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -/*************************************************************************/ -#ifndef PATCH_9_FRAME_H -#define PATCH_9_FRAME_H - -#include "scene/gui/control.h" -/** - @author Juan Linietsky -*/ -class NinePatchRect : public Control { - - GDCLASS(NinePatchRect, Control); - -public: - enum AxisStretchMode { - AXIS_STRETCH_MODE_STRETCH, - AXIS_STRETCH_MODE_TILE, - AXIS_STRETCH_MODE_TILE_FIT, - }; - - bool draw_center; - int margin[4]; - Rect2 region_rect; - Ref texture; - - AxisStretchMode axis_h, axis_v; - -protected: - void _notification(int p_what); - virtual Size2 get_minimum_size() const; - static void _bind_methods(); - -public: - void set_texture(const Ref &p_tex); - Ref get_texture() const; - - void set_patch_margin(Margin p_margin, int p_size); - int get_patch_margin(Margin p_margin) const; - - void set_region_rect(const Rect2 &p_region_rect); - Rect2 get_region_rect() const; - - void set_draw_center(bool p_enabled); - bool is_draw_center_enabled() const; - - void set_h_axis_stretch_mode(AxisStretchMode p_mode); - AxisStretchMode get_h_axis_stretch_mode() const; - - void set_v_axis_stretch_mode(AxisStretchMode p_mode); - AxisStretchMode get_v_axis_stretch_mode() const; - - NinePatchRect(); - ~NinePatchRect(); -}; - -VARIANT_ENUM_CAST(NinePatchRect::AxisStretchMode) -#endif // PATCH_9_FRAME_H diff --git a/scene/register_scene_types.cpp b/scene/register_scene_types.cpp index 3bdfe097c9..4d3e2c709c 100644 --- a/scene/register_scene_types.cpp +++ b/scene/register_scene_types.cpp @@ -86,7 +86,7 @@ #include "scene/gui/option_button.h" #include "scene/gui/panel.h" #include "scene/gui/panel_container.h" -#include "scene/gui/patch_9_rect.h" +#include "scene/gui/nine_patch_rect.h" #include "scene/gui/popup_menu.h" #include "scene/gui/progress_bar.h" #include "scene/gui/reference_rect.h" -- cgit v1.2.3