Skip to content

Commit

Permalink
faster moving and scrolling in keyboard control mode
Browse files Browse the repository at this point in the history
  • Loading branch information
DevAlone committed May 21, 2017
1 parent 50fe652 commit a6591d7
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 15 deletions.
24 changes: 20 additions & 4 deletions mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,25 @@ void MainWindow::keyPressEvent(QKeyEvent* event)
bool MainWindow::event(QEvent* event)
{
if (event->type() == QEvent::KeyPress || event->type() == QEvent::KeyRelease) {
// TODO: add checks for out of field and scroll
// TODO: change step from 1 pixel size to size of 1 cell
// TODO: faster moving

QKeyEvent* keyEvent = static_cast<QKeyEvent*>(event);
QPoint cursorPos = cursor().pos();

auto cell = minesFieldWidget->getCellByMousePoint(minesFieldWidget->mapFromGlobal(cursorPos));
if (cell.x() <= 0) {
cell.setX(0);
minesFieldWidget->horizontalScrollPosChanged(-1);
} else if (cell.x() > minesFieldWidget->getViewport().cols - 1) {
cell.setX(minesFieldWidget->getViewport().cols - 1);
minesFieldWidget->horizontalScrollPosChanged(1);
}
if (cell.y() <= 0) {
cell.setY(0);
minesFieldWidget->verticalScrollPosChanged(-1);
} else if (cell.y() > minesFieldWidget->getViewport().rows - 1) {
cell.setY(minesFieldWidget->getViewport().rows - 1);
minesFieldWidget->verticalScrollPosChanged(1);
}

qDebug() << cursorPos << "?" << cell.x() << ":" << cell.y();

auto stepSize = minesFieldWidget->getCellSize() + minesFieldWidget->getBorderSize();
Expand All @@ -53,6 +64,11 @@ bool MainWindow::event(QEvent* event)

auto key = keyEvent->key();

if (keyEvent->modifiers() & Qt::ControlModifier)
stepSize = QSize(stepSize.width() * 2, stepSize.height() * 2);
if (keyEvent->modifiers() & Qt::ShiftModifier)
stepSize = QSize(stepSize.width() * 4, stepSize.height() * 4);

if (event->type() == QEvent::KeyPress) {
if (key == Qt::Key_A)
cursorPos.setX(cursorPos.x() - stepSize.width());
Expand Down
8 changes: 6 additions & 2 deletions minesfieldwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@ void MinesFieldWidget::paintEvent(QPaintEvent* event)
return;
}

if (!updatingMutex.tryLock())
return;
updatingMutex.lock();

painter.drawPixmap(0, 0, pixmap);

Expand Down Expand Up @@ -290,6 +289,11 @@ QColor MinesFieldWidget::getCellColor(Cell::CellState cellState, int minesAround
return color;
}

Viewport MinesFieldWidget::getViewport() const
{
return viewport;
}

QSize MinesFieldWidget::getBorderSize() const
{
return borderSize;
Expand Down
17 changes: 9 additions & 8 deletions minesfieldwidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@
#include <QWidget>
#include <QtWidgets>

struct Viewport {
long long x, y;
long width, height;
long start_col, start_row;
long cols, rows;
};

class MinesFieldWidgetUpdaterThread;

class MinesFieldWidget : public QWidget {
Expand Down Expand Up @@ -38,6 +45,8 @@ class MinesFieldWidget : public QWidget {

Point getCellByMousePoint(const QPoint& mousePoint);
Point convertCellPointToAbsolute(const Point& point);
Viewport getViewport() const;

signals:

public slots:
Expand Down Expand Up @@ -66,14 +75,6 @@ private slots:
QColor getCellColor(Cell::CellState cellState, int minesAroundCell);

QPixmap pixmap;

struct Viewport {
long long x, y;
long width, height;
long start_col, start_row;
long cols, rows;
};

Viewport viewport;

QSize cellSize;
Expand Down
2 changes: 1 addition & 1 deletion minesfieldwidgetupdaterthread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ void MinesFieldWidgetUpdaterThread::run()
auto& cells = minesFieldWidget->field->getCells();
const Cell& cell = cells.at(i);

int minesAroundCell = cell.minesAround(); // TODO: ?
int minesAroundCell = cell.minesAround();
QColor color = minesFieldWidget->getCellColor(cell.cellState(), minesAroundCell);

painter.fillRect((x - minesFieldWidget->viewport.start_col) * stepX + minesFieldWidget->borderSize.width(),
Expand Down

0 comments on commit a6591d7

Please sign in to comment.