Skip to content

Commit

Permalink
Operator overloading for lua wrappers
Browse files Browse the repository at this point in the history
[CI BUILD]
  • Loading branch information
andybak committed Oct 8, 2023
1 parent f51a2f9 commit 6cabf25
Show file tree
Hide file tree
Showing 20 changed files with 136 additions and 57 deletions.
4 changes: 4 additions & 0 deletions Assets/Resources/LuaModules/__autocomplete.lua
Expand Up @@ -1452,6 +1452,10 @@ function Rotation:SlerpUnclamped(a, b, t) end
---@return Rotation # The rotation that represents applying both rotations in turn
function Rotation:Multiply(other) end

---@param other Rotation The rotation to compare
---@return boolean # true if this rotation is not equal to the specified rotation; otherwise, false
function Rotation:NotEquals(other) end



---@class Selection
Expand Down
Expand Up @@ -25,7 +25,7 @@ function Main()
if framesSinceChange > framesBetweenChanges then

-- A vector from the actual Brush.Position to the calculated one used for drawing
vector = Brush.position:Subtract(currentPosition)
vector = Brush.position - currentPosition

-- Store whether the vector is positive or negative in each axis
signs = Vector3:New(
Expand Down Expand Up @@ -62,8 +62,8 @@ function Main()
end

-- Shift the brush position based on the value calculated above
currentPosition = currentPosition:Add(vector)
currentPosition = currentPosition + vector

return Transform:New(currentPosition, currentRotation)

end
Expand Down
Expand Up @@ -19,7 +19,7 @@ function Main()
elseif Brush.triggerIsPressed then

-- Move the pointer in the direction we were facing when we pressed the trigger
currentPos = currentPos:Add(direction:Multiply(speed))
currentPos = currentPos + (direction * speed)
return Transform:New(currentPos, currentRotation)

end
Expand Down
4 changes: 2 additions & 2 deletions Assets/Resources/LuaScriptExamples/PointerScript.Loops.lua
Expand Up @@ -11,7 +11,7 @@ function Main()

--Move the pointer in a circular path around the actual brush position
angle = (App.time * speed) % 360
position = Vector2:PointOnCircle(angle):Multiply(radius):OnZ()
return Transform:New(position)
position2d = Vector2:PointOnCircle(angle) * radius
return Transform:New(position2d:OnZ())

end
Expand Up @@ -19,7 +19,7 @@ function Main()

--Similar to the LaserBeam PointerScript except we can change the direction during "flight"
direction = Brush.direction
currentPos = currentPos:Add(direction:Multiply(speed))
currentPos = currentPos + (direction * speed)
return Transform:New(currentPos, currentRotation)

end
Expand Down
Expand Up @@ -17,7 +17,7 @@ function Main()
v = Brush.distanceDrawn * vScale
p = Sphere(u,v)
return Transform:New(
initialPos:Add(p),
initialPos + p,
Rotation:LookRotation(p, Vector3.right)
)
end
Expand Down
4 changes: 2 additions & 2 deletions Assets/Resources/LuaScriptExamples/PointerScript.Spirals.lua
Expand Up @@ -13,8 +13,8 @@ function Main()

currentRadius = radius * Brush.timeSincePressed
angle = Brush.timeSincePressed * speed
position = Vector2:PointOnCircle(angle):Multiply(currentRadius):OnZ()
return Transform:New(position, Brush.rotation)
position2d = Vector2:PointOnCircle(angle) * currentRadius
return Transform:New(position2d:OnZ(), Brush.rotation)

end

Expand Down
11 changes: 6 additions & 5 deletions Assets/Resources/LuaScriptExamples/PointerScript.Wander.lua
Expand Up @@ -32,11 +32,12 @@ function Main()
end

-- Wandering path based on a noise field
currentPos = currentPos:Add(
speed * (-0.5 + Math:PerlinNoise(currentPos.y, currentPos.z)),
speed * (-0.5 + Math:PerlinNoise(currentPos.x, currentPos.z)),
speed * (-0.5 + Math:PerlinNoise(currentPos.x, currentPos.y))
)
currentPos = currentPos +
Vector3:New(
speed * (-0.5 + Math:PerlinNoise(currentPos.y, currentPos.z)),
speed * (-0.5 + Math:PerlinNoise(currentPos.x, currentPos.z)),
speed * (-0.5 + Math:PerlinNoise(currentPos.x, currentPos.y))
)

return Transform:New(currentPos, Brush.rotation)

Expand Down
Expand Up @@ -7,5 +7,5 @@ Parameters = {
}

function Main()
return Random.onUnitSphere:Multiply(amount * 0.1)
return Random.onUnitSphere * (amount * 0.1)
end
30 changes: 15 additions & 15 deletions Assets/Resources/LuaScriptExamples/SymmetryScript.Boids.lua
Expand Up @@ -28,18 +28,18 @@ function Boid:update(dt, boids)
local sep, align, coh = self:calculateForces(boids)

-- Calculate a force vector pointing towards the origin
local attractionToOrigin = Vector3.zero:Subtract(self.position)
local attractionToOrigin = Vector3.zero - self.position
attractionToOrigin = attractionToOrigin.normalized

self.velocity = self.velocity:Add(sep:Multiply(separationForce))
self.velocity = self.velocity:Add(align:Multiply(alignmentForce))
self.velocity = self.velocity:Add(coh:Multiply(cohesionForce))
self.velocity = self.velocity + (sep * separationForce)
self.velocity = self.velocity + (align * alignmentForce)
self.velocity = self.velocity + (coh * cohesionForce)

-- Apply the attraction to origin force
self.velocity = self.velocity:Add(attractionToOrigin:Multiply(originForce))
self.velocity = self.velocity + (attractionToOrigin * originForce)

self.velocity = self.velocity:ClampMagnitude(5)
self.position = self.position:Add(self.velocity:Multiply(dt))
self.position = self.position + (self.velocity * dt)
self.orientation = Rotation:LookRotation(self.velocity, Vector3.up)
end

Expand All @@ -51,21 +51,21 @@ function Boid:calculateForces(boids)

for _, other in ipairs(boids) do
if other ~= self then
local direction = self.position:Subtract(other.position)
local direction = self.position - other.position
local distanceSquared = direction.sqrMagnitude
if distanceSquared < 25 and distanceSquared > 0 then
separation = separation:Add(direction:Divide(distanceSquared))
alignment = alignment:Add(other.velocity)
cohesion = cohesion:Add(other.position)
separation = separation + (direction / distanceSquared)
alignment = alignment + other.velocity
cohesion = cohesion + other.position
count = count + 1
end
end
end

if count > 0 then
separation = separation:Divide(count)
alignment = alignment:Divide(count).normalized
cohesion = cohesion:Divide(count):Subtract(self.position).normalized
separation = separation / count
alignment = (alignment / count).normalized
cohesion = ((cohesion / count) - self.position).normalized
end

return separation, alignment, cohesion
Expand All @@ -87,8 +87,8 @@ function Main()
-- Initialize boids with random positions and velocities
boids = {}
for i = 1, copies do
local position = Random.insideUnitSphere:Multiply(0.25)
local velocity = Random.insideUnitSphere:Multiply(0.1)
local position = Random.insideUnitSphere * 0.25
local velocity = Random.insideUnitSphere * 0.1
boids[i] = Boid.new(position, velocity)
end
symmetryHueShift.generate(copies, initialHsv)
Expand Down
4 changes: 2 additions & 2 deletions Assets/Resources/LuaScriptExamples/SymmetryScript.Spin.lua
Expand Up @@ -13,8 +13,8 @@ function Main()
pointers = Path:New()
for i = 1.0, copies do
angle = (App.time * speed) + (360 * (i / copies))
position = Vector2:PointOnCircle(angle):Multiply(radius):OnZ()
pointers:Insert(Transform:New(position))
position2d = Vector2:PointOnCircle(angle) * radius
pointers:Insert(Transform:New(position:OnZ()))
end
return pointers
end
4 changes: 2 additions & 2 deletions Assets/Resources/LuaScriptExamples/SymmetryScript.ToGuide.lua
Expand Up @@ -23,9 +23,9 @@ function Main()
end

pointers = Path:New()
brushOffset = Brush.position:Subtract(brushInitialPosition)
brushOffset = Brush.position - brushInitialPosition
for i = 0.0, copies do
position = Vector3.Lerp(Symmetry.current.position, guide.position, i/copies):Add(brushOffset)
position = Vector3.Lerp(Symmetry.current.position, guide.position, i/copies) + brushOffset
pointers:Insert(position)
end
return pointers
Expand Down
Expand Up @@ -15,9 +15,9 @@ function Main()
radius = Tool.vector.magnitude
angle = 360.0 / sides
for a = 0, 360 - angle, angle do
position = Vector2:PointOnCircle(a):Multiply(radius):OnY()
position2d = Vector2:PointOnCircle(a) * radius
rotation = Rotation:New(0, -a, 0)
path.Insert(Transform:New(position, rotation, ((Math.pi * 0.5) / sides) * radius))
path.Insert(Transform:New(position2d:OnY(), rotation, ((Math.pi * 0.5) / sides) * radius))
end
path:TransformBy(Transform:New(Tool.startPoint.position, Tool.rotation));
CameraPath:FromPath(path, true)
Expand Down
16 changes: 8 additions & 8 deletions Assets/Resources/LuaScriptExamples/ToolScript.Cube.lua
Expand Up @@ -13,16 +13,16 @@ function createFace(center, normal, up)
local right = Vector3:Cross(normal, up)
local rotation = Rotation:LookRotation(normal, up)

local normal = normal:Multiply(inset)
local center = center:Add(normal)
local normal = normal * inset
local center = center + normal

local up = up:Multiply(1 - inset)
local right = right:Multiply(1 - inset)
local up = up * (1 - inset)
local right = right * (1 - inset)

local topLeft = (center:Add(up)):Subtract(right)
local topRight = (center:Add(up)):Add(right)
local bottomRight = (center:Subtract(up)):Add(right)
local bottomLeft = (center:Subtract(up)):Subtract(right)
local topLeft = center + up - right
local topRight = center + up + right
local bottomRight = center - up + right
local bottomLeft = center - up - right

local face = Path:New()
face:Insert(Transform:New(topLeft, rotation))
Expand Down
Expand Up @@ -24,9 +24,9 @@ function Main()
origin = Brush.position

if Random.value < amount then
randomOffset = Random.insideUnitSphere:Multiply(spread)
randomOffset = Random.insideUnitSphere * spread
return drawCube(
origin:Add(randomOffset),
origin + randomOffset,
Random.value * maxSize
)
end
Expand Down
20 changes: 18 additions & 2 deletions Assets/Scripts/API/Lua/Wrappers/ColorApiWrapper.cs
Expand Up @@ -12,8 +12,10 @@
// See the License for the specific language governing permissions and
// limitations under the License.

using System;
using MoonSharp.Interpreter;
using UnityEngine;
using Object = System.Object;

namespace TiltBrush
{
Expand Down Expand Up @@ -182,6 +184,13 @@ public Vector3 hsv

[LuaDocsDescription("The color yellow")]
public static Color yellow => Color.yellow;

// Operators

public static Color operator +(ColorApiWrapper a, ColorApiWrapper b) => a._Color + b._Color;
public static Color operator -(ColorApiWrapper a, ColorApiWrapper b) => a._Color - b._Color;
public static Color operator *(ColorApiWrapper a, ColorApiWrapper b) => a._Color * b._Color;
public static Color operator /(ColorApiWrapper a, float b) => a._Color / b;

[LuaDocsDescription("Adds the specified color to this color")]
[LuaDocsExample("newColor = color1:Add(color2)")]
Expand Down Expand Up @@ -235,7 +244,14 @@ public Vector3 hsv
[LuaDocsExample("if color1:Equals(color2) then print(\"colors are the same\") end")]
[LuaDocsParameter("other", "The color to compare")]
[LuaDocsReturnValue("true if this color is equal to the specified color; otherwise, false")]
public bool Equals(Color other) => _Color == other;
public bool Equals(ColorApiWrapper other) => Equals(other._Color);

public override bool Equals(Object obj)
{
var other = obj as ColorApiWrapper;
return other != null && _Color == other._Color;
}
public override int GetHashCode() => 0; // Always return 0. Dicts and HashSets will have to use Equals to compare

[LuaDocsDescription("Determines whether this color is equal to the specified RGB values")]
[LuaDocsExample("if color1:Equals(1, 0, 0) then print(\"the color is red\") end")]
Expand All @@ -249,7 +265,7 @@ public Vector3 hsv
[LuaDocsExample("if color1:NotEquals(color2) then print(\"colors are different\") end")]
[LuaDocsParameter("other", "The color to compare")]
[LuaDocsReturnValue("true if this color is not equal to the specified color; otherwise, false")]
public bool NotEquals(Color other) => _Color != other;
public bool NotEquals(ColorApiWrapper other) => !Equals(other);

[LuaDocsDescription("Determines whether this color is not equal to the specified RGB values")]
[LuaDocsExample("if color1:NotEquals(0, 1, 0) then print(\"color is not green\") end")]
Expand Down
27 changes: 25 additions & 2 deletions Assets/Scripts/API/Lua/Wrappers/RotationApiWrapper.cs
@@ -1,4 +1,3 @@
using System.Collections.Generic;
using MoonSharp.Interpreter;
using UnityEngine;

Expand Down Expand Up @@ -252,6 +251,9 @@ public Quaternion SetLookRotation(Vector3 view, Vector3 up)
public static Quaternion SlerpUnclamped(Quaternion a, Quaternion b, float t) => Quaternion.SlerpUnclamped(a, b, t);

// Operators

public static Quaternion operator *(RotationApiWrapper a, RotationApiWrapper b) => a._Quaternion * b._Quaternion;

[LuaDocsDescription(@"Combines two rotations")]
[LuaDocsExample(@"result = myRotation:Multiply(anotherRotation)")]
[LuaDocsParameter("other", "The other rotation")]
Expand All @@ -262,6 +264,27 @@ public Quaternion SetLookRotation(Vector3 view, Vector3 up)
[LuaDocsExample(@"if myRotation:Equals(otherRotation) then print(""Equal!"") end")]
[LuaDocsParameter("other", "The rotation to comapre to")]
[LuaDocsReturnValue(@"True if they are the same as each other")]
public bool Equals(Quaternion other) => _Quaternion == other;
public bool Equals(RotationApiWrapper other) => Equals(other._Quaternion);

[LuaDocsDescription("Determines whether this rotation is not equal to the specified rotation")]
[LuaDocsExample("if myRotation:NotEquals(rot2) then print(\"rotations are different\") end")]
[LuaDocsParameter("other", "The rotation to compare")]
[LuaDocsReturnValue("true if this rotation is not equal to the specified rotation; otherwise, false")]
public bool NotEquals(RotationApiWrapper other) => !Equals(other);

[LuaDocsDescription("Determines whether this rotation is equal to the specified xyz values")]
[LuaDocsExample("if myRotation:Equals(0, 90, 0) then print(\"90 degree turn to the right\") end")]
[LuaDocsParameter("x", "The x value to compare")]
[LuaDocsParameter("y", "The y value to compare")]
[LuaDocsParameter("z", "The z value to compare")]
[LuaDocsReturnValue("true if this rotation is equal to the specified xyz values; otherwise, false")]
public bool Equals(float x, float y, float z) => _Quaternion == Quaternion.Euler(x, y, z);

public override bool Equals(System.Object obj)
{
var other = obj as RotationApiWrapper;
return other != null && _Quaternion == other._Quaternion;
}
public override int GetHashCode() => 0; // Always return 0. Lookups will have to use Equals to compare
}
}
14 changes: 12 additions & 2 deletions Assets/Scripts/API/Lua/Wrappers/TransformApiWrapper.cs
Expand Up @@ -185,15 +185,25 @@ public float scale
public static TrTransform identity => TrTransform.identity;

