Skip to content

Commit 2b24ddb

Browse files
committed
Missing files from colors change.
1 parent 91b6a6d commit 2b24ddb

File tree

8 files changed

+294
-10
lines changed

8 files changed

+294
-10
lines changed

Common/Cpp/ListenerSet.h

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <map>
1111
#include <atomic>
1212
#include <mutex>
13+
#include "Common/Cpp/Concurrency/SpinLock.h"
1314

1415
//#include <iostream>
1516
//using std::cout;
@@ -30,12 +31,14 @@ class ListenerSet{
3031
}
3132

3233
void add(ListenerType& listener){
33-
std::lock_guard<std::mutex> lg(m_lock);
34+
WriteSpinLock lg(m_lock);
35+
// std::lock_guard<std::mutex> lg(m_lock);
3436
m_listeners[&listener]++;
3537
m_count.store(m_listeners.size(), std::memory_order_relaxed);
3638
}
3739
void remove(ListenerType& listener){
38-
std::lock_guard<std::mutex> lg(m_lock);
40+
WriteSpinLock lg(m_lock);
41+
// std::lock_guard<std::mutex> lg(m_lock);
3942
auto iter = m_listeners.find(&listener);
4043
if (iter == m_listeners.end()){
4144
return;
@@ -51,7 +54,8 @@ class ListenerSet{
5154
if (empty()){
5255
return;
5356
}
54-
std::lock_guard<std::mutex> lg(m_lock);
57+
ReadSpinLock lg(m_lock);
58+
// std::lock_guard<std::mutex> lg(m_lock);
5559
for (auto& item : m_listeners){
5660
lambda(*item.first);
5761
}
@@ -61,7 +65,8 @@ class ListenerSet{
6165
if (empty()){
6266
return;
6367
}
64-
std::lock_guard<std::mutex> lg(m_lock);
68+
ReadSpinLock lg(m_lock);
69+
// std::lock_guard<std::mutex> lg(m_lock);
6570
for (auto& item : m_listeners){
6671
ListenerType& listener = *item.first;
6772
size_t count = item.second;
@@ -76,7 +81,8 @@ class ListenerSet{
7681
if (empty()){
7782
return;
7883
}
79-
std::lock_guard<std::mutex> lg(m_lock);
84+
ReadSpinLock lg(m_lock);
85+
// std::lock_guard<std::mutex> lg(m_lock);
8086
for (auto& item : m_listeners){
8187
(item.first->*function)(std::forward<Args>(args)...);
8288
}
@@ -86,7 +92,8 @@ class ListenerSet{
8692
if (empty()){
8793
return;
8894
}
89-
std::lock_guard<std::mutex> lg(m_lock);
95+
ReadSpinLock lg(m_lock);
96+
// std::lock_guard<std::mutex> lg(m_lock);
9097
for (auto& item : m_listeners){
9198
ListenerType& listener = *item.first;
9299
size_t count = item.second;
@@ -101,7 +108,8 @@ class ListenerSet{
101108
// skip the lock when there are no listeners.
102109
std::atomic<size_t> m_count;
103110

104-
mutable std::mutex m_lock;
111+
mutable SpinLock m_lock;
112+
// mutable std::mutex m_lock;
105113
std::map<ListenerType*, size_t> m_listeners;
106114
};
107115

Common/Cpp/Options/ColorOption.cpp

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
/* Color Option
2+
*
3+
* From: https://github.com/PokemonAutomation/
4+
*
5+
*/
6+
7+
#include "Common/Cpp/Json/JsonValue.h"
8+
#include "ColorOption.h"
9+
10+
namespace PokemonAutomation{
11+
12+
13+
ColorCell::ColorCell(const ColorCell& x)
14+
: ConfigOption(x)
15+
, m_default_value(x.m_default_value)
16+
, m_current_value(x)
17+
{}
18+
ColorCell::ColorCell(
19+
LockMode lock_while_running,
20+
bool allow_alpha,
21+
uint32_t default_value, uint32_t current_value
22+
)
23+
: ConfigOption(lock_while_running)
24+
, m_allow_alpha(allow_alpha)
25+
, m_default_value(default_value)
26+
, m_current_value(current_value)
27+
{
28+
if (!m_allow_alpha){
29+
m_default_value &= 0x00ffffff;
30+
m_current_value &= 0x00ffffff;
31+
}
32+
}
33+
uint32_t ColorCell::default_value() const{
34+
return m_default_value;
35+
}
36+
std::string ColorCell::to_str() const{
37+
static const char HEX_DIGITS[] = "0123456789ABCDEF";
38+
std::string str(6, '0');
39+
uint32_t x = *this;
40+
str[0] = HEX_DIGITS[(x >> 20) & 0xf];
41+
str[1] = HEX_DIGITS[(x >> 16) & 0xf];
42+
str[2] = HEX_DIGITS[(x >> 12) & 0xf];
43+
str[3] = HEX_DIGITS[(x >> 8) & 0xf];
44+
str[4] = HEX_DIGITS[(x >> 4) & 0xf];
45+
str[5] = HEX_DIGITS[(x >> 0) & 0xf];
46+
return str;
47+
}
48+
ColorCell::operator uint32_t() const{
49+
return m_current_value.load(std::memory_order_relaxed);
50+
}
51+
void ColorCell::set(uint32_t x){
52+
if (!m_allow_alpha){
53+
x &= 0x00ffffff;
54+
}
55+
if (x != m_current_value.exchange(x, std::memory_order_relaxed)){
56+
report_value_changed(this);
57+
}
58+
}
59+
void ColorCell::set(const std::string& str){
60+
size_t max_digits = m_allow_alpha ? 8 : 6;
61+
62+
uint32_t x = 0;
63+
size_t count = 0;
64+
for (char ch : str){
65+
if (count >= max_digits){
66+
break;
67+
}
68+
switch (ch){
69+
case '\0':
70+
break;
71+
case '-':
72+
case ':':
73+
case ' ':
74+
continue;
75+
}
76+
77+
if ('0' <= ch && ch <= '9'){
78+
x <<= 4;
79+
x |= ch - '0';
80+
count++;
81+
continue;
82+
}
83+
if ('a' <= ch && ch <= 'f'){
84+
x <<= 4;
85+
x |= ch - 'a' + 10;
86+
count++;
87+
continue;
88+
}
89+
if ('A' <= ch && ch <= 'F'){
90+
x <<= 4;
91+
x |= ch - 'A' + 10;
92+
count++;
93+
continue;
94+
}
95+
96+
break;
97+
}
98+
if (!m_allow_alpha){
99+
x &= 0x00ffffff;
100+
}
101+
set(x);
102+
}
103+
104+
void ColorCell::load_json(const JsonValue& json){
105+
uint32_t value;
106+
if (!json.read_integer(value)){
107+
return;
108+
}
109+
set(value);
110+
}
111+
JsonValue ColorCell::to_json() const{
112+
return (uint32_t)*this;
113+
}
114+
void ColorCell::restore_defaults(){
115+
set(m_default_value);
116+
}
117+
118+
119+
120+
}

Common/Cpp/Options/ColorOption.h

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/* Color Option
2+
*
3+
* From: https://github.com/PokemonAutomation/
4+
*
5+
*/
6+
7+
#ifndef PokemonAutomation_ColorOption_H
8+
#define PokemonAutomation_ColorOption_H
9+
10+
#include <atomic>
11+
#include "Common/Cpp/Options/ConfigOption.h"
12+
13+
namespace PokemonAutomation{
14+
15+
16+
17+
class ColorCell : public ConfigOption{
18+
public:
19+
ColorCell(const ColorCell& x);
20+
ColorCell(
21+
LockMode lock_while_running,
22+
bool allow_alpha,
23+
uint32_t default_value, uint32_t current_value
24+
);
25+
26+
uint32_t default_value() const;
27+
std::string to_str() const;
28+
29+
operator uint32_t() const;
30+
void set(uint32_t x);
31+
void set(const std::string& str);
32+
33+
virtual void load_json(const JsonValue& json) override;
34+
virtual JsonValue to_json() const override;
35+
virtual void restore_defaults() override;
36+
37+
virtual ConfigWidget* make_QtWidget(QWidget& parent) override;
38+
39+
40+
private:
41+
bool m_allow_alpha;
42+
uint32_t m_default_value;
43+
std::atomic<uint32_t> m_current_value;
44+
};
45+
46+
47+
48+
}
49+
#endif

Common/Cpp/Options/FloatingPointOption.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,7 @@ struct FloatingPointCell::Data{
4141
FloatingPointCell::~FloatingPointCell() = default;
4242
FloatingPointCell::FloatingPointCell(const FloatingPointCell& x)
4343
: ConfigOption(x)
44-
, m_data(CONSTRUCT_TOKEN, x.min_value(), x.max_value(), x.default_value(), x.default_value())
45-
, m_label(x.m_label)
44+
, m_data(CONSTRUCT_TOKEN, x.min_value(), x.max_value(), x.default_value(), x)
4645
{}
4746
FloatingPointCell::FloatingPointCell(
4847
LockMode lock_while_running,

Common/Cpp/Options/FloatingPointOption.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ class FloatingPointCell : public ConfigOption{
5151
private:
5252
struct Data;
5353
Pimpl<Data> m_data;
54-
const std::string m_label;
5554
};
5655

5756

Common/Qt/Options/ColorWidget.cpp

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/* Color Widget
2+
*
3+
* From: https://github.com/PokemonAutomation/
4+
*
5+
*/
6+
7+
#include <QLabel>
8+
#include <QLineEdit>
9+
#include "QHBoxLayout"
10+
#include "ColorWidget.h"
11+
12+
namespace PokemonAutomation{
13+
14+
15+
ConfigWidget* ColorCell::make_QtWidget(QWidget& parent){
16+
return new ColorCellWidget(parent, *this);
17+
}
18+
19+
20+
ColorCellWidget::~ColorCellWidget(){
21+
m_value.remove_listener(*this);
22+
}
23+
ColorCellWidget::ColorCellWidget(QWidget& parent, ColorCell& value)
24+
: QWidget(&parent)
25+
, ConfigWidget(value, *this)
26+
, m_value(value)
27+
{
28+
QHBoxLayout* layout = new QHBoxLayout(this);
29+
layout->setContentsMargins(0, 0, 5, 0);
30+
31+
QString str = QString::fromStdString(value.to_str());
32+
33+
m_line_edit = new QLineEdit(str, this);
34+
m_line_edit->setFixedWidth(60);
35+
layout->addWidget(m_line_edit);
36+
37+
m_box = new QLabel(this);
38+
m_box->setTextFormat(Qt::RichText);
39+
layout->addWidget(m_box);
40+
41+
ColorCellWidget::update_value();
42+
43+
connect(
44+
m_line_edit, &QLineEdit::editingFinished,
45+
this, [this](){
46+
m_value.set(m_line_edit->text().toStdString());
47+
update_value();
48+
}
49+
);
50+
51+
m_value.add_listener(*this);
52+
}
53+
void ColorCellWidget::update_value(){
54+
QString str = QString::fromStdString(m_value.to_str());
55+
m_line_edit->setText(str);
56+
m_box->setText("<font color=\"#" + str + "\">&#x2b24;</font>");
57+
}
58+
void ColorCellWidget::value_changed(void* object){
59+
QMetaObject::invokeMethod(this, [this]{
60+
update_value();
61+
}, Qt::QueuedConnection);
62+
}
63+
64+
65+
66+
67+
}

Common/Qt/Options/ColorWidget.h

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/* Color Widget
2+
*
3+
* From: https://github.com/PokemonAutomation/
4+
*
5+
*/
6+
7+
#ifndef PokemonAutomation_ColorWidget_H
8+
#define PokemonAutomation_ColorWidget_H
9+
10+
#include <QWidget>
11+
#include "Common/Cpp/Options/ColorOption.h"
12+
#include "ConfigWidget.h"
13+
14+
class QLabel;
15+
class QLineEdit;
16+
17+
namespace PokemonAutomation{
18+
19+
20+
21+
class ColorCellWidget : public QWidget, public ConfigWidget{
22+
public:
23+
~ColorCellWidget();
24+
ColorCellWidget(QWidget& parent, ColorCell& value);
25+
26+
virtual void update_value() override;
27+
virtual void value_changed(void* object) override;
28+
29+
30+
private:
31+
ColorCell& m_value;
32+
QLineEdit* m_line_edit;
33+
QLabel* m_box;
34+
};
35+
36+
37+
38+
39+
}
40+
#endif

Common/Qt/Options/EditableTableWidget.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,8 @@ void EditableTableWidget::update_value(){
217217
QWidget* cell_widget = new QWidget(this);
218218
QVBoxLayout* layout = new QVBoxLayout(cell_widget);
219219
layout->setContentsMargins(0, 0, 0, 0);
220+
// cell_widget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
221+
widget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
220222
layout->addWidget(widget);
221223
// cell_widgets.emplace_back(widget);
222224
m_table->setCellWidget((int)index_new, c, cell_widget);

0 commit comments

Comments
 (0)