Skip to content

Commit

Permalink
make Viewport a singleton
Browse files Browse the repository at this point in the history
  • Loading branch information
chrxh committed Oct 16, 2024
1 parent 1a12723 commit 2797168
Show file tree
Hide file tree
Showing 22 changed files with 104 additions and 108 deletions.
2 changes: 1 addition & 1 deletion source/Gui/AutosaveWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ void _AutosaveWindow::createSavepoint()
printOverlayMessage("Creating save point ...");
static int i = 0;
auto senderInfo = SenderInfo{.senderId = SenderId{AutosaveSenderId}, .wishResultData = true, .wishErrorInfo = true};
auto saveData = SaveSimulationRequestData{"d:\\test" + std::to_string(++i) + ".sim", Viewport::getZoomFactor(), Viewport::getCenterInWorldPos()};
auto saveData = SaveSimulationRequestData{"d:\\test" + std::to_string(++i) + ".sim", Viewport::get().getZoomFactor(), Viewport::get().getCenterInWorldPos()};
auto jobId = _persisterFacade->scheduleSaveSimulationToFile(senderInfo, saveData);

_savePoints.emplace_front(SavepointState::InQueue, jobId.value, "", "", 0);
Expand Down
2 changes: 1 addition & 1 deletion source/Gui/BrowserWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1274,7 +1274,7 @@ void _BrowserWindow::onReplaceResource(BrowserLeaf const& leaf)
auto func = [&] {
auto data = [&]() -> std::variant<ReplaceNetworkResourceRequestData::SimulationData, ReplaceNetworkResourceRequestData::GenomeData> {
if (_currentWorkspace.resourceType == NetworkResourceType_Simulation) {
return ReplaceNetworkResourceRequestData::SimulationData{.zoom = Viewport::getZoomFactor(), .center = Viewport::getCenterInWorldPos()};
return ReplaceNetworkResourceRequestData::SimulationData{.zoom = Viewport::get().getZoomFactor(), .center = Viewport::get().getCenterInWorldPos()};
} else {
return ReplaceNetworkResourceRequestData::GenomeData{.description = _genomeEditorWindow.lock()->getCurrentGenome()};
}
Expand Down
4 changes: 2 additions & 2 deletions source/Gui/CreatorWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ void _CreatorWindow::processIntern()
void _CreatorWindow::onDrawing()
{
auto mousePos = ImGui::GetMousePos();
auto pos = Viewport::mapViewToWorldPosition({mousePos.x, mousePos.y});
auto pos = Viewport::get().mapViewToWorldPosition({mousePos.x, mousePos.y});
if (!_drawingDataDescription.isEmpty()) {
_simulationFacade->removeSelectedObjects(false);
}
Expand Down Expand Up @@ -364,7 +364,7 @@ void _CreatorWindow::validationAndCorrection()

RealVector2D _CreatorWindow::getRandomPos() const
{
auto result = Viewport::getCenterInWorldPos();
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;
Expand Down
28 changes: 14 additions & 14 deletions source/Gui/EditorController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -180,26 +180,26 @@ void _EditorController::onInspectObjects(std::vector<CellOrParticleDescription>
RealVector2D center;
int num = 0;
for (auto const& entity : entities) {
auto entityPos = Viewport::mapWorldToViewPosition(DescriptionEditService::getPos(entity), borderlessRendering);
auto entityPos = Viewport::get().mapWorldToViewPosition(DescriptionEditService::getPos(entity), borderlessRendering);
center += entityPos;
++num;
}
center = center / num;

float maxDistanceFromCenter = 0;
for (auto const& entity : entities) {
auto entityPos = Viewport::mapWorldToViewPosition(DescriptionEditService::getPos(entity), borderlessRendering);
auto entityPos = Viewport::get().mapWorldToViewPosition(DescriptionEditService::getPos(entity), borderlessRendering);
auto distanceFromCenter = toFloat(Math::length(entityPos - center));
maxDistanceFromCenter = std::max(maxDistanceFromCenter, distanceFromCenter);
}
auto viewSize = Viewport::getViewSize();
auto viewSize = Viewport::get().getViewSize();
auto factorX = maxDistanceFromCenter == 0 ? 1.0f : viewSize.x / maxDistanceFromCenter / 3.8f;
auto factorY = maxDistanceFromCenter == 0 ? 1.0f : viewSize.y / maxDistanceFromCenter / 3.4f;

for (auto const& entity : newEntities) {
auto id = DescriptionEditService::getId(entity);
_editorModel->addInspectedEntity(entity);
auto entityPos = Viewport::mapWorldToViewPosition(DescriptionEditService::getPos(entity), borderlessRendering);
auto entityPos = Viewport::get().mapWorldToViewPosition(DescriptionEditService::getPos(entity), borderlessRendering);
auto windowPosX = (entityPos.x - center.x) * factorX + center.x;
auto windowPosY = (entityPos.y - center.y) * factorY + center.y;
windowPosX = std::min(std::max(windowPosX, 0.0f), toFloat(viewSize.x) - 300.0f) + 40.0f;
Expand Down Expand Up @@ -286,8 +286,8 @@ void _EditorController::processInspectorWindows()

void _EditorController::onSelectObjects(RealVector2D const& viewPos, bool modifierKeyPressed)
{
auto pos = Viewport::mapViewToWorldPosition({viewPos.x, viewPos.y});
auto zoom = Viewport::getZoomFactor();
auto pos = Viewport::get().mapViewToWorldPosition({viewPos.x, viewPos.y});
auto zoom = Viewport::get().getZoomFactor();
if (!modifierKeyPressed) {
_simulationFacade->switchSelection(pos, std::max(0.5f, 10.0f / zoom));
} else {
Expand All @@ -302,8 +302,8 @@ void _EditorController::onMoveSelectedObjects(
RealVector2D const& prevWorldPos)
{
auto start = prevWorldPos;
auto end = Viewport::mapViewToWorldPosition({viewPos.x, viewPos.y});
auto zoom = Viewport::getZoomFactor();
auto end = Viewport::get().mapViewToWorldPosition({viewPos.x, viewPos.y});
auto zoom = Viewport::get().getZoomFactor();
auto delta = end - start;

ShallowUpdateSelectionData updateData;
Expand All @@ -320,7 +320,7 @@ void _EditorController::onFixateSelectedObjects(RealVector2D const& viewPos, Rea
auto selectionPosition = RealVector2D{shallowData.centerPosX, shallowData.centerPosY};
auto selectionDelta = selectionPosition - selectionPositionOnClick;

auto mouseStart = Viewport::mapViewToWorldPosition(viewPos);
auto mouseStart = Viewport::get().mapViewToWorldPosition(viewPos);
auto mouseEnd = prevWorldPos;
auto mouseDelta = mouseStart - mouseEnd;

Expand All @@ -338,7 +338,7 @@ void _EditorController::onFixateSelectedObjects(RealVector2D const& viewPos, Rea
void _EditorController::onAccelerateSelectedObjects(RealVector2D const& viewPos, RealVector2D const& prevWorldPos)
{
auto start = prevWorldPos;
auto end = Viewport::mapViewToWorldPosition({viewPos.x, viewPos.y});
auto end = Viewport::get().mapViewToWorldPosition({viewPos.x, viewPos.y});
auto delta = end - start;

ShallowUpdateSelectionData updateData;
Expand All @@ -351,15 +351,15 @@ void _EditorController::onAccelerateSelectedObjects(RealVector2D const& viewPos,
void _EditorController::onApplyForces(RealVector2D const& viewPos, RealVector2D const& prevWorldPos)
{
auto start = prevWorldPos;
auto end = Viewport::mapViewToWorldPosition({viewPos.x, viewPos.y});
auto zoom = Viewport::getZoomFactor();
auto end = Viewport::get().mapViewToWorldPosition({viewPos.x, viewPos.y});
auto zoom = Viewport::get().getZoomFactor();
_simulationFacade->applyForce_async(start, end, (end - start) / 50.0 * std::min(5.0f, zoom), 20.0f / zoom);
}

void _EditorController::onUpdateSelectionRect(RealRect const& rect)
{
auto startPos = Viewport::mapViewToWorldPosition(rect.topLeft);
auto endPos = Viewport::mapViewToWorldPosition(rect.bottomRight);
auto startPos = Viewport::get().mapViewToWorldPosition(rect.topLeft);
auto endPos = Viewport::get().mapViewToWorldPosition(rect.bottomRight);
auto topLeft = RealVector2D{std::min(startPos.x, endPos.x), std::min(startPos.y, endPos.y)};
auto bottomRight = RealVector2D{std::max(startPos.x, endPos.x), std::max(startPos.y, endPos.y)};

Expand Down
6 changes: 3 additions & 3 deletions source/Gui/FileTransferController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ void FileTransferController::onOpenSimulation()
}
_persisterFacade->restart();

Viewport::setCenterInWorldPos(data.deserializedSimulation.auxiliaryData.center);
Viewport::setZoomFactor(data.deserializedSimulation.auxiliaryData.zoom);
Viewport::get().setCenterInWorldPos(data.deserializedSimulation.auxiliaryData.center);
Viewport::get().setZoomFactor(data.deserializedSimulation.auxiliaryData.zoom);
_temporalControlWindow->onSnapshot();
printOverlayMessage(data.simulationName + ".sim");
},
Expand All @@ -93,7 +93,7 @@ void FileTransferController::onSaveSimulation()
_referencePath = firstFilenameCopy.remove_filename().string();
printOverlayMessage("Saving ...");
auto senderInfo = SenderInfo{.senderId = SenderId{FileTransferSenderId}, .wishResultData = false, .wishErrorInfo = true};
auto saveData = SaveSimulationRequestData{firstFilename.string(), Viewport::getZoomFactor(), Viewport::getCenterInWorldPos()};
auto saveData = SaveSimulationRequestData{firstFilename.string(), Viewport::get().getZoomFactor(), Viewport::get().getCenterInWorldPos()};
_persisterFacade->scheduleSaveSimulationToFile(senderInfo, saveData);
});
}
Expand Down
2 changes: 1 addition & 1 deletion source/Gui/GenomeEditorWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -933,7 +933,7 @@ void _GenomeEditorWindow::onNodeIncreaseSequenceNumber()

void _GenomeEditorWindow::onCreateSpore()
{
auto pos = Viewport::getCenterInWorldPos();
auto pos = Viewport::get().getCenterInWorldPos();
pos.x += (toFloat(std::rand()) / RAND_MAX - 0.5f) * 8;
pos.y += (toFloat(std::rand()) / RAND_MAX - 0.5f) * 8;

Expand Down
2 changes: 1 addition & 1 deletion source/Gui/ImageToPatternDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ void _ImageToPatternDialog::show()
}

DescriptionEditService::reconnectCells(dataDesc, 1 * 1.5f);
dataDesc.setCenter(Viewport::getCenterInWorldPos());
dataDesc.setCenter(Viewport::get().getCenterInWorldPos());

_simulationFacade->addAndSelectSimulationData(dataDesc);
//TODO: update pattern editor
Expand Down
2 changes: 1 addition & 1 deletion source/Gui/InspectorWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ void _InspectorWindow::process()
processParticle(std::get<ParticleDescription>(entity));
}
ImDrawList* drawList = ImGui::GetBackgroundDrawList();
auto entityPos = Viewport::mapWorldToViewPosition(DescriptionEditService::getPos(entity), borderlessRendering);
auto entityPos = Viewport::get().mapWorldToViewPosition(DescriptionEditService::getPos(entity), borderlessRendering);
auto factor = StyleRepository::get().scale(1);

drawList->AddLine(
Expand Down
2 changes: 1 addition & 1 deletion source/Gui/MainWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ _MainWindow::_MainWindow(SimulationFacade const& simulationFacade, PersisterFaca
NetworkService::init();

//init controllers, windows and dialogs
Viewport::init(_simulationFacade);
Viewport::get().init(_simulationFacade);
_persisterFacade->init(_simulationFacade);
_uiController = std::make_shared<_UiController>();
_autosaveController = std::make_shared<_AutosaveController>(_simulationFacade);
Expand Down
4 changes: 2 additions & 2 deletions source/Gui/NetworkTransferController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ void NetworkTransferController::onDownload(DownloadNetworkResourceRequestData co
}
_persisterFacade->restart();

Viewport::setCenterInWorldPos(deserializedSimulation.auxiliaryData.center);
Viewport::setZoomFactor(deserializedSimulation.auxiliaryData.zoom);
Viewport::get().setCenterInWorldPos(deserializedSimulation.auxiliaryData.center);
Viewport::get().setZoomFactor(deserializedSimulation.auxiliaryData.zoom);
_temporalControlWindow->onSnapshot();
} else {
_editorController->setOn(true);
Expand Down
4 changes: 2 additions & 2 deletions source/Gui/NewSimulationDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ void _NewSimulationDialog::onNewSimulation()
generalSettings.worldSizeX = _width;
generalSettings.worldSizeY = _height;
_simulationFacade->newSimulation(std::nullopt, 0, generalSettings, parameters);
Viewport::setCenterInWorldPos({toFloat(_width) / 2, toFloat(_height) / 2});
Viewport::setZoomFactor(4.0f);
Viewport::get().setCenterInWorldPos({toFloat(_width) / 2, toFloat(_height) / 2});
Viewport::get().setZoomFactor(4.0f);
_temporalControlWindow->onSnapshot();
}
4 changes: 2 additions & 2 deletions source/Gui/OverlayMessageController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ void OverlayMessageController::processLoadingBar()

ImDrawList* drawList = ImGui::GetBackgroundDrawList();

auto viewSize = toRealVector2D(Viewport::getViewSize());
auto viewSize = toRealVector2D(Viewport::get().getViewSize());
auto width = viewSize.x / 6 + 1.0f;
auto height = scale(20.0f);
auto center = ImVec2{viewSize.x / 2, viewSize.y - scale(60.0f)};
Expand Down Expand Up @@ -168,7 +168,7 @@ void OverlayMessageController::processMessage()
if (_withLightning && ++_counter > 2) {
auto durationAfterSomeTicks = std::chrono::duration_cast<std::chrono::milliseconds>(now - *_ticksLaterTimepoint);
float lightningAlpha = std::max(0.0f, 0.7f - static_cast<float>(durationAfterSomeTicks.count()) / FadeoutLightningDuration);
auto viewSize = toRealVector2D(Viewport::getViewSize());
auto viewSize = toRealVector2D(Viewport::get().getViewSize());
drawList->AddRectFilled({0, 0}, {viewSize.x, viewSize.y}, ImColor::HSV(0.0f, 0.0f, 1.0f, lightningAlpha));
}
drawList->AddText(
Expand Down
4 changes: 2 additions & 2 deletions source/Gui/PatternEditorWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ void _PatternEditorWindow::onOpenPattern()
_startingPath = firstFilenameCopy.remove_filename().string();
ClusteredDataDescription content;
if (SerializerService::deserializeContentFromFile(content, firstFilename.string())) {
auto center = Viewport::getCenterInWorldPos();
auto center = Viewport::get().getCenterInWorldPos();
content.setCenter(center);
_simulationFacade->addAndSelectSimulationData(DataDescription(content));
_editorModel->update();
Expand Down Expand Up @@ -387,7 +387,7 @@ bool _PatternEditorWindow::isPastingPossible() const
void _PatternEditorWindow::onPaste()
{
auto data = *_copiedSelection;
auto center = Viewport::getCenterInWorldPos();
auto center = Viewport::get().getCenterInWorldPos();
data.setCenter(center);
DescriptionEditService::generateNewCreatureIds(data);
_simulationFacade->addAndSelectSimulationData(data);
Expand Down
4 changes: 2 additions & 2 deletions source/Gui/SerializationHelperService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ DeserializedSimulation SerializationHelperService::getDeserializedSerialization(
DeserializedSimulation result;
result.auxiliaryData.timestep = static_cast<uint32_t>(simulationFacade->getCurrentTimestep());
result.auxiliaryData.realTime = simulationFacade->getRealTime();
result.auxiliaryData.zoom = Viewport::getZoomFactor();
result.auxiliaryData.center = Viewport::getCenterInWorldPos();
result.auxiliaryData.zoom = Viewport::get().getZoomFactor();
result.auxiliaryData.center = Viewport::get().getCenterInWorldPos();
result.auxiliaryData.generalSettings = simulationFacade->getGeneralSettings();
result.auxiliaryData.simulationParameters = simulationFacade->getSimulationParameters();
result.statistics = simulationFacade->getStatisticsHistory().getCopiedData();
Expand Down
Loading

0 comments on commit 2797168

Please sign in to comment.