Oubliette Class Reference

#include <oubliette.h>

Inheritance diagram for Oubliette:

Inheritance graph
[legend]
Collaboration diagram for Oubliette:

Collaboration graph
[legend]
List of all members.

Detailed Description

Definition at line 36 of file oubliette.h.

Signals

void characterMoved (const QPoint &pt)

Public Member Functions

 Oubliette ()
 ~Oubliette ()
const Cursorcharacter () const
QPoint visualCursorPos () const

Protected Member Functions

void paintEvent (QPaintEvent *)
void keyPressEvent (QKeyEvent *)
void timerEvent (QTimerEvent *)
void showEvent (QShowEvent *)

Private Slots

void showInventoryItem (QListWidgetItem *lwi)
void showInstructions ()
void showVictory ()

Private Member Functions

void showInventory ()
void animateItem (const Item *item, const QPoint &pos)
bool tryMove (const QPoint &newPos)
void updateExplored ()
void paintOubliette (QPainter *p, const QRect &rect)
void fillTile (QPainter *p, int x, int y, Tile tile)
void fillTile (QPainter *p, const QPoint &point, Tile tile)

Private Attributes

OubliettePlan m_oubliettePlan
Cursor m_character
QPoint m_oldCursorPosition
int m_currentLevel
QList< ItemEffect * > m_effects
int m_timerID


Constructor & Destructor Documentation

Oubliette::Oubliette (  ) 

Definition at line 64 of file oubliette.cpp.

References QWidget::backgroundRole(), Qt::black, center(), QSize::height(), OubliettePlan::level(), m_character, m_currentLevel, m_oldCursorPosition, m_oubliettePlan, QWidget::palette(), Cursor::position(), QPalette::setColor(), QWidget::setFixedSize(), QWidget::setPalette(), Cursor::setPosition(), OublietteLevel::size(), TileHeight, TileWidth, and QSize::width().

00065     : m_timerID(-1)
00066 {
00067     // set a fixed size, pased on the oubliette size
00068     m_currentLevel = 1;
00069     QSize sz = m_oubliettePlan.level(m_currentLevel)->size();
00070     setFixedSize(sz.width() * TileWidth, sz.height() * TileHeight);
00071     m_character.setPosition(QRect(QPoint(0, 0), sz).center(), false);
00072     m_oldCursorPosition = m_character.position();
00073     QPalette pal = palette();
00074     pal.setColor(backgroundRole(), Qt::black);
00075     setPalette(pal);
00076 }

Here is the call graph for this function:

Oubliette::~Oubliette (  ) 

Definition at line 78 of file oubliette.cpp.

00079 {
00080 }


Member Function Documentation

const Cursor& Oubliette::character (  )  const [inline]

Definition at line 42 of file oubliette.h.

References m_character.

00042 { return m_character; }

QPoint Oubliette::visualCursorPos (  )  const

Definition at line 291 of file oubliette.cpp.

References m_character, Cursor::position(), TileHeight, TileWidth, QPoint::x(), and QPoint::y().

Referenced by keyPressEvent(), and OublietteView::OublietteView().

00292 {
00293     QPoint pt = m_character.position();
00294     return QPoint(pt.x() * TileWidth, pt.y() * TileHeight);
00295 }

Here is the call graph for this function:

void Oubliette::paintEvent ( QPaintEvent pe  )  [protected, virtual]

This event handler can be reimplemented in a subclass to receive paint events which are passed in the event parameter.

A paint event is a request to repaint all or part of the widget. It can happen as a result of repaint() or update(), or because the widget was obscured and has now been uncovered, or for many other reasons.

Many widgets can simply repaint their entire surface when asked to, but some slow widgets need to optimize by painting only the requested region: QPaintEvent::region(). This speed optimization does not change the result, as painting is clipped to that region during event processing. QListView and QTableView do this, for example.

