Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions SerialPrograms/Source/CommonFramework/Panels/PanelList.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#include "Common/Cpp/Exceptions.h"
#include "CommonFramework/Logging/Logger.h"
#include "CommonFramework/Panels/PanelSession.h"
#include "PanelList.h"

//#include <iostream>
Expand Down Expand Up @@ -33,6 +34,19 @@ PanelListDescriptor::PanelListDescriptor(
}
}

bool load_panel_session(
const std::shared_ptr<const PanelDescriptor>& descriptor,
const JsonValue& panel_json,
PanelHolder& holder
){
if (!holder.report_new_panel_intent(*descriptor)){
return false;
}
std::unique_ptr<PanelSession> panel = descriptor->make_panel();
panel->load_json(panel_json);
holder.load_panel(descriptor, std::move(panel));
return true;
}



Expand Down
11 changes: 11 additions & 0 deletions SerialPrograms/Source/CommonFramework/Panels/PanelList.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#ifndef PokemonAutomation_PanelList_H
#define PokemonAutomation_PanelList_H

#include <memory>
#include <vector>
#include "CommonFramework/ImageTypes/ImageRGB32.h"
#include "CommonFramework/Panels/PanelTools.h"
Expand Down Expand Up @@ -48,6 +49,16 @@ class PanelListDescriptor{
};


// Instantiate `descriptor` (non-null), load `panel_json` into it, and install
// it in `holder`. Returns false, loading nothing, if
// `holder.report_new_panel_intent()` declines.
bool load_panel_session(
const std::shared_ptr<const PanelDescriptor>& descriptor,
const JsonValue& panel_json,
PanelHolder& holder
);



}
#endif
Original file line number Diff line number Diff line change
Expand Up @@ -87,24 +87,22 @@
}


