Skip to content

Commit

Permalink
Refactor east- to west-const in OpenSimCreator/UI root
Browse files Browse the repository at this point in the history
  • Loading branch information
adamkewley committed Jun 17, 2024
1 parent 1e3a89c commit ad537f2
Show file tree
Hide file tree
Showing 12 changed files with 201 additions and 282 deletions.
2 changes: 1 addition & 1 deletion apps/osc/Debian/Packaging.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ install(
TARGETS osc
RUNTIME_DEPENDENCIES
DIRECTORIES $<TARGET_FILE_DIR:SimTKcommon>
POST_EXCLUDE_REGEXES "^/usr/*" "^/lib/*" # don't copy system-provided dependencies
POST_EXCLUDE_REGEXES "^/usr/*" "^/lib/*" # don't copy system-provided dependencies
)

# install-time: install a user-facing `osc.toml` config file
Expand Down
26 changes: 20 additions & 6 deletions scripts/format_sources.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import os
import re

_blacklisted_directories = {'resources', 'MacOS', 'Linux', 'Windows'}

def num_leading_tabs(s):
rv = 0
for c in s:
Expand All @@ -16,7 +18,7 @@ def num_leading_tabs(s):
return rv

def read_lines(path):
with open(path) as f:
with open(path, encoding='utf-8') as f:
return f.read().splitlines()

def replace_leading_tabs(line):
Expand All @@ -26,11 +28,19 @@ def replace_leading_tabs(line):
else:
return (line, False)

def should_skip_dir(dirname):
for blacklisted_directory in _blacklisted_directories:
if blacklisted_directory in dirname:
return True
return False

# recursively tries to convert any tabs to spaces in the source tree
#
# (tabs sometimes slip into the source tree from editing files in terminals)
def replace_leading_tabs_with_spaces_in(dirpath):
for root, subdirs, files in os.walk(dirpath):
if should_skip_dir(root):
continue
for file in files:
path = os.path.join(root, file)
lines = read_lines(path)
Expand All @@ -44,7 +54,7 @@ def replace_leading_tabs_with_spaces_in(dirpath):

if should_write:
new_content = "\n".join(new_lines) + "\n" # trailing newline
with open(path, "w") as f:
with open(path, "w", encoding='utf-8') as f:
f.write(new_content)

# recursively tries to strip whitespace from all files in a source
Expand All @@ -54,28 +64,32 @@ def replace_leading_tabs_with_spaces_in(dirpath):
def strip_whitespace_for_all_files_in(dirname):
pattern = re.compile(r"[ \t]+$", re.MULTILINE)
for root, subdirs, files in os.walk(dirname):
if should_skip_dir(root):
continue
for file in files:
p = os.path.join(root, file)

new_content = None
with open(p) as f:
with open(p, encoding='utf-8') as f:
content = f.read()
match = re.search(pattern, content)
if match:
new_content = re.sub(pattern, "", content)
if new_content:
with open(p, "w") as f:
with open(p, "w", encoding='utf-8') as f:
f.write(new_content)

# recursively adds trailing newline to any files missing one in the given dir
def ensure_all_files_have_trailing_newline(dirpath):
for root, subdirs, files in os.walk(dirpath):
if should_skip_dir(root):
continue
for file in files:
path = os.path.join(root, file)
with open(path, "r") as f:
with open(path, "r", encoding='utf-8') as f:
content = f.read()
if not content.endswith("\n"):
with open(path, "w") as f:
with open(path, "w", encoding='utf-8') as f:
f.write(content + "\n")


