Further work on the primitive generator

Tue, 02 Jul 2013 00:06:06 +0300

author
Santeri Piippo <crimsondusk64@gmail.com>
date
Tue, 02 Jul 2013 00:06:06 +0300
changeset 311
c6e38e5e4f33
parent 310
c62edce5668c
child 312
97620579d86c

Further work on the primitive generator

src/common.h file | annotate | diff | comparison | revisions
src/dialogs.cpp file | annotate | diff | comparison | revisions
src/ldtypes.cpp file | annotate | diff | comparison | revisions
src/primitives.cpp file | annotate | diff | comparison | revisions
src/widgets.cpp file | annotate | diff | comparison | revisions
src/widgets.h file | annotate | diff | comparison | revisions
--- a/src/common.h	Mon Jul 01 23:07:35 2013 +0300
+++ b/src/common.h	Tue Jul 02 00:06:06 2013 +0300
@@ -176,6 +176,9 @@
 static const bool yup = true;
 static const bool nope = false;
 
+static const str CALicense = "!LICENSE Redistributable under CCAL version 2.0 : see CAreadme.txt",
+	NonCALicense = "!LICENSE Not redistributable : see NonCAreadme.txt";
+
 class ForgeWindow;
 class LDObject;
 class bbox;
--- a/src/dialogs.cpp	Mon Jul 01 23:07:35 2013 +0300
+++ b/src/dialogs.cpp	Tue Jul 02 00:06:06 2013 +0300
@@ -411,18 +411,18 @@
 		LDBFC::NoCertify;
 	
 	idx = dlg.rb_license->value ();
-	const char* license =
-		(idx == CCAL) ? "Redistributable under CCAL version 2.0 : see CAreadme.txt" :
-		(idx == NonCA) ? "Not redistributable : see NonCAreadme.txt" :
-		null;
+	const str license =
+		(idx == CCAL) ? CALicense :
+		(idx == NonCA) ? NonCALicense :
+		"";
 	
 	*g_curfile << new LDComment (dlg.le_name->text ());
 	*g_curfile << new LDComment ("Name: <untitled>.dat");
 	*g_curfile << new LDComment (fmt ("Author: %1", author));
 	*g_curfile << new LDComment (fmt ("!LDRAW_ORG Unofficial_Part"));
 	
-	if (license != null)
-		*g_curfile << new LDComment (fmt ("!LICENSE %1", license));
+	if( license != "" )
+		*g_curfile << new LDComment ( license );
 	
 	*g_curfile << new LDEmpty;
 	*g_curfile << new LDBFC (BFCType);
--- a/src/ldtypes.cpp	Mon Jul 01 23:07:35 2013 +0300
+++ b/src/ldtypes.cpp	Tue Jul 02 00:06:06 2013 +0300
@@ -551,32 +551,7 @@
 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 // =============================================================================
 str LDRadial::makeFileName () {
-	short numer = segments (),
-		denom = divisions ();
-	
-	// Simplify the fractional part, but the denominator must be at least 4.
-	simplify (numer, denom);
-	
-	if (denom < 4) {
-		const short factor = (4 / denom);
-		
-		numer *= factor;
-		denom *= factor;
-	}
-	
-	// Compose some general information: prefix, fraction, root, ring number
-	str prefix = (divisions () == lores) ? "" : fmt ("%1/", divisions ());
-	str frac = fmt ("%1-%2", numer, denom);
-	str root = g_radialNameRoots[type ()];
-	str num = (type () == Ring || type () == Cone) ? fmt ("%1", number ()) : "";
-	
-	// Truncate the root if necessary (7-16rin4.dat for instance).
-	// However, always keep the root at least 2 characters.
-	short extra = (frac.length () + num.length () + root.length ()) - 8;
-	root.chop (min<short> (max<short> (extra, 0), 2));
-	
-	// Stick them all together and return the result.
-	return (prefix + frac + root + num + ".dat");
+	return "";
 }
 
 // =============================================================================
--- a/src/primitives.cpp	Mon Jul 01 23:07:35 2013 +0300
+++ b/src/primitives.cpp	Tue Jul 02 00:06:06 2013 +0300
@@ -24,6 +24,7 @@
 #include "gui.h"
 #include "primitives.h"
 #include "ui_makeprim.h"
+#include "misc.h"
 
 vector<PrimitiveCategory> g_PrimitiveCategories;
 static PrimitiveLister* g_activePrimLister = null;
@@ -373,11 +374,13 @@
 					v1( x1, 0.0f, z1 ),
 					v2( x2, 0.0f, z2 );
 				
+				// Disc negatives need to go the other way around, otherwise
+				// they'll end up upside-down.
 				LDTriangle* seg = new LDTriangle;
 				seg->setColor( maincolor );
-				seg->setVertex( 0, v0 );
+				seg->setVertex( type == Disc ? 0 : 2, v0 );
 				seg->setVertex( 1, v1 );
-				seg->setVertex( 2, v2 );
+				seg->setVertex( type == Disc ? 2 : 0, v2 );
 				obj = seg;
 			}
 			break;
