Skip to content

Commit

Permalink
more ScriptObject stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
scheffle committed Aug 18, 2024
1 parent be830a8 commit 0f8b35b
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 44 deletions.
6 changes: 3 additions & 3 deletions vstgui/uidescription-scripting/detail/drawable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ void JavaScriptDrawable::onDraw (CDrawContext* context, const CRect& rect, const

auto rectVar = makeScriptRect (rect);
auto scriptRoot = scriptContext->getRoot ();
ScriptAddChildScoped scs (*scriptRoot, "view", scriptObject->getVar ());
ScriptAddChildScoped scs2 (*scriptRoot, "context", drawContext.getVar ());
ScriptAddChildScoped scs3 (*scriptRoot, "rect", rectVar.getVar ());
ScriptAddChildScoped scs (*scriptRoot, "view", *scriptObject);
ScriptAddChildScoped scs2 (*scriptRoot, "context", drawContext);
ScriptAddChildScoped scs3 (*scriptRoot, "rect", rectVar);
scriptContext->evalScript ("view.draw(context, rect);"sv);

drawContext.setDrawContext (nullptr, nullptr);
Expand Down
6 changes: 3 additions & 3 deletions vstgui/uidescription-scripting/detail/drawcontextobject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -468,7 +468,7 @@ struct DrawContextObject::Impl
if (auto path = owned (context->createRoundRectGraphicsPath (rect, radius)))
{
GraphicsPathScriptObject obj (path);
var->setReturnVar (obj.getVar ());
var->setReturnVar (obj);
}
}

Expand All @@ -479,7 +479,7 @@ struct DrawContextObject::Impl
if (auto path = owned (context->createGraphicsPath ()))
{
GraphicsPathScriptObject obj (path);
var->setReturnVar (obj.getVar ());
var->setReturnVar (obj);
}
}

Expand All @@ -495,7 +495,7 @@ struct DrawContextObject::Impl
CGradient::create (startColorPosition, endColorPosition, startColor, endColor)))
{
GradientScriptObject obj (gradient, uiDesc);
var->setReturnVar (obj.getVar ());
var->setReturnVar (obj);
}
}

Expand Down
59 changes: 54 additions & 5 deletions vstgui/uidescription-scripting/detail/scriptobject.h
Original file line number Diff line number Diff line change
Expand Up @@ -115,53 +115,102 @@ struct ScriptObject
}
}

CScriptVar* getVar () const { return scriptVar; }
operator CScriptVar* () const
{
validate ();
return scriptVar;
}
CScriptVar* operator->() const
{
validate ();
return scriptVar;
}

CScriptVar* getVar () const
{
validate ();
return scriptVar;
}
CScriptVar* take ()
{
auto v = scriptVar;
scriptVar = nullptr;
return v;
}

void addChild (std::string_view name, ScriptObject&& obj)
{
scriptVar->addChild (name, obj.getVar ());
validate ();
scriptVar->addChild (name, obj);
}
void addChild (std::string_view name, double d)
{
validate ();
scriptVar->addChild (name, new CScriptVar (d));
}
void addChild (std::string_view name, int64_t i)
{
validate ();
scriptVar->addChild (name, new CScriptVar (i));
}
void addChild (std::string_view name, int32_t i)
{
validate ();
scriptVar->addChild (name, new CScriptVar (static_cast<int64_t> (i)));
}
void addChild (std::string_view name, std::string_view value)
{
validate ();
scriptVar->addChild (name, new CScriptVar (TJS::string {value.data (), value.size ()}));
}
void addFunc (std::string_view name, std::function<void (CScriptVar*)>&& func)
{
validate ();
scriptVar->addChild (name, createJSFunction (std::move (func)));
}
void addFunc (std::string_view name, std::function<void (CScriptVar*)>&& func,
const std::initializer_list<std::string_view>& argNames)
{
validate ();
scriptVar->addChild (name, createJSFunction (std::move (func), argNames));
}

void addFunc (std::string_view name, std::string_view code)
{
validate ();
auto funcVar = new TJS::CScriptVar (TJS::TINYJS_BLANK_DATA, TJS::SCRIPTVAR_FUNCTION);
funcVar->setFunctionScript (code);
scriptVar->addChild (name, funcVar);
}

using OnDestroyFunc = std::function<void (CScriptVar*)>;
void setOnDestroy (OnDestroyFunc&& f)
{
validate ();
scriptVar->setLifeTimeObserver (OnDestroy::make (std::move (f)));
}

protected:
CScriptVar* scriptVar {nullptr};
struct OnDestroy : TJS::IScriptVarLifeTimeObserver
{
static OnDestroy* make (OnDestroyFunc f) { return new OnDestroy (std::move (f)); }

private:
OnDestroy (OnDestroyFunc f) : func (std::move (f)) {}

void onDestroy (CScriptVar* var)
{
func (var);
delete this;
}
OnDestroyFunc func;
};

void validate () const
{
if (scriptVar == nullptr)
scriptVar = TJS::owning (new CScriptVar ());
}

mutable CScriptVar* scriptVar {nullptr};
};

