fixed camera rotation fixed
This commit is contained in:
@@ -7,6 +7,7 @@
|
||||
#include <modules/voxel/terrain/voxel_lod_terrain.h>
|
||||
#include <modules/imgmapper/voxel_generator_imgmapper.h>
|
||||
#include "from_string.h"
|
||||
#include "editor_event.h"
|
||||
#include "world_editor.h"
|
||||
#include "buildings_editor.h"
|
||||
|
||||
@@ -165,13 +166,15 @@ void BuildingsEditor::activate()
|
||||
for (i = 0; i < (int)ui_handlers.size(); i++)
|
||||
assert(ui_handlers[i]);
|
||||
active = true;
|
||||
editor->event.add_listener(this, &BuildingsEditor::event_handler);
|
||||
EditorEvent::get_singleton()->event.add_listener(
|
||||
this, &BuildingsEditor::event_handler);
|
||||
}
|
||||
|
||||
void BuildingsEditor::deactivate()
|
||||
{
|
||||
assert(active);
|
||||
editor->event.remove_listener(this, &BuildingsEditor::event_handler);
|
||||
EditorEvent::get_singleton()->event.remove_listener(
|
||||
this, &BuildingsEditor::event_handler);
|
||||
print_line("BuildingsEditor DEACTIVE");
|
||||
int i;
|
||||
for (i = 0; i < (int)ui_handlers.size(); i++)
|
||||
@@ -464,7 +467,7 @@ void BuildingsEditor::change_building_type(const String &type_name)
|
||||
|
||||
void BuildingsEditor::emit(const String &event_string, const Array &event_args)
|
||||
{
|
||||
editor->event.emit(event_string, event_args);
|
||||
EditorEvent::get_singleton()->event.emit(event_string, event_args);
|
||||
}
|
||||
|
||||
void BuildingsEditor::remove_buildings_by_prefix(const String &prefix)
|
||||
|
||||
@@ -3,6 +3,8 @@
|
||||
#include <scene/main/viewport.h>
|
||||
#include <scene/3d/camera.h>
|
||||
#include <core/os/input.h>
|
||||
#include <core/math/math_funcs.h>
|
||||
#include "editor_event.h"
|
||||
#include "nav_panel.h"
|
||||
|
||||
NavPanel::NavPanel()
|
||||
@@ -17,6 +19,10 @@ NavPanel::~NavPanel()
|
||||
{
|
||||
}
|
||||
|
||||
static bool h_active = false, v_active = false, r_active = false;
|
||||
static Transform camera_xform;
|
||||
static bool transform_changhed = false;
|
||||
|
||||
void NavPanel::_notification(int which)
|
||||
{
|
||||
switch (which) {
|
||||
@@ -33,6 +39,7 @@ void NavPanel::_notification(int which)
|
||||
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_EXIT_TREE: {
|
||||
if (h) {
|
||||
@@ -51,6 +58,46 @@ void NavPanel::_notification(int which)
|
||||
r = nullptr;
|
||||
}
|
||||
} break;
|
||||
case NOTIFICATION_PROCESS:
|
||||
if (r_active) {
|
||||
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");
|
||||
float delta = get_physics_process_delta_time();
|
||||
Transform move_xform(camera_xform.basis, Vector3());
|
||||
Vector3 front = move_xform.xform(Vector3(0, 0, -1));
|
||||
Vector3 side = move_xform.xform(Vector3(0, 0, -1));
|
||||
if (Math::abs(zz) > 0.1f) {
|
||||
camera_xform.origin +=
|
||||
front * 60.0f * zz * delta;
|
||||
transform_changhed = true;
|
||||
}
|
||||
if (Math::abs(xx) > 0.1f) {
|
||||
camera_xform.origin +=
|
||||
side * 60.0f * xx * delta;
|
||||
transform_changhed = true;
|
||||
}
|
||||
if (Math::abs(hh) > 0.1f && Math::abs(xx) < 0.1f &&
|
||||
Math::abs(zz) < 0.1f) {
|
||||
camera_xform.origin.y += 60.0f * hh * delta;
|
||||
transform_changhed = true;
|
||||
}
|
||||
}
|
||||
if (transform_changhed) {
|
||||
Camera *cam = get_viewport()->get_camera();
|
||||
camera_xform.orthonormalize();
|
||||
cam->set_global_transform(camera_xform);
|
||||
Array move_args;
|
||||
move_args.push_back(camera_xform);
|
||||
EditorEvent::get_singleton()->event.emit(
|
||||
"editor_camera_moved", move_args);
|
||||
transform_changhed = false;
|
||||
print_line("moved: " +
|
||||
(camera_xform.origin.operator String()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -63,9 +110,6 @@ void NavPanel::_bind_methods()
|
||||
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")) {
|
||||
@@ -115,7 +159,7 @@ 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();
|
||||
camera_xform = cam->get_global_transform();
|
||||
rotations = camera_xform.basis.get_euler();
|
||||
r_active = true;
|
||||
Input::get_singleton()->set_mouse_mode(
|
||||
@@ -130,24 +174,22 @@ void NavPanel::gui_input_handler_r(const Ref<InputEvent> &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;
|
||||
rotations.x = CLAMP(rotations.x + rel.y, -Math_PI / 3.0f,
|
||||
Math_PI / 3.0f);
|
||||
|
||||
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);
|
||||
transform_changhed = true;
|
||||
#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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,6 +13,7 @@
|
||||
#include <scene/scene_string_names.h>
|
||||
#include "road_lines_editor.h"
|
||||
#include "world_editor.h"
|
||||
#include "editor_event.h"
|
||||
#include "buildings_editor.h"
|
||||
|
||||
WorldEditor::WorldEditor()
|
||||
@@ -48,12 +49,14 @@ WorldEditor::WorldEditor()
|
||||
InputMap::get_singleton()->add_action("editor_cam3");
|
||||
if (!InputMap::get_singleton()->has_action("mouse1"))
|
||||
InputMap::get_singleton()->add_action("mouse1");
|
||||
event.add_listener(this, &WorldEditor::event_signal_handler);
|
||||
EditorEvent::get_singleton()->event.add_listener(
|
||||
this, &WorldEditor::event_signal_handler);
|
||||
}
|
||||
|
||||
WorldEditor::~WorldEditor()
|
||||
{
|
||||
event.remove_listener(this, &WorldEditor::event_signal_handler);
|
||||
EditorEvent::get_singleton()->event.remove_listener(
|
||||
this, &WorldEditor::event_signal_handler);
|
||||
if (road_lines_editor) {
|
||||
memdelete(road_lines_editor);
|
||||
road_lines_editor = nullptr;
|
||||
@@ -167,7 +170,7 @@ void WorldEditor::tools_button(const String &button)
|
||||
goto end;
|
||||
change[0] = current_mode;
|
||||
change[1] = modes[button];
|
||||
event.emit("mode_change_pre", change);
|
||||
EditorEvent::get_singleton()->event.emit("mode_change_pre", change);
|
||||
switch (current_mode) {
|
||||
case MODE_ROAD_LINES:
|
||||
road_lines_editor->exit();
|
||||
@@ -193,7 +196,7 @@ void WorldEditor::tools_button(const String &button)
|
||||
mode_npc();
|
||||
break;
|
||||
}
|
||||
event.emit("mode_change_post", change);
|
||||
EditorEvent::get_singleton()->event.emit("mode_change_post", change);
|
||||
current_mode = modes[button];
|
||||
end:;
|
||||
}
|
||||
@@ -277,7 +280,7 @@ StreamWorld *WorldEditor::get_stream_world()
|
||||
void WorldEditor::world_command_result(const String &what, const Array &data)
|
||||
{
|
||||
print_line("what: " + what);
|
||||
event.emit("result:" + what, data);
|
||||
EditorEvent::get_singleton()->event.emit("result:" + what, data);
|
||||
}
|
||||
|
||||
void WorldEditor::_notification(int which)
|
||||
@@ -332,10 +335,10 @@ void WorldEditor::_notification(int which)
|
||||
Vector2 position =
|
||||
get_viewport()->get_mouse_position();
|
||||
args.push_back(position);
|
||||
event.emit("mouse_drag_off", args);
|
||||
EditorEvent::get_singleton()->event.emit(
|
||||
"mouse_drag_off", args);
|
||||
}
|
||||
}
|
||||
Transform cam_xform = cam->get_global_transform();
|
||||
if (current_camera_mode == 1) {
|
||||
Vector2 mouse_pos =
|
||||
get_viewport()->get_mouse_position();
|
||||
@@ -346,33 +349,6 @@ void WorldEditor::_notification(int which)
|
||||
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;
|
||||
}
|
||||
if (moved) {
|
||||
cam->set_global_transform(cam_xform);
|
||||
Array move_args;
|
||||
move_args.push_back(cam_xform);
|
||||
event.emit("editor_camera_moved", move_args);
|
||||
}
|
||||
}
|
||||
if (!dragging && drag_delay >= 0.0f)
|
||||
drag_delay -= delta;
|
||||
@@ -406,13 +382,14 @@ void WorldEditor::_unhandled_input(const Ref<InputEvent> &event)
|
||||
Array args;
|
||||
Vector2 position = get_viewport()->get_mouse_position();
|
||||
args.push_back(position);
|
||||
this->event.emit("mouse_press", args);
|
||||
EditorEvent::get_singleton()->event.emit("mouse_press", args);
|
||||
} else if (Input::get_singleton()->is_action_pressed("mouse1")) {
|
||||
if (dragging) {
|
||||
Array args;
|
||||
Vector2 position = get_viewport()->get_mouse_position();
|
||||
args.push_back(position);
|
||||
this->event.emit("mouse_drag", args);
|
||||
EditorEvent::get_singleton()->event.emit("mouse_drag",
|
||||
args);
|
||||
} else {
|
||||
if (drag_delay < 0.0f && !dragging) {
|
||||
dragging = true;
|
||||
@@ -420,7 +397,8 @@ void WorldEditor::_unhandled_input(const Ref<InputEvent> &event)
|
||||
Vector2 position =
|
||||
get_viewport()->get_mouse_position();
|
||||
args.push_back(position);
|
||||
this->event.emit("mouse_drag_on", args);
|
||||
EditorEvent::get_singleton()->event.emit(
|
||||
"mouse_drag_on", args);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -50,75 +50,6 @@ public:
|
||||
void editor_command(const String &command, const Array &args);
|
||||
int get_camera_mode() const;
|
||||
int get_current_mode() const;
|
||||
class EventHelper {
|
||||
class event_listener_ptrs {
|
||||
public:
|
||||
class H {};
|
||||
H *obj;
|
||||
void (H::*method)(const String &event,
|
||||
const Array &args);
|
||||
void execute(const String &event,
|
||||
const Array &args) const
|
||||
{
|
||||
(obj->*method)(event, args);
|
||||
}
|
||||
};
|
||||
std::list<event_listener_ptrs> listeners;
|
||||
typedef event_listener_ptrs::H *obj_t;
|
||||
typedef void (event_listener_ptrs::H::*method_t)(
|
||||
const String &event, const Array &args);
|
||||
|
||||
public:
|
||||
template <class T>
|
||||
void add_listener(T *obj, void (T::*method)(const String &event,
|
||||
const Array &args))
|
||||
{
|
||||
int size = listeners.size();
|
||||
auto evl = listeners.begin();
|
||||
bool bad = false;
|
||||
while (evl != listeners.end()) {
|
||||
const event_listener_ptrs &xev = *evl;
|
||||
if (xev.obj == reinterpret_cast<obj_t>(obj) &&
|
||||
xev.method == reinterpret_cast<method_t>(
|
||||
method)) {
|
||||
bad = true;
|
||||
break;
|
||||
}
|
||||
evl++;
|
||||
}
|
||||
assert(!bad);
|
||||
if (bad)
|
||||
return;
|
||||
event_listener_ptrs ev;
|
||||
ev.obj = reinterpret_cast<obj_t>(obj);
|
||||
ev.method = reinterpret_cast<method_t>(method);
|
||||
listeners.push_back(ev);
|
||||
assert((int)listeners.size() == size + 1);
|
||||
}
|
||||
template <class T>
|
||||
void remove_listener(T *obj,
|
||||
void (T::*method)(const String &event,
|
||||
const Array &args))
|
||||
{
|
||||
listeners.remove_if([obj,
|
||||
method](const event_listener_ptrs
|
||||
&e) {
|
||||
return e.obj == reinterpret_cast<obj_t>(obj) &&
|
||||
e.method == reinterpret_cast<method_t>(
|
||||
method);
|
||||
});
|
||||
}
|
||||
void emit(const String &event, const Array &args)
|
||||
{
|
||||
auto evl = listeners.begin();
|
||||
while (evl != listeners.end()) {
|
||||
const event_listener_ptrs &xev = *evl;
|
||||
xev.execute(event, args);
|
||||
evl++;
|
||||
}
|
||||
}
|
||||
};
|
||||
EventHelper event;
|
||||
void event_signal_handler(const String &event, const Array &args);
|
||||
};
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user