@@ -2,14 +2,15 @@ package main
2
2
3
3
import (
4
4
"flag"
5
+ "fmt"
6
+ "github.com/aldernero/gaul"
7
+ "github.com/aldernero/sketchy"
8
+ "github.com/hajimehoshi/ebiten/v2"
9
+ "github.com/hajimehoshi/ebiten/v2/inpututil"
5
10
"github.com/tdewolff/canvas"
6
11
"image/color"
7
12
"log"
8
13
"math"
9
-
10
- "github.com/aldernero/sketchy"
11
- "github.com/hajimehoshi/ebiten/v2"
12
- "github.com/hajimehoshi/ebiten/v2/inpututil"
13
14
)
14
15
15
16
// Define a grid of tiles and a random number generator
@@ -23,29 +24,42 @@ const (
23
24
)
24
25
25
26
type Truchet struct {
26
- rows int
27
- cols int
28
- tiles []Tile
27
+ rows int
28
+ cols int
29
+ originX float64
30
+ originY float64
31
+ cellSize float64
32
+ tiles []Tile
29
33
}
30
34
31
- func (t * Truchet ) init (r int , c int , s * sketchy.Sketch ) {
32
- t .rows = r
33
- t .cols = c
34
- t .tiles = []Tile {}
35
- for i := 0 ; i < r * c ; i ++ {
35
+ func (t * Truchet ) init (cellSize float64 , s * sketchy.Sketch ) {
36
+ t .rows = int (s .Height () / cellSize )
37
+ t .cols = int (s .Width () / cellSize )
38
+ t .cellSize = cellSize
39
+ t .originX = 0.5 * (s .Width () - float64 (t .cols )* cellSize )
40
+ t .originY = 0.5 * (s .Height () - float64 (t .rows )* cellSize )
41
+ t .tiles = make ([]Tile , t .rows * t .cols )
42
+ for i := 0 ; i < t .rows * t .cols ; i ++ {
36
43
// Use opensimplex noise to make it more interesting
37
- noise := s .Rand .Noise2D (float64 (i % c ), float64 (i / r ))
44
+ noise := s .Rand .Noise2D (float64 (i % t . rows ), float64 (i ) / float64 ( t . cols ))
38
45
tile := BackSlash
39
46
if noise > 0.5 {
40
47
tile = ForwardSlash
41
48
}
42
- //fmt.Println(i, noise, tile)
43
- t .tiles = append (t .tiles , tile )
49
+ t .tiles [i ] = tile
44
50
}
51
+ fmt .Println (t .rows , t .cols , t .originX , t .originY , t .cellSize )
45
52
}
46
53
47
- func (t * Truchet ) flip (r int , c int ) {
54
+ func (t * Truchet ) flip (x , y float64 ) {
55
+ // Convert x, y to row, col
56
+ r := int (math .Floor ((y - t .originY ) / t .cellSize ))
57
+ c := int (math .Floor ((x - t .originX ) / t .cellSize ))
48
58
i := r * t .cols + c
59
+ if i < 0 || i >= len (t .tiles ) {
60
+ fmt .Println (x , y , r , c , i )
61
+ fmt .Printf ("Invalid tile index: %d Tile count: %d" , i , len (t .tiles ))
62
+ }
49
63
val := t .tiles [i ]
50
64
switch val {
51
65
case EmptyTile :
@@ -57,6 +71,18 @@ func (t *Truchet) flip(r int, c int) {
57
71
}
58
72
}
59
73
74
+ func (t * Truchet ) rectForTile (i int ) gaul.Rect {
75
+ cell := t .cellSize
76
+ x := cell * float64 (i % t .cols ) + t .originX
77
+ y := cell * float64 (i / t .cols ) + t .originY
78
+ return gaul.Rect {
79
+ X : x ,
80
+ Y : y ,
81
+ W : t .cellSize ,
82
+ H : t .cellSize ,
83
+ }
84
+ }
85
+
60
86
func setup (s * sketchy.Sketch ) {
61
87
cellSize := s .Slider ("cellSize" )
62
88
s .Rand .SetSeed (s .RandomSeed )
@@ -67,7 +93,7 @@ func setup(s *sketchy.Sketch) {
67
93
s .Rand .SetNoiseScaleY (s .Slider ("yscale" ))
68
94
s .Rand .SetNoiseOffsetX (s .Slider ("xoffset" ))
69
95
s .Rand .SetNoiseOffsetY (s .Slider ("yoffset" ))
70
- board .init (int ( s . SketchCanvas . W / cellSize ), int ( s . SketchCanvas . H / cellSize ) , s )
96
+ board .init (cellSize , s )
71
97
}
72
98
73
99
func update (s * sketchy.Sketch ) {
@@ -77,33 +103,36 @@ func update(s *sketchy.Sketch) {
77
103
setup (s )
78
104
}
79
105
// flip one tile
80
- if inpututil .IsMouseButtonJustReleased (ebiten .MouseButtonLeft ) {
106
+ if inpututil .IsMouseButtonJustReleased (ebiten .MouseButtonRight ) {
81
107
x , y := ebiten .CursorPosition ()
82
- if s .PointInSketchArea (float64 (x ), float64 (y )) {
83
- p := s .CanvasCoords (float64 (x ), float64 (y ))
84
- c := int (math .Floor (float64 (board .rows ) * p .X / s .SketchCanvas .W ))
85
- r := int (math .Floor (float64 (board .cols ) * p .Y / s .SketchCanvas .H ))
86
- board .flip (r , c )
87
- }
108
+ p := s .CanvasCoords (float64 (x ), float64 (y ))
109
+ board .flip (p .X , p .Y )
88
110
}
89
111
}
90
112
91
113
func draw (s * sketchy.Sketch , c * canvas.Context ) {
92
114
// Drawing code goes here
93
115
c .SetStrokeColor (color .White )
94
- c .SetStrokeWidth (0.7 )
95
- dx := c .Width () / float64 (board .cols )
96
- dy := c .Height () / float64 (board .rows )
116
+ c .SetStrokeWidth (0.5 )
117
+ // draw board rectangle
118
+ c .MoveTo (board .originX , board .originY )
119
+ c .LineTo (board .originX + float64 (board .cols )* board .cellSize , board .originY )
120
+ c .LineTo (board .originX + float64 (board .cols )* board .cellSize , board .originY + float64 (board .rows )* board .cellSize )
121
+ c .LineTo (board .originX , board .originY + float64 (board .rows )* board .cellSize )
122
+ c .LineTo (board .originX , board .originY )
123
+ // draw tiles
124
+ c .SetStrokeWidth (0.5 )
97
125
for i , t := range board .tiles {
98
- x := dx * float64 (i % board .cols )
99
- y := dy * float64 (i / board .rows )
126
+ rect := board .rectForTile (i )
100
127
switch t {
101
128
case BackSlash :
102
- c .MoveTo (x , y )
103
- c .LineTo (x + dx , y + dy )
129
+ c .MoveTo (rect . X , rect . Y )
130
+ c .LineTo (rect . X + board . cellSize , rect . Y + board . cellSize )
104
131
case ForwardSlash :
105
- c .MoveTo (x , y + dy )
106
- c .LineTo (x + dx , y )
132
+ c .MoveTo (rect .X , rect .Y + board .cellSize )
133
+ c .LineTo (rect .X + board .cellSize , rect .Y )
134
+ default :
135
+ // do nothing
107
136
}
108
137
}
109
138
c .Stroke ()
@@ -131,6 +160,8 @@ func main() {
131
160
s .Drawer = draw
132
161
s .Init ()
133
162
setup (s )
163
+ fmt .Println (s .SketchWidth , s .SketchHeight )
164
+ fmt .Println (s .Width (), s .Height ())
134
165
ebiten .SetWindowSize (int (s .SketchWidth ), int (s .SketchHeight ))
135
166
ebiten .SetWindowTitle ("Sketchy - " + s .Title )
136
167
ebiten .SetWindowResizingMode (ebiten .WindowResizingModeDisabled )
0 commit comments