void PanelListWidget::handle_panel_clicked(const std::string& text){

Check warning on line 90 in SerialPrograms/Source/CommonFramework/Panels/UI/PanelListWidget.cpp

View workflow job for this annotation

GitHub Actions / code-health

Code health (advisory)

PokemonAutomation::PanelListWidget::handle_panel_clicked( const std :: string & text): function_lines=26 (advisory above 25, limit 50)
auto iter = m_panel_map.find(text);
if (iter == m_panel_map.end()){
PERSISTENT_SETTINGS().panels[JSON_PROGRAM_PANEL] = "";
return;
}
std::shared_ptr<const PanelDescriptor>& descriptor = iter->second;
const std::shared_ptr<const PanelDescriptor>& descriptor = iter->second;
// cout << descriptor->display_name() << endl;
if (!m_panel_holder.report_new_panel_intent(*descriptor)){
return;
}
try{
std::unique_ptr<PanelSession> panel = descriptor->make_panel();
// try{
panel->load_json(PERSISTENT_SETTINGS().panels[descriptor->identifier()]);
// }catch (ParseException&){}
m_panel_holder.load_panel(descriptor, std::move(panel));

if (!load_panel_session(
descriptor,
PERSISTENT_SETTINGS().panels[descriptor->identifier()],
m_panel_holder
)){
return;
}
PERSISTENT_SETTINGS().panels[JSON_PROGRAM_PANEL] = iter->first;
}catch (const Exception& error){
QMessageBox box;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "CommonFramework/Tools/GlobalThreadPools.h"
#include "CommonFramework/Windows/LabChannel_Tests.h"
#include "CommonFramework/Startup/StartupArguments_Tests.h"
#include "PanelCatalog_Tests.h"
#include "CommonFramework/Startup/LabStartupPolicy_Tests.h"
#include "CommonFramework/Devices/LabDeviceAccessQt_Tests.h"
#include "CommonTools/VisualDetectors/BlackBorderDetector.h"
Expand Down Expand Up @@ -70,7 +71,7 @@



UnitTestDatabase make_UNIT_TESTS_ALL(){

Check warning on line 74 in SerialPrograms/Source/ComputerPrograms/ComputerPrograms_UnitTestRunner.cpp

View workflow job for this annotation

GitHub Actions / code-health

Code health (advisory)

PokemonAutomation::ComputerPrograms::make_UNIT_TESTS_ALL(): function_lines=36 (advisory above 25, limit 50)
UnitTestDatabase ret;

add_tests_QtEventThreadPool(ret, GlobalThreadPools::computation_normal());
Expand All @@ -86,6 +87,7 @@
add_tests_AudioFileLoader(ret);
add_tests_LabChannel(ret);
add_tests_StartupArguments(ret);
add_tests_PanelCatalog(ret);
add_tests_LabStartupPolicy(ret);
add_tests_LabDeviceAccess(ret);
OCR::add_tests(ret);
Expand Down
66 changes: 66 additions & 0 deletions SerialPrograms/Source/PanelCatalog.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/* Panel Catalog
*
* From: https://github.com/PokemonAutomation/
*
*/

#include "CommonFramework/StaticGlobals.h"
#include "ML/ML_Panels.h"
#include "NintendoSwitch/NintendoSwitch_Panels.h"
#include "VideoGames/PokemonSwSh/PokemonSwSh_Panels.h"
#include "VideoGames/PokemonHome/PokemonHome_Panels.h"
#include "PokemonBDSP/PokemonBDSP_Panels.h"
#include "VideoGames/PokemonFRLG/PokemonFRLG_Panels.h"
#include "VideoGames/PokemonLA/PokemonLA_Panels.h"
#include "VideoGames/PokemonLGPE/PokemonLGPE_Panels.h"
#include "VideoGames/PokemonRSE/PokemonRSE_Panels.h"
#include "VideoGames/PokemonSV/PokemonSV_Panels.h"
#include "VideoGames/PokemonLZA/PokemonLZA_Panels.h"
#include "VideoGames/PokemonPokopia/PokemonPokopia_Panels.h"
#include "VideoGames/ZeldaTotK/ZeldaTotK_Panels.h"
#include "PanelCatalog.h"

namespace PokemonAutomation{


std::vector<std::unique_ptr<PanelListDescriptor>> make_all_panel_lists(){
std::vector<std::unique_ptr<PanelListDescriptor>> ret;
ret.emplace_back(std::make_unique<NintendoSwitch::PanelListFactory>());
ret.emplace_back(std::make_unique<NintendoSwitch::PokemonHome::PanelListFactory>());
ret.emplace_back(std::make_unique<NintendoSwitch::PokemonLGPE::PanelListFactory>());
ret.emplace_back(std::make_unique<NintendoSwitch::PokemonSwSh::PanelListFactory>());
ret.emplace_back(std::make_unique<NintendoSwitch::PokemonBDSP::PanelListFactory>());
ret.emplace_back(std::make_unique<NintendoSwitch::PokemonLA::PanelListFactory>());
ret.emplace_back(std::make_unique<NintendoSwitch::PokemonSV::PanelListFactory>());
ret.emplace_back(std::make_unique<NintendoSwitch::PokemonLZA::PanelListFactory>());
ret.emplace_back(std::make_unique<NintendoSwitch::PokemonFRLG::PanelListFactory>());
ret.emplace_back(std::make_unique<NintendoSwitch::PokemonPokopia::PanelListFactory>());
if (STATIC_GLOBALS.DEVELOPER_MODE){
ret.emplace_back(std::make_unique<NintendoSwitch::PokemonRSE::PanelListFactory>());
}

ret.emplace_back(std::make_unique<NintendoSwitch::ZeldaTotK::PanelListFactory>());

if (STATIC_GLOBALS.DEVELOPER_MODE){
ret.emplace_back(std::make_unique<ML::PanelListFactory>());
}
return ret;
}

std::optional<PanelMatch> find_panel(
const std::vector<std::unique_ptr<PanelListDescriptor>>& lists,
std::string_view identifier
){
for (size_t c = 0; c < lists.size(); c++){
for (PanelEntry& entry : lists[c]->panels()){
if (entry.descriptor == nullptr || entry.descriptor->identifier() != identifier){
continue;
}
return PanelMatch{.category_index = c, .entry = std::move(entry)};
}
}
return std::nullopt;
}


} // namespace PokemonAutomation
38 changes: 38 additions & 0 deletions SerialPrograms/Source/PanelCatalog.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/* Panel Catalog
*
* From: https://github.com/PokemonAutomation/
*
*/

#ifndef PokemonAutomation_PanelCatalog_H
#define PokemonAutomation_PanelCatalog_H

#include <cstddef>
#include <memory>
#include <optional>
#include <string_view>
#include <vector>
#include "CommonFramework/Panels/PanelList.h"

namespace PokemonAutomation{


// Every program category, in dropdown order.
std::vector<std::unique_ptr<PanelListDescriptor>> make_all_panel_lists();


// A panel found by find_panel(), with the index of its list in `lists`.
struct PanelMatch{
size_t category_index;
PanelEntry entry;
};

// Scans each list's panels() for the panel with `identifier`.
std::optional<PanelMatch> find_panel(
const std::vector<std::unique_ptr<PanelListDescriptor>>& lists,
std::string_view identifier
);


} // namespace PokemonAutomation
#endif
125 changes: 125 additions & 0 deletions SerialPrograms/Source/PanelCatalog_Tests.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
/* Panel Catalog Tests
*
* From: https://github.com/PokemonAutomation/
*
*/

#include "Common/Cpp/Json/JsonValue.h"
#include "CommonFramework/Panels/PanelDescriptor.h"
#include "CommonFramework/Panels/PanelList.h"
#include "CommonFramework/Panels/PanelSession.h"
#include "PanelCatalog.h"
#include "PanelCatalog_Tests.h"

namespace PokemonAutomation{
namespace{


class FakePanelDescriptor final : public PanelDescriptor{
public:
explicit FakePanelDescriptor(std::string identifier)
: PanelDescriptor(
identifier,
/*category=*/"FakeCategory",
/*display_name=*/identifier,
/*doc_link=*/"",
/*description=*/"",
PanelQualityStanding::DEVELOPER,
/*restore_defaults_button=*/false
)
{}

virtual std::unique_ptr<PanelSession> make_panel() const override{
return std::make_unique<PanelSession>(*this);
}
};

// One category: a divider, then the panel "Fake:Alpha".
class FakePanelList final : public PanelListDescriptor{
public:
FakePanelList()
: PanelListDescriptor("FakeList")
{}

protected:
virtual std::vector<PanelEntry> make_panels() const override{
std::vector<PanelEntry> ret;
ret.emplace_back("Divider");
ret.emplace_back(std::make_unique<FakePanelDescriptor>("Fake:Alpha"));
return ret;
}
};

class DecliningPanelHolder final : public PanelHolder{
public:
virtual bool report_new_panel_intent(const PanelDescriptor&) override{
return false;
}
virtual void load_panel(
std::shared_ptr<const PanelDescriptor>,
std::unique_ptr<PanelSession>
) override{
loaded = true;
}
virtual void on_busy() override{}
virtual void on_idle() override{}

bool loaded = false;
};


class FindPanelPastDividerTest final : public UnitTest{
public:
FindPanelPastDividerTest()
: UnitTest("PanelCatalog/FindPanel/past a divider")
{}

virtual UnitTestResult run(Logger&, CancellableScope&) const override{
std::vector<std::unique_ptr<PanelListDescriptor>> lists;
lists.emplace_back(std::make_unique<FakePanelList>());

const std::optional<PanelMatch> match = find_panel(lists, "Fake:Alpha");
if (!match){
return "find_panel() did not find Fake:Alpha.";
}
if (match->category_index != 0){
return "find_panel() returned the wrong category index.";
}
if (match->entry.descriptor == nullptr || match->entry.descriptor->identifier() != "Fake:Alpha"){
return "find_panel() returned the wrong panel.";
}
return true;
}
};

class LoadPanelSessionDeclinedTest final : public UnitTest{
public:
LoadPanelSessionDeclinedTest()
: UnitTest("PanelCatalog/LoadPanelSession/declined intent skips load")
{}

virtual UnitTestResult run(Logger&, CancellableScope&) const override{
DecliningPanelHolder holder;
const std::shared_ptr<const PanelDescriptor> descriptor =
std::make_shared<FakePanelDescriptor>("Fake:Alpha");

if (load_panel_session(descriptor, JsonValue(), holder)){
return "load_panel_session() returned true after the holder declined.";
}
if (holder.loaded){
return "load_panel_session() loaded a panel after the holder declined.";
}
return true;
}
};


} // namespace

void add_tests_PanelCatalog(UnitTestDatabase& database){
database.add<FindPanelPastDividerTest>();
database.add<LoadPanelSessionDeclinedTest>();
}


} // namespace PokemonAutomation
18 changes: 18 additions & 0 deletions SerialPrograms/Source/PanelCatalog_Tests.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/* Panel Catalog Tests
*
* From: https://github.com/PokemonAutomation/
*
*/

#ifndef PokemonAutomation_PanelCatalog_Tests_H
#define PokemonAutomation_PanelCatalog_Tests_H

#include "Common/Cpp/TestRunners/UnitTestDatabase.h"

namespace PokemonAutomation{

// Register PanelCatalog and load_panel_session() unit tests.
void add_tests_PanelCatalog(UnitTestDatabase& database);

} // namespace PokemonAutomation
#endif
Loading
Loading