Working on map lines editing GUI

This commit is contained in:
2024-08-29 03:22:21 +03:00
parent bbb13546b1
commit 4f1477982a
4 changed files with 308 additions and 43 deletions

View File

@@ -56,7 +56,7 @@ anchor_bottom = 1.0
margin_left = -160.0
[node name="ColorRect" type="ColorRect" parent="VBoxContainer"]
margin_right = 160.0
margin_right = 162.0
margin_bottom = 30.0
rect_min_size = Vector2( 160, 30 )
size_flags_horizontal = 3
@@ -77,58 +77,58 @@ valign = 1
[node name="select_buildings" type="Button" parent="VBoxContainer"]
unique_name_in_owner = true
margin_top = 34.0
margin_right = 160.0
margin_right = 162.0
margin_bottom = 54.0
text = "Buildings Mode"
[node name="select_navigation" type="Button" parent="VBoxContainer"]
unique_name_in_owner = true
margin_top = 58.0
margin_right = 160.0
margin_right = 162.0
margin_bottom = 78.0
text = "Navigation Mode"
[node name="select_poi" type="Button" parent="VBoxContainer"]
unique_name_in_owner = true
margin_top = 82.0
margin_right = 160.0
margin_right = 162.0
margin_bottom = 102.0
text = "POI Mode"
[node name="select_road_lines" type="Button" parent="VBoxContainer"]
unique_name_in_owner = true
margin_top = 106.0
margin_right = 160.0
margin_right = 162.0
margin_bottom = 126.0
text = "Road Lines Mode"
[node name="select_npc" type="Button" parent="VBoxContainer"]
unique_name_in_owner = true
margin_top = 130.0
margin_right = 160.0
margin_right = 162.0
margin_bottom = 150.0
text = "NPC Mode"
[node name="v_buildings" type="VBoxContainer" parent="VBoxContainer"]
unique_name_in_owner = true
margin_top = 154.0
margin_right = 160.0
margin_right = 162.0
margin_bottom = 274.0
[node name="HSeparator" type="HSeparator" parent="VBoxContainer/v_buildings"]
margin_right = 160.0
margin_right = 162.0
margin_bottom = 4.0
[node name="Label" type="Label" parent="VBoxContainer/v_buildings"]
margin_top = 8.0
margin_right = 160.0
margin_right = 162.0
margin_bottom = 22.0
text = "Buildings mode"
[node name="buildings_edit_mode" type="OptionButton" parent="VBoxContainer/v_buildings"]
unique_name_in_owner = true
margin_top = 26.0
margin_right = 160.0
margin_right = 162.0
margin_bottom = 46.0
text = "Select"
items = [ "Select", null, false, 0, null, "Move", null, false, 1, null, "Rotate", null, false, 2, null ]
@@ -136,98 +136,162 @@ selected = 0
[node name="Label2" type="Label" parent="VBoxContainer/v_buildings"]
margin_top = 50.0
margin_right = 160.0
margin_right = 162.0
margin_bottom = 64.0
text = "Building type"
[node name="building_type" type="OptionButton" parent="VBoxContainer/v_buildings"]
unique_name_in_owner = true
margin_top = 68.0
margin_right = 160.0
margin_right = 162.0
margin_bottom = 88.0
text = "Building Type"
[node name="HSeparator2" type="HSeparator" parent="VBoxContainer/v_buildings"]
margin_top = 92.0
margin_right = 160.0
margin_right = 162.0
margin_bottom = 96.0
[node name="buildings_save" type="Button" parent="VBoxContainer/v_buildings"]
unique_name_in_owner = true
margin_top = 100.0
margin_right = 160.0
margin_right = 162.0
margin_bottom = 120.0
text = "Save Buildings"
[node name="v_navigation" type="VBoxContainer" parent="VBoxContainer"]
unique_name_in_owner = true
margin_top = 278.0
margin_right = 160.0
margin_right = 162.0
margin_bottom = 300.0
[node name="HSeparator" type="HSeparator" parent="VBoxContainer/v_navigation"]
margin_right = 160.0
margin_right = 162.0
margin_bottom = 4.0
[node name="Label" type="Label" parent="VBoxContainer/v_navigation"]
margin_top = 8.0
margin_right = 160.0
margin_right = 162.0
margin_bottom = 22.0
text = "Navigation mode"
[node name="v_poi" type="VBoxContainer" parent="VBoxContainer"]
unique_name_in_owner = true
margin_top = 304.0
margin_right = 160.0
margin_right = 162.0
margin_bottom = 326.0
[node name="HSeparator" type="HSeparator" parent="VBoxContainer/v_poi"]
margin_right = 160.0
margin_right = 162.0
margin_bottom = 4.0
[node name="Label" type="Label" parent="VBoxContainer/v_poi"]
margin_top = 8.0
margin_right = 160.0
margin_right = 162.0
margin_bottom = 22.0
text = "POI mode"
[node name="v_road_lines" type="VBoxContainer" parent="VBoxContainer"]
unique_name_in_owner = true
margin_top = 330.0
margin_right = 160.0
margin_bottom = 456.0
margin_right = 162.0
margin_bottom = 636.0
[node name="HSeparator" type="HSeparator" parent="VBoxContainer/v_road_lines"]
margin_right = 160.0
margin_right = 162.0
margin_bottom = 4.0
[node name="Label" type="Label" parent="VBoxContainer/v_road_lines"]
[node name="road_lines_base" type="PanelContainer" parent="VBoxContainer/v_road_lines"]
unique_name_in_owner = true
margin_top = 8.0
margin_right = 160.0
margin_bottom = 22.0
margin_right = 162.0
margin_bottom = 240.0
[node name="VBoxContainer" type="VBoxContainer" parent="VBoxContainer/v_road_lines/road_lines_base"]
margin_left = 7.0
margin_top = 7.0
margin_right = 155.0
margin_bottom = 225.0
size_flags_horizontal = 3
size_flags_vertical = 3
[node name="Label" type="Label" parent="VBoxContainer/v_road_lines/road_lines_base/VBoxContainer"]
margin_right = 148.0
margin_bottom = 14.0
text = "Road Lines mode"
[node name="lines_list" type="ItemList" parent="VBoxContainer/v_road_lines"]
[node name="lines_list" type="ItemList" parent="VBoxContainer/v_road_lines/road_lines_base/VBoxContainer"]
unique_name_in_owner = true
margin_top = 26.0
margin_right = 160.0
margin_bottom = 126.0
margin_top = 18.0
margin_right = 148.0
margin_bottom = 118.0
rect_min_size = Vector2( 0, 100 )
size_flags_horizontal = 3
[node name="line_index" type="SpinBox" parent="VBoxContainer/v_road_lines/road_lines_base/VBoxContainer"]
unique_name_in_owner = true
margin_top = 122.0
margin_right = 148.0
margin_bottom = 146.0
[node name="road_lines_move_point" type="Button" parent="VBoxContainer/v_road_lines/road_lines_base/VBoxContainer"]
unique_name_in_owner = true
margin_top = 150.0
margin_right = 148.0
margin_bottom = 170.0
text = "Move Point To Cursor"
[node name="road_lines_create_point" type="Button" parent="VBoxContainer/v_road_lines/road_lines_base/VBoxContainer"]
unique_name_in_owner = true
margin_top = 174.0
margin_right = 148.0
margin_bottom = 194.0
text = "Create Point"
[node name="road_lines_create_new_line" type="Button" parent="VBoxContainer/v_road_lines/road_lines_base/VBoxContainer"]
unique_name_in_owner = true
margin_top = 198.0
margin_right = 148.0
margin_bottom = 218.0
text = "Creeate New Line"
[node name="road_lines_create_new_line_dlg" type="PanelContainer" parent="VBoxContainer/v_road_lines"]
unique_name_in_owner = true
margin_top = 244.0
margin_right = 162.0
margin_bottom = 306.0
[node name="v" type="VBoxContainer" parent="VBoxContainer/v_road_lines/road_lines_create_new_line_dlg"]
margin_left = 7.0
margin_top = 7.0
margin_right = 155.0
margin_bottom = 55.0
[node name="road_lines_create_new_line_name" type="LineEdit" parent="VBoxContainer/v_road_lines/road_lines_create_new_line_dlg/v"]
unique_name_in_owner = true
margin_right = 148.0
margin_bottom = 24.0
[node name="road_lines_create_new_cancel" type="Button" parent="VBoxContainer/v_road_lines/road_lines_create_new_line_dlg/v"]
unique_name_in_owner = true
margin_top = 28.0
margin_right = 148.0
margin_bottom = 48.0
text = "Cancel"
[node name="v_npc" type="VBoxContainer" parent="VBoxContainer"]
unique_name_in_owner = true
margin_top = 460.0
margin_right = 160.0
margin_bottom = 482.0
margin_top = 640.0
margin_right = 162.0
margin_bottom = 662.0
[node name="HSeparator" type="HSeparator" parent="VBoxContainer/v_npc"]
margin_right = 160.0
margin_right = 162.0
margin_bottom = 4.0
[node name="Label" type="Label" parent="VBoxContainer/v_npc"]
margin_top = 8.0
margin_right = 160.0
margin_right = 162.0
margin_bottom = 22.0
text = "NPC mode"

