Actual line editing
This commit is contained in:
@@ -4,6 +4,7 @@
|
||||
#include <scene/gui/item_list.h>
|
||||
#include <scene/gui/button.h>
|
||||
#include <scene/gui/line_edit.h>
|
||||
#include <scene/gui/spin_box.h>
|
||||
#include <scene/3d/immediate_geometry.h>
|
||||
#include <scene/3d/camera.h>
|
||||
#include <core/io/config_file.h>
|
||||
@@ -107,6 +108,55 @@ protected:
|
||||
}
|
||||
};
|
||||
|
||||
class HandlePointSelection : public Object {
|
||||
GDCLASS(HandlePointSelection, Object)
|
||||
RoadLinesEditor *editor;
|
||||
|
||||
public:
|
||||
HandlePointSelection(RoadLinesEditor *editor)
|
||||
: Object()
|
||||
, editor(editor)
|
||||
{
|
||||
SpinBox *sp_line_point =
|
||||
editor->get_as_node<SpinBox>("%line_index");
|
||||
sp_line_point->connect("value_changed", this,
|
||||
"handle_value_change");
|
||||
Button *bt_line_set_point =
|
||||
editor->get_as_node<Button>("%road_lines_set_point");
|
||||
bt_line_set_point->connect("pressed", this, "handle_set_point");
|
||||
}
|
||||
virtual ~HandlePointSelection()
|
||||
{
|
||||
Button *bt_line_set_point =
|
||||
editor->get_as_node<Button>("%road_lines_set_point");
|
||||
bt_line_set_point->disconnect("pressed", this,
|
||||
"handle_set_point");
|
||||
SpinBox *sp_line_point =
|
||||
editor->get_as_node<SpinBox>("%line_index");
|
||||
sp_line_point->disconnect("value_changed", this,
|
||||
"handle_value_change");
|
||||
}
|
||||
|
||||
protected:
|
||||
void handle_value_change(float value)
|
||||
{
|
||||
int index = (int)value;
|
||||
editor->set_line_index(index);
|
||||
}
|
||||
void handle_set_point()
|
||||
{
|
||||
editor->set_point_to_cursor();
|
||||
}
|
||||
static void _bind_methods()
|
||||
{
|
||||
ClassDB::bind_method(
|
||||
D_METHOD("handle_value_change", "value"),
|
||||
&HandlePointSelection::handle_value_change);
|
||||
ClassDB::bind_method(D_METHOD("handle_set_point"),
|
||||
&HandlePointSelection::handle_set_point);
|
||||
}
|
||||
};
|
||||
|
||||
class HandleCreateNewLine : public Object {
|
||||
GDCLASS(HandleCreateNewLine, Object)
|
||||
RoadLinesEditor *editor;
|
||||
@@ -211,6 +261,7 @@ protected:
|
||||
|
||||
static HandleSelection *selection_handler = nullptr;
|
||||
static HandleCreateNewLine *new_line_handler = nullptr;
|
||||
static HandlePointSelection *point_selection_handler = nullptr;
|
||||
|
||||
RoadLinesEditor::RoadLinesEditor(WorldEditor *editor)
|
||||
: active(false)
|
||||
@@ -231,10 +282,64 @@ Node *RoadLinesEditor::scene()
|
||||
}
|
||||
|
||||
static String current_line = "";
|
||||
void RoadLinesEditor::update_line_geometry()
|
||||
{
|
||||
if (!lines.has(current_line)) {
|
||||
if (line_im)
|
||||
line_im->clear();
|
||||
return;
|
||||
}
|
||||
if (line_im) {
|
||||
int i;
|
||||
line_im->clear();
|
||||
line_im->begin(Mesh::PRIMITIVE_LINES);
|
||||
line_im->set_color(Color(1.0f, 0.0f, 0.0f, 1.0f));
|
||||
line_im->add_vertex(Vector3(0.0f, -100.0f, 0.0f));
|
||||
line_im->set_color(Color(1.0f, 0.0f, 0.0f, 1.0f));
|
||||
line_im->add_vertex(Vector3(0.0f, 100.0f, 0.0f));
|
||||
line_im->end();
|
||||
if (lines[current_line].points.size() > 1) {
|
||||
line_im->begin(Mesh::PRIMITIVE_LINES);
|
||||
for (i = 0;
|
||||
i < (int)lines[current_line].points.size() - 1;
|
||||
i++) {
|
||||
Vector3 pt1 =
|
||||
lines[current_line].points[i].origin;
|
||||
Vector3 pt2 =
|
||||
lines[current_line].points[i + 1].origin;
|
||||
line_im->set_color(
|
||||
Color(0.0f, 0.0f, 0.5f, 1.0f));
|
||||
line_im->add_vertex(pt1);
|
||||
line_im->set_color(
|
||||
Color(0.0f, 0.0f, 0.5f, 1.0f));
|
||||
line_im->add_vertex(pt2);
|
||||
line_im->set_color(
|
||||
Color(0.0f, 0.0f, 0.5f, 1.0f));
|
||||
line_im->add_vertex(pt2);
|
||||
line_im->set_color(
|
||||
Color(0.0f, 0.0f, 0.5f, 1.0f));
|
||||
line_im->add_vertex(pt2 +
|
||||
Vector3(0.0f, 1.0f, 0.0f));
|
||||
}
|
||||
line_im->end();
|
||||
}
|
||||
}
|
||||
}
|
||||
void RoadLinesEditor::select_line(const String &line_name)
|
||||
{
|
||||
print_line("selected line: " + line_name);
|
||||
current_line = line_name;
|
||||
assert(lines.has(line_name));
|
||||
if (current_line != line_name) {
|
||||
SpinBox *sp_line_index = get_as_node<SpinBox>("%line_index");
|
||||
sp_line_index->set_max(lines[line_name].points.size() - 1);
|
||||
sp_line_index->set_min(0);
|
||||
current_line = line_name;
|
||||
sp_line_index->set_value(0);
|
||||
/* as actual index of SpinBox might not change
|
||||
call point selection explicitly */
|
||||
set_line_index(0);
|
||||
update_line_geometry();
|
||||
}
|
||||
update_ui();
|
||||
}
|
||||
|
||||
@@ -243,6 +348,17 @@ bool RoadLinesEditor::line_exists(const String &line_name)
|
||||
return lines.has(line_name);
|
||||
}
|
||||
|
||||
void RoadLinesEditor::set_point_to_cursor()
|
||||
{
|
||||
print_line("set_point_to_cursor");
|
||||
Spatial *cursor = get_as_node<Spatial>("%line_cursor");
|
||||
Transform xform = cursor->get_global_transform();
|
||||
SpinBox *sp_line_index = get_as_node<SpinBox>("%line_index");
|
||||
int index = (int)sp_line_index->get_value();
|
||||
lines[current_line].points[index].origin = xform.origin;
|
||||
update_line_geometry();
|
||||
}
|
||||
|
||||
void RoadLinesEditor::update(float delta)
|
||||
{
|
||||
if (!active)
|
||||
@@ -273,15 +389,8 @@ void RoadLinesEditor::editor_event(const String &event, const Array &args)
|
||||
print_line("RoadLinesEditor::event: " + event);
|
||||
if (event == "mouse_press") {
|
||||
if (cursor_enabled) {
|
||||
/* Raycasting outside physics process */
|
||||
Spatial *cursor = get_as_node<Spatial>("%line_cursor");
|
||||
/*
|
||||
var camera = get_viewport().get_camera()
|
||||
var start = camera.project_ray_origin(position)
|
||||
var normal = camera.project_ray_normal(position)
|
||||
var end = start + normal * camera.get_zfar()
|
||||
var space_state = get_world().direct_space_state
|
||||
var result = space_state.intersect_ray(start, end, [], 1 << 15, false, true)
|
||||
*/
|
||||
Vector2 position = args[0];
|
||||
Camera *cam = editor->get_viewport()->get_camera();
|
||||
Vector3 start = cam->project_ray_origin(position);
|
||||
@@ -330,7 +439,8 @@ void RoadLinesEditor::update_ui()
|
||||
e = e->next();
|
||||
index++;
|
||||
}
|
||||
lines_list->set_current(selected_index);
|
||||
if (selected_index >= 0)
|
||||
lines_list->set_current(selected_index);
|
||||
}
|
||||
|
||||
void RoadLinesEditor::create_new_line_at_cursor(const String &line_name)
|
||||
@@ -338,6 +448,18 @@ void RoadLinesEditor::create_new_line_at_cursor(const String &line_name)
|
||||
print_line("creating new line called: " + line_name);
|
||||
}
|
||||
|
||||
void RoadLinesEditor::set_line_index(int index)
|
||||
{
|
||||
assert(lines.has(current_line));
|
||||
assert(index < (int)lines[current_line].points.size());
|
||||
Vector3 cursor_position = lines[current_line].points[index].origin;
|
||||
Spatial *cursor = get_as_node<Spatial>("%line_cursor");
|
||||
cursor->set_global_transform(Transform(Basis(), cursor_position));
|
||||
Array pargs;
|
||||
pargs.push_back(cursor_position);
|
||||
editor->emit_signal("editor_event", "line_cursor_motion", pargs);
|
||||
}
|
||||
|
||||
void RoadLinesEditor::activate()
|
||||
{
|
||||
assert(!active);
|
||||
@@ -368,6 +490,8 @@ void RoadLinesEditor::activate()
|
||||
selection_handler = memnew(HandleSelection(this));
|
||||
if (!new_line_handler)
|
||||
new_line_handler = memnew(HandleCreateNewLine(this));
|
||||
if (!point_selection_handler)
|
||||
point_selection_handler = memnew(HandlePointSelection(this));
|
||||
|
||||
active = true;
|
||||
}
|
||||
@@ -399,6 +523,10 @@ void RoadLinesEditor::deactivate()
|
||||
memdelete(gd_editor_event);
|
||||
gd_editor_event = nullptr;
|
||||
}
|
||||
if (point_selection_handler) {
|
||||
memdelete(point_selection_handler);
|
||||
point_selection_handler = nullptr;
|
||||
}
|
||||
active = false;
|
||||
}
|
||||
|
||||
|
||||
@@ -11,8 +11,10 @@ public:
|
||||
RoadLinesEditor(WorldEditor *editor);
|
||||
virtual ~RoadLinesEditor();
|
||||
Node *scene();
|
||||
void update_line_geometry();
|
||||
void select_line(const String &line_name);
|
||||
bool line_exists(const String &line_name);
|
||||
void set_point_to_cursor();
|
||||
void update(float delta);
|
||||
void exit();
|
||||
void editor_command(const String &command, const Array &args);
|
||||
@@ -20,6 +22,7 @@ public:
|
||||
int get_camera_mode() const;
|
||||
void update_ui();
|
||||
void create_new_line_at_cursor(const String &line_name);
|
||||
void set_line_index(int index);
|
||||
template <class T> T *get_as_node(const String &path);
|
||||
|
||||
protected:
|
||||
|
||||
Reference in New Issue
Block a user