summaryrefslogtreecommitdiffstats
path: root/Tools/QtBiomeVisualiser/BiomeView.cpp
blob: 671942440ebb0b604cfcc19c22e7500e2540d4b3 (plain) (blame)
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
#include "Globals.h"
#include "BiomeView.h"
#include "Chunk.h"
#include <QPainter>
#include <QResizeEvent>





static const int DELTA_STEP = 120;  // The normal per-notch wheel delta





BiomeView::BiomeView(QWidget * parent) :
	super(parent),
	m_X(0),
	m_Z(0),
	m_Zoom(1),
	m_IsMouseDragging(false),
	m_MouseWheelDelta(0)
{
	// Create the image used for undefined chunks:
	int offset = 0;
	for (int y = 0; y < 16; y++)
	{
		for (int x = 0; x < 16; x++)
		{
			uchar color = (((x & 8) ^ (y & 8)) == 0) ? 0x44 : 0x88;
			m_EmptyChunkImage[offset++] = color;
			m_EmptyChunkImage[offset++] = color;
			m_EmptyChunkImage[offset++] = color;
			m_EmptyChunkImage[offset++] = 0xff;
		}
	}

	// Create the startup image:
	redraw();

	// Add a chunk-update callback mechanism:
	connect(&m_Cache, SIGNAL(chunkAvailable(int, int)), this, SLOT(chunkAvailable(int, int)));

	// Allow keyboard interaction:
	setFocusPolicy(Qt::StrongFocus);
}




QSize BiomeView::minimumSizeHint() const
{
	return QSize(300, 300);
}





QSize BiomeView::sizeHint() const
{
	return QSize(800, 600);
}





void BiomeView::setChunkSource(std::shared_ptr<ChunkSource> a_ChunkSource)
{
	// Replace the source in the cache:
	m_Cache.setChunkSource(a_ChunkSource);

	// Redraw with the new source:
	redraw();
}





void BiomeView::redraw()
{
	if (!hasData())
	{
		// No data means no image is displayed, no need to compose:
		update();
		return;
	}

	int chunksize = 16 * m_Zoom;

	// first find the center block position
	int centerchunkx = floor(m_X / 16);
	int centerchunkz = floor(m_Z / 16);
	// and the center of the screen
	int centerx = m_Image.width()  / 2;
	int centery = m_Image.height() / 2;
	// and align for panning
	centerx -= (m_X - centerchunkx * 16) * m_Zoom;
	centery -= (m_Z - centerchunkz * 16) * m_Zoom;
	// now calculate the topleft block on the screen
	int startx = centerchunkx - centerx / chunksize - 1;
	int startz = centerchunkz - centery / chunksize - 1;
	// and the dimensions of the screen in blocks
	int blockswide = m_Image.width()  / chunksize + 3;
	int blockstall = m_Image.height() / chunksize + 3;

	for (int z = startz; z < startz + blockstall; z++)
	{
		for (int x = startx; x < startx + blockswide; x++)
		{
			drawChunk(x, z);
		}
	}
	update();
}





void BiomeView::chunkAvailable(int a_ChunkX, int a_ChunkZ)
{
	drawChunk(a_ChunkX, a_ChunkZ);
	update();
}





void BiomeView::drawChunk(int a_ChunkX, int a_ChunkZ)
{
	if (!hasData())
	{
		return;
	}

	//fetch the chunk:
	ChunkPtr chunk = m_Cache.fetch(a_ChunkX, a_ChunkZ);

	// Figure out where on the screen this chunk should be drawn:
	// first find the center chunk
	int centerchunkx = floor(m_X / 16);
	int centerchunkz = floor(m_Z / 16);
	// and the center chunk screen coordinates
	int centerx = m_Image.width()  / 2;
	int centery = m_Image.height() / 2;
	// which need to be shifted to account for panning inside that chunk
	centerx -= (m_X - centerchunkx * 16) * m_Zoom;
	centery -= (m_Z - centerchunkz * 16) * m_Zoom;
	// centerx,y now points to the top left corner of the center chunk
	// so now calculate our x,y in relation
	double chunksize = 16 * m_Zoom;
	centerx += (a_ChunkX - centerchunkx) * chunksize;
	centery += (a_ChunkZ - centerchunkz) * chunksize;

	int srcoffset = 0;
	uchar * bits = m_Image.bits();
	int imgstride = m_Image.bytesPerLine();

	int skipx = 0,skipy = 0;
	int blockwidth = chunksize, blockheight = chunksize;
	// now if we're off the screen we need to crop
	if (centerx < 0)
	{
		skipx = -centerx;
		centerx = 0;
	}
	if (centery < 0)
	{
		skipy = -centery;
		centery = 0;
	}
	// or the other side, we need to trim
	if (centerx + blockwidth > m_Image.width())
	{
		blockwidth = m_Image.width() - centerx;
	}
	if (centery + blockheight > m_Image.height())
	{
		blockheight = m_Image.height() - centery;
	}
	if ((blockwidth <= 0) || (skipx >= blockwidth))
	{
		return;
	}
	int imgoffset = centerx * 4 + centery * imgstride;

	// If the chunk is valid, use its data; otherwise use the empty placeholder:
	const uchar * src = m_EmptyChunkImage;
	if (chunk.get() != nullptr)
	{
		src = chunk->getImage();
	}

	// Blit or scale-blit the image:
	for (int z = skipy; z < blockheight; z++, imgoffset += imgstride)
	{
		srcoffset = floor((double)z / m_Zoom) * 16 * 4;
		if (m_Zoom == 1.0)
		{
			memcpy(bits + imgoffset, src + srcoffset + skipx * 4, (blockwidth - skipx) * 4);
		}
		else
		{
			int xofs = 0;
			for (int x = skipx; x < blockwidth; x++, xofs +=4)
			{
				memcpy(bits + imgoffset + xofs, src + srcoffset + (int)floor((double)x / m_Zoom) * 4, 4);
			}
		}
	}
}





