summaryrefslogtreecommitdiff
path: root/mapwidget.go
blob: e8b23b3e7c3af8e073943d843bc17bd2471a9fa1 (plain)
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
package main

import (
	"fmt"
	"github.com/kch42/gomcmap/mcmap"
	"github.com/mattn/go-gtk/gdk"
	"github.com/mattn/go-gtk/glib"
	"github.com/mattn/go-gtk/gtk"
	"math"
	"unsafe"
)

const (
	zoom      = 2
	tileSize  = zoom * mcmap.ChunkSizeXZ
	cacheSize = 4
)

type tileCmd int

const (
	cmdUpdateTiles tileCmd = iota
	cmdFlushTiles
	cmdSave
)

func renderTile(chunk *mcmap.Chunk) (maptile, biotile *gdk.Pixmap, biocache []mcmap.Biome) {
	maptile = emptyPixmap(tileSize, tileSize, 24)
	mtDrawable := maptile.GetDrawable()
	mtGC := gdk.NewGC(mtDrawable)

	biotile = emptyPixmap(tileSize, tileSize, 24)
	btDrawable := biotile.GetDrawable()
	btGC := gdk.NewGC(btDrawable)

	biocache = make([]mcmap.Biome, mcmap.ChunkRectXZ)

	i := 0
	for z := 0; z < mcmap.ChunkSizeXZ; z++ {
	scanX:
		for x := 0; x < mcmap.ChunkSizeXZ; x++ {
			bio := chunk.Biome(x, z)
			btGC.SetRgbFgColor(bioColors[bio])
			btDrawable.DrawRectangle(btGC, true, x*zoom, z*zoom, zoom, zoom)

			biocache[i] = bio
			i++

			for y := chunk.Height(x, z); y >= 0; y-- {
				if col, ok := blockColors[chunk.Block(x, y, z).ID]; ok {
					mtGC.SetRgbFgColor(col)
					mtDrawable.DrawRectangle(mtGC, true, x*zoom, z*zoom, zoom, zoom)
					continue scanX
				}
			}

			mtGC.SetRgbFgColor(gdk.NewColor("#ffffff"))
			mtDrawable.DrawRectangle(mtGC, true, x*zoom, z*zoom, zoom, zoom)
		}
	}

	return
}

type MapWidget struct {
	dArea *gtk.DrawingArea
	w, h  int

	reportFail func(msg string)
	updateInfo func(x, z int, bio mcmap.Biome)

	isInit bool

	showBiomes bool

	offX, offZ                 int
	mx1, mx2, my1, my2         int
	primMButton, middleMButton bool

	pixmap   *gdk.Pixmap
	pixmapGC *gdk.GC
	gdkwin   *gdk.Window

	bg *gdk.Pixmap

	region *CachedRegion

	maptiles map[XZPos]*gdk.Pixmap
	biotiles map[XZPos]*gdk.Pixmap
	bioCache map[XZPos][]mcmap.Biome

	redraw   chan bool
	tileCmds chan tileCmd

	tool Tool
	bio  mcmap.Biome
}

var (
	checker1 = gdk.NewColor("#222222")
	checker2 = gdk.NewColor("#444444")
)

func emptyPixmap(w, h, depth int) *gdk.Pixmap {
	return gdk.NewPixmap(new(gdk.Drawable), w, h, depth)
}

func (mw *MapWidget) SetShowBiomes(b bool) {
	mw.showBiomes = b
	mw.redraw <- true
}

func (mw *MapWidget) SetTool(t Tool) {
	mw.tool = t
}

func (mw *MapWidget) SetBiome(bio mcmap.Biome) {
	mw.bio = bio
}

func (mw *MapWidget) DArea() *gtk.DrawingArea { return mw.dArea }