@@ -403,6 +406,49 @@
 	                          "Cone";
 }
 
+static const str g_radialNameRoots[] = {
+	"edge",
+	"cyli",
+	"disc",
+	"ndis",
+	"ring",
+	"con"
+};
+
+// =============================================================================
+// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
+// =============================================================================
+str radialFileName( PrimitiveType type, int segs, int divs, int num )
+{
+	short numer = segs,
+		denom = divs;
+	
+	// Simplify the fractional part, but the denominator must be at least 4.
+	simplify( numer, denom );
+	
+	if( denom < 4 )
+	{
+		const short factor = 4 / denom;
+		
+		numer *= factor;
+		denom *= factor;
+	}
+	
+	// Compose some general information: prefix, fraction, root, ring number
+	str prefix = (divs == lores) ? "" : fmt( "%1/", divs );
+	str frac = fmt( "%1-%2", numer, denom );
+	str root = g_radialNameRoots[type];
+	str numstr = ( type == Ring || type == Cone ) ? fmt ( "%1", num ) : "";
+	
+	// Truncate the root if necessary (7-16rin4.dat for instance).
+	// However, always keep the root at least 2 characters.
+	int extra = ( frac.length() + numstr.length() + root.length() ) - 8;
+	root.chop( min<short>( max<short>( extra, 0 ), 2 ));
+	
+	// Stick them all together and return the result.
+	return prefix + frac + root + numstr + ".dat";
+}
+
 // =============================================================================
 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 // =============================================================================
@@ -412,7 +458,6 @@
 	Ui::MakePrimUI ui;
 	ui.setupUi( dlg );
 	
-exec:
 	if( !dlg->exec() )
 		return;
 	
@@ -428,23 +473,32 @@
 		                              Cone;
 	
 	// Make the description
-	str descr = fmt ("%1 / %2 %3", segs, divs, primitiveTypeName( type ));
+	str frac = ftoa(( (float) segs ) / divs );
+	str name = radialFileName( type, segs, divs, num );
+	str descr;
+	
+	// Ensure that there's decimals, even if they're 0.
+	if( frac.indexOf( "." ) == -1 )
+		frac += ".0";
 	
 	if (type == Ring || type == Cone)
-		descr += fmt (" %1", num);
+		descr = fmt( "%1 %2 x %3", primitiveTypeName( type ), num, frac );
+	else
+		descr = fmt( "%1 %2", primitiveTypeName( type ), frac );
 	
 	LDOpenFile* f = new LDOpenFile;
+	f->setName( QFileDialog::getSaveFileName( null, QObject::tr( "Save Primitive" ), name ));
 	
 	*f << new LDComment( descr );
-	*f << new LDComment( fmt( "Name: ???.dat" ));
+	*f << new LDComment( fmt( "Name: %1", name ));
 	*f << new LDComment( fmt( "Author: LDForge" ));
 	*f << new LDComment( fmt( "!LDRAW_ORG Unofficial_%1Primitive", divs == hires ? "48_" : "" ));
-	*f << new LDComment( "Redistributable under CCAL version 2.0 : see CAreadme.txt" );
+	*f << new LDComment( CALicense );
 	*f << new LDEmpty;
 	*f << new LDBFC( LDBFC::CertifyCCW );
 	*f << new LDEmpty;
 	*f << makePrimitive( type, segs, divs, num );
 	
-	g_win->save( f, true );
+	g_win->save( f, false );
 	delete f;
 }
\ No newline at end of file
--- a/src/widgets.cpp	Mon Jul 01 23:07:35 2013 +0300
+++ b/src/widgets.cpp	Tue Jul 02 00:06:06 2013 +0300
@@ -174,4 +174,17 @@
 
 bool CheckBoxGroup::buttonChecked (int key) {
 	return m_vals[key]->isChecked ();
+}
+
+SpinBox::SpinBox( QWidget* parent ) : QSpinBox( parent ) {}
+SpinBox::SpinBox() : QSpinBox() {}
+
+void SpinBox::enable()
+{
+	setEnabled( true );
+}
+
+void SpinBox::disable()
+{
+	setEnabled( false );
 }
\ No newline at end of file
--- a/src/widgets.h	Mon Jul 01 23:07:35 2013 +0300
+++ b/src/widgets.h	Tue Jul 02 00:06:06 2013 +0300
@@ -20,6 +20,7 @@
 #define WIDGETS_H
 
 #include <QGroupBox>
+#include <QSpinBox>
 #include <map>
 #include "common.h"
 #include "types.h"
@@ -107,4 +108,22 @@
 	void buttonChanged		();
 };
 
+// =============================================================================
+// SpinBox
+// =============================================================================
+class SpinBox : public QSpinBox
+{
+	Q_OBJECT
+public:
+	explicit SpinBox( QWidget* parent = 0 );
+	SpinBox();
+	
+public slots:
+	void enable();
+	void disable();
+	
+private:
+	Q_DISABLE_COPY( SpinBox )
+};
+
 #endif // WIDGETS_H
\ No newline at end of file

mercurial