void BiomeView::resizeEvent(QResizeEvent * a_Event)
{
	m_Image = QImage(a_Event->size(), QImage::Format_RGB32);
	redraw();
}





void BiomeView::paintEvent(QPaintEvent * a_Event)
{
	QPainter p(this);
	if (hasData())
	{
		p.drawImage(QPoint(0, 0), m_Image);
	}
	else
	{
		p.drawText(a_Event->rect(), Qt::AlignCenter, "No chunk source selected");
	}
	p.end();
}





void BiomeView::mousePressEvent(QMouseEvent * a_Event)
{
	m_LastX = a_Event->x();
	m_LastY = a_Event->y();
	m_IsMouseDragging = true;
}





void BiomeView::mouseMoveEvent(QMouseEvent * a_Event)
{
	if (m_IsMouseDragging)
	{
		// The user is dragging the mouse, move the view around:
		m_X += (m_LastX - a_Event->x()) / m_Zoom;
		m_Z += (m_LastY - a_Event->y()) / m_Zoom;
		m_LastX = a_Event->x();
		m_LastY = a_Event->y();
		redraw();
		return;
	}

	// TODO: Update the status bar info for the biome currently pointed at
}





void BiomeView::mouseReleaseEvent(QMouseEvent *)
{
	m_IsMouseDragging = false;
}





void BiomeView::wheelEvent(QWheelEvent * a_Event)
{
	m_MouseWheelDelta += a_Event->delta();
	while (m_MouseWheelDelta >= DELTA_STEP)
	{
		increaseZoom();
		m_MouseWheelDelta -= DELTA_STEP;
	}
	while (m_MouseWheelDelta <= -DELTA_STEP)
	{
		decreaseZoom();
		m_MouseWheelDelta += DELTA_STEP;
	}
}





void BiomeView::keyPressEvent(QKeyEvent * a_Event)
{
	switch (a_Event->key())
	{
		case Qt::Key_Up:
		case Qt::Key_W:
		{
			m_Z -= 10.0 / m_Zoom;
			redraw();
			break;
		}

		case Qt::Key_Down:
		case Qt::Key_S:
		{
			m_Z += 10.0 / m_Zoom;
			redraw();
			break;
		}

		case Qt::Key_Left:
		case Qt::Key_A:
		{
			m_X -= 10.0 / m_Zoom;
			redraw();
			break;
		}

		case Qt::Key_Right:
		case Qt::Key_D:
		{
			m_X += 10.0 / m_Zoom;
			redraw();
			break;
		}

		case Qt::Key_PageUp:
		case Qt::Key_Q:
		{
			increaseZoom();
			break;
		}

		case Qt::Key_PageDown:
		case Qt::Key_E:
		{
			decreaseZoom();
			break;
		}
	}
}





void BiomeView::decreaseZoom()
{
	if (m_Zoom > 1.001)
	{
		m_Zoom--;
		if (m_Zoom < 1.0)
		{
			// Just crossed the 100%, fixate the 100% threshold:
			m_Zoom = 1.0;
		}
	}
	else if (m_Zoom > 0.01)
	{
		m_Zoom = m_Zoom / 2;
	}
	redraw();
}





void BiomeView::increaseZoom()
{
	if (m_Zoom > 0.99)
	{
		if (m_Zoom > 20.0)
		{
			// Zoom too large
			return;
		}
		m_Zoom++;
	}
	else
	{
		m_Zoom = m_Zoom * 2;
		if (m_Zoom > 1.0)
		{
			// Just crossed the 100%, fixate the 100% threshold:
			m_Zoom = 1.0;
		}
	}
	redraw();
}