gg/cmd/test/camera.go

81 lines
1.4 KiB
Go

package main
import "surdeus.su/core/gg/ox"
import "surdeus.su/core/gg/mx"
import "surdeus.su/core/gg"
import "log"
var (
camera *Camera
)
type Camera struct {
ox.Camera
ScaleSpeed mx.Float
RotationSpeed mx.Float
}
func NewCamera() *Camera {
camera := &Camera{}
camera.Camera = ox.NewCamera()
camera.ScaleSpeed = 5.
camera.RotationSpeed = .3
return camera
}
func (cam *Camera) OnUpdate(c gg.Context) {
dt := c.Engine().DT().Seconds()
mov := c.Events().Mouse.Move
wheel := c.Events().Mouse.Wheel
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)
}
for _, btn := range c.Events().Mouse.Downs {
switch btn.MouseButton {
case gg.MouseButtonLeft :
t := NewTri(ox.T().SetPosition(
c.Engine().GetAbsCursorPosition(),
))
t.Spawned = true
c.Engine().Spawn(t)
}
}
// Scale.
if wheel != nil {
k := 1.5
log.Println(wheel.Offset.Y)
if wheel.Offset.Y < 0 {
k = 1/k
}
cam.Scale(mx.V2(
k,
//wheel.Offset.Y * dt * cam.ScaleSpeed * 40,
))
}
// Scale and rotation.
for _, key := range c.Engine().GetKeyboardKeys() {
switch key {
case gg.KeyF:
cam.AddScale(mx.VX(d*cam.ScaleSpeed * dt))
case gg.KeyG:
cam.AddScale(mx.VY(d*cam.ScaleSpeed * dt))
case gg.KeyR:
cam.Rotate(d*mx.Pi * cam.RotationSpeed * dt)
}
}
}