Skip to content

Commit

Permalink
karm-scene: Added the ability to change PaperStock per page and hidin…
Browse files Browse the repository at this point in the history
…g background.
  • Loading branch information
sleepy-monax committed Dec 12, 2024
1 parent fb33497 commit a87c761
Show file tree
Hide file tree
Showing 10 changed files with 43 additions and 28 deletions.
10 changes: 7 additions & 3 deletions src/libs/karm-scene/base.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@

#include <karm-gfx/canvas.h>
#include <karm-io/emit.h>
#include <karm-print/base.h>
#include <karm-print/printer.h>

namespace Karm::Scene {

struct PaintOptions {
bool showBackgroundGraphics = true;
};

struct Node {
isize zIndex = 0;

Expand All @@ -17,9 +21,9 @@ struct Node {
/// The bounding rectangle of the node
virtual Math::Rectf bound() { return {}; }

virtual void paint(Gfx::Canvas &, Math::Rectf = Math::Rectf::MAX) {}
virtual void paint(Gfx::Canvas &, Math::Rectf = Math::Rectf::MAX, PaintOptions = {}) {}

virtual void print(Print::Printer &) {}
virtual void print(Print::Printer &, PaintOptions = {.showBackgroundGraphics = false}) {}

virtual void repr(Io::Emit &e) const {
e("(node z:{})", zIndex);
Expand Down
14 changes: 8 additions & 6 deletions src/libs/karm-scene/box.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,17 @@ struct Box : public Node {
return _bound;
}

void paint(Gfx::Canvas &ctx, Math::Rectf r) override {
void paint(Gfx::Canvas &ctx, Math::Rectf r, PaintOptions o) override {
if (not r.colide(bound()))
return;

for (auto &background : _backgrounds) {
ctx.beginPath();
auto radii = _borders.radii.reduceOverlap(_bound.size());
ctx.rect(_bound, radii);
ctx.fill(background);
if (o.showBackgroundGraphics) {
for (auto &background : _backgrounds) {
ctx.beginPath();
auto radii = _borders.radii.reduceOverlap(_bound.size());
ctx.rect(_bound, radii);
ctx.fill(background);
}
}

_borders.paint(ctx, _bound);
Expand Down
2 changes: 1 addition & 1 deletion src/libs/karm-scene/image.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ struct Image : public Node {
return _bound;
}

void paint(Gfx::Canvas &ctx, Math::Rectf r) override {
void paint(Gfx::Canvas &ctx, Math::Rectf r, PaintOptions) override {
if (not r.colide(bound()))
return;

Expand Down
13 changes: 7 additions & 6 deletions src/libs/karm-scene/page.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,18 @@
namespace Karm::Scene {

struct Page : public Stack {
Math::Vec2i _size;
Print::PaperStock _paper;

Page(Math::Vec2i size, Opt<Math::Trans2f> transform = NONE) : Stack(transform), _size(size) {}
Page(Print::PaperStock paper, Opt<Math::Trans2f> transform = NONE)
: Stack(transform), _paper(paper) {}

void print(Print::Printer &doc) override {
Stack::print(doc);
paint(doc.beginPage(), _size.cast<f64>());
void print(Print::Printer &doc, PaintOptions o) override {
Stack::print(doc, o);
paint(doc.beginPage(_paper), _paper.size().cast<f64>(), o);
}

Math::Rectf bound() override {
return _size.cast<f64>();
return _paper.size().cast<f64>();
}

void repr(Io::Emit &e) const override {
Expand Down
2 changes: 1 addition & 1 deletion src/libs/karm-scene/shape.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ struct Shape : public Node {
return _path.bound();
}

void paint(Gfx::Canvas &g, Math::Rectf r) override {
void paint(Gfx::Canvas &g, Math::Rectf r, PaintOptions) override {
if (not bound().colide(r))
return;

Expand Down
8 changes: 4 additions & 4 deletions src/libs/karm-scene/stack.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ struct Stack : public Node {
return rect;
}

void paint(Gfx::Canvas &g, Math::Rectf r) override {
void paint(Gfx::Canvas &g, Math::Rectf r, PaintOptions o) override {
if (not bound().colide(r))
return;

Expand All @@ -40,15 +40,15 @@ struct Stack : public Node {
}

for (auto &child : _children)
child->paint(g, r);
child->paint(g, r, o);

if (_transform)
g.pop();
}

void print(Print::Printer &p) override {
void print(Print::Printer &p, PaintOptions o) override {
for (auto &child : _children)
child->print(p);
child->print(p, o);
}

void repr(Io::Emit &e) const override {
Expand Down
2 changes: 1 addition & 1 deletion src/libs/karm-scene/text.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ struct Text : public Node {
return {_origin, _prose->size()};
}

void paint(Gfx::Canvas &g, Math::Rectf r) override {
void paint(Gfx::Canvas &g, Math::Rectf r, PaintOptions) override {
if (not bound().colide(r))
return;

Expand Down
6 changes: 6 additions & 0 deletions src/libs/karm-ui/layout.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,12 @@ struct Sizing : public ProxyNode<Sizing> {
return _rect;
}

void reconcile(Sizing &o) override {
_min = o._min;
_max = o._max;
ProxyNode<Sizing>::reconcile(o);
}

void layout(Math::Recti bound) override {
_rect = bound;
child().layout(bound);
Expand Down
12 changes: 7 additions & 5 deletions src/libs/karm-ui/view.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -367,12 +367,14 @@ Child canvas(OnPaint onPaint) {

struct SceneCanvas : public View<SceneCanvas> {
Strong<Scene::Node> _scene;
Scene::PaintOptions _options;

SceneCanvas(Strong<Scene::Node> scene)
: _scene(std::move(scene)) {}
SceneCanvas(Strong<Scene::Node> scene, Scene::PaintOptions options)
: _scene(std::move(scene)), _options(options) {}

void reconcile(SceneCanvas &o) override {
_scene = o._scene;
_options = o._options;
View<SceneCanvas>::reconcile(o);
}

Expand All @@ -388,7 +390,7 @@ struct SceneCanvas : public View<SceneCanvas> {

auto rectInScene = trans.inverse().apply(rect.cast<f64>()).bound();

_scene->paint(g, Math::Rectf::MAX);
_scene->paint(g, Math::Rectf::MAX, _options);

g.pop();
}
Expand All @@ -402,8 +404,8 @@ struct SceneCanvas : public View<SceneCanvas> {
}
};

Child canvas(Strong<Scene::Node> child) {
return makeStrong<SceneCanvas>(std::move(child));
Child canvas(Strong<Scene::Node> child, Scene::PaintOptions options) {
return makeStrong<SceneCanvas>(std::move(child), options);
}

// MARK: Filter ----------------------------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion src/libs/karm-ui/view.h
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ using OnPaint = Func<void(Gfx::Canvas &g, Math::Vec2i size)>;

Child canvas(OnPaint onPaint);

Child canvas(Strong<Scene::Node> child);
Child canvas(Strong<Scene::Node> child, Scene::PaintOptions options = {});

// MARK: Blur ------------------------------------------------------------------

Expand Down

0 comments on commit a87c761

Please sign in to comment.