Qt also tries to speed up painting by merging multiple paint events into one. When update() is called several times or the window system sends several paint events, Qt merges these events into one event with a larger region (see QRegion::united()). repaint() does not permit this optimization, so we suggest using update() whenever possible.

When the paint event occurs, the update region has normally been erased, so that you're painting on the widget's background.

The background can be set using setBackgroundRole() and setPalette().

From Qt 4.0, QWidget automatically double-buffers its painting, so there's no need to write double-buffering code in paintEvent() to avoid flicker.

Note: Under X11 it is possible to toggle the global double buffering by calling qt_x11_set_global_double_buffer(). Example usage:

See also:
event(), repaint(), update(), QPainter, QPixmap, QPaintEvent, {Analog Clock Example}

Reimplemented from QWidget.

Definition at line 88 of file oubliette.cpp.

References QDesktopWidget::availableGeometry(), QApplication::desktop(), QPixmapCache::find(), QList< T >::indexOf(), QPixmapCache::insert(), QPixmap::isNull(), m_character, m_effects, QWidget::mapToGlobal(), QWidget::move(), QRect::moveCenter(), p, paintOubliette(), Cursor::position(), QWidget::QPixmap, QPaintEvent::rect(), ItemEffect::rect, QList< T >::removeAt(), QWidget::resize(), QWidget::show(), ItemEffect::step, TileHeight, TileWidth, and ItemEffect::TotalSteps.

00089 {
00090     QPainter p(this);
00091     paintOubliette(&p, pe->rect());
00092 
00093     QPoint pt = m_character.position();
00094     QPixmap pm;
00095     QString pcString = ":/qthack/images/human.png";
00096     QPixmapCache::find(pcString, pm);
00097     if (pm.isNull()) {
00098         pm = QPixmap(pcString);
00099         QPixmapCache::insert(pcString, pm);
00100     }
00101     p.drawPixmap(pt.x() * TileWidth, pt.y() * TileHeight, TileWidth, TileHeight, pm);
00102 
00103     // paint the moving cards
00104     foreach (ItemEffect *effect, m_effects) {
00105         if (effect->step >= ItemEffect::TotalSteps) {
00106             m_effects.removeAt(m_effects.indexOf(effect));
00107             ItemDialog *dlg = new ItemDialog(this, effect->item);
00108             QRect dlgRect(mapToGlobal(effect->rect.topLeft()), effect->rect.size());
00109             QRect desktopRect = QApplication::desktop()->availableGeometry(this);
00110             if (dlgRect.intersect(desktopRect) != dlgRect)
00111                 dlgRect.moveCenter(desktopRect.center());
00112             dlg->resize(dlgRect.size());
00113             dlg->move(dlgRect.topLeft());
00114             dlg->show();
00115             delete effect;
00116             continue;
00117         }
00118         p.fillRect(effect->rect, QColor(150, 150, 150, 240));
00119     }
00120 }

Here is the call graph for this function:

void Oubliette::keyPressEvent ( QKeyEvent ke  )  [protected, virtual]

This event handler, for event event, can be reimplemented in a subclass to receive key press events for the widget.

A widget must call setFocusPolicy() to accept focus initially and have focus in order to receive a key press event.

If you reimplement this handler, it is very important that you ignore() the event if you do not understand it, so that the widget's parent can interpret it.

The default implementation closes popup widgets if the user presses Esc. Otherwise the event is ignored.

See also:
keyReleaseEvent(), QKeyEvent::ignore(), setFocusPolicy(), focusInEvent(), focusOutEvent(), event(), QKeyEvent, {Tetrix Example}

Reimplemented from QWidget.

Definition at line 177 of file oubliette.cpp.

References characterMoved(), emit, QKeyEvent::key(), Qt::Key_Down, Qt::Key_H, Qt::Key_I, Qt::Key_J, Qt::Key_K, Qt::Key_L, Qt::Key_Left, Qt::Key_Right, Qt::Key_Up, QWidget::keyPressEvent(), m_character, Cursor::position(), QPoint::rx(), QPoint::ry(), showInventory(), TileHeight, TileWidth, tryMove(), QWidget::update(), visualCursorPos(), QPoint::x(), and QPoint::y().