Expand Down
28 changes: 14 additions & 14 deletions src/OpenSimCreator/UI/IMainUIStateAPI.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ namespace osc
class IMainUIStateAPI : public ITabHost {
protected:
IMainUIStateAPI() = default;
IMainUIStateAPI(IMainUIStateAPI const&) = default;
IMainUIStateAPI(const IMainUIStateAPI&) = default;
IMainUIStateAPI(IMainUIStateAPI&&) noexcept = default;
IMainUIStateAPI& operator=(IMainUIStateAPI const&) = default;
IMainUIStateAPI& operator=(const IMainUIStateAPI&) = default;
IMainUIStateAPI& operator=(IMainUIStateAPI&&) noexcept = default;
public:
virtual ~IMainUIStateAPI() noexcept = default;

ParamBlock const& getSimulationParams() const
const ParamBlock& getSimulationParams() const
{
return implGetSimulationParams();
}
Expand All @@ -33,41 +33,41 @@ namespace osc
{
return implGetNumUserOutputExtractors();
}
OutputExtractor const& getUserOutputExtractor(int index) const
const OutputExtractor& getUserOutputExtractor(int index) const
{
return implGetUserOutputExtractor(index);
}
void addUserOutputExtractor(OutputExtractor const& extractor)
void addUserOutputExtractor(const OutputExtractor& extractor)
{
return implAddUserOutputExtractor(extractor);
}
void removeUserOutputExtractor(int index)
{
implRemoveUserOutputExtractor(index);
}
bool hasUserOutputExtractor(OutputExtractor const& extractor) const
bool hasUserOutputExtractor(const OutputExtractor& extractor) const
{
return implHasUserOutputExtractor(extractor);
}
bool removeUserOutputExtractor(OutputExtractor const& extractor)
bool removeUserOutputExtractor(const OutputExtractor& extractor)
{
return implRemoveUserOutputExtractor(extractor);
}
bool overwriteOrAddNewUserOutputExtractor(OutputExtractor const& old, OutputExtractor const& newer)
bool overwriteOrAddNewUserOutputExtractor(const OutputExtractor& old, const OutputExtractor& newer)
{
return implOverwriteOrAddNewUserOutputExtractor(old, newer);
}

private:
virtual ParamBlock const& implGetSimulationParams() const = 0;
virtual const ParamBlock& implGetSimulationParams() const = 0;
virtual ParamBlock& implUpdSimulationParams() = 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;
};
}
4 changes: 2 additions & 2 deletions src/OpenSimCreator/UI/IPopupAPI.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ namespace osc
class IPopupAPI {
protected:
IPopupAPI() = default;
IPopupAPI(IPopupAPI const&) = default;
IPopupAPI(const IPopupAPI&) = default;
IPopupAPI(IPopupAPI&&) noexcept = default;
IPopupAPI& operator=(IPopupAPI const&) = default;
IPopupAPI& operator=(const IPopupAPI&) = default;
IPopupAPI& operator=(IPopupAPI&&) noexcept = default;
public:
virtual ~IPopupAPI() noexcept = default;
Expand Down
52 changes: 20 additions & 32 deletions src/OpenSimCreator/UI/LoadingTab.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ using namespace osc;

namespace
{
std::unique_ptr<UndoableModelStatePair> LoadOsimIntoUndoableModel(std::filesystem::path const& p)
std::unique_ptr<UndoableModelStatePair> LoadOsimIntoUndoableModel(const std::filesystem::path& p)
{
return std::make_unique<UndoableModelStatePair>(p);
}
Expand All @@ -38,14 +38,13 @@ class osc::LoadingTab::Impl final {
public:

Impl(
ParentPtr<IMainUIStateAPI> const& parent_,
const ParentPtr<IMainUIStateAPI>& parent_,
std::filesystem::path path_) :

m_Parent{parent_},
m_OsimPath{std::move(path_)},
m_LoadingResult{std::async(std::launch::async, LoadOsimIntoUndoableModel, m_OsimPath)}
{
}
{}

UID getID() const
{
Expand All @@ -59,39 +58,34 @@ class osc::LoadingTab::Impl final {

void on_tick()
{
auto const dt = static_cast<float>(App::get().frame_delta_since_last_frame().count());
const auto dt = static_cast<float>(App::get().frame_delta_since_last_frame().count());

// tick the progress bar up a little bit
m_LoadingProgress += (dt * (1.0f - m_LoadingProgress))/2.0f;

// if there's an error, then the result came through (it's an error)
// and this screen should just continuously show the error until the
// user decides to transition back
if (!m_LoadingErrorMsg.empty())
{
if (!m_LoadingErrorMsg.empty()) {
return;
}

// otherwise, poll for the result and catch any exceptions that bubble
// up from the background thread
std::unique_ptr<UndoableModelStatePair> result = nullptr;
try
{
if (m_LoadingResult.wait_for(std::chrono::seconds{0}) == std::future_status::ready)
{
try {
if (m_LoadingResult.wait_for(std::chrono::seconds{0}) == std::future_status::ready) {
result = m_LoadingResult.get();
}
}
catch (std::exception const& ex)
{
catch (const std::exception& ex) {
log_info("LoadingScreen::on_tick: exception thrown while loading model: %s", ex.what());
m_LoadingErrorMsg = ex.what();
return;
}

// if there was a result (a newly-loaded model), handle it
if (result)
{
if (result) {
// add newly-loaded model to the "Recent Files" list
App::singleton<RecentFiles>()->push_back(m_OsimPath);

Expand All @@ -105,37 +99,32 @@ class osc::LoadingTab::Impl final {

void onDraw()
{
Rect const viewportUIRect = ui::get_main_viewport_workspace_uiscreenspace_rect();
Vec2 const viewportDims = dimensions_of(viewportUIRect);
Vec2 const menuDimsGuess = {0.3f * viewportDims.x, 6.0f * ui::get_text_line_height()};
const Rect viewportUIRect = ui::get_main_viewport_workspace_uiscreenspace_rect();
const Vec2 viewportDims = dimensions_of(viewportUIRect);
const Vec2 menuDimsGuess = {0.3f * viewportDims.x, 6.0f * ui::get_text_line_height()};

// center the menu
{
Vec2 const menuTopLeft = 0.5f * (viewportDims - menuDimsGuess);
const Vec2 menuTopLeft = 0.5f * (viewportDims - menuDimsGuess);
ui::set_next_panel_pos(viewportUIRect.p1 + menuTopLeft);
ui::set_next_panel_size({menuDimsGuess.x, -1.0f});
}

if (m_LoadingErrorMsg.empty())
{
if (ui::begin_panel("Loading Message", nullptr, ImGuiWindowFlags_NoTitleBar))
{
if (m_LoadingErrorMsg.empty()) {
if (ui::begin_panel("Loading Message", nullptr, ImGuiWindowFlags_NoTitleBar)) {
ui::draw_text("loading: %s", m_OsimPath.string().c_str());
ui::draw_progress_bar(m_LoadingProgress);
}
ui::end_panel();
}
else
{
if (ui::begin_panel("Error Message", nullptr, ImGuiWindowFlags_NoTitleBar))
{
else {
if (ui::begin_panel("Error Message", nullptr, ImGuiWindowFlags_NoTitleBar)) {
ui::draw_text_wrapped("An error occurred while loading the file:");
ui::draw_dummy({0.0f, 5.0f});
ui::draw_text_wrapped(m_LoadingErrorMsg);
ui::draw_dummy({0.0f, 5.0f});

if (ui::draw_button("try again"))
{
if (ui::draw_button("try again")) {
m_Parent->add_and_select_tab<LoadingTab>(m_Parent, m_OsimPath);
m_Parent->close_tab(m_TabID);
}
Expand Down Expand Up @@ -171,12 +160,11 @@ class osc::LoadingTab::Impl final {
// public API (PIMPL)

osc::LoadingTab::LoadingTab(
ParentPtr<IMainUIStateAPI> const& parent_,
const ParentPtr<IMainUIStateAPI>& parent_,
std::filesystem::path path_) :

m_Impl{std::make_unique<Impl>(parent_, std::move(path_))}
{
}
{}

osc::LoadingTab::LoadingTab(LoadingTab&&) noexcept = default;
osc::LoadingTab& osc::LoadingTab::operator=(LoadingTab&&) noexcept = default;
Expand Down
6 changes: 3 additions & 3 deletions src/OpenSimCreator/UI/LoadingTab.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ namespace osc
{
class LoadingTab final : public ITab {
public:
LoadingTab(ParentPtr<IMainUIStateAPI> const&, std::filesystem::path);
LoadingTab(LoadingTab const&) = delete;
LoadingTab(const ParentPtr<IMainUIStateAPI>&, std::filesystem::path);
LoadingTab(const LoadingTab&) = delete;
LoadingTab(LoadingTab&&) noexcept;
LoadingTab& operator=(LoadingTab const&) = delete;
LoadingTab& operator=(const LoadingTab&) = delete;
LoadingTab& operator=(LoadingTab&&) noexcept;
~LoadingTab() noexcept override;

Expand Down
Loading

0 comments on commit ad537f2

Please sign in to comment.