YouDunIt/modules/you_done_it/photo_inbox.cpp

75 lines
2.2 KiB
C++

#include "photo_inbox.h"
#include "core/input/input_event.h"
#include "core/object/class_db.h"
#include "core/os/keyboard.h"
#include "you_done_it/clue_db.h"
#include "you_done_it/macros.h"
#include "you_done_it/pinned_photo.h"
#include "you_done_it/server_node.h"
void PhotoInbox::_bind_methods() {
BIND_HPROPERTY(Variant::OBJECT, photo_scene, PROPERTY_HINT_RESOURCE_TYPE, "PackedScene");
}
void PhotoInbox::enter_tree() {
if (ServerNode * server{ ServerNode::get_singleton() }) {
server->connect(ServerNode::sig_clue_revealed, callable_mp(this, &self_type::on_clue_revealed));
} else {
// enable process unhandled input if no server is found, as this implies we should be running in test mode.
set_process_unhandled_input(true);
print_line("No ServerNode singleton found, Running PhotoInbox in testing mode");
}
}
void PhotoInbox::unhandled_input(Ref<InputEvent> const &event) {
Ref<InputEventKey> key{ event };
if (key.is_valid() && key->is_pressed()) {
switch (key->get_key_label()) {
default:
break;
case Key::KEY_1:
on_clue_revealed(NetworkData::CLUE_FIRST);
get_viewport()->set_input_as_handled();
break;
case Key::KEY_2:
on_clue_revealed(NetworkData::CLUE_SECOND);
get_viewport()->set_input_as_handled();
break;
}
}
}
void PhotoInbox::_notification(int what) {
if (Engine::get_singleton()->is_editor_hint()) {
return;
}
switch (what) {
default:
return;
case NOTIFICATION_ENTER_TREE:
enter_tree();
return;
}
}
void PhotoInbox::on_clue_revealed(NetworkData::ClueID clue) {
Node *instantiated{ photo_scene->instantiate() };
if (!instantiated) {
return;
}
if (PinnedPhoto * photo{ cast_to<PinnedPhoto>(instantiated) }) {
photo->set_global_position(get_global_position());
photo->set_visible(false); // make invisible until setup is complete to avoid delay
get_parent()->add_child(instantiated);
photo->set_clue(clue);
photo->set_visible(true);
}
}
void PhotoInbox::set_photo_scene(Ref<PackedScene> scene) {
this->photo_scene = ClassDB::is_parent_class(scene->get_state()->get_node_type(0), "Node2D") ? scene : Ref<PackedScene>();
}
Ref<PackedScene> PhotoInbox::get_photo_scene() const {
return this->photo_scene;
}