Skip to content

Commit

Permalink
selectable toolbar button introduced
Browse files Browse the repository at this point in the history
  • Loading branch information
chrxh committed Oct 14, 2023
1 parent 1bee30c commit 91a4454
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 67 deletions.
40 changes: 40 additions & 0 deletions source/Gui/AlienImGui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -574,6 +574,7 @@ bool AlienImGui::SelectableButton(CheckButtonParameters const& parameters, bool&
auto buttonColorActive = ImColor(ImGui::GetStyle().Colors[ImGuiCol_ButtonActive]);
if (value) {
buttonColor = buttonColorActive;
buttonColorHovered = buttonColorActive;
}

ImGui::PushStyleColor(ImGuiCol_Button, (ImVec4)buttonColor);
Expand Down Expand Up @@ -819,6 +820,45 @@ bool AlienImGui::ToolbarButton(std::string const& text)
return result;
}

bool AlienImGui::SelectableToolbarButton(std::string const& text, int& value, int selectionValue, int deselectionValue)
{
auto id = std::to_string(ImGui::GetID(text.c_str()));

ImGui::PushFont(StyleRepository::getInstance().getIconFont());
ImGui::PushStyleVar(ImGuiStyleVar_ButtonTextAlign, {0.5f, 0.75f});
auto color = Const::ToolbarButtonTextColor;
float h, s, v;
ImGui::ColorConvertRGBtoHSV(color.Value.x, color.Value.y, color.Value.z, h, s, v);

auto buttonColor = Const::ToolbarButtonBackgroundColor;
auto buttonColorHovered = ImColor::HSV(h, s, v * 0.3f);
auto buttonColorActive = ImColor::HSV(h, s, v * 0.45f);
if (value == selectionValue) {
buttonColor = buttonColorActive;
buttonColorHovered = buttonColorActive;
}

ImGui::PushStyleColor(ImGuiCol_Button, (ImVec4)buttonColor);
ImGui::PushStyleColor(ImGuiCol_ButtonHovered, (ImVec4)buttonColorHovered);
ImGui::PushStyleColor(ImGuiCol_ButtonActive, (ImVec4)buttonColorActive);

ImGui::PushStyleColor(ImGuiCol_Text, static_cast<ImVec4>(Const::ToolbarButtonTextColor));
auto buttonSize = scale(40.0f);
auto result = ImGui::Button(text.c_str(), {buttonSize, buttonSize});
if (result) {
if (value == selectionValue) {
value = deselectionValue;
} else {
value = selectionValue;
}
}

ImGui::PopStyleColor(4);
ImGui::PopStyleVar();
ImGui::PopFont();
return result;
}

