From 41ecc12e75a39308243826a20706f8732cdf0837 Mon Sep 17 00:00:00 2001 From: mrbesen Date: Sat, 23 Apr 2022 00:39:45 +0200 Subject: [PATCH] display champion images --- include/datadragon.h | 3 ++ include/mainwindow.h | 5 +++ include/stagesettings.h | 12 +++++++ src/datadragon.cpp | 20 ++++++++++++ src/lolautoaccept.cpp | 1 + src/mainwindow.cpp | 7 ++++- src/stagesettings.cpp | 38 +++++++++++++++++++++++ ui/mainwindow.ui | 20 +++++++----- ui/stagesettings.ui | 69 ++++++++++++++++++++++++++++++++++++++++- 9 files changed, 166 insertions(+), 9 deletions(-) diff --git a/include/datadragon.h b/include/datadragon.h index bb82a36..18cd746 100644 --- a/include/datadragon.h +++ b/include/datadragon.h @@ -14,6 +14,7 @@ public: struct ChampData { public: + ChampData(); ChampData(const QJsonObject& source); std::string name; @@ -32,7 +33,9 @@ public: const std::string& getVersion(); const std::vector& getChamps(); cv::Mat getImage(const std::string& champid, ImageType imgtype = ImageType::SQUARE); + const ChampData& getBestMatchingChamp(const std::string& name); + static const ChampData EMPTYCHAMP; protected: std::string getCDNString() const; QByteArray requestRaw(const std::string& url); diff --git a/include/mainwindow.h b/include/mainwindow.h index e3d6495..460af86 100644 --- a/include/mainwindow.h +++ b/include/mainwindow.h @@ -4,6 +4,7 @@ #include #include +#include "datadragon.h" #include "lolautoaccept.h" QT_BEGIN_NAMESPACE @@ -28,9 +29,13 @@ private slots: void pickedited(const QString&); private: + // returns empty string on no match + const DataDragon::ChampData& getBestMatchingChamp(const std::string& name); + Ui::MainWindow *ui; LolAutoAccept& lolaa; std::thread lolaathread; + DataDragon dd; }; #endif // MAINWINDOW_H diff --git a/include/stagesettings.h b/include/stagesettings.h index e5566d4..c19a7a4 100644 --- a/include/stagesettings.h +++ b/include/stagesettings.h @@ -3,6 +3,8 @@ #include +#include "datadragon.h" + namespace Ui { class StageSettings; } @@ -26,6 +28,11 @@ public: QString getChampion() const; void setChampion(const QString& str); + + void setDataDragon(DataDragon* dd); + + void resizeEvent(QResizeEvent *event) override; + private slots: void championChangedinternal(const QString& str); void toggledinternal(int state); @@ -35,7 +42,12 @@ signals: void championChanged(const QString&); private: + void rescaleImage(); + Ui::StageSettings *ui; + DataDragon* dd = nullptr; + int currentdisplayedChampKey; + cv::Mat img; }; #endif // STAGESETTINGS_H diff --git a/src/datadragon.cpp b/src/datadragon.cpp index c4c816d..1b9062b 100644 --- a/src/datadragon.cpp +++ b/src/datadragon.cpp @@ -11,6 +11,7 @@ static const std::string BASEURL = "https://ddragon.leagueoflegends.com/"; +const DataDragon::ChampData DataDragon::EMPTYCHAMP; static size_t arrayWriteCallback(char* contents, size_t size, size_t nmemb, void* userdata) { if(userdata) { @@ -52,6 +53,8 @@ DataDragon::DataDragon() { curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, arrayWriteCallback); } +DataDragon::ChampData::ChampData() : key(-1) {} + DataDragon::ChampData::ChampData(const QJsonObject& source) { name = getValue(source, "name", ""); id = getValue(source, "id", ""); @@ -139,9 +142,26 @@ cv::Mat DataDragon::getImage(const std::string& champid, ImageType imgtype) { } cv::Mat decodedImage = cv::imdecode(rawData, cv::IMREAD_COLOR); + cv::cvtColor(decodedImage, decodedImage, cv::COLOR_BGR2RGB); return decodedImage; } +static std::string toLower(const std::string& in) { + return QString::fromStdString(in).toLower().toStdString(); +} + +const DataDragon::ChampData& DataDragon::getBestMatchingChamp(const std::string& name) { + getChamps(); + // for now: just check for a perfect hit + std::string lower = toLower(name); + for(const ChampData& it : champs) { + if(toLower(it.id) == lower || toLower(it.name) == lower) { + return it; + } + } + return EMPTYCHAMP; +} + std::string DataDragon::getCDNString() const { return "cdn/" + version + "/"; } diff --git a/src/lolautoaccept.cpp b/src/lolautoaccept.cpp index ded9074..763ac4b 100644 --- a/src/lolautoaccept.cpp +++ b/src/lolautoaccept.cpp @@ -207,6 +207,7 @@ void LolAutoAccept::run() { void LolAutoAccept::stop() { shouldrun = false; + state = State::LOBBY; } void LolAutoAccept::stopJoinThread() { diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index 0a0c79a..e65272c 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -1,11 +1,16 @@ #include "mainwindow.h" #include "ui_mainwindow.h" - #include +#include + MainWindow::MainWindow(LolAutoAccept& lolaa, QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow), lolaa(lolaa) { ui->setupUi(this); + + ui->prepickstage->setDataDragon(&dd); + ui->banstage->setDataDragon(&dd); + ui->pickstage->setDataDragon(&dd); } MainWindow::~MainWindow() { diff --git a/src/stagesettings.cpp b/src/stagesettings.cpp index dd074e9..04dfafe 100644 --- a/src/stagesettings.cpp +++ b/src/stagesettings.cpp @@ -1,9 +1,13 @@ #include "stagesettings.h" #include "ui_stagesettings.h" +#include + StageSettings::StageSettings(QWidget *parent) : QWidget(parent), ui(new Ui::StageSettings) { ui->setupUi(this); setMinimumSize(ui->gridLayout->minimumSize()); + + rescaleImage(); } StageSettings::~StageSettings() { @@ -35,10 +39,44 @@ void StageSettings::setChampion(const QString& str) { ui->lineEdit->setText(str); } +void StageSettings::setDataDragon(DataDragon* dd_) { + dd = dd_; +} + +void StageSettings::resizeEvent([[maybe_unused]] QResizeEvent* event) { + rescaleImage(); +} + void StageSettings::championChangedinternal(const QString& str) { emit championChanged(str); + + if(dd) { + const DataDragon::ChampData& cd = dd->getBestMatchingChamp(str.toStdString()); + if(cd.key > 0) { + Log::info << "bestmatching: " << cd; + if(cd.key != currentdisplayedChampKey) { + img = dd->getImage(cd.id); + rescaleImage(); + currentdisplayedChampKey = cd.key; + } + } + } } void StageSettings::toggledinternal(int state) { emit toggled(state == Qt::CheckState::Checked); } + +void StageSettings::rescaleImage() { + if(img.empty()) return; + + auto qimg = QImage((uchar*) img.data, img.cols, img.rows, img.step, QImage::Format_RGB888); + auto p = QPixmap::fromImage(qimg); + + // get label dimensions + int w = ui->champImg->width(); + int h = ui->champImg->height(); + + // set a scaled pixmap to a w x h window keeping its aspect ratio + ui->champImg->setPixmap(p.scaled(w, h, Qt::KeepAspectRatio)); // +} diff --git a/ui/mainwindow.ui b/ui/mainwindow.ui index afb40b7..fef7b76 100644 --- a/ui/mainwindow.ui +++ b/ui/mainwindow.ui @@ -6,20 +6,26 @@ 0 0 - 800 - 600 + 400 + 635 LoL-Auto-Accept + + + 0 + 0 + + 10 50 - 731 + 381 491 @@ -44,7 +50,7 @@ true - + 0 0 @@ -66,7 +72,7 @@ - + 0 0 @@ -88,7 +94,7 @@ - + 0 0 @@ -115,7 +121,7 @@ 0 0 - 800 + 400 24 diff --git a/ui/stagesettings.ui b/ui/stagesettings.ui index b5f26a6..63c06b3 100644 --- a/ui/stagesettings.ui +++ b/ui/stagesettings.ui @@ -7,15 +7,39 @@ 0 0 400 - 300 + 250 + + + 0 + 0 + + + + + 300 + 130 + + + + + 300 + 130 + + + + + 0 + 0 + + @@ -29,6 +53,12 @@ + + + 0 + 0 + + 160 @@ -44,6 +74,43 @@ + + + + + 1 + 1 + + + + + 32 + 32 + + + + + 64 + 64 + + + + + 4 + 4 + + + + + 32 + 32 + + + + + + +