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

@@ -14,6 +14,19 @@ onready var vmode = {
# var item = $"%building_type".get_item_text(index)
# $WorldEditor.editor_command("change_building_type", [selected_building, item])
#var nav_active = false
#func nav_gui_input(event: InputEvent):
# if Input.is_action_just_pressed("mouse1"):
# Input.mouse_mode = Input.MOUSE_MODE_CAPTURED
# nav_active = true
# elif Input.is_action_just_released("mouse1"):
# Input.mouse_mode = Input.MOUSE_MODE_VISIBLE
# nav_active = false
# if nav_active && Input.is_action_pressed("mouse1"):
# if event is InputEventMouseMotion:
# get_viewport().get_camera().global_transform.origin.x += event.relative.x
# get_viewport().get_camera().global_transform.origin.z -= event.relative.y
func _ready():
for b in [
$"%select_buildings",
@@ -30,7 +43,7 @@ func _ready():
$"%building_cursor".hide()
$"%line_cursor".hide()
# $"%building_type".connect("item_selected", self, "change_building_type")
# $"%NavPanel".connect("gui_input", self, "nav_gui_input")
var ignore_events = [
"result:get_closest_building",
"mouse_drag",

View File

@@ -58,9 +58,17 @@ anchor_right = 1.0
anchor_bottom = 1.0
margin_left = -232.0
[node name="ColorRect" type="ColorRect" parent="VBoxContainer"]
[node name="NavPanel" type="NavPanel" parent="VBoxContainer"]
unique_name_in_owner = true
margin_right = 232.0
margin_bottom = 30.0
margin_bottom = 48.0
rect_min_size = Vector2( 48, 48 )
focus_mode = 2
[node name="ColorRect" type="ColorRect" parent="VBoxContainer"]
margin_top = 52.0
margin_right = 232.0
margin_bottom = 82.0
rect_min_size = Vector2( 160, 30 )
size_flags_horizontal = 3
size_flags_vertical = 5
@@ -79,44 +87,44 @@ valign = 1
[node name="select_buildings" type="Button" parent="VBoxContainer"]
unique_name_in_owner = true
margin_top = 34.0
margin_top = 86.0
margin_right = 232.0
margin_bottom = 54.0
margin_bottom = 106.0
text = "Buildings Mode"
[node name="select_navigation" type="Button" parent="VBoxContainer"]
unique_name_in_owner = true
margin_top = 58.0
margin_top = 110.0
margin_right = 232.0
margin_bottom = 78.0
margin_bottom = 130.0
text = "Navigation Mode"
[node name="select_poi" type="Button" parent="VBoxContainer"]
unique_name_in_owner = true
margin_top = 82.0
margin_top = 134.0
margin_right = 232.0
margin_bottom = 102.0
margin_bottom = 154.0
text = "POI Mode"
[node name="select_road_lines" type="Button" parent="VBoxContainer"]
unique_name_in_owner = true
margin_top = 106.0
margin_top = 158.0
margin_right = 232.0
margin_bottom = 126.0
margin_bottom = 178.0
text = "Road Lines Mode"
[node name="select_npc" type="Button" parent="VBoxContainer"]
unique_name_in_owner = true
margin_top = 130.0
margin_top = 182.0
margin_right = 232.0
margin_bottom = 150.0
margin_bottom = 202.0
text = "NPC Mode"
[node name="v_buildings" type="VBoxContainer" parent="VBoxContainer"]
unique_name_in_owner = true
margin_top = 154.0
margin_top = 206.0
margin_right = 232.0
margin_bottom = 322.0
margin_bottom = 374.0
[node name="HSeparator" type="HSeparator" parent="VBoxContainer/v_buildings"]
margin_right = 232.0
@@ -178,9 +186,9 @@ text = "Save Buildings"
[node name="v_navigation" type="VBoxContainer" parent="VBoxContainer"]
unique_name_in_owner = true
margin_top = 326.0
margin_top = 378.0
margin_right = 232.0
margin_bottom = 348.0
margin_bottom = 400.0
[node name="HSeparator" type="HSeparator" parent="VBoxContainer/v_navigation"]
margin_right = 232.0
@@ -194,9 +202,9 @@ text = "Navigation mode"
[node name="v_poi" type="VBoxContainer" parent="VBoxContainer"]
unique_name_in_owner = true
margin_top = 352.0
margin_top = 404.0
margin_right = 232.0
margin_bottom = 374.0
margin_bottom = 426.0
[node name="HSeparator" type="HSeparator" parent="VBoxContainer/v_poi"]
margin_right = 232.0
@@ -210,9 +218,9 @@ text = "POI mode"
[node name="v_road_lines" type="VBoxContainer" parent="VBoxContainer"]
unique_name_in_owner = true
margin_top = 378.0
margin_top = 430.0
margin_right = 232.0
margin_bottom = 986.0
margin_bottom = 1038.0
[node name="HSeparator" type="HSeparator" parent="VBoxContainer/v_road_lines"]
margin_right = 232.0
@@ -469,9 +477,9 @@ text = "Cancel"
[node name="v_npc" type="VBoxContainer" parent="VBoxContainer"]
unique_name_in_owner = true
margin_top = 990.0
margin_top = 1042.0
margin_right = 232.0
margin_bottom = 1012.0
margin_bottom = 1064.0
[node name="HSeparator" type="HSeparator" parent="VBoxContainer/v_npc"]
margin_right = 232.0

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()