forked from MrBesen/soundboard
arrow keys working
This commit is contained in:
parent
2ba6aef1de
commit
a2ce79da7a
|
@ -4,6 +4,7 @@
|
|||
#include <QMainWindow>
|
||||
#include <qlayoutitem.h>
|
||||
#include <vector>
|
||||
#include <functional>
|
||||
|
||||
#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<void(uint8_t&,uint8_t&)> stepf);
|
||||
QLayoutItem* findNextButton(std::function<void(uint8_t&,uint8_t&)> stepf);
|
||||
|
||||
Ui::Soundboard *ui;
|
||||
std::vector<SoundButton*> 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;
|
||||
};
|
||||
|
|
|
@ -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<std::vector<Config::ButtonConfig>>& buttonConfigs = config.rootConfig.buttons;
|
||||
for (const std::vector<Config::ButtonConfig>& 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<void(uint8_t&,uint8_t&)> 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<void(uint8_t&,uint8_t&)> 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;
|
||||
}
|
Loading…
Reference in New Issue