func (mw *MapWidget) doTileCmds() {
	for cmd := range mw.tileCmds {
		switch cmd {
		case cmdSave:
			if err := mw.region.Flush(); err != nil {
				mw.reportFail(fmt.Sprintf("Error while flushing cache: %s", err))
				return
			}
			if err := mw.region.Region.Save(); err != nil {
				mw.reportFail(fmt.Sprintf("Error while saving: %s", err))
				return
			}
		case cmdFlushTiles:
			if err := mw.region.Flush(); err != nil {
				mw.reportFail(fmt.Sprintf("Error while flushing cache: %s", err))
				return
			}

			gdk.ThreadsEnter()
			for _, mt := range mw.maptiles {
				mt.Unref()
			}
			for _, bt := range mw.biotiles {
				bt.Unref()
			}
			gdk.ThreadsLeave()

			mw.maptiles = make(map[XZPos]*gdk.Pixmap)
			mw.biotiles = make(map[XZPos]*gdk.Pixmap)
			mw.bioCache = make(map[XZPos][]mcmap.Biome)
		case cmdUpdateTiles:
			todelete := make(map[XZPos]bool)

			startX := int(math.Floor(float64(mw.offX) / tileSize))
			startZ := int(math.Floor(float64(mw.offZ) / tileSize))
			endX := int(math.Ceil(float64(mw.offX+mw.w) / tileSize))
			endZ := int(math.Ceil(float64(mw.offZ+mw.h) / tileSize))

			for pos := range mw.maptiles {
				if (pos.X < startX) || (pos.Z < startZ) || (pos.X >= endX) || (pos.Z >= endZ) {
					todelete[pos] = true
				}
			}

			gdk.ThreadsEnter()
			for pos := range todelete {
				if tile, ok := mw.maptiles[pos]; ok {
					tile.Unref()
					delete(mw.maptiles, pos)
				}

				if tile, ok := mw.biotiles[pos]; ok {
					tile.Unref()
					delete(mw.biotiles, pos)
				}

				if _, ok := mw.bioCache[pos]; ok {
					delete(mw.bioCache, pos)
				}
			}

			if mw.region != nil {
				for z := startZ; z < endZ; z++ {
				scanX:
					for x := startX; x < endX; x++ {
						pos := XZPos{x, z}

						if _, ok := mw.biotiles[pos]; ok {
							continue scanX
						}

						chunk, err := mw.region.Chunk(x, z)
						switch err {
						case nil:
						case mcmap.NotAvailable:
							continue scanX
						default:
							mw.reportFail(fmt.Sprintf("Could not get chunk %d, %d: %s", x, z, err))
							return
						}

						mw.maptiles[pos], mw.biotiles[pos], mw.bioCache[pos] = renderTile(chunk)
						chunk.MarkUnused()

						gdk.ThreadsLeave()
						mw.redraw <- true
						gdk.ThreadsEnter()
					}
				}
			}

			gdk.ThreadsLeave()
		}
	}
}

func (mw *MapWidget) configure() {
	if mw.pixmap != nil {
		mw.pixmap.Unref()
	}

	alloc := mw.dArea.GetAllocation()
	mw.w = alloc.Width
	mw.h = alloc.Height

	if !mw.isInit {
		mw.offX = -(mw.w / 2)
		mw.offZ = -(mw.h / 2)
		mw.isInit = true
	}

	mw.pixmap = gdk.NewPixmap(mw.dArea.GetWindow().GetDrawable(), mw.w, mw.h, 24)
	mw.pixmapGC = gdk.NewGC(mw.pixmap.GetDrawable())

	mw.drawBg()
	gdk.ThreadsLeave()
	mw.redraw <- true
	gdk.ThreadsEnter()
}

func (mw *MapWidget) drawBg() {
	if mw.bg != nil {
		mw.bg.Unref()
	}

	mw.bg = emptyPixmap(mw.w, mw.h, 24)
	drawable := mw.bg.GetDrawable()
	gc := gdk.NewGC(drawable)

	w := int(math.Ceil(float64(mw.w) / 32))
	h := int(math.Ceil(float64(mw.h) / 32))

	for y := 0; y < h; y++ {
		for x := 0; x < w; x++ {
			if (x % 2) == (y % 2) {
				gc.SetRgbFgColor(checker1)
			} else {
				gc.SetRgbFgColor(checker2)
			}
			drawable.DrawRectangle(gc, true, x*32, y*32, 32, 32)
		}
	}
}

