Initial camera control code added

This commit is contained in:
2024-08-19 22:05:15 +03:00
parent d1dc024353
commit 0daf02795e
3 changed files with 109 additions and 11 deletions

View File

@@ -209,6 +209,7 @@ func check_edit_building():
dragging = false
drag_delay = 0.2
func _physics_process(delta):
assert($WorldEditor is WorldEditor)
var editor_mode = $WorldEditor.get_current_mode()
var camera_mode = $WorldEditor.get_camera_mode()
if camera_mode == 1:
@@ -254,18 +255,8 @@ func _physics_process(delta):
$Area.global_transform.origin.x = $Camera.global_transform.origin.x
$Area.global_transform.origin.z = $Camera.global_transform.origin.z
func setup_cam1():
Input.mouse_mode = Input.MOUSE_MODE_CAPTURED
var camera_mode = $WorldEditor.get_camera_mode()
if camera_mode == -1:
$Camera.global_transform.origin.y = 80.0
$Camera.global_transform.basis = Basis().rotated(Vector3(1, 0, 0), -PI / 2.0)
$WorldEditor.set_camera_mode(1)
func setup_cam2():
var camera_mode = $WorldEditor.get_camera_mode()
Input.mouse_mode = Input.MOUSE_MODE_CAPTURED
if camera_mode == -1:
$Camera.global_transform.origin.y = 80.0
$WorldEditor.set_camera_mode(2)
$Camera.global_transform.basis = Basis().rotated(Vector3(1, 0, 0), -PI / 4.0)
func setup_cam3():
Input.mouse_mode = Input.MOUSE_MODE_VISIBLE
$WorldEditor.set_camera_mode(3)

View File

@@ -1,8 +1,12 @@
#include <core/object.h>
#include <core/engine.h>
#include <core/os/input.h>
#include <core/input_map.h>
#include <scene/gui/control.h>
#include <scene/gui/box_container.h>
#include <scene/gui/button.h>
#include <scene/main/viewport.h>
#include <scene/3d/camera.h>
#include <scene/scene_string_names.h>
#include "world_editor.h"
@@ -12,7 +16,21 @@ WorldEditor::WorldEditor()
, editor_menu(nullptr)
, current_mode(-1)
, current_camera_mode(-1)
, motion(Vector2())
, old_mouse_pos(Vector2(-1, -1))
{
if (!InputMap::get_singleton()->has_action("left"))
InputMap::get_singleton()->add_action("left");
if (!InputMap::get_singleton()->has_action("right"))
InputMap::get_singleton()->add_action("right");
if (!InputMap::get_singleton()->has_action("backward"))
InputMap::get_singleton()->add_action("backward");
if (!InputMap::get_singleton()->has_action("forward"))
InputMap::get_singleton()->add_action("forward");
if (!InputMap::get_singleton()->has_action("action"))
InputMap::get_singleton()->add_action("action");
if (!InputMap::get_singleton()->has_action("action2"))
InputMap::get_singleton()->add_action("action2");
}
WorldEditor::~WorldEditor()
@@ -21,7 +39,43 @@ WorldEditor::~WorldEditor()
void WorldEditor::set_camera_mode(int mode)
{
print_line("set_camera_mode: " + itos(mode));
bool update_transform = false;
Camera *cam = get_viewport()->get_camera();
if (!cam)
return;
Transform cam_xform = cam->get_global_transform();
if (current_camera_mode == -1) {
cam_xform.origin.y = 80.0f;
update_transform = true;
}
current_camera_mode = mode;
switch (mode) {
case 0:
break;
case 1:
Input::get_singleton()->set_mouse_mode(
Input::MOUSE_MODE_CAPTURED);
cam_xform.basis =
Basis().rotated(Vector3(1, 0, 0), -Math_PI / 2.0);
update_transform = true;
break;
case 2:
Input::get_singleton()->set_mouse_mode(
Input::MOUSE_MODE_CAPTURED);
cam_xform.basis =
Basis().rotated(Vector3(1, 0, 0), -Math_PI / 4.0);
update_transform = true;
break;
case 3:
Input::get_singleton()->set_mouse_mode(
Input::MOUSE_MODE_VISIBLE);
break;
default:
break;
}
if (update_transform)
cam->set_global_transform(cam_xform);
}
int WorldEditor::get_camera_mode() const
@@ -190,12 +244,60 @@ void WorldEditor::_notification(int which)
"world_command_result");
}
}
set_process_unhandled_input(true);
} break;
case NOTIFICATION_EXIT_TREE:
set_process_unhandled_input(false);
break;
case NOTIFICATION_PHYSICS_PROCESS: {
if (!is_inside_tree())
return;
Camera *cam = get_viewport()->get_camera();
if (!cam)
return;
float delta = get_physics_process_delta_time();
Transform cam_xform = cam->get_global_transform();
if (current_camera_mode == 1) {
Vector2 mouse_pos =
get_viewport()->get_mouse_position();
if (old_mouse_pos.x < 0) {
old_mouse_pos = mouse_pos;
motion = Vector2();
} else {
motion = mouse_pos - old_mouse_pos;
old_mouse_pos = mouse_pos;
}
bool moved = false;
float h = cam_xform.origin.y;
float xx = Input::get_singleton()->get_axis("left",
"right");
float zz = Input::get_singleton()->get_axis("backward",
"forward");
float hh = Input::get_singleton()->get_axis("action2",
"action");
if (Math::abs(zz) > 0.1f) {
cam_xform.origin.z -= Math::abs(h) * zz * delta;
moved = true;
}
if (Math::abs(xx) > 0.1f) {
cam_xform.origin.x += Math::abs(h) * xx * delta;
moved = true;
}
if (Math::abs(hh) > 0.1f && Math::abs(xx) < 0.1f &&
Math::abs(zz) < 0.1f) {
cam_xform.origin.y += 10.0f * hh * delta;
moved = true;
}
}
} break;
}
}
void WorldEditor::_unhandled_input(const Ref<InputEvent> &event)
{
print_line("unhandled_input: " + event->as_text());
}
void WorldEditor::world_exited()
{
stream_world = nullptr;
@@ -217,6 +319,8 @@ void WorldEditor::_bind_methods()
&WorldEditor::set_camera_mode);
ClassDB::bind_method(D_METHOD("get_camera_mode"),
&WorldEditor::get_camera_mode);
ClassDB::bind_method(D_METHOD("_unhandled_input", "event"),
&WorldEditor::_unhandled_input);
ADD_SIGNAL(MethodInfo("editor_event",
PropertyInfo(Variant::STRING, "event_name"),
PropertyInfo(Variant::ARRAY, "args")));

View File

@@ -21,12 +21,15 @@ protected:
StreamWorld *get_stream_world();
void world_command_result(const String &what, const Array &data);
void _notification(int which);
void _unhandled_input(const Ref<InputEvent> &event);
static void _bind_methods();
private:
void world_exited();
int current_mode;
int current_camera_mode;
Vector2 motion;
Vector2 old_mouse_pos;
public:
WorldEditor();