From 28dbf374e8d0c16c95cd45abd219def5a4e8e133 Mon Sep 17 00:00:00 2001 From: T0b1 Date: Wed, 21 Jun 2023 02:19:21 +0200 Subject: [PATCH] variable WIP --- src/frontend/window.cpp | 77 +++++++++++++++++++++++++++++++++++------ 1 file changed, 67 insertions(+), 10 deletions(-) diff --git a/src/frontend/window.cpp b/src/frontend/window.cpp index 9293cf9..30273d0 100644 --- a/src/frontend/window.cpp +++ b/src/frontend/window.cpp @@ -1068,20 +1068,14 @@ bool WatchWindow::draw(Frontend &frontend) for (size_t i = 0; i < extra_slots.size(); ++i) { - auto res_idx = frontend.target->data_idx_for_src_id(extra_slots[i].id); + auto res_idx = frontend.target->data_idx_for_src_id(extra_slots[i].id); + auto no_success = true; if (res_idx) { const auto &node = *frontend.target->data_res_nodes[*res_idx]; - if (!node.success || node.children.size() != 1) - { - // TODO: allow edit - ImGui::TableNextRow(); - ImGui::TableSetColumnIndex(0); - ImGui::Text("%s", extra_slots[i].bak.c_str()); - ImGui::TableNextColumn(); - ImGui::TextDisabled(""); - } else + if (node.success && node.children.size() == 1) { + no_success = false; const auto &child_node = *frontend.target->data_res_nodes[node.children[0]]; this->draw_value(frontend, child_node.type_id, &extra_slots[i], @@ -1096,6 +1090,69 @@ bool WatchWindow::draw(Frontend &frontend) } } } + if (no_success) + { + auto &slot = extra_slots[i]; + ImGui::TableNextRow(); + ImGui::TableSetColumnIndex(0); + + auto is_editing = false; + if (slot.is_editing) + { + is_editing = true; + if (slot.edit_was_started) + { + slot.edit_was_started = false; + ImGui::SetKeyboardFocusHere(); + } + if (ImGui::InputText("##dddd", slot.buf, sizeof(slot.buf), + ImGuiInputTextFlags_AutoSelectAll + | ImGuiInputTextFlags_EnterReturnsTrue) + && ImGui::IsItemDeactivatedAfterEdit()) + { + if (slot.buf[0] != '\0') + { + frontend.target->backend->remove_data_node(slot.id); + using namespace data::source; + frontend.target->backend->add_data_node(Node{ + .id = slot.id, + .type = Node::Type::source, + .data = + Source{.type = Source::Type::variable, + .data = Source::Variable{.expr_path = slot.buf}}}); + } + + slot.bak = slot.buf; + slot.is_editing = false; + } + if (ImGui::IsItemDeactivated()) + { + memcpy(slot.buf, slot.bak.data(), slot.bak.size()); + slot.buf[slot.bak.size()] = '\0'; + slot.is_editing = false; + } + } + + if (!is_editing) + { + char tree_id_buf[128]; + std::snprintf(tree_id_buf, sizeof(tree_id_buf), "slot%lu", i); + // TODO: better id + ImGui::TreeNodeEx(tree_id_buf, + ImGuiTreeNodeFlags_SpanFullWidth + | ImGuiTreeNodeFlags_Leaf + | ImGuiTreeNodeFlags_NoTreePushOnOpen, + "%s", slot.bak.c_str()); + if (ImGui::IsMouseDoubleClicked(ImGuiMouseButton_Left) + && ImGui::IsItemHovered()) + { + slot.is_editing = true; + slot.edit_was_started = true; + } + } + ImGui::TableNextColumn(); + ImGui::TextDisabled(""); + } } ImGui::TableNextRow();