2020-04-22 06:50:45 +00:00
|
|
|
package scripting
|
|
|
|
|
|
|
|
import (
|
2022-09-25 04:58:01 +00:00
|
|
|
"fmt"
|
2020-04-22 06:50:45 +00:00
|
|
|
|
2022-09-24 22:17:25 +00:00
|
|
|
"git.kirsle.net/SketchyMaze/doodle/pkg/log"
|
|
|
|
"git.kirsle.net/SketchyMaze/doodle/pkg/physics"
|
2025-02-14 04:12:28 +00:00
|
|
|
"git.kirsle.net/SketchyMaze/doodle/pkg/scripting/api"
|
2022-09-24 22:17:25 +00:00
|
|
|
"git.kirsle.net/SketchyMaze/doodle/pkg/shmem"
|
|
|
|
"git.kirsle.net/SketchyMaze/doodle/pkg/sound"
|
2020-04-22 06:50:45 +00:00
|
|
|
"git.kirsle.net/go/render"
|
|
|
|
)
|
|
|
|
|
2022-01-18 05:28:05 +00:00
|
|
|
// SEE ALSO: uix/scripting.go for more global functions
|
|
|
|
|
2020-04-22 06:50:45 +00:00
|
|
|
// JSProxy offers a function API interface to expose to Doodad javascripts.
|
|
|
|
// These methods safely give the JS access to important attributes and functions
|
|
|
|
// without exposing unintended API surface area in the process.
|
|
|
|
type JSProxy map[string]interface{}
|
|
|
|
|
2022-09-25 04:58:01 +00:00
|
|
|
// ProxyLog wraps a console.log function to inject the script's name.
|
|
|
|
func ProxyLog(vm *VM, fn func(string, ...interface{})) func(string, ...interface{}) {
|
|
|
|
var prefix = fmt.Sprintf("[%s] ", vm.Name)
|
|
|
|
return func(msg string, v ...interface{}) {
|
|
|
|
fn(prefix+msg, v...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-22 06:50:45 +00:00
|
|
|
// NewJSProxy initializes the API structure for JavaScript binding.
|
|
|
|
func NewJSProxy(vm *VM) JSProxy {
|
2022-09-25 04:58:01 +00:00
|
|
|
|
2025-02-14 04:12:28 +00:00
|
|
|
// jp := JSProxy{}
|
|
|
|
// return jp
|
|
|
|
|
2020-04-22 06:50:45 +00:00
|
|
|
return JSProxy{
|
|
|
|
// Console logging.
|
2025-02-14 04:12:28 +00:00
|
|
|
"console": api.Console{
|
|
|
|
Log: ProxyLog(vm, log.Info),
|
|
|
|
Debug: ProxyLog(vm, log.Debug),
|
|
|
|
Warn: ProxyLog(vm, log.Warn),
|
|
|
|
Error: ProxyLog(vm, log.Error),
|
|
|
|
}.ToMap(),
|
2020-04-22 06:50:45 +00:00
|
|
|
|
2020-05-23 03:07:48 +00:00
|
|
|
// Audio API.
|
|
|
|
"Sound": map[string]interface{}{
|
|
|
|
"Play": sound.PlaySound,
|
|
|
|
},
|
|
|
|
|
2020-04-22 06:50:45 +00:00
|
|
|
// Type constructors.
|
|
|
|
"RGBA": render.RGBA,
|
|
|
|
"Point": render.NewPoint,
|
|
|
|
"Vector": physics.NewVector,
|
|
|
|
|
|
|
|
// Useful types and functions.
|
|
|
|
"Flash": shmem.Flash,
|
|
|
|
"GetTick": func() uint64 {
|
|
|
|
return shmem.Tick
|
|
|
|
},
|
2025-02-14 04:12:28 +00:00
|
|
|
"time": api.NewTime().ToMap(),
|
2020-04-22 06:50:45 +00:00
|
|
|
|
|
|
|
// Bindings into the VM.
|
|
|
|
"Events": vm.Events,
|
|
|
|
"setTimeout": vm.SetTimeout,
|
|
|
|
"setInterval": vm.SetInterval,
|
|
|
|
"clearTimeout": vm.ClearTimer,
|
|
|
|
"clearInterval": vm.ClearTimer,
|
|
|
|
|
|
|
|
// Self for an actor to inspect themselves.
|
|
|
|
"Self": vm.Self,
|
|
|
|
}
|
|
|
|
}
|