diff options
author | RĂ©mi Verschelde <remi@verschelde.fr> | 2016-06-26 09:43:45 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2016-06-26 09:43:45 +0200 |
commit | b02d67be308a9f756a9447afe14ed276a18813d2 (patch) | |
tree | 4695473cf122dc1d703737d1d84b1490b3d23f6c | |
parent | 620da09b8714b446b2fc2fb0a9d897ce502f0377 (diff) | |
parent | 79c500bee1bb53164a78855419eca8543193f77f (diff) |
Merge pull request #5307 from MarianoGnu/master
TextureRegion improvements
-rw-r--r-- | scene/2d/sprite.cpp | 2 | ||||
-rw-r--r-- | scene/gui/patch_9_frame.cpp | 5 | ||||
-rw-r--r-- | scene/resources/style_box.cpp | 5 | ||||
-rw-r--r-- | scene/resources/texture.cpp | 5 | ||||
-rw-r--r-- | tools/editor/icons/2x/icon_zoom_less.png | bin | 0 -> 162 bytes | |||
-rw-r--r-- | tools/editor/icons/2x/icon_zoom_more.png | bin | 0 -> 290 bytes | |||
-rw-r--r-- | tools/editor/icons/2x/icon_zoom_reset.png | bin | 0 -> 689 bytes | |||
-rw-r--r-- | tools/editor/icons/icon_zoom_less.png | bin | 0 -> 149 bytes | |||
-rw-r--r-- | tools/editor/icons/icon_zoom_more.png | bin | 0 -> 232 bytes | |||
-rw-r--r-- | tools/editor/icons/icon_zoom_reset.png | bin | 0 -> 430 bytes | |||
-rw-r--r-- | tools/editor/icons/source/icon_zoom_more.svg | 104 | ||||
-rw-r--r-- | tools/editor/plugins/texture_region_editor_plugin.cpp | 891 | ||||
-rw-r--r-- | tools/editor/plugins/texture_region_editor_plugin.h | 57 |
13 files changed, 711 insertions, 358 deletions
diff --git a/scene/2d/sprite.cpp b/scene/2d/sprite.cpp index 3e6384ea2c..27aa08ec5b 100644 --- a/scene/2d/sprite.cpp +++ b/scene/2d/sprite.cpp @@ -120,6 +120,7 @@ void Sprite::set_texture(const Ref<Texture>& p_texture) { } #endif update(); + emit_signal("texture_changed"); item_rect_changed(); } @@ -323,6 +324,7 @@ void Sprite::_bind_methods() { ObjectTypeDB::bind_method(_MD("get_modulate"),&Sprite::get_modulate); ADD_SIGNAL(MethodInfo("frame_changed")); + ADD_SIGNAL(MethodInfo("texture_changed")); ADD_PROPERTYNZ( PropertyInfo( Variant::OBJECT, "texture", PROPERTY_HINT_RESOURCE_TYPE,"Texture"), _SCS("set_texture"),_SCS("get_texture")); ADD_PROPERTYNO( PropertyInfo( Variant::BOOL, "centered"), _SCS("set_centered"),_SCS("is_centered")); diff --git a/scene/gui/patch_9_frame.cpp b/scene/gui/patch_9_frame.cpp index a6a3490ad2..9ad6398359 100644 --- a/scene/gui/patch_9_frame.cpp +++ b/scene/gui/patch_9_frame.cpp @@ -79,6 +79,8 @@ void Patch9Frame::_bind_methods() { ObjectTypeDB::bind_method(_MD("set_draw_center","draw_center"), & Patch9Frame::set_draw_center ); ObjectTypeDB::bind_method(_MD("get_draw_center"), & Patch9Frame::get_draw_center ); + ADD_SIGNAL(MethodInfo("texture_changed")); + ADD_PROPERTYNZ( PropertyInfo( Variant::OBJECT, "texture", PROPERTY_HINT_RESOURCE_TYPE, "Texture"), _SCS("set_texture"),_SCS("get_texture") ); ADD_PROPERTYNO( PropertyInfo( Variant::COLOR, "modulate"), _SCS("set_modulate"),_SCS("get_modulate") ); ADD_PROPERTYNO( PropertyInfo( Variant::BOOL, "draw_center"), _SCS("set_draw_center"),_SCS("get_draw_center") ); @@ -93,11 +95,14 @@ void Patch9Frame::_bind_methods() { void Patch9Frame::set_texture(const Ref<Texture>& 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"); } Ref<Texture> Patch9Frame::get_texture() const { diff --git a/scene/resources/style_box.cpp b/scene/resources/style_box.cpp index a61ffe8e97..8580ffdc5a 100644 --- a/scene/resources/style_box.cpp +++ b/scene/resources/style_box.cpp @@ -104,7 +104,10 @@ StyleBox::StyleBox() { void StyleBoxTexture::set_texture(RES p_texture) { + if (texture==p_texture) + return; texture=p_texture; + emit_signal("texture_changed"); emit_changed(); } @@ -207,6 +210,8 @@ void StyleBoxTexture::_bind_methods() { ObjectTypeDB::bind_method(_MD("set_draw_center","enable"),&StyleBoxTexture::set_draw_center); ObjectTypeDB::bind_method(_MD("get_draw_center"),&StyleBoxTexture::get_draw_center); + ADD_SIGNAL(MethodInfo("texture_changed")); + 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 ); diff --git a/scene/resources/texture.cpp b/scene/resources/texture.cpp index 1893bfe524..2fa00c7da7 100644 --- a/scene/resources/texture.cpp +++ b/scene/resources/texture.cpp @@ -526,8 +526,11 @@ uint32_t AtlasTexture::get_flags() const{ void AtlasTexture::set_atlas(const Ref<Texture>& p_atlas){ + if (atlas==p_atlas) + return; atlas=p_atlas; emit_changed(); + emit_signal("atlas_changed"); } Ref<Texture> AtlasTexture::get_atlas() const{ @@ -569,6 +572,8 @@ void AtlasTexture::_bind_methods() { ObjectTypeDB::bind_method(_MD("set_margin","margin"),&AtlasTexture::set_margin); ObjectTypeDB::bind_method(_MD("get_margin"),&AtlasTexture::get_margin); + ADD_SIGNAL(MethodInfo("atlas_changed")); + ADD_PROPERTY( PropertyInfo( Variant::OBJECT, "atlas", PROPERTY_HINT_RESOURCE_TYPE,"Texture"), _SCS("set_atlas"),_SCS("get_atlas") ); ADD_PROPERTY( PropertyInfo( Variant::RECT2, "region"), _SCS("set_region"),_SCS("get_region") ); ADD_PROPERTY( PropertyInfo( Variant::RECT2, "margin"), _SCS("set_margin"),_SCS("get_margin") ); diff --git a/tools/editor/icons/2x/icon_zoom_less.png b/tools/editor/icons/2x/icon_zoom_less.png Binary files differnew file mode 100644 index 0000000000..d483db55ce --- /dev/null +++ b/tools/editor/icons/2x/icon_zoom_less.png diff --git a/tools/editor/icons/2x/icon_zoom_more.png b/tools/editor/icons/2x/icon_zoom_more.png Binary files differnew file mode 100644 index 0000000000..8f9ef77849 --- /dev/null +++ b/tools/editor/icons/2x/icon_zoom_more.png diff --git a/tools/editor/icons/2x/icon_zoom_reset.png b/tools/editor/icons/2x/icon_zoom_reset.png Binary files differnew file mode 100644 index 0000000000..092215b3e2 --- /dev/null +++ b/tools/editor/icons/2x/icon_zoom_reset.png diff --git a/tools/editor/icons/icon_zoom_less.png b/tools/editor/icons/icon_zoom_less.png Binary files differnew file mode 100644 index 0000000000..fd8ef9075e --- /dev/null +++ b/tools/editor/icons/icon_zoom_less.png diff --git a/tools/editor/icons/icon_zoom_more.png b/tools/editor/icons/icon_zoom_more.png Binary files differnew file mode 100644 index 0000000000..8e818db1ee --- /dev/null +++ b/tools/editor/icons/icon_zoom_more.png diff --git a/tools/editor/icons/icon_zoom_reset.png b/tools/editor/icons/icon_zoom_reset.png Binary files differnew file mode 100644 index 0000000000..fa8a9d197e --- /dev/null +++ b/tools/editor/icons/icon_zoom_reset.png diff --git a/tools/editor/icons/source/icon_zoom_more.svg b/tools/editor/icons/source/icon_zoom_more.svg new file mode 100644 index 0000000000..87acdfb021 --- /dev/null +++ b/tools/editor/icons/source/icon_zoom_more.svg @@ -0,0 +1,104 @@ +<?xml version="1.0" encoding="UTF-8" standalone="no"?> +<!-- Created with Inkscape (http://www.inkscape.org/) --> + +<svg + xmlns:dc="http://purl.org/dc/elements/1.1/" + xmlns:cc="http://creativecommons.org/ns#" + xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" + xmlns:svg="http://www.w3.org/2000/svg" + xmlns="http://www.w3.org/2000/svg" + xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd" + xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape" + width="16" + height="16" + viewBox="0 0 16 16" + id="svg2" + version="1.1" + inkscape:version="0.91 r13725" + inkscape:export-filename="/home/djrm/Projects/godot/tools/editor/icons/icon_zoom.png" + inkscape:export-xdpi="90" + inkscape:export-ydpi="90" + sodipodi:docname="icon_zoom_more.svg"> + <defs + id="defs4" /> + <sodipodi:namedview + id="base" + pagecolor="#ffffff" + bordercolor="#666666" + borderopacity="1.0" + inkscape:pageopacity="0.0" + inkscape:pageshadow="2" + inkscape:zoom="22.627417" + inkscape:cx="3.7772222" + inkscape:cy="13.690414" + inkscape:document-units="px" + inkscape:current-layer="layer1" + showgrid="true" + units="px" + inkscape:snap-bbox="true" + inkscape:bbox-paths="true" + inkscape:bbox-nodes="true" + inkscape:snap-bbox-edge-midpoints="true" + inkscape:snap-bbox-midpoints="false" + inkscape:snap-object-midpoints="true" + inkscape:snap-center="true" + inkscape:window-width="1920" + inkscape:window-height="1018" + inkscape:window-x="-8" + inkscape:window-y="-8" + inkscape:window-maximized="1"> + <inkscape:grid + type="xygrid" + id="grid3336" /> + </sodipodi:namedview> + <metadata + id="metadata7"> + <rdf:RDF> + <cc:Work + rdf:about=""> + <dc:format>image/svg+xml</dc:format> + <dc:type + rdf:resource="http://purl.org/dc/dcmitype/StillImage" /> + <dc:title></dc:title> + </cc:Work> + </rdf:RDF> + </metadata> + <g + inkscape:label="Layer 1" + inkscape:groupmode="layer" + id="layer1" + transform="translate(0,-1036.3622)"> + <g + id="layer1-6" + inkscape:label="Capa 1" + transform="translate(-201.5751,205.0256)"> + <g + inkscape:label="Layer 1" + id="layer1-0-4" + transform="matrix(48.459085,0,0,53.967813,-126.63031,-55835.691)"> + <g + transform="translate(0.51853114,-0.01988754)" + id="g4182-8"> + <rect + style="fill:#e0e0e0;fill-opacity:0.99607843" + id="rect4167" + width="0.042994563" + height="0.26204652" + x="6.3978949" + y="1050.0524" + rx="0" + ry="0" /> + <rect + ry="0" + rx="0" + y="1050.165" + x="6.2806396" + height="0.040943757" + width="0.28011176" + id="rect4169-6" + style="fill:#e0e0e0;fill-opacity:0.99607843" /> + </g> + </g> + </g> + </g> +</svg> diff --git a/tools/editor/plugins/texture_region_editor_plugin.cpp b/tools/editor/plugins/texture_region_editor_plugin.cpp index 57db19b736..8515008982 100644 --- a/tools/editor/plugins/texture_region_editor_plugin.cpp +++ b/tools/editor/plugins/texture_region_editor_plugin.cpp @@ -29,21 +29,30 @@ /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /*************************************************************************/ +#include "core/core_string_names.h" #include "texture_region_editor_plugin.h" #include "scene/gui/check_box.h" #include "os/input.h" #include "os/keyboard.h" +void draw_margin_line(Control *edit_draw, Vector2 from, Vector2 to){ + Vector2 line = (to-from).normalized() * 10; + while ((to - from).length_squared() > 200) { + edit_draw->draw_line(from, from + line,Color(0.97, 0.2, 0.2),2); + from += line*2; + } +} + void TextureRegionEditor::_region_draw() { Ref<Texture> base_tex = NULL; - if(node_type == "Sprite" && node_sprite) + if(node_sprite) base_tex = node_sprite->get_texture(); - else if(node_type == "Patch9Frame" && node_patch9) + else if(node_patch9) base_tex = node_patch9->get_texture(); - else if(node_type == "StyleBoxTexture" && obj_styleBox) + else if(obj_styleBox.is_valid()) base_tex = obj_styleBox->get_texture(); - else if(node_type == "AtlasTexture" && atlas_tex) + else if(atlas_tex.is_valid()) base_tex = atlas_tex->get_atlas(); if (base_tex.is_null()) return; @@ -57,29 +66,63 @@ void TextureRegionEditor::_region_draw() edit_draw->draw_texture(base_tex,Point2()); VS::get_singleton()->canvas_item_add_set_transform(edit_draw->get_canvas_item(),Matrix32()); - if (snap_show_grid) { + if (snap_mode == SNAP_GRID) { Size2 s = edit_draw->get_size(); int last_cell; if (snap_step.x!=0) { - for(int i=0;i<s.width;i++) { - int cell = Math::fast_ftoi(Math::floor((mtx.affine_inverse().xform(Vector2(i,0)).x-snap_offset.x)/snap_step.x)); - if (i==0) + if (snap_separation.x == 0) + for(int i=0;i<s.width;i++) { + int cell = Math::fast_ftoi(Math::floor((mtx.affine_inverse().xform(Vector2(i,0)).x-snap_offset.x)/snap_step.x)); + if (i==0) + last_cell=cell; + if (last_cell!=cell) + edit_draw->draw_line(Point2(i,0),Point2(i,s.height),Color(0.3,0.7,1,0.3)); last_cell=cell; - if (last_cell!=cell) - edit_draw->draw_line(Point2(i,0),Point2(i,s.height),Color(0.3,0.7,1,0.3)); - last_cell=cell; - } + } + else + for(int i=0;i<s.width;i++) { + int cell = Math::fast_ftoi(Math::floor((mtx.affine_inverse().xform(Vector2(i,0)).x-snap_offset.x)/(snap_step.x+snap_separation.x))); + if (i==0) + last_cell=cell; + if (last_cell!=cell) + edit_draw->draw_rect(Rect2(i-snap_separation.x*draw_zoom,0,snap_separation.x*draw_zoom,s.height),Color(0.3,0.7,1,0.3)); + last_cell=cell; + } } if (snap_step.y!=0) { - for(int i=0;i<s.height;i++) { - int cell = Math::fast_ftoi(Math::floor((mtx.affine_inverse().xform(Vector2(0,i)).y-snap_offset.y)/snap_step.y)); - if (i==0) + if (snap_separation.y == 0) + for(int i=0;i<s.height;i++) { + int cell = Math::fast_ftoi(Math::floor((mtx.affine_inverse().xform(Vector2(0,i)).y-snap_offset.y)/snap_step.y)); + if (i==0) + last_cell=cell; + if (last_cell!=cell) + edit_draw->draw_line(Point2(0,i),Point2(s.width,i),Color(0.3,0.7,1,0.3)); last_cell=cell; - if (last_cell!=cell) - edit_draw->draw_line(Point2(0,i),Point2(s.width,i),Color(0.3,0.7,1,0.3)); - last_cell=cell; + } + else + for(int i=0;i<s.height;i++) { + int cell = Math::fast_ftoi(Math::floor((mtx.affine_inverse().xform(Vector2(0,i)).y-snap_offset.y)/(snap_step.y+snap_separation.y))); + if (i==0) + last_cell=cell; + if (last_cell!=cell) + edit_draw->draw_rect(Rect2(0,i-snap_separation.y*draw_zoom,s.width,snap_separation.y*draw_zoom),Color(0.3,0.7,1,0.3)); + last_cell=cell; + } + } + } else if (snap_mode == SNAP_AUTOSLICE) { + for (List<Rect2>::Element *E = autoslice_cache.front();E;E=E->next()) { + Rect2 r = E->get(); + Vector2 endpoints[4]={ + mtx.basis_xform(r.pos), + mtx.basis_xform(r.pos+Vector2(r.size.x,0)), + mtx.basis_xform(r.pos+r.size), + mtx.basis_xform(r.pos+Vector2(0,r.size.y)) + }; + for(int i=0;i<4;i++) { + int next = (i+1)%4; + edit_draw->draw_line(endpoints[i]-draw_ofs, endpoints[next]-draw_ofs, Color(0.3,0.7,1,1) , 2); } } } @@ -96,8 +139,6 @@ void TextureRegionEditor::_region_draw() mtx.basis_xform(rect.pos+Vector2(0,rect.size.y)) }; Color color(0.9,0.5,0.5); - if(this->editing_region == REGION_PATCH_MARGIN) - color = Color(0.21, 0.79, 0.31); for(int i=0;i<4;i++) { int prev = (i+3)%4; @@ -108,12 +149,14 @@ void TextureRegionEditor::_region_draw() edit_draw->draw_line(endpoints[i]-draw_ofs, endpoints[next]-draw_ofs, color , 2); - edit_draw->draw_texture(select_handle,(endpoints[i]+ofs-(select_handle->get_size()/2)).floor()-draw_ofs); + if (snap_mode != SNAP_AUTOSLICE) + edit_draw->draw_texture(select_handle,(endpoints[i]+ofs-(select_handle->get_size()/2)).floor()-draw_ofs); ofs = (endpoints[next]-endpoints[i])/2; ofs += (endpoints[next]-endpoints[i]).tangent().normalized()*(select_handle->get_size().width/2); - edit_draw->draw_texture(select_handle,(endpoints[i]+ofs-(select_handle->get_size()/2)).floor()-draw_ofs); + if (snap_mode != SNAP_AUTOSLICE) + edit_draw->draw_texture(select_handle,(endpoints[i]+ofs-(select_handle->get_size()/2)).floor()-draw_ofs); scroll_rect.expand_to(endpoints[i]); } @@ -132,6 +175,31 @@ void TextureRegionEditor::_region_draw() vscroll->set_val(draw_ofs.y); vscroll->set_step(0.001); 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); + } else if (obj_styleBox.is_valid()) { + margins[0] = obj_styleBox->get_margin_size(MARGIN_TOP); + margins[1] = obj_styleBox->get_margin_size(MARGIN_BOTTOM); + margins[2] = obj_styleBox->get_margin_size(MARGIN_LEFT); + margins[3] = obj_styleBox->get_margin_size(MARGIN_RIGHT); + } + Vector2 pos[4] = { + mtx.basis_xform(Vector2(0,margins[0]))+Vector2(0,endpoints[0].y-draw_ofs.y), + -mtx.basis_xform(Vector2(0,margins[1]))+Vector2(0,endpoints[2].y-draw_ofs.y), + mtx.basis_xform(Vector2(margins[2],0))+Vector2(endpoints[0].x-draw_ofs.x,0), + -mtx.basis_xform(Vector2(margins[3],0))+Vector2(endpoints[2].x-draw_ofs.x,0)}; + + draw_margin_line(edit_draw,pos[0],pos[0]+Vector2(edit_draw->get_size().x,0)); + draw_margin_line(edit_draw,pos[1],pos[1]+Vector2(edit_draw->get_size().x,0)); + draw_margin_line(edit_draw,pos[2],pos[2]+Vector2(0,edit_draw->get_size().y)); + draw_margin_line(edit_draw,pos[3],pos[3]+Vector2(0,edit_draw->get_size().y)); + } } void TextureRegionEditor::_region_input(const InputEvent& p_input) @@ -153,84 +221,176 @@ void TextureRegionEditor::_region_input(const InputEvent& p_input) if (p_input.type==InputEvent::MOUSE_BUTTON) { - const InputEventMouseButton &mb=p_input.mouse_button; if (mb.button_index==BUTTON_LEFT) { - if (mb.pressed) { - - drag_from=mtx.affine_inverse().xform(Vector2(mb.x,mb.y)); - drag_from=snap_point(drag_from); - drag=true; - if(node_type == "Sprite" && node_sprite ) - rect_prev=node_sprite->get_region_rect(); - else if(node_type == "AtlasTexture" && atlas_tex) - rect_prev=atlas_tex->get_region(); - 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++) { - - Vector2 tuv=endpoints[i]; - if (tuv.distance_to(Vector2(mb.x,mb.y))<8) { - drag_index=i; - creating = false; + if (node_patch9 || 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); + } else if (obj_styleBox.is_valid()) { + margins[0] = obj_styleBox->get_margin_size(MARGIN_TOP); + margins[1] = obj_styleBox->get_margin_size(MARGIN_BOTTOM); + margins[2] = obj_styleBox->get_margin_size(MARGIN_LEFT); + margins[3] = obj_styleBox->get_margin_size(MARGIN_RIGHT); + } + Vector2 pos[4] = { + mtx.basis_xform(rect.pos+Vector2(0,margins[0]))-draw_ofs, + mtx.basis_xform(rect.pos+rect.size-Vector2(0,margins[1]))-draw_ofs, + mtx.basis_xform(rect.pos+Vector2(margins[2],0))-draw_ofs, + mtx.basis_xform(rect.pos+rect.size-Vector2(margins[3],0))-draw_ofs}; + if (Math::abs(mb.y - pos[0].y) < 8) { + edited_margin = 0; + prev_margin = margins[0]; + } else if (Math::abs(mb.y - pos[1].y) < 8) { + edited_margin = 1; + prev_margin = margins[1]; + } else if (Math::abs(mb.x - pos[2].x) < 8) { + edited_margin = 2; + prev_margin = margins[2]; + } else if (Math::abs(mb.x - pos[3].x) < 8) { + edited_margin = 3; + prev_margin = margins[3]; + } + if (edited_margin >= 0) { + drag_from=Vector2(mb.x,mb.y); + drag=true; } } + if ( edited_margin < 0 && snap_mode == SNAP_AUTOSLICE) { + Vector2 point = mtx.affine_inverse().xform(Vector2(mb.x,mb.y)); + for (List<Rect2>::Element *E=autoslice_cache.front();E;E=E->next()) { + if (E->get().has_point(point)) { + rect = E->get(); + if (Input::get_singleton()->is_key_pressed(KEY_CONTROL)&&!(Input::get_singleton()->is_key_pressed(KEY_SHIFT|KEY_ALT))) { + Rect2 r; + if(node_sprite ) + r=node_sprite->get_region_rect(); + else if(node_patch9) + r=node_patch9->get_region_rect(); + else if(obj_styleBox.is_valid()) + r=obj_styleBox->get_region_rect(); + else if(atlas_tex.is_valid()) + r =atlas_tex->get_region(); + rect.expand_to(r.pos); + rect.expand_to(r.pos+r.size); + } + undo_redo->create_action("Set Region Rect"); + 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 (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()); + } else if (atlas_tex.is_valid()) { + undo_redo->add_do_method(atlas_tex.ptr(),"set_region",rect); + undo_redo->add_undo_method(atlas_tex.ptr(),"set_region",atlas_tex->get_region()); + } + undo_redo->add_do_method(edit_draw,"update"); + undo_redo->add_undo_method(edit_draw,"update"); + undo_redo->commit_action(); + break; + } + } + } else if (edited_margin < 0) { + print_line("EDIT RECTANGLE!!!"); + drag_from=mtx.affine_inverse().xform(Vector2(mb.x,mb.y)); + if (snap_mode == SNAP_PIXEL) + drag_from = drag_from.snapped(Vector2(1,1)); + else if (snap_mode == SNAP_GRID) + drag_from=snap_point(drag_from); + 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(obj_styleBox.is_valid()) + rect_prev=obj_styleBox->get_region_rect(); + else if(atlas_tex.is_valid()) + rect_prev=atlas_tex->get_region(); + + for (int i=0; i<8;i++) { + Vector2 tuv=endpoints[i]; + if (tuv.distance_to(Vector2(mb.x,mb.y))<8) { + drag_index=i; + } + } - if (drag_index==-1) { - creating = true; - rect = Rect2(drag_from,Size2()); + if (drag_index==-1) { + creating = true; + rect = Rect2(drag_from,Size2()); + } } } else if (drag) { - if(editing_region == REGION_TEXTURE_REGION) { - undo_redo->create_action("Set region_rect"); - if(node_type == "Sprite" && node_sprite ){ + print_line("DRAGING!!!"); + 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); + } 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); + obj_styleBox->emit_signal(CoreStringNames::get_singleton()->changed); + } + edited_margin = -1; + } else { + undo_redo->create_action("Set Region Rect"); + if(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); } - else if(node_type == "AtlasTexture" && atlas_tex ){ - undo_redo->add_do_method(atlas_tex ,"set_region",atlas_tex->get_region()); - undo_redo->add_undo_method(atlas_tex,"set_region",rect_prev); + 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_type == "Patch9Frame" && node_patch9){ + else if(node_patch9){ + } 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 (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); } - else if(node_type == "StyleBoxTexture" && obj_styleBox){ - undo_redo->add_do_method(obj_styleBox ,"set_region_rect",obj_styleBox->get_region_rect()); - undo_redo->add_undo_method(obj_styleBox,"set_region_rect",rect_prev); - } - undo_redo->add_do_method(edit_draw,"update"); - undo_redo->add_undo_method(edit_draw,"update"); - undo_redo->commit_action(); + drag_index = -1; } + undo_redo->add_do_method(edit_draw,"update"); + undo_redo->add_undo_method(edit_draw,"update"); + undo_redo->commit_action(); drag=false; + creating = false; } } else if (mb.button_index==BUTTON_RIGHT && mb.pressed) { if (drag) { drag=false; - apply_rect(rect_prev); - rect=rect_prev; - edit_draw->update(); + 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 (obj_styleBox.is_valid()) + obj_styleBox->set_margin_size(m[edited_margin],prev_margin); + edited_margin = -1; + } else { + apply_rect(rect_prev); + rect=rect_prev; + edit_draw->update(); + drag_index = -1; + } } - - } else if (mb.button_index==BUTTON_WHEEL_UP && mb.pressed) { - - zoom->set_val( zoom->get_val()/0.9 ); - } else if (mb.button_index==BUTTON_WHEEL_DOWN && mb.pressed) { - - zoom->set_val( zoom->get_val()*0.9); } - } else if (p_input.type==InputEvent::MOUSE_MOTION) { const InputEventMouseMotion &mm=p_input.mouse_motion; @@ -243,67 +403,89 @@ void TextureRegionEditor::_region_input(const InputEvent& p_input) } else if (drag) { - Vector2 new_pos = mtx.affine_inverse().xform(Vector2(mm.x,mm.y)); - new_pos = snap_point(new_pos); - - if (creating) { - rect = Rect2(drag_from,Size2()); - rect.expand_to(new_pos); - apply_rect(rect); - edit_draw->update(); - return; - } - - 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; - 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; - 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; - 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; - case 4: { - 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; - 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; - case 7: { - Vector2 p=rect_prev.pos+Vector2(rect_prev.size.x,0); - rect = Rect2(p,Size2(0,rect_prev.size.y)); + if (edited_margin >= 0) { + float new_margin; + if (edited_margin == 0) + new_margin = prev_margin + (mm.y-drag_from.y) / draw_zoom; + else if (edited_margin == 1) + new_margin = prev_margin - (mm.y-drag_from.y) / draw_zoom; + else if (edited_margin == 2) + new_margin = prev_margin + (mm.x-drag_from.x) / draw_zoom; + else if (edited_margin == 3) + new_margin = prev_margin - (mm.x-drag_from.x) / draw_zoom; + 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 (obj_styleBox.is_valid()) + obj_styleBox->set_margin_size(m[edited_margin],new_margin); + } else { + Vector2 new_pos = mtx.affine_inverse().xform(Vector2(mm.x,mm.y)); + if (snap_mode == SNAP_PIXEL) + new_pos = new_pos.snapped(Vector2(1,1)); + else if (snap_mode == SNAP_GRID) + new_pos=snap_point(new_pos); + + if (creating) { + rect = Rect2(drag_from,Size2()); rect.expand_to(new_pos); apply_rect(rect); - } break; + edit_draw->update(); + return; + } + 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; + 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; + 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; + 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; + case 4: { + 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; + 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; + 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; + + } } edit_draw->update(); } @@ -318,19 +500,21 @@ void TextureRegionEditor::_scroll_changed(float) draw_ofs.x=hscroll->get_val(); draw_ofs.y=vscroll->get_val(); - draw_zoom=zoom->get_val(); - print_line("_scroll_changed"); edit_draw->update(); } -void TextureRegionEditor::_set_use_snap(bool p_use) +void TextureRegionEditor::_set_snap_mode(int p_mode) { - use_snap=p_use; -} + snap_mode_button->get_popup()->set_item_checked(snap_mode,false); + snap_mode = p_mode; + snap_mode_button->set_text(snap_mode_button->get_popup()->get_item_text(p_mode)); + snap_mode_button->get_popup()->set_item_checked(snap_mode,true); + + if (snap_mode == SNAP_GRID) + hb_grid->show(); + else + hb_grid->hide(); -void TextureRegionEditor::_set_show_grid(bool p_show) -{ - snap_show_grid=p_show; edit_draw->update(); } @@ -358,42 +542,59 @@ void TextureRegionEditor::_set_snap_step_y(float p_val) edit_draw->update(); } -void TextureRegionEditor::apply_rect(const Rect2& rect){ +void TextureRegionEditor::_set_snap_sep_x(float p_val) +{ + snap_separation.x = p_val; + edit_draw->update(); +} - if(this->editing_region == REGION_TEXTURE_REGION) { - if(node_sprite) - 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(atlas_tex) - atlas_tex->set_region(rect); +void TextureRegionEditor::_set_snap_sep_y(float p_val) +{ + snap_separation.y = p_val; + edit_draw->update(); +} + +void TextureRegionEditor::_zoom_in() +{ + if (draw_zoom < 8) { + draw_zoom *= 2; + edit_draw->update(); } - 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::_zoom_reset() +{ + if (draw_zoom == 1) return; + draw_zoom = 1; + edit_draw->update(); +} + +void TextureRegionEditor::_zoom_out() +{ + if (draw_zoom > 0.25) { + draw_zoom /= 2; + edit_draw->update(); } } +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(obj_styleBox.is_valid()) + obj_styleBox->set_region_rect(rect); + else if(atlas_tex.is_valid()) + atlas_tex->set_region(rect); +} + void TextureRegionEditor::_notification(int p_what) { switch(p_what) { 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")); - b_snap_enable->set_icon( get_icon("Snap", "EditorIcons")); + zoom_out->set_icon(get_icon("ZoomLess", "EditorIcons")); + zoom_reset->set_icon(get_icon("ZoomReset", "EditorIcons")); + zoom_in->set_icon(get_icon("ZoomMore", "EditorIcons")); icon_zoom->set_texture( get_icon("Zoom", "EditorIcons")); } break; } @@ -401,149 +602,165 @@ 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 || p_obj == atlas_tex) { - node_patch9 = NULL; - node_sprite = NULL; - obj_styleBox = NULL; - atlas_tex = NULL; + if(p_obj == node_sprite || p_obj == node_patch9 || p_obj == obj_styleBox.ptr() || p_obj == atlas_tex.ptr()) { + node_patch9 = NULL; + node_sprite = NULL; + obj_styleBox = Ref<StyleBox>(NULL); + atlas_tex = Ref<AtlasTexture>(NULL); hide(); } } void TextureRegionEditor::_bind_methods() { - ObjectTypeDB::bind_method(_MD("_edit_node"),&TextureRegionEditor::_edit_node); ObjectTypeDB::bind_method(_MD("_edit_region"),&TextureRegionEditor::_edit_region); - ObjectTypeDB::bind_method(_MD("_edit_margin"),&TextureRegionEditor::_edit_margin); ObjectTypeDB::bind_method(_MD("_region_draw"),&TextureRegionEditor::_region_draw); ObjectTypeDB::bind_method(_MD("_region_input"),&TextureRegionEditor::_region_input); ObjectTypeDB::bind_method(_MD("_scroll_changed"),&TextureRegionEditor::_scroll_changed); ObjectTypeDB::bind_method(_MD("_node_removed"),&TextureRegionEditor::_node_removed); - ObjectTypeDB::bind_method(_MD("_set_use_snap"),&TextureRegionEditor::_set_use_snap); - ObjectTypeDB::bind_method(_MD("_set_show_grid"),&TextureRegionEditor::_set_show_grid); + ObjectTypeDB::bind_method(_MD("_set_snap_mode"),&TextureRegionEditor::_set_snap_mode); ObjectTypeDB::bind_method(_MD("_set_snap_off_x"),&TextureRegionEditor::_set_snap_off_x); ObjectTypeDB::bind_method(_MD("_set_snap_off_y"),&TextureRegionEditor::_set_snap_off_y); ObjectTypeDB::bind_method(_MD("_set_snap_step_x"),&TextureRegionEditor::_set_snap_step_x); ObjectTypeDB::bind_method(_MD("_set_snap_step_y"),&TextureRegionEditor::_set_snap_step_y); + ObjectTypeDB::bind_method(_MD("_set_snap_sep_x"),&TextureRegionEditor::_set_snap_sep_x); + ObjectTypeDB::bind_method(_MD("_set_snap_sep_y"),&TextureRegionEditor::_set_snap_sep_y); + ObjectTypeDB::bind_method(_MD("_zoom_in"),&TextureRegionEditor::_zoom_in); + ObjectTypeDB::bind_method(_MD("_zoom_reset"),&TextureRegionEditor::_zoom_reset); + ObjectTypeDB::bind_method(_MD("_zoom_out"),&TextureRegionEditor::_zoom_out); } void TextureRegionEditor::edit(Object *p_obj) { + if (node_sprite && node_sprite->is_connected("texture_changed",this,"_edit_region")) + node_sprite->disconnect("texture_changed",this,"_edit_region"); + if (node_patch9 && node_patch9->is_connected("texture_changed",this,"_edit_region")) + node_patch9->disconnect("texture_changed",this,"_edit_region"); + if (obj_styleBox.is_valid() && obj_styleBox->is_connected("texture_changed",this,"_edit_region")) + obj_styleBox->disconnect("texture_changed",this,"_edit_region"); + if (atlas_tex.is_valid() && atlas_tex->is_connected("atlas_changed",this,"_edit_region")) + atlas_tex->disconnect("atlas_changed",this,"_edit_region"); if (p_obj) { - margin_button->hide(); - node_type = p_obj->get_type(); - if(node_type == "Sprite"){ - node_sprite = p_obj->cast_to<Sprite>(); - node_patch9 = NULL; - obj_styleBox = NULL; - atlas_tex = NULL; - } - else if(node_type == "AtlasTexture") { - atlas_tex = p_obj->cast_to<AtlasTexture>(); - node_sprite = NULL; - node_patch9 = NULL; - obj_styleBox = NULL; - } - else if(node_type == "Patch9Frame") { - node_patch9 = p_obj->cast_to<Patch9Frame>(); - node_sprite = NULL; - obj_styleBox = NULL; - atlas_tex = NULL; - margin_button->show(); - } - else if(node_type == "StyleBoxTexture") { - obj_styleBox = p_obj->cast_to<StyleBoxTexture>(); - node_sprite = NULL; - node_patch9 = NULL; - atlas_tex = NULL; - margin_button->show(); + node_sprite = p_obj->cast_to<Sprite>(); + node_patch9 = p_obj->cast_to<Patch9Frame>(); + if (p_obj->cast_to<StyleBoxTexture>()) + obj_styleBox = Ref<StyleBoxTexture>(p_obj->cast_to<StyleBoxTexture>()); + if (p_obj->cast_to<AtlasTexture>()) { + atlas_tex = Ref<AtlasTexture>(p_obj->cast_to<AtlasTexture>()); + atlas_tex->connect("atlas_changed",this,"_edit_region"); + } else { + p_obj->connect("texture_changed",this,"_edit_region"); } p_obj->connect("exit_tree",this,"_node_removed",varray(p_obj),CONNECT_ONESHOT); + _edit_region(); } else { if(node_sprite) node_sprite->disconnect("exit_tree",this,"_node_removed"); - else if(atlas_tex) - atlas_tex->disconnect("exit_tree",this,"_node_removed"); else if(node_patch9) node_patch9->disconnect("exit_tree",this,"_node_removed"); - else if(obj_styleBox) + else if(obj_styleBox.is_valid()) obj_styleBox->disconnect("exit_tree",this,"_node_removed"); - node_sprite = NULL; - node_patch9 = NULL; - obj_styleBox = NULL; - atlas_tex = NULL; + else if(atlas_tex.is_valid()) + atlas_tex->disconnect("exit_tree",this,"_node_removed"); + + node_sprite = NULL; + node_patch9 = NULL; + obj_styleBox = Ref<StyleBoxTexture>(NULL); + atlas_tex = Ref<AtlasTexture>(NULL); } + edit_draw->update(); } void TextureRegionEditor::_edit_region() { - this->_edit_node(REGION_TEXTURE_REGION); - dlg_editor->set_title(TTR("Texture Region Editor")); -} - -void TextureRegionEditor::_edit_margin() -{ - this->_edit_node(REGION_PATCH_MARGIN); - dlg_editor->set_title(TTR("Scale Region Editor")); -} - -void TextureRegionEditor::_edit_node(int region) -{ Ref<Texture> texture = NULL; - if(node_type == "Sprite" && node_sprite ) + if(node_sprite ) texture = node_sprite->get_texture(); - else if(node_type == "Patch9Frame" && node_patch9 ) + else if(node_patch9 ) texture = node_patch9->get_texture(); - else if(node_type == "StyleBoxTexture" && obj_styleBox) + else if(obj_styleBox.is_valid()) texture = obj_styleBox->get_texture(); - else if(node_type == "AtlasTexture" && atlas_tex) + else if(atlas_tex.is_valid()) texture = atlas_tex->get_atlas(); if (texture.is_null()) { - error->set_text(TTR("No texture in this node.\nSet a texture to be able to edit region.")); - error->popup_centered_minsize(); return; } - if(node_type == "Sprite" && node_sprite ) - 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(); - else if(node_type == "AtlasTexture" && atlas_tex) - tex_region = atlas_tex->get_region(); - rect = tex_region; - - 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)); + autoslice_cache.clear(); + Image i; + if (i.load(texture->get_path()) == OK) { + BitMap bm; + bm.create_from_image_alpha(i); + for (int y = 0; y < i.get_height(); y++) { + for (int x = 0; x < i.get_width(); x++) { + if (bm.get_bit(Point2(x,y))) { + bool found = false; + for (List<Rect2>::Element *E = autoslice_cache.front(); E; E=E->next()) { + Rect2 grown = E->get().grow(1.5); + if (grown.has_point(Point2(x,y))) { + E->get().expand_to(Point2(x,y)); + E->get().expand_to(Point2(x+1,y+1)); + x = E->get().pos.x+E->get().size.x-1; + bool merged = true; + while (merged) { + merged = false; + for (List<Rect2>::Element *F = autoslice_cache.front(); F; F=F->next()) { + if (F==E) + continue; + if (E->get().grow(1).intersects(F->get())) { + E->get().expand_to(F->get().pos); + E->get().expand_to(F->get().pos+F->get().size); + F=F->prev(); + autoslice_cache.erase(F->next()); + merged = true; + } + } + } + found = true; + break; + } + } + if (!found) { + Rect2 new_rect(x,y,1,1); + autoslice_cache.push_back(new_rect); + } + } + } } } - dlg_editor->popup_centered_ratio(0.85); - dlg_editor->get_ok()->release_focus(); - editing_region = region; + if(node_sprite ) + rect = node_sprite->get_region_rect(); + else if(node_patch9 ) + rect = node_patch9->get_region_rect(); + else if(obj_styleBox.is_valid()) + rect = obj_styleBox->get_region_rect(); + else if (atlas_tex.is_valid()) + rect = atlas_tex->get_region(); + + edit_draw->update(); } -inline float _snap_scalar(float p_offset, float p_step, float p_target) { - return p_step != 0 ? Math::stepify(p_target - p_offset, p_step) + p_offset : p_target; +inline float _snap_scalar(float p_offset, float p_step, float separation, float p_target) { + if (p_step != 0) { + float a = Math::stepify(p_target - p_offset, p_step+separation) + p_offset; + float b = a; + if (p_target >= 0) + b -= separation; + else + b += p_step; + return (Math::abs(p_target-a) < Math::abs(p_target-b)) ? a : b; + } + return p_target; } Vector2 TextureRegionEditor::snap_point(Vector2 p_target) const { - if (use_snap) { - p_target.x = _snap_scalar(snap_offset.x, snap_step.x, p_target.x); - p_target.y = _snap_scalar(snap_offset.y, snap_step.y, p_target.y); + if (snap_mode == SNAP_GRID) { + p_target.x = _snap_scalar(snap_offset.x, snap_step.x, snap_separation.x, p_target.x); + p_target.y = _snap_scalar(snap_offset.y, snap_step.y, snap_separation.y, p_target.y); } - p_target = p_target.snapped(Size2(1, 1)); return p_target; } @@ -552,56 +769,40 @@ TextureRegionEditor::TextureRegionEditor(EditorNode* p_editor) { node_sprite = NULL; node_patch9 = NULL; - atlas_tex = NULL; + obj_styleBox = Ref<StyleBoxTexture>(NULL); + atlas_tex = Ref<AtlasTexture>(NULL); editor=p_editor; undo_redo = editor->get_undo_redo(); snap_step=Vector2(10,10); - use_snap=false; - snap_show_grid=false; + snap_separation = Vector2(0,0); drag=false; - add_child( memnew( VSeparator )); - region_button = memnew( ToolButton ); - add_child(region_button); - region_button->set_tooltip(TTR("Texture Region Editor")); - region_button->connect("pressed",this,"_edit_region"); - - margin_button = memnew( ToolButton ); - add_child(margin_button); - margin_button->set_tooltip(TTR("Scale Region Editor")); - margin_button->connect("pressed",this,"_edit_margin"); - - dlg_editor = memnew( AcceptDialog ); - add_child(dlg_editor); - dlg_editor->set_self_opacity(0.9); - VBoxContainer *main_vb = memnew( VBoxContainer ); - dlg_editor->add_child(main_vb); - dlg_editor->set_child_rect(main_vb); + add_child(main_vb); + main_vb->set_area_as_parent_rect(0); HBoxContainer *hb_tools = memnew( HBoxContainer ); main_vb->add_child(hb_tools); - b_snap_enable = memnew( ToolButton ); - hb_tools->add_child(b_snap_enable); - b_snap_enable->set_text(TTR("Snap")); - b_snap_enable->set_focus_mode(FOCUS_NONE); - b_snap_enable->set_toggle_mode(true); - b_snap_enable->set_pressed(use_snap); - b_snap_enable->set_tooltip(TTR("Enable Snap")); - b_snap_enable->connect("toggled",this,"_set_use_snap"); - - b_snap_grid = memnew( ToolButton ); - hb_tools->add_child(b_snap_grid); - b_snap_grid->set_text(TTR("Grid")); - b_snap_grid->set_focus_mode(FOCUS_NONE); - b_snap_grid->set_toggle_mode(true); - b_snap_grid->set_pressed(snap_show_grid); - b_snap_grid->set_tooltip(TTR("Show Grid")); - b_snap_grid->connect("toggled",this,"_set_show_grid"); - - hb_tools->add_child( memnew( VSeparator )); - hb_tools->add_child( memnew( Label(TTR("Grid Offset:")) ) ); + hb_tools->add_child(memnew( Label(TTR("Snap Mode:")) )); + + snap_mode_button = memnew( MenuButton ); + hb_tools->add_child(snap_mode_button); + snap_mode_button->set_text(TTR("<None>")); + PopupMenu *p = snap_mode_button->get_popup(); + p->add_item(TTR("<None>"),0); + p->add_item(TTR("Pixel Snap"),1); + p->add_item(TTR("Grid Snap"),2); + p->add_item(TTR("Auto Slice"),3); + for (int i = 0; i < 4; i++) + p->set_item_as_checkable(i,true); + p->set_item_checked(0,true); + p->connect("item_pressed", this, "_set_snap_mode"); + hb_grid = memnew( HBoxContainer ); + hb_tools->add_child(hb_grid); + hb_grid->add_child( memnew( VSeparator )); + + hb_grid->add_child( memnew( Label(TTR("Offset:")) ) ); sb_off_x = memnew( SpinBox ); sb_off_x->set_min(-256); @@ -610,7 +811,7 @@ TextureRegionEditor::TextureRegionEditor(EditorNode* p_editor) sb_off_x->set_val(snap_offset.x); sb_off_x->set_suffix("px"); sb_off_x->connect("value_changed", this, "_set_snap_off_x"); - hb_tools->add_child(sb_off_x); + hb_grid->add_child(sb_off_x); sb_off_y = memnew( SpinBox ); sb_off_y->set_min(-256); @@ -619,10 +820,10 @@ TextureRegionEditor::TextureRegionEditor(EditorNode* p_editor) sb_off_y->set_val(snap_offset.y); sb_off_y->set_suffix("px"); sb_off_y->connect("value_changed", this, "_set_snap_off_y"); - hb_tools->add_child(sb_off_y); + hb_grid->add_child(sb_off_y); - hb_tools->add_child( memnew( VSeparator )); - hb_tools->add_child( memnew( Label(TTR("Grid Step:")) ) ); + hb_grid->add_child( memnew( VSeparator )); + hb_grid->add_child( memnew( Label(TTR("Step:")) ) ); sb_step_x = memnew( SpinBox ); sb_step_x->set_min(-256); @@ -631,7 +832,7 @@ TextureRegionEditor::TextureRegionEditor(EditorNode* p_editor) sb_step_x->set_val(snap_step.x); sb_step_x->set_suffix("px"); sb_step_x->connect("value_changed", this, "_set_snap_step_x"); - hb_tools->add_child(sb_step_x); + hb_grid->add_child(sb_step_x); sb_step_y = memnew( SpinBox ); sb_step_y->set_min(-256); @@ -640,7 +841,30 @@ TextureRegionEditor::TextureRegionEditor(EditorNode* p_editor) sb_step_y->set_val(snap_step.y); sb_step_y->set_suffix("px"); sb_step_y->connect("value_changed", this, "_set_snap_step_y"); - hb_tools->add_child(sb_step_y); + hb_grid->add_child(sb_step_y); + + hb_grid->add_child( memnew( VSeparator )); + hb_grid->add_child( memnew( Label(TTR("Separation:")) ) ); + + sb_sep_x = memnew( SpinBox ); + sb_sep_x->set_min(0); + sb_sep_x->set_max(256); + sb_sep_x->set_step(1); + sb_sep_x->set_val(snap_separation.x); + sb_sep_x->set_suffix("px"); + sb_sep_x->connect("value_changed", this, "_set_snap_sep_x"); + hb_grid->add_child(sb_sep_x); + + sb_sep_y = memnew( SpinBox ); + sb_sep_y->set_min(0); + sb_sep_y->set_max(256); + sb_sep_y->set_step(1); + sb_sep_y->set_val(snap_separation.y); + sb_sep_y->set_suffix("px"); + sb_sep_y->connect("value_changed", this, "_set_snap_sep_y"); + hb_grid->add_child(sb_sep_y); + + hb_grid->hide(); HBoxContainer *main_hb = memnew( HBoxContainer ); main_vb->add_child(main_hb); @@ -649,25 +873,24 @@ TextureRegionEditor::TextureRegionEditor(EditorNode* p_editor) main_hb->set_v_size_flags(SIZE_EXPAND_FILL); edit_draw->set_h_size_flags(SIZE_EXPAND_FILL); + Control * separator = memnew( Control ); + separator->set_h_size_flags(Control::SIZE_EXPAND_FILL); + hb_tools->add_child(separator); - hb_tools->add_child( memnew( VSeparator )); icon_zoom = memnew( TextureFrame ); hb_tools->add_child(icon_zoom); - zoom = memnew( HSlider ); - zoom->set_min(0.01); - zoom->set_max(4); - zoom->set_val(1); - zoom->set_step(0.01); - hb_tools->add_child(zoom); - zoom->set_custom_minimum_size(Size2(200,0)); - zoom_value = memnew( SpinBox ); - zoom->share(zoom_value); - zoom_value->set_custom_minimum_size(Size2(50,0)); - hb_tools->add_child(zoom_value); - zoom->connect("value_changed",this,"_scroll_changed"); + zoom_out = memnew( ToolButton ); + zoom_out->connect("pressed", this, "_zoom_out"); + hb_tools->add_child(zoom_out); + zoom_reset = memnew( ToolButton ); + zoom_reset->connect("pressed", this, "_zoom_reset"); + hb_tools->add_child(zoom_reset); + zoom_in = memnew( ToolButton ); + zoom_in->connect("pressed", this, "_zoom_in"); + hb_tools->add_child(zoom_in); vscroll = memnew( VScrollBar); main_hb->add_child(vscroll); @@ -681,9 +904,6 @@ TextureRegionEditor::TextureRegionEditor(EditorNode* p_editor) draw_zoom=1.0; updating_scroll=false; - error = memnew( AcceptDialog); - add_child(error); - } void TextureRegionEditorPlugin::edit(Object *p_node) @@ -699,10 +919,13 @@ bool TextureRegionEditorPlugin::handles(Object *p_obj) const void TextureRegionEditorPlugin::make_visible(bool p_visible) { if (p_visible) { - region_editor->show(); + region_button->show(); + if (region_button->is_pressed()) + region_editor->show(); } else { - region_editor->hide(); + region_button->hide(); region_editor->edit(NULL); + region_editor->hide(); } } @@ -710,11 +933,11 @@ void TextureRegionEditorPlugin::make_visible(bool p_visible) Dictionary TextureRegionEditorPlugin::get_state() const { Dictionary state; - state["zoom"]=region_editor->zoom->get_val(); + state["zoom"]=region_editor->draw_zoom; state["snap_offset"]=region_editor->snap_offset; state["snap_step"]=region_editor->snap_step; - state["use_snap"]=region_editor->use_snap; - state["snap_show_grid"]=region_editor->snap_show_grid; + state["snap_separation"]=region_editor->snap_separation; + state["snap_mode"]=region_editor->snap_mode; return state; } @@ -722,7 +945,7 @@ void TextureRegionEditorPlugin::set_state(const Dictionary& p_state){ Dictionary state=p_state; if (state.has("zoom")) { - region_editor->zoom->set_val(p_state["zoom"]); + region_editor->draw_zoom = p_state["zoom"]; } if (state.has("snap_step")) { @@ -739,22 +962,28 @@ void TextureRegionEditorPlugin::set_state(const Dictionary& p_state){ region_editor->snap_offset = ofs; } - if (state.has("use_snap")) { - region_editor->use_snap=state["use_snap"]; - region_editor->b_snap_enable->set_pressed(state["use_snap"]); + if (state.has("snap_separation")) { + Vector2 sep = state["snap_separation"]; + region_editor->sb_sep_x->set_val(sep.x); + region_editor->sb_sep_y->set_val(sep.y); + region_editor->snap_separation = sep; } - if (state.has("snap_show_grid")) { - region_editor->snap_show_grid=state["snap_show_grid"]; - region_editor->b_snap_grid->set_pressed(state["snap_show_grid"]); + if (state.has("snap_mode")) { + region_editor->_set_snap_mode(state["snap_mode"]); } + } TextureRegionEditorPlugin::TextureRegionEditorPlugin(EditorNode *p_node) { editor = p_node; - region_editor= memnew ( TextureRegionEditor(p_node) ); - CanvasItemEditor::get_singleton()->add_control_to_menu_panel(region_editor); + region_editor = memnew ( TextureRegionEditor(p_node) ); + + region_button = p_node->add_bottom_panel_item(TTR("Texture Region"), region_editor); + region_button->set_tooltip(TTR("Texture Region Editor")); + region_editor->set_custom_minimum_size(Size2(0,200)); region_editor->hide(); + region_button->hide(); } diff --git a/tools/editor/plugins/texture_region_editor_plugin.h b/tools/editor/plugins/texture_region_editor_plugin.h index 1e4888b06d..3658a38f11 100644 --- a/tools/editor/plugins/texture_region_editor_plugin.h +++ b/tools/editor/plugins/texture_region_editor_plugin.h @@ -40,69 +40,73 @@ #include "scene/resources/style_box.h" #include "scene/resources/texture.h" -class TextureRegionEditor : public HBoxContainer { +class TextureRegionEditor : public Control { - OBJ_TYPE(TextureRegionEditor, HBoxContainer ); - enum RegionType { - REGION_TEXTURE_REGION, - REGION_PATCH_MARGIN + OBJ_TYPE(TextureRegionEditor, Control ); + + enum SnapMode { + SNAP_NONE, + SNAP_PIXEL, + SNAP_GRID, + SNAP_AUTOSLICE }; friend class TextureRegionEditorPlugin; - ToolButton *region_button; - ToolButton *margin_button; - ToolButton *b_snap_enable; - ToolButton *b_snap_grid; + MenuButton *snap_mode_button; TextureFrame *icon_zoom; - HSlider *zoom; - SpinBox *zoom_value; + ToolButton *zoom_in; + ToolButton *zoom_reset; + ToolButton *zoom_out; + HBoxContainer * hb_grid; //For showing/hiding the grid controls when changing the SnapMode SpinBox *sb_step_y; SpinBox *sb_step_x; SpinBox *sb_off_y; SpinBox *sb_off_x; + SpinBox *sb_sep_y; + SpinBox *sb_sep_x; Control *edit_draw; VScrollBar *vscroll; HScrollBar *hscroll; EditorNode *editor; - AcceptDialog *dlg_editor; UndoRedo* undo_redo; Vector2 draw_ofs; float draw_zoom; bool updating_scroll; - bool use_snap; - bool snap_show_grid; + int snap_mode; Vector2 snap_offset; Vector2 snap_step; + Vector2 snap_separation; - - String node_type; Patch9Frame *node_patch9; Sprite *node_sprite; - StyleBoxTexture *obj_styleBox; - AtlasTexture *atlas_tex; + Ref<StyleBoxTexture> obj_styleBox; + Ref<AtlasTexture> atlas_tex; - int editing_region; Rect2 rect; Rect2 rect_prev; - Rect2 tex_region; + float prev_margin; + int edited_margin; + List<Rect2> autoslice_cache; bool drag; bool creating; Vector2 drag_from; int drag_index; - AcceptDialog *error; - - void _set_use_snap(bool p_use); - void _set_show_grid(bool p_show); + void _set_snap_mode(int p_mode); void _set_snap_off_x(float p_val); void _set_snap_off_y(float p_val); void _set_snap_step_x(float p_val); void _set_snap_step_y(float p_val); + void _set_snap_sep_x(float p_val); + void _set_snap_sep_y(float p_val); + void _zoom_in(); + void _zoom_reset(); + void _zoom_out(); void apply_rect(const Rect2& rect); protected: @@ -114,9 +118,7 @@ protected: public: - void _edit_node(int tex_region); void _edit_region(); - void _edit_margin(); void _region_draw(); void _region_input(const InputEvent &p_input); void _scroll_changed(float); @@ -130,11 +132,12 @@ class TextureRegionEditorPlugin : public EditorPlugin { OBJ_TYPE( TextureRegionEditorPlugin, EditorPlugin ); + Button *region_button; TextureRegionEditor *region_editor; EditorNode *editor; public: - virtual String get_name() const { return "SpriteRegion"; } + virtual String get_name() const { return "TextureRegion"; } bool has_main_screen() const { return false; } virtual void edit(Object *p_node); virtual bool handles(Object *p_node) const; |