dynamic sizing

This commit is contained in:
2026-01-05 14:41:26 -06:00
parent 3b44c20469
commit a107a268f7
2 changed files with 82 additions and 57 deletions

View File

@@ -21,7 +21,7 @@ type TextureCam struct {
/** RenderCtx **/
type RenderCtx struct {
TargetBounds rl.Rectangle
TargetBounds rl.RectangleInt32
SourceWidth int32
SourceHeight int32
Time float64
@@ -200,7 +200,7 @@ func (s *Sketch) Draw(ctx *RenderCtx) {
func (s *Sketch) calcOutputRectKeepingAspectRatio(ctx *RenderCtx) rl.Rectangle {
sourceAspect := float32(ctx.SourceWidth) / float32(ctx.SourceHeight)
targetAspect := ctx.TargetBounds.Width / ctx.TargetBounds.Height
targetAspect := float32(ctx.TargetBounds.Width) / float32(ctx.TargetBounds.Height)
outputWidth := ctx.TargetBounds.Width
outputHeight := ctx.TargetBounds.Height
@@ -209,22 +209,22 @@ func (s *Sketch) calcOutputRectKeepingAspectRatio(ctx *RenderCtx) rl.Rectangle {
// source is relatively taller than the target
// so we set the output height to the target height
// and calculate the width based on source aspect and center
outputWidth = outputHeight * sourceAspect
outputWidth = int32(float32(outputHeight) * sourceAspect)
} else {
// source is relatively wider than the target
// so we set the output width to the target width
// and calculate the height based on source aspect and center
outputHeight = outputWidth / sourceAspect
outputHeight = int32(float32(outputWidth) / sourceAspect)
}
// output width and height are correct -- center within TargetBounds
x := ctx.TargetBounds.X + ctx.TargetBounds.Width / 2.0 - outputWidth / 2.0
y := ctx.TargetBounds.Y + ctx.TargetBounds.Height / 2.0 - outputHeight / 2.0
return rl.Rectangle {
X: x, Y: y,
Width: outputWidth,
Height: outputHeight,
return rl.Rectangle{
X: float32(x), Y: float32(y),
Width: float32(outputWidth),
Height: float32(outputHeight),
}
}
@@ -244,7 +244,7 @@ func (s *Sketch) Update(ctx *RenderCtx) {
if rl.IsMouseButtonDown(rl.MouseRightButton) {
// get mouse delta from last frame
delta := rl.GetMouseDelta()
sourceScale := float32(ctx.SourceWidth) / ctx.TargetBounds.Width
sourceScale := float32(ctx.SourceWidth) / float32(ctx.TargetBounds.Width)
// compute the amount to move scaled by the camera zoom
delta = rl.Vector2Scale(delta, -sourceScale/s.cam.Zoom)
delta.Y = -delta.Y