Skip to content

Commit

Permalink
add functions for customizing focus drawing in script views
Browse files Browse the repository at this point in the history
  • Loading branch information
scheffle committed Oct 19, 2024
1 parent 218b495 commit 9b5805a
Show file tree
Hide file tree
Showing 8 changed files with 116 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,19 @@ view.draw = function(context, dirtyRect) {
context.drawGraphicsPath(roundRect, "stroked");
};

view.drawFocusOnTop = function () { return false; };

view.getFocusPath = function (path, focusWidth) {
var bounds = view.getBounds();
path.addRoundRect(bounds, 4);
bounds.left -= focusWidth;
bounds.right += focusWidth;
bounds.top -= focusWidth;
bounds.bottom += focusWidth;
path.addRoundRect(bounds, 4);
return true;
};

view.onMouseDown = function(view, event) {
if(!event.mouseButtons.left)
return;
Expand Down
63 changes: 63 additions & 0 deletions vstgui/uidescription-scripting/detail/drawable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@

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

//------------------------------------------------------------------------
Expand Down Expand Up @@ -62,6 +65,48 @@ void JavaScriptDrawable::onDraw (CDrawContext* context, const CRect& rect, const
//------------------------------------------------------------------------
void JavaScriptDrawable::setup (ViewScriptObject* inObject) { scriptObject = inObject; }

//------------------------------------------------------------------------
bool JavaScriptDrawable::onDrawFocusOnTop ()
{
auto scriptContext = scriptObject->getContext ();
if (!scriptContext)
return false;

auto scriptRoot = scriptContext->getRoot ();
ScriptAddChildScoped scs (*scriptRoot, "view", *scriptObject);
auto boolResult = scriptContext->evalScript ("view.drawFocusOnTop();"sv);
return boolResult->isNumeric () ? boolResult->getInt () : false;
}

//------------------------------------------------------------------------
bool JavaScriptDrawable::onGetFocusPath (CGraphicsPath& outPath, CCoord focusWidth,
const CRect& viewSize)
{
if (auto scriptContext = scriptObject->getContext ())
{
auto scriptRoot = scriptContext->getRoot ();
auto path = makeOwned<CGraphicsPath> (outPath);
ScriptObject focusWidthVar;
focusWidthVar->setDouble (focusWidth);
ScriptAddChildScoped scs (*scriptRoot, "view", *scriptObject);
ScriptAddChildScoped scs2 (*scriptRoot, "path", makeGraphicsPathScriptObject (path));
ScriptAddChildScoped scs3 (*scriptRoot, "focusWidth", focusWidthVar);
auto boolResult = scriptContext->evalScript ("view.getFocusPath(path, focusWidth);"sv);
if (boolResult->isNumeric ())
{
if (boolResult->getInt () == 1)
{
CGraphicsTransform tm;
tm.translate (viewSize.left, viewSize.top);
outPath.addPath (*path, &tm);
return true;
}
return false;
}
}
return false;
}

//------------------------------------------------------------------------
//------------------------------------------------------------------------
//------------------------------------------------------------------------
Expand All @@ -70,6 +115,15 @@ void JavaScriptDrawableView::drawRect (CDrawContext* context, const CRect& rect)
onDraw (context, rect, getViewSize ());
}

//------------------------------------------------------------------------
bool JavaScriptDrawableView::drawFocusOnTop () { return onDrawFocusOnTop (); }

//------------------------------------------------------------------------
bool JavaScriptDrawableView::getFocusPath (CGraphicsPath& outPath)
{
return onGetFocusPath (outPath, getFrame ()->getFocusWidth (), getViewSize ());
}

//------------------------------------------------------------------------
//------------------------------------------------------------------------
//------------------------------------------------------------------------
Expand All @@ -81,6 +135,15 @@ void JavaScriptDrawableControl::drawRect (CDrawContext* context, const CRect& re
onDraw (context, rect, getViewSize ());
}

//------------------------------------------------------------------------
bool JavaScriptDrawableControl::drawFocusOnTop () { return onDrawFocusOnTop (); }

//------------------------------------------------------------------------
bool JavaScriptDrawableControl::getFocusPath (CGraphicsPath& outPath)
{
return onGetFocusPath (outPath, getFrame ()->getFocusWidth (), getViewSize ());
}

//------------------------------------------------------------------------
//------------------------------------------------------------------------
//------------------------------------------------------------------------
Expand Down
7 changes: 7 additions & 0 deletions vstgui/uidescription-scripting/detail/drawable.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ namespace ScriptingInternal {
struct JavaScriptDrawable
{
void onDraw (CDrawContext* context, const CRect& rect, const CRect& viewSize);
bool onDrawFocusOnTop ();
bool onGetFocusPath (CGraphicsPath& outPath, CCoord focusWidth, const CRect& viewSize);

void setup (ViewScriptObject* object);

Expand All @@ -28,11 +30,14 @@ struct JavaScriptDrawable

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

void drawRect (CDrawContext* context, const CRect& rect) override;
bool drawFocusOnTop () override;
bool getFocusPath (CGraphicsPath& outPath) override;
};

//------------------------------------------------------------------------
Expand All @@ -43,6 +48,8 @@ struct JavaScriptDrawableControl : CControl,

void draw (CDrawContext* pContext) override;
void drawRect (CDrawContext* context, const CRect& rect) override;
bool drawFocusOnTop () override;
bool getFocusPath (CGraphicsPath& outPath) override;

CLASS_METHODS_NOCOPY (JavaScriptDrawableControl, CControl);
};
Expand Down
6 changes: 6 additions & 0 deletions vstgui/uidescription-scripting/detail/drawcontextobject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,12 @@ struct GraphicsPathScriptObject : ScriptObject
}
};

