Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Pan viewport with middle mouse button

  • Loading branch information...
commit 9f36ff76e8701630ff110ed7503ba90ad22048f4 1 parent 91e0e37
Antti Keränen authored January 23, 2013

Showing 2 changed files with 84 additions and 59 deletions. Show diff stats Hide diff stats

  1. 139  editor_gl.cpp
  2. 4  editor_gl.h
139  editor_gl.cpp
@@ -24,6 +24,9 @@ C_GLEditor::C_GLEditor(QWidget* parent, QStandardItem* root) :
24 24
 	m_ActivePoly=m_Polygons.back();
25 25
 	m_PointPrecision=0.01f;
26 26
 	m_SplitFirst=m_SplitSecond=NULL;
  27
+
  28
+	m_ViewPortX=0;
  29
+	m_ViewPortY=0;
27 30
 }
28 31
 C_GLEditor::~C_GLEditor()
29 32
 {
@@ -69,7 +72,7 @@ void C_GLEditor::M_PaintPolygon(const C_Polygon& p)
69 72
 	{
70 73
 		std::pair<float, float> pos=it->M_Pos();
71 74
 		qglColor(it->M_Color());
72  
-		glVertex2f(pos.first, pos.second);
  75
+		glVertex2f(m_ViewPortX+pos.first, m_ViewPortY+pos.second);
73 76
 	}
74 77
 	glEnd();
75 78
 }
@@ -89,7 +92,7 @@ void C_GLEditor::M_PaintPoints(const C_Polygon& p)
89 92
 		}
90 93
 		else qglColor(QColor::fromRgbF(0.4f, 0.4f, 0.4f, 1.0f));
91 94
 		std::pair<float, float> pos=it->M_Pos();
92  
-		glVertex3f(pos.first, pos.second, -1.0f);
  95
+		glVertex3f(m_ViewPortX+pos.first, m_ViewPortY+pos.second, -1.0f);
93 96
 	}
94 97
 	glEnd();
95 98
 }
@@ -134,47 +137,52 @@ bool C_GLEditor::M_MouseOverVertex(float x, float y, const C_Vertex& v)
134 137
 
135 138
 void C_GLEditor::mousePressEvent(QMouseEvent* e)
136 139
 {
137  
-	float x=M_RoundToPrecision((float)e->pos().x()/(this->width()/2)-1.0f, m_PointPrecision);
138  
-	float y=-M_RoundToPrecision((float)e->pos().y()/(this->height()/2)-1.0f, m_PointPrecision);
139  
-	if(m_Mode==Insert)
  140
+	float xraw=(float)e->pos().x()/(this->width()/2)-1.0f;
  141
+	float yraw=-((float)e->pos().y()/(this->height()/2)-1.0f);
  142
+	float x=M_RoundToPrecision(xraw, m_PointPrecision);
  143
+	float y=M_RoundToPrecision(yraw, m_PointPrecision);
  144
+	if(e->buttons() & Qt::LeftButton)
140 145
 	{
141  
-		emit S_MousePressed(m_ActivePoly->M_Root(),x,y,-1);
142  
-		m_Drag=false;
143  
-		for(C_Polygon::iterator it=m_ActivePoly->begin(); it!=m_ActivePoly->end(); ++it)
  146
+		if(m_Mode==Insert)
144 147
 		{
145  
-			it->M_SetSelection(false);
  148
+			emit S_MousePressed(m_ActivePoly->M_Root(),x-m_ViewPortX,y-m_ViewPortY,-1);
  149
+			m_Drag=false;
  150
+			for(C_Polygon::iterator it=m_ActivePoly->begin(); it!=m_ActivePoly->end(); ++it)
  151
+			{
  152
+				it->M_SetSelection(false);
  153
+			}
  154
+			m_ActivePoly->M_Last().M_SetSelection(true);
146 155
 		}
147  
-		m_ActivePoly->M_Last().M_SetSelection(true);
148  
-	}
149  
-	else
150  
-	{
151  
-		m_Drag=true;
152  
-		for(C_Polygon::iterator it=m_ActivePoly->begin(); it!=m_ActivePoly->end(); ++it)
  156
+		else
153 157
 		{
154  
-			if(M_MouseOverVertex(x,y,*it))
  158
+			m_Drag=true;
  159
+			for(C_Polygon::iterator it=m_ActivePoly->begin(); it!=m_ActivePoly->end(); ++it)
155 160
 			{
156  
-				m_Drag=false;
157  
-				if(!it->M_Selected())
  161
+				if(M_MouseOverVertex(xraw,yraw,*it))
158 162
 				{
159  
-					for(C_Polygon::iterator itt=m_ActivePoly->begin(); itt!=m_ActivePoly->end(); ++itt)
  163
+					m_Drag=false;
  164
+					if(!it->M_Selected())
160 165
 					{
161  
-						itt->M_SetSelection(false);
  166
+						for(C_Polygon::iterator itt=m_ActivePoly->begin(); itt!=m_ActivePoly->end(); ++itt)
  167
+						{
  168
+							itt->M_SetSelection(false);
  169
+						}
  170
+						it->M_SetSelection(true);
  171
+						break;
162 172
 					}
163  
-					it->M_SetSelection(true);
164  
-					break;
165 173
 				}
166 174
 			}
167  
-		}
168  
-		if(m_Drag)
169  
-		{
170  
-			for(C_Polygon::iterator itt=m_ActivePoly->begin(); itt!=m_ActivePoly->end(); ++itt)
  175
+			if(m_Drag)
171 176
 			{
172  
-				itt->M_SetSelection(false);
  177
+				for(C_Polygon::iterator itt=m_ActivePoly->begin(); itt!=m_ActivePoly->end(); ++itt)
  178
+				{
  179
+					itt->M_SetSelection(false);
  180
+				}
173 181
 			}
174 182
 		}
175 183
 	}
176 184
 	m_SplitFirst=m_SplitSecond=NULL;
177  
-	m_LastClick=std::make_pair(x,y);
  185
+	m_LastClick=std::make_pair(xraw,yraw);
178 186
 	m_LastMousePos=m_LastClick;
179 187
 	updateGL();
180 188
 }
