~narrow-m/mcplanner/trunk

« back to all changes in this revision

Viewing changes to world.cpp

  • Committer: Moritz Schmale
  • Date: 2011-01-09 23:40:11 UTC
  • Revision ID: narrow.m@gmail.com-20110109234011-b2cv8dbhildd41z2
First commit.
Features:
-Drawing Blocks on multiple Layers
-Saving, Loading
-Choosing Block to draw from an iconed list
-Generating a plain Block of specifieable height width and depth as well as top and fill BlockType.
-PainterlyPack is used for the artwork. In later Versions, you can specify an own terrain.png to use your favorite artwork.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include "world.h"
 
2
 
 
3
World::World(QObject *parent) :
 
4
    QGraphicsScene(parent)
 
5
{
 
6
        clear();
 
7
}
 
8
 
 
9
void World::save(QIODevice *device)
 
10
{
 
11
        //Presuming that the device is open
 
12
        if(!device->isOpen())
 
13
        {
 
14
                qDebug()<<"Device is not open";
 
15
                return;
 
16
        }
 
17
        QDataStream ds(device);
 
18
        QList<QVariant> layers;
 
19
        foreach(Layer *layer, myLayers.values()){
 
20
                QMap<QString, QVariant> data;
 
21
                data.insert("y", layer->y());
 
22
                QVariant matrix = layer->variantMatrix();
 
23
                data.insert("matrix", matrix);
 
24
                layers.append(data);
 
25
        }
 
26
        ds<<QVariant(layers);
 
27
}
 
28
 
 
29
void World::load(QIODevice *device)
 
30
{
 
31
        if(!device->isOpen()){
 
32
                qDebug()<<"Device is not open";
 
33
                return;
 
34
        }
 
35
        QDataStream ds(device);
 
36
        clear();
 
37
        QVariant data;
 
38
        ds>>data;
 
39
        QList<QVariant> layers=data.toList();
 
40
        foreach(QVariant layer, layers){
 
41
                QMap<QString, QVariant> layerData=layer.toMap();
 
42
                int y=layerData["y"].toInt();
 
43
                Layer *l;
 
44
                if(y!=0){
 
45
                        l=addLayer(y>0);
 
46
                } else {
 
47
                        l=myLayers.value(0);
 
48
                }
 
49
                QVariant matrix=layerData["matrix"];
 
50
                l->setVariantMatrix(matrix);
 
51
        }
 
52
}
 
53
 
 
54
Layer * World::addLayer(bool onTop)
 
55
{
 
56
        Layer *l=new Layer;
 
57
        addItem(l);
 
58
        int min, max;
 
59
        getMinMaxLayer(min, max);
 
60
        int layer;
 
61
        if(onTop)
 
62
                layer=max+1;
 
63
        else
 
64
                layer=min-1;
 
65
        myLayers.insert(layer, l);
 
66
        l->setWorld(this);
 
67
        l->setY(layer);
 
68
        l->setActiveLayer(true);
 
69
        l->resize();
 
70
        l->setZValue(layer);
 
71
        qDebug()<<"adding Layer"<<layer;
 
72
        emit(layerCountChanged(myLayers.count()));
 
73
        emit(activeLayerChanged(layer));
 
74
        return l;
 
75
}
 
76
 
 
77
void World::getMinMaxLayer(int &min, int &max)
 
78
{
 
79
        min=10; max=-1;
 
80
        foreach(int key, myLayers.keys()){
 
81
                if(key<min){
 
82
                        min=key;
 
83
                }
 
84
                if(key>max){
 
85
                        max=key;
 
86
                }
 
87
        }
 
88
}
 
89
 
 
90
void World::setActiveLayer(int layer)
 
91
{
 
92
        int min, max;
 
93
        getMinMaxLayer(min, max);
 
94
        foreach(Layer *l, myLayers.values()){
 
95
                l->setVisible(true);
 
96
                l->setActiveLayer(false);
 
97
        }
 
98
        if(layer>=min && layer <=max){
 
99
                if(layer!=max){
 
100
                        for(int i=layer+1;i<=max;i++){
 
101
                                myLayers.value(i)->setVisible(false);
 
102
                        }
 
103
                        myLayers.value(layer)->setActiveLayer(true);
 
104
                }
 
105
        } else {
 
106
                qDebug()<<"This layer does not exist:"<<layer<<". Should also never happen :(";
 
107
        }
 
108
}
 
109
 
 
110
Block * World::addBlock(QPoint pos, int y, BlockType type)
 
111
{
 
112
        int min, max;
 
113
        getMinMaxLayer(min, max);
 
114
        if(min<=y&&y<=max){
 
115
                Layer *l=myLayers.value(y);
 
116
                return l->addBlock(pos, type);
 
117
        } else {
 
118
                qDebug()<<"This layer does not exist:"<<y<<". Should also never happen :(";
 
119
                return 0;
 
120
        }
 
121
}
 
122
 
 
123
QRect World::boundaries()
 
124
{
 
125
        return myBoundaries;
 
126
}
 
127
 
 
128
void World::addPointToBoundaries(QPoint pos)
 
129
{
 
130
        QPainterPath path;
 
131
        path.addRect(myBoundaries);
 
132
        path.addRect(QRect(pos-QPoint(1,1), pos+QPoint(1,1)));
 
133
        myBoundaries=path.boundingRect().toRect();
 
134
        foreach(Layer *l, myLayers.values()){
 
135
                l->resize();
 
136
        }
 
137
}
 
138
 
 
139
void World::clear()
 
140
{
 
141
        QGraphicsScene::clear();
 
142
        myLayers.clear();
 
143
        myBoundaries=QRect(-1,-1,2,2);
 
144
        addLayer(true);
 
145
}