Skip to content

Commit

Permalink
Add+rollout DropFileEvent
Browse files Browse the repository at this point in the history
  • Loading branch information
adamkewley committed Sep 15, 2024
1 parent 57fd286 commit ba828ee
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 38 deletions.
11 changes: 2 additions & 9 deletions src/OpenSimCreator/UI/MeshImporter/MeshImporterSharedState.cpp
Original file line number Diff line number Diff line change
@@ -1,19 +1,12 @@
#include "MeshImporterSharedState.h"

#include <oscar/Platform/Event.h>
#include <SDL_events.h>

bool osc::mi::MeshImporterSharedState::onEvent(const Event& ev)
{
const SDL_Event& e = ev;

// if the user drags + drops a file into the window, assume it's a meshfile
// and start loading it
if (e.type == SDL_DROPFILE && e.drop.file != nullptr)
{
m_DroppedFiles.emplace_back(e.drop.file);
if (const auto* dropfile = dynamic_cast<const DropFileEvent*>(&ev)) {
m_DroppedFiles.emplace_back(dropfile->path());
return true;
}

return false;
}
32 changes: 11 additions & 21 deletions src/OpenSimCreator/UI/ModelEditor/ModelEditorTab.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -182,18 +182,16 @@ class osc::ModelEditorTab::Impl final : public IEditorAPI {
App::upd().make_main_loop_polling();
}

bool onEvent(const SDL_Event& e)
bool onEvent(const Event& ev)
{
if (e.type == SDL_KEYDOWN)
{
const SDL_Event& e = ev;
if (e.type == SDL_KEYDOWN) {
return onKeydownEvent(e.key);
}
else if (e.type == SDL_DROPFILE)
{
return onDropEvent(e.drop);
else if (const auto* dropfile = dynamic_cast<const DropFileEvent*>(&ev)) {
return onDropEvent(*dropfile);
}
else
{
else {
return false;
}
}
Expand Down Expand Up @@ -318,22 +316,14 @@ class osc::ModelEditorTab::Impl final : public IEditorAPI {
return std::move(ss).str();
}

bool onDropEvent(const SDL_DropEvent& e)
bool onDropEvent(const DropFileEvent& e)
{
if (e.type != SDL_DROPFILE || e.file == nullptr)
{
return false;
if (e.path().extension() == ".sto") {
return ActionLoadSTOFileAgainstModel(m_Parent, *m_Model, e.path());
}
const std::string_view filename{e.file};

if (filename.ends_with(".sto"))
{
return ActionLoadSTOFileAgainstModel(m_Parent, *m_Model, e.file);
}
else if (filename.ends_with(".osim"))
{
else if (e.path().extension() == ".osim") {
// if the user drops an osim file on this tab then it should be loaded
m_Parent->add_and_select_tab<LoadingTab>(m_Parent, e.file);
m_Parent->add_and_select_tab<LoadingTab>(m_Parent, e.path());
return true;
}

Expand Down
12 changes: 6 additions & 6 deletions src/OpenSimCreator/UI/SplashTab.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
#include <oscar/Utils/Algorithms.h>
#include <oscar/Utils/CStringView.h>
#include <oscar/Utils/ParentPtr.h>
#include <SDL_events.h>

#include <filesystem>
#include <span>
Expand Down Expand Up @@ -128,12 +127,13 @@ class osc::SplashTab::Impl final {
App::upd().make_main_loop_polling();
}

bool onEvent(const SDL_Event& e)
bool onEvent(const Event& e)
{
if (e.type == SDL_DROPFILE && e.drop.file != nullptr && std::string_view{e.drop.file}.ends_with(".osim")) {
// if the user drops an osim file on this tab then it should be loaded
m_Parent->add_and_select_tab<LoadingTab>(m_Parent, e.drop.file);
return true;
if (const auto* dropfile = dynamic_cast<const DropFileEvent*>(&e)) {
if (dropfile->path().extension() == ".osim") {
m_Parent->add_and_select_tab<LoadingTab>(m_Parent, dropfile->path());
return true;
}
}
return false;
}
Expand Down
18 changes: 16 additions & 2 deletions src/oscar/Platform/Event.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,24 @@
#include "Event.h"

#include <oscar/Utils/Assertions.h>

#include <SDL_events.h>

#include <memory>

using namespace osc;

osc::DropFileEvent::DropFileEvent(const SDL_Event& e) :
Event{e},
path_{e.drop.file}
{}

std::unique_ptr<Event> osc::parse_into_event(const SDL_Event& e)
{
return std::make_unique<RawEvent>(e);
}
if (e.type == SDL_DROPFILE and e.drop.file) {
return std::make_unique<DropFileEvent>(e);
}
else {
return std::make_unique<RawEvent>(e);
}
}
11 changes: 11 additions & 0 deletions src/oscar/Platform/Event.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once

#include <filesystem>
#include <memory>

union SDL_Event;
Expand Down Expand Up @@ -29,5 +30,15 @@ namespace osc
explicit RawEvent(const SDL_Event& e) : Event{e} {}
};

class DropFileEvent : public Event {
public:
explicit DropFileEvent(const SDL_Event& e);

const std::filesystem::path& path() const { return path_; }

private:
std::filesystem::path path_;
};

std::unique_ptr<Event> parse_into_event(const SDL_Event&);
}

0 comments on commit ba828ee

Please sign in to comment.