Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
darbyjohnston committed Sep 27, 2024
1 parent 5714076 commit 0e4f72a
Show file tree
Hide file tree
Showing 15 changed files with 471 additions and 65 deletions.
5 changes: 5 additions & 0 deletions bin/toucan-edit/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,19 @@ set(HEADERS
Gap.h
GapWidget.h
IContainer.h
IContainerInline.h
IItem.h
IItemWidget.h
ImageGraph.h
MediaReference.h
MenuBar.h
PlaybackModel.h
SelectionModel.h
Stack.h
StackWidget.h
ThumbnailGenerator.h
Timeline.h
TimelineConvert.h
TimelineView.h
TimelineWidget.h
TimeUnitsModel.h
Expand All @@ -43,13 +46,15 @@ set(SOURCE
IItem.cpp
IItemWidget.cpp
ImageGraph.cpp
MediaReference.cpp
MenuBar.cpp
PlaybackModel.cpp
SelectionModel.cpp
Stack.cpp
StackWidget.cpp
ThumbnailGenerator.cpp
Timeline.cpp
TimelineConvert.cpp
TimelineView.cpp
TimelineWidget.cpp
TimeUnitsModel.cpp
Expand Down
24 changes: 24 additions & 0 deletions bin/toucan-edit/Clip.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,35 @@

#include "Clip.h"

#include "MediaReference.h"

namespace toucan
{
Clip::Clip()
{}

Clip::~Clip()
{}

const Clip::MediaReferences& Clip::getMediaReferences() const
{
return _mediaReferences;
}

void Clip::setMediaReferences(const MediaReferences& value)
{
_mediaReferences = value;
}

const std::string Clip::defaultMediaKey = "DEFAULT_MEDIA";

const std::string& Clip::getActiveMediaReference() const
{
return _activeMediaReference;
}

void Clip::setActiveMediaReference(const std::string& value)
{
_activeMediaReference = value;
}
}
18 changes: 17 additions & 1 deletion bin/toucan-edit/Clip.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,32 @@

#include "IItem.h"

#include <vector>
#include <map>

namespace toucan
{
class MediaReference;

class Clip : public IItem
{
public:
Clip();

virtual ~Clip();

using MediaReferences = std::map<std::string, std::shared_ptr<MediaReference> >;

const MediaReferences& getMediaReferences() const;
void setMediaReferences(const MediaReferences&);

static const std::string defaultMediaKey;

const std::string& getActiveMediaReference() const;
void setActiveMediaReference(const std::string&);

private:
MediaReferences _mediaReferences;
std::string _activeMediaReference = defaultMediaKey;
};
}

2 changes: 1 addition & 1 deletion bin/toucan-edit/Document.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ namespace toucan
{
throw std::runtime_error(errorStatus.full_description);
}
_timeline = Timeline::create(path, otioTimeline);
_timeline = convertFrom(path, otioTimeline);

const auto& globalStartTime = otioTimeline->global_start_time();
const OTIO_NS::RationalTime& duration = otioTimeline->duration();
Expand Down
16 changes: 16 additions & 0 deletions bin/toucan-edit/IContainer.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@

namespace toucan
{
enum class Find
{
Recurse,
Shallow
};

class IContainer : public IItem
{
public:
Expand All @@ -21,8 +27,18 @@ namespace toucan
void addChild(const std::shared_ptr<IItem>&);
void removeChild(const std::shared_ptr<IItem>&);

template<typename T>
std::shared_ptr<T> find(Find find = Find::Recurse) const;
template<typename T>
std::vector<std::shared_ptr<T> > findAll(Find find = Find::Recurse) const;

private:
template<typename T>
void _findAll(Find, std::vector<std::shared_ptr<T> >&) const;

std::vector<std::shared_ptr<IItem> > _children;
};
}

#include "IContainerInline.h"

54 changes: 54 additions & 0 deletions bin/toucan-edit/IContainerInline.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// SPDX-License-Identifier: Apache-2.0
// Copyright (c) 2024 Darby Johnston
// All rights reserved.

namespace toucan
{
template<typename T>
inline std::shared_ptr<T> IContainer::find(Find find) const
{
std::shared_ptr<IItem> out;
for (const auto& child : _children)
{
if ((out = std::dynamic_pointer_cast<T>(child)))
{
break;
}
if (Find::Recurse == find)
{
if (auto container = std::dynamic_pointer_cast<IContainer>(child))
{
out = container->find<T>();
}
}
}
return out;
}

template<typename T>
inline std::vector<std::shared_ptr<T> > IContainer::findAll(Find find) const
{
std::vector<std::shared_ptr<T> > out;
_findAll(find, out);
return out;
}

template<typename T>
inline void IContainer::_findAll(Find find, std::vector<std::shared_ptr<T> >& out) const
{
for (const auto& child : _children)
{
if (auto t = std::dynamic_pointer_cast<T>(child))
{
out.push_back(t);
}
if (Find::Recurse == find)
{
if (auto container = std::dynamic_pointer_cast<IContainer>(child))
{
container->_findAll<T>(find, out);
}
}
}
}
}
106 changes: 103 additions & 3 deletions bin/toucan-edit/ImageGraph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,53 @@

#include "ImageGraph.h"

#include "Clip.h"
#include "Gap.h"
#include "MediaReference.h"
#include "Stack.h"
#include "Timeline.h"
#include "Track.h"

#include <toucan/Comp.h>
#include <toucan/Read.h>
#include <toucan/Util.h>

namespace toucan
{
ImageGraph::ImageGraph(
const std::shared_ptr<Timeline>& timeline,
const ImageGraphOptions& options) :
_timeline(timeline),
_options(options)
{}
{
// Get the image size from the first clip.
for (const auto& clip : timeline->getStack()->findAll<Clip>())
{
auto refs = clip->getMediaReferences();
auto i = refs.find(clip->getActiveMediaReference());
if (i == refs.end())
{
i = refs.begin();
}
if (i != refs.end())
{
if (auto externalRef = std::dynamic_pointer_cast<ExternalReference>(i->second))
{
const std::filesystem::path path = getMediaPath(
timeline->getPath(),
externalRef->getURL());
const OIIO::ImageBuf buf(path.string());
const auto& spec = buf.spec();
if (spec.width > 0)
{
_imageSize.x = spec.width;
_imageSize.y = spec.height;
break;
}
}
}
}
}

ImageGraph::~ImageGraph()
{}
Expand All @@ -22,8 +61,69 @@ namespace toucan
}

std::shared_ptr<IImageNode> ImageGraph::exec(
const std::shared_ptr<ImageEffectHost>&,
const OTIO_NS::RationalTime&)
const std::shared_ptr<ImageEffectHost>& host,
const OTIO_NS::RationalTime& time)
{
// Set the background color.
OTIO_NS::AnyDictionary metaData;
metaData["size"] = vecToAny(_imageSize);
metaData["color"] = vecToAny(IMATH_NAMESPACE::V4f(0.F, 0.F, 0.F, 1.F));
std::shared_ptr<IImageNode> node = host->createNode("toucan:Fill", metaData);

// Loop over the tracks.
for (const auto& track : _timeline->getStack()->findAll<Track>(Find::Shallow))
{
if (TrackKind::video == track->getKind())
{
// Process this track.
auto trackNode = _track(host, time, track);

// Get the track effects.
//const auto& effects = track->effects();
//if (!effects.empty())
//{
// trackNode = _effects(host, effects, trackNode);
//}

// Composite this track over the previous track.
std::vector<std::shared_ptr<IImageNode> > nodes;
if (trackNode)
{
nodes.push_back(trackNode);
}
if (node)
{
nodes.push_back(node);
}
auto comp = std::make_shared<CompNode>(nodes);
comp->setPremult(true);
node = comp;
}
}


// Get the stack effects.
//const auto& effects = stack->effects();
//if (!effects.empty())
//{
// node = _effects(host, effects, node);
//}

return node;
}

std::shared_ptr<IImageNode> ImageGraph::_track(
const std::shared_ptr<ImageEffectHost>& host,
const OTIO_NS::RationalTime& time,
const std::shared_ptr<Track>& track)
{
return nullptr;
}

std::shared_ptr<IImageNode> ImageGraph::_item(
const std::shared_ptr<ImageEffectHost>& host,
const OTIO_NS::RationalTime& time,
const std::shared_ptr<IItem>& item)
{
return nullptr;
}
Expand Down
18 changes: 18 additions & 0 deletions bin/toucan-edit/ImageGraph.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,17 @@

#pragma once

#include <toucan/ImageEffectHost.h>
#include <toucan/ImageNode.h>
#include <toucan/MessageLog.h>

#include <opentimelineio/version.h>

namespace toucan
{
class IItem;
class Timeline;
class Track;

//! Image graph options.
struct ImageGraphOptions
Expand All @@ -38,6 +41,21 @@ namespace toucan
const OTIO_NS::RationalTime&);

private:
std::shared_ptr<IImageNode> _track(
const std::shared_ptr<ImageEffectHost>&,
const OTIO_NS::RationalTime&,
const std::shared_ptr<Track>&);

std::shared_ptr<IImageNode> _item(
const std::shared_ptr<ImageEffectHost>&,
const OTIO_NS::RationalTime&,
const std::shared_ptr<IItem>&);

//std::shared_ptr<IImageNode> _effects(
// const std::shared_ptr<ImageEffectHost>&,
// const std::vector<std::shared_ptr<Effect> >&,
// const std::shared_ptr<IImageNode>&);

std::shared_ptr<Timeline> _timeline;
ImageGraphOptions _options;
IMATH_NAMESPACE::V2i _imageSize = IMATH_NAMESPACE::V2i(0, 0);
Expand Down
Loading

0 comments on commit 0e4f72a

Please sign in to comment.