Skip to content

Commit

Permalink
Refactor mesh warp toolbar into separate cpp (#892)
Browse files Browse the repository at this point in the history
  • Loading branch information
adamkewley committed Jun 25, 2024
1 parent 27f411e commit 776e5db
Show file tree
Hide file tree
Showing 3 changed files with 159 additions and 123 deletions.
1 change: 1 addition & 0 deletions src/OpenSimCreator/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,7 @@ add_library(OpenSimCreator STATIC
UI/MeshWarper/MeshWarpingTabResultMeshPanel.h
UI/MeshWarper/MeshWarpingTabSharedState.h
UI/MeshWarper/MeshWarpingTabStatusBar.h
UI/MeshWarper/MeshWarpingTabToolbar.cpp
UI/MeshWarper/MeshWarpingTabToolbar.h
UI/MeshWarper/MeshWarpingTabUserSelection.h

Expand Down
147 changes: 147 additions & 0 deletions src/OpenSimCreator/UI/MeshWarper/MeshWarpingTabToolbar.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
#include "MeshWarpingTabToolbar.h"

#include <OpenSimCreator/Documents/Landmarks/LandmarkCSVFlags.h>
#include <OpenSimCreator/Documents/MeshWarper/UndoableTPSDocumentActions.h>
#include <OpenSimCreator/UI/MeshWarper/MeshWarpingTabSharedState.h>
#include <OpenSimCreator/UI/Shared/BasicWidgets.h>

#include <IconsFontAwesome5.h>
#include <oscar/UI/oscimgui.h>
#include <oscar/UI/ImGuiHelpers.h>
#include <oscar/UI/Widgets/RedoButton.h>
#include <oscar/UI/Widgets/UndoButton.h>

#include <memory>
#include <string>
#include <string_view>
#include <utility>

class osc::MeshWarpingTabToolbar::Impl final {
public:
Impl(
std::string_view label,
std::shared_ptr<MeshWarpingTabSharedState> tabState_) :

m_Label{label},
m_State{std::move(tabState_)}
{}

void onDraw()
{
if (BeginToolbar(m_Label))
{
drawContent();
}
ui::end_panel();
}

private:
void drawContent()
{
// document-related stuff
drawNewDocumentButton();
ui::same_line();
drawOpenDocumentButton();
ui::same_line();
drawSaveLandmarksButton();
ui::same_line();

ui::draw_separator(ImGuiSeparatorFlags_Vertical);
ui::same_line();

// undo/redo-related stuff
m_UndoButton.on_draw();
ui::same_line();
m_RedoButton.onDraw();
ui::same_line();

ui::draw_separator(ImGuiSeparatorFlags_Vertical);
ui::same_line();

// camera stuff
drawCameraLockCheckbox();
ui::same_line();

ui::draw_separator(ImGuiSeparatorFlags_Vertical);
ui::same_line();
}

void drawNewDocumentButton()
{
if (ui::draw_button(ICON_FA_FILE))
{
ActionCreateNewDocument(m_State->updUndoable());
}
ui::draw_tooltip_if_item_hovered(
"Create New Document",
"Creates the default scene (undoable)"
);
}

void drawOpenDocumentButton()
{
ui::draw_button(ICON_FA_FOLDER_OPEN);
if (ui::begin_popup_context_menu("##OpenFolder", ImGuiPopupFlags_MouseButtonLeft))
{
if (ui::draw_menu_item("Load Source Mesh"))
{
ActionLoadMeshFile(m_State->updUndoable(), TPSDocumentInputIdentifier::Source);
}
if (ui::draw_menu_item("Load Destination Mesh"))
{
ActionLoadMeshFile(m_State->updUndoable(), TPSDocumentInputIdentifier::Destination);
}
ui::end_popup();
}
ui::draw_tooltip_if_item_hovered(
"Open File",
"Open Source/Destination data"
);
}

void drawSaveLandmarksButton()
{
if (ui::draw_button(ICON_FA_SAVE))
{
ActionSavePairedLandmarksToCSV(m_State->getScratch(), lm::LandmarkCSVFlags::NoNames);
}
ui::draw_tooltip_if_item_hovered(
"Save Landmarks to CSV (no names)",
"Saves all pair-able landmarks to a CSV file, for external processing\n\n(legacy behavior: does not export names: use 'File' menu if you want the names)"
);
}

void drawCameraLockCheckbox()
{
ui::draw_checkbox("link cameras", &m_State->linkCameras);
ui::same_line();
if (!m_State->linkCameras)
{
ui::begin_disabled();
}
ui::draw_checkbox("only link rotation", &m_State->onlyLinkRotation);
if (!m_State->linkCameras)
{
ui::end_disabled();
}
}

std::string m_Label;
std::shared_ptr<MeshWarpingTabSharedState> m_State;
UndoButton m_UndoButton{m_State->editedDocument};
RedoButton m_RedoButton{m_State->editedDocument};
};

osc::MeshWarpingTabToolbar::MeshWarpingTabToolbar(
std::string_view label,
std::shared_ptr<MeshWarpingTabSharedState> sharedState) :
m_Impl{std::make_unique<Impl>(label, std::move(sharedState))}
{}
osc::MeshWarpingTabToolbar::MeshWarpingTabToolbar(MeshWarpingTabToolbar&&) noexcept = default;
osc::MeshWarpingTabToolbar& osc::MeshWarpingTabToolbar::operator=(MeshWarpingTabToolbar&&) noexcept = default;
osc::MeshWarpingTabToolbar::~MeshWarpingTabToolbar() noexcept = default;

void osc::MeshWarpingTabToolbar::onDraw()
{
m_Impl->onDraw();
}
134 changes: 11 additions & 123 deletions src/OpenSimCreator/UI/MeshWarper/MeshWarpingTabToolbar.h
Original file line number Diff line number Diff line change
@@ -1,23 +1,9 @@
#pragma once

#include <OpenSimCreator/Documents/Landmarks/LandmarkCSVFlags.h>
#include <OpenSimCreator/Documents/MeshWarper/UndoableTPSDocumentActions.h>
#include <OpenSimCreator/UI/MeshWarper/MeshWarpingTabSharedState.h>
#include <OpenSimCreator/UI/Shared/BasicWidgets.h>

#include <IconsFontAwesome5.h>
#include <oscar/UI/ImGuiHelpers.h>
#include <oscar/UI/oscimgui.h>
#include <oscar/UI/oscimgui_internal.h>
#include <oscar/UI/Widgets/RedoButton.h>
#include <oscar/UI/Widgets/UndoButton.h>

#include <memory>
#include <string>
#include <string_view>
#include <utility>

using osc::lm::LandmarkCSVFlags;
namespace osc { struct MeshWarpingTabSharedState; }

namespace osc
{
Expand All @@ -26,116 +12,18 @@ namespace osc
public:
MeshWarpingTabToolbar(
std::string_view label,
std::shared_ptr<MeshWarpingTabSharedState> tabState_) :
std::shared_ptr<MeshWarpingTabSharedState>
);
MeshWarpingTabToolbar(const MeshWarpingTabToolbar&) = delete;
MeshWarpingTabToolbar(MeshWarpingTabToolbar&&) noexcept;
MeshWarpingTabToolbar& operator=(const MeshWarpingTabToolbar&) = delete;
MeshWarpingTabToolbar& operator=(MeshWarpingTabToolbar&&) noexcept;
~MeshWarpingTabToolbar() noexcept;

m_Label{label},
m_State{std::move(tabState_)}
{
}

void onDraw()
{
if (BeginToolbar(m_Label))
{
drawContent();
}
ui::end_panel();
}
void onDraw();

private:
void drawContent()
{
// document-related stuff
drawNewDocumentButton();
ui::same_line();
drawOpenDocumentButton();
ui::same_line();
drawSaveLandmarksButton();
ui::same_line();

ui::draw_separator(ImGuiSeparatorFlags_Vertical);
ui::same_line();

// undo/redo-related stuff
m_UndoButton.on_draw();
ui::same_line();
m_RedoButton.onDraw();
ui::same_line();

ui::draw_separator(ImGuiSeparatorFlags_Vertical);
ui::same_line();

// camera stuff
drawCameraLockCheckbox();
ui::same_line();

ui::draw_separator(ImGuiSeparatorFlags_Vertical);
ui::same_line();
}

void drawNewDocumentButton()
{
if (ui::draw_button(ICON_FA_FILE))
{
ActionCreateNewDocument(m_State->updUndoable());
}
ui::draw_tooltip_if_item_hovered(
"Create New Document",
"Creates the default scene (undoable)"
);
}

void drawOpenDocumentButton()
{
ui::draw_button(ICON_FA_FOLDER_OPEN);
if (ui::begin_popup_context_menu("##OpenFolder", ImGuiPopupFlags_MouseButtonLeft))
{
if (ui::draw_menu_item("Load Source Mesh"))
{
ActionLoadMeshFile(m_State->updUndoable(), TPSDocumentInputIdentifier::Source);
}
if (ui::draw_menu_item("Load Destination Mesh"))
{
ActionLoadMeshFile(m_State->updUndoable(), TPSDocumentInputIdentifier::Destination);
}
ui::end_popup();
}
ui::draw_tooltip_if_item_hovered(
"Open File",
"Open Source/Destination data"
);
}

void drawSaveLandmarksButton()
{
if (ui::draw_button(ICON_FA_SAVE))
{
ActionSavePairedLandmarksToCSV(m_State->getScratch(), LandmarkCSVFlags::NoNames);
}
ui::draw_tooltip_if_item_hovered(
"Save Landmarks to CSV (no names)",
"Saves all pair-able landmarks to a CSV file, for external processing\n\n(legacy behavior: does not export names: use 'File' menu if you want the names)"
);
}

void drawCameraLockCheckbox()
{
ui::draw_checkbox("link cameras", &m_State->linkCameras);
ui::same_line();
if (!m_State->linkCameras)
{
ui::begin_disabled();
}
ui::draw_checkbox("only link rotation", &m_State->onlyLinkRotation);
if (!m_State->linkCameras)
{
ui::end_disabled();
}
}

std::string m_Label;
std::shared_ptr<MeshWarpingTabSharedState> m_State;
UndoButton m_UndoButton{m_State->editedDocument};
RedoButton m_RedoButton{m_State->editedDocument};
class Impl;
std::unique_ptr<Impl> m_Impl;
};
}

0 comments on commit 776e5db

Please sign in to comment.