@@ -211,14 +219,16 @@ void C_GLEditor::M_Split()
211 219
 
212 220
 void C_GLEditor::mouseMoveEvent(QMouseEvent* e)
213 221
 {
214  
-	float x=M_RoundToPrecision((float)e->pos().x()/(this->width()/2)-1.0f, m_PointPrecision);
215  
-	float y=-M_RoundToPrecision((float)e->pos().y()/(this->height()/2)-1.0f, m_PointPrecision);
  222
+	float xraw=(float)e->pos().x()/(this->width()/2)-1.0f;
  223
+	float yraw=-((float)e->pos().y()/(this->height()/2)-1.0f);
  224
+	float x=M_RoundToPrecision(xraw, m_PointPrecision);
  225
+	float y=M_RoundToPrecision(yraw, m_PointPrecision);
216 226
 	if(e->buttons() & Qt::LeftButton)
217 227
 	{
218 228
 		if(m_Mode==Insert)
219 229
 		{
220 230
 			m_Drag=false;
221  
-			emit S_SetPos(m_ActivePoly->M_Last(),x,y);
  231
+			emit S_SetPos(m_ActivePoly->M_Last(),x-m_ViewPortX,y-m_ViewPortY);
222 232
 			//m_ActivePoly->M_Last().M_SetPos(x,y);
223 233
 		}
224 234
 		else
@@ -228,10 +238,10 @@ void C_GLEditor::mouseMoveEvent(QMouseEvent* e)
228 238
 
229 239
 			if(m_Drag)
230 240
 			{
231  
-				m_DragPoints[0]=std::min(m_LastClick.first, x);
232  
-				m_DragPoints[1]=std::max(m_LastClick.first, x);
233  
-				m_DragPoints[2]=std::min(m_LastClick.second, y);
234  
-				m_DragPoints[3]=std::max(m_LastClick.second, y);
  241
+				m_DragPoints[0]=std::min(m_LastClick.first, xraw);
  242
+				m_DragPoints[1]=std::max(m_LastClick.first, xraw);
  243
+				m_DragPoints[2]=std::min(m_LastClick.second, yraw);
  244
+				m_DragPoints[3]=std::max(m_LastClick.second, yraw);
235 245
 
236 246
 				int selected=0;
237 247
 				int selectedsum=0;
@@ -275,49 +285,60 @@ void C_GLEditor::mouseMoveEvent(QMouseEvent* e)
275 285
 						std::pair<float, float> prevPos=it->M_Pos();
276 286
 						emit S_SetPos(
277 287
 							*it,
278  
-							prevPos.first+(x-lmx),
279  
-							prevPos.second+(y-lmy));
  288
+							prevPos.first+(x-M_RoundToPrecision(lmx, m_PointPrecision)),
  289
+							prevPos.second+(y-M_RoundToPrecision(lmy, m_PointPrecision)));
280 290
 					}
281 291
 				}
282 292
 			}
283 293
 		}
