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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
|
package main
import (
"fmt"
"math/rand"
)
type Point struct{ X, Y int }
type TerrainColor struct {
Color string `yaml:"color"`
Weight int `yaml:"weight"`
}
type Terrain struct {
Name string `yaml:"name"`
Symbol string `yaml:"symbol"`
ASCII string `yaml:"ascii"`
Colors []TerrainColor `yaml:"colors"`
}
func (t Terrain) GetSymbol(unicode bool) string {
if unicode {
return t.Symbol
}
return t.ASCII
}
func (t Terrain) PickColor() string {
if len(t.Colors) == 0 {
return "0"
}
if len(t.Colors) == 1 {
return t.Colors[0].Color
}
total := 0
for _, c := range t.Colors {
total += c.Weight
}
r := rand.Intn(total)
for _, c := range t.Colors {
r -= c.Weight
if r < 0 {
return c.Color
}
}
return t.Colors[0].Color
}
type Cell struct {
Terrain int `yaml:"t"`
Color string `yaml:"c,omitempty"` // persisted ANSI color — set when drawn, stays fixed
Text string `yaml:"x,omitempty"`
}
type TextLabel struct {
Text string `yaml:"text"`
Start Point `yaml:"start"`
Color string `yaml:"color,omitempty"`
}
type Map struct {
Name string `yaml:"name"`
Width int `yaml:"width"`
Height int `yaml:"height"`
Grid [][]Cell `yaml:"-"`
Palette []Terrain `yaml:"palette"`
TextLabels []TextLabel `yaml:"text_labels,omitempty"`
Submaps map[Point]*Map `yaml:"-"`
Parent *Map `yaml:"-"`
Filename string `yaml:"-"`
}
func NewMap(name string, w, h int, palette []Terrain) *Map {
grid := make([][]Cell, h)
for y := range grid {
grid[y] = make([]Cell, w)
for x := range grid[y] {
grid[y][x].Terrain = -1
}
}
return &Map{
Name: name,
Width: w,
Height: h,
Grid: grid,
Palette: palette,
Submaps: make(map[Point]*Map),
}
}
func (m *Map) Clone() *Map {
grid := make([][]Cell, m.Height)
for y := range grid {
grid[y] = make([]Cell, m.Width)
copy(grid[y], m.Grid[y])
}
c := &Map{
Name: m.Name,
Width: m.Width,
Height: m.Height,
Grid: grid,
Palette: m.Palette,
Submaps: make(map[Point]*Map),
Parent: m.Parent,
}
for _, tl := range m.TextLabels {
c.TextLabels = append(c.TextLabels, tl)
}
for pt, sub := range m.Submaps {
c.Submaps[pt] = sub
}
return c
}
func (m *Map) InBounds(p Point) bool {
return p.X >= 0 && p.X < m.Width && p.Y >= 0 && p.Y < m.Height
}
func (m *Map) SetCell(p Point, terrain int, color ...string) {
if !m.InBounds(p) {
return
}
text := m.Grid[p.Y][p.X].Text
clr := ""
if len(color) > 0 {
clr = color[0]
}
m.Grid[p.Y][p.X] = Cell{Terrain: terrain, Color: clr, Text: text}
}
func (m *Map) SetText(p Point, text string) {
if !m.InBounds(p) {
return
}
m.Grid[p.Y][p.X].Text = text
}
func (m *Map) CellAt(p Point) Cell {
if !m.InBounds(p) {
return Cell{Terrain: -1}
}
return m.Grid[p.Y][p.X]
}
type UndoStack struct {
states []undoEntry
pos int
}
type undoEntry struct {
target *Map
state *Map
}
func (u *UndoStack) Push(target *Map) {
keep := u.pos + 1
if keep > len(u.states) {
keep = len(u.states)
}
u.states = append(u.states[:keep], undoEntry{target: target, state: target.Clone()})
u.pos = len(u.states) - 1
if len(u.states) > 100 {
u.states = u.states[1:]
u.pos--
}
}
func (u *UndoStack) Undo() *undoEntry {
if u.pos <= 0 {
return nil
}
u.pos--
return &u.states[u.pos]
}
func (u *UndoStack) Redo() *undoEntry {
if u.pos >= len(u.states)-1 {
return nil
}
u.pos++
return &u.states[u.pos]
}
type Tool int
const (
ToolBrush Tool = iota
ToolSelect
ToolErase
ToolFill
ToolLine
ToolRect
ToolCircle
ToolText
)
func (t Tool) String() string {
switch t {
case ToolBrush:
return "Brush"
case ToolSelect:
return "Select"
case ToolErase:
return "Erase"
case ToolFill:
return "Fill"
case ToolLine:
return "Line"
case ToolRect:
return "Rectangle"
case ToolCircle:
return "Circle"
case ToolText:
return "Text"
}
return ""
}
type Mode int
const (
ModeNormal Mode = iota
ModeDialog
ModeLinePreview
ModeRectPreview
ModeCirclePreview
ModeTextEdit
)
type DialogType int
const (
DialogNone DialogType = iota
DialogSaveAs
DialogOpenMap
DialogResize
DialogQuitConfirm
DialogDeleteSubmapConfirm
DialogRenameSymbol
DialogRenameMap
DialogFileSave
DialogFileOpen
)
func clamp(v, lo, hi int) int {
if v < lo {
return lo
}
if v > hi {
return hi
}
return v
}
func DemoModel() {
m := NewMap("test", 10, 5, nil)
if m.Width != 10 || m.Height != 5 || m.Grid[0][0].Terrain != -1 {
panic("NewMap broken")
}
m2 := m.Clone()
m2.Grid[0][0].Terrain = 0
if m.Grid[0][0].Terrain != -1 {
panic("Clone shares data")
}
fmt.Println("model: ok")
}
|