Skip to content

Commit

Permalink
Portfolio Properties
Browse files Browse the repository at this point in the history
Fixed several small type conversion issues and added the ability to use
script libraries across multiple ObiScripts.
  • Loading branch information
KMullins-Xam committed Feb 17, 2017
1 parent 782ff0a commit aaf1e9b
Show file tree
Hide file tree
Showing 34 changed files with 265 additions and 65 deletions.
Binary file added Images/Intro19.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Images/Intro20.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion KimonoCore/DScript/ScriptEngine.Factor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ private ScriptVarLink Factor(ref bool execute)
}
if (_currentLexer.TokenType == ScriptLex.LexTypes.RFalse)
{
_currentLexer.Match(ScriptLex.LexTypes.RTrue);
_currentLexer.Match(ScriptLex.LexTypes.RFalse);
return new ScriptVarLink(new ScriptVar(0), null);
}
if (_currentLexer.TokenType == ScriptLex.LexTypes.RNull)
Expand Down
2 changes: 1 addition & 1 deletion KimonoCore/DScript/ScriptEngine.Statement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ private void Statement(ref bool execute)
else if (_currentLexer.TokenType == ScriptLex.LexTypes.RVar)
{
//creating variables
//TODO: make this less shit
//TODO: Clean up variable generation

_currentLexer.Match(ScriptLex.LexTypes.RVar);

Expand Down
47 changes: 46 additions & 1 deletion KimonoCore/DScript/ScriptEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,55 @@ public void Trace()
Root.Trace(0, null);
}

private List<string> IncludedLibraries = new List<string>();

private bool LibraryIncluded(string name)
{
// Scan list of included libraries
foreach (string library in IncludedLibraries)
{
// Found?
if (library == name) return true;
}

// No
return false;
}

public void Execute(String code)
{
ScriptLex oldLex = _currentLexer;
Stack<ScriptVar> oldScopes = _scopes;
Stack<ScriptVar> oldScopes = _scopes;

// Preprocess script and expand any libraries if needed
IncludedLibraries.Clear();
foreach (KimonoProperty property in ObiScriptPortfolio.Portfolio.Properties)
{
// Is this a script library?
if (property is KimonoPropertyLibrary)
{
// Yes, see if it has been included in the current script
var token = $"using {property.Name};";

// Included?
if (code.Contains(token))
{
// Yes, is the library already included?
if (LibraryIncluded(property.Name))
{
// Yes, just remove this call
code = code.Replace(token, "");
}
else
{
// No, expand the library into this script and
// mark library as included
code = code.Replace(token, property.ObiScript);
IncludedLibraries.Add(property.Name);
}
}
}
}

using (_currentLexer = new ScriptLex(code))
{
Expand Down
1 change: 1 addition & 0 deletions KimonoCore/KimonoCore.projitems
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@
<Compile Include="$(MSBuildThisFileDirectory)DScript\Utils.cs" />
<Compile Include="$(MSBuildThisFileDirectory)ObiScript\ObiScriptEngine.cs" />
<Compile Include="$(MSBuildThisFileDirectory)ObiScript\ObiScriptPortfolio.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Properties\KimonoPropertyLibrary.cs" />
</ItemGroup>
<ItemGroup>
<None Include="$(MSBuildThisFileDirectory)License.txt" />
Expand Down
16 changes: 15 additions & 1 deletion KimonoCore/KimonoPortfolio.cs
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,20 @@ public KimonoPropertyBoolean AddPropertyBoolean()
return property;
}

/// <summary>
/// Adds a new library property.
/// </summary>
/// <returns>The new `KimonoPropertyBoolean`.</returns>
public KimonoPropertyLibrary AddPropertyLibrary()
{
// Add property
var property = new KimonoPropertyLibrary();
AddNewProperty(property);

// Return new property
return property;
}

/// <summary>
/// Adds a new color property.
/// </summary>
Expand Down Expand Up @@ -1258,7 +1272,7 @@ public virtual string ToCode(CodeOutputOS outputOS, CodeOutputLanguage outputLan
}
break;
case CodeOutputLanguage.ObiScript:
// TODO: Add ObiScript output
sourceCode += "// Portfolios are not supported in ObiScript\n";
break;
}