//------------------------------------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion vstgui/uidescription-scripting/detail/viewscriptobject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ ViewScriptObject::ViewScriptObject (CView* view, IViewScriptObjectContext* conte
addFunc ("getBounds"sv, [view] (CScriptVar* var) {
auto bounds = view->getViewSize ();
bounds.originize ();
var->setReturnVar (makeScriptRect (bounds).getVar ());
var->setReturnVar (makeScriptRect (bounds));
});
addFunc ("getParent"sv, [view, context] (CScriptVar* var) {
auto parentView = view->getParentView ();
Expand Down
53 changes: 21 additions & 32 deletions vstgui/uidescription-scripting/uiscripting.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,37 +57,27 @@ class ScriptContext : public IScriptContextInternal
};

//------------------------------------------------------------------------
struct TimerScriptObject : CScriptVar
struct TimerScriptObject : ScriptObject
{
template<typename Proc>
TimerScriptObject (uint64_t fireTime, CScriptVar* _callback, Proc timerProc)
: CScriptVar (TINYJS_BLANK_DATA, SCRIPTVAR_OBJECT), callback (owning (_callback))
{
timer = makeOwned<CVSTGUITimer> (
[timerProc, this] (auto) {
if (!timerProc (callback))
auto cb = owning (_callback);
auto t = makeOwned<CVSTGUITimer> (
[timerProc, cb] (auto timer) {
if (!timerProc (cb))
timer->stop ();
},
static_cast<uint32_t> (fireTime), false);
addChild ("invalid"sv, createJSFunction ([this] (auto) { timer = nullptr; }));
addChild ("start"sv, createJSFunction ([this] (auto) {
if (timer)
timer->start ();
}));
addChild ("stop"sv, createJSFunction ([this] (auto) {
if (timer)
timer->stop ();
}));
}
~TimerScriptObject () noexcept
{
timer = nullptr;
callback->release ();
scriptVar->setCustomData (t);
addFunc ("invalid"sv, [t] (auto v) mutable {
t = nullptr;
v->setCustomData (nullptr);
});
addFunc ("start"sv, [t] (auto) { t->start (); });
addFunc ("stop"sv, [t] (auto) { t->stop (); });
setOnDestroy ([cb] (auto) { cb->release (); });
}

private:
SharedPointer<CVSTGUITimer> timer;
CScriptVar* callback;
};

//------------------------------------------------------------------------
Expand Down Expand Up @@ -128,7 +118,7 @@ struct ScriptContext::Impl : ViewListenerAdapter,
registerFunctions (jsContext.get ());
registerMathFunctions (jsContext.get ());

jsContext->getRoot ()->addChild ("uiDesc"sv, uiDescObject.getVar ());
jsContext->getRoot ()->addChild ("uiDesc"sv, uiDescObject);

jsContext->addNative (
"function createTimer(context, fireTime, callback)"sv, [this] (CScriptVar* var) {
Expand All @@ -144,15 +134,14 @@ struct ScriptContext::Impl : ViewListenerAdapter,
throw CScriptException (
"Expect function as second parameter on timer creation");
}
auto timerObj = new TimerScriptObject (
auto timerObj = TimerScriptObject (
fireTime->getInt (), callback->deepCopy (), [this, context] (auto callback) {
using namespace ScriptingInternal;
ScriptAddChildScoped scs (*jsContext->getRoot (), "timerContext", context);
return evalScript (callback, "timerCallback (timerContext); ",
"timerCallback");
});
var->setReturnVar (owning (timerObj));
timerObj->release ();
var->setReturnVar (timerObj);
});
jsContext->addNative (
"function iterateSubViews(view, context, callback)"sv, [this] (CScriptVar* var) {
Expand Down Expand Up @@ -320,7 +309,7 @@ struct ScriptContext::Impl : ViewListenerAdapter,
callWhenScriptHasFunction (view, "onSizeChanged"sv, [&] (auto This, auto& obj) {
auto newSize = view->getViewSize ();
ScriptObject newSizeObject = ScriptingInternal::makeScriptRect (newSize);
ScriptAddChildScoped scs (*jsContext->getRoot (), "newSize", newSizeObject.getVar ());
ScriptAddChildScoped scs (*jsContext->getRoot (), "newSize", newSizeObject);
static constexpr auto script = R"(view.onSizeChanged(view, newSize);)"sv;
This->evalScript (obj->getVar (), script);
});
Expand Down Expand Up @@ -385,7 +374,7 @@ struct ScriptContext::Impl : ViewListenerAdapter,

void checkEventConsumed (ScriptObject& obj, Event& event)
{
if (auto consume = obj.getVar ()->findChild ("consumed"))
if (auto consume = obj->findChild ("consumed"))
{
if (consume->getVar ()->isInt () && consume->getVar ()->getInt ())
event.consumed = true;
Expand All @@ -395,7 +384,7 @@ struct ScriptContext::Impl : ViewListenerAdapter,
void callEventFunction (CScriptVar* var, Event& event, std::string_view script) noexcept
{
auto scriptEvent = ScriptingInternal::makeScriptEvent (event);
ScriptAddChildScoped scs (*jsContext->getRoot (), "event", scriptEvent.getVar ());
ScriptAddChildScoped scs (*jsContext->getRoot (), "event", scriptEvent);
evalScript (var, script);
checkEventConsumed (scriptEvent, event);
}
Expand Down Expand Up @@ -477,8 +466,8 @@ struct ScriptContext::Impl : ViewListenerAdapter,
{
callWhenScriptHasFunction (control, "onValueChanged"sv, [&] (auto This, auto& obj) {
ScriptObject controlValue;
controlValue.getVar ()->setDouble (control->getValue ());
ScriptAddChildScoped scs (*jsContext->getRoot (), "value", controlValue.getVar ());
controlValue->setDouble (control->getValue ());
ScriptAddChildScoped scs (*jsContext->getRoot (), "value", controlValue);
static constexpr auto script = R"(view.onValueChanged(view, value);)"sv;
This->evalScript (obj->getVar (), script);
});
Expand Down

0 comments on commit 0f8b35b

Please sign in to comment.