Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add recent user models #312

Merged
merged 1 commit into from
Nov 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion x-IMU3-GUI/Source/OpenGL/Common/GLResources.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class GLResources
Model arrow { context };
Model board { context };
Model housing { context };
Model custom { context };
Model user { context };
PlaneModel plane;
TextQuad textQuad;
LineBuffer graphGridBuffer { true };
Expand Down
15 changes: 6 additions & 9 deletions x-IMU3-GUI/Source/OpenGL/ThreeDView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ void ThreeDView::setSettings(Settings settings_)
{
std::scoped_lock _(settingsMutex);
settings = settings_;

resources->user.setModel(settings.userModel);
}

ThreeDView::Settings ThreeDView::getSettings() const
Expand All @@ -93,11 +95,6 @@ ThreeDView::Settings ThreeDView::getSettings() const
return settings;
}

void ThreeDView::setCustomModel(const juce::File& file)
{
resources->custom.setModel(file);
}

bool ThreeDView::isLoading() const
{
switch (settings.model)
Expand All @@ -106,8 +103,8 @@ bool ThreeDView::isLoading() const
return resources->board.isLoading();
case Model::housing:
return resources->housing.isLoading();
case Model::custom:
return resources->custom.isLoading();
case Model::user:
return resources->user.isLoading();
}
return false;
}
Expand Down Expand Up @@ -151,8 +148,8 @@ void ThreeDView::renderModel(const glm::mat4& projectionMatrix, const glm::mat4&
case Model::housing:
resources->housing.renderWithMaterials(threeDViewShader);
break;
case Model::custom:
resources->custom.renderWithMaterials(threeDViewShader);
case Model::user:
resources->user.renderWithMaterials(threeDViewShader);
break;
}
}
Expand Down
5 changes: 2 additions & 3 deletions x-IMU3-GUI/Source/OpenGL/ThreeDView.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class ThreeDView : public OpenGLComponent
{
board,
housing,
custom,
user,
};

enum class AxesConvention
Expand All @@ -35,6 +35,7 @@ class ThreeDView : public OpenGLComponent
bool axesEnabled = true;
bool compassEnabled = true;
Model model { Model::housing };
juce::File userModel;
AxesConvention axesConvention { AxesConvention::nwu };
};

Expand All @@ -48,8 +49,6 @@ class ThreeDView : public OpenGLComponent

Settings getSettings() const;

void setCustomModel(const juce::File& file);

bool isLoading() const;

void setHudEnabled(const bool enabled);
Expand Down
12 changes: 9 additions & 3 deletions x-IMU3-GUI/Source/OpenGL/ThreeDView/Model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,19 @@ void Model::setModel(const juce::String& objFileContent, const juce::String& mtl
});
}