Expand Down
2 changes: 1 addition & 1 deletion KimonoCore/KimonoSketch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1909,7 +1909,7 @@ public virtual string ToCode(CodeOutputOS outputOS, CodeOutputLanguage outputLan
}
break;
case CodeOutputLanguage.ObiScript:
// TODO: Add ObiScript output
sourceCode += "// Sketches are not supported in ObiScript\n";
break;
}

Expand Down
4 changes: 2 additions & 2 deletions KimonoCore/ObiScript/ObiScriptEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,12 @@ public static void Text(string text, object userData)
/// <returns>The boolean result from the script.</returns>
/// <param name="state">The boolean value to send to the property.</param>
/// <param name="userData">The `ScriptEngine` executing the script.</param>
public static void Boolean(bool state, object userData)
public static void Boolean(int state, object userData)
{
// Save result
EvaluationResult.Successful = true;
EvaluationResult.ErrorMessage = "";
EvaluationResult.Value = state;
EvaluationResult.Value = (state == 1);
}

/// <summary>
Expand Down
4 changes: 2 additions & 2 deletions KimonoCore/ObiScript/ObiScriptPortfolio.cs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ public static bool GetBoolean(string name, object userData)
/// <param name="name">The name of the property to set.</param>
/// <param name="value">The new value.</param>
/// <param name="userData">The `ScriptEngine` executing the script.</param>
public static void SetBoolean(string name, bool value, object userData)
public static void SetBoolean(string name, int value, object userData)
{
// Get requested property
var property = FindProperty(name);
Expand All @@ -140,7 +140,7 @@ public static void SetBoolean(string name, bool value, object userData)
{
// Yes, return value
var prop = property as KimonoPropertyBoolean;
prop.Value = value;
prop.Value = (value == 1);
}
}

Expand Down
88 changes: 88 additions & 0 deletions KimonoCore/Properties/KimonoPropertyLibrary.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
using System;
using SkiaSharp;

