Godot update, camera nav widget, closes #35

This commit is contained in:
2024-09-17 02:27:31 +03:00
parent 49e7b9f970
commit 43bc11b6e8
6 changed files with 220 additions and 24 deletions

View File

@@ -0,0 +1,153 @@
#include <scene/gui/box_container.h>
#include <scene/gui/color_rect.h>
#include <scene/main/viewport.h>
#include <scene/3d/camera.h>
#include <core/os/input.h>
#include "nav_panel.h"
NavPanel::NavPanel()
: HBoxContainer()
, h(nullptr)
, v(nullptr)
, r(nullptr)
{
}
NavPanel::~NavPanel()
{
}
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->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");
} break;
case NOTIFICATION_EXIT_TREE: {
if (h) {
h->disconnect("gui_input", this, "gui_input_handler_h");
memdelete(h);
h = nullptr;
}
if (v) {
v->disconnect("gui_input", this, "gui_input_handler_v");
memdelete(v);
v = nullptr;
}
if (r) {
r->disconnect("gui_input", this, "gui_input_handler_r");
memdelete(r);
r = nullptr;
}
} break;
}
}
void NavPanel::_bind_methods()
{
ClassDB::bind_method(D_METHOD("gui_input_handler_h", "e"),
&NavPanel::gui_input_handler_h);
ClassDB::bind_method(D_METHOD("gui_input_handler_v", "e"),
&NavPanel::gui_input_handler_v);
ClassDB::bind_method(D_METHOD("gui_input_handler_r", "e"),
&NavPanel::gui_input_handler_r);
}
static bool h_active = false, v_active = false, r_active = false;
void NavPanel::gui_input_handler_h(const Ref<InputEvent> &e)
{
if (Input::get_singleton()->is_action_just_pressed("mouse1")) {
h_active = true;
Input::get_singleton()->set_mouse_mode(
Input::MOUSE_MODE_CAPTURED);
} else if (Input::get_singleton()->is_action_just_released("mouse1")) {
h_active = false;
Input::get_singleton()->set_mouse_mode(
Input::MOUSE_MODE_VISIBLE);
}
Ref<InputEventMouseMotion> mm = e;
if (mm.is_valid() && h_active) {
Camera *cam = get_viewport()->get_camera();
Transform camera_xform = cam->get_global_transform();
Vector2 rel = mm->get_relative();
Vector3 mdir(rel.x, 0, rel.y);
camera_xform.origin +=
mdir.rotated(Vector3(0, 1, 0), rotations.y);
cam->set_global_transform(camera_xform);
}
}
void NavPanel::gui_input_handler_v(const Ref<InputEvent> &e)
{
if (Input::get_singleton()->is_action_just_pressed("mouse1")) {
v_active = true;
Input::get_singleton()->set_mouse_mode(
Input::MOUSE_MODE_CAPTURED);
} else if (Input::get_singleton()->is_action_just_released("mouse1")) {
v_active = false;
Input::get_singleton()->set_mouse_mode(
Input::MOUSE_MODE_VISIBLE);
}
Ref<InputEventMouseMotion> mm = e;
if (mm.is_valid() && v_active) {
Camera *cam = get_viewport()->get_camera();
Transform camera_xform = cam->get_global_transform();
Vector2 rel = mm->get_relative();
camera_xform.origin.x += rel.x;
camera_xform.origin.y += rel.y;
cam->set_global_transform(camera_xform);
}
}
void NavPanel::gui_input_handler_r(const Ref<InputEvent> &e)
{
Camera *cam = get_viewport()->get_camera();
if (Input::get_singleton()->is_action_just_pressed("mouse1")) {
Transform camera_xform = cam->get_global_transform();
rotations = camera_xform.basis.get_euler();
r_active = true;
Input::get_singleton()->set_mouse_mode(
Input::MOUSE_MODE_CAPTURED);
} else if (Input::get_singleton()->is_action_just_released("mouse1")) {
r_active = false;
Input::get_singleton()->set_mouse_mode(
Input::MOUSE_MODE_VISIBLE);
}
Ref<InputEventMouseMotion> mm = e;
Vector2 sensivity(0.06f, 0.05f);
if (mm.is_valid() && r_active) {
Vector2 rel = mm->get_relative() * sensivity;
Transform camera_xform = cam->get_global_transform();
rotations.y -= rel.x;
camera_xform.basis =
Basis().rotated(Vector3(0, 1, 0), rotations.y);
rotations.x = CLAMP(rotations.x + rel.y, -Math_PI / 3.0f,
Math_PI / 3.0f);
Vector3 axis =
Vector3(1, 0, 0).rotated(Vector3(0, 1, 0), rotations.y);
camera_xform.basis =
camera_xform.basis.rotated(axis, rotations.x);
#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
camera_xform.orthonormalize();
cam->set_global_transform(camera_xform);
}
}

View File

@@ -0,0 +1,20 @@
#ifndef NAV_PANEL_H
#define NAV_PANEL_H
#include <scene/gui/box_container.h>
class ColorRect;
class NavPanel : public HBoxContainer {
GDCLASS(NavPanel, HBoxContainer)
public:
NavPanel();
virtual ~NavPanel();
private:
ColorRect *h, *v, *r;
Vector3 rotations;
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);
};
#endif

View File

@@ -2,12 +2,14 @@
#include "stream.h"
#include "road_debug.h"
#include "world_editor.h"
#include "nav_panel.h"
void register_stream_types()
{
ClassDB::register_class<StreamWorld>();
ClassDB::register_class<RoadDebug>();
ClassDB::register_class<WorldEditor>();
ClassDB::register_class<NavPanel>();
}
void unregister_stream_types()