void Model::setModel(const juce::File& objFile)
void Model::setModel(const juce::File& objFile_)
{
if (objFile == objFile_)
{
return;
}
objFile = objFile_;

loading = true;
juce::Thread::launch([&, self = juce::WeakReference(this), objFile]
juce::Thread::launch([&, self = juce::WeakReference(this), objFile_]
{
auto newObject = std::make_shared<WavefrontObjFile>();
newObject->load(objFile);
newObject->load(objFile_);

juce::MessageManager::callAsync([&, self, newObject]() mutable
{
Expand Down
2 changes: 2 additions & 0 deletions x-IMU3-GUI/Source/OpenGL/ThreeDView/Model.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ class Model
private:
juce::OpenGLContext& context;

juce::File objFile;

std::mutex objectLock;
std::shared_ptr<WavefrontObjFile> object;
bool fillBuffersPending = false;
Expand Down
36 changes: 32 additions & 4 deletions x-IMU3-GUI/Source/Windows/ThreeDViewWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ void ThreeDViewWindow::writeToValueTree(const ThreeDView::Settings& settings)
settingsTree.setProperty("axesEnabled", settings.axesEnabled, nullptr);
settingsTree.setProperty("compassEnabled", settings.compassEnabled, nullptr);
settingsTree.setProperty("model", static_cast<int>(settings.model), nullptr);
settingsTree.setProperty("userModel", settings.userModel.getFullPathName(), nullptr);
settingsTree.setProperty("axesConvention", static_cast<int>(settings.axesConvention), nullptr);
}

Expand All @@ -211,6 +212,7 @@ ThreeDView::Settings ThreeDViewWindow::readFromValueTree() const
settings.axesEnabled = settingsTree.getProperty("axesEnabled", settings.axesEnabled);
settings.compassEnabled = settingsTree.getProperty("compassEnabled", settings.axesEnabled);
settings.model = static_cast<ThreeDView::Model>((int) settingsTree.getProperty("model", static_cast<int>(settings.model)));
settings.userModel = settingsTree["userModel"];
settings.axesConvention = static_cast<ThreeDView::AxesConvention>((int) settingsTree.getProperty("axesConvention", static_cast<int>(settings.axesConvention)));
return settings;
}
Expand Down Expand Up @@ -294,19 +296,45 @@ juce::PopupMenu ThreeDViewWindow::getMenu()
settings.model = ThreeDView::Model::housing;
writeToValueTree(settings);
});
menu.addItem("Custom", true, threeDView.getSettings().model == ThreeDView::Model::custom, [&]

juce::PopupMenu userModelsMenu;
userModelsMenu.addItem("Load...", [&]
{
juce::FileChooser fileChooser("Select Custom Model", juce::File(), "*.obj");
juce::FileChooser fileChooser("Select Model", juce::File(), "*.obj");

if (fileChooser.browseForFileToOpen())
{
threeDView.setCustomModel(fileChooser.getResult());
const auto objFileOriginal = fileChooser.getResult();
const auto mtlFileOriginal = objFileOriginal.withFileExtension(".mtl");

const auto objFileCopy = userModelsDirectory.getChildFile(objFileOriginal.getFileName());
const auto mtlFileCopy = userModelsDirectory.getChildFile(mtlFileOriginal.getFileName());

userModelsDirectory.createDirectory();
objFileOriginal.copyFileTo(objFileCopy);
mtlFileOriginal.copyFileTo(mtlFileCopy);

auto settings = threeDView.getSettings();
settings.model = ThreeDView::Model::custom;
settings.model = ThreeDView::Model::user;
settings.userModel = objFileCopy;
writeToValueTree(settings);
}
});
userModelsMenu.addSeparator();
userModelsMenu.addCustomItem(-1, std::make_unique<PopupMenuHeader>("RECENT"), nullptr);
for (const auto& file : userModelsDirectory.findChildFiles(juce::File::findFiles, false, "*.obj"))
{
const auto ticked = (threeDView.getSettings().model == ThreeDView::Model::user) && (threeDView.getSettings().userModel == file);
userModelsMenu.addItem(file.getFileNameWithoutExtension(), true, ticked, [&, file]
{
auto settings = threeDView.getSettings();
settings.model = ThreeDView::Model::user;
settings.userModel = file;
writeToValueTree(settings);
});
}

menu.addSubMenu("User", userModelsMenu, true, nullptr, threeDView.getSettings().model == ThreeDView::Model::user);

menu.addSeparator();
menu.addCustomItem(-1, std::make_unique<PopupMenuHeader>("AXES CONVENTION"), nullptr);
Expand Down
3 changes: 3 additions & 0 deletions x-IMU3-GUI/Source/Windows/ThreeDViewWindow.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once

#include "ApplicationSettings.h"
#include "DevicePanel/DevicePanel.h"
#include <juce_gui_basics/juce_gui_basics.h>
#include "OpenGL/Common/GLRenderer.h"
Expand Down Expand Up @@ -73,6 +74,8 @@ class ThreeDViewWindow : public Window,
Icon accelerationRecoveryIcon { BinaryData::vibration_grey_svg, "Acceleration Recovery" };
Icon magneticRecoveryIcon { BinaryData::magnet_grey_svg, "Magnetic Recovery" };

const juce::File userModelsDirectory = ApplicationSettings::getDirectory().getChildFile("User Models");

static float wrapAngle(float angle);

void writeToValueTree(const ThreeDView::Settings& settings);
Expand Down