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

@@ -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;
}