/* This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/. */ #include "stdafx.h" #include "application/editoruilayer.h" #include "utilities/Globals.h" #include "utilities/utilities.h" #include "scene/scenenode.h" #include "scene/scenelayers.h" #include "scene/scene.h" #include "rendering/renderer.h" #include "utilities/translation.h" #include "imgui/imgui_internal.h" glm::vec2 editor_ui::m_viewmin{0.0f}; glm::vec2 editor_ui::m_viewmax{0.0f}; editor_ui::editor_ui() { clear_panels(); // bind the panels with ui object. maybe not the best place for this but, eh add_external_panel(&m_nodebankpanel); add_external_panel(&m_layerspanel); add_external_panel(&m_includespanel); // the menu is a part of the layout of the editor, not a pop-up as in the driver mode m_menu_always = true; } void editor_ui::render_dockspace() { // windows of the editor are docked around the 3d view, which is left free in the middle auto *viewport{ImGui::GetMainViewport()}; auto const dockspace{ImGui::GetID("###editordockspace")}; if (m_layoutreset || ImGui::DockBuilderGetNode(dockspace) == nullptr) { m_layoutreset = false; build_default_layout(dockspace); } ImGui::DockSpaceOverViewport(dockspace, viewport, ImGuiDockNodeFlags_PassthruCentralNode); if (auto const *central{ImGui::DockBuilderGetCentralNode(dockspace)}; central != nullptr) { m_viewmin = {central->Pos.x, central->Pos.y}; m_viewmax = {central->Pos.x + central->Size.x, central->Pos.y + central->Size.y}; } else { m_viewmin = {viewport->WorkPos.x, viewport->WorkPos.y}; m_viewmax = {viewport->WorkPos.x + viewport->WorkSize.x, viewport->WorkPos.y + viewport->WorkSize.y}; } } void editor_ui::build_default_layout(unsigned int const Dockspace) { auto *viewport{ImGui::GetMainViewport()}; ImGui::DockBuilderRemoveNode(Dockspace); ImGui::DockBuilderAddNode(Dockspace, ImGuiDockNodeFlags_DockSpace | ImGuiDockNodeFlags_PassthruCentralNode); ImGui::DockBuilderSetNodePos(Dockspace, viewport->WorkPos); ImGui::DockBuilderSetNodeSize(Dockspace, viewport->WorkSize); // widths in pixels the windows need, as parts of the room there is auto const width{std::max(viewport->WorkSize.x, 1.0f)}; auto const height{std::max(viewport->WorkSize.y, 1.0f)}; auto const left_width{std::min(400.0f * Global.ui_scale, width * 0.3f)}; auto const right_width{std::min(460.0f * Global.ui_scale, width * 0.3f)}; auto const bottom_height{std::min(280.0f * Global.ui_scale, height * 0.35f)}; ImGuiID center{Dockspace}; auto const right{ImGui::DockBuilderSplitNode(center, ImGuiDir_Right, right_width / width, nullptr, ¢er)}; auto const left{ImGui::DockBuilderSplitNode(center, ImGuiDir_Left, left_width / (width - right_width), nullptr, ¢er)}; auto const bottom{ImGui::DockBuilderSplitNode(center, ImGuiDir_Down, bottom_height / height, nullptr, ¢er)}; // left: settings of the tool in use above, the scenery below ImGuiID lefttop{left}; auto const leftbottom{ImGui::DockBuilderSplitNode(lefttop, ImGuiDir_Down, 0.5f, nullptr, &lefttop)}; // windows are found by the stable part of their names, after ### ImGui::DockBuilderDockWindow("###tooloptions", lefttop); for (auto const *window : {"###scenetree", "###Layers", "###scenehierarchy"}) ImGui::DockBuilderDockWindow(window, leftbottom); for (auto const *window : {"###inspector", "###editorsettings", "###Include database"}) ImGui::DockBuilderDockWindow(window, right); for (auto const *window : {"###Node bank", "###profilestrip", "###trackanalysis", "###structuregauge", "###editorhistory", "###modelsets"}) ImGui::DockBuilderDockWindow(window, bottom); ImGui::DockBuilderFinish(Dockspace); } void editor_ui::render_() { auto *viewport{ImGui::GetMainViewport()}; auto const flags{ImGuiWindowFlags_NoScrollbar | ImGuiWindowFlags_NoSavedSettings | ImGuiWindowFlags_MenuBar}; // toolbar under the menu: the tools of the editor mode if (ImGui::BeginViewportSideBar("##editortoolbar", viewport, ImGuiDir_Up, ImGui::GetFrameHeight(), flags)) { if (ImGui::BeginMenuBar()) { if (m_toolbaroptions) m_toolbaroptions(); ImGui::EndMenuBar(); } } ImGui::End(); render_tool_options(); render_inspector(); render_scene(); // status bar: the outcome of the last operation, and the selected node if (ImGui::BeginViewportSideBar("##editorstatusbar", viewport, ImGuiDir_Down, ImGui::GetFrameHeight(), flags)) { if (ImGui::BeginMenuBar()) { if (false == m_layerspanel.status.empty()) { if (m_layerspanel.status_error) ImGui::TextColored(ImVec4(1.0f, 0.45f, 0.4f, 1.0f), "%s", m_layerspanel.status.c_str()); else ImGui::TextUnformatted(m_layerspanel.status.c_str()); } if (m_node != nullptr) { auto const name{m_node->name().empty() ? std::string{"(none)"} : m_node->name()}; auto const label{std::string{STR_C("Selected:")} + " " + name}; ImGui::SameLine(std::max(ImGui::GetCursorPosX(), ImGui::GetWindowWidth() - ImGui::CalcTextSize(label.c_str()).x - ImGui::GetStyle().ItemSpacing.x * 2.0f)); ImGui::TextUnformatted(label.c_str()); } ImGui::EndMenuBar(); } } ImGui::End(); } // updates state of UI elements void editor_ui::update() { set_tooltip(""); if (Global.ControlPicking && DebugModeFlag) { const auto sceneryNode = GfxRenderer->Pick_Node(); const std::string content = sceneryNode ? sceneryNode->tooltip() : ""; set_tooltip(content); } ui_layer::update(); m_itempropertiespanel.update(m_node); m_functionspanel.update(m_node); } void editor_ui::render_mode_options(nodebank_panel::edit_mode const Mode) { switch (Mode) { case nodebank_panel::MODIFY: ImGui::TextDisabled(STR_C("LMB: select node F: focus End: drop to ground Del: delete")); break; case nodebank_panel::COPY: ImGui::TextDisabled(STR_C("LMB: copy the clicked model to the node bank")); break; case nodebank_panel::ADD: { ImGui::TextDisabled(STR_C("LMB: insert a model at the cursor")); ImGui::Checkbox(STR_C("Random model from set"), &m_insertrandom); if (m_insertrandom) { m_nodebankpanel.set_combo(STR_C("Set##insert"), m_insertset, nullptr); auto const count{m_nodebankpanel.set_entries(m_insertset).size()}; ImGui::TextDisabled(STR_C("%zu templates in set%s"), count, count == 0 ? ", the node bank selection is used" : ""); } else { ImGui::TextDisabled(STR_C("Inserts the template selected in the node bank")); } m_functionspanel.render_controls(); break; } case nodebank_panel::BRUSH: ImGui::TextDisabled(STR_C("Hold LMB: paint models along the cursor path")); m_brushobjects.render_options(m_nodebankpanel); m_functionspanel.render_controls(); break; case nodebank_panel::FILL: if (m_filloptions) m_filloptions(); break; default: break; } } void editor_ui::render_tool_options() { if (false == m_tooloptionsopen) return; ImGui::SetNextWindowSize(ImVec2S(400, 300), ImGuiCond_FirstUseEver); auto const current{mode()}; auto const active{m_workspace == workspace::terrain || (m_workspace == workspace::roads && current != nodebank_panel::TRACK) ? m_workspace : workspace::none}; // a change of the mode brings the window forward, in case it shares its dock with another one auto const shown{active != workspace::none ? 100 + static_cast(active) : static_cast(current)}; if (shown != m_toolmode) { m_toolmode = shown; ImGui::SetNextWindowFocus(); } if (ImGui::Begin((std::string(STR_C("Tool options")) + "###tooloptions").c_str(), &m_tooloptionsopen, ImGuiWindowFlags_NoFocusOnAppearing | ImGuiWindowFlags_NoCollapse)) { char const *name{STR_C("Select")}; switch (current) { case nodebank_panel::ADD: name = STR_C("Insert"); break; case nodebank_panel::BRUSH: name = STR_C("Brush"); break; case nodebank_panel::FILL: name = STR_C("Area fill"); break; case nodebank_panel::COPY: name = STR_C("Copy to bank"); break; case nodebank_panel::TRACK: name = STR_C("Tracks"); break; default: break; } if (active == workspace::roads) name = "Roads"; else if (active == workspace::terrain) name = STR_C("Terrain"); ImGui::SeparatorText(name); if (active != workspace::none) { auto const &tools{m_workspacetools[static_cast(active)]}; if (tools) tools(); } else if (current == nodebank_panel::TRACK) { if (m_tracktools) m_tracktools(); } else render_mode_options(current); if (ImGui::CollapsingHeader(STR_C("Gizmo"), ImGuiTreeNodeFlags_DefaultOpen)) { if (m_gizmooptions) m_gizmooptions(); } } ImGui::End(); } void editor_ui::render_inspector() { if (false == m_inspectoropen) return; ImGui::SetNextWindowSize(ImVec2S(420, 480), ImGuiCond_FirstUseEver); if (ImGui::Begin((std::string(STR_C("Inspector")) + "###inspector").c_str(), &m_inspectoropen, ImGuiWindowFlags_NoFocusOnAppearing | ImGuiWindowFlags_NoCollapse)) { if (mode() == nodebank_panel::TRACK) { // in the track mode the selection is a path if (m_trackselection) m_trackselection(); ImGui::End(); return; } if (auto const &selection{m_workspaceselection[static_cast(m_workspace)]}) { selection(); ImGui::End(); return; } if (ImGui::CollapsingHeader(STR_C("Node properties"), ImGuiTreeNodeFlags_DefaultOpen)) { ImGui::Indent(); m_itempropertiespanel.render_body(); ImGui::Unindent(); } if (ImGui::CollapsingHeader(STR_C("Array"))) { if (m_arrayoptions) m_arrayoptions(); } if (m_bendexpand) { ImGui::SetNextItemOpen(true); m_bendexpand = false; } if (ImGui::CollapsingHeader(STR_C("Bend along the track"))) { if (m_bendoptions) m_bendoptions(); } } ImGui::End(); } void editor_ui::scene_rebuild() { m_sceneentries.clear(); m_sceneentries.reserve(scene::Hierarchy.size()); for (auto const &entry : scene::Hierarchy) { if (entry.second == nullptr) continue; m_sceneentries.push_back({entry.first, entry.second->layer(), entry.second->group(), entry.second->name()}); } std::sort(m_sceneentries.begin(), m_sceneentries.end(), [](scene_entry const &Left, scene_entry const &Right) { if (Left.layer != Right.layer) return Left.layer < Right.layer; if (Left.group != Right.group) return Left.group < Right.group; return Left.name < Right.name; }); m_scenesize = scene::Hierarchy.size(); m_scenefilterused = "\x01"; // matches are found again } void editor_ui::scene_row(std::size_t const Entry) { auto const &entry{m_sceneentries[Entry]}; // the node is looked up again, it may have been removed since the list was made auto const lookup{scene::Hierarchy.find(entry.uuid)}; auto *node{lookup != scene::Hierarchy.end() ? lookup->second : nullptr}; if (node == nullptr) { ImGui::TextDisabled("%s", entry.name.c_str()); return; } auto const label{(node->name().empty() ? std::string{"(none)"} : node->name()) + "##" + entry.uuid}; if (ImGui::Selectable(label.c_str(), node == m_node, ImGuiSelectableFlags_AllowDoubleClick) && m_sceneselect) m_sceneselect(node, ImGui::IsMouseDoubleClicked(ImGuiMouseButton_Left)); if (ImGui::IsItemHovered()) { auto const location{node->location()}; ImGui::SetTooltip("%s\n%.1f, %.1f, %.1f\n%s", entry.uuid.c_str(), location.x, location.y, location.z, STR_C("Double click: fly to it")); } } void editor_ui::render_scene() { if (false == m_sceneopen) return; ImGui::SetNextWindowSize(ImVec2S(400, 420), ImGuiCond_FirstUseEver); if (ImGui::Begin((std::string(STR_C("Scene")) + "###scenetree").c_str(), &m_sceneopen, ImGuiWindowFlags_NoFocusOnAppearing | ImGuiWindowFlags_NoCollapse)) { if (m_scenesize != scene::Hierarchy.size()) scene_rebuild(); if (ImGui::Button(STR_C("Refresh"))) scene_rebuild(); ImGui::SameLine(); ImGui::SetNextItemWidth(-FLT_MIN); ImGui::InputTextWithHint("##scenefilter", STR_C("Filter: name or uuid"), m_scenefilter, IM_ARRAYSIZE(m_scenefilter)); ImGui::TextDisabled(STR_C("%zu models"), m_sceneentries.size()); if (ImGui::BeginChild("##scenelist", ImVec2(0.0f, 0.0f), ImGuiChildFlags_Borders)) { std::string const filter{m_scenefilter}; if (false == filter.empty()) { // a flat list of what matches the filter if (filter != m_scenefilterused) { m_scenematches.clear(); for (std::size_t idx = 0; idx < m_sceneentries.size(); ++idx) if (contains(m_sceneentries[idx].name, filter) || contains(m_sceneentries[idx].uuid, filter)) m_scenematches.push_back(idx); m_scenefilterused = filter; } ImGuiListClipper clipper; clipper.Begin(static_cast(m_scenematches.size())); while (clipper.Step()) for (int row = clipper.DisplayStart; row < clipper.DisplayEnd; ++row) scene_row(m_scenematches[row]); } else { // layers, in them the groups (made by the includes), in these the models std::size_t layerstart{0}; while (layerstart < m_sceneentries.size()) { auto const layer{m_sceneentries[layerstart].layer}; auto layerend{layerstart}; while (layerend < m_sceneentries.size() && m_sceneentries[layerend].layer == layer) ++layerend; auto const layername{scene::Layers.valid(layer) ? scene::Layers.layer(layer).name : std::string{STR_C("(no layer)")}}; auto const layerlabel{layername + " (" + std::to_string(layerend - layerstart) + ")##layer" + std::to_string(layer)}; if (ImGui::TreeNodeEx(layerlabel.c_str(), m_sceneentries.size() == layerend - layerstart ? ImGuiTreeNodeFlags_DefaultOpen : 0)) { auto groupstart{layerstart}; while (groupstart < layerend) { auto const group{m_sceneentries[groupstart].group}; auto groupend{groupstart}; while (groupend < layerend && m_sceneentries[groupend].group == group) ++groupend; auto const rows = [&](std::size_t const From, std::size_t const To) { ImGuiListClipper clipper; clipper.Begin(static_cast(To - From)); while (clipper.Step()) for (int row = clipper.DisplayStart; row < clipper.DisplayEnd; ++row) scene_row(From + row); }; if (group == null_handle) { rows(groupstart, groupend); } else { auto const grouplabel{std::string{STR_C("Group")} + " " + std::to_string(group) + " (" + std::to_string(groupend - groupstart) + ")##group" + std::to_string(group)}; if (ImGui::TreeNode(grouplabel.c_str())) { rows(groupstart, groupend); ImGui::TreePop(); } } groupstart = groupend; } ImGui::TreePop(); } layerstart = layerend; } } } ImGui::EndChild(); } ImGui::End(); } void editor_ui::render_menu_contents() { if (ImGui::BeginMenu(STR_C("File"))) { if (ImGui::MenuItem(STR_C("New scenery...")) && m_newscenery) m_newscenery(); if (ImGui::IsItemHovered()) ImGui::SetTooltip("%s", STR_C("The editor starts again with the wizard of a new scenery: its name and its centre on the map")); if (ImGui::MenuItem(STR_C("Open scenery...")) && m_openscenery) m_openscenery(); if (ImGui::IsItemHovered()) ImGui::SetTooltip("%s", STR_C("The editor starts again with another scenery of the scenery folder")); // changes go to the scenery files only if the scenery was loaded with its sources tracked auto const editsession{false == scene::Layers.empty()}; if (ImGui::MenuItem(STR_C("Save"), STR_C("Ctrl+S"), false, editsession) && m_save) { m_save(); } if (false == editsession && ImGui::IsItemHovered(ImGuiHoveredFlags_AllowWhenDisabled)) { ImGui::SetTooltip("%s", STR_C("Start the simulator with -edit to save changes to the scenery files")); } if (ImGui::MenuItem(STR_C("Export scenery dump"), STR_C("Ctrl+Shift+F11")) && m_export) { m_export(); } if (ImGui::IsItemHovered()) { ImGui::SetTooltip("%s", STR_C("Writes the whole scene to _export files next to the scenario, the scenery files are left as they are")); } ImGui::EndMenu(); } ui_layer::render_menu_contents(); if (ImGui::BeginMenu(STR_C("Mode windows"))) { if (ImGui::MenuItem(STR_C("Reset window layout"))) m_layoutreset = true; ImGui::Separator(); ImGui::MenuItem(STR_C("Tool options"), nullptr, &m_tooloptionsopen); ImGui::MenuItem(STR_C("Inspector"), nullptr, &m_inspectoropen); ImGui::MenuItem(STR_C("Scene"), nullptr, &m_sceneopen); ImGui::MenuItem(STR_C("Node bank"), nullptr, &m_nodebankpanel.is_open); ImGui::MenuItem(STR_C("Layers"), nullptr, &m_layerspanel.is_open); ImGui::MenuItem(STR_C("Include database"), nullptr, &m_includespanel.is_open); ImGui::EndMenu(); } if (m_menuoptions) m_menuoptions(); } void editor_ui::set_file_actions(std::function Save, std::function Export) { m_save = Save; m_export = std::move(Export); m_layerspanel.save = std::move(Save); } void editor_ui::set_status(std::string const &Status, bool const Error) { m_layerspanel.status = Status; m_layerspanel.status_error = Error; } void editor_ui::render_rotation_controls() { m_functionspanel.render_controls(); } void editor_ui::set_node(scene::basic_node *Node) { m_node = Node; m_itempropertiespanel.update(m_node); m_functionspanel.update(m_node); } void editor_ui::set_include(include_selection *Include) { m_itempropertiespanel.include(Include); } void editor_ui::add_node_template(const std::string &desc) { m_nodebankpanel.add_template(desc); } std::string const *editor_ui::get_active_node_template(bool bypassRandom) { if (!bypassRandom) { // random pick from the active tool's set; an empty set falls back to the node bank selection std::string const *pick{nullptr}; if (m_nodebankpanel.mode == nodebank_panel::BRUSH && m_brushobjects.useRandom) { if (m_brushobjects.source.kind == model_set_ref::source::manual) pick = m_brushobjects.Objects.empty() ? nullptr : m_brushobjects.GetRandomObject(); else pick = m_nodebankpanel.random_template(m_brushobjects.source); } else if (m_nodebankpanel.mode == nodebank_panel::ADD && m_insertrandom) { pick = m_nodebankpanel.random_template(m_insertset); } if (pick != nullptr) return pick; } return m_nodebankpanel.get_active_template(); } nodebank_panel::edit_mode editor_ui::mode() { return m_track ? nodebank_panel::TRACK : m_nodebankpanel.mode; } void editor_ui::set_mode(nodebank_panel::edit_mode const Mode) { m_nodebankpanel.mode = Mode; m_nodebankpanel.is_open = true; } float editor_ui::getSpacing() { return m_brushobjects.spacing; } functions_panel::rotation_mode editor_ui::rot_mode() { return m_functionspanel.rot_mode; } float editor_ui::rot_val() { return m_functionspanel.rot_value; } bool editor_ui::rot_from_last() { return m_functionspanel.rot_from_last; }