diff --git a/core/variant_op.cpp b/core/variant_op.cpp index 1f0f038d776..6c2667c7e91 100644 --- a/core/variant_op.cpp +++ b/core/variant_op.cpp @@ -3354,7 +3354,59 @@ void Variant::interpolate(const Variant& a, const Variant& b, float c,Variant &r case INT_ARRAY:{ r_dst=a; } return; case REAL_ARRAY:{ r_dst=a; } return; case STRING_ARRAY:{ r_dst=a; } return; - case VECTOR3_ARRAY:{ r_dst=a; } return; + case VECTOR2_ARRAY:{ + const DVector *arr_a=reinterpret_cast* >(a._data._mem); + const DVector *arr_b=reinterpret_cast* >(b._data._mem); + int sz = arr_a->size(); + if (sz==0 || arr_b->size()!=sz) { + + r_dst=a; + } else { + + DVector v; + v.resize(sz); + { + DVector::Write vw=v.write(); + DVector::Read ar=arr_a->read(); + DVector::Read br=arr_b->read(); + + for(int i=0;i *arr_a=reinterpret_cast* >(a._data._mem); + const DVector *arr_b=reinterpret_cast* >(b._data._mem); + int sz = arr_a->size(); + if (sz==0 || arr_b->size()!=sz) { + + r_dst=a; + } else { + + DVector v; + v.resize(sz); + { + DVector::Write vw=v.write(); + DVector::Read ar=arr_a->read(); + DVector::Read br=arr_b->read(); + + for(int i=0;i& p_texture) { + + begin=p_texture; + update(); +} + +Ref PathTexture::get_begin_texture() const{ + + return begin; +} + +void PathTexture::set_repeat_texture(const Ref& p_texture){ + + repeat=p_texture; + update(); + +} +Ref PathTexture::get_repeat_texture() const{ + + return repeat; +} + +void PathTexture::set_end_texture(const Ref& p_texture){ + + end=p_texture; + update(); +} +Ref PathTexture::get_end_texture() const{ + + return end; +} + +void PathTexture::set_subdivisions(int p_amount){ + + ERR_FAIL_INDEX(p_amount,32); + subdivs=p_amount; + update(); + +} + +int PathTexture::get_subdivisions() const{ + + return subdivs; +} + +void PathTexture::set_overlap(int p_amount){ + + overlap=p_amount; + update(); +} +int PathTexture::get_overlap() const{ + + return overlap; +} + + +PathTexture::PathTexture() { + + overlap=0; + subdivs=1; +} diff --git a/scene/2d/path_texture.h b/scene/2d/path_texture.h new file mode 100644 index 00000000000..0e63758b105 --- /dev/null +++ b/scene/2d/path_texture.h @@ -0,0 +1,34 @@ +#ifndef PATH_TEXTURE_H +#define PATH_TEXTURE_H + +#include "scene/2d/node_2d.h" + +class PathTexture : public Node2D { + OBJ_TYPE( PathTexture, Node2D ); + + Ref begin; + Ref repeat; + Ref end; + int subdivs; + bool overlap; +public: + + void set_begin_texture(const Ref& p_texture); + Ref get_begin_texture() const; + + void set_repeat_texture(const Ref& p_texture); + Ref get_repeat_texture() const; + + void set_end_texture(const Ref& p_texture); + Ref get_end_texture() const; + + void set_subdivisions(int p_amount); + int get_subdivisions() const; + + void set_overlap(int p_amount); + int get_overlap() const; + + PathTexture(); +}; + +#endif // PATH_TEXTURE_H diff --git a/scene/2d/polygon_2d.cpp b/scene/2d/polygon_2d.cpp new file mode 100644 index 00000000000..2b4be734afd --- /dev/null +++ b/scene/2d/polygon_2d.cpp @@ -0,0 +1,363 @@ +#include "polygon_2d.h" + +Rect2 Polygon2D::get_item_rect() const { + + + if (rect_cache_dirty){ + int l =polygon.size(); + DVector::Read r = polygon.read(); + item_rect=Rect2(); + for(int i=0;i points; + Vector uvs; + + points.resize(polygon.size()); + + int len = points.size(); + { + + DVector::Read polyr =polygon.read(); + for(int i=0;ihighest_y) { + highest_idx=i; + highest_y=points[i].y; + } + int ni=(i+1)%len; + sum+=(points[ni].x-points[i].x)*(points[ni].y+points[i].y); + } + + bounds=bounds.grow(invert_border); + + Vector2 ep[7]={ + Vector2(points[highest_idx].x,points[highest_idx].y+invert_border), + Vector2(bounds.pos+bounds.size), + Vector2(bounds.pos+Vector2(bounds.size.x,0)), + Vector2(bounds.pos), + Vector2(bounds.pos+Vector2(0,bounds.size.y)), + Vector2(points[highest_idx].x-CMP_EPSILON,points[highest_idx].y+invert_border), + Vector2(points[highest_idx].x-CMP_EPSILON,points[highest_idx].y), + }; + + + if (sum>0) { + SWAP(ep[1],ep[4]); + SWAP(ep[2],ep[3]); + SWAP(ep[5],ep[0]); + SWAP(ep[6],points[highest_idx]); + } + + points.resize(points.size()+7); + for(int i=points.size()-1;i>=highest_idx+7;i--) { + + points[i]=points[i-7]; + } + + for(int i=0;i<7;i++) { + + points[highest_idx+i+1]=ep[i]; + } + + + len=points.size(); + + } + + if (texture.is_valid()) { + + Matrix32 texmat(tex_rot,tex_ofs); + texmat.scale(tex_scale); + Size2 tex_size=Vector2(1,1); + + tex_size=texture->get_size(); + uvs.resize(points.size()); + + if (points.size()==uv.size()) { + + DVector::Read uvr = uv.read(); + + for(int i=0;i colors; + colors.push_back(color); + Vector indices = Geometry::triangulate_polygon(points); + + VS::get_singleton()->canvas_item_add_triangle_array(get_canvas_item(),indices,points,colors,uvs,texture.is_valid()?texture->get_rid():RID()); + + } break; + } +} + + +void Polygon2D::set_polygon(const DVector& p_polygon) { + polygon=p_polygon; + rect_cache_dirty=true; + update(); +} + +DVector Polygon2D::get_polygon() const{ + + return polygon; +} + + +void Polygon2D::set_uv(const DVector& p_uv) { + + uv=p_uv; + update(); +} + +DVector Polygon2D::get_uv() const{ + + return uv; +} + +void Polygon2D::set_color(const Color& p_color){ + + color=p_color; + update(); +} +Color Polygon2D::get_color() const{ + + return color; +} + +void Polygon2D::set_texture(const Ref& p_texture){ + + texture=p_texture; + + if (texture.is_valid()) { + uint32_t flags=texture->get_flags(); + flags&=~Texture::FLAG_REPEAT; + if (tex_tile) + flags|=Texture::FLAG_REPEAT; + + texture->set_flags(flags); + } + update(); +} +Ref Polygon2D::get_texture() const{ + + return texture; +} + + +void Polygon2D::set_texture_offset(const Vector2& p_offset){ + + tex_ofs=p_offset; + update(); +} +Vector2 Polygon2D::get_texture_offset() const{ + + return tex_ofs; +} + +void Polygon2D::set_texture_rotation(float p_rot){ + + tex_rot=p_rot; + update(); +} +float Polygon2D::get_texture_rotation() const{ + + return tex_rot; +} + +void Polygon2D::set_texture_repeat(bool p_enable){ + + tex_tile=p_enable; + if (texture.is_valid()) { + uint32_t flags=texture->get_flags(); + flags&=~Texture::FLAG_REPEAT; + if (p_enable) + flags|=Texture::FLAG_REPEAT; + texture->set_flags(flags); + } + update(); +} +bool Polygon2D::get_texture_repeat() const{ + + return tex_tile; +} + +void Polygon2D::_set_texture_rotationd(float p_rot){ + + set_texture_rotation(Math::deg2rad(p_rot)); +} +float Polygon2D::_get_texture_rotationd() const{ + + return Math::rad2deg(get_texture_rotation()); +} + + +void Polygon2D::set_texture_scale(const Vector2& p_scale){ + + tex_scale=p_scale; + update(); +} +Vector2 Polygon2D::get_texture_scale() const{ + + return tex_scale; +} + +void Polygon2D::set_invert(bool p_invert){ + + invert=p_invert; + update(); +} +bool Polygon2D::get_invert() const{ + + return invert; +} + +void Polygon2D::set_invert_border(float p_invert_border){ + + invert_border=p_invert_border; + update(); +} +float Polygon2D::get_invert_border() const{ + + return invert_border; +} + +void Polygon2D::set_offset(const Vector2& p_offset) { + + offset=p_offset; + rect_cache_dirty=true; + update(); +} + +Vector2 Polygon2D::get_offset() const { + + return offset; +} + + +void Polygon2D::_bind_methods() { + + ObjectTypeDB::bind_method(_MD("set_polygon","polygon"),&Polygon2D::set_polygon); + ObjectTypeDB::bind_method(_MD("get_polygon"),&Polygon2D::get_polygon); + + ObjectTypeDB::bind_method(_MD("set_uv","uv"),&Polygon2D::set_uv); + ObjectTypeDB::bind_method(_MD("get_uv"),&Polygon2D::get_uv); + + ObjectTypeDB::bind_method(_MD("set_color","color"),&Polygon2D::set_color); + ObjectTypeDB::bind_method(_MD("get_color"),&Polygon2D::get_color); + + ObjectTypeDB::bind_method(_MD("set_texture","texture"),&Polygon2D::set_texture); + ObjectTypeDB::bind_method(_MD("get_texture"),&Polygon2D::get_texture); + + ObjectTypeDB::bind_method(_MD("set_texture_offset","texture_offset"),&Polygon2D::set_texture_offset); + ObjectTypeDB::bind_method(_MD("get_texture_offset"),&Polygon2D::get_texture_offset); + + ObjectTypeDB::bind_method(_MD("set_texture_rotation","texture_rotation"),&Polygon2D::set_texture_rotation); + ObjectTypeDB::bind_method(_MD("get_texture_rotation"),&Polygon2D::get_texture_rotation); + + ObjectTypeDB::bind_method(_MD("_set_texture_rotationd","texture_rotation"),&Polygon2D::_set_texture_rotationd); + ObjectTypeDB::bind_method(_MD("_get_texture_rotationd"),&Polygon2D::_get_texture_rotationd); + + ObjectTypeDB::bind_method(_MD("set_texture_scale","texture_scale"),&Polygon2D::set_texture_scale); + ObjectTypeDB::bind_method(_MD("get_texture_scale"),&Polygon2D::get_texture_scale); + + ObjectTypeDB::bind_method(_MD("set_texture_repeat","enable"),&Polygon2D::set_texture_repeat); + ObjectTypeDB::bind_method(_MD("get_texture_repeat"),&Polygon2D::get_texture_repeat); + + ObjectTypeDB::bind_method(_MD("set_invert","invert"),&Polygon2D::set_invert); + ObjectTypeDB::bind_method(_MD("get_invert"),&Polygon2D::get_invert); + + ObjectTypeDB::bind_method(_MD("set_invert_border","invert_border"),&Polygon2D::set_invert_border); + ObjectTypeDB::bind_method(_MD("get_invert_border"),&Polygon2D::get_invert_border); + + ObjectTypeDB::bind_method(_MD("set_offset","offset"),&Polygon2D::set_offset); + ObjectTypeDB::bind_method(_MD("get_offset"),&Polygon2D::get_offset); + + + + ADD_PROPERTY( PropertyInfo(Variant::VECTOR2_ARRAY,"polygon"),_SCS("set_polygon"),_SCS("get_polygon")); + ADD_PROPERTY( PropertyInfo(Variant::VECTOR2_ARRAY,"uv"),_SCS("set_uv"),_SCS("get_uv")); + ADD_PROPERTY( PropertyInfo(Variant::COLOR,"color"),_SCS("set_color"),_SCS("get_color")); + ADD_PROPERTY( PropertyInfo(Variant::VECTOR2,"offset"),_SCS("set_offset"),_SCS("get_offset")); + ADD_PROPERTY( PropertyInfo(Variant::OBJECT,"texture/texture",PROPERTY_HINT_RESOURCE_TYPE,"Texture"),_SCS("set_texture"),_SCS("get_texture")); + ADD_PROPERTY( PropertyInfo(Variant::VECTOR2,"texture/offset"),_SCS("set_texture_offset"),_SCS("get_texture_offset")); + ADD_PROPERTY( PropertyInfo(Variant::VECTOR2,"texture/scale"),_SCS("set_texture_scale"),_SCS("get_texture_scale")); + ADD_PROPERTY( PropertyInfo(Variant::REAL,"texture/rotation",PROPERTY_HINT_RANGE,"-1440,1440,0.1"),_SCS("_set_texture_rotationd"),_SCS("_get_texture_rotationd")); + ADD_PROPERTY( PropertyInfo(Variant::BOOL,"texture/repeat"),_SCS("set_texture_repeat"),_SCS("get_texture_repeat")); + ADD_PROPERTY( PropertyInfo(Variant::BOOL,"invert/enable"),_SCS("set_invert"),_SCS("get_invert")); + ADD_PROPERTY( PropertyInfo(Variant::REAL,"invert/border",PROPERTY_HINT_RANGE,"0.1,16384,0.1"),_SCS("set_invert_border"),_SCS("get_invert_border")); + +} + +Polygon2D::Polygon2D() { + + invert=0; + invert_border=100; + tex_rot=0; + tex_tile=true; + tex_scale=Vector2(1,1); + rect_cache_dirty=true; +} diff --git a/scene/2d/polygon_2d.h b/scene/2d/polygon_2d.h new file mode 100644 index 00000000000..38fa57b9b44 --- /dev/null +++ b/scene/2d/polygon_2d.h @@ -0,0 +1,78 @@ +#ifndef POLYGON_2D_H +#define POLYGON_2D_H + +#include "scene/2d/node_2d.h" + +class Polygon2D : public Node2D { + + OBJ_TYPE(Polygon2D,Node2D); + + DVector polygon; + DVector uv; + Color color; + Ref texture; + Vector2 tex_scale; + Vector2 tex_ofs; + bool tex_tile; + float tex_rot; + bool invert; + float invert_border; + + Vector2 offset; + mutable bool rect_cache_dirty; + mutable Rect2 item_rect; + + void _set_texture_rotationd(float p_rot); + float _get_texture_rotationd() const; + +protected: + + void _notification(int p_what); + static void _bind_methods(); +public: + + void set_polygon(const DVector& p_polygon); + DVector get_polygon() const; + + void set_uv(const DVector& p_uv); + DVector get_uv() const; + + void set_color(const Color& p_color); + Color get_color() const; + + void set_texture(const Ref& p_texture); + Ref get_texture() const; + + void set_texture_offset(const Vector2& p_offset); + Vector2 get_texture_offset() const; + + void set_texture_rotation(float p_rot); + float get_texture_rotation() const; + + void set_texture_scale(const Vector2& p_scale); + Vector2 get_texture_scale() const; + + void set_texture_repeat(bool p_rot); + bool get_texture_repeat() const; + + void set_invert(bool p_rot); + bool get_invert() const; + + void set_invert_border(float p_border); + float get_invert_border() const; + + void set_offset(const Vector2& p_offset); + Vector2 get_offset() const; + + //editor stuff + + virtual void edit_set_pivot(const Point2& p_pivot); + virtual Point2 edit_get_pivot() const; + virtual bool edit_has_pivot() const; + + virtual Rect2 get_item_rect() const; + + Polygon2D(); +}; + +#endif // POLYGON_2D_H diff --git a/scene/register_scene_types.cpp b/scene/register_scene_types.cpp index ff8660a3874..a468f0a3791 100644 --- a/scene/register_scene_types.cpp +++ b/scene/register_scene_types.cpp @@ -82,6 +82,7 @@ #include "scene/2d/canvas_item.h" #include "scene/2d/sprite.h" #include "scene/2d/animated_sprite.h" +#include "scene/2d/polygon_2d.h" #include "scene/2d/visibility_notifier_2d.h" @@ -468,6 +469,7 @@ void register_scene_types() { ObjectTypeDB::register_type(); ObjectTypeDB::register_type(); ObjectTypeDB::register_type(); + ObjectTypeDB::register_type(); ObjectTypeDB::set_type_enabled("CollisionShape2D",false); ObjectTypeDB::set_type_enabled("CollisionPolygon2D",false); diff --git a/servers/visual/rasterizer.cpp b/servers/visual/rasterizer.cpp index 610e9a6a1bc..e160b4dccce 100644 --- a/servers/visual/rasterizer.cpp +++ b/servers/visual/rasterizer.cpp @@ -96,7 +96,7 @@ RID Rasterizer::_create_shader(const FixedMaterialShaderKey& p_key) { } else { uv_str=_TEXUVSTR(VS::FIXED_MATERIAL_PARAM_NORMAL); } - scode+="vec3 normal=tex( fmp_normal_tex,"+uv_str+").xyz * 2.0 - vec3(1.0,1.0,1.0);\n"; + scode+="vec3 normal=tex( fmp_normal_tex,"+uv_str+").xyz * vec3(2.0,2.0,1.0) - vec3(1.0,1.0,0.0);\n"; scode+="NORMAL = mix( NORMAL,mat3(TANGENT,BINORMAL,NORMAL) * normal, fmp_normal);\n"; code+=scode; } diff --git a/servers/visual/visual_server_raster.cpp b/servers/visual/visual_server_raster.cpp index f171b47e9c2..7c1f03b71ba 100644 --- a/servers/visual/visual_server_raster.cpp +++ b/servers/visual/visual_server_raster.cpp @@ -3432,7 +3432,7 @@ void VisualServerRaster::canvas_item_add_triangle_array(RID p_item, const Vector ERR_FAIL_COND(!canvas_item); int ps = p_points.size(); - ERR_FAIL_COND(!p_colors.empty() && p_colors.size()!=ps); + ERR_FAIL_COND(!p_colors.empty() && p_colors.size()!=ps && p_colors.size()!=1); ERR_FAIL_COND(!p_uvs.empty() && p_uvs.size()!=ps); Vector indices = p_indices; diff --git a/tools/editor/editor_node.cpp b/tools/editor/editor_node.cpp index a86cdbfeb58..a6243df69d3 100644 --- a/tools/editor/editor_node.cpp +++ b/tools/editor/editor_node.cpp @@ -86,6 +86,7 @@ #include "plugins/tile_set_editor_plugin.h" #include "plugins/animation_player_editor_plugin.h" #include "plugins/baked_light_editor_plugin.h" +#include "plugins/polygon_2d_editor_plugin.h" // end #include "tools/editor/io_plugins/editor_texture_import_plugin.h" #include "tools/editor/io_plugins/editor_scene_import_plugin.h" @@ -1323,6 +1324,7 @@ void EditorNode::_edit_current() { /* Take care of PLUGIN EDITOR */ + EditorPlugin *main_plugin = editor_data.get_editor(current_obj); if (main_plugin) { @@ -2759,7 +2761,7 @@ Error EditorNode::load_scene(const String& p_scene) { top_pallete->set_current_tab(0); //always go to scene - //push_item(new_scene); + push_item(new_scene); return OK; } @@ -4088,6 +4090,7 @@ EditorNode::EditorNode() { add_editor_plugin( memnew( Path2DEditorPlugin(this) ) ); add_editor_plugin( memnew( PathEditorPlugin(this) ) ); add_editor_plugin( memnew( BakedLightEditorPlugin(this) ) ); + add_editor_plugin( memnew( Polygon2DEditorPlugin(this) ) ); for(int i=0;i::Element *E=bone_list.front();E;E=E->next()) { + + Object *b = ObjectDB::get_instance(E->get().bone); + if (!b) { + viewport->update(); + break; + } + + Node2D *b2 = b->cast_to(); + if (!b2) { + continue; + } + + if (b2->get_global_transform()!=E->get().xform) { + + E->get().xform=b2->get_global_transform(); + viewport->update(); + } + } } if (p_what==NOTIFICATION_ENTER_SCENE) { diff --git a/tools/editor/plugins/canvas_item_editor_plugin.h b/tools/editor/plugins/canvas_item_editor_plugin.h index 425e691f078..15ac7b1bb3c 100644 --- a/tools/editor/plugins/canvas_item_editor_plugin.h +++ b/tools/editor/plugins/canvas_item_editor_plugin.h @@ -167,6 +167,7 @@ class CanvasItemEditor : public VBoxContainer { struct BoneList { + Matrix32 xform; Vector2 from; Vector2 to; ObjectID bone; diff --git a/tools/editor/plugins/collision_polygon_editor_plugin.cpp b/tools/editor/plugins/collision_polygon_editor_plugin.cpp index fc40320e29c..93ad918b0e0 100644 --- a/tools/editor/plugins/collision_polygon_editor_plugin.cpp +++ b/tools/editor/plugins/collision_polygon_editor_plugin.cpp @@ -96,8 +96,8 @@ void CollisionPolygonEditor::_wip_close() { undo_redo->create_action("Create Poly"); undo_redo->add_undo_method(node,"set_polygon",node->get_polygon()); undo_redo->add_do_method(node,"set_polygon",wip); - undo_redo->add_do_method(canvas_item_editor,"update"); - undo_redo->add_undo_method(canvas_item_editor,"update"); + undo_redo->add_do_method(canvas_item_editor->get_viewport_control(),"update"); + undo_redo->add_undo_method(canvas_item_editor->get_viewport_control(),"update"); undo_redo->commit_action(); wip.clear(); wip_active=false; @@ -186,8 +186,8 @@ bool CollisionPolygonEditor::forward_input_event(const InputEvent& p_event) { undo_redo->add_undo_method(node,"set_polygon",poly); poly.push_back(cpoint); undo_redo->add_do_method(node,"set_polygon",poly); - undo_redo->add_do_method(canvas_item_editor,"update"); - undo_redo->add_undo_method(canvas_item_editor,"update"); + undo_redo->add_do_method(canvas_item_editor->get_viewport_control(),"update"); + undo_redo->add_undo_method(canvas_item_editor->get_viewport_control(),"update"); undo_redo->commit_action(); return true; } @@ -265,8 +265,8 @@ bool CollisionPolygonEditor::forward_input_event(const InputEvent& p_event) { undo_redo->create_action("Edit Poly"); undo_redo->add_do_method(node,"set_polygon",poly); undo_redo->add_undo_method(node,"set_polygon",pre_move_edit); - undo_redo->add_do_method(canvas_item_editor,"update"); - undo_redo->add_undo_method(canvas_item_editor,"update"); + undo_redo->add_do_method(canvas_item_editor->get_viewport_control(),"update"); + undo_redo->add_undo_method(canvas_item_editor->get_viewport_control(),"update"); undo_redo->commit_action(); edited_point=-1; @@ -300,8 +300,8 @@ bool CollisionPolygonEditor::forward_input_event(const InputEvent& p_event) { undo_redo->add_undo_method(node,"set_polygon",poly); poly.remove(closest_idx); undo_redo->add_do_method(node,"set_polygon",poly); - undo_redo->add_do_method(canvas_item_editor,"update"); - undo_redo->add_undo_method(canvas_item_editor,"update"); + undo_redo->add_do_method(canvas_item_editor->get_viewport_control(),"update"); + undo_redo->add_undo_method(canvas_item_editor->get_viewport_control(),"update"); undo_redo->commit_action(); return true; } diff --git a/tools/editor/plugins/polygon_2d_editor_plugin.cpp b/tools/editor/plugins/polygon_2d_editor_plugin.cpp new file mode 100644 index 00000000000..7dd8dd30353 --- /dev/null +++ b/tools/editor/plugins/polygon_2d_editor_plugin.cpp @@ -0,0 +1,880 @@ +/*************************************************************************/ +/* collision_polygon_editor_plugin.cpp */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* http://www.godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ +/* */ +/* 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 "polygon_2d_editor_plugin.h" +#include "canvas_item_editor_plugin.h" +#include "os/file_access.h" +#include "tools/editor/editor_settings.h" +#include "os/keyboard.h" +#include "os/input.h" + +void Polygon2DEditor::_notification(int p_what) { + + switch(p_what) { + + case NOTIFICATION_READY: { + + button_create->set_icon( get_icon("Edit","EditorIcons")); + button_edit->set_icon( get_icon("MovePoint","EditorIcons")); + button_edit->set_pressed(true); + button_uv->set_icon( get_icon("Uv","EditorIcons")); + + uv_button[UV_MODE_EDIT_POINT]->set_icon(get_icon("ToolSelect","EditorIcons")); + uv_button[UV_MODE_MOVE]->set_icon(get_icon("ToolMove","EditorIcons")); + uv_button[UV_MODE_ROTATE]->set_icon(get_icon("ToolRotate","EditorIcons")); + uv_button[UV_MODE_SCALE]->set_icon(get_icon("ToolScale","EditorIcons")); + + + } break; + case NOTIFICATION_FIXED_PROCESS: { + + + } break; + } + +} +void Polygon2DEditor::_node_removed(Node *p_node) { + + if(p_node==node) { + node=NULL; + hide(); + } + +} + + +Vector2 Polygon2DEditor::snap_point(const Vector2& p_point) const { + + if (canvas_item_editor->is_snap_active()) { + + return p_point.snapped(Vector2(1,1)*canvas_item_editor->get_snap()); + + } else { + return p_point; + } +} + +void Polygon2DEditor::_menu_option(int p_option) { + + switch(p_option) { + + case MODE_CREATE: { + + mode=MODE_CREATE; + button_create->set_pressed(true); + button_edit->set_pressed(false); + } break; + case MODE_EDIT: { + + mode=MODE_EDIT; + button_create->set_pressed(false); + button_edit->set_pressed(true); + } break; + case MODE_EDIT_UV: { + + if (node->get_texture().is_null()) { + + error->set_text("No texture in this polygon.\nSet a texture to be able to edit UV."); + error->popup_centered_minsize(Size2(300,70)); + return; + } + + + DVector points = node->get_polygon(); + DVector uvs = node->get_uv(); + if (uvs.size()!=points.size()) { + undo_redo->create_action("Create UV Map"); + undo_redo->add_do_method(node,"set_uv",points); + undo_redo->add_undo_method(node,"set_uv",uvs); + undo_redo->add_do_method(uv_edit_draw,"update"); + undo_redo->add_undo_method(uv_edit_draw,"update"); + undo_redo->commit_action(); + + } + + + uv_edit->popup_centered_ratio(0.85); + } break; + case UVEDIT_POLYGON_TO_UV: { + + DVector points = node->get_polygon(); + if (points.size()==0) + break; + DVector uvs = node->get_uv(); + undo_redo->create_action("Create UV Map"); + undo_redo->add_do_method(node,"set_uv",points); + undo_redo->add_undo_method(node,"set_uv",uvs); + undo_redo->add_do_method(uv_edit_draw,"update"); + undo_redo->add_undo_method(uv_edit_draw,"update"); + undo_redo->commit_action(); + + + } break; + case UVEDIT_UV_TO_POLYGON: { + + DVector points = node->get_polygon(); + DVector uvs = node->get_uv(); + if (uvs.size()==0) + break; + + undo_redo->create_action("Create UV Map"); + undo_redo->add_do_method(node,"set_polygon",uvs); + undo_redo->add_undo_method(node,"set_polygon",points); + undo_redo->add_do_method(uv_edit_draw,"update"); + undo_redo->add_undo_method(uv_edit_draw,"update"); + undo_redo->commit_action(); + + } break; + case UVEDIT_UV_CLEAR: { + + DVector uvs = node->get_uv(); + if (uvs.size()==0) + break; + undo_redo->create_action("Create UV Map"); + undo_redo->add_do_method(node,"set_uv",DVector()); + undo_redo->add_undo_method(node,"set_uv",uvs); + undo_redo->add_do_method(uv_edit_draw,"update"); + undo_redo->add_undo_method(uv_edit_draw,"update"); + undo_redo->commit_action(); + + } break; + + + } +} + +void Polygon2DEditor::_wip_close() { + + undo_redo->create_action("Create Poly"); + undo_redo->add_undo_method(node,"set_polygon",node->get_polygon()); + undo_redo->add_do_method(node,"set_polygon",wip); + undo_redo->add_do_method(canvas_item_editor->get_viewport_control(),"update"); + undo_redo->add_undo_method(canvas_item_editor->get_viewport_control(),"update"); + undo_redo->commit_action(); + wip.clear(); + wip_active=false; + mode=MODE_EDIT; + button_edit->set_pressed(true); + button_create->set_pressed(false); + edited_point=-1; +} + +bool Polygon2DEditor::forward_input_event(const InputEvent& p_event) { + + + switch(p_event.type) { + + case InputEvent::MOUSE_BUTTON: { + + const InputEventMouseButton &mb=p_event.mouse_button; + + Matrix32 xform = canvas_item_editor->get_canvas_transform() * node->get_global_transform(); + + + Vector2 gpoint = Point2(mb.x,mb.y); + Vector2 cpoint = canvas_item_editor->get_canvas_transform().affine_inverse().xform(gpoint); + cpoint=snap_point(cpoint); + cpoint = node->get_global_transform().affine_inverse().xform(cpoint); + + + Vector poly = Variant(node->get_polygon()); + + //first check if a point is to be added (segment split) + real_t grab_treshold=EDITOR_DEF("poly_editor/point_grab_radius",8); + + switch(mode) { + + + case MODE_CREATE: { + + if (mb.button_index==BUTTON_LEFT && mb.pressed) { + + + if (!wip_active) { + + wip.clear(); + wip.push_back( cpoint-node->get_offset() ); + wip_active=true; + edited_point_pos=cpoint; + canvas_item_editor->get_viewport_control()->update(); + edited_point=1; + return true; + } else { + + + if (wip.size()>1 && xform.xform(wip[0]+node->get_offset()).distance_to(gpoint)get_offset() ); + edited_point=wip.size(); + canvas_item_editor->get_viewport_control()->update(); + return true; + + //add wip point + } + } + } else if (mb.button_index==BUTTON_RIGHT && mb.pressed && wip_active) { + _wip_close(); + } + + + + } break; + + case MODE_EDIT: { + + if (mb.button_index==BUTTON_LEFT) { + if (mb.pressed) { + + if (mb.mod.control) { + + + if (poly.size() < 3) { + + undo_redo->create_action("Edit Poly"); + undo_redo->add_undo_method(node,"set_polygon",poly); + poly.push_back(cpoint); + undo_redo->add_do_method(node,"set_polygon",poly); + undo_redo->add_do_method(canvas_item_editor->get_viewport_control(),"update"); + undo_redo->add_undo_method(canvas_item_editor->get_viewport_control(),"update"); + undo_redo->commit_action(); + return true; + } + + //search edges + int closest_idx=-1; + Vector2 closest_pos; + real_t closest_dist=1e10; + for(int i=0;iget_offset()), + xform.xform(poly[(i+1)%poly.size()]+node->get_offset()) }; + + Vector2 cp = Geometry::get_closest_point_to_segment_2d(gpoint,points); + if (cp.distance_squared_to(points[0])=0) { + + pre_move_edit=poly; + poly.insert(closest_idx+1,xform.affine_inverse().xform(closest_pos)-node->get_offset()); + edited_point=closest_idx+1; + edited_point_pos=xform.affine_inverse().xform(closest_pos); + node->set_polygon(Variant(poly)); + canvas_item_editor->get_viewport_control()->update(); + return true; + } + } else { + + //look for points to move + + int closest_idx=-1; + Vector2 closest_pos; + real_t closest_dist=1e10; + for(int i=0;iget_offset()); + + real_t d = cp.distance_to(gpoint); + if (d=0) { + + pre_move_edit=poly; + edited_point=closest_idx; + edited_point_pos=xform.affine_inverse().xform(closest_pos); + canvas_item_editor->get_viewport_control()->update(); + return true; + } + } + } else { + + if (edited_point!=-1) { + + //apply + + ERR_FAIL_INDEX_V(edited_point,poly.size(),false); + poly[edited_point]=edited_point_pos-node->get_offset(); + undo_redo->create_action("Edit Poly"); + undo_redo->add_do_method(node,"set_polygon",poly); + undo_redo->add_undo_method(node,"set_polygon",pre_move_edit); + undo_redo->add_do_method(canvas_item_editor->get_viewport_control(),"update"); + undo_redo->add_undo_method(canvas_item_editor->get_viewport_control(),"update"); + undo_redo->commit_action(); + + edited_point=-1; + return true; + } + } + } if (mb.button_index==BUTTON_RIGHT && mb.pressed && edited_point==-1) { + + + + int closest_idx=-1; + Vector2 closest_pos; + real_t closest_dist=1e10; + for(int i=0;iget_offset()); + + real_t d = cp.distance_to(gpoint); + if (d=0) { + + + undo_redo->create_action("Edit Poly (Remove Point)"); + undo_redo->add_undo_method(node,"set_polygon",poly); + poly.remove(closest_idx); + undo_redo->add_do_method(node,"set_polygon",poly); + undo_redo->add_do_method(canvas_item_editor->get_viewport_control(),"update"); + undo_redo->add_undo_method(canvas_item_editor->get_viewport_control(),"update"); + undo_redo->commit_action(); + return true; + } + + } + + + + } break; + } + + + + } break; + case InputEvent::MOUSE_MOTION: { + + const InputEventMouseMotion &mm=p_event.mouse_motion; + + if (edited_point!=-1 && (wip_active || mm.button_mask&BUTTON_MASK_LEFT)) { + + Vector2 gpoint = Point2(mm.x,mm.y); + Vector2 cpoint = canvas_item_editor->get_canvas_transform().affine_inverse().xform(gpoint); + cpoint=snap_point(cpoint); + edited_point_pos = node->get_global_transform().affine_inverse().xform(cpoint); + + canvas_item_editor->get_viewport_control()->update(); + + } + + } break; + } + + return false; +} +void Polygon2DEditor::_canvas_draw() { + + if (!node) + return; + + Control *vpc = canvas_item_editor->get_viewport_control(); + + Vector poly; + + if (wip_active) + poly=wip; + else + poly=Variant(node->get_polygon()); + + + Matrix32 xform = canvas_item_editor->get_canvas_transform() * node->get_global_transform(); + Ref handle= get_icon("EditorHandle","EditorIcons"); + + int len = poly.size(); + + for(int i=0;iget_offset()); + if ((wip_active && i==poly.size()-1) || (((i+1)%poly.size())==edited_point)) + p2=edited_point_pos; + else + p2 = poly[(i+1)%poly.size()]+node->get_offset(); + + Vector2 point = xform.xform(p); + Vector2 next_point = xform.xform(p2); + + Color col=Color(1,0.3,0.1,0.8); + vpc->draw_line(point,next_point,col,2); + vpc->draw_texture(handle,point-handle->get_size()*0.5); + } +} + + +void Polygon2DEditor::_uv_mode(int p_mode) { + + + uv_mode=UVMode(p_mode); + for(int i=0;iset_pressed(p_mode==i); + } +} + + +void Polygon2DEditor::_uv_input(const InputEvent& p_input) { + + + Matrix32 mtx; + mtx.elements[2]=-uv_draw_ofs; + mtx.scale_basis(Vector2(uv_draw_zoom,uv_draw_zoom)); + + if (p_input.type==InputEvent::MOUSE_BUTTON) { + + + const InputEventMouseButton &mb=p_input.mouse_button; + + if (mb.button_index==BUTTON_LEFT) { + + + if (mb.pressed) { + + uv_drag_from=Vector2(mb.x,mb.y); + uv_drag=true; + uv_prev=node->get_uv(); + uv_move_current=uv_mode; + if (uv_move_current==UV_MODE_EDIT_POINT) { + + if (mb.mod.shift && mb.mod.command) + uv_move_current=UV_MODE_SCALE; + else if (mb.mod.shift) + uv_move_current=UV_MODE_MOVE; + else if (mb.mod.command) + uv_move_current=UV_MODE_ROTATE; + } + + if (uv_move_current==UV_MODE_EDIT_POINT) { + + uv_drag_index=-1; + for(int i=0;icreate_action("Transform UV Map"); + undo_redo->add_do_method(node,"set_uv",node->get_uv()); + undo_redo->add_undo_method(node,"set_uv",uv_prev); + undo_redo->add_do_method(uv_edit_draw,"update"); + undo_redo->add_undo_method(uv_edit_draw,"update"); + undo_redo->commit_action(); + + uv_drag=false; + } + + } else if (mb.button_index==BUTTON_RIGHT && mb.pressed) { + + if (uv_drag) { + + uv_drag=false; + node->set_uv(uv_prev); + uv_edit_draw->update(); + } + + } else if (mb.button_index==BUTTON_WHEEL_UP && mb.pressed) { + + uv_zoom->set_val( uv_zoom->get_val()/0.9 ); + } else if (mb.button_index==BUTTON_WHEEL_DOWN && mb.pressed) { + + uv_zoom->set_val( uv_zoom->get_val()*0.9); + } + + } else if (p_input.type==InputEvent::MOUSE_MOTION) { + + const InputEventMouseMotion &mm=p_input.mouse_motion; + + if (mm.button_mask&BUTTON_MASK_MIDDLE || Input::get_singleton()->is_key_pressed(KEY_SPACE)) { + + Vector2 drag(mm.relative_x,mm.relative_y); + uv_hscroll->set_val( uv_hscroll->get_val()-drag.x ); + uv_vscroll->set_val( uv_vscroll->get_val()-drag.y ); + + } else if (uv_drag) { + + Vector2 uv_drag_to(mm.x,mm.y); + Vector2 drag = mtx.affine_inverse().xform(uv_drag_to) - mtx.affine_inverse().xform(uv_drag_from); + + + switch(uv_move_current) { + + case UV_MODE_EDIT_POINT: { + + DVector uv_new=uv_prev; + uv_new.set( uv_drag_index, uv_new[uv_drag_index]+drag ); + node->set_uv(uv_new); + } break; + case UV_MODE_MOVE: { + + DVector uv_new=uv_prev; + for(int i=0;iset_uv(uv_new); + + + } break; + case UV_MODE_ROTATE: { + + Vector2 center; + DVector uv_new=uv_prev; + + for(int i=0;iset_uv(uv_new); + + } break; + case UV_MODE_SCALE: { + + Vector2 center; + DVector uv_new=uv_prev; + + for(int i=0;iset_uv(uv_new); + } break; + + + } + uv_edit_draw->update(); + } + + } + +} + + +void Polygon2DEditor::_uv_scroll_changed(float) { + + if (updating_uv_scroll) + return; + + uv_draw_ofs.x=uv_hscroll->get_val(); + uv_draw_ofs.y=uv_vscroll->get_val(); + uv_draw_zoom=uv_zoom->get_val(); + uv_edit_draw->update(); +} + +void Polygon2DEditor::_uv_draw() { + + Ref base_tex = node->get_texture(); + if (base_tex.is_null()) + return; + + Matrix32 mtx; + mtx.elements[2]=-uv_draw_ofs; + mtx.scale_basis(Vector2(uv_draw_zoom,uv_draw_zoom)); + + VS::get_singleton()->canvas_item_set_clip(uv_edit_draw->get_canvas_item(),true); + VS::get_singleton()->canvas_item_add_set_transform(uv_edit_draw->get_canvas_item(),mtx); + uv_edit_draw->draw_texture(base_tex,Point2()); + VS::get_singleton()->canvas_item_add_set_transform(uv_edit_draw->get_canvas_item(),Matrix32()); + + DVector uvs = node->get_uv(); + Ref handle = get_icon("EditorHandle","EditorIcons"); + + Rect2 rect(Point2(),mtx.basis_xform(base_tex->get_size())); + rect.expand_to(mtx.basis_xform(uv_edit_draw->get_size())); + + for(int i=0;idraw_line(mtx.xform(uvs[i]),mtx.xform(uvs[next]),Color(0.9,0.5,0.5),2); + uv_edit_draw->draw_texture(handle,mtx.xform(uvs[i])-handle->get_size()*0.5); + rect.expand_to(mtx.basis_xform(uvs[i])); + } + + rect=rect.grow(200); + updating_uv_scroll=true; + uv_hscroll->set_min(rect.pos.x); + uv_hscroll->set_max(rect.pos.x+rect.size.x); + uv_hscroll->set_page(uv_edit_draw->get_size().x); + uv_hscroll->set_val(uv_draw_ofs.x); + uv_hscroll->set_step(0.001); + + uv_vscroll->set_min(rect.pos.y); + uv_vscroll->set_max(rect.pos.y+rect.size.y); + uv_vscroll->set_page(uv_edit_draw->get_size().y); + uv_vscroll->set_val(uv_draw_ofs.y); + uv_vscroll->set_step(0.001); + updating_uv_scroll=false; + +} + +void Polygon2DEditor::edit(Node *p_collision_polygon) { + + if (!canvas_item_editor) { + canvas_item_editor=CanvasItemEditor::get_singleton(); + } + + + if (p_collision_polygon) { + + node=p_collision_polygon->cast_to(); + if (!canvas_item_editor->get_viewport_control()->is_connected("draw",this,"_canvas_draw")) + canvas_item_editor->get_viewport_control()->connect("draw",this,"_canvas_draw"); + wip.clear(); + wip_active=false; + edited_point=-1; + + } else { + node=NULL; + + if (canvas_item_editor->get_viewport_control()->is_connected("draw",this,"_canvas_draw")) + canvas_item_editor->get_viewport_control()->disconnect("draw",this,"_canvas_draw"); + + } + +} + +void Polygon2DEditor::_bind_methods() { + + ObjectTypeDB::bind_method(_MD("_menu_option"),&Polygon2DEditor::_menu_option); + ObjectTypeDB::bind_method(_MD("_canvas_draw"),&Polygon2DEditor::_canvas_draw); + ObjectTypeDB::bind_method(_MD("_uv_mode"),&Polygon2DEditor::_uv_mode); + ObjectTypeDB::bind_method(_MD("_uv_draw"),&Polygon2DEditor::_uv_draw); + ObjectTypeDB::bind_method(_MD("_uv_input"),&Polygon2DEditor::_uv_input); + ObjectTypeDB::bind_method(_MD("_uv_scroll_changed"),&Polygon2DEditor::_uv_scroll_changed); + + +} + +Polygon2DEditor::Polygon2DEditor(EditorNode *p_editor) { + + canvas_item_editor=NULL; + editor=p_editor; + undo_redo = editor->get_undo_redo(); + + add_child( memnew( VSeparator )); + button_create = memnew( ToolButton ); + add_child(button_create); + button_create->connect("pressed",this,"_menu_option",varray(MODE_CREATE)); + button_create->set_toggle_mode(true); + + button_edit = memnew( ToolButton ); + add_child(button_edit); + button_edit->connect("pressed",this,"_menu_option",varray(MODE_EDIT)); + button_edit->set_toggle_mode(true); + + button_uv = memnew( ToolButton ); + add_child(button_uv); + button_uv->connect("pressed",this,"_menu_option",varray(MODE_EDIT_UV)); + + //add_constant_override("separation",0); + +#if 0 + options = memnew( MenuButton ); + add_child(options); + options->set_area_as_parent_rect(); + options->set_text("Polygon"); + //options->get_popup()->add_item("Parse BBCODE",PARSE_BBCODE); + options->get_popup()->connect("item_pressed", this,"_menu_option"); +#endif + + mode = MODE_EDIT; + wip_active=false; + + uv_mode=UV_MODE_EDIT_POINT; + uv_edit = memnew( AcceptDialog ); + add_child(uv_edit); + uv_edit->set_title("Polygon 2D UV Editor"); + uv_edit->set_self_opacity(0.9); + + VBoxContainer *uv_main_vb = memnew( VBoxContainer ); + uv_edit->add_child(uv_main_vb); + uv_edit->set_child_rect(uv_main_vb); + HBoxContainer *uv_mode_hb = memnew( HBoxContainer ); + uv_main_vb->add_child(uv_mode_hb); + for(int i=0;iset_toggle_mode(true); + uv_mode_hb->add_child(uv_button[i]); + uv_button[i]->connect("pressed",this,"_uv_mode",varray(i)); + uv_button[i]->set_focus_mode(FOCUS_NONE); + } + + uv_button[0]->set_tooltip("Move Point\nCtrl: Rotate\nShift: Move All\n:Shift+Ctrl: Scale"); + uv_button[1]->set_tooltip("Move Polygon"); + uv_button[2]->set_tooltip("Rotate Polygon"); + uv_button[3]->set_tooltip("Scale Polygon"); + + uv_button[0]->set_pressed(true); + HBoxContainer *uv_main_hb = memnew( HBoxContainer ); + uv_main_vb->add_child(uv_main_hb); + uv_edit_draw = memnew( Control ); + uv_main_hb->add_child(uv_edit_draw); + uv_main_hb->set_v_size_flags(SIZE_EXPAND_FILL); + uv_edit_draw->set_h_size_flags(SIZE_EXPAND_FILL); + uv_menu = memnew( MenuButton ); + uv_mode_hb->add_child(uv_menu); + uv_menu->set_text("Edit"); + uv_menu->get_popup()->add_item("Polygon->UV",UVEDIT_POLYGON_TO_UV); + uv_menu->get_popup()->add_item("UV->Polygon",UVEDIT_UV_TO_POLYGON); + uv_menu->get_popup()->add_separator(); + uv_menu->get_popup()->add_item("Clear UV",UVEDIT_UV_CLEAR); + uv_menu->get_popup()->connect("item_pressed",this,"_menu_option"); + uv_mode_hb->add_child( memnew( VSeparator )); + uv_icon_zoom = memnew( TextureFrame ); + uv_main_hb->add_child( uv_icon_zoom ); + uv_zoom = memnew( HSlider ); + uv_zoom->set_min(0.01); + uv_zoom->set_max(4); + uv_zoom->set_val(1); + uv_zoom->set_step(0.01); + uv_mode_hb->add_child(uv_zoom); + uv_zoom->set_custom_minimum_size(Size2(200,0)); + uv_zoom_value = memnew( SpinBox ); + uv_zoom->share(uv_zoom_value); + uv_zoom_value->set_custom_minimum_size(Size2(50,0)); + uv_mode_hb->add_child(uv_zoom_value); + uv_zoom->connect("value_changed",this,"_uv_scroll_changed"); + + + + uv_vscroll = memnew( VScrollBar); + uv_main_hb->add_child(uv_vscroll); + uv_vscroll->connect("value_changed",this,"_uv_scroll_changed"); + uv_hscroll = memnew( HScrollBar ); + uv_main_vb->add_child(uv_hscroll); + uv_hscroll->connect("value_changed",this,"_uv_scroll_changed"); + + uv_edit_draw->connect("draw",this,"_uv_draw"); + uv_edit_draw->connect("input_event",this,"_uv_input"); + uv_draw_zoom=1.0; + uv_drag_index=-1; + uv_drag=false; + updating_uv_scroll=false; + + error = memnew( AcceptDialog); + add_child(error); + +} + + +void Polygon2DEditorPlugin::edit(Object *p_object) { + + + collision_polygon_editor->edit(p_object->cast_to()); +} + +bool Polygon2DEditorPlugin::handles(Object *p_object) const { + + return p_object->is_type("Polygon2D"); +} + +void Polygon2DEditorPlugin::make_visible(bool p_visible) { + + if (p_visible) { + collision_polygon_editor->show(); + } else { + + collision_polygon_editor->hide(); + collision_polygon_editor->edit(NULL); + } + +} + +Polygon2DEditorPlugin::Polygon2DEditorPlugin(EditorNode *p_node) { + + editor=p_node; + collision_polygon_editor = memnew( Polygon2DEditor(p_node) ); + CanvasItemEditor::get_singleton()->add_control_to_menu_panel(collision_polygon_editor); + + collision_polygon_editor->hide(); + +} + + +Polygon2DEditorPlugin::~Polygon2DEditorPlugin() +{ +} + diff --git a/tools/editor/plugins/polygon_2d_editor_plugin.h b/tools/editor/plugins/polygon_2d_editor_plugin.h new file mode 100644 index 00000000000..88d1c204934 --- /dev/null +++ b/tools/editor/plugins/polygon_2d_editor_plugin.h @@ -0,0 +1,123 @@ +#ifndef POLYGON_2D_EDITOR_PLUGIN_H +#define POLYGON_2D_EDITOR_PLUGIN_H + +#include "tools/editor/editor_plugin.h" +#include "tools/editor/editor_node.h" +#include "scene/2d/polygon_2d.h" +#include "scene/gui/tool_button.h" +#include "scene/gui/button_group.h" + +/** + @author Juan Linietsky +*/ +class CanvasItemEditor; + +class Polygon2DEditor : public HBoxContainer { + + OBJ_TYPE(Polygon2DEditor, HBoxContainer ); + + UndoRedo *undo_redo; + enum Mode { + + MODE_CREATE, + MODE_EDIT, + MODE_EDIT_UV, + UVEDIT_POLYGON_TO_UV, + UVEDIT_UV_TO_POLYGON, + UVEDIT_UV_CLEAR + + }; + + enum UVMode { + UV_MODE_EDIT_POINT, + UV_MODE_MOVE, + UV_MODE_ROTATE, + UV_MODE_SCALE, + UV_MODE_MAX + }; + + Mode mode; + + UVMode uv_mode; + AcceptDialog *uv_edit; + ToolButton *uv_button[4]; + Control *uv_edit_draw; + HSlider *uv_zoom; + SpinBox *uv_zoom_value; + HScrollBar *uv_hscroll; + VScrollBar *uv_vscroll; + MenuButton *uv_menu; + TextureFrame *uv_icon_zoom; + + Vector2 uv_draw_ofs; + float uv_draw_zoom; + DVector uv_prev; + int uv_drag_index; + bool uv_drag; + UVMode uv_move_current; + Vector2 uv_drag_from; + bool updating_uv_scroll; + + + + AcceptDialog *error; + + ToolButton *button_create; + ToolButton *button_edit; + ToolButton *button_uv; + + CanvasItemEditor *canvas_item_editor; + EditorNode *editor; + Panel *panel; + Polygon2D *node; + MenuButton *options; + + int edited_point; + Vector2 edited_point_pos; + Vector pre_move_edit; + Vector wip; + bool wip_active; + + void _uv_scroll_changed(float); + void _uv_input(const InputEvent& p_input); + void _uv_draw(); + void _uv_mode(int p_mode); + void _wip_close(); + void _canvas_draw(); + void _menu_option(int p_option); + +protected: + void _notification(int p_what); + void _node_removed(Node *p_node); + static void _bind_methods(); +public: + + Vector2 snap_point(const Vector2& p_point) const; + bool forward_input_event(const InputEvent& p_event); + void edit(Node *p_collision_polygon); + Polygon2DEditor(EditorNode *p_editor); +}; + +class Polygon2DEditorPlugin : public EditorPlugin { + + OBJ_TYPE( Polygon2DEditorPlugin, EditorPlugin ); + + Polygon2DEditor *collision_polygon_editor; + EditorNode *editor; + +public: + + virtual bool forward_input_event(const InputEvent& p_event) { return collision_polygon_editor->forward_input_event(p_event); } + + virtual String get_name() const { return "Polygon2D"; } + bool has_main_screen() const { return false; } + virtual void edit(Object *p_node); + virtual bool handles(Object *p_node) const; + virtual void make_visible(bool p_visible); + + Polygon2DEditorPlugin(EditorNode *p_node); + ~Polygon2DEditorPlugin(); + +}; + +#endif // POLYGON_2D_EDITOR_PLUGIN_H