Skip to content

ProCraft for Android: An Android native painting app written in Rust

Rust

docs: a slower demo at a person's pace, menus readable

Josh6abd173ab7c5
diff --git a/ATTRIBUTION.md b/ATTRIBUTION.md
index 27105c0..edffc92 100644
--- a/ATTRIBUTION.md
+++ b/ATTRIBUTION.md
@@ -16,7 +16,7 @@ them.
| `apps/procraft-android/android/app/src/main/assets/environments/*.hdr` | 3D view light panoramas (11), halved to 512 x 256 and scaled by `tools/env/make_env.py` | Sergej Majboroda, Greg Zaal, Andreas Mischok, Rico Cilliers (per file in the folder's `README.md`) | <https://polyhaven.com> | CC0 1.0 |
| `assets/app-icon/procraft-{1024,512}.png`, `apps/procraft-android/android/app/src/main/res/drawable-*dpi/ic_launcher_{foreground,monochrome}.png`, `apps/procraft-android/android/app/src/main/res/drawable-nodpi/procraft_mark.png`, `apps/procraft-android/android/app/src/main/res/mipmap-anydpi/ic_launcher.xml` | App icon: a paintbrush in a warm-to-cool blend on black | ProCraft contributors | Original work, drawn in code by `tools/app-icon/make_icon.py`; see [`assets/app-icon/README.md`](assets/app-icon/README.md) | MIT OR Apache-2.0 |
| `apps/procraft-android/android/app/src/main/res/drawable/widget_back.xml`, `apps/procraft-android/android/app/src/main/res/drawable-v31/widget_back.xml` | Recent-artwork widget background | ProCraft contributors | Original work | MIT OR Apache-2.0 |
-| `docs/images/{canvas,layers,brushes,studio,colour,actions,adjust,transform}.jpg`, `docs/images/demo.gif`, `docs/images/demo.mp4` | The README's screenshots and demo: ProCraft on a tablet, painting *Dusk* | ProCraft contributors | Original work: the painting made in the app by `tools/showcase/paint_dusk.py`, captured from the screen | MIT OR Apache-2.0 |
+| `docs/images/{canvas,layers,brushes,studio,colour,actions,adjust,transform}.jpg`, `docs/images/demo.webp`, `docs/images/demo.mp4` | The README's screenshots and demo: ProCraft on a tablet, painting *Dusk* | ProCraft contributors | Original work: the painting made in the app by `tools/showcase/paint_dusk2.py`, captured from the screen | MIT OR Apache-2.0 |
| Brushes, brush shapes and grains | Made in code at run time (`crates/engine`) | ProCraft contributors | Original work | MIT OR Apache-2.0 |
### Lucide icons in `LucideIcons.kt`
diff --git a/README.md b/README.md
index 5cca302..025783a 100644
--- a/README.md
+++ b/README.md
@@ -36,9 +36,9 @@
<br>
<p align="center">
- <img src="docs/images/demo.gif" alt="Dusk painted in ProCraft from a blank canvas: the sky filled indigo and airbrushed in bands of purple, magenta, orange and gold; a glow and a sun drawn as a circle that snaps round and is filled by dragging the colour in; two ranges of hills outlined and filled the same way; pines and birds inked; stars airbrushed and made to glow with Bloom; the Layers panel opened on its seven layers" width="100%">
+ <img src="docs/images/demo.webp" alt="Dusk painted in ProCraft from a blank canvas, at a person's pace: the Soft Airbrush picked in the brush library and its size set; colours picked on the Classic square; the sky filled indigo by dragging the colour onto the canvas and airbrushed in bands of purple, magenta, orange and gold; a new layer for a glow and one for the sun, a circle that snaps round and is filled; two ranges of hills outlined and filled; pines and birds inked; stars airbrushed and made to glow with Bloom; the Layers panel opened on its seven layers" width="100%">
<br>
- <sub><b>Dusk</b>, painted from a blank canvas on a Lenovo Legion Tab: the sky filled and airbrushed, a sun snapped round and filled by dragging the colour in, hills filled the same way, pines inked, stars with Bloom. 4× playback with the idle pauses cut; <a href="docs/images/demo.mp4">full video</a> (2×). Every stroke is a pen stroke with pressure, played by the <a href="tools/showcase">demo script</a>.</sub>
+ <sub><b>Dusk</b>, painted from a blank canvas on a Lenovo Legion Tab: every menu shown at close to real speed, the drawing about 3x. The brush library, the colour picker, the layers, a sun snapped round and filled, hills filled the same way, pines, stars with Bloom. <a href="docs/images/demo.mp4">Video</a> (MP4, 2:21). Every stroke is a pen stroke with pressure, eased in and out, played by the <a href="tools/showcase">demo script</a>.</sub>
</p>
<br>
diff --git a/docs/images/demo.gif b/docs/images/demo.gif
deleted file mode 100644
index 597e4b5..0000000
Binary files a/docs/images/demo.gif and /dev/null differ
diff --git a/docs/images/demo.mp4 b/docs/images/demo.mp4
index db7379a..b2d4043 100644
Binary files a/docs/images/demo.mp4 and b/docs/images/demo.mp4 differ
diff --git a/docs/images/demo.webp b/docs/images/demo.webp
new file mode 100644
index 0000000..b3141b0
Binary files /dev/null and b/docs/images/demo.webp differ
diff --git a/tools/showcase/README.md b/tools/showcase/README.md
index 9bf80e2..b77bb51 100644
--- a/tools/showcase/README.md
+++ b/tools/showcase/README.md
@@ -1,29 +1,31 @@
# The showcase
-The README's demo (`docs/images/demo.gif`, `demo.mp4`) and screenshots were made with these scripts
+The README's demo (`docs/images/demo.webp`, `demo.mp4`) and screenshots were made with these scripts
on a Lenovo Legion Tab (2560 x 1600, held sideways), with ProCraft open on an empty gallery.
- `penhid.py`: a virtual pen digitizer (tip, in range, X, Y and 12-bit pressure) for Android's `hid`
shell command, and scripts of pen strokes with real timing.
- `drive.py`: adb taps for the interface (brushes, colours by hex, sizes, layers, the colour dragged
onto the canvas to fill) and pen strokes in screen pixels.
-- `paint_dusk.py`: paints *Dusk* from a blank canvas, stage by stage
- (`new sky sun hills trees stars bloom tour gallery`).
+- `human.py`: strokes as a hand makes them: eased speed, a slight wobble, pressure that rises
+ and falls, pauses between moves.
+- `paint_dusk2.py`: paints *Dusk* from a blank canvas at a person's pace (menus held open, colours
+ picked on the Classic square), stage by stage, and logs each span as menu or drawing.
+- `record_take.py`: records a take (screenrecord) while `paint_dusk2.py` paints, with its log.
+- `edit_take.py`: plays each logged span at its own speed (menus near real time, drawing about 3x),
+ turns P3 into sRGB, and joins them into the video.
- `to_srgb.py`: the tablet's captures hold Display P3 values; this makes sRGB JPEGs for the web.
```sh
export ANDROID_SERIAL=<the tablet>
-adb shell screenrecord --size 1920x1200 --bit-rate 16M --time-limit 0 /sdcard/showcase.mp4 &
-python paint_dusk.py
-adb shell pkill -INT screenrecord && adb pull /sdcard/showcase.mp4
-
-# drop the idle frames, turn P3 into sRGB, then 2x for the video and 4x for the GIF
-ffmpeg -i showcase.mp4 -vf "mpdecimate=hi=768:lo=320:frac=0.33,setpts=N/(30*TB),colorspace=all=bt709:iprimaries=smpte432:itrc=srgb:ispace=bt709:irange=tv,format=yuv420p" -r 30 -an dedup.mp4
-ffmpeg -i dedup.mp4 -vf "setpts=PTS/2,scale=1600:1000:flags=lanczos,tpad=stop_mode=clone:stop_duration=3" -r 30 -an -crf 23 -movflags +faststart demo.mp4
-ffmpeg -i dedup.mp4 -vf "setpts=PTS/4,fps=12,scale=800:-1:flags=lanczos,tpad=stop_mode=clone:stop_duration=2.5,split[a][b];[a]palettegen=max_colors=200:stats_mode=diff[p];[b][p]paletteuse=dither=bayer:bayer_scale=4:diff_mode=rectangle" demo.gif
+python record_take.py take # take/take.mp4 and take/take.json
+python edit_take.py take 2.6 edit # edit/demo_full.mp4 (2.6 s: the log's lag behind the video)
+ffmpeg -i edit/demo_full.mp4 -vf "fps=12,scale=960:-1:flags=lanczos" -c:v libwebp_anim -loop 0 -q:v 62 demo.webp
```
Notes from making it: start pen strokes clear of the sidebar (left) and of any system handle on the
screen's edge; drag the colour out of the colour button with a plain swipe (holding first takes the
previous colour) starting a little below the top edge (nearer, it pulls down the system bars); and
-give a fill a closed outline inside the canvas, its points no more than a stroke's width apart.
+give a fill a closed outline inside the canvas, its points no more than a stroke's width apart,
+drawn without wobble or thin ends; a brush change forgets the size slider's place (each brush
+keeps its own size); and keep taps away from the pen button at the top right edge.
diff --git a/tools/showcase/drive.py b/tools/showcase/drive.py
index df3b46a..95a3506 100644
--- a/tools/showcase/drive.py
+++ b/tools/showcase/drive.py
@@ -62,8 +62,8 @@ class Pen:
def __init__(self):
self.s = Script(map_xy=screen_to_pen)
- def stroke(self, pts, step_ms=8):
- self.s.stroke(pts, step_ms)
+ def stroke(self, pts, step_ms=8, **timing):
+ self.s.stroke(pts, step_ms, **timing)
return self
def pause(self, ms):
diff --git a/tools/showcase/edit_take.py b/tools/showcase/edit_take.py
new file mode 100644
index 0000000..7163077
--- /dev/null
+++ b/tools/showcase/edit_take.py
@@ -0,0 +1,73 @@
+"""Cuts the take into the README video: each logged span played at its own speed (the first colour
+pick, brush pick and new layer close to real time so they can be read, repeats faster, fills and
+the layers slow enough to see, drawing at about 3x), the take's P3 colours made sRGB, the spans
+joined, then a full MP4 and an animated WebP and GIF for the README.
+
+usage: python -I edit_take.py <take dir> <offset s> <out dir>
+"""
+import json
+import os
+import subprocess
+import sys
+
+take, offset, out = sys.argv[1], float(sys.argv[2]), sys.argv[3]
+os.makedirs(out, exist_ok=True)
+j = json.load(open(os.path.join(take, "take.json"), encoding="utf-8"))
+segs = sorted(j["segments"], key=lambda s: s["start"])
+seen = {}
+
+
+def speed(s):
+ kind, label = s["kind"], s["label"]
+ if kind == "draw":
+ return 1.6 if label == "sun circle" else 3.0
+ family = label.split(" ")[0]
+ n = seen.get(family, 0)
+ seen[family] = n + 1
+ if family == "new":
+ return 1.6 if label == "new canvas" else (1.5 if n == 0 else 3.0)
+ if family == "colour":
+ return 1.5 if n < 2 else 3.0
+ if family == "brush":
+ return 1.6 if n == 0 else 2.6
+ if family in ("size", "opacity"):
+ return 2.0
+ if family == "drop":
+ return 1.2
+ if family == "bloom":
+ return 1.3
+ if family == "layers":
+ return 0.9
+ return 2.0
+
+
+plan = []
+t = 0.0
+for s in segs:
+ if s["label"] == "gallery":
+ break
+ a, b = s["start"] + offset, s["end"] + offset
+ if a > t + 0.05:
+ plan.append((t, a, 6.0, "idle"))
+ plan.append((max(a, t), b, speed(s), s["label"]))
+ t = b
+plan.append((t, t + 1.2, 0.8, "end hold"))
+
+colour = "colorspace=all=bt709:iprimaries=smpte432:itrc=srgb:ispace=bt709:irange=tv"
+parts = []
+for i, (a, b, sp, label) in enumerate(plan):
+ if b - a < 0.05:
+ continue
+ part = os.path.join(out, f"part_{i:03d}.mp4")
+ subprocess.run(["ffmpeg", "-v", "error", "-y", "-ss", f"{a:.3f}", "-to", f"{b:.3f}", "-i", os.path.join(take, "take.mp4"),
+ "-vf", f"setpts=(PTS-STARTPTS)/{sp},fps=30,{colour},scale=1600:1000:flags=lanczos,format=yuv420p",
+ "-an", "-c:v", "libx264", "-crf", "18", "-preset", "veryfast", part], check=True)
+ parts.append(part)
+with open(os.path.join(out, "parts.txt"), "w", encoding="utf-8") as f:
+ for p in parts:
+ f.write(f"file '{p}'\n")
+full = os.path.join(out, "demo_full.mp4")
+subprocess.run(["ffmpeg", "-v", "error", "-y", "-f", "concat", "-safe", "0", "-i", os.path.join(out, "parts.txt"),
+ "-c:v", "libx264", "-crf", "23", "-preset", "slow", "-pix_fmt", "yuv420p", "-movflags", "+faststart", full], check=True)
+total = sum((b - a) / sp for a, b, sp, _ in plan)
+print("parts", len(parts), "length about", round(total, 1), "s")
diff --git a/tools/showcase/human.py b/tools/showcase/human.py
new file mode 100644
index 0000000..d42cdfc
--- /dev/null
+++ b/tools/showcase/human.py
@@ -0,0 +1,70 @@
+"""Makes scripted pen strokes and taps read as a person's: strokes that start and end slowly, a
+slight wobble, pressure that rises and falls, and pauses between moves.
+"""
+import math
+import random
+
+rng = random.Random(11)
+
+
+def smooth_noise(n, amp, scale=18):
+ """n values of a smooth wobble, about +-amp."""
+ knots = [rng.uniform(-1, 1) for _ in range(n // scale + 3)]
+ out = []
+ for i in range(n):
+ t = i / scale
+ k = int(t)
+ f = t - k
+ f = f * f * (3 - 2 * f)
+ out.append(amp * (knots[k] * (1 - f) + knots[k + 1] * f))
+ return out
+
+
+def resample(path, step):
+ """Points along a polyline `path` [(x, y, p)], about `step` px apart."""
+ out = [path[0]]
+ carry = 0.0
+ for (x0, y0, p0), (x1, y1, p1) in zip(path, path[1:]):
+ seg = math.hypot(x1 - x0, y1 - y0)
+ if seg == 0:
+ continue
+ d = step - carry
+ while d <= seg:
+ t = d / seg
+ out.append((x0 + (x1 - x0) * t, y0 + (y1 - y0) * t, p0 + (p1 - p0) * t))
+ d += step
+ carry = seg - (d - step)
+ if out[-1][:2] != path[-1][:2]:
+ out.append(path[-1])
+ return out
+
+
+def stroke(path, seconds, wobble=1.6, taper_in=0.12, taper_out=0.18, step=10):
+ """A person's version of `path`: (points, per-point delays in ms). The speed eases in and out
+ (slow at the ends), the line wobbles a little across, and the pressure rises at the start
+ and falls at the end."""
+ pts = resample(path, step)
+ n = len(pts)
+ if n < 2:
+ return pts, [16] * n
+ wob = smooth_noise(n, wobble)
+ out = []
+ for i, (x, y, p) in enumerate(pts):
+ a, b = pts[max(0, i - 1)], pts[min(n - 1, i + 1)]
+ dx, dy = b[0] - a[0], b[1] - a[1]
+ ln = math.hypot(dx, dy) or 1.0
+ nx, ny = -dy / ln, dx / ln
+ t = i / (n - 1)
+ rise = min(1.0, t / taper_in) if taper_in else 1.0
+ fall = min(1.0, (1 - t) / taper_out) if taper_out else 1.0
+ shape = (0.35 + 0.65 * math.sin(math.pi / 2 * rise)) * (0.3 + 0.7 * math.sin(math.pi / 2 * fall))
+ out.append((x + nx * wob[i], y + ny * wob[i], max(0.04, min(1.0, p * shape))))
+ # time per point: an eased speed, slower at both ends
+ weights = [1.0 / (0.35 + math.sin(math.pi * (i + 0.5) / n)) for i in range(n)]
+ total = sum(weights)
+ dts = [max(4, int(seconds * 1000 * w / total)) for w in weights]
+ return out, dts
+
+
+def pause(lo=0.25, hi=0.7):
+ return rng.uniform(lo, hi)
diff --git a/tools/showcase/paint_dusk.py b/tools/showcase/paint_dusk.py
deleted file mode 100644
index 8588b9a..0000000
--- a/tools/showcase/paint_dusk.py
+++ /dev/null
@@ -1,164 +0,0 @@
-"""Paints "Dusk" in ProCraft on the tablet, start to finish, the way a person would: a new canvas,
-a sky filled and airbrushed, a sun drawn with QuickShape and filled with ColorDrop, two ranges of
-hills filled with ColorDrop, pines, stars with Bloom, a look at the layers, back to the gallery.
-
-usage: python -I paint_dusk.py [stage ...] (stages: new sky sun hills trees stars bloom tour gallery)
-"""
-import math
-import os
-import random
-import sys
-import time
-
-sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
-import drive as d # noqa: E402
-
-random.seed(7)
-
-
-def hill(x, base, a1, f1, p1, a2, f2, p2):
- return base + a1 * math.sin(f1 * x / 2560 * 2 * math.pi + p1) + a2 * math.sin(f2 * x / 2560 * 2 * math.pi + p2)
-
-
-FAR = dict(base=1150, a1=70, f1=1.3, p1=0.6, a2=28, f2=3.7, p2=1.9)
-NEAR = dict(base=1330, a1=55, f1=0.9, p1=2.4, a2=22, f2=4.3, p2=0.4)
-SUN = (1760, 1010, 118)
-
-
-def new_canvas():
- d.tap(2476, 65, wait=1.0)
- at = d.bounds_of("Screen Size")
- d.tap(*at, wait=2.5)
-
-
-def sky():
- d.brush("Airbrushing", 0)
- d.size(1.0)
- d.opacity(1.0)
- d.colour("1E1B4B", wait=1.0)
- d.colour_drop(1280, 700)
- for hexcode, ys in (("4A2A7E", (560, 660)), ("B33F7C", (860, 950)), ("F07F4F", (1090, 1160)),
- ("FFC46B", (1270, 1340))):
- d.colour(hexcode, wait=0.5)
- pen = d.Pen()
- for k, y in enumerate(ys):
- # start clear of the sidebar (left) and the system's edge handle (right, 820-1040)
- x0, x1 = (140, 2552) if 820 <= y <= 1040 else (2552, 8)
- pen.stroke(d.wave(x0, x1, y, 26, 1.2 + 0.3 * k, 0.78, n=110, phase=k * 1.7), step_ms=7)
- pen.play("sky")
-
-
-def line(a, b, step=26, p=0.85):
- n = max(2, int(math.hypot(b[0] - a[0], b[1] - a[1]) / step))
- return [(a[0] + (b[0] - a[0]) * i / n, a[1] + (b[1] - a[1]) * i / n, p) for i in range(1, n + 1)]
-
-
-def sun():
- cx, cy, r = SUN
- # a glow on its own layer, soft, round the sun's place
- d.new_layer()
- d.size(0.6)
- d.colour("FFD9A0", wait=0.5)
- pen = d.Pen()
- for k, rr in enumerate((r + 50, r + 95, r + 150)):
- pen.stroke([(cx + rr * math.cos(t), cy + rr * math.sin(t), 0.42 - 0.1 * k)
- for t in [i / 90 * 2 * math.pi for i in range(91)]], step_ms=8)
- pen.play("glow")
- d.new_layer()
- d.colour("FFF1D2", wait=0.5)
- # the disc: a circle held at its end so QuickShape makes it round, then filled by ColorDrop
- d.brush("Inking", 0)
- d.size(0.25)
- pts = [(cx + r * math.cos(t) * (1 + 0.03 * math.sin(3 * t)), cy + r * math.sin(t) * (1 + 0.03 * math.cos(2 * t)), 0.7)
- for t in [i / 90 * 2 * math.pi * 1.02 - math.pi / 2 for i in range(91)]]
- pts += [pts[-1]] * 110
- d.Pen().stroke(pts, step_ms=8).play("circle")
- time.sleep(0.8)
- d.colour_drop(cx, cy)
-
-
-def hills():
- for hexcode, shape, drop_y in (("7B3F8E", FAR, 1530), ("3B2056", NEAR, 1575)):
- d.new_layer()
- d.colour(hexcode, wait=1.0)
- # a closed outline just inside the canvas: the hill's crest right to left, then down,
- # along the bottom and back up, so ColorDrop has a sealed shape to fill
- crest = [(x, hill(x, **shape), 0.85) for x in range(2552, 7, -22)] + [(8, hill(8, **shape), 0.85)]
- pts = crest + line((8, crest[-1][1]), (8, 1590)) + line((8, 1590), (2552, 1590)) + line((2552, 1590), crest[0][:2])
- d.Pen().stroke(pts, step_ms=7).play("hill")
- time.sleep(0.4)
- d.colour_drop(1280, drop_y)
-
-
-def pine(pen, x, base, h):
- # a trunk, then branches zigzagging out wider as they come down
- pen.stroke([(x, base + 6, 0.9), (x, base - h, 0.5)], step_ms=30)
- pts = []
- rows = max(8, int(h / 9))
- for i in range(rows + 1):
- t = i / rows
- y = base - h + 8 + t * (h - 18)
- w = 4 + t * h * 0.30
- side = -1 if i % 2 == 0 else 1
- pts.append((x + side * w, y, 0.45 + 0.4 * t))
- pen.stroke(pts, step_ms=22)
-
-
-def trees():
- d.new_layer()
- d.colour("150B22", wait=0.5)
- d.size(0.55)
- pen = d.Pen()
- for x, h in ((210, 210), (300, 160), (380, 250), (470, 180), (1090, 150), (1160, 205), (1240, 130),
- (2050, 230), (2140, 170), (2230, 260), (2330, 190), (2420, 150)):
- pine(pen, x, hill(x, **NEAR) + 30, h)
- pen.play("trees")
- # birds near the sun, with a fine brush
- d.size(0.10)
- pen = d.Pen()
- for bx, by, s in ((1500, 760, 26), (1560, 720, 20), (1620, 790, 16)):
- pen.stroke([(bx - s, by - s * 0.4, 0.4), (bx - s * 0.5, by - s * 0.6, 0.5), (bx, by, 0.6),
- (bx + s * 0.5, by - s * 0.6, 0.5), (bx + s, by - s * 0.4, 0.4)], step_ms=25)
- pen.play("birds")
-
-
-def stars():
- d.new_layer()
- d.brush("Airbrushing", 1)
- d.size(0.14)
- d.colour("FFFFFF", wait=0.5)
- pen = d.Pen()
- for _ in range(46):
- x = random.uniform(90, 2500)
- y = random.uniform(120, 560) if random.random() < 0.8 else random.uniform(560, 760)
- if (x - SUN[0]) ** 2 + (y - SUN[1]) ** 2 < 330 ** 2:
- continue
- p = random.uniform(0.35, 1.0)
- pen.stroke([(x, y, p)] * 4, step_ms=8)
- pen.play("stars")
-
-
-def bloom():
- d.tap(*d.ADJUST, wait=0.8)
- d.tap(300, 1122, wait=1.2)
- d.swipe(700, 800, 1900, 800, 1600, wait=1.2)
- d.tap(*d.ADJUST, wait=1.0)
-
-
-def tour():
- d.tap(*d.LAYERS, wait=2.6)
- d.tap(*d.LAYERS, wait=0.8)
-
-
-def gallery():
- d.tap(136, 42, wait=2.5)
-
-
-STAGES = {"new": new_canvas, "sky": sky, "sun": sun, "hills": hills, "trees": trees, "stars": stars,
- "bloom": bloom, "tour": tour, "gallery": gallery}
-
-if __name__ == "__main__":
- for name in (sys.argv[1:] or list(STAGES)):
- t = time.time()
- STAGES[name]()
- print(f"{name}: {time.time() - t:.1f} s", flush=True)
diff --git a/tools/showcase/paint_dusk2.py b/tools/showcase/paint_dusk2.py
new file mode 100644
index 0000000..f56b0c9
--- /dev/null
+++ b/tools/showcase/paint_dusk2.py
@@ -0,0 +1,308 @@
+"""Paints "Dusk" in ProCraft on the tablet at a person's pace, for the README's demo: menus held
+open long enough to read, colours picked on the Classic square, strokes that ease in and out
+with a wobble and real pressure, pauses between moves. Each span of time is logged as
+"menu", "draw" or (between them) idle, so the video can play menus at full speed, drawing
+faster, and cut the idle time down.
+
+usage: python -I paint_dusk2.py <segments.json> [stage ...]
+"""
+import colorsys
+import contextlib
+import json
+import math
+import os
+import random
+import sys
+import time
+
+sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
+import drive as d # noqa: E402
+import human # noqa: E402
+
+random.seed(7)
+LOG = []
+T0 = time.time()
+
+
+def seg(kind, label=""):
+ t = time.time() - T0
+ yield
+ LOG.append({"kind": kind, "start": round(t, 3), "end": round(time.time() - T0, 3), "label": label})
+
+
+def hold(s):
+ time.sleep(s)
+
+
+# --- the interface, at a person's pace -------------------------------------------------------
+
+CLASSIC_HUE = (1830, 2488, 944) # the hue strip: x from, x to, y
+CLASSIC_FIELD = (1824, 2494, 250, 900) # saturation across, brightness down
+# the hue strip, measured: (x, the hue a tap there picks); its hues aren't spread evenly
+HUE_AT = [(1836, 0.4), (1900, 15.0), (1980, 40.8), (2060, 94.2), (2140, 124.2), (2220, 180.8), (2300, 226.2),
+ (2380, 273.1), (2440, 310.0), (2480, 348.5)]
+
+
+def hue_x(hue):
+ pts = HUE_AT + [(2492, 360.0)]
+ for (xa, ha), (xb, hb) in zip(pts, pts[1:]):
+ if ha <= hue <= hb:
+ return xa + (xb - xa) * (hue - ha) / (hb - ha)
+ return pts[0][0]
+
+
+_classic_chosen = False
+_size = None
+
+
+def pick(hexcode, label=""):
+ """Opens the colour panel, picks the colour on the Classic square (hue strip, then the
+ square), lets it be seen, closes the panel."""
+ global _classic_chosen
+ r, g, b = (int(hexcode[i:i + 2], 16) / 255 for i in (0, 2, 4))
+ h, s, v = colorsys.rgb_to_hsv(r, g, b)
+ with seg("menu", f"colour {label or hexcode}"):
+ if not d.panel_open():
+ d.tap(*d.COLOUR, wait=1.0)
+ if not _classic_chosen:
+ at = d.bounds_of("Classic")
+ if at:
+ d.tap(at[0], at[1] - 34, wait=1.0)
+ _classic_chosen = True
+ d.tap(hue_x(h * 360), CLASSIC_HUE[2], wait=0.8)
+ fx0, fx1, fy0, fy1 = CLASSIC_FIELD
+ d.tap(fx0 + s * (fx1 - fx0), fy0 + (1 - v) * (fy1 - fy0), wait=1.4)
+ d.tap(*d.COLOUR, wait=0.6)
+
+
+def brush(set_name, index, label=""):
+ global _size
+ # each brush keeps its own size: the slider's place is unknown again
+ _size = None
+ with seg("menu", f"brush {label}"):
+ if not d.library_open():
+ d.tap(*d.PAINT, wait=1.2)
+ if not d.library_open():
+ d.tap(*d.PAINT, wait=1.2)
+ hold(0.6)
+ d.tap(1620, d.SETS[set_name], wait=1.3)
+ d.tap(2000, 300 + 176 * index, wait=1.4)
+ tree = d.ui()
+ if 'text="Brush Studio"' in tree:
+ at = d.bounds_of("Cancel", tree)
+ d.tap(*(at or (2333, 84)), wait=0.8)
+ tree = d.ui()
+ if 'text="Brush library"' in tree:
+ d.tap(*d.PAINT, wait=0.6)
+
+
+def size(frac):
+ """Drags the size slider's thumb from where it is to `frac` of the track, as a hand would."""
+ global _size
+ x, top, bottom = d.SIZE_TRACK
+ ty = bottom - frac * (bottom - top)
+ with seg("menu", f"size {frac}"):
+ if _size is None:
+ d.swipe(x, top + 10, x, bottom + 300, 300, wait=0.2)
+ d.swipe(x, bottom, x, ty, 700, wait=0.6)
+ else:
+ d.swipe(x, bottom - _size * (bottom - top), x, ty, 700, wait=0.6)
+ _size = frac
+
+
+def new_layer():
+ with seg("menu", "new layer"):
+ d.tap(*d.LAYERS, wait=1.2)
+ d.tap(2460, 176, wait=1.5)
+ d.tap(*d.LAYERS, wait=0.5)
+
+
+def colour_drop(x, y):
+ with seg("menu", "drop colour"):
+ d.swipe(2432, 74, x, y, 1300, wait=1.6)
+
+
+# --- strokes -----------------------------------------------------------------------------------
+
+def draw(strokes, label=""):
+ """strokes: [(path, seconds)] or [(path, seconds, options for human.stroke)]; played as one pen
+ session with a person's pauses between."""
+ pen = d.Pen()
+ for i, stroke in enumerate(strokes):
+ path, seconds = stroke[0], stroke[1]
+ pts, dts = human.stroke(path, seconds, **(stroke[2] if len(stroke) > 2 else {}))
+ pen.stroke(pts, dts=dts, hover_ms=int(human.rng.uniform(120, 260)))
+ if i < len(strokes) - 1:
+ pen.pause(int(human.pause() * 1000))
+ local = os.path.join(d.HERE, "pen.json")
+ pen.s.write(local)
+ d.adb("push", local, "/data/local/tmp/pen.json")
+ with seg("draw", label):
+ d.sh("hid /data/local/tmp/pen.json")
+
+
+def hill(x, base, a1, f1, p1, a2, f2, p2):
+ return base + a1 * math.sin(f1 * x / 2560 * 2 * math.pi + p1) + a2 * math.sin(f2 * x / 2560 * 2 * math.pi + p2)
+
+
+def line(a, b, step=24, p=0.85):
+ n = max(2, int(math.hypot(b[0] - a[0], b[1] - a[1]) / step))
+ return [(a[0] + (b[0] - a[0]) * i / n, a[1] + (b[1] - a[1]) * i / n, p) for i in range(1, n + 1)]
+
+
+# an outline that a fill will need closed: no wobble, no thinning at its ends
+SEALED = dict(wobble=0.0, taper_in=0.0, taper_out=0.0)
+FAR = dict(base=1150, a1=70, f1=1.3, p1=0.6, a2=28, f2=3.7, p2=1.9)
+NEAR = dict(base=1330, a1=55, f1=0.9, p1=2.4, a2=22, f2=4.3, p2=0.4)
+SUN = (1760, 1010, 118)
+
+
+# --- the painting ------------------------------------------------------------------------------
+
+def new_canvas():
+ with seg("menu", "new canvas"):
+ hold(0.8)
+ at = None
+ for _ in range(3):
+ if 'text="New canvas"' not in d.ui():
+ d.tap(2476, 65, wait=1.6)
+ at = d.bounds_of("Screen Size")
+ if at:
+ break
+ hold(0.6)
+ d.tap(*at, wait=2.4)
+
+
+def sky():
+ brush("Airbrushing", 0, "Soft Airbrush")
+ size(1.0)
+ with seg("menu", "opacity"):
+ d.opacity(1.0)
+ pick("1E1B4B", "indigo")
+ colour_drop(1280, 700)
+ for hexcode, ys in (("4A2A7E", (560, 680)), ("B33F7C", (880,)), ("F07F4F", (1100, 1180)), ("FFC46B", (1320,))):
+ pick(hexcode)
+ strokes = []
+ for k, y in enumerate(ys):
+ x0, x1 = (140, 2552) if 820 <= y <= 1040 else (2552, 8)
+ strokes.append((d.wave(x0, x1, y, 26, 1.2 + 0.3 * k, 0.8, n=60, phase=k * 1.7), 1.7))
+ draw(strokes, f"sky {hexcode}")
+
+
+def sun():
+ cx, cy, r = SUN
+ new_layer()
+ size(0.6)
+ pick("FFD9A0", "glow")
+ rings = []
+ for k, rr in enumerate((r + 50, r + 100, r + 150)):
+ rings.append(([(cx + rr * math.cos(t), cy + rr * math.sin(t), 0.45 - 0.1 * k)
+ for t in [i / 60 * 2 * math.pi for i in range(61)]], 1.5))
+ draw(rings, "glow")
+ new_layer()
+ pick("FFF1D2", "sun")
+ brush("Inking", 0, "Round Pen")
+ size(0.25)
+ pts = [(cx + r * math.cos(t) * (1 + 0.03 * math.sin(3 * t)), cy + r * math.sin(t) * (1 + 0.03 * math.cos(2 * t)), 0.7)
+ for t in [i / 60 * 2 * math.pi * 1.1 - math.pi / 2 for i in range(61)]]
+ path, dts = human.stroke(pts, 1.5, **SEALED)
+ # held still at the end, so the circle snaps round
+ pen = d.Pen()
+ pen.stroke(path + [path[-1]] * 70, dts=dts + [12] * 70, hover_ms=200)
+ local = os.path.join(d.HERE, "pen.json")
+ pen.s.write(local)
+ d.adb("push", local, "/data/local/tmp/pen.json")
+ with seg("draw", "sun circle"):
+ d.sh("hid /data/local/tmp/pen.json")
+ hold(1.0)
+ colour_drop(cx, cy)
+
+
+def hills():
+ for hexcode, shape, drop_y, label in (("7B3F8E", FAR, 1530, "far hills"), ("3B2056", NEAR, 1575, "near hills")):
+ new_layer()
+ pick(hexcode, label)
+ crest = [(x, hill(x, **shape), 0.85) for x in range(2552, 7, -22)] + [(8, hill(8, **shape), 0.85)]
+ outline = crest + line((8, crest[-1][1]), (8, 1590)) + line((8, 1590), (2552, 1590)) + line((2552, 1590), crest[0][:2])
+ draw([(outline + outline[1:6], 4.4, SEALED)], label)
+ colour_drop(1280, drop_y)
+
+
+def pine(x, base, h):
+ rows = max(8, int(h / 9))
+ zig = []
+ for i in range(rows + 1):
+ t = i / rows
+ side = -1 if i % 2 == 0 else 1
+ zig.append((x + side * (4 + t * h * 0.30), base - h + 8 + t * (h - 18), 0.5 + 0.4 * t))
+ return [([(x, base + 6, 0.9), (x, base - h, 0.6)], 0.35), (zig, 0.9)]
+
+
+def trees():
+ new_layer()
+ pick("150B22", "pines")
+ size(0.55)
+ for group in (((210, 210), (300, 160), (380, 250), (470, 180)), ((1090, 150), (1160, 205), (1240, 130)),
+ ((2050, 230), (2140, 170), (2230, 260), (2330, 190), (2420, 150))):
+ strokes = []
+ for x, h in group:
+ strokes += pine(x, hill(x, **NEAR) + 30, h)
+ draw(strokes, "pines")
+ size(0.10)
+ birds = []
+ for bx, by, s in ((1500, 760, 26), (1560, 720, 20), (1620, 790, 16)):
+ birds.append(([(bx - s, by - s * 0.4, 0.4), (bx - s * 0.5, by - s * 0.6, 0.5), (bx, by, 0.6),
+ (bx + s * 0.5, by - s * 0.6, 0.5), (bx + s, by - s * 0.4, 0.4)], 0.45))
+ draw(birds, "birds")
+
+
+def stars():
+ new_layer()
+ brush("Airbrushing", 1, "Hard Airbrush")
+ size(0.14)
+ pick("FFFFFF", "stars")
+ dabs = []
+ for _ in range(40):
+ x = random.uniform(160, 2440)
+ y = random.uniform(120, 560) if random.random() < 0.8 else random.uniform(560, 760)
+ if (x - SUN[0]) ** 2 + (y - SUN[1]) ** 2 < 330 ** 2:
+ continue
+ p = random.uniform(0.35, 1.0)
+ dabs.append(([(x, y, p), (x + 1, y + 1, p)], 0.06))
+ draw(dabs, "stars")
+
+
+def bloom():
+ with seg("menu", "bloom"):
+ d.tap(*d.ADJUST, wait=1.8)
+ at = d.bounds_of("Bloom")
+ d.tap(*at, wait=1.6)
+ d.swipe(700, 800, 1900, 800, 2200, wait=2.0)
+ d.tap(*d.ADJUST, wait=1.2)
+
+
+def tour():
+ with seg("menu", "layers"):
+ d.tap(*d.LAYERS, wait=3.5)
+ d.tap(*d.LAYERS, wait=1.5)
+
+
+def gallery():
+ with seg("menu", "gallery"):
+ d.tap(136, 42, wait=3.5)
+
+
+STAGES = {"new": new_canvas, "sky": sky, "sun": sun, "hills": hills, "trees": trees, "stars": stars,
+ "bloom": bloom, "tour": tour, "gallery": gallery}
+
+if __name__ == "__main__":
+ out = sys.argv[1]
+ try:
+ for name in (sys.argv[2:] or list(STAGES)):
+ t = time.time()
+ STAGES[name]()
+ print(f"{name}: {time.time() - t:.1f} s", flush=True)
+ finally:
+ with open(out, "w", encoding="utf-8") as f:
+ json.dump({"t0": T0, "segments": LOG}, f, indent=1)
diff --git a/tools/showcase/penhid.py b/tools/showcase/penhid.py
index abc6cb6..523e76c 100644
--- a/tools/showcase/penhid.py
+++ b/tools/showcase/penhid.py
@@ -68,14 +68,17 @@ class Script:
self.events.append({"id": 1, "command": "report",
"report": [1, buttons, xi & 255, xi >> 8, yi & 255, yi >> 8, pi & 255, pi >> 8]})
- def stroke(self, points, step_ms=8):
- """points: (x, y, pressure) in 0..1; the pen comes into range, touches, moves and lifts."""
+ def stroke(self, points, step_ms=8, dts=None, hover_ms=None):
+ """points: (x, y, pressure) in 0..1; the pen comes into range, touches, moves and lifts.
+ `dts` gives each point's own delay in ms (else `step_ms` each); `hover_ms` how long the
+ pen hovers before touching."""
+ dts = dts or [step_ms] * len(points)
x0, y0, _ = points[0]
self.report(False, True, x0, y0, 0)
- self.delay(step_ms * 2)
- for x, y, p in points:
+ self.delay(hover_ms if hover_ms is not None else step_ms * 2)
+ for (x, y, p), dt in zip(points, dts):
self.report(True, True, x, y, max(p, 0.02))
- self.delay(step_ms)
+ self.delay(dt)
x1, y1, _ = points[-1]
self.report(False, True, x1, y1, 0)
self.delay(step_ms)
diff --git a/tools/showcase/record_take.py b/tools/showcase/record_take.py
new file mode 100644
index 0000000..0e40aa2
--- /dev/null
+++ b/tools/showcase/record_take.py
@@ -0,0 +1,38 @@
+"""Records the human-paced take: starts screenrecord on the tablet, paints Dusk with
+paint_dusk2 (which logs each span), stops the recording and pulls it, and writes the log with
+the recording's start time on the same clock.
+
+usage: python -I record_take.py <out dir>
+"""
+import json
+import os
+import subprocess
+import sys
+import time
+
+sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
+import drive as d # noqa: E402
+
+out = sys.argv[1]
+os.makedirs(out, exist_ok=True)
+d.sh("rm -f /sdcard/take.mp4")
+rec = subprocess.Popen([d.ADB, "shell", "screenrecord", "--size", "1920x1200", "--bit-rate", "16M",
+ "--time-limit", "0", "/sdcard/take.mp4"])
+rec_start = time.time()
+time.sleep(2.0)
+import paint_dusk2 as p # noqa: E402 (its clock starts here)
+
+try:
+ for name in ["new", "sky", "sun", "hills", "trees", "stars", "bloom", "tour", "gallery"]:
+ t = time.time()
+ p.STAGES[name]()
+ print(f"{name}: {time.time() - t:.1f} s", flush=True)
+finally:
+ time.sleep(1.5)
+ d.sh("pkill -INT screenrecord")
+ rec.wait(timeout=30)
+ time.sleep(2.0)
+ d.adb("pull", "/sdcard/take.mp4", os.path.join(out, "take.mp4"))
+ with open(os.path.join(out, "take.json"), "w", encoding="utf-8") as f:
+ json.dump({"rec_start": rec_start, "t0": p.T0, "segments": p.LOG}, f, indent=1)
+ print("saved", flush=True)