Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Plugin scripting #699

Open
wants to merge 896 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 250 commits
Commits
Show all changes
896 commits
Select commit Hold shift + click to select a range
aae42d1
Merge remote-tracking branch 'upstream/main' into experiments/moonsharp
andybak Aug 15, 2023
dfca920
Use ordered dither for opacity
andybak Aug 16, 2023
006a5e4
Fix clipping logic
andybak Aug 16, 2023
b3c0d20
Update completion logic for other shaders as well
andybak Aug 16, 2023
bd9ccd7
Pseudo-opacity for opaque (and hdr where needed) brushes
andybak Aug 17, 2023
88347eb
Allow GetTagFilteredBrushList to be called with tag lists as parameters
andybak Aug 17, 2023
1079bcc
Another brush shader (this one is outside the main folder)
andybak Aug 17, 2023
6302cb4
Brush list API improvements
andybak Aug 17, 2023
09ad1fc
API tweak. Remove broken example (for now)
andybak Aug 18, 2023
f4fc2ad
[CI BUILD]
andybak Aug 18, 2023
8ec8a03
Merge branch 'main' into experiments/moonsharp
andybak Aug 23, 2023
71d68c9
Merge branch 'main' into experiments/moonsharp
andybak Aug 23, 2023
5571025
Don't create a new group for every API stroke
andybak Aug 27, 2023
dafcbf3
Merge remote-tracking branch 'upstream/main' into experiments/moonsharp
andybak Aug 30, 2023
ba390f4
Fix the manifest post-merge
andybak Aug 30, 2023
070cbdc
WIP depth capture
andybak Aug 31, 2023
fefc882
Merge branch 'experiments/moonsharp' of https://github.com/IxxyXR/ope…
andybak Aug 31, 2023
c325564
Refactor to reduce code duplication. Other small improvements.
andybak Aug 31, 2023
46d640f
WIP: export layer xforms (via groups at present)
andybak Aug 31, 2023
1839edf
WIP depth camera mode UI
andybak Sep 3, 2023
42888e5
Enable depth camera mode on mobile and desktop
andybak Sep 3, 2023
67bd93b
Correct pointer rotation property for scripts.
andybak Sep 3, 2023
8266b9b
Fix multimirror pointer colors
andybak Sep 3, 2023
d5c2c88
Switch to LuaLS format for the autocomplete. Other API fixes and tweaks.
andybak Sep 5, 2023
2aac526
Remove animation scripts until we fix save/load and export
andybak Sep 5, 2023
cc2923c
Update meta files
andybak Sep 5, 2023
2802522
Use LocalPose for Canvas transforms. Remove experimental CanvasTransf…
andybak Sep 6, 2023
6437851
Merge remote-tracking branch 'upstream/main' into experiments/moonsharp
andybak Sep 7, 2023
6196773
Post-merge fix
andybak Sep 7, 2023
c06f055
API fixes to respect correct canvas transforms
andybak Sep 7, 2023
1eb8b9a
Merge remote-tracking branch 'upstream/main' into experiments/moonsharp
andybak Sep 7, 2023
a512f67
Merge remote-tracking branch 'upstream/main' into experiments/moonsharp
andybak Sep 12, 2023
ca6b4f2
Deselect widgets via API
andybak Sep 12, 2023
ee5e9fc
Remove unused default param
andybak Sep 12, 2023
84d1c94
Refactor out common logic for choosing a canvas to deselect to.
andybak Sep 12, 2023
3c5568c
The endpoint docs were inaccurate
andybak Sep 12, 2023
12d51b6
SelectAll for layer
andybak Sep 12, 2023
cd54b96
SelectAll and Invert should apply to the active canvas
andybak Sep 12, 2023
2885848
Select/Deselect for StrokeList
andybak Sep 12, 2023
e6d0a51
More missing Deselect api methods
andybak Sep 12, 2023
2cc8b3f
SelectAll doesn't belong on Selection object
andybak Sep 12, 2023
ce5903f
Fix an oversight
andybak Sep 12, 2023
7c20e28
Always supply a canvas to SelectAll and Invert
andybak Sep 12, 2023
e02ebc0
SelectionWidget axis locks should be based on selection canvas not ac…
andybak Sep 12, 2023
089df60
Update __autocomplete.lua
andybak Sep 12, 2023
a92b3eb
Do something sensible if canvas is null
andybak Sep 12, 2023
3a7830e
Docs and autocomplete generation fixes
andybak Sep 19, 2023
34cd64c
Merge remote-tracking branch 'upstream/main' into experiments/moonsharp
andybak Sep 19, 2023
f41c065
Fix examples scripts and update docs
andybak Sep 19, 2023
631f7bc
Merge remote-tracking branch 'upstream/main' into experiments/moonsharp
andybak Sep 30, 2023
1bdda3d
API docs update
andybak Oct 1, 2023
a497c42
fix autocomplete syntax for return values
andybak Oct 1, 2023
a3c37d0
Some example script fixes
andybak Oct 1, 2023
607785f
Hide pseudo-private method _SubdivideSegments
andybak Oct 3, 2023
2605761
Lerping for transforms
andybak Oct 3, 2023
d80585c
(Untested) settings for brush position, rotation and pressure
andybak Oct 3, 2023
02e59c5
Update lua autocomplete
andybak Oct 3, 2023
c972f19
dotnet-format
andybak Oct 3, 2023
eaf8ef0
Allow position and rotation constructors to accept vector3 and rotati…
andybak Oct 3, 2023
5bef563
Allow symmetry and tool plugins to return lua arrays
andybak Oct 3, 2023
31587b4
Simplify the example
andybak Oct 3, 2023
9ea247a
dotnet-format
andybak Oct 3, 2023
95fdab2
make inverse a property. Use "Position" for example snapshot code
andybak Oct 5, 2023
a0475b2
Small docs tweak
andybak Oct 5, 2023
f51a2f9
dotnet format
andybak Oct 5, 2023
6cabf25
Operator overloading for lua wrappers
andybak Oct 8, 2023
bbe4147
dotnet-format
andybak Oct 8, 2023
d952a5b
Fix some broken scripts
andybak Oct 8, 2023
2a803e6
Remove some buggy scripts
andybak Oct 8, 2023
e9b9d15
Fix typo
andybak Oct 8, 2023
f36610e
Fix camera path not respecting scene/canvas transforms
andybak Oct 10, 2023
41e3108
Remove clockwise and anticlockwise properties of rotation - they were…
andybak Oct 10, 2023
5a7b0b3
Update example scripts now anti and clockwise properties are removed
andybak Oct 10, 2023
f500784
Some extra overloads for API methods
andybak Oct 10, 2023
321e745
Update autocomplete
andybak Oct 10, 2023
f5d1047
Fix default argument for svg draw path. Fix svg object case in lua docs
andybak Oct 14, 2023
3f1a1f0
Allow identity to be used as a default parameter in other places
andybak Oct 14, 2023
1cc811b
Fix spiral example plugin
andybak Oct 14, 2023
aeffac8
dotnet-format
andybak Oct 14, 2023
35d59c5
Merge remote-tracking branch 'upstream/main' into experiments/moonsharp
andybak Oct 25, 2023
285c241
Update UnityXRInputAction.cs
andybak Oct 26, 2023
87a64e3
Merge branch 'main' into experiments/moonsharp
andybak Nov 7, 2023
b519368
Fixes for API docs and autocomplete
andybak Nov 7, 2023
d68c1a4
Merge branch 'experiments/moonsharp' of https://github.com/ixxyxr/ope…
andybak Nov 21, 2023
0463cd8
Merge remote-tracking branch 'upstream/main' into experiments/moonsharp
andybak Nov 21, 2023
fba21da
Merge remote-tracking branch 'upstream/main' into experiments/moonsharp
andybak Nov 23, 2023
81f328d
[CI BUILD]
andybak Nov 23, 2023
3a0a585
Merge remote-tracking branch 'upstream/main' into experiments/moonsharp
andybak Nov 24, 2023
c41535c
dotnet-format
andybak Nov 24, 2023
61be36e
Merge remote-tracking branch 'upstream/main' into experiments/moonsharp
andybak Nov 27, 2023
0d7eae8
Excessive amount of try/catch to pin down a bug on android builds
andybak Nov 27, 2023
06fd2c8
Merge remote-tracking branch 'upstream/main' into experiments/moonsharp
andybak Nov 27, 2023
f5f258a
Quick debug test to see which panels are loading on Quest
andybak Nov 27, 2023
b34476c
More debug info and a small refactor
andybak Nov 28, 2023
1b71e30
Use our fork of moonsharp
andybak Nov 28, 2023
dd9fa91
Revert "Quick debug test to see which panels are loading on Quest"
andybak Nov 28, 2023
1c72866
Log to the Controller Console to avoid messing with logcat
andybak Nov 28, 2023
4e618ed
Merge remote-tracking branch 'upstream/main' into experiments/moonsharp
andybak Nov 29, 2023
501ce63
More debug code
andybak Nov 29, 2023
6d9ef1b
Revert "More debug code"
andybak Dec 1, 2023
003a2e9
Change stripping level
andybak Dec 1, 2023
06d2fb7
[CI BUILD]
andybak Dec 1, 2023
dc8b4ef
Remove the logging now code stripping issue is fixed.
andybak Dec 2, 2023
f04efc4
Merge remote-tracking branch 'upstream/main' into experiments/moonsharp
andybak Dec 2, 2023
6cbedc1
Merge remote-tracking branch 'upstream/main' into experiments/moonsharp
andybak Dec 5, 2023
ba25622
[CI BUILD]
andybak Dec 5, 2023
ed09461
Use a regular select multiple for background scripts
andybak Dec 6, 2023
f5b9b33
Fix http API for environments (broken by localization)
andybak Dec 6, 2023
2566c26
Fix some shader compilation errors
andybak Dec 6, 2023
635499a
Fix Leaky Pen on Quest
andybak Dec 6, 2023
c61a671
Make StandardSingleSided more consistent with StandardDoubleSided
andybak Dec 6, 2023
ecd7a8d
Merge remote-tracking branch 'upstream/main' into experiments/moonsharp
andybak Dec 7, 2023
eef540e
Merge remote-tracking branch 'upstream/main' into experiments/moonsharp
andybak Dec 7, 2023
14828ae
Fix another shader error
andybak Dec 7, 2023
5cd1c13
[CI BUILD]
andybak Dec 7, 2023
1fb57c5
Merge remote-tracking branch 'upstream/main' into experiments/moonsharp
andybak Dec 13, 2023
2103362
[CI BUILD]
andybak Dec 13, 2023
ceccc52
Merge remote-tracking branch 'upstream/main' into experiments/moonsharp
andybak Dec 20, 2023
a116084
Typo crept in last merge
andybak Dec 20, 2023
f6c13c1
Merge remote-tracking branch 'upstream/main' into experiments/moonsharp
andybak Jan 1, 2024
cab4a3b
[CI BUILD]
andybak Jan 2, 2024
f6c8e66
Switch to "Parameters.foo" syntax in plugins
andybak Jan 4, 2024
99fe604
Small refactor for efficiency
andybak Jan 4, 2024
e1bc3d9
small autocomplete update
andybak Jan 4, 2024
7d19797
Tool should be a top level class to appear in autocomplete
andybak Jan 4, 2024
de18f7e
Fix Tool autocomplete docs not generating
andybak Jan 4, 2024
49c426c
hard code Moonsharp builtins into autocomplete. Fix type for Tool.vector
andybak Jan 4, 2024
ba0f0c7
Make autocomplete happy
andybak Jan 4, 2024
d90e8d2
Shader fix
andybak Jan 4, 2024
29a8160
Gridfollow rotation fix. Better defaults
andybak Jan 4, 2024
0ffb9ad
Support a shorter shorthand for generating a T transform
andybak Jan 4, 2024
91abcb8
Fix symmetry widget transform api. Don't store an undo
andybak Jan 4, 2024
ba9ce34
Example script fix
andybak Jan 4, 2024
ff79583
Update __autocomplete.lua
andybak Jan 4, 2024
b749f3e
Docs and example script tweaks
andybak Jan 5, 2024
8d7bc50
dotnet-format
andybak Jan 5, 2024
9724c6f
Round slider values in the widget
andybak Jan 5, 2024
8198108
Fix symmetryhueshift include for recent changes
andybak Jan 5, 2024
80fb06f
Revert "Round slider values in the widget"
andybak Jan 6, 2024
440716d
Maintain float value internally but send int out to subscribers
andybak Jan 6, 2024
44cac39
Example script fixes
andybak Jan 6, 2024
aa29bc4
Fix ducklings
andybak Jan 6, 2024
047212a
Incremental GC
andybak Jan 6, 2024
925c2d9
Color widget. Refactor popups to allow popups to open other popups.
andybak Jan 8, 2024
4a98f04
More script widget types (WIP)
andybak Jan 10, 2024
8d7887d
Update scripts to use new widgets
andybak Jan 10, 2024
bbdd4ec
Fix paging popup with dynamic items
andybak Jan 12, 2024
5bb91a5
Update example script to use new widgets.
andybak Jan 12, 2024
3077bf9
Fewer items per page to stop overlap with nav buttons
andybak Jan 12, 2024
efc1473
Fix Scripts Panel collider and centering
andybak Jan 12, 2024
0267cf1
Bigger script parameter popup
andybak Jan 12, 2024
34f7137
Small cosmetic and docs tweaks
andybak Jan 14, 2024
5ee5f6e
Matrix and Vector4 wrappers
andybak Jan 16, 2024
efb46b5
API support for getting matrices from multimirror
andybak Jan 16, 2024
28bfa29
API docs fix
andybak Jan 16, 2024
99bc967
Register the Vector4 and Matrix types
andybak Jan 16, 2024
925df56
Return MatrixList
andybak Jan 16, 2024
477d312
Update __autocomplete.lua
andybak Jan 16, 2024
de1231f
MatrixListWrapper should implement IPathApiWrapper
andybak Jan 17, 2024
2a39ae0
Handle matrix lists the same as any IPathApiWrapper instance
andybak Jan 17, 2024
054e219
Register MatrixList
andybak Jan 17, 2024
fad5984
Setters for position, rotation and scale on Matrix wrapper
andybak Jan 17, 2024
5ba4682
Setter for MatrixList indexer
andybak Jan 17, 2024
b8a255a
Symmetry scripts refactor and fix
andybak Jan 17, 2024
17b59c9
Update __autocomplete.lua
andybak Jan 17, 2024
2f8719e
Merge remote-tracking branch 'upstream/main' into experiments/moonsharp
andybak Jan 18, 2024
c917492
Remove some junk comments
andybak Jan 18, 2024
dbc8788
Post-merge fix
andybak Jan 18, 2024
64f962c
Setters for indexers
andybak Jan 28, 2024
947bc28
Merge remote-tracking branch 'upstream/main' into experiments/moonsharp
andybak Feb 28, 2024
d1327b3
dotnet-format
andybak Feb 28, 2024
7b142ee
Display image picker icons with correct aspect ratio
andybak Feb 28, 2024
b390bf7
Assign image button results correctly
andybak Feb 28, 2024
f3157ee
Merge remote-tracking branch 'upstream/main' into experiments/moonsharp
andybak Mar 5, 2024
278cada
Merge remote-tracking branch 'upstream/main' into experiments/moonsharp
andybak Mar 23, 2024
157a1ae
Merge remote-tracking branch 'upstream/main' into experiments/moonsharp
andybak Apr 3, 2024
7145bf5
Merge remote-tracking branch 'upstream/main' into experiments/moonsharp
andybak Apr 7, 2024
2b088b1
Post-merge panel fixes
andybak Apr 7, 2024
db6d69f
picker popup fixes
andybak Apr 12, 2024
7184f47
picker popup fixes
andybak Apr 12, 2024
26ef2a0
svg mesh import fix
andybak Apr 12, 2024
ce18925
Image api fixes
andybak Apr 12, 2024
569f30e
Minimal port of text tool - just enough to allow API creation of text…
andybak Apr 13, 2024
cbaf7a9
Add Open Type glyph loader
andybak Apr 13, 2024
d3e32d4
Update the vector graphic package
andybak Apr 14, 2024
48d09a9
Add various text utility methods
andybak Apr 14, 2024
46d4c05
formatting
andybak Apr 14, 2024
4aa88b9
Image extrusion (WIP)
andybak Apr 14, 2024
34a478e
Needed for image extrusion
andybak Apr 14, 2024
e0ef08f
Fixes for image and text widget changes
andybak Apr 14, 2024
3a7f442
"Save as" fixed (overwriting and file extensions)
andybak Apr 14, 2024
2fbe853
Http API endpoint for image extrusion (WIP)
andybak Apr 14, 2024
69f1e72
Move example usages into the endpoint attribute
andybak Apr 16, 2024
0bbacb2
Merge branch 'experiments/moonsharp' of https://github.com/ixxyxr/ope…
andybak Apr 16, 2024
b761664
Stub for text extrusion. Disable image extrusion for now as it doesn'…
andybak Apr 16, 2024
75793d6
Remove debug log
andybak Apr 16, 2024
e6db885
Change folder where open type fonts are looked for
andybak Apr 16, 2024
28cfff8
Refactor and simplify stroke drawing for Lua and Http APIs
andybak Apr 17, 2024
6e57852
Endpoint for drawing full SVG docs
andybak Apr 21, 2024
c673df0
API SVG fixes [CI BUILD]
andybak Apr 22, 2024
4597a97
Merge remote-tracking branch 'upstream/main' into experiments/moonsharp
andybak Apr 27, 2024
b942409
Merge remote-tracking branch 'upstream/main' into feature/plugin-scri…
andybak Apr 30, 2024
dd1dda2
Rename dither param to _Dissolve and ensure it's on all brush shaders
andybak May 1, 2024
68db335
Update example script to use _Dissolve
andybak May 1, 2024
54d63cd
Merge remote-tracking branch 'origin/main' into feature/plugin-scripting
andybak May 1, 2024
76747f8
Merge remote-tracking branch 'origin/main' into feature/plugin-scripting
andybak May 3, 2024
eb0a028
Post-merge fixes
andybak May 3, 2024
fe75f1a
Merge remote-tracking branch 'origin/main' into feature/plugin-scripting
andybak May 3, 2024
9fcbd31
Post-merge fix
andybak May 5, 2024
e8c5bb4
Merge remote-tracking branch 'origin/main' into feature/plugin-scripting
andybak May 19, 2024
82496b0
[CI BUILD]
andybak Jun 1, 2024
81df4e2
Merge remote-tracking branch 'origin/main' into feature/plugin-scripting
andybak Aug 15, 2024
5517311
Merge remote-tracking branch 'origin/main' into feature/plugin-scripting
andybak Sep 15, 2024
206ed65
Fix shaders (mostly the dissolve logic)
andybak Sep 15, 2024
3d781c1
Fix bubblewand dissolve
andybak Sep 15, 2024
4b20a56
Util to read current git branch
andybak Sep 25, 2024
ea10dba
Allow loading of bundled sketches with "tiltbrush://builtin/" prefix
andybak Sep 25, 2024
f051a8a
Tweak location and content of profiling summary
andybak Sep 25, 2024
bad1dad
Merge remote-tracking branch 'origin/feature/profile-bundled-sketches…
andybak Sep 25, 2024
b49ac7e
[CI BUILD]
andybak Oct 4, 2024
bca9dbf
Stroke debug command
andybak Oct 5, 2024
368e12c
Fix spelling
andybak Oct 5, 2024
64641a3
Add another fps bucket to profiler
andybak Oct 5, 2024
8b7ab35
Update Zapbox SDK (#770)
andybak Sep 26, 2024
6d2dbbf
gzip config.vdf before storing as a secret (#771)
mikeage Sep 26, 2024
48a3f78
Fix speling
andybak Sep 25, 2024
371e9e1
Include build string in profile summary. Add a 40fps bucket
andybak Sep 25, 2024
2f8db0c
Use mult compile flags for scripted shader properties
andybak Oct 5, 2024
4a3db8e
Merge remote-tracking branch 'origin/main' into feature/plugin-scripting
andybak Oct 6, 2024
3e69c0f
Post-merge package changes
andybak Oct 6, 2024
f370722
Merge remote-tracking branch 'origin/main' into feature/plugin-scripting
andybak Oct 9, 2024
9f38097
Set the default sdk back to openxr. Assorted unity changes
andybak Oct 9, 2024
0b05029
Fix Ballistic Missile script
andybak Oct 9, 2024
da61275
Refactor the api interface for controller actions
andybak Oct 10, 2024
bc08adc
Better up vector for scripted tool
andybak Oct 10, 2024
76abfeb
Scripts panel shouldn't start fixed
andybak Dec 14, 2024
c1cc8a3
Unity being unity
andybak Dec 14, 2024
b5e1ff0
Merge remote-tracking branch 'origin/main' into feature/plugin-scripting
andybak Dec 14, 2024
6d45c06
Yet another Rider update
andybak Dec 14, 2024
f360aea
Make the console window a prefab
andybak Dec 14, 2024
e91b4dd
Basic ram logging for Android
andybak Dec 14, 2024
d2b49fd
ram log font size and bounding box
andybak Dec 14, 2024
e2f64fc
Merge remote-tracking branch 'origin/feature/ram-logging' into featur…
andybak Dec 14, 2024
ada8e60
Post-merge fix
andybak Dec 14, 2024
5916392
Lazy init of Lua stuff
andybak Dec 17, 2024
324762c
Make sure "New Sketch" cleans up lua stuff properly
andybak Dec 17, 2024
42e7c21
Merge remote-tracking branch 'origin/main' into feature/plugin-scripting
andybak Dec 17, 2024
30c1628
Delete test.jpg
andybak Dec 18, 2024
bc14a39
Remove commented out update method
andybak Dec 18, 2024
8b775da
Delete .gitkeep
andybak Dec 18, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
10 changes: 9 additions & 1 deletion Assets/Editor/BrushLister.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,15 @@ public static string getBrushRowString(BrushDescriptor brush, bool experimental)
{
manifest = "Experimental";
}
return $"{brush.Description}\t{brush.m_AudioReactive}\t{prefabName}\t{materialName}\t{shaderName}\t{brushScripts}\t{experimental}\t{brush.m_SupersededBy}\t{manifest}";

string semantics = "";
if (brush.m_BrushPrefab != null)
{
var script = brush.m_BrushPrefab.GetComponentInChildren<BaseBrushScript>();
var layout = script.GetVertexLayout(brush);
semantics = $"{layout.texcoord0.semantic}\t{layout.texcoord1.semantic}\t{layout.texcoord2.semantic}";
}
return $"{brush.m_DurableName}\t{brush.m_AudioReactive}\t{prefabName}\t{materialName}\t{shaderName}\t{brushScripts}\t{experimental}\t{brush.m_SupersededBy}\t{manifest}\t{semantics}";
}

public static string sniffBrushScript(BrushDescriptor brush)
Expand Down
15 changes: 15 additions & 0 deletions Assets/Editor/LuaAssetImporter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System.IO;
using UnityEditor.AssetImporters;
using UnityEngine;

[ScriptedImporter(1, "lua")]
public class LuaAssetImporter : ScriptedImporter
{
// Allow loading of .lua files as TextAssets
public override void OnImportAsset(AssetImportContext ctx)
{
TextAsset subAsset = new TextAsset(File.ReadAllText(ctx.assetPath));
ctx.AddObjectToAsset("text", subAsset);
ctx.SetMainObject(subAsset);
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

127 changes: 127 additions & 0 deletions Assets/Editor/LuaDocsGenerator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
// Copyright 2023 The Tilt Brush Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

using System.Collections.Generic;
using System.IO;
using System.Text;
using MoonSharp.Interpreter;
using UnityEditor;
using UnityEngine;

namespace TiltBrush
{

public class LuaDocsGenerator : Editor
{
[MenuItem("Open Brush/API/Generate Lua Docs")]
static void GenerateDocs()
{
if (!Application.isPlaying)
{
Debug.LogError("You can only run this whilst in Play Mode");
return;
}

Script script = new Script();

// Initializing this list triggers the docs generation via RegisterApiClasses
LuaDocsRegistration.ApiDocClasses = new List<LuaDocsClass>();
LuaManager.Instance.RegisterApiClasses(script);

// List wrappers aren't places in the script's namespace but we do want to register them
// so we can generate docs for them.
LuaDocsRegistration.RegisterForDocs(typeof(CameraPathListApiWrapper), false);
LuaDocsRegistration.RegisterForDocs(typeof(EnvironmentListApiWrapper), false);
LuaDocsRegistration.RegisterForDocs(typeof(GuideListApiWrapper), false);
LuaDocsRegistration.RegisterForDocs(typeof(ImageListApiWrapper), false);
LuaDocsRegistration.RegisterForDocs(typeof(LayerListApiWrapper), false);
LuaDocsRegistration.RegisterForDocs(typeof(ModelListApiWrapper), false);
LuaDocsRegistration.RegisterForDocs(typeof(VideoListApiWrapper), false);
LuaDocsRegistration.RegisterForDocs(typeof(StrokeListApiWrapper), false);

// Manually add some entries that aren't added the standard way
var transformProp = new LuaDocsType { PrimitiveType = LuaDocsPrimitiveType.UserData, CustomTypeName = "Transform" };
var vector3Prop = new LuaDocsType { PrimitiveType = LuaDocsPrimitiveType.UserData, CustomTypeName = "Vector3" };
var rotationProp = new LuaDocsType { PrimitiveType = LuaDocsPrimitiveType.UserData, CustomTypeName = "Rotation" };
var toolApiDocClass = new LuaDocsClass
{
Name = "Tool",
Methods = new List<LuaDocsMethod>(),
EnumValues = new List<LuaDocsEnumValue>(),
Description = "A class to interact with Scripted Tools",
IsTopLevelClass = true,
Properties = new List<LuaDocsProperty>
{
new() {Name=LuaNames.ToolScriptStartPoint, PropertyType = transformProp, Description = "The position and orientation of the point where the trigger was pressed"},
new() {Name=LuaNames.ToolScriptEndPoint, PropertyType = transformProp, Description = "The position and orientation of the point where the trigger was released"},
new() {Name=LuaNames.ToolScriptVector, PropertyType = vector3Prop, Description = "The vector from startPoint to endPoint"},
new() {Name=LuaNames.ToolScriptRotation, PropertyType = rotationProp, Description = "The rotation from startPoint to endPoint"},
}
};
LuaDocsRegistration.ApiDocClasses.Add(toolApiDocClass);

// JSON docs if needed
// var json = JsonConvert.SerializeObject(LuaDocsRegistration.ApiDocClasses, Formatting.Indented);
// File.WriteAllText(Path.Join(docsPath, "docs.json"), json);

// Generate __autocomplete.lua
var autocomplete = new StringBuilder();
autocomplete.Append("---@meta\n\n");
string autocompleteFilePath = Path.Combine("Assets/Resources/LuaModules", "__autocomplete.lua");
foreach (var klass in LuaDocsRegistration.ApiDocClasses)
{
// Only top level classes are included in autocomplete
// This excludes all ListWrapper classes which can't be instantiated directly
// And don't have any useful static members
if (!klass.IsTopLevelClass) continue;
autocomplete.Append(klass.AutocompleteSerialize());
}

// Some hardcoded Moonsharp built-ins
autocomplete.Append(@"
---@class json
json = {}
---@param jsonString string The JSON string to parse
---@return table # A table representing the parsed JSON
function json:parse(jsonString) end

---@param table table The table to serialize to JSON
---@return string # The JSON representation of the table
function json:serialize(table) end

---@return jsonNull # a special value which is a representation of a null in JSON
function json:null() end

---@return bool # true if the value specified is a null read from JSON
function json:isNull() end
");

File.WriteAllText(autocompleteFilePath, autocomplete.ToString());
LuaManager.Instance.CopyLuaModules(); // Update the copy in User docs (also done on app start)

// Generate markdown docs
string docsPath = Path.Join(LuaManager.Instance.UserPluginsPath(), "LuaDocs");
if (!Directory.Exists(docsPath)) Directory.CreateDirectory(docsPath);
foreach (var klass in LuaDocsRegistration.ApiDocClasses)
{
var markDown = klass.MarkdownSerialize();
File.WriteAllText(Path.Join(docsPath, $"{klass.Name.ToLower()}.md"), markDown);
}

// Done
LuaDocsRegistration.ApiDocClasses = null;
Debug.Log($"Finished Generating Lua Docs");
}
}
}
3 changes: 3 additions & 0 deletions Assets/Editor/LuaDocsGenerator.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

176 changes: 176 additions & 0 deletions Assets/Editor/LuaExampleScriptsTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
// Copyright 2023 The Tilt Brush Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

using System;
using System.Linq;
using MoonSharp.Interpreter;
using UnityEditor;
using UnityEngine;

namespace TiltBrush
{

// Attempts to call all known functions on all scripts.
// Currently lots of false positives because we aren't calling the script as it's usually called.
// But still a useful smoke test for syntax errors and name changes.
public class LuaExampleScriptsTest : Editor
{
public static DynValue CallFunctionIfExists(Script script, string fnName)
{
var fn = script.Globals.Get(fnName).Function;
if (fn != null)
{
try
{
return script.Call(fn);
}
catch (InterpreterException e)
{
string msg = e.DecoratedMessage ?? e.Message;
msg = LuaManager.ReformatLuaError(script, fnName, msg);
Debug.LogError(msg);
}
catch (Exception e)
{
string msg = e.Message;
msg = LuaManager.ReformatLuaError(script, fnName, msg);
Debug.LogError(msg);
}
}
return null;
}

[MenuItem("Open Brush/API/Test Example Scripts")]
static void DoTest()
{
if (!Application.isPlaying)
{
Debug.LogError("You can only run this whilst in Play Mode");
return;
}

var pointerScripts = LuaManager.Instance.Scripts[LuaApiCategory.PointerScript];
var symmetryScripts = LuaManager.Instance.Scripts[LuaApiCategory.SymmetryScript];
var toolScripts = LuaManager.Instance.Scripts[LuaApiCategory.ToolScript];
var backgroundScripts = LuaManager.Instance.Scripts[LuaApiCategory.BackgroundScript];

foreach (var example in pointerScripts)
{
var script = example.Value;
TestAllKnown(script);
}

foreach (var example in symmetryScripts)
{
var script = example.Value;
TestAllKnown(script);
}

foreach (var example in toolScripts)
{
var script = example.Value;
LuaManager.Instance.SetApiProperty(script, $"Tool.{LuaNames.ToolScriptVector}", Vector3.one);
LuaManager.Instance.SetApiProperty(script, $"Tool.{LuaNames.ToolScriptRotation}", Quaternion.LookRotation(Vector3.one));
TestAllKnown(script);
}

foreach (var example in backgroundScripts)
{
var script = example.Value;
TestAllKnown(script);
}

void TestAllKnown(Script script)
{
LuaManager.Instance.InitScript(script);
DynValue result;
result = CallFunctionIfExists(script, LuaNames.Start);
result = CallFunctionIfExists(script, LuaNames.Main);
result = CallFunctionIfExists(script, LuaNames.End);
}
}
}

public class TestSpecificScript : EditorWindow
{
private int selectedIndex = 0;
private string inputText = "";

[MenuItem("Open Brush/API/Test Specific Script")]
static void Init()
{
if (!Application.isPlaying)
{
Debug.LogError("You can only run this whilst in Play Mode");
return;
}
TestSpecificScript window = (TestSpecificScript)GetWindow(typeof(TestSpecificScript));
window.Show();
}

void OnGUI()
{
if (!Application.isPlaying) return;

EditorGUILayout.BeginVertical();

string[] scriptNames = LuaManager.Instance.GetScriptNames(LuaApiCategory.ToolScript).ToArray();
selectedIndex = EditorGUILayout.Popup("Dropdown", selectedIndex, scriptNames);

if (GUILayout.Button("Submit"))
{
SubmitDropDownValue(scriptNames[selectedIndex]);
}

inputText = EditorGUILayout.TextField("Input Text", inputText);

if (Event.current.type == EventType.KeyDown
&& Event.current.keyCode == KeyCode.Return
&& GUI.GetNameOfFocusedControl() == "TextField")
{
SubmitTextValue(inputText);
Event.current.Use(); // Consume this event, we don't want to trigger other behavior with return key.
}

if (GUILayout.Button("Submit Text"))
{
SubmitTextValue(inputText);
}

EditorGUILayout.EndVertical();
}

void SubmitTextValue(string cmd)
{
Script script = new Script();
LuaManager.Instance.InitScriptOnce(script);
var result = script.DoString($"return {cmd}");
Debug.Log(result);
}

void SubmitDropDownValue(string value)
{
var script = LuaManager.Instance.Scripts[LuaApiCategory.ToolScript][value];
SketchSurfacePanel.m_Instance.EnableSpecificTool(BaseTool.ToolType.ScriptedTool);
LuaManager.Instance.SetActiveScriptByName(LuaApiCategory.ToolScript, value);

var startPoint = Vector3.up;
var endPoint = Vector3.up + Vector3.one;
LuaManager.Instance.SetApiProperty(script, $"Tool.{LuaNames.ToolScriptVector}", endPoint - startPoint);
LuaManager.Instance.SetApiProperty(script, $"Tool.{LuaNames.ToolScriptRotation}", Quaternion.LookRotation(endPoint - startPoint));
LuaManager.Instance.DoToolScript(LuaNames.Main, TrTransform.T(startPoint), TrTransform.T(endPoint));
}
}

}
11 changes: 11 additions & 0 deletions Assets/Editor/LuaExampleScriptsTest.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading