Lots of work on porting PropTool, and the editor plugin itself.

This commit is contained in:
Relintai 2020-02-27 23:58:23 +01:00
parent 6178cf92d6
commit 2bf00fd547
4 changed files with 357 additions and 120 deletions

View File

@ -1,7 +1,18 @@
#include "prop_tool.h" #include "prop_tool.h"
#include "../props/prop_data.h" #include "../props/prop_data.h"
#include "../props/prop_data_entity.h"
#include "../props/prop_data_light.h"
#include "../props/prop_data_mesh.h"
#include "../props/prop_data_prop.h" #include "../props/prop_data_prop.h"
#include "../props/prop_data_scene.h"
#include "prop_tool_entity.h"
#include "prop_tool_light.h"
#include "prop_tool_mesh.h"
#include "prop_tool_prop.h"
#include "prop_tool_scene.h"
#include "core/io/resource_saver.h" #include "core/io/resource_saver.h"
#include "prop_tool_editor_plugin.h" #include "prop_tool_editor_plugin.h"
@ -68,60 +79,65 @@ void PropTool::save_node(Node *node, Transform parent_transform) {
} }
void PropTool::rebuild_hierarchy() { void PropTool::rebuild_hierarchy() {
/* for (int i = 0; i < get_child_count(); ++i)
for ch in get_children(): get_child(i)->queue_delete();
ch.queue_free()
if (!_target_prop.is_valid())
if target_prop == null: return;
return
_snap_to_mesh = _target_prop->get_snap_to_mesh();
snap_to_mesh = target_prop.snap_to_mesh _snap_axis = _target_prop->get_snap_axis();
snap_axis = target_prop.snap_axis
for (int i = 0; i < _target_prop->get_prop_count(); ++i) {
for i in range(target_prop.get_prop_count()): Ref<PropDataEntry> prop = _target_prop->get_prop(i);
print(i)
var prop : PropDataEntry = target_prop.get_prop(i) Ref<PropDataLight> prop_light = prop;
Ref<PropDataMesh> prop_mesh = prop;
if prop is PropDataLight: Ref<PropDataScene> prop_scene = prop;
var l : PropToolLight = PropToolLight.new() Ref<PropDataProp> prop_prop = prop;
Ref<PropDataEntity> prop_entity = prop;
add_child(l)
l.owner = self if (prop_light.is_valid()) {
l.transform = prop.transform PropToolLight *l = memnew(PropToolLight);
l.set_data(prop as PropDataLight) add_child(l);
elif prop is PropDataMesh: l->set_owner(this);
var m : PropToolMesh = PropToolMesh.new() l->set_transform(prop->get_transform());
add_child(m) l->set_data(prop_light);
m.owner = self } else if (prop_mesh.is_valid()) {
m.transform = prop.transform PropToolMesh *m = memnew(PropToolMesh);
m.set_data(prop as PropDataMesh) add_child(m);
elif prop is PropDataScene: m->set_owner(this);
var s : PropToolScene = PropToolScene.new() m->set_transform(prop->get_transform());
add_child(s) m->set_data(prop_mesh);
s.owner = self } else if (prop_scene.is_valid()) {
s.transform = prop.transform PropToolScene *s = memnew(PropToolScene);
s.set_data(prop as PropDataScene) add_child(s);
elif prop is PropDataProp: s->set_owner(this);
var s : Node = plugin.create_or_get_scene(prop.prop) s->set_transform(prop->get_transform());
add_child(s) s->set_data(prop_scene);
s.owner = self } else if (prop_prop.is_valid()) {
s.transform = prop.transform PropTool *s = _plugin->create_or_get_scene(prop_prop->get_prop());
#s.set_target_prop(prop.prop)
elif prop is PropDataEntity: add_child(s);
var s : PropToolEntity = PropToolEntity.new() s->set_owner(this);
s->set_transform(prop->get_transform());
add_child(s) s->set_target_prop(prop_prop->get_prop());
s.owner = self } else if (prop_entity.is_valid()) {
s.transform = prop.transform PropToolEntity *s = memnew(PropToolEntity);
s.set_data(prop as PropDataEntity) add_child(s);
*/ s->set_owner(this);
s->set_transform(prop->get_transform());
s->set_data(prop_entity);
}
}
} }
void PropTool::refresh_set(bool value) { void PropTool::refresh_set(bool value) {
@ -167,9 +183,9 @@ void PropTool::load_scene_for(PropTool *t, const Ref<PropData> &prop) {
t->queue_delete(); t->queue_delete();
//Node *s = _plugin->create_or_get_scene(prop); Node *s = _plugin->create_or_get_scene(prop);
//add_child(s); add_child(s);
//s->set_owner(this); //s->set_owner(this);
} }

View File

@ -32,6 +32,7 @@ class PropData;
#include "scene/3d/spatial.h" #include "scene/3d/spatial.h"
class propData; class propData;
class PropToolEditorPlugin;
class PropTool : public Spatial { class PropTool : public Spatial {
GDCLASS(PropTool, Spatial); GDCLASS(PropTool, Spatial);
@ -60,7 +61,7 @@ private:
Ref<PropData> _target_prop; Ref<PropData> _target_prop;
bool _snap_to_mesh; bool _snap_to_mesh;
Vector3 _snap_axis; Vector3 _snap_axis;
EditorPlugin *_plugin; PropToolEditorPlugin *_plugin;
}; };
#endif #endif

View File

@ -24,65 +24,286 @@ SOFTWARE.
#include "editor/editor_scale.h" #include "editor/editor_scale.h"
void PropDataEditor::edit(const Ref<PropData> &prop) { void PropToolEditorPlugin::edit(Object *p_object) {
Ref<PropData> pedited_prop(p_object);
String p = create_or_get_scene_path(pedited_prop);
get_editor_interface()->open_scene_from_path(p);
} }
bool PropToolEditorPlugin::handles(Object *p_object) const {
void PropDataEditor::_bind_methods() {
}
PropDataEditor::PropDataEditor(EditorNode *p_editor) {
set_h_size_flags(SIZE_EXPAND_FILL);
set_custom_minimum_size(Size2(0, 200) * EDSCALE);
Tree *tree = memnew(Tree);
tree->set_custom_minimum_size(Size2(100, 0) * EDSCALE);
add_child(tree);
ViewportContainer *container = memnew(ViewportContainer);
container->set_h_size_flags(SIZE_EXPAND_FILL);
container->set_h_size_flags(SIZE_EXPAND_FILL);
container->set_v_size_flags(SIZE_EXPAND_FILL);
add_child(container);
Viewport *viewport = memnew(Viewport);
viewport->set_size(Size2(400, 400));
container->add_child(viewport);
set_split_offset(200 * EDSCALE);
}
PropDataEditor::~PropDataEditor() {
}
void PropDataEditorPlugin::edit(Object *p_object) {
}
bool PropDataEditorPlugin::handles(Object *p_object) const {
bool handles = p_object->is_class("PropData"); bool handles = p_object->is_class("PropData");
if (handles) { if (handles) {
prop_editor_button->show(); light_button->show();
mesh_button->show();
prop_button->show();
scene_button->show();
entity_button->show();
} else { } else {
prop_editor_button->hide(); light_button->hide();
mesh_button->hide();
prop_button->hide();
scene_button->hide();
entity_button->hide();
} }
return handles; return handles;
} }
void PropDataEditorPlugin::make_visible(bool p_visible) { void PropToolEditorPlugin::make_visible(bool p_visible) {
} }
PropDataEditorPlugin::PropDataEditorPlugin(EditorNode *p_node) { String PropToolEditorPlugin::create_or_get_scene_path(const Ref<PropData> &data) {
String path = temp_path + data->get_path().get_file().get_basename() + ".tscn";
//Directory dir : Directory = Directory.new()
//if not dir.file_exists(path):
// create_scene(data)
return path
}
PropTool *PropToolEditorPlugin::create_or_get_scene(const Ref<PropData> &data) {
/*
ERR_FAIL_COND_V(!data.is_valid(), NULL);
String path = temp_path + data->get_path().get_file().get_basename() + ".tscn";
var dir : Directory = Directory.new();
Ref<PackedScene> ps;
if not dir.file_exists(path):;
ps = create_scene(data);
else:;
ps = ResourceLoader.load(path, "PackedScene");
if ps == null:;
return null;
var pt : PropTool = ps.instance() as PropTool;
pt.plugin = self;
return pt;
*/
return NULL;
}
Ref<PackedScene> PropToolEditorPlugin::create_scene(const Ref<PropData> &data) {
/*
func create_scene(data: PropData) -> PackedScene:
var pt : PropTool = PropTool.new()
pt.plugin = self
pt.set_target_prop(data)
var ps : PackedScene = PackedScene.new()
ps.pack(pt)
var err = ResourceSaver.save(temp_path + data.resource_path.get_file().get_basename() + ".tscn", ps)
pt.queue_free()
return ps
*/
return Ref<PackedScene>();
}
PropToolEditorPlugin::PropToolEditorPlugin(EditorNode *p_node) {
temp_path = "res://addons/prop_tool/scenes/temp/";
editor = p_node; editor = p_node;
prop_editor = memnew(PropDataEditor(editor)); light_button = memnew(ToolButton);
light_button->set_text("Light");
light_button->connect("pressed", this, "add_light");
prop_editor_button = add_control_to_bottom_panel(prop_editor, "Prop"); mesh_button = memnew(ToolButton);
mesh_button->set_text("Mesh");
mesh_button->connect("pressed", this, "add_mesh");
prop_editor->hide(); prop_button = memnew(ToolButton);
prop_button->set_text("Prop");
prop_button->connect("pressed", this, "add_prop");
scene_button = memnew(ToolButton);
scene_button->set_text("Scene");
scene_button->connect("pressed", this, "add_scene");
entity_button = memnew(ToolButton);
entity_button->set_text("Entity");
entity_button->connect("pressed", this, "add_entity");
add_control_to_container(EditorPlugin::CONTAINER_SPATIAL_EDITOR_MENU, light_button);
add_control_to_container(EditorPlugin::CONTAINER_SPATIAL_EDITOR_MENU, mesh_button);
add_control_to_container(EditorPlugin::CONTAINER_SPATIAL_EDITOR_MENU, prop_button);
add_control_to_container(EditorPlugin::CONTAINER_SPATIAL_EDITOR_MENU, scene_button);
add_control_to_container(EditorPlugin::CONTAINER_SPATIAL_EDITOR_MENU, entity_button);
light_button->hide();
mesh_button->hide();
prop_button->hide();
scene_button->hide();
entity_button->hide();
//connect("scene_changed", this, "scene_changed");
} }
PropDataEditorPlugin::~PropDataEditorPlugin() { PropToolEditorPlugin::~PropToolEditorPlugin() {
_edited_prop.unref();
} }
/*
func scene_changed(scene):
if scene.has_method("set_target_prop"):
scene.plugin = self
if not buttons_added:
light_button.show()
mesh_button.show()
prop_button.show()
scene_button.show()
entity_button.show()
# add_control_to_container(EditorPlugin.CONTAINER_SPATIAL_EDITOR_MENU, light_button)
# add_control_to_container(EditorPlugin.CONTAINER_SPATIAL_EDITOR_MENU, mesh_button)
# add_control_to_container(EditorPlugin.CONTAINER_SPATIAL_EDITOR_MENU, prop_button)
# add_control_to_container(EditorPlugin.CONTAINER_SPATIAL_EDITOR_MENU, scene_button)
# add_control_to_container(EditorPlugin.CONTAINER_SPATIAL_EDITOR_MENU, entity_button)
buttons_added = true
else:
if buttons_added:
light_button.hide()
mesh_button.hide()
prop_button.hide()
scene_button.hide()
entity_button.hide()
#
# remove_control_from_container(EditorPlugin.CONTAINER_SPATIAL_EDITOR_MENU, light_button)
# remove_control_from_container(EditorPlugin.CONTAINER_SPATIAL_EDITOR_MENU, mesh_button)
# remove_control_from_container(EditorPlugin.CONTAINER_SPATIAL_EDITOR_MENU, prop_button)
# remove_control_from_container(EditorPlugin.CONTAINER_SPATIAL_EDITOR_MENU, scene_button)
# remove_control_from_container(EditorPlugin.CONTAINER_SPATIAL_EDITOR_MENU, entity_button)
buttons_added = false
func apply_changes() -> void:
var scene : Node = get_editor_interface().get_edited_scene_root()
if scene is PropTool:
# if scene.has_method("set_target_prop") and scene.has_method("save"):
scene.save()
func add_light():
var selection : Array = get_editor_interface().get_selection().get_selected_nodes()
var selected : Node
if selection.size() != 1:
selected = get_editor_interface().get_edited_scene_root()
else:
selected = selection[0]
var s : Node = selected
var n = PropToolLight.new()
var u : UndoRedo = get_undo_redo()
u.create_action("Add Light")
u.add_do_method(s, "add_child", n)
u.add_do_property(n, "owner", get_editor_interface().get_edited_scene_root())
u.add_undo_method(s, "remove_child", n)
u.commit_action()
get_editor_interface().get_selection().clear()
get_editor_interface().get_selection().add_node(n)
func add_mesh():
var selected : Array = get_editor_interface().get_selection().get_selected_nodes()
if selected.size() != 1:
return
var s : Node = selected[0]
var n = PropToolMesh.new()
var u : UndoRedo = get_undo_redo()
u.create_action("Add Mesh")
u.add_do_method(s, "add_child", n)
u.add_do_property(n, "owner", get_editor_interface().get_edited_scene_root())
u.add_undo_method(s, "remove_child", n)
u.commit_action()
get_editor_interface().get_selection().clear()
get_editor_interface().get_selection().add_node(n)
func add_prop():
var selected : Array = get_editor_interface().get_selection().get_selected_nodes()
if selected.size() != 1:
return
var s : Node = selected[0]
var n = PropTool.new()
var u : UndoRedo = get_undo_redo()
u.create_action("Add Prop")
u.add_do_method(s, "add_child", n)
u.add_do_property(n, "owner", get_editor_interface().get_edited_scene_root())
u.add_undo_method(s, "remove_child", n)
u.commit_action()
get_editor_interface().get_selection().clear()
get_editor_interface().get_selection().add_node(n)
func add_scene():
var selected : Array = get_editor_interface().get_selection().get_selected_nodes()
if selected.size() != 1:
return
var s : Node = selected[0]
var n = PropToolScene.new()
var u : UndoRedo = get_undo_redo()
u.create_action("Add Scene")
u.add_do_method(s, "add_child", n)
u.add_do_property(n, "owner", get_editor_interface().get_edited_scene_root())
u.add_undo_method(s, "remove_child", n)
u.commit_action()
get_editor_interface().get_selection().clear()
get_editor_interface().get_selection().add_node(n)
func add_entity():
var selected : Array = get_editor_interface().get_selection().get_selected_nodes()
if selected.size() != 1:
return
var s : Node = selected[0]
var n = PropToolEntity.new()
var u : UndoRedo = get_undo_redo()
u.create_action("Add Entity")
u.add_do_method(s, "add_child", n)
u.add_do_property(n, "owner", get_editor_interface().get_edited_scene_root())
u.add_undo_method(s, "remove_child", n)
u.commit_action()
get_editor_interface().get_selection().clear()
get_editor_interface().get_selection().add_node(n)
*/

View File

@ -36,25 +36,8 @@ SOFTWARE.
#include "editor/plugins/spatial_editor_plugin.h" #include "editor/plugins/spatial_editor_plugin.h"
class PropDataEditor : public HSplitContainer { class PropToolEditorPlugin : public EditorPlugin {
GDCLASS(PropDataEditor, HSplitContainer); GDCLASS(PropToolEditorPlugin, EditorPlugin);
public:
void edit(const Ref<PropData> &prop);
PropDataEditor() {}
PropDataEditor(EditorNode *p_editor);
~PropDataEditor();
protected:
static void _bind_methods();
private:
};
class PropDataEditorPlugin : public EditorPlugin {
GDCLASS(PropDataEditorPlugin, EditorPlugin);
public: public:
virtual String get_name() const { return "PropData"; } virtual String get_name() const { return "PropData"; }
@ -63,14 +46,30 @@ public:
virtual bool handles(Object *p_object) const; virtual bool handles(Object *p_object) const;
virtual void make_visible(bool p_visible); virtual void make_visible(bool p_visible);
PropDataEditorPlugin(EditorNode *p_node); String create_or_get_scene_path(const Ref<PropData> &data);
~PropDataEditorPlugin(); PropTool *create_or_get_scene(const Ref<PropData> &data);
Ref<PackedScene> create_scene(const Ref<PropData> &data);
PropToolEditorPlugin(EditorNode *p_node);
~PropToolEditorPlugin();
protected: protected:
void _notification(int p_what);
private: private:
PropDataEditor *prop_editor; String temp_path;
ToolButton *prop_editor_button;
ToolButton *light_button;
ToolButton *mesh_button;
ToolButton *prop_button;
ToolButton *scene_button;
ToolButton *entity_button;
ToolButton *edited_prop;
EditorNode *editor; EditorNode *editor;
Ref<PropData> _edited_prop;
}; };
#endif #endif