84 lines
2.3 KiB
C++
84 lines
2.3 KiB
C++
#include "file_popup.h"
|
|
#include "core/config/engine.h"
|
|
#include "core/input/input_event.h"
|
|
#include "core/math/math_funcs.h"
|
|
#include "scene/main/node.h"
|
|
#include "scene/main/viewport.h"
|
|
#include "you_done_it/macros.h"
|
|
|
|
void FilePopup::_bind_methods() {
|
|
BIND_HPROPERTY(Variant::OBJECT, open_position, PROPERTY_HINT_NODE_TYPE, "Node2D");
|
|
ClassDB::bind_method(D_METHOD("request_open"), &self_type::request_open);
|
|
ClassDB::bind_method(D_METHOD("request_close"), &self_type::request_close);
|
|
ClassDB::bind_method(D_METHOD("is_open"), &self_type::is_open);
|
|
}
|
|
|
|
void FilePopup::enter_tree() {
|
|
this->home_position = get_global_transform();
|
|
}
|
|
|
|
void FilePopup::process(double delta) {
|
|
if (!this->open_position) {
|
|
set_process(false);
|
|
return;
|
|
}
|
|
this->animation_progress = Math::move_toward(this->animation_progress, this->desire_open ? 1.0 : 0.0, delta * this->ANIM_SPEED);
|
|
this->set_global_position(this->home_position.get_origin().lerp(this->open_position->get_global_position(), this->animation_progress));
|
|
this->set_global_rotation(Math::lerp_angle(this->home_position.get_rotation(), this->open_position->get_global_rotation(), (float)this->animation_progress));
|
|
if (this->animation_progress == 0.0) {
|
|
set_process(false);
|
|
set_process_unhandled_input(false);
|
|
}
|
|
if (this->animation_progress == 1.0) {
|
|
set_process(false);
|
|
}
|
|
}
|
|
|
|
void FilePopup::unhandled_input(Ref<InputEvent> const &what) {
|
|
Ref<InputEventMouseButton> button{ what };
|
|
if (button.is_valid() && button->is_pressed()) {
|
|
this->desire_open = false;
|
|
set_process(true);
|
|
get_viewport()->set_input_as_handled();
|
|
}
|
|
}
|
|
|
|
void FilePopup::_notification(int what) {
|
|
if (Engine::get_singleton()->is_editor_hint()) {
|
|
return;
|
|
}
|
|
switch (what) {
|
|
default:
|
|
return;
|
|
case NOTIFICATION_ENTER_TREE:
|
|
set_process_unhandled_input(true);
|
|
enter_tree();
|
|
return;
|
|
case NOTIFICATION_PROCESS:
|
|
process(get_process_delta_time());
|
|
return;
|
|
}
|
|
}
|
|
|
|
void FilePopup::request_open() {
|
|
this->desire_open = true;
|
|
set_process_unhandled_input(true);
|
|
set_process(true);
|
|
set_visible(true);
|
|
}
|
|
|
|
void FilePopup::request_close() {
|
|
this->desire_open = false;
|
|
}
|
|
|
|
bool FilePopup::is_open() const {
|
|
return this->animation_progress == 1.0;
|
|
}
|
|
|
|
void FilePopup::set_open_position(Node2D *node) {
|
|
this->open_position = node;
|
|
}
|
|
|
|
Node2D *FilePopup::get_open_position() const {
|
|
return this->open_position;
|
|
}
|