aboutsummaryrefslogtreecommitdiff
path: root/extension/src
diff options
context:
space:
mode:
Diffstat (limited to 'extension/src')
-rw-r--r--extension/src/MapSingleton.cpp35
-rw-r--r--extension/src/MapSingleton.hpp5
2 files changed, 33 insertions, 7 deletions
diff --git a/extension/src/MapSingleton.cpp b/extension/src/MapSingleton.cpp
index 31cbd98..8c38926 100644
--- a/extension/src/MapSingleton.cpp
+++ b/extension/src/MapSingleton.cpp
@@ -14,7 +14,8 @@ void MapSingleton::_bind_methods() {
ClassDB::bind_method(D_METHOD("load_region_file", "file_path"), &MapSingleton::load_region_file);
ClassDB::bind_method(D_METHOD("load_province_shape_file", "file_path"), &MapSingleton::load_province_shape_file);
- ClassDB::bind_method(D_METHOD("get_province_identifier_from_pixel_coords", "coords"), &MapSingleton::get_province_identifier_from_pixel_coords);
+ ClassDB::bind_method(D_METHOD("get_province_index_from_uv_coords", "coords"), &MapSingleton::get_province_index_from_uv_coords);
+ ClassDB::bind_method(D_METHOD("get_province_identifier_from_uv_coords", "coords"), &MapSingleton::get_province_identifier_from_uv_coords);
ClassDB::bind_method(D_METHOD("get_width"), &MapSingleton::get_width);
ClassDB::bind_method(D_METHOD("get_height"), &MapSingleton::get_height);
ClassDB::bind_method(D_METHOD("get_province_index_image"), &MapSingleton::get_province_index_image);
@@ -235,15 +236,37 @@ Error MapSingleton::load_province_shape_file(String const& file_path) {
return err;
}
-String MapSingleton::get_province_identifier_from_pixel_coords(Vector2i const& coords) const {
+Province* MapSingleton::get_province_from_uv_coords(godot::Vector2 const& coords) {
if (province_index_image.is_valid()) {
const PackedByteArray index_data_array = province_index_image->get_data();
Province::index_t const* index_data = reinterpret_cast<Province::index_t const*>(index_data_array.ptr());
- const int32_t x_mod_w = UtilityFunctions::posmod(coords.x, get_width());
- const int32_t y_mod_h = UtilityFunctions::posmod(coords.y, get_height());
- Province const* province = map.get_province_by_index(index_data[x_mod_w + y_mod_h * get_width()]);
- if (province != nullptr) return province->get_identifier().c_str();
+ const int32_t x_mod_w = UtilityFunctions::posmod(coords.x, 1.0f) * get_width();
+ const int32_t y_mod_h = UtilityFunctions::posmod(coords.y, 1.0f) * get_height();
+ return map.get_province_by_index(index_data[x_mod_w + y_mod_h * get_width()]);
}
+ return nullptr;
+}
+
+Province const* MapSingleton::get_province_from_uv_coords(godot::Vector2 const& coords) const {
+ if (province_index_image.is_valid()) {
+ const PackedByteArray index_data_array = province_index_image->get_data();
+ Province::index_t const* index_data = reinterpret_cast<Province::index_t const*>(index_data_array.ptr());
+ const int32_t x_mod_w = UtilityFunctions::posmod(coords.x, 1.0f) * get_width();
+ const int32_t y_mod_h = UtilityFunctions::posmod(coords.y, 1.0f) * get_height();
+ return map.get_province_by_index(index_data[x_mod_w + y_mod_h * get_width()]);
+ }
+ return nullptr;
+}
+
+int32_t MapSingleton::get_province_index_from_uv_coords(Vector2 const& coords) const {
+ Province const* province = get_province_from_uv_coords(coords);
+ if (province != nullptr) return province->get_index();
+ return Province::NULL_INDEX;
+}
+
+String MapSingleton::get_province_identifier_from_uv_coords(Vector2 const& coords) const {
+ Province const* province = get_province_from_uv_coords(coords);
+ if (province != nullptr) return province->get_identifier().c_str();
return String{};
}
diff --git a/extension/src/MapSingleton.hpp b/extension/src/MapSingleton.hpp
index 7d50d55..9205e92 100644
--- a/extension/src/MapSingleton.hpp
+++ b/extension/src/MapSingleton.hpp
@@ -35,7 +35,10 @@ namespace OpenVic2 {
godot::Error load_region_file(godot::String const& file_path);
godot::Error load_province_shape_file(godot::String const& file_path);
- godot::String get_province_identifier_from_pixel_coords(godot::Vector2i const& coords) const;
+ Province* get_province_from_uv_coords(godot::Vector2 const& coords);
+ Province const* get_province_from_uv_coords(godot::Vector2 const& coords) const;
+ int32_t get_province_index_from_uv_coords(godot::Vector2 const& coords) const;
+ godot::String get_province_identifier_from_uv_coords(godot::Vector2 const& coords) const;
int32_t get_width() const;
int32_t get_height() const;
godot::Ref<godot::Image> get_province_index_image() const;