-
Notifications
You must be signed in to change notification settings - Fork 0
/
cursor.go
79 lines (65 loc) · 1.94 KB
/
cursor.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package main
import (
"image"
"github.com/hajimehoshi/ebiten"
)
// Cursor is the cursor
type Cursor struct {
position Vec2i
size Vec2i
center Vec2i
currentCursor int
cursors []Sprite
image *ebiten.Image
}
func createCursor(image *ebiten.Image) Cursor {
return Cursor{
newVec2i(0, 0),
newVec2i(0, 0),
newVec2i(0, 0),
0,
[]Sprite{
createSprite(newVec2i(0, 0), newVec2i(15, 15), newVec2i(15, 15), iUISpritesheet),
createSprite(newVec2i(16, 0), newVec2i(30, 14), newVec2i(14, 14), iUISpritesheet),
createSprite(newVec2i(31, 0), newVec2i(45, 14), newVec2i(14, 14), iUISpritesheet),
createSprite(newVec2i(46, 0), newVec2i(50, 4), newVec2i(4, 4), iUISpritesheet),
},
image,
}
}
func (c *Cursor) update() {
x, y := ebiten.CursorPosition()
if ebiten.IsKeyPressed(ebiten.Key1) {
c.currentCursor = 0
} else if ebiten.IsKeyPressed(ebiten.Key2) {
c.currentCursor = 1
} else if ebiten.IsKeyPressed(ebiten.Key3) {
c.currentCursor = 2
} else if ebiten.IsKeyPressed(ebiten.Key4) {
c.currentCursor = 3
}
if c.checkInScreen(x, y) {
c.position = newVec2i(x-c.size.x/2, y-c.size.y/2)
}
c.size = newVec2i(c.cursors[c.currentCursor].size.x, c.cursors[c.currentCursor].size.y)
c.center = newVec2i(c.position.x+c.size.x/2, c.position.y+c.size.y/2)
}
// Checks if the mouse is in the screen
func (c *Cursor) checkInScreen(x int, y int) bool {
if x >= 0 && y >= 0 && x <= screenWidth && y <= screenHeight {
return true
}
return false
}
func (c *Cursor) render(screen *ebiten.Image) {
op := &ebiten.DrawImageOptions{}
op.GeoM.Translate(float64(c.position.x), float64(c.position.y))
op.Filter = ebiten.FilterNearest // Maybe fix rotation grossness?
cursorRect := image.Rect(
c.cursors[c.currentCursor].startPosition.x,
c.cursors[c.currentCursor].startPosition.y,
c.cursors[c.currentCursor].endPosition.x,
c.cursors[c.currentCursor].endPosition.y,
)
screen.DrawImage(c.image.SubImage(cursorRect).(*ebiten.Image), op)
}