Diffstat (limited to 'noncore/games/wordgame/wordgame.cpp') (more/less context) (show whitespace changes)
-rw-r--r-- | noncore/games/wordgame/wordgame.cpp | 71 |
1 files changed, 57 insertions, 14 deletions
diff --git a/noncore/games/wordgame/wordgame.cpp b/noncore/games/wordgame/wordgame.cpp index ca4352d..16d37b3 100644 --- a/noncore/games/wordgame/wordgame.cpp +++ b/noncore/games/wordgame/wordgame.cpp | |||
@@ -50,24 +50,31 @@ | |||
50 | 50 | ||
51 | #include <stdlib.h> | 51 | #include <stdlib.h> |
52 | #include <unistd.h> | 52 | #include <unistd.h> |
53 | #include <pwd.h> | 53 | #include <pwd.h> |
54 | #include <sys/types.h> | 54 | #include <sys/types.h> |
55 | 55 | ||
56 | enum RuleEffects { | 56 | enum RuleEffects { |
57 | Multiplier=15, | 57 | Multiplier=15, |
58 | MultiplyAll=64, | 58 | MultiplyAll=64, |
59 | Start=128 | 59 | Start=128 |
60 | }; | 60 | }; |
61 | 61 | ||
62 | static int tile_smallw = 16; | ||
63 | static int tile_smallh = 16; | ||
64 | static int tile_bigw = 22; | ||
65 | static int tile_bigh = 22; | ||
66 | static int tile_stweak = -2; | ||
67 | static int tile_btweak = -1; | ||
68 | |||
62 | static const int rack_tiles=7; | 69 | static const int rack_tiles=7; |
63 | 70 | ||
64 | const char* sampleWGR= | 71 | const char* sampleWGR= |
65 | "wordgame_shapes\n" | 72 | "wordgame_shapes\n" |
66 | "15 15\n" | 73 | "15 15\n" |
67 | "400001040100004\n" | 74 | "400001040100004\n" |
68 | "030000000000030\n" | 75 | "030000000000030\n" |
69 | "002002000200200\n" | 76 | "002002000200200\n" |
70 | "000300020003000\n" | 77 | "000300020003000\n" |
71 | "000020000020000\n" | 78 | "000020000020000\n" |
72 | "102001000100201\n" | 79 | "102001000100201\n" |
73 | "000000202000000\n" | 80 | "000000202000000\n" |
@@ -102,24 +109,33 @@ const char* sampleWGR= | |||
102 | "7 t 1\n" | 109 | "7 t 1\n" |
103 | "7 n 1\n" | 110 | "7 n 1\n" |
104 | "7 a 1\n" | 111 | "7 a 1\n" |
105 | "7 r 1\n" | 112 | "7 r 1\n" |
106 | "8 s 1\n" | 113 | "8 s 1\n" |
107 | "8 i 1\n" | 114 | "8 i 1\n" |
108 | "11 e 1\n" | 115 | "11 e 1\n" |
109 | "0\n"; | 116 | "0\n"; |
110 | 117 | ||
111 | WordGame::WordGame( QWidget* parent, const char* name, WFlags fl ) : | 118 | WordGame::WordGame( QWidget* parent, const char* name, WFlags fl ) : |
112 | QMainWindow(parent, name, fl) | 119 | QMainWindow(parent, name, fl) |
113 | { | 120 | { |
121 | if ( qApp->desktop()->width() < 240 ) { | ||
122 | tile_smallw = 10; | ||
123 | tile_smallh = 10; | ||
124 | tile_bigw = 16; | ||
125 | tile_bigh = 16; | ||
126 | tile_stweak = 0; | ||
127 | tile_btweak = 0; | ||
128 | } | ||
129 | |||
114 | setIcon( Resource::loadPixmap( "wordgame" ) ); | 130 | setIcon( Resource::loadPixmap( "wordgame" ) ); |
115 | setCaption( tr("Word Game") ); | 131 | setCaption( tr("Word Game") ); |
116 | 132 | ||
117 | setToolBarsMovable( FALSE ); | 133 | setToolBarsMovable( FALSE ); |
118 | vbox = new QVBox(this); | 134 | vbox = new QVBox(this); |
119 | 135 | ||
120 | setCentralWidget(vbox); | 136 | setCentralWidget(vbox); |
121 | toolbar = new QPEToolBar(this); | 137 | toolbar = new QPEToolBar(this); |
122 | addToolBar(toolbar, Bottom); | 138 | addToolBar(toolbar, Bottom); |
123 | reset = new QToolButton(Resource::loadPixmap("back"), tr("Back"), "", this, SLOT(resetTurn()), toolbar); | 139 | reset = new QToolButton(Resource::loadPixmap("back"), tr("Back"), "", this, SLOT(resetTurn()), toolbar); |
124 | done = new QToolButton(Resource::loadPixmap("done"), tr("Done"), "", this, SLOT(endTurn()), toolbar); | 140 | done = new QToolButton(Resource::loadPixmap("done"), tr("Done"), "", this, SLOT(endTurn()), toolbar); |
125 | scoreinfo = new ScoreInfo(toolbar); | 141 | scoreinfo = new ScoreInfo(toolbar); |
@@ -238,24 +254,25 @@ void WordGame::startGame() | |||
238 | startGame(names); | 254 | startGame(names); |
239 | } else { | 255 | } else { |
240 | // error... | 256 | // error... |
241 | delete newgame; | 257 | delete newgame; |
242 | close(); | 258 | close(); |
243 | } | 259 | } |
244 | } | 260 | } |
245 | 261 | ||
246 | void WordGame::startGame(const QStringList& playerlist) | 262 | void WordGame::startGame(const QStringList& playerlist) |
247 | { | 263 | { |
248 | toolbar->show(); | 264 | toolbar->show(); |
249 | racks = new QWidgetStack(vbox); | 265 | racks = new QWidgetStack(vbox); |
266 | racks->setFixedHeight(TileItem::bigHeight()+2); | ||
250 | namelist.clear(); | 267 | namelist.clear(); |
251 | nplayers=0; | 268 | nplayers=0; |
252 | for (QStringList::ConstIterator it=playerlist.begin(); it!=playerlist.end(); ++it) | 269 | for (QStringList::ConstIterator it=playerlist.begin(); it!=playerlist.end(); ++it) |
253 | addPlayer(*it); | 270 | addPlayer(*it); |
254 | scoreinfo->init(namelist); | 271 | scoreinfo->init(namelist); |
255 | 272 | ||
256 | if ( nplayers ) { | 273 | if ( nplayers ) { |
257 | player=0; | 274 | player=0; |
258 | readyRack(player); | 275 | readyRack(player); |
259 | } | 276 | } |
260 | 277 | ||
261 | board->show(); | 278 | board->show(); |
@@ -274,38 +291,43 @@ bool WordGame::loadRules(const QString &name) | |||
274 | QString title = name; | 291 | QString title = name; |
275 | title.truncate( title.length() - 6 ); | 292 | title.truncate( title.length() - 6 ); |
276 | setCaption( title ); | 293 | setCaption( title ); |
277 | 294 | ||
278 | QString shapepixmap; | 295 | QString shapepixmap; |
279 | ts >> shapepixmap; | 296 | ts >> shapepixmap; |
280 | int htiles,vtiles; | 297 | int htiles,vtiles; |
281 | ts >> htiles >> vtiles; | 298 | ts >> htiles >> vtiles; |
282 | 299 | ||
283 | if ( htiles < 3 || vtiles < 3 ) | 300 | if ( htiles < 3 || vtiles < 3 ) |
284 | return FALSE; | 301 | return FALSE; |
285 | 302 | ||
286 | QPixmap bgshapes = Resource::loadPixmap(shapepixmap); | ||
287 | QString rule_shapes; | 303 | QString rule_shapes; |
288 | for (int i=0; i<vtiles; i++) { | 304 | for (int i=0; i<vtiles; i++) { |
289 | QString line; | 305 | QString line; |
290 | ts >> line; | 306 | ts >> line; |
291 | rule_shapes += line; | 307 | rule_shapes += line; |
292 | } | 308 | } |
293 | static int rule_effects[12]; | 309 | static int rule_effects[12]; |
294 | int re=0,e; | 310 | int re=0,e; |
295 | ts >> e; | 311 | ts >> e; |
296 | while ( e && re < 10 ) { | 312 | while ( e && re < 10 ) { |
297 | rule_effects[re] = e; | 313 | rule_effects[re] = e; |
298 | if ( re++ < 10 ) ts >> e; | 314 | if ( re++ < 10 ) ts >> e; |
299 | } | 315 | } |
316 | |||
317 | QImage shim = Resource::loadImage(shapepixmap); | ||
318 | shim = shim.smoothScale((re-1)*TileItem::smallWidth(),TileItem::smallHeight()); | ||
319 | QPixmap bgshapes; | ||
320 | bgshapes.convertFromImage(shim); | ||
321 | |||
300 | rule_effects[re++] = 100; // default bonus | 322 | rule_effects[re++] = 100; // default bonus |
301 | board = new Board(bgshapes, htiles, vtiles, vbox); | 323 | board = new Board(bgshapes, htiles, vtiles, vbox); |
302 | board->setRules(rule_shapes, rule_effects); | 324 | board->setRules(rule_shapes, rule_effects); |
303 | connect(board, SIGNAL(temporaryScore(int)), scoreinfo, SLOT(showTemporaryScore(int))); | 325 | connect(board, SIGNAL(temporaryScore(int)), scoreinfo, SLOT(showTemporaryScore(int))); |
304 | 326 | ||
305 | bag = new Bag; | 327 | bag = new Bag; |
306 | 328 | ||
307 | int count; | 329 | int count; |
308 | ts >> count; | 330 | ts >> count; |
309 | while ( count ) { | 331 | while ( count ) { |
310 | QString text; | 332 | QString text; |
311 | int value; | 333 | int value; |
@@ -672,107 +694,123 @@ qDebug("%d,%d: %s (%d) for %d",current.x(),current.y(),st.latin1(),n,s); | |||
672 | for ( i=0; i<blused; i++ ) | 694 | for ( i=0; i<blused; i++ ) |
673 | best_blankvalues[i] = blankvalues[i]; | 695 | best_blankvalues[i] = blankvalues[i]; |
674 | best_n = n; | 696 | best_n = n; |
675 | best_blused = blused; | 697 | best_blused = blused; |
676 | best_score = s; | 698 | best_score = s; |
677 | best_dir = d; | 699 | best_dir = d; |
678 | best_start = current; | 700 | best_start = current; |
679 | } | 701 | } |
680 | } | 702 | } |
681 | 703 | ||
682 | int TileItem::smallWidth() | 704 | int TileItem::smallWidth() |
683 | { | 705 | { |
684 | return 16; | 706 | return tile_smallw; |
685 | } | 707 | } |
686 | 708 | ||
687 | int TileItem::smallHeight() | 709 | int TileItem::smallHeight() |
688 | { | 710 | { |
689 | return 16; | 711 | return tile_smallh; |
690 | } | 712 | } |
691 | 713 | ||
692 | int TileItem::bigWidth() | 714 | int TileItem::bigWidth() |
693 | { | 715 | { |
694 | return 22; | 716 | return tile_bigw; |
695 | } | 717 | } |
696 | 718 | ||
697 | int TileItem::bigHeight() | 719 | int TileItem::bigHeight() |
698 | { | 720 | { |
699 | return 22; | 721 | return tile_bigh; |
700 | } | 722 | } |
701 | 723 | ||
702 | void TileItem::setState( State state ) | 724 | void TileItem::setState( State state ) |
703 | { | 725 | { |
704 | hide(); | 726 | hide(); |
705 | s = state; | 727 | s = state; |
706 | show(); // ### use update() in Qt 3.0 | 728 | show(); // ### use update() in Qt 3.0 |
707 | } | 729 | } |
708 | 730 | ||
709 | void TileItem::setTile(const Tile& tile) | 731 | void TileItem::setTile(const Tile& tile) |
710 | { | 732 | { |
711 | hide(); | 733 | hide(); |
712 | t = tile; | 734 | t = tile; |
713 | show(); // ### use update() in Qt 3.0 | 735 | show(); // ### use update() in Qt 3.0 |
714 | } | 736 | } |
715 | 737 | ||
716 | void TileItem::setBig(bool b) | 738 | void TileItem::setBig(bool b) |
717 | { | 739 | { |
718 | big = b; | 740 | big = b; |
719 | } | 741 | } |
720 | 742 | ||
721 | void TileItem::drawShape(QPainter& p) | 743 | void TileItem::drawShape(QPainter& p) |
722 | { | 744 | { |
723 | static QFont value_font("heletica",8); | 745 | static QFont *value_font=0; |
724 | static QFont big_font("smoothtimes",17); | 746 | static QFont *big_font=0; |
725 | static QFont small_font("smoothtimes",10); | 747 | static QFont *small_font=0; |
748 | if ( !value_font ) { | ||
749 | value_font = new QFont("helvetica",8); | ||
750 | if ( TileItem::bigWidth() < 20 ) { | ||
751 | big_font = new QFont("helvetica",12); | ||
752 | small_font = new QFont("helvetica",8); | ||
753 | } else { | ||
754 | big_font = new QFont("smoothtimes",17); | ||
755 | small_font = new QFont("smoothtimes",10); | ||
756 | } | ||
757 | } | ||
726 | 758 | ||
727 | QRect area(x(),y(),width(),height()); | 759 | QRect area(x(),y(),width(),height()); |
728 | p.setBrush(s == Floating ? yellow/*lightGray*/ : white); | 760 | p.setBrush(s == Floating ? yellow/*lightGray*/ : white); |
729 | p.drawRect(area); | 761 | p.drawRect(area); |
730 | if ( big ) { | 762 | if ( big ) { |
731 | p.setFont(value_font); | 763 | p.setFont(*value_font); |
732 | QString n = QString::number(t.value()); | 764 | QString n = QString::number(t.value()); |
733 | int w = p.fontMetrics().width('1'); | 765 | int w = p.fontMetrics().width('1'); |
734 | int h = p.fontMetrics().height(); | 766 | int h = p.fontMetrics().height(); |
735 | w *= n.length(); | 767 | w *= n.length(); |
736 | QRect valuearea(x()+width()-w-2,y()+height()-h+1,w,h); | 768 | QRect valuearea(x()+width()-w-1,y()+height()-h,w,h); |
737 | p.drawText(valuearea,AlignCenter,n); | 769 | p.drawText(valuearea,AlignCenter,n); |
738 | p.setFont(big_font); | 770 | p.setFont(*big_font); |
739 | area = QRect(x(),y(),width()-2,height()-1); | 771 | area = QRect(x(),y()+tile_btweak,width()-4,height()-1); |
740 | } else { | 772 | } else { |
741 | p.setFont(small_font); | 773 | p.setFont(*small_font); |
742 | area = QRect(x(),y()+2,width(),height()-2); | 774 | area = QRect(x()+1+tile_stweak,y()+1,width(),height()-3); |
743 | } | 775 | } |
744 | if ( t.value() == 0 ) | 776 | if ( t.value() == 0 ) |
745 | p.setPen(darkGray); | 777 | p.setPen(darkGray); |
746 | p.drawText(area,AlignCenter,t.text().upper()); | 778 | p.drawText(area,AlignCenter,t.text().upper()); |
747 | } | 779 | } |
748 | 780 | ||
749 | Board::Board(QPixmap bgshapes, int w, int h, QWidget* parent) : | 781 | Board::Board(QPixmap bgshapes, int w, int h, QWidget* parent) : |
750 | QCanvasView(new QCanvas(bgshapes,w,h, TileItem::smallWidth(), TileItem::smallHeight()), | 782 | QCanvasView(new QCanvas(bgshapes,w,h, TileItem::smallWidth(), TileItem::smallHeight()), |
751 | parent) | 783 | parent) |
752 | { | 784 | { |
785 | setFixedSize(w*TileItem::smallWidth(),h*TileItem::smallHeight()); | ||
753 | grid = new TileItem*[w*h]; | 786 | grid = new TileItem*[w*h]; |
754 | memset(grid,0,w*h*sizeof(TileItem*)); | 787 | memset(grid,0,w*h*sizeof(TileItem*)); |
755 | setFrameStyle(0); | 788 | setFrameStyle(0); |
756 | setHScrollBarMode(AlwaysOff); | 789 | setHScrollBarMode(AlwaysOff); |
757 | setVScrollBarMode(AlwaysOff); | 790 | setVScrollBarMode(AlwaysOff); |
758 | current_rack = 0; | 791 | current_rack = 0; |
759 | shown_n = 0; | 792 | shown_n = 0; |
760 | } | 793 | } |
761 | 794 | ||
762 | Board::~Board() | 795 | Board::~Board() |
763 | { | 796 | { |
764 | delete canvas(); | 797 | delete canvas(); |
765 | } | 798 | } |
766 | 799 | ||
800 | QSize Board::sizeHint() const | ||
801 | { | ||
802 | return QSize(canvas()->width(),canvas()->height()); | ||
803 | } | ||
804 | |||
767 | void Board::writeConfig(Config& cfg) | 805 | void Board::writeConfig(Config& cfg) |
768 | { | 806 | { |
769 | QStringList t; | 807 | QStringList t; |
770 | int n=canvas()->tilesHorizontally()*canvas()->tilesVertically(); | 808 | int n=canvas()->tilesHorizontally()*canvas()->tilesVertically(); |
771 | for (int i=0; i<n; i++) | 809 | for (int i=0; i<n; i++) |
772 | t.append( grid[i] ? grid[i]->tile().key() : QString(".") ); | 810 | t.append( grid[i] ? grid[i]->tile().key() : QString(".") ); |
773 | cfg.writeEntry("Board",t,';'); | 811 | cfg.writeEntry("Board",t,';'); |
774 | } | 812 | } |
775 | 813 | ||
776 | void Board::readConfig(Config& cfg) | 814 | void Board::readConfig(Config& cfg) |
777 | { | 815 | { |
778 | clear(); | 816 | clear(); |
@@ -1172,24 +1210,29 @@ Rack::Rack(int ntiles, QWidget* parent) : QCanvasView( | |||
1172 | setHScrollBarMode(AlwaysOff); | 1210 | setHScrollBarMode(AlwaysOff); |
1173 | setVScrollBarMode(AlwaysOff); | 1211 | setVScrollBarMode(AlwaysOff); |
1174 | canvas()->setBackgroundColor(gray); | 1212 | canvas()->setBackgroundColor(gray); |
1175 | dragging = 0; | 1213 | dragging = 0; |
1176 | } | 1214 | } |
1177 | 1215 | ||
1178 | Rack::~Rack() | 1216 | Rack::~Rack() |
1179 | { | 1217 | { |
1180 | clear(); | 1218 | clear(); |
1181 | delete canvas(); | 1219 | delete canvas(); |
1182 | } | 1220 | } |
1183 | 1221 | ||
1222 | QSize Rack::sizeHint() const | ||
1223 | { | ||
1224 | return QSize(-1,TileItem::bigHeight()+2); | ||
1225 | } | ||
1226 | |||
1184 | void Rack::clear() | 1227 | void Rack::clear() |
1185 | { | 1228 | { |
1186 | for (int i=0; i<n; i++) | 1229 | for (int i=0; i<n; i++) |
1187 | delete item[i]; | 1230 | delete item[i]; |
1188 | n=0; | 1231 | n=0; |
1189 | } | 1232 | } |
1190 | 1233 | ||
1191 | void Rack::writeConfig(Config& cfg) | 1234 | void Rack::writeConfig(Config& cfg) |
1192 | { | 1235 | { |
1193 | QStringList l; | 1236 | QStringList l; |
1194 | for (int i=0; i<n; i++) | 1237 | for (int i=0; i<n; i++) |
1195 | l.append(tile(i).key()); | 1238 | l.append(tile(i).key()); |