Enum stuff

Sun, 08 May 2016 16:11:40 +0300

author
Teemu Piippo <teemu@compsta2.com>
date
Sun, 08 May 2016 16:11:40 +0300
changeset 1034
a05fc8256a3a
parent 1033
32153a1dbb15
child 1035
8a60250175ba

Enum stuff

src/addObjectDialog.cpp file | annotate | diff | comparison | revisions
src/dialogs.cpp file | annotate | diff | comparison | revisions
src/glRenderer.cpp file | annotate | diff | comparison | revisions
src/glRenderer.h file | annotate | diff | comparison | revisions
src/glShared.h file | annotate | diff | comparison | revisions
src/ldObject.h file | annotate | diff | comparison | revisions
src/macros.h file | annotate | diff | comparison | revisions
src/mainwindow.cpp file | annotate | diff | comparison | revisions
src/toolsets/viewtoolset.cpp file | annotate | diff | comparison | revisions
--- a/src/addObjectDialog.cpp	Sun May 08 15:26:58 2016 +0300
+++ b/src/addObjectDialog.cpp	Sun May 08 16:11:40 2016 +0300
@@ -80,7 +80,7 @@
 			for_enum (BfcStatement, i)
 			{
 				// Separate these in two columns
-				if (int (i) == int (BfcStatement::NumValues) / 2)
+				if (int(i) == ENUM_LIMIT(BfcStatement, Count) / 2)
 					rb_bfcType->rowBreak();
 
 				rb_bfcType->addButton (LDBfc::statementToString (i));
@@ -358,8 +358,8 @@
 		{
 			LDBfc* bfc = InitObject<LDBfc> (obj);
 			int value = dlg.rb_bfcType->value();
-			if (value == qBound (0, value, int (BfcStatement::NumValues) - 1))
-				bfc->setStatement (BfcStatement (dlg.rb_bfcType->value()));
+			if (valueInEnum<BfcStatement>(value))
+				bfc->setStatement(BfcStatement(value));
 		} break;
 
 		case OBJ_SubfileReference:
--- a/src/dialogs.cpp	Sun May 08 15:26:58 2016 +0300
+++ b/src/dialogs.cpp	Sun May 08 16:11:40 2016 +0300
@@ -52,18 +52,18 @@
 
 	m_cameraArgs =
 	{
-		{ ui->top,    ETopCamera },
-		{ ui->bottom, EBottomCamera },
-		{ ui->front,  EFrontCamera },
-		{ ui->back,   EBackCamera },
-		{ ui->left,   ELeftCamera },
-		{ ui->right,  ERightCamera }
+		{ ui->top,    TopCamera },
+		{ ui->bottom, BottomCamera },
+		{ ui->front,  FrontCamera },
+		{ ui->back,   BackCamera },
+		{ ui->left,   LeftCamera },
+		{ ui->right,  RightCamera }
 	};
 
 	Camera cam = g_win->renderer()->camera();
 
-	if (cam == EFreeCamera)
-		cam = ETopCamera;
+	if (cam == FreeCamera)
+		cam = TopCamera;
 
 	connect (ui->width, SIGNAL (valueChanged (double)), this, SLOT (slot_dimensionsChanged()));
 	connect (ui->height, SIGNAL (valueChanged (double)), this, SLOT (slot_dimensionsChanged()));
@@ -135,7 +135,7 @@
 
 int OverlayDialog::camera() const
 {
-	return RadioSwitch<int> (ETopCamera, m_cameraArgs);
+	return RadioSwitch<int> (TopCamera, m_cameraArgs);
 }
 
 void OverlayDialog::slot_fpath()
--- a/src/glRenderer.cpp	Sun May 08 15:26:58 2016 +0300
+++ b/src/glRenderer.cpp	Sun May 08 16:11:40 2016 +0300
@@ -43,7 +43,7 @@
 #include "documentmanager.h"
 #include "grid.h"
 
-const LDFixedCamera g_FixedCameras[6] =
+const CameraInfo g_cameraInfo[NumCameras] =
 {
 	{{  1,  0, 0 }, X, Z, false, false, false }, // top
 	{{  0,  0, 0 }, X, Y, false,  true, false }, // front
@@ -51,6 +51,7 @@
 	{{ -1,  0, 0 }, X, Z, false,  true, true }, // bottom
 	{{  0,  0, 0 }, X, Y,  true,  true, true }, // back
 	{{  0, -1, 0 }, Z, Y, false,  true, true }, // right
+	{{  1,  0, 0 }, X, Z, false, false, false }, // free (defensive dummy data)
 };
 
 ConfigOption (QColor BackgroundColor = "#FFFFFF")
@@ -99,9 +100,9 @@
 	connect (m_toolTipTimer, SIGNAL (timeout()), this, SLOT (slot_toolTipTimer()));
 
 	// Init camera icons
-	for (Camera cam = EFirstCamera; cam < ENumCameras; ++cam)
+	for (Camera cam = EFirstCamera; cam < NumCameras; ++cam)
 	{
-		const char* cameraIconNames[ENumCameras] =
+		const char* cameraIconNames[NumCameras] =
 		{
 			"camera-top", "camera-front", "camera-left",
 			"camera-bottom", "camera-back", "camera-right",
@@ -141,7 +142,7 @@
 	for (CameraIcon& info : m_cameraIcons)
 	{
 		// MATH
-		int x1 = (m_width - (info.camera != EFreeCamera ? 48 : 16)) + ((i % 3) * 16) - 1;
+		int x1 = (m_width - (info.camera != FreeCamera ? 48 : 16)) + ((i % 3) * 16) - 1;
 		int y1 = ((i / 3) * 16) + 1;
 
 		info.sourceRect = QRect (0, 0, 16, 16);
@@ -393,7 +394,7 @@
 	glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
 	glEnable (GL_DEPTH_TEST);
 
-	if (camera() != EFreeCamera)
+	if (camera() != FreeCamera)
 	{
 		glMatrixMode (GL_PROJECTION);
 		glPushMatrix();
@@ -402,15 +403,15 @@
 		glOrtho (-m_virtualWidth, m_virtualWidth, -m_virtualHeight, m_virtualHeight, -100.0f, 100.0f);
 		glTranslatef (panning (X), panning (Y), 0.0f);
 
-		if (camera() != EFrontCamera and camera() != EBackCamera)
+		if (camera() != FrontCamera and camera() != BackCamera)
 		{
-			glRotatef (90.0f, g_FixedCameras[camera()].glrotate[0],
-				g_FixedCameras[camera()].glrotate[1],
-				g_FixedCameras[camera()].glrotate[2]);
+			glRotatef (90.0f, g_cameraInfo[camera()].glrotate[0],
+				g_cameraInfo[camera()].glrotate[1],
+				g_cameraInfo[camera()].glrotate[2]);
 		}
 
 		// Back camera needs to be handled differently
-		if (camera() == EBackCamera)
+		if (camera() == BackCamera)
 		{
 			glRotatef (180.0f, 1.0f, 0.0f, 0.0f);
 			glRotatef (180.0f, 0.0f, 0.0f, 1.0f);
@@ -531,11 +532,11 @@
 //
 Vertex GLRenderer::convert2dTo3d (const QPoint& pos2d, bool snap) const
 {
-	if (camera() == EFreeCamera)
+	if (camera() == FreeCamera)
 		return Origin;
 
 	Vertex pos3d;
-	const LDFixedCamera* cam = &g_FixedCameras[camera()];
+	const CameraInfo* cam = &g_cameraInfo[camera()];
 	Axis axisX = cam->localX;
 	Axis axisY = cam->localY;
 	int signX = cam->negatedX ? -1 : 1;
@@ -571,10 +572,10 @@
 //
 QPoint GLRenderer::convert3dTo2d (const Vertex& pos3d)
 {
-	if (camera() == EFreeCamera)
+	if (camera() == FreeCamera)
 		return QPoint (0, 0);
 
-	const LDFixedCamera* cam = &g_FixedCameras[camera()];
+	const CameraInfo* cam = &g_cameraInfo[camera()];
 	const Axis axisX = cam->localX;
 	const Axis axisY = cam->localY;
 	const int negXFac = cam->negatedX ? -1 : 1;
@@ -638,7 +639,7 @@
 	}
 #endif
 
-	if (camera() != EFreeCamera and not isPicking())
+	if (camera() != FreeCamera and not isPicking())
 	{
 		// Paint the overlay image if we have one
 		const LDGLOverlay& overlay = currentDocumentData().overlays[camera()];
@@ -675,7 +676,7 @@
 		for (CameraIcon& info : m_cameraIcons)
 		{
 			// Don't draw the free camera icon when we can't use the free camera
-			if (&info == &m_cameraIcons[EFreeCamera] and not m_currentEditMode->allowFreeCamera())
+			if (&info == &m_cameraIcons[FreeCamera] and not m_currentEditMode->allowFreeCamera())
 				continue;
 
 			painter.drawPixmap (info.targetRect, info.image, info.sourceRect);
@@ -829,7 +830,7 @@
 			m_panning = true;
 			m_isCameraMoving = true;
 		}
-		else if (left and camera() == EFreeCamera)
+		else if (left and camera() == FreeCamera)
 		{
 			rotation (X) = rotation (X) + dy;
 			rotation (Y) = rotation (Y) + dx;
@@ -854,7 +855,7 @@
 #endif
 
 	// Calculate 3d position of the cursor
-	m_position3D = (camera() != EFreeCamera) ? convert2dTo3d (m_mousePosition, true) : Origin;
+	m_position3D = (camera() != FreeCamera) ? convert2dTo3d (m_mousePosition, true) : Origin;
 
 	highlightCursorObject();
 	update();
@@ -910,7 +911,7 @@
 void GLRenderer::setCamera (const Camera camera)
 {
 	// The edit mode may forbid the free camera.
-	if (m_currentEditMode->allowFreeCamera() or camera != EFreeCamera)
+	if (m_currentEditMode->allowFreeCamera() or camera != FreeCamera)
 	{
 		m_camera = camera;
 		m_config->setCamera((int) camera);
@@ -1043,8 +1044,8 @@
 	m_currentEditMode = AbstractEditMode::createByType (this, a);
 
 	// If we cannot use the free camera, use the top one instead.
-	if (camera() == EFreeCamera and not m_currentEditMode->allowFreeCamera())
-		setCamera (ETopCamera);
+	if (camera() == FreeCamera and not m_currentEditMode->allowFreeCamera())
+		setCamera (TopCamera);
 
 	m_window->updateEditModeActions();
 	update();
@@ -1104,7 +1105,7 @@
 //
 void GLRenderer::getRelativeAxes (Axis& relX, Axis& relY) const
 {
-	const LDFixedCamera* cam = &g_FixedCameras[camera()];
+	const CameraInfo* cam = &g_cameraInfo[camera()];
 	relX = cam->localX;
 	relY = cam->localY;
 }
@@ -1113,7 +1114,7 @@
 //
 Axis GLRenderer::getRelativeZ() const
 {
-	const LDFixedCamera* cam = &g_FixedCameras[camera()];
+	const CameraInfo* cam = &g_cameraInfo[camera()];
 	return (Axis) (3 - cam->localX - cam->localY);
 }
 
@@ -1174,7 +1175,7 @@
 	if (camid == (Camera) -1)
 		camid = camera();
 
-	const LDFixedCamera* cam = &g_FixedCameras[camid];
+	const CameraInfo* cam = &g_cameraInfo[camid];
 	return (y) ? cam->localY : cam->localX;
 }
 
@@ -1210,8 +1211,8 @@
 
 	Axis localX = getCameraAxis (false, camera);
 	Axis localY = getCameraAxis (true, camera);
-	int signX = g_FixedCameras[camera].negatedX ? -1 : 1;
-	int signY = g_FixedCameras[camera].negatedY ? -1 : 1;
+	int signX = g_cameraInfo[camera].negatedX ? -1 : 1;
+	int signY = g_cameraInfo[camera].negatedY ? -1 : 1;
 
 	info.v0 = info.v1 = Origin;
 	info.v0.setCoordinate (localX, -(info.offsetX * info.width * signX) / image->width());
@@ -1235,7 +1236,7 @@
 //
 void GLRenderer::clearOverlay()
 {
-	if (camera() == EFreeCamera)
+	if (camera() == FreeCamera)
 		return;
 
 	LDGLOverlay& info = currentDocumentData().overlays[camera()];
@@ -1249,7 +1250,7 @@
 //
 void GLRenderer::setDepthValue (double depth)
 {
-	if (camera() < EFreeCamera)
+	if (camera() < FreeCamera)
 		currentDocumentData().depthValues[camera()] = depth;
 }
 
@@ -1257,7 +1258,7 @@
 //
 double GLRenderer::getDepthValue() const
 {
-	if (camera() < EFreeCamera)
+	if (camera() < FreeCamera)
 		return currentDocumentData().depthValues[camera()];
 	else
 		return 0.0;
@@ -1269,13 +1270,13 @@
 {
 	switch (camera)
 	{
-	case ETopCamera: return tr ("Top Camera");
-	case EFrontCamera: return tr ("Front Camera");
-	case ELeftCamera: return tr ("Left Camera");
-	case EBottomCamera: return tr ("Bottom Camera");
-	case EBackCamera: return tr ("Back Camera");
-	case ERightCamera: return tr ("Right Camera");
-	case EFreeCamera: return tr ("Free Camera");
+	case TopCamera: return tr ("Top Camera");
+	case FrontCamera: return tr ("Front Camera");
+	case LeftCamera: return tr ("Left Camera");
+	case BottomCamera: return tr ("Bottom Camera");
+	case BackCamera: return tr ("Back Camera");
+	case RightCamera: return tr ("Right Camera");
+	case FreeCamera: return tr ("Free Camera");
 	default: break;
 	}
 
@@ -1427,9 +1428,9 @@
 //
 void GLRenderer::initOverlaysFromObjects()
 {
-	for (Camera camera = EFirstCamera; camera < ENumCameras; ++camera)
+	for (Camera camera = EFirstCamera; camera < NumCameras; ++camera)
 	{
-		if (camera == EFreeCamera)
+		if (camera == FreeCamera)
 			continue;
 
 		LDGLOverlay& meta = currentDocumentData().overlays[camera];
@@ -1456,9 +1457,9 @@
 //
 void GLRenderer::updateOverlayObjects()
 {
-	for (Camera cam = EFirstCamera; cam < ENumCameras; ++cam)
+	for (Camera cam = EFirstCamera; cam < NumCameras; ++cam)
 	{
-		if (cam == EFreeCamera)
+		if (cam == FreeCamera)
 			continue;
 
 		LDGLOverlay& meta = currentDocumentData().overlays[cam];
@@ -1604,12 +1605,12 @@
 	return m_position3D;
 }
 
-const LDFixedCamera& GLRenderer::cameraInfo (Camera camera) const
+const CameraInfo& GLRenderer::cameraInfo (Camera camera) const
 {
 	if (camera >= EFirstCamera and camera <= ELastFixedCamera)
-		return g_FixedCameras[camera];
+		return g_cameraInfo[camera];
 	else
-		return g_FixedCameras[0];
+		return g_cameraInfo[0];
 }
 
 bool GLRenderer::mouseHasMoved() const
@@ -1635,7 +1636,7 @@
 
 int GLRenderer::depthNegateFactor() const
 {
-	return g_FixedCameras[camera()].negatedDepth ? -1 : 1;
+	return g_cameraInfo[camera()].negatedDepth ? -1 : 1;
 }
 
 Qt::KeyboardModifiers GLRenderer::keyboardModifiers() const
--- a/src/glRenderer.h	Sun May 08 15:26:58 2016 +0300
+++ b/src/glRenderer.h	Sun May 08 16:11:40 2016 +0300
@@ -35,7 +35,7 @@
 class QTimer;
 class MagicWandMode;
 
-struct LDFixedCamera
+struct CameraInfo
 {
 	int glrotate[3];
 	Axis localX;
@@ -105,20 +105,20 @@
 
 enum Camera
 {
-	ETopCamera,
-	EFrontCamera,
-	ELeftCamera,
-	EBottomCamera,
-	EBackCamera,
-	ERightCamera,
-	EFreeCamera,
+	TopCamera,
+	FrontCamera,
+	LeftCamera,
+	BottomCamera,
+	BackCamera,
+	RightCamera,
+	FreeCamera,
 
-	ENumCameras,
-	ELastFixedCamera = ERightCamera,
-	EFirstCamera = ETopCamera
+	NumCameras,
+	ELastFixedCamera = RightCamera,
+	EFirstCamera = TopCamera
 };
 
-MAKE_ITERABLE_ENUM(Camera, ETopCamera, EFreeCamera)
+MAKE_ITERABLE_ENUM(Camera, TopCamera, FreeCamera)
 
 struct CameraIcon
 {
@@ -139,6 +139,7 @@
 	~GLRenderer();
 
 	Camera camera() const;
+	const CameraInfo& cameraInfo (Camera camera) const;
 	QString cameraName (Camera camera) const;
 	QByteArray capturePixels();
 	void clearOverlay();
@@ -157,7 +158,6 @@
 	void forgetObject (LDObject* obj);
 	Axis getCameraAxis (bool y, Camera camid = (Camera) -1);
 	double getDepthValue() const;
-	const LDFixedCamera& cameraInfo (Camera camera) const;
 	LDGLOverlay& getOverlay (int newcam);
 	void getRelativeAxes (Axis& relX, Axis& relY) const;
 	Axis getRelativeZ() const;
--- a/src/glShared.h	Sun May 08 15:26:58 2016 +0300
+++ b/src/glShared.h	Sun May 08 16:11:40 2016 +0300
@@ -68,7 +68,7 @@
 
 enum
 {
-	NumVbos = EnumLimits::SurfaceVboType::Count * EnumLimits::ComplementVboType::Count,
+	NumVbos = ENUM_LIMIT(SurfaceVboType, Count) * ENUM_LIMIT(ComplementVboType, Count),
 };
 
 #ifndef USE_QT5
--- a/src/ldObject.h	Sun May 08 15:26:58 2016 +0300
+++ b/src/ldObject.h	Sun May 08 16:11:40 2016 +0300
@@ -260,7 +260,7 @@
 //
 // Represents a 0 BFC statement in the LDraw code.
 //
-enum class BfcStatement
+enum BfcStatement
 {
 	CertifyCCW,
 	CCW,
@@ -272,10 +272,9 @@
 	ClipCCW,
 	ClipCW,
 	NoClip,
+};
 
-	NumValues,
-	FirstValue = CertifyCCW
-};
+MAKE_ITERABLE_ENUM(BfcStatement, CertifyCCW, NoClip)
 
 class LDBfc : public LDObject
 {
--- a/src/macros.h	Sun May 08 15:26:58 2016 +0300
+++ b/src/macros.h	Sun May 08 16:11:40 2016 +0300
@@ -44,7 +44,11 @@
 #define for_axes(AX) for (const Axis AX : std::initializer_list<const Axis> ({X, Y, Z}))
 
 #define MAKE_ITERABLE_ENUM(T, FIRST, LAST) \
-	namespace EnumLimits { namespace T { enum { First = FIRST, Last = LAST, Count = LAST - FIRST + 1 }; } } \
+	template<> \
+	struct EnumLimits<T> \
+	{\
+		enum { First = FIRST, Last = LAST, Count = LAST - FIRST + 1 };\
+	}; \
 	inline T operator++ (T& a) { a = (T) ((int) a + 1); return a; } \
 	inline T operator-- (T& a) { a = (T) ((int) a - 1); return a; } \
 	inline T operator++ (T& a, int) { T result = a; a = (T) ((int) a + 1); return result; } \
@@ -54,18 +58,28 @@
 # define USE_QT5
 #endif
 
+#define ConfigOption(...)
+
 #define FOR_ENUM_NAME_HELPER(LINE) enum_iterator_ ## LINE
 #define FOR_ENUM_NAME(LINE) FOR_ENUM_NAME_HELPER(LINE)
+#define FOR_ENUM_TYPE(ENUM) typename std::underlying_type<ENUM>::type
+#define ENUM_LIMIT(ENUM, T) FOR_ENUM_TYPE(ENUM)(EnumLimits<ENUM>::T)
 
 #define for_enum(ENUM, NAME) \
-	for (std::underlying_type<ENUM>::type FOR_ENUM_NAME (__LINE__) = \
-		std::underlying_type<ENUM>::type (ENUM::FirstValue); \
-		FOR_ENUM_NAME (__LINE__) < std::underlying_type<ENUM>::type (ENUM::NumValues); \
+	for (FOR_ENUM_TYPE(ENUM) FOR_ENUM_NAME(__LINE__) = ENUM_LIMIT(ENUM, First); \
+		FOR_ENUM_NAME (__LINE__) <= ENUM_LIMIT(ENUM, Last); \
 		++FOR_ENUM_NAME (__LINE__)) \
-	for (ENUM NAME = ENUM (FOR_ENUM_NAME (__LINE__)); NAME != ENUM::NumValues; \
-		NAME = ENUM::NumValues)
+	for (ENUM NAME = ENUM(FOR_ENUM_NAME(__LINE__)); NAME != ENUM_LIMIT(ENUM, Last) + 1; \
+		NAME = ENUM(EnumLimits<ENUM>::Last + 1))
+
+template<typename T>
+struct EnumLimits {};
 
-#define ConfigOption(...)
+template<typename Enum>
+bool valueInEnum(typename std::underlying_type<Enum>::type x)
+{
+	return x >= ENUM_LIMIT(Enum, First) and x <= ENUM_LIMIT(Enum, Last);
+}
 
 #define DEFINE_FLAG_ACCESS_METHODS \
 	bool checkFlag(Flag flag) const { return !!(m_flags & flag); } \
--- a/src/mainwindow.cpp	Sun May 08 15:26:58 2016 +0300
+++ b/src/mainwindow.cpp	Sun May 08 16:11:40 2016 +0300
@@ -772,7 +772,7 @@
 		contextMenu->addAction (ui.actionSubfileSelection);
 	}
 
-	if (renderer()->camera() != EFreeCamera)
+	if (renderer()->camera() != FreeCamera)
 	{
 		contextMenu->addSeparator();
 		contextMenu->addAction (ui.actionSetDrawDepth);
--- a/src/toolsets/viewtoolset.cpp	Sun May 08 15:26:58 2016 +0300
+++ b/src/toolsets/viewtoolset.cpp	Sun May 08 16:11:40 2016 +0300
@@ -179,7 +179,7 @@
 
 void ViewToolset::setDrawDepth()
 {
-	if (m_window->renderer()->camera() == EFreeCamera)
+	if (m_window->renderer()->camera() == FreeCamera)
 		return;
 
 	bool ok;

mercurial