Skip to content

Commit

Permalink
2D slider
Browse files Browse the repository at this point in the history
  • Loading branch information
chrxh committed Sep 22, 2024
1 parent 3915d9b commit f15a8d9
Show file tree
Hide file tree
Showing 7 changed files with 112 additions and 21 deletions.
1 change: 1 addition & 0 deletions source/Base/VersionChecker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ namespace
return result;
}
}

bool VersionChecker::isVersionOutdated(std::string const& otherVersionString)
{
auto otherParts = getVersionParts(otherVersionString);
Expand Down
63 changes: 59 additions & 4 deletions source/Gui/AlienImGui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -1334,14 +1386,16 @@ bool AlienImGui::AngleAlignmentCombo(AngleAlignmentComboParameters& parameters,

namespace
{
int& getIdBasedValue(std::unordered_map<unsigned int, int>& idToValueMap, int defaultValue = 0)
template<typename T>
int& getIdBasedValue(std::unordered_map<unsigned int, T>& 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(
Expand All @@ -1350,8 +1404,8 @@ void AlienImGui::NeuronSelection(
std::vector<float>& biases,
std::vector<NeuronActivationFunction>& 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);
Expand Down Expand Up @@ -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();
Expand Down
17 changes: 17 additions & 0 deletions source/Gui/AlienImGui.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<float>, defaultValueX, std::nullopt);
MEMBER_DECLARATION(SliderFloat2Parameters, std::optional<float>, defaultValueY, std::nullopt);
MEMBER_DECLARATION(SliderFloat2Parameters, std::optional<std::string>, tooltip, std::nullopt);
MEMBER_DECLARATION(SliderFloat2Parameters, std::optional<std::function<bool(void)>>, getMousePickerEnabledFunc, std::nullopt);
MEMBER_DECLARATION(SliderFloat2Parameters, std::optional<std::function<void(bool)>>, setMousePickerEnabledFunc, std::nullopt);
};
static bool SliderFloat2(SliderFloat2Parameters const& parameters, float& valueX, float& valueY);

struct SliderInputFloatParameters
{
MEMBER_DECLARATION(SliderInputFloatParameters, std::string, name, "");
Expand Down
34 changes: 17 additions & 17 deletions source/Gui/SimulationParametersWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "MessageDialog.h"
#include "RadiationSourcesWindow.h"
#include "OverlayMessageController.h"
#include "SimulationView.h"
#include "StyleRepository.h"

namespace
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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")
Expand Down
4 changes: 4 additions & 0 deletions source/Gui/SimulationParametersWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -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] = {};
Expand All @@ -47,4 +48,7 @@ class _SimulationParametersWindow : public _AlienWindow

bool _featureListOpen = false;
float _featureListHeight = 200.0f;

std::function<bool(void)> _getMousePickerEnabledFunc;
std::function<void(bool)> _setMousePickerEnabledFunc;
};
10 changes: 10 additions & 0 deletions source/Gui/SimulationView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
4 changes: 4 additions & 0 deletions source/Gui/SimulationView.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ class _SimulationView
void setContrast(float value);
void setMotionBlur(float value);

bool getMousePickerEnabled() const;
void setMousePickerEnabled(bool value);

private:
void processEvents();

Expand Down Expand Up @@ -90,6 +93,7 @@ class _SimulationView
std::chrono::steady_clock::time_point lastTime;
};
std::optional<MouseWheelAction> _mouseWheelAction;
bool _mousePickerEnabled = false;

ModeController _modeWindow;
SimulationController _simController;
Expand Down

0 comments on commit f15a8d9

Please sign in to comment.