//------------------------------------------------------------------------
ScriptObject makeGraphicsPathScriptObject (const SharedPointer<CGraphicsPath>& p)
{
return GraphicsPathScriptObject (p);
}

//------------------------------------------------------------------------
struct DrawContextObject::Impl
{
Expand Down
1 change: 1 addition & 0 deletions vstgui/uidescription-scripting/detail/drawcontextobject.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ namespace VSTGUI {
namespace ScriptingInternal {

TJS::CScriptVar* makeTransformMatrixObject ();
ScriptObject makeGraphicsPathScriptObject (const SharedPointer<CGraphicsPath>& p);

//------------------------------------------------------------------------
struct DrawContextObject : ScriptObject,
Expand Down
2 changes: 1 addition & 1 deletion vstgui/uidescription-scripting/detail/viewscriptobject.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ struct IViewScriptObjectContext
virtual IUIDescription* getUIDescription () const = 0;
virtual ViewScriptObject* addView (CView* view) = 0;
virtual ViewScriptMap::iterator removeView (CView* view) = 0;
virtual bool evalScript (std::string_view script) noexcept = 0;
virtual ScriptObject evalScript (std::string_view script) noexcept = 0;
virtual TJS::CScriptVar* getRoot () const = 0;
};

Expand Down
13 changes: 7 additions & 6 deletions vstgui/uidescription-scripting/uiscripting.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ struct ScriptContext::Impl : ViewListenerAdapter,
control->unregisterControlListener (this);
}

bool evalScript (std::string_view script) noexcept override
ScriptObject evalScript (std::string_view script) noexcept override
{
try
{
Expand All @@ -258,6 +258,7 @@ struct ScriptContext::Impl : ViewListenerAdapter,
DebugPrint ("%s\n", result.getVar ()->getString ().data ());
#endif
}
return result.getVar ();
}
catch (const CScriptException& exc)
{
Expand All @@ -266,16 +267,16 @@ struct ScriptContext::Impl : ViewListenerAdapter,
#endif
if (onScriptException)
onScriptException (exc.text);
return false;
return {};
}
return true;
return {};
}

bool evalScript (CScriptVar* object, std::string_view script,
const std::string& objectName = "view") noexcept
ScriptObject evalScript (CScriptVar* object, std::string_view script,
const std::string& objectName = "view") noexcept
{
if (!object || script.empty ())
return false;
return {};
vstgui_assert (object->getRefs () > 0);
object->addRef ();
ScriptAddChildScoped scs (*jsContext->getRoot (), objectName, object);
Expand Down
18 changes: 18 additions & 0 deletions vstgui/uidescription-scripting/uiscripting.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,24 @@ view.draw = function(drawContext, dirtyRect) {
The [drawContext](#the-drawcontext-object) parameter is the object where you call its method to draw things into the view
and the dirtyRect parameter contains the rectangle that needs to be drawn.

Additionally the following two functions can be implemented to provide custom focus drawing:
```js
view.drawFocusOnTop = function () { return false; };

view.getFocusPath = function(path, focusWidth) {
var bounds = view.getBounds();
path.addRoundRect (bounds, 4);
bounds.left -= focusWidth;
bounds.right += focusWidth;
bounds.top -= focusWidth;
bounds.bottom += focusWidth;
path.addRoundRect (bounds, 4);
return true;
};
```

See the C++ IFocusDrawing interface for a description of these methods.

### The JavaScriptDrawableControl

You can add a `JavaScriptDrawableControl` from the view types to your view hierarchy to create a
Expand Down

0 comments on commit 9b5805a

Please sign in to comment.