From cfd84625f0dbd5590e94ecf851bd7b7db5c7665b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20Verschelde?= Date: Tue, 24 Mar 2020 09:54:49 +0100 Subject: Move DocData and Collada out of their subfolders Now that the unused DocDump was removed, the `editor/doc` subfolder is redundant. Similarly, there's no reason for Collada to have a subfolder for itself when glTF or OBJ don't. --- editor/SCsub | 4 +- editor/collada/SCsub | 5 - editor/collada/collada.cpp | 2582 ------------------------------ editor/collada/collada.h | 647 -------- editor/doc/SCsub | 5 - editor/doc/doc_data.cpp | 1217 -------------- editor/doc/doc_data.h | 117 -- editor/doc_data.cpp | 1217 ++++++++++++++ editor/doc_data.h | 117 ++ editor/editor_help.h | 2 +- editor/import/collada.cpp | 2582 ++++++++++++++++++++++++++++++ editor/import/collada.h | 647 ++++++++ editor/import/editor_import_collada.cpp | 2 +- main/main.cpp | 4 +- modules/gdscript/language_server/lsp.hpp | 2 +- modules/mono/editor/bindings_generator.h | 2 +- 16 files changed, 4570 insertions(+), 4582 deletions(-) delete mode 100644 editor/collada/SCsub delete mode 100644 editor/collada/collada.cpp delete mode 100644 editor/collada/collada.h delete mode 100644 editor/doc/SCsub delete mode 100644 editor/doc/doc_data.cpp delete mode 100644 editor/doc/doc_data.h create mode 100644 editor/doc_data.cpp create mode 100644 editor/doc_data.h create mode 100644 editor/import/collada.cpp create mode 100644 editor/import/collada.h diff --git a/editor/SCsub b/editor/SCsub index e54383350e..6e8679b770 100644 --- a/editor/SCsub +++ b/editor/SCsub @@ -56,7 +56,7 @@ if env['tools']: except OSError: pass - _make_doc_data_class_path(os.path.join(env.Dir('#').abspath, "editor/doc")) + _make_doc_data_class_path(os.path.join(env.Dir('#').abspath, "editor")) docs = sorted(docs) env.Depends("#editor/doc_data_compressed.gen.h", docs) @@ -85,8 +85,6 @@ if env['tools']: env.add_source_files(env.editor_sources, "*.cpp") - SConscript('collada/SCsub') - SConscript('doc/SCsub') SConscript('debugger/SCsub') SConscript('fileserver/SCsub') SConscript('icons/SCsub') diff --git a/editor/collada/SCsub b/editor/collada/SCsub deleted file mode 100644 index 2b1e889fb0..0000000000 --- a/editor/collada/SCsub +++ /dev/null @@ -1,5 +0,0 @@ -#!/usr/bin/env python - -Import('env') - -env.add_source_files(env.editor_sources, "*.cpp") diff --git a/editor/collada/collada.cpp b/editor/collada/collada.cpp deleted file mode 100644 index 8ef9d17083..0000000000 --- a/editor/collada/collada.cpp +++ /dev/null @@ -1,2582 +0,0 @@ -/*************************************************************************/ -/* collada.cpp */ -/*************************************************************************/ -/* This file is part of: */ -/* GODOT ENGINE */ -/* https://godotengine.org */ -/*************************************************************************/ -/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */ -/* */ -/* 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 "collada.h" - -#include - -//#define DEBUG_DEFAULT_ANIMATION -//#define DEBUG_COLLADA -#ifdef DEBUG_COLLADA -#define COLLADA_PRINT(m_what) print_line(m_what) -#else -#define COLLADA_PRINT(m_what) -#endif - -#define COLLADA_IMPORT_SCALE_SCENE - -/* HELPERS */ - -String Collada::Effect::get_texture_path(const String &p_source, Collada &state) const { - - const String &image = p_source; - ERR_FAIL_COND_V(!state.state.image_map.has(image), ""); - return state.state.image_map[image].path; -} - -Transform Collada::get_root_transform() const { - - Transform unit_scale_transform; -#ifndef COLLADA_IMPORT_SCALE_SCENE - unit_scale_transform.scale(Vector3(state.unit_scale, state.unit_scale, state.unit_scale)); -#endif - return unit_scale_transform; -} - -void Collada::Vertex::fix_unit_scale(Collada &state) { -#ifdef COLLADA_IMPORT_SCALE_SCENE - vertex *= state.state.unit_scale; -#endif -} - -static String _uri_to_id(const String &p_uri) { - - if (p_uri.begins_with("#")) - return p_uri.substr(1, p_uri.size() - 1); - else - return p_uri; -} - -/** HELPER FUNCTIONS **/ - -Transform Collada::fix_transform(const Transform &p_transform) { - - Transform tr = p_transform; - -#ifndef NO_UP_AXIS_SWAP - - if (state.up_axis != Vector3::AXIS_Y) { - - for (int i = 0; i < 3; i++) - SWAP(tr.basis[1][i], tr.basis[state.up_axis][i]); - for (int i = 0; i < 3; i++) - SWAP(tr.basis[i][1], tr.basis[i][state.up_axis]); - - SWAP(tr.origin[1], tr.origin[state.up_axis]); - - tr.basis[state.up_axis][0] = -tr.basis[state.up_axis][0]; - tr.basis[state.up_axis][1] = -tr.basis[state.up_axis][1]; - tr.basis[0][state.up_axis] = -tr.basis[0][state.up_axis]; - tr.basis[1][state.up_axis] = -tr.basis[1][state.up_axis]; - tr.origin[state.up_axis] = -tr.origin[state.up_axis]; - } -#endif - - //tr.scale(Vector3(state.unit_scale.unit_scale.unit_scale)); - return tr; - //return state.matrix_fix * p_transform; -} - -static Transform _read_transform_from_array(const Vector &array, int ofs = 0) { - - Transform tr; - // i wonder why collada matrices are transposed, given that's opposed to opengl.. - tr.basis.elements[0][0] = array[0 + ofs]; - tr.basis.elements[0][1] = array[1 + ofs]; - tr.basis.elements[0][2] = array[2 + ofs]; - tr.basis.elements[1][0] = array[4 + ofs]; - tr.basis.elements[1][1] = array[5 + ofs]; - tr.basis.elements[1][2] = array[6 + ofs]; - tr.basis.elements[2][0] = array[8 + ofs]; - tr.basis.elements[2][1] = array[9 + ofs]; - tr.basis.elements[2][2] = array[10 + ofs]; - tr.origin.x = array[3 + ofs]; - tr.origin.y = array[7 + ofs]; - tr.origin.z = array[11 + ofs]; - return tr; -} - -/* STRUCTURES */ - -Transform Collada::Node::compute_transform(Collada &state) const { - - Transform xform; - - for (int i = 0; i < xform_list.size(); i++) { - - Transform xform_step; - const XForm &xf = xform_list[i]; - switch (xf.op) { - - case XForm::OP_ROTATE: { - if (xf.data.size() >= 4) { - - xform_step.rotate(Vector3(xf.data[0], xf.data[1], xf.data[2]), Math::deg2rad(xf.data[3])); - } - } break; - case XForm::OP_SCALE: { - - if (xf.data.size() >= 3) { - - xform_step.scale(Vector3(xf.data[0], xf.data[1], xf.data[2])); - } - - } break; - case XForm::OP_TRANSLATE: { - - if (xf.data.size() >= 3) { - - xform_step.origin = Vector3(xf.data[0], xf.data[1], xf.data[2]); - } - - } break; - case XForm::OP_MATRIX: { - - if (xf.data.size() >= 16) { - xform_step = _read_transform_from_array(xf.data, 0); - } - - } break; - default: { - } - } - - xform = xform * xform_step; - } - -#ifdef COLLADA_IMPORT_SCALE_SCENE - xform.origin *= state.state.unit_scale; -#endif - return xform; -} - -Transform Collada::Node::get_transform() const { - - return default_transform; -} - -Transform Collada::Node::get_global_transform() const { - - if (parent) - return parent->get_global_transform() * default_transform; - else - return default_transform; -} - -Vector Collada::AnimationTrack::get_value_at_time(float p_time) const { - - ERR_FAIL_COND_V(keys.size() == 0, Vector()); - int i = 0; - - for (i = 0; i < keys.size(); i++) { - - if (keys[i].time > p_time) - break; - } - - if (i == 0) - return keys[0].data; - if (i == keys.size()) - return keys[keys.size() - 1].data; - - switch (keys[i].interp_type) { - - case INTERP_BEZIER: //wait for bezier - case INTERP_LINEAR: { - - float c = (p_time - keys[i - 1].time) / (keys[i].time - keys[i - 1].time); - - if (keys[i].data.size() == 16) { - //interpolate a matrix - Transform src = _read_transform_from_array(keys[i - 1].data); - Transform dst = _read_transform_from_array(keys[i].data); - - Transform interp = c < 0.001 ? src : src.interpolate_with(dst, c); - - Vector ret; - ret.resize(16); - Transform tr; - // i wonder why collada matrices are transposed, given that's opposed to opengl.. - ret.write[0] = interp.basis.elements[0][0]; - ret.write[1] = interp.basis.elements[0][1]; - ret.write[2] = interp.basis.elements[0][2]; - ret.write[4] = interp.basis.elements[1][0]; - ret.write[5] = interp.basis.elements[1][1]; - ret.write[6] = interp.basis.elements[1][2]; - ret.write[8] = interp.basis.elements[2][0]; - ret.write[9] = interp.basis.elements[2][1]; - ret.write[10] = interp.basis.elements[2][2]; - ret.write[3] = interp.origin.x; - ret.write[7] = interp.origin.y; - ret.write[11] = interp.origin.z; - ret.write[12] = 0; - ret.write[13] = 0; - ret.write[14] = 0; - ret.write[15] = 1; - - return ret; - } else { - - Vector dest; - dest.resize(keys[i].data.size()); - for (int j = 0; j < dest.size(); j++) { - - dest.write[j] = keys[i].data[j] * c + keys[i - 1].data[j] * (1.0 - c); - } - return dest; - //interpolate one by one - } - } break; - } - - ERR_FAIL_V(Vector()); -} - -void Collada::_parse_asset(XMLParser &parser) { - - while (parser.read() == OK) { - - if (parser.get_node_type() == XMLParser::NODE_ELEMENT) { - - String name = parser.get_node_name(); - - if (name == "up_axis") { - - parser.read(); - if (parser.get_node_data() == "X_UP") - state.up_axis = Vector3::AXIS_X; - if (parser.get_node_data() == "Y_UP") - state.up_axis = Vector3::AXIS_Y; - if (parser.get_node_data() == "Z_UP") - state.up_axis = Vector3::AXIS_Z; - - COLLADA_PRINT("up axis: " + parser.get_node_data()); - } else if (name == "unit") { - - state.unit_scale = parser.get_attribute_value("meter").to_double(); - COLLADA_PRINT("unit scale: " + rtos(state.unit_scale)); - } - - } else if (parser.get_node_type() == XMLParser::NODE_ELEMENT_END && parser.get_node_name() == "asset") - break; //end of - } -} - -void Collada::_parse_image(XMLParser &parser) { - - String id = parser.get_attribute_value("id"); - - if (!(state.import_flags & IMPORT_FLAG_SCENE)) { - if (!parser.is_empty()) - parser.skip_section(); - return; - } - - Image image; - - if (state.version < State::Version(1, 4, 0)) { - /* <1.4 */ - String path = parser.get_attribute_value("source").strip_edges(); - if (path.find("://") == -1 && path.is_rel_path()) { - // path is relative to file being loaded, so convert to a resource path - image.path = ProjectSettings::get_singleton()->localize_path(state.local_path.get_base_dir().plus_file(path.percent_decode())); - } - } else { - - while (parser.read() == OK) { - - if (parser.get_node_type() == XMLParser::NODE_ELEMENT) { - - String name = parser.get_node_name(); - - if (name == "init_from") { - - parser.read(); - String path = parser.get_node_data().strip_edges().percent_decode(); - - if (path.find("://") == -1 && path.is_rel_path()) { - // path is relative to file being loaded, so convert to a resource path - path = ProjectSettings::get_singleton()->localize_path(state.local_path.get_base_dir().plus_file(path)); - - } else if (path.find("file:///") == 0) { - path = path.replace_first("file:///", ""); - path = ProjectSettings::get_singleton()->localize_path(path); - } - - image.path = path; - - } else if (name == "data") { - - ERR_PRINT("COLLADA Embedded image data not supported!"); - - } else if (name == "extra" && !parser.is_empty()) - parser.skip_section(); - - } else if (parser.get_node_type() == XMLParser::NODE_ELEMENT_END && parser.get_node_name() == "image") - break; //end of - } - } - - state.image_map[id] = image; -} - -void Collada::_parse_material(XMLParser &parser) { - - if (!(state.import_flags & IMPORT_FLAG_SCENE)) { - if (!parser.is_empty()) - parser.skip_section(); - return; - } - - Material material; - - String id = parser.get_attribute_value("id"); - if (parser.has_attribute("name")) - material.name = parser.get_attribute_value("name"); - - if (state.version < State::Version(1, 4, 0)) { - /* <1.4 */ - ERR_PRINT("Collada Materials < 1.4 are not supported (yet)"); - } else { - - while (parser.read() == OK) { - - if (parser.get_node_type() == XMLParser::NODE_ELEMENT && parser.get_node_name() == "instance_effect") { - - material.instance_effect = _uri_to_id(parser.get_attribute_value("url")); - } else if (parser.get_node_type() == XMLParser::NODE_ELEMENT_END && parser.get_node_name() == "material") - break; //end of - } - } - - state.material_map[id] = material; -} - -//! reads floats from inside of xml element until end of xml element -Vector Collada::_read_float_array(XMLParser &parser) { - - if (parser.is_empty()) - return Vector(); - - Vector splitters; - splitters.push_back(" "); - splitters.push_back("\n"); - splitters.push_back("\r"); - splitters.push_back("\t"); - - Vector array; - while (parser.read() == OK) { - // TODO: check for comments inside the element - // and ignore them. - - if (parser.get_node_type() == XMLParser::NODE_TEXT) { - // parse float data - String str = parser.get_node_data(); - array = str.split_floats_mk(splitters, false); - //array=str.split_floats(" ",false); - } else if (parser.get_node_type() == XMLParser::NODE_ELEMENT_END) - break; // end parsing text - } - - return array; -} - -Vector Collada::_read_string_array(XMLParser &parser) { - - if (parser.is_empty()) - return Vector(); - - Vector array; - while (parser.read() == OK) { - // TODO: check for comments inside the element - // and ignore them. - - if (parser.get_node_type() == XMLParser::NODE_TEXT) { - // parse String data - String str = parser.get_node_data(); - array = str.split_spaces(); - } else if (parser.get_node_type() == XMLParser::NODE_ELEMENT_END) - break; // end parsing text - } - - return array; -} - -Transform Collada::_read_transform(XMLParser &parser) { - - if (parser.is_empty()) - return Transform(); - - Vector array; - while (parser.read() == OK) { - // TODO: check for comments inside the element - // and ignore them. - - if (parser.get_node_type() == XMLParser::NODE_TEXT) { - // parse float data - String str = parser.get_node_data(); - array = str.split_spaces(); - } else if (parser.get_node_type() == XMLParser::NODE_ELEMENT_END) - break; // end parsing text - } - - ERR_FAIL_COND_V(array.size() != 16, Transform()); - Vector farr; - farr.resize(16); - for (int i = 0; i < 16; i++) { - farr.write[i] = array[i].to_double(); - } - - return _read_transform_from_array(farr); -} - -String Collada::_read_empty_draw_type(XMLParser &parser) { - - String empty_draw_type = ""; - - if (parser.is_empty()) - return empty_draw_type; - - while (parser.read() == OK) { - if (parser.get_node_type() == XMLParser::NODE_TEXT) { - empty_draw_type = parser.get_node_data(); - } else if (parser.get_node_type() == XMLParser::NODE_ELEMENT_END) - break; // end parsing text - } - return empty_draw_type; -} - -Variant Collada::_parse_param(XMLParser &parser) { - - if (parser.is_empty()) - return Variant(); - - String from = parser.get_node_name(); - Variant data; - - while (parser.read() == OK) { - if (parser.get_node_type() == XMLParser::NODE_ELEMENT) { - - if (parser.get_node_name() == "float") { - - parser.read(); - if (parser.get_node_type() == XMLParser::NODE_TEXT) { - - data = parser.get_node_data().to_double(); - } - } else if (parser.get_node_name() == "float2") { - - Vector v2 = _read_float_array(parser); - - if (v2.size() >= 2) { - - data = Vector2(v2[0], v2[1]); - } - } else if (parser.get_node_name() == "float3") { - - Vector v3 = _read_float_array(parser); - - if (v3.size() >= 3) { - - data = Vector3(v3[0], v3[1], v3[2]); - } - } else if (parser.get_node_name() == "float4") { - - Vector v4 = _read_float_array(parser); - - if (v4.size() >= 4) { - - data = Color(v4[0], v4[1], v4[2], v4[3]); - } - } else if (parser.get_node_name() == "sampler2D") { - - while (parser.read() == OK) { - - if (parser.get_node_type() == XMLParser::NODE_ELEMENT) { - - if (parser.get_node_name() == "source") { - - parser.read(); - - if (parser.get_node_type() == XMLParser::NODE_TEXT) { - - data = parser.get_node_data(); - } - } - } else if (parser.get_node_type() == XMLParser::NODE_ELEMENT_END && parser.get_node_name() == "sampler2D") - break; - } - } else if (parser.get_node_name() == "surface") { - - while (parser.read() == OK) { - - if (parser.get_node_type() == XMLParser::NODE_ELEMENT) { - - if (parser.get_node_name() == "init_from") { - - parser.read(); - - if (parser.get_node_type() == XMLParser::NODE_TEXT) { - - data = parser.get_node_data(); - } - } - } else if (parser.get_node_type() == XMLParser::NODE_ELEMENT_END && parser.get_node_name() == "surface") - break; - } - } - - } else if (parser.get_node_type() == XMLParser::NODE_ELEMENT_END && parser.get_node_name() == from) - break; - } - - COLLADA_PRINT("newparam ending " + parser.get_node_name()); - return data; -} - -void Collada::_parse_effect_material(XMLParser &parser, Effect &effect, String &id) { - - if (!(state.import_flags & IMPORT_FLAG_SCENE)) { - if (!parser.is_empty()) - parser.skip_section(); - return; - } - - while (parser.read() == OK) { - - if (parser.get_node_type() == XMLParser::NODE_ELEMENT) { - - // first come the tags we descend, but ignore the top-levels - - COLLADA_PRINT("node name: " + parser.get_node_name()); - - if (!parser.is_empty() && (parser.get_node_name() == "profile_COMMON" || parser.get_node_name() == "technique" || parser.get_node_name() == "extra")) { - - _parse_effect_material(parser, effect, id); // try again - - } else if (parser.get_node_name() == "newparam") { - String name = parser.get_attribute_value("sid"); - Variant value = _parse_param(parser); - effect.params[name] = value; - COLLADA_PRINT("param: " + name + " value:" + String(value)); - - } else if (parser.get_node_name() == "constant" || - parser.get_node_name() == "lambert" || - parser.get_node_name() == "phong" || - parser.get_node_name() == "blinn") { - - COLLADA_PRINT("shade model: " + parser.get_node_name()); - while (parser.read() == OK) { - - if (parser.get_node_type() == XMLParser::NODE_ELEMENT) { - - String what = parser.get_node_name(); - - if (what == "emission" || - what == "diffuse" || - what == "specular" || - what == "reflective") { - - // color or texture types - while (parser.read() == OK) { - - if (parser.get_node_type() == XMLParser::NODE_ELEMENT) { - - if (parser.get_node_name() == "color") { - - Vector colorarr = _read_float_array(parser); - COLLADA_PRINT("colorarr size: " + rtos(colorarr.size())); - - if (colorarr.size() >= 3) { - - // alpha strangely not alright? maybe it needs to be multiplied by value as a channel intensity - Color color(colorarr[0], colorarr[1], colorarr[2], 1.0); - if (what == "diffuse") - effect.diffuse.color = color; - if (what == "specular") - effect.specular.color = color; - if (what == "emission") - effect.emission.color = color; - - COLLADA_PRINT(what + " color: " + color); - } - - } else if (parser.get_node_name() == "texture") { - - String sampler = parser.get_attribute_value("texture"); - if (!effect.params.has(sampler)) { - ERR_PRINT(String("Couldn't find sampler: " + sampler + " in material:" + id).utf8().get_data()); - } else { - String surface = effect.params[sampler]; - - if (!effect.params.has(surface)) { - ERR_PRINT(String("Couldn't find surface: " + surface + " in material:" + id).utf8().get_data()); - } else { - String uri = effect.params[surface]; - - if (what == "diffuse") { - effect.diffuse.texture = uri; - } else if (what == "specular") { - effect.specular.texture = uri; - } else if (what == "emission") { - effect.emission.texture = uri; - } else if (what == "bump") { - if (parser.has_attribute("bumptype") && parser.get_attribute_value("bumptype") != "NORMALMAP") { - WARN_PRINT("'bump' texture type is not NORMALMAP, only NORMALMAP is supported."); - } - - effect.bump.texture = uri; - } - - COLLADA_PRINT(what + " texture: " + uri); - } - } - } else if (!parser.is_empty()) - parser.skip_section(); - - } else if (parser.get_node_type() == XMLParser::NODE_ELEMENT_END && parser.get_node_name() == what) - break; - } - - } else if (what == "shininess") { - effect.shininess = _parse_param(parser); - } - } else if (parser.get_node_type() == XMLParser::NODE_ELEMENT_END && (parser.get_node_name() == "constant" || - parser.get_node_name() == "lambert" || - parser.get_node_name() == "phong" || - parser.get_node_name() == "blinn")) - break; - } - } else if (parser.get_node_name() == "double_sided" || parser.get_node_name() == "show_double_sided") { // colladamax / google earth - - // 3DS Max / Google Earth double sided extension - parser.read(); - effect.found_double_sided = true; - effect.double_sided = parser.get_node_data().to_int(); - COLLADA_PRINT("double sided: " + itos(parser.get_node_data().to_int())); - } else if (parser.get_node_name() == "unshaded") { - parser.read(); - effect.unshaded = parser.get_node_data().to_int(); - } else if (parser.get_node_name() == "bump") { - - // color or texture types - while (parser.read() == OK) { - - if (parser.get_node_type() == XMLParser::NODE_ELEMENT) { - - if (parser.get_node_name() == "texture") { - - String sampler = parser.get_attribute_value("texture"); - if (!effect.params.has(sampler)) { - ERR_PRINT(String("Couldn't find sampler: " + sampler + " in material:" + id).utf8().get_data()); - } else { - String surface = effect.params[sampler]; - - if (!effect.params.has(surface)) { - ERR_PRINT(String("Couldn't find surface: " + surface + " in material:" + id).utf8().get_data()); - } else { - String uri = effect.params[surface]; - - if (parser.has_attribute("bumptype") && parser.get_attribute_value("bumptype") != "NORMALMAP") { - WARN_PRINT("'bump' texture type is not NORMALMAP, only NORMALMAP is supported."); - } - - effect.bump.texture = uri; - COLLADA_PRINT(" bump: " + uri); - } - } - } else if (!parser.is_empty()) - parser.skip_section(); - - } else if (parser.get_node_type() == XMLParser::NODE_ELEMENT_END && parser.get_node_name() == "bump") - break; - } - - } else if (!parser.is_empty()) - parser.skip_section(); - } else if (parser.get_node_type() == XMLParser::NODE_ELEMENT_END && - (parser.get_node_name() == "effect" || - parser.get_node_name() == "profile_COMMON" || - parser.get_node_name() == "technique" || - parser.get_node_name() == "extra")) - break; - } -} - -void Collada::_parse_effect(XMLParser &parser) { - - if (!(state.import_flags & IMPORT_FLAG_SCENE)) { - if (!parser.is_empty()) - parser.skip_section(); - return; - } - - String id = parser.get_attribute_value("id"); - - Effect effect; - if (parser.has_attribute("name")) - effect.name = parser.get_attribute_value("name"); - _parse_effect_material(parser, effect, id); - - state.effect_map[id] = effect; - - COLLADA_PRINT("Effect ID:" + id); -} - -void Collada::_parse_camera(XMLParser &parser) { - - if (!(state.import_flags & IMPORT_FLAG_SCENE)) { - if (!parser.is_empty()) - parser.skip_section(); - return; - } - - String id = parser.get_attribute_value("id"); - - state.camera_data_map[id] = CameraData(); - CameraData &camera = state.camera_data_map[id]; - - while (parser.read() == OK) { - - if (parser.get_node_type() == XMLParser::NODE_ELEMENT) { - - String name = parser.get_node_name(); - - if (name == "perspective") { - - camera.mode = CameraData::MODE_PERSPECTIVE; - } else if (name == "orthographic") { - - camera.mode = CameraData::MODE_ORTHOGONAL; - } else if (name == "xfov") { - - parser.read(); - camera.perspective.x_fov = parser.get_node_data().to_double(); - - } else if (name == "yfov") { - - parser.read(); - camera.perspective.y_fov = parser.get_node_data().to_double(); - } else if (name == "xmag") { - - parser.read(); - camera.orthogonal.x_mag = parser.get_node_data().to_double(); - - } else if (name == "ymag") { - - parser.read(); - camera.orthogonal.y_mag = parser.get_node_data().to_double(); - } else if (name == "aspect_ratio") { - - parser.read(); - camera.aspect = parser.get_node_data().to_double(); - - } else if (name == "znear") { - - parser.read(); - camera.z_near = parser.get_node_data().to_double(); - - } else if (name == "zfar") { - - parser.read(); - camera.z_far = parser.get_node_data().to_double(); - } - - } else if (parser.get_node_type() == XMLParser::NODE_ELEMENT_END && parser.get_node_name() == "camera") - break; //end of - } - - COLLADA_PRINT("Camera ID:" + id); -} - -void Collada::_parse_light(XMLParser &parser) { - - if (!(state.import_flags & IMPORT_FLAG_SCENE)) { - if (!parser.is_empty()) - parser.skip_section(); - return; - } - - String id = parser.get_attribute_value("id"); - - state.light_data_map[id] = LightData(); - LightData &light = state.light_data_map[id]; - - while (parser.read() == OK) { - - if (parser.get_node_type() == XMLParser::NODE_ELEMENT) { - - String name = parser.get_node_name(); - - if (name == "ambient") { - - light.mode = LightData::MODE_AMBIENT; - } else if (name == "directional") { - - light.mode = LightData::MODE_DIRECTIONAL; - } else if (name == "point") { - - light.mode = LightData::MODE_OMNI; - } else if (name == "spot") { - - light.mode = LightData::MODE_SPOT; - } else if (name == "color") { - - parser.read(); - Vector colorarr = _read_float_array(parser); - COLLADA_PRINT("colorarr size: " + rtos(colorarr.size())); - - if (colorarr.size() >= 4) { - // alpha strangely not alright? maybe it needs to be multiplied by value as a channel intensity - Color color(colorarr[0], colorarr[1], colorarr[2], 1.0); - light.color = color; - } - - } else if (name == "constant_attenuation") { - - parser.read(); - light.constant_att = parser.get_node_data().to_double(); - } else if (name == "linear_attenuation") { - - parser.read(); - light.linear_att = parser.get_node_data().to_double(); - } else if (name == "quadratic_attenuation") { - - parser.read(); - light.quad_att = parser.get_node_data().to_double(); - } else if (name == "falloff_angle") { - - parser.read(); - light.spot_angle = parser.get_node_data().to_double(); - - } else if (name == "falloff_exponent") { - - parser.read(); - light.spot_exp = parser.get_node_data().to_double(); - } - - } else if (parser.get_node_type() == XMLParser::NODE_ELEMENT_END && parser.get_node_name() == "light") - break; //end of - } - - COLLADA_PRINT("Light ID:" + id); -} - -void Collada::_parse_curve_geometry(XMLParser &parser, String p_id, String p_name) { - - if (!(state.import_flags & IMPORT_FLAG_SCENE)) { - if (!parser.is_empty()) - parser.skip_section(); - return; - } - - //load everything into a pre dictionary - - state.curve_data_map[p_id] = CurveData(); - - CurveData &curvedata = state.curve_data_map[p_id]; - curvedata.name = p_name; - - COLLADA_PRINT("curve name: " + p_name); - - String current_source; - // handles geometry node and the curve children in this loop - // read sources with arrays and accessor for each curve - if (parser.is_empty()) { - return; - } - - while (parser.read() == OK) { - - if (parser.get_node_type() == XMLParser::NODE_ELEMENT) { - - String section = parser.get_node_name(); - - if (section == "source") { - - String id = parser.get_attribute_value("id"); - curvedata.sources[id] = CurveData::Source(); - current_source = id; - COLLADA_PRINT("source data: " + id); - - } else if (section == "float_array" || section == "array") { - // create a new array and read it. - if (curvedata.sources.has(current_source)) { - - curvedata.sources[current_source].array = _read_float_array(parser); - COLLADA_PRINT("section: " + current_source + " read " + itos(curvedata.sources[current_source].array.size()) + " values."); - } - } else if (section == "Name_array") { - // create a new array and read it. - if (curvedata.sources.has(current_source)) { - - curvedata.sources[current_source].sarray = _read_string_array(parser); - COLLADA_PRINT("section: " + current_source + " read " + itos(curvedata.sources[current_source].array.size()) + " values."); - } - - } else if (section == "technique_common") { - //skip it - } else if (section == "accessor") { // child of source (below a technique tag) - - if (curvedata.sources.has(current_source)) { - curvedata.sources[current_source].stride = parser.get_attribute_value("stride").to_int(); - COLLADA_PRINT("section: " + current_source + " stride " + itos(curvedata.sources[current_source].stride)); - } - } else if (section == "control_vertices") { - - while (parser.read() == OK) { - - if (parser.get_node_type() == XMLParser::NODE_ELEMENT) { - - if (parser.get_node_name() == "input") { - - String semantic = parser.get_attribute_value("semantic"); - String source = _uri_to_id(parser.get_attribute_value("source")); - - curvedata.control_vertices[semantic] = source; - - COLLADA_PRINT(section + " input semantic: " + semantic + " source: " + source); - } - } else if (parser.get_node_type() == XMLParser::NODE_ELEMENT_END && parser.get_node_name() == section) - break; - } - - } else if (!parser.is_empty()) { - - parser.skip_section(); - } - } else if (parser.get_node_type() == XMLParser::NODE_ELEMENT_END && parser.get_node_name() == "spline") - break; - } -} - -void Collada::_parse_mesh_geometry(XMLParser &parser, String p_id, String p_name) { - - if (!(state.import_flags & IMPORT_FLAG_SCENE)) { - if (!parser.is_empty()) - parser.skip_section(); - return; - } - - //load everything into a pre dictionary - - state.mesh_data_map[p_id] = MeshData(); - - MeshData &meshdata = state.mesh_data_map[p_id]; - meshdata.name = p_name; - - COLLADA_PRINT("mesh name: " + p_name); - - String current_source; - // handles geometry node and the mesh children in this loop - // read sources with arrays and accessor for each mesh - if (parser.is_empty()) { - return; - } - - while (parser.read() == OK) { - - if (parser.get_node_type() == XMLParser::NODE_ELEMENT) { - - String section = parser.get_node_name(); - - if (section == "source") { - - String id = parser.get_attribute_value("id"); - meshdata.sources[id] = MeshData::Source(); - current_source = id; - COLLADA_PRINT("source data: " + id); - - } else if (section == "float_array" || section == "array") { - // create a new array and read it. - if (meshdata.sources.has(current_source)) { - - meshdata.sources[current_source].array = _read_float_array(parser); - COLLADA_PRINT("section: " + current_source + " read " + itos(meshdata.sources[current_source].array.size()) + " values."); - } - } else if (section == "technique_common") { - //skip it - } else if (section == "accessor") { // child of source (below a technique tag) - - if (meshdata.sources.has(current_source)) { - meshdata.sources[current_source].stride = parser.get_attribute_value("stride").to_int(); - COLLADA_PRINT("section: " + current_source + " stride " + itos(meshdata.sources[current_source].stride)); - } - } else if (section == "vertices") { - - MeshData::Vertices vert; - String id = parser.get_attribute_value("id"); - - while (parser.read() == OK) { - - if (parser.get_node_type() == XMLParser::NODE_ELEMENT) { - - if (parser.get_node_name() == "input") { - - String semantic = parser.get_attribute_value("semantic"); - String source = _uri_to_id(parser.get_attribute_value("source")); - - vert.sources[semantic] = source; - - COLLADA_PRINT(section + " input semantic: " + semantic + " source: " + source); - } - } else if (parser.get_node_type() == XMLParser::NODE_ELEMENT_END && parser.get_node_name() == section) - break; - } - - meshdata.vertices[id] = vert; - - } else if (section == "triangles" || section == "polylist" || section == "polygons") { - - bool polygons = (section == "polygons"); - if (polygons) { - WARN_PRINT("Primitive type \"polygons\" is not well supported (concave shapes may fail). To ensure that the geometry is properly imported, please re-export using \"triangles\" or \"polylist\"."); - } - MeshData::Primitives prim; - - if (parser.has_attribute("material")) - prim.material = parser.get_attribute_value("material"); - prim.count = parser.get_attribute_value("count").to_int(); - prim.vertex_size = 0; - int last_ref = 0; - - while (parser.read() == OK) { - - if (parser.get_node_type() == XMLParser::NODE_ELEMENT) { - - if (parser.get_node_name() == "input") { - - String semantic = parser.get_attribute_value("semantic"); - String source = _uri_to_id(parser.get_attribute_value("source")); - - if (semantic == "TEXCOORD") { - /* - if (parser.has_attribute("set"))// a texcoord - semantic+=parser.get_attribute_value("set"); - else - semantic="TEXCOORD0";*/ - semantic = "TEXCOORD" + itos(last_ref++); - } - int offset = parser.get_attribute_value("offset").to_int(); - - MeshData::Primitives::SourceRef sref; - sref.source = source; - sref.offset = offset; - prim.sources[semantic] = sref; - prim.vertex_size = MAX(prim.vertex_size, offset + 1); - - COLLADA_PRINT(section + " input semantic: " + semantic + " source: " + source + " offset: " + itos(offset)); - - } else if (parser.get_node_name() == "p") { //indices - - Vector values = _read_float_array(parser); - if (polygons) { - - ERR_CONTINUE(prim.vertex_size == 0); - prim.polygons.push_back(values.size() / prim.vertex_size); - int from = prim.indices.size(); - prim.indices.resize(from + values.size()); - for (int i = 0; i < values.size(); i++) - prim.indices.write[from + i] = values[i]; - - } else if (prim.vertex_size > 0) { - prim.indices = values; - } - - COLLADA_PRINT("read " + itos(values.size()) + " index values"); - - } else if (parser.get_node_name() == "vcount") { // primitive - - Vector values = _read_float_array(parser); - prim.polygons = values; - COLLADA_PRINT("read " + itos(values.size()) + " polygon values"); - } - } else if (parser.get_node_type() == XMLParser::NODE_ELEMENT_END && parser.get_node_name() == section) - break; - } - - meshdata.primitives.push_back(prim); - - } else if (parser.get_node_name() == "double_sided") { - - parser.read(); - meshdata.found_double_sided = true; - meshdata.double_sided = parser.get_node_data().to_int(); - - } else if (parser.get_node_name() == "polygons") { - ERR_PRINT("Primitive type \"polygons\" not supported, re-export using \"polylist\" or \"triangles\"."); - } else if (!parser.is_empty()) { - - parser.skip_section(); - } - } else if (parser.get_node_type() == XMLParser::NODE_ELEMENT_END && parser.get_node_name() == "mesh") - break; - } -} - -void Collada::_parse_skin_controller(XMLParser &parser, String p_id) { - - state.skin_controller_data_map[p_id] = SkinControllerData(); - SkinControllerData &skindata = state.skin_controller_data_map[p_id]; - - skindata.base = _uri_to_id(parser.get_attribute_value("source")); - - String current_source; - - while (parser.read() == OK) { - - if (parser.get_node_type() == XMLParser::NODE_ELEMENT) { - - String section = parser.get_node_name(); - - if (section == "bind_shape_matrix") { - - skindata.bind_shape = _read_transform(parser); -#ifdef COLLADA_IMPORT_SCALE_SCENE - skindata.bind_shape.origin *= state.unit_scale; - -#endif - COLLADA_PRINT("skeleton bind shape transform: " + skindata.bind_shape); - - } else if (section == "source") { - - String id = parser.get_attribute_value("id"); - skindata.sources[id] = SkinControllerData::Source(); - current_source = id; - COLLADA_PRINT("source data: " + id); - - } else if (section == "float_array" || section == "array") { - // create a new array and read it. - if (skindata.sources.has(current_source)) { - - skindata.sources[current_source].array = _read_float_array(parser); - COLLADA_PRINT("section: " + current_source + " read " + itos(skindata.sources[current_source].array.size()) + " values."); - } - } else if (section == "Name_array" || section == "IDREF_array") { - // create a new array and read it. - - if (section == "IDREF_array") - skindata.use_idrefs = true; - if (skindata.sources.has(current_source)) { - - skindata.sources[current_source].sarray = _read_string_array(parser); - if (section == "IDREF_array") { - Vector sa = skindata.sources[current_source].sarray; - for (int i = 0; i < sa.size(); i++) - state.idref_joints.insert(sa[i]); - } - COLLADA_PRINT("section: " + current_source + " read " + itos(skindata.sources[current_source].array.size()) + " values."); - } - } else if (section == "technique_common") { - //skip it - } else if (section == "accessor") { // child of source (below a technique tag) - - if (skindata.sources.has(current_source)) { - - int stride = 1; - if (parser.has_attribute("stride")) - stride = parser.get_attribute_value("stride").to_int(); - - skindata.sources[current_source].stride = stride; - COLLADA_PRINT("section: " + current_source + " stride " + itos(skindata.sources[current_source].stride)); - } - - } else if (section == "joints") { - - SkinControllerData::Joints joint; - - while (parser.read() == OK) { - - if (parser.get_node_type() == XMLParser::NODE_ELEMENT) { - - if (parser.get_node_name() == "input") { - - String semantic = parser.get_attribute_value("semantic"); - String source = _uri_to_id(parser.get_attribute_value("source")); - - joint.sources[semantic] = source; - - COLLADA_PRINT(section + " input semantic: " + semantic + " source: " + source); - } - } else if (parser.get_node_type() == XMLParser::NODE_ELEMENT_END && parser.get_node_name() == section) - break; - } - - skindata.joints = joint; - - } else if (section == "vertex_weights") { - - SkinControllerData::Weights weights; - - weights.count = parser.get_attribute_value("count").to_int(); - - while (parser.read() == OK) { - - if (parser.get_node_type() == XMLParser::NODE_ELEMENT) { - - if (parser.get_node_name() == "input") { - - String semantic = parser.get_attribute_value("semantic"); - String source = _uri_to_id(parser.get_attribute_value("source")); - - int offset = parser.get_attribute_value("offset").to_int(); - - SkinControllerData::Weights::SourceRef sref; - sref.source = source; - sref.offset = offset; - weights.sources[semantic] = sref; - - COLLADA_PRINT(section + " input semantic: " + semantic + " source: " + source + " offset: " + itos(offset)); - - } else if (parser.get_node_name() == "v") { //indices - - Vector values = _read_float_array(parser); - weights.indices = values; - COLLADA_PRINT("read " + itos(values.size()) + " index values"); - - } else if (parser.get_node_name() == "vcount") { // weightsitive - - Vector values = _read_float_array(parser); - weights.sets = values; - COLLADA_PRINT("read " + itos(values.size()) + " polygon values"); - } - } else if (parser.get_node_type() == XMLParser::NODE_ELEMENT_END && parser.get_node_name() == section) - break; - } - - skindata.weights = weights; - } - /* - else if (!parser.is_empty()) - parser.skip_section(); - */ - - } else if (parser.get_node_type() == XMLParser::NODE_ELEMENT_END && parser.get_node_name() == "skin") - break; - } - - /* STORE REST MATRICES */ - - Vector rests; - ERR_FAIL_COND(!skindata.joints.sources.has("JOINT")); - ERR_FAIL_COND(!skindata.joints.sources.has("INV_BIND_MATRIX")); - - String joint_arr = skindata.joints.sources["JOINT"]; - String ibm = skindata.joints.sources["INV_BIND_MATRIX"]; - - ERR_FAIL_COND(!skindata.sources.has(joint_arr)); - ERR_FAIL_COND(!skindata.sources.has(ibm)); - - SkinControllerData::Source &joint_source = skindata.sources[joint_arr]; - SkinControllerData::Source &ibm_source = skindata.sources[ibm]; - - ERR_FAIL_COND(joint_source.sarray.size() != ibm_source.array.size() / 16); - - for (int i = 0; i < joint_source.sarray.size(); i++) { - - String name = joint_source.sarray[i]; - Transform xform = _read_transform_from_array(ibm_source.array, i * 16); //<- this is a mistake, it must be applied to vertices - xform.affine_invert(); // inverse for rest, because it's an inverse -#ifdef COLLADA_IMPORT_SCALE_SCENE - xform.origin *= state.unit_scale; -#endif - skindata.bone_rest_map[name] = xform; - } -} - -void Collada::_parse_morph_controller(XMLParser &parser, String p_id) { - - state.morph_controller_data_map[p_id] = MorphControllerData(); - MorphControllerData &morphdata = state.morph_controller_data_map[p_id]; - - morphdata.mesh = _uri_to_id(parser.get_attribute_value("source")); - morphdata.mode = parser.get_attribute_value("method"); - String current_source; - - while (parser.read() == OK) { - - if (parser.get_node_type() == XMLParser::NODE_ELEMENT) { - - String section = parser.get_node_name(); - - if (section == "source") { - - String id = parser.get_attribute_value("id"); - morphdata.sources[id] = MorphControllerData::Source(); - current_source = id; - COLLADA_PRINT("source data: " + id); - - } else if (section == "float_array" || section == "array") { - // create a new array and read it. - if (morphdata.sources.has(current_source)) { - - morphdata.sources[current_source].array = _read_float_array(parser); - COLLADA_PRINT("section: " + current_source + " read " + itos(morphdata.sources[current_source].array.size()) + " values."); - } - } else if (section == "Name_array" || section == "IDREF_array") { - // create a new array and read it. - - /* - if (section=="IDREF_array") - morphdata.use_idrefs=true; - */ - if (morphdata.sources.has(current_source)) { - - morphdata.sources[current_source].sarray = _read_string_array(parser); - /* - if (section=="IDREF_array") { - Vector sa = morphdata.sources[current_source].sarray; - for(int i=0;icontroller = type == "instance_controller"; - geom->source = _uri_to_id(parser.get_attribute_value_safe("url")); - - if (parser.is_empty()) //nothing else to parse... - return geom; - // try to find also many materials and skeletons! - while (parser.read() == OK) { - - if (parser.get_node_type() == XMLParser::NODE_ELEMENT) { - - if (parser.get_node_name() == "instance_material") { - - String symbol = parser.get_attribute_value("symbol"); - String target = _uri_to_id(parser.get_attribute_value("target")); - - NodeGeometry::Material mat; - mat.target = target; - geom->material_map[symbol] = mat; - COLLADA_PRINT("uses material: '" + target + "' on primitive'" + symbol + "'"); - } else if (parser.get_node_name() == "skeleton") { - - parser.read(); - String uri = _uri_to_id(parser.get_node_data()); - if (uri != "") { - geom->skeletons.push_back(uri); - } - } - - } else if (parser.get_node_type() == XMLParser::NODE_ELEMENT_END && parser.get_node_name() == type) - break; - } - - if (geom->controller) { - - if (geom->skeletons.empty()) { - //XSI style - - if (state.skin_controller_data_map.has(geom->source)) { - SkinControllerData *skin = &state.skin_controller_data_map[geom->source]; - //case where skeletons reference bones with IDREF (XSI) - ERR_FAIL_COND_V(!skin->joints.sources.has("JOINT"), geom); - String joint_arr = skin->joints.sources["JOINT"]; - ERR_FAIL_COND_V(!skin->sources.has(joint_arr), geom); - Collada::SkinControllerData::Source &joint_source = skin->sources[joint_arr]; - geom->skeletons = joint_source.sarray; //quite crazy, but should work. - } - } - } - - return geom; -} - -Collada::Node *Collada::_parse_visual_instance_camera(XMLParser &parser) { - - NodeCamera *cam = memnew(NodeCamera); - cam->camera = _uri_to_id(parser.get_attribute_value_safe("url")); - - if (state.up_axis == Vector3::AXIS_Z) //collada weirdness - cam->post_transform.basis.rotate(Vector3(1, 0, 0), -Math_PI * 0.5); - - if (parser.is_empty()) //nothing else to parse... - return cam; - - while (parser.read() == OK) { - - if (parser.get_node_type() == XMLParser::NODE_ELEMENT_END && parser.get_node_name() == "instance_camera") - break; - } - - return cam; -} - -Collada::Node *Collada::_parse_visual_instance_light(XMLParser &parser) { - - NodeLight *cam = memnew(NodeLight); - cam->light = _uri_to_id(parser.get_attribute_value_safe("url")); - - if (state.up_axis == Vector3::AXIS_Z) //collada weirdness - cam->post_transform.basis.rotate(Vector3(1, 0, 0), -Math_PI * 0.5); - - if (parser.is_empty()) //nothing else to parse... - return cam; - - while (parser.read() == OK) { - - if (parser.get_node_type() == XMLParser::NODE_ELEMENT_END && parser.get_node_name() == "instance_light") - break; - } - - return cam; -} - -Collada::Node *Collada::_parse_visual_node_instance_data(XMLParser &parser) { - - String instance_type = parser.get_node_name(); - - if (instance_type == "instance_geometry" || instance_type == "instance_controller") { - return _parse_visual_instance_geometry(parser); - } else if (instance_type == "instance_camera") { - - return _parse_visual_instance_camera(parser); - } else if (instance_type == "instance_light") { - return _parse_visual_instance_light(parser); - } - - if (parser.is_empty()) //nothing else to parse... - return NULL; - - while (parser.read() == OK) { - - if (parser.get_node_type() == XMLParser::NODE_ELEMENT_END && parser.get_node_name() == instance_type) - break; - } - - return NULL; -} - -Collada::Node *Collada::_parse_visual_scene_node(XMLParser &parser) { - - String name; - - String id = parser.get_attribute_value_safe("id"); - - bool found_name = false; - - if (id == "") { - - id = "%NODEID%" + itos(Math::rand()); - - } else { - found_name = true; - } - - Vector xform_list; - Vector children; - - String empty_draw_type = ""; - - Node *node = NULL; - - name = parser.has_attribute("name") ? parser.get_attribute_value_safe("name") : parser.get_attribute_value_safe("id"); - if (name == "") { - - name = id; - } else { - found_name = true; - } - - if ((parser.has_attribute("type") && parser.get_attribute_value("type") == "JOINT") || state.idref_joints.has(name)) { - // handle a bone - - NodeJoint *joint = memnew(NodeJoint); - - if (parser.has_attribute("sid")) { //bones may not have sid - joint->sid = parser.get_attribute_value("sid"); - //state.bone_map[joint->sid]=joint; - } else if (state.idref_joints.has(name)) { - joint->sid = name; //kind of a cheat but.. - } else if (parser.has_attribute("name")) { - joint->sid = parser.get_attribute_value_safe("name"); - } - - if (joint->sid != "") { - state.sid_to_node_map[joint->sid] = id; - } - - node = joint; - } - - while (parser.read() == OK) { - - if (parser.get_node_type() == XMLParser::NODE_ELEMENT) { - - String section = parser.get_node_name(); - - if (section == "translate") { - Node::XForm xf; - if (parser.has_attribute("sid")) { - xf.id = parser.get_attribute_value("sid"); - } - xf.op = Node::XForm::OP_TRANSLATE; - - Vector xlt = _read_float_array(parser); - xf.data = xlt; - xform_list.push_back(xf); - - } else if (section == "rotate") { - Node::XForm xf; - if (parser.has_attribute("sid")) { - xf.id = parser.get_attribute_value("sid"); - } - xf.op = Node::XForm::OP_ROTATE; - - Vector rot = _read_float_array(parser); - xf.data = rot; - - xform_list.push_back(xf); - - } else if (section == "scale") { - Node::XForm xf; - if (parser.has_attribute("sid")) { - xf.id = parser.get_attribute_value("sid"); - } - - xf.op = Node::XForm::OP_SCALE; - - Vector scale = _read_float_array(parser); - - xf.data = scale; - - xform_list.push_back(xf); - - } else if (section == "matrix") { - Node::XForm xf; - if (parser.has_attribute("sid")) { - xf.id = parser.get_attribute_value("sid"); - } - xf.op = Node::XForm::OP_MATRIX; - - Vector matrix = _read_float_array(parser); - - xf.data = matrix; - String mtx; - for (int i = 0; i < matrix.size(); i++) - mtx += " " + rtos(matrix[i]); - - xform_list.push_back(xf); - - } else if (section == "visibility") { - Node::XForm xf; - if (parser.has_attribute("sid")) { - xf.id = parser.get_attribute_value("sid"); - } - xf.op = Node::XForm::OP_VISIBILITY; - - Vector visible = _read_float_array(parser); - - xf.data = visible; - - xform_list.push_back(xf); - - } else if (section == "empty_draw_type") { - empty_draw_type = _read_empty_draw_type(parser); - } else if (section == "technique" || section == "extra") { - - } else if (section != "node") { - //usually what defines the type of node - if (section.begins_with("instance_")) { - - if (!node) { - - node = _parse_visual_node_instance_data(parser); - - } else { - ERR_PRINT("Multiple instance_* not supported."); - } - } - - } else { - - /* Found a child node!! what to do..*/ - - Node *child = _parse_visual_scene_node(parser); - children.push_back(child); - } - - } else if (parser.get_node_type() == XMLParser::NODE_ELEMENT_END && parser.get_node_name() == "node") - break; - } - - if (!node) { - - node = memnew(Node); //generic node, nothing of relevance found - } - - node->noname = !found_name; - node->xform_list = xform_list; - node->children = children; - for (int i = 0; i < children.size(); i++) { - node->children[i]->parent = node; - } - - node->name = name; - node->id = id; - node->empty_draw_type = empty_draw_type; - - if (node->children.size() == 1) { - if (node->children[0]->noname && !node->noname) { - node->children[0]->name = node->name; - node->name = node->name + "-base"; - } - } - - node->default_transform = node->compute_transform(*this); - state.scene_map[id] = node; - - return node; -} - -void Collada::_parse_visual_scene(XMLParser &parser) { - - String id = parser.get_attribute_value("id"); - - if (parser.is_empty()) { - return; - } - - state.visual_scene_map[id] = VisualScene(); - VisualScene &vscene = state.visual_scene_map[id]; - - if (parser.has_attribute("name")) - vscene.name = parser.get_attribute_value("name"); - - while (parser.read() == OK) { - - if (parser.get_node_type() == XMLParser::NODE_ELEMENT) { - - String section = parser.get_node_name(); - - if (section == "node") { - vscene.root_nodes.push_back(_parse_visual_scene_node(parser)); - } - - } else if (parser.get_node_type() == XMLParser::NODE_ELEMENT_END && parser.get_node_name() == "visual_scene") - break; - } - - COLLADA_PRINT("Scene ID:" + id); -} - -void Collada::_parse_animation(XMLParser &parser) { - - if (!(state.import_flags & IMPORT_FLAG_ANIMATION)) { - if (!parser.is_empty()) - parser.skip_section(); - - return; - } - - Map> float_sources; - Map> string_sources; - Map source_strides; - Map> samplers; - Map> source_param_names; - Map> source_param_types; - - String id = ""; - if (parser.has_attribute("id")) - id = parser.get_attribute_value("id"); - - String current_source; - String current_sampler; - Vector channel_sources; - Vector channel_targets; - - while (parser.read() == OK) { - - if (parser.get_node_type() == XMLParser::NODE_ELEMENT) { - - String name = parser.get_node_name(); - if (name == "source") { - - current_source = parser.get_attribute_value("id"); - source_param_names[current_source] = Vector(); - source_param_types[current_source] = Vector(); - - } else if (name == "float_array") { - - if (current_source != "") { - float_sources[current_source] = _read_float_array(parser); - } - - } else if (name == "Name_array") { - - if (current_source != "") { - string_sources[current_source] = _read_string_array(parser); - } - } else if (name == "accessor") { - - if (current_source != "" && parser.has_attribute("stride")) { - source_strides[current_source] = parser.get_attribute_value("stride").to_int(); - } - } else if (name == "sampler") { - - current_sampler = parser.get_attribute_value("id"); - samplers[current_sampler] = Map(); - } else if (name == "param") { - - if (parser.has_attribute("name")) - source_param_names[current_source].push_back(parser.get_attribute_value("name")); - else - source_param_names[current_source].push_back(""); - - if (parser.has_attribute("type")) - source_param_types[current_source].push_back(parser.get_attribute_value("type")); - else - source_param_types[current_source].push_back(""); - - } else if (name == "input") { - - if (current_sampler != "") { - - samplers[current_sampler][parser.get_attribute_value("semantic")] = parser.get_attribute_value("source"); - } - - } else if (name == "channel") { - - channel_sources.push_back(parser.get_attribute_value("source")); - channel_targets.push_back(parser.get_attribute_value("target")); - } - - } else if (parser.get_node_type() == XMLParser::NODE_ELEMENT_END && parser.get_node_name() == "animation") - break; //end of - } - - for (int i = 0; i < channel_sources.size(); i++) { - - String source = _uri_to_id(channel_sources[i]); - String target = channel_targets[i]; - ERR_CONTINUE(!samplers.has(source)); - Map &sampler = samplers[source]; - - ERR_CONTINUE(!sampler.has("INPUT")); //no input semantic? wtf? - String input_id = _uri_to_id(sampler["INPUT"]); - COLLADA_PRINT("input id is " + input_id); - ERR_CONTINUE(!float_sources.has(input_id)); - - ERR_CONTINUE(!sampler.has("OUTPUT")); - String output_id = _uri_to_id(sampler["OUTPUT"]); - ERR_CONTINUE(!float_sources.has(output_id)); - - ERR_CONTINUE(!source_param_names.has(output_id)); - - Vector &names = source_param_names[output_id]; - - for (int l = 0; l < names.size(); l++) { - - String name = names[l]; - - Vector &time_keys = float_sources[input_id]; - int key_count = time_keys.size(); - - AnimationTrack track; //begin crating track - track.id = id; - - track.keys.resize(key_count); - - for (int j = 0; j < key_count; j++) { - track.keys.write[j].time = time_keys[j]; - state.animation_length = MAX(state.animation_length, time_keys[j]); - } - - //now read actual values - - int stride = 1; - - if (source_strides.has(output_id)) - stride = source_strides[output_id]; - int output_len = stride / names.size(); - - ERR_CONTINUE(output_len == 0); - ERR_CONTINUE(!float_sources.has(output_id)); - - Vector &output = float_sources[output_id]; - - ERR_CONTINUE_MSG((output.size() / stride) != key_count, "Wrong number of keys in output."); - - for (int j = 0; j < key_count; j++) { - track.keys.write[j].data.resize(output_len); - for (int k = 0; k < output_len; k++) - track.keys.write[j].data.write[k] = output[l + j * stride + k]; //super weird but should work: - } - - if (sampler.has("INTERPOLATION")) { - - String interp_id = _uri_to_id(sampler["INTERPOLATION"]); - ERR_CONTINUE(!string_sources.has(interp_id)); - Vector &interps = string_sources[interp_id]; - ERR_CONTINUE(interps.size() != key_count); - - for (int j = 0; j < key_count; j++) { - if (interps[j] == "BEZIER") - track.keys.write[j].interp_type = AnimationTrack::INTERP_BEZIER; - else - track.keys.write[j].interp_type = AnimationTrack::INTERP_LINEAR; - } - } - - if (sampler.has("IN_TANGENT") && sampler.has("OUT_TANGENT")) { - //bezier control points.. - String intangent_id = _uri_to_id(sampler["IN_TANGENT"]); - ERR_CONTINUE(!float_sources.has(intangent_id)); - Vector &intangents = float_sources[intangent_id]; - - ERR_CONTINUE(intangents.size() != key_count * 2 * names.size()); - - String outangent_id = _uri_to_id(sampler["OUT_TANGENT"]); - ERR_CONTINUE(!float_sources.has(outangent_id)); - Vector &outangents = float_sources[outangent_id]; - ERR_CONTINUE(outangents.size() != key_count * 2 * names.size()); - - for (int j = 0; j < key_count; j++) { - track.keys.write[j].in_tangent = Vector2(intangents[j * 2 * names.size() + 0 + l * 2], intangents[j * 2 * names.size() + 1 + l * 2]); - track.keys.write[j].out_tangent = Vector2(outangents[j * 2 * names.size() + 0 + l * 2], outangents[j * 2 * names.size() + 1 + l * 2]); - } - } - - if (target.find("/") != -1) { //transform component - track.target = target.get_slicec('/', 0); - track.param = target.get_slicec('/', 1); - if (track.param.find(".") != -1) - track.component = track.param.get_slice(".", 1).to_upper(); - track.param = track.param.get_slice(".", 0); - if (names.size() > 1 && track.component == "") { - //this is a guess because the collada spec is ambiguous here... - //i suppose if you have many names (outputs) you can't use a component and i should abide to that. - track.component = name; - } - } else { - track.target = target; - } - - state.animation_tracks.push_back(track); - - if (!state.referenced_tracks.has(target)) - state.referenced_tracks[target] = Vector(); - - state.referenced_tracks[target].push_back(state.animation_tracks.size() - 1); - - if (id != "") { - if (!state.by_id_tracks.has(id)) - state.by_id_tracks[id] = Vector(); - - state.by_id_tracks[id].push_back(state.animation_tracks.size() - 1); - } - - COLLADA_PRINT("loaded animation with " + itos(key_count) + " keys"); - } - } -} - -void Collada::_parse_animation_clip(XMLParser &parser) { - - if (!(state.import_flags & IMPORT_FLAG_ANIMATION)) { - if (!parser.is_empty()) - parser.skip_section(); - - return; - } - - AnimationClip clip; - - if (parser.has_attribute("name")) - clip.name = parser.get_attribute_value("name"); - else if (parser.has_attribute("id")) - clip.name = parser.get_attribute_value("id"); - if (parser.has_attribute("start")) - clip.begin = parser.get_attribute_value("start").to_double(); - if (parser.has_attribute("end")) - clip.end = parser.get_attribute_value("end").to_double(); - - while (parser.read() == OK) { - - if (parser.get_node_type() == XMLParser::NODE_ELEMENT) { - - String name = parser.get_node_name(); - if (name == "instance_animation") { - - String url = _uri_to_id(parser.get_attribute_value("url")); - clip.tracks.push_back(url); - } - - } else if (parser.get_node_type() == XMLParser::NODE_ELEMENT_END && parser.get_node_name() == "animation_clip") - break; //end of - } - - state.animation_clips.push_back(clip); -} - -void Collada::_parse_scene(XMLParser &parser) { - - if (parser.is_empty()) { - return; - } - - while (parser.read() == OK) { - - if (parser.get_node_type() == XMLParser::NODE_ELEMENT) { - - String name = parser.get_node_name(); - - if (name == "instance_visual_scene") { - - state.root_visual_scene = _uri_to_id(parser.get_attribute_value("url")); - } else if (name == "instance_physics_scene") { - - state.root_physics_scene = _uri_to_id(parser.get_attribute_value("url")); - } - - } else if (parser.get_node_type() == XMLParser::NODE_ELEMENT_END && parser.get_node_name() == "scene") - break; //end of - } -} - -void Collada::_parse_library(XMLParser &parser) { - - if (parser.is_empty()) { - return; - } - - while (parser.read() == OK) { - - if (parser.get_node_type() == XMLParser::NODE_ELEMENT) { - - String name = parser.get_node_name(); - COLLADA_PRINT("library name is: " + name); - if (name == "image") { - - _parse_image(parser); - } else if (name == "material") { - - _parse_material(parser); - } else if (name == "effect") { - - _parse_effect(parser); - } else if (name == "camera") { - - _parse_camera(parser); - } else if (name == "light") { - - _parse_light(parser); - } else if (name == "geometry") { - - String id = parser.get_attribute_value("id"); - String name2 = parser.get_attribute_value_safe("name"); - while (parser.read() == OK) { - - if (parser.get_node_type() == XMLParser::NODE_ELEMENT) { - - if (parser.get_node_name() == "mesh") { - state.mesh_name_map[id] = (name2 != "") ? name2 : id; - _parse_mesh_geometry(parser, id, name2); - } else if (parser.get_node_name() == "spline") { - state.mesh_name_map[id] = (name2 != "") ? name2 : id; - _parse_curve_geometry(parser, id, name2); - } else if (!parser.is_empty()) - parser.skip_section(); - } else if (parser.get_node_type() == XMLParser::NODE_ELEMENT_END && parser.get_node_name() == "geometry") - break; - } - - } else if (name == "controller") { - - _parse_controller(parser); - } else if (name == "animation") { - - _parse_animation(parser); - } else if (name == "animation_clip") { - - _parse_animation_clip(parser); - } else if (name == "visual_scene") { - - COLLADA_PRINT("visual scene"); - _parse_visual_scene(parser); - } else if (!parser.is_empty()) - parser.skip_section(); - - } else if (parser.get_node_type() == XMLParser::NODE_ELEMENT_END && parser.get_node_name().begins_with("library_")) - break; //end of - } -} - -void Collada::_joint_set_owner(Collada::Node *p_node, NodeSkeleton *p_owner) { - - if (p_node->type == Node::TYPE_JOINT) { - - NodeJoint *nj = static_cast(p_node); - nj->owner = p_owner; - - for (int i = 0; i < nj->children.size(); i++) { - - _joint_set_owner(nj->children.write[i], p_owner); - } - } -} - -void Collada::_create_skeletons(Collada::Node **p_node, NodeSkeleton *p_skeleton) { - - Node *node = *p_node; - - if (node->type == Node::TYPE_JOINT) { - - if (!p_skeleton) { - - // ohohohoohoo it's a joint node, time to work! - NodeSkeleton *sk = memnew(NodeSkeleton); - *p_node = sk; - sk->children.push_back(node); - sk->parent = node->parent; - node->parent = sk; - p_skeleton = sk; - } - - NodeJoint *nj = static_cast(node); - nj->owner = p_skeleton; - } else { - p_skeleton = NULL; - } - - for (int i = 0; i < node->children.size(); i++) { - _create_skeletons(&node->children.write[i], p_skeleton); - } -} - -bool Collada::_remove_node(Node *p_parent, Node *p_node) { - - for (int i = 0; i < p_parent->children.size(); i++) { - - if (p_parent->children[i] == p_node) { - p_parent->children.remove(i); - return true; - } - if (_remove_node(p_parent->children[i], p_node)) - return true; - } - - return false; -} - -void Collada::_remove_node(VisualScene *p_vscene, Node *p_node) { - - for (int i = 0; i < p_vscene->root_nodes.size(); i++) { - if (p_vscene->root_nodes[i] == p_node) { - - p_vscene->root_nodes.remove(i); - return; - } - if (_remove_node(p_vscene->root_nodes[i], p_node)) - return; - } - - ERR_PRINT("ERROR: Not found node to remove?"); -} - -void Collada::_merge_skeletons(VisualScene *p_vscene, Node *p_node) { - - if (p_node->type == Node::TYPE_GEOMETRY) { - - NodeGeometry *gnode = static_cast(p_node); - if (gnode->controller) { - - // recount skeletons used - Set skeletons; - - for (int i = 0; i < gnode->skeletons.size(); i++) { - - String nodeid = gnode->skeletons[i]; - - ERR_CONTINUE(!state.scene_map.has(nodeid)); //weird, it should have it... - -#ifdef NO_SAFE_CAST - NodeJoint *nj = static_cast(state.scene_map[nodeid]); -#else - NodeJoint *nj = dynamic_cast(state.scene_map[nodeid]); -#endif - ERR_CONTINUE(!nj); //broken collada - ERR_CONTINUE(!nj->owner); //weird, node should have a skeleton owner - - skeletons.insert(nj->owner); - } - - if (skeletons.size() > 1) { - - //do the merger!! - Set::Element *E = skeletons.front(); - NodeSkeleton *base = E->get(); - - for (E = E->next(); E; E = E->next()) { - - NodeSkeleton *merged = E->get(); - _remove_node(p_vscene, merged); - for (int i = 0; i < merged->children.size(); i++) { - - _joint_set_owner(merged->children[i], base); - base->children.push_back(merged->children[i]); - merged->children[i]->parent = base; - } - - merged->children.clear(); //take children from it - memdelete(merged); - } - } - } - } - - for (int i = 0; i < p_node->children.size(); i++) { - _merge_skeletons(p_vscene, p_node->children[i]); - } -} - -void Collada::_merge_skeletons2(VisualScene *p_vscene) { - - for (Map::Element *E = state.skin_controller_data_map.front(); E; E = E->next()) { - - SkinControllerData &cd = E->get(); - - NodeSkeleton *skeleton = NULL; - - for (Map::Element *F = cd.bone_rest_map.front(); F; F = F->next()) { - - String name; - - if (!state.sid_to_node_map.has(F->key())) { - continue; - } - - name = state.sid_to_node_map[F->key()]; - - ERR_CONTINUE(!state.scene_map.has(name)); - - Node *node = state.scene_map[name]; - ERR_CONTINUE(node->type != Node::TYPE_JOINT); - - NodeSkeleton *sk = NULL; - - while (node && !sk) { - - if (node->type == Node::TYPE_SKELETON) { - sk = static_cast(node); - } - node = node->parent; - } - - ERR_CONTINUE(!sk); - - if (!skeleton) { - skeleton = sk; - continue; - } - - if (skeleton != sk) { - //whoa.. wtf, merge. - _remove_node(p_vscene, sk); - for (int i = 0; i < sk->children.size(); i++) { - - _joint_set_owner(sk->children[i], skeleton); - skeleton->children.push_back(sk->children[i]); - sk->children[i]->parent = skeleton; - } - - sk->children.clear(); //take children from it - memdelete(sk); - } - } - } -} - -bool Collada::_optimize_skeletons(VisualScene *p_vscene, Node *p_node) { - - Node *node = p_node; - - if (node->type == Node::TYPE_SKELETON && node->parent && node->parent->type == Node::TYPE_NODE && node->parent->children.size() == 1) { - //replace parent by this... - Node *parent = node->parent; - - //i wonder if this is alright.. i think it is since created skeleton (first joint) is already animated by bone.. - node->id = parent->id; - node->name = parent->name; - node->xform_list = parent->xform_list; - node->default_transform = parent->default_transform; - - state.scene_map[node->id] = node; - node->parent = parent->parent; - - if (parent->parent) { - Node *gp = parent->parent; - bool found = false; - for (int i = 0; i < gp->children.size(); i++) { - - if (gp->children[i] == parent) { - gp->children.write[i] = node; - found = true; - break; - } - } - if (!found) { - ERR_PRINT("BUG"); - } - } else { - - bool found = false; - - for (int i = 0; i < p_vscene->root_nodes.size(); i++) { - - if (p_vscene->root_nodes[i] == parent) { - - p_vscene->root_nodes.write[i] = node; - found = true; - break; - } - } - if (!found) { - ERR_PRINT("BUG"); - } - } - - parent->children.clear(); - memdelete(parent); - return true; - } - - for (int i = 0; i < node->children.size(); i++) { - - if (_optimize_skeletons(p_vscene, node->children[i])) - return false; //stop processing, go up - } - - return false; -} - -bool Collada::_move_geometry_to_skeletons(VisualScene *p_vscene, Node *p_node, List *p_mgeom) { - - // Bind Shape Matrix scales the bones and makes them gigantic, so the matrix then shrinks the model? - // Solution: apply the Bind Shape Matrix to the VERTICES, and if the object comes scaled, it seems to be left alone! - - if (p_node->type == Node::TYPE_GEOMETRY) { - - NodeGeometry *ng = static_cast(p_node); - if (ng->ignore_anim) - return false; //already made child of skeleton and processeg - - if (ng->controller && ng->skeletons.size()) { - - String nodeid = ng->skeletons[0]; - - ERR_FAIL_COND_V(!state.scene_map.has(nodeid), false); //weird, it should have it... -#ifdef NO_SAFE_CAST - NodeJoint *nj = static_cast(state.scene_map[nodeid]); -#else - NodeJoint *nj = dynamic_cast(state.scene_map[nodeid]); -#endif - ERR_FAIL_COND_V(!nj, false); - ERR_FAIL_COND_V(!nj->owner, false); //weird, node should have a skeleton owner - - NodeSkeleton *sk = nj->owner; - - Node *p = sk->parent; - bool node_is_parent_of_skeleton = false; - - while (p) { - if (p == p_node) { - node_is_parent_of_skeleton = true; - break; - } - p = p->parent; // try again - } - - ERR_FAIL_COND_V(node_is_parent_of_skeleton, false); - - //this should be correct - ERR_FAIL_COND_V(!state.skin_controller_data_map.has(ng->source), false); - SkinControllerData &skin = state.skin_controller_data_map[ng->source]; - Transform skel_inv = sk->get_global_transform().affine_inverse(); - p_node->default_transform = skel_inv * (skin.bind_shape /* p_node->get_global_transform()*/); // i honestly have no idea what to do with a previous model xform.. most exporters ignore it - - //make rests relative to the skeleton (they seem to be always relative to world) - for (Map::Element *E = skin.bone_rest_map.front(); E; E = E->next()) { - - E->get() = skel_inv * E->get(); //make the bone rest local to the skeleton - state.bone_rest_map[E->key()] = E->get(); // make it remember where the bone is globally, now that it's relative - } - - //but most exporters seem to work only if i do this.. - //p_node->default_transform = p_node->get_global_transform(); - - //p_node->default_transform=Transform(); //this seems to be correct, because bind shape makes the object local to the skeleton - p_node->ignore_anim = true; // collada may animate this later, if it does, then this is not supported (redo your original asset and don't animate the base mesh) - p_node->parent = sk; - //sk->children.push_back(0,p_node); //avoid INFINITE loop - p_mgeom->push_back(p_node); - return true; - } - } - - for (int i = 0; i < p_node->children.size(); i++) { - - if (_move_geometry_to_skeletons(p_vscene, p_node->children[i], p_mgeom)) { - p_node->children.remove(i); - i--; - } - } - - return false; -} - -void Collada::_find_morph_nodes(VisualScene *p_vscene, Node *p_node) { - - if (p_node->type == Node::TYPE_GEOMETRY) { - - NodeGeometry *nj = static_cast(p_node); - - if (nj->controller) { - - String base = nj->source; - - while (base != "" && !state.mesh_data_map.has(base)) { - - if (state.skin_controller_data_map.has(base)) { - - SkinControllerData &sk = state.skin_controller_data_map[base]; - base = sk.base; - } else if (state.morph_controller_data_map.has(base)) { - - state.morph_ownership_map[base] = nj->id; - break; - } else { - ERR_FAIL_MSG("Invalid scene."); - } - } - } - } - - for (int i = 0; i < p_node->children.size(); i++) { - - _find_morph_nodes(p_vscene, p_node->children[i]); - } -} - -void Collada::_optimize() { - - for (Map::Element *E = state.visual_scene_map.front(); E; E = E->next()) { - - VisualScene &vs = E->get(); - for (int i = 0; i < vs.root_nodes.size(); i++) { - _create_skeletons(&vs.root_nodes.write[i]); - } - - for (int i = 0; i < vs.root_nodes.size(); i++) { - _merge_skeletons(&vs, vs.root_nodes[i]); - } - - _merge_skeletons2(&vs); - - for (int i = 0; i < vs.root_nodes.size(); i++) { - _optimize_skeletons(&vs, vs.root_nodes[i]); - } - - for (int i = 0; i < vs.root_nodes.size(); i++) { - - List mgeom; - if (_move_geometry_to_skeletons(&vs, vs.root_nodes[i], &mgeom)) { - vs.root_nodes.remove(i); - i--; - } - - while (!mgeom.empty()) { - - Node *n = mgeom.front()->get(); - n->parent->children.push_back(n); - mgeom.pop_front(); - } - } - - for (int i = 0; i < vs.root_nodes.size(); i++) { - _find_morph_nodes(&vs, vs.root_nodes[i]); - } - } -} - -int Collada::get_uv_channel(String p_name) { - - if (!channel_map.has(p_name)) { - - ERR_FAIL_COND_V(channel_map.size() == 2, 0); - - channel_map[p_name] = channel_map.size(); - } - - return channel_map[p_name]; -} - -Error Collada::load(const String &p_path, int p_flags) { - - Ref parserr = memnew(XMLParser); - XMLParser &parser = *parserr.ptr(); - Error err = parser.open(p_path); - ERR_FAIL_COND_V_MSG(err, err, "Cannot open Collada file '" + p_path + "'."); - - state.local_path = ProjectSettings::get_singleton()->localize_path(p_path); - state.import_flags = p_flags; - /* Skip headers */ - while ((err = parser.read()) == OK) { - - if (parser.get_node_type() == XMLParser::NODE_ELEMENT) { - - if (parser.get_node_name() == "COLLADA") { - break; - } else if (!parser.is_empty()) - parser.skip_section(); // unknown section, likely headers - } - } - - ERR_FAIL_COND_V_MSG(err != OK, ERR_FILE_CORRUPT, "Corrupted Collada file '" + p_path + "'."); - - /* Start loading Collada */ - - { - //version - String version = parser.get_attribute_value("version"); - state.version.major = version.get_slice(".", 0).to_int(); - state.version.minor = version.get_slice(".", 1).to_int(); - state.version.rev = version.get_slice(".", 2).to_int(); - COLLADA_PRINT("Collada VERSION: " + version); - } - - while ((err = parser.read()) == OK) { - - /* Read all the main sections.. */ - - if (parser.get_node_type() != XMLParser::NODE_ELEMENT) - continue; //no idea what this may be, but skipping anyway - - String section = parser.get_node_name(); - - COLLADA_PRINT("section: " + section); - - if (section == "asset") { - _parse_asset(parser); - - } else if (section.begins_with("library_")) { - - _parse_library(parser); - } else if (section == "scene") { - - _parse_scene(parser); - } else if (!parser.is_empty()) { - parser.skip_section(); // unknown section, likely headers - } - } - - _optimize(); - return OK; -} - -Collada::Collada() { -} diff --git a/editor/collada/collada.h b/editor/collada/collada.h deleted file mode 100644 index 4707d7d779..0000000000 --- a/editor/collada/collada.h +++ /dev/null @@ -1,647 +0,0 @@ -/*************************************************************************/ -/* collada.h */ -/*************************************************************************/ -/* This file is part of: */ -/* GODOT ENGINE */ -/* https://godotengine.org */ -/*************************************************************************/ -/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */ -/* */ -/* 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. */ -/*************************************************************************/ - -#ifndef COLLADA_H -#define COLLADA_H - -#include "core/io/xml_parser.h" -#include "core/map.h" -#include "core/project_settings.h" -#include "scene/resources/material.h" - -class Collada { -public: - enum ImportFlags { - IMPORT_FLAG_SCENE = 1, - IMPORT_FLAG_ANIMATION = 2 - }; - - struct Image { - - String path; - }; - - struct Material { - - String name; - String instance_effect; - }; - - struct Effect { - - String name; - Map params; - - struct Channel { - - int uv_idx; - String texture; - Color color; - Channel() { uv_idx = 0; } - }; - - Channel diffuse, specular, emission, bump; - float shininess; - bool found_double_sided; - bool double_sided; - bool unshaded; - - String get_texture_path(const String &p_source, Collada &state) const; - - Effect() { - diffuse.color = Color(1, 1, 1, 1); - double_sided = true; - found_double_sided = false; - shininess = 40; - unshaded = false; - } - }; - - struct CameraData { - - enum Mode { - MODE_PERSPECTIVE, - MODE_ORTHOGONAL - }; - - Mode mode; - - union { - struct { - float x_fov; - float y_fov; - } perspective; - struct { - float x_mag; - float y_mag; - } orthogonal; - }; - - float aspect; - float z_near; - float z_far; - - CameraData() : - mode(MODE_PERSPECTIVE), - aspect(1), - z_near(0.1), - z_far(100) { - perspective.x_fov = 0; - perspective.y_fov = 0; - } - }; - - struct LightData { - - enum Mode { - MODE_AMBIENT, - MODE_DIRECTIONAL, - MODE_OMNI, - MODE_SPOT - }; - - Mode mode; - - Color color; - - float constant_att; - float linear_att; - float quad_att; - - float spot_angle; - float spot_exp; - - LightData() : - mode(MODE_AMBIENT), - color(Color(1, 1, 1, 1)), - constant_att(0), - linear_att(0), - quad_att(0), - spot_angle(45), - spot_exp(1) { - } - }; - - struct MeshData { - - String name; - struct Source { - - Vector array; - int stride; - }; - - Map sources; - - struct Vertices { - - Map sources; - }; - - Map vertices; - - struct Primitives { - - struct SourceRef { - - String source; - int offset; - }; - - String material; - Map sources; - Vector polygons; - Vector indices; - int count; - int vertex_size; - }; - - Vector primitives; - - bool found_double_sided; - bool double_sided; - - MeshData() { - found_double_sided = false; - double_sided = true; - } - }; - - struct CurveData { - - String name; - bool closed; - - struct Source { - - Vector sarray; - Vector array; - int stride; - }; - - Map sources; - - Map control_vertices; - - CurveData() { - - closed = false; - } - }; - struct SkinControllerData { - - String base; - bool use_idrefs; - - Transform bind_shape; - - struct Source { - - Vector sarray; //maybe for names - Vector array; - int stride; - Source() { - stride = 1; - } - }; - - Map sources; - - struct Joints { - - Map sources; - } joints; - - struct Weights { - - struct SourceRef { - - String source; - int offset; - }; - - String material; - Map sources; - Vector sets; - Vector indices; - int count; - } weights; - - Map bone_rest_map; - - SkinControllerData() { use_idrefs = false; } - }; - - struct MorphControllerData { - - String mesh; - String mode; - - struct Source { - - int stride; - Vector sarray; //maybe for names - Vector array; - Source() { stride = 1; } - }; - - Map sources; - - Map targets; - MorphControllerData() {} - }; - - struct Vertex { - - int idx; - Vector3 vertex; - Vector3 normal; - Vector3 uv; - Vector3 uv2; - Plane tangent; - Color color; - int uid; - struct Weight { - int bone_idx; - float weight; - bool operator<(const Weight w) const { return weight > w.weight; } //heaviest first - }; - - Vector weights; - - void fix_weights() { - - weights.sort(); - if (weights.size() > 4) { - //cap to 4 and make weights add up 1 - weights.resize(4); - float total = 0; - for (int i = 0; i < 4; i++) - total += weights[i].weight; - if (total) - for (int i = 0; i < 4; i++) - weights.write[i].weight /= total; - } - } - - void fix_unit_scale(Collada &state); - - bool operator<(const Vertex &p_vert) const { - - if (uid == p_vert.uid) { - if (vertex == p_vert.vertex) { - if (normal == p_vert.normal) { - if (uv == p_vert.uv) { - if (uv2 == p_vert.uv2) { - - if (!weights.empty() || !p_vert.weights.empty()) { - - if (weights.size() == p_vert.weights.size()) { - - for (int i = 0; i < weights.size(); i++) { - if (weights[i].bone_idx != p_vert.weights[i].bone_idx) - return weights[i].bone_idx < p_vert.weights[i].bone_idx; - - if (weights[i].weight != p_vert.weights[i].weight) - return weights[i].weight < p_vert.weights[i].weight; - } - } else { - return weights.size() < p_vert.weights.size(); - } - } - - return (color < p_vert.color); - } else - return (uv2 < p_vert.uv2); - } else - return (uv < p_vert.uv); - } else - return (normal < p_vert.normal); - } else - return vertex < p_vert.vertex; - } else - return uid < p_vert.uid; - } - - Vertex() { - uid = 0; - idx = 0; - } - }; - struct Node { - - enum Type { - - TYPE_NODE, - TYPE_JOINT, - TYPE_SKELETON, //this bone is not collada, it's added afterwards as optimization - TYPE_LIGHT, - TYPE_CAMERA, - TYPE_GEOMETRY - }; - - struct XForm { - - enum Op { - OP_ROTATE, - OP_SCALE, - OP_TRANSLATE, - OP_MATRIX, - OP_VISIBILITY - }; - - String id; - Op op; - Vector data; - }; - - Type type; - - String name; - String id; - String empty_draw_type; - bool noname; - Vector xform_list; - Transform default_transform; - Transform post_transform; - Vector children; - - Node *parent; - - Transform compute_transform(Collada &state) const; - Transform get_global_transform() const; - Transform get_transform() const; - - bool ignore_anim; - - Node() { - noname = false; - type = TYPE_NODE; - parent = NULL; - ignore_anim = false; - } - virtual ~Node() { - for (int i = 0; i < children.size(); i++) - memdelete(children[i]); - }; - }; - - struct NodeSkeleton : public Node { - - NodeSkeleton() { type = TYPE_SKELETON; } - }; - - struct NodeJoint : public Node { - - NodeSkeleton *owner; - String sid; - NodeJoint() { - type = TYPE_JOINT; - owner = NULL; - } - }; - - struct NodeGeometry : public Node { - - bool controller; - String source; - - struct Material { - String target; - }; - - Map material_map; - Vector skeletons; - - NodeGeometry() { type = TYPE_GEOMETRY; } - }; - - struct NodeCamera : public Node { - - String camera; - - NodeCamera() { type = TYPE_CAMERA; } - }; - - struct NodeLight : public Node { - - String light; - - NodeLight() { type = TYPE_LIGHT; } - }; - - struct VisualScene { - - String name; - Vector root_nodes; - - ~VisualScene() { - for (int i = 0; i < root_nodes.size(); i++) - memdelete(root_nodes[i]); - } - }; - - struct AnimationClip { - - String name; - float begin; - float end; - Vector tracks; - - AnimationClip() { - begin = 0; - end = 1; - } - }; - - struct AnimationTrack { - - String id; - String target; - String param; - String component; - bool property; - - enum InterpolationType { - INTERP_LINEAR, - INTERP_BEZIER - }; - - struct Key { - - enum Type { - TYPE_FLOAT, - TYPE_MATRIX - }; - - float time; - Vector data; - Point2 in_tangent; - Point2 out_tangent; - InterpolationType interp_type; - - Key() { interp_type = INTERP_LINEAR; } - }; - - Vector get_value_at_time(float p_time) const; - - Vector keys; - - AnimationTrack() { property = false; } - }; - - /****************/ - /* IMPORT STATE */ - /****************/ - - struct State { - - int import_flags; - - float unit_scale; - Vector3::Axis up_axis; - bool z_up; - - struct Version { - - int major, minor, rev; - - bool operator<(const Version &p_ver) const { return (major == p_ver.major) ? ((minor == p_ver.minor) ? (rev < p_ver.rev) : minor < p_ver.minor) : major < p_ver.major; } - Version(int p_major = 0, int p_minor = 0, int p_rev = 0) { - major = p_major; - minor = p_minor; - rev = p_rev; - } - } version; - - Map camera_data_map; - Map mesh_data_map; - Map light_data_map; - Map curve_data_map; - - Map mesh_name_map; - Map morph_name_map; - Map morph_ownership_map; - Map skin_controller_data_map; - Map morph_controller_data_map; - - Map image_map; - Map material_map; - Map effect_map; - - Map visual_scene_map; - Map scene_map; - Set idref_joints; - Map sid_to_node_map; - //Map bone_map; - - Map bone_rest_map; - - String local_path; - String root_visual_scene; - String root_physics_scene; - - Vector animation_clips; - Vector animation_tracks; - Map> referenced_tracks; - Map> by_id_tracks; - - float animation_length; - - State() : - import_flags(0), - unit_scale(1.0), - up_axis(Vector3::AXIS_Y), - animation_length(0) { - } - } state; - - Error load(const String &p_path, int p_flags = 0); - - Collada(); - - Transform fix_transform(const Transform &p_transform); - - Transform get_root_transform() const; - - int get_uv_channel(String p_name); - -private: // private stuff - Map channel_map; - - void _parse_asset(XMLParser &parser); - void _parse_image(XMLParser &parser); - void _parse_material(XMLParser &parser); - void _parse_effect_material(XMLParser &parser, Effect &effect, String &id); - void _parse_effect(XMLParser &parser); - void _parse_camera(XMLParser &parser); - void _parse_light(XMLParser &parser); - void _parse_animation_clip(XMLParser &parser); - - void _parse_mesh_geometry(XMLParser &parser, String p_id, String p_name); - void _parse_curve_geometry(XMLParser &parser, String p_id, String p_name); - - void _parse_skin_controller(XMLParser &parser, String p_id); - void _parse_morph_controller(XMLParser &parser, String p_id); - void _parse_controller(XMLParser &parser); - - Node *_parse_visual_instance_geometry(XMLParser &parser); - Node *_parse_visual_instance_camera(XMLParser &parser); - Node *_parse_visual_instance_light(XMLParser &parser); - - Node *_parse_visual_node_instance_data(XMLParser &parser); - Node *_parse_visual_scene_node(XMLParser &parser); - void _parse_visual_scene(XMLParser &parser); - - void _parse_animation(XMLParser &parser); - void _parse_scene(XMLParser &parser); - void _parse_library(XMLParser &parser); - - Variant _parse_param(XMLParser &parser); - Vector _read_float_array(XMLParser &parser); - Vector _read_string_array(XMLParser &parser); - Transform _read_transform(XMLParser &parser); - String _read_empty_draw_type(XMLParser &parser); - - void _joint_set_owner(Collada::Node *p_node, NodeSkeleton *p_owner); - void _create_skeletons(Collada::Node **p_node, NodeSkeleton *p_skeleton = NULL); - void _find_morph_nodes(VisualScene *p_vscene, Node *p_node); - bool _remove_node(Node *p_parent, Node *p_node); - void _remove_node(VisualScene *p_vscene, Node *p_node); - void _merge_skeletons2(VisualScene *p_vscene); - void _merge_skeletons(VisualScene *p_vscene, Node *p_node); - bool _optimize_skeletons(VisualScene *p_vscene, Node *p_node); - - bool _move_geometry_to_skeletons(VisualScene *p_vscene, Node *p_node, List *p_mgeom); - - void _optimize(); -}; - -#endif // COLLADA_H diff --git a/editor/doc/SCsub b/editor/doc/SCsub deleted file mode 100644 index 2b1e889fb0..0000000000 --- a/editor/doc/SCsub +++ /dev/null @@ -1,5 +0,0 @@ -#!/usr/bin/env python - -Import('env') - -env.add_source_files(env.editor_sources, "*.cpp") diff --git a/editor/doc/doc_data.cpp b/editor/doc/doc_data.cpp deleted file mode 100644 index 66134b4428..0000000000 --- a/editor/doc/doc_data.cpp +++ /dev/null @@ -1,1217 +0,0 @@ -/*************************************************************************/ -/* doc_data.cpp */ -/*************************************************************************/ -/* This file is part of: */ -/* GODOT ENGINE */ -/* https://godotengine.org */ -/*************************************************************************/ -/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */ -/* */ -/* 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 "doc_data.h" - -#include "core/engine.h" -#include "core/global_constants.h" -#include "core/io/compression.h" -#include "core/io/marshalls.h" -#include "core/os/dir_access.h" -#include "core/project_settings.h" -#include "core/script_language.h" -#include "core/version.h" -#include "scene/resources/theme.h" - -void DocData::merge_from(const DocData &p_data) { - - for (Map::Element *E = class_list.front(); E; E = E->next()) { - - ClassDoc &c = E->get(); - - if (!p_data.class_list.has(c.name)) - continue; - - const ClassDoc &cf = p_data.class_list[c.name]; - - c.description = cf.description; - c.brief_description = cf.brief_description; - c.tutorials = cf.tutorials; - - for (int i = 0; i < c.methods.size(); i++) { - - MethodDoc &m = c.methods.write[i]; - - for (int j = 0; j < cf.methods.size(); j++) { - - if (cf.methods[j].name != m.name) - continue; - if (cf.methods[j].arguments.size() != m.arguments.size()) - continue; - // since polymorphic functions are allowed we need to check the type of - // the arguments so we make sure they are different. - int arg_count = cf.methods[j].arguments.size(); - Vector arg_used; - arg_used.resize(arg_count); - for (int l = 0; l < arg_count; ++l) - arg_used.write[l] = false; - // also there is no guarantee that argument ordering will match, so we - // have to check one by one so we make sure we have an exact match - for (int k = 0; k < arg_count; ++k) { - for (int l = 0; l < arg_count; ++l) - if (cf.methods[j].arguments[k].type == m.arguments[l].type && !arg_used[l]) { - arg_used.write[l] = true; - break; - } - } - bool not_the_same = false; - for (int l = 0; l < arg_count; ++l) - if (!arg_used[l]) // at least one of the arguments was different - not_the_same = true; - if (not_the_same) - continue; - - const MethodDoc &mf = cf.methods[j]; - - m.description = mf.description; - break; - } - } - - for (int i = 0; i < c.signals.size(); i++) { - - MethodDoc &m = c.signals.write[i]; - - for (int j = 0; j < cf.signals.size(); j++) { - - if (cf.signals[j].name != m.name) - continue; - const MethodDoc &mf = cf.signals[j]; - - m.description = mf.description; - break; - } - } - - for (int i = 0; i < c.constants.size(); i++) { - - ConstantDoc &m = c.constants.write[i]; - - for (int j = 0; j < cf.constants.size(); j++) { - - if (cf.constants[j].name != m.name) - continue; - const ConstantDoc &mf = cf.constants[j]; - - m.description = mf.description; - break; - } - } - - for (int i = 0; i < c.properties.size(); i++) { - - PropertyDoc &p = c.properties.write[i]; - - for (int j = 0; j < cf.properties.size(); j++) { - - if (cf.properties[j].name != p.name) - continue; - const PropertyDoc &pf = cf.properties[j]; - - p.description = pf.description; - break; - } - } - - for (int i = 0; i < c.theme_properties.size(); i++) { - - PropertyDoc &p = c.theme_properties.write[i]; - - for (int j = 0; j < cf.theme_properties.size(); j++) { - - if (cf.theme_properties[j].name != p.name) - continue; - const PropertyDoc &pf = cf.theme_properties[j]; - - p.description = pf.description; - break; - } - } - } -} - -void DocData::remove_from(const DocData &p_data) { - for (Map::Element *E = p_data.class_list.front(); E; E = E->next()) { - if (class_list.has(E->key())) - class_list.erase(E->key()); - } -} - -static void return_doc_from_retinfo(DocData::MethodDoc &p_method, const PropertyInfo &p_retinfo) { - - if (p_retinfo.type == Variant::INT && p_retinfo.usage & PROPERTY_USAGE_CLASS_IS_ENUM) { - p_method.return_enum = p_retinfo.class_name; - if (p_method.return_enum.begins_with("_")) //proxy class - p_method.return_enum = p_method.return_enum.substr(1, p_method.return_enum.length()); - p_method.return_type = "int"; - } else if (p_retinfo.class_name != StringName()) { - p_method.return_type = p_retinfo.class_name; - } else if (p_retinfo.hint == PROPERTY_HINT_RESOURCE_TYPE) { - p_method.return_type = p_retinfo.hint_string; - } else if (p_retinfo.type == Variant::NIL && p_retinfo.usage & PROPERTY_USAGE_NIL_IS_VARIANT) { - p_method.return_type = "Variant"; - } else if (p_retinfo.type == Variant::NIL) { - p_method.return_type = "void"; - } else { - p_method.return_type = Variant::get_type_name(p_retinfo.type); - } -} - -static void argument_doc_from_arginfo(DocData::ArgumentDoc &p_argument, const PropertyInfo &p_arginfo) { - - p_argument.name = p_arginfo.name; - - if (p_arginfo.type == Variant::INT && p_arginfo.usage & PROPERTY_USAGE_CLASS_IS_ENUM) { - p_argument.enumeration = p_arginfo.class_name; - if (p_argument.enumeration.begins_with("_")) //proxy class - p_argument.enumeration = p_argument.enumeration.substr(1, p_argument.enumeration.length()); - p_argument.type = "int"; - } else if (p_arginfo.class_name != StringName()) { - p_argument.type = p_arginfo.class_name; - } else if (p_arginfo.hint == PROPERTY_HINT_RESOURCE_TYPE) { - p_argument.type = p_arginfo.hint_string; - } else if (p_arginfo.type == Variant::NIL) { - // Parameters cannot be void, so PROPERTY_USAGE_NIL_IS_VARIANT is not necessary - p_argument.type = "Variant"; - } else { - p_argument.type = Variant::get_type_name(p_arginfo.type); - } -} - -static Variant get_documentation_default_value(const StringName &p_class_name, const StringName &p_property_name, bool &r_default_value_valid) { - - Variant default_value = Variant(); - r_default_value_valid = false; - - if (ClassDB::can_instance(p_class_name)) { - default_value = ClassDB::class_get_default_property_value(p_class_name, p_property_name, &r_default_value_valid); - } else { - // Cannot get default value of classes that can't be instanced - List inheriting_classes; - ClassDB::get_direct_inheriters_from_class(p_class_name, &inheriting_classes); - for (List::Element *E2 = inheriting_classes.front(); E2; E2 = E2->next()) { - if (ClassDB::can_instance(E2->get())) { - default_value = ClassDB::class_get_default_property_value(E2->get(), p_property_name, &r_default_value_valid); - if (r_default_value_valid) - break; - } - } - } - - return default_value; -} - -void DocData::generate(bool p_basic_types) { - - List classes; - ClassDB::get_class_list(&classes); - classes.sort_custom(); - // Move ProjectSettings, so that other classes can register properties there. - classes.move_to_back(classes.find("ProjectSettings")); - - bool skip_setter_getter_methods = true; - - while (classes.size()) { - - Set setters_getters; - - String name = classes.front()->get(); - String cname = name; - if (cname.begins_with("_")) //proxy class - cname = cname.substr(1, name.length()); - - class_list[cname] = ClassDoc(); - ClassDoc &c = class_list[cname]; - c.name = cname; - c.inherits = ClassDB::get_parent_class(name); - - List properties; - List own_properties; - if (name == "ProjectSettings") { - //special case for project settings, so settings can be documented - ProjectSettings::get_singleton()->get_property_list(&properties); - own_properties = properties; - } else { - ClassDB::get_property_list(name, &properties); - ClassDB::get_property_list(name, &own_properties, true); - } - - List::Element *EO = own_properties.front(); - for (List::Element *E = properties.front(); E; E = E->next()) { - bool inherited = EO == NULL; - if (EO && EO->get() == E->get()) { - inherited = false; - EO = EO->next(); - } - - if (E->get().usage & PROPERTY_USAGE_GROUP || E->get().usage & PROPERTY_USAGE_CATEGORY || E->get().usage & PROPERTY_USAGE_INTERNAL) - continue; - - PropertyDoc prop; - - prop.name = E->get().name; - - prop.overridden = inherited; - - bool default_value_valid = false; - Variant default_value; - - if (name == "ProjectSettings") { - // Special case for project settings, so that settings are not taken from the current project's settings - if (E->get().name == "script" || - ProjectSettings::get_singleton()->get_order(E->get().name) >= ProjectSettings::NO_BUILTIN_ORDER_BASE) { - continue; - } - if (E->get().usage & PROPERTY_USAGE_EDITOR) { - default_value = ProjectSettings::get_singleton()->property_get_revert(E->get().name); - default_value_valid = true; - } - } else { - default_value = get_documentation_default_value(name, E->get().name, default_value_valid); - - if (inherited) { - bool base_default_value_valid = false; - Variant base_default_value = get_documentation_default_value(ClassDB::get_parent_class(name), E->get().name, base_default_value_valid); - if (!default_value_valid || !base_default_value_valid || default_value == base_default_value) - continue; - } - } - - //used to track uninitialized values using valgrind - //print_line("getting default value for " + String(name) + "." + String(E->get().name)); - if (default_value_valid && default_value.get_type() != Variant::OBJECT) { - prop.default_value = default_value.get_construct_string().replace("\n", ""); - } - - StringName setter = ClassDB::get_property_setter(name, E->get().name); - StringName getter = ClassDB::get_property_getter(name, E->get().name); - - prop.setter = setter; - prop.getter = getter; - - bool found_type = false; - if (getter != StringName()) { - MethodBind *mb = ClassDB::get_method(name, getter); - if (mb) { - PropertyInfo retinfo = mb->get_return_info(); - - found_type = true; - if (retinfo.type == Variant::INT && retinfo.usage & PROPERTY_USAGE_CLASS_IS_ENUM) { - prop.enumeration = retinfo.class_name; - prop.type = "int"; - } else if (retinfo.class_name != StringName()) { - prop.type = retinfo.class_name; - } else if (retinfo.hint == PROPERTY_HINT_RESOURCE_TYPE) { - - prop.type = retinfo.hint_string; - } else if (retinfo.type == Variant::NIL && retinfo.usage & PROPERTY_USAGE_NIL_IS_VARIANT) { - - prop.type = "Variant"; - } else if (retinfo.type == Variant::NIL) { - prop.type = "void"; - } else { - prop.type = Variant::get_type_name(retinfo.type); - } - } - - setters_getters.insert(getter); - } - - if (setter != StringName()) { - - setters_getters.insert(setter); - } - - if (!found_type) { - - if (E->get().type == Variant::OBJECT && E->get().hint == PROPERTY_HINT_RESOURCE_TYPE) - prop.type = E->get().hint_string; - else - prop.type = Variant::get_type_name(E->get().type); - } - - c.properties.push_back(prop); - } - - List method_list; - ClassDB::get_method_list(name, &method_list, true); - method_list.sort(); - - for (List::Element *E = method_list.front(); E; E = E->next()) { - - if (E->get().name == "" || (E->get().name[0] == '_' && !(E->get().flags & METHOD_FLAG_VIRTUAL))) - continue; //hidden, don't count - - if (skip_setter_getter_methods && setters_getters.has(E->get().name)) { - // Don't skip parametric setters and getters, i.e. method which require - // one or more parameters to define what property should be set or retrieved. - // E.g. CPUParticles::set_param(Parameter param, float value). - if (E->get().arguments.size() == 0 /* getter */ || (E->get().arguments.size() == 1 && E->get().return_val.type == Variant::NIL /* setter */)) { - continue; - } - } - - MethodDoc method; - - method.name = E->get().name; - - if (E->get().flags & METHOD_FLAG_VIRTUAL) - method.qualifiers = "virtual"; - - if (E->get().flags & METHOD_FLAG_CONST) { - if (method.qualifiers != "") - method.qualifiers += " "; - method.qualifiers += "const"; - } else if (E->get().flags & METHOD_FLAG_VARARG) { - if (method.qualifiers != "") - method.qualifiers += " "; - method.qualifiers += "vararg"; - } - - for (int i = -1; i < E->get().arguments.size(); i++) { - - if (i == -1) { -#ifdef DEBUG_METHODS_ENABLED - return_doc_from_retinfo(method, E->get().return_val); -#endif - } else { - - const PropertyInfo &arginfo = E->get().arguments[i]; - ArgumentDoc argument; - argument_doc_from_arginfo(argument, arginfo); - - int darg_idx = i - (E->get().arguments.size() - E->get().default_arguments.size()); - if (darg_idx >= 0) { - Variant default_arg = E->get().default_arguments[darg_idx]; - argument.default_value = default_arg.get_construct_string(); - } - - method.arguments.push_back(argument); - } - } - - c.methods.push_back(method); - } - - List signal_list; - ClassDB::get_signal_list(name, &signal_list, true); - - if (signal_list.size()) { - - for (List::Element *EV = signal_list.front(); EV; EV = EV->next()) { - - MethodDoc signal; - signal.name = EV->get().name; - for (int i = 0; i < EV->get().arguments.size(); i++) { - - const PropertyInfo &arginfo = EV->get().arguments[i]; - ArgumentDoc argument; - argument_doc_from_arginfo(argument, arginfo); - - signal.arguments.push_back(argument); - } - - c.signals.push_back(signal); - } - } - - List constant_list; - ClassDB::get_integer_constant_list(name, &constant_list, true); - - for (List::Element *E = constant_list.front(); E; E = E->next()) { - - ConstantDoc constant; - constant.name = E->get(); - constant.value = itos(ClassDB::get_integer_constant(name, E->get())); - constant.enumeration = ClassDB::get_integer_constant_enum(name, E->get()); - c.constants.push_back(constant); - } - - //theme stuff - - { - List l; - Theme::get_default()->get_constant_list(cname, &l); - for (List::Element *E = l.front(); E; E = E->next()) { - - PropertyDoc pd; - pd.name = E->get(); - pd.type = "int"; - pd.default_value = itos(Theme::get_default()->get_constant(E->get(), cname)); - c.theme_properties.push_back(pd); - } - - l.clear(); - Theme::get_default()->get_color_list(cname, &l); - for (List::Element *E = l.front(); E; E = E->next()) { - - PropertyDoc pd; - pd.name = E->get(); - pd.type = "Color"; - pd.default_value = Variant(Theme::get_default()->get_color(E->get(), cname)).get_construct_string(); - c.theme_properties.push_back(pd); - } - - l.clear(); - Theme::get_default()->get_icon_list(cname, &l); - for (List::Element *E = l.front(); E; E = E->next()) { - - PropertyDoc pd; - pd.name = E->get(); - pd.type = "Texture2D"; - c.theme_properties.push_back(pd); - } - l.clear(); - Theme::get_default()->get_font_list(cname, &l); - for (List::Element *E = l.front(); E; E = E->next()) { - - PropertyDoc pd; - pd.name = E->get(); - pd.type = "Font"; - c.theme_properties.push_back(pd); - } - l.clear(); - Theme::get_default()->get_stylebox_list(cname, &l); - for (List::Element *E = l.front(); E; E = E->next()) { - - PropertyDoc pd; - pd.name = E->get(); - pd.type = "StyleBox"; - c.theme_properties.push_back(pd); - } - } - - classes.pop_front(); - } - - { - // So we can document the concept of Variant even if it's not a usable class per se. - class_list["Variant"] = ClassDoc(); - class_list["Variant"].name = "Variant"; - } - - if (!p_basic_types) - return; - - // Add Variant types. - for (int i = 0; i < Variant::VARIANT_MAX; i++) { - if (i == Variant::NIL) - continue; // Not exposed outside of 'null', should not be in class list. - if (i == Variant::OBJECT) - continue; // Use the core type instead. - - String cname = Variant::get_type_name(Variant::Type(i)); - - class_list[cname] = ClassDoc(); - ClassDoc &c = class_list[cname]; - c.name = cname; - - Callable::CallError cerror; - Variant v = Variant::construct(Variant::Type(i), NULL, 0, cerror); - - List method_list; - v.get_method_list(&method_list); - method_list.sort(); - Variant::get_constructor_list(Variant::Type(i), &method_list); - - for (List::Element *E = method_list.front(); E; E = E->next()) { - - MethodInfo &mi = E->get(); - MethodDoc method; - - method.name = mi.name; - - for (int j = 0; j < mi.arguments.size(); j++) { - - PropertyInfo arginfo = mi.arguments[j]; - ArgumentDoc ad; - argument_doc_from_arginfo(ad, mi.arguments[j]); - ad.name = arginfo.name; - - int darg_idx = mi.default_arguments.size() - mi.arguments.size() + j; - if (darg_idx >= 0) { - Variant default_arg = mi.default_arguments[darg_idx]; - ad.default_value = default_arg.get_construct_string(); - } - - method.arguments.push_back(ad); - } - - return_doc_from_retinfo(method, mi.return_val); - - if (mi.flags & METHOD_FLAG_VARARG) { - if (method.qualifiers != "") - method.qualifiers += " "; - method.qualifiers += "vararg"; - } - - c.methods.push_back(method); - } - - List properties; - v.get_property_list(&properties); - for (List::Element *E = properties.front(); E; E = E->next()) { - - PropertyInfo pi = E->get(); - PropertyDoc property; - property.name = pi.name; - property.type = Variant::get_type_name(pi.type); - property.default_value = v.get(pi.name).get_construct_string(); - - c.properties.push_back(property); - } - - List constants; - Variant::get_constants_for_type(Variant::Type(i), &constants); - - for (List::Element *E = constants.front(); E; E = E->next()) { - - ConstantDoc constant; - constant.name = E->get(); - Variant value = Variant::get_constant_value(Variant::Type(i), E->get()); - constant.value = value.get_type() == Variant::INT ? itos(value) : value.get_construct_string(); - c.constants.push_back(constant); - } - } - - //built in constants and functions - - { - - String cname = "@GlobalScope"; - class_list[cname] = ClassDoc(); - ClassDoc &c = class_list[cname]; - c.name = cname; - - for (int i = 0; i < GlobalConstants::get_global_constant_count(); i++) { - - ConstantDoc cd; - cd.name = GlobalConstants::get_global_constant_name(i); - cd.value = itos(GlobalConstants::get_global_constant_value(i)); - cd.enumeration = GlobalConstants::get_global_constant_enum(i); - c.constants.push_back(cd); - } - - List singletons; - Engine::get_singleton()->get_singletons(&singletons); - - //servers (this is kind of hackish) - for (List::Element *E = singletons.front(); E; E = E->next()) { - - PropertyDoc pd; - Engine::Singleton &s = E->get(); - if (!s.ptr) { - continue; - } - pd.name = s.name; - pd.type = s.ptr->get_class(); - while (String(ClassDB::get_parent_class(pd.type)) != "Object") - pd.type = ClassDB::get_parent_class(pd.type); - if (pd.type.begins_with("_")) - pd.type = pd.type.substr(1, pd.type.length()); - c.properties.push_back(pd); - } - } - - //built in script reference - - { - - for (int i = 0; i < ScriptServer::get_language_count(); i++) { - - ScriptLanguage *lang = ScriptServer::get_language(i); - String cname = "@" + lang->get_name(); - class_list[cname] = ClassDoc(); - ClassDoc &c = class_list[cname]; - c.name = cname; - - List minfo; - - lang->get_public_functions(&minfo); - - for (List::Element *E = minfo.front(); E; E = E->next()) { - - MethodInfo &mi = E->get(); - MethodDoc md; - md.name = mi.name; - - if (mi.flags & METHOD_FLAG_VARARG) { - if (md.qualifiers != "") - md.qualifiers += " "; - md.qualifiers += "vararg"; - } - - return_doc_from_retinfo(md, mi.return_val); - - for (int j = 0; j < mi.arguments.size(); j++) { - - ArgumentDoc ad; - argument_doc_from_arginfo(ad, mi.arguments[j]); - - int darg_idx = j - (mi.arguments.size() - mi.default_arguments.size()); - if (darg_idx >= 0) { - Variant default_arg = E->get().default_arguments[darg_idx]; - ad.default_value = default_arg.get_construct_string(); - } - - md.arguments.push_back(ad); - } - - c.methods.push_back(md); - } - - List> cinfo; - lang->get_public_constants(&cinfo); - - for (List>::Element *E = cinfo.front(); E; E = E->next()) { - - ConstantDoc cd; - cd.name = E->get().first; - cd.value = E->get().second; - c.constants.push_back(cd); - } - } - } -} - -static Error _parse_methods(Ref &parser, Vector &methods) { - - String section = parser->get_node_name(); - String element = section.substr(0, section.length() - 1); - - while (parser->read() == OK) { - - if (parser->get_node_type() == XMLParser::NODE_ELEMENT) { - - if (parser->get_node_name() == element) { - - DocData::MethodDoc method; - ERR_FAIL_COND_V(!parser->has_attribute("name"), ERR_FILE_CORRUPT); - method.name = parser->get_attribute_value("name"); - if (parser->has_attribute("qualifiers")) - method.qualifiers = parser->get_attribute_value("qualifiers"); - - while (parser->read() == OK) { - - if (parser->get_node_type() == XMLParser::NODE_ELEMENT) { - - String name = parser->get_node_name(); - if (name == "return") { - - ERR_FAIL_COND_V(!parser->has_attribute("type"), ERR_FILE_CORRUPT); - method.return_type = parser->get_attribute_value("type"); - if (parser->has_attribute("enum")) { - method.return_enum = parser->get_attribute_value("enum"); - } - } else if (name == "argument") { - - DocData::ArgumentDoc argument; - ERR_FAIL_COND_V(!parser->has_attribute("name"), ERR_FILE_CORRUPT); - argument.name = parser->get_attribute_value("name"); - ERR_FAIL_COND_V(!parser->has_attribute("type"), ERR_FILE_CORRUPT); - argument.type = parser->get_attribute_value("type"); - if (parser->has_attribute("enum")) { - argument.enumeration = parser->get_attribute_value("enum"); - } - - method.arguments.push_back(argument); - - } else if (name == "description") { - - parser->read(); - if (parser->get_node_type() == XMLParser::NODE_TEXT) - method.description = parser->get_node_data(); - } - - } else if (parser->get_node_type() == XMLParser::NODE_ELEMENT_END && parser->get_node_name() == element) - break; - } - - methods.push_back(method); - - } else { - ERR_FAIL_V_MSG(ERR_FILE_CORRUPT, "Invalid tag in doc file: " + parser->get_node_name() + "."); - } - - } else if (parser->get_node_type() == XMLParser::NODE_ELEMENT_END && parser->get_node_name() == section) - break; - } - - return OK; -} - -Error DocData::load_classes(const String &p_dir) { - - Error err; - DirAccessRef da = DirAccess::open(p_dir, &err); - if (!da) { - return err; - } - - da->list_dir_begin(); - String path; - path = da->get_next(); - while (path != String()) { - if (!da->current_is_dir() && path.ends_with("xml")) { - Ref parser = memnew(XMLParser); - Error err2 = parser->open(p_dir.plus_file(path)); - if (err2) - return err2; - - _load(parser); - } - path = da->get_next(); - } - - da->list_dir_end(); - - return OK; -} -Error DocData::erase_classes(const String &p_dir) { - - Error err; - DirAccessRef da = DirAccess::open(p_dir, &err); - if (!da) { - return err; - } - - List to_erase; - - da->list_dir_begin(); - String path; - path = da->get_next(); - while (path != String()) { - if (!da->current_is_dir() && path.ends_with("xml")) { - to_erase.push_back(path); - } - path = da->get_next(); - } - da->list_dir_end(); - - while (to_erase.size()) { - da->remove(to_erase.front()->get()); - to_erase.pop_front(); - } - - return OK; -} -Error DocData::_load(Ref parser) { - - Error err = OK; - - while ((err = parser->read()) == OK) { - - if (parser->get_node_type() == XMLParser::NODE_ELEMENT && parser->get_node_name() == "?xml") { - parser->skip_section(); - } - - if (parser->get_node_type() != XMLParser::NODE_ELEMENT) - continue; //no idea what this may be, but skipping anyway - - ERR_FAIL_COND_V(parser->get_node_name() != "class", ERR_FILE_CORRUPT); - - ERR_FAIL_COND_V(!parser->has_attribute("name"), ERR_FILE_CORRUPT); - String name = parser->get_attribute_value("name"); - class_list[name] = ClassDoc(); - ClassDoc &c = class_list[name]; - - c.name = name; - if (parser->has_attribute("inherits")) - c.inherits = parser->get_attribute_value("inherits"); - - while (parser->read() == OK) { - - if (parser->get_node_type() == XMLParser::NODE_ELEMENT) { - - String name2 = parser->get_node_name(); - - if (name2 == "brief_description") { - - parser->read(); - if (parser->get_node_type() == XMLParser::NODE_TEXT) - c.brief_description = parser->get_node_data(); - - } else if (name2 == "description") { - parser->read(); - if (parser->get_node_type() == XMLParser::NODE_TEXT) - c.description = parser->get_node_data(); - } else if (name2 == "tutorials") { - while (parser->read() == OK) { - - if (parser->get_node_type() == XMLParser::NODE_ELEMENT) { - - String name3 = parser->get_node_name(); - - if (name3 == "link") { - - parser->read(); - if (parser->get_node_type() == XMLParser::NODE_TEXT) - c.tutorials.push_back(parser->get_node_data().strip_edges()); - } else { - ERR_FAIL_V_MSG(ERR_FILE_CORRUPT, "Invalid tag in doc file: " + name3 + "."); - } - } else if (parser->get_node_type() == XMLParser::NODE_ELEMENT_END && parser->get_node_name() == "tutorials") - break; // End of . - } - } else if (name2 == "methods") { - - Error err2 = _parse_methods(parser, c.methods); - ERR_FAIL_COND_V(err2, err2); - - } else if (name2 == "signals") { - - Error err2 = _parse_methods(parser, c.signals); - ERR_FAIL_COND_V(err2, err2); - } else if (name2 == "members") { - - while (parser->read() == OK) { - - if (parser->get_node_type() == XMLParser::NODE_ELEMENT) { - - String name3 = parser->get_node_name(); - - if (name3 == "member") { - - PropertyDoc prop2; - - ERR_FAIL_COND_V(!parser->has_attribute("name"), ERR_FILE_CORRUPT); - prop2.name = parser->get_attribute_value("name"); - ERR_FAIL_COND_V(!parser->has_attribute("type"), ERR_FILE_CORRUPT); - prop2.type = parser->get_attribute_value("type"); - if (parser->has_attribute("setter")) - prop2.setter = parser->get_attribute_value("setter"); - if (parser->has_attribute("getter")) - prop2.getter = parser->get_attribute_value("getter"); - if (parser->has_attribute("enum")) - prop2.enumeration = parser->get_attribute_value("enum"); - if (!parser->is_empty()) { - parser->read(); - if (parser->get_node_type() == XMLParser::NODE_TEXT) - prop2.description = parser->get_node_data(); - } - c.properties.push_back(prop2); - } else { - ERR_FAIL_V_MSG(ERR_FILE_CORRUPT, "Invalid tag in doc file: " + name3 + "."); - } - - } else if (parser->get_node_type() == XMLParser::NODE_ELEMENT_END && parser->get_node_name() == "members") - break; // End of . - } - - } else if (name2 == "theme_items") { - - while (parser->read() == OK) { - - if (parser->get_node_type() == XMLParser::NODE_ELEMENT) { - - String name3 = parser->get_node_name(); - - if (name3 == "theme_item") { - - PropertyDoc prop2; - - ERR_FAIL_COND_V(!parser->has_attribute("name"), ERR_FILE_CORRUPT); - prop2.name = parser->get_attribute_value("name"); - ERR_FAIL_COND_V(!parser->has_attribute("type"), ERR_FILE_CORRUPT); - prop2.type = parser->get_attribute_value("type"); - if (!parser->is_empty()) { - parser->read(); - if (parser->get_node_type() == XMLParser::NODE_TEXT) - prop2.description = parser->get_node_data(); - } - c.theme_properties.push_back(prop2); - } else { - ERR_FAIL_V_MSG(ERR_FILE_CORRUPT, "Invalid tag in doc file: " + name3 + "."); - } - - } else if (parser->get_node_type() == XMLParser::NODE_ELEMENT_END && parser->get_node_name() == "theme_items") - break; // End of . - } - - } else if (name2 == "constants") { - - while (parser->read() == OK) { - - if (parser->get_node_type() == XMLParser::NODE_ELEMENT) { - - String name3 = parser->get_node_name(); - - if (name3 == "constant") { - - ConstantDoc constant2; - ERR_FAIL_COND_V(!parser->has_attribute("name"), ERR_FILE_CORRUPT); - constant2.name = parser->get_attribute_value("name"); - ERR_FAIL_COND_V(!parser->has_attribute("value"), ERR_FILE_CORRUPT); - constant2.value = parser->get_attribute_value("value"); - if (parser->has_attribute("enum")) { - constant2.enumeration = parser->get_attribute_value("enum"); - } - if (!parser->is_empty()) { - parser->read(); - if (parser->get_node_type() == XMLParser::NODE_TEXT) - constant2.description = parser->get_node_data(); - } - c.constants.push_back(constant2); - } else { - ERR_FAIL_V_MSG(ERR_FILE_CORRUPT, "Invalid tag in doc file: " + name3 + "."); - } - - } else if (parser->get_node_type() == XMLParser::NODE_ELEMENT_END && parser->get_node_name() == "constants") - break; // End of . - } - - } else { - - ERR_FAIL_V_MSG(ERR_FILE_CORRUPT, "Invalid tag in doc file: " + name2 + "."); - } - - } else if (parser->get_node_type() == XMLParser::NODE_ELEMENT_END && parser->get_node_name() == "class") - break; // End of . - } - } - - return OK; -} - -static void _write_string(FileAccess *f, int p_tablevel, const String &p_string) { - - if (p_string == "") - return; - String tab; - for (int i = 0; i < p_tablevel; i++) - tab += "\t"; - f->store_string(tab + p_string + "\n"); -} - -Error DocData::save_classes(const String &p_default_path, const Map &p_class_path) { - - for (Map::Element *E = class_list.front(); E; E = E->next()) { - - ClassDoc &c = E->get(); - - String save_path; - if (p_class_path.has(c.name)) { - save_path = p_class_path[c.name]; - } else { - save_path = p_default_path; - } - - Error err; - String save_file = save_path.plus_file(c.name + ".xml"); - FileAccessRef f = FileAccess::open(save_file, FileAccess::WRITE, &err); - - ERR_CONTINUE_MSG(err != OK, "Can't write doc file: " + save_file + "."); - - _write_string(f, 0, ""); - - String header = ""); - _write_string(f, 2, c.brief_description.strip_edges().xml_escape()); - _write_string(f, 1, ""); - - _write_string(f, 1, ""); - _write_string(f, 2, c.description.strip_edges().xml_escape()); - _write_string(f, 1, ""); - - _write_string(f, 1, ""); - for (int i = 0; i < c.tutorials.size(); i++) { - _write_string(f, 2, "" + c.tutorials.get(i).xml_escape() + ""); - } - _write_string(f, 1, ""); - - _write_string(f, 1, ""); - - c.methods.sort(); - - for (int i = 0; i < c.methods.size(); i++) { - - const MethodDoc &m = c.methods[i]; - - String qualifiers; - if (m.qualifiers != "") - qualifiers += " qualifiers=\"" + m.qualifiers.xml_escape() + "\""; - - _write_string(f, 2, ""); - - if (m.return_type != "") { - - String enum_text; - if (m.return_enum != String()) { - enum_text = " enum=\"" + m.return_enum + "\""; - } - _write_string(f, 3, ""); - _write_string(f, 3, ""); - } - - for (int j = 0; j < m.arguments.size(); j++) { - - const ArgumentDoc &a = m.arguments[j]; - - String enum_text; - if (a.enumeration != String()) { - enum_text = " enum=\"" + a.enumeration + "\""; - } - - if (a.default_value != "") - _write_string(f, 3, ""); - else - _write_string(f, 3, ""); - - _write_string(f, 3, ""); - } - - _write_string(f, 3, ""); - _write_string(f, 4, m.description.strip_edges().xml_escape()); - _write_string(f, 3, ""); - - _write_string(f, 2, ""); - } - - _write_string(f, 1, ""); - - if (c.properties.size()) { - _write_string(f, 1, ""); - - c.properties.sort(); - - for (int i = 0; i < c.properties.size(); i++) { - - String additional_attributes; - if (c.properties[i].enumeration != String()) { - additional_attributes += " enum=\"" + c.properties[i].enumeration + "\""; - } - if (c.properties[i].default_value != String()) { - additional_attributes += " default=\"" + c.properties[i].default_value.xml_escape(true) + "\""; - } - - const PropertyDoc &p = c.properties[i]; - - if (c.properties[i].overridden) { - _write_string(f, 2, ""); - } else { - _write_string(f, 2, ""); - _write_string(f, 3, p.description.strip_edges().xml_escape()); - _write_string(f, 2, ""); - } - } - _write_string(f, 1, ""); - } - - if (c.signals.size()) { - - c.signals.sort(); - - _write_string(f, 1, ""); - for (int i = 0; i < c.signals.size(); i++) { - - const MethodDoc &m = c.signals[i]; - _write_string(f, 2, ""); - for (int j = 0; j < m.arguments.size(); j++) { - - const ArgumentDoc &a = m.arguments[j]; - _write_string(f, 3, ""); - _write_string(f, 3, ""); - } - - _write_string(f, 3, ""); - _write_string(f, 4, m.description.strip_edges().xml_escape()); - _write_string(f, 3, ""); - - _write_string(f, 2, ""); - } - - _write_string(f, 1, ""); - } - - _write_string(f, 1, ""); - - for (int i = 0; i < c.constants.size(); i++) { - - const ConstantDoc &k = c.constants[i]; - if (k.enumeration != String()) { - _write_string(f, 2, ""); - } else { - _write_string(f, 2, ""); - } - _write_string(f, 3, k.description.strip_edges().xml_escape()); - _write_string(f, 2, ""); - } - - _write_string(f, 1, ""); - - if (c.theme_properties.size()) { - - c.theme_properties.sort(); - - _write_string(f, 1, ""); - for (int i = 0; i < c.theme_properties.size(); i++) { - - const PropertyDoc &p = c.theme_properties[i]; - - if (p.default_value != "") - _write_string(f, 2, ""); - else - _write_string(f, 2, ""); - - _write_string(f, 3, p.description.strip_edges().xml_escape()); - - _write_string(f, 2, ""); - } - _write_string(f, 1, ""); - } - - _write_string(f, 0, ""); - } - - return OK; -} - -Error DocData::load_compressed(const uint8_t *p_data, int p_compressed_size, int p_uncompressed_size) { - - Vector data; - data.resize(p_uncompressed_size); - Compression::decompress(data.ptrw(), p_uncompressed_size, p_data, p_compressed_size, Compression::MODE_DEFLATE); - class_list.clear(); - - Ref parser = memnew(XMLParser); - Error err = parser->open_buffer(data); - if (err) - return err; - - _load(parser); - - return OK; -} diff --git a/editor/doc/doc_data.h b/editor/doc/doc_data.h deleted file mode 100644 index 073705f0b1..0000000000 --- a/editor/doc/doc_data.h +++ /dev/null @@ -1,117 +0,0 @@ -/*************************************************************************/ -/* doc_data.h */ -/*************************************************************************/ -/* This file is part of: */ -/* GODOT ENGINE */ -/* https://godotengine.org */ -/*************************************************************************/ -/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */ -/* */ -/* 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. */ -/*************************************************************************/ - -#ifndef DOC_DATA_H -#define DOC_DATA_H - -#include "core/io/xml_parser.h" -#include "core/map.h" -#include "core/variant.h" - -class DocData { -public: - struct ArgumentDoc { - - String name; - String type; - String enumeration; - String default_value; - }; - - struct MethodDoc { - - String name; - String return_type; - String return_enum; - String qualifiers; - String description; - Vector arguments; - bool operator<(const MethodDoc &p_md) const { - return name < p_md.name; - } - }; - - struct ConstantDoc { - - String name; - String value; - String enumeration; - String description; - }; - - struct PropertyDoc { - - String name; - String type; - String enumeration; - String description; - String setter, getter; - String default_value; - bool overridden; - bool operator<(const PropertyDoc &p_prop) const { - return name < p_prop.name; - } - PropertyDoc() { - overridden = false; - } - }; - - struct ClassDoc { - - String name; - String inherits; - String category; - String brief_description; - String description; - Vector tutorials; - Vector methods; - Vector signals; - Vector constants; - Vector properties; - Vector theme_properties; - }; - - String version; - - Map class_list; - Error _load(Ref parser); - -public: - void merge_from(const DocData &p_data); - void remove_from(const DocData &p_data); - void generate(bool p_basic_types = false); - Error load_classes(const String &p_dir); - static Error erase_classes(const String &p_dir); - Error save_classes(const String &p_default_path, const Map &p_class_path); - - Error load_compressed(const uint8_t *p_data, int p_compressed_size, int p_uncompressed_size); -}; - -#endif // DOC_DATA_H diff --git a/editor/doc_data.cpp b/editor/doc_data.cpp new file mode 100644 index 0000000000..66134b4428 --- /dev/null +++ b/editor/doc_data.cpp @@ -0,0 +1,1217 @@ +/*************************************************************************/ +/* doc_data.cpp */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */ +/* */ +/* 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 "doc_data.h" + +#include "core/engine.h" +#include "core/global_constants.h" +#include "core/io/compression.h" +#include "core/io/marshalls.h" +#include "core/os/dir_access.h" +#include "core/project_settings.h" +#include "core/script_language.h" +#include "core/version.h" +#include "scene/resources/theme.h" + +void DocData::merge_from(const DocData &p_data) { + + for (Map::Element *E = class_list.front(); E; E = E->next()) { + + ClassDoc &c = E->get(); + + if (!p_data.class_list.has(c.name)) + continue; + + const ClassDoc &cf = p_data.class_list[c.name]; + + c.description = cf.description; + c.brief_description = cf.brief_description; + c.tutorials = cf.tutorials; + + for (int i = 0; i < c.methods.size(); i++) { + + MethodDoc &m = c.methods.write[i]; + + for (int j = 0; j < cf.methods.size(); j++) { + + if (cf.methods[j].name != m.name) + continue; + if (cf.methods[j].arguments.size() != m.arguments.size()) + continue; + // since polymorphic functions are allowed we need to check the type of + // the arguments so we make sure they are different. + int arg_count = cf.methods[j].arguments.size(); + Vector arg_used; + arg_used.resize(arg_count); + for (int l = 0; l < arg_count; ++l) + arg_used.write[l] = false; + // also there is no guarantee that argument ordering will match, so we + // have to check one by one so we make sure we have an exact match + for (int k = 0; k < arg_count; ++k) { + for (int l = 0; l < arg_count; ++l) + if (cf.methods[j].arguments[k].type == m.arguments[l].type && !arg_used[l]) { + arg_used.write[l] = true; + break; + } + } + bool not_the_same = false; + for (int l = 0; l < arg_count; ++l) + if (!arg_used[l]) // at least one of the arguments was different + not_the_same = true; + if (not_the_same) + continue; + + const MethodDoc &mf = cf.methods[j]; + + m.description = mf.description; + break; + } + } + + for (int i = 0; i < c.signals.size(); i++) { + + MethodDoc &m = c.signals.write[i]; + + for (int j = 0; j < cf.signals.size(); j++) { + + if (cf.signals[j].name != m.name) + continue; + const MethodDoc &mf = cf.signals[j]; + + m.description = mf.description; + break; + } + } + + for (int i = 0; i < c.constants.size(); i++) { + + ConstantDoc &m = c.constants.write[i]; + + for (int j = 0; j < cf.constants.size(); j++) { + + if (cf.constants[j].name != m.name) + continue; + const ConstantDoc &mf = cf.constants[j]; + + m.description = mf.description; + break; + } + } + + for (int i = 0; i < c.properties.size(); i++) { + + PropertyDoc &p = c.properties.write[i]; + + for (int j = 0; j < cf.properties.size(); j++) { + + if (cf.properties[j].name != p.name) + continue; + const PropertyDoc &pf = cf.properties[j]; + + p.description = pf.description; + break; + } + } + + for (int i = 0; i < c.theme_properties.size(); i++) { + + PropertyDoc &p = c.theme_properties.write[i]; + + for (int j = 0; j < cf.theme_properties.size(); j++) { + + if (cf.theme_properties[j].name != p.name) + continue; + const PropertyDoc &pf = cf.theme_properties[j]; + + p.description = pf.description; + break; + } + } + } +} + +void DocData::remove_from(const DocData &p_data) { + for (Map::Element *E = p_data.class_list.front(); E; E = E->next()) { + if (class_list.has(E->key())) + class_list.erase(E->key()); + } +} + +static void return_doc_from_retinfo(DocData::MethodDoc &p_method, const PropertyInfo &p_retinfo) { + + if (p_retinfo.type == Variant::INT && p_retinfo.usage & PROPERTY_USAGE_CLASS_IS_ENUM) { + p_method.return_enum = p_retinfo.class_name; + if (p_method.return_enum.begins_with("_")) //proxy class + p_method.return_enum = p_method.return_enum.substr(1, p_method.return_enum.length()); + p_method.return_type = "int"; + } else if (p_retinfo.class_name != StringName()) { + p_method.return_type = p_retinfo.class_name; + } else if (p_retinfo.hint == PROPERTY_HINT_RESOURCE_TYPE) { + p_method.return_type = p_retinfo.hint_string; + } else if (p_retinfo.type == Variant::NIL && p_retinfo.usage & PROPERTY_USAGE_NIL_IS_VARIANT) { + p_method.return_type = "Variant"; + } else if (p_retinfo.type == Variant::NIL) { + p_method.return_type = "void"; + } else { + p_method.return_type = Variant::get_type_name(p_retinfo.type); + } +} + +static void argument_doc_from_arginfo(DocData::ArgumentDoc &p_argument, const PropertyInfo &p_arginfo) { + + p_argument.name = p_arginfo.name; + + if (p_arginfo.type == Variant::INT && p_arginfo.usage & PROPERTY_USAGE_CLASS_IS_ENUM) { + p_argument.enumeration = p_arginfo.class_name; + if (p_argument.enumeration.begins_with("_")) //proxy class + p_argument.enumeration = p_argument.enumeration.substr(1, p_argument.enumeration.length()); + p_argument.type = "int"; + } else if (p_arginfo.class_name != StringName()) { + p_argument.type = p_arginfo.class_name; + } else if (p_arginfo.hint == PROPERTY_HINT_RESOURCE_TYPE) { + p_argument.type = p_arginfo.hint_string; + } else if (p_arginfo.type == Variant::NIL) { + // Parameters cannot be void, so PROPERTY_USAGE_NIL_IS_VARIANT is not necessary + p_argument.type = "Variant"; + } else { + p_argument.type = Variant::get_type_name(p_arginfo.type); + } +} + +static Variant get_documentation_default_value(const StringName &p_class_name, const StringName &p_property_name, bool &r_default_value_valid) { + + Variant default_value = Variant(); + r_default_value_valid = false; + + if (ClassDB::can_instance(p_class_name)) { + default_value = ClassDB::class_get_default_property_value(p_class_name, p_property_name, &r_default_value_valid); + } else { + // Cannot get default value of classes that can't be instanced + List inheriting_classes; + ClassDB::get_direct_inheriters_from_class(p_class_name, &inheriting_classes); + for (List::Element *E2 = inheriting_classes.front(); E2; E2 = E2->next()) { + if (ClassDB::can_instance(E2->get())) { + default_value = ClassDB::class_get_default_property_value(E2->get(), p_property_name, &r_default_value_valid); + if (r_default_value_valid) + break; + } + } + } + + return default_value; +} + +void DocData::generate(bool p_basic_types) { + + List classes; + ClassDB::get_class_list(&classes); + classes.sort_custom(); + // Move ProjectSettings, so that other classes can register properties there. + classes.move_to_back(classes.find("ProjectSettings")); + + bool skip_setter_getter_methods = true; + + while (classes.size()) { + + Set setters_getters; + + String name = classes.front()->get(); + String cname = name; + if (cname.begins_with("_")) //proxy class + cname = cname.substr(1, name.length()); + + class_list[cname] = ClassDoc(); + ClassDoc &c = class_list[cname]; + c.name = cname; + c.inherits = ClassDB::get_parent_class(name); + + List properties; + List own_properties; + if (name == "ProjectSettings") { + //special case for project settings, so settings can be documented + ProjectSettings::get_singleton()->get_property_list(&properties); + own_properties = properties; + } else { + ClassDB::get_property_list(name, &properties); + ClassDB::get_property_list(name, &own_properties, true); + } + + List::Element *EO = own_properties.front(); + for (List::Element *E = properties.front(); E; E = E->next()) { + bool inherited = EO == NULL; + if (EO && EO->get() == E->get()) { + inherited = false; + EO = EO->next(); + } + + if (E->get().usage & PROPERTY_USAGE_GROUP || E->get().usage & PROPERTY_USAGE_CATEGORY || E->get().usage & PROPERTY_USAGE_INTERNAL) + continue; + + PropertyDoc prop; + + prop.name = E->get().name; + + prop.overridden = inherited; + + bool default_value_valid = false; + Variant default_value; + + if (name == "ProjectSettings") { + // Special case for project settings, so that settings are not taken from the current project's settings + if (E->get().name == "script" || + ProjectSettings::get_singleton()->get_order(E->get().name) >= ProjectSettings::NO_BUILTIN_ORDER_BASE) { + continue; + } + if (E->get().usage & PROPERTY_USAGE_EDITOR) { + default_value = ProjectSettings::get_singleton()->property_get_revert(E->get().name); + default_value_valid = true; + } + } else { + default_value = get_documentation_default_value(name, E->get().name, default_value_valid); + + if (inherited) { + bool base_default_value_valid = false; + Variant base_default_value = get_documentation_default_value(ClassDB::get_parent_class(name), E->get().name, base_default_value_valid); + if (!default_value_valid || !base_default_value_valid || default_value == base_default_value) + continue; + } + } + + //used to track uninitialized values using valgrind + //print_line("getting default value for " + String(name) + "." + String(E->get().name)); + if (default_value_valid && default_value.get_type() != Variant::OBJECT) { + prop.default_value = default_value.get_construct_string().replace("\n", ""); + } + + StringName setter = ClassDB::get_property_setter(name, E->get().name); + StringName getter = ClassDB::get_property_getter(name, E->get().name); + + prop.setter = setter; + prop.getter = getter; + + bool found_type = false; + if (getter != StringName()) { + MethodBind *mb = ClassDB::get_method(name, getter); + if (mb) { + PropertyInfo retinfo = mb->get_return_info(); + + found_type = true; + if (retinfo.type == Variant::INT && retinfo.usage & PROPERTY_USAGE_CLASS_IS_ENUM) { + prop.enumeration = retinfo.class_name; + prop.type = "int"; + } else if (retinfo.class_name != StringName()) { + prop.type = retinfo.class_name; + } else if (retinfo.hint == PROPERTY_HINT_RESOURCE_TYPE) { + + prop.type = retinfo.hint_string; + } else if (retinfo.type == Variant::NIL && retinfo.usage & PROPERTY_USAGE_NIL_IS_VARIANT) { + + prop.type = "Variant"; + } else if (retinfo.type == Variant::NIL) { + prop.type = "void"; + } else { + prop.type = Variant::get_type_name(retinfo.type); + } + } + + setters_getters.insert(getter); + } + + if (setter != StringName()) { + + setters_getters.insert(setter); + } + + if (!found_type) { + + if (E->get().type == Variant::OBJECT && E->get().hint == PROPERTY_HINT_RESOURCE_TYPE) + prop.type = E->get().hint_string; + else + prop.type = Variant::get_type_name(E->get().type); + } + + c.properties.push_back(prop); + } + + List method_list; + ClassDB::get_method_list(name, &method_list, true); + method_list.sort(); + + for (List::Element *E = method_list.front(); E; E = E->next()) { + + if (E->get().name == "" || (E->get().name[0] == '_' && !(E->get().flags & METHOD_FLAG_VIRTUAL))) + continue; //hidden, don't count + + if (skip_setter_getter_methods && setters_getters.has(E->get().name)) { + // Don't skip parametric setters and getters, i.e. method which require + // one or more parameters to define what property should be set or retrieved. + // E.g. CPUParticles::set_param(Parameter param, float value). + if (E->get().arguments.size() == 0 /* getter */ || (E->get().arguments.size() == 1 && E->get().return_val.type == Variant::NIL /* setter */)) { + continue; + } + } + + MethodDoc method; + + method.name = E->get().name; + + if (E->get().flags & METHOD_FLAG_VIRTUAL) + method.qualifiers = "virtual"; + + if (E->get().flags & METHOD_FLAG_CONST) { + if (method.qualifiers != "") + method.qualifiers += " "; + method.qualifiers += "const"; + } else if (E->get().flags & METHOD_FLAG_VARARG) { + if (method.qualifiers != "") + method.qualifiers += " "; + method.qualifiers += "vararg"; + } + + for (int i = -1; i < E->get().arguments.size(); i++) { + + if (i == -1) { +#ifdef DEBUG_METHODS_ENABLED + return_doc_from_retinfo(method, E->get().return_val); +#endif + } else { + + const PropertyInfo &arginfo = E->get().arguments[i]; + ArgumentDoc argument; + argument_doc_from_arginfo(argument, arginfo); + + int darg_idx = i - (E->get().arguments.size() - E->get().default_arguments.size()); + if (darg_idx >= 0) { + Variant default_arg = E->get().default_arguments[darg_idx]; + argument.default_value = default_arg.get_construct_string(); + } + + method.arguments.push_back(argument); + } + } + + c.methods.push_back(method); + } + + List signal_list; + ClassDB::get_signal_list(name, &signal_list, true); + + if (signal_list.size()) { + + for (List::Element *EV = signal_list.front(); EV; EV = EV->next()) { + + MethodDoc signal; + signal.name = EV->get().name; + for (int i = 0; i < EV->get().arguments.size(); i++) { + + const PropertyInfo &arginfo = EV->get().arguments[i]; + ArgumentDoc argument; + argument_doc_from_arginfo(argument, arginfo); + + signal.arguments.push_back(argument); + } + + c.signals.push_back(signal); + } + } + + List constant_list; + ClassDB::get_integer_constant_list(name, &constant_list, true); + + for (List::Element *E = constant_list.front(); E; E = E->next()) { + + ConstantDoc constant; + constant.name = E->get(); + constant.value = itos(ClassDB::get_integer_constant(name, E->get())); + constant.enumeration = ClassDB::get_integer_constant_enum(name, E->get()); + c.constants.push_back(constant); + } + + //theme stuff + + { + List l; + Theme::get_default()->get_constant_list(cname, &l); + for (List::Element *E = l.front(); E; E = E->next()) { + + PropertyDoc pd; + pd.name = E->get(); + pd.type = "int"; + pd.default_value = itos(Theme::get_default()->get_constant(E->get(), cname)); + c.theme_properties.push_back(pd); + } + + l.clear(); + Theme::get_default()->get_color_list(cname, &l); + for (List::Element *E = l.front(); E; E = E->next()) { + + PropertyDoc pd; + pd.name = E->get(); + pd.type = "Color"; + pd.default_value = Variant(Theme::get_default()->get_color(E->get(), cname)).get_construct_string(); + c.theme_properties.push_back(pd); + } + + l.clear(); + Theme::get_default()->get_icon_list(cname, &l); + for (List::Element *E = l.front(); E; E = E->next()) { + + PropertyDoc pd; + pd.name = E->get(); + pd.type = "Texture2D"; + c.theme_properties.push_back(pd); + } + l.clear(); + Theme::get_default()->get_font_list(cname, &l); + for (List::Element *E = l.front(); E; E = E->next()) { + + PropertyDoc pd; + pd.name = E->get(); + pd.type = "Font"; + c.theme_properties.push_back(pd); + } + l.clear(); + Theme::get_default()->get_stylebox_list(cname, &l); + for (List::Element *E = l.front(); E; E = E->next()) { + + PropertyDoc pd; + pd.name = E->get(); + pd.type = "StyleBox"; + c.theme_properties.push_back(pd); + } + } + + classes.pop_front(); + } + + { + // So we can document the concept of Variant even if it's not a usable class per se. + class_list["Variant"] = ClassDoc(); + class_list["Variant"].name = "Variant"; + } + + if (!p_basic_types) + return; + + // Add Variant types. + for (int i = 0; i < Variant::VARIANT_MAX; i++) { + if (i == Variant::NIL) + continue; // Not exposed outside of 'null', should not be in class list. + if (i == Variant::OBJECT) + continue; // Use the core type instead. + + String cname = Variant::get_type_name(Variant::Type(i)); + + class_list[cname] = ClassDoc(); + ClassDoc &c = class_list[cname]; + c.name = cname; + + Callable::CallError cerror; + Variant v = Variant::construct(Variant::Type(i), NULL, 0, cerror); + + List method_list; + v.get_method_list(&method_list); + method_list.sort(); + Variant::get_constructor_list(Variant::Type(i), &method_list); + + for (List::Element *E = method_list.front(); E; E = E->next()) { + + MethodInfo &mi = E->get(); + MethodDoc method; + + method.name = mi.name; + + for (int j = 0; j < mi.arguments.size(); j++) { + + PropertyInfo arginfo = mi.arguments[j]; + ArgumentDoc ad; + argument_doc_from_arginfo(ad, mi.arguments[j]); + ad.name = arginfo.name; + + int darg_idx = mi.default_arguments.size() - mi.arguments.size() + j; + if (darg_idx >= 0) { + Variant default_arg = mi.default_arguments[darg_idx]; + ad.default_value = default_arg.get_construct_string(); + } + + method.arguments.push_back(ad); + } + + return_doc_from_retinfo(method, mi.return_val); + + if (mi.flags & METHOD_FLAG_VARARG) { + if (method.qualifiers != "") + method.qualifiers += " "; + method.qualifiers += "vararg"; + } + + c.methods.push_back(method); + } + + List properties; + v.get_property_list(&properties); + for (List::Element *E = properties.front(); E; E = E->next()) { + + PropertyInfo pi = E->get(); + PropertyDoc property; + property.name = pi.name; + property.type = Variant::get_type_name(pi.type); + property.default_value = v.get(pi.name).get_construct_string(); + + c.properties.push_back(property); + } + + List constants; + Variant::get_constants_for_type(Variant::Type(i), &constants); + + for (List::Element *E = constants.front(); E; E = E->next()) { + + ConstantDoc constant; + constant.name = E->get(); + Variant value = Variant::get_constant_value(Variant::Type(i), E->get()); + constant.value = value.get_type() == Variant::INT ? itos(value) : value.get_construct_string(); + c.constants.push_back(constant); + } + } + + //built in constants and functions + + { + + String cname = "@GlobalScope"; + class_list[cname] = ClassDoc(); + ClassDoc &c = class_list[cname]; + c.name = cname; + + for (int i = 0; i < GlobalConstants::get_global_constant_count(); i++) { + + ConstantDoc cd; + cd.name = GlobalConstants::get_global_constant_name(i); + cd.value = itos(GlobalConstants::get_global_constant_value(i)); + cd.enumeration = GlobalConstants::get_global_constant_enum(i); + c.constants.push_back(cd); + } + + List singletons; + Engine::get_singleton()->get_singletons(&singletons); + + //servers (this is kind of hackish) + for (List::Element *E = singletons.front(); E; E = E->next()) { + + PropertyDoc pd; + Engine::Singleton &s = E->get(); + if (!s.ptr) { + continue; + } + pd.name = s.name; + pd.type = s.ptr->get_class(); + while (String(ClassDB::get_parent_class(pd.type)) != "Object") + pd.type = ClassDB::get_parent_class(pd.type); + if (pd.type.begins_with("_")) + pd.type = pd.type.substr(1, pd.type.length()); + c.properties.push_back(pd); + } + } + + //built in script reference + + { + + for (int i = 0; i < ScriptServer::get_language_count(); i++) { + + ScriptLanguage *lang = ScriptServer::get_language(i); + String cname = "@" + lang->get_name(); + class_list[cname] = ClassDoc(); + ClassDoc &c = class_list[cname]; + c.name = cname; + + List minfo; + + lang->get_public_functions(&minfo); + + for (List::Element *E = minfo.front(); E; E = E->next()) { + + MethodInfo &mi = E->get(); + MethodDoc md; + md.name = mi.name; + + if (mi.flags & METHOD_FLAG_VARARG) { + if (md.qualifiers != "") + md.qualifiers += " "; + md.qualifiers += "vararg"; + } + + return_doc_from_retinfo(md, mi.return_val); + + for (int j = 0; j < mi.arguments.size(); j++) { + + ArgumentDoc ad; + argument_doc_from_arginfo(ad, mi.arguments[j]); + + int darg_idx = j - (mi.arguments.size() - mi.default_arguments.size()); + if (darg_idx >= 0) { + Variant default_arg = E->get().default_arguments[darg_idx]; + ad.default_value = default_arg.get_construct_string(); + } + + md.arguments.push_back(ad); + } + + c.methods.push_back(md); + } + + List> cinfo; + lang->get_public_constants(&cinfo); + + for (List>::Element *E = cinfo.front(); E; E = E->next()) { + + ConstantDoc cd; + cd.name = E->get().first; + cd.value = E->get().second; + c.constants.push_back(cd); + } + } + } +} + +static Error _parse_methods(Ref &parser, Vector &methods) { + + String section = parser->get_node_name(); + String element = section.substr(0, section.length() - 1); + + while (parser->read() == OK) { + + if (parser->get_node_type() == XMLParser::NODE_ELEMENT) { + + if (parser->get_node_name() == element) { + + DocData::MethodDoc method; + ERR_FAIL_COND_V(!parser->has_attribute("name"), ERR_FILE_CORRUPT); + method.name = parser->get_attribute_value("name"); + if (parser->has_attribute("qualifiers")) + method.qualifiers = parser->get_attribute_value("qualifiers"); + + while (parser->read() == OK) { + + if (parser->get_node_type() == XMLParser::NODE_ELEMENT) { + + String name = parser->get_node_name(); + if (name == "return") { + + ERR_FAIL_COND_V(!parser->has_attribute("type"), ERR_FILE_CORRUPT); + method.return_type = parser->get_attribute_value("type"); + if (parser->has_attribute("enum")) { + method.return_enum = parser->get_attribute_value("enum"); + } + } else if (name == "argument") { + + DocData::ArgumentDoc argument; + ERR_FAIL_COND_V(!parser->has_attribute("name"), ERR_FILE_CORRUPT); + argument.name = parser->get_attribute_value("name"); + ERR_FAIL_COND_V(!parser->has_attribute("type"), ERR_FILE_CORRUPT); + argument.type = parser->get_attribute_value("type"); + if (parser->has_attribute("enum")) { + argument.enumeration = parser->get_attribute_value("enum"); + } + + method.arguments.push_back(argument); + + } else if (name == "description") { + + parser->read(); + if (parser->get_node_type() == XMLParser::NODE_TEXT) + method.description = parser->get_node_data(); + } + + } else if (parser->get_node_type() == XMLParser::NODE_ELEMENT_END && parser->get_node_name() == element) + break; + } + + methods.push_back(method); + + } else { + ERR_FAIL_V_MSG(ERR_FILE_CORRUPT, "Invalid tag in doc file: " + parser->get_node_name() + "."); + } + + } else if (parser->get_node_type() == XMLParser::NODE_ELEMENT_END && parser->get_node_name() == section) + break; + } + + return OK; +} + +Error DocData::load_classes(const String &p_dir) { + + Error err; + DirAccessRef da = DirAccess::open(p_dir, &err); + if (!da) { + return err; + } + + da->list_dir_begin(); + String path; + path = da->get_next(); + while (path != String()) { + if (!da->current_is_dir() && path.ends_with("xml")) { + Ref parser = memnew(XMLParser); + Error err2 = parser->open(p_dir.plus_file(path)); + if (err2) + return err2; + + _load(parser); + } + path = da->get_next(); + } + + da->list_dir_end(); + + return OK; +} +Error DocData::erase_classes(const String &p_dir) { + + Error err; + DirAccessRef da = DirAccess::open(p_dir, &err); + if (!da) { + return err; + } + + List to_erase; + + da->list_dir_begin(); + String path; + path = da->get_next(); + while (path != String()) { + if (!da->current_is_dir() && path.ends_with("xml")) { + to_erase.push_back(path); + } + path = da->get_next(); + } + da->list_dir_end(); + + while (to_erase.size()) { + da->remove(to_erase.front()->get()); + to_erase.pop_front(); + } + + return OK; +} +Error DocData::_load(Ref parser) { + + Error err = OK; + + while ((err = parser->read()) == OK) { + + if (parser->get_node_type() == XMLParser::NODE_ELEMENT && parser->get_node_name() == "?xml") { + parser->skip_section(); + } + + if (parser->get_node_type() != XMLParser::NODE_ELEMENT) + continue; //no idea what this may be, but skipping anyway + + ERR_FAIL_COND_V(parser->get_node_name() != "class", ERR_FILE_CORRUPT); + + ERR_FAIL_COND_V(!parser->has_attribute("name"), ERR_FILE_CORRUPT); + String name = parser->get_attribute_value("name"); + class_list[name] = ClassDoc(); + ClassDoc &c = class_list[name]; + + c.name = name; + if (parser->has_attribute("inherits")) + c.inherits = parser->get_attribute_value("inherits"); + + while (parser->read() == OK) { + + if (parser->get_node_type() == XMLParser::NODE_ELEMENT) { + + String name2 = parser->get_node_name(); + + if (name2 == "brief_description") { + + parser->read(); + if (parser->get_node_type() == XMLParser::NODE_TEXT) + c.brief_description = parser->get_node_data(); + + } else if (name2 == "description") { + parser->read(); + if (parser->get_node_type() == XMLParser::NODE_TEXT) + c.description = parser->get_node_data(); + } else if (name2 == "tutorials") { + while (parser->read() == OK) { + + if (parser->get_node_type() == XMLParser::NODE_ELEMENT) { + + String name3 = parser->get_node_name(); + + if (name3 == "link") { + + parser->read(); + if (parser->get_node_type() == XMLParser::NODE_TEXT) + c.tutorials.push_back(parser->get_node_data().strip_edges()); + } else { + ERR_FAIL_V_MSG(ERR_FILE_CORRUPT, "Invalid tag in doc file: " + name3 + "."); + } + } else if (parser->get_node_type() == XMLParser::NODE_ELEMENT_END && parser->get_node_name() == "tutorials") + break; // End of . + } + } else if (name2 == "methods") { + + Error err2 = _parse_methods(parser, c.methods); + ERR_FAIL_COND_V(err2, err2); + + } else if (name2 == "signals") { + + Error err2 = _parse_methods(parser, c.signals); + ERR_FAIL_COND_V(err2, err2); + } else if (name2 == "members") { + + while (parser->read() == OK) { + + if (parser->get_node_type() == XMLParser::NODE_ELEMENT) { + + String name3 = parser->get_node_name(); + + if (name3 == "member") { + + PropertyDoc prop2; + + ERR_FAIL_COND_V(!parser->has_attribute("name"), ERR_FILE_CORRUPT); + prop2.name = parser->get_attribute_value("name"); + ERR_FAIL_COND_V(!parser->has_attribute("type"), ERR_FILE_CORRUPT); + prop2.type = parser->get_attribute_value("type"); + if (parser->has_attribute("setter")) + prop2.setter = parser->get_attribute_value("setter"); + if (parser->has_attribute("getter")) + prop2.getter = parser->get_attribute_value("getter"); + if (parser->has_attribute("enum")) + prop2.enumeration = parser->get_attribute_value("enum"); + if (!parser->is_empty()) { + parser->read(); + if (parser->get_node_type() == XMLParser::NODE_TEXT) + prop2.description = parser->get_node_data(); + } + c.properties.push_back(prop2); + } else { + ERR_FAIL_V_MSG(ERR_FILE_CORRUPT, "Invalid tag in doc file: " + name3 + "."); + } + + } else if (parser->get_node_type() == XMLParser::NODE_ELEMENT_END && parser->get_node_name() == "members") + break; // End of . + } + + } else if (name2 == "theme_items") { + + while (parser->read() == OK) { + + if (parser->get_node_type() == XMLParser::NODE_ELEMENT) { + + String name3 = parser->get_node_name(); + + if (name3 == "theme_item") { + + PropertyDoc prop2; + + ERR_FAIL_COND_V(!parser->has_attribute("name"), ERR_FILE_CORRUPT); + prop2.name = parser->get_attribute_value("name"); + ERR_FAIL_COND_V(!parser->has_attribute("type"), ERR_FILE_CORRUPT); + prop2.type = parser->get_attribute_value("type"); + if (!parser->is_empty()) { + parser->read(); + if (parser->get_node_type() == XMLParser::NODE_TEXT) + prop2.description = parser->get_node_data(); + } + c.theme_properties.push_back(prop2); + } else { + ERR_FAIL_V_MSG(ERR_FILE_CORRUPT, "Invalid tag in doc file: " + name3 + "."); + } + + } else if (parser->get_node_type() == XMLParser::NODE_ELEMENT_END && parser->get_node_name() == "theme_items") + break; // End of . + } + + } else if (name2 == "constants") { + + while (parser->read() == OK) { + + if (parser->get_node_type() == XMLParser::NODE_ELEMENT) { + + String name3 = parser->get_node_name(); + + if (name3 == "constant") { + + ConstantDoc constant2; + ERR_FAIL_COND_V(!parser->has_attribute("name"), ERR_FILE_CORRUPT); + constant2.name = parser->get_attribute_value("name"); + ERR_FAIL_COND_V(!parser->has_attribute("value"), ERR_FILE_CORRUPT); + constant2.value = parser->get_attribute_value("value"); + if (parser->has_attribute("enum")) { + constant2.enumeration = parser->get_attribute_value("enum"); + } + if (!parser->is_empty()) { + parser->read(); + if (parser->get_node_type() == XMLParser::NODE_TEXT) + constant2.description = parser->get_node_data(); + } + c.constants.push_back(constant2); + } else { + ERR_FAIL_V_MSG(ERR_FILE_CORRUPT, "Invalid tag in doc file: " + name3 + "."); + } + + } else if (parser->get_node_type() == XMLParser::NODE_ELEMENT_END && parser->get_node_name() == "constants") + break; // End of . + } + + } else { + + ERR_FAIL_V_MSG(ERR_FILE_CORRUPT, "Invalid tag in doc file: " + name2 + "."); + } + + } else if (parser->get_node_type() == XMLParser::NODE_ELEMENT_END && parser->get_node_name() == "class") + break; // End of . + } + } + + return OK; +} + +static void _write_string(FileAccess *f, int p_tablevel, const String &p_string) { + + if (p_string == "") + return; + String tab; + for (int i = 0; i < p_tablevel; i++) + tab += "\t"; + f->store_string(tab + p_string + "\n"); +} + +Error DocData::save_classes(const String &p_default_path, const Map &p_class_path) { + + for (Map::Element *E = class_list.front(); E; E = E->next()) { + + ClassDoc &c = E->get(); + + String save_path; + if (p_class_path.has(c.name)) { + save_path = p_class_path[c.name]; + } else { + save_path = p_default_path; + } + + Error err; + String save_file = save_path.plus_file(c.name + ".xml"); + FileAccessRef f = FileAccess::open(save_file, FileAccess::WRITE, &err); + + ERR_CONTINUE_MSG(err != OK, "Can't write doc file: " + save_file + "."); + + _write_string(f, 0, ""); + + String header = ""); + _write_string(f, 2, c.brief_description.strip_edges().xml_escape()); + _write_string(f, 1, ""); + + _write_string(f, 1, ""); + _write_string(f, 2, c.description.strip_edges().xml_escape()); + _write_string(f, 1, ""); + + _write_string(f, 1, ""); + for (int i = 0; i < c.tutorials.size(); i++) { + _write_string(f, 2, "" + c.tutorials.get(i).xml_escape() + ""); + } + _write_string(f, 1, ""); + + _write_string(f, 1, ""); + + c.methods.sort(); + + for (int i = 0; i < c.methods.size(); i++) { + + const MethodDoc &m = c.methods[i]; + + String qualifiers; + if (m.qualifiers != "") + qualifiers += " qualifiers=\"" + m.qualifiers.xml_escape() + "\""; + + _write_string(f, 2, ""); + + if (m.return_type != "") { + + String enum_text; + if (m.return_enum != String()) { + enum_text = " enum=\"" + m.return_enum + "\""; + } + _write_string(f, 3, ""); + _write_string(f, 3, ""); + } + + for (int j = 0; j < m.arguments.size(); j++) { + + const ArgumentDoc &a = m.arguments[j]; + + String enum_text; + if (a.enumeration != String()) { + enum_text = " enum=\"" + a.enumeration + "\""; + } + + if (a.default_value != "") + _write_string(f, 3, ""); + else + _write_string(f, 3, ""); + + _write_string(f, 3, ""); + } + + _write_string(f, 3, ""); + _write_string(f, 4, m.description.strip_edges().xml_escape()); + _write_string(f, 3, ""); + + _write_string(f, 2, ""); + } + + _write_string(f, 1, ""); + + if (c.properties.size()) { + _write_string(f, 1, ""); + + c.properties.sort(); + + for (int i = 0; i < c.properties.size(); i++) { + + String additional_attributes; + if (c.properties[i].enumeration != String()) { + additional_attributes += " enum=\"" + c.properties[i].enumeration + "\""; + } + if (c.properties[i].default_value != String()) { + additional_attributes += " default=\"" + c.properties[i].default_value.xml_escape(true) + "\""; + } + + const PropertyDoc &p = c.properties[i]; + + if (c.properties[i].overridden) { + _write_string(f, 2, ""); + } else { + _write_string(f, 2, ""); + _write_string(f, 3, p.description.strip_edges().xml_escape()); + _write_string(f, 2, ""); + } + } + _write_string(f, 1, ""); + } + + if (c.signals.size()) { + + c.signals.sort(); + + _write_string(f, 1, ""); + for (int i = 0; i < c.signals.size(); i++) { + + const MethodDoc &m = c.signals[i]; + _write_string(f, 2, ""); + for (int j = 0; j < m.arguments.size(); j++) { + + const ArgumentDoc &a = m.arguments[j]; + _write_string(f, 3, ""); + _write_string(f, 3, ""); + } + + _write_string(f, 3, ""); + _write_string(f, 4, m.description.strip_edges().xml_escape()); + _write_string(f, 3, ""); + + _write_string(f, 2, ""); + } + + _write_string(f, 1, ""); + } + + _write_string(f, 1, ""); + + for (int i = 0; i < c.constants.size(); i++) { + + const ConstantDoc &k = c.constants[i]; + if (k.enumeration != String()) { + _write_string(f, 2, ""); + } else { + _write_string(f, 2, ""); + } + _write_string(f, 3, k.description.strip_edges().xml_escape()); + _write_string(f, 2, ""); + } + + _write_string(f, 1, ""); + + if (c.theme_properties.size()) { + + c.theme_properties.sort(); + + _write_string(f, 1, ""); + for (int i = 0; i < c.theme_properties.size(); i++) { + + const PropertyDoc &p = c.theme_properties[i]; + + if (p.default_value != "") + _write_string(f, 2, ""); + else + _write_string(f, 2, ""); + + _write_string(f, 3, p.description.strip_edges().xml_escape()); + + _write_string(f, 2, ""); + } + _write_string(f, 1, ""); + } + + _write_string(f, 0, ""); + } + + return OK; +} + +Error DocData::load_compressed(const uint8_t *p_data, int p_compressed_size, int p_uncompressed_size) { + + Vector data; + data.resize(p_uncompressed_size); + Compression::decompress(data.ptrw(), p_uncompressed_size, p_data, p_compressed_size, Compression::MODE_DEFLATE); + class_list.clear(); + + Ref parser = memnew(XMLParser); + Error err = parser->open_buffer(data); + if (err) + return err; + + _load(parser); + + return OK; +} diff --git a/editor/doc_data.h b/editor/doc_data.h new file mode 100644 index 0000000000..073705f0b1 --- /dev/null +++ b/editor/doc_data.h @@ -0,0 +1,117 @@ +/*************************************************************************/ +/* doc_data.h */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */ +/* */ +/* 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. */ +/*************************************************************************/ + +#ifndef DOC_DATA_H +#define DOC_DATA_H + +#include "core/io/xml_parser.h" +#include "core/map.h" +#include "core/variant.h" + +class DocData { +public: + struct ArgumentDoc { + + String name; + String type; + String enumeration; + String default_value; + }; + + struct MethodDoc { + + String name; + String return_type; + String return_enum; + String qualifiers; + String description; + Vector arguments; + bool operator<(const MethodDoc &p_md) const { + return name < p_md.name; + } + }; + + struct ConstantDoc { + + String name; + String value; + String enumeration; + String description; + }; + + struct PropertyDoc { + + String name; + String type; + String enumeration; + String description; + String setter, getter; + String default_value; + bool overridden; + bool operator<(const PropertyDoc &p_prop) const { + return name < p_prop.name; + } + PropertyDoc() { + overridden = false; + } + }; + + struct ClassDoc { + + String name; + String inherits; + String category; + String brief_description; + String description; + Vector tutorials; + Vector methods; + Vector signals; + Vector constants; + Vector properties; + Vector theme_properties; + }; + + String version; + + Map class_list; + Error _load(Ref parser); + +public: + void merge_from(const DocData &p_data); + void remove_from(const DocData &p_data); + void generate(bool p_basic_types = false); + Error load_classes(const String &p_dir); + static Error erase_classes(const String &p_dir); + Error save_classes(const String &p_default_path, const Map &p_class_path); + + Error load_compressed(const uint8_t *p_data, int p_compressed_size, int p_uncompressed_size); +}; + +#endif // DOC_DATA_H diff --git a/editor/editor_help.h b/editor/editor_help.h index a690e10e7e..0d3ecf9bd0 100644 --- a/editor/editor_help.h +++ b/editor/editor_help.h @@ -32,7 +32,7 @@ #define EDITOR_HELP_H #include "editor/code_editor.h" -#include "editor/doc/doc_data.h" +#include "editor/doc_data.h" #include "editor/editor_plugin.h" #include "scene/gui/margin_container.h" #include "scene/gui/menu_button.h" diff --git a/editor/import/collada.cpp b/editor/import/collada.cpp new file mode 100644 index 0000000000..8ef9d17083 --- /dev/null +++ b/editor/import/collada.cpp @@ -0,0 +1,2582 @@ +/*************************************************************************/ +/* collada.cpp */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */ +/* */ +/* 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 "collada.h" + +#include + +//#define DEBUG_DEFAULT_ANIMATION +//#define DEBUG_COLLADA +#ifdef DEBUG_COLLADA +#define COLLADA_PRINT(m_what) print_line(m_what) +#else +#define COLLADA_PRINT(m_what) +#endif + +#define COLLADA_IMPORT_SCALE_SCENE + +/* HELPERS */ + +String Collada::Effect::get_texture_path(const String &p_source, Collada &state) const { + + const String &image = p_source; + ERR_FAIL_COND_V(!state.state.image_map.has(image), ""); + return state.state.image_map[image].path; +} + +Transform Collada::get_root_transform() const { + + Transform unit_scale_transform; +#ifndef COLLADA_IMPORT_SCALE_SCENE + unit_scale_transform.scale(Vector3(state.unit_scale, state.unit_scale, state.unit_scale)); +#endif + return unit_scale_transform; +} + +void Collada::Vertex::fix_unit_scale(Collada &state) { +#ifdef COLLADA_IMPORT_SCALE_SCENE + vertex *= state.state.unit_scale; +#endif +} + +static String _uri_to_id(const String &p_uri) { + + if (p_uri.begins_with("#")) + return p_uri.substr(1, p_uri.size() - 1); + else + return p_uri; +} + +/** HELPER FUNCTIONS **/ + +Transform Collada::fix_transform(const Transform &p_transform) { + + Transform tr = p_transform; + +#ifndef NO_UP_AXIS_SWAP + + if (state.up_axis != Vector3::AXIS_Y) { + + for (int i = 0; i < 3; i++) + SWAP(tr.basis[1][i], tr.basis[state.up_axis][i]); + for (int i = 0; i < 3; i++) + SWAP(tr.basis[i][1], tr.basis[i][state.up_axis]); + + SWAP(tr.origin[1], tr.origin[state.up_axis]); + + tr.basis[state.up_axis][0] = -tr.basis[state.up_axis][0]; + tr.basis[state.up_axis][1] = -tr.basis[state.up_axis][1]; + tr.basis[0][state.up_axis] = -tr.basis[0][state.up_axis]; + tr.basis[1][state.up_axis] = -tr.basis[1][state.up_axis]; + tr.origin[state.up_axis] = -tr.origin[state.up_axis]; + } +#endif + + //tr.scale(Vector3(state.unit_scale.unit_scale.unit_scale)); + return tr; + //return state.matrix_fix * p_transform; +} + +static Transform _read_transform_from_array(const Vector &array, int ofs = 0) { + + Transform tr; + // i wonder why collada matrices are transposed, given that's opposed to opengl.. + tr.basis.elements[0][0] = array[0 + ofs]; + tr.basis.elements[0][1] = array[1 + ofs]; + tr.basis.elements[0][2] = array[2 + ofs]; + tr.basis.elements[1][0] = array[4 + ofs]; + tr.basis.elements[1][1] = array[5 + ofs]; + tr.basis.elements[1][2] = array[6 + ofs]; + tr.basis.elements[2][0] = array[8 + ofs]; + tr.basis.elements[2][1] = array[9 + ofs]; + tr.basis.elements[2][2] = array[10 + ofs]; + tr.origin.x = array[3 + ofs]; + tr.origin.y = array[7 + ofs]; + tr.origin.z = array[11 + ofs]; + return tr; +} + +/* STRUCTURES */ + +Transform Collada::Node::compute_transform(Collada &state) const { + + Transform xform; + + for (int i = 0; i < xform_list.size(); i++) { + + Transform xform_step; + const XForm &xf = xform_list[i]; + switch (xf.op) { + + case XForm::OP_ROTATE: { + if (xf.data.size() >= 4) { + + xform_step.rotate(Vector3(xf.data[0], xf.data[1], xf.data[2]), Math::deg2rad(xf.data[3])); + } + } break; + case XForm::OP_SCALE: { + + if (xf.data.size() >= 3) { + + xform_step.scale(Vector3(xf.data[0], xf.data[1], xf.data[2])); + } + + } break; + case XForm::OP_TRANSLATE: { + + if (xf.data.size() >= 3) { + + xform_step.origin = Vector3(xf.data[0], xf.data[1], xf.data[2]); + } + + } break; + case XForm::OP_MATRIX: { + + if (xf.data.size() >= 16) { + xform_step = _read_transform_from_array(xf.data, 0); + } + + } break; + default: { + } + } + + xform = xform * xform_step; + } + +#ifdef COLLADA_IMPORT_SCALE_SCENE + xform.origin *= state.state.unit_scale; +#endif + return xform; +} + +Transform Collada::Node::get_transform() const { + + return default_transform; +} + +Transform Collada::Node::get_global_transform() const { + + if (parent) + return parent->get_global_transform() * default_transform; + else + return default_transform; +} + +Vector Collada::AnimationTrack::get_value_at_time(float p_time) const { + + ERR_FAIL_COND_V(keys.size() == 0, Vector()); + int i = 0; + + for (i = 0; i < keys.size(); i++) { + + if (keys[i].time > p_time) + break; + } + + if (i == 0) + return keys[0].data; + if (i == keys.size()) + return keys[keys.size() - 1].data; + + switch (keys[i].interp_type) { + + case INTERP_BEZIER: //wait for bezier + case INTERP_LINEAR: { + + float c = (p_time - keys[i - 1].time) / (keys[i].time - keys[i - 1].time); + + if (keys[i].data.size() == 16) { + //interpolate a matrix + Transform src = _read_transform_from_array(keys[i - 1].data); + Transform dst = _read_transform_from_array(keys[i].data); + + Transform interp = c < 0.001 ? src : src.interpolate_with(dst, c); + + Vector ret; + ret.resize(16); + Transform tr; + // i wonder why collada matrices are transposed, given that's opposed to opengl.. + ret.write[0] = interp.basis.elements[0][0]; + ret.write[1] = interp.basis.elements[0][1]; + ret.write[2] = interp.basis.elements[0][2]; + ret.write[4] = interp.basis.elements[1][0]; + ret.write[5] = interp.basis.elements[1][1]; + ret.write[6] = interp.basis.elements[1][2]; + ret.write[8] = interp.basis.elements[2][0]; + ret.write[9] = interp.basis.elements[2][1]; + ret.write[10] = interp.basis.elements[2][2]; + ret.write[3] = interp.origin.x; + ret.write[7] = interp.origin.y; + ret.write[11] = interp.origin.z; + ret.write[12] = 0; + ret.write[13] = 0; + ret.write[14] = 0; + ret.write[15] = 1; + + return ret; + } else { + + Vector dest; + dest.resize(keys[i].data.size()); + for (int j = 0; j < dest.size(); j++) { + + dest.write[j] = keys[i].data[j] * c + keys[i - 1].data[j] * (1.0 - c); + } + return dest; + //interpolate one by one + } + } break; + } + + ERR_FAIL_V(Vector()); +} + +void Collada::_parse_asset(XMLParser &parser) { + + while (parser.read() == OK) { + + if (parser.get_node_type() == XMLParser::NODE_ELEMENT) { + + String name = parser.get_node_name(); + + if (name == "up_axis") { + + parser.read(); + if (parser.get_node_data() == "X_UP") + state.up_axis = Vector3::AXIS_X; + if (parser.get_node_data() == "Y_UP") + state.up_axis = Vector3::AXIS_Y; + if (parser.get_node_data() == "Z_UP") + state.up_axis = Vector3::AXIS_Z; + + COLLADA_PRINT("up axis: " + parser.get_node_data()); + } else if (name == "unit") { + + state.unit_scale = parser.get_attribute_value("meter").to_double(); + COLLADA_PRINT("unit scale: " + rtos(state.unit_scale)); + } + + } else if (parser.get_node_type() == XMLParser::NODE_ELEMENT_END && parser.get_node_name() == "asset") + break; //end of + } +} + +void Collada::_parse_image(XMLParser &parser) { + + String id = parser.get_attribute_value("id"); + + if (!(state.import_flags & IMPORT_FLAG_SCENE)) { + if (!parser.is_empty()) + parser.skip_section(); + return; + } + + Image image; + + if (state.version < State::Version(1, 4, 0)) { + /* <1.4 */ + String path = parser.get_attribute_value("source").strip_edges(); + if (path.find("://") == -1 && path.is_rel_path()) { + // path is relative to file being loaded, so convert to a resource path + image.path = ProjectSettings::get_singleton()->localize_path(state.local_path.get_base_dir().plus_file(path.percent_decode())); + } + } else { + + while (parser.read() == OK) { + + if (parser.get_node_type() == XMLParser::NODE_ELEMENT) { + + String name = parser.get_node_name(); + + if (name == "init_from") { + + parser.read(); + String path = parser.get_node_data().strip_edges().percent_decode(); + + if (path.find("://") == -1 && path.is_rel_path()) { + // path is relative to file being loaded, so convert to a resource path + path = ProjectSettings::get_singleton()->localize_path(state.local_path.get_base_dir().plus_file(path)); + + } else if (path.find("file:///") == 0) { + path = path.replace_first("file:///", ""); + path = ProjectSettings::get_singleton()->localize_path(path); + } + + image.path = path; + + } else if (name == "data") { + + ERR_PRINT("COLLADA Embedded image data not supported!"); + + } else if (name == "extra" && !parser.is_empty()) + parser.skip_section(); + + } else if (parser.get_node_type() == XMLParser::NODE_ELEMENT_END && parser.get_node_name() == "image") + break; //end of + } + } + + state.image_map[id] = image; +} + +void Collada::_parse_material(XMLParser &parser) { + + if (!(state.import_flags & IMPORT_FLAG_SCENE)) { + if (!parser.is_empty()) + parser.skip_section(); + return; + } + + Material material; + + String id = parser.get_attribute_value("id"); + if (parser.has_attribute("name")) + material.name = parser.get_attribute_value("name"); + + if (state.version < State::Version(1, 4, 0)) { + /* <1.4 */ + ERR_PRINT("Collada Materials < 1.4 are not supported (yet)"); + } else { + + while (parser.read() == OK) { + + if (parser.get_node_type() == XMLParser::NODE_ELEMENT && parser.get_node_name() == "instance_effect") { + + material.instance_effect = _uri_to_id(parser.get_attribute_value("url")); + } else if (parser.get_node_type() == XMLParser::NODE_ELEMENT_END && parser.get_node_name() == "material") + break; //end of + } + } + + state.material_map[id] = material; +} + +//! reads floats from inside of xml element until end of xml element +Vector Collada::_read_float_array(XMLParser &parser) { + + if (parser.is_empty()) + return Vector(); + + Vector splitters; + splitters.push_back(" "); + splitters.push_back("\n"); + splitters.push_back("\r"); + splitters.push_back("\t"); + + Vector array; + while (parser.read() == OK) { + // TODO: check for comments inside the element + // and ignore them. + + if (parser.get_node_type() == XMLParser::NODE_TEXT) { + // parse float data + String str = parser.get_node_data(); + array = str.split_floats_mk(splitters, false); + //array=str.split_floats(" ",false); + } else if (parser.get_node_type() == XMLParser::NODE_ELEMENT_END) + break; // end parsing text + } + + return array; +} + +Vector Collada::_read_string_array(XMLParser &parser) { + + if (parser.is_empty()) + return Vector(); + + Vector array; + while (parser.read() == OK) { + // TODO: check for comments inside the element + // and ignore them. + + if (parser.get_node_type() == XMLParser::NODE_TEXT) { + // parse String data + String str = parser.get_node_data(); + array = str.split_spaces(); + } else if (parser.get_node_type() == XMLParser::NODE_ELEMENT_END) + break; // end parsing text + } + + return array; +} + +Transform Collada::_read_transform(XMLParser &parser) { + + if (parser.is_empty()) + return Transform(); + + Vector array; + while (parser.read() == OK) { + // TODO: check for comments inside the element + // and ignore them. + + if (parser.get_node_type() == XMLParser::NODE_TEXT) { + // parse float data + String str = parser.get_node_data(); + array = str.split_spaces(); + } else if (parser.get_node_type() == XMLParser::NODE_ELEMENT_END) + break; // end parsing text + } + + ERR_FAIL_COND_V(array.size() != 16, Transform()); + Vector farr; + farr.resize(16); + for (int i = 0; i < 16; i++) { + farr.write[i] = array[i].to_double(); + } + + return _read_transform_from_array(farr); +} + +String Collada::_read_empty_draw_type(XMLParser &parser) { + + String empty_draw_type = ""; + + if (parser.is_empty()) + return empty_draw_type; + + while (parser.read() == OK) { + if (parser.get_node_type() == XMLParser::NODE_TEXT) { + empty_draw_type = parser.get_node_data(); + } else if (parser.get_node_type() == XMLParser::NODE_ELEMENT_END) + break; // end parsing text + } + return empty_draw_type; +} + +Variant Collada::_parse_param(XMLParser &parser) { + + if (parser.is_empty()) + return Variant(); + + String from = parser.get_node_name(); + Variant data; + + while (parser.read() == OK) { + if (parser.get_node_type() == XMLParser::NODE_ELEMENT) { + + if (parser.get_node_name() == "float") { + + parser.read(); + if (parser.get_node_type() == XMLParser::NODE_TEXT) { + + data = parser.get_node_data().to_double(); + } + } else if (parser.get_node_name() == "float2") { + + Vector v2 = _read_float_array(parser); + + if (v2.size() >= 2) { + + data = Vector2(v2[0], v2[1]); + } + } else if (parser.get_node_name() == "float3") { + + Vector v3 = _read_float_array(parser); + + if (v3.size() >= 3) { + + data = Vector3(v3[0], v3[1], v3[2]); + } + } else if (parser.get_node_name() == "float4") { + + Vector v4 = _read_float_array(parser); + + if (v4.size() >= 4) { + + data = Color(v4[0], v4[1], v4[2], v4[3]); + } + } else if (parser.get_node_name() == "sampler2D") { + + while (parser.read() == OK) { + + if (parser.get_node_type() == XMLParser::NODE_ELEMENT) { + + if (parser.get_node_name() == "source") { + + parser.read(); + + if (parser.get_node_type() == XMLParser::NODE_TEXT) { + + data = parser.get_node_data(); + } + } + } else if (parser.get_node_type() == XMLParser::NODE_ELEMENT_END && parser.get_node_name() == "sampler2D") + break; + } + } else if (parser.get_node_name() == "surface") { + + while (parser.read() == OK) { + + if (parser.get_node_type() == XMLParser::NODE_ELEMENT) { + + if (parser.get_node_name() == "init_from") { + + parser.read(); + + if (parser.get_node_type() == XMLParser::NODE_TEXT) { + + data = parser.get_node_data(); + } + } + } else if (parser.get_node_type() == XMLParser::NODE_ELEMENT_END && parser.get_node_name() == "surface") + break; + } + } + + } else if (parser.get_node_type() == XMLParser::NODE_ELEMENT_END && parser.get_node_name() == from) + break; + } + + COLLADA_PRINT("newparam ending " + parser.get_node_name()); + return data; +} + +void Collada::_parse_effect_material(XMLParser &parser, Effect &effect, String &id) { + + if (!(state.import_flags & IMPORT_FLAG_SCENE)) { + if (!parser.is_empty()) + parser.skip_section(); + return; + } + + while (parser.read() == OK) { + + if (parser.get_node_type() == XMLParser::NODE_ELEMENT) { + + // first come the tags we descend, but ignore the top-levels + + COLLADA_PRINT("node name: " + parser.get_node_name()); + + if (!parser.is_empty() && (parser.get_node_name() == "profile_COMMON" || parser.get_node_name() == "technique" || parser.get_node_name() == "extra")) { + + _parse_effect_material(parser, effect, id); // try again + + } else if (parser.get_node_name() == "newparam") { + String name = parser.get_attribute_value("sid"); + Variant value = _parse_param(parser); + effect.params[name] = value; + COLLADA_PRINT("param: " + name + " value:" + String(value)); + + } else if (parser.get_node_name() == "constant" || + parser.get_node_name() == "lambert" || + parser.get_node_name() == "phong" || + parser.get_node_name() == "blinn") { + + COLLADA_PRINT("shade model: " + parser.get_node_name()); + while (parser.read() == OK) { + + if (parser.get_node_type() == XMLParser::NODE_ELEMENT) { + + String what = parser.get_node_name(); + + if (what == "emission" || + what == "diffuse" || + what == "specular" || + what == "reflective") { + + // color or texture types + while (parser.read() == OK) { + + if (parser.get_node_type() == XMLParser::NODE_ELEMENT) { + + if (parser.get_node_name() == "color") { + + Vector colorarr = _read_float_array(parser); + COLLADA_PRINT("colorarr size: " + rtos(colorarr.size())); + + if (colorarr.size() >= 3) { + + // alpha strangely not alright? maybe it needs to be multiplied by value as a channel intensity + Color color(colorarr[0], colorarr[1], colorarr[2], 1.0); + if (what == "diffuse") + effect.diffuse.color = color; + if (what == "specular") + effect.specular.color = color; + if (what == "emission") + effect.emission.color = color; + + COLLADA_PRINT(what + " color: " + color); + } + + } else if (parser.get_node_name() == "texture") { + + String sampler = parser.get_attribute_value("texture"); + if (!effect.params.has(sampler)) { + ERR_PRINT(String("Couldn't find sampler: " + sampler + " in material:" + id).utf8().get_data()); + } else { + String surface = effect.params[sampler]; + + if (!effect.params.has(surface)) { + ERR_PRINT(String("Couldn't find surface: " + surface + " in material:" + id).utf8().get_data()); + } else { + String uri = effect.params[surface]; + + if (what == "diffuse") { + effect.diffuse.texture = uri; + } else if (what == "specular") { + effect.specular.texture = uri; + } else if (what == "emission") { + effect.emission.texture = uri; + } else if (what == "bump") { + if (parser.has_attribute("bumptype") && parser.get_attribute_value("bumptype") != "NORMALMAP") { + WARN_PRINT("'bump' texture type is not NORMALMAP, only NORMALMAP is supported."); + } + + effect.bump.texture = uri; + } + + COLLADA_PRINT(what + " texture: " + uri); + } + } + } else if (!parser.is_empty()) + parser.skip_section(); + + } else if (parser.get_node_type() == XMLParser::NODE_ELEMENT_END && parser.get_node_name() == what) + break; + } + + } else if (what == "shininess") { + effect.shininess = _parse_param(parser); + } + } else if (parser.get_node_type() == XMLParser::NODE_ELEMENT_END && (parser.get_node_name() == "constant" || + parser.get_node_name() == "lambert" || + parser.get_node_name() == "phong" || + parser.get_node_name() == "blinn")) + break; + } + } else if (parser.get_node_name() == "double_sided" || parser.get_node_name() == "show_double_sided") { // colladamax / google earth + + // 3DS Max / Google Earth double sided extension + parser.read(); + effect.found_double_sided = true; + effect.double_sided = parser.get_node_data().to_int(); + COLLADA_PRINT("double sided: " + itos(parser.get_node_data().to_int())); + } else if (parser.get_node_name() == "unshaded") { + parser.read(); + effect.unshaded = parser.get_node_data().to_int(); + } else if (parser.get_node_name() == "bump") { + + // color or texture types + while (parser.read() == OK) { + + if (parser.get_node_type() == XMLParser::NODE_ELEMENT) { + + if (parser.get_node_name() == "texture") { + + String sampler = parser.get_attribute_value("texture"); + if (!effect.params.has(sampler)) { + ERR_PRINT(String("Couldn't find sampler: " + sampler + " in material:" + id).utf8().get_data()); + } else { + String surface = effect.params[sampler]; + + if (!effect.params.has(surface)) { + ERR_PRINT(String("Couldn't find surface: " + surface + " in material:" + id).utf8().get_data()); + } else { + String uri = effect.params[surface]; + + if (parser.has_attribute("bumptype") && parser.get_attribute_value("bumptype") != "NORMALMAP") { + WARN_PRINT("'bump' texture type is not NORMALMAP, only NORMALMAP is supported."); + } + + effect.bump.texture = uri; + COLLADA_PRINT(" bump: " + uri); + } + } + } else if (!parser.is_empty()) + parser.skip_section(); + + } else if (parser.get_node_type() == XMLParser::NODE_ELEMENT_END && parser.get_node_name() == "bump") + break; + } + + } else if (!parser.is_empty()) + parser.skip_section(); + } else if (parser.get_node_type() == XMLParser::NODE_ELEMENT_END && + (parser.get_node_name() == "effect" || + parser.get_node_name() == "profile_COMMON" || + parser.get_node_name() == "technique" || + parser.get_node_name() == "extra")) + break; + } +} + +void Collada::_parse_effect(XMLParser &parser) { + + if (!(state.import_flags & IMPORT_FLAG_SCENE)) { + if (!parser.is_empty()) + parser.skip_section(); + return; + } + + String id = parser.get_attribute_value("id"); + + Effect effect; + if (parser.has_attribute("name")) + effect.name = parser.get_attribute_value("name"); + _parse_effect_material(parser, effect, id); + + state.effect_map[id] = effect; + + COLLADA_PRINT("Effect ID:" + id); +} + +void Collada::_parse_camera(XMLParser &parser) { + + if (!(state.import_flags & IMPORT_FLAG_SCENE)) { + if (!parser.is_empty()) + parser.skip_section(); + return; + } + + String id = parser.get_attribute_value("id"); + + state.camera_data_map[id] = CameraData(); + CameraData &camera = state.camera_data_map[id]; + + while (parser.read() == OK) { + + if (parser.get_node_type() == XMLParser::NODE_ELEMENT) { + + String name = parser.get_node_name(); + + if (name == "perspective") { + + camera.mode = CameraData::MODE_PERSPECTIVE; + } else if (name == "orthographic") { + + camera.mode = CameraData::MODE_ORTHOGONAL; + } else if (name == "xfov") { + + parser.read(); + camera.perspective.x_fov = parser.get_node_data().to_double(); + + } else if (name == "yfov") { + + parser.read(); + camera.perspective.y_fov = parser.get_node_data().to_double(); + } else if (name == "xmag") { + + parser.read(); + camera.orthogonal.x_mag = parser.get_node_data().to_double(); + + } else if (name == "ymag") { + + parser.read(); + camera.orthogonal.y_mag = parser.get_node_data().to_double(); + } else if (name == "aspect_ratio") { + + parser.read(); + camera.aspect = parser.get_node_data().to_double(); + + } else if (name == "znear") { + + parser.read(); + camera.z_near = parser.get_node_data().to_double(); + + } else if (name == "zfar") { + + parser.read(); + camera.z_far = parser.get_node_data().to_double(); + } + + } else if (parser.get_node_type() == XMLParser::NODE_ELEMENT_END && parser.get_node_name() == "camera") + break; //end of + } + + COLLADA_PRINT("Camera ID:" + id); +} + +void Collada::_parse_light(XMLParser &parser) { + + if (!(state.import_flags & IMPORT_FLAG_SCENE)) { + if (!parser.is_empty()) + parser.skip_section(); + return; + } + + String id = parser.get_attribute_value("id"); + + state.light_data_map[id] = LightData(); + LightData &light = state.light_data_map[id]; + + while (parser.read() == OK) { + + if (parser.get_node_type() == XMLParser::NODE_ELEMENT) { + + String name = parser.get_node_name(); + + if (name == "ambient") { + + light.mode = LightData::MODE_AMBIENT; + } else if (name == "directional") { + + light.mode = LightData::MODE_DIRECTIONAL; + } else if (name == "point") { + + light.mode = LightData::MODE_OMNI; + } else if (name == "spot") { + + light.mode = LightData::MODE_SPOT; + } else if (name == "color") { + + parser.read(); + Vector colorarr = _read_float_array(parser); + COLLADA_PRINT("colorarr size: " + rtos(colorarr.size())); + + if (colorarr.size() >= 4) { + // alpha strangely not alright? maybe it needs to be multiplied by value as a channel intensity + Color color(colorarr[0], colorarr[1], colorarr[2], 1.0); + light.color = color; + } + + } else if (name == "constant_attenuation") { + + parser.read(); + light.constant_att = parser.get_node_data().to_double(); + } else if (name == "linear_attenuation") { + + parser.read(); + light.linear_att = parser.get_node_data().to_double(); + } else if (name == "quadratic_attenuation") { + + parser.read(); + light.quad_att = parser.get_node_data().to_double(); + } else if (name == "falloff_angle") { + + parser.read(); + light.spot_angle = parser.get_node_data().to_double(); + + } else if (name == "falloff_exponent") { + + parser.read(); + light.spot_exp = parser.get_node_data().to_double(); + } + + } else if (parser.get_node_type() == XMLParser::NODE_ELEMENT_END && parser.get_node_name() == "light") + break; //end of + } + + COLLADA_PRINT("Light ID:" + id); +} + +void Collada::_parse_curve_geometry(XMLParser &parser, String p_id, String p_name) { + + if (!(state.import_flags & IMPORT_FLAG_SCENE)) { + if (!parser.is_empty()) + parser.skip_section(); + return; + } + + //load everything into a pre dictionary + + state.curve_data_map[p_id] = CurveData(); + + CurveData &curvedata = state.curve_data_map[p_id]; + curvedata.name = p_name; + + COLLADA_PRINT("curve name: " + p_name); + + String current_source; + // handles geometry node and the curve children in this loop + // read sources with arrays and accessor for each curve + if (parser.is_empty()) { + return; + } + + while (parser.read() == OK) { + + if (parser.get_node_type() == XMLParser::NODE_ELEMENT) { + + String section = parser.get_node_name(); + + if (section == "source") { + + String id = parser.get_attribute_value("id"); + curvedata.sources[id] = CurveData::Source(); + current_source = id; + COLLADA_PRINT("source data: " + id); + + } else if (section == "float_array" || section == "array") { + // create a new array and read it. + if (curvedata.sources.has(current_source)) { + + curvedata.sources[current_source].array = _read_float_array(parser); + COLLADA_PRINT("section: " + current_source + " read " + itos(curvedata.sources[current_source].array.size()) + " values."); + } + } else if (section == "Name_array") { + // create a new array and read it. + if (curvedata.sources.has(current_source)) { + + curvedata.sources[current_source].sarray = _read_string_array(parser); + COLLADA_PRINT("section: " + current_source + " read " + itos(curvedata.sources[current_source].array.size()) + " values."); + } + + } else if (section == "technique_common") { + //skip it + } else if (section == "accessor") { // child of source (below a technique tag) + + if (curvedata.sources.has(current_source)) { + curvedata.sources[current_source].stride = parser.get_attribute_value("stride").to_int(); + COLLADA_PRINT("section: " + current_source + " stride " + itos(curvedata.sources[current_source].stride)); + } + } else if (section == "control_vertices") { + + while (parser.read() == OK) { + + if (parser.get_node_type() == XMLParser::NODE_ELEMENT) { + + if (parser.get_node_name() == "input") { + + String semantic = parser.get_attribute_value("semantic"); + String source = _uri_to_id(parser.get_attribute_value("source")); + + curvedata.control_vertices[semantic] = source; + + COLLADA_PRINT(section + " input semantic: " + semantic + " source: " + source); + } + } else if (parser.get_node_type() == XMLParser::NODE_ELEMENT_END && parser.get_node_name() == section) + break; + } + + } else if (!parser.is_empty()) { + + parser.skip_section(); + } + } else if (parser.get_node_type() == XMLParser::NODE_ELEMENT_END && parser.get_node_name() == "spline") + break; + } +} + +void Collada::_parse_mesh_geometry(XMLParser &parser, String p_id, String p_name) { + + if (!(state.import_flags & IMPORT_FLAG_SCENE)) { + if (!parser.is_empty()) + parser.skip_section(); + return; + } + + //load everything into a pre dictionary + + state.mesh_data_map[p_id] = MeshData(); + + MeshData &meshdata = state.mesh_data_map[p_id]; + meshdata.name = p_name; + + COLLADA_PRINT("mesh name: " + p_name); + + String current_source; + // handles geometry node and the mesh children in this loop + // read sources with arrays and accessor for each mesh + if (parser.is_empty()) { + return; + } + + while (parser.read() == OK) { + + if (parser.get_node_type() == XMLParser::NODE_ELEMENT) { + + String section = parser.get_node_name(); + + if (section == "source") { + + String id = parser.get_attribute_value("id"); + meshdata.sources[id] = MeshData::Source(); + current_source = id; + COLLADA_PRINT("source data: " + id); + + } else if (section == "float_array" || section == "array") { + // create a new array and read it. + if (meshdata.sources.has(current_source)) { + + meshdata.sources[current_source].array = _read_float_array(parser); + COLLADA_PRINT("section: " + current_source + " read " + itos(meshdata.sources[current_source].array.size()) + " values."); + } + } else if (section == "technique_common") { + //skip it + } else if (section == "accessor") { // child of source (below a technique tag) + + if (meshdata.sources.has(current_source)) { + meshdata.sources[current_source].stride = parser.get_attribute_value("stride").to_int(); + COLLADA_PRINT("section: " + current_source + " stride " + itos(meshdata.sources[current_source].stride)); + } + } else if (section == "vertices") { + + MeshData::Vertices vert; + String id = parser.get_attribute_value("id"); + + while (parser.read() == OK) { + + if (parser.get_node_type() == XMLParser::NODE_ELEMENT) { + + if (parser.get_node_name() == "input") { + + String semantic = parser.get_attribute_value("semantic"); + String source = _uri_to_id(parser.get_attribute_value("source")); + + vert.sources[semantic] = source; + + COLLADA_PRINT(section + " input semantic: " + semantic + " source: " + source); + } + } else if (parser.get_node_type() == XMLParser::NODE_ELEMENT_END && parser.get_node_name() == section) + break; + } + + meshdata.vertices[id] = vert; + + } else if (section == "triangles" || section == "polylist" || section == "polygons") { + + bool polygons = (section == "polygons"); + if (polygons) { + WARN_PRINT("Primitive type \"polygons\" is not well supported (concave shapes may fail). To ensure that the geometry is properly imported, please re-export using \"triangles\" or \"polylist\"."); + } + MeshData::Primitives prim; + + if (parser.has_attribute("material")) + prim.material = parser.get_attribute_value("material"); + prim.count = parser.get_attribute_value("count").to_int(); + prim.vertex_size = 0; + int last_ref = 0; + + while (parser.read() == OK) { + + if (parser.get_node_type() == XMLParser::NODE_ELEMENT) { + + if (parser.get_node_name() == "input") { + + String semantic = parser.get_attribute_value("semantic"); + String source = _uri_to_id(parser.get_attribute_value("source")); + + if (semantic == "TEXCOORD") { + /* + if (parser.has_attribute("set"))// a texcoord + semantic+=parser.get_attribute_value("set"); + else + semantic="TEXCOORD0";*/ + semantic = "TEXCOORD" + itos(last_ref++); + } + int offset = parser.get_attribute_value("offset").to_int(); + + MeshData::Primitives::SourceRef sref; + sref.source = source; + sref.offset = offset; + prim.sources[semantic] = sref; + prim.vertex_size = MAX(prim.vertex_size, offset + 1); + + COLLADA_PRINT(section + " input semantic: " + semantic + " source: " + source + " offset: " + itos(offset)); + + } else if (parser.get_node_name() == "p") { //indices + + Vector values = _read_float_array(parser); + if (polygons) { + + ERR_CONTINUE(prim.vertex_size == 0); + prim.polygons.push_back(values.size() / prim.vertex_size); + int from = prim.indices.size(); + prim.indices.resize(from + values.size()); + for (int i = 0; i < values.size(); i++) + prim.indices.write[from + i] = values[i]; + + } else if (prim.vertex_size > 0) { + prim.indices = values; + } + + COLLADA_PRINT("read " + itos(values.size()) + " index values"); + + } else if (parser.get_node_name() == "vcount") { // primitive + + Vector values = _read_float_array(parser); + prim.polygons = values; + COLLADA_PRINT("read " + itos(values.size()) + " polygon values"); + } + } else if (parser.get_node_type() == XMLParser::NODE_ELEMENT_END && parser.get_node_name() == section) + break; + } + + meshdata.primitives.push_back(prim); + + } else if (parser.get_node_name() == "double_sided") { + + parser.read(); + meshdata.found_double_sided = true; + meshdata.double_sided = parser.get_node_data().to_int(); + + } else if (parser.get_node_name() == "polygons") { + ERR_PRINT("Primitive type \"polygons\" not supported, re-export using \"polylist\" or \"triangles\"."); + } else if (!parser.is_empty()) { + + parser.skip_section(); + } + } else if (parser.get_node_type() == XMLParser::NODE_ELEMENT_END && parser.get_node_name() == "mesh") + break; + } +} + +void Collada::_parse_skin_controller(XMLParser &parser, String p_id) { + + state.skin_controller_data_map[p_id] = SkinControllerData(); + SkinControllerData &skindata = state.skin_controller_data_map[p_id]; + + skindata.base = _uri_to_id(parser.get_attribute_value("source")); + + String current_source; + + while (parser.read() == OK) { + + if (parser.get_node_type() == XMLParser::NODE_ELEMENT) { + + String section = parser.get_node_name(); + + if (section == "bind_shape_matrix") { + + skindata.bind_shape = _read_transform(parser); +#ifdef COLLADA_IMPORT_SCALE_SCENE + skindata.bind_shape.origin *= state.unit_scale; + +#endif + COLLADA_PRINT("skeleton bind shape transform: " + skindata.bind_shape); + + } else if (section == "source") { + + String id = parser.get_attribute_value("id"); + skindata.sources[id] = SkinControllerData::Source(); + current_source = id; + COLLADA_PRINT("source data: " + id); + + } else if (section == "float_array" || section == "array") { + // create a new array and read it. + if (skindata.sources.has(current_source)) { + + skindata.sources[current_source].array = _read_float_array(parser); + COLLADA_PRINT("section: " + current_source + " read " + itos(skindata.sources[current_source].array.size()) + " values."); + } + } else if (section == "Name_array" || section == "IDREF_array") { + // create a new array and read it. + + if (section == "IDREF_array") + skindata.use_idrefs = true; + if (skindata.sources.has(current_source)) { + + skindata.sources[current_source].sarray = _read_string_array(parser); + if (section == "IDREF_array") { + Vector sa = skindata.sources[current_source].sarray; + for (int i = 0; i < sa.size(); i++) + state.idref_joints.insert(sa[i]); + } + COLLADA_PRINT("section: " + current_source + " read " + itos(skindata.sources[current_source].array.size()) + " values."); + } + } else if (section == "technique_common") { + //skip it + } else if (section == "accessor") { // child of source (below a technique tag) + + if (skindata.sources.has(current_source)) { + + int stride = 1; + if (parser.has_attribute("stride")) + stride = parser.get_attribute_value("stride").to_int(); + + skindata.sources[current_source].stride = stride; + COLLADA_PRINT("section: " + current_source + " stride " + itos(skindata.sources[current_source].stride)); + } + + } else if (section == "joints") { + + SkinControllerData::Joints joint; + + while (parser.read() == OK) { + + if (parser.get_node_type() == XMLParser::NODE_ELEMENT) { + + if (parser.get_node_name() == "input") { + + String semantic = parser.get_attribute_value("semantic"); + String source = _uri_to_id(parser.get_attribute_value("source")); + + joint.sources[semantic] = source; + + COLLADA_PRINT(section + " input semantic: " + semantic + " source: " + source); + } + } else if (parser.get_node_type() == XMLParser::NODE_ELEMENT_END && parser.get_node_name() == section) + break; + } + + skindata.joints = joint; + + } else if (section == "vertex_weights") { + + SkinControllerData::Weights weights; + + weights.count = parser.get_attribute_value("count").to_int(); + + while (parser.read() == OK) { + + if (parser.get_node_type() == XMLParser::NODE_ELEMENT) { + + if (parser.get_node_name() == "input") { + + String semantic = parser.get_attribute_value("semantic"); + String source = _uri_to_id(parser.get_attribute_value("source")); + + int offset = parser.get_attribute_value("offset").to_int(); + + SkinControllerData::Weights::SourceRef sref; + sref.source = source; + sref.offset = offset; + weights.sources[semantic] = sref; + + COLLADA_PRINT(section + " input semantic: " + semantic + " source: " + source + " offset: " + itos(offset)); + + } else if (parser.get_node_name() == "v") { //indices + + Vector values = _read_float_array(parser); + weights.indices = values; + COLLADA_PRINT("read " + itos(values.size()) + " index values"); + + } else if (parser.get_node_name() == "vcount") { // weightsitive + + Vector values = _read_float_array(parser); + weights.sets = values; + COLLADA_PRINT("read " + itos(values.size()) + " polygon values"); + } + } else if (parser.get_node_type() == XMLParser::NODE_ELEMENT_END && parser.get_node_name() == section) + break; + } + + skindata.weights = weights; + } + /* + else if (!parser.is_empty()) + parser.skip_section(); + */ + + } else if (parser.get_node_type() == XMLParser::NODE_ELEMENT_END && parser.get_node_name() == "skin") + break; + } + + /* STORE REST MATRICES */ + + Vector rests; + ERR_FAIL_COND(!skindata.joints.sources.has("JOINT")); + ERR_FAIL_COND(!skindata.joints.sources.has("INV_BIND_MATRIX")); + + String joint_arr = skindata.joints.sources["JOINT"]; + String ibm = skindata.joints.sources["INV_BIND_MATRIX"]; + + ERR_FAIL_COND(!skindata.sources.has(joint_arr)); + ERR_FAIL_COND(!skindata.sources.has(ibm)); + + SkinControllerData::Source &joint_source = skindata.sources[joint_arr]; + SkinControllerData::Source &ibm_source = skindata.sources[ibm]; + + ERR_FAIL_COND(joint_source.sarray.size() != ibm_source.array.size() / 16); + + for (int i = 0; i < joint_source.sarray.size(); i++) { + + String name = joint_source.sarray[i]; + Transform xform = _read_transform_from_array(ibm_source.array, i * 16); //<- this is a mistake, it must be applied to vertices + xform.affine_invert(); // inverse for rest, because it's an inverse +#ifdef COLLADA_IMPORT_SCALE_SCENE + xform.origin *= state.unit_scale; +#endif + skindata.bone_rest_map[name] = xform; + } +} + +void Collada::_parse_morph_controller(XMLParser &parser, String p_id) { + + state.morph_controller_data_map[p_id] = MorphControllerData(); + MorphControllerData &morphdata = state.morph_controller_data_map[p_id]; + + morphdata.mesh = _uri_to_id(parser.get_attribute_value("source")); + morphdata.mode = parser.get_attribute_value("method"); + String current_source; + + while (parser.read() == OK) { + + if (parser.get_node_type() == XMLParser::NODE_ELEMENT) { + + String section = parser.get_node_name(); + + if (section == "source") { + + String id = parser.get_attribute_value("id"); + morphdata.sources[id] = MorphControllerData::Source(); + current_source = id; + COLLADA_PRINT("source data: " + id); + + } else if (section == "float_array" || section == "array") { + // create a new array and read it. + if (morphdata.sources.has(current_source)) { + + morphdata.sources[current_source].array = _read_float_array(parser); + COLLADA_PRINT("section: " + current_source + " read " + itos(morphdata.sources[current_source].array.size()) + " values."); + } + } else if (section == "Name_array" || section == "IDREF_array") { + // create a new array and read it. + + /* + if (section=="IDREF_array") + morphdata.use_idrefs=true; + */ + if (morphdata.sources.has(current_source)) { + + morphdata.sources[current_source].sarray = _read_string_array(parser); + /* + if (section=="IDREF_array") { + Vector sa = morphdata.sources[current_source].sarray; + for(int i=0;icontroller = type == "instance_controller"; + geom->source = _uri_to_id(parser.get_attribute_value_safe("url")); + + if (parser.is_empty()) //nothing else to parse... + return geom; + // try to find also many materials and skeletons! + while (parser.read() == OK) { + + if (parser.get_node_type() == XMLParser::NODE_ELEMENT) { + + if (parser.get_node_name() == "instance_material") { + + String symbol = parser.get_attribute_value("symbol"); + String target = _uri_to_id(parser.get_attribute_value("target")); + + NodeGeometry::Material mat; + mat.target = target; + geom->material_map[symbol] = mat; + COLLADA_PRINT("uses material: '" + target + "' on primitive'" + symbol + "'"); + } else if (parser.get_node_name() == "skeleton") { + + parser.read(); + String uri = _uri_to_id(parser.get_node_data()); + if (uri != "") { + geom->skeletons.push_back(uri); + } + } + + } else if (parser.get_node_type() == XMLParser::NODE_ELEMENT_END && parser.get_node_name() == type) + break; + } + + if (geom->controller) { + + if (geom->skeletons.empty()) { + //XSI style + + if (state.skin_controller_data_map.has(geom->source)) { + SkinControllerData *skin = &state.skin_controller_data_map[geom->source]; + //case where skeletons reference bones with IDREF (XSI) + ERR_FAIL_COND_V(!skin->joints.sources.has("JOINT"), geom); + String joint_arr = skin->joints.sources["JOINT"]; + ERR_FAIL_COND_V(!skin->sources.has(joint_arr), geom); + Collada::SkinControllerData::Source &joint_source = skin->sources[joint_arr]; + geom->skeletons = joint_source.sarray; //quite crazy, but should work. + } + } + } + + return geom; +} + +Collada::Node *Collada::_parse_visual_instance_camera(XMLParser &parser) { + + NodeCamera *cam = memnew(NodeCamera); + cam->camera = _uri_to_id(parser.get_attribute_value_safe("url")); + + if (state.up_axis == Vector3::AXIS_Z) //collada weirdness + cam->post_transform.basis.rotate(Vector3(1, 0, 0), -Math_PI * 0.5); + + if (parser.is_empty()) //nothing else to parse... + return cam; + + while (parser.read() == OK) { + + if (parser.get_node_type() == XMLParser::NODE_ELEMENT_END && parser.get_node_name() == "instance_camera") + break; + } + + return cam; +} + +Collada::Node *Collada::_parse_visual_instance_light(XMLParser &parser) { + + NodeLight *cam = memnew(NodeLight); + cam->light = _uri_to_id(parser.get_attribute_value_safe("url")); + + if (state.up_axis == Vector3::AXIS_Z) //collada weirdness + cam->post_transform.basis.rotate(Vector3(1, 0, 0), -Math_PI * 0.5); + + if (parser.is_empty()) //nothing else to parse... + return cam; + + while (parser.read() == OK) { + + if (parser.get_node_type() == XMLParser::NODE_ELEMENT_END && parser.get_node_name() == "instance_light") + break; + } + + return cam; +} + +Collada::Node *Collada::_parse_visual_node_instance_data(XMLParser &parser) { + + String instance_type = parser.get_node_name(); + + if (instance_type == "instance_geometry" || instance_type == "instance_controller") { + return _parse_visual_instance_geometry(parser); + } else if (instance_type == "instance_camera") { + + return _parse_visual_instance_camera(parser); + } else if (instance_type == "instance_light") { + return _parse_visual_instance_light(parser); + } + + if (parser.is_empty()) //nothing else to parse... + return NULL; + + while (parser.read() == OK) { + + if (parser.get_node_type() == XMLParser::NODE_ELEMENT_END && parser.get_node_name() == instance_type) + break; + } + + return NULL; +} + +Collada::Node *Collada::_parse_visual_scene_node(XMLParser &parser) { + + String name; + + String id = parser.get_attribute_value_safe("id"); + + bool found_name = false; + + if (id == "") { + + id = "%NODEID%" + itos(Math::rand()); + + } else { + found_name = true; + } + + Vector xform_list; + Vector children; + + String empty_draw_type = ""; + + Node *node = NULL; + + name = parser.has_attribute("name") ? parser.get_attribute_value_safe("name") : parser.get_attribute_value_safe("id"); + if (name == "") { + + name = id; + } else { + found_name = true; + } + + if ((parser.has_attribute("type") && parser.get_attribute_value("type") == "JOINT") || state.idref_joints.has(name)) { + // handle a bone + + NodeJoint *joint = memnew(NodeJoint); + + if (parser.has_attribute("sid")) { //bones may not have sid + joint->sid = parser.get_attribute_value("sid"); + //state.bone_map[joint->sid]=joint; + } else if (state.idref_joints.has(name)) { + joint->sid = name; //kind of a cheat but.. + } else if (parser.has_attribute("name")) { + joint->sid = parser.get_attribute_value_safe("name"); + } + + if (joint->sid != "") { + state.sid_to_node_map[joint->sid] = id; + } + + node = joint; + } + + while (parser.read() == OK) { + + if (parser.get_node_type() == XMLParser::NODE_ELEMENT) { + + String section = parser.get_node_name(); + + if (section == "translate") { + Node::XForm xf; + if (parser.has_attribute("sid")) { + xf.id = parser.get_attribute_value("sid"); + } + xf.op = Node::XForm::OP_TRANSLATE; + + Vector xlt = _read_float_array(parser); + xf.data = xlt; + xform_list.push_back(xf); + + } else if (section == "rotate") { + Node::XForm xf; + if (parser.has_attribute("sid")) { + xf.id = parser.get_attribute_value("sid"); + } + xf.op = Node::XForm::OP_ROTATE; + + Vector rot = _read_float_array(parser); + xf.data = rot; + + xform_list.push_back(xf); + + } else if (section == "scale") { + Node::XForm xf; + if (parser.has_attribute("sid")) { + xf.id = parser.get_attribute_value("sid"); + } + + xf.op = Node::XForm::OP_SCALE; + + Vector scale = _read_float_array(parser); + + xf.data = scale; + + xform_list.push_back(xf); + + } else if (section == "matrix") { + Node::XForm xf; + if (parser.has_attribute("sid")) { + xf.id = parser.get_attribute_value("sid"); + } + xf.op = Node::XForm::OP_MATRIX; + + Vector matrix = _read_float_array(parser); + + xf.data = matrix; + String mtx; + for (int i = 0; i < matrix.size(); i++) + mtx += " " + rtos(matrix[i]); + + xform_list.push_back(xf); + + } else if (section == "visibility") { + Node::XForm xf; + if (parser.has_attribute("sid")) { + xf.id = parser.get_attribute_value("sid"); + } + xf.op = Node::XForm::OP_VISIBILITY; + + Vector visible = _read_float_array(parser); + + xf.data = visible; + + xform_list.push_back(xf); + + } else if (section == "empty_draw_type") { + empty_draw_type = _read_empty_draw_type(parser); + } else if (section == "technique" || section == "extra") { + + } else if (section != "node") { + //usually what defines the type of node + if (section.begins_with("instance_")) { + + if (!node) { + + node = _parse_visual_node_instance_data(parser); + + } else { + ERR_PRINT("Multiple instance_* not supported."); + } + } + + } else { + + /* Found a child node!! what to do..*/ + + Node *child = _parse_visual_scene_node(parser); + children.push_back(child); + } + + } else if (parser.get_node_type() == XMLParser::NODE_ELEMENT_END && parser.get_node_name() == "node") + break; + } + + if (!node) { + + node = memnew(Node); //generic node, nothing of relevance found + } + + node->noname = !found_name; + node->xform_list = xform_list; + node->children = children; + for (int i = 0; i < children.size(); i++) { + node->children[i]->parent = node; + } + + node->name = name; + node->id = id; + node->empty_draw_type = empty_draw_type; + + if (node->children.size() == 1) { + if (node->children[0]->noname && !node->noname) { + node->children[0]->name = node->name; + node->name = node->name + "-base"; + } + } + + node->default_transform = node->compute_transform(*this); + state.scene_map[id] = node; + + return node; +} + +void Collada::_parse_visual_scene(XMLParser &parser) { + + String id = parser.get_attribute_value("id"); + + if (parser.is_empty()) { + return; + } + + state.visual_scene_map[id] = VisualScene(); + VisualScene &vscene = state.visual_scene_map[id]; + + if (parser.has_attribute("name")) + vscene.name = parser.get_attribute_value("name"); + + while (parser.read() == OK) { + + if (parser.get_node_type() == XMLParser::NODE_ELEMENT) { + + String section = parser.get_node_name(); + + if (section == "node") { + vscene.root_nodes.push_back(_parse_visual_scene_node(parser)); + } + + } else if (parser.get_node_type() == XMLParser::NODE_ELEMENT_END && parser.get_node_name() == "visual_scene") + break; + } + + COLLADA_PRINT("Scene ID:" + id); +} + +void Collada::_parse_animation(XMLParser &parser) { + + if (!(state.import_flags & IMPORT_FLAG_ANIMATION)) { + if (!parser.is_empty()) + parser.skip_section(); + + return; + } + + Map> float_sources; + Map> string_sources; + Map source_strides; + Map> samplers; + Map> source_param_names; + Map> source_param_types; + + String id = ""; + if (parser.has_attribute("id")) + id = parser.get_attribute_value("id"); + + String current_source; + String current_sampler; + Vector channel_sources; + Vector channel_targets; + + while (parser.read() == OK) { + + if (parser.get_node_type() == XMLParser::NODE_ELEMENT) { + + String name = parser.get_node_name(); + if (name == "source") { + + current_source = parser.get_attribute_value("id"); + source_param_names[current_source] = Vector(); + source_param_types[current_source] = Vector(); + + } else if (name == "float_array") { + + if (current_source != "") { + float_sources[current_source] = _read_float_array(parser); + } + + } else if (name == "Name_array") { + + if (current_source != "") { + string_sources[current_source] = _read_string_array(parser); + } + } else if (name == "accessor") { + + if (current_source != "" && parser.has_attribute("stride")) { + source_strides[current_source] = parser.get_attribute_value("stride").to_int(); + } + } else if (name == "sampler") { + + current_sampler = parser.get_attribute_value("id"); + samplers[current_sampler] = Map(); + } else if (name == "param") { + + if (parser.has_attribute("name")) + source_param_names[current_source].push_back(parser.get_attribute_value("name")); + else + source_param_names[current_source].push_back(""); + + if (parser.has_attribute("type")) + source_param_types[current_source].push_back(parser.get_attribute_value("type")); + else + source_param_types[current_source].push_back(""); + + } else if (name == "input") { + + if (current_sampler != "") { + + samplers[current_sampler][parser.get_attribute_value("semantic")] = parser.get_attribute_value("source"); + } + + } else if (name == "channel") { + + channel_sources.push_back(parser.get_attribute_value("source")); + channel_targets.push_back(parser.get_attribute_value("target")); + } + + } else if (parser.get_node_type() == XMLParser::NODE_ELEMENT_END && parser.get_node_name() == "animation") + break; //end of + } + + for (int i = 0; i < channel_sources.size(); i++) { + + String source = _uri_to_id(channel_sources[i]); + String target = channel_targets[i]; + ERR_CONTINUE(!samplers.has(source)); + Map &sampler = samplers[source]; + + ERR_CONTINUE(!sampler.has("INPUT")); //no input semantic? wtf? + String input_id = _uri_to_id(sampler["INPUT"]); + COLLADA_PRINT("input id is " + input_id); + ERR_CONTINUE(!float_sources.has(input_id)); + + ERR_CONTINUE(!sampler.has("OUTPUT")); + String output_id = _uri_to_id(sampler["OUTPUT"]); + ERR_CONTINUE(!float_sources.has(output_id)); + + ERR_CONTINUE(!source_param_names.has(output_id)); + + Vector &names = source_param_names[output_id]; + + for (int l = 0; l < names.size(); l++) { + + String name = names[l]; + + Vector &time_keys = float_sources[input_id]; + int key_count = time_keys.size(); + + AnimationTrack track; //begin crating track + track.id = id; + + track.keys.resize(key_count); + + for (int j = 0; j < key_count; j++) { + track.keys.write[j].time = time_keys[j]; + state.animation_length = MAX(state.animation_length, time_keys[j]); + } + + //now read actual values + + int stride = 1; + + if (source_strides.has(output_id)) + stride = source_strides[output_id]; + int output_len = stride / names.size(); + + ERR_CONTINUE(output_len == 0); + ERR_CONTINUE(!float_sources.has(output_id)); + + Vector &output = float_sources[output_id]; + + ERR_CONTINUE_MSG((output.size() / stride) != key_count, "Wrong number of keys in output."); + + for (int j = 0; j < key_count; j++) { + track.keys.write[j].data.resize(output_len); + for (int k = 0; k < output_len; k++) + track.keys.write[j].data.write[k] = output[l + j * stride + k]; //super weird but should work: + } + + if (sampler.has("INTERPOLATION")) { + + String interp_id = _uri_to_id(sampler["INTERPOLATION"]); + ERR_CONTINUE(!string_sources.has(interp_id)); + Vector &interps = string_sources[interp_id]; + ERR_CONTINUE(interps.size() != key_count); + + for (int j = 0; j < key_count; j++) { + if (interps[j] == "BEZIER") + track.keys.write[j].interp_type = AnimationTrack::INTERP_BEZIER; + else + track.keys.write[j].interp_type = AnimationTrack::INTERP_LINEAR; + } + } + + if (sampler.has("IN_TANGENT") && sampler.has("OUT_TANGENT")) { + //bezier control points.. + String intangent_id = _uri_to_id(sampler["IN_TANGENT"]); + ERR_CONTINUE(!float_sources.has(intangent_id)); + Vector &intangents = float_sources[intangent_id]; + + ERR_CONTINUE(intangents.size() != key_count * 2 * names.size()); + + String outangent_id = _uri_to_id(sampler["OUT_TANGENT"]); + ERR_CONTINUE(!float_sources.has(outangent_id)); + Vector &outangents = float_sources[outangent_id]; + ERR_CONTINUE(outangents.size() != key_count * 2 * names.size()); + + for (int j = 0; j < key_count; j++) { + track.keys.write[j].in_tangent = Vector2(intangents[j * 2 * names.size() + 0 + l * 2], intangents[j * 2 * names.size() + 1 + l * 2]); + track.keys.write[j].out_tangent = Vector2(outangents[j * 2 * names.size() + 0 + l * 2], outangents[j * 2 * names.size() + 1 + l * 2]); + } + } + + if (target.find("/") != -1) { //transform component + track.target = target.get_slicec('/', 0); + track.param = target.get_slicec('/', 1); + if (track.param.find(".") != -1) + track.component = track.param.get_slice(".", 1).to_upper(); + track.param = track.param.get_slice(".", 0); + if (names.size() > 1 && track.component == "") { + //this is a guess because the collada spec is ambiguous here... + //i suppose if you have many names (outputs) you can't use a component and i should abide to that. + track.component = name; + } + } else { + track.target = target; + } + + state.animation_tracks.push_back(track); + + if (!state.referenced_tracks.has(target)) + state.referenced_tracks[target] = Vector(); + + state.referenced_tracks[target].push_back(state.animation_tracks.size() - 1); + + if (id != "") { + if (!state.by_id_tracks.has(id)) + state.by_id_tracks[id] = Vector(); + + state.by_id_tracks[id].push_back(state.animation_tracks.size() - 1); + } + + COLLADA_PRINT("loaded animation with " + itos(key_count) + " keys"); + } + } +} + +void Collada::_parse_animation_clip(XMLParser &parser) { + + if (!(state.import_flags & IMPORT_FLAG_ANIMATION)) { + if (!parser.is_empty()) + parser.skip_section(); + + return; + } + + AnimationClip clip; + + if (parser.has_attribute("name")) + clip.name = parser.get_attribute_value("name"); + else if (parser.has_attribute("id")) + clip.name = parser.get_attribute_value("id"); + if (parser.has_attribute("start")) + clip.begin = parser.get_attribute_value("start").to_double(); + if (parser.has_attribute("end")) + clip.end = parser.get_attribute_value("end").to_double(); + + while (parser.read() == OK) { + + if (parser.get_node_type() == XMLParser::NODE_ELEMENT) { + + String name = parser.get_node_name(); + if (name == "instance_animation") { + + String url = _uri_to_id(parser.get_attribute_value("url")); + clip.tracks.push_back(url); + } + + } else if (parser.get_node_type() == XMLParser::NODE_ELEMENT_END && parser.get_node_name() == "animation_clip") + break; //end of + } + + state.animation_clips.push_back(clip); +} + +void Collada::_parse_scene(XMLParser &parser) { + + if (parser.is_empty()) { + return; + } + + while (parser.read() == OK) { + + if (parser.get_node_type() == XMLParser::NODE_ELEMENT) { + + String name = parser.get_node_name(); + + if (name == "instance_visual_scene") { + + state.root_visual_scene = _uri_to_id(parser.get_attribute_value("url")); + } else if (name == "instance_physics_scene") { + + state.root_physics_scene = _uri_to_id(parser.get_attribute_value("url")); + } + + } else if (parser.get_node_type() == XMLParser::NODE_ELEMENT_END && parser.get_node_name() == "scene") + break; //end of + } +} + +void Collada::_parse_library(XMLParser &parser) { + + if (parser.is_empty()) { + return; + } + + while (parser.read() == OK) { + + if (parser.get_node_type() == XMLParser::NODE_ELEMENT) { + + String name = parser.get_node_name(); + COLLADA_PRINT("library name is: " + name); + if (name == "image") { + + _parse_image(parser); + } else if (name == "material") { + + _parse_material(parser); + } else if (name == "effect") { + + _parse_effect(parser); + } else if (name == "camera") { + + _parse_camera(parser); + } else if (name == "light") { + + _parse_light(parser); + } else if (name == "geometry") { + + String id = parser.get_attribute_value("id"); + String name2 = parser.get_attribute_value_safe("name"); + while (parser.read() == OK) { + + if (parser.get_node_type() == XMLParser::NODE_ELEMENT) { + + if (parser.get_node_name() == "mesh") { + state.mesh_name_map[id] = (name2 != "") ? name2 : id; + _parse_mesh_geometry(parser, id, name2); + } else if (parser.get_node_name() == "spline") { + state.mesh_name_map[id] = (name2 != "") ? name2 : id; + _parse_curve_geometry(parser, id, name2); + } else if (!parser.is_empty()) + parser.skip_section(); + } else if (parser.get_node_type() == XMLParser::NODE_ELEMENT_END && parser.get_node_name() == "geometry") + break; + } + + } else if (name == "controller") { + + _parse_controller(parser); + } else if (name == "animation") { + + _parse_animation(parser); + } else if (name == "animation_clip") { + + _parse_animation_clip(parser); + } else if (name == "visual_scene") { + + COLLADA_PRINT("visual scene"); + _parse_visual_scene(parser); + } else if (!parser.is_empty()) + parser.skip_section(); + + } else if (parser.get_node_type() == XMLParser::NODE_ELEMENT_END && parser.get_node_name().begins_with("library_")) + break; //end of + } +} + +void Collada::_joint_set_owner(Collada::Node *p_node, NodeSkeleton *p_owner) { + + if (p_node->type == Node::TYPE_JOINT) { + + NodeJoint *nj = static_cast(p_node); + nj->owner = p_owner; + + for (int i = 0; i < nj->children.size(); i++) { + + _joint_set_owner(nj->children.write[i], p_owner); + } + } +} + +void Collada::_create_skeletons(Collada::Node **p_node, NodeSkeleton *p_skeleton) { + + Node *node = *p_node; + + if (node->type == Node::TYPE_JOINT) { + + if (!p_skeleton) { + + // ohohohoohoo it's a joint node, time to work! + NodeSkeleton *sk = memnew(NodeSkeleton); + *p_node = sk; + sk->children.push_back(node); + sk->parent = node->parent; + node->parent = sk; + p_skeleton = sk; + } + + NodeJoint *nj = static_cast(node); + nj->owner = p_skeleton; + } else { + p_skeleton = NULL; + } + + for (int i = 0; i < node->children.size(); i++) { + _create_skeletons(&node->children.write[i], p_skeleton); + } +} + +bool Collada::_remove_node(Node *p_parent, Node *p_node) { + + for (int i = 0; i < p_parent->children.size(); i++) { + + if (p_parent->children[i] == p_node) { + p_parent->children.remove(i); + return true; + } + if (_remove_node(p_parent->children[i], p_node)) + return true; + } + + return false; +} + +void Collada::_remove_node(VisualScene *p_vscene, Node *p_node) { + + for (int i = 0; i < p_vscene->root_nodes.size(); i++) { + if (p_vscene->root_nodes[i] == p_node) { + + p_vscene->root_nodes.remove(i); + return; + } + if (_remove_node(p_vscene->root_nodes[i], p_node)) + return; + } + + ERR_PRINT("ERROR: Not found node to remove?"); +} + +void Collada::_merge_skeletons(VisualScene *p_vscene, Node *p_node) { + + if (p_node->type == Node::TYPE_GEOMETRY) { + + NodeGeometry *gnode = static_cast(p_node); + if (gnode->controller) { + + // recount skeletons used + Set skeletons; + + for (int i = 0; i < gnode->skeletons.size(); i++) { + + String nodeid = gnode->skeletons[i]; + + ERR_CONTINUE(!state.scene_map.has(nodeid)); //weird, it should have it... + +#ifdef NO_SAFE_CAST + NodeJoint *nj = static_cast(state.scene_map[nodeid]); +#else + NodeJoint *nj = dynamic_cast(state.scene_map[nodeid]); +#endif + ERR_CONTINUE(!nj); //broken collada + ERR_CONTINUE(!nj->owner); //weird, node should have a skeleton owner + + skeletons.insert(nj->owner); + } + + if (skeletons.size() > 1) { + + //do the merger!! + Set::Element *E = skeletons.front(); + NodeSkeleton *base = E->get(); + + for (E = E->next(); E; E = E->next()) { + + NodeSkeleton *merged = E->get(); + _remove_node(p_vscene, merged); + for (int i = 0; i < merged->children.size(); i++) { + + _joint_set_owner(merged->children[i], base); + base->children.push_back(merged->children[i]); + merged->children[i]->parent = base; + } + + merged->children.clear(); //take children from it + memdelete(merged); + } + } + } + } + + for (int i = 0; i < p_node->children.size(); i++) { + _merge_skeletons(p_vscene, p_node->children[i]); + } +} + +void Collada::_merge_skeletons2(VisualScene *p_vscene) { + + for (Map::Element *E = state.skin_controller_data_map.front(); E; E = E->next()) { + + SkinControllerData &cd = E->get(); + + NodeSkeleton *skeleton = NULL; + + for (Map::Element *F = cd.bone_rest_map.front(); F; F = F->next()) { + + String name; + + if (!state.sid_to_node_map.has(F->key())) { + continue; + } + + name = state.sid_to_node_map[F->key()]; + + ERR_CONTINUE(!state.scene_map.has(name)); + + Node *node = state.scene_map[name]; + ERR_CONTINUE(node->type != Node::TYPE_JOINT); + + NodeSkeleton *sk = NULL; + + while (node && !sk) { + + if (node->type == Node::TYPE_SKELETON) { + sk = static_cast(node); + } + node = node->parent; + } + + ERR_CONTINUE(!sk); + + if (!skeleton) { + skeleton = sk; + continue; + } + + if (skeleton != sk) { + //whoa.. wtf, merge. + _remove_node(p_vscene, sk); + for (int i = 0; i < sk->children.size(); i++) { + + _joint_set_owner(sk->children[i], skeleton); + skeleton->children.push_back(sk->children[i]); + sk->children[i]->parent = skeleton; + } + + sk->children.clear(); //take children from it + memdelete(sk); + } + } + } +} + +bool Collada::_optimize_skeletons(VisualScene *p_vscene, Node *p_node) { + + Node *node = p_node; + + if (node->type == Node::TYPE_SKELETON && node->parent && node->parent->type == Node::TYPE_NODE && node->parent->children.size() == 1) { + //replace parent by this... + Node *parent = node->parent; + + //i wonder if this is alright.. i think it is since created skeleton (first joint) is already animated by bone.. + node->id = parent->id; + node->name = parent->name; + node->xform_list = parent->xform_list; + node->default_transform = parent->default_transform; + + state.scene_map[node->id] = node; + node->parent = parent->parent; + + if (parent->parent) { + Node *gp = parent->parent; + bool found = false; + for (int i = 0; i < gp->children.size(); i++) { + + if (gp->children[i] == parent) { + gp->children.write[i] = node; + found = true; + break; + } + } + if (!found) { + ERR_PRINT("BUG"); + } + } else { + + bool found = false; + + for (int i = 0; i < p_vscene->root_nodes.size(); i++) { + + if (p_vscene->root_nodes[i] == parent) { + + p_vscene->root_nodes.write[i] = node; + found = true; + break; + } + } + if (!found) { + ERR_PRINT("BUG"); + } + } + + parent->children.clear(); + memdelete(parent); + return true; + } + + for (int i = 0; i < node->children.size(); i++) { + + if (_optimize_skeletons(p_vscene, node->children[i])) + return false; //stop processing, go up + } + + return false; +} + +bool Collada::_move_geometry_to_skeletons(VisualScene *p_vscene, Node *p_node, List *p_mgeom) { + + // Bind Shape Matrix scales the bones and makes them gigantic, so the matrix then shrinks the model? + // Solution: apply the Bind Shape Matrix to the VERTICES, and if the object comes scaled, it seems to be left alone! + + if (p_node->type == Node::TYPE_GEOMETRY) { + + NodeGeometry *ng = static_cast(p_node); + if (ng->ignore_anim) + return false; //already made child of skeleton and processeg + + if (ng->controller && ng->skeletons.size()) { + + String nodeid = ng->skeletons[0]; + + ERR_FAIL_COND_V(!state.scene_map.has(nodeid), false); //weird, it should have it... +#ifdef NO_SAFE_CAST + NodeJoint *nj = static_cast(state.scene_map[nodeid]); +#else + NodeJoint *nj = dynamic_cast(state.scene_map[nodeid]); +#endif + ERR_FAIL_COND_V(!nj, false); + ERR_FAIL_COND_V(!nj->owner, false); //weird, node should have a skeleton owner + + NodeSkeleton *sk = nj->owner; + + Node *p = sk->parent; + bool node_is_parent_of_skeleton = false; + + while (p) { + if (p == p_node) { + node_is_parent_of_skeleton = true; + break; + } + p = p->parent; // try again + } + + ERR_FAIL_COND_V(node_is_parent_of_skeleton, false); + + //this should be correct + ERR_FAIL_COND_V(!state.skin_controller_data_map.has(ng->source), false); + SkinControllerData &skin = state.skin_controller_data_map[ng->source]; + Transform skel_inv = sk->get_global_transform().affine_inverse(); + p_node->default_transform = skel_inv * (skin.bind_shape /* p_node->get_global_transform()*/); // i honestly have no idea what to do with a previous model xform.. most exporters ignore it + + //make rests relative to the skeleton (they seem to be always relative to world) + for (Map::Element *E = skin.bone_rest_map.front(); E; E = E->next()) { + + E->get() = skel_inv * E->get(); //make the bone rest local to the skeleton + state.bone_rest_map[E->key()] = E->get(); // make it remember where the bone is globally, now that it's relative + } + + //but most exporters seem to work only if i do this.. + //p_node->default_transform = p_node->get_global_transform(); + + //p_node->default_transform=Transform(); //this seems to be correct, because bind shape makes the object local to the skeleton + p_node->ignore_anim = true; // collada may animate this later, if it does, then this is not supported (redo your original asset and don't animate the base mesh) + p_node->parent = sk; + //sk->children.push_back(0,p_node); //avoid INFINITE loop + p_mgeom->push_back(p_node); + return true; + } + } + + for (int i = 0; i < p_node->children.size(); i++) { + + if (_move_geometry_to_skeletons(p_vscene, p_node->children[i], p_mgeom)) { + p_node->children.remove(i); + i--; + } + } + + return false; +} + +void Collada::_find_morph_nodes(VisualScene *p_vscene, Node *p_node) { + + if (p_node->type == Node::TYPE_GEOMETRY) { + + NodeGeometry *nj = static_cast(p_node); + + if (nj->controller) { + + String base = nj->source; + + while (base != "" && !state.mesh_data_map.has(base)) { + + if (state.skin_controller_data_map.has(base)) { + + SkinControllerData &sk = state.skin_controller_data_map[base]; + base = sk.base; + } else if (state.morph_controller_data_map.has(base)) { + + state.morph_ownership_map[base] = nj->id; + break; + } else { + ERR_FAIL_MSG("Invalid scene."); + } + } + } + } + + for (int i = 0; i < p_node->children.size(); i++) { + + _find_morph_nodes(p_vscene, p_node->children[i]); + } +} + +void Collada::_optimize() { + + for (Map::Element *E = state.visual_scene_map.front(); E; E = E->next()) { + + VisualScene &vs = E->get(); + for (int i = 0; i < vs.root_nodes.size(); i++) { + _create_skeletons(&vs.root_nodes.write[i]); + } + + for (int i = 0; i < vs.root_nodes.size(); i++) { + _merge_skeletons(&vs, vs.root_nodes[i]); + } + + _merge_skeletons2(&vs); + + for (int i = 0; i < vs.root_nodes.size(); i++) { + _optimize_skeletons(&vs, vs.root_nodes[i]); + } + + for (int i = 0; i < vs.root_nodes.size(); i++) { + + List mgeom; + if (_move_geometry_to_skeletons(&vs, vs.root_nodes[i], &mgeom)) { + vs.root_nodes.remove(i); + i--; + } + + while (!mgeom.empty()) { + + Node *n = mgeom.front()->get(); + n->parent->children.push_back(n); + mgeom.pop_front(); + } + } + + for (int i = 0; i < vs.root_nodes.size(); i++) { + _find_morph_nodes(&vs, vs.root_nodes[i]); + } + } +} + +int Collada::get_uv_channel(String p_name) { + + if (!channel_map.has(p_name)) { + + ERR_FAIL_COND_V(channel_map.size() == 2, 0); + + channel_map[p_name] = channel_map.size(); + } + + return channel_map[p_name]; +} + +Error Collada::load(const String &p_path, int p_flags) { + + Ref parserr = memnew(XMLParser); + XMLParser &parser = *parserr.ptr(); + Error err = parser.open(p_path); + ERR_FAIL_COND_V_MSG(err, err, "Cannot open Collada file '" + p_path + "'."); + + state.local_path = ProjectSettings::get_singleton()->localize_path(p_path); + state.import_flags = p_flags; + /* Skip headers */ + while ((err = parser.read()) == OK) { + + if (parser.get_node_type() == XMLParser::NODE_ELEMENT) { + + if (parser.get_node_name() == "COLLADA") { + break; + } else if (!parser.is_empty()) + parser.skip_section(); // unknown section, likely headers + } + } + + ERR_FAIL_COND_V_MSG(err != OK, ERR_FILE_CORRUPT, "Corrupted Collada file '" + p_path + "'."); + + /* Start loading Collada */ + + { + //version + String version = parser.get_attribute_value("version"); + state.version.major = version.get_slice(".", 0).to_int(); + state.version.minor = version.get_slice(".", 1).to_int(); + state.version.rev = version.get_slice(".", 2).to_int(); + COLLADA_PRINT("Collada VERSION: " + version); + } + + while ((err = parser.read()) == OK) { + + /* Read all the main sections.. */ + + if (parser.get_node_type() != XMLParser::NODE_ELEMENT) + continue; //no idea what this may be, but skipping anyway + + String section = parser.get_node_name(); + + COLLADA_PRINT("section: " + section); + + if (section == "asset") { + _parse_asset(parser); + + } else if (section.begins_with("library_")) { + + _parse_library(parser); + } else if (section == "scene") { + + _parse_scene(parser); + } else if (!parser.is_empty()) { + parser.skip_section(); // unknown section, likely headers + } + } + + _optimize(); + return OK; +} + +Collada::Collada() { +} diff --git a/editor/import/collada.h b/editor/import/collada.h new file mode 100644 index 0000000000..4707d7d779 --- /dev/null +++ b/editor/import/collada.h @@ -0,0 +1,647 @@ +/*************************************************************************/ +/* collada.h */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */ +/* */ +/* 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. */ +/*************************************************************************/ + +#ifndef COLLADA_H +#define COLLADA_H + +#include "core/io/xml_parser.h" +#include "core/map.h" +#include "core/project_settings.h" +#include "scene/resources/material.h" + +class Collada { +public: + enum ImportFlags { + IMPORT_FLAG_SCENE = 1, + IMPORT_FLAG_ANIMATION = 2 + }; + + struct Image { + + String path; + }; + + struct Material { + + String name; + String instance_effect; + }; + + struct Effect { + + String name; + Map params; + + struct Channel { + + int uv_idx; + String texture; + Color color; + Channel() { uv_idx = 0; } + }; + + Channel diffuse, specular, emission, bump; + float shininess; + bool found_double_sided; + bool double_sided; + bool unshaded; + + String get_texture_path(const String &p_source, Collada &state) const; + + Effect() { + diffuse.color = Color(1, 1, 1, 1); + double_sided = true; + found_double_sided = false; + shininess = 40; + unshaded = false; + } + }; + + struct CameraData { + + enum Mode { + MODE_PERSPECTIVE, + MODE_ORTHOGONAL + }; + + Mode mode; + + union { + struct { + float x_fov; + float y_fov; + } perspective; + struct { + float x_mag; + float y_mag; + } orthogonal; + }; + + float aspect; + float z_near; + float z_far; + + CameraData() : + mode(MODE_PERSPECTIVE), + aspect(1), + z_near(0.1), + z_far(100) { + perspective.x_fov = 0; + perspective.y_fov = 0; + } + }; + + struct LightData { + + enum Mode { + MODE_AMBIENT, + MODE_DIRECTIONAL, + MODE_OMNI, + MODE_SPOT + }; + + Mode mode; + + Color color; + + float constant_att; + float linear_att; + float quad_att; + + float spot_angle; + float spot_exp; + + LightData() : + mode(MODE_AMBIENT), + color(Color(1, 1, 1, 1)), + constant_att(0), + linear_att(0), + quad_att(0), + spot_angle(45), + spot_exp(1) { + } + }; + + struct MeshData { + + String name; + struct Source { + + Vector array; + int stride; + }; + + Map sources; + + struct Vertices { + + Map sources; + }; + + Map vertices; + + struct Primitives { + + struct SourceRef { + + String source; + int offset; + }; + + String material; + Map sources; + Vector polygons; + Vector indices; + int count; + int vertex_size; + }; + + Vector primitives; + + bool found_double_sided; + bool double_sided; + + MeshData() { + found_double_sided = false; + double_sided = true; + } + }; + + struct CurveData { + + String name; + bool closed; + + struct Source { + + Vector sarray; + Vector array; + int stride; + }; + + Map sources; + + Map control_vertices; + + CurveData() { + + closed = false; + } + }; + struct SkinControllerData { + + String base; + bool use_idrefs; + + Transform bind_shape; + + struct Source { + + Vector sarray; //maybe for names + Vector array; + int stride; + Source() { + stride = 1; + } + }; + + Map sources; + + struct Joints { + + Map sources; + } joints; + + struct Weights { + + struct SourceRef { + + String source; + int offset; + }; + + String material; + Map sources; + Vector sets; + Vector indices; + int count; + } weights; + + Map bone_rest_map; + + SkinControllerData() { use_idrefs = false; } + }; + + struct MorphControllerData { + + String mesh; + String mode; + + struct Source { + + int stride; + Vector sarray; //maybe for names + Vector array; + Source() { stride = 1; } + }; + + Map sources; + + Map targets; + MorphControllerData() {} + }; + + struct Vertex { + + int idx; + Vector3 vertex; + Vector3 normal; + Vector3 uv; + Vector3 uv2; + Plane tangent; + Color color; + int uid; + struct Weight { + int bone_idx; + float weight; + bool operator<(const Weight w) const { return weight > w.weight; } //heaviest first + }; + + Vector weights; + + void fix_weights() { + + weights.sort(); + if (weights.size() > 4) { + //cap to 4 and make weights add up 1 + weights.resize(4); + float total = 0; + for (int i = 0; i < 4; i++) + total += weights[i].weight; + if (total) + for (int i = 0; i < 4; i++) + weights.write[i].weight /= total; + } + } + + void fix_unit_scale(Collada &state); + + bool operator<(const Vertex &p_vert) const { + + if (uid == p_vert.uid) { + if (vertex == p_vert.vertex) { + if (normal == p_vert.normal) { + if (uv == p_vert.uv) { + if (uv2 == p_vert.uv2) { + + if (!weights.empty() || !p_vert.weights.empty()) { + + if (weights.size() == p_vert.weights.size()) { + + for (int i = 0; i < weights.size(); i++) { + if (weights[i].bone_idx != p_vert.weights[i].bone_idx) + return weights[i].bone_idx < p_vert.weights[i].bone_idx; + + if (weights[i].weight != p_vert.weights[i].weight) + return weights[i].weight < p_vert.weights[i].weight; + } + } else { + return weights.size() < p_vert.weights.size(); + } + } + + return (color < p_vert.color); + } else + return (uv2 < p_vert.uv2); + } else + return (uv < p_vert.uv); + } else + return (normal < p_vert.normal); + } else + return vertex < p_vert.vertex; + } else + return uid < p_vert.uid; + } + + Vertex() { + uid = 0; + idx = 0; + } + }; + struct Node { + + enum Type { + + TYPE_NODE, + TYPE_JOINT, + TYPE_SKELETON, //this bone is not collada, it's added afterwards as optimization + TYPE_LIGHT, + TYPE_CAMERA, + TYPE_GEOMETRY + }; + + struct XForm { + + enum Op { + OP_ROTATE, + OP_SCALE, + OP_TRANSLATE, + OP_MATRIX, + OP_VISIBILITY + }; + + String id; + Op op; + Vector data; + }; + + Type type; + + String name; + String id; + String empty_draw_type; + bool noname; + Vector xform_list; + Transform default_transform; + Transform post_transform; + Vector children; + + Node *parent; + + Transform compute_transform(Collada &state) const; + Transform get_global_transform() const; + Transform get_transform() const; + + bool ignore_anim; + + Node() { + noname = false; + type = TYPE_NODE; + parent = NULL; + ignore_anim = false; + } + virtual ~Node() { + for (int i = 0; i < children.size(); i++) + memdelete(children[i]); + }; + }; + + struct NodeSkeleton : public Node { + + NodeSkeleton() { type = TYPE_SKELETON; } + }; + + struct NodeJoint : public Node { + + NodeSkeleton *owner; + String sid; + NodeJoint() { + type = TYPE_JOINT; + owner = NULL; + } + }; + + struct NodeGeometry : public Node { + + bool controller; + String source; + + struct Material { + String target; + }; + + Map material_map; + Vector skeletons; + + NodeGeometry() { type = TYPE_GEOMETRY; } + }; + + struct NodeCamera : public Node { + + String camera; + + NodeCamera() { type = TYPE_CAMERA; } + }; + + struct NodeLight : public Node { + + String light; + + NodeLight() { type = TYPE_LIGHT; } + }; + + struct VisualScene { + + String name; + Vector root_nodes; + + ~VisualScene() { + for (int i = 0; i < root_nodes.size(); i++) + memdelete(root_nodes[i]); + } + }; + + struct AnimationClip { + + String name; + float begin; + float end; + Vector tracks; + + AnimationClip() { + begin = 0; + end = 1; + } + }; + + struct AnimationTrack { + + String id; + String target; + String param; + String component; + bool property; + + enum InterpolationType { + INTERP_LINEAR, + INTERP_BEZIER + }; + + struct Key { + + enum Type { + TYPE_FLOAT, + TYPE_MATRIX + }; + + float time; + Vector data; + Point2 in_tangent; + Point2 out_tangent; + InterpolationType interp_type; + + Key() { interp_type = INTERP_LINEAR; } + }; + + Vector get_value_at_time(float p_time) const; + + Vector keys; + + AnimationTrack() { property = false; } + }; + + /****************/ + /* IMPORT STATE */ + /****************/ + + struct State { + + int import_flags; + + float unit_scale; + Vector3::Axis up_axis; + bool z_up; + + struct Version { + + int major, minor, rev; + + bool operator<(const Version &p_ver) const { return (major == p_ver.major) ? ((minor == p_ver.minor) ? (rev < p_ver.rev) : minor < p_ver.minor) : major < p_ver.major; } + Version(int p_major = 0, int p_minor = 0, int p_rev = 0) { + major = p_major; + minor = p_minor; + rev = p_rev; + } + } version; + + Map camera_data_map; + Map mesh_data_map; + Map light_data_map; + Map curve_data_map; + + Map mesh_name_map; + Map morph_name_map; + Map morph_ownership_map; + Map skin_controller_data_map; + Map morph_controller_data_map; + + Map image_map; + Map material_map; + Map effect_map; + + Map visual_scene_map; + Map scene_map; + Set idref_joints; + Map sid_to_node_map; + //Map bone_map; + + Map bone_rest_map; + + String local_path; + String root_visual_scene; + String root_physics_scene; + + Vector animation_clips; + Vector animation_tracks; + Map> referenced_tracks; + Map> by_id_tracks; + + float animation_length; + + State() : + import_flags(0), + unit_scale(1.0), + up_axis(Vector3::AXIS_Y), + animation_length(0) { + } + } state; + + Error load(const String &p_path, int p_flags = 0); + + Collada(); + + Transform fix_transform(const Transform &p_transform); + + Transform get_root_transform() const; + + int get_uv_channel(String p_name); + +private: // private stuff + Map channel_map; + + void _parse_asset(XMLParser &parser); + void _parse_image(XMLParser &parser); + void _parse_material(XMLParser &parser); + void _parse_effect_material(XMLParser &parser, Effect &effect, String &id); + void _parse_effect(XMLParser &parser); + void _parse_camera(XMLParser &parser); + void _parse_light(XMLParser &parser); + void _parse_animation_clip(XMLParser &parser); + + void _parse_mesh_geometry(XMLParser &parser, String p_id, String p_name); + void _parse_curve_geometry(XMLParser &parser, String p_id, String p_name); + + void _parse_skin_controller(XMLParser &parser, String p_id); + void _parse_morph_controller(XMLParser &parser, String p_id); + void _parse_controller(XMLParser &parser); + + Node *_parse_visual_instance_geometry(XMLParser &parser); + Node *_parse_visual_instance_camera(XMLParser &parser); + Node *_parse_visual_instance_light(XMLParser &parser); + + Node *_parse_visual_node_instance_data(XMLParser &parser); + Node *_parse_visual_scene_node(XMLParser &parser); + void _parse_visual_scene(XMLParser &parser); + + void _parse_animation(XMLParser &parser); + void _parse_scene(XMLParser &parser); + void _parse_library(XMLParser &parser); + + Variant _parse_param(XMLParser &parser); + Vector _read_float_array(XMLParser &parser); + Vector _read_string_array(XMLParser &parser); + Transform _read_transform(XMLParser &parser); + String _read_empty_draw_type(XMLParser &parser); + + void _joint_set_owner(Collada::Node *p_node, NodeSkeleton *p_owner); + void _create_skeletons(Collada::Node **p_node, NodeSkeleton *p_skeleton = NULL); + void _find_morph_nodes(VisualScene *p_vscene, Node *p_node); + bool _remove_node(Node *p_parent, Node *p_node); + void _remove_node(VisualScene *p_vscene, Node *p_node); + void _merge_skeletons2(VisualScene *p_vscene); + void _merge_skeletons(VisualScene *p_vscene, Node *p_node); + bool _optimize_skeletons(VisualScene *p_vscene, Node *p_node); + + bool _move_geometry_to_skeletons(VisualScene *p_vscene, Node *p_node, List *p_mgeom); + + void _optimize(); +}; + +#endif // COLLADA_H diff --git a/editor/import/editor_import_collada.cpp b/editor/import/editor_import_collada.cpp index 3cc6e7a50c..f7da8c27f9 100644 --- a/editor/import/editor_import_collada.cpp +++ b/editor/import/editor_import_collada.cpp @@ -31,8 +31,8 @@ #include "editor_import_collada.h" #include "core/os/os.h" -#include "editor/collada/collada.h" #include "editor/editor_node.h" +#include "editor/import/collada.h" #include "scene/3d/camera.h" #include "scene/3d/light.h" #include "scene/3d/mesh_instance.h" diff --git a/main/main.cpp b/main/main.cpp index 887d423d52..0047d05771 100644 --- a/main/main.cpp +++ b/main/main.cpp @@ -71,8 +71,8 @@ #include "servers/register_server_types.h" #ifdef TOOLS_ENABLED -#include "editor/doc/doc_data.h" -#include "editor/doc/doc_data_class_path.gen.h" +#include "editor/doc_data.h" +#include "editor/doc_data_class_path.gen.h" #include "editor/editor_node.h" #include "editor/editor_settings.h" #include "editor/progress_dialog.h" diff --git a/modules/gdscript/language_server/lsp.hpp b/modules/gdscript/language_server/lsp.hpp index a2dcc48820..914c9742e1 100644 --- a/modules/gdscript/language_server/lsp.hpp +++ b/modules/gdscript/language_server/lsp.hpp @@ -33,7 +33,7 @@ #include "core/class_db.h" #include "core/list.h" -#include "editor/doc/doc_data.h" +#include "editor/doc_data.h" namespace lsp { diff --git a/modules/mono/editor/bindings_generator.h b/modules/mono/editor/bindings_generator.h index b133923c25..bebe489d02 100644 --- a/modules/mono/editor/bindings_generator.h +++ b/modules/mono/editor/bindings_generator.h @@ -33,7 +33,7 @@ #include "core/class_db.h" #include "core/string_builder.h" -#include "editor/doc/doc_data.h" +#include "editor/doc_data.h" #include "editor/editor_help.h" #if defined(DEBUG_METHODS_ENABLED) && defined(TOOLS_ENABLED) -- cgit v1.2.3