Skip to content

Commit

Permalink
Refactor UI/Simulation to west-const
Browse files Browse the repository at this point in the history
  • Loading branch information
adamkewley committed Jun 17, 2024
1 parent ec29aef commit 8baee94
Show file tree
Hide file tree
Showing 29 changed files with 165 additions and 212 deletions.
11 changes: 3 additions & 8 deletions src/OpenSimCreator/Documents/Model/BasicModelStatePair.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ class osc::BasicModelStatePair::Impl final {

explicit Impl(IConstModelStatePair const& p) :
Impl{p.getModel(), p.getState(), p.getFixupScaleFactor()}
{
}
{}

explicit Impl(std::filesystem::path const& osimPath) :
m_Model{std::make_unique<OpenSim::Model>(osimPath.string())}
Expand All @@ -31,8 +30,7 @@ class osc::BasicModelStatePair::Impl final {

Impl(OpenSim::Model const& m, SimTK::State const& st) :
Impl{m, st, 1.0f}
{
}
{}

Impl(OpenSim::Model const& m, SimTK::State const& st, float fixupScaleFactor) :
m_Model(std::make_unique<OpenSim::Model>(m)),
Expand Down Expand Up @@ -88,8 +86,6 @@ class osc::BasicModelStatePair::Impl final {
};


// public API

osc::BasicModelStatePair::BasicModelStatePair() :
m_Impl{std::make_unique<Impl>()}
{}
Expand All @@ -104,8 +100,7 @@ osc::BasicModelStatePair::BasicModelStatePair(std::filesystem::path const& p) :

osc::BasicModelStatePair::BasicModelStatePair(OpenSim::Model const& model, SimTK::State const& state) :
m_Impl{std::make_unique<Impl>(model, state)}
{
}
{}
osc::BasicModelStatePair::BasicModelStatePair(BasicModelStatePair const&) = default;
osc::BasicModelStatePair::BasicModelStatePair(BasicModelStatePair&&) noexcept = default;
osc::BasicModelStatePair& osc::BasicModelStatePair::operator=(BasicModelStatePair const&) = default;
Expand Down
1 change: 1 addition & 0 deletions src/OpenSimCreator/Documents/Model/BasicModelStatePair.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ namespace osc
BasicModelStatePair& operator=(BasicModelStatePair const&);
BasicModelStatePair& operator=(BasicModelStatePair&&) noexcept;
~BasicModelStatePair() noexcept override;

private:
OpenSim::Model const& implGetModel() const final;
SimTK::State const& implGetState() const final;
Expand Down
18 changes: 3 additions & 15 deletions src/OpenSimCreator/Documents/Model/ModelStateCommit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ class osc::ModelStateCommit::Impl final {
public:
Impl(IConstModelStatePair const& msp, std::string_view message) :
Impl{msp, message, UID::empty()}
{
}
{}

Impl(IConstModelStatePair const& msp, std::string_view message, UID parent) :
m_MaybeParentID{parent},
Expand Down Expand Up @@ -87,23 +86,12 @@ class osc::ModelStateCommit::Impl final {
};


// public API (PIMPL)

osc::ModelStateCommit::ModelStateCommit(IConstModelStatePair const& p, std::string_view message) :
m_Impl{std::make_shared<Impl>(p, message)}
{
}

{}
osc::ModelStateCommit::ModelStateCommit(IConstModelStatePair const& p, std::string_view message, UID parent) :
m_Impl{std::make_shared<Impl>(p, message, parent)}
{
}

osc::ModelStateCommit::ModelStateCommit(ModelStateCommit const&) = default;
osc::ModelStateCommit::ModelStateCommit(ModelStateCommit&&) noexcept = default;
osc::ModelStateCommit& osc::ModelStateCommit::operator=(ModelStateCommit const&) = default;
osc::ModelStateCommit& osc::ModelStateCommit::operator=(ModelStateCommit&&) noexcept = default;
osc::ModelStateCommit::~ModelStateCommit() noexcept = default;
{}

UID osc::ModelStateCommit::getID() const
{
Expand Down
5 changes: 0 additions & 5 deletions src/OpenSimCreator/Documents/Model/ModelStateCommit.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,6 @@ namespace osc
public:
ModelStateCommit(IConstModelStatePair const&, std::string_view message);
ModelStateCommit(IConstModelStatePair const&, std::string_view message, UID parent);
ModelStateCommit(ModelStateCommit const&);
ModelStateCommit(ModelStateCommit&&) noexcept;
ModelStateCommit& operator=(ModelStateCommit const&);
ModelStateCommit& operator=(ModelStateCommit&&) noexcept;
~ModelStateCommit() noexcept;

UID getID() const;
bool hasParent() const;
Expand Down
4 changes: 2 additions & 2 deletions src/OpenSimCreator/Documents/Model/ModelStatePairInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
#include <OpenSimCreator/Utils/OpenSimHelpers.h>

osc::ModelStatePairInfo::ModelStatePairInfo() = default;

osc::ModelStatePairInfo::ModelStatePairInfo(IConstModelStatePair const& msp) :
m_ModelVersion{msp.getModelVersion()},
m_StateVersion{msp.getStateVersion()},
m_Selection{GetAbsolutePathOrEmpty(msp.getSelected())},
m_Hover{GetAbsolutePathOrEmpty(msp.getHovered())},
m_FixupScaleFactor{msp.getFixupScaleFactor()}
{
}
{}
14 changes: 4 additions & 10 deletions src/OpenSimCreator/Documents/Model/ObjectPropertyEdit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,10 @@ namespace
// an empty string
std::string GetAbsPathOrEmptyIfNotAComponent(OpenSim::Object const& obj)
{
if (auto const* c = dynamic_cast<OpenSim::Component const*>(&obj))
{
if (auto const* c = dynamic_cast<OpenSim::Component const*>(&obj)) {
return GetAbsolutePathString(*c);
}
else
{
else {
return std::string{};
}
}
Expand All @@ -35,9 +33,7 @@ osc::ObjectPropertyEdit::ObjectPropertyEdit(

m_PropertyName{prop.getName()},
m_Updater{std::move(updater)}
{
}

{}
osc::ObjectPropertyEdit::ObjectPropertyEdit(
OpenSim::Object const& obj,
OpenSim::AbstractProperty const& prop,
Expand All @@ -46,9 +42,7 @@ osc::ObjectPropertyEdit::ObjectPropertyEdit(
m_ComponentAbsPath{GetAbsPathOrEmptyIfNotAComponent(obj)},
m_PropertyName{prop.getName()},
m_Updater{std::move(updater)}
{
}

{}
std::string const& osc::ObjectPropertyEdit::getComponentAbsPath() const
{
return m_ComponentAbsPath;
Expand Down
10 changes: 5 additions & 5 deletions src/OpenSimCreator/Documents/Model/UndoableModelStatePair.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ class osc::UndoableModelStatePair::Impl final {
}
}

std::filesystem::path const& getFilesystemPath() const
std::filesystem::path getFilesystemPath() const
{
return getFilesystemLocation();
}
Expand All @@ -259,7 +259,7 @@ class osc::UndoableModelStatePair::Impl final {
return m_MaybeFilesystemTimestamp;
}

ModelStateCommit const& getLatestCommit() const
ModelStateCommit getLatestCommit() const
{
return getHeadCommit();
}
Expand Down Expand Up @@ -651,7 +651,7 @@ class osc::UndoableModelStatePair::Impl final {
m_CurrentHead = c->getID();
}

std::filesystem::path const& getFilesystemLocation() const
std::filesystem::path getFilesystemLocation() const
{
return m_MaybeFilesystemLocation;
}
Expand Down Expand Up @@ -742,7 +742,7 @@ std::string osc::UndoableModelStatePair::recommendedDocumentName() const
return m_Impl->recommendedDocumentName();
}

std::filesystem::path const& osc::UndoableModelStatePair::getFilesystemPath() const
std::filesystem::path osc::UndoableModelStatePair::getFilesystemPath() const
{
return m_Impl->getFilesystemPath();
}
Expand All @@ -768,7 +768,7 @@ std::filesystem::file_time_type osc::UndoableModelStatePair::getLastFilesystemWr
}


ModelStateCommit const& osc::UndoableModelStatePair::getLatestCommit() const
ModelStateCommit osc::UndoableModelStatePair::getLatestCommit() const
{
return m_Impl->getLatestCommit();
}
Expand Down
18 changes: 14 additions & 4 deletions src/OpenSimCreator/Documents/Model/UndoableModelStatePair.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,18 @@ namespace SimTK { class State; }

namespace osc
{
// a model + state pair that automatically reinitializes (i.e. like `AutoFinalizingModelStatePair`),
// but it also has support for snapshotting with .commit()
// `UndoableModelStatePair` is an `IModelStatePair` that's designed for immediate UI usage.
//
// Key features:
//
// - supports deferred (queued) undo/redo/rollback
// - supports deferred (queued) model mutation
// - all deferred changes can be applied by calling `applyQueuedMutations`
//
// The reason why all mutations are deferred is so that the UI and associated systems can
// safely get references to elements inside the `OpenSim::Model` and `SimTK::State` without
// having to tiptoe around reference invalidation due to mid-drawcall mutations (a
// drawback/benefit of immediate UIs)
class UndoableModelStatePair final : public IModelStatePair {
public:

Expand Down Expand Up @@ -56,7 +66,7 @@ namespace osc
// returns the full filesystem path of the model's on-disk location, if applicable
//
// returns an empty path if the model has not been saved to disk
std::filesystem::path const& getFilesystemPath() const;
std::filesystem::path getFilesystemPath() const;

// sets the full filesystem path of the model's on-disk location
//
Expand All @@ -76,7 +86,7 @@ namespace osc

// returns latest *comitted* model state (i.e. not the one being actively edited, but the one saved into
// the safer undo/redo buffer)
ModelStateCommit const& getLatestCommit() const;
ModelStateCommit getLatestCommit() const;

// manipulate undo/redo state
bool canUndo() const;
Expand Down
2 changes: 1 addition & 1 deletion src/OpenSimCreator/UI/ModelEditor/ModelMusclePlotPanel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1941,7 +1941,7 @@ namespace

// trick: we "know" that the last edit to the model was a coordinate edit in this plot's
// independent variable, so we can skip recomputing it
ModelStateCommit const& commitAfter = getShared().getModel().getLatestCommit();
ModelStateCommit commitAfter = getShared().getModel().getLatestCommit();
m_Lines.setActivePlotCommit(commitAfter);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/OpenSimCreator/UI/Simulation/ISimulatorUIAPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ std::vector<OutputExtractor> osc::ISimulatorUIAPI::getAllUserOutputExtractors()
return rv;
}

std::optional<std::filesystem::path> osc::ISimulatorUIAPI::tryPromptToSaveOutputsAsCSV(std::span<OutputExtractor const> outputs) const
std::optional<std::filesystem::path> osc::ISimulatorUIAPI::tryPromptToSaveOutputsAsCSV(std::span<const OutputExtractor> outputs) const
{
return TryExportOutputsToCSV(getSimulation(), outputs);
}
Expand Down
32 changes: 16 additions & 16 deletions src/OpenSimCreator/UI/Simulation/ISimulatorUIAPI.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ namespace osc
class ISimulatorUIAPI {
protected:
ISimulatorUIAPI() = default;
ISimulatorUIAPI(ISimulatorUIAPI const&) = default;
ISimulatorUIAPI(const ISimulatorUIAPI&) = default;
ISimulatorUIAPI(ISimulatorUIAPI&&) noexcept = default;
ISimulatorUIAPI& operator=(ISimulatorUIAPI const&) = default;
ISimulatorUIAPI& operator=(const ISimulatorUIAPI&) = default;
ISimulatorUIAPI& operator=(ISimulatorUIAPI&&) noexcept = default;
public:
virtual ~ISimulatorUIAPI() noexcept = default;

ISimulation const& getSimulation() const { return implGetSimulation(); }
const ISimulation& getSimulation() const { return implGetSimulation(); }
ISimulation& updSimulation() { return implUpdSimulation(); }

SimulationUIPlaybackState getSimulationPlaybackState() { return implGetSimulationPlaybackState(); }
Expand All @@ -53,24 +53,24 @@ namespace osc
std::optional<SimulationReport> trySelectReportBasedOnScrubbing() { return implTrySelectReportBasedOnScrubbing(); }

int getNumUserOutputExtractors() const { return implGetNumUserOutputExtractors(); }
OutputExtractor const& getUserOutputExtractor(int i) const { return implGetUserOutputExtractor(i); }
const OutputExtractor& getUserOutputExtractor(int i) const { return implGetUserOutputExtractor(i); }
std::vector<OutputExtractor> getAllUserOutputExtractors() const;
void addUserOutputExtractor(OutputExtractor const& o) { implAddUserOutputExtractor(o); }
void addUserOutputExtractor(const OutputExtractor& o) { implAddUserOutputExtractor(o); }
void removeUserOutputExtractor(int i) { implRemoveUserOutputExtractor(i); }
bool hasUserOutputExtractor(OutputExtractor const& o) { return implHasUserOutputExtractor(o); }
bool removeUserOutputExtractor(OutputExtractor const& o) { return implRemoveUserOutputExtractor(o); }
bool overwriteOrAddNewUserOutputExtractor(OutputExtractor const& old, OutputExtractor const& newer) { return implOverwriteOrAddNewUserOutputExtractor(old, newer); }
std::optional<std::filesystem::path> tryPromptToSaveOutputsAsCSV(std::span<OutputExtractor const>) const;
bool hasUserOutputExtractor(const OutputExtractor& o) { return implHasUserOutputExtractor(o); }
bool removeUserOutputExtractor(const OutputExtractor& o) { return implRemoveUserOutputExtractor(o); }
bool overwriteOrAddNewUserOutputExtractor(const OutputExtractor& old, const OutputExtractor& newer) { return implOverwriteOrAddNewUserOutputExtractor(old, newer); }
std::optional<std::filesystem::path> tryPromptToSaveOutputsAsCSV(std::span<const OutputExtractor>) const;
std::optional<std::filesystem::path> tryPromptToSaveOutputsAsCSV(std::initializer_list<OutputExtractor> il) const
{
return tryPromptToSaveOutputsAsCSV(std::span<OutputExtractor const>{il});
return tryPromptToSaveOutputsAsCSV(std::span<const OutputExtractor>{il});
}
std::optional<std::filesystem::path> tryPromptToSaveAllOutputsAsCSV() const;

SimulationModelStatePair* tryGetCurrentSimulationState() { return implTryGetCurrentSimulationState(); }

private:
virtual ISimulation const& implGetSimulation() const = 0;
virtual const ISimulation& implGetSimulation() const = 0;
virtual ISimulation& implUpdSimulation() = 0;

virtual SimulationUIPlaybackState implGetSimulationPlaybackState() = 0;
Expand All @@ -86,12 +86,12 @@ namespace osc
virtual std::optional<SimulationReport> implTrySelectReportBasedOnScrubbing() = 0;

virtual int implGetNumUserOutputExtractors() const = 0;
virtual OutputExtractor const& implGetUserOutputExtractor(int) const = 0;
virtual void implAddUserOutputExtractor(OutputExtractor const&) = 0;
virtual const OutputExtractor& implGetUserOutputExtractor(int) const = 0;
virtual void implAddUserOutputExtractor(const OutputExtractor&) = 0;
virtual void implRemoveUserOutputExtractor(int) = 0;
virtual bool implHasUserOutputExtractor(OutputExtractor const&) const = 0;
virtual bool implRemoveUserOutputExtractor(OutputExtractor const&) = 0;
virtual bool implOverwriteOrAddNewUserOutputExtractor(OutputExtractor const&, OutputExtractor const&) = 0;
virtual bool implHasUserOutputExtractor(const OutputExtractor&) const = 0;
virtual bool implRemoveUserOutputExtractor(const OutputExtractor&) = 0;
virtual bool implOverwriteOrAddNewUserOutputExtractor(const OutputExtractor&, const OutputExtractor&) = 0;

virtual SimulationModelStatePair* implTryGetCurrentSimulationState() = 0;
};
Expand Down
19 changes: 7 additions & 12 deletions src/OpenSimCreator/UI/Simulation/ModelStatePairContextMenu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class osc::ModelStatePairContextMenu::Impl final : public StandardPopup {
Impl(
std::string_view panelName_,
std::shared_ptr<IModelStatePair> model_,
ParentPtr<IMainUIStateAPI> const& api_,
const ParentPtr<IMainUIStateAPI>& api_,
std::optional<std::string> maybeComponentAbsPath_) :

StandardPopup{panelName_, {10.0f, 10.0f}, ImGuiWindowFlags_NoMove},
Expand All @@ -36,16 +36,13 @@ class osc::ModelStatePairContextMenu::Impl final : public StandardPopup {

void impl_draw_content() override
{
if (!m_MaybeComponentAbsPath)
{
if (!m_MaybeComponentAbsPath) {
drawRightClickedNothingContextMenu();
}
else if (OpenSim::Component const* c = FindComponent(m_Model->getModel(), *m_MaybeComponentAbsPath))
{
else if (const OpenSim::Component* c = FindComponent(m_Model->getModel(), *m_MaybeComponentAbsPath)) {
drawRightClickedSomethingContextMenu(*c);
}
else
{
else {
drawRightClickedNothingContextMenu();
}
}
Expand All @@ -55,7 +52,7 @@ class osc::ModelStatePairContextMenu::Impl final : public StandardPopup {
ui::draw_text_disabled("(clicked nothing)");
}

void drawRightClickedSomethingContextMenu(OpenSim::Component const& c)
void drawRightClickedSomethingContextMenu(const OpenSim::Component& c)
{
// draw context menu for whatever's selected
ui::draw_text_unformatted(c.getName());
Expand All @@ -65,7 +62,7 @@ class osc::ModelStatePairContextMenu::Impl final : public StandardPopup {
ui::draw_dummy({0.0f, 3.0f});

DrawSelectOwnerMenu(*m_Model, c);
DrawWatchOutputMenu(c, [this](OpenSim::AbstractOutput const& output, std::optional<ComponentOutputSubfield> subfield)
DrawWatchOutputMenu(c, [this](const OpenSim::AbstractOutput& output, std::optional<ComponentOutputSubfield> subfield)
{
if (subfield) {
m_API->addUserOutputExtractor(OutputExtractor{ComponentOutputExtractor{output, *subfield}});
Expand All @@ -90,12 +87,10 @@ class osc::ModelStatePairContextMenu::Impl final : public StandardPopup {
};


// public API

osc::ModelStatePairContextMenu::ModelStatePairContextMenu(
std::string_view panelName_,
std::shared_ptr<IModelStatePair> model_,
ParentPtr<IMainUIStateAPI> const& api_,
const ParentPtr<IMainUIStateAPI>& api_,
std::optional<std::string> maybeComponentAbsPath_) :

m_Impl{std::make_unique<Impl>(panelName_, std::move(model_), api_, std::move(maybeComponentAbsPath_))}
Expand Down
6 changes: 3 additions & 3 deletions src/OpenSimCreator/UI/Simulation/ModelStatePairContextMenu.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ namespace osc
ModelStatePairContextMenu(
std::string_view panelName,
std::shared_ptr<IModelStatePair>,
ParentPtr<IMainUIStateAPI> const&,
const ParentPtr<IMainUIStateAPI>&,
std::optional<std::string> maybeComponentAbsPath
);
ModelStatePairContextMenu(ModelStatePairContextMenu const&) = delete;
ModelStatePairContextMenu(const ModelStatePairContextMenu&) = delete;
ModelStatePairContextMenu(ModelStatePairContextMenu&&) noexcept;
ModelStatePairContextMenu& operator=(ModelStatePairContextMenu const&) = delete;
ModelStatePairContextMenu& operator=(const ModelStatePairContextMenu&) = delete;
ModelStatePairContextMenu& operator=(ModelStatePairContextMenu&&) noexcept;
~ModelStatePairContextMenu() noexcept;

Expand Down
Loading

0 comments on commit 8baee94

Please sign in to comment.