00178 {
00179     QPoint newPos = m_character.position();
00180     switch (ke->key()) {
00181     case Qt::Key_Up:
00182     case Qt::Key_K:
00183         newPos.ry() -= 1;
00184         break;
00185     case Qt::Key_Down:
00186     case Qt::Key_J:
00187         newPos.ry() += 1;
00188         break;
00189     case Qt::Key_Left:
00190     case Qt::Key_H:
00191         newPos.rx() -= 1;
00192         break;
00193     case Qt::Key_Right:
00194     case Qt::Key_L:
00195         newPos.rx() += 1;
00196         break;
00197     case Qt::Key_I:
00198         showInventory();
00199         break;
00200     default:
00201         QWidget::keyPressEvent(ke);
00202     }
00203     if (tryMove(newPos)) {
00204         QRect r(QPoint((newPos.x() - 8) * TileWidth, (newPos.y() - 8) * TileHeight),
00205                 QSize(24 * TileWidth, 24 * TileHeight));
00206         update(r);
00207         emit characterMoved(visualCursorPos());
00208     }
00209 }

Here is the call graph for this function:

void Oubliette::timerEvent ( QTimerEvent  )  [protected, virtual]

This event handler can be reimplemented in a subclass to receive timer events for the object.

QTimer provides a higher-level interface to the timer functionality, and also more general information about timers. The timer event is passed in the event parameter.

See also:
startTimer(), killTimer(), event()

Reimplemented from QObject.

Definition at line 306 of file oubliette.cpp.

References QRect::adjust(), QList< T >::isEmpty(), QObject::killTimer(), m_effects, m_timerID, ItemEffect::rect, ItemEffect::step, ItemEffect::TotalSteps, and QWidget::update().

00307 {
00308     if (m_effects.isEmpty()) {
00309         killTimer(m_timerID);
00310         m_timerID = -1;
00311     }
00312     QRegion region;
00313     foreach (ItemEffect *effect, m_effects) {
00314         if (effect->step >= ItemEffect::TotalSteps) {
00315             region += effect->rect;
00316             continue;
00317         }
00318         effect->rect.adjust(-10, -5, 10, 5);
00319         ++effect->step;
00320         region += effect->rect;
00321     }
00322     update(region);
00323 }

Here is the call graph for this function:

void Oubliette::showEvent ( QShowEvent ev  )  [protected, virtual]

This event handler can be reimplemented in a subclass to receive widget show events which are passed in the event parameter.

Non-spontaneous show events are sent to widgets immediately before they are shown. The spontaneous show events of windows are delivered afterwards.

Note: A widget receives spontaneous show and hide events when its mapping status is changed by the window system, e.g. a spontaneous hide event when the user minimizes the window, and a spontaneous show event when the window is restored again. After receiving a spontaneous hide event, a widget is still considered visible in the sense of isVisible().

See also:
visible, event(), QShowEvent

Reimplemented from QWidget.

Definition at line 82 of file oubliette.cpp.

References QWidget::showEvent(), showInstructions(), QTimer::singleShot(), and SLOT.

00083 {
00084     QWidget::showEvent(ev);
00085     QTimer::singleShot(0, this, SLOT(showInstructions()));
00086 }

Here is the call graph for this function:

void Oubliette::showInventoryItem ( QListWidgetItem lwi  )  [private, slot]

Definition at line 325 of file oubliette.cpp.

References QList< T >::at(), QListWidgetItem::data(), QDialog::exec(), Cursor::items(), m_character, and QVariant::toInt().

Referenced by showInventory().

00326 {
00327     ItemDialog *dlg = new ItemDialog(this, m_character.items().at(lwi->data(99).toInt()));
00328     dlg->exec();
00329 }

