Point create/Delete and cursor lineedit display

This commit is contained in:
2024-09-03 22:42:28 +03:00
parent 802ff0fd53
commit ab9387a06a
2 changed files with 108 additions and 27 deletions

View File

@@ -225,10 +225,10 @@ protected:
{ {
switch (id) { switch (id) {
case 11: case 11:
/* TODO: create point */ editor->line_create_point();
break; break;
case 12: case 12:
/* TODO: delete point */ editor->line_delete_point();
break; break;
case 21: case 21:
/* Create line */ /* Create line */
@@ -382,15 +382,20 @@ void RoadLinesEditor::update_line_geometry()
} }
} }
} }
void RoadLinesEditor::update_line_index_ui()
{
SpinBox *sp_line_index = get_as_node<SpinBox>("%line_index");
sp_line_index->set_max(lines[current_line].points.size() - 1);
sp_line_index->set_min(0);
}
void RoadLinesEditor::select_line(const String &line_name) void RoadLinesEditor::select_line(const String &line_name)
{ {
print_line("selected line: " + line_name); print_line("selected line: " + line_name);
assert(lines.has(line_name)); assert(lines.has(line_name));
if (current_line != 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; current_line = line_name;
update_line_index_ui();
SpinBox *sp_line_index = get_as_node<SpinBox>("%line_index");
sp_line_index->set_value(0); sp_line_index->set_value(0);
/* as actual index of SpinBox might not change /* as actual index of SpinBox might not change
call point selection explicitly */ call point selection explicitly */
@@ -405,25 +410,67 @@ bool RoadLinesEditor::line_exists(const String &line_name)
return lines.has(line_name); return lines.has(line_name);
} }
void RoadLinesEditor::line_create_point()
{
/* TODO: Create point in line */
print_line("line_create_point");
Vector3 position = get_cursor_position();
Transform xform(Basis(), position);
int index = get_line_index();
lines[current_line].points.insert(
lines[current_line].points.begin() + index + 1, xform);
update_line_geometry();
update_line_index_ui();
SpinBox *sp_line_index = get_as_node<SpinBox>("%line_index");
sp_line_index->set_value(index + 1);
// move_cursor_to_point();
}
void RoadLinesEditor::line_delete_point()
{
/* TODO: Delete point from line */
print_line("line_delete_point");
int index = get_line_index();
if (lines[current_line].points.size() < 2)
return;
lines[current_line].points.erase(lines[current_line].points.begin() +
index);
update_line_geometry();
update_line_index_ui();
SpinBox *sp_line_index = get_as_node<SpinBox>("%line_index");
sp_line_index->set_value(0);
index = MAX(0, index - 1);
sp_line_index->set_value(index);
}
static const String cursor_name = "%line_cursor";
void RoadLinesEditor::set_point_to_cursor() void RoadLinesEditor::set_point_to_cursor()
{ {
print_line("set_point_to_cursor"); print_line("set_point_to_cursor");
Spatial *cursor = get_as_node<Spatial>("%line_cursor"); Spatial *cursor = get_as_node<Spatial>(cursor_name);
Transform xform = cursor->get_global_transform(); Transform xform = cursor->get_global_transform();
SpinBox *sp_line_index = get_as_node<SpinBox>("%line_index"); int index = get_line_index();
int index = (int)sp_line_index->get_value();
lines[current_line].points[index].origin = xform.origin; lines[current_line].points[index].origin = xform.origin;
update_line_geometry(); update_line_geometry();
set_ui_point_position(lines[current_line].points[index].origin);
}
int RoadLinesEditor::get_line_index()
{
print_line("get_line_index");
SpinBox *sp_line_index = get_as_node<SpinBox>("%line_index");
int index = (int)sp_line_index->get_value();
return index;
} }
void RoadLinesEditor::move_cursor_to_point() void RoadLinesEditor::move_cursor_to_point()
{ {
print_line("move_cursor_to_point"); print_line("move_cursor_to_point");
Spatial *cursor = get_as_node<Spatial>("%line_cursor"); int index = get_line_index();
SpinBox *sp_line_index = get_as_node<SpinBox>("%line_index");
int index = (int)sp_line_index->get_value();
Transform xform(Basis(), lines[current_line].points[index].origin); Transform xform(Basis(), lines[current_line].points[index].origin);
cursor->set_global_transform(xform); set_cursor_position(xform.origin);
set_ui_cursor_position(xform.origin);
} }
void RoadLinesEditor::update(float delta) void RoadLinesEditor::update(float delta)
@@ -433,10 +480,10 @@ void RoadLinesEditor::update(float delta)
// print_line("road_lines_editor"); // print_line("road_lines_editor");
if (!cursor_enabled && get_camera_mode() == 3) { if (!cursor_enabled && get_camera_mode() == 3) {
cursor_enabled = true; cursor_enabled = true;
get_as_node<Spatial>("%line_cursor")->show(); get_as_node<Spatial>(cursor_name)->show();
} else if (cursor_enabled && get_camera_mode() != 3) { } else if (cursor_enabled && get_camera_mode() != 3) {
cursor_enabled = false; cursor_enabled = false;
get_as_node<Spatial>("%line_cursor")->hide(); get_as_node<Spatial>(cursor_name)->hide();
} }
} }
@@ -457,7 +504,6 @@ void RoadLinesEditor::editor_event(const String &event, const Array &args)
if (event == "mouse_press" || event == "mouse_drag") { if (event == "mouse_press" || event == "mouse_drag") {
if (cursor_enabled) { if (cursor_enabled) {
/* Raycasting outside physics process */ /* Raycasting outside physics process */
Spatial *cursor = get_as_node<Spatial>("%line_cursor");
Vector2 position = args[0]; Vector2 position = args[0];
Camera *cam = editor->get_viewport()->get_camera(); Camera *cam = editor->get_viewport()->get_camera();
Vector3 start = cam->project_ray_origin(position); Vector3 start = cam->project_ray_origin(position);
@@ -482,13 +528,8 @@ void RoadLinesEditor::editor_event(const String &event, const Array &args)
(1 << 15) | (1 << 0), true, (1 << 15) | (1 << 0), true,
true); true);
if (result.rid != RID()) { if (result.rid != RID()) {
cursor->set_global_transform( set_cursor_position(result.position);
Transform(Basis(), result.position)); set_ui_cursor_position(result.position);
Array pargs;
pargs.push_back(result.position);
editor->emit_signal("editor_event",
"line_cursor_motion",
pargs);
} }
end:; end:;
} }
@@ -539,17 +580,49 @@ void RoadLinesEditor::create_new_line_at_cursor(const String &line_name)
print_line("creating new line called: " + line_name); print_line("creating new line called: " + line_name);
} }
void RoadLinesEditor::set_line_index(int index) Vector3 RoadLinesEditor::get_cursor_position()
{ {
assert(lines.has(current_line)); Spatial *cursor = get_as_node<Spatial>(cursor_name);
assert(index < (int)lines[current_line].points.size()); return cursor->get_global_transform().origin;
Vector3 cursor_position = lines[current_line].points[index].origin; }
Spatial *cursor = get_as_node<Spatial>("%line_cursor");
void RoadLinesEditor::set_cursor_position(const Vector3 &cursor_position)
{
Spatial *cursor = get_as_node<Spatial>(cursor_name);
cursor->set_global_transform(Transform(Basis(), cursor_position)); cursor->set_global_transform(Transform(Basis(), cursor_position));
Array pargs; Array pargs;
pargs.push_back(cursor_position); pargs.push_back(cursor_position);
editor->emit_signal("editor_event", "line_cursor_motion", pargs); editor->emit_signal("editor_event", "line_cursor_motion", pargs);
} }
void RoadLinesEditor::set_ui_cursor_position(const Vector3 &cursor_position)
{
LineEdit *cursor_x = get_as_node<LineEdit>("%cursor_x");
LineEdit *cursor_y = get_as_node<LineEdit>("%cursor_y");
LineEdit *cursor_z = get_as_node<LineEdit>("%cursor_z");
cursor_x->set_text(String::num(cursor_position.x));
cursor_y->set_text(String::num(cursor_position.y));
cursor_z->set_text(String::num(cursor_position.z));
}
void RoadLinesEditor::set_ui_point_position(const Vector3 &point_position)
{
LineEdit *point_x = get_as_node<LineEdit>("%point_x");
LineEdit *point_y = get_as_node<LineEdit>("%point_y");
LineEdit *point_z = get_as_node<LineEdit>("%point_z");
point_x->set_text(String::num(point_position.x));
point_y->set_text(String::num(point_position.y));
point_z->set_text(String::num(point_position.z));
}
void RoadLinesEditor::set_line_index(int index)
{
assert(lines.has(current_line));
assert(index < (int)lines[current_line].points.size());
Vector3 point_position = lines[current_line].points[index].origin;
Vector3 cursor_position = point_position;
set_cursor_position(cursor_position);
set_ui_cursor_position(cursor_position);
set_ui_point_position(point_position);
}
void RoadLinesEditor::line_list_filter_changed(const String &text) void RoadLinesEditor::line_list_filter_changed(const String &text)
{ {

View File

@@ -15,9 +15,13 @@ public:
virtual ~RoadLinesEditor(); virtual ~RoadLinesEditor();
Node *scene(); Node *scene();
void update_line_geometry(); void update_line_geometry();
void update_line_index_ui();
void select_line(const String &line_name); void select_line(const String &line_name);
bool line_exists(const String &line_name); bool line_exists(const String &line_name);
void line_create_point();
void line_delete_point();
void set_point_to_cursor(); void set_point_to_cursor();
int get_line_index();
void move_cursor_to_point(); void move_cursor_to_point();
void update(float delta); void update(float delta);
void exit(); void exit();
@@ -26,6 +30,10 @@ public:
int get_camera_mode() const; int get_camera_mode() const;
void update_ui(); void update_ui();
void create_new_line_at_cursor(const String &line_name); void create_new_line_at_cursor(const String &line_name);
Vector3 get_cursor_position();
void set_cursor_position(const Vector3 &cursor_position);
void set_ui_cursor_position(const Vector3 &cursor_position);
void set_ui_point_position(const Vector3 &point_position);
void set_line_index(int index); void set_line_index(int index);
void line_list_filter_changed(const String &text); void line_list_filter_changed(const String &text);
template <class T> T *get_as_node(const String &path); template <class T> T *get_as_node(const String &path);