Skip to content

Commit

Permalink
make singletons, part 3
Browse files Browse the repository at this point in the history
  • Loading branch information
chrxh committed Oct 20, 2024
1 parent ea1c35c commit 0a88f00
Show file tree
Hide file tree
Showing 28 changed files with 341 additions and 380 deletions.
2 changes: 1 addition & 1 deletion source/Gui/BrowserWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1253,7 +1253,7 @@ void BrowserWindow::onReplaceResource(BrowserLeaf const& leaf)
if (_currentWorkspace.resourceType == NetworkResourceType_Simulation) {
return ReplaceNetworkResourceRequestData::SimulationData{.zoom = Viewport::get().getZoomFactor(), .center = Viewport::get().getCenterInWorldPos()};
} else {
return ReplaceNetworkResourceRequestData::GenomeData{.description = EditorController::get().getGenomeEditorWindow()->getCurrentGenome()};
return ReplaceNetworkResourceRequestData::GenomeData{.description = GenomeEditorWindow::get().getCurrentGenome()};
}
}();
NetworkTransferController::get().onReplace(ReplaceNetworkResourceRequestData{
Expand Down
16 changes: 9 additions & 7 deletions source/Gui/ChangeColorDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,17 @@
#include "AlienImGui.h"
#include "StyleRepository.h"

_ChangeColorDialog::_ChangeColorDialog(
std::function<GenomeDescription()> const& getGenomeFunc,
std::function<void(GenomeDescription const&)> const& setGenomeFunc)
void ChangeColorDialog::init(std::function<GenomeDescription()> const& getGenomeFunc, std::function<void(GenomeDescription const&)> const& setGenomeFunc)
{
_getGenomeFunc = getGenomeFunc;
_setGenomeFunc = setGenomeFunc;
}

ChangeColorDialog::ChangeColorDialog()
: AlienDialog("Change color")
, _getGenomeFunc(getGenomeFunc)
, _setGenomeFunc(setGenomeFunc)
{}

void _ChangeColorDialog::processIntern()
void ChangeColorDialog::processIntern()
{
AlienImGui::Group("Color transition rule");
if (ImGui::BeginTable("##", 3, ImGuiTableFlags_SizingStretchProp)) {
Expand Down Expand Up @@ -62,7 +64,7 @@ void _ChangeColorDialog::processIntern()
}
}

void _ChangeColorDialog::onChangeColor(GenomeDescription& genome)
void ChangeColorDialog::onChangeColor(GenomeDescription& genome)
{
for (auto& node : genome.cells) {
if (node.color == _sourceColor) {
Expand Down
9 changes: 7 additions & 2 deletions source/Gui/ChangeColorDialog.h
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
#pragma once

#include "EngineInterface/GenomeDescriptions.h"
#include "Base/Singleton.h"

#include "AlienDialog.h"

class _ChangeColorDialog : public AlienDialog
class ChangeColorDialog : public AlienDialog
{
MAKE_SINGLETON_NO_DEFAULT_CONSTRUCTION(ChangeColorDialog);

public:
_ChangeColorDialog(std::function<GenomeDescription()> const& getGenomeFunc, std::function<void(GenomeDescription const&)> const& setGenomeFunc);
void init(std::function<GenomeDescription()> const& getGenomeFunc, std::function<void(GenomeDescription const&)> const& setGenomeFunc);

private:
ChangeColorDialog();

void processIntern() override;

void onChangeColor(GenomeDescription& genome);
Expand Down
57 changes: 30 additions & 27 deletions source/Gui/CreatorWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,12 @@ namespace
auto const RightColumnWidth = 160.0f;
}

_CreatorWindow::_CreatorWindow(EditorModel const& editorModel, SimulationFacade const& simulationFacade)
: AlienWindow("Creator", "editors.creator", false), _editorModel(editorModel)
, _simulationFacade(simulationFacade)
void CreatorWindow::init(SimulationFacade const& simulationFacade)
{
_simulationFacade = simulationFacade;
}

void _CreatorWindow::processIntern()
void CreatorWindow::processIntern()
{
AlienImGui::SelectableToolbarButton(ICON_FA_SUN, _mode, CreationMode_CreateParticle, CreationMode_CreateParticle);
AlienImGui::Tooltip(ModeText.at(CreationMode_CreateParticle));
Expand Down Expand Up @@ -69,11 +68,11 @@ void _CreatorWindow::processIntern()
if (ImGui::BeginChild("##", ImVec2(0, ImGui::GetContentRegionAvail().y - scale(50.0f)), false, ImGuiWindowFlags_HorizontalScrollbar)) {
AlienImGui::Group(ModeText.at(_mode));

auto color = _editorModel->getDefaultColorCode();
auto color = EditorModel::get().getDefaultColorCode();
AlienImGui::ComboColor(AlienImGui::ComboColorParameters().name("Color").textWidth(RightColumnWidth).tooltip(Const::GenomeColorTooltip), color);
_editorModel->setDefaultColorCode(color);
EditorModel::get().setDefaultColorCode(color);
if (_mode == CreationMode_Drawing) {
auto pencilWidth = _editorModel->getPencilWidth();
auto pencilWidth = EditorModel::get().getPencilWidth();
AlienImGui::SliderFloat(
AlienImGui::SliderFloatParameters()
.name("Pencil radius")
Expand All @@ -83,7 +82,7 @@ void _CreatorWindow::processIntern()
.format("%.1f")
.tooltip(Const::CreatorPencilRadiusTooltip),
&pencilWidth);
_editorModel->setPencilWidth(pencilWidth);
EditorModel::get().setPencilWidth(pencilWidth);
}
AlienImGui::InputFloat(
AlienImGui::InputFloatParameters().name("Energy").format("%.2f").textWidth(RightColumnWidth).tooltip(Const::CellEnergyTooltip), _energy);
Expand Down Expand Up @@ -173,13 +172,13 @@ void _CreatorWindow::processIntern()
if (_mode == CreationMode_CreateDisc) {
createDisc();
}
_editorModel->update();
EditorModel::get().update();
}
}
validationAndCorrection();
}

void _CreatorWindow::onDrawing()
void CreatorWindow::onDrawing()
{
auto mousePos = ImGui::GetMousePos();
auto pos = Viewport::get().mapViewToWorldPosition({mousePos.x, mousePos.y});
Expand All @@ -188,18 +187,18 @@ void _CreatorWindow::onDrawing()
}

auto createAlignedCircle = [&](auto pos) {
if (_editorModel->getPencilWidth() > 1 + NEAR_ZERO) {
if (EditorModel::get().getPencilWidth() > 1 + NEAR_ZERO) {
pos.x = toFloat(toInt(pos.x));
pos.y = toFloat(toInt(pos.y));
}
return DescriptionEditService::createUnconnectedCircle(DescriptionEditService::CreateUnconnectedCircleParameters()
.center(pos)
.radius(_editorModel->getPencilWidth())
.radius(EditorModel::get().getPencilWidth())
.energy(_energy)
.stiffness(_stiffness)
.cellDistance(1.0f)
.maxConnections(MAX_CELL_BONDS)
.color(_editorModel->getDefaultColorCode())
.color(EditorModel::get().getDefaultColorCode())
.barrier(_barrier));
};

Expand Down Expand Up @@ -229,16 +228,20 @@ void _CreatorWindow::onDrawing()
}

_simulationFacade->reconnectSelectedObjects();
_editorModel->update();
EditorModel::get().update();
}

void _CreatorWindow::finishDrawing()
void CreatorWindow::finishDrawing()
{
_drawingDataDescription.clear();
_drawingOccupancy.clear();
}

void _CreatorWindow::createCell()
CreatorWindow::CreatorWindow()
: AlienWindow("Creator", "editors.creator", false)
{}

void CreatorWindow::createCell()
{
auto creatureId = toInt(NumberGenerator::get().getRandomInt(std::numeric_limits<int>::max()));

Expand All @@ -248,7 +251,7 @@ void _CreatorWindow::createCell()
.setStiffness(_stiffness)
.setMaxConnections(_maxConnections)
.setExecutionOrderNumber(_lastExecutionNumber)
.setColor(_editorModel->getDefaultColorCode())
.setColor(EditorModel::get().getDefaultColorCode())
.setBarrier(_barrier)
.setCreatureId(creatureId);
if (_ascendingExecutionNumbers) {
Expand All @@ -259,14 +262,14 @@ void _CreatorWindow::createCell()
incExecutionNumber();
}

void _CreatorWindow::createParticle()
void CreatorWindow::createParticle()
{
auto particle = ParticleDescription().setPos(getRandomPos()).setEnergy(_energy);
auto data = DataDescription().addParticle(particle);
_simulationFacade->addAndSelectSimulationData(data);
}

void _CreatorWindow::createRectangle()
void CreatorWindow::createRectangle()
{
if (_rectHorizontalCells <= 0 || _rectVerticalCells <= 0) {
return;
Expand All @@ -280,14 +283,14 @@ void _CreatorWindow::createRectangle()
.stiffness(_stiffness)
.removeStickiness(!_makeSticky)
.maxConnections(MAX_CELL_BONDS)
.color(_editorModel->getDefaultColorCode())
.color(EditorModel::get().getDefaultColorCode())
.center(getRandomPos())
.barrier(_barrier));

_simulationFacade->addAndSelectSimulationData(data);
}

void _CreatorWindow::createHexagon()
void CreatorWindow::createHexagon()
{
if (_layers <= 0) {
return;
Expand All @@ -299,13 +302,13 @@ void _CreatorWindow::createHexagon()
.stiffness(_stiffness)
.removeStickiness(!_makeSticky)
.maxConnections(MAX_CELL_BONDS)
.color(_editorModel->getDefaultColorCode())
.color(EditorModel::get().getDefaultColorCode())
.center(getRandomPos())
.barrier(_barrier));
_simulationFacade->addAndSelectSimulationData(data);
}

void _CreatorWindow::createDisc()
void CreatorWindow::createDisc()
{
if (_innerRadius > _outerRadius || _innerRadius < 0 || _outerRadius <= 0) {
return;
Expand All @@ -332,7 +335,7 @@ void _CreatorWindow::createDisc()
.setStiffness(_stiffness)
.setPos(relPos)
.setMaxConnections(MAX_CELL_BONDS)
.setColor(_editorModel->getDefaultColorCode())
.setColor(EditorModel::get().getDefaultColorCode())
.setBarrier(_barrier));
}
}
Expand All @@ -345,7 +348,7 @@ void _CreatorWindow::createDisc()
_simulationFacade->addAndSelectSimulationData(data);
}

void _CreatorWindow::validationAndCorrection()
void CreatorWindow::validationAndCorrection()
{
_energy = std::max(0.0f, _energy);
_stiffness = std::min(1.0f, std::max(0.0f, _stiffness));
Expand All @@ -357,15 +360,15 @@ void _CreatorWindow::validationAndCorrection()
_innerRadius = std::max(1.0f, _innerRadius);
}

RealVector2D _CreatorWindow::getRandomPos() const
RealVector2D CreatorWindow::getRandomPos() const
{
auto result = Viewport::get().getCenterInWorldPos();
result.x += (toFloat(std::rand()) / RAND_MAX - 0.5f) * 8;
result.y += (toFloat(std::rand()) / RAND_MAX - 0.5f) * 8;
return result;
}

void _CreatorWindow::incExecutionNumber()
void CreatorWindow::incExecutionNumber()
{
if (_ascendingExecutionNumbers) {
auto parameters = _simulationFacade->getSimulationParameters();
Expand Down
10 changes: 7 additions & 3 deletions source/Gui/CreatorWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#include "Definitions.h"
#include "AlienWindow.h"
#include "Base/Singleton.h"

using CreationMode = int;
enum CreationMode_
Expand All @@ -17,15 +18,19 @@ enum CreationMode_
CreationMode_Drawing
};

class _CreatorWindow : public AlienWindow
class CreatorWindow : public AlienWindow
{
MAKE_SINGLETON_NO_DEFAULT_CONSTRUCTION(CreatorWindow);

public:
_CreatorWindow(EditorModel const& editorModel, SimulationFacade const& simulationFacade);
void init(SimulationFacade const& simulationFacade);

void onDrawing();
void finishDrawing();

private:
CreatorWindow();

void processIntern();

void createCell();
Expand Down Expand Up @@ -66,6 +71,5 @@ class _CreatorWindow : public AlienWindow

CreationMode _mode = CreationMode_Drawing;

EditorModel _editorModel;
SimulationFacade _simulationFacade;
};
27 changes: 8 additions & 19 deletions source/Gui/Definitions.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,33 +54,24 @@ class GettingStartedWindow;

class DisplaySettingsDialog;

class _EditorModel;
using EditorModel = std::shared_ptr<_EditorModel>;
class EditorModel;

class EditorController;

class _SelectionWindow;
using SelectionWindow = std::shared_ptr<_SelectionWindow>;
class SelectionWindow;

class _PatternEditorWindow;
using PatternEditorWindow = std::shared_ptr<_PatternEditorWindow>;
class PatternEditorWindow;

class WindowController;

class _ResizeWorldDialog;
using ResizeWorldDialog = std::shared_ptr<_ResizeWorldDialog>;

class _OpenSymbolsDialog;
using OpenSymbolsDialog = std::shared_ptr<_OpenSymbolsDialog>;
class ResizeWorldDialog;

class _InspectorWindow;
using InspectorWindow = std::shared_ptr<_InspectorWindow>;

class _CreatorWindow;
using CreatorWindow = std::shared_ptr<_CreatorWindow>;
class CreatorWindow;

class _MultiplierWindow;
using MultiplierWindow = std::shared_ptr<_MultiplierWindow>;
class MultiplierWindow;

class PatternAnalysisDialog;

Expand Down Expand Up @@ -110,13 +101,11 @@ class NewPasswordDialog;

class ImageToPatternDialog;

class _GenomeEditorWindow;
using GenomeEditorWindow = std::shared_ptr<_GenomeEditorWindow>;
class GenomeEditorWindow;

class RadiationSourcesWindow;

class _ChangeColorDialog;
using ChangeColorDialog = std::shared_ptr<_ChangeColorDialog>;
class ChangeColorDialog;

class AutosaveWindow;

Expand Down
Loading

0 comments on commit 0a88f00

Please sign in to comment.