Skip to content

Commit

Permalink
janky dragging
Browse files Browse the repository at this point in the history
  • Loading branch information
nhanb committed Jun 28, 2022
1 parent 3700d5c commit 0dc16de
Showing 1 changed file with 48 additions and 9 deletions.
57 changes: 48 additions & 9 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package main
import (
"bytes"
"embed"
"fmt"
_ "image/png"
"log"
"path/filepath"
Expand All @@ -26,11 +25,22 @@ type Anim struct {
Frames []*ebiten.Image
}

type Position struct{ x, y int }

type Game struct {
CurrentAnim *Anim
CurrentFrame int
Ticks int
ShouldResetToIdle bool
IsDragging bool
StartMouseX int
StartMouseY int
}

func GlobalCursorPosition() (x, y int) {
cx, cy := ebiten.CursorPosition()
wx, wy := ebiten.WindowPosition()
return cx + wx, cy + wy
}

func (g *Game) Update() error {
Expand All @@ -42,6 +52,33 @@ func (g *Game) Update() error {
return nil
}

if inpututil.IsMouseButtonJustPressed(ebiten.MouseButtonLeft) {
g.IsDragging = true
g.CurrentAnim = Drag
g.Ticks = 0
g.CurrentFrame = 0
g.StartMouseX, g.StartMouseY = GlobalCursorPosition()
return nil
}
if inpututil.IsMouseButtonJustReleased(ebiten.MouseButtonLeft) {
g.IsDragging = false
g.CurrentAnim = Idle
g.Ticks = 0
g.CurrentFrame = 0
return nil
}

if g.IsDragging {
currentX, currentY := GlobalCursorPosition()
diffX := currentX - g.StartMouseX
diffY := currentY - g.StartMouseY

wx, wy := ebiten.WindowPosition()
ebiten.SetWindowPosition(wx+diffX, wy+diffY)

g.StartMouseX, g.StartMouseY = GlobalCursorPosition()
}

g.Ticks++
if g.Ticks < 10 {
return nil
Expand All @@ -60,14 +97,16 @@ func (g *Game) Update() error {
}

func (g *Game) Draw(screen *ebiten.Image) {
x, y := ebiten.WindowPosition()
ebitenutil.DebugPrint(
screen,
fmt.Sprintf(
"Ticks: %d\nCurrentFrame: %d\nx: %v, y: %v",
g.Ticks, g.CurrentFrame, x, y,
),
)
/*
x, y := ebiten.WindowPosition()
ebitenutil.DebugPrint(
screen,
fmt.Sprintf(
"Ticks: %d\nCurrentFrame: %d\nx: %v, y: %v",
g.Ticks, g.CurrentFrame, x, y,
),
)
*/
screen.DrawImage(g.CurrentAnim.Frames[g.CurrentFrame], nil)
}

Expand Down

0 comments on commit 0dc16de

Please sign in to comment.