View File

@@ -2,6 +2,8 @@
#include <cassert>
#include <scene/main/viewport.h>
#include <scene/gui/item_list.h>
#include <scene/gui/button.h>
#include <scene/gui/line_edit.h>
#include <scene/3d/immediate_geometry.h>
#include <core/io/config_file.h>
#include <core/os/file_access.h>
@@ -62,6 +64,153 @@ static Ref<Material> debug_material;
__evhandler(editor_event, RoadLinesEditor);
static __evhandler_type(editor_event, RoadLinesEditor) * gd_editor_event;
class HandleSelection : public Object {
GDCLASS(HandleSelection, Object)
RoadLinesEditor *editor;
public:
HandleSelection(RoadLinesEditor *editor)
: Object()
, editor(editor)
{
Node *lines_list_node =
editor->scene()->get_node(NodePath("%lines_list"));
ItemList *lines_list =
Object::cast_to<ItemList>(lines_list_node);
lines_list->connect("item_selected", this, "handler");
}
virtual ~HandleSelection()
{
Node *lines_list_node =
editor->scene()->get_node(NodePath("%lines_list"));
ItemList *lines_list =
Object::cast_to<ItemList>(lines_list_node);
lines_list->disconnect("item_selected", this, "handler");
}
protected:
void handler(int index)
{
if (index < 0)
return;
Node *lines_list_node =
editor->scene()->get_node(NodePath("%lines_list"));
ItemList *lines_list =
Object::cast_to<ItemList>(lines_list_node);
editor->select_line(lines_list->get_item_text(index));
}
static void _bind_methods()
{
ClassDB::bind_method(D_METHOD("handler", "index"),
&HandleSelection::handler);
}
};
class HandleCreateNewLine : public Object {
GDCLASS(HandleCreateNewLine, Object)
RoadLinesEditor *editor;
public:
HandleCreateNewLine(RoadLinesEditor *editor)
: Object()
, editor(editor)
{
Button *main_button = editor->get_as_node<Button>(
"%road_lines_create_new_line");
Button *cancel_button = editor->get_as_node<Button>(
"%road_lines_create_new_cancel");
LineEdit *line_name = editor->get_as_node<LineEdit>(
"%road_lines_create_new_line_name");
main_button->connect("pressed", this, "main_handler");
cancel_button->connect("pressed", this, "cancel_handler");
line_name->connect("text_entered", this, "entered_handler");
line_name->connect("text_changed", this, "changed_handler");
}
virtual ~HandleCreateNewLine()
{
Button *main_button = editor->get_as_node<Button>(
"%road_lines_create_new_line");
Button *cancel_button = editor->get_as_node<Button>(
"%road_lines_create_new_cancel");
LineEdit *line_name = editor->get_as_node<LineEdit>(
"%road_lines_create_new_line_name");
line_name->disconnect("text_changed", this, "changed_handler");
line_name->disconnect("text_entered", this, "entered_handler");
cancel_button->disconnect("pressed", this, "cancel_handler");
main_button->disconnect("pressed", this, "main_handler");
}
protected:
void main_handler()
{
editor->get_as_node<Control>("%road_lines_base")->hide();
editor->get_as_node<Control>("%road_lines_create_new_line_dlg")
->show();
}
void cancel_handler()
{
LineEdit *line_name = editor->get_as_node<LineEdit>(
"%road_lines_create_new_line_name");
editor->get_as_node<Control>("%road_lines_base")->show();
editor->get_as_node<Control>("%road_lines_create_new_line_dlg")
->hide();
line_name->set_text("");
}
bool check_line_name(const String &line_name)
{
String text = line_name.strip_edges();
if (editor->line_exists(text) || text.length() < 5 ||
text.find("_") < 0 || text.ends_with("_"))
return false;
return true;
}
void entered_handler(const String &new_text)
{
String text = new_text.strip_edges();
LineEdit *line_name = editor->get_as_node<LineEdit>(
"%road_lines_create_new_line_name");
if (!check_line_name(text)) {
line_name->add_color_override(
"font_color", Color(1.0f, 0.0f, 0.0f, 1.0f));
return;
} else
line_name->remove_color_override("font_color");
/* TODO: do what is needed to create line in editor */
editor->create_new_line_at_cursor(text);
/* clean text */
line_name->set_text("");
editor->get_as_node<Control>("%road_lines_base")->show();
editor->get_as_node<Control>("%road_lines_create_new_line_dlg")
->hide();
}
void changed_handler(const String &new_text)
{
LineEdit *line_name = editor->get_as_node<LineEdit>(
"%road_lines_create_new_line_name");
String text = new_text.strip_edges();
if (!check_line_name(text)) {
line_name->add_color_override(
"font_color", Color(1.0f, 0.0f, 0.0f, 1.0f));
return;
} else
line_name->remove_color_override("font_color");
}
static void _bind_methods()
{
ClassDB::bind_method(D_METHOD("main_handler"),
&HandleCreateNewLine::main_handler);
ClassDB::bind_method(D_METHOD("cancel_handler"),
&HandleCreateNewLine::cancel_handler);
ClassDB::bind_method(D_METHOD("entered_handler", "new_text"),
&HandleCreateNewLine::entered_handler);
ClassDB::bind_method(D_METHOD("changed_handler", "new_text"),
&HandleCreateNewLine::changed_handler);
}
};
static HandleSelection *selection_handler = nullptr;
static HandleCreateNewLine *new_line_handler = nullptr;
RoadLinesEditor::RoadLinesEditor(WorldEditor *editor)
: active(false)
, editor(editor)
@@ -74,11 +223,29 @@ RoadLinesEditor::~RoadLinesEditor()
deactivate();
}
Node *RoadLinesEditor::scene()
{
return editor->get_tree()->get_current_scene();
}
static String current_line = "";
void RoadLinesEditor::select_line(const String &line_name)
{
print_line("selected line: " + line_name);
current_line = line_name;
update_ui();
}
bool RoadLinesEditor::line_exists(const String &line_name)
{
return lines.has(line_name);
}
void RoadLinesEditor::update(float delta)
{
if (!active)
activate();
print_line("road_lines_editor");
// print_line("road_lines_editor");
}
void RoadLinesEditor::exit()
@@ -99,20 +266,29 @@ void RoadLinesEditor::editor_event(const String &event, const Array &args)
void RoadLinesEditor::update_ui()
{
Node *lines_list_node =
editor->get_tree()->get_current_scene()->get_node(
NodePath("%lines_list"));
ItemList *lines_list = Object::cast_to<ItemList>(lines_list_node);
assert(lines_list);
get_as_node<Control>("%road_lines_base")->show();
get_as_node<Control>("%road_lines_create_new_line_dlg")->hide();
ItemList *lines_list = get_as_node<ItemList>("%lines_list");
List<String> line_keys;
lines.get_key_list(&line_keys);
List<String>::Element *e = line_keys.front();
lines_list->clear();
int selected_index = -1;
int index = 0;
while (e) {
String key = e->get();
if (key == current_line)
selected_index = index;
lines_list->add_item(key);
e = e->next();
index++;
}
lines_list->set_current(selected_index);
}
void RoadLinesEditor::create_new_line_at_cursor(const String &line_name)
{
print_line("creating new line called: " + line_name);
}
void RoadLinesEditor::activate()
@@ -131,6 +307,7 @@ void RoadLinesEditor::activate()
tmpmat->set_flag(SpatialMaterial::FLAG_DISABLE_DEPTH_TEST, true);
tmpmat->set_flag(SpatialMaterial::FLAG_UNSHADED, true);
debug_material = tmpmat;
line_im->set_material_override(debug_material);
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));
@@ -140,6 +317,10 @@ void RoadLinesEditor::activate()
gd_editor_event = memnew(
__evhandler_type(editor_event, RoadLinesEditor)(this));
gd_editor_event->connect(editor, "editor_event");
if (!selection_handler)
selection_handler = memnew(HandleSelection(this));
if (!new_line_handler)
new_line_handler = memnew(HandleCreateNewLine(this));
active = true;
}
@@ -159,6 +340,14 @@ void RoadLinesEditor::deactivate()
gd_editor_event->disconnect(editor, "editor_event");
if (debug_material.is_valid())
debug_material.unref();
if (new_line_handler) {
memdelete(new_line_handler);
new_line_handler = nullptr;
}
if (selection_handler) {
memdelete(selection_handler);
selection_handler = nullptr;
}
if (gd_editor_event) {
memdelete(gd_editor_event);
gd_editor_event = nullptr;
@@ -213,3 +402,11 @@ void RoadLinesEditor::load_data()
e = e->next();
}
}
template <class T> T *RoadLinesEditor::get_as_node(const String &path)
{
Node *node = scene()->get_node(NodePath(path));
assert(node);
T *ret = Object::cast_to<T>(node);
assert(ret);
return ret;
}

View File

@@ -9,12 +9,16 @@ class RoadLinesEditor {
public:
RoadLinesEditor(WorldEditor *editor);
virtual ~RoadLinesEditor();
Node *scene();
void select_line(const String &line_name);
bool line_exists(const String &line_name);
void update(float delta);
void exit();
void editor_command(const String &command, const Array &args);
void editor_event(const String &event, const Array &args);
void update_ui();
void create_new_line_at_cursor(const String &line_name);
template <class T> T *get_as_node(const String &path);
protected:
void activate();

View File

@@ -345,7 +345,7 @@ void WorldEditor::_notification(int which)
drag_delay -= delta;
switch (current_mode) {
case MODE_ROAD_LINES:
print_line("current_mode: " + itos(current_mode));
// print_line("current_mode: " + itos(current_mode));
road_lines_editor->update(delta);
break;
}