automated snapshot

This commit is contained in:
sumi
2025-12-22 16:36:33 -06:00
parent ee5e9d7d5d
commit 96a4a34ae0
38 changed files with 262 additions and 161 deletions

View File

@@ -8,23 +8,39 @@ import (
type Sketch struct {
sourceWidth int32
sourceHeight int32
cam *TextureCam
layerTools map[string]LayerTools
layerToolsOrdered []LayerTools
composite rl.RenderTexture2D
}
type TextureCam struct {
LookAt rl.Vector2
Zoom float32
}
func NewSketch(sourceWidth, sourceHeight int32) Sketch {
// point at source center
// put source center at center of screen
var camera = TextureCam {
LookAt: rl.Vector2{X: float32(sourceWidth) / 2.0, Y: float32(sourceHeight) / 2.0},
Zoom: 1.0,
}
return Sketch {
sourceWidth: sourceWidth,
sourceHeight: sourceHeight,
layerTools: make(map[string]LayerTools),
layerToolsOrdered: []LayerTools {},
composite: rl.LoadRenderTexture(sourceWidth, sourceHeight),
cam: &camera,
}
}
func (s *Sketch) CreateLayer(name string, layer Layer, sourceWidth int32, sourceHeight int32) {
texture := rl.LoadRenderTexture(sourceWidth, sourceHeight)
func (s *Sketch) CreateLayer(name string, layer Layer) {
texture := rl.LoadRenderTexture(s.sourceWidth, s.sourceHeight)
layerTools := LayerTools {
name: name,
texture: &texture,
@@ -80,44 +96,49 @@ func (s *Sketch) Draw(ctx *RenderCtx) {
}
func (s *Sketch) CalcViewport(ctx *RenderCtx) rl.Rectangle {
viewportWidth := rl.Clamp(float32(ctx.SourceWidth) / ctx.Cam.Zoom, 0, float32(ctx.SourceWidth))
viewportHeight := rl.Clamp(float32(ctx.SourceHeight) / ctx.Cam.Zoom, 0, float32(ctx.SourceHeight))
viewportWidth := rl.Clamp(float32(ctx.SourceWidth) / s.cam.Zoom, 0, float32(ctx.SourceWidth))
viewportHeight := rl.Clamp(float32(ctx.SourceHeight) / s.cam.Zoom, 0, float32(ctx.SourceHeight))
return rl.Rectangle{
X: rl.Clamp(ctx.Cam.LookAt.X - viewportWidth/2.0, 0, float32(ctx.SourceWidth)-viewportWidth),
Y: rl.Clamp(ctx.Cam.LookAt.Y - viewportHeight/2.0, 0, float32(ctx.SourceHeight)-viewportHeight),
X: rl.Clamp(s.cam.LookAt.X - viewportWidth/2.0, 0, float32(ctx.SourceWidth)-viewportWidth),
Y: rl.Clamp(s.cam.LookAt.Y - viewportHeight/2.0, 0, float32(ctx.SourceHeight)-viewportHeight),
Width: float32(viewportWidth),
Height: -float32(viewportHeight),
}
}
func (s *Sketch) Update(ctx *RenderCtx) {
if rl.IsMouseButtonDown(rl.MouseRightButton) {
// get mouse delta from last frame
delta := rl.GetMouseDelta()
// compute the amount to move scaled by the camera zoom
delta = rl.Vector2Scale(delta, -sourceScale/ctx.Cam.Zoom)
delta = rl.Vector2Scale(delta, -sourceScale/s.cam.Zoom)
delta.Y = -delta.Y
ctx.Cam.LookAt = rl.Vector2Add(ctx.Cam.LookAt, delta)
s.cam.LookAt = rl.Vector2Add(s.cam.LookAt, delta)
}
// clamp LookAt to be somewhere on the texture
ctx.Cam.LookAt.X = rl.Clamp(ctx.Cam.LookAt.X, 0, float32(ctx.SourceWidth-1))
ctx.Cam.LookAt.Y = rl.Clamp(ctx.Cam.LookAt.Y, 0, float32(ctx.SourceHeight-1))
s.cam.LookAt.X = rl.Clamp(s.cam.LookAt.X, 0, float32(ctx.SourceWidth-1))
s.cam.LookAt.Y = rl.Clamp(s.cam.LookAt.Y, 0, float32(ctx.SourceHeight-1))
// Zoom based on mouse wheel
wheel := rl.GetMouseWheelMove()
if wheel != 0 {
const zoomIncrement float32 = 0.05
if wheel > 0 {
ctx.Cam.Zoom *= 1+zoomIncrement
s.cam.Zoom *= 1+zoomIncrement
} else {
ctx.Cam.Zoom *= 1-zoomIncrement
s.cam.Zoom *= 1-zoomIncrement
}
}
// clamp zoom to > 1 so we don't ever zoom out more than necessary
ctx.Cam.Zoom = rl.Clamp(ctx.Cam.Zoom, 1, math.MaxInt64)
s.cam.Zoom = rl.Clamp(s.cam.Zoom, 1, math.MaxInt64)
}
func (s *Sketch) ResetCamera() {
s.cam.LookAt = rl.Vector2{X: float32(s.sourceWidth) / 2.0, Y: float32(s.sourceHeight) / 2.0}
s.cam.Zoom = 1.0
}
type SketchCapture struct {
@@ -218,5 +239,4 @@ type RenderCtx struct {
SourceHeight int32
Time float64
Ports map[string]float64
Cam *TextureCam
}