Skip to content

Commit

Permalink
Make minor refactors to OpenSimCreator/UI
Browse files Browse the repository at this point in the history
  • Loading branch information
adamkewley committed Jun 18, 2024
1 parent 8baee94 commit b4e413a
Show file tree
Hide file tree
Showing 10 changed files with 65 additions and 76 deletions.
52 changes: 18 additions & 34 deletions src/OpenSimCreator/UI/Shared/ObjectPropertiesEditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,20 +56,6 @@ namespace
// helper functions
namespace
{
// unpack a SimTK::Vec6 into an array
std::array<float, 6> ToArray(SimTK::Vec6 const& v)
{
return
{
static_cast<float>(v[0]),
static_cast<float>(v[1]),
static_cast<float>(v[2]),
static_cast<float>(v[3]),
static_cast<float>(v[4]),
static_cast<float>(v[5]),
};
}

// returns an updater function that deletes an element from a list property
template<typename T>
std::function<void(OpenSim::AbstractProperty&)> MakePropElementDeleter(int idx)
Expand Down Expand Up @@ -115,31 +101,29 @@ namespace
}

// draws the property name and (optionally) comment tooltip
void DrawPropertyName(OpenSim::AbstractProperty const& prop)
void DrawPropertyName(const OpenSim::AbstractProperty& prop)
{
ui::draw_text_unformatted(prop.getName());

if (!prop.getComment().empty())
{
if (!prop.getComment().empty()) {
ui::same_line();
ui::draw_help_marker(prop.getComment());
}
}

// wraps an object accessor with property information so that an individual
// property accesssor with the same lifetime semantics as the object can exist
std::function<OpenSim::AbstractProperty const*()> MakePropertyAccessor(
std::function<OpenSim::Object const*()> const& objAccessor,
std::string const& propertyName)
std::function<const OpenSim::AbstractProperty*()> MakePropertyAccessor(
const std::function<const OpenSim::Object*()>& objAccessor,
const std::string& propertyName)
{
return [objAccessor, propertyName]() -> OpenSim::AbstractProperty const*
return [objAccessor, propertyName]() -> const OpenSim::AbstractProperty*
{
OpenSim::Object const* maybeObj = objAccessor();
if (!maybeObj)
{
const OpenSim::Object* maybeObj = objAccessor();
if (!maybeObj) {
return nullptr;
}
OpenSim::Object const& obj = *maybeObj;
const OpenSim::Object& obj = *maybeObj;

if (!obj.hasProperty(propertyName))
{
Expand All @@ -159,12 +143,12 @@ namespace

// draws a little vertical line, which is usually used to visually indicate
// x/y/z to the user
void DrawColoredDimensionHintVerticalLine(Color const& color)
void DrawColoredDimensionHintVerticalLine(const Color& color)
{
ImDrawList* const l = ui::get_panel_draw_list();
Vec2 const p = ui::get_cursor_screen_pos();
float const h = ui::get_text_line_height() + 2.0f*ui::get_style_frame_padding().y + 2.0f*ui::get_style_frame_border_size();
Vec2 const dims = Vec2{4.0f, h};
const Vec2 p = ui::get_cursor_screen_pos();
const float h = ui::get_text_line_height() + 2.0f*ui::get_style_frame_padding().y + 2.0f*ui::get_style_frame_border_size();
const Vec2 dims = Vec2{4.0f, h};
l->AddRectFilled(p, p + dims, ui::to_ImU32(color));
ui::set_cursor_screen_pos({p.x + 4.0f, p.y});
}
Expand Down Expand Up @@ -338,7 +322,7 @@ namespace
}

std::string GenerateVecFrameAnnotationLabel(
OpenSim::AbstractProperty const& backingProperty,
const OpenSim::AbstractProperty& backingProperty,
Vec3::size_type ithDimension)
{
std::stringstream ss;
Expand All @@ -357,14 +341,14 @@ namespace
class IPropertyEditor {
protected:
IPropertyEditor() = default;
IPropertyEditor(IPropertyEditor const&) = default;
IPropertyEditor(const IPropertyEditor&) = default;
IPropertyEditor(IPropertyEditor&&) noexcept = default;
IPropertyEditor& operator=(IPropertyEditor const&) = default;
IPropertyEditor& operator=(const IPropertyEditor&) = default;
IPropertyEditor& operator=(IPropertyEditor&&) noexcept = default;
public:
virtual ~IPropertyEditor() noexcept = default;

bool isCompatibleWith(OpenSim::AbstractProperty const& prop) const
bool isCompatibleWith(const OpenSim::AbstractProperty& prop) const
{
return implIsCompatibleWith(prop);
}
Expand All @@ -375,7 +359,7 @@ namespace
}

private:
virtual bool implIsCompatibleWith(OpenSim::AbstractProperty const&) const = 0;
virtual bool implIsCompatibleWith(const OpenSim::AbstractProperty&) const = 0;
virtual std::optional<std::function<void(OpenSim::AbstractProperty&)>> implOnDraw() = 0;
};

Expand Down
8 changes: 4 additions & 4 deletions src/OpenSimCreator/UI/Shared/ObjectPropertiesEditor.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ namespace osc
public:
ObjectPropertiesEditor(
IPopupAPI*,
std::shared_ptr<UndoableModelStatePair const> targetModel,
std::function<OpenSim::Object const*()> objectGetter
std::shared_ptr<const UndoableModelStatePair> targetModel,
std::function<const OpenSim::Object*()> objectGetter
);
ObjectPropertiesEditor(ObjectPropertiesEditor const&) = delete;
ObjectPropertiesEditor(const ObjectPropertiesEditor&) = delete;
ObjectPropertiesEditor(ObjectPropertiesEditor&&) noexcept;
ObjectPropertiesEditor& operator=(ObjectPropertiesEditor const&) = delete;
ObjectPropertiesEditor& operator=(const ObjectPropertiesEditor&) = delete;
ObjectPropertiesEditor& operator=(ObjectPropertiesEditor&&) noexcept;
~ObjectPropertiesEditor() noexcept;

Expand Down
4 changes: 1 addition & 3 deletions src/OpenSimCreator/UI/Shared/ParamBlockEditorPopup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,7 @@ class osc::ParamBlockEditorPopup::Impl final : public StandardPopup {

osc::ParamBlockEditorPopup::ParamBlockEditorPopup(std::string_view popupName, ParamBlock* paramBlock) :
m_Impl{std::make_unique<Impl>(popupName, paramBlock)}
{
}

{}
osc::ParamBlockEditorPopup::ParamBlockEditorPopup(ParamBlockEditorPopup&&) noexcept = default;
osc::ParamBlockEditorPopup& osc::ParamBlockEditorPopup::operator=(ParamBlockEditorPopup&&) noexcept = default;
osc::ParamBlockEditorPopup::~ParamBlockEditorPopup() noexcept = default;
Expand Down
4 changes: 2 additions & 2 deletions src/OpenSimCreator/UI/Shared/ParamBlockEditorPopup.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ namespace osc
std::string_view popupName,
ParamBlock*
);
ParamBlockEditorPopup(ParamBlockEditorPopup const&) = delete;
ParamBlockEditorPopup(const ParamBlockEditorPopup&) = delete;
ParamBlockEditorPopup(ParamBlockEditorPopup&&) noexcept;
ParamBlockEditorPopup& operator=(ParamBlockEditorPopup const&) = delete;
ParamBlockEditorPopup& operator=(const ParamBlockEditorPopup&) = delete;
ParamBlockEditorPopup& operator=(ParamBlockEditorPopup&&) noexcept;
~ParamBlockEditorPopup() noexcept;

Expand Down
26 changes: 9 additions & 17 deletions src/OpenSimCreator/UI/Shared/PropertiesPanel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,10 @@ using namespace osc;

namespace
{
void DrawActionsMenu(IEditorAPI* editorAPI, std::shared_ptr<UndoableModelStatePair> const& model)
void DrawActionsMenu(IEditorAPI* editorAPI, const std::shared_ptr<UndoableModelStatePair>& model)
{
OpenSim::Component const* selection = model->getSelected();
if (!selection)
{
const OpenSim::Component* const selection = model->getSelected();
if (!selection) {
return;
}

Expand Down Expand Up @@ -57,10 +56,8 @@ namespace

void onDraw()
{
OpenSim::Component const* const selected = m_Model->getSelected();

if (!selected)
{
const OpenSim::Component* const selected = m_Model->getSelected();
if (!selected) {
return; // don't do anything if nothing is selected
}

Expand Down Expand Up @@ -95,7 +92,7 @@ namespace
private:
std::shared_ptr<UndoableModelStatePair> m_Model;
UID m_LastModelVersion;
OpenSim::Component const* m_LastSelected = nullptr;
const OpenSim::Component* m_LastSelected = nullptr;
std::string m_EditedName;
};
}
Expand All @@ -111,8 +108,7 @@ class osc::PropertiesPanel::Impl final : public StandardPanelImpl {
m_EditorAPI{editorAPI},
m_Model{std::move(model)},
m_SelectionPropertiesEditor{editorAPI, m_Model, [model = m_Model](){ return model->getSelected(); }}
{
}
{}

private:
void impl_draw_content() final
Expand All @@ -124,7 +120,7 @@ class osc::PropertiesPanel::Impl final : public StandardPanelImpl {
}

ui::push_id(m_Model->getSelected());
ScopeGuard const g{[]() { ui::pop_id(); }};
const ScopeGuard g{[]() { ui::pop_id(); }};

// draw an actions row with a button that opens the context menu
//
Expand Down Expand Up @@ -155,16 +151,12 @@ class osc::PropertiesPanel::Impl final : public StandardPanelImpl {
};


// public API (PIMPL)

osc::PropertiesPanel::PropertiesPanel(
std::string_view panelName,
IEditorAPI* editorAPI,
std::shared_ptr<UndoableModelStatePair> model) :
m_Impl{std::make_unique<Impl>(panelName, editorAPI, std::move(model))}
{
}

{}
osc::PropertiesPanel::PropertiesPanel(PropertiesPanel&&) noexcept = default;
osc::PropertiesPanel& osc::PropertiesPanel::operator=(PropertiesPanel&&) noexcept = default;
osc::PropertiesPanel::~PropertiesPanel() noexcept = default;
Expand Down
4 changes: 2 additions & 2 deletions src/OpenSimCreator/UI/Shared/PropertiesPanel.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ namespace osc
IEditorAPI*,
std::shared_ptr<UndoableModelStatePair>
);
PropertiesPanel(PropertiesPanel const&) = delete;
PropertiesPanel(const PropertiesPanel&) = delete;
PropertiesPanel(PropertiesPanel&&) noexcept;
PropertiesPanel& operator=(PropertiesPanel const&) = delete;
PropertiesPanel& operator=(const PropertiesPanel&) = delete;
PropertiesPanel& operator=(PropertiesPanel&&) noexcept;
~PropertiesPanel() noexcept;

Expand Down
18 changes: 9 additions & 9 deletions src/OpenSimCreator/UI/Shared/Readonly3DModelViewer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class osc::Readonly3DModelViewer::Impl final {
return m_MaybeLastHittest && m_MaybeLastHittest->is_hovered;
}

std::optional<SceneCollision> onDraw(IConstModelStatePair const& rs)
std::optional<SceneCollision> onDraw(const IConstModelStatePair& rs)
{
// if this is the first frame being rendered, auto-focus the scene
if (!m_MaybeLastHittest)
Expand Down Expand Up @@ -98,7 +98,7 @@ class osc::Readonly3DModelViewer::Impl final {
);

// update current+retained hittest
ui::HittestResult const hittest = ui::hittest_last_drawn_item();
const ui::HittestResult hittest = ui::hittest_last_drawn_item();
m_MaybeLastHittest = hittest;

// if allowed, hittest the scene
Expand All @@ -114,7 +114,7 @@ class osc::Readonly3DModelViewer::Impl final {

// draw 2D ImGui overlays
auto renderParamsBefore = m_Params;
bool const edited = DrawViewerImGuiOverlays(
const bool edited = DrawViewerImGuiOverlays(
m_Params,
m_CachedModelRenderer.getDrawlist(),
m_CachedModelRenderer.bounds(),
Expand All @@ -124,7 +124,7 @@ class osc::Readonly3DModelViewer::Impl final {
);
if (edited)
{
auto const& renderParamsAfter = m_Params;
const auto& renderParamsAfter = m_Params;
SaveModelRendererParamsDifference(
renderParamsBefore,
renderParamsAfter,
Expand Down Expand Up @@ -152,12 +152,12 @@ class osc::Readonly3DModelViewer::Impl final {
std::nullopt;
}

PolarPerspectiveCamera const& getCamera() const
const PolarPerspectiveCamera& getCamera() const
{
return m_Params.camera;
}

void setCamera(PolarPerspectiveCamera const& camera)
void setCamera(const PolarPerspectiveCamera& camera)
{
m_Params.camera = camera;
}
Expand Down Expand Up @@ -230,7 +230,7 @@ bool osc::Readonly3DModelViewer::isMousedOver() const
return m_Impl->isMousedOver();
}

std::optional<SceneCollision> osc::Readonly3DModelViewer::onDraw(IConstModelStatePair const& rs)
std::optional<SceneCollision> osc::Readonly3DModelViewer::onDraw(const IConstModelStatePair& rs)
{
return m_Impl->onDraw(rs);
}
Expand All @@ -240,12 +240,12 @@ std::optional<Rect> osc::Readonly3DModelViewer::getScreenRect() const
return m_Impl->getScreenRect();
}

PolarPerspectiveCamera const& osc::Readonly3DModelViewer::getCamera() const
const PolarPerspectiveCamera& osc::Readonly3DModelViewer::getCamera() const
{
return m_Impl->getCamera();
}

void osc::Readonly3DModelViewer::setCamera(PolarPerspectiveCamera const& camera)
void osc::Readonly3DModelViewer::setCamera(const PolarPerspectiveCamera& camera)
{
m_Impl->setCamera(camera);
}
10 changes: 5 additions & 5 deletions src/OpenSimCreator/UI/Shared/Readonly3DModelViewer.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,19 @@ namespace osc
std::string_view parentPanelName_,
Readonly3DModelViewerFlags = Readonly3DModelViewerFlags::None
);
Readonly3DModelViewer(Readonly3DModelViewer const&) = delete;
Readonly3DModelViewer(const Readonly3DModelViewer&) = delete;
Readonly3DModelViewer(Readonly3DModelViewer&&) noexcept;
Readonly3DModelViewer& operator=(Readonly3DModelViewer const&) = delete;
Readonly3DModelViewer& operator=(const Readonly3DModelViewer&) = delete;
Readonly3DModelViewer& operator=(Readonly3DModelViewer&&) noexcept;
~Readonly3DModelViewer() noexcept;

bool isMousedOver() const;
bool isLeftClicked() const;
bool isRightClicked() const;
std::optional<SceneCollision> onDraw(IConstModelStatePair const&);
std::optional<SceneCollision> onDraw(const IConstModelStatePair&);
std::optional<Rect> getScreenRect() const;
PolarPerspectiveCamera const& getCamera() const;
void setCamera(PolarPerspectiveCamera const&);
const PolarPerspectiveCamera& getCamera() const;
void setCamera(const PolarPerspectiveCamera&);

private:
class Impl;
Expand Down
12 changes: 12 additions & 0 deletions src/OpenSimCreator/Utils/SimTKHelpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,18 @@ Quat osc::ToQuat(const SimTK::Rotation& r)
};
}

std::array<float, 6> osc::ToArray(const SimTK::Vec6& v)
{
return {
static_cast<float>(v[0]),
static_cast<float>(v[1]),
static_cast<float>(v[2]),
static_cast<float>(v[3]),
static_cast<float>(v[4]),
static_cast<float>(v[5]),
};
}

Transform osc::decompose_to_transform(const SimTK::Transform& t)
{
return Transform{.rotation = ToQuat(t.R()), .position = ToVec3(t.p())};
Expand Down
3 changes: 3 additions & 0 deletions src/OpenSimCreator/Utils/SimTKHelpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
#include <oscar/Maths/Vec3.h>
#include <oscar/Maths/Vec4.h>

#include <array>

namespace osc { struct Color; }
namespace osc { struct Transform; }

Expand All @@ -33,5 +35,6 @@ namespace osc
Mat3 ToMat3(const SimTK::Mat33&);
Mat4 mat4_cast(const SimTK::Rotation&);
Quat ToQuat(const SimTK::Rotation&);
std::array<float, 6> ToArray(const SimTK::Vec6&);
Transform decompose_to_transform(const SimTK::Transform&);
}

0 comments on commit b4e413a

Please sign in to comment.