Made drawing an edit mode, allowing me to add more modes in the future

Wed, 15 May 2013 01:25:04 +0300

author
Santeri Piippo <crimsondusk64@gmail.com>
date
Wed, 15 May 2013 01:25:04 +0300
changeset 207
e8c47e51e03d
parent 206
c069e7b5f5f3
child 208
0bb57b81c92a

Made drawing an edit mode, allowing me to add more modes in the future

icons/draw.png file | annotate | diff | comparison | revisions
icons/mode-draw.png file | annotate | diff | comparison | revisions
icons/mode-select.png file | annotate | diff | comparison | revisions
ldforge.qrc file | annotate | diff | comparison | revisions
src/extprogs.cpp file | annotate | diff | comparison | revisions
src/gldraw.cpp file | annotate | diff | comparison | revisions
src/gldraw.h file | annotate | diff | comparison | revisions
src/gui.cpp file | annotate | diff | comparison | revisions
src/gui.h file | annotate | diff | comparison | revisions
src/gui_actions.cpp file | annotate | diff | comparison | revisions
Binary file icons/draw.png has changed
Binary file icons/mode-draw.png has changed
Binary file icons/mode-select.png has changed
--- a/ldforge.qrc	Tue May 14 23:05:04 2013 +0300
+++ b/ldforge.qrc	Wed May 15 01:25:04 2013 +0300
@@ -36,7 +36,6 @@
 	<file>./icons/cut.png</file>
 	<file>./icons/delete.png</file>
 	<file>./icons/del-line.png</file>
-	<file>./icons/draw.png</file>
 	<file>./icons/empty.png</file>
 	<file>./icons/error.png</file>
 	<file>./icons/exit.png</file>
@@ -61,6 +60,8 @@
 	<file>./icons/line.png</file>
 	<file>./icons/mail.png</file>
 	<file>./icons/make-borders.png</file>
+	<file>./icons/mode-draw.png</file>
+	<file>./icons/mode-select.png</file>
 	<file>./icons/move-x-neg.png</file>
 	<file>./icons/move-x-pos.png</file>
 	<file>./icons/move-y-neg.png</file>
--- a/src/extprogs.cpp	Tue May 14 23:05:04 2013 +0300
+++ b/src/extprogs.cpp	Wed May 15 01:25:04 2013 +0300
@@ -106,6 +106,27 @@
 }
 
 // =============================================================================
+void writeObjects (std::vector<LDObject*>& objects, FILE* fp) {
+	for (LDObject* obj : objects) {
+		if (obj->getType () == LDObject::Subfile) {
+			vector<LDObject*> objs = static_cast<LDSubfile*> (obj)->inlineContents (true, false);
+			
+			writeObjects (objs, fp);
+			for (LDObject* obj : objs)
+				delete obj;
+		} else if (obj->getType () == LDObject::Radial) {
+			vector<LDObject*> objs = static_cast<LDRadial*> (obj)->decompose (true);
+			
+			writeObjects (objs, fp);
+			for (LDObject* obj : objs)
+				delete obj;
+		} else {
+			str line = fmt ("%s\r\n", obj->getContents ().chars ());
+			fwrite (line.chars(), 1, ~line, fp);
+		}
+	}
+}
+
 void writeObjects (std::vector<LDObject*>& objects, str fname) {
 	// Write the input file
 	FILE* fp = fopen (fname, "w");
@@ -114,10 +135,7 @@
 		return;
 	}
 	
-	for (LDObject* obj : objects) {
-		str line = fmt ("%s\r\n", obj->getContents ().chars ());
-		fwrite (line.chars(), 1, ~line, fp);
-	}
+	writeObjects (objects, fp);
 	
 #ifndef RELEASE
 	ushort idx = rand ();
--- a/src/gldraw.cpp	Tue May 14 23:05:04 2013 +0300
+++ b/src/gldraw.cpp	Wed May 15 01:25:04 2013 +0300
@@ -98,7 +98,7 @@
 	m_picking = m_rangepick = false;
 	m_camera = (GL::Camera) gl_camera.value;
 	m_drawToolTip = false;
