From f15a8d993d46c1bf997f7c74824617dae8b74c52 Mon Sep 17 00:00:00 2001 From: Christian Heinemann Date: Sun, 22 Sep 2024 17:54:15 +0200 Subject: [PATCH] 2D slider --- source/Base/VersionChecker.cpp | 1 + source/Gui/AlienImGui.cpp | 63 +++++++++++++++++++++-- source/Gui/AlienImGui.h | 17 ++++++ source/Gui/SimulationParametersWindow.cpp | 34 ++++++------ source/Gui/SimulationParametersWindow.h | 4 ++ source/Gui/SimulationView.cpp | 10 ++++ source/Gui/SimulationView.h | 4 ++ 7 files changed, 112 insertions(+), 21 deletions(-) diff --git a/source/Base/VersionChecker.cpp b/source/Base/VersionChecker.cpp index d00d4a86f..d19239864 100644 --- a/source/Base/VersionChecker.cpp +++ b/source/Base/VersionChecker.cpp @@ -76,6 +76,7 @@ namespace return result; } } + bool VersionChecker::isVersionOutdated(std::string const& otherVersionString) { auto otherParts = getVersionParts(otherVersionString); diff --git a/source/Gui/AlienImGui.cpp b/source/Gui/AlienImGui.cpp index fde9a0cf7..e8734ab4c 100644 --- a/source/Gui/AlienImGui.cpp +++ b/source/Gui/AlienImGui.cpp @@ -60,6 +60,58 @@ bool AlienImGui::SliderInt(SliderIntParameters const& parameters, int* value, bo return BasicSlider(parameters, value, enabled); } +bool AlienImGui::SliderFloat2(SliderFloat2Parameters const& parameters, float& valueX, float& valueY) +{ + ImGui::PushID(parameters._name.c_str()); + + auto mousePickerButtonSize = parameters._getMousePickerEnabledFunc ? scale(50.0f) + ImGui::GetStyle().FramePadding.x * 2 : 0.0f; + auto sliderWidth = (ImGui::GetContentRegionAvail().x - scale(parameters._textWidth) - mousePickerButtonSize) / 2 - ImGui::GetStyle().FramePadding.x; + ImGui::SetNextItemWidth(sliderWidth); + bool result = ImGui::SliderFloat("##sliderX", &valueX, parameters._minX, parameters._maxX, parameters._format.c_str(), 0); + ImGui::SameLine(); + ImGui::SetNextItemWidth(sliderWidth); + result |= ImGui::SliderFloat("##sliderY", &valueY, parameters._minY, parameters._maxY, parameters._format.c_str(), 0); + + //revert button + if (parameters._defaultValueX) { + ImGui::SameLine(); + + ImGui::BeginDisabled(valueX == *parameters._defaultValueX && valueY == *parameters._defaultValueY); + if (revertButton(parameters._name)) { + valueX = *parameters._defaultValueX; + valueY = *parameters._defaultValueY; + } + ImGui::EndDisabled(); + } + + //text + if (!parameters._name.empty()) { + ImGui::SameLine(); + ImGui::TextUnformatted(parameters._name.c_str()); + } + + //tooltip + if (parameters._tooltip) { + AlienImGui::HelpMarker(*parameters._tooltip); + } + + ImGui::PopID(); + return result; + + // //mouse picker + // if (parameters._getMousePickerEnabledFunc) { + // ImGui::SameLine(); + // if (ImGui::Button(ICON_FA_CROSSHAIRS)) { + // auto mousePickerEnabled = parameters._getMousePickerEnabledFunc.value()(); + // parameters._setMousePickerEnabledFunc.value()(!mousePickerEnabled); + // } + // if (parameters._getMousePickerEnabledFunc.value()()) { + // auto mousePos = ImGui::GetMousePos(); + // value[0] = mousePos.x; + // } + // } +} + void AlienImGui::SliderInputFloat(SliderInputFloatParameters const& parameters, float& value) { auto textWidth = StyleRepository::getInstance().scale(parameters._textWidth); @@ -1334,14 +1386,16 @@ bool AlienImGui::AngleAlignmentCombo(AngleAlignmentComboParameters& parameters, namespace { - int& getIdBasedValue(std::unordered_map& idToValueMap, int defaultValue = 0) + template + int& getIdBasedValue(std::unordered_map& idToValueMap, T const& defaultValue) { auto id = ImGui::GetID(""); if (!idToValueMap.contains(id)) { - idToValueMap[id] = 0; + idToValueMap[id] = defaultValue; } return idToValueMap.at(id); } + } void AlienImGui::NeuronSelection( @@ -1350,8 +1404,8 @@ void AlienImGui::NeuronSelection( std::vector& biases, std::vector& activationFunctions) { - auto& selectedInput = getIdBasedValue(_neuronSelectedInput); - auto& selectedOutput = getIdBasedValue(_neuronSelectedOutput); + auto& selectedInput = getIdBasedValue(_neuronSelectedInput, 0); + auto& selectedOutput = getIdBasedValue(_neuronSelectedOutput, 0); auto setDefaultColors = [] { ImGui::PushStyleColor(ImGuiCol_Button, (ImVec4)Const::ToggleButtonColor); ImGui::PushStyleColor(ImGuiCol_ButtonHovered, (ImVec4)Const::ToggleButtonHoveredColor); @@ -1702,6 +1756,7 @@ bool AlienImGui::BasicSlider(Parameter const& parameters, T* value, bool* enable ImGui::PopID(); if (color == 0) { + //revert button if (parameters._defaultValue) { ImGui::SameLine(); diff --git a/source/Gui/AlienImGui.h b/source/Gui/AlienImGui.h index a28dbd5c2..fc1900908 100644 --- a/source/Gui/AlienImGui.h +++ b/source/Gui/AlienImGui.h @@ -49,6 +49,23 @@ class AlienImGui }; static bool SliderInt(SliderIntParameters const& parameters, int* value, bool* enabled = nullptr); + struct SliderFloat2Parameters + { + MEMBER_DECLARATION(SliderFloat2Parameters, std::string, name, ""); + MEMBER_DECLARATION(SliderFloat2Parameters, float, minX, 0); + MEMBER_DECLARATION(SliderFloat2Parameters, float, minY, 0); + MEMBER_DECLARATION(SliderFloat2Parameters, float, maxX, 0); + MEMBER_DECLARATION(SliderFloat2Parameters, float, maxY, 0); + MEMBER_DECLARATION(SliderFloat2Parameters, std::string, format, "%.3f"); + MEMBER_DECLARATION(SliderFloat2Parameters, float, textWidth, 100); + MEMBER_DECLARATION(SliderFloat2Parameters, std::optional, defaultValueX, std::nullopt); + MEMBER_DECLARATION(SliderFloat2Parameters, std::optional, defaultValueY, std::nullopt); + MEMBER_DECLARATION(SliderFloat2Parameters, std::optional, tooltip, std::nullopt); + MEMBER_DECLARATION(SliderFloat2Parameters, std::optional>, getMousePickerEnabledFunc, std::nullopt); + MEMBER_DECLARATION(SliderFloat2Parameters, std::optional>, setMousePickerEnabledFunc, std::nullopt); + }; + static bool SliderFloat2(SliderFloat2Parameters const& parameters, float& valueX, float& valueY); + struct SliderInputFloatParameters { MEMBER_DECLARATION(SliderInputFloatParameters, std::string, name, ""); diff --git a/source/Gui/SimulationParametersWindow.cpp b/source/Gui/SimulationParametersWindow.cpp index 048f9f521..c9497a929 100644 --- a/source/Gui/SimulationParametersWindow.cpp +++ b/source/Gui/SimulationParametersWindow.cpp @@ -16,6 +16,7 @@ #include "MessageDialog.h" #include "RadiationSourcesWindow.h" #include "OverlayMessageController.h" +#include "SimulationView.h" #include "StyleRepository.h" namespace @@ -72,6 +73,9 @@ _SimulationParametersWindow::_SimulationParametersWindow( for (int i = 0; i < CellFunction_Count; ++i) { _cellFunctionStrings.emplace_back(Const::CellFunctionToStringMap.at(i)); } + + _getMousePickerEnabledFunc = [&]() { return _simView->getMousePickerEnabled(); }; + _setMousePickerEnabledFunc = [&](bool value) { _simView->setMousePickerEnabled(value); }; } _SimulationParametersWindow::~_SimulationParametersWindow() @@ -1654,24 +1658,20 @@ bool _SimulationParametersWindow::processSpot(int index) spot.shapeType)) { createDefaultSpotData(spot); } - AlienImGui::SliderFloat( - AlienImGui::SliderFloatParameters() - .name("Position X") - .textWidth(RightColumnWidth) - .min(0) - .max(toFloat(worldSize.x)) - .defaultValue(&origSpot.posX) + AlienImGui::SliderFloat2( + AlienImGui::SliderFloat2Parameters() + .name("Position") + .textWidth(RightColumnWidth) + .minX(0) + .maxX(toFloat(worldSize.x)) + .minY(0) + .maxY(toFloat(worldSize.y)) + .defaultValueX(origSpot.posX) + .defaultValueY(origSpot.posY) .format("%.2f"), - &spot.posX); - AlienImGui::SliderFloat( - AlienImGui::SliderFloatParameters() - .name("Position Y") - .textWidth(RightColumnWidth) - .min(0) - .max(toFloat(worldSize.y)) - .defaultValue(&origSpot.posY) - .format("%.2f"), - &spot.posY); + spot.posX, + spot.posY); + AlienImGui::SliderFloat( AlienImGui::SliderFloatParameters() .name("Velocity X") diff --git a/source/Gui/SimulationParametersWindow.h b/source/Gui/SimulationParametersWindow.h index 92f9ce2a6..0a0a9f44c 100644 --- a/source/Gui/SimulationParametersWindow.h +++ b/source/Gui/SimulationParametersWindow.h @@ -35,6 +35,7 @@ class _SimulationParametersWindow : public _AlienWindow void validationAndCorrection(SimulationParametersSpot& spot, SimulationParameters const& parameters) const; SimulationController _simController; + SimulationView _simView; RadiationSourcesWindow _radiationSourcesWindow; uint32_t _savedPalette[32] = {}; @@ -47,4 +48,7 @@ class _SimulationParametersWindow : public _AlienWindow bool _featureListOpen = false; float _featureListHeight = 200.0f; + + std::function _getMousePickerEnabledFunc; + std::function _setMousePickerEnabledFunc; }; \ No newline at end of file diff --git a/source/Gui/SimulationView.cpp b/source/Gui/SimulationView.cpp index 2da40a7df..6aa274339 100644 --- a/source/Gui/SimulationView.cpp +++ b/source/Gui/SimulationView.cpp @@ -388,6 +388,16 @@ void _SimulationView::setMotionBlur(float value) updateMotionBlur(); } +bool _SimulationView::getMousePickerEnabled() const +{ + return _mousePickerEnabled; +} + +void _SimulationView::setMousePickerEnabled(bool value) +{ + _mousePickerEnabled = value; +} + void _SimulationView::updateImageFromSimulation() { auto worldRect = Viewport::getVisibleWorldRect(); diff --git a/source/Gui/SimulationView.h b/source/Gui/SimulationView.h index bafed75fc..87918245e 100644 --- a/source/Gui/SimulationView.h +++ b/source/Gui/SimulationView.h @@ -28,6 +28,9 @@ class _SimulationView void setContrast(float value); void setMotionBlur(float value); + bool getMousePickerEnabled() const; + void setMousePickerEnabled(bool value); + private: void processEvents(); @@ -90,6 +93,7 @@ class _SimulationView std::chrono::steady_clock::time_point lastTime; }; std::optional _mouseWheelAction; + bool _mousePickerEnabled = false; ModeController _modeWindow; SimulationController _simController;