wave-survival/modules/wave_survival/heads_up_display.cpp
2025-08-30 23:08:16 +02:00

57 lines
1.6 KiB
C++

#include "heads_up_display.h"
HeadsUpDisplay *HeadsUpDisplay::singleton_instance{ nullptr };
void HeadsUpDisplay::_bind_methods() {
ClassDB::bind_static_method("HeadsUpDisplay", D_METHOD("get_singleton"), &self_type::get_singleton);
ClassDB::bind_method(D_METHOD("set_tooltip", "tooltip"), &self_type::set_tooltip);
ClassDB::bind_method(D_METHOD("set_health_percentage", "percentage"), &self_type::set_health_percentage);
}
void HeadsUpDisplay::on_child_entered(Node *child) {
child->connect("child_entered_tree", callable_mp(this, &self_type::on_child_entered));
if (child->is_unique_name_in_owner() && child->get_name() == "Tooltip") {
this->tooltip = cast_to<Label>(child);
}
if (child->is_unique_name_in_owner() && child->get_name() == "Healthbar") {
this->healthbar = cast_to<ProgressBar>(child);
}
}
void HeadsUpDisplay::enter_tree() {
connect("child_entered_tree", callable_mp(this, &self_type::on_child_entered));
singleton_instance = this;
}
void HeadsUpDisplay::exit_tree() {
singleton_instance = nullptr;
}
void HeadsUpDisplay::_notification(int what) {
if (Engine::get_singleton()->is_editor_hint()) {
return;
}
switch (what) {
default:
return;
case NOTIFICATION_ENTER_TREE:
enter_tree();
return;
case NOTIFICATION_EXIT_TREE:
exit_tree();
return;
}
}
HeadsUpDisplay *HeadsUpDisplay::get_singleton() {
return singleton_instance;
}
void HeadsUpDisplay::set_tooltip(String const &tooltip) {
this->tooltip->set_text(tooltip);
}
void HeadsUpDisplay::set_health_percentage(double health_percentage) {
this->healthbar->set_value(health_percentage);
}