-	m_planeDraw = false;
+	m_editmode = Select;
 	
 	m_toolTipTimer = new QTimer (this);
 	m_toolTipTimer->setSingleShot (true);
@@ -501,16 +501,16 @@
 		paint.drawText (m_width - textSize.width (), m_height - 16, textSize.width (),
 			textSize.height (), Qt::AlignCenter, text);
 		
-		// If we're plane drawing, draw the vertices onto the screen.
-		if (m_planeDraw) {
-			ushort numverts = m_planeDrawVerts.size () + 1;
+		// If we're drawing, draw the vertices onto the screen.
+		if (m_editmode == Draw) {
+			ushort numverts = m_drawedVerts.size () + 1;
 			const short blipsize = 8;
 			
 			if (numverts > 0) {
 				QPoint* poly = new QPoint[numverts];
 				
 				uchar i = 0;
-				for (vertex& vert : m_planeDrawVerts) {
+				for (vertex& vert : m_drawedVerts) {
 					poly[i] = coordconv3_2 (vert);
 					++i;
 				}
@@ -541,7 +541,7 @@
 	}
 	
 	// Camera icons
-	if (!m_picking && m_planeDraw == false) {
+	if (!m_picking && m_editmode == Select) {
 		// Draw a background for the selected camera
 		paint.setPen (m_thinBorderPen);
 		paint.setBrush (QBrush (QColor (0, 128, 160, 128)));
@@ -712,7 +712,7 @@
 				g_glInvert = !g_glInvert;
 			
 			LDObject* prev = ref->prev ();
-			if (prev->getType () == LDObject::BFC && static_cast<LDBFC*> (prev)->type == LDBFC::InvertNext)
+			if (prev && prev->getType () == LDObject::BFC && static_cast<LDBFC*> (prev)->type == LDBFC::InvertNext)
 				g_glInvert = !g_glInvert;
 			
 			for (LDObject* obj : objs) {
@@ -734,7 +734,7 @@
 				g_glInvert = !g_glInvert;
 			
 			LDObject* prev = rad->prev ();
-			if (prev->getType () == LDObject::BFC && static_cast<LDBFC*> (prev)->type == LDBFC::InvertNext)
+			if (prev && prev->getType () == LDObject::BFC && static_cast<LDBFC*> (prev)->type == LDBFC::InvertNext)
 				g_glInvert = !g_glInvert;
 			
 			for (LDObject* obj : objs) {
@@ -813,22 +813,22 @@
 	const bool wasRight = (m_lastButtons & Qt::RightButton) && !(ev->buttons() & Qt::RightButton);
 	
 	if (wasLeft) {
-		if (m_planeDraw) {
+		if (m_editmode == Draw) {
 			// If we have 4 verts, stop drawing.
-			if (m_planeDrawVerts.size () >= 4) {
-				endPlaneDraw (true);
+			if (m_drawedVerts.size () >= 4) {
+				endDraw (true);
 				return;
 			}
 			
 			// If we picked an already-existing vertex, stop drawing
-			for (vertex& vert : m_planeDrawVerts) {
+			for (vertex& vert : m_drawedVerts) {
 				if (vert == m_hoverpos) {
-					endPlaneDraw (true);
+					endDraw (true);
 					return;
 				}
 			}
 			
-			m_planeDrawVerts.push_back (m_hoverpos);
+			m_drawedVerts.push_back (m_hoverpos);
 			
 			update ();
 			return;
@@ -845,13 +845,10 @@
 		return;
 	}
 	
-	if (wasRight && m_planeDraw) {
-		if (m_planeDrawVerts.size () > 0) {
+	if (wasRight && m_editmode == Draw) {
+		if (m_drawedVerts.size () > 0) {
 			// Remove the last vertex
-			m_planeDrawVerts.erase (m_planeDrawVerts.end () - 1);
-		} else {
-			endPlaneDraw (false);
-			return;
+			m_drawedVerts.erase (m_drawedVerts.end () - 1);
 		}
 		
 		update ();
@@ -942,6 +939,7 @@
 void GLRenderer::setCamera (const GL::Camera cam) {
 	m_camera = cam;
 	gl_camera = (int) cam;
+	g_win->updateEditModeActions ();
 }
 
 // =============================================================================
@@ -1088,32 +1086,44 @@
 }
 
 // =============================================================================
-void GLRenderer::beginPlaneDraw () {
-	if (m_camera == Free)
-		return; // Cannot draw with the free camera
-	
-	m_planeDraw = true;
+void GLRenderer::setEditMode (EditMode mode) {
+	switch (mode) {
+	case Select:
+		unsetCursor ();
+		setContextMenuPolicy (Qt::DefaultContextMenu);
+		break;
 	
-	// Disable the context menu - we need the right mouse button
-	// for removing vertices.
-	setContextMenuPolicy (Qt::NoContextMenu);
+	case Draw:
+		if (m_camera == Free)
+			return; // Cannot draw with the free camera
+		
+		// Disable the context menu - we need the right mouse button
+		// for removing vertices.
+		setContextMenuPolicy (Qt::NoContextMenu);
+		
+		// Use the crosshair cursor when drawing.
+		setCursor (Qt::CrossCursor);
+		
+		// Clear the selection when beginning to draw.
+		// FIXME: make the selection clearing stuff in ::pick a method and use it
+		// here! This code doesn't update the GL lists.
+		g_win->sel ().clear ();
+		g_win->updateSelection ();
+		m_drawedVerts.clear ();
+		break;
+	}
 	
-	// Use the crosshair cursor when plane drawing.
-	setCursor (Qt::CrossCursor);
+	m_editmode = mode;
 	
-	// Clear the selection when beginning to draw onto a plane.
-	// FIXME: make the selection clearing stuff in ::pick a method and use it
-	// here! This code doesn't update the GL lists.
-	g_win->sel ().clear ();
-	g_win->updateSelection ();
+	g_win->updateEditModeActions ();
 	update ();
 }
 
 // =============================================================================
-void GLRenderer::endPlaneDraw (bool accept) {
+void GLRenderer::endDraw (bool accept) {
 	// If we accepted, clean the selection and create the object
 	if (accept) {
-		vector<vertex>& verts = m_planeDrawVerts;
+		vector<vertex>& verts = m_drawedVerts;
 		LDObject* obj = null;
 		
 		switch (verts.size ()) {
@@ -1159,15 +1169,7 @@
 		}
 	}
 	
-	m_planeDraw = false;
-	m_planeDrawVerts.clear ();
-	
-	unsetCursor ();
-	
-	// Restore the context menu
-	setContextMenuPolicy (Qt::DefaultContextMenu);
-	
-	update ();
+	m_drawedVerts.clear ();
 }
 
 // =============================================================================
--- a/src/gldraw.h	Tue May 14 23:05:04 2013 +0300
+++ b/src/gldraw.h	Wed May 15 01:25:04 2013 +0300
@@ -42,16 +42,8 @@
 	Q_OBJECT
 	
 public:
-	enum Camera {
-		Top,
-		Front,
-		Left,
-		Bottom,
-		Back,
-		Right,
-		Free
-	};
-	
+	enum Camera { Top, Front, Left, Bottom, Back, Right, Free };
+	enum EditMode { Select, Draw };
 	enum ListType { NormalList, PickList, BFCFrontList, BFCBackList };
 	
 	GLRenderer (QWidget* parent = null);
@@ -63,7 +55,8 @@
 	void		clearOverlay		();
 	void		compileObject		(LDObject* obj);
 	void		compileAllObjects	();
-	void		endPlaneDraw		(bool accept);
+	EditMode	editMode			() const { return m_editmode; }
+	void		endDraw		(bool accept);
 	QColor		getMainColor		();
 	void		hardRefresh		();
 	bool		picking				() const { return m_picking; }
@@ -71,7 +64,8 @@
 	void		resetAngles		();
 	uchar*		screencap			(ushort& w, ushort& h);
 	void		setBackground		();
-	void		setCamera			(const GLRenderer::Camera cam);
+	void		setCamera			(const Camera cam);
+	void		setEditMode		(const EditMode mode);
 	void		setupOverlay		();
 	void		setZoom				(const double zoom) { m_zoom = zoom; }
 	double		zoom				() const { return m_zoom; }
@@ -98,13 +92,14 @@
 	ulong m_totalmove;
 	vertex m_hoverpos;
 	double m_virtWidth, m_virtHeight, m_rotX, m_rotY, m_rotZ, m_panX, m_panY, m_zoom;
-	bool m_darkbg, m_picking, m_rangepick, m_addpick, m_drawToolTip, m_screencap, m_planeDraw;
+	bool m_darkbg, m_picking, m_rangepick, m_addpick, m_drawToolTip, m_screencap;
 	QPoint m_pos, m_rangeStart;
 	QPen m_thinBorderPen, m_thickBorderPen;
 	Camera m_camera, m_toolTipCamera;
 	uint m_axeslist;
 	ushort m_width, m_height;
-	std::vector<vertex> m_planeDrawVerts;
+	std::vector<vertex> m_drawedVerts;
+	EditMode m_editmode;
 	
 	void	calcCameraIcons	();												// Compute geometry for camera icons
 	void	clampAngle			(double& angle) const;							// Clamps an angle to [0, 360]
--- a/src/gui.cpp	Tue May 14 23:05:04 2013 +0300
+++ b/src/gui.cpp	Wed May 15 01:25:04 2013 +0300
@@ -54,6 +54,11 @@
 extern_cfg (float, gl_maincolor_alpha);
 extern_cfg (bool, gl_wireframe);
 
+const char* g_modeActionNames[] = {
+	"modeSelect",
+	"modeDraw"
+};
+
 // =============================================================================
 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 // =============================================================================
@@ -115,10 +120,12 @@
 	
 	findAction ("axes")->setCheckable (true);
 	findAction ("axes")->setChecked (gl_axes);
-	
+
 	findAction ("wireframe")->setCheckable (true);
 	findAction ("wireframe")->setChecked (gl_wireframe);
 	
+	updateEditModeActions ();
+	
 	// things not implemented yet
 	findAction ("help")->setEnabled (false);
 	
@@ -187,7 +194,8 @@
 	addMenuAction ("newVertex");			// New Vertex
 	addMenuAction ("newRadial");			// New Radial
 	menu->addSeparator ();					// -----
-	addMenuAction ("draw");				// Draw Mode
+	addMenuAction ("modeSelect");			// Select Mode
+	addMenuAction ("modeDraw");			// Draw Mode
 	
 	// Edit menu
 	initMenu ("&Edit");
@@ -257,7 +265,6 @@
 	addMenuAction ("aboutQt");				// About Qt
 }
 
-
 // =============================================================================
 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 // =============================================================================
@@ -373,8 +380,6 @@
 	addToolBar (Qt::RightToolBarArea, m_colorToolBar);
 	
 	// ==========================================
-	// Left area toolbars
-	//g_ToolBarArea = Qt::LeftToolBarArea;
 	initSingleToolBar ("Tools");
 	addToolBarAction ("setColor");
 	addToolBarAction ("invert");
@@ -398,6 +403,13 @@
 	addToolBarAction ("intersector");
 	addToolBarAction ("isecalc");
 	addToolBarAction ("coverer");
+	
+	// ==========================================
+	g_ToolBarArea = Qt::LeftToolBarArea;
+	initSingleToolBar ("Modes");
+	addToolBarAction ("modeSelect");
+	addToolBarAction ("modeDraw");
+	
 	updateToolBars ();
 }
 
@@ -703,21 +715,25 @@
 	const QList<QListWidgetItem*> items = m_objList->selectedItems ();
 	
 	for (LDObject* obj : g_curfile->m_objs)
-	for (QListWidgetItem* qItem : items) {
-		if (qItem == obj->qObjListEntry) {
+	for (QListWidgetItem* item : items) {
+		if (item == obj->qObjListEntry) {
 			m_sel.push_back (obj);
 			break;
 		}
 	}
 	
 	// Update the GL renderer
-	for (LDObject* obj : m_sel)
+	for (LDObject* obj : m_sel) {
+		printf ("recompile %lu\n", obj->getIndex (g_curfile));
 		m_renderer->compileObject (obj);
+	}
 	
-	for (LDObject* obj : priorSelection)
+	for (LDObject* obj : priorSelection) {
+		printf ("recompile %lu\n", obj->getIndex (g_curfile));
 		m_renderer->compileObject (obj);
+	}
 	
-	m_renderer->refresh ();
+	m_renderer->update ();
 }
 
 // =============================================================================
@@ -939,6 +955,22 @@
 }
 
 // ========================================================================================================================================
+void ForgeWindow::updateEditModeActions () {
+	const GL::EditMode mode = R ()->editMode ();
+	const size_t numModeActions = (sizeof g_modeActionNames / sizeof *g_modeActionNames);
+	assert ((size_t) mode < numModeActions);
+	
+	for (size_t i = 0; i < numModeActions; ++i) {
+		QAction* act = findAction (g_modeActionNames[i]);
+		
+		act->setCheckable (true);
+		act->setChecked (i == (size_t) mode);
+	}
+	
+	findAction ("modeDraw")->setEnabled (R ()->camera () != GL::Free);
+}
+
+// ========================================================================================================================================
 void ObjectList::contextMenuEvent (QContextMenuEvent* ev) {
 	g_win->spawnContextMenu (ev->globalPos ());
 }
--- a/src/gui.h	Tue May 14 23:05:04 2013 +0300
+++ b/src/gui.h	Wed May 15 01:25:04 2013 +0300
@@ -131,6 +131,7 @@
 	void updateRecentFilesMenu ();
 	void updateSelection ();
 	void updateGridToolBar ();
+	void updateEditModeActions ();
 	bool isSelected (LDObject* obj);
 	short getSelectedColor();
 	LDObject::Type uniformSelectedType ();
--- a/src/gui_actions.cpp	Tue May 14 23:05:04 2013 +0300
+++ b/src/gui_actions.cpp	Wed May 15 01:25:04 2013 +0300
@@ -261,17 +261,17 @@
 // =============================================================================
 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 // =============================================================================
-MAKE_ACTION (gridCoarse, "Coarse Grid", "grid-coarse", "Set the grid to Coarse", CTRL (1)) {
+MAKE_ACTION (gridCoarse, "Coarse Grid", "grid-coarse", "Set the grid to Coarse", (0)) {
 	grid = Grid::Coarse;
 	g_win->updateGridToolBar ();
 }
 
-MAKE_ACTION (gridMedium, "Medium Grid", "grid-medium", "Set the grid to Medium", CTRL (2)) {
+MAKE_ACTION (gridMedium, "Medium Grid", "grid-medium", "Set the grid to Medium", (0)) {
 	grid = Grid::Medium;
 	g_win->updateGridToolBar ();
 }
 
-MAKE_ACTION (gridFine, "Fine Grid", "grid-fine", "Set the grid to Fine", CTRL (3)) {
+MAKE_ACTION (gridFine, "Fine Grid", "grid-fine", "Set the grid to Fine", (0)) {
 	grid = Grid::Fine;
 	g_win->updateGridToolBar ();
 }
@@ -397,11 +397,6 @@
 }
 
 // =========================================================================================================================================
-MAKE_ACTION (draw, "Draw Mode", "draw", "Begin drawing geometry", KEY (Insert)) {
-	g_win->R ()->beginPlaneDraw ();
-}
-
-// =========================================================================================================================================
 MAKE_ACTION (visibility, "Toggle Visibility", "visibility", "Toggles visibility/hiding on objects.", (0)) {
 	for (LDObject* obj : g_win->sel ())
 		obj->setHidden (!obj->hidden ());
@@ -421,4 +416,13 @@
 
 MAKE_ACTION (clearOverlay, "Clear Overlay Image", "overlay-clear", "Clear the overlay image.", (0)) {
 	g_win->R ()->clearOverlay ();
+}
+
+// =========================================================================================================================================
+MAKE_ACTION (modeSelect, "Select Mode", "mode-select", "Select objects from the camera view.", CTRL (1)) {
+	g_win->R ()->setEditMode (GL::Select);
+}
+
+MAKE_ACTION (modeDraw, "Draw Mode", "mode-draw", "Draw objects into the camera view.", CTRL (2)) {
+	g_win->R ()->setEditMode (GL::Draw);
 }
\ No newline at end of file

mercurial