Skip to content

Commit

Permalink
karm-gfx: Renamed the plot familly of functions.
Browse files Browse the repository at this point in the history
  • Loading branch information
sleepy-monax committed Mar 3, 2024
1 parent a888d4f commit 8b0b571
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 82 deletions.
2 changes: 1 addition & 1 deletion src/apps/hideo-colors/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ struct HsvPicker : public Ui::View<HsvPicker> {

for (isize y = 0; y < bound().height; y++) {
for (isize x = 0; x < bound().width; x++) {
g.debugPlot({bound().x + x, bound().y + y}, sampleColor({x, y}));
g.plot(Math::Edgei{bound().x + x, bound().y + y}, sampleColor({x, y}));
}
}

Expand Down
62 changes: 10 additions & 52 deletions src/libs/karm-gfx/context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -372,14 +372,14 @@ void Context::fill(Math::Vec2f baseline, Str str) {

/* --- Debug ---------------------------------------------------------------- */

void Context::debugPlot(Math::Vec2i point, Color color) {
void Context::plot(Math::Vec2i point, Color color) {
point = applyOrigin(point);
if (clip().contains(point)) {
mutPixels().blend(point, color);
}
}

void Context::debugLine(Math::Edgei edge, Color color) {
void Context::plot(Math::Edgei edge, Color color) {
isize dx = Math::abs(edge.ex - edge.sx);
isize sx = edge.sx < edge.ex ? 1 : -1;

Expand All @@ -389,7 +389,7 @@ void Context::debugLine(Math::Edgei edge, Color color) {
isize err = dx + dy, e2;

for (;;) {
debugPlot({edge.sx, edge.sy}, color);
plot(edge.start, color);
if (edge.sx == edge.ex and edge.sy == edge.ey)
break;
e2 = 2 * err;
Expand All @@ -404,59 +404,17 @@ void Context::debugLine(Math::Edgei edge, Color color) {
}
}

void Context::debugRect(Math::Recti rect, Color color) {
void Context::plot(Math::Recti rect, Color color) {
rect = {rect.xy, rect.wh - 1};
debugLine({rect.topStart(), rect.topEnd()}, color);
debugLine({rect.topEnd(), rect.bottomEnd()}, color);
debugLine({rect.bottomEnd(), rect.bottomStart()}, color);
debugLine({rect.bottomStart(), rect.topStart()}, color);
plot(Math::Edgei{rect.topStart(), rect.topEnd()}, color);
plot(Math::Edgei{rect.topEnd(), rect.bottomEnd()}, color);
plot(Math::Edgei{rect.bottomEnd(), rect.bottomStart()}, color);
plot(Math::Edgei{rect.bottomStart(), rect.topStart()}, color);
}

void Context::debugArrow(Math::Vec2i from, Math::Vec2i to, Color color) {
isize const SIZE = 16;

Math::Vec2i dir = to - from;
Math::Vec2i perp = {-dir.y, dir.x};

f64 len = dir.len();
f64 scale = SIZE / len;

Math::Vec2i p1 = to - dir * scale;
Math::Vec2i p2 = p1 + perp * scale;
Math::Vec2i p3 = p1 - perp * scale;

debugLine({from, to}, color);
debugLine({to, p2}, color);
debugLine({to, p3}, color);
}

void Context::debugDoubleArrow(Math::Vec2i from, Math::Vec2i to, Color color) {
isize const SIZE = 8;

Math::Vec2f dir = (to - from).cast<f64>();
Math::Vec2f perp = {-dir.y, dir.x};

f64 len = dir.len();
f64 scale = SIZE / len;

Math::Vec2i p1 = (to - dir * scale).cast<isize>();
Math::Vec2i p2 = (p1 + perp * scale).cast<isize>();
Math::Vec2i p3 = (p1 - perp * scale).cast<isize>();

Math::Vec2i p4 = (from + dir * scale).cast<isize>();
Math::Vec2i p5 = (p4 + perp * scale).cast<isize>();
Math::Vec2i p6 = (p4 - perp * scale).cast<isize>();

debugLine({from, to}, color);
debugLine({to, p2}, color);
debugLine({to, p3}, color);
debugLine({from, p5}, color);
debugLine({from, p6}, color);
}

void Context::debugTrace(Gfx::Color color) {
void Context::plot(Gfx::Color color) {
for (auto edge : _rast.shape()) {
debugLine(edge.cast<isize>(), color);
plot(edge.cast<isize>(), color);
}
}

Expand Down
14 changes: 4 additions & 10 deletions src/libs/karm-gfx/context.h
Original file line number Diff line number Diff line change
Expand Up @@ -250,22 +250,16 @@ struct Context {
// non-antialiased shapes and lines.

// Plot a point.
void debugPlot(Math::Vec2i point, Color color);
void plot(Math::Vec2i point, Color color);

// Draw a line.
void debugLine(Math::Edgei edge, Color color);
void plot(Math::Edgei edge, Color color);

// Draw a rectangle.
void debugRect(Math::Recti rect, Color color);

// Draw an arrow.
void debugArrow(Math::Vec2i from, Math::Vec2i to, Color color);

// Draw a double arrow.
void debugDoubleArrow(Math::Vec2i from, Math::Vec2i to, Color color);
void plot(Math::Recti rect, Color color);

// Draw the current path as a polygon.
void debugTrace(Gfx::Color color);
void plot(Gfx::Color color);

/* --- Paths ------------------------------------------------------------ */

Expand Down
6 changes: 3 additions & 3 deletions src/libs/karm-ui/host.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,11 @@ struct PerfGraph {
g.strokeStyle(Gfx::stroke(Gfx::WHITE.withOpacity(0.5)).withAlign(Gfx::INSIDE_ALIGN));
g.stroke(Math::Edgei{0, (isize)(FRAME_TIME * 1000 * 2), 256, (isize)(FRAME_TIME * 1000 * 2)});

for (usize i = 0; i < 256; ++i) {
for (isize i = 0; i < 256; ++i) {
auto e = _records[(_index + i) % 256];

g.debugRect(
{(isize)i, 0, 1, (isize)e.duration().toMSecs() * 2},
g.plot(
Math::Recti{i, 0, 1, (isize)e.duration().toMSecs() * 2},
e.color());
}

Expand Down
6 changes: 3 additions & 3 deletions src/libs/karm-ui/input.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -299,13 +299,13 @@ struct Input : public View<Input> {
// g.fill(baseline, _text);

if (debugShowLayoutBounds) {
g.debugLine(
{
g.plot(
Math::Edgei{
bound().topStart() + m.baseline.cast<isize>(),
bound().topEnd() + m.baseline.cast<isize>(),
},
Gfx::PINK);
g.debugRect(bound(), Gfx::CYAN);
g.plot(bound(), Gfx::CYAN);
}

g.restore();
Expand Down
13 changes: 6 additions & 7 deletions src/libs/karm-ui/layout.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ struct Empty : public View<Empty> {
void paint(Gfx::Context &g, Math::Recti) override {
if (debugShowEmptyBounds) {
auto b = bound();
g.debugRect(b, Gfx::WHITE.withOpacity(0.2));
g.debugLine({b.topStart(), b.bottomEnd()}, Gfx::WHITE.withOpacity(0.2));
g.debugLine({b.topEnd(), b.bottomStart()}, Gfx::WHITE.withOpacity(0.2));
g.plot(b, Gfx::WHITE.withOpacity(0.2));
g.plot(Math::Edgei{b.topStart(), b.bottomEnd()}, Gfx::WHITE.withOpacity(0.2));
g.plot(Math::Edgei{b.topEnd(), b.bottomStart()}, Gfx::WHITE.withOpacity(0.2));
}
}
};
Expand Down Expand Up @@ -239,7 +239,7 @@ struct Spacing : public ProxyNode<Spacing> {
void paint(Gfx::Context &g, Math::Recti r) override {
child().paint(g, r);
if (debugShowLayoutBounds) {
g.debugRect(child().bound(), Gfx::LIME);
g.plot(child().bound(), Gfx::LIME);
}
}

Expand Down Expand Up @@ -275,9 +275,8 @@ struct AspectRatio : public ProxyNode<AspectRatio> {

void paint(Gfx::Context &g, Math::Recti r) override {
child().paint(g, r);
if (debugShowLayoutBounds) {
g.debugRect(child().bound(), Gfx::INDIGO);
}
if (debugShowLayoutBounds)
g.plot(child().bound(), Gfx::INDIGO);
}

Math::Vec2i size(Math::Vec2i s, Layout::Hint) override {
Expand Down
4 changes: 2 additions & 2 deletions src/libs/karm-ui/scroll.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@ struct Scroll : public ProxyNode<Scroll> {
child().paint(g, r);

if (debugShowScrollBounds)
g.debugRect(child().bound(), Gfx::PINK);
g.plot(child().bound(), Gfx::PINK);

g.restore();

if (debugShowScrollBounds)
g.debugRect(_bound, Gfx::CYAN);
g.plot(_bound, Gfx::CYAN);
}

void event(Sys::Event &e) override {
Expand Down
7 changes: 3 additions & 4 deletions src/libs/karm-ui/view.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -229,12 +229,11 @@ struct Text : public View<Text> {
void paint(Gfx::Context &g, Math::Recti) override {
g.save();
g.clip(bound());
// g.debugRect(bound(), Gfx::PINK);
g.origin(bound().xy);
_text.paint(g);
g.restore();
if (debugShowLayoutBounds)
g.debugRect(bound(), Gfx::CYAN);
g.plot(bound(), Gfx::CYAN);
}

void layout(Math::Recti bound) override {
Expand Down Expand Up @@ -300,7 +299,7 @@ struct Icon : public View<Icon> {
g.fillStyle(_color.unwrap());
g.fill(bound().topStart(), _icon);
if (debugShowLayoutBounds)
g.debugRect(bound(), Gfx::CYAN);
g.plot(bound(), Gfx::CYAN);
g.restore();
}

Expand Down Expand Up @@ -338,7 +337,7 @@ struct Image : public View<Image> {
}

if (debugShowLayoutBounds)
g.debugRect(bound(), Gfx::CYAN);
g.plot(bound(), Gfx::CYAN);
}

Math::Vec2i size(Math::Vec2i, Layout::Hint) override {
Expand Down

0 comments on commit 8b0b571

Please sign in to comment.