summaryrefslogtreecommitdiff
authordrw <drw>2004-12-20 14:49:49 (UTC)
committer drw <drw>2004-12-20 14:49:49 (UTC)
commit29b27cf61a7cb862a0236c8b319ff1843669142c (patch) (side-by-side diff)
treea2dee12e4241103259840385020ae317c8814d69
parent480a3ae1b4ed502fbd75e345c41ad4c469eff140 (diff)
downloadopie-29b27cf61a7cb862a0236c8b319ff1843669142c.zip
opie-29b27cf61a7cb862a0236c8b319ff1843669142c.tar.gz
opie-29b27cf61a7cb862a0236c8b319ff1843669142c.tar.bz2
Reapply fix for bug #1017 - Tetrix on high resolution screens
Diffstat (more/less context) (show whitespace changes)
-rw-r--r--noncore/games/tetrix/qtetrix.cpp58
-rw-r--r--noncore/games/tetrix/qtetrix.h8
-rw-r--r--noncore/games/tetrix/qtetrixb.cpp4
3 files changed, 58 insertions, 12 deletions
diff --git a/noncore/games/tetrix/qtetrix.cpp b/noncore/games/tetrix/qtetrix.cpp
index 20cf1a7..a6a5f34 100644
--- a/noncore/games/tetrix/qtetrix.cpp
+++ b/noncore/games/tetrix/qtetrix.cpp
@@ -1,174 +1,208 @@
/**********************************************************************
** Copyright (C) 2000 Trolltech AS. All rights reserved.
**
** This file is part of Qtopia Environment.
**
** This file may be distributed and/or modified under the terms of the
** GNU General Public License version 2 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file.
**
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
**
** See http://www.trolltech.com/gpl/ for GPL licensing information.
**
** Contact info@trolltech.com if any conditions of this licensing are
** not clear to you.
**
**********************************************************************/
#include "qtetrix.h"
#include <qpe/resource.h>
#include <qlabel.h>
#include <qdatetime.h>
#include <qlayout.h>
+#include <qtimer.h>
#include "ohighscoredlg.h"
void drawTetrixButton( QPainter *p, int x, int y, int w, int h,
const QColor *color )
{
QColor fc;
if ( color ) {
QPointArray a;
a.setPoints( 3, x,y+h-1, x,y, x+w-1,y );
p->setPen( color->light() );
p->drawPolyline( a );
a.setPoints( 3, x+1,y+h-1, x+w-1,y+h-1, x+w-1,y+1 );
p->setPen( color->dark() );
p->drawPolyline( a );
x++;
y++;
w -= 2;
h -= 2;
fc = *color;
}
else
fc = p->backgroundColor();
p->fillRect( x, y, w, h, fc );
}
ShowNextPiece::ShowNextPiece( QWidget *parent, const char *name )
: QFrame( parent, name )
{
setFrameStyle( QFrame::Panel | QFrame::Sunken );
xOffset = -1; // -1 until first resizeEvent.
}
void ShowNextPiece::resizeEvent( QResizeEvent *e )
{
QSize sz = e->size();
blockWidth = (sz.width() - 3)/5;
blockHeight = (sz.height() - 3)/6;
xOffset = (sz.width() - 3)/5;
yOffset = (sz.height() - 3)/6;
}
void ShowNextPiece::paintEvent( QPaintEvent * )
{
QPainter p( this );
drawFrame( &p );
p.end(); // explicit end() so any slots can paint too
emit update();
}
void ShowNextPiece::drawNextSquare(int x, int y,QColor *color)
{
if (xOffset == -1) // Before first resizeEvent?
return;
QPainter paint;
paint.begin(this);
drawTetrixButton( &paint, xOffset+x*blockWidth, yOffset+y*blockHeight,
blockWidth, blockHeight, color );
paint.end();
}
QTetrix::QTetrix( QWidget *parent, const char *name, WFlags f )
: QMainWindow( parent, name, f )
{
setIcon( Resource::loadPixmap( "tetrix_icon" ) );
setCaption( tr("Tetrix" ) );
QTime t = QTime::currentTime();
TetrixPiece::setRandomSeed( (((double)t.hour())+t.minute()+t.second())/
(24+60+60) );
QWidget *gameArea = new QWidget( this );
setCentralWidget( gameArea );
QGridLayout *gl = new QGridLayout( gameArea, 5, 3, 8 );
+ gl->setColStretch( 1, 5 );
+ gl->setColStretch( 2, 10 );
QLabel *l;
l = new QLabel( tr("Next"), gameArea );
gl->addWidget( l, 0, 0 );
showNext = new ShowNextPiece(gameArea);
showNext->setBackgroundColor(QColor(0,0,0));
gl->addWidget( showNext, 0, 1 );
l = new QLabel( tr("Score"), gameArea );
gl->addWidget( l, 1, 0 );
showScore = new QLabel(gameArea);
gl->addWidget( showScore, 1, 1 );
l = new QLabel( tr("Level"), gameArea );
gl->addWidget( l, 2, 0 );
showLevel = new QLabel(gameArea);
gl->addWidget( showLevel, 2, 1 );
l = new QLabel( tr("Removed"), gameArea );
gl->addWidget( l, 3, 0 );
showLines = new QLabel(gameArea);
gl->addWidget( showLines, 3, 1 );
board = new QTetrixBoard(gameArea);
board->setBackgroundColor(QColor(0,0,0));
- board->setFixedWidth( 124 );
gl->addMultiCellWidget( board, 0, 4, 2, 2 );
- gl->addColSpacing( 2, 100 );
- gl->addColSpacing( 1, 35 );
- gl->addRowSpacing( 0, 35 );
QPushButton *pb = new QPushButton( tr("Start"), gameArea );
pb->setFocusPolicy( NoFocus );
connect( pb, SIGNAL( clicked() ), board, SLOT( start() ) );
gl->addMultiCellWidget( pb, 4, 4, 0, 1 );
connect( board, SIGNAL(gameOverSignal()), SLOT(gameOver()) );
- connect( board, SIGNAL(drawNextSquareSignal(int,int,QColor*)), showNext,
- SLOT(drawNextSquare(int,int,QColor*)) );
+ connect( board, SIGNAL(drawNextSquareSignal(int,int,QColor*)), this,
+ SLOT(setNext(int,int,QColor*)) );
connect( showNext, SIGNAL(update()), board, SLOT(updateNext()) );
- connect( board, SIGNAL(updateScoreSignal(int)), showScore,
- SLOT(setNum(int)) );
- connect( board, SIGNAL(updateLevelSignal(int)), showLevel,
- SLOT(setNum(int)));
- connect( board, SIGNAL(updateRemovedSignal(int)), showLines,
- SLOT(setNum(int)));
+ connect( board, SIGNAL(updateScoreSignal(int)), showScore, SLOT(setNum(int)) );
+ connect( board, SIGNAL(updateLevelSignal(int)), showLevel, SLOT(setNum(int)) );
+ connect( board, SIGNAL(updateRemovedSignal(int)), showLines, SLOT(setNum(int)) );
showScore->setNum( 0 );
showLevel->setNum( 0 );
showLines->setNum( 0 );
board->revealNextPiece(TRUE);
board->setFocusPolicy( StrongFocus );
+
+ QTimer::singleShot( -1, this, SLOT(setup()) );
+}
+
+void QTetrix::setup()
+{
+ resizeEvent( 0x0 );
}
void QTetrix::gameOver()
{
OHighscore *hs = new OHighscore( showScore->text().toInt() , showLevel->text().toInt() );
if ( hs->isNewhighscore )
hs->insertData( hs->getName(), showScore->text().toInt() , showLevel->text().toInt() );
OHighscoreDialog hscdlg( hs, this, "OHighscoreDialog", true );
hscdlg.exec();
}
void QTetrix::quit()
{
close();
}
+
+void QTetrix::setNext( int x, int y, QColor *color )
+{
+ resizeEvent( 0x0 );
+ showNext->drawNextSquare( x, y, color );
+}
+
+void QTetrix::resizeEvent( QResizeEvent * )
+{
+ // Set size of board
+ int widthFactor = board->QFrame::width() / board->boardWidth();
+ if ( widthFactor < 1 )
+ widthFactor = 1;
+ int heightFactor = board->QFrame::height() / board->boardHeight();
+ if ( heightFactor < 1 )
+ heightFactor = 1;
+ widthFactor > heightFactor ? board->resize( heightFactor * board->boardWidth() + 2,
+ heightFactor * board->boardHeight() + 2 )
+ : board->resize( widthFactor * board->boardWidth() + 2,
+ widthFactor * board->boardHeight() + 2 );
+
+ // Set size of preview widget
+ widthFactor = showNext->width() / 5;
+ if ( widthFactor < 1 )
+ widthFactor = 1;
+ heightFactor = showNext->height() / 6;
+ if ( heightFactor < 1 )
+ heightFactor = 1;
+ widthFactor > heightFactor ? showNext->resize( heightFactor * 5 + 2, heightFactor * 6 + 2 )
+ : showNext->resize( widthFactor * 5 + 2, widthFactor * 6 + 2 );
+}
diff --git a/noncore/games/tetrix/qtetrix.h b/noncore/games/tetrix/qtetrix.h
index c8959c5..8c44b77 100644
--- a/noncore/games/tetrix/qtetrix.h
+++ b/noncore/games/tetrix/qtetrix.h
@@ -1,77 +1,85 @@
/**********************************************************************
** Copyright (C) 2000 Trolltech AS. All rights reserved.
**
** This file is part of Qtopia Environment.
**
** This file may be distributed and/or modified under the terms of the
** GNU General Public License version 2 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file.
**
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
**
** See http://www.trolltech.com/gpl/ for GPL licensing information.
**
** Contact info@trolltech.com if any conditions of this licensing are
** not clear to you.
**
**********************************************************************/
#ifndef QTETRIX_H
#define QTETRIX_H
#include "qtetrixb.h"
#include <qframe.h>
#include <qlcdnumber.h>
#include <qpushbutton.h>
#include <qpainter.h>
#include <qmainwindow.h>
class QLabel;
class ShowNextPiece : public QFrame
{
Q_OBJECT
friend class QTetrix;
public:
ShowNextPiece( QWidget *parent=0, const char *name=0 );
public slots:
void drawNextSquare( int x, int y,QColor *color );
signals:
void update();
private:
void paintEvent( QPaintEvent * );
void resizeEvent( QResizeEvent * );
int blockWidth,blockHeight;
int xOffset,yOffset;
};
class QTetrix : public QMainWindow
{
Q_OBJECT
public:
static QString appName() { return QString::fromLatin1("tetrix"); }
QTetrix( QWidget *parent=0, const char *name=0, WFlags f=0 );
void startGame() { board->startGame(); }
public slots:
+ void setup();
void gameOver();
void quit();
+
+ void setNext( int x, int y, QColor *color );
+// void setScore( int score );
+// void setLevel( int level );
+// void setLines( int lines );
+
private:
void keyPressEvent( QKeyEvent *e ) { board->keyPressEvent(e); }
+ void resizeEvent( QResizeEvent * );
QTetrixBoard *board;
ShowNextPiece *showNext;
QLabel *showScore;
QLabel *showLevel;
QLabel *showLines;
};
void drawTetrixButton( QPainter *, int x, int y, int w, int h,
const QColor *color );
#endif
diff --git a/noncore/games/tetrix/qtetrixb.cpp b/noncore/games/tetrix/qtetrixb.cpp
index 3c179df..8c41fb2 100644
--- a/noncore/games/tetrix/qtetrixb.cpp
+++ b/noncore/games/tetrix/qtetrixb.cpp
@@ -1,249 +1,253 @@
/**********************************************************************
** Copyright (C) 2000 Trolltech AS. All rights reserved.
**
** This file is part of Qtopia Environment.
**
** This file may be distributed and/or modified under the terms of the
** GNU General Public License version 2 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file.
**
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
**
** See http://www.trolltech.com/gpl/ for GPL licensing information.
**
** Contact info@trolltech.com if any conditions of this licensing are
** not clear to you.
**
**********************************************************************/
#include "qtetrixb.h"
#include "qtetrix.h"
#include <qtimer.h>
const int waitAfterLineTime = 500;
QTetrixBoard::QTetrixBoard( QWidget *p, const char *name )
: QFrame( p, name )
{
setFrameStyle( QFrame::Panel | QFrame::Sunken );
paint = 0;
timer = new QTimer(this);
connect( timer, SIGNAL(timeout()), SLOT(timeout()) );
colors[0].setRgb(200,100,100);
colors[1].setRgb(100,200,100);
colors[2].setRgb(100,100,200);
colors[3].setRgb(200,200,100);
colors[4].setRgb(200,100,200);
colors[5].setRgb(100,200,200);
colors[6].setRgb(218,170, 0);
xOffset = -1; // -1 until a resizeEvent is received.
blockWidth = 20;
yOffset = 30;
blockHeight = 20;
noGame = TRUE;
isPaused = FALSE;
waitingAfterLine = FALSE;
updateTimeoutTime(); // Sets timeoutTime
}
void QTetrixBoard::startGame(int gameType,int fillRandomLines)
{
if ( isPaused )
return; // ignore if game is paused
noGame = FALSE;
+
GenericTetrix::startGame( gameType, fillRandomLines );
// Note that the timer is started by updateLevel!
}
void QTetrixBoard::pause()
{
if ( noGame ) // game not active
return;
isPaused = !isPaused;
if ( isPaused ) {
timer->stop();
hideBoard();
}
else
timer->start(timeoutTime);
update();
}
void QTetrixBoard::drawSquare(int x,int y,int value)
{
if (xOffset == -1) // Before first resizeEvent?
return;
const int X = xOffset + x*blockWidth;
const int Y = yOffset + (y - 1)*blockHeight;
bool localPainter = paint == 0;
QPainter *p;
if ( localPainter )
p = new QPainter( this );
else
p = paint;
drawTetrixButton( p, X, Y, blockWidth, blockHeight,
value == 0 ? 0 : &colors[value-1] );
/*
if ( value != 0 ) {
QColor tc, bc;
tc = colors[value-1].light();
bc = colors[value-1].dark();
p->drawShadePanel( X, Y, blockWidth, blockHeight,
tc, bc, 1, colors[value-1], TRUE );
}
else
p->fillRect( X, Y, blockWidth, blockHeight, backgroundColor() );
*/
if ( localPainter )
delete p;
}
void QTetrixBoard::drawNextSquare( int x, int y, int value )
{
if ( value == 0 )
emit drawNextSquareSignal (x, y, 0 );
else
emit drawNextSquareSignal( x, y, &colors[value-1] );
}
void QTetrixBoard::updateRemoved( int noOfLines )
{
if ( noOfLines > 0 ) {
timer->stop();
timer->start( waitAfterLineTime );
waitingAfterLine = TRUE;
}
emit updateRemovedSignal( noOfLines );
}
void QTetrixBoard::updateScore( int newScore )
{
emit updateScoreSignal( newScore );
}
void QTetrixBoard::updateLevel( int newLevel )
{
timer->stop();
updateTimeoutTime();
timer->start( timeoutTime );
emit updateLevelSignal( newLevel );
}
void QTetrixBoard::pieceDropped(int)
{
if ( waitingAfterLine ) // give player a break if a line has been removed
return;
newPiece();
}
void QTetrixBoard::gameOver()
{
timer->stop();
noGame = TRUE;
emit gameOverSignal();
}
void QTetrixBoard::timeout()
{
if ( waitingAfterLine ) {
timer->stop();
waitingAfterLine = FALSE;
newPiece();
timer->start( timeoutTime );
} else {
oneLineDown();
}
}
void QTetrixBoard::drawContents( QPainter *p )
{
const char *text = "Press \"Pause\"";
QRect r = contentsRect();
paint = p; // set widget painter
if ( isPaused ) {
p->drawText( r, AlignCenter | AlignVCenter, text );
return;
}
int x1,y1,x2,y2;
x1 = (r.left() - xOffset) / blockWidth;
if (x1 < 0)
x1 = 0;
if (x1 >= boardWidth())
x1 = boardWidth() - 1;
x2 = (r.right() - xOffset) / blockWidth;
if (x2 < 0)
x2 = 0;
if (x2 >= boardWidth())
x2 = boardWidth() - 1;
y1 = (r.top() - yOffset) / blockHeight;
if (y1 < 0)
y1 = 0;
if (y1 >= boardHeight())
y1 = boardHeight() - 1;
y2 = (r.bottom() - yOffset) / blockHeight;
if (y2 < 0)
y2 = 0;
if (y2 >= boardHeight())
y2 = boardHeight() - 1;
updateBoard( x1, y1, x2, y2, TRUE );
paint = 0; // reset widget painter
return;
}
void QTetrixBoard::resizeEvent(QResizeEvent *e)
{
QSize sz = e->size();
+
blockWidth = (sz.width() - 2)/10;
blockHeight = (sz.height() - 2)/22;
+/* blockWidth > blockHeight ? blockWidth = blockHeight
+ : blockHeight = blockWidth;*/
xOffset = 1;
//yOffset = 1;
yOffset = (sz.height() - 2) - (blockHeight *22);
}
void QTetrixBoard::keyPressEvent( QKeyEvent *e )
{
if ( noGame || isPaused || waitingAfterLine )
return;
switch( e->key() ) {
case Key_Left :
moveLeft();
break;
case Key_Right :
moveRight();
break;
case Key_Down :
// rotateRight();
dropDown();
break;
case Key_Up :
rotateLeft();
break;
case Key_Space :
dropDown();
break;
case Key_D :
oneLineDown();
break;
default:
return;
}
e->accept();
}
void QTetrixBoard::updateTimeoutTime()
{
timeoutTime = 1000/(1 + getLevel());
}