Skip to content

Commit

Permalink
implement JSON editor
Browse files Browse the repository at this point in the history
  • Loading branch information
bb1950328 committed Jan 3, 2024
1 parent e3d1220 commit 89bee1d
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 9 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,7 @@ target_sources(ImGuiColorTextEdit PRIVATE
src/lib/ImGuiColorTextEdit/TextEditor.cpp
src/lib/ImGuiColorTextEdit/TextEditor.h
)
target_compile_definitions(ImGuiColorTextEdit PRIVATE IMGUI_DISABLE_OBSOLETE_FUNCTIONS)
target_include_directories(ImGuiColorTextEdit PRIVATE src/lib/imgui)
target_include_directories(ImGuiColorTextEdit INTERFACE src/lib/ImGuiColorTextEdit)

Expand Down
63 changes: 55 additions & 8 deletions src/gui/windows/window_settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "imgui_stdlib.h"
#include "window_settings.h"

#include "TextEditor.h"
#include "imgui_internal.h"
#include "../../info_providers/bricklink_constants_provider.h"

Expand Down Expand Up @@ -39,7 +40,7 @@ namespace bricksim::gui::windows::settings {
ImGui::SameLine();
if (ImGui::Button(ICON_FA_FOLDER_OPEN, {promptButtonSize, promptButtonSize})) {
char* selectedPath;
const auto extendedPath = util::replaceSpecialPaths(path);
const auto extendedPath = util::replaceSpecialPaths(path).string();
if (isDirectory) {
selectedPath = tinyfd_selectFolderDialog(label, extendedPath.c_str());
} else {
Expand Down Expand Up @@ -442,6 +443,12 @@ namespace bricksim::gui::windows::settings {
return "Settings";
}

struct JsonEditorData {
TextEditor editor;
std::string originalJson;
std::string errorMessage;
};

template<typename D>
void drawTabs(D& data) {
if (ImGui::BeginTabBar("##settingsTabs")) {
Expand All @@ -450,19 +457,59 @@ namespace bricksim::gui::windows::settings {
ImGui::EndTabItem();
}
if (ImGui::BeginTabItem("JSON")) {
const auto json = json_dto::to_json(data);
//todo make this an ImGuiColorTextEdit
ImGui::Text("%s", json.c_str());
static uomap_t<std::size_t, JsonEditorData> allEditors;
auto it = allEditors.find(typeid(D).hash_code());
if (it == allEditors.end()) {
JsonEditorData editorData;
//editorData.originalJson = json_helper::to_pretty_json(data);
it = allEditors.emplace(typeid(D).hash_code(), editorData).first;
}
auto& editorData = it->second;
auto& editor = editorData.editor;

const auto currentDataJson = json_helper::to_pretty_json(data);
if (currentDataJson != editorData.originalJson) {
//changed outside of editor
editorData.originalJson = currentDataJson;
editor.SetText(currentDataJson);
}

if (!editorData.errorMessage.empty()) {
ImGui::PushStyleColor(ImGuiCol_Text, ImVec4(color::RED));
ImGui::TextWrapped(ICON_FA_TRIANGLE_EXCLAMATION" %s", editorData.errorMessage.c_str());
ImGui::PopStyleColor();
}
editor.Render("##json");
if (editor.IsTextChanged()) {
const auto currentText = editor.GetText();
try {
data = json_dto::from_json<D>(currentText);
editorData.errorMessage.clear();
editor.SetErrorMarkers({});
editorData.originalJson = json_helper::to_pretty_json(data);
} catch (json_dto::ex_t ex) {
editorData.errorMessage = ex.what();
const size_t offsetIdx = editorData.errorMessage.find("offset: ");
if (offsetIdx != std::string::npos) {
const auto offsetStr = editorData.errorMessage.substr(offsetIdx + 8, editorData.errorMessage.find_first_not_of("0123456789", offsetIdx));
const auto offset = std::stoul(offsetStr);
const auto offsetLineNo = std::count(currentText.begin(), currentText.begin() + offset, '\n');
TextEditor::ErrorMarkers errorMarkers;
errorMarkers.insert(std::make_pair(offsetLineNo + 1, editorData.errorMessage));
editor.SetErrorMarkers(errorMarkers);
}
}
}
ImGui::EndTabItem();
}
ImGui::EndTabBar();
}
}

struct CategoryTreeState {
int currentIndex = 0;
int selectedIndex = -1;
std::function<void()> tabsFunction = [] {};
int currentIndex;
int selectedIndex;
std::function<void()> tabsFunction;
};

template<typename D>
Expand Down Expand Up @@ -563,7 +610,7 @@ namespace bricksim::gui::windows::settings {
const float buttonHeight = (ImGui::GetFontSize() + ImGui::GetStyle().FramePadding.y * 2);
const float mainHeight = ImGui::GetContentRegionAvail().y - buttonHeight - ImGui::GetStyle().FramePadding.y * 4 - 1.f;
ImGui::BeginChild("##categoryTree", {treeWidth, mainHeight});
static CategoryTreeState treeState;
static CategoryTreeState treeState{0, 1, [] {}};
treeState.currentIndex = 0;
drawCategoryTree(treeState, editingConfig);
ImGui::EndChild();
Expand Down
11 changes: 11 additions & 0 deletions src/helpers/json_helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,4 +98,15 @@ namespace bricksim::json_helper {
}
}
};

template<typename T>
std::string to_pretty_json(const T& value) {
rapidjson::StringBuffer jsonBuffer;
rapidjson::PrettyWriter jsonWriter(jsonBuffer);
rapidjson::Document d;
json_dto::json_output_t jout(d, d.GetAllocator());
jout << value;
d.Accept(jsonWriter);
return jsonBuffer.GetString();
}
}
2 changes: 1 addition & 1 deletion src/lib/ImGuiColorTextEdit

0 comments on commit 89bee1d

Please sign in to comment.