gg/cmd/test/camera.go

82 lines
1.4 KiB
Go
Raw Permalink 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"
2024-06-01 22:01:17 +03:00
import "log"
2024-06-01 16:07:28 +03:00
var (
2024-06-01 22:01:17 +03:00
camera *Camera
2024-06-01 16:07:28 +03:00
)
type Camera struct {
ox.Camera
ScaleSpeed mx.Float
RotationSpeed mx.Float
}
2024-06-01 22:01:17 +03:00
func NewCamera() *Camera {
camera := &Camera{}
camera.Camera = ox.NewCamera()
camera.ScaleSpeed = 5.
camera.RotationSpeed = .3
return camera
}
2024-06-01 16:07:28 +03:00
func (cam *Camera) OnUpdate(c gg.Context) {
dt := c.Engine().DT().Seconds()
mov := c.Events().Mouse.Move
2024-06-03 11:04:42 +03:00
wheel := c.Events().Mouse.Wheel
2024-06-01 16:07:28 +03:00
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)
}
2024-06-03 11:04:42 +03:00
for _, btn := range c.Events().Mouse.Downs {
2024-06-02 22:27:11 +03:00
switch btn.MouseButton {
case gg.MouseButtonLeft :
t := NewTri(ox.T().SetPosition(
c.Engine().GetAbsCursorPosition(),
))
t.Spawned = true
c.Engine().Spawn(t)
}
}
2024-06-01 16:07:28 +03:00
// Scale.
if wheel != nil {
2024-06-01 22:01:17 +03:00
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,
2024-06-01 16:07:28 +03:00
))
}
// 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))
2024-06-01 16:07:28 +03:00
case gg.KeyR:
cam.Rotate(d*mx.Pi * cam.RotationSpeed * dt)
2024-06-01 16:07:28 +03:00
}
}
}