Initial cleanups for MMNode.

This commit is contained in:
Relintai 2022-06-07 01:56:45 +02:00
parent 4c5c313b10
commit 4589a29066
2 changed files with 80 additions and 130 deletions

View File

@ -1,28 +1,14 @@
#include "mm_node.h" #include "mm_node.h"
#include "modules/material_maker/nodes/mm_node_universal_property.h"
Vector2 MMNode::get_graph_position() { Vector2 MMNode::get_graph_position() {
return graph_position; return graph_position;
} }
void MMNode::set_graph_position(const Vector2 &val) { void MMNode::set_graph_position(const Vector2 &pos) {
graph_position = val; graph_position = pos;
} emit_changed();
Array MMNode::get_input_properties() {
return input_properties;
}
void MMNode::set_input_properties(const Array &val) {
input_properties = val;
}
Array MMNode::get_output_properties() {
return output_properties;
}
void MMNode::set_output_properties(const Array &val) {
output_properties = val;
} }
bool MMNode::get_properties_initialized() const { bool MMNode::get_properties_initialized() const {
@ -38,25 +24,23 @@ bool MMNode::get_dirty() const {
} }
void MMNode::set_dirty(const bool val) { void MMNode::set_dirty(const bool val) {
bool changed = val != dirty;
dirty = val; dirty = val;
if (changed) {
emit_changed();
}
} }
//tool; bool MMNode::render(const Ref<MMMaterial> &material) {
//export(Vector2) ;
Vector2 graph_position = Vector2();
Array input_properties = ;
Array output_properties = ;
bool properties_initialized = false;
bool dirty = true;
//MMMateial;
bool MMNode::render(const Variant &material) {
if (!dirty) { if (!dirty) {
return false; return false;
} }
for (p in input_properties) { for (int i = 0; i < input_properties.size(); ++i) {
if (p.input_property && p.input_property.owner.dirty) { Ref<MMNodeUniversalProperty> p = input_properties[i];
if (p->get_input_property().is_valid() && p->get_input_property()->get_owner()->get_dirty()) {
return false; return false;
} }
} }
@ -66,30 +50,30 @@ bool MMNode::render(const Variant &material) {
return true; return true;
} }
//MMMateial; void MMNode::_render(const Ref<MMMaterial> &material) {
void MMNode::_render(const Variant &material) {
pass;
} }
Image MMNode::render_image(const Variant &material) { Ref<Image> MMNode::render_image(const Ref<MMMaterial> &material) {
Ref<Image> image = Image.new(); Ref<Image> image;
image.create(material.image_size.x, material.image_size.y, false, Image.FORMAT_RGBA8); image.instance();
image.lock();
float w = image.get_width();
float h = image.get_height();
float pseed = randf() + randi();
for (int x = 0; x < image.get_width(); ++x) { //x in range(image.get_width()) image->create(material->image_size.x, material->image_size.y, false, Image::FORMAT_RGBA8);
image->lock();
for (int y = 0; y < image.get_height(); ++y) { //y in range(image.get_height()) float w = image->get_width();
float h = image->get_height();
float pseed = Math::randf() + Math::rand();
for (int x = 0; x < image->get_width(); ++x) { //x in range(image.get_width())
for (int y = 0; y < image->get_height(); ++y) { //y in range(image.get_height())
Vector2 v = Vector2(x / w, y / h); Vector2 v = Vector2(x / w, y / h);
Color col = get_value_for(v, pseed); Color col = get_value_for(v, pseed);
image.set_pixel(x, y, col); image->set_pixel(x, y, col);
} }
} }
image.unlock(); image->unlock();
return image; return image;
} }
@ -105,7 +89,6 @@ void MMNode::init_properties() {
} }
void MMNode::_init_properties() { void MMNode::_init_properties() {
pass;
} }
void MMNode::register_methods(const Variant &mm_graph_node) { void MMNode::register_methods(const Variant &mm_graph_node) {
@ -114,62 +97,51 @@ void MMNode::register_methods(const Variant &mm_graph_node) {
} }
void MMNode::_register_methods(const Variant &mm_graph_node) { void MMNode::_register_methods(const Variant &mm_graph_node) {
pass;
} }
Vector2 MMNode::get_graph_position() { void MMNode::register_input_property(const Ref<MMNodeUniversalProperty> &p_prop) {
return graph_position; Ref<MMNodeUniversalProperty> prop = p_prop;
}
void MMNode::set_graph_position(const Vector2 &pos) { prop->set_owner(this);
graph_position = pos;
emit_changed();
}
void MMNode::register_input_property(const MMNodeUniversalProperty &prop) { if (!prop->is_connected("changed", this, "on_input_property_changed")) {
prop.owner = self; prop->connect("changed", this, "on_input_property_changed");
if (!prop.is_connected("changed", self, "on_input_property_changed")) {
prop.connect("changed", self, "on_input_property_changed");
} }
input_properties.append(prop); input_properties.push_back(prop);
} }
void MMNode::unregister_input_property(const MMNodeUniversalProperty &prop) { void MMNode::unregister_input_property(const Ref<MMNodeUniversalProperty> &p_prop) {
if (prop.owner == self) { Ref<MMNodeUniversalProperty> prop = p_prop;
prop.owner = null;
if (prop->get_owner() == this) {
prop->set_owner(nullptr);
} }
if (prop.is_connected("changed", self, "on_input_property_changed")) { if (prop->is_connected("changed", this, "on_input_property_changed")) {
prop.disconnect("changed", self, "on_input_property_changed"); prop->disconnect("changed", this, "on_input_property_changed");
} }
input_properties.erase(prop); input_properties.erase(prop);
} }
void MMNode::register_output_property(const MMNodeUniversalProperty &prop) { void MMNode::register_output_property(const Ref<MMNodeUniversalProperty> &p_prop) {
prop.owner = self; Ref<MMNodeUniversalProperty> prop = p_prop;
output_properties.append(prop);
prop->set_owner(this);
output_properties.push_back(prop);
} }
void MMNode::unregister_output_property(const MMNodeUniversalProperty &prop) { void MMNode::unregister_output_property(const Ref<MMNodeUniversalProperty> &p_prop) {
if (prop.owner == self) { Ref<MMNodeUniversalProperty> prop = p_prop;
prop.owner = null;
if (prop->get_owner() == this) {
prop->set_owner(nullptr);
} }
output_properties.erase(prop); output_properties.erase(prop);
} }
void MMNode::set_dirty(const bool val) {
bool changed = val != dirty;
dirty = val;
if (changed) {
emit_changed();
}
}
void MMNode::on_input_property_changed() { void MMNode::on_input_property_changed() {
set_dirty(true); set_dirty(true);
emit_changed(); emit_changed();
@ -187,12 +159,8 @@ void MMNode::on_input_property_changed() {
//func editor_register_node_script(category : String, script_path : String); //func editor_register_node_script(category : String, script_path : String);
// same as the above, but for scripts; // same as the above, but for scripts;
//func editor_unregister_node_script(category : String, cls : String); //func editor_unregister_node_script(category : String, cls : String);
}
MMNode::MMNode() { MMNode::MMNode() {
graph_position = Vector2();
input_properties = ;
output_properties = ;
properties_initialized = false; properties_initialized = false;
dirty = true; dirty = true;
} }
@ -200,19 +168,11 @@ MMNode::MMNode() {
MMNode::~MMNode() { MMNode::~MMNode() {
} }
static void MMNode::_bind_methods() { void MMNode::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_graph_position"), &MMNode::get_graph_position); ClassDB::bind_method(D_METHOD("get_graph_position"), &MMNode::get_graph_position);
ClassDB::bind_method(D_METHOD("set_graph_position", "value"), &MMNode::set_graph_position); ClassDB::bind_method(D_METHOD("set_graph_position", "value"), &MMNode::set_graph_position);
ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "graph_position"), "set_graph_position", "get_graph_position"); ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "graph_position"), "set_graph_position", "get_graph_position");
ClassDB::bind_method(D_METHOD("get_input_properties"), &MMNode::get_input_properties);
ClassDB::bind_method(D_METHOD("set_input_properties", "value"), &MMNode::set_input_properties);
ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "input_properties"), "set_input_properties", "get_input_properties");
ClassDB::bind_method(D_METHOD("get_output_properties"), &MMNode::get_output_properties);
ClassDB::bind_method(D_METHOD("set_output_properties", "value"), &MMNode::set_output_properties);
ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "output_properties"), "set_output_properties", "get_output_properties");
ClassDB::bind_method(D_METHOD("get_properties_initialized"), &MMNode::get_properties_initialized); ClassDB::bind_method(D_METHOD("get_properties_initialized"), &MMNode::get_properties_initialized);
ClassDB::bind_method(D_METHOD("set_properties_initialized", "value"), &MMNode::set_properties_initialized); ClassDB::bind_method(D_METHOD("set_properties_initialized", "value"), &MMNode::set_properties_initialized);
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "properties_initialized"), "set_properties_initialized", "get_properties_initialized"); ADD_PROPERTY(PropertyInfo(Variant::BOOL, "properties_initialized"), "set_properties_initialized", "get_properties_initialized");

