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

Scene bindings #439

Merged
merged 4 commits into from
Jan 31, 2025
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
9 changes: 7 additions & 2 deletions src/app/GUI/Expressions/expressionhighlighter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ ExpressionHighlighter::ExpressionHighlighter(
"\\s*=\\s*" + propPath);
mFrameValueSetRegex = QRegularExpression("^\\s*"
"([A-Za-z_][A-Za-z0-9_]*)"
"\\s*=\\s*(\\$frame|\\$value)");
"\\s*=\\s*(\\$frame|\\$scene.fps|\\$scene.width|\\$scene.height|\\$scene.rangeMin|\\$scene.rangeMax|\\$value)");
// const auto propPathRegex = QRegularExpression(propPath);

mPropPathFormat.setFontWeight(QFont::Bold);
Expand All @@ -61,7 +61,12 @@ ExpressionHighlighter::ExpressionHighlighter(

const QStringList specs = {
QStringLiteral("$value"),
QStringLiteral("$frame")
QStringLiteral("$frame"),
QStringLiteral("$scene.fps"),
QStringLiteral("$scene.width"),
QStringLiteral("$scene.height"),
QStringLiteral("$scene.rangeMin"),
QStringLiteral("$scene.rangeMax")
};

QTextCharFormat specialFormat;
Expand Down
2 changes: 2 additions & 0 deletions src/core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ set(
Boxes/nullobject.cpp
Expressions/expression.cpp
Expressions/framebinding.cpp
Expressions/scenebinding.cpp
Expressions/propertybinding.cpp
Animators/SmartPath/listofnodes.cpp
Animators/SmartPath/smartpath.cpp
Expand Down Expand Up @@ -359,6 +360,7 @@ set(
Boxes/nullobject.h
Expressions/expression.h
Expressions/framebinding.h
Expressions/scenebinding.h
Expressions/propertybinding.h
Animators/SmartPath/listofnodes.h
Animators/SmartPath/smartpath.h
Expand Down
39 changes: 37 additions & 2 deletions src/core/Expressions/propertybindingparser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@
#include "propertybindingparser.h"

#include "exceptions.h"

#include "framebinding.h"
#include "valuebinding.h"
#include "scenebinding.h"
#include "appsupport.h"

void skipSpaces(const QString& exp, int& position) {
Expand Down Expand Up @@ -84,7 +84,7 @@ QString parse(const QString& exp, int& pos, const int n) {

bool parse(const QString& exp, int& pos, const QString& test) {
int newPos = pos;
const auto value = parse(exp, newPos, QString("$value").count());
const auto value = parse(exp, newPos, test.count());
if(value == test) {
pos = newPos;
return true;
Expand All @@ -103,6 +103,26 @@ bool parseFrame(const QString& exp, int& pos) {
return parse(exp, pos, "$frame");
}

bool parseSceneFPS(const QString& exp, int& pos) {
return parse(exp, pos, "$scene.fps");
}

bool parseSceneWidth(const QString& exp, int& pos) {
return parse(exp, pos, "$scene.width");
}

bool parseSceneHeight(const QString& exp, int& pos) {
return parse(exp, pos, "$scene.height");
}

bool parseSceneRangeMax(const QString& exp, int& pos) {
return parse(exp, pos, "$scene.rangeMax");
}

bool parseSceneRangeMin(const QString& exp, int& pos) {
return parse(exp, pos, "$scene.rangeMin");
}

void parseBinding(const QString& exp, int& pos, QString& binding) {
while(pos < exp.count()) {
const auto& c = exp.at(pos++);
Expand All @@ -128,6 +148,21 @@ qsptr<PropertyBindingBase> PropertyBindingParser::parseBinding(
skipSpaces(exp, pos);
if(parseFrame(exp, pos)) {
result = FrameBinding::sCreate(context);
} else if(parseSceneFPS(exp, pos)) {
result = SceneBinding::sCreate(context,
SceneBinding::SceneBindingFps);
} else if(parseSceneWidth(exp, pos)) {
result = SceneBinding::sCreate(context,
SceneBinding::SceneBindingWidth);
} else if(parseSceneHeight(exp, pos)) {
result = SceneBinding::sCreate(context,
SceneBinding::SceneBindingHeight);
} else if(parseSceneRangeMin(exp, pos)) {
result = SceneBinding::sCreate(context,
SceneBinding::SceneBindingRangeMin);
} else if(parseSceneRangeMax(exp, pos)) {
result = SceneBinding::sCreate(context,
SceneBinding::SceneBindingRangeMax);
} else if(parseValue(exp, pos)) {
result = ValueBinding::sCreate(context);
} else {
Expand Down
102 changes: 102 additions & 0 deletions src/core/Expressions/scenebinding.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/*
#
# Friction - https://friction.graphics
#
# Copyright (c) Ole-André Rodlie and contributors
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# See 'README.md' for more information.
#
*/

#include "scenebinding.h"

SceneBinding::SceneBinding(const Property* const context,
const SceneBinding::SceneBindingType &binding)
: PropertyBindingBase(context)
, mBindingType(binding)
{

}

qsptr<SceneBinding> SceneBinding::sCreate(const Property* const context,
const SceneBindingType &binding)
{
const auto result = new SceneBinding(context, binding);
return qsptr<SceneBinding>(result);
}

QJSValue SceneBinding::getJSValue(QJSEngine& e)
{
Q_UNUSED(e);
if (mContext) {
qreal val = 0.0;
switch (mBindingType) {
case SceneBindingFps:
val = mContext->prp_getSceneFPS();
break;
case SceneBindingWidth:
val = mContext->prp_getSceneWidth();
break;
case SceneBindingHeight:
val = mContext->prp_getSceneHeight();
break;
case SceneBindingRangeMin:
val = mContext->prp_getSceneRangeMin();
break;
case SceneBindingRangeMax:
val = mContext->prp_getSceneRangeMax();
break;
}
return QJSValue(val);
}
return QJSValue::NullValue;
}

QJSValue SceneBinding::getJSValue(QJSEngine& e,
const qreal relFrame)
{
Q_UNUSED(relFrame);
return getJSValue(e);
}

FrameRange SceneBinding::identicalRelRange(const int absFrame)
{
Q_UNUSED(absFrame);
return FrameRange::EMINMAX;
}

FrameRange SceneBinding::nextNonUnaryIdenticalRelRange(const int absFrame)
{
Q_UNUSED(absFrame);
return FrameRange::EMINMAX;
}

QString SceneBinding::path() const
{
switch (mBindingType) {
case SceneBindingFps:
return "$scene.fps";
case SceneBindingWidth:
return "$scene.width";
case SceneBindingHeight:
return "$scene.height";
case SceneBindingRangeMin:
return "$scene.rangeMin";
case SceneBindingRangeMax:
return "$scene.rangeMax";
}
return QString();
}
58 changes: 58 additions & 0 deletions src/core/Expressions/scenebinding.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
#
# Friction - https://friction.graphics
#
# Copyright (c) Ole-André Rodlie and contributors
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# See 'README.md' for more information.
#
*/

#ifndef SCENE_BINDING_H
#define SCENE_BINDING_H

#include "propertybindingbase.h"

class CORE_EXPORT SceneBinding : public PropertyBindingBase
{
public:
enum SceneBindingType {
SceneBindingFps,
SceneBindingWidth,
SceneBindingHeight,
SceneBindingRangeMin,
SceneBindingRangeMax
};

SceneBinding(const Property* const context,
const SceneBindingType &binding);

static qsptr<SceneBinding> sCreate(const Property* const context,
const SceneBindingType &binding);

QJSValue getJSValue(QJSEngine& e);
QJSValue getJSValue(QJSEngine& e,
const qreal relFrame);

FrameRange identicalRelRange(const int absFrame);
FrameRange nextNonUnaryIdenticalRelRange(const int absFrame);
QString path() const;

private:
SceneBindingType mBindingType;
};

#endif // SCENE_BINDING_H
35 changes: 35 additions & 0 deletions src/core/Properties/property.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -401,3 +401,38 @@ void Property::prp_selectionChangeTriggered(const bool shiftPressed) {
mParentScene->addToSelectedProps(this);
}
}

qreal Property::prp_getSceneFPS() const {
if (mParentScene) {
return mParentScene->getFps();
}
return 0.0;
}

int Property::prp_getSceneWidth() const {
if (mParentScene) {
return mParentScene->getCanvasWidth();
}
return 0;
}

int Property::prp_getSceneHeight() const {
if (mParentScene) {
return mParentScene->getCanvasHeight();
}
return 0;
}

int Property::prp_getSceneRangeMin() const {
if (mParentScene) {
return mParentScene->getMinFrame();
}
return 0;
}

int Property::prp_getSceneRangeMax() const {
if (mParentScene) {
return mParentScene->getMaxFrame();
}
return 0;
}
5 changes: 5 additions & 0 deletions src/core/Properties/property.h
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,11 @@ class CORE_EXPORT Property : public SingleWidgetTarget {
static QString prp_sFixName(const QString &name);
static bool prp_sValidateName(const QString& name,
QString* error = nullptr);
qreal prp_getSceneFPS() const;
int prp_getSceneWidth() const;
int prp_getSceneHeight() const;
int prp_getSceneRangeMin() const;
int prp_getSceneRangeMax() const;
protected:
void setPointsHandler(const stdsptr<PointsHandler>& handler);

Expand Down