From acb76ffa0f45c5369e103f8868356d333ab2d954 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sun, 11 May 2025 12:22:19 +0100 Subject: Build networks with new interface --- ui/editNetwork.cpp | 91 ++++++++++++++++++++++++++++-------------------------- 1 file changed, 47 insertions(+), 44 deletions(-) (limited to 'ui/editNetwork.cpp') diff --git a/ui/editNetwork.cpp b/ui/editNetwork.cpp index c900191..cab13f2 100644 --- a/ui/editNetwork.cpp +++ b/ui/editNetwork.cpp @@ -1,32 +1,53 @@ #include "editNetwork.h" -#include "builders/freeExtend.h" -#include "builders/join.h" -#include "builders/straight.h" #include "imgui_wrap.h" #include #include #include #include -constexpr const glm::u8vec4 TRANSPARENT_BLUE {30, 50, 255, 200}; - -EditNetwork::EditNetwork(Network * n) : network {n}, blue {1, 1, &TRANSPARENT_BLUE} { } +EditNetwork::EditNetwork(Network * n) : network {n} { } bool -EditNetwork::click(const SDL_MouseButtonEvent & e, const Ray & ray) +EditNetwork::click(const SDL_MouseButtonEvent & event, const Ray & ray) { - if (builder && (e.button == SDL_BUTTON_LEFT || e.button == SDL_BUTTON_MIDDLE)) { - builder->click(network, gameState->terrain.get(), e, ray); - return true; + switch (event.button) { + case SDL_BUTTON_MIDDLE: + currentStart.reset(); + candidates.clear(); + return true; + case SDL_BUTTON_LEFT: + if (!currentStart) { + currentStart = resolveRay(ray); + } + else { + if (const auto def = resolveRay(ray)) { + candidates = network->create(CreationDefinition {.fromEnd = *currentStart, .toEnd = *def}); + for (const auto & link : candidates) { + network->add(gameState->terrain.get(), link); + } + if (continuousMode) { + currentStart = def; + currentStart->direction = candidates.back()->endAt(def->position)->dir; + } + else { + currentStart.reset(); + } + candidates.clear(); + } + } + return true; + default: + return false; } - return false; } bool -EditNetwork::move(const SDL_MouseMotionEvent & e, const Ray & ray) +EditNetwork::move(const SDL_MouseMotionEvent &, const Ray & ray) { - if (builder) { - builder->move(network, gameState->terrain.get(), e, ray); + if (currentStart) { + if (const auto def = resolveRay(ray)) { + candidates = network->create(CreationDefinition {.fromEnd = *currentStart, .toEnd = *def}); + } } return false; } @@ -38,30 +59,22 @@ EditNetwork::handleInput(const SDL_Event &) } void -EditNetwork::render(const SceneShader & shader, const Frustum & frustum) const -{ - if (builder) { - blue.bind(); - shader.absolute.use(); - builder->render(shader, frustum); - } -} - -void -EditNetwork::Builder::render(const SceneShader & shader, const Frustum & frustum) const +EditNetwork::render(const SceneShader &, const Frustum &) const { - candidateLinks.apply(&Renderable::render, shader, frustum); } -void -EditNetwork::Builder::setHeightsFor(Network * network, const Link::CCollection & links, GeoData::SetHeightsOpts opts) +std::optional +EditNetwork::resolveRay(const Ray & ray) const { - opts.surface = network->getBaseSurface(); - const auto width = network->getBaseWidth(); - - for (const auto & link : links) { - gameState->terrain->setHeights(link->getBase(width), opts); + if (const auto position = gameState->terrain->intersectRay(ray)) { + const auto node = network->intersectRayNodes(ray); + if (node) { + const auto direction = network->findNodeDirection(node); + return CreationDefinitionEnd {.position = node->pos, .direction = direction}; + } + return CreationDefinitionEnd {.position = position->first, .direction = std::nullopt}; } + return {}; } void @@ -69,16 +82,6 @@ EditNetwork::render(bool & open) { ImGui::SetNextWindowSize({-1, -1}); ImGui::Begin("Edit Network", &open); - - auto builderChoice = [this](const char * name) { - if (ImGui::RadioButton(name, dynamic_cast(builder.get()))) { - builder = std::make_unique(); - } - }; - builderChoice.operator()("Straight"); - builderChoice.operator()("Join"); - builderChoice.operator()("Free Extend"); - ImGui::TextUnformatted(builder ? builder->hint().c_str() : "Select a build mode"); - + ImGui::Checkbox("Continuous mode", &continuousMode); ImGui::End(); } -- cgit v1.2.3