#include "CraftDialog.h" #include <qcolordialog.h> #include <qcursor.h> #include <qfiledialog.h> #include <qgrid.h> #include <qmessagebox.h> #include <qpixmap.h> #include <qpushbutton.h> #include <qslider.h> #include <qvbox.h> #include <math.h> CraftDialog::CraftDialog(void) { b1 = QColor( 0, 0, 0 ); b2 = QColor( 16, 64, 32 ); f1 = QColor( 32, 128, 200 ); f2 = QColor( 32, 136, 164 ); depth = 4; best = 0x0; Rule* r; QGrid* grid = new QGrid( 2, this ); choices = new CraftButton*[4]; for ( int c = 0 ; c < 4 ; c++ ) { r = new Rule(); r->InitBasic(); r->Mutate(); choices[c] = new CraftButton( grid, r ); choices[c]->garden = new Garden(); connect( choices[c], SIGNAL( Clicked( CraftButton* ) ), this, SLOT( Generate( CraftButton* ) ) ); } grid->setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Expanding ); QVBox* options = new QVBox( this ); new QWidget( options ); colors = new QButtonGroup( 2, Horizontal, "Colors", options ); new QPushButton( "Change Color", colors ); new QPushButton( "Change Color", colors ); new QPushButton( "Change Color", colors ); new QPushButton( "Change Color", colors ); connect( colors, SIGNAL( clicked(int) ), this, SLOT( ChangeColor(int) ) ); colors->setSizePolicy( QSizePolicy::Fixed, QSizePolicy::Fixed ); new QWidget( options ); QLabel* dlabel = new QLabel( "Pattern complexity", options ); QSlider* dslider = new QSlider( 2, 8, 1, depth, Horizontal, options ); dlabel->setBuddy( dslider ); dslider->setSizePolicy( QSizePolicy::MinimumExpanding, QSizePolicy::Fixed ); dlabel->setSizePolicy( QSizePolicy::Fixed, QSizePolicy::Fixed ); connect( dslider, SIGNAL( valueChanged(int) ), this, SLOT( ChangeDepth(int) ) ); new QWidget( options ); favorite = new QLabel( options ); favorite->setScaledContents( true ); favorite->setFixedSize( 200, 200 ); QPushButton* save = new QPushButton( "Save...", options ); connect( save, SIGNAL( clicked() ), this, SLOT( SaveFavorite() ) ); new QWidget( options ); options->setSizePolicy( QSizePolicy::Fixed, QSizePolicy::Minimum ); UpdateColors(); MakeUnique(); } CraftDialog::~CraftDialog(void) { delete[4] choices; } void CraftDialog::ChangeColor( int c ) { QColor oldcol; switch( c ) { case 0: oldcol = b1; break; case 1: oldcol = b2; break; case 2: oldcol = f1; break; case 3: oldcol = f2; break; } QColor newcol = QColorDialog::getColor( oldcol, this ); switch( c ) { case 0: b1 = newcol; break; case 1: b2 = newcol; break; case 2: f1 = newcol; break; case 3: f2 = newcol; break; } UpdateColors(); } void CraftDialog::ChangeDepth( int d ) { depth = d; UpdateColors(); } void CraftDialog::Generate( CraftButton* b ) { setCursor( QCursor::BusyCursor ); best = b; favorite->setPixmap( *b->pixmap() ); for ( int c = 0 ; c < 4 ; c++ ) { if ( choices[c] == b ) continue; choices[c]->rule->Copy( b->rule ); delete choices[c]->garden; choices[c]->garden = new Garden(); choices[c]->rule->Mutate(); FillButton( choices[c] ); } MakeUnique(); setCursor( QCursor::ArrowCursor ); } void CraftDialog::SaveFavorite() { if ( !best ) { QMessageBox::information ( this, "Oops.", QString("You haven't ") + "picked a pattern yet, so I'm not sure what you want " + "to save.\nTry clicking on one of the four patterns.", QMessageBox::Ok ); return; } QString s = QFileDialog::getSaveFileName( QString::null, "PNG Images (*.png)", this, "save file dialog", "Choose a filename to save under" ); if ( s.isNull() ) return; QImage save; save = *(favorite->pixmap()); save = save.scale( 512, 512 ); save.save( s, "PNG" ); } void CraftDialog::FillButton( CraftButton* b ) { while( b->garden->Depth() < depth ) b->garden->Grow( b->rule ); b->setPixmap( QPixmap( GetImage( b->garden ) ) ); } void CraftDialog::UpdateColors() { setCursor( QCursor::BusyCursor ); QPixmap p( 20, 20 ); for ( int c = 0 ; c < 4 ; c++ ) { FillButton( choices[c] ); switch( c ) { case 0: p.fill( b1 ); break; case 1: p.fill( b2 ); break; case 2: p.fill( f1 ); break; case 3: p.fill( f2 ); break; } ((QPushButton*)colors->find( c ))->setIconSet( p ); } if ( best ) favorite->setPixmap( *best->pixmap() ); setCursor( QCursor::ArrowCursor ); } QImage CraftDialog::GetImage( Garden* g ) { int dim = (int)pow( 2, depth ); QImage image( dim, dim, 32 ); for ( int x = 0 ; x < dim ; x++ ) { for ( int y = 0 ; y < dim ; y++ ) { if ( g->Pixel( x, y, depth ) ) { if ( x % 2 == y % 2 ) image.setPixel( x, y, f1.rgb() ); else image.setPixel( x, y, f2.rgb() ); } else { if ( x % 2 == y % 2 ) image.setPixel( x, y, b1.rgb() ); else image.setPixel( x, y, b2.rgb() ); } } } return image; } void CraftDialog::MakeUnique() { QImage ib; QImage ic; for ( int c = 0 ; c < 4 ; c++ ) { if ( choices[c] == best || ( !c && !best ) ) continue; for ( int b = 0 ; b < 4 ; b++ ) { if ( b == c ) continue; for ( int a = 0 ; a < 25 ; a++ ) { ib = *choices[b]->pixmap(); ic = *choices[c]->pixmap(); if ( ib != ic ) break; choices[c]->rule->Mutate(); delete choices[c]->garden; choices[c]->garden = new Garden(); FillButton( choices[c] ); } } } }
# | Change | User | Description | Committed | |
---|---|---|---|---|---|
#2 | 4790 | Sam Stafford |
SquareCraft optimizations... patterns are now only recalculated when the rules change, not when the depth or colors do. |
||
#1 | 4776 | Sam Stafford | A simple Qt frontend to the "squaregarden" algorithms. |