Skip to content

Commit

Permalink
karm-scene: Moved page to karm-scene. Added viewbox and transform.
Browse files Browse the repository at this point in the history
  • Loading branch information
sleepy-monax committed Dec 23, 2024
1 parent 4c5ea9b commit 99f98f5
Show file tree
Hide file tree
Showing 15 changed files with 161 additions and 80 deletions.
8 changes: 6 additions & 2 deletions src/apps/hideo-zoo/page-print-dialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,12 @@ static inline Page PAGE_PRINT_DIALOG{
[](auto &n) {
Ui::showDialog(
n,
Kr::printDialog([](Print::Settings const &Settings) -> Vec<Strong<Scene::Page>> {
return {makeStrong<Scene::Page>(Settings.paper)};
Kr::printDialog([](Print::Settings const &s) -> Vec<Print::Page> {
return {
{s.paper},
{s.paper},
{s.paper},
};
})
);
},
Expand Down
23 changes: 20 additions & 3 deletions src/libs/karm-kira/print-dialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ namespace Karm::Kira {
struct State {
PrintPreview preview;
Print::Settings settings = {};
Vec<Strong<Scene::Page>> pages = preview(settings);
Vec<Print::Page> pages = preview(settings);
};

struct ChangePaper {
Expand All @@ -37,12 +37,17 @@ struct ToggleHeaderFooter {};

struct ToggleBackgroundGraphics {};

struct ChangeScale {
f64 scale;
};

using Action = Union<
ChangePaper,
ChangeOrientation,
ChangeMargin,
ToggleHeaderFooter,
ToggleBackgroundGraphics>;
ToggleBackgroundGraphics,
ChangeScale>;

static void reduce(State &s, Action a) {
bool shouldUpdatePreview = false;
Expand All @@ -62,6 +67,9 @@ static void reduce(State &s, Action a) {
} else if (a.is<ToggleBackgroundGraphics>()) {
s.settings.backgroundGraphics = not s.settings.backgroundGraphics;
shouldUpdatePreview = true;
} else if (auto changeScale = a.is<ChangeScale>()) {
s.settings.scale = clamp(changeScale->scale, 0.1, 10.);
shouldUpdatePreview = true;
}

if (shouldUpdatePreview) {
Expand Down Expand Up @@ -111,7 +119,7 @@ Ui::Child _printPaper(State const &s, usize index) {

return Ui::stack(
Ui::canvas(
s.pages[index],
s.pages[index].content(),
{
.showBackgroundGraphics = s.settings.backgroundGraphics,
}
Expand Down Expand Up @@ -301,6 +309,15 @@ Ui::Child _printSettings(State const &s) {
"Margins"s
),

numberRow(
s.settings.scale,
[](auto &n, f64 scale) {
Model::bubble<ChangeScale>(n, ChangeScale{scale});
},
0.1,
"Scale"s
),

checkboxRow(
s.settings.headerFooter,
[&](auto &n, ...) {
Expand Down
4 changes: 2 additions & 2 deletions src/libs/karm-kira/print-dialog.h
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
#pragma once

#include <karm-scene/page.h>
#include <karm-print/page.h>
#include <karm-ui/input.h>

#include "_prelude.h"

namespace Karm::Kira {

using PrintPreview = SharedFunc<Vec<Strong<Scene::Page>>(Print::Settings const &)>;
using PrintPreview = SharedFunc<Vec<Print::Page>(Print::Settings const &)>;

Ui::Child printDialog(PrintPreview preview);

Expand Down
3 changes: 2 additions & 1 deletion src/libs/karm-print/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"type": "lib",
"description": "Print documents and images.",
"requires": [
"karm-pdf"
"karm-pdf",
"karm-scene"
]
}
33 changes: 33 additions & 0 deletions src/libs/karm-print/page.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#pragma once

#include <karm-scene/base.h>
#include <karm-scene/stack.h>
#include <karm-scene/viewbox.h>

#include "paper.h"
#include "printer.h"

namespace Karm::Print {

struct Page {
PaperStock _paper;
Strong<Scene::Node> _content;

Page(PaperStock paper, Opt<Strong<Scene::Node>> content = NONE)
: _paper(paper), _content(content ? content.take() : makeStrong<Scene::Stack>()) {}

Strong<Scene::Node> content() const {
return makeStrong<Scene::Viewbox>(_paper.size(), _content);
}

void print(Print::Printer &doc, Scene::PaintOptions o = {.showBackgroundGraphics = false}) {
auto &canvas = doc.beginPage(_paper);
content()->paint(canvas, _paper.size().cast<f64>(), o);
}

void repr(Io::Emit &e) const {
e("(page paper:{} root:{})", _paper, content());
}
};

} // namespace Karm::Print
6 changes: 5 additions & 1 deletion src/libs/karm-print/paper.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ struct PaperStock {
PaperStock landscape() const {
return {name, height, width};
}

void repr(Io::Emit &e) const {
e("(paper {} {}x{})", name, width, height);
}
};

struct PaperSeries {
Expand Down Expand Up @@ -162,7 +166,7 @@ struct Settings {
Margins margins = Margins::DEFAULT;
Orientation orientation = Orientation::PORTRAIT;

double scale = 1.;
f64 scale = 1.;
bool headerFooter = true;
bool backgroundGraphics = false;
};
Expand Down
3 changes: 0 additions & 3 deletions src/libs/karm-scene/base.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

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

namespace Karm::Scene {

Expand All @@ -23,8 +22,6 @@ struct Node {

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

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

virtual void repr(Io::Emit &e) const {
e("(node z:{})", zIndex);
}
Expand Down
3 changes: 1 addition & 2 deletions src/libs/karm-scene/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
"type": "lib",
"description": "2D scene graph library",
"requires": [
"karm-gfx",
"karm-print"
"karm-gfx"
]
}
36 changes: 0 additions & 36 deletions src/libs/karm-scene/page.h

This file was deleted.

17 changes: 1 addition & 16 deletions src/libs/karm-scene/stack.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@ namespace Karm::Scene {

struct Stack : public Node {
Vec<Strong<Node>> _children;
Opt<Math::Trans2f> _transform;

Stack(Opt<Math::Trans2f> transform = NONE) : _transform(transform) {}

void add(Strong<Node> child) {
_children.pushBack(child);
Expand All @@ -27,28 +24,16 @@ struct Stack : public Node {
Math::Rectf rect;
for (auto &child : _children)
rect = rect.mergeWith(child->bound());

return rect;
}

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

if (_transform) {
g.push();
g.transform(_transform.unwrap());
}

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

if (_transform)
g.pop();
}

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

void repr(Io::Emit &e) const override {
Expand Down
40 changes: 40 additions & 0 deletions src/libs/karm-scene/transform.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#pragma once

#include "base.h"

namespace Karm::Scene {

struct Transform : public Node {
Strong<Node> _content;
Math::Trans2f _transform;

Transform(Strong<Node> content, Math::Trans2f transform)
: _content(content), _transform(transform) {}

void prepare() override {
_content->prepare();
}

Math::Rectf bound() override {
return _transform
.inverse()
.apply(_content->bound())
.bound();
}

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

g.push();
g.transform(_transform);
_content->paint(g, r, o);
g.pop();
}

void repr(Io::Emit &e) const override {
e("(transform transform:{} content:{})", _transform, _content);
}
};

} // namespace Karm::Scene
39 changes: 39 additions & 0 deletions src/libs/karm-scene/viewbox.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@

#include "base.h"

namespace Karm::Scene {

struct Viewbox : public Node {
Strong<Node> _content;
Math::Rectf _viewbox;

Viewbox(Math::Rectf viewbox, Strong<Node> content)
: _content(content), _viewbox(viewbox) {}

void prepare() override {
_content->prepare();
}

Math::Rectf bound() override {
return _viewbox.size();
}

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

g.push();
g.origin(_viewbox.xy);
g.clip(_viewbox);

_content->paint(g, r, o);

g.pop();
}

void repr(Io::Emit &e) const override {
e("(viewbox {} content:{})", _viewbox, _content);
}
};

} // namespace Karm::Scene
Loading

0 comments on commit 99f98f5

Please sign in to comment.