mirror of
https://github.com/Relintai/terraman_2d.git
synced 2024-11-12 10:15:18 +01:00
324 lines
11 KiB
C++
324 lines
11 KiB
C++
/*
|
|
Copyright (c) 2019-2021 Péter Magyar
|
|
|
|
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 "terra_material_cache_2d.h"
|
|
|
|
#ifdef PROPS_PRESENT
|
|
#include "../../props/props/prop_data.h"
|
|
#include "../../props/props/prop_data_prop.h"
|
|
|
|
#if MESH_DATA_RESOURCE_PRESENT
|
|
#include "../../mesh_data_resource/props/prop_data_mesh_data.h"
|
|
#endif
|
|
#endif
|
|
|
|
bool TerraMaterialCache2D::get_initialized() {
|
|
return _initialized;
|
|
}
|
|
void TerraMaterialCache2D::set_initialized(const bool value) {
|
|
_initialized = value;
|
|
}
|
|
|
|
int TerraMaterialCache2D::get_ref_count() {
|
|
return _ref_count;
|
|
}
|
|
void TerraMaterialCache2D::set_ref_count(const int value) {
|
|
_ref_count = value;
|
|
}
|
|
void TerraMaterialCache2D::inc_ref_count() {
|
|
_ref_count += 1;
|
|
}
|
|
void TerraMaterialCache2D::dec_ref_count() {
|
|
_ref_count -= 1;
|
|
}
|
|
|
|
//Materials
|
|
Ref<Material> TerraMaterialCache2D::material_get(const int index) {
|
|
ERR_FAIL_INDEX_V(index, _materials.size(), Ref<Material>(NULL));
|
|
|
|
return _materials[index];
|
|
}
|
|
|
|
Ref<Material> TerraMaterialCache2D::material_lod_get(const int index) {
|
|
ERR_FAIL_COND_V(_materials.size() == 0, Ref<Material>(NULL));
|
|
|
|
if (index < 0) {
|
|
return _materials[0];
|
|
}
|
|
|
|
if (index >= _materials.size()) {
|
|
return _materials[_materials.size() - 1];
|
|
}
|
|
|
|
return _materials[index];
|
|
}
|
|
|
|
void TerraMaterialCache2D::material_add(const Ref<Material> &value) {
|
|
ERR_FAIL_COND(!value.is_valid());
|
|
|
|
_materials.push_back(value);
|
|
}
|
|
|
|
void TerraMaterialCache2D::material_set(const int index, const Ref<Material> &value) {
|
|
ERR_FAIL_INDEX(index, _materials.size());
|
|
|
|
_materials.set(index, value);
|
|
}
|
|
|
|
void TerraMaterialCache2D::material_remove(const int index) {
|
|
_materials.remove(index);
|
|
}
|
|
|
|
int TerraMaterialCache2D::material_get_num() const {
|
|
return _materials.size();
|
|
}
|
|
|
|
void TerraMaterialCache2D::materials_clear() {
|
|
_materials.clear();
|
|
}
|
|
|
|
Vector<Variant> TerraMaterialCache2D::materials_get() {
|
|
VARIANT_ARRAY_GET(_materials);
|
|
}
|
|
|
|
void TerraMaterialCache2D::materials_set(const Vector<Variant> &materials) {
|
|
_materials.clear();
|
|
|
|
for (int i = 0; i < materials.size(); i++) {
|
|
Ref<Material> material = Ref<Material>(materials[i]);
|
|
|
|
_materials.push_back(material);
|
|
}
|
|
}
|
|
|
|
//Surfaces
|
|
Ref<TerraSurface2D> TerraMaterialCache2D::surface_get(const int index) {
|
|
ERR_FAIL_INDEX_V(index, _surfaces.size(), Ref<TerraSurface2D>());
|
|
|
|
return _surfaces[index];
|
|
}
|
|
Ref<TerraSurface2D> TerraMaterialCache2D::surface_id_get(const int id) {
|
|
Ref<TerraSurface2D> surface;
|
|
|
|
for (int i = 0; i < _surfaces.size(); ++i) {
|
|
surface = _surfaces[i];
|
|
|
|
if (surface.is_valid()) {
|
|
if (surface->get_id() == id) {
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
return surface;
|
|
}
|
|
void TerraMaterialCache2D::surface_add(Ref<TerraSurface2D> value) {
|
|
ERR_FAIL_COND(!value.is_valid());
|
|
|
|
_surfaces.push_back(value);
|
|
}
|
|
void TerraMaterialCache2D::surface_set(int index, Ref<TerraSurface2D> value) {
|
|
ERR_FAIL_COND(index < 0);
|
|
|
|
if (_surfaces.size() < index) {
|
|
_surfaces.resize(index + 1);
|
|
}
|
|
|
|
_surfaces.set(index, value);
|
|
}
|
|
void TerraMaterialCache2D::surface_remove(const int index) {
|
|
_surfaces.remove(index);
|
|
}
|
|
int TerraMaterialCache2D::surface_get_num() const {
|
|
return _surfaces.size();
|
|
}
|
|
void TerraMaterialCache2D::surfaces_clear() {
|
|
_surfaces.clear();
|
|
}
|
|
|
|
void TerraMaterialCache2D::additional_texture_add(const Ref<Texture> &texture) {
|
|
_additional_textures.push_back(texture);
|
|
}
|
|
void TerraMaterialCache2D::additional_texture_remove(const Ref<Texture> &texture) {
|
|
for (int i = 0; i < _additional_textures.size(); ++i) {
|
|
if (_additional_textures[i] == texture) {
|
|
_additional_textures.remove(i);
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
void TerraMaterialCache2D::additional_texture_remove_index(const int index) {
|
|
ERR_FAIL_INDEX(index, _additional_textures.size());
|
|
|
|
_additional_textures.remove(index);
|
|
}
|
|
void TerraMaterialCache2D::additional_textures_clear() {
|
|
_additional_textures.clear();
|
|
}
|
|
int TerraMaterialCache2D::additional_texture_count() {
|
|
return _additional_textures.size();
|
|
}
|
|
Ref<Texture> TerraMaterialCache2D::additional_texture_get(const int index) {
|
|
ERR_FAIL_INDEX_V(index, _additional_textures.size(), Ref<Texture>());
|
|
|
|
return _additional_textures[index];
|
|
}
|
|
Ref<AtlasTexture> TerraMaterialCache2D::additional_texture_get_atlas(const int index) {
|
|
ERR_FAIL_INDEX_V(index, _additional_textures.size(), Ref<AtlasTexture>());
|
|
|
|
return additional_texture_get_atlas_tex(_additional_textures[index]);
|
|
}
|
|
Ref<AtlasTexture> TerraMaterialCache2D::additional_texture_get_atlas_tex(const Ref<Texture> &texture) {
|
|
return Ref<AtlasTexture>();
|
|
}
|
|
Rect2 TerraMaterialCache2D::additional_texture_get_uv_rect(const Ref<Texture> &texture) {
|
|
return Rect2(0, 0, 1, 1);
|
|
}
|
|
|
|
#ifdef PROPS_PRESENT
|
|
void TerraMaterialCache2D::prop_add_textures(const Ref<PropData> &prop) {
|
|
if (!prop.is_valid()) {
|
|
return;
|
|
}
|
|
|
|
for (int i = 0; i < prop->get_prop_count(); ++i) {
|
|
#if MESH_DATA_RESOURCE_PRESENT
|
|
Ref<PropDataMeshData> pdm = prop->get_prop(i);
|
|
|
|
if (pdm.is_valid()) {
|
|
Ref<Texture> tex = pdm->get_texture();
|
|
|
|
if (!tex.is_valid())
|
|
continue;
|
|
|
|
additional_texture_add(tex);
|
|
}
|
|
#endif
|
|
|
|
Ref<PropDataProp> pdp = prop->get_prop(i);
|
|
|
|
if (pdp.is_valid()) {
|
|
prop_add_textures(pdp);
|
|
}
|
|
}
|
|
}
|
|
void TerraMaterialCache2D::prop_remove_textures(const Ref<PropData> &prop) {
|
|
if (!prop.is_valid()) {
|
|
return;
|
|
}
|
|
|
|
for (int i = 0; i < prop->get_prop_count(); ++i) {
|
|
#if MESH_DATA_RESOURCE_PRESENT
|
|
Ref<PropDataMeshData> pdm = prop->get_prop(i);
|
|
|
|
if (pdm.is_valid()) {
|
|
Ref<Texture> tex = pdm->get_texture();
|
|
|
|
if (!tex.is_valid())
|
|
continue;
|
|
|
|
additional_texture_remove(tex);
|
|
}
|
|
#endif
|
|
|
|
Ref<PropDataProp> pdp = prop->get_prop(i);
|
|
|
|
if (pdp.is_valid()) {
|
|
prop_remove_textures(pdp);
|
|
}
|
|
}
|
|
}
|
|
#endif
|
|
|
|
void TerraMaterialCache2D::refresh_rects() {
|
|
_initialized = true;
|
|
}
|
|
|
|
void TerraMaterialCache2D::setup_material_albedo(Ref<Texture> texture) {
|
|
if (has_method("_setup_material_albedo"))
|
|
call("_setup_material_albedo", texture);
|
|
}
|
|
|
|
TerraMaterialCache2D::TerraMaterialCache2D() {
|
|
_ref_count = 0;
|
|
_initialized = false;
|
|
}
|
|
|
|
TerraMaterialCache2D::~TerraMaterialCache2D() {
|
|
_materials.clear();
|
|
_surfaces.clear();
|
|
}
|
|
|
|
void TerraMaterialCache2D::_bind_methods() {
|
|
ClassDB::bind_method(D_METHOD("get_initialized"), &TerraMaterialCache2D::get_initialized);
|
|
ClassDB::bind_method(D_METHOD("set_initialized", "value"), &TerraMaterialCache2D::set_initialized);
|
|
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "initialized"), "set_initialized", "get_initialized");
|
|
|
|
ClassDB::bind_method(D_METHOD("get_ref_count"), &TerraMaterialCache2D::get_ref_count);
|
|
ClassDB::bind_method(D_METHOD("set_ref_count", "value"), &TerraMaterialCache2D::set_ref_count);
|
|
ADD_PROPERTY(PropertyInfo(Variant::INT, "mat_ref_count"), "set_ref_count", "get_ref_count");
|
|
ClassDB::bind_method(D_METHOD("inc_ref_count"), &TerraMaterialCache2D::inc_ref_count);
|
|
ClassDB::bind_method(D_METHOD("dec_ref_count"), &TerraMaterialCache2D::dec_ref_count);
|
|
|
|
BIND_VMETHOD(MethodInfo("_setup_material_albedo", PropertyInfo(Variant::OBJECT, "texture", PROPERTY_HINT_RESOURCE_TYPE, "Texture")));
|
|
|
|
ClassDB::bind_method(D_METHOD("material_get", "index"), &TerraMaterialCache2D::material_get);
|
|
ClassDB::bind_method(D_METHOD("material_lod_get", "index"), &TerraMaterialCache2D::material_lod_get);
|
|
ClassDB::bind_method(D_METHOD("material_add", "value"), &TerraMaterialCache2D::material_add);
|
|
ClassDB::bind_method(D_METHOD("material_set", "index", "value"), &TerraMaterialCache2D::material_set);
|
|
ClassDB::bind_method(D_METHOD("material_remove", "index"), &TerraMaterialCache2D::material_remove);
|
|
ClassDB::bind_method(D_METHOD("material_get_num"), &TerraMaterialCache2D::material_get_num);
|
|
ClassDB::bind_method(D_METHOD("materials_clear"), &TerraMaterialCache2D::materials_clear);
|
|
|
|
ClassDB::bind_method(D_METHOD("materials_get"), &TerraMaterialCache2D::materials_get);
|
|
ClassDB::bind_method(D_METHOD("materials_set"), &TerraMaterialCache2D::materials_set);
|
|
ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "materials", PROPERTY_HINT_NONE, "17/17:Material", PROPERTY_USAGE_DEFAULT, "Material"), "materials_set", "materials_get");
|
|
|
|
ClassDB::bind_method(D_METHOD("surface_get", "index"), &TerraMaterialCache2D::surface_get);
|
|
ClassDB::bind_method(D_METHOD("surface_id_get", "index"), &TerraMaterialCache2D::surface_id_get);
|
|
|
|
ClassDB::bind_method(D_METHOD("surface_add", "value"), &TerraMaterialCache2D::surface_add);
|
|
ClassDB::bind_method(D_METHOD("surface_set", "index", "surface"), &TerraMaterialCache2D::surface_set);
|
|
ClassDB::bind_method(D_METHOD("surface_remove", "index"), &TerraMaterialCache2D::surface_remove);
|
|
ClassDB::bind_method(D_METHOD("surface_get_num"), &TerraMaterialCache2D::surface_get_num);
|
|
ClassDB::bind_method(D_METHOD("surfaces_clear"), &TerraMaterialCache2D::surfaces_clear);
|
|
|
|
ClassDB::bind_method(D_METHOD("additional_texture_add", "texture"), &TerraMaterialCache2D::additional_texture_add);
|
|
ClassDB::bind_method(D_METHOD("additional_texture_remove", "texture"), &TerraMaterialCache2D::additional_texture_remove);
|
|
ClassDB::bind_method(D_METHOD("additional_texture_remove_index", "index"), &TerraMaterialCache2D::additional_texture_remove_index);
|
|
ClassDB::bind_method(D_METHOD("additional_textures_clear"), &TerraMaterialCache2D::additional_textures_clear);
|
|
ClassDB::bind_method(D_METHOD("additional_texture_count"), &TerraMaterialCache2D::additional_texture_count);
|
|
ClassDB::bind_method(D_METHOD("additional_texture_get", "index"), &TerraMaterialCache2D::additional_texture_get);
|
|
ClassDB::bind_method(D_METHOD("additional_texture_get_atlas", "index"), &TerraMaterialCache2D::additional_texture_get_atlas);
|
|
ClassDB::bind_method(D_METHOD("additional_texture_get_atlas_tex", "index"), &TerraMaterialCache2D::additional_texture_get_atlas_tex);
|
|
ClassDB::bind_method(D_METHOD("additional_texture_get_uv_rect", "texture"), &TerraMaterialCache2D::additional_texture_get_uv_rect);
|
|
|
|
#ifdef PROPS_PRESENT
|
|
ClassDB::bind_method(D_METHOD("prop_add_textures", "prop"), &TerraMaterialCache2D::prop_add_textures);
|
|
ClassDB::bind_method(D_METHOD("prop_remove_textures", "prop"), &TerraMaterialCache2D::prop_remove_textures);
|
|
#endif
|
|
|
|
ClassDB::bind_method(D_METHOD("refresh_rects"), &TerraMaterialCache2D::refresh_rects);
|
|
|
|
ClassDB::bind_method(D_METHOD("setup_material_albedo", "texture"), &TerraMaterialCache2D::setup_material_albedo);
|
|
}
|