void Oubliette::showInstructions (  )  [private, slot]

Definition at line 331 of file oubliette.cpp.

References QMessageBox::information(), and QWidget::window().

Referenced by showEvent().

00332 {
00333     QMessageBox::information(window(), tr("Easter Egg Found"),
00334                              tr("Welcome to the Trolltech Business Card Hunt\n"
00335                                 "Use the direction keys to move around and find"
00336                                 " the business cards for all the trolls."));
00337 }

void Oubliette::showVictory (  )  [private, slot]

Definition at line 339 of file oubliette.cpp.

References QMessageBox::information(), QCoreApplication::instance(), m_character, QCoreApplication::quit(), Cursor::totalSteps(), value, and QWidget::window().

Referenced by tryMove().

00340 {
00341     int value = QMessageBox::information(window(), tr("You Did It!"),
00342             tr("You've collected all the Trolltech cards. It took you %1 steps.\n"
00343                "There's nothing more here. You should get back to work.").arg(m_character.totalSteps()),
00344             tr("That's rather anti-climatic"), tr("Quit"));
00345     if (value == 1)
00346         QApplication::instance()->quit();
00347 }

void Oubliette::showInventory (  )  [private]

Definition at line 211 of file oubliette.cpp.

References QGridLayout::addWidget(), Qt::AlignCenter, QObject::connect(), d, i, QList< T >::isEmpty(), Cursor::items(), OubliettePlan::level(), m_character, m_currentLevel, m_oubliettePlan, Item::name(), Item::pixmapName(), row, QLabel::setAlignment(), QListWidgetItem::setData(), QPushButton::setDefault(), QWidget::setFocus(), QListWidgetItem::setIcon(), showInventoryItem(), SIGNAL, QList< T >::size(), SLOT, OublietteLevel::totalItems(), Qt::WA_DeleteOnClose, and Qt::WA_ShowModal.

Referenced by keyPressEvent().

00212 {
00213     QDialog *d = new QDialog();
00214     d->setWindowTitle(tr("Inventory"));
00215     QGridLayout *gl = new QGridLayout(d);
00216     int row = 0;
00217     QLabel *label;
00218     const QList<const Item *> items = m_character.items();
00219     if (items.isEmpty()) {
00220         label = new QLabel(tr("You have <B>No</B> Items"), d);
00221         label->setAlignment(Qt::AlignCenter);
00222         gl->addWidget(label, row, 0, 1, 4);
00223         ++row;
00224     } else {
00225         QListWidget *lw = new QListWidget(d);
00226         gl->addWidget(lw, 0, 0, 1, 4);
00227         int i = 0;
00228         foreach (const Item *item, items) {
00229             QListWidgetItem *lwi = new QListWidgetItem(item->name(), lw);
00230             lwi->setIcon(QIcon(item->pixmapName()));
00231             lwi->setData(99, i);
00232             ++i;
00233         }
00234         connect(lw, SIGNAL(itemActivated(QListWidgetItem*)),
00235                 this, SLOT(showInventoryItem(QListWidgetItem*)));
00236     }
00237     label = new QLabel(tr("You have %1 of %2 items")
00238                       .arg(items.size())
00239                       .arg(m_oubliettePlan.level(m_currentLevel)->totalItems()), d);
00240     gl->addWidget(label, 1, 1, 1, 1);
00241     QPushButton *btn = new QPushButton(tr("OK"), d);
00242     btn->setDefault(true);
00243     btn->setFocus();
00244     gl->addWidget(btn, 2, 3, 1, 1);
00245     connect(btn, SIGNAL(clicked()), d, SLOT(accept()));
00246     d->setAttribute(Qt::WA_ShowModal);
00247     d->setAttribute(Qt::WA_DeleteOnClose);
00248     d->show();
00249 }

Here is the call graph for this function:

void Oubliette::animateItem ( const Item item,
const QPoint pos 
) [private]

