-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
122 lines (106 loc) · 2.64 KB
/
main.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package main
import (
"github.com/llgcode/draw2d/draw2dimg"
"github.com/llgcode/draw2d/draw2dkit"
"github.com/markfarnan/go-canvas/canvas"
"image/color"
"syscall/js"
)
var cells [][]Cell
var done chan struct{}
var cvs *canvas.Canvas2d
const width = 40
const height = 20
const cellSize = 20.0
var pressed = false
func main() {
cvs, _ = canvas.NewCanvas2d(true)
cvs_obj := js.
Global().
Get("document").
Call("getElementsByTagName", "canvas").
Index(0)
cvs_obj.Call("addEventListener", "mousemove", js.FuncOf(handleMouseMove))
cvs_obj.Call("addEventListener", "mousedown", js.FuncOf(handleMouseDown))
cvs_obj.Call("addEventListener", "mouseup" , js.FuncOf(handleMouseUp ))
for i := 0; i < width; i++ {
var column []Cell
for j := 0; j < height; j++ {
var t CellType = Liquid
var v = 0.0
if j == height - 1 || j == 0 || i == 0 || i == width - 1 {
t = Solid
} else if false {
t = Liquid
v = 1.0
}
column = append(column, NewCell(i, j, t, v))
}
cells = append(cells, column)
}
cvs.Start(60, Render)
<-done
}
func handleMouseDown(this js.Value, args []js.Value) interface{} {
pressed = true
return nil
}
func handleMouseUp(this js.Value, args []js.Value) interface{} {
pressed = false
return nil
}
func handleMouseMove(this js.Value, args []js.Value) interface{} {
mouseEvent := args[0]
shift := mouseEvent.Get("shiftKey").Bool()
ctrl := mouseEvent.Get("ctrlKey").Bool()
if pressed {
x := int((mouseEvent.Get("offsetX").Float()) / cellSize)
y := int((mouseEvent.Get("offsetY").Float()) / cellSize)
if x >= width - 1 || y >= height - 1 || y < 1 || x < 1 {
return nil
}
if !ctrl && !shift && cells[x][y].cellType != Solid {
cells[x][y].cellType = Liquid
cells[x][y].volume = maxVolume + maxPressure
} else if ctrl && !shift {
cells[x][y].cellType = Solid
} else if shift && !ctrl {
cells[x][y].cellType = Void
cells[x][y].volume = 0
}
}
return nil
}
func drawCell(gc *draw2dimg.GraphicContext, c Cell) {
col := c.GetColor()
gc.SetFillColor(col)
gc.SetStrokeColor(col)
step := cellSize / maxVolume
liquidVolume := 0.0
if c.volume > 0 && c.cellType == Liquid {
liquidVolume = cellSize - step * c.GetVolume()
}
gc.BeginPath()
draw2dkit.Rectangle(
gc,
float64(c.x) * cellSize,
float64(c.y) * cellSize + liquidVolume,
float64(c.x + 1) * cellSize,
float64(c.y + 1) * cellSize,
)
gc.FillStroke()
gc.Close()
}
func Render(gc *draw2dimg.GraphicContext) bool {
simulate(cells)
simulate(cells)
simulate(cells)
gc.SetFillColor(color.RGBA{0xff, 0xff, 0xff, 0xff})
gc.Clear()
for _, row := range cells {
for _, cell := range row {
drawCell(gc, cell)
}
}
return true
}