2020-01-18 11:38:21 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
2022-02-10 22:28:48 +03:00
|
|
|
* Copyright (c) 2022, the SerenityOS developers.
|
2020-01-18 11:38:21 +03:00
|
|
|
*
|
2021-04-22 11:24:48 +03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 11:38:21 +03:00
|
|
|
*/
|
|
|
|
|
2019-09-04 21:18:41 +03:00
|
|
|
#include "SampleWidget.h"
|
2020-02-06 22:33:02 +03:00
|
|
|
#include <LibGUI/Painter.h>
|
2019-09-04 21:18:41 +03:00
|
|
|
|
2022-01-10 00:46:06 +03:00
|
|
|
SampleWidget::SampleWidget()
|
|
|
|
{
|
|
|
|
MUST(set_render_sample_count(512));
|
|
|
|
}
|
|
|
|
|
2022-05-06 23:14:16 +03:00
|
|
|
void SampleWidget::render(GUI::PaintEvent& event, FixedArray<float> const& samples)
|
2019-09-04 21:18:41 +03:00
|
|
|
{
|
2020-02-02 17:07:41 +03:00
|
|
|
GUI::Frame::paint_event(event);
|
|
|
|
GUI::Painter painter(*this);
|
2019-09-04 21:18:41 +03:00
|
|
|
|
2019-10-29 19:31:49 +03:00
|
|
|
painter.add_clip_rect(event.rect());
|
2019-09-04 21:18:41 +03:00
|
|
|
painter.fill_rect(frame_inner_rect(), Color::Black);
|
|
|
|
|
2019-10-29 19:31:49 +03:00
|
|
|
int x_offset = frame_inner_rect().x();
|
|
|
|
int x = x_offset;
|
|
|
|
int y_offset = frame_inner_rect().center().y();
|
|
|
|
|
2022-01-10 00:46:06 +03:00
|
|
|
if (samples.size() > 0) {
|
|
|
|
float samples_per_pixel = samples.size() / static_cast<float>(frame_inner_rect().width());
|
|
|
|
float sample_index = 0;
|
2019-10-29 19:31:49 +03:00
|
|
|
|
2022-01-10 00:46:06 +03:00
|
|
|
while (sample_index < samples.size()) {
|
|
|
|
float sample = AK::abs(samples[sample_index]);
|
|
|
|
for (size_t i = 1; i < static_cast<size_t>(samples_per_pixel + 0.5f); i++)
|
|
|
|
sample = max(sample, AK::abs(samples[sample_index]));
|
2019-10-29 19:31:49 +03:00
|
|
|
|
2022-01-10 00:46:06 +03:00
|
|
|
Gfx::IntPoint min_point = { x, y_offset + static_cast<int>(-sample * frame_inner_rect().height() / 2) };
|
|
|
|
Gfx::IntPoint max_point = { x, y_offset + static_cast<int>(sample * frame_inner_rect().height() / 2) };
|
|
|
|
painter.draw_line(min_point, max_point, Color::Green);
|
|
|
|
sample_index += samples_per_pixel;
|
|
|
|
x++;
|
2019-10-29 19:31:49 +03:00
|
|
|
}
|
2019-11-04 21:45:19 +03:00
|
|
|
} else {
|
|
|
|
painter.draw_line({ x, y_offset }, { frame_inner_rect().width(), y_offset }, Color::Green);
|
2019-09-04 21:18:41 +03:00
|
|
|
}
|
|
|
|
}
|