Skip to content

Commit

Permalink
Refactor more controls to the Widget API
Browse files Browse the repository at this point in the history
  • Loading branch information
adamkewley committed Feb 13, 2025
1 parent 64a438a commit cc86688
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 41 deletions.
4 changes: 2 additions & 2 deletions libopensimcreator/UI/FrameDefinition/FrameDefinitionTab.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -954,7 +954,7 @@ class osc::FrameDefinitionTab::Impl final : public TabPrivate {

explicit Impl(FrameDefinitionTab& owner, Widget& parent_) :
TabPrivate{owner, &parent_, c_TabStringID},
m_Toolbar{"##FrameDefinitionToolbar", parent_, m_Model}
m_Toolbar{&parent_, "##FrameDefinitionToolbar", m_Model}
{
m_PanelManager->register_toggleable_panel(
"Navigator",
Expand Down Expand Up @@ -1080,7 +1080,7 @@ class osc::FrameDefinitionTab::Impl final : public TabPrivate {
{
ui::enable_dockspace_over_main_viewport();

m_Toolbar.onDraw();
m_Toolbar.on_draw();
m_PanelManager->on_draw();
m_PopupManager.on_draw();
}
Expand Down
19 changes: 11 additions & 8 deletions libopensimcreator/UI/FrameDefinition/FrameDefinitionTabToolbar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,19 @@
#include <utility>

osc::FrameDefinitionTabToolbar::FrameDefinitionTabToolbar(
std::string_view label_,
Widget& tabHost_,
Widget* parent,
std::string_view name,
std::shared_ptr<UndoableModelStatePair> model_) :

m_Label{label_},
m_Parent{tabHost_.weak_ref()},
Widget{parent},
m_Model{std::move(model_)}
{}
{
set_name(name);
}

void osc::FrameDefinitionTabToolbar::onDraw()
void osc::FrameDefinitionTabToolbar::impl_on_draw()
{
if (BeginToolbar(m_Label, Vec2{5.0f, 5.0f})) {
if (BeginToolbar(name(), Vec2{5.0f, 5.0f})) {
drawContent();
}
ui::end_panel();
Expand All @@ -50,7 +51,9 @@ void osc::FrameDefinitionTabToolbar::drawExportToOpenSimButton()
ui::begin_disabled();
}
if (ui::draw_button(OSC_ICON_FILE_EXPORT " Export to OpenSim")) {
fd::ActionExportFrameDefinitionSceneModelToEditorTab(*m_Parent, *m_Model);
if (parent()) {
fd::ActionExportFrameDefinitionSceneModelToEditorTab(*parent(), *m_Model);
}
}
if (numBodies == 0) {
ui::end_disabled();
Expand Down
18 changes: 8 additions & 10 deletions libopensimcreator/UI/FrameDefinition/FrameDefinitionTabToolbar.h
Original file line number Diff line number Diff line change
@@ -1,32 +1,30 @@
#pragma once

#include <libopensimcreator/Documents/Model/UndoableModelStatePair.h>

#include <liboscar/Platform/Widget.h>
#include <liboscar/Utils/LifetimedPtr.h>

#include <cstddef>
#include <memory>
#include <string_view>

namespace osc { class UndoableModelStatePair; }

namespace osc
{
class FrameDefinitionTabToolbar final {
class FrameDefinitionTabToolbar final : public Widget {
public:
FrameDefinitionTabToolbar(
std::string_view,
Widget&,
explicit FrameDefinitionTabToolbar(
Widget* parent,
std::string_view name,
std::shared_ptr<UndoableModelStatePair>
);

void onDraw();
private:
void impl_on_draw() final;

void drawContent();
void drawExportToOpenSimButton();
void drawExportToOpenSimTooltipContent(size_t);

std::string m_Label;
LifetimedPtr<Widget> m_Parent;
std::shared_ptr<UndoableModelStatePair> m_Model;
};
}
25 changes: 12 additions & 13 deletions libopensimcreator/UI/Shared/ObjectPropertiesEditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include <liboscar/Platform/IconCodepoints.h>
#include <liboscar/Platform/Log.h>
#include <liboscar/Platform/Widget.h>
#include <liboscar/Platform/WidgetPrivate.h>
#include <liboscar/UI/Events/OpenPanelEvent.h>
#include <liboscar/UI/Events/OpenPopupEvent.h>
#include <liboscar/UI/oscimgui.h>
Expand Down Expand Up @@ -1655,14 +1656,15 @@ namespace
}

// top-level implementation of the properties editor
class osc::ObjectPropertiesEditor::Impl final {
class osc::ObjectPropertiesEditor::Impl final : public WidgetPrivate {
public:
Impl(
Widget* parentWidget,
explicit Impl(
Widget& owner,
Widget* parent,
std::shared_ptr<const IVersionedComponentAccessor> targetComponent,
std::function<const OpenSim::Object*()> objectGetter) :

m_ParentWidget{parentWidget ? parentWidget->weak_ref() : nullptr},
WidgetPrivate{owner, parent},
m_TargetComponent{std::move(targetComponent)},
m_ObjectGetter{std::move(objectGetter)}
{}
Expand Down Expand Up @@ -1782,7 +1784,7 @@ class osc::ObjectPropertiesEditor::Impl final {
// need to create a new editor because either it hasn't been made yet or the existing
// editor is for a different type
it->second = c_Registry.tryCreateEditor({
.parent = m_ParentWidget,
.parent = parent()->weak_ref(),
.component = m_TargetComponent,
.objectAccessor = m_ObjectGetter,
.propertyAccessor = MakePropertyAccessor(m_ObjectGetter, prop.getName()),
Expand All @@ -1792,7 +1794,6 @@ class osc::ObjectPropertiesEditor::Impl final {
return it->second.get();
}

LifetimedPtr<Widget> m_ParentWidget;
std::shared_ptr<const IVersionedComponentAccessor> m_TargetComponent;
std::function<const OpenSim::Object*()> m_ObjectGetter;
std::unordered_set<std::string> m_Blacklist;
Expand All @@ -1802,14 +1803,12 @@ class osc::ObjectPropertiesEditor::Impl final {


osc::ObjectPropertiesEditor::ObjectPropertiesEditor(
Widget* parentWidget,
Widget* parent,
std::shared_ptr<const IVersionedComponentAccessor> targetComponent,
std::function<const OpenSim::Object*()> objectGetter) :

m_Impl{std::make_unique<Impl>(parentWidget, std::move(targetComponent), std::move(objectGetter))}
Widget{std::make_unique<Impl>(*this, parent, std::move(targetComponent), std::move(objectGetter))}
{}
osc::ObjectPropertiesEditor::ObjectPropertiesEditor(ObjectPropertiesEditor&&) noexcept = default;
osc::ObjectPropertiesEditor& osc::ObjectPropertiesEditor::operator=(ObjectPropertiesEditor&&) noexcept = default;
osc::ObjectPropertiesEditor::~ObjectPropertiesEditor() noexcept = default;
void osc::ObjectPropertiesEditor::insertInBlacklist(std::string_view propertyName) { m_Impl->insertInBlacklist(propertyName); }
std::optional<ObjectPropertyEdit> osc::ObjectPropertiesEditor::onDraw() { return m_Impl->onDraw(); }
void osc::ObjectPropertiesEditor::insertInBlacklist(std::string_view propertyName) { private_data().insertInBlacklist(propertyName); }
std::optional<ObjectPropertyEdit> osc::ObjectPropertiesEditor::onDraw() { return private_data().onDraw(); }
void osc::ObjectPropertiesEditor::impl_on_draw() { private_data().onDraw(); }
15 changes: 7 additions & 8 deletions libopensimcreator/UI/Shared/ObjectPropertiesEditor.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

#include <libopensimcreator/Documents/Model/ObjectPropertyEdit.h>

#include <liboscar/Platform/Widget.h>

#include <functional>
#include <memory>
#include <optional>
Expand All @@ -12,26 +14,23 @@ namespace osc { class Widget; }

namespace osc
{
class ObjectPropertiesEditor final {
class ObjectPropertiesEditor final : public Widget {
public:
explicit ObjectPropertiesEditor(
Widget* parentWidget,
Widget* parent,
std::shared_ptr<const IVersionedComponentAccessor> targetComponent,
std::function<const OpenSim::Object*()> objectGetter
);
ObjectPropertiesEditor(const ObjectPropertiesEditor&) = delete;
ObjectPropertiesEditor(ObjectPropertiesEditor&&) noexcept;
ObjectPropertiesEditor& operator=(const ObjectPropertiesEditor&) = delete;
ObjectPropertiesEditor& operator=(ObjectPropertiesEditor&&) noexcept;
~ObjectPropertiesEditor() noexcept;

void insertInBlacklist(std::string_view propertyName);

// does not actually apply any property changes - the caller should check+apply the return value
std::optional<ObjectPropertyEdit> onDraw();

private:
void impl_on_draw() final;

class Impl;
std::unique_ptr<Impl> m_Impl;
OSC_WIDGET_DATA_GETTERS(Impl);
};
}

0 comments on commit cc86688

Please sign in to comment.