284 294
 	}
285  
-	else if(m_Mode==Edit)
  295
+	else
286 296
 	{
287  
-		for(C_Polygon::iterator it=m_ActivePoly->begin(); it!=m_ActivePoly->end(); ++it)
  297
+		if(e->buttons() & Qt::MiddleButton)
288 298
 		{
289  
-			if(M_MouseOverVertex(x,y,*it)) it->M_SetHovering(true);
290  
-			else it->M_SetHovering(false);
  299
+			m_ViewPortX-=m_LastMousePos.first - xraw;
  300
+			m_ViewPortY-=m_LastMousePos.second - yraw;
  301
+		}
  302
+		if(m_Mode==Edit)
  303
+		{
  304
+			for(C_Polygon::iterator it=m_ActivePoly->begin(); it!=m_ActivePoly->end(); ++it)
  305
+			{
  306
+				if(M_MouseOverVertex(xraw,yraw,*it)) it->M_SetHovering(true);
  307
+				else it->M_SetHovering(false);
  308
+			}
291 309
 		}
292 310
 	}
293 311
 	updateGL();
294  
-	m_LastMousePos=std::make_pair(x,y);
  312
+	m_LastMousePos=std::make_pair(xraw,yraw);
295 313
 }
296 314
 
297 315
 void C_GLEditor::mouseReleaseEvent(QMouseEvent* e)
298 316
 {
299  
-	float x=M_RoundToPrecision((float)e->pos().x()/(this->width()/2)-1.0f, m_PointPrecision);
300  
-	float y=-M_RoundToPrecision((float)e->pos().y()/(this->height()/2)-1.0f, m_PointPrecision);
301  
-	if(m_Mode==Insert)
302  
-	{
303  
-		emit S_SetPos(m_ActivePoly->M_Last(),x,y);
304  
-		m_ActivePoly->M_Last().M_SetSelection(false);
305  
-	}
306  
-	else
  317
+	if(e->buttons() & Qt::LeftButton)
307 318
 	{
308  
-		// Can't remember what this was for...caused some strange problems after m_Drag.
309  
-		/*
310  
-		for(C_Polygon::iterator it=m_ActivePoly->begin(); it!=m_ActivePoly->end(); ++it)
  319
+		float x=M_RoundToPrecision((float)e->pos().x()/(this->width()/2)-1.0f, m_PointPrecision);
  320
+		float y=-M_RoundToPrecision((float)e->pos().y()/(this->height()/2)-1.0f, m_PointPrecision);
  321
+		if(m_Mode==Insert)
311 322
 		{
312  
-			float lmx=m_LastMousePos.first;
313  
-			float lmy=m_LastMousePos.second;
314  
-			if(it->M_Selected())
  323
+			emit S_SetPos(m_ActivePoly->M_Last(),x-m_ViewPortX,y-m_ViewPortY);
  324
+			m_ActivePoly->M_Last().M_SetSelection(false);
  325
+		}
  326
+		else
  327
+		{
  328
+			// Can't remember what this was for...caused some strange problems after m_Drag.
  329
+			/*
  330
+			for(C_Polygon::iterator it=m_ActivePoly->begin(); it!=m_ActivePoly->end(); ++it)
315 331
 			{
316  
-				std::pair<float, float> prevPos=it->M_Pos();
317  
-				emit S_SetPos(*it,prevPos.first+(x-lmx),prevPos.second+(y-lmy));
  332
+				float lmx=m_LastMousePos.first;
  333
+				float lmy=m_LastMousePos.second;
  334
+				if(it->M_Selected())
  335
+				{
  336
+					std::pair<float, float> prevPos=it->M_Pos();
  337
+					emit S_SetPos(*it,prevPos.first+(x-lmx),prevPos.second+(y-lmy));
  338
+				}
318 339
 			}
  340
+			*/
319 341
 		}
320  
-		*/
321 342
 	}
322 343
 	if(m_Drag)
323 344
 	{
4  editor_gl.h
@@ -27,6 +27,10 @@ class C_GLEditor : public QGLWidget
27 27
 		C_Polygon* m_ActivePoly;
28 28
 		Mode m_Mode;
29 29
 		DrawMode m_DrawMode;
  30
+
  31
+		float m_ViewPortX;
  32
+		float m_ViewPortY;
  33
+
30 34
 		static const unsigned m_GridSize=20;
31 35
 		float m_Grid[m_GridSize];
32 36
 		std::pair<float, float> m_LastClick;

0 notes on commit 9f36ff7

Please sign in to comment.
Something went wrong with that request. Please try again.