From a2ce79da7a7b88e673566cca2381ad59a1ea68cb Mon Sep 17 00:00:00 2001 From: mrbesen Date: Wed, 15 Dec 2021 15:25:09 +0100 Subject: [PATCH] arrow keys working --- include/mainwindow.h | 11 ++-- src/mainwindow.cpp | 121 +++++++++++++++++++++---------------------- 2 files changed, 64 insertions(+), 68 deletions(-) diff --git a/include/mainwindow.h b/include/mainwindow.h index 5d3277f..a50ca42 100644 --- a/include/mainwindow.h +++ b/include/mainwindow.h @@ -4,6 +4,7 @@ #include #include #include +#include #include "config.h" #include "sound.h" @@ -39,9 +40,8 @@ private: void removeAllButtons(); void loadButtonsFromConfig(); - void selectCurrentButton(); - void unselectCurrentButton(); - QLayoutItem* findCurrentButton() const; + void reselectNext(std::function stepf); + QLayoutItem* findNextButton(std::function stepf); Ui::Soundboard *ui; std::vector soundbuttons; @@ -53,8 +53,9 @@ private: QxtGlobalShortcut* right = nullptr; QxtGlobalShortcut* play = nullptr; - uint8_t xpos = 0; - uint8_t ypos = 0; + // "real" coordinates + uint8_t xCoord = 0; + uint8_t yCoord = 0; Config config; }; diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index 99e7cc4..e19527e 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -69,55 +69,49 @@ void MainWindow::stop() { Sound::instance().stopAll(); } +static uint8_t wrap(uint8_t a, uint8_t max) { + if(a >= max) + return max-1; + return a; +} + +static uint8_t wrap0(uint8_t a, uint8_t max) { + if(a >= max) + return 0; + return a; +} + void MainWindow::moveUp() { Log::debug << "up"; - unselectCurrentButton(); - ypos--; - if(ypos >= ui->gridLayout->rowCount()) { - ypos = ui->gridLayout->rowCount()-1; - } - selectCurrentButton(); + reselectNext([max = ui->gridLayout->rowCount()](uint8_t& x, uint8_t& y){ y = wrap(--y, max); }); + Log::debug << "x " << (int) xCoord << " y " << (int) yCoord; } void MainWindow::moveDown() { Log::debug << "down"; - unselectCurrentButton(); - ypos++; - if(ypos >= ui->gridLayout->rowCount()) { - ypos = ui->gridLayout->rowCount()-1; - } - selectCurrentButton(); + reselectNext([max = ui->gridLayout->rowCount()](uint8_t& x, uint8_t& y){ y = wrap0(++y, max); }); + Log::debug << "x " << (int) xCoord << " y " << (int) yCoord; } void MainWindow::moveLeft() { Log::debug << "left"; - unselectCurrentButton(); - xpos--; - if(xpos >= ui->gridLayout->columnCount()) { - xpos = ui->gridLayout->columnCount()-1; - } - selectCurrentButton(); + reselectNext([max = ui->gridLayout->columnCount()](uint8_t& x, uint8_t& y){ x = wrap(--x, max); }); + Log::debug << "x " << (int) xCoord << " y " << (int) yCoord; } void MainWindow::moveRight() { Log::debug << "right"; - unselectCurrentButton(); - xpos++; - if(xpos >= ui->gridLayout->columnCount()) { - xpos = ui->gridLayout->columnCount()-1; - } - selectCurrentButton(); + reselectNext([max = ui->gridLayout->columnCount()](uint8_t& x, uint8_t& y){ x = wrap0(++x, max); }); + Log::debug << "x " << (int) xCoord << " y " << (int) yCoord; } void MainWindow::playCurrent() { Log::debug << "play"; - QLayoutItem* item = findCurrentButton(); - if(item) { - if(item->widget()) { - QPushButton* button = (QPushButton*) item->widget(); - button->click(); - } + QLayoutItem* item = ui->gridLayout->itemAtPosition(yCoord, xCoord); + if(item && item->widget()) { + SoundButton* button = (SoundButton*) item->widget(); + button->click(); } } @@ -156,6 +150,8 @@ void MainWindow::loadButtonsFromConfig() { int y = 0; const std::vector>& buttonConfigs = config.rootConfig.buttons; for (const std::vector& line : buttonConfigs) { + if(line.empty()) continue; + for (const Config::ButtonConfig& bc : line) { if (!bc.isValid()) continue; @@ -178,49 +174,48 @@ void MainWindow::loadButtonsFromConfig() { y++; } - xpos = 0; - ypos = 0; + xCoord = 0; + yCoord = 0; + + // select the first button + QLayoutItem* item = ui->gridLayout->itemAtPosition(0, 0); + if(item && item->widget()) { + ((SoundButton*) item->widget())->setHighlighted(true); + } // ui->gridLayoutWidget->setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding)); } -void MainWindow::selectCurrentButton() { - QLayoutItem* item = findCurrentButton(); - if(item) { - if(item->widget()) { - SoundButton* button = (SoundButton*) item->widget(); - button->setHighlighted(true); +void MainWindow::reselectNext(std::function stepf) { + QLayoutItem* item = ui->gridLayout->itemAtPosition(yCoord, xCoord); + QLayoutItem* newitem = findNextButton(stepf); + + if(item != newitem) { + if(item && item->widget()) { + ((SoundButton*) item->widget())->setHighlighted(false); + } + if(newitem && newitem->widget()) { + ((SoundButton*) newitem->widget())->setHighlighted(true); } } } -void MainWindow::unselectCurrentButton() { - QLayoutItem* item = findCurrentButton(); - if(item) { - if(item->widget()) { - SoundButton* button = (SoundButton*) item->widget(); - button->setHighlighted(false); - } - } -} +QLayoutItem* MainWindow::findNextButton(std::function stepf) { + // need copy for while condition + uint8_t x = xCoord; + uint8_t y = yCoord; -QLayoutItem* MainWindow::findCurrentButton() const { - QLayoutItem* lastitem = nullptr; - int8_t rstep = -1; - for(uint8_t step = 0; step < ui->gridLayout->columnCount() && rstep < xpos; ++step) { - Log::info << "check: " << (int) ypos << " " << (int) step; + QLayoutItem* currentItem = ui->gridLayout->itemAtPosition(y, x); + QLayoutItem* item = currentItem; - QLayoutItem* item = ui->gridLayout->itemAtPosition(ypos, step); - if(lastitem == item) continue; + do { + stepf(x, y); + item = ui->gridLayout->itemAtPosition(y, x); + } while(item == currentItem && (x != xCoord || y != yCoord)); - rstep++; - lastitem = item; - } + xCoord = x; + yCoord = y; - if(lastitem != nullptr && rstep == xpos) { - return lastitem; - } - - Log::debug << "current button is null"; - return nullptr; + // might the same item + return item; } \ No newline at end of file