setting&autostart&widgets (OptionField and Waveform) imp (#2286)

* test

* test

* format

* format

* tune order
This commit is contained in:
sommermorgentraum 2024-10-07 03:58:03 +08:00 committed by GitHub
parent 536d25db64
commit d4edb5f5f9
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 69 additions and 22 deletions

View file

@ -1697,10 +1697,12 @@ bool ImageOptionsField::on_touch(const TouchEvent event) {
OptionsField::OptionsField(
Point parent_pos,
size_t length,
options_t options)
options_t options,
bool centered)
: Widget{{parent_pos, {8 * (int)length, 16}}},
length_{length},
options_{std::move(options)} {
options_{std::move(options)},
centered_{centered} {
set_focusable(true);
}
@ -1792,8 +1794,19 @@ void OptionsField::paint(Painter& painter) {
std::string_view temp = selected_index_name();
if (temp.length() > length_)
temp = temp.substr(0, length_);
Point draw_pos = screen_pos();
if (centered_) {
// 8 is because big font width is 8px
// type is from: struct Point : constexpr int x() const
int text_width = temp.length() * 8;
int available_width = length_ * 8;
int x_offset = (available_width - text_width) / 2;
draw_pos = {draw_pos.x() + x_offset, draw_pos.y()};
}
painter.draw_string(
screen_pos(),
draw_pos,
paint_style,
temp);
}
@ -2622,6 +2635,8 @@ void Waveform::set_length(const uint32_t new_length) {
}
void Waveform::paint(Painter& painter) {
// previously it's upside down , low level is up and high level is down, which doesn't make sense,
// if that was made for a reason, feel free to revert.
size_t n;
Coord y, y_offset = screen_rect().location().y();
Coord prev_x = screen_rect().location().x(), prev_y;
@ -2642,7 +2657,7 @@ void Waveform::paint(Painter& painter) {
x = 0;
h--;
for (n = 0; n < length_; n++) {
y = *(data_start++) ? h : 0;
y = *(data_start++) ? 0 : h;
if (n) {
if (y != prev_y)
@ -2658,9 +2673,10 @@ void Waveform::paint(Painter& painter) {
// Analog waveform: each value is a point's Y coordinate
x = prev_x + x_inc;
h /= 2;
prev_y = y_offset + h - (*(data_start++) * y_scale);
prev_y = y_offset + h + (*(data_start++) * y_scale);
for (n = 1; n < length_; n++) {
y = y_offset + h - (*(data_start++) * y_scale);
y = y_offset + h + (*(data_start++) * y_scale);
display.draw_line({prev_x, prev_y}, {(Coord)x, y}, color_);
prev_x = x;

View file

@ -680,7 +680,7 @@ class OptionsField : public Widget {
std::function<void(size_t, value_t)> on_change{};
std::function<void(void)> on_show_options{};
OptionsField(Point parent_pos, size_t length, options_t options);
OptionsField(Point parent_pos, size_t length, options_t options, bool centered = false);
options_t& options() { return options_; }
const options_t& options() const { return options_; }
@ -708,6 +708,7 @@ class OptionsField : public Widget {
const size_t length_;
options_t options_;
size_t selected_index_{0};
bool centered_{false}; // e.g.: length as screen_width/8, x position as 0, it will be centered in x axis
};
// A TextEdit is bound to a string reference and allows the string
@ -1036,4 +1037,4 @@ class OptionTabView : public View {
} /* namespace ui */
#endif /*__UI_WIDGET_H__*/
#endif /*__UI_WIDGET_H__*/