namespace KimonoCore
{
public class KimonoPropertyLibrary : KimonoProperty
{
#region Computed Properties
/// <summary>
/// Gets a value indicating whether this <see cref="T:KimonoCore.KimonoProperty"/> gets value from script.
/// </summary>
/// <value><c>true</c> if gets value from script; otherwise, <c>false</c>.</value>
public override bool GetsValueFromScript
{
get
{
return true;
}
}

/// <summary>
/// Gets a value indicating whether this <see cref="T:KimonoCore.KimonoPropertyColor"/> is obi script value.
/// </summary>
/// <value><c>true</c> if is obi script value; otherwise, <c>false</c>.</value>
public override bool IsObiScriptValue
{
get
{
return true;
}
}
#endregion

#region Constructors
public KimonoPropertyLibrary()
{
// Initialize
Name = "Script Library";
ObiScript = "// Add one or more ObiScript `function` definitions to this library,\n" +
"// then add `using library-name;` to the top of any other ObiScript to use these\n" +
"// functions inside of that script.";
}
#endregion

#region Public Methods
/// <summary>
/// Evaluate this instance by executing any attached Obi Script to get the new
/// value for the `KimonoProperty`.
/// </summary>
/// <returns>The result of the Obi Script execution as a `ObiScriptResult`.</returns>
public override ObiScriptResult Evaluate()
{
// Is there a script attached?
if (GetsValueFromScript)
{
// Execute the script to check for errors
ObiScriptEngine.Runtime.Execute(ObiScript);

}

// Return the result of executing the script
return ObiScriptEngine.EvaluationResult;
}
#endregion

#region Cloning
/// <summary>
/// Clone this instance.
/// </summary>
/// <returns>The clone.</returns>
public override KimonoProperty Clone()
{
// Make copy
var newProperty = new KimonoPropertyLibrary()
{
Name = this.Name,
Usage = this.Usage,
IsObiScriptValue = this.IsObiScriptValue,
GetsValueFromScript = this.GetsValueFromScript,
ObiScript = this.ObiScript
};

// Return clone
return newProperty;
}
#endregion
}
}
4 changes: 2 additions & 2 deletions KimonoCore/Properties/KimonoPropertyNumber.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@ public override ObiScriptResult Evaluate()
if (ObiScriptEngine.EvaluationResult.Successful)
{
// Was the right type returned?
if (ObiScriptEngine.EvaluationResult.Value is double || ObiScriptEngine.EvaluationResult.Value is int)
if (ObiScriptEngine.EvaluationResult.Value is double)
{
// Yes, save it
Value = (float)ObiScriptEngine.EvaluationResult.Value;
Value = float.Parse(ObiScriptEngine.EvaluationResult.Value.ToString());
}
else
{
Expand Down
2 changes: 1 addition & 1 deletion KimonoCore/Shapes/KimonoShapeArrow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -578,7 +578,7 @@ public override string ToCode(CodeOutputOS outputOS, CodeOutputLanguage outputLa
sourceCode += ToCSharp(outputLibrary);
break;
case CodeOutputLanguage.ObiScript:
// TODO: Add ObiScript output
sourceCode += "// Shapes are not supported in ObiScript\n";
break;
}

Expand Down
2 changes: 1 addition & 1 deletion KimonoCore/Shapes/KimonoShapeBezier.cs
Original file line number Diff line number Diff line change
Expand Up @@ -713,7 +713,7 @@ public override string ToCode(CodeOutputOS outputOS, CodeOutputLanguage outputLa
sourceCode += ToCSharp(outputLibrary);
break;
case CodeOutputLanguage.ObiScript:
// TODO: Add ObiScript output
sourceCode += "// Shapes are not supported in ObiScript\n";
break;
}

Expand Down
2 changes: 1 addition & 1 deletion KimonoCore/Shapes/KimonoShapeGroup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1905,7 +1905,7 @@ public override string ToCode(CodeOutputOS outputOS, CodeOutputLanguage outputLa
sourceCode += ToCSharp(outputLibrary);
break;
case CodeOutputLanguage.ObiScript:
// TODO: Add ObiScript output
sourceCode += "// Shapes are not supported in ObiScript\n";
break;
}

Expand Down
2 changes: 1 addition & 1 deletion KimonoCore/Shapes/KimonoShapeLine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ public override string ToCode(CodeOutputOS outputOS, CodeOutputLanguage outputLa
sourceCode += ToCSharp(outputLibrary);
break;
case CodeOutputLanguage.ObiScript:
// TODO: Add ObiScript output
sourceCode += "// Shapes are not supported in ObiScript\n";
break;
}

Expand Down
2 changes: 1 addition & 1 deletion KimonoCore/Shapes/KimonoShapeOval.cs
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ public override string ToCode(CodeOutputOS outputOS, CodeOutputLanguage outputLa
sourceCode += ToCSharp(outputLibrary);
break;
case CodeOutputLanguage.ObiScript:
// TODO: Add ObiScript output
sourceCode += "// Shapes are not supported in ObiScript\n";
break;
}

Expand Down
2 changes: 1 addition & 1 deletion KimonoCore/Shapes/KimonoShapePolygon.cs
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ public override string ToCode(CodeOutputOS outputOS, CodeOutputLanguage outputLa
sourceCode += ToCSharp(outputLibrary);
break;
case CodeOutputLanguage.ObiScript:
// TODO: Add ObiScript output
sourceCode += "// Shapes are not supported in ObiScript\n";
break;
}

Expand Down
2 changes: 1 addition & 1 deletion KimonoCore/Shapes/KimonoShapeRect.cs
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ public override string ToCode(CodeOutputOS outputOS, CodeOutputLanguage outputLa
sourceCode += ToCSharp(outputLibrary);
break;
case CodeOutputLanguage.ObiScript:
// TODO: Add ObiScript output
sourceCode += "// Shapes are not supported in ObiScript\n";
break;
}

Expand Down
2 changes: 1 addition & 1 deletion KimonoCore/Shapes/KimonoShapeRoundRect.cs
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ public override string ToCode(CodeOutputOS outputOS, CodeOutputLanguage outputLa
sourceCode += ToCSharp(outputLibrary);
break;
case CodeOutputLanguage.ObiScript:
// TODO: Add ObiScript output
sourceCode += "// Shapes are not supported in ObiScript\n";
break;
}

Expand Down
2 changes: 1 addition & 1 deletion KimonoCore/Shapes/KimonoShapeStar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -563,7 +563,7 @@ public override string ToCode(CodeOutputOS outputOS, CodeOutputLanguage outputLa
sourceCode += ToCSharp(outputLibrary);
break;
case CodeOutputLanguage.ObiScript:
// TODO: Add ObiScript output
sourceCode += "// Shapes are not supported in ObiScript\n";
break;
}

Expand Down
2 changes: 1 addition & 1 deletion KimonoCore/Shapes/KimonoShapeText.cs
Original file line number Diff line number Diff line change
Expand Up @@ -735,7 +735,7 @@ public override string ToCode(CodeOutputOS outputOS, CodeOutputLanguage outputLa
sourceCode += ToCSharp(outputLibrary);
break;
case CodeOutputLanguage.ObiScript:
// TODO: Add ObiScript output
sourceCode += "// Shapes are not supported in ObiScript\n";
break;
}

Expand Down
2 changes: 1 addition & 1 deletion KimonoCore/Shapes/KimonoShapeTriangle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ public override string ToCode(CodeOutputOS outputOS, CodeOutputLanguage outputLa
sourceCode += ToCSharp(outputLibrary);
break;
case CodeOutputLanguage.ObiScript:
// TODO: Add ObiScript output
sourceCode += "// Shapes are not supported in ObiScript\n";
break;
}

Expand Down
2 changes: 1 addition & 1 deletion KimonoCore/Shapes/KimonoShapeVector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -605,7 +605,7 @@ public override string ToCode(CodeOutputOS outputOS, CodeOutputLanguage outputLa
sourceCode += ToCSharp(outputLibrary);
break;
case CodeOutputLanguage.ObiScript:
// TODO: Add ObiScript output
sourceCode += "// Shapes are not supported in ObiScript\n";
break;
}

Expand Down
4 changes: 2 additions & 2 deletions KimonoCore/Styles/KimonoColor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -629,10 +629,10 @@ public virtual string ToCode(CodeOutputOS outputOS, CodeOutputLanguage outputLan
switch (outputLanguage)
{
case CodeOutputLanguage.CSharp:
sourceCode += $"var {KimonoCodeGenerator.MakeElementName(Name)} = {KimonoCodeGenerator.ColorToCode(outputLibrary,Color)};";
sourceCode += $"var {KimonoCodeGenerator.MakeElementName(Name)} = {KimonoCodeGenerator.ColorToCode(outputLibrary,Color)};\n";
break;
case CodeOutputLanguage.ObiScript:
sourceCode += $"#Color({Color.Red}, {Color.Green}, {Color.Blue}, {Color.Alpha})";
sourceCode += $"Return.Color(\"{Name}\");\n";
break;
}

Expand Down
2 changes: 1 addition & 1 deletion KimonoCore/Styles/KimonoGradient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -789,7 +789,7 @@ public virtual string ToObiScript()

// Generate code
sourceCode = "// Obi Script Selector for this gradient\n";
sourceCode += $"[gradient:{Name}]";
sourceCode += $"Return.Gradient(\"{Name}\");\n";

// Return results
return sourceCode;
Expand Down
2 changes: 1 addition & 1 deletion KimonoCore/Styles/KimonoStyle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1350,7 +1350,7 @@ public virtual string ToObiScript()

// Generate code
sourceCode = "// Obi Script Selector for this style\n";
sourceCode += $"[style:{Name}]";
sourceCode += $"Return.Style(\"{Name}\");\n";

// Return results
return sourceCode;
Expand Down
Loading

0 comments on commit aaf1e9b

Please sign in to comment.