Added camera controls

This commit is contained in:
2024-10-12 17:49:13 +03:00
parent 2424659058
commit 3241905ad1
7 changed files with 1204 additions and 35 deletions

View File

@@ -3,6 +3,7 @@
#include <vector>
#include <main/main.h>
#include <core/engine.h>
#include <core/os/input.h>
#include <scene/resources/packed_scene.h>
#include <scene/resources/surface_tool.h>
#include <scene/3d/mesh_instance.h>
@@ -58,8 +59,21 @@ public:
virtual ~LayoutEditor()
{
}
void update()
{
float delta =
SceneTree::get_singleton()->get_idle_process_time();
// print_line("update: " + String::num(delta));
float rot =
Input::get_singleton()->get_axis("action2", "action") *
delta * 10.0f;
rot = Math::stepify(rot, Math_PI / 8.0);
get_as_node<Spatial>("%camera_rot")->rotate_y(rot);
}
void activate()
{
SceneTree::get_singleton()->connect("idle_frame", this,
"update");
editor->update_element_meshes();
get_as_node<Control>("%layout_editor")->show();
get_as_node<Spatial>("%bg_floor")->show();
@@ -97,6 +111,8 @@ public:
get_as_node<Control>("%layout_editor")->hide();
EditorEvent::get_singleton()->event.remove_listener(
this, &LayoutEditor::event_signal_handler);
SceneTree::get_singleton()->disconnect("idle_frame", this,
"update");
}
void event_signal_handler(const String &event,
const Vector<Variant> &args)
@@ -198,6 +214,12 @@ public:
current_layout, is_exterior, current_floor);
print_line("visualize: " + itos(current_floor));
visualize_below_and_current(current_layout, is_exterior);
Transform xform = editor->get_viewport()
->get_camera()
->get_global_transform();
xform.origin.y = 23 + current_floor * 5;
editor->get_viewport()->get_camera()->set_global_transform(
xform);
}
void select_grid_element(int index)
{
@@ -263,6 +285,7 @@ public:
&LayoutEditor::select_grid_rotation);
ClassDB::bind_method(D_METHOD("grow_cell"),
&LayoutEditor::grow_cell);
ClassDB::bind_method(D_METHOD("update"), &LayoutEditor::update);
}
void clear_vis(const String &key, bool exterior)
{

View File

@@ -9,9 +9,11 @@
NavPanel::NavPanel()
: HBoxContainer()
, flags(0x7)
, h(nullptr)
, v(nullptr)
, r(nullptr)
, yr(nullptr)
{
}
@@ -19,7 +21,8 @@ NavPanel::~NavPanel()
{
}
static bool h_active = false, v_active = false, r_active = false;
static bool h_active = false, v_active = false, r_active = false,
yr_active = false;
static Transform camera_xform;
static bool transform_changhed = false;
@@ -27,22 +30,9 @@ void NavPanel::_notification(int which)
{
switch (which) {
case NOTIFICATION_ENTER_TREE: {
h = memnew(ColorRect);
v = memnew(ColorRect);
r = memnew(ColorRect);
h->set_custom_minimum_size(Vector2(48, 48));
v->set_custom_minimum_size(Vector2(48, 48));
r->set_custom_minimum_size(Vector2(48, 48));
add_child(h);
add_child(v);
add_child(r);
h->set_focus_mode(Control::FOCUS_CLICK);
v->set_focus_mode(Control::FOCUS_CLICK);
r->set_focus_mode(Control::FOCUS_CLICK);
h->connect("gui_input", this, "gui_input_handler_h");
v->connect("gui_input", this, "gui_input_handler_v");
r->connect("gui_input", this, "gui_input_handler_r");
set_process(true);
} break;
case NOTIFICATION_READY: {
update_flags();
} break;
case NOTIFICATION_EXIT_TREE: {
if (h) {
@@ -110,6 +100,14 @@ void NavPanel::_bind_methods()
&NavPanel::gui_input_handler_v);
ClassDB::bind_method(D_METHOD("gui_input_handler_r", "e"),
&NavPanel::gui_input_handler_r);
ClassDB::bind_method(D_METHOD("gui_input_handler_yr", "e"),
&NavPanel::gui_input_handler_yr);
ClassDB::bind_method(D_METHOD("set_flags", "flags"),
&NavPanel::set_flags);
ClassDB::bind_method(D_METHOD("get_flags"), &NavPanel::get_flags);
ADD_PROPERTY(PropertyInfo(Variant::INT, "flags", PROPERTY_HINT_FLAGS,
"XZ,XY,RotMove,YRotOrigin"),
"set_flags", "get_flags");
}
void NavPanel::gui_input_handler_h(const Ref<InputEvent> &e)
{
@@ -195,11 +193,113 @@ void NavPanel::gui_input_handler_r(const Ref<InputEvent> &e)
camera_xform.basis.rotated(axis, rotations.x);
transform_changhed = true;
get_viewport()->set_input_as_handled();
#if 0
camera_xform.basis = camera_xform.basis.rotated(
Vector3(0, 1, 0), rel.x * 0.1);
camera_xform.basis = camera_xform.basis.rotated(
Vector3(1, 0, 0), rel.y * 0.1);
#endif
}
}
void NavPanel::gui_input_handler_yr(const Ref<InputEvent> &e)
{
Camera *cam = get_viewport()->get_camera();
Ref<InputEventMouseButton> mb = e;
if (Input::get_singleton()->is_action_just_pressed("mouse1")) {
yr_active = true;
Input::get_singleton()->set_mouse_mode(
Input::MOUSE_MODE_CAPTURED);
yr->grab_focus();
origin_rotation = Transform();
y_rotation = Transform();
transform_backup = cam->get_global_transform();
} else if (Input::get_singleton()->is_action_just_released("mouse1")) {
yr_active = false;
Input::get_singleton()->set_mouse_mode(
Input::MOUSE_MODE_VISIBLE);
origin_rotation = Transform();
y_rotation = Transform();
cam->set_global_transform(transform_backup);
}
if (mb.is_valid())
get_viewport()->set_input_as_handled();
Ref<InputEventMouseMotion> mm = e;
Vector2 sensivity(0.06f, 0.05f);
if (mm.is_valid() && yr_active) {
Vector2 rel = mm->get_relative() * sensivity;
origin_rotation.basis = origin_rotation.basis.rotated(
Vector3(0, 1, 0), rel.x * 0.1f);
origin_rotation.orthonormalize();
y_rotation.basis = y_rotation.basis.rotated(Vector3(1, 0, 0),
rel.y * 0.1f);
camera_xform = origin_rotation * y_rotation * transform_backup;
cam->set_global_transform(camera_xform);
get_viewport()->set_input_as_handled();
}
}
uint32_t NavPanel::get_flags() const
{
return flags;
}
void NavPanel::set_flags(uint32_t flags)
{
if (this->flags != flags) {
this->flags = flags;
if (is_inside_tree())
update_flags();
}
}
void NavPanel::update_flags()
{
set_process(false);
bool h_on = !!(flags & (1 << 0));
bool v_on = !!(flags & (1 << 1));
bool r_on = !!(flags & (1 << 2));
bool yr_on = !!(flags & (1 << 3));
if (h && !h_on) {
h->disconnect("gui_input", this, "gui_input_handler_h");
h->queue_delete();
h = nullptr;
}
if (v && !v_on) {
v->disconnect("gui_input", this, "gui_input_handler_v");
v->queue_delete();
v = nullptr;
}
if (r && !r_on) {
r->disconnect("gui_input", this, "gui_input_handler_r");
r->queue_delete();
r = nullptr;
}
if (yr && !yr_on) {
yr->disconnect("gui_input", this, "gui_input_handler_yr");
yr->queue_delete();
yr = nullptr;
}
if (!h && h_on) {
h = memnew(ColorRect);
h->set_custom_minimum_size(Vector2(48, 48));
add_child(h);
h->set_focus_mode(Control::FOCUS_CLICK);
h->connect("gui_input", this, "gui_input_handler_h");
}
if (!v && v_on) {
v = memnew(ColorRect);
v->set_custom_minimum_size(Vector2(48, 48));
add_child(v);
v->set_focus_mode(Control::FOCUS_CLICK);
v->connect("gui_input", this, "gui_input_handler_v");
}
if (!r && r_on) {
r = memnew(ColorRect);
r->set_custom_minimum_size(Vector2(48, 48));
add_child(r);
r->set_focus_mode(Control::FOCUS_CLICK);
r->connect("gui_input", this, "gui_input_handler_r");
}
if (!yr && yr_on) {
yr = memnew(ColorRect);
yr->set_custom_minimum_size(Vector2(48, 48));
add_child(yr);
yr->set_focus_mode(Control::FOCUS_CLICK);
yr->connect("gui_input", this, "gui_input_handler_yr");
}
set_process(true);
}

View File

@@ -9,12 +9,18 @@ public:
virtual ~NavPanel();
private:
ColorRect *h, *v, *r;
uint32_t flags;
ColorRect *h, *v, *r, *yr;
Vector3 rotations;
Transform origin_rotation, y_rotation, transform_backup;
void _notification(int which);
static void _bind_methods();
void gui_input_handler_h(const Ref<InputEvent> &e);
void gui_input_handler_v(const Ref<InputEvent> &e);
void gui_input_handler_r(const Ref<InputEvent> &e);
void gui_input_handler_yr(const Ref<InputEvent> &e);
uint32_t get_flags() const;
void set_flags(uint32_t flags);
void update_flags();
};
#endif