void AlienImGui::VerticalSeparator(float length)
{
ImDrawList* drawList = ImGui::GetWindowDrawList();
Expand Down
2 changes: 2 additions & 0 deletions source/Gui/AlienImGui.h
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,8 @@ class AlienImGui
static void Group(std::string const& text);

static bool ToolbarButton(std::string const& text);
static bool SelectableToolbarButton(std::string const& text, int& value, int selectionValue, int deselectionValue);

static void VerticalSeparator(float length = 23.0f);
static void ToolbarSeparator();
static bool Button(std::string const& text, float size = 0);
Expand Down
76 changes: 32 additions & 44 deletions source/Gui/CreatorWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@
namespace
{
auto const ModeText = std::unordered_map<CreationMode, std::string>{
{CreationMode::CreateParticle, "Create a single energy particle"},
{CreationMode::CreateCell, "Create a single cell"},
{CreationMode::CreateRectangle, "Create a rectangular cell network"},
{CreationMode::CreateHexagon, "Create a hexagonal cell network"},
{CreationMode::CreateDisc, "Create a disc-shaped cell network"},
{CreationMode::Drawing, "Draw freehand cell network"},
{CreationMode_CreateParticle, "Create a single energy particle"},
{CreationMode_CreateCell, "Create a single cell"},
{CreationMode_CreateRectangle, "Create a rectangular cell network"},
{CreationMode_CreateHexagon, "Create a hexagonal cell network"},
{CreationMode_CreateDisc, "Create a disc-shaped cell network"},
{CreationMode_Drawing, "Draw freehand cell network"},
};

auto const RightColumnWidth = 160.0f;
Expand All @@ -43,48 +43,36 @@ _CreatorWindow::_CreatorWindow(EditorModel const& editorModel, SimulationControl

void _CreatorWindow::processIntern()
{
if (AlienImGui::ToolbarButton(ICON_FA_SUN)) {
_mode = CreationMode::CreateParticle;
}
AlienImGui::Tooltip(ModeText.at(CreationMode::CreateParticle));
AlienImGui::SelectableToolbarButton(ICON_FA_SUN, _mode, CreationMode_CreateParticle, CreationMode_CreateParticle);
AlienImGui::Tooltip(ModeText.at(CreationMode_CreateParticle));

ImGui::SameLine();
if (AlienImGui::ToolbarButton(ICON_DOT)) {
_mode = CreationMode::CreateCell;
}
AlienImGui::Tooltip(ModeText.at(CreationMode::CreateCell));
AlienImGui::SelectableToolbarButton(ICON_DOT, _mode, CreationMode_CreateCell, CreationMode_CreateCell);
AlienImGui::Tooltip(ModeText.at(CreationMode_CreateCell));

ImGui::SameLine();
if (AlienImGui::ToolbarButton(ICON_RECTANGLE)) {
_mode = CreationMode::CreateRectangle;
}
AlienImGui::Tooltip(ModeText.at(CreationMode::CreateRectangle));
AlienImGui::SelectableToolbarButton(ICON_RECTANGLE, _mode, CreationMode_CreateRectangle, CreationMode_CreateRectangle);
AlienImGui::Tooltip(ModeText.at(CreationMode_CreateRectangle));

ImGui::SameLine();
if (AlienImGui::ToolbarButton(ICON_HEXAGON)) {
_mode = CreationMode::CreateHexagon;
}
AlienImGui::Tooltip(ModeText.at(CreationMode::CreateHexagon));
AlienImGui::SelectableToolbarButton(ICON_HEXAGON, _mode, CreationMode_CreateHexagon, CreationMode_CreateHexagon);
AlienImGui::Tooltip(ModeText.at(CreationMode_CreateHexagon));

ImGui::SameLine();
if (AlienImGui::ToolbarButton(ICON_DISC)) {
_mode = CreationMode::CreateDisc;
}
AlienImGui::Tooltip(ModeText.at(CreationMode::CreateDisc));
AlienImGui::SelectableToolbarButton(ICON_DISC, _mode, CreationMode_CreateDisc, CreationMode_CreateDisc);
AlienImGui::Tooltip(ModeText.at(CreationMode_CreateDisc));

ImGui::SameLine();
if (AlienImGui::ToolbarButton(ICON_FA_PAINT_BRUSH)) {
_mode = CreationMode::Drawing;
}
AlienImGui::Tooltip(ModeText.at(CreationMode::Drawing));
AlienImGui::SelectableToolbarButton(ICON_FA_PAINT_BRUSH, _mode, CreationMode_Drawing, CreationMode_Drawing);
AlienImGui::Tooltip(ModeText.at(CreationMode_Drawing));

if (ImGui::BeginChild("##", ImVec2(0, ImGui::GetContentRegionAvail().y - scale(50.0f)), false, ImGuiWindowFlags_HorizontalScrollbar)) {
AlienImGui::Group(ModeText.at(_mode));

auto color = _editorModel->getDefaultColorCode();
AlienImGui::ComboColor(AlienImGui::ComboColorParameters().name("Color").textWidth(RightColumnWidth).tooltip(Const::GenomeColorTooltip), color);
_editorModel->setDefaultColorCode(color);
if (_mode == CreationMode::Drawing) {
if (_mode == CreationMode_Drawing) {
auto pencilWidth = _editorModel->getPencilWidth();
AlienImGui::SliderFloat(
AlienImGui::SliderFloatParameters()
Expand All @@ -99,13 +87,13 @@ void _CreatorWindow::processIntern()
}
AlienImGui::InputFloat(
AlienImGui::InputFloatParameters().name("Energy").format("%.2f").textWidth(RightColumnWidth).tooltip(Const::CellEnergyTooltip), _energy);
if (_mode != CreationMode::CreateParticle) {
if (_mode != CreationMode_CreateParticle) {
AlienImGui::SliderFloat(
AlienImGui::SliderFloatParameters().name("Stiffness").max(1.0f).min(0.0f).textWidth(RightColumnWidth).tooltip(Const::CellStiffnessTooltip),
&_stiffness);
}

if (_mode == CreationMode::CreateCell) {
if (_mode == CreationMode_CreateCell) {
AlienImGui::SliderInt(
AlienImGui::SliderIntParameters()
.name("Max connections")
Expand All @@ -120,27 +108,27 @@ void _CreatorWindow::processIntern()
.tooltip(Const::CreatorAscendingExecutionOrderNumberTooltip),
_ascendingExecutionNumbers);
}
if (_mode == CreationMode::CreateRectangle) {
if (_mode == CreationMode_CreateRectangle) {
AlienImGui::InputInt(
AlienImGui::InputIntParameters().name("Horizontal cells").textWidth(RightColumnWidth).tooltip(Const::CreatorRectangleWidthTooltip),
_rectHorizontalCells);
AlienImGui::InputInt(
AlienImGui::InputIntParameters().name("Vertical cells").textWidth(RightColumnWidth).tooltip(Const::CreatorRectangleHeightTooltip),
_rectVerticalCells);
}
if (_mode == CreationMode::CreateHexagon) {
if (_mode == CreationMode_CreateHexagon) {
AlienImGui::InputInt(
AlienImGui::InputIntParameters().name("Layers").textWidth(RightColumnWidth).tooltip(Const::CreatorHexagonLayersTooltip), _layers);
}
if (_mode == CreationMode::CreateDisc) {
if (_mode == CreationMode_CreateDisc) {
AlienImGui::InputFloat(
AlienImGui::InputFloatParameters().name("Outer radius").textWidth(RightColumnWidth).format("%.2f").tooltip(Const::CreatorDiscOuterRadiusTooltip),
_outerRadius);
AlienImGui::InputFloat(
AlienImGui::InputFloatParameters().name("Inner radius").textWidth(RightColumnWidth).format("%.2f").tooltip(Const::CreatorDiscInnerRadiusTooltip),
_innerRadius);
}
if (_mode == CreationMode::CreateRectangle || _mode == CreationMode::CreateHexagon || _mode == CreationMode::CreateDisc) {
if (_mode == CreationMode_CreateRectangle || _mode == CreationMode_CreateHexagon || _mode == CreationMode_CreateDisc) {
AlienImGui::InputFloat(
AlienImGui::InputFloatParameters()
.name("Cell distance")
Expand All @@ -150,7 +138,7 @@ void _CreatorWindow::processIntern()
.tooltip(Const::CreatorDistanceTooltip),
_cellDistance);
}
if (_mode != CreationMode::CreateParticle & _mode != CreationMode::CreateCell) {
if (_mode != CreationMode_CreateParticle & _mode != CreationMode_CreateCell) {
AlienImGui::Checkbox(AlienImGui::CheckboxParameters().name("Sticky").textWidth(RightColumnWidth).tooltip(Const::CreatorStickyTooltip), _makeSticky);
}
AlienImGui::Checkbox(
Expand All @@ -159,27 +147,27 @@ void _CreatorWindow::processIntern()
ImGui::EndChild();

AlienImGui::Separator();
if (_mode == CreationMode::Drawing) {
if (_mode == CreationMode_Drawing) {
auto text = _editorModel->isDrawMode() ? "End drawing" : "Start drawing";
if (AlienImGui::Button(text)) {
_editorModel->setDrawMode(!_editorModel->isDrawMode());
}
} else {
_editorModel->setDrawMode(false);
if (AlienImGui::Button("Build")) {
if (_mode == CreationMode::CreateCell) {
if (_mode == CreationMode_CreateCell) {
createCell();
}
if (_mode == CreationMode::CreateParticle) {
if (_mode == CreationMode_CreateParticle) {
createParticle();
}
if (_mode == CreationMode::CreateRectangle) {
if (_mode == CreationMode_CreateRectangle) {
createRectangle();
}
if (_mode == CreationMode::CreateHexagon) {
if (_mode == CreationMode_CreateHexagon) {
createHexagon();
}
if (_mode == CreationMode::CreateDisc) {
if (_mode == CreationMode_CreateDisc) {
createDisc();
}
_editorModel->update();
Expand Down
17 changes: 9 additions & 8 deletions source/Gui/CreatorWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@
#include "Definitions.h"
#include "AlienWindow.h"

enum class CreationMode
using CreationMode = int;
enum CreationMode_
{
CreateParticle,
CreateCell,
CreateRectangle,
CreateHexagon,
CreateDisc,
Drawing
CreationMode_CreateParticle,
CreationMode_CreateCell,
CreationMode_CreateRectangle,
CreationMode_CreateHexagon,
CreationMode_CreateDisc,
CreationMode_Drawing
};

class _CreatorWindow : public _AlienWindow
Expand Down Expand Up @@ -63,7 +64,7 @@ class _CreatorWindow : public _AlienWindow
DescriptionHelper::Occupancy _drawingOccupancy;
RealVector2D _lastDrawPos;

CreationMode _mode = CreationMode::Drawing;
CreationMode _mode = CreationMode_Drawing;

EditorModel _editorModel;
SimulationController _simController;
Expand Down
18 changes: 7 additions & 11 deletions source/Gui/MultiplierWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
namespace
{
auto const ModeText = std::unordered_map<MultiplierMode, std::string>{
{MultiplierMode::Grid, "Grid multiplier"},
{MultiplierMode::Random, "Random multiplier"},
{MultiplierMode_Grid, "Grid multiplier"},
{MultiplierMode_Random, "Random multiplier"},
};

auto const RightColumnWidth = 200.0f;
Expand All @@ -30,24 +30,20 @@ _MultiplierWindow::_MultiplierWindow(EditorModel const& editorModel, SimulationC

void _MultiplierWindow::processIntern()
{
if (AlienImGui::ToolbarButton(ICON_GRID)) {
_mode = MultiplierMode::Grid;
}
AlienImGui::SelectableToolbarButton(ICON_GRID, _mode, MultiplierMode_Grid, MultiplierMode_Grid);

ImGui::SameLine();
if (AlienImGui::ToolbarButton(ICON_RANDOM)) {
_mode = MultiplierMode::Random;
}
AlienImGui::SelectableToolbarButton(ICON_RANDOM, _mode, MultiplierMode_Random, MultiplierMode_Random);

if (ImGui::BeginChild("##", ImVec2(0, ImGui::GetContentRegionAvail().y - scale(50.0f)), false, ImGuiWindowFlags_HorizontalScrollbar)) {

ImGui::BeginDisabled(_editorModel->isSelectionEmpty());

AlienImGui::Group(ModeText.at(_mode));
if (_mode == MultiplierMode::Grid) {
if (_mode == MultiplierMode_Grid) {
processGridPanel();
}
if (_mode == MultiplierMode::Random) {
if (_mode == MultiplierMode_Random) {
processRandomPanel();
}
ImGui::EndDisabled();
Expand Down Expand Up @@ -153,7 +149,7 @@ void _MultiplierWindow::onBuild()
{
_origSelection = _simController->getSelectedSimulationData(true);
auto multiplicationResult = [&] {
if (_mode == MultiplierMode::Grid) {
if (_mode == MultiplierMode_Grid) {
return DescriptionHelper::gridMultiply(_origSelection, _gridParameters);
} else {
auto data = _simController->getSimulationData();
Expand Down
9 changes: 5 additions & 4 deletions source/Gui/MultiplierWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@
#include "Definitions.h"
#include "AlienWindow.h"

enum class MultiplierMode
using MultiplierMode = int;
enum MultiplierMode_
{
Grid,
Random
MultiplierMode_Grid,
MultiplierMode_Random
};

class _MultiplierWindow : public _AlienWindow
Expand All @@ -32,7 +33,7 @@ class _MultiplierWindow : public _AlienWindow
SimulationController _simController;
Viewport _viewport;

MultiplierMode _mode = MultiplierMode::Grid;
MultiplierMode _mode = MultiplierMode_Grid;

DescriptionHelper::GridMultiplyParameters _gridParameters;
DescriptionHelper::RandomMultiplyParameters _randomParameters;
Expand Down

0 comments on commit 91a4454

Please sign in to comment.