Definition at line 297 of file oubliette.cpp.

References QList< T >::append(), m_effects, m_timerID, QWidget::pos(), and QObject::startTimer().

Referenced by tryMove().

00298 {
00299     ItemEffect *newEffect = new ItemEffect(item, pos);
00300     m_effects.append(newEffect);
00301     if (m_timerID == -1) {
00302         m_timerID = startTimer(20);
00303     }
00304 }

Here is the call graph for this function:

bool Oubliette::tryMove ( const QPoint newPos  )  [private]

Definition at line 255 of file oubliette.cpp.

References Cursor::addItem(), animateItem(), QList< T >::clear(), Tile::ClosedDoor, Tile::Floor, QList< T >::isEmpty(), Cursor::items(), Tile::items, level, OubliettePlan::level(), m_character, m_currentLevel, m_oldCursorPosition, m_oubliettePlan, Tile::OpenDoor, Cursor::position(), Cursor::setPosition(), showVictory(), QTimer::singleShot(), QList< T >::size(), SLOT, OublietteLevel::totalItems(), Tile::type, and updateExplored().

Referenced by keyPressEvent().

00256 {
00257     OublietteLevel *level = m_oubliettePlan.level(m_currentLevel);
00258     Tile le = level->tile(newPos);
00259     if (le.type == Tile::Floor || le.type == Tile::ClosedDoor || le.type == Tile::OpenDoor) {
00260         m_oldCursorPosition = m_character.position();
00261         m_character.setPosition(newPos);
00262         if (le.type == Tile::ClosedDoor) {
00263             le.type = Tile::OpenDoor;
00264             level->setTile(newPos, le);
00265         }
00266         if (!le.items.isEmpty()) {
00267             foreach (const Item *item, le.items) {
00268                 m_character.addItem(item);
00269                 animateItem(item, newPos);
00270                 if (m_character.items().size() == m_oubliettePlan.level(m_currentLevel)->totalItems())
00271                     QTimer::singleShot(0, this, SLOT(showVictory()));
00272 
00273             }
00274             le.items.clear();
00275             level->setTile(newPos, le);
00276         }
00277         updateExplored();
00278         return true;
00279     }
00280     return false;
00281 }

Here is the call graph for this function:

void Oubliette::updateExplored (  )  [private]

Definition at line 283 of file oubliette.cpp.

References level, OubliettePlan::level(), m_character, m_currentLevel, m_oubliettePlan, Cursor::position(), FOV::start(), QPoint::x(), and QPoint::y().

Referenced by tryMove().

00284 {
00285     static SIMPLEFOV fov;
00286     QPoint exploredPoint = m_character.position();
00287     OublietteLevel *level = m_oubliettePlan.level(m_currentLevel);
00288     fov.start(level, exploredPoint.x(), exploredPoint.y(), 8);
00289 }

Here is the call graph for this function:

void Oubliette::paintOubliette ( QPainter p,
const QRect rect 
) [private]

Definition at line 122 of file oubliette.cpp.

References fillTile(), QRect::height(), level, OubliettePlan::level(), m_currentLevel, m_oubliettePlan, p, TileHeight, TileWidth, QRect::width(), QRect::x(), QWidget::x(), QRect::y(), and QWidget::y().

Referenced by paintEvent().

00123 {
00124    // snap down
00125     int tile_x1 = paintRect.x() / TileWidth;
00126     int tile_y1 = paintRect.y() / TileHeight;
00127 
00128     // snap up
00129     int tile_x2 = (paintRect.x() + paintRect.width()) / TileWidth + TileWidth;
00130     int tile_y2 = (paintRect.y() + paintRect.height()) / TileHeight + TileHeight;
00131 
00132     OublietteLevel *level = m_oubliettePlan.level(m_currentLevel);
00133     for (int y = tile_y1; y <= tile_y2; ++y) {
00134         for (int x = tile_x1; x <= tile_x2; ++x) {
00135             fillTile(p, x, y, level->tile(x, y));
00136         }
00137     }
00138 }

