Further fixes to bad color handling. Allow main color be represented with arbitrary transparency.

Mon, 25 Mar 2013 15:20:56 +0200

author
Santeri Piippo <crimsondusk64@gmail.com>
date
Mon, 25 Mar 2013 15:20:56 +0200
changeset 68
c637b172d565
parent 67
d523a370a17a
child 69
6790dea720a8

Further fixes to bad color handling. Allow main color be represented with arbitrary transparency.

cfgdef.h file | annotate | diff | comparison | revisions
colors.cpp file | annotate | diff | comparison | revisions
colors.h file | annotate | diff | comparison | revisions
config.cpp file | annotate | diff | comparison | revisions
gldraw.cpp file | annotate | diff | comparison | revisions
gldraw.h file | annotate | diff | comparison | revisions
gui.cpp file | annotate | diff | comparison | revisions
zz_addObjectDialog.cpp file | annotate | diff | comparison | revisions
zz_colorSelectDialog.cpp file | annotate | diff | comparison | revisions
zz_configDialog.cpp file | annotate | diff | comparison | revisions
zz_configDialog.h file | annotate | diff | comparison | revisions
--- a/cfgdef.h	Mon Mar 25 01:04:20 2013 +0200
+++ b/cfgdef.h	Mon Mar 25 15:20:56 2013 +0200
@@ -26,6 +26,7 @@
 SECT (gl, GLRenderer)
 CFG (str, gl, bgcolor, "Background color", "#CCCCD9")
 CFG (str, gl, maincolor, "Main color", "#707078")
+CFG (float, gl, maincolor_alpha, "Main color translucency [0.0 - 1.0]", 1.0)
 CFG (int, gl, linethickness, "Line thickness", 2)
 CFG (bool, gl, colorbfc, "Green-red BFC view", true)
 
--- a/colors.cpp	Mon Mar 25 01:04:20 2013 +0200
+++ b/colors.cpp	Mon Mar 25 15:20:56 2013 +0200
@@ -69,9 +69,10 @@
 	{79,	"Ghost White",	"#FFFFFF",	0.875},
 	{294,	"Trans Phosphorus",	"#E0FFB0",	0.6},
 	{378,	"Sand Green",	"#80A080",	1.0},
+	{511,	"Rubber White",	"#F8F8F8",	1.0},
 };
 
-color* g_LDColors[MAX_COLORS];
+static color* g_LDColors[MAX_COLORS];
 static bool g_bColorsInit = false;
 
 void initColors () {
@@ -89,4 +90,15 @@
 	}
 	
 	g_bColorsInit = true;
+}
+
+// =============================================================================
+// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
+// =============================================================================
+color* getColor (short dColorNum) {
+	// Check bounds
+	if (dColorNum < 0 || dColorNum >= MAX_COLORS)
+		return nullptr;
+	
+	return g_LDColors[dColorNum];
 }
\ No newline at end of file
--- a/colors.h	Mon Mar 25 01:04:20 2013 +0200
+++ b/colors.h	Mon Mar 25 15:20:56 2013 +0200
@@ -37,6 +37,8 @@
 } TemporaryColorMeta;
 
 void initColors ();
-extern color* g_LDColors[MAX_COLORS];
+
+// Safely gets a color with the given number or nullptr if no such color.
+color* getColor (short dColorNum);
 
 #endif // __COLORS_H__
