Skip to content

Commit

Permalink
2D Skeletons WORK IN PROGRESS
Browse files Browse the repository at this point in the history
  • Loading branch information
reduz committed Feb 21, 2018
1 parent 612ab4b commit 7cd867c
Show file tree
Hide file tree
Showing 17 changed files with 488 additions and 26 deletions.
2 changes: 1 addition & 1 deletion drivers/dummy/rasterizer_dummy.h
Original file line number Diff line number Diff line change
Expand Up @@ -619,7 +619,7 @@ class RasterizerCanvasDummy : public RasterizerCanvas {
void canvas_begin(){};
void canvas_end(){};

void canvas_render_items(Item *p_item_list, int p_z, const Color &p_modulate, Light *p_light){};
void canvas_render_items(Item *p_item_list, int p_z, const Color &p_modulate, Light *p_light,const Transform2D& p_transform){};
void canvas_debug_viewport_shadows(Light *p_lights_with_shadow){};

void canvas_light_shadow_buffer_update(RID p_buffer, const Transform2D &p_light_xform, int p_light_mask, float p_near, float p_far, LightOccluderInstance *p_occluders, CameraMatrix *p_xform_cache) {}
Expand Down
2 changes: 1 addition & 1 deletion drivers/gles3/rasterizer_canvas_gles3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1042,7 +1042,7 @@ void RasterizerCanvasGLES3::_copy_texscreen(const Rect2 &p_rect) {
glEnable(GL_BLEND);
}

void RasterizerCanvasGLES3::canvas_render_items(Item *p_item_list, int p_z, const Color &p_modulate, Light *p_light) {
void RasterizerCanvasGLES3::canvas_render_items(Item *p_item_list, int p_z, const Color &p_modulate, Light *p_light, const Transform2D &p_transform) {

Item *current_clip = NULL;
RasterizerStorageGLES3::Shader *shader_cache = NULL;
Expand Down
2 changes: 1 addition & 1 deletion drivers/gles3/rasterizer_canvas_gles3.h
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ class RasterizerCanvasGLES3 : public RasterizerCanvas {
_FORCE_INLINE_ void _canvas_item_render_commands(Item *p_item, Item *current_clip, bool &reclip);
_FORCE_INLINE_ void _copy_texscreen(const Rect2 &p_rect);

virtual void canvas_render_items(Item *p_item_list, int p_z, const Color &p_modulate, Light *p_light);
virtual void canvas_render_items(Item *p_item_list, int p_z, const Color &p_modulate, Light *p_light, const Transform2D &p_transform);
virtual void canvas_debug_viewport_shadows(Light *p_lights_with_shadow);

virtual void canvas_light_shadow_buffer_update(RID p_buffer, const Transform2D &p_light_xform, int p_light_mask, float p_near, float p_far, LightOccluderInstance *p_occluders, CameraMatrix *p_xform_cache);
Expand Down
2 changes: 2 additions & 0 deletions editor/editor_node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@
#include "editor/plugins/script_text_editor.h"
#include "editor/plugins/shader_editor_plugin.h"
#include "editor/plugins/shader_graph_editor_plugin.h"
#include "editor/plugins/skeleton_2d_editor_plugin.h"
#include "editor/plugins/spatial_editor_plugin.h"
#include "editor/plugins/sprite_editor_plugin.h"
#include "editor/plugins/sprite_frames_editor_plugin.h"
Expand Down Expand Up @@ -5673,6 +5674,7 @@ EditorNode::EditorNode() {
add_editor_plugin(memnew(MeshLibraryEditorPlugin(this)));
add_editor_plugin(memnew(StyleBoxEditorPlugin(this)));
add_editor_plugin(memnew(SpriteEditorPlugin(this)));
add_editor_plugin(memnew(Skeleton2DEditorPlugin(this)));
add_editor_plugin(memnew(ParticlesEditorPlugin(this)));
add_editor_plugin(memnew(ResourcePreloaderEditorPlugin(this)));
add_editor_plugin(memnew(ItemListEditorPlugin(this)));
Expand Down
115 changes: 115 additions & 0 deletions editor/plugins/skeleton_2d_editor_plugin.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
#include "skeleton_2d_editor_plugin.h"

#include "canvas_item_editor_plugin.h"
#include "scene/2d/mesh_instance_2d.h"
#include "scene/gui/box_container.h"
#include "thirdparty/misc/clipper.hpp"

void Skeleton2DEditor::_node_removed(Node *p_node) {

if (p_node == node) {
node = NULL;
options->hide();
}
}

void Skeleton2DEditor::edit(Skeleton2D *p_sprite) {

node = p_sprite;
}

void Skeleton2DEditor::_menu_option(int p_option) {

switch (p_option) {
case MENU_OPTION_MAKE_REST: {

if (node->get_bone_count() == 0) {
err_dialog->set_text(TTR("This skeleton has no bones, create some children Bone2D nodes."));
err_dialog->popup_centered_minsize();
return;
}
UndoRedo *ur = EditorNode::get_singleton()->get_undo_redo();
ur->create_action("Create Rest Pose from Bones");
for (int i = 0; i < node->get_bone_count(); i++) {
Bone2D *bone = node->get_bone(i);
ur->add_do_method(bone, "set_rest", bone->get_transform());
ur->add_undo_method(bone, "set_rest", bone->get_rest());
}
ur->commit_action();

} break;
case MENU_OPTION_SET_REST: {
if (node->get_bone_count() == 0) {
err_dialog->set_text(TTR("This skeleton has no bones, create some children Bone2D nodes."));
err_dialog->popup_centered_minsize();
return;
}
UndoRedo *ur = EditorNode::get_singleton()->get_undo_redo();
ur->create_action("Set Rest Pose to Bones");
for (int i = 0; i < node->get_bone_count(); i++) {
Bone2D *bone = node->get_bone(i);
ur->add_do_method(bone, "set_transform", bone->get_rest());
ur->add_undo_method(bone, "set_transform", bone->get_transform());
}
ur->commit_action();

} break;
}
}

void Skeleton2DEditor::_bind_methods() {

ClassDB::bind_method("_menu_option", &Skeleton2DEditor::_menu_option);
}

Skeleton2DEditor::Skeleton2DEditor() {

options = memnew(MenuButton);

CanvasItemEditor::get_singleton()->add_control_to_menu_panel(options);

options->set_text(TTR("Skeleton2D"));
options->set_icon(EditorNode::get_singleton()->get_gui_base()->get_icon("Skeleton2D", "EditorIcons"));

options->get_popup()->add_item(TTR("Make Rest Pose (From Bones)"), MENU_OPTION_MAKE_REST);
options->get_popup()->add_separator();
options->get_popup()->add_item(TTR("Set Bones to Rest Pose"), MENU_OPTION_SET_REST);

options->get_popup()->connect("id_pressed", this, "_menu_option");

err_dialog = memnew(AcceptDialog);
add_child(err_dialog);
}

void Skeleton2DEditorPlugin::edit(Object *p_object) {

sprite_editor->edit(Object::cast_to<Skeleton2D>(p_object));
}

bool Skeleton2DEditorPlugin::handles(Object *p_object) const {

return p_object->is_class("Skeleton2D");
}

void Skeleton2DEditorPlugin::make_visible(bool p_visible) {

if (p_visible) {
sprite_editor->options->show();
} else {

sprite_editor->options->hide();
sprite_editor->edit(NULL);
}
}

Skeleton2DEditorPlugin::Skeleton2DEditorPlugin(EditorNode *p_node) {

editor = p_node;
sprite_editor = memnew(Skeleton2DEditor);
editor->get_viewport()->add_child(sprite_editor);

//sprite_editor->options->hide();
}

Skeleton2DEditorPlugin::~Skeleton2DEditorPlugin() {
}
55 changes: 55 additions & 0 deletions editor/plugins/skeleton_2d_editor_plugin.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#ifndef SKELETON_2D_EDITOR_PLUGIN_H
#define SKELETON_2D_EDITOR_PLUGIN_H

#include "editor/editor_node.h"
#include "editor/editor_plugin.h"
#include "scene/2d/skeleton_2d.h"
#include "scene/gui/spin_box.h"

class Skeleton2DEditor : public Control {

GDCLASS(Skeleton2DEditor, Control);

enum Menu {
MENU_OPTION_MAKE_REST,
MENU_OPTION_SET_REST,
};

Skeleton2D *node;

MenuButton *options;
AcceptDialog *err_dialog;

void _menu_option(int p_option);

//void _create_uv_lines();
friend class Skeleton2DEditorPlugin;

protected:
void _node_removed(Node *p_node);
static void _bind_methods();

public:
void edit(Skeleton2D *p_sprite);
Skeleton2DEditor();
};

class Skeleton2DEditorPlugin : public EditorPlugin {

GDCLASS(Skeleton2DEditorPlugin, EditorPlugin);

Skeleton2DEditor *sprite_editor;
EditorNode *editor;

public:
virtual String get_name() const { return "Skeleton2D"; }
bool has_main_screen() const { return false; }
virtual void edit(Object *p_object);
virtual bool handles(Object *p_object) const;
virtual void make_visible(bool p_visible);

Skeleton2DEditorPlugin(EditorNode *p_node);
~Skeleton2DEditorPlugin();
};

#endif // SKELETON_2D_EDITOR_PLUGIN_H
6 changes: 3 additions & 3 deletions scene/2d/canvas_item.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -779,13 +779,13 @@ void CanvasItem::draw_colored_polygon(const Vector<Point2> &p_points, const Colo
VisualServer::get_singleton()->canvas_item_add_polygon(canvas_item, p_points, colors, p_uvs, rid, rid_normal, p_antialiased);
}

void CanvasItem::draw_mesh(const Ref<Mesh> &p_mesh, const Ref<Texture> &p_texture, const Ref<Texture> &p_normal_map, RID p_skeleton) {
void CanvasItem::draw_mesh(const Ref<Mesh> &p_mesh, const Ref<Texture> &p_texture, const Ref<Texture> &p_normal_map) {

ERR_FAIL_COND(p_mesh.is_null());
RID texture_rid = p_texture.is_valid() ? p_texture->get_rid() : RID();
RID normal_map_rid = p_normal_map.is_valid() ? p_normal_map->get_rid() : RID();

VisualServer::get_singleton()->canvas_item_add_mesh(canvas_item, p_mesh->get_rid(), texture_rid, normal_map_rid, p_skeleton);
VisualServer::get_singleton()->canvas_item_add_mesh(canvas_item, p_mesh->get_rid(), texture_rid, normal_map_rid);
}
void CanvasItem::draw_multimesh(const Ref<MultiMesh> &p_multimesh, const Ref<Texture> &p_texture, const Ref<Texture> &p_normal_map) {

Expand Down Expand Up @@ -1032,7 +1032,7 @@ void CanvasItem::_bind_methods() {
ClassDB::bind_method(D_METHOD("draw_colored_polygon", "points", "color", "uvs", "texture", "normal_map", "antialiased"), &CanvasItem::draw_colored_polygon, DEFVAL(PoolVector2Array()), DEFVAL(Variant()), DEFVAL(Variant()), DEFVAL(false));
ClassDB::bind_method(D_METHOD("draw_string", "font", "position", "text", "modulate", "clip_w"), &CanvasItem::draw_string, DEFVAL(Color(1, 1, 1)), DEFVAL(-1));
ClassDB::bind_method(D_METHOD("draw_char", "font", "position", "char", "next", "modulate"), &CanvasItem::draw_char, DEFVAL(Color(1, 1, 1)));
ClassDB::bind_method(D_METHOD("draw_mesh", "mesh", "texture", "normal_map", "skeleton"), &CanvasItem::draw_mesh, DEFVAL(Ref<Texture>()), DEFVAL(RID()));
ClassDB::bind_method(D_METHOD("draw_mesh", "mesh", "texture", "normal_map"), &CanvasItem::draw_mesh, DEFVAL(Ref<Texture>()));
ClassDB::bind_method(D_METHOD("draw_multimesh", "mesh", "texture", "normal_map"), &CanvasItem::draw_mesh, DEFVAL(Ref<Texture>()));

ClassDB::bind_method(D_METHOD("draw_set_transform", "position", "rotation", "scale"), &CanvasItem::draw_set_transform);
Expand Down
2 changes: 1 addition & 1 deletion scene/2d/canvas_item.h
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ class CanvasItem : public Node {
void draw_polygon(const Vector<Point2> &p_points, const Vector<Color> &p_colors, const Vector<Point2> &p_uvs = Vector<Point2>(), Ref<Texture> p_texture = Ref<Texture>(), const Ref<Texture> &p_normal_map = Ref<Texture>(), bool p_antialiased = false);
void draw_colored_polygon(const Vector<Point2> &p_points, const Color &p_color, const Vector<Point2> &p_uvs = Vector<Point2>(), Ref<Texture> p_texture = Ref<Texture>(), const Ref<Texture> &p_normal_map = Ref<Texture>(), bool p_antialiased = false);

void draw_mesh(const Ref<Mesh> &p_mesh, const Ref<Texture> &p_texture, const Ref<Texture> &p_normal_map, RID p_skeleton);
void draw_mesh(const Ref<Mesh> &p_mesh, const Ref<Texture> &p_texture, const Ref<Texture> &p_normal_map);
void draw_multimesh(const Ref<MultiMesh> &p_multimesh, const Ref<Texture> &p_texture, const Ref<Texture> &p_normal_map);

void draw_string(const Ref<Font> &p_font, const Point2 &p_pos, const String &p_text, const Color &p_modulate = Color(1, 1, 1), int p_clip_w = -1);
Expand Down
2 changes: 1 addition & 1 deletion scene/2d/mesh_instance_2d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ void MeshInstance2D::_notification(int p_what) {

if (p_what == NOTIFICATION_DRAW) {
if (mesh.is_valid()) {
draw_mesh(mesh, texture, normal_map, RID());
draw_mesh(mesh, texture, normal_map);
}
}
}
Expand Down
Loading

0 comments on commit 7cd867c

Please sign in to comment.