Project::OSiRiON - Git repositories
Project::OSiRiON
News . About . Screenshots . Downloads . Forum . Wiki . Tracker . Git
summaryrefslogtreecommitdiff
blob: 32ec3a0a8fdb5f1030db443a947eaa7c1b1ecfbf (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
/*
   mapwidget.cc
   This file is part of the Project::OSiRiON world editor 
   and is distributed under the terms and conditions of 
   the GNU General Public License version 2
*/

#include "mapwidget.h"
#include "mapentity.h"

#include <QPainter>
#include <QWheelEvent>
#include <QDebug>

namespace editor
{

MapWidget::MapWidget(QWidget *parent) : QWidget(parent)
{
	//setMinimumSize(256,256);
	
	// zoom sucks but it works
	mapwidget_zoom = 16;
}

void MapWidget::wheelEvent(QWheelEvent *event)
{
	if (event->orientation() == Qt::Vertical) {
		mapwidget_zoom -= event->delta() / 120;
		if (mapwidget_zoom < 4)
			mapwidget_zoom = 4;
		else if (mapwidget_zoom > 64) {
			mapwidget_zoom = 64;
		}
		
		resizeChildren();
		update();
		event->accept();		
	}
}

void MapWidget::resizeChildren()
{
	const float scale = (float) width() / (float) (mapwidget_zoom * 256);
	
	for (int i = 0; i < mapwidget_enties.size(); ++i) {
		MapEntity * entity = mapwidget_enties.at(i);
		int iconsize = 12;
		int x = width() / 2 + (int) (entity->location(0) * scale) - iconsize /2;
		int y = height() / 2 + (int)(entity->location(1) * scale) - iconsize /2;
		
		entity->setGeometry(x, y, iconsize, iconsize);
		//qDebug() << "Moving entity to " << x << " " << y;
	}

}

void MapWidget::resizeEvent (QResizeEvent *event)
{
	resizeChildren();
}

void MapWidget::paintEvent(QPaintEvent *event)
{
	QPen pen(Qt::blue, 1, Qt::SolidLine);
	QPainter painter(this);
	
	painter.setPen(pen);

	// mapwidget_zoom is used to scale from game units to screen coorinates	
	// 1 grid square is 256 game units
	// gridsize is the size of one grid square in screen coordinates
	int gridsize = width() / mapwidget_zoom;
	
	int x = width() / 2;
	while (x > 0) {
		painter.drawLine(x, 0, x, height());
		x -= gridsize;
	}
	x = width() / 2 + gridsize;
	while (x <= width()) {
		painter.drawLine(x, 0, x, height());
		x += gridsize;
	}
	
	int y = height() / 2;
	while (y > 0) {
		painter.drawLine(0, y, width(), y);
		y -= gridsize;
	}
	y = height() / 2 + gridsize;
	while (y <= height()) {
		painter.drawLine(0, y, width(), y);
		y += gridsize;
	}

	QWidget::paintEvent(event);
}

MapEntity *MapWidget::addEntity()
{
	MapEntity *mapentity = new MapEntity(this);
	mapwidget_enties.append(mapentity);
	
	mapentity->show();
	
	resizeChildren();
	update();
	
	return mapentity;
}

}