func (mw *MapWidget) compose() {
	drawable := mw.pixmap.GetDrawable()
	gc := mw.pixmapGC

	drawable.DrawDrawable(gc, mw.bg.GetDrawable(), 0, 0, 0, 0, -1, -1)

	var tiles map[XZPos]*gdk.Pixmap
	if mw.showBiomes {
		tiles = mw.biotiles
	} else {
		tiles = mw.maptiles
	}

	for pos, tile := range tiles {
		x := (pos.X * tileSize) - mw.offX
		y := (pos.Z * tileSize) - mw.offZ

		drawable.DrawDrawable(gc, tile.GetDrawable(), 0, 0, x, y, tileSize, tileSize)
	}
}

func (mw *MapWidget) movement(ctx *glib.CallbackContext) {
	if mw.gdkwin == nil {
		mw.gdkwin = mw.dArea.GetWindow()
	}
	arg := ctx.Args(0)
	mev := *(**gdk.EventMotion)(unsafe.Pointer(&arg))
	var mt gdk.ModifierType
	if mev.IsHint != 0 {
		mw.gdkwin.GetPointer(&(mw.mx2), &(mw.my2), &mt)
	} else {
		mw.mx2, mw.my2 = int(mev.X), int(mev.Y)
	}

	x := (mw.offX + mw.mx2) / zoom
	z := (mw.offZ + mw.my2) / zoom
	cx, cz, cbx, cbz := mcmap.BlockToChunk(x, z)
	bio := mcmap.Biome(mcmap.BioUncalculated)
	if bc, ok := mw.bioCache[XZPos{cx, cz}]; ok {
		bio = bc[cbz*mcmap.ChunkSizeXZ+cbx]
	}
	mw.updateInfo(x, z, bio)

	if mw.middleMButton {
		if (mw.mx1 != -1) && (mw.my1 != -1) {
			mw.offX += mw.mx1 - mw.mx2
			mw.offZ += mw.my1 - mw.my2

			gdk.ThreadsLeave()
			mw.tileCmds <- cmdUpdateTiles
			gdk.ThreadsEnter()
		}
	}

	if mw.primMButton {
		mw.tool.Do(mw.bio, mw, x, z)

		gdk.ThreadsLeave()
		mw.redraw <- true
		gdk.ThreadsEnter()
	}

	mw.mx1, mw.my1 = mw.mx2, mw.my2
}

func (mw *MapWidget) Save() {
	mw.tileCmds <- cmdSave
}

func (mw *MapWidget) buttonChanged(ctx *glib.CallbackContext) {
	arg := ctx.Args(0)
	bev := *(**gdk.EventButton)(unsafe.Pointer(&arg))

	switch gdk.EventType(bev.Type) {
	case gdk.BUTTON_RELEASE:
		mw.primMButton, mw.middleMButton = false, false
	case gdk.BUTTON_PRESS:
		switch bev.Button {
		case 1:
			if mw.region == nil {
				return
			}
			x := (mw.offX + int(bev.X)) / zoom
			z := (mw.offZ + int(bev.Y)) / zoom
			mw.tool.Do(mw.bio, mw, x, z)

			gdk.ThreadsLeave()
			mw.redraw <- true
			gdk.ThreadsEnter()

			if !mw.tool.SingleClick() {
				mw.primMButton = true
			}
		case 2:
			mw.middleMButton = true
		}
	}
}

func (mw *MapWidget) expose() {
	mw.dArea.GetWindow().GetDrawable().DrawDrawable(mw.pixmapGC, mw.pixmap.GetDrawable(), 0, 0, 0, 0, -1, -1)
}

