Skip to content

Commit

Permalink
Refactor MeshWarpingTabSharedState to use getters etc. (#892)
Browse files Browse the repository at this point in the history
  • Loading branch information
adamkewley committed Jun 25, 2024
1 parent e46c97e commit 777ad37
Show file tree
Hide file tree
Showing 9 changed files with 199 additions and 139 deletions.
21 changes: 9 additions & 12 deletions src/OpenSimCreator/UI/MeshWarper/MeshWarpingTab.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <OpenSimCreator/UI/MeshWarper/MeshWarpingTabToolbar.h>

#include <SDL_events.h>
#include <oscar/Platform/App.h>
#include <oscar/UI/Panels/LogViewerPanel.h>
#include <oscar/UI/Panels/PanelManager.h>
#include <oscar/UI/Panels/PerfPanel.h>
Expand Down Expand Up @@ -57,7 +58,7 @@ class osc::MeshWarpingTab::Impl final {
"History",
[state = m_Shared](std::string_view panelName)
{
return std::make_shared<UndoRedoPanel>(panelName, state->editedDocument);
return std::make_shared<UndoRedoPanel>(panelName, state->getUndoableSharedPtr());
},
ToggleablePanelFlags::Default - ToggleablePanelFlags::IsEnabledByDefault
);
Expand Down Expand Up @@ -104,31 +105,29 @@ class osc::MeshWarpingTab::Impl final {
{
App::upd().make_main_loop_waiting();
m_PanelManager->on_mount();
m_Shared->popupManager.on_mount();
m_Shared->on_mount();
}

void on_unmount()
{
m_Shared->on_unmount();
m_PanelManager->on_unmount();
App::upd().make_main_loop_polling();
}

bool onEvent(const SDL_Event& e)
{
if (e.type == SDL_KEYDOWN)
{
if (e.type == SDL_KEYDOWN) {
return onKeydownEvent(e.key);
}
else
{
} else {
return false;
}
}

void on_tick()
{
// re-perform hover test each frame
m_Shared->currentHover.reset();
m_Shared->setHover(std::nullopt);

// garbage collect panel data
m_PanelManager->on_tick();
Expand All @@ -146,9 +145,7 @@ class osc::MeshWarpingTab::Impl final {
m_TopToolbar.onDraw();
m_PanelManager->on_draw();
m_StatusBar.onDraw();

// draw active popups over the UI
m_Shared->popupManager.on_draw();
m_Shared->on_draw();
}

private:
Expand Down Expand Up @@ -202,7 +199,7 @@ class osc::MeshWarpingTab::Impl final {
ParentPtr<ITabHost> m_Parent;

// top-level state that all panels can potentially access
std::shared_ptr<MeshWarpingTabSharedState> m_Shared = std::make_shared<MeshWarpingTabSharedState>(m_TabID, m_Parent);
std::shared_ptr<MeshWarpingTabSharedState> m_Shared = std::make_shared<MeshWarpingTabSharedState>(m_TabID, m_Parent, App::singleton<SceneCache>(App::resource_loader()));

// available/active panels that the user can toggle via the `window` menu
std::shared_ptr<PanelManager> m_PanelManager = std::make_shared<PanelManager>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace osc
{
// append decorations that are common to all panels to the given output vector
void AppendCommonDecorations(
const MeshWarpingTabSharedState& sharedState,
MeshWarpingTabSharedState& sharedState,
const Mesh& tpsSourceOrDestinationMesh,
bool wireframeMode,
const std::function<void(SceneDecoration&&)>& out,
Expand All @@ -30,13 +30,13 @@ namespace osc
{
out({
.mesh = tpsSourceOrDestinationMesh,
.material = sharedState.wireframe_material,
.material = sharedState.wireframe_material(),
});
}

// add grid decorations
draw_xz_grid(*sharedState.meshCache, out);
draw_xz_floor_lines(*sharedState.meshCache, out, 100.0f);
draw_xz_grid(sharedState.updSceneCache(), out);
draw_xz_floor_lines(sharedState.updSceneCache(), out, 100.0f);
}

// returns the amount by which non-participating landmarks should be scaled w.r.t. pariticpating ones
Expand Down
49 changes: 18 additions & 31 deletions src/OpenSimCreator/UI/MeshWarper/MeshWarpingTabInputMeshPanel.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,11 @@ namespace osc
// state update: tell central state if something's being hovered in this panel
if (landmarkCollision)
{
m_State->currentHover = landmarkCollision;
m_State->setHover(landmarkCollision);
}
else if (meshCollision)
{
m_State->currentHover.emplace(m_DocumentIdentifier, meshCollision->position);
m_State->setHover(m_DocumentIdentifier, meshCollision->position);
}

// update camera: NOTE: make sure it's updated *before* rendering; otherwise, it'll be one frame late
Expand All @@ -114,26 +114,13 @@ namespace osc
void updateCamera()
{
// if the cameras are linked together, ensure this camera is updated from the linked camera
if (m_State->linkCameras && m_Camera != m_State->linkedCameraBase)
{
if (m_State->onlyLinkRotation)
{
m_Camera.phi = m_State->linkedCameraBase.phi;
m_Camera.theta = m_State->linkedCameraBase.theta;
}
else
{
m_Camera = m_State->linkedCameraBase;
}
}
m_State->updateOneCameraFromLinkedBase(m_Camera);

// if the user interacts with the render, update the camera as necessary
if (m_LastTextureHittestResult.is_hovered)
{
if (ui::update_polar_camera_from_mouse_inputs(m_Camera, dimensions_of(m_LastTextureHittestResult.item_screen_rect)))
{
m_State->linkedCameraBase = m_Camera; // reflects latest modification
}
if (m_LastTextureHittestResult.is_hovered and
ui::update_polar_camera_from_mouse_inputs(m_Camera, dimensions_of(m_LastTextureHittestResult.item_screen_rect))) {

m_State->setLinkedBaseCamera(m_Camera);
}
}

Expand Down Expand Up @@ -287,9 +274,9 @@ namespace osc

SceneDecoration decoration
{
.mesh = m_State->landmarkSphere,
.mesh = m_State->getLandmarkSphereMesh(),
.transform = {.scale = Vec3{m_LandmarkRadius}, .position = *maybeLocation},
.color = IsFullyPaired(landmarkPair) ? m_State->pairedLandmarkColor : m_State->unpairedLandmarkColor,
.color = IsFullyPaired(landmarkPair) ? m_State->getPairedLandmarkColor() : m_State->getUnpairedLandmarkColor(),
};

const TPSDocumentElementID landmarkID{landmarkPair.uid, TPSDocumentElementType::Landmark, m_DocumentIdentifier};
Expand Down Expand Up @@ -324,13 +311,13 @@ namespace osc
{
SceneDecoration decoration
{
.mesh = m_State->landmarkSphere,
.mesh = m_State->getLandmarkSphereMesh(),
.transform =
{
.scale = Vec3{GetNonParticipatingLandmarkScaleFactor()*m_LandmarkRadius},
.position = npl.location,
},
.color = m_State->nonParticipatingLandmarkColor,
.color = m_State->getNonParticipatingLandmarkColor(),
};
const TPSDocumentElementID id{npl.uid, TPSDocumentElementType::NonParticipatingLandmark, m_DocumentIdentifier};
if (m_State->isSelected(id))
Expand All @@ -353,15 +340,15 @@ namespace osc
const bool nonParticipating = isUserPlacingNonParticipatingLandmark();

const Color color = nonParticipating ?
m_State->nonParticipatingLandmarkColor :
m_State->unpairedLandmarkColor;
m_State->getNonParticipatingLandmarkColor() :
m_State->getUnpairedLandmarkColor();

const float radius = nonParticipating ?
GetNonParticipatingLandmarkScaleFactor()*m_LandmarkRadius :
m_LandmarkRadius;

decorationConsumer({
.mesh = m_State->landmarkSphere,
.mesh = m_State->getLandmarkSphereMesh(),
.transform = {.scale = Vec3{radius}, .position = meshCollisionPosition},
.color = color.with_alpha(0.8f), // faded
});
Expand Down Expand Up @@ -417,7 +404,7 @@ namespace osc
{
ActionDeleteSceneElementsByID(
m_State->updUndoable(),
m_State->userSelection.getUnderlyingSet()
m_State->getUnderlyingSelectionSet()
);
m_State->clearSelection();
}
Expand All @@ -428,7 +415,7 @@ namespace osc
// draws 2D ImGui overlays over the scene render
void draw2DOverlayUI(const Rect& renderRect)
{
ui::set_cursor_screen_pos(renderRect.p1 + m_State->overlayPadding);
ui::set_cursor_screen_pos(renderRect.p1 + m_State->getOverlayPadding());

drawInformationIcon();
ui::same_line();
Expand Down Expand Up @@ -574,7 +561,7 @@ namespace osc
if (ui::draw_button(ICON_FA_EXPAND_ARROWS_ALT))
{
auto_focus(m_Camera, m_State->getScratchMesh(m_DocumentIdentifier).bounds(), aspect_ratio_of(m_LastTextureHittestResult.item_screen_rect));
m_State->linkedCameraBase = m_Camera;
m_State->setLinkedBaseCamera(m_Camera);
}
ui::draw_tooltip_if_item_hovered("Autoscale Scene", "Zooms camera to try and fit everything in the scene into the viewer");
}
Expand All @@ -587,7 +574,7 @@ namespace osc
const ImGuiSliderFlags flags = ImGuiSliderFlags_Logarithmic;

const CStringView label = "landmark radius";
ui::set_next_item_width(ui::get_content_region_avail().x - ui::calc_text_size(label).x - ui::get_style_item_inner_spacing().x - m_State->overlayPadding.x);
ui::set_next_item_width(ui::get_content_region_avail().x - ui::calc_text_size(label).x - ui::get_style_item_inner_spacing().x - m_State->getOverlayPadding().x);
ui::draw_float_slider(label, &m_LandmarkRadius, 0.0001f, 100.0f, "%.4f", flags);
}

Expand Down
16 changes: 6 additions & 10 deletions src/OpenSimCreator/UI/MeshWarper/MeshWarpingTabNavigatorPanel.h
Original file line number Diff line number Diff line change
Expand Up @@ -221,23 +221,19 @@ namespace osc
ui::get_panel_draw_list()->AddCircleFilled(
circle.origin,
circle.radius,
ui::to_ImU32(m_State->nonParticipatingLandmarkColor)
ui::to_ImU32(m_State->getNonParticipatingLandmarkColor())
);

tryDrawCircleHighlight(circle, isSelected, isHovered);
}

Color landmarkDotColor(bool hasLocation, bool isPaired) const
{
if (hasLocation)
{
if (isPaired)
{
return m_State->pairedLandmarkColor;
}
else
{
return m_State->unpairedLandmarkColor;
if (hasLocation) {
if (isPaired) {
return m_State->getPairedLandmarkColor();
} else {
return m_State->getUnpairedLandmarkColor();
}
}
return Color::half_grey();
Expand Down
27 changes: 8 additions & 19 deletions src/OpenSimCreator/UI/MeshWarper/MeshWarpingTabResultMeshPanel.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,25 +61,14 @@ namespace osc
void updateCamera()
{
// if cameras are linked together, ensure all cameras match the "base" camera
if (m_State->linkCameras && m_Camera != m_State->linkedCameraBase)
{
if (m_State->onlyLinkRotation)
{
m_Camera.phi = m_State->linkedCameraBase.phi;
m_Camera.theta = m_State->linkedCameraBase.theta;
}
else
{
m_Camera = m_State->linkedCameraBase;
}
}
m_State->updateOneCameraFromLinkedBase(m_Camera);

// update camera if user drags it around etc.
if (m_LastTextureHittestResult.is_hovered)
{
if (ui::update_polar_camera_from_mouse_inputs(m_Camera, dimensions_of(m_LastTextureHittestResult.item_screen_rect)))
{
m_State->linkedCameraBase = m_Camera; // reflects latest modification
m_State->setLinkedBaseCamera(m_Camera); // reflects latest modification
}
}
}
Expand Down Expand Up @@ -162,11 +151,11 @@ namespace osc
}
if (ui::draw_menu_item("Warped Non-Participating Landmarks to CSV"))
{
ActionSaveWarpedNonParticipatingLandmarksToCSV(m_State->getScratch(), m_State->meshResultCache);
ActionSaveWarpedNonParticipatingLandmarksToCSV(m_State->getScratch(), m_State->updResultCache());
}
if (ui::draw_menu_item("Warped Non-Participating Landmark Positions to CSV"))
{
ActionSaveWarpedNonParticipatingLandmarksToCSV(m_State->getScratch(), m_State->meshResultCache, LandmarkCSVFlags::NoHeader | LandmarkCSVFlags::NoNames);
ActionSaveWarpedNonParticipatingLandmarksToCSV(m_State->getScratch(), m_State->updResultCache(), LandmarkCSVFlags::NoHeader | LandmarkCSVFlags::NoNames);
}
if (ui::draw_menu_item("Landmark Pairs to CSV"))
{
Expand All @@ -190,7 +179,7 @@ namespace osc
m_State->getResultMesh().bounds(),
aspect_ratio_of(m_LastTextureHittestResult.item_screen_rect)
);
m_State->linkedCameraBase = m_Camera;
m_State->setLinkedBaseCamera(m_Camera);
}
ui::draw_tooltip_if_item_hovered(
"Autoscale Scene",
Expand All @@ -206,7 +195,7 @@ namespace osc
const ImGuiSliderFlags flags = ImGuiSliderFlags_Logarithmic;

const CStringView label = "landmark radius";
ui::set_next_item_width(ui::get_content_region_avail().x - ui::calc_text_size(label).x - ui::get_style_item_inner_spacing().x - m_State->overlayPadding.x);
ui::set_next_item_width(ui::get_content_region_avail().x - ui::calc_text_size(label).x - ui::get_style_item_inner_spacing().x - m_State->getOverlayPadding().x);
ui::draw_float_slider(label, &m_LandmarkRadius, 0.0001f, 100.0f, "%.4f", flags);
}

Expand Down Expand Up @@ -254,12 +243,12 @@ namespace osc
for (const Vec3& nonParticipatingLandmarkPos : m_State->getResultNonParticipatingLandmarkLocations())
{
decorationConsumer({
.mesh = m_State->landmarkSphere,
.mesh = m_State->getLandmarkSphereMesh(),
.transform = {
.scale = Vec3{GetNonParticipatingLandmarkScaleFactor()*m_LandmarkRadius},
.position = nonParticipatingLandmarkPos,
},
.color = m_State->nonParticipatingLandmarkColor,
.color = m_State->getNonParticipatingLandmarkColor(),
});
}

Expand Down
Loading

0 comments on commit 777ad37

Please sign in to comment.