00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include <math.h>
00023
00024
00025 #include <QTimer>
00026 #include <QTimer>
00027 #include <QColor>
00028
00029
00030 #include <klocale.h>
00031 #include <kdebug.h>
00032 #include <kplayer.h>
00033
00034
00035 #include "kwin4view.h"
00036 #include "displayintro.h"
00037 #include "displaygame.h"
00038 #include "spritenotify.h"
00039 #include "score.h"
00040
00041
00042
00043 #define VIEW_ASPECT_RATIO 1.6
00044
00045
00046 void KWinGraphicsView::paintEvent(QPaintEvent* event)
00047 {
00048 QPaintEvent* newEvent = new QPaintEvent(event->region().boundingRect());
00049 QGraphicsView::paintEvent(newEvent);
00050 delete newEvent;
00051 }
00052
00053
00054 KWin4View::KWin4View(QSize size, int advancePeriod, QGraphicsScene* scene, ThemeManager* theme, QWidget* parent)
00055 : KWinGraphicsView(scene, parent)
00056 {
00057
00058 mScene = scene;
00059 mTheme = theme;
00060 mAdvancePeriod = advancePeriod;
00061
00062
00063 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
00064 setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
00065 setCacheMode(QGraphicsView::CacheBackground);
00066
00067
00068 scene->setBackgroundBrush(QColor(0,0,128));
00069
00070
00071
00072 QTimer* timer = new QTimer(this);
00073 connect(timer, SIGNAL(timeout()), this, SLOT(updateAndAdvance()));
00074 timer->start(advancePeriod);
00075
00076
00077 mIsRunning = false;
00078
00079
00080
00081 resize(size);
00082 scene->setSceneRect(0, 0, this->width(), this->height());
00083 adjustSize();
00084
00085
00086 setInteractive(true);
00087
00088
00089 mTheme->rescale(this->width());
00090
00091
00092 mGameDisplay = 0;
00093 mIntroDisplay = new DisplayIntro(advancePeriod, scene, mTheme, this);
00094 mIntroDisplay->start();
00095 }
00096
00097
00098
00099 KWin4View::~KWin4View()
00100 {
00101 if (mIntroDisplay) delete mIntroDisplay;
00102 if (mGameDisplay) delete mGameDisplay;
00103 }
00104
00105
00106
00107 void KWin4View::updateAndAdvance()
00108 {
00109 scene()->advance();
00110
00111
00112 }
00113
00114
00115
00116 void KWin4View::initGame(Score* scoreData)
00117 {
00118 if (mIntroDisplay) delete mIntroDisplay;
00119 mIntroDisplay = 0;
00120 if (!mGameDisplay)
00121 {
00122 mGameDisplay = new DisplayGame(mAdvancePeriod, mScene, mTheme, this);
00123 }
00124 mGameDisplay->start();
00125
00126
00127 scoreData->setDisplay(mGameDisplay->score());
00128
00129 mIsRunning = true;
00130 }
00131
00132
00133
00134 void KWin4View::endGame()
00135 {
00136 mIsRunning = false;
00137 mGameDisplay->displayEnd();
00138 }
00139
00140
00141
00142 void KWin4View::resizeEvent (QResizeEvent* e)
00143 {
00144 kDebug() << "++++ KWin4View::resizeEvent "<<e->size().width()<<" , "<< e->size().height() <<endl;
00145
00146 if (scene())
00147 {
00148 scene()->setSceneRect(0, 0, e->size().width(), e->size().height());
00149 }
00150 QSizeF size = QSizeF(e->size());
00151
00152
00153 double aspect = size.width() / size.height();
00154 if (aspect > VIEW_ASPECT_RATIO) mTheme->rescale(int(e->size().height()*VIEW_ASPECT_RATIO));
00155 else mTheme->rescale(int(e->size().width()));
00156 }
00157
00158
00159
00160
00161
00162
00163
00164 void KWin4View::mouseInput(KGameIO* input, QDataStream& stream, QMouseEvent* mouse, bool* eatevent)
00165 {
00166
00167 if (mouse->type() != QEvent::MouseButtonPress ) return;
00168 if (mouse->button() != Qt::LeftButton) return ;
00169 if (!mIsRunning) return;
00170
00171
00172 KPlayer* player=input->player();
00173 if (!player->myTurn())
00174 {
00175 kDebug() <<" Kwin4View::TODO wrongPlayer " << endl;
00176
00177 return;
00178 }
00179
00180
00181 int x = -1;
00182 if (mGameDisplay) x = mGameDisplay->mapMouseToMove(mouse->pos());
00183 if (x<0) return;
00184
00185
00186 qint32 move = x;
00187 qint32 pl = player->userId();
00188 stream << pl << move;
00189 *eatevent=true;
00190 }
00191
00192
00193
00194
00195
00196
00197 void KWin4View::keyInput(KGameIO* input, QDataStream& stream, QKeyEvent* key, bool* eatevent)
00198 {
00199
00200 if (!mIsRunning) return;
00201
00202
00203 if (key->type() != QEvent::KeyPress) return ;
00204
00205
00206 int code=key->key();
00207 if (code< Qt::Key_1 || code> Qt::Key_7) return ;
00208
00209
00210
00211 KPlayer *player=input->player();
00212 if (!player->myTurn())
00213 {
00214 kDebug() <<" Kwin4View::TODO wrongPlayer " << endl;
00215
00216 return;
00217 }
00218
00219
00220 qint32 move = code-Qt::Key_1;
00221 qint32 pl = player->userId();
00222 stream << pl << move;
00223 *eatevent=true;
00224 }
00225
00226
00227
00228
00229 void KWin4View::displayMove(int x, int y, int color, int xarrow, int colorarrow, int no, bool animation)
00230 {
00231 mGameDisplay->displayArrow(xarrow, colorarrow);
00232
00233 SpriteNotify* notify = mGameDisplay->displayPiece(x, y, color, no, animation);
00234 if (notify && animation)
00235 {
00236 QObject::disconnect(notify,SIGNAL(signalNotify(QGraphicsItem*,int)),
00237 this,SLOT(moveDone(QGraphicsItem*,int)));
00238 connect(notify,SIGNAL(signalNotify(QGraphicsItem*,int)),
00239 this,SLOT(moveDone(QGraphicsItem*,int)));
00240 }
00241 mGameDisplay->displayHint(0,0,false);
00242 }
00243
00244
00245
00246 void KWin4View::displayStar(int x, int y, int no)
00247 {
00248 mGameDisplay->displayStar(x, y, no);
00249 }
00250
00251
00252 void KWin4View::displayHint(int x, int y)
00253 {
00254 mGameDisplay->displayHint(x, y, true);
00255 }
00256
00257
00258 void KWin4View::moveDone(QGraphicsItem* , int mode)
00259 {
00260 emit signalMoveDone(mode);
00261 }
00262
00263 #include "kwin4view.moc"