// Operators

public static TrTransform operator *(TransformApiWrapper a, TransformApiWrapper b) => a._TrTransform * b._TrTransform;

[LuaDocsDescription(@"Combines another transform with this one (Does the same as ""TransformBy"")")]
[LuaDocsExample("newTransform = myTransform:Multiply(Transform.up)")]
[LuaDocsParameter("other", "The Transform to apply to this one")]
public TrTransform Multiply(TrTransform other) => _TrTransform * other;

[LuaDocsDescription("Is this transform equal to another?")]
[LuaDocsExample(@"if myTransform:Equals(Transform.up) then print(""Equal to Transform.up"")")]
[LuaDocsParameter("other", "The Transform to compare to this one")]
public bool Equals(TrTransform other) => _TrTransform == other;
public bool Equals(TransformApiWrapper other) => Equals(other._TrTransform);

public override bool Equals(System.Object obj)
{
var other = obj as TransformApiWrapper;
return other != null && _TrTransform == other._TrTransform;
}
public override int GetHashCode() => 0; // Always return 0. Lookups will have to use Equals to compare

[LuaDocsDescription("Interpolates between two transforms")]
[LuaDocsExample("newTransform = Transform:Lerp(transformA, transformB, 0.25)")]
Expand Down

0 comments on commit 6cabf25

Please sign in to comment.