View File

@ -1,6 +1,16 @@
#ifndef MM_NODE_H #ifndef MM_NODE_H
#define MM_NODE_H #define MM_NODE_H
#include "core/image.h"
#include "core/math/vector2.h"
#include "core/math/vector3.h"
#include "core/variant.h"
#include "core/resource.h"
class MMMaterial;
class MMNodeUniversalProperty;
class MMNode : public Resource { class MMNode : public Resource {
GDCLASS(MMNode, Resource); GDCLASS(MMNode, Resource);
@ -8,33 +18,29 @@ public:
Vector2 get_graph_position(); Vector2 get_graph_position();
void set_graph_position(const Vector2 &val); void set_graph_position(const Vector2 &val);
Array get_input_properties();
void set_input_properties(const Array &val);
Array get_output_properties();
void set_output_properties(const Array &val);
bool get_properties_initialized() const; bool get_properties_initialized() const;
void set_properties_initialized(const bool val); void set_properties_initialized(const bool val);
bool get_dirty() const; bool get_dirty() const;
void set_dirty(const bool val); void set_dirty(const bool val);
bool render(const Variant &material); bool render(const Ref<MMMaterial> &material);
void _render(const Variant &material); void _render(const Ref<MMMaterial> &material);
Image render_image(const Variant &material); Ref<Image> render_image(const Ref<MMMaterial> &material);
Color get_value_for(const Vector2 &uv, const int pseed); Color get_value_for(const Vector2 &uv, const int pseed);
void init_properties(); void init_properties();
void _init_properties(); void _init_properties();
void register_methods(const Variant &mm_graph_node); void register_methods(const Variant &mm_graph_node);
void _register_methods(const Variant &mm_graph_node); void _register_methods(const Variant &mm_graph_node);
Vector2 get_graph_position();
void set_graph_position(const Vector2 &pos); void register_input_property(const Ref<MMNodeUniversalProperty> &prop);
void register_input_property(const MMNodeUniversalProperty &prop); void unregister_input_property(const Ref<MMNodeUniversalProperty> &prop);
void unregister_input_property(const MMNodeUniversalProperty &prop);
void register_output_property(const MMNodeUniversalProperty &prop); void register_output_property(const Ref<MMNodeUniversalProperty> &prop);
void unregister_output_property(const MMNodeUniversalProperty &prop); void unregister_output_property(const Ref<MMNodeUniversalProperty> &prop);
void set_dirty(const bool val);
void on_input_property_changed(); void on_input_property_changed();
MMNode(); MMNode();
@ -43,27 +49,11 @@ public:
protected: protected:
static void _bind_methods(); static void _bind_methods();
//tool Vector2 graph_position;
//export(Vector2) Vector<Ref<MMNodeUniversalProperty>> input_properties;
Vector2 graph_position = Vector2(); Vector<Ref<MMNodeUniversalProperty>> output_properties;
Array input_properties = ; bool properties_initialized;
Array output_properties = ; bool dirty;
bool properties_initialized = false;
bool dirty = true;
//MMMateial
//MMMateial
// Add it to the MMAlgos bing class instead.
// Not a perfect fit, but a better fit.
//func editor_register_node_class(category : String, cls : String)
// -> c++ method, adds node to the editor gui (add button)
// in gdscript a plugin should instance an MMNode and call it to populate the add menu
// with MMNodes
// in c++ it should have a static counterpart.
// register_types should populate c++ types with this
//func editor_unregister_node_class(category : String, cls : String)
//func editor_register_node_script(category : String, script_path : String)
// same as the above, but for scripts
//func editor_unregister_node_script(category : String, cls : String)
}; };
#endif #endif