\ No newline at end of file
--- a/config.cpp	Mon Mar 25 01:04:20 2013 +0200
+++ b/config.cpp	Mon Mar 25 15:20:56 2013 +0200
@@ -103,6 +103,9 @@
 // =============================================================================
 // Load the configuration from file
 bool config::load () {
+	// Locale must be disabled for atof
+	setlocale (LC_NUMERIC, "C");
+	
 	FILE* fp = fopen (filepath().chars(), "r");
 	char linedata[MAX_INI_LINE];
 	char* line;
@@ -230,6 +233,10 @@
 // =============================================================================
 // Save the configuration to disk
 bool config::save () {
+	// The function will write floats, disable the locale now so that they
+	// are written properly.
+	setlocale (LC_NUMERIC, "C");
+	
 #ifdef APPNAME
 	#ifdef CONFIG_WITH_QT
 		// If the directory doesn't exist, create it now.
--- a/gldraw.cpp	Mon Mar 25 01:04:20 2013 +0200
+++ b/gldraw.cpp	Mon Mar 25 15:20:56 2013 +0200
@@ -44,7 +44,7 @@
 	glLoadIdentity();
 	glMatrixMode (GL_MODELVIEW);
 	
-	setColor (gl_bgcolor.value, &glClearColor);
+	setBackground ();
 	
 	glEnable (GL_POLYGON_OFFSET_FILL);
 	glPolygonOffset (1.0f, 1.0f);
@@ -69,21 +69,36 @@
 // =============================================================================
 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 // =============================================================================
-void renderer::setColor (str zColor,
-	void (*func) (float, float, float, float))
-{
-	QColor col (zColor.chars());
+void renderer::setMainColor () {
+	QColor col (gl_maincolor.value.chars());
 	
 	if (!col.isValid ())
 		return;
 	
-	(*func) (
+	glColor4f (
+		((double)col.red()) / 255.0f,
+		((double)col.green()) / 255.0f,
+		((double)col.blue()) / 255.0f,
+		gl_maincolor_alpha);
+}
+
+// -----------------------------------------------------------------------------
+void renderer::setBackground () {
+	QColor col (gl_bgcolor.value.chars());
+	
+	if (!col.isValid ())
+		return;
+	
+	glClearColor (
 		((double)col.red()) / 255.0f,
 		((double)col.green()) / 255.0f,
 		((double)col.blue()) / 255.0f,
 		1.0f);
 }
 
+// =============================================================================
+// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
+// =============================================================================
 static vector<short> g_daWarnedColors;
 void renderer::setObjectColor (LDObject* obj, bool bBackSide) {
 	if (obj->dColor == -1)
@@ -100,35 +115,36 @@
 		return;
 	}
 	
-	if (obj->dColor == dMainColor)
-		setColor (gl_maincolor, glColor4f);
-	else {
-		color* col = g_LDColors[obj->dColor];
+	if (obj->dColor == dMainColor) {
+		setMainColor ();
+		return;
+	}
+	
+	color* col = getColor (obj->dColor);
+	
+	if (!col) {
+		// The color was unknown. Use main color to make the object at least
+		// not appear pitch-black.
+		setMainColor ();
 		
-		if (!col) {
-			// Warn about unknown colors, but only once.
-			for (long i = 0; i < g_daWarnedColors.size(); ++i)
-				if (g_daWarnedColors[i] == obj->dColor)
-					return;
-			
-			printf ("%s: Unknown color %d!\n", __func__, obj->dColor);
-			g_daWarnedColors.push_back (obj->dColor);
-			
-			// Set the main color to make the object at least not appear
-			// pitch-black.
-			setColor (gl_maincolor, glColor4f);
-			return;
-		}
+		// Warn about the unknown colors, but only once.
+		for (long i = 0; i < (long)g_daWarnedColors.size(); ++i)
+			if (g_daWarnedColors[i] == obj->dColor)
+				return;
 		
-		QColor qCol (col->zColor.chars());
-		
-		if (qCol.isValid ())
-			glColor4f (
-				((double)qCol.red()) / 255.0f,
-				((double)qCol.green()) / 255.0f,
-				((double)qCol.blue()) / 255.0f,
-				col->fAlpha);
+		printf ("%s: Unknown color %d!\n", __func__, obj->dColor);
+		g_daWarnedColors.push_back (obj->dColor);
+		return;
 	}
+	
+	QColor qCol (col->zColor.chars());
+	
+	if (qCol.isValid ())
+		glColor4f (
+			((double)qCol.red()) / 255.0f,
+			((double)qCol.green()) / 255.0f,
+			((double)qCol.blue()) / 255.0f,
+			col->fAlpha);
 }
 
 // =============================================================================
--- a/gldraw.h	Mon Mar 25 01:04:20 2013 +0200
+++ b/gldraw.h	Mon Mar 25 15:20:56 2013 +0200
@@ -30,7 +30,7 @@
 	renderer (QWidget* parent = nullptr);
 	void hardRefresh ();
 	void compileObjects ();
-	void setColor (str zColor, void (*func) (float, float, float, float));
+	void setBackground ();
 	
 	double fRotX, fRotY, fRotZ;
 	QPoint lastPos;
@@ -51,6 +51,7 @@
 	void compileVertex (vertex& vrt);
 	void clampAngle (double& fAngle);
 	void setObjectColor (LDObject* obj, bool bBackSide);
+	void setMainColor ();
 };
 
 #endif // __GLDRAW_H__
\ No newline at end of file
--- a/gui.cpp	Mon Mar 25 01:04:20 2013 +0200
+++ b/gui.cpp	Mon Mar 25 15:20:56 2013 +0200
@@ -665,7 +665,7 @@
 		{
 			// If the object isn't in the main or edge color, draw this
 			// list entry in said color.
-			color* col = g_LDColors[obj->dColor];
+			color* col = getColor (obj->dColor);
 			if (col)
 				item->setForeground (0, QColor (col->zColor.chars()));
 		}
--- a/zz_addObjectDialog.cpp	Mon Mar 25 01:04:20 2013 +0200
+++ b/zz_addObjectDialog.cpp	Mon Mar 25 15:20:56 2013 +0200
@@ -81,7 +81,7 @@
 		dColor = (type == OBJ_CondLine || type == OBJ_Line) ? dEdgeColor : dMainColor;
 		
 		qColorButton = new QPushButton;
-		setButtonBackground (qColorButton, g_LDColors[dColor]->zColor);
+		setButtonBackground (qColorButton, getColor (dColor)->zColor);
 		connect (qColorButton, SIGNAL (clicked ()), this, SLOT (slot_colorButtonClicked ()));
 	}
 	
@@ -141,7 +141,7 @@
 // =============================================================================
 void AddObjectDialog::slot_colorButtonClicked () {
 	ColorSelectDialog::staticDialog (dColor, dColor, this);
-	setButtonBackground (qColorButton, g_LDColors[dColor]->zColor);
+	setButtonBackground (qColorButton, getColor (dColor)->zColor);
 }
 
 // =============================================================================
--- a/zz_colorSelectDialog.cpp	Mon Mar 25 01:04:20 2013 +0200
+++ b/zz_colorSelectDialog.cpp	Mon Mar 25 15:20:56 2013 +0200
@@ -39,6 +39,10 @@
 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 // =============================================================================
 ColorSelectDialog::ColorSelectDialog (short dDefault, QWidget* parent) : QDialog (parent) {
+	// Remove the default color if it's invalid
+	if (!getColor (dDefault))
+		dDefault = -1;
+	
 	qScene = new QGraphicsScene;
 	qView = new QGraphicsView (qScene);
 	dSelColor = dDefault;
@@ -93,7 +97,7 @@
 	// Draw the color rectangles.
 	qScene->clear ();
 	for (short i = 0; i < MAX_COLORS; ++i) {
-		color* meta = g_LDColors[i];
+		color* meta = getColor (i);
 		if (!meta)
 			continue;
 		
@@ -125,13 +129,15 @@
 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 // =============================================================================
 void ColorSelectDialog::drawColorInfo () {
-	if (dSelColor == -1) {
+	color* col = getColor (dSelColor);
+	
+	if (dSelColor == -1 || !col) {
 		qColorInfo->setText ("---");
 		return;
 	}
 	
 	qColorInfo->setText (str::mkfmt ("%d - %s",
-		dSelColor, g_LDColors[dSelColor]->zName.chars()));
+		dSelColor, col->zName.chars()));
 }
 
 // =============================================================================
@@ -144,7 +150,7 @@
 	ulong y = ((ulong)qPoint.y () - (g_dSquareSize / 2)) / g_dSquareSize;
 	ulong idx = (y * g_dNumColumns) + x;
 	
-	color* col = g_LDColors[idx];
+	color* col = getColor (idx);
 	if (!col)
 		return;
 	
--- a/zz_configDialog.cpp	Mon Mar 25 01:04:20 2013 +0200
+++ b/zz_configDialog.cpp	Mon Mar 25 15:20:56 2013 +0200
@@ -59,11 +59,18 @@
 	connect (qGLForegroundButton, SIGNAL (clicked ()),
 		this, SLOT (slot_setGLForeground ()));
 	
+	qGLForegroundAlphaLabel = new QLabel ("Translucency:");
+	qGLForegroundAlpha = new QSlider (Qt::Horizontal);
+	qGLForegroundAlpha->setRange (1, 10);
+	qGLForegroundAlpha->setTickInterval (1);
+	qGLForegroundAlpha->setSliderPosition (gl_maincolor_alpha * 10.0f);
+	qGLForegroundAlpha->setTickPosition (QSlider::TicksAbove);
+	
 	qGLLineThicknessLabel = new QLabel ("Line thickness:");
 	qGLLineThickness = new QSlider (Qt::Horizontal);
 	qGLLineThickness->setRange (1, 8);
 	qGLLineThickness->setSliderPosition (gl_linethickness);
-	qGLLineThickness->setTickPosition (QSlider::TicksBothSides);
+	qGLLineThickness->setTickPosition (QSlider::TicksAbove);
 	qGLLineThickness->setTickInterval (1);
 	
 	qLVColorize = new QCheckBox ("Colorize polygons in list view");
@@ -86,6 +93,8 @@
 	
 	layout->addWidget (qGLLineThicknessLabel, 2, 0);
 	layout->addWidget (qGLLineThickness, 2, 1);
+	layout->addWidget (qGLForegroundAlphaLabel, 2, 2);
+	layout->addWidget (qGLForegroundAlpha, 2, 3);
 	
 	layout->addWidget (qLVColorize, 3, 0, 1, 2);
 	layout->addWidget (qGLColorBFC, 3, 2, 1, 2);
@@ -162,6 +171,7 @@
 		APPLY_CHECKBOX (dlg.qLVColorize, lv_colorize)
 		APPLY_CHECKBOX (dlg.qGLColorBFC, gl_colorbfc)
 		
+		gl_maincolor_alpha = ((double)dlg.qGLForegroundAlpha->value ()) / 10.0f;
 		gl_linethickness = dlg.qGLLineThickness->value ();
 		
 		// Save the config
@@ -170,7 +180,7 @@
 		// Reload all subfiles
 		reloadAllSubfiles ();
 		
-		window->R->setColor (gl_bgcolor, glClearColor);
+		window->R->setBackground ();
 		window->refresh ();
 	}
 }
\ No newline at end of file
--- a/zz_configDialog.h	Mon Mar 25 01:04:20 2013 +0200
+++ b/zz_configDialog.h	Mon Mar 25 15:20:56 2013 +0200
@@ -29,12 +29,13 @@
 	
 public:
 	QLabel* qLDrawPathLabel;
-	QLabel* qGLBackgroundLabel, *qGLForegroundLabel, *qGLLineThicknessLabel;
+	QLabel* qGLBackgroundLabel, *qGLForegroundLabel, *qGLForegroundAlphaLabel;
+	QLabel* qGLLineThicknessLabel;
 	QLineEdit* qLDrawPath;
 	QPushButton* qLDrawPathFindButton;
 	QPushButton* qGLBackgroundButton, *qGLForegroundButton;
 	QCheckBox* qLVColorize, *qGLColorBFC;
-	QSlider* qGLLineThickness;
+	QSlider* qGLForegroundAlpha, *qGLLineThickness;
 	
 	QDialogButtonBox* qButtons;
 	

mercurial