Enhanced StyleBoxTexture:

Add texture region support for StyleBoxTexture.
  Quick region(texture region and scale region) selection for StyleBoxTexture with TextureRegionEditorPlugin.
This commit is contained in:
Geequlim 2016-06-05 00:40:53 +08:00
parent 9546b2ffd4
commit 5894060abb
4 changed files with 130 additions and 69 deletions

View file

@ -138,7 +138,7 @@ void StyleBoxTexture::draw(RID p_canvas_item,const Rect2& p_rect) const {
r.pos.y-=expand_margin[MARGIN_TOP];
r.size.x+=expand_margin[MARGIN_LEFT]+expand_margin[MARGIN_RIGHT];
r.size.y+=expand_margin[MARGIN_TOP]+expand_margin[MARGIN_BOTTOM];
VisualServer::get_singleton()->canvas_item_add_style_box( p_canvas_item,r,Rect2(),texture->get_rid(),Vector2(margin[MARGIN_LEFT],margin[MARGIN_TOP]),Vector2(margin[MARGIN_RIGHT],margin[MARGIN_BOTTOM]),draw_center);
VisualServer::get_singleton()->canvas_item_add_style_box( p_canvas_item,r,region_rect,texture->get_rid(),Vector2(margin[MARGIN_LEFT],margin[MARGIN_TOP]),Vector2(margin[MARGIN_RIGHT],margin[MARGIN_BOTTOM]),draw_center);
}
void StyleBoxTexture::set_draw_center(bool p_draw) {
@ -175,6 +175,20 @@ float StyleBoxTexture::get_expand_margin_size(Margin p_expand_margin) const {
return expand_margin[p_expand_margin];
}
void StyleBoxTexture::set_region_rect(const Rect2& p_region_rect) {
if (region_rect==p_region_rect)
return;
region_rect=p_region_rect;
emit_changed();
}
Rect2 StyleBoxTexture::get_region_rect() const {
return region_rect;
}
void StyleBoxTexture::_bind_methods() {
@ -187,10 +201,14 @@ void StyleBoxTexture::_bind_methods() {
ObjectTypeDB::bind_method(_MD("set_expand_margin_size","margin","size"),&StyleBoxTexture::set_expand_margin_size);
ObjectTypeDB::bind_method(_MD("get_expand_margin_size","margin"),&StyleBoxTexture::get_expand_margin_size);
ObjectTypeDB::bind_method(_MD("set_region_rect","region"),&StyleBoxTexture::set_region_rect);
ObjectTypeDB::bind_method(_MD("get_region_rect"),&StyleBoxTexture::get_region_rect);
ObjectTypeDB::bind_method(_MD("set_draw_center","enable"),&StyleBoxTexture::set_draw_center);
ObjectTypeDB::bind_method(_MD("get_draw_center"),&StyleBoxTexture::get_draw_center);
ADD_PROPERTY( PropertyInfo( Variant::OBJECT, "texture", PROPERTY_HINT_RESOURCE_TYPE, "Texture" ), _SCS("set_texture"),_SCS("get_texture") );
ADD_PROPERTYNZ( PropertyInfo( Variant::RECT2, "region_rect"), _SCS("set_region_rect"),_SCS("get_region_rect"));
ADD_PROPERTYI( PropertyInfo( Variant::REAL, "margin/left", PROPERTY_HINT_RANGE,"0,2048,1" ), _SCS("set_margin_size"),_SCS("get_margin_size"), MARGIN_LEFT );
ADD_PROPERTYI( PropertyInfo( Variant::REAL, "margin/right", PROPERTY_HINT_RANGE,"0,2048,1" ), _SCS("set_margin_size"),_SCS("get_margin_size"), MARGIN_RIGHT );
ADD_PROPERTYI( PropertyInfo( Variant::REAL, "margin/top", PROPERTY_HINT_RANGE,"0,2048,1" ), _SCS("set_margin_size"),_SCS("get_margin_size"), MARGIN_TOP);

View file

@ -81,6 +81,7 @@ class StyleBoxTexture : public StyleBox {
float expand_margin[4];
float margin[4];
Rect2 region_rect;
Ref<Texture> texture;
bool draw_center;
@ -98,6 +99,9 @@ public:
void set_margin_size(Margin p_margin,float p_size);
float get_margin_size(Margin p_margin) const;
void set_region_rect(const Rect2& p_region_rect);
Rect2 get_region_rect() const;
void set_texture(RES p_texture);
RES get_texture() const;

View file

@ -41,6 +41,8 @@ void TextureRegionEditor::_region_draw()
base_tex = node_sprite->get_texture();
else if(node_type == "Patch9Frame" && node_patch9)
base_tex = node_patch9->get_texture();
else if(node_type == "StyleBoxTexture" && obj_styleBox)
base_tex = obj_styleBox->get_texture();
if (base_tex.is_null())
return;
@ -164,6 +166,8 @@ void TextureRegionEditor::_region_input(const InputEvent& p_input)
rect_prev=node_sprite->get_region_rect();
else if(node_type == "Patch9Frame" && node_patch9)
rect_prev=node_patch9->get_region_rect();
else if(node_type == "StyleBoxTexture" && obj_styleBox)
rect_prev=obj_styleBox->get_region_rect();
drag_index=-1;
for(int i=0;i<8;i++) {
@ -184,12 +188,12 @@ void TextureRegionEditor::_region_input(const InputEvent& p_input)
if(editing_region == REGION_TEXTURE_REGION) {
undo_redo->create_action("Set region_rect");
if(node_type == "Sprite" && node_sprite ){
undo_redo->add_do_method(node_sprite ,"set_region_rect",node_sprite->get_region_rect());
undo_redo->add_undo_method(node_sprite,"set_region_rect",rect_prev);
undo_redo->add_do_method(node_sprite ,"set_region_rect",node_sprite->get_region_rect());
undo_redo->add_undo_method(node_sprite,"set_region_rect",rect_prev);
}
else if(node_type == "Patch9Frame" && 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);
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);
}
undo_redo->add_do_method(edit_draw,"update");
undo_redo->add_undo_method(edit_draw,"update");
@ -240,53 +244,53 @@ void TextureRegionEditor::_region_input(const InputEvent& p_input)
switch(drag_index) {
case 0: {
Vector2 p=rect_prev.pos+rect_prev.size;
rect = Rect2(p,Size2());
rect.expand_to(new_pos);
apply_rect(rect);
} break;
Vector2 p=rect_prev.pos+rect_prev.size;
rect = Rect2(p,Size2());
rect.expand_to(new_pos);
apply_rect(rect);
} break;
case 1: {
Vector2 p=rect_prev.pos+Vector2(0,rect_prev.size.y);
rect = Rect2(p,Size2(rect_prev.size.x,0));
rect.expand_to(new_pos);
apply_rect(rect);
} break;
Vector2 p=rect_prev.pos+Vector2(0,rect_prev.size.y);
rect = Rect2(p,Size2(rect_prev.size.x,0));
rect.expand_to(new_pos);
apply_rect(rect);
} break;
case 2: {
Vector2 p=rect_prev.pos+Vector2(0,rect_prev.size.y);
rect = Rect2(p,Size2());
rect.expand_to(new_pos);
apply_rect(rect);
} break;
Vector2 p=rect_prev.pos+Vector2(0,rect_prev.size.y);
rect = Rect2(p,Size2());
rect.expand_to(new_pos);
apply_rect(rect);
} break;
case 3: {
Vector2 p=rect_prev.pos;
rect = Rect2(p,Size2(0,rect_prev.size.y));
rect.expand_to(new_pos);
apply_rect(rect);
} break;
Vector2 p=rect_prev.pos;
rect = Rect2(p,Size2(0,rect_prev.size.y));
rect.expand_to(new_pos);
apply_rect(rect);
} break;
case 4: {
Vector2 p=rect_prev.pos;
rect = Rect2(p,Size2());
rect.expand_to(new_pos);
apply_rect(rect);
} break;
Vector2 p=rect_prev.pos;
rect = Rect2(p,Size2());
rect.expand_to(new_pos);
apply_rect(rect);
} break;
case 5: {
Vector2 p=rect_prev.pos;
rect = Rect2(p,Size2(rect_prev.size.x,0));
rect.expand_to(new_pos);
apply_rect(rect);
} break;
Vector2 p=rect_prev.pos;
rect = Rect2(p,Size2(rect_prev.size.x,0));
rect.expand_to(new_pos);
apply_rect(rect);
} break;
case 6: {
Vector2 p=rect_prev.pos+Vector2(rect_prev.size.x,0);
rect = Rect2(p,Size2());
rect.expand_to(new_pos);
apply_rect(rect);
} break;
Vector2 p=rect_prev.pos+Vector2(rect_prev.size.x,0);
rect = Rect2(p,Size2());
rect.expand_to(new_pos);
apply_rect(rect);
} break;
case 7: {
Vector2 p=rect_prev.pos+Vector2(rect_prev.size.x,0);
rect = Rect2(p,Size2(0,rect_prev.size.y));
rect.expand_to(new_pos);
apply_rect(rect);
} break;
Vector2 p=rect_prev.pos+Vector2(rect_prev.size.x,0);
rect = Rect2(p,Size2(0,rect_prev.size.y));
rect.expand_to(new_pos);
apply_rect(rect);
} break;
}
edit_draw->update();
@ -349,19 +353,29 @@ void TextureRegionEditor::apply_rect(const Rect2& rect){
node_sprite->set_region_rect(rect);
else if(node_patch9)
node_patch9->set_region_rect(rect);
else if(obj_styleBox)
obj_styleBox->set_region_rect(rect);
}
else if(node_patch9 && this->editing_region == REGION_PATCH_MARGIN) {
node_patch9->set_patch_margin(MARGIN_LEFT, rect.pos.x - tex_region.pos.x);
node_patch9->set_patch_margin(MARGIN_RIGHT, tex_region.pos.x+tex_region.size.width-(rect.pos.x+rect.size.width));
node_patch9->set_patch_margin(MARGIN_TOP, rect.pos.y - tex_region.pos.y);
node_patch9->set_patch_margin(MARGIN_BOTTOM, tex_region.pos.y+tex_region.size.height-(rect.pos.y+rect.size.height));
else if(this->editing_region == REGION_PATCH_MARGIN) {
if(node_patch9) {
node_patch9->set_patch_margin(MARGIN_LEFT, rect.pos.x - tex_region.pos.x);
node_patch9->set_patch_margin(MARGIN_RIGHT, tex_region.pos.x+tex_region.size.width-(rect.pos.x+rect.size.width));
node_patch9->set_patch_margin(MARGIN_TOP, rect.pos.y - tex_region.pos.y);
node_patch9->set_patch_margin(MARGIN_BOTTOM, tex_region.pos.y+tex_region.size.height-(rect.pos.y+rect.size.height));
}
else if(obj_styleBox) {
obj_styleBox->set_margin_size(MARGIN_LEFT, rect.pos.x - tex_region.pos.x);
obj_styleBox->set_margin_size(MARGIN_RIGHT, tex_region.pos.x+tex_region.size.width-(rect.pos.x+rect.size.width));
obj_styleBox->set_margin_size(MARGIN_TOP, rect.pos.y - tex_region.pos.y);
obj_styleBox->set_margin_size(MARGIN_BOTTOM, tex_region.pos.y+tex_region.size.height-(rect.pos.y+rect.size.height));
}
}
}
void TextureRegionEditor::_notification(int p_what)
{
switch(p_what) {
case NOTIFICATION_READY: {
case NOTIFICATION_READY: {
region_button->set_icon( get_icon("RegionEdit","EditorIcons"));
margin_button->set_icon( get_icon("Patch9Frame", "EditorIcons"));
b_snap_grid->set_icon( get_icon("Grid", "EditorIcons"));
@ -371,11 +385,12 @@ void TextureRegionEditor::_notification(int p_what)
}
}
void TextureRegionEditor::_node_removed(Node *p_node)
void TextureRegionEditor::_node_removed(Object *p_obj)
{
if(p_node == node_sprite || p_node == node_patch9) {
if(p_obj == node_sprite || p_obj == node_patch9 || p_obj == obj_styleBox) {
node_patch9 = NULL;
node_sprite = NULL;
obj_styleBox = NULL;
hide();
}
}
@ -397,28 +412,39 @@ void TextureRegionEditor::_bind_methods()
ObjectTypeDB::bind_method(_MD("_set_snap_step_y"),&TextureRegionEditor::_set_snap_step_y);
}
void TextureRegionEditor::edit(Node *p_node)
void TextureRegionEditor::edit(Object *p_obj)
{
if (p_node) {
if (p_obj) {
margin_button->hide();
node_type = p_node->get_type();
node_type = p_obj->get_type();
if(node_type == "Sprite"){
node_sprite = p_node->cast_to<Sprite>();
node_sprite = p_obj->cast_to<Sprite>();
node_patch9 = NULL;
obj_styleBox = NULL;
}
else if(node_type == "Patch9Frame") {
node_patch9 = p_node->cast_to<Patch9Frame>();
node_patch9 = p_obj->cast_to<Patch9Frame>();
node_sprite = NULL;
obj_styleBox = NULL;
margin_button->show();
}
p_node->connect("exit_tree",this,"_node_removed",varray(p_node),CONNECT_ONESHOT);
else if(node_type == "StyleBoxTexture") {
obj_styleBox = p_obj->cast_to<StyleBoxTexture>();
node_sprite = NULL;
node_patch9 = NULL;
margin_button->show();
}
p_obj->connect("exit_tree",this,"_node_removed",varray(p_obj),CONNECT_ONESHOT);
} else {
if(node_sprite)
node_sprite->disconnect("exit_tree",this,"_node_removed");
else if(node_patch9)
node_patch9->disconnect("exit_tree",this,"_node_removed");
else if(obj_styleBox)
obj_styleBox->disconnect("exit_tree",this,"_node_removed");
node_sprite = NULL;
node_patch9 = NULL;
obj_styleBox = NULL;
}
}
@ -441,6 +467,8 @@ void TextureRegionEditor::_edit_node(int region)
texture = node_sprite->get_texture();
else if(node_type == "Patch9Frame" && node_patch9 )
texture = node_patch9->get_texture();
else if(node_type == "StyleBoxTexture" && obj_styleBox)
texture = obj_styleBox->get_texture();
if (texture.is_null()) {
error->set_text(TTR("No texture in this node.\nSet a texture to be able to edit region."));
@ -452,12 +480,21 @@ void TextureRegionEditor::_edit_node(int region)
tex_region = node_sprite->get_region_rect();
else if(node_type == "Patch9Frame" && node_patch9 )
tex_region = node_patch9->get_region_rect();
else if(node_type == "StyleBoxTexture" && obj_styleBox)
tex_region = obj_styleBox->get_region_rect();
rect = tex_region;
if(region == REGION_PATCH_MARGIN && node_patch9) {
Patch9Frame *node = node_patch9->cast_to<Patch9Frame>();
rect.pos += Point2(node->get_patch_margin(MARGIN_LEFT),node->get_patch_margin(MARGIN_TOP));
rect.size -= Size2(node->get_patch_margin(MARGIN_RIGHT)+node->get_patch_margin(MARGIN_LEFT), node->get_patch_margin(MARGIN_BOTTOM)+node->get_patch_margin(MARGIN_TOP));
if(region == REGION_PATCH_MARGIN) {
if(node_patch9){
Patch9Frame *node = node_patch9;
rect.pos += Point2(node->get_patch_margin(MARGIN_LEFT),node->get_patch_margin(MARGIN_TOP));
rect.size -= Size2(node->get_patch_margin(MARGIN_RIGHT)+node->get_patch_margin(MARGIN_LEFT), node->get_patch_margin(MARGIN_BOTTOM)+node->get_patch_margin(MARGIN_TOP));
}
else if(obj_styleBox) {
StyleBoxTexture * node = obj_styleBox;
rect.pos += Point2(node->get_margin_size(MARGIN_LEFT),node->get_margin_size(MARGIN_TOP));
rect.size -= Size2(node->get_margin_size(MARGIN_RIGHT)+node->get_margin_size(MARGIN_LEFT), node->get_margin_size(MARGIN_BOTTOM)+node->get_margin_size(MARGIN_TOP));
}
}
dlg_editor->popup_centered_ratio(0.85);
@ -619,12 +656,12 @@ TextureRegionEditor::TextureRegionEditor(EditorNode* p_editor)
void TextureRegionEditorPlugin::edit(Object *p_node)
{
region_editor->edit(p_node->cast_to<Node>());
region_editor->edit(p_node);
}
bool TextureRegionEditorPlugin::handles(Object *p_node) const
bool TextureRegionEditorPlugin::handles(Object *p_obj) const
{
return p_node->is_type("Sprite") || p_node->is_type("Patch9Frame");
return p_obj->is_type("Sprite") || p_obj->is_type("Patch9Frame") || p_obj->is_type("StyleBoxTexture");
}
void TextureRegionEditorPlugin::make_visible(bool p_visible)

View file

@ -37,6 +37,7 @@
#include "tools/editor/editor_node.h"
#include "scene/2d/sprite.h"
#include "scene/gui/patch_9_frame.h"
#include "scene/resources/style_box.h"
class TextureRegionEditor : public HBoxContainer {
@ -80,6 +81,8 @@ class TextureRegionEditor : public HBoxContainer {
String node_type;
Patch9Frame *node_patch9;
Sprite *node_sprite;
StyleBoxTexture *obj_styleBox;
int editing_region;
Rect2 rect;
Rect2 rect_prev;
@ -102,14 +105,13 @@ class TextureRegionEditor : public HBoxContainer {
protected:
void _notification(int p_what);
void _node_removed(Node *p_node);
void _node_removed(Object *p_obj);
static void _bind_methods();
Vector2 snap_point(Vector2 p_target) const;
public:
void edit();
void _edit_node(int tex_region);
void _edit_region();
void _edit_margin();
@ -117,7 +119,7 @@ public:
void _region_input(const InputEvent &p_input);
void _scroll_changed(float);
void edit(Node *p_sprite);
void edit(Object *p_obj);
TextureRegionEditor(EditorNode* p_editor);
};