// // overrides // void TilesetWidget::mousePressEvent(QMouseEvent * event) { event->accept(); auto state = MainWindow::getCurrentState(); if (!state) return; if (event->button() == Qt::LeftButton) { auto pos = event->localPos(); auto tileProperties = state->getTileProperties(); int tw = tileProperties.size.width(); int th = tileProperties.size.height(); int x = (pos.x() - OFFSET) / _pixelSize.width() / 8 / tw; int y = (pos.y() - OFFSET) / _pixelSize.height() / 8 / th; int tileIndex = x + y * (_columns / tw); // quick sanity check if (! (tileIndex >= 0 && tileIndex < _maxTiles)) return; if (QGuiApplication::keyboardModifiers() & Qt::ShiftModifier) { // Click + Shift == select mode _selecting = true; if (x >= _cursorPos.x()) x++; if (y >= _cursorPos.y()) y++; _selectingSize = {x - _cursorPos.x(), y - _cursorPos.y()}; // sanity check _selectingSize = { qBound(-_cursorPos.x(), _selectingSize.width(), _tileColums - _cursorPos.x()), qBound(-_cursorPos.y(), _selectingSize.height(), _tileRows - _cursorPos.y()) }; update(); } else { // different and valid tileIndex? if (_cursorPos.x() != x || _cursorPos.y() != y) { _selecting = false; _selectingSize = {1,1}; _cursorPos = {x,y}; state->setTileIndex(tileIndex); update(); } } } }
void AnimatedSprite::stop(HashedString animSequence, int frame) { currentSequence = spriteData->getSequence(animSequence); assert(currentSequence); startTime = GameApp::getSingleton().getTimer()->getTime(); startFrame = frame; playing = false; setTileIndex(currentSequence->getStartIndex() + frame); }
void AnimatedSprite::update(GameTime& time) { if (playing) { if (currentSequence->getFPS() > 0 && currentSequence->getLength() > 0) { u32 currentTime = time.getTime(); u32 deltaTime = currentTime - startTime; u32 currentFrame = deltaTime*currentSequence->getFPS()/1000.0; currentFrame += startFrame; // if looping is disabled, it will stop animating at the last frame. if (!looping && currentFrame >= currentSequence->getLength()) { playing = false; return; } currentFrame %= currentSequence->getLength(); setTileIndex(currentSequence->getStartIndex() + currentFrame); } } }
void TilesetWidget::keyPressEvent(QKeyEvent *event) { event->accept(); auto state = MainWindow::getCurrentState(); if (!state) return; QPoint point; switch (event->key()) { case Qt::Key_Left: point = {-1,0}; break; case Qt::Key_Right: point = {+1,0}; break; case Qt::Key_Down: point = {0,+1}; break; case Qt::Key_Up: point = {0,-1}; break; default: QWidget::keyPressEvent(event); return; } bool selecting = (event->modifiers() & Qt::ShiftModifier); // disabling selecting? if (_selecting && !selecting) { _selectingSize = {1,1}; } else { if (selecting) { _selectingSize += {point.x(), point.y()}; if (_selectingSize.width() == 0) _selectingSize.setWidth(_selectingSize.width() + 1 * point.x()); if (_selectingSize.height() == 0) _selectingSize.setHeight(_selectingSize.height() + 1 * point.y()); _selectingSize = { qBound(-_cursorPos.x(), _selectingSize.width(), _tileColums-_cursorPos.x()), qBound(-_cursorPos.y(), _selectingSize.height(), _tileRows-_cursorPos.y()) }; } else { auto pos = _cursorPos + point; pos = {qBound(0, pos.x(), _tileColums-1), qBound(0, pos.y(), _tileRows-1)}; int tileIdx = pos.y() * _tileColums + pos.x(); if (pos != _cursorPos && tileIdx >=0 && tileIdx < _maxTiles) { _cursorPos = pos; state->setTileIndex(tileIdx); } } } _selecting = selecting; update(); }