Skip to content

Commit

Permalink
split up file and move stuff into detail folder
Browse files Browse the repository at this point in the history
  • Loading branch information
scheffle committed Feb 4, 2024
1 parent 347b9b6 commit a93c129
Show file tree
Hide file tree
Showing 11 changed files with 1,223 additions and 1,003 deletions.
9 changes: 9 additions & 0 deletions vstgui/uidescription-scripting/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,15 @@ set(SCRIPTING_SOURCES
tiny-js/TinyJS_MathFunctions.h
tiny-js/TinyJS.cpp
tiny-js/TinyJS.h
detail/scriptobject.h
detail/uidescscriptobject.h
detail/converters.h
detail/drawcontextobject.cpp
detail/drawcontextobject.h
detail/drawable.cpp
detail/drawable.h
detail/viewscriptobject.cpp
detail/viewscriptobject.h
uiscripting.cpp
uiscripting.h
uiscripting.md
Expand Down
125 changes: 125 additions & 0 deletions vstgui/uidescription-scripting/detail/converters.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
// This file is part of VSTGUI. It is subject to the license terms
// in the LICENSE file found in the top-level directory of this
// distribution and at http://github.com/steinbergmedia/vstgui/LICENSE

#pragma once

#include "scriptobject.h"
#include "../../lib/vstguifwd.h"
#include "../../uidescription/uidescriptionfwd.h"
#include "../../lib/events.h"
#include "../../lib/crect.h"

//------------------------------------------------------------------------
namespace VSTGUI {
namespace ScriptingInternal {

//------------------------------------------------------------------------
inline ScriptObject makeScriptRect (const CRect& rect)
{
using namespace std::literals;
ScriptObject obj;
obj.addChild ("left"sv, rect.left);
obj.addChild ("top"sv, rect.top);
obj.addChild ("right"sv, rect.right);
obj.addChild ("bottom"sv, rect.bottom);
return obj;
}

//------------------------------------------------------------------------
inline ScriptObject makeScriptPoint (const CPoint& point)
{
using namespace std::literals;
ScriptObject obj;
obj.addChild ("x"sv, point.x);
obj.addChild ("y"sv, point.y);
return obj;
}

//------------------------------------------------------------------------
inline CPoint fromScriptPoint (TJS::CScriptVar& var)
{
using namespace std::literals;
CPoint result {};
if (auto xVar = var.findChild ("x"sv))
result.x = xVar->getVar ()->getDouble ();
else
throw TJS::CScriptException ("Not a point object, missing 'x' member");
if (auto yVar = var.findChild ("y"sv))
result.y = yVar->getVar ()->getDouble ();
else
throw TJS::CScriptException ("Not a point object, missing 'y' member");
return result;
}

//------------------------------------------------------------------------
inline CRect fromScriptRect (TJS::CScriptVar& var)
{
using namespace std::literals;
CRect result {};
auto leftVar = var.findChild ("left"sv);
auto topVar = var.findChild ("top"sv);
auto rightVar = var.findChild ("right"sv);
auto bottomVar = var.findChild ("bottom"sv);
if (!leftVar || !topVar || !rightVar || !bottomVar)
throw TJS::CScriptException ("Expecting a rect object here");
result.left = leftVar->getVar ()->getDouble ();
result.top = topVar->getVar ()->getDouble ();
result.right = rightVar->getVar ()->getDouble ();
result.bottom = bottomVar->getVar ()->getDouble ();
return result;
}

//------------------------------------------------------------------------
inline ScriptObject makeScriptEvent (const Event& event)
{
using namespace std::literals;
ScriptObject obj;
if (auto modifierEvent = asModifierEvent (event))
{
ScriptObject mod;
mod.addChild ("shift"sv, modifierEvent->modifiers.has (ModifierKey::Shift));
mod.addChild ("alt"sv, modifierEvent->modifiers.has (ModifierKey::Alt));
mod.addChild ("control"sv, modifierEvent->modifiers.has (ModifierKey::Control));
mod.addChild ("super"sv, modifierEvent->modifiers.has (ModifierKey::Super));
obj.addChild ("modifiers"sv, std::move (mod));
}
if (auto mouseEvent = asMousePositionEvent (event))
{
obj.addChild ("mousePosition"sv, makeScriptPoint (mouseEvent->mousePosition));
}
if (auto mouseEvent = asMouseEvent (event))
{
ScriptObject buttons;
buttons.addChild ("left"sv, mouseEvent->buttonState.has (MouseButton::Left));
buttons.addChild ("right"sv, mouseEvent->buttonState.has (MouseButton::Right));
buttons.addChild ("middle"sv, mouseEvent->buttonState.has (MouseButton::Middle));
obj.addChild ("mouseButtons"sv, std::move (buttons));
}
if (event.type == EventType::MouseWheel)
{
const auto& wheelEvent = castMouseWheelEvent (event);
ScriptObject wheel;
wheel.addChild ("deltaX"sv, wheelEvent.deltaX);
wheel.addChild ("deltaY"sv, wheelEvent.deltaY);
wheel.addChild (
"directionInvertedFromDevice"sv,
wheelEvent.flags & MouseWheelEvent::Flags::DirectionInvertedFromDevice ? true : false);
wheel.addChild ("preciceDelta"sv,
wheelEvent.flags & MouseWheelEvent::Flags::PreciseDeltas ? true : false);
obj.addChild ("mouseWheel"sv, std::move (wheel));
}
if (auto keyEvent = asKeyboardEvent (event))
{
ScriptObject key;
key.addChild ("character"sv, static_cast<int> (keyEvent->character));
key.addChild ("virtual"sv, static_cast<int> (keyEvent->virt));
key.addChild ("isRepeat"sv, keyEvent->isRepeat);
}
obj.addChild ("consume"sv, 0);
return obj;
}

//------------------------------------------------------------------------
} // ScriptingInternal
} // VSTGUI
125 changes: 125 additions & 0 deletions vstgui/uidescription-scripting/detail/drawable.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
// This file is part of VSTGUI. It is subject to the license terms
// in the LICENSE file found in the top-level directory of this
// distribution and at http://github.com/steinbergmedia/vstgui/LICENSE

#include "drawable.h"
#include "converters.h"
#include "../../lib/cdrawcontext.h"
#include "../../uidescription/detail/uiviewcreatorattributes.h"

//------------------------------------------------------------------------
namespace VSTGUI {
namespace ScriptingInternal {
using namespace std::literals;
using namespace TJS;

//------------------------------------------------------------------------
//------------------------------------------------------------------------
//------------------------------------------------------------------------
void JavaScriptDrawable::onDraw (CDrawContext* context, const CRect& rect, const CRect& viewSize)
{
if (!scriptObject)
{
auto dashLength = std::round ((viewSize.getWidth () * 2 + viewSize.getHeight () * 2) / 40.);
CLineStyle ls (CLineStyle::kLineCapButt, CLineStyle::kLineJoinMiter, 0,
{dashLength, dashLength});

auto lineWidth = 1.;
auto size = viewSize;
size.inset (lineWidth / 2., lineWidth / 2.);
context->setLineStyle (ls);
context->setLineWidth (lineWidth);
context->setFrameColor (kBlackCColor);
context->drawRect (size, kDrawStroked);

ls.setDashPhase (dashLength * lineWidth);
context->setLineStyle (ls);
context->setFrameColor (kWhiteCColor);
context->drawRect (size, kDrawStroked);
return;
}
auto scriptContext = scriptObject->getContext ();
if (!scriptContext)
return;
context->saveGlobalState ();

drawContext.setDrawContext (context, scriptContext->getUIDescription ());

CDrawContext::Transform tm (*context, CGraphicsTransform ().translate (viewSize.getTopLeft ()));

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

drawContext.setDrawContext (nullptr, nullptr);

context->restoreGlobalState ();
}

//------------------------------------------------------------------------
void JavaScriptDrawable::setup (ViewScriptObject* inObject) { scriptObject = inObject; }

//------------------------------------------------------------------------
//------------------------------------------------------------------------
//------------------------------------------------------------------------
void JavaScriptDrawableView::drawRect (CDrawContext* context, const CRect& rect)
{
onDraw (context, rect, getViewSize ());
}

//------------------------------------------------------------------------
//------------------------------------------------------------------------
//------------------------------------------------------------------------
void JavaScriptDrawableControl::draw (CDrawContext* context) { drawRect (context, getViewSize ()); }

//------------------------------------------------------------------------
void JavaScriptDrawableControl::drawRect (CDrawContext* context, const CRect& rect)
{
onDraw (context, rect, getViewSize ());
}

//------------------------------------------------------------------------
//------------------------------------------------------------------------
//------------------------------------------------------------------------
IdStringPtr JavaScriptDrawableViewCreator::getViewName () const { return "JavaScriptDrawableView"; }

//------------------------------------------------------------------------
IdStringPtr JavaScriptDrawableViewCreator::getBaseViewName () const
{
return UIViewCreator::kCView;
}

//------------------------------------------------------------------------
CView* JavaScriptDrawableViewCreator::create (const UIAttributes& attributes,
const IUIDescription* description) const
{
return new JavaScriptDrawableView (CRect ());
}

//------------------------------------------------------------------------
//------------------------------------------------------------------------
//------------------------------------------------------------------------
IdStringPtr JavaScriptDrawableControlCreator::getViewName () const
{
return "JavaScriptDrawableControl";
}

//------------------------------------------------------------------------
IdStringPtr JavaScriptDrawableControlCreator::getBaseViewName () const
{
return UIViewCreator::kCControl;
}

//------------------------------------------------------------------------
CView* JavaScriptDrawableControlCreator::create (const UIAttributes& attributes,
const IUIDescription* description) const
{
return new JavaScriptDrawableControl (CRect ());
}

//------------------------------------------------------------------------
} // ScriptingInternal
} // VSTGUI
70 changes: 70 additions & 0 deletions vstgui/uidescription-scripting/detail/drawable.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// This file is part of VSTGUI. It is subject to the license terms
// in the LICENSE file found in the top-level directory of this
// distribution and at http://github.com/steinbergmedia/vstgui/LICENSE

#pragma once

#include "viewscriptobject.h"
#include "drawcontextobject.h"
#include "../../lib/cview.h"
#include "../../lib/controls/ccontrol.h"
#include "../../uidescription/iviewcreator.h"

//------------------------------------------------------------------------
namespace VSTGUI {
namespace ScriptingInternal {

//------------------------------------------------------------------------
struct JavaScriptDrawable
{
void onDraw (CDrawContext* context, const CRect& rect, const CRect& viewSize);

void setup (ViewScriptObject* object);

private:
ViewScriptObject* scriptObject {nullptr};
DrawContextObject drawContext;
};

//------------------------------------------------------------------------
struct JavaScriptDrawableView : CView,
JavaScriptDrawable
{
using CView::CView;

void drawRect (CDrawContext* context, const CRect& rect) override;
};

//------------------------------------------------------------------------
struct JavaScriptDrawableControl : CControl,
JavaScriptDrawable
{
using CControl::CControl;

void draw (CDrawContext* pContext) override;
void drawRect (CDrawContext* context, const CRect& rect) override;

CLASS_METHODS_NOCOPY (JavaScriptDrawableControl, CControl);
};

//------------------------------------------------------------------------
struct JavaScriptDrawableViewCreator : ViewCreatorAdapter
{
IdStringPtr getViewName () const override;
IdStringPtr getBaseViewName () const override;
CView* create (const UIAttributes& attributes,
const IUIDescription* description) const override;
};

//------------------------------------------------------------------------
struct JavaScriptDrawableControlCreator : ViewCreatorAdapter
{
IdStringPtr getViewName () const override;
IdStringPtr getBaseViewName () const override;
CView* create (const UIAttributes& attributes,
const IUIDescription* description) const override;
};

//------------------------------------------------------------------------
} // ScriptingInternal
} // VSTGUI
Loading

0 comments on commit a93c129

Please sign in to comment.