-
Notifications
You must be signed in to change notification settings - Fork 0
/
mainwindow.cpp
160 lines (117 loc) · 2.98 KB
/
mainwindow.cpp
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
/**
* QBoids
*
* Visual art based on Craig Reynolds' boids algorithm
* (www.red3d.com/cwr/boids/)
*
* Algorithms based on the pseudocode at:
* http://www.vergenet.net/~conrad/boids/pseudocode.html
*/
#include <QGraphicsScene>
#include <QTimer>
#include <QDebug>
#include <QKeyEvent>
#include <QAction>
#include <QMenuBar>
#include <QMenu>
#include <QSettings>
#include <QGraphicsSceneMouseEvent>
#include <math.h>
#include "mainwindow.hpp"
#include "boid.hpp"
#include "flock.hpp"
#include "landscape.hpp"
#include "landscapescene.hpp"
#include "simulation.hpp"
int INITIAL_NUM_BOIDS = 4;
MainWindow::MainWindow()
{
qsrand( 20 );
setGeometry( 0, 0, 500, 300 );
setupLandscape();
createActions();
createMenus();
createSimulation();
}
MainWindow::~MainWindow()
{}
void MainWindow::setupLandscape()
{
landscapeView_ = new Landscape( this );
landscapeView_->setGeometry( 0, 0, 500, 300 );
landscapeScene_ = new LandscapeScene( this, this );
landscapeScene_->setBackgroundBrush( Qt::black );
landscapeView_->setBackgroundBrush( Qt::black );
landscapeView_->setScene( landscapeScene_ );
landscapeScene_->setSceneRect( 0, 0, landscapeView_->viewport()->width(),
landscapeView_->viewport()->height() );
setCentralWidget( landscapeView_ );
}
void MainWindow::createActions()
{
QSettings settings( "NoodleSoft", "qboids" );
pauseAct_ = new QAction( tr("Toggle &Pause"), this );
QByteArray pauseKey = settings.value( "keys/pause", "Space" ).toByteArray();
pauseAct_->setShortcut( tr( pauseKey.data() ) );
pauseAct_->setStatusTip( tr("Pause/unpause the simulation") );
connect( pauseAct_, SIGNAL( triggered() ),
this, SLOT( togglePause() ) );
quitAct_ = new QAction(tr("&Quit"), this);
quitAct_->setShortcut(tr("Ctrl+Q"));
quitAct_->setStatusTip(tr("Quit the application"));
connect( quitAct_, SIGNAL(triggered()), this, SLOT(close()) );
}
void MainWindow::createMenus()
{
fileMenu_ = menuBar()->addMenu(tr("&File"));
fileMenu_->addAction(quitAct_);
simMenu_ = menuBar()->addMenu(tr("&Simulation"));
simMenu_->addAction( pauseAct_ );
}
void MainWindow::createSimulation()
{
simulation_ = new Simulation(
INITIAL_NUM_BOIDS,
landscapeView_,
landscapeScene_);
}
void MainWindow::addBoid()
{
simulation_->addBoid();
}
void MainWindow::addBoid2()
{
simulation_->addBoid2();
}
void MainWindow::removeBoid()
{
simulation_->removeBoid();
}
void MainWindow::removeBoid2()
{
simulation_->removeBoid2();
}
void MainWindow::togglePause()
{
simulation_->togglePause();
}
void MainWindow::toggleScatter()
{
simulation_->toggleScatter();
}
void MainWindow::toggleAvoid()
{
simulation_->toggleAvoid();
}
void MainWindow::toggleMatch()
{
simulation_->toggleMatch();
}
void MainWindow::toggleTails()
{
simulation_->toggleTails();
}
int MainWindow::ticksSinceStart()
{
return simulation_->ticksSinceStart();
}