Skip to content

Commit

Permalink
Added colour-modulated button state textures + block colour progress …
Browse files Browse the repository at this point in the history
…bars
  • Loading branch information
Hop311 committed Dec 26, 2023
1 parent d114eca commit 12d4102
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 18 deletions.
94 changes: 77 additions & 17 deletions extension/src/openvic-extension/utility/UITools.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,24 +139,68 @@ static bool generate_icon(generate_gui_args_t&& args) {
godot_progress_bar, false, vformat("Failed to create TextureProgressBar for GUI icon %s", icon_name)
);

const StringName back_texture_file =
std_view_to_godot_string_name(icon.get_sprite()->cast_to<GFX::ProgressBar>()->get_back_texture_file());
const Ref<ImageTexture> back_texture = args.asset_manager.get_texture(back_texture_file);
GFX::ProgressBar const* progress_bar = icon.get_sprite()->cast_to<GFX::ProgressBar>();

Ref<ImageTexture> back_texture;
if (!progress_bar->get_back_texture_file().empty()) {
const StringName back_texture_file = std_view_to_godot_string_name(progress_bar->get_back_texture_file());
back_texture = args.asset_manager.get_texture(back_texture_file);
if (back_texture.is_null()) {
UtilityFunctions::push_error(
"Failed to load progress bar sprite back texture ", back_texture_file, " for GUI icon ", icon_name
);
ret = false;
}
}
if (back_texture.is_null()) {
const Color back_colour = Utilities::to_godot_color(progress_bar->get_back_colour());
back_texture = Utilities::make_solid_colour_texture(
back_colour, progress_bar->get_size().x, progress_bar->get_size().y
);
if (back_texture.is_null()) {
UtilityFunctions::push_error(
"Failed to generate progress bar sprite ", back_colour, " back texture for GUI icon ", icon_name
);
ret = false;
}
}
if (back_texture.is_valid()) {
godot_progress_bar->set_under_texture(back_texture);
} else {
UtilityFunctions::push_error("Failed to load progress bar base sprite ", back_texture_file, " for GUI icon ", icon_name);
UtilityFunctions::push_error(
"Failed to create and set progress bar sprite back texture for GUI icon ", icon_name
);
ret = false;
}

const StringName progress_texture_file =
std_view_to_godot_string_name(icon.get_sprite()->cast_to<GFX::ProgressBar>()->get_progress_texture_file());
const Ref<ImageTexture> progress_texture = args.asset_manager.get_texture(progress_texture_file);
Ref<ImageTexture> progress_texture;
if (!progress_bar->get_progress_texture_file().empty()) {
const StringName progress_texture_file = std_view_to_godot_string_name(progress_bar->get_progress_texture_file());
progress_texture = args.asset_manager.get_texture(progress_texture_file);
if (progress_texture.is_null()) {
UtilityFunctions::push_error(
"Failed to load progress bar sprite progress texture ", progress_texture_file, " for GUI icon ", icon_name
);
ret = false;
}
}
if (progress_texture.is_null()) {
const Color progress_colour = Utilities::to_godot_color(progress_bar->get_progress_colour());
progress_texture = Utilities::make_solid_colour_texture(
progress_colour, progress_bar->get_size().x, progress_bar->get_size().y
);
if (progress_texture.is_null()) {
UtilityFunctions::push_error(
"Failed to generate progress bar sprite ", progress_colour, " progress texture for GUI icon ", icon_name
);
ret = false;
}
}
if (progress_texture.is_valid()) {
godot_progress_bar->set_progress_texture(progress_texture);
} else {
UtilityFunctions::push_error(
"Failed to load progress bar base sprite ", progress_texture_file, " for GUI icon ", icon_name
"Failed to create and set progress bar sprite progress texture for GUI icon ", icon_name
);
ret = false;
}
Expand Down Expand Up @@ -232,15 +276,25 @@ static bool generate_button(generate_gui_args_t&& args) {

if (texture.is_valid()) {
godot_button->set_custom_minimum_size(texture->get_size());

static const std::vector<std::pair<StringName, Color>> button_texture_themes {
{ "normal", { 1.0f, 1.0f, 1.0f } }, { "hover", { 1.25f, 1.25f, 1.25f } },
{ "pressed", { 0.75f, 0.75f, 0.75f } }, { "disabled", { 0.5f, 0.5f, 0.5f } }
};

Ref<StyleBoxTexture> stylebox;
stylebox.instantiate();
if (stylebox.is_valid()) {
static const StringName theme_name_normal = "normal";
stylebox->set_texture(texture);
godot_button->add_theme_stylebox_override(theme_name_normal, stylebox);
} else {
UtilityFunctions::push_error("Failed to load instantiate texture stylebox for GUI button ", button_name);
ret = false;
for (auto const& [theme_name, colour] : button_texture_themes) {
stylebox.instantiate();
if (stylebox.is_valid()) {
stylebox->set_texture(texture);
stylebox->set_modulate(colour);
godot_button->add_theme_stylebox_override(theme_name, stylebox);
} else {
UtilityFunctions::push_error(
"Failed to instantiate ", theme_name, " texture stylebox for GUI button ", button_name
);
ret = false;
}
}
}
} else {
Expand All @@ -257,8 +311,14 @@ static bool generate_button(generate_gui_args_t&& args) {
UtilityFunctions::push_error("Failed to load font for GUI button ", button_name);
ret = false;
}

static const std::vector<StringName> button_font_themes {
"font_color", "font_hover_color", "font_hover_pressed_color", "font_pressed_color", "font_disabled_color"
};
const Color colour = Utilities::to_godot_color(button.get_font()->get_colour());
godot_button->add_theme_color_override("font_color", colour);
for (StringName const& theme_name : button_font_themes) {
godot_button->add_theme_color_override(theme_name, colour);
}
}

args.result = godot_button;
Expand Down
3 changes: 2 additions & 1 deletion game/src/Game/GameSession/Topbar.gd
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ func _update_info() -> void:
if _date_label:
_date_label.text = GameSingleton.get_longform_date()

# TODO - add disabled state textures so this doesn't hide the buttons
# TODO - decide whether to disable these or not
# (they don't appear to get disabled in the base game)
#if _speed_up_button:
# _speed_up_button.disabled = not GameSingleton.can_increase_speed()

Expand Down

0 comments on commit 12d4102

Please sign in to comment.