simulation.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038 #include "simulation.h"
00039 #include <cmath>
00040
00041 Simulation::Simulation(QWidget *parent, const char *name)
00042 :QGLWidget(parent, name)
00043 {
00044 setFormat(QGLFormat(DoubleBuffer | DepthBuffer));
00045 sizeOfList = 0;
00046 angle = 0.0F;
00047 scale = 1.0F;
00048 right = false;
00049 maxX=minX=maxY=minY=0;
00050
00051 colors[0] = yellow;
00052 colors[1] = blue;
00053 colors[2] = green;
00054
00055 colors[3] = white;
00056 colors[4] = red;
00057 colors[5] = QColor(255,85,0);
00058 colors[6] = QColor(0,170,255);
00059 }
00060
00061
00062 void Simulation::initializeGL()
00063 {
00064 qglClearColor(black);
00065 glShadeModel(GL_FLAT);
00066 glEnable(GL_DEPTH_TEST);
00067 glEnable(GL_CULL_FACE);
00068 }
00069
00070 void Simulation::resizeGL(int w, int h)
00071 {
00072 glViewport(0, 0, w, h);
00073 glMatrixMode(GL_PROJECTION);
00074 glLoadIdentity();
00075 GLfloat x = (GLfloat)w/h;
00076 glFrustum(-x, x, -1.0, 1.0, 4.0, 15.0);
00077 glMatrixMode(GL_MODELVIEW);
00078 }
00079
00080 void Simulation::paintGL()
00081 {
00082 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
00083 draw();
00084 }
00085
00086 void Simulation::draw()
00087 {
00088 int i;
00089
00090 glMatrixMode(GL_MODELVIEW);
00091 glLoadIdentity();
00092 glTranslatef(0.0,0.0,-10.0);
00093
00094
00095 qglColor(white);
00096 glRotatef(0+angle,0,0,1);
00097 for(i=0 ; i<40 ; i++)
00098 {
00099 glBegin(GL_LINES);
00100 glVertex2f(-5.0+i*0.25, -5.0);
00101 glVertex2f(-5.0+i*0.25,5.0);
00102 glEnd();
00103 }
00104
00105
00106 glBegin(GL_TRIANGLES);
00107 glVertex2f(0.0,0.1);
00108 glVertex2f(-0.1,-0.1);
00109 glVertex2f(0.1,-0.1);
00110 glEnd();
00111 glBegin(GL_QUADS);
00112 glVertex2f(-0.025,-0.2);
00113 glVertex2f(0.025,-0.2);
00114 glVertex2f(0.025,-0.1);
00115 glVertex2f(-0.025,-0.1);
00116 glEnd();
00117
00118 }
00119
00120 void Simulation::mousePressEvent(QMouseEvent *event)
00121 {
00122 lastPos = event->pos();
00123 }
00124
00125 void Simulation::mouseReleaseEvent(QMouseEvent *event)
00126 {
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146 }
00147
00148 void Simulation::mouseMoveEvent(QMouseEvent *event)
00149 {
00150 int dx = event->x() - lastPos.x();
00151 int dy = event->y() - lastPos.y();
00152 lastPos = event->pos();
00153
00154 for(int i=0 ; i<sizeOfList ; i++)
00155 {
00156 if(event->state() & LeftButton)
00157 {
00158 if(
00159
00160
00161
00162 iconlist[i].selected ==2)
00163 {
00164 iconlist[i].setPosition(dx, dy);
00165 updateGL();
00166 }
00167 }
00168 else if(event->state() & RightButton)
00169 {
00170 }
00171
00172 }
00173 }
00174
00175 void Simulation::createIcon(int num)
00176 {
00177 int i;
00178 int oldSize = sizeOfList;
00179 sizeOfList = num;
00180
00181
00182 if(oldSize != 0 && oldSize <= sizeOfList)
00183 {
00184 iconImage *temp = (iconImage *)malloc(sizeof(iconImage)*(sizeOfList+4));
00185 for(i=0 ; i<oldSize ; i++)
00186 temp[i] = iconlist[i];
00187 for(i = oldSize ; i<sizeOfList ; i++)
00188 {
00189 iconlist[i].x = 300;
00190 iconlist[i].y = 220;
00191 iconlist[i].glx = 0.0;
00192 iconlist[i].gly = 0.0;
00193 iconlist[i].edgeSize = 22;
00194 iconlist[i].selected = 0;
00195 }
00196 }
00197 else
00198 {
00199 iconlist = (iconImage *)malloc(sizeof(iconImage)*sizeOfList);
00200 for(i=0; i<sizeOfList ; i++)
00201 {
00202
00203 iconlist[i].x = 300;
00204 iconlist[i].y = 220;
00205 iconlist[i].glx = 0.0;
00206 iconlist[i].gly = 0.0;
00207 iconlist[i].edgeSize = 22;
00208 iconlist[i].selected = 0;
00209 }
00210 }
00211 updateGL();
00212 }
00213
00214 void Simulation::currentItem(int item)
00215 {
00216 for(int i=0 ; i<sizeOfList ; i++)
00217 {
00218 if(i == item)
00219 iconlist[i].selected = 2;
00220 else
00221 {
00222 if( i < 4)
00223 iconlist[i].selected = 3+i;
00224 else
00225 iconlist[i].selected = 1;
00226 }
00227 }
00228 updateGL();
00229 }
00230
00231 void Simulation::getCoord(int i, int &x, int &y)
00232 {
00233 x = iconlist[i].x;
00234 y = iconlist[i].y;
00235 }
00236
00237 void Simulation::setCoordByPix(int i, int x, int y)
00238 {
00239 iconlist[i].x = x;
00240 iconlist[i].y = y;
00241 iconlist[i].glx = (GLfloat)(x-300) * 0.01137F;
00242 iconlist[i].gly = -(GLfloat)(y-220) * 0.01137F;
00243 updateGL();
00244 }
00245
00246 void Simulation::setCoordByGL(int i, float glx, float gly)
00247 {
00248 iconlist[i].glx = glx;
00249 iconlist[i].gly = gly;
00250 iconlist[i].x = int(glx/0.01137F + 300.0F + 0.5F);
00251 iconlist[i].y = int(gly/0.01137F + 220.0F + 0.5F);
00252 updateGL();
00253 }
00254