Here is the call graph for this function:

void Oubliette::fillTile ( QPainter p,
int  x,
int  y,
Tile  tile 
) [private]

Definition at line 140 of file oubliette.cpp.

References QList< T >::at(), Tile::ClosedDoor, Tile::Explored, QPixmapCache::find(), Tile::flags, Tile::Floor, QPixmapCache::insert(), QList< T >::isEmpty(), QString::isEmpty(), QPixmap::isNull(), Tile::items, Tile::OpenDoor, p, Item::pixmapName(), QWidget::QPixmap, TileHeight, TileWidth, Tile::type, and Tile::Wall.

Referenced by fillTile(), and paintOubliette().

00141 {
00142     QString str;
00143     switch (le.type) {
00144     case Tile::ClosedDoor:
00145         str = ":/qthack/images/dngn_closed_door.png";
00146         break;
00147     case Tile::OpenDoor:
00148         str = ":/qthack/images/dngn_open_door.png";
00149         break;
00150     case Tile::Wall:
00151         str = ":/qthack/images/dngn_rock_wall_07.png";
00152         break;
00153     case Tile::Floor:
00154         break;
00155     default:
00156         return; // Don't draw it...
00157     }
00158 
00159     if (!le.items.isEmpty())
00160         str = le.items.at(0)->pixmapName();
00161     if (le.flags & Tile::Explored) {
00162         if (str.isEmpty()) {
00163             p->fillRect(x * TileWidth, y * TileHeight, TileWidth, TileHeight, QColor("teal"));
00164         } else {
00165             p->fillRect(x * TileWidth, y * TileHeight, TileWidth, TileHeight, QColor("teal"));
00166             QPixmap pm;
00167             QPixmapCache::find(str, pm);
00168             if (pm.isNull()) {
00169                 pm = QPixmap(str);
00170                 QPixmapCache::insert(str, pm);
00171             }
00172             p->drawPixmap(x * TileWidth, y * TileHeight, TileWidth, TileHeight, pm);
00173         }
00174     }
00175 }

Here is the call graph for this function:

void Oubliette::fillTile ( QPainter p,
const QPoint point,
Tile  tile 
) [inline, private]

Definition at line 63 of file oubliette.h.

References fillTile(), p, QPoint::x(), and QPoint::y().

00064     { fillTile(p, point.x(), point.y(), tile); }

Here is the call graph for this function:

void Oubliette::characterMoved ( const QPoint pt  )  [signal]

Referenced by keyPressEvent().


Member Data Documentation

OubliettePlan Oubliette::m_oubliettePlan [private]

Definition at line 70 of file oubliette.h.

Referenced by Oubliette(), paintOubliette(), showInventory(), tryMove(), and updateExplored().

Cursor Oubliette::m_character [private]

Definition at line 71 of file oubliette.h.

Referenced by character(), keyPressEvent(), Oubliette(), paintEvent(), showInventory(), showInventoryItem(), showVictory(), tryMove(), updateExplored(), and visualCursorPos().

QPoint Oubliette::m_oldCursorPosition [private]

Definition at line 72 of file oubliette.h.

Referenced by Oubliette(), and tryMove().

int Oubliette::m_currentLevel [private]

Definition at line 73 of file oubliette.h.

Referenced by Oubliette(), paintOubliette(), showInventory(), tryMove(), and updateExplored().

QList<ItemEffect *> Oubliette::m_effects [private]

Definition at line 74 of file oubliette.h.

Referenced by animateItem(), paintEvent(), and timerEvent().

int Oubliette::m_timerID [private]

Definition at line 75 of file oubliette.h.

Referenced by animateItem(), and timerEvent().


The documentation for this class was generated from the following files:
Generated on Thu Mar 15 15:15:58 2007 for Qt 4.2 User's Guide by  doxygen 1.5.1