gg/cmd/test/camera.go

56 lines
986 B
Go
Raw Normal View History

2024-06-01 16:07:28 +03:00
package main
import "surdeus.su/core/gg/ox"
import "surdeus.su/core/gg/mx"
import "surdeus.su/core/gg"
var (
camera = &Camera{
Camera: ox.NewCamera(),
ScaleSpeed: 5.,
RotationSpeed: .3,
}
)
type Camera struct {
ox.ObjectImpl
ox.Camera
ScaleSpeed mx.Float
RotationSpeed mx.Float
}
func (cam *Camera) OnUpdate(c gg.Context) {
dt := c.Engine().DT().Seconds()
mov := c.Events().Mouse.Move
wheel := c.Events().Mouse.WheelChange
shiftPressed := c.Engine().IsPressed(gg.KeyShift)
d := float64(1.)
if shiftPressed {
d *= -1
}
// Moving.
if mov != nil && c.Engine().IsButtoned(
gg.MouseButtonRight,
) {
cam.Move(mov.AbsDelta)
}
// Scale.
if wheel != nil {
cam.AddScale(mx.V2(
wheel.Offset.Y * dt * cam.ScaleSpeed * 40,
))
}
// Scale and rotation.
for _, key := range c.Engine().GetKeyboardKeys() {
switch key {
case gg.KeyF:
cam.AddScale(mx.V2(d*cam.ScaleSpeed * dt))
case gg.KeyR:
cam.Rotate(d*mx.Pi * cam.ScaleSpeed * dt)
}
}
}