Skip to content

Commit

Permalink
allow position selection via mouse
Browse files Browse the repository at this point in the history
  • Loading branch information
chrxh committed Sep 22, 2024
1 parent f15a8d9 commit 0fe2591
Show file tree
Hide file tree
Showing 7 changed files with 104 additions and 81 deletions.
46 changes: 24 additions & 22 deletions source/Gui/AlienImGui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,22 +64,37 @@ bool AlienImGui::SliderFloat2(SliderFloat2Parameters const& parameters, float& v
{
ImGui::PushID(parameters._name.c_str());

auto mousePickerButtonSize = parameters._getMousePickerEnabledFunc ? scale(50.0f) + ImGui::GetStyle().FramePadding.x * 2 : 0.0f;
auto mousePickerButtonSize = parameters._getMousePickerEnabledFunc ? scale(20.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);
bool result = ImGui::SliderFloat("##sliderX", &valueX, parameters._min.x, parameters._max.x, parameters._format.c_str(), 0);
ImGui::SameLine();
ImGui::SetNextItemWidth(sliderWidth);
result |= ImGui::SliderFloat("##sliderY", &valueY, parameters._minY, parameters._maxY, parameters._format.c_str(), 0);
result |= ImGui::SliderFloat("##sliderY", &valueY, parameters._min.y, parameters._max.y, parameters._format.c_str(), 0);

//mouse picker
if (parameters._getMousePickerEnabledFunc) {
ImGui::SameLine();
auto mousePickerEnabled = parameters._getMousePickerEnabledFunc.value()();
if (AlienImGui::SelectableButton(AlienImGui::SelectableButtonParameters().name(ICON_FA_CROSSHAIRS), mousePickerEnabled)) {
parameters._setMousePickerEnabledFunc.value()(mousePickerEnabled);
}
if (parameters._getMousePickerEnabledFunc.value()()) {
if (auto pos = parameters._getMousePickerPositionFunc.value()()) {
valueX = pos->x;
valueY = pos->y;
}
}
}

//revert button
if (parameters._defaultValueX) {
if (parameters._defaultValue) {
ImGui::SameLine();

ImGui::BeginDisabled(valueX == *parameters._defaultValueX && valueY == *parameters._defaultValueY);
ImGui::BeginDisabled(valueX == parameters._defaultValue->x && valueY == parameters._defaultValue->y);
if (revertButton(parameters._name)) {
valueX = *parameters._defaultValueX;
valueY = *parameters._defaultValueY;
valueX = parameters._defaultValue->x;
valueY = parameters._defaultValue->y;
}
ImGui::EndDisabled();
}
Expand All @@ -97,19 +112,6 @@ bool AlienImGui::SliderFloat2(SliderFloat2Parameters const& parameters, float& v

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)
Expand Down Expand Up @@ -171,7 +173,7 @@ bool AlienImGui::InputInt(InputIntParameters const& parameters, int& value, bool
if (showInfinity) {
ImGui::SameLine();
ImGui::BeginDisabled(parameters._readOnly);
if (SelectableButton(CheckButtonParameters().name(ICON_FA_INFINITY).tooltip(parameters._tooltip).width(infinityButtonWidth), isInfinity)) {
if (SelectableButton(SelectableButtonParameters().name(ICON_FA_INFINITY).tooltip(parameters._tooltip).width(infinityButtonWidth), isInfinity)) {
if (isInfinity) {
value = std::numeric_limits<int>::max();
} else {
Expand Down Expand Up @@ -681,7 +683,7 @@ bool AlienImGui::Checkbox(CheckboxParameters const& parameters, bool& value)
return result;
}

bool AlienImGui::SelectableButton(CheckButtonParameters const& parameters, bool& value)
bool AlienImGui::SelectableButton(SelectableButtonParameters const& parameters, bool& value)
{
auto buttonColor = ImColor(ImGui::GetStyle().Colors[ImGuiCol_Button]);
auto buttonColorHovered = ImColor(ImGui::GetStyle().Colors[ImGuiCol_ButtonHovered]);
Expand Down
20 changes: 9 additions & 11 deletions source/Gui/AlienImGui.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,17 +52,15 @@ class AlienImGui
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, RealVector2D, min, RealVector2D());
MEMBER_DECLARATION(SliderFloat2Parameters, RealVector2D, max, RealVector2D());
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<RealVector2D>, defaultValue, 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);
MEMBER_DECLARATION(SliderFloat2Parameters, std::optional<std::function<std::optional<RealVector2D>(void)>>, getMousePickerPositionFunc, std::nullopt);
};
static bool SliderFloat2(SliderFloat2Parameters const& parameters, float& valueX, float& valueY);

Expand Down Expand Up @@ -240,13 +238,13 @@ class AlienImGui
};
static bool ToggleButton(ToggleButtonParameters const& parameters, bool& value);

struct CheckButtonParameters
struct SelectableButtonParameters
{
MEMBER_DECLARATION(CheckButtonParameters, std::string, name, "");
MEMBER_DECLARATION(CheckButtonParameters, std::optional<std::string>, tooltip, std::nullopt);
MEMBER_DECLARATION(CheckButtonParameters, float, width, 0);
MEMBER_DECLARATION(SelectableButtonParameters, std::string, name, "");
MEMBER_DECLARATION(SelectableButtonParameters, std::optional<std::string>, tooltip, std::nullopt);
MEMBER_DECLARATION(SelectableButtonParameters, float, width, 0);
};
static bool SelectableButton(CheckButtonParameters const& parameters, bool& value);
static bool SelectableButton(SelectableButtonParameters const& parameters, bool& value);

static void Text(std::string const& text);
static void BoldText(std::string const& text);
Expand Down
2 changes: 1 addition & 1 deletion source/Gui/MainWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ _MainWindow::_MainWindow(SimulationController const& simController, GuiLogger co
_temporalControlWindow = std::make_shared<_TemporalControlWindow>(_simController, _statisticsWindow);
_spatialControlWindow = std::make_shared<_SpatialControlWindow>(_simController, _temporalControlWindow);
_radiationSourcesWindow = std::make_shared<_RadiationSourcesWindow>(_simController);
_simulationParametersWindow = std::make_shared<_SimulationParametersWindow>(_simController, _radiationSourcesWindow);
_simulationParametersWindow = std::make_shared<_SimulationParametersWindow>(_simController, _radiationSourcesWindow, _simulationView);
_gpuSettingsDialog = std::make_shared<_GpuSettingsDialog>(_simController);
_startupController = std::make_shared<_StartupController>(_simController, _temporalControlWindow);
_exitDialog = std::make_shared<_ExitDialog>(_onExit);
Expand Down
23 changes: 13 additions & 10 deletions source/Gui/SimulationParametersWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,12 @@ namespace

_SimulationParametersWindow::_SimulationParametersWindow(
SimulationController const& simController,
RadiationSourcesWindow const& radiationSourcesWindow)
RadiationSourcesWindow const& radiationSourcesWindow,
SimulationView const& simulationView)
: _AlienWindow("Simulation parameters", "windows.simulation parameters", false)
, _simController(simController)
, _radiationSourcesWindow(radiationSourcesWindow)
, _simulationView(simulationView)
{
for (int n = 0; n < IM_ARRAYSIZE(_savedPalette); n++) {
ImVec4 color;
Expand All @@ -74,8 +76,9 @@ _SimulationParametersWindow::_SimulationParametersWindow(
_cellFunctionStrings.emplace_back(Const::CellFunctionToStringMap.at(i));
}

_getMousePickerEnabledFunc = [&]() { return _simView->getMousePickerEnabled(); };
_setMousePickerEnabledFunc = [&](bool value) { _simView->setMousePickerEnabled(value); };
_getMousePickerEnabledFunc = [&]() { return _simulationView->getMousePickerEnabled(); };
_setMousePickerEnabledFunc = [&](bool value) { _simulationView->setMousePickerEnabled(value); };
_getMousePickerPositionFunc = [&]() { return _simulationView->getMousePickerPosition(); };
}

_SimulationParametersWindow::~_SimulationParametersWindow()
Expand Down Expand Up @@ -1662,13 +1665,13 @@ bool _SimulationParametersWindow::processSpot(int index)
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"),
.min({0, 0})
.max(toRealVector2D(worldSize))
.defaultValue(RealVector2D{origSpot.posX, origSpot.posY})
.format("%.2f")
.getMousePickerEnabledFunc(_getMousePickerEnabledFunc)
.setMousePickerEnabledFunc(_setMousePickerEnabledFunc)
.getMousePickerPositionFunc(_getMousePickerPositionFunc),
spot.posX,
spot.posY);

Expand Down
8 changes: 6 additions & 2 deletions source/Gui/SimulationParametersWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@
class _SimulationParametersWindow : public _AlienWindow
{
public:
_SimulationParametersWindow(SimulationController const& simController, RadiationSourcesWindow const& radiationSourcesWindow);
_SimulationParametersWindow(
SimulationController const& simController,
RadiationSourcesWindow const& radiationSourcesWindow,
SimulationView const& simulationView);
~_SimulationParametersWindow();

private:
Expand All @@ -35,7 +38,7 @@ class _SimulationParametersWindow : public _AlienWindow
void validationAndCorrection(SimulationParametersSpot& spot, SimulationParameters const& parameters) const;

SimulationController _simController;
SimulationView _simView;
SimulationView _simulationView;
RadiationSourcesWindow _radiationSourcesWindow;

uint32_t _savedPalette[32] = {};
Expand All @@ -51,4 +54,5 @@ class _SimulationParametersWindow : public _AlienWindow

std::function<bool(void)> _getMousePickerEnabledFunc;
std::function<void(bool)> _setMousePickerEnabledFunc;
std::function<std::optional<RealVector2D>(void)> _getMousePickerPositionFunc;
};
85 changes: 50 additions & 35 deletions source/Gui/SimulationView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ void _SimulationView::leftMouseButtonPressed(IntVector2D const& viewPos)

void _SimulationView::leftMouseButtonHold(IntVector2D const& viewPos, IntVector2D const& prevViewPos)
{
if (_modeWindow->getMode() == _ModeController::Mode::Navigation) {
if (_navigationState == NavigationState::Moving && _modeWindow->getMode() == _ModeController::Mode::Navigation) {
Viewport::zoom(viewPos, calcZoomFactor(_lastZoomTimepoint ? *_lastZoomTimepoint : std::chrono::steady_clock::now()));
}
}
Expand Down Expand Up @@ -235,42 +235,47 @@ void _SimulationView::processEvents()
IntVector2D prevMousePosInt = _prevMousePosInt ? *_prevMousePosInt : mousePosInt;

if (!ImGui::GetIO().WantCaptureMouse) {
if (ImGui::IsMouseClicked(ImGuiMouseButton_Left)) {
leftMouseButtonPressed(mousePosInt);
}
if (ImGui::IsMouseDown(ImGuiMouseButton_Left)) {
leftMouseButtonHold(mousePosInt, prevMousePosInt);
}
if (ImGui::GetIO().MouseWheel > 0) {
mouseWheelUp(mousePosInt, std::abs(ImGui::GetIO().MouseWheel));
}
if (ImGui::IsMouseReleased(ImGuiMouseButton_Left)) {
leftMouseButtonReleased();
}
if (_mousePickerEnabled) {
if (ImGui::IsMouseClicked(ImGuiMouseButton_Left)) {
_mousePickerEnabled = false;
}
} else {
if (ImGui::IsMouseClicked(ImGuiMouseButton_Left)) {
leftMouseButtonPressed(mousePosInt);
}
if (ImGui::IsMouseDown(ImGuiMouseButton_Left)) {
leftMouseButtonHold(mousePosInt, prevMousePosInt);
}
if (ImGui::GetIO().MouseWheel > 0) {
mouseWheelUp(mousePosInt, std::abs(ImGui::GetIO().MouseWheel));
}
if (ImGui::IsMouseReleased(ImGuiMouseButton_Left)) {
leftMouseButtonReleased();
}

if (ImGui::IsMouseClicked(ImGuiMouseButton_Right)) {
rightMouseButtonPressed();
}
if (ImGui::IsMouseDown(ImGuiMouseButton_Right)) {
rightMouseButtonHold(mousePosInt);
}
if (ImGui::GetIO().MouseWheel < 0) {
mouseWheelDown(mousePosInt, std::abs(ImGui::GetIO().MouseWheel));
}
if (ImGui::IsMouseReleased(ImGuiMouseButton_Right)) {
rightMouseButtonReleased();
}
if (ImGui::IsMouseClicked(ImGuiMouseButton_Right)) {
rightMouseButtonPressed();
}
if (ImGui::IsMouseDown(ImGuiMouseButton_Right)) {
rightMouseButtonHold(mousePosInt);
}
if (ImGui::GetIO().MouseWheel < 0) {
mouseWheelDown(mousePosInt, std::abs(ImGui::GetIO().MouseWheel));
}
if (ImGui::IsMouseReleased(ImGuiMouseButton_Right)) {
rightMouseButtonReleased();
}

if (ImGui::IsMouseClicked(ImGuiMouseButton_Middle)) {
middleMouseButtonPressed(mousePosInt);
}
if (ImGui::IsMouseDown(ImGuiMouseButton_Middle)) {
middleMouseButtonHold(mousePosInt);
}
if (ImGui::IsMouseReleased(ImGuiMouseButton_Middle)) {
middleMouseButtonReleased();
if (ImGui::IsMouseClicked(ImGuiMouseButton_Middle)) {
middleMouseButtonPressed(mousePosInt);
}
if (ImGui::IsMouseDown(ImGuiMouseButton_Middle)) {
middleMouseButtonHold(mousePosInt);
}
if (ImGui::IsMouseReleased(ImGuiMouseButton_Middle)) {
middleMouseButtonReleased();
}
}

drawCursor();
}
processMouseWheel(mousePosInt);
Expand Down Expand Up @@ -398,6 +403,16 @@ void _SimulationView::setMousePickerEnabled(bool value)
_mousePickerEnabled = value;
}

std::optional<RealVector2D> _SimulationView::getMousePickerPosition() const
{
if (ImGui::GetIO().WantCaptureMouse) {
return std::nullopt;
}

auto mousePos = ImGui::GetMousePos();
return Viewport::mapViewToWorldPosition({mousePos.x, mousePos.y});
}

void _SimulationView::updateImageFromSimulation()
{
auto worldRect = Viewport::getVisibleWorldRect();
Expand Down Expand Up @@ -498,7 +513,7 @@ void _SimulationView::drawCursor()
ImGui::SetMouseCursor(ImGuiMouseCursor_None);
}

if (_modeWindow->getMode() == _ModeController::Mode::Editor) {
if (_mousePickerEnabled || _modeWindow->getMode() == _ModeController::Mode::Editor) {
if (!_editorModel->isDrawMode() || _simController->isSimulationRunning()) {
auto cursorSize = scale(CursorRadius);

Expand Down
1 change: 1 addition & 0 deletions source/Gui/SimulationView.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class _SimulationView

bool getMousePickerEnabled() const;
void setMousePickerEnabled(bool value);
std::optional<RealVector2D> getMousePickerPosition() const;

private:
void processEvents();
Expand Down

0 comments on commit 0fe2591

Please sign in to comment.