-
Notifications
You must be signed in to change notification settings - Fork 0
/
imdop.go
159 lines (132 loc) · 3.35 KB
/
imdop.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package draw
import (
"fmt"
"github.com/faiface/pixel"
"github.com/faiface/pixel/imdraw"
"image/color"
"strings"
)
type ImdCircle struct {
radius, thickness int
center pixel.Vec
}
func (circle ImdCircle) String() string {
return fmt.Sprintf("Circle radius %v center %v thickness %v",
circle.radius,
circle.center.String(),
circle.thickness)
}
func Circle(radius int, center pixel.Vec, thickness int) ImdOp {
return ImdCircle{
radius: radius,
thickness: thickness,
center: center,
}
}
func (circle ImdCircle) Render(imd *imdraw.IMDraw) {
imd.Push(circle.center)
imd.Circle(float64(circle.radius), float64(circle.thickness))
}
func (circle ImdCircle) Lines() []string {
return []string{circle.String()}
}
type ImdLine struct {
from, to pixel.Vec
thickness int
}
func (line ImdLine) String() string {
return fmt.Sprintf("Line from %v to %v thickness %v",
line.from.String(),
line.to.String(),
line.thickness)
}
func Line(from pixel.Vec, to pixel.Vec, thickness int) ImdOp {
return ImdLine{from: from, to: to, thickness: thickness}
}
func (line ImdLine) Render(imd *imdraw.IMDraw) {
imd.Push(line.from)
imd.Push(line.to)
imd.Line(float64(line.thickness))
}
func (line ImdLine) Lines() []string {
return []string{line.String()}
}
type ImdRectangle struct {
from, to pixel.Vec
thickness int
}
func (rectangle ImdRectangle) String() string {
var rectangleStyle string
rectangleStyle = "(filled)"
if rectangle.thickness != 0 {
rectangleStyle = fmt.Sprintf("thickness %v", rectangle.thickness)
}
return fmt.Sprintf("Rectangle from %v to %v %v",
rectangle.from.String(),
rectangle.to.String(),
rectangleStyle)
}
func Rectangle(from pixel.Vec, to pixel.Vec, thickness int) ImdOp {
return ImdRectangle{from: from, to: to, thickness: thickness}
}
func (rectangle ImdRectangle) Render(imd *imdraw.IMDraw) {
imd.Push(rectangle.from)
imd.Push(rectangle.to)
imd.Rectangle(float64(rectangle.thickness))
}
func (rectangle ImdRectangle) Lines() []string {
return []string{rectangle.String()}
}
type ImdColor struct {
color color.Color
Operation ImdOp
}
func (color ImdColor) String() string {
return strings.Join(color.Lines(), "\n")
}
func (color ImdColor) Render(imd *imdraw.IMDraw) {
// TODO: do we want to reset color to previous state?
imd.Color = color.color
color.Operation.Render(imd)
}
func Colored(color color.Color, imdOp ImdOp) ImdOp {
return ImdColor{
color: color,
Operation: imdOp,
}
}
func (color ImdColor) Lines() []string {
head := fmt.Sprintf("Color %v:", color.color)
body := color.Operation.Lines()
return headerWithIndentedBody(head, body)
}
type ImdSequence struct {
imdOps []ImdOp
}
func ImdOpSequence(imdOps ...ImdOp) ImdSequence {
return ImdSequence{
imdOps: imdOps,
}
}
func (sequence ImdSequence) Render(imd *imdraw.IMDraw) {
for _, imdOp := range sequence.imdOps {
imdOp.Render(imd)
}
}
func (sequence ImdSequence) String() string {
head := "ImdOp Sequence:"
body := make([]string, 0)
for _, op := range sequence.imdOps {
for _, line := range op.Lines() {
body = append(body, line)
}
}
return strings.Join(headerWithIndentedBody(head, body), "\n")
}
func (sequence ImdSequence) Lines() []string {
return strings.Split(sequence.String(), "\n")
}
func (sequence ImdSequence) Then(imdOp ImdOp) ImdSequence {
ops := append(sequence.imdOps, imdOp)
return ImdOpSequence(ops...)
}