aboutsummaryrefslogtreecommitdiff
path: root/extension/src/openvic-extension/classes
diff options
context:
space:
mode:
Diffstat (limited to 'extension/src/openvic-extension/classes')
-rw-r--r--extension/src/openvic-extension/classes/GFXIconTexture.cpp112
-rw-r--r--extension/src/openvic-extension/classes/GFXIconTexture.hpp52
-rw-r--r--extension/src/openvic-extension/classes/MapMesh.cpp163
-rw-r--r--extension/src/openvic-extension/classes/MapMesh.hpp34
4 files changed, 361 insertions, 0 deletions
diff --git a/extension/src/openvic-extension/classes/GFXIconTexture.cpp b/extension/src/openvic-extension/classes/GFXIconTexture.cpp
new file mode 100644
index 0000000..9edfb1b
--- /dev/null
+++ b/extension/src/openvic-extension/classes/GFXIconTexture.cpp
@@ -0,0 +1,112 @@
+#include "GFXIconTexture.hpp"
+
+#include <godot_cpp/variant/utility_functions.hpp>
+
+#include "openvic-extension/singletons/AssetManager.hpp"
+#include "openvic-extension/singletons/GameSingleton.hpp"
+#include "openvic-extension/utility/ClassBindings.hpp"
+#include "openvic-extension/utility/Utilities.hpp"
+
+using namespace godot;
+using namespace OpenVic;
+
+using OpenVic::Utilities::std_view_to_godot_string;
+using OpenVic::Utilities::std_view_to_godot_string_name;
+
+void GFXIconTexture::_bind_methods() {
+ OV_BIND_METHOD(GFXIconTexture::clear);
+
+ OV_BIND_METHOD(GFXIconTexture::set_gfx_texture_sprite_name, { "gfx_texture_sprite_name" }, DEFVAL(GFX::NO_FRAMES));
+ OV_BIND_METHOD(GFXIconTexture::get_gfx_texture_sprite_name);
+
+ OV_BIND_METHOD(GFXIconTexture::set_icon_index, { "new_icon_index" });
+ OV_BIND_METHOD(GFXIconTexture::get_icon_index);
+ OV_BIND_METHOD(GFXIconTexture::get_icon_count);
+
+ ADD_PROPERTY(PropertyInfo(Variant::INT, "icon_index"), "set_icon_index", "get_icon_index");
+}
+
+GFXIconTexture::GFXIconTexture()
+ : gfx_texture_sprite { nullptr }, icon_index { GFX::NO_FRAMES }, icon_count { GFX::NO_FRAMES } {}
+
+Ref<GFXIconTexture> GFXIconTexture::make_gfx_icon_texture(GFX::TextureSprite const* gfx_texture_sprite, GFX::frame_t icon) {
+ Ref<GFXIconTexture> icon_texture;
+ icon_texture.instantiate();
+ ERR_FAIL_NULL_V(icon_texture, icon_texture);
+ icon_texture->set_gfx_texture_sprite(gfx_texture_sprite, icon);
+ return icon_texture;
+}
+
+void GFXIconTexture::clear() {
+ gfx_texture_sprite = nullptr;
+ set_atlas(nullptr);
+ set_region({});
+ icon_index = GFX::NO_FRAMES;
+ icon_count = GFX::NO_FRAMES;
+}
+
+Error GFXIconTexture::set_gfx_texture_sprite(GFX::TextureSprite const* new_gfx_texture_sprite, GFX::frame_t icon) {
+ if (gfx_texture_sprite != new_gfx_texture_sprite) {
+ if (new_gfx_texture_sprite == nullptr) {
+ clear();
+ return OK;
+ }
+ AssetManager* asset_manager = AssetManager::get_singleton();
+ ERR_FAIL_NULL_V(asset_manager, FAILED);
+ const StringName texture_file = std_view_to_godot_string_name(new_gfx_texture_sprite->get_texture_file());
+ const Ref<ImageTexture> texture = asset_manager->get_texture(texture_file);
+ ERR_FAIL_NULL_V_MSG(texture, FAILED, "Failed to load texture: " + texture_file);
+ gfx_texture_sprite = new_gfx_texture_sprite;
+ set_atlas(texture);
+ icon_index = GFX::NO_FRAMES;
+ icon_count = gfx_texture_sprite->get_no_of_frames();
+ }
+ return set_icon_index(icon);
+}
+
+Error GFXIconTexture::set_gfx_texture_sprite_name(String const& gfx_texture_sprite_name, GFX::frame_t icon) {
+ if (gfx_texture_sprite_name.is_empty()) {
+ return set_gfx_texture_sprite(nullptr);
+ }
+ GameSingleton* game_singleton = GameSingleton::get_singleton();
+ ERR_FAIL_NULL_V(game_singleton, FAILED);
+ GFX::Sprite const* sprite = game_singleton->get_gfx_sprite(gfx_texture_sprite_name);
+ ERR_FAIL_NULL_V_MSG(sprite, FAILED, "GFX sprite not found: " + gfx_texture_sprite_name);
+ GFX::TextureSprite const* new_texture_sprite = sprite->cast_to<GFX::TextureSprite>();
+ ERR_FAIL_NULL_V_MSG(
+ new_texture_sprite, FAILED, "Invalid type for GFX sprite " + gfx_texture_sprite_name + ": " +
+ std_view_to_godot_string(sprite->get_type()) + " (expected " +
+ std_view_to_godot_string(GFX::TextureSprite::get_type_static()) + ")"
+ );
+ return set_gfx_texture_sprite(new_texture_sprite, icon);
+}
+
+String GFXIconTexture::get_gfx_texture_sprite_name() const {
+ return gfx_texture_sprite != nullptr ? std_view_to_godot_string(gfx_texture_sprite->get_name()) : String {};
+}
+
+Error GFXIconTexture::set_icon_index(int32_t new_icon_index) {
+ const Ref<Texture2D> atlas_texture = get_atlas();
+ ERR_FAIL_NULL_V(atlas_texture, FAILED);
+ const Vector2 size = atlas_texture->get_size();
+ if (icon_count <= GFX::NO_FRAMES) {
+ if (new_icon_index > GFX::NO_FRAMES) {
+ UtilityFunctions::push_warning("Invalid icon index ", new_icon_index, " for texture with no frames!");
+ }
+ icon_index = GFX::NO_FRAMES;
+ set_region({ {}, size });
+ return OK;
+ }
+ if (GFX::NO_FRAMES < new_icon_index && new_icon_index <= icon_count) {
+ icon_index = new_icon_index;
+ } else {
+ icon_index = icon_count;
+ if (new_icon_index > icon_count) {
+ UtilityFunctions::push_warning(
+ "Invalid icon index ", new_icon_index, " out of count ", icon_count, " - defaulting to ", icon_index
+ );
+ }
+ }
+ set_region({ (icon_index - 1) * size.x / icon_count, 0, size.x / icon_count, size.y });
+ return OK;
+}
diff --git a/extension/src/openvic-extension/classes/GFXIconTexture.hpp b/extension/src/openvic-extension/classes/GFXIconTexture.hpp
new file mode 100644
index 0000000..3ed5b3e
--- /dev/null
+++ b/extension/src/openvic-extension/classes/GFXIconTexture.hpp
@@ -0,0 +1,52 @@
+#pragma once
+
+#include <godot_cpp/classes/atlas_texture.hpp>
+
+#include <openvic-simulation/interface/GFX.hpp>
+
+namespace OpenVic {
+ class GFXIconTexture : public godot::AtlasTexture {
+ GDCLASS(GFXIconTexture, godot::AtlasTexture)
+
+ /* PROPERTY automatically defines getter functions:
+ * - get_gfx_texture_sprite
+ * - get_icon_index
+ * - get_icon_count */
+ GFX::TextureSprite const* PROPERTY(gfx_texture_sprite);
+ GFX::frame_t PROPERTY(icon_index);
+ GFX::frame_t PROPERTY(icon_count);
+
+ protected:
+ static void _bind_methods();
+
+ public:
+ GFXIconTexture();
+
+ static godot::Ref<GFXIconTexture> make_gfx_icon_texture(
+ GFX::TextureSprite const* gfx_texture_sprite, GFX::frame_t icon = GFX::NO_FRAMES
+ );
+
+ /* Discard the GFX::TextureSprite, atlas texture and icon index. */
+ void clear();
+
+ /* Set the GFX::TextureSprite, load its texture as an atlas and set its displayed icon */
+ godot::Error set_gfx_texture_sprite(
+ GFX::TextureSprite const* new_gfx_texture_sprite, GFX::frame_t icon = GFX::NO_FRAMES
+ );
+
+ /* Search for a GFX::TextureSprite with the specfied name and,
+ * if successful, call set_gfx_texture_sprite to set it and its icon */
+ godot::Error set_gfx_texture_sprite_name(
+ godot::String const& gfx_texture_sprite_name, GFX::frame_t icon = GFX::NO_FRAMES
+ );
+
+ /* Return the name of the GFX::TextureSprite, or an empty String if it's null */
+ godot::String get_gfx_texture_sprite_name() const;
+
+ /* Set icon_index to a value between one and icon_count (inclusive), and update the AtlasTexture's region
+ * to display that frame. An index of zero can be used if gfx_texture_sprite has no frames (zero icon_count).
+ * If zero is used but icon_count is non-zero, icon_index defaults to icon_count (the last frame,
+ * not the first frame because it is often empty). */
+ godot::Error set_icon_index(GFX::frame_t new_icon_index);
+ };
+}
diff --git a/extension/src/openvic-extension/classes/MapMesh.cpp b/extension/src/openvic-extension/classes/MapMesh.cpp
new file mode 100644
index 0000000..a557105
--- /dev/null
+++ b/extension/src/openvic-extension/classes/MapMesh.cpp
@@ -0,0 +1,163 @@
+#include "MapMesh.hpp"
+
+#include <godot_cpp/templates/vector.hpp>
+
+#include "openvic-extension/utility/ClassBindings.hpp"
+
+using namespace godot;
+using namespace OpenVic;
+
+void MapMesh::_bind_methods() {
+ OV_BIND_METHOD(MapMesh::set_aspect_ratio, { "ratio" });
+ OV_BIND_METHOD(MapMesh::get_aspect_ratio);
+
+ OV_BIND_METHOD(MapMesh::set_repeat_proportion, { "proportion" });
+ OV_BIND_METHOD(MapMesh::get_repeat_proportion);
+
+ OV_BIND_METHOD(MapMesh::set_subdivide_width, { "divisions" });
+ OV_BIND_METHOD(MapMesh::get_subdivide_width);
+
+ OV_BIND_METHOD(MapMesh::set_subdivide_depth, { "divisions" });
+ OV_BIND_METHOD(MapMesh::get_subdivide_depth);
+
+ OV_BIND_METHOD(MapMesh::get_core_aabb);
+ OV_BIND_METHOD(MapMesh::is_valid_uv_coord);
+
+ ADD_PROPERTY(
+ PropertyInfo(Variant::FLOAT, "aspect_ratio", PROPERTY_HINT_NONE, "suffix:m"), "set_aspect_ratio", "get_aspect_ratio"
+ );
+ ADD_PROPERTY(
+ PropertyInfo(Variant::FLOAT, "repeat_proportion", PROPERTY_HINT_NONE, "suffix:m"), "set_repeat_proportion",
+ "get_repeat_proportion"
+ );
+ ADD_PROPERTY(
+ PropertyInfo(Variant::INT, "subdivide_width", PROPERTY_HINT_RANGE, "0,100,1,or_greater"), "set_subdivide_width",
+ "get_subdivide_width"
+ );
+ ADD_PROPERTY(
+ PropertyInfo(Variant::INT, "subdivide_depth", PROPERTY_HINT_RANGE, "0,100,1,or_greater"), "set_subdivide_depth",
+ "get_subdivide_depth"
+ );
+}
+
+void MapMesh::_request_update() {
+ // Hack to trigger _update_lightmap_size and _request_update in PrimitiveMesh
+ set_add_uv2(get_add_uv2());
+}
+
+void MapMesh::set_aspect_ratio(const float ratio) {
+ aspect_ratio = ratio;
+ _request_update();
+}
+
+float MapMesh::get_aspect_ratio() const {
+ return aspect_ratio;
+}
+
+void MapMesh::set_repeat_proportion(const float proportion) {
+ repeat_proportion = proportion;
+ _request_update();
+}
+
+float MapMesh::get_repeat_proportion() const {
+ return repeat_proportion;
+}
+
+void MapMesh::set_subdivide_width(const int32_t divisions) {
+ subdivide_w = divisions > 0 ? divisions : 0;
+ _request_update();
+}
+
+int32_t MapMesh::get_subdivide_width() const {
+ return subdivide_w;
+}
+
+void MapMesh::set_subdivide_depth(const int32_t divisions) {
+ subdivide_d = divisions > 0 ? divisions : 0;
+ _request_update();
+}
+
+int32_t MapMesh::get_subdivide_depth() const {
+ return subdivide_d;
+}
+
+AABB MapMesh::get_core_aabb() const {
+ const Vector3 size { aspect_ratio, 0.0f, 1.0f };
+ return AABB { size * -0.5f, size };
+}
+
+bool MapMesh::is_valid_uv_coord(godot::Vector2 const& uv) const {
+ return 0.0f <= uv.y && uv.y <= 1.0f;
+}
+
+Array MapMesh::_create_mesh_array() const {
+ Array arr;
+ arr.resize(Mesh::ARRAY_MAX);
+
+ const int32_t vertex_count = (subdivide_w + 2) * (subdivide_d + 2);
+ const int32_t indice_count = (subdivide_w + 1) * (subdivide_d + 1) * 6;
+
+ PackedVector3Array points;
+ PackedVector3Array normals;
+ PackedFloat32Array tangents;
+ PackedVector2Array uvs;
+ PackedInt32Array indices;
+
+ points.resize(vertex_count);
+ normals.resize(vertex_count);
+ tangents.resize(vertex_count * 4);
+ uvs.resize(vertex_count);
+ indices.resize(indice_count);
+
+ static const Vector3 normal { 0.0f, 1.0f, 0.0f };
+ const Size2 uv_size { 1.0f + 2.0f * repeat_proportion, 1.0f };
+ const Size2 size { aspect_ratio * uv_size.x, uv_size.y }, start_pos = size * -0.5f;
+
+ int32_t point_index = 0, thisrow = 0, prevrow = 0, indice_index = 0;
+ Vector2 subdivide_step { 1.0f / (subdivide_w + 1.0f), 1.0f / (subdivide_d + 1.0f) };
+ Vector3 point { 0.0f, 0.0f, start_pos.y };
+ Vector2 point_step = subdivide_step * size;
+ Vector2 uv {}, uv_step = subdivide_step * uv_size;
+
+ for (int32_t j = 0; j <= subdivide_d + 1; ++j) {
+ point.x = start_pos.x;
+ uv.x = -repeat_proportion;
+
+ for (int32_t i = 0; i <= subdivide_w + 1; ++i) {
+ points[point_index] = point;
+ normals[point_index] = normal;
+ tangents[point_index * 4 + 0] = 1.0f;
+ tangents[point_index * 4 + 1] = 0.0f;
+ tangents[point_index * 4 + 2] = 0.0f;
+ tangents[point_index * 4 + 3] = 1.0f;
+ uvs[point_index] = uv;
+ point_index++;
+
+ if (i > 0 && j > 0) {
+ indices[indice_index + 0] = prevrow + i - 1;
+ indices[indice_index + 1] = prevrow + i;
+ indices[indice_index + 2] = thisrow + i - 1;
+ indices[indice_index + 3] = prevrow + i;
+ indices[indice_index + 4] = thisrow + i;
+ indices[indice_index + 5] = thisrow + i - 1;
+ indice_index += 6;
+ }
+
+ point.x += point_step.x;
+ uv.x += uv_step.x;
+ }
+
+ point.z += point_step.y;
+ uv.y += uv_step.y;
+ prevrow = thisrow;
+ thisrow = point_index;
+ }
+
+ arr[Mesh::ARRAY_VERTEX] = points;
+ arr[Mesh::ARRAY_NORMAL] = normals;
+ arr[Mesh::ARRAY_TANGENT] = tangents;
+ arr[Mesh::ARRAY_TEX_UV] = uvs;
+ arr[Mesh::ARRAY_INDEX] = indices;
+
+ return arr;
+}
diff --git a/extension/src/openvic-extension/classes/MapMesh.hpp b/extension/src/openvic-extension/classes/MapMesh.hpp
new file mode 100644
index 0000000..38b208c
--- /dev/null
+++ b/extension/src/openvic-extension/classes/MapMesh.hpp
@@ -0,0 +1,34 @@
+#pragma once
+
+#include <godot_cpp/classes/primitive_mesh.hpp>
+
+namespace OpenVic {
+ class MapMesh : public godot::PrimitiveMesh {
+ GDCLASS(MapMesh, godot::PrimitiveMesh)
+
+ float aspect_ratio = 2.0f, repeat_proportion = 0.5f;
+ int32_t subdivide_w = 0, subdivide_d = 0;
+
+ protected:
+ static void _bind_methods();
+ void _request_update();
+
+ public:
+ void set_aspect_ratio(const float ratio);
+ float get_aspect_ratio() const;
+
+ void set_repeat_proportion(const float proportion);
+ float get_repeat_proportion() const;
+
+ void set_subdivide_width(const int32_t divisions);
+ int32_t get_subdivide_width() const;
+
+ void set_subdivide_depth(const int32_t divisions);
+ int32_t get_subdivide_depth() const;
+
+ godot::AABB get_core_aabb() const;
+ bool is_valid_uv_coord(godot::Vector2 const& uv) const;
+
+ godot::Array _create_mesh_array() const override;
+ };
+}