Modules/Sketch2D/bench/BenchInput.cpp
agent b552d7c324 Port agent-work onto master
Brings the agent-work branch tip onto master, keeping master's architecture
and putting every agent-work feature on top of it: the window/GpuDevice/
RenderTarget split, explicit per-instance profilers instead of a global, the
input layer with scroll sources and ends, the Widgets core and components
(shared SelectionModel/Commands lists, popup stack, titled panels, prompt
dialogs, file browser, kanban, outline tree, markdown, docking), the gallery
on the new UI, the Objects explorer and interpreter fixes, the Taskboard, the
Sketch2D render path and its tests and bench, and the headless Wayland
runner the UI tests drive.

No MODULES_BUILD_* switches are introduced: platform differences are plain
if (ANDROID) / if (NOT ANDROID).
2026-11-02 11:00:51 +03:00

104 lines
3.4 KiB
C++

#include "BenchInput.hpp"
#include <algorithm>
#include <cmath>
namespace tp::sketch2d::bench {
namespace {
constexpr double kPi = 3.14159265358979;
class Random {
public:
explicit Random(uint32_t seed) :
mState(seed ? seed : 1) {}
double next() {
mState ^= mState << 13;
mState ^= mState >> 17;
mState ^= mState << 5;
return (double) mState / 4294967296.0;
}
double between(double low, double high) { return low + (high - low) * next(); }
private:
uint32_t mState;
};
struct Pen {
Vec2F position;
double heading = 0;
double turnRate = 0;
};
PointerSample sampleAt(bool pen, double strokeFraction, double timeMs, double heading) {
PointerSample sample;
if (!pen) return sample;
sample.device = PointerDevice::Stylus;
const double rise = std::min(1.0, strokeFraction * 6.0);
const double fall = std::min(1.0, (1.0 - strokeFraction) * 5.0);
sample.pressure = (halnf) std::clamp(0.25 + 0.7 * rise * fall, 0.05, 1.0);
sample.tilt = { (halnf) (0.3 * std::cos(heading)), (halnf) (0.3 * std::sin(heading)) };
sample.timeMs = (uint32_t) timeMs;
return sample;
}
void keepInside(Pen& pen, const RectF& area) {
const Vec2F center = area.center();
const Vec2F half = area.size * 0.5f;
const Vec2F offset = pen.position - center;
if (std::fabs(offset.x) < half.x * 0.85f && std::fabs(offset.y) < half.y * 0.85f) return;
const double towardCenter = std::atan2(-offset.y, -offset.x);
double difference = towardCenter - pen.heading;
while (difference > kPi)
difference -= 2 * kPi;
while (difference < -kPi)
difference += 2 * kPi;
pen.heading += difference * 0.2;
pen.position.x = std::clamp(pen.position.x, area.pos.x, area.pos.x + area.size.x);
pen.position.y = std::clamp(pen.position.y, area.pos.y, area.pos.y + area.size.y);
}
void push(InputRecording& recording, double timeMs, InputEvent event) {
recording.events.push_back({ timeMs, std::move(event) });
}
}
InputRecording fastScribble(const ScribbleOptions& options) {
InputRecording recording;
Random random(options.seed);
Pen pen;
pen.position = options.area.center();
pen.heading = random.between(0, 2 * kPi);
double time = 0;
while (time < options.durationMs) {
const double strokeEnd = std::min(time + options.strokeMs, options.durationMs);
const double strokeLength = strokeEnd - time;
bool down = false;
for (double now = time; now <= strokeEnd; now += options.sampleIntervalMs) {
const double fraction = strokeLength > 0 ? (now - time) / strokeLength : 1.0;
if (random.next() < 0.04) pen.turnRate = random.between(-0.35, 0.35);
pen.heading += pen.turnRate + random.between(-0.05, 0.05);
const double step = options.speedPixelsPerSecond * options.sampleIntervalMs / 1000.0;
pen.position += Vec2F((halnf) (std::cos(pen.heading) * step), (halnf) (std::sin(pen.heading) * step));
keepInside(pen, options.area);
push(recording, now, PointerSampled{ sampleAt(options.pen, fraction, now, pen.heading) });
push(recording, now, PointerMoved{ pen.position });
if (!down) {
push(recording, now, PointerButtonChanged{ PointerButton::Left, true });
down = true;
}
}
push(recording, strokeEnd, PointerButtonChanged{ PointerButton::Left, false });
if (options.pen) push(recording, strokeEnd, PointerSampled{ PointerSample{} });
time = strokeEnd + options.gapMs;
}
push(recording, time, PointerLeft{});
return recording;
}
}