func (mw *MapWidget) guiUpdater() {
	for _ = range mw.redraw {
		gdk.ThreadsEnter()
		mw.compose()
		mw.expose()
		mw.dArea.GetWindow().Invalidate(nil, false)
		gdk.ThreadsLeave()
	}
}

func (mw *MapWidget) init() {
	mw.redraw = make(chan bool)
	mw.tileCmds = make(chan tileCmd)

	mw.maptiles = make(map[XZPos]*gdk.Pixmap)
	mw.biotiles = make(map[XZPos]*gdk.Pixmap)
	mw.bioCache = make(map[XZPos][]mcmap.Biome)

	mw.showBiomes = true

	mw.mx1, mw.my1 = -1, -1

	go mw.doTileCmds()
	go mw.guiUpdater()

	mw.dArea = gtk.NewDrawingArea()
	mw.dArea.Connect("configure-event", mw.configure)
	mw.dArea.Connect("expose-event", mw.expose)
	mw.dArea.Connect("motion-notify-event", mw.movement)
	mw.dArea.Connect("button-press-event", mw.buttonChanged)
	mw.dArea.Connect("button-release-event", mw.buttonChanged)

	mw.dArea.SetEvents(int(gdk.POINTER_MOTION_MASK | gdk.POINTER_MOTION_HINT_MASK | gdk.BUTTON_PRESS_MASK | gdk.BUTTON_RELEASE_MASK))
}

func (mw *MapWidget) setRegion(region *mcmap.Region) {
	mw.tileCmds <- cmdFlushTiles
	mw.region = NewCachedRegion(region, cacheSize)
	mw.tileCmds <- cmdUpdateTiles
}

func (mw *MapWidget) GetBiomeAt(x, z int) (mcmap.Biome, bool) {
	cx, cz, bx, bz := mcmap.BlockToChunk(x, z)
	pos := XZPos{cx, cz}

	if bc, ok := mw.bioCache[pos]; ok {
		return bc[bz*mcmap.ChunkSizeXZ+bx], true
	}

	chunk, err := mw.region.Chunk(cx, cz)
	switch err {
	case nil:
	case mcmap.NotAvailable:
		return mcmap.BioUncalculated, false
	default:
		mw.reportFail(fmt.Sprintf("Error while getting chunk %d, %d: %s", cx, cz, err))
		return mcmap.BioUncalculated, false
	}

	bc := make([]mcmap.Biome, mcmap.ChunkRectXZ)
	i := 0
	for z := 0; z < mcmap.ChunkSizeXZ; z++ {
		for x := 0; x < mcmap.ChunkSizeXZ; x++ {
			bc[i] = chunk.Biome(x, z)
			i++
		}
	}
	mw.bioCache[pos] = bc

	return chunk.Biome(bx, bz), true
}

func (mw *MapWidget) SetBiomeAt(x, z int, bio mcmap.Biome) {
	cx, cz, bx, bz := mcmap.BlockToChunk(x, z)
	pos := XZPos{cx, cz}

	chunk, err := mw.region.Chunk(cx, cz)
	switch err {
	case nil:
	case mcmap.NotAvailable:
		return
	default:
		mw.reportFail(fmt.Sprintf("Error while getting chunk %d, %d: %s", cx, cz, err))
		return
	}

	chunk.SetBiome(bx, bz, bio)
	chunk.MarkModified()

	// Update cache
	if bc, ok := mw.bioCache[pos]; ok {
		bc[bz*mcmap.ChunkSizeXZ+bx] = bio
	}

	// Update tile
	if tile, ok := mw.biotiles[pos]; ok {
		drawable := tile.GetDrawable()
		gc := gdk.NewGC(drawable)
		gc.SetRgbFgColor(bioColors[bio])
		drawable.DrawRectangle(gc, true, bx*zoom, bz*zoom, zoom, zoom)
	}
}

func NewMapWidget(reportFail func(msg string), updateInfo func(x, z int, bio mcmap.Biome)) *MapWidget {
	mw := &MapWidget{reportFail: reportFail, updateInfo: updateInfo}
	mw.init()
	return mw
}