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 5c04132 commit 2743e7f
Show file tree
Hide file tree
Showing 20 changed files with 219 additions and 80 deletions.
36 changes: 36 additions & 0 deletions src/libs/karm-kira/number.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#include <karm-ui/focus.h>
#include <karm-ui/layout.h>
#include <mdi/minus.h>
#include <mdi/plus.h>

#include "number.h"

namespace Karm::Kira {

Ui::Child number(f64 value, Ui::OnChange<f64> onChange, f64 step) {
return Ui::hflow(
Ui::button(
[onChange, value, step](auto &n) {
onChange(n, value - step);
},
Ui::ButtonStyle::subtle(),
Mdi::MINUS
),
Ui::labelMedium("{}", value) | Ui::insets({0, 4}) | Ui::center(),
Ui::button(
[onChange, value, step](auto &n) {
onChange(n, value + step);
},
Ui::ButtonStyle::subtle(),
Mdi::PLUS
)
) |
Ui::box({
.borderRadii = 4,
.borderWidth = 1,
.borderFill = Ui::GRAY800,
}) |
Ui::focusable();
}

} // namespace Karm::Kira
11 changes: 11 additions & 0 deletions src/libs/karm-kira/number.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#pragma once

#include <karm-ui/input.h>

#include "_prelude.h"

namespace Karm::Kira {

Ui::Child number(f64 value, Ui::OnChange<f64> onChange, f64 step = 1);

} // namespace Karm::Kira
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
10 changes: 10 additions & 0 deletions src/libs/karm-kira/row.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#include "checkbox.h"
#include "color-input.h"
#include "number.h"
#include "radio.h"
#include "row.h"
#include "select.h"
Expand Down Expand Up @@ -158,6 +159,15 @@ Ui::Child colorRow(Gfx::Color c, Ui::OnChange<Gfx::Color> onChange, String title
);
}

Ui::Child numberRow(f64 value, Ui::OnChange<f64> onChange, f64 step, String title) {
return rowContent(
NONE,
title,
NONE,
number(value, std::move(onChange), step)
);
}

Ui::Child treeRow(Opt<Ui::Slot> leading, String title, Opt<String> subtitle, Ui::Slot child) {
return Ui::state(false, [=, leading = std::move(leading), child = std::move(child)](bool state, auto bind) {
return vflow(
Expand Down
2 changes: 2 additions & 0 deletions src/libs/karm-kira/row.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ Ui::Child selectRow(Ui::Child value, Ui::Slots options, String title);

Ui::Child colorRow(Gfx::Color color, Ui::OnChange<Gfx::Color> onChange, String title);

Ui::Child numberRow(f64 value, Ui::OnChange<f64> onChange, f64 step, String title);

Ui::Child treeRow(Opt<Ui::Slot> leading, String title, Opt<String> subtitle, Ui::Slot child);

Ui::Child treeRow(Opt<Ui::Slot> leading, String title, Opt<String> subtitle, Ui::Slots children);
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
Loading

0 comments on commit 2743e7f

Please sign in to comment.