Skip to content

Commit b0b043d

Browse files
committed
Next iteration of the tables.
1 parent 4439cb8 commit b0b043d

File tree

11 files changed

+84
-32
lines changed

11 files changed

+84
-32
lines changed

Common/Cpp/Options/CheckboxDropdownOption.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,7 @@ class CheckboxDropdownBase : public ConfigOption{
2525
: m_label(std::move(label))
2626
{}
2727

28-
const std::string& label() const{
29-
return m_label;
30-
}
28+
virtual std::string current_label() const = 0;
3129
virtual size_t size() const = 0;
3230
virtual const std::string& name_at_index(size_t index) const = 0;
3331

@@ -38,7 +36,7 @@ class CheckboxDropdownBase : public ConfigOption{
3836

3937
virtual ConfigWidget* make_QtWidget(QWidget& parent) override;
4038

41-
private:
39+
protected:
4240
std::string m_label;
4341
};
4442

@@ -49,6 +47,8 @@ class CheckboxDropdownCell : public CheckboxDropdownBase{
4947
using Database = CheckboxDropdownDatabase<FlagEnum>;
5048

5149
public:
50+
// If "label" is not empty, it will always display that on the dropdown.
51+
// If "label" is empty, it will display the checked items instead.
5252
CheckboxDropdownCell(
5353
std::string label,
5454
const Database& database,
@@ -95,6 +95,7 @@ class CheckboxDropdownCell : public CheckboxDropdownBase{
9595

9696

9797
public:
98+
virtual std::string current_label() const override;
9899
virtual size_t size() const override;
99100
virtual const std::string& name_at_index(size_t index) const override;
100101

Common/Cpp/Options/CheckboxDropdownOption.tpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,34 @@ FlagEnum CheckboxDropdownCell<FlagEnum>::current_value() const{
4949
return m_current;
5050
}
5151

52+
53+
template <typename FlagEnum>
54+
std::string CheckboxDropdownCell<FlagEnum>::current_label() const{
55+
ReadSpinLock lg(m_lock);
56+
if (!m_label.empty()){
57+
return m_label;
58+
}
59+
60+
std::string ret;
61+
bool first = true;
62+
for (const FlagEnumEntry<FlagEnum>& entry : m_database){
63+
if (is_empty(m_current & entry.value)){
64+
continue;
65+
}
66+
if (!first){
67+
ret += ", ";
68+
}
69+
first = false;
70+
71+
ret += entry.display;
72+
}
73+
74+
if (ret.size() > 30){
75+
ret = "( ... )";
76+
}
77+
78+
return ret;
79+
}
5280
template <typename FlagEnum>
5381
bool CheckboxDropdownCell<FlagEnum>::is_set(FlagEnum value) const{
5482
ReadSpinLock lg(m_lock);

Common/Qt/CheckboxDropdown.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,10 @@ class CheckboxDropdown : public QComboBox{
6969
return m_items[index];
7070
}
7171

72+
void setLabel(const QString& label){
73+
setItemText(0, label);
74+
}
75+
7276
virtual bool eventFilter(QObject* obj, QEvent* event) override{
7377
// cout << "eventFilter()" << endl;
7478
if (event->type() != QEvent::MouseButtonRelease){

Common/Qt/Options/CheckboxDropdownWidget.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ CheckboxDropdownCellWidget::~CheckboxDropdownCellWidget(){
2020
m_value.remove_listener(*this);
2121
}
2222
CheckboxDropdownCellWidget::CheckboxDropdownCellWidget(QWidget& parent, CheckboxDropdownBase& value)
23-
: CheckboxDropdown(&parent, QString::fromStdString(value.label()))
23+
: CheckboxDropdown(&parent, QString::fromStdString(value.current_label()))
2424
, ConfigWidget(value, *this)
2525
, m_value(value)
2626
{
@@ -39,10 +39,13 @@ CheckboxDropdownCellWidget::CheckboxDropdownCellWidget(QWidget& parent, Checkbox
3939
);
4040
}
4141

42+
CheckboxDropdownCellWidget::update_value();
43+
4244
m_value.add_listener(*this);
4345
}
4446

4547
void CheckboxDropdownCellWidget::update_value(){
48+
setLabel(QString::fromStdString(m_value.current_label()));
4649
size_t total_items = CheckboxDropdown::size();
4750
for (size_t index = 0; index < total_items; index++){
4851
(*this)[index]->setChecked(m_value[index]);

Common/Qt/Options/EditableTableWidget.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,16 @@ EditableTableWidget::EditableTableWidget(QWidget& parent, EditableTableOption& v
156156
}
157157

158158
void EditableTableWidget::update_value(){
159+
// Refresh the header in case that changed.
160+
QStringList header;
161+
for (const std::string& name : m_value.make_header()){
162+
header << QString::fromStdString(name);
163+
}
164+
header << "" << "" << "";
165+
m_table->setColumnCount(int(header.size()));
166+
m_table->setHorizontalHeaderLabels(header);
167+
168+
// Now update the table.
159169
std::vector<std::shared_ptr<EditableTableRow>> latest = m_value.current_refs();
160170
// cout << "latest.size() = " << latest.size() << endl;
161171

SerialPrograms/Source/Controllers/ControllerStateTable.cpp

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
#include "ControllerTypeStrings.h"
1010
#include "ControllerStateTable.h"
1111

12+
#include "NintendoSwitch/Controllers/Procon/NintendoSwitch_ProControllerTable.h"
13+
#include "NintendoSwitch/Controllers/Joycon/NintendoSwitch_JoyconTable.h"
14+
1215
namespace PokemonAutomation{
1316

1417

@@ -18,8 +21,18 @@ struct ControllerTypeEntry{
1821
std::vector<std::string> headers;
1922
};
2023

21-
std::map<ControllerClass, ControllerTypeEntry>& controller_map(){
22-
static std::map<ControllerClass, ControllerTypeEntry> map;
24+
25+
std::map<ControllerClass, ControllerTypeEntry> controller_map;
26+
27+
28+
const std::map<ControllerClass, ControllerTypeEntry>& make_controller_map(){
29+
NintendoSwitch::register_procon_tables();
30+
NintendoSwitch::register_joycon_tables();
31+
return controller_map;
32+
}
33+
34+
const std::map<ControllerClass, ControllerTypeEntry>& get_controller_map(){
35+
static const std::map<ControllerClass, ControllerTypeEntry>& map = make_controller_map();
2336
return map;
2437
}
2538

@@ -30,7 +43,7 @@ void ControllerCommandTable::register_controller_type(
3043
RowFactory factory,
3144
std::vector<std::string> headers
3245
){
33-
auto& map = controller_map();
46+
auto& map = controller_map;
3447
if (map.emplace(
3548
type, ControllerTypeEntry{factory, std::move(headers)}
3649
).second
@@ -46,7 +59,7 @@ void ControllerCommandTable::register_controller_type(
4659

4760

4861
std::vector<std::string> ControllerCommandTable::make_header() const{
49-
auto& map = controller_map();
62+
auto& map = get_controller_map();
5063
auto iter = map.find(m_type);
5164
if (iter != map.end()){
5265
return iter->second.headers;
@@ -58,7 +71,7 @@ std::vector<std::string> ControllerCommandTable::make_header() const{
5871
);
5972
}
6073
std::unique_ptr<EditableTableRow> ControllerCommandTable::make_row(){
61-
auto& map = controller_map();
74+
auto& map = get_controller_map();
6275
auto iter = map.find(m_type);
6376
if (iter != map.end()){
6477
return iter->second.factory(*this);
@@ -108,7 +121,7 @@ void ControllerCommandTables::on_config_value_changed(void* object){
108121
EnumDropdownDatabase<ControllerClass> ControllerCommandTables::make_database(
109122
const std::vector<ControllerClass>& controller_list
110123
){
111-
auto& map = controller_map();
124+
auto& map = get_controller_map();
112125
EnumDropdownDatabase<ControllerClass> ret;
113126
for (ControllerClass type : controller_list){
114127
if (map.find(type) == map.end()){

SerialPrograms/Source/Controllers/ControllerStateTable.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ class ControllerStateRow : public EditableTableRow{
2626

2727

2828

29+
2930
class ControllerCommandTable : public EditableTableOption{
3031
public:
3132
using RowFactory = std::unique_ptr<ControllerStateRow> (*)(ControllerCommandTable& parent);

SerialPrograms/Source/NintendoSwitch/Controllers/Joycon/NintendoSwitch_JoyconTable.cpp

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,6 @@
88
#include "Common/Cpp/Options/CheckboxDropdownDatabase.h"
99
#include "NintendoSwitch_JoyconTable.h"
1010

11-
// REMOVE
12-
#include "Common/Compiler.h"
13-
#include <iostream>
14-
using std::cout;
15-
using std::endl;
16-
1711
namespace PokemonAutomation{
1812
namespace NintendoSwitch{
1913

@@ -29,7 +23,7 @@ const CheckboxDropdownDatabase<Button>& LeftJoycon_Button_Database(){
2923
{Button::BUTTON_ZL, "ZL", "ZL"},
3024
{Button::BUTTON_MINUS, "-", "-"},
3125
{Button::BUTTON_LCLICK, "L-click", "L-click"},
32-
{Button::BUTTON_CAPTURE, "capture", "Capture"},
26+
{Button::BUTTON_CAPTURE, "Capture", "Capture"},
3327
{Button::BUTTON_LEFT_SR, "SR", "SR"},
3428
{Button::BUTTON_LEFT_SL, "SL", "SL"},
3529
};
@@ -45,16 +39,15 @@ const CheckboxDropdownDatabase<Button>& RightJoycon_Button_Database(){
4539
{Button::BUTTON_R, "RL", "RL"},
4640
{Button::BUTTON_PLUS, "+", "+"},
4741
{Button::BUTTON_RCLICK, "R-click", "R-click"},
48-
{Button::BUTTON_HOME, "home", "Home"},
42+
{Button::BUTTON_HOME, "Home", "Home"},
4943
{Button::BUTTON_RIGHT_SR, "SR", "SR"},
5044
{Button::BUTTON_RIGHT_SL, "SL", "SL"},
5145
{Button::BUTTON_C, "C", "C (Switch 2)"},
5246
};
5347
return database;
5448
}
5549

56-
PA_NO_INLINE int initialize_Joycons(){
57-
cout << "asdf" << endl;
50+
void register_joycon_tables(){
5851
ControllerCommandTable::register_controller_type(
5952
ControllerClass::NintendoSwitch_LeftJoycon,
6053
ControllerCommandTable::make_row<JoyconStateRow>,
@@ -75,9 +68,7 @@ PA_NO_INLINE int initialize_Joycons(){
7568
"Joystick (Y)",
7669
}
7770
);
78-
return 123;
7971
}
80-
int init_Joycons = initialize_Joycons();
8172

8273

8374

@@ -87,7 +78,7 @@ JoyconStateRow::JoyconStateRow(EditableTableOption& parent_table)
8778
: ControllerStateRow(parent_table)
8879
, DURATION(LockMode::LOCK_WHILE_RUNNING, "200 ms")
8980
, BUTTONS(
90-
"Buttons",
81+
"",
9182
static_cast<ControllerCommandTable&>(parent_table).type() == ControllerClass::NintendoSwitch_LeftJoycon
9283
? LeftJoycon_Button_Database()
9384
: RightJoycon_Button_Database(),

SerialPrograms/Source/NintendoSwitch/Controllers/Joycon/NintendoSwitch_JoyconTable.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ class JoyconStateRow : public ControllerStateRow{
3939
};
4040

4141

42-
extern int init_Joycons;
42+
void register_joycon_tables();
43+
4344

4445

4546
}

SerialPrograms/Source/NintendoSwitch/Controllers/Procon/NintendoSwitch_ProControllerTable.cpp

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ const CheckboxDropdownDatabase<Button>& ProController_Button_Database(){
3131
{Button::BUTTON_PLUS, "+", "+"},
3232
{Button::BUTTON_LCLICK, "L-click", "L-click"},
3333
{Button::BUTTON_RCLICK, "R-click", "R-click"},
34-
{Button::BUTTON_HOME, "home", "Home"},
35-
{Button::BUTTON_CAPTURE, "capture", "Capture"},
34+
{Button::BUTTON_HOME, "Home", "Home"},
35+
{Button::BUTTON_CAPTURE, "Capture", "Capture"},
3636
{Button::BUTTON_GR, "GR", "GR (Switch 2)"},
3737
{Button::BUTTON_GL, "GL", "GL (Switch 2)"},
3838
{Button::BUTTON_C, "C", "C (Switch 2)"},
@@ -54,7 +54,7 @@ const EnumDropdownDatabase<DpadPosition>& ProController_Dpad_Database(){
5454
return database;
5555
}
5656

57-
int initialize_ProController(){
57+
void register_procon_tables(){
5858
ControllerCommandTable::register_controller_type(
5959
ControllerClass::NintendoSwitch_ProController,
6060
ControllerCommandTable::make_row<ProControllerStateRow>,
@@ -68,17 +68,15 @@ int initialize_ProController(){
6868
"Right JS (Y)",
6969
}
7070
);
71-
return 0;
7271
}
73-
int init_ProController = initialize_ProController();
7472

7573

7674

7775
ProControllerStateRow::ProControllerStateRow(EditableTableOption& parent_table)
7876
: ControllerStateRow(parent_table)
7977
, DURATION(LockMode::LOCK_WHILE_RUNNING, "200 ms")
8078
, BUTTONS(
81-
"Buttons",
79+
"",
8280
ProController_Button_Database(),
8381
LockMode::UNLOCK_WHILE_RUNNING,
8482
BUTTON_NONE
@@ -141,7 +139,7 @@ JsonValue ProControllerStateRow::to_json() const{
141139
get_state(state);
142140

143141
JsonObject json = state.to_json();
144-
json["duration_in_ms"] = DURATION.to_json();
142+
json["ms"] = DURATION.to_json();
145143
return json;
146144
}
147145

0 commit comments

Comments
 (0)