aboutsummaryrefslogtreecommitdiff
path: root/extension/src/openvic-extension/singletons
diff options
context:
space:
mode:
author Hop311 <Hop3114@gmail.com>2024-08-30 23:30:12 +0200
committer GitHub <noreply@github.com>2024-08-30 23:30:12 +0200
commit2e0bc5b556b9c6df46a8cdd48d3f109e0ac76b63 (patch)
tree2c521b99fe6cd0a7fd5d6a29e55645b3415792b3 /extension/src/openvic-extension/singletons
parentf54e454afb90f8868e7c62529e2a388fdaadf20b (diff)
parentbdc2ba527bc02e7cdf977f6040f2ca85aa4f9a94 (diff)
Merge pull request #253 from OpenVicProject/tooltip
Tooltips
Diffstat (limited to 'extension/src/openvic-extension/singletons')
-rw-r--r--extension/src/openvic-extension/singletons/AssetManager.cpp23
-rw-r--r--extension/src/openvic-extension/singletons/AssetManager.hpp6
-rw-r--r--extension/src/openvic-extension/singletons/MenuSingleton.cpp35
-rw-r--r--extension/src/openvic-extension/singletons/MenuSingleton.hpp14
4 files changed, 77 insertions, 1 deletions
diff --git a/extension/src/openvic-extension/singletons/AssetManager.cpp b/extension/src/openvic-extension/singletons/AssetManager.cpp
index d17edd0..3b37c8c 100644
--- a/extension/src/openvic-extension/singletons/AssetManager.cpp
+++ b/extension/src/openvic-extension/singletons/AssetManager.cpp
@@ -120,6 +120,29 @@ Ref<ImageTexture> AssetManager::get_texture(StringName const& path, LoadFlags lo
}
}
+Ref<StyleBoxTexture> AssetManager::make_stylebox_texture(Ref<Texture2D> const& texture, Vector2 const& border) {
+ ERR_FAIL_NULL_V(texture, nullptr);
+
+ Ref<StyleBoxTexture> stylebox;
+ stylebox.instantiate();
+ ERR_FAIL_NULL_V(stylebox, nullptr);
+
+ stylebox->set_texture(texture);
+
+ static const StringName changed_signal = "changed";
+ static const StringName emit_changed_func = "emit_changed";
+ texture->connect(changed_signal, Callable { *stylebox, emit_changed_func }, Object::CONNECT_PERSIST);
+
+ if (border != Vector2 {}) {
+ stylebox->set_texture_margin(SIDE_LEFT, border.x);
+ stylebox->set_texture_margin(SIDE_RIGHT, border.x);
+ stylebox->set_texture_margin(SIDE_TOP, border.y);
+ stylebox->set_texture_margin(SIDE_BOTTOM, border.y);
+ }
+
+ return stylebox;
+}
+
Ref<FontFile> AssetManager::get_font(StringName const& name) {
const font_map_t::const_iterator it = fonts.find(name);
if (it != fonts.end()) {
diff --git a/extension/src/openvic-extension/singletons/AssetManager.hpp b/extension/src/openvic-extension/singletons/AssetManager.hpp
index deca309..96cb880 100644
--- a/extension/src/openvic-extension/singletons/AssetManager.hpp
+++ b/extension/src/openvic-extension/singletons/AssetManager.hpp
@@ -3,7 +3,9 @@
#include <godot_cpp/classes/atlas_texture.hpp>
#include <godot_cpp/classes/font_file.hpp>
#include <godot_cpp/classes/image_texture.hpp>
+#include <godot_cpp/classes/style_box_texture.hpp>
#include <godot_cpp/core/class_db.hpp>
+#include <godot_cpp/variant/vector2.hpp>
#include <openvic-simulation/interface/GFXSprite.hpp>
@@ -68,6 +70,10 @@ namespace OpenVic {
godot::StringName const& path, LoadFlags load_flags = LOAD_FLAG_CACHE_TEXTURE
);
+ static godot::Ref<godot::StyleBoxTexture> make_stylebox_texture(
+ godot::Ref<godot::Texture2D> const& texture, godot::Vector2 const& border = {}
+ );
+
/* Search for and load a font with the specified name from the game defines' font directory, first checking the
* AssetManager's font cache in case it has already been loaded, and returning nullptr if font loading fails. */
godot::Ref<godot::FontFile> get_font(godot::StringName const& name);
diff --git a/extension/src/openvic-extension/singletons/MenuSingleton.cpp b/extension/src/openvic-extension/singletons/MenuSingleton.cpp
index 7a5f47f..367462b 100644
--- a/extension/src/openvic-extension/singletons/MenuSingleton.cpp
+++ b/extension/src/openvic-extension/singletons/MenuSingleton.cpp
@@ -30,6 +30,10 @@ StringName const& MenuSingleton::_signal_search_cache_changed() {
static const StringName signal_search_cache_changed = "search_cache_changed";
return signal_search_cache_changed;
}
+StringName const& MenuSingleton::_signal_update_tooltip() {
+ static const StringName signal_update_tooltip = "update_tooltip";
+ return signal_update_tooltip;
+}
String MenuSingleton::get_state_name(State const& state) const {
StateSet const& state_set = state.get_state_set();
@@ -103,6 +107,16 @@ String MenuSingleton::get_country_adjective(CountryInstance const& country) cons
}
void MenuSingleton::_bind_methods() {
+ /* TOOLTIP */
+ OV_BIND_METHOD(MenuSingleton::show_tooltip, { "text", "substitution_dict", "position" });
+ OV_BIND_METHOD(MenuSingleton::show_control_tooltip, { "text", "substitution_dict", "control" });
+ OV_BIND_METHOD(MenuSingleton::hide_tooltip);
+
+ ADD_SIGNAL(MethodInfo(
+ _signal_update_tooltip(), PropertyInfo(Variant::STRING, "text"),
+ PropertyInfo(Variant::DICTIONARY, "substitution_dict"), PropertyInfo(Variant::VECTOR2, "position")
+ ));
+
/* PROVINCE OVERVIEW PANEL */
OV_BIND_METHOD(MenuSingleton::get_province_info_from_index, { "index" });
OV_BIND_METHOD(MenuSingleton::get_province_building_count);
@@ -205,6 +219,25 @@ MenuSingleton::~MenuSingleton() {
singleton = nullptr;
}
+/* TOOLTIP */
+
+void MenuSingleton::show_tooltip(String const& text, Dictionary const& substitution_dict, Vector2 const& position) {
+ emit_signal(_signal_update_tooltip(), text, substitution_dict, position);
+}
+
+void MenuSingleton::show_control_tooltip(String const& text, Dictionary const& substitution_dict, Control const* control) {
+ ERR_FAIL_NULL(control);
+
+ using namespace OpenVic::Utilities::literals;
+ static const Vector2 offset { 0.0_real, 64.0_real };
+
+ show_tooltip(text, substitution_dict, control->get_global_position() + offset);
+}
+
+void MenuSingleton::hide_tooltip() {
+ show_tooltip({}, {}, {});
+}
+
/* PROVINCE OVERVIEW PANEL */
static TypedArray<Dictionary> _make_buildings_dict_array(
@@ -561,7 +594,7 @@ Error MenuSingleton::generate_search_cache() {
return OK;
}
-void MenuSingleton::update_search_results(godot::String const& text) {
+void MenuSingleton::update_search_results(String const& text) {
// Sanatise input
const String search_text = text.strip_edges().to_lower();
diff --git a/extension/src/openvic-extension/singletons/MenuSingleton.hpp b/extension/src/openvic-extension/singletons/MenuSingleton.hpp
index 190e3ea..022bce5 100644
--- a/extension/src/openvic-extension/singletons/MenuSingleton.hpp
+++ b/extension/src/openvic-extension/singletons/MenuSingleton.hpp
@@ -1,5 +1,6 @@
#pragma once
+#include <godot_cpp/classes/control.hpp>
#include <godot_cpp/classes/image.hpp>
#include <openvic-simulation/pop/Pop.hpp>
@@ -95,6 +96,10 @@ namespace OpenVic {
static godot::StringName const& _signal_population_menu_pops_changed();
/* Emitted when the collection of possible search results changes. */
static godot::StringName const& _signal_search_cache_changed();
+ /* Emitted when the current tooltip changes. Arguments: text (godot::String), substitution_dict (godot::Dictionary),
+ * position (godot::Vector2). If text is empty then the tooltip will be hidden, otherwise the text will be shown at
+ * the given position. */
+ static godot::StringName const& _signal_update_tooltip();
godot::String get_state_name(State const& state) const;
godot::String get_country_name(CountryInstance const& country) const;
@@ -110,6 +115,15 @@ namespace OpenVic {
MenuSingleton();
~MenuSingleton();
+ /* TOOLTIP */
+ void show_tooltip(
+ godot::String const& text, godot::Dictionary const& substitution_dict, godot::Vector2 const& position
+ );
+ void show_control_tooltip(
+ godot::String const& text, godot::Dictionary const& substitution_dict, godot::Control const* control
+ );
+ void hide_tooltip();
+
/* PROVINCE OVERVIEW PANEL */
/* Get info to display in Province Overview Panel, packaged in a Dictionary using StringName constants as keys. */
godot::Dictionary get_province_info_from_index(int32_t index) const;