diff --git a/Ports/JavaSE/nbproject/project.properties b/Ports/JavaSE/nbproject/project.properties index 614a94f84c0..758b80b3101 100644 --- a/Ports/JavaSE/nbproject/project.properties +++ b/Ports/JavaSE/nbproject/project.properties @@ -36,12 +36,12 @@ endorsed.classpath= # unused by the simulator integration screenshot tests that drive this # build, and the user-facing codenameone-javase jar is the Maven one. # -# JavaSEJoglSurface/JavaSEGLDevice are the JOGL (OpenGL) 3D backend; JOGL is +# JavaSEJoglSurface/JavaSEGLDevice/JavaSEJoglFoldTarget are the JOGL (OpenGL) 3D backend; JOGL is # only a Maven dependency, so the Ant build (no JOGL on its classpath) skips # them. JavaSEPort loads JavaSEJoglSurface reflectively and falls back to the # software renderer when it is absent, so the 3D API still works (software) in # the Ant simulator and gets the real GPU backend in the Maven simulator. -excludes=com/codename1/testing/junit/**,com/codename1/impl/javase/JavaSEJoglSurface.java,com/codename1/impl/javase/JavaSEGLDevice.java +excludes=com/codename1/testing/junit/**,com/codename1/impl/javase/JavaSEJoglSurface.java,com/codename1/impl/javase/JavaSEGLDevice.java,com/codename1/impl/javase/JavaSEJoglFoldTarget.java file.reference.Filters.jar=../../../cn1-binaries/javase/Filters.jar file.reference.commons-codec.jar=../../../cn1-binaries/javase/commons-codec.jar file.reference.commons-compress.jar=../../../cn1-binaries/javase/commons-compress.jar diff --git a/Ports/JavaSE/src/com/codename1/impl/javase/AnimationClock.java b/Ports/JavaSE/src/com/codename1/impl/javase/AnimationClock.java new file mode 100644 index 00000000000..72257d68ce6 --- /dev/null +++ b/Ports/JavaSE/src/com/codename1/impl/javase/AnimationClock.java @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2026, Codename One and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Codename One designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the LICENSE file that accompanied this code. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Codename One through http://www.codenameone.com/ if you + * need additional information or have any questions. + */ +package com.codename1.impl.javase; + +import java.util.concurrent.atomic.AtomicLong; + +/// The time the simulator's own animations (folding, turning) and fold +/// scripts run on. It is the wall clock, except during a lockstep recording, +/// where the recorder sets it to the moment of the frame it is about to +/// capture: each video frame then shows the motion exactly one frame +/// interval further on, however long the previous frame took to render. +final class AnimationClock { + private static final AtomicLong PINNED = new AtomicLong(-1); + + private AnimationClock() { + } + + static long now() { + long p = PINNED.get(); + return p >= 0 ? p : System.currentTimeMillis(); + } + + static void pin(long epochMs) { + PINNED.set(epochMs); + } + + static void release() { + PINNED.set(-1); + } + + static boolean isPinned() { + return PINNED.get() >= 0; + } +} diff --git a/Ports/JavaSE/src/com/codename1/impl/javase/FoldController.java b/Ports/JavaSE/src/com/codename1/impl/javase/FoldController.java new file mode 100644 index 00000000000..e62f26757b0 --- /dev/null +++ b/Ports/JavaSE/src/com/codename1/impl/javase/FoldController.java @@ -0,0 +1,165 @@ +/* + * Copyright (c) 2026, Codename One and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Codename One designates this + * particular file as subject to the "Classpath" exception as provided + * by Codename One in the LICENSE file that accompanied this code. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Codename One through http://www.codenameone.com/ if + * you need additional information or have any questions. + */ +package com.codename1.impl.javase; + +import com.codename1.ui.DevicePosture; + +/// Drives the hinge angle of a foldable skin between postures. An animated +/// change eases the angle toward its target one step at a time; `step` +/// takes the clock as an argument so the motion is testable without a timer. +/// +/// The host learns three things: every new angle (to repaint the 3D fold), +/// a posture change when the angle crosses a `FoldableSkin` threshold (the +/// app relays out mid-motion, as on a device), and when the device reaches +/// or leaves fully closed, which is when the display moves to the cover +/// screen and back. Leaving closed is reported before the first open frame, +/// so the inner screen is in place before it is drawn. +final class FoldController { + + interface Host { + /// The angle moved; repaint. + void angleChanged(int angle, boolean animating); + + /// The posture category changed (a `DevicePosture` POSTURE_ constant). + void postureChanged(int posture); + + /// The device became fully closed (true) or started opening (false). + void coverChanged(boolean closed); + } + + static final long DEFAULT_DURATION_MS = 700; + + private final Host host; + private double angle = 180; + private int posture = DevicePosture.POSTURE_FLAT; + private boolean closed; + private double fromAngle; + private double toAngle = 180; + private long startMs; + private long durationMs; + private boolean animating; + + FoldController(Host host) { + this.host = host; + } + + int getAngle() { + return (int) Math.round(angle); + } + + double getExactAngle() { + return angle; + } + + int getPosture() { + return posture; + } + + boolean isClosed() { + return closed; + } + + boolean isAnimating() { + return animating; + } + + int getTargetAngle() { + return (int) Math.round(toAngle); + } + + /// Moves to `target` degrees (0 closed, 180 flat) over `duration` + /// milliseconds, or at once when `duration` is 0 or less. + void moveTo(int target, long duration, long nowMs) { + double t = Math.max(0, Math.min(180, target)); + if (duration <= 0) { + animating = false; + toAngle = t; + apply(t, false); + return; + } + fromAngle = angle; + toAngle = t; + startMs = nowMs; + // A partial move takes a proportional share of the full sweep. + durationMs = Math.max(1, (long) (duration * Math.abs(t - angle) / 180.0)); + animating = Math.abs(t - angle) > 0.01; + if (animating && closed) { + // Leaving closed: back to the inner display before any open frame. + closed = false; + host.coverChanged(false); + } + if (!animating) { + apply(t, false); + } + } + + /// Advances an animation to `nowMs`. Returns true while it is still running. + boolean step(long nowMs) { + if (!animating) { + return false; + } + double f = (nowMs - startMs) / (double) durationMs; + if (f >= 1) { + animating = false; + apply(toAngle, false); + return false; + } + f = Math.max(0, f); + // Ease in and out (smootherstep): zero velocity and acceleration at + // both ends, so a hinge picks up and settles without a jolt. + double e = f * f * f * (f * (f * 6 - 15) + 10); + apply(fromAngle + (toAngle - fromAngle) * e, true); + return true; + } + + private void apply(double a, boolean moving) { + angle = a; + boolean nowClosed = !moving && a < 0.5; + if (closed && !nowClosed) { + // Opening: the inner display goes back first, so the posture + // event below is delivered to an app already at the inner size. + closed = false; + host.coverChanged(false); + } + int p = FoldableSkin.postureForAngle((int) Math.round(a)); + if (p != posture) { + posture = p; + host.postureChanged(p); + } + if (nowClosed && !closed) { + closed = true; + host.coverChanged(true); + } + host.angleChanged(getAngle(), moving); + } + + /// Puts the controller at rest at `a` without notifying the host, for a + /// newly loaded skin. + void reset(int a) { + animating = false; + angle = Math.max(0, Math.min(180, a)); + toAngle = angle; + posture = FoldableSkin.postureForAngle(a); + closed = angle < 0.5; + } +} diff --git a/Ports/JavaSE/src/com/codename1/impl/javase/FoldRenderer.java b/Ports/JavaSE/src/com/codename1/impl/javase/FoldRenderer.java new file mode 100644 index 00000000000..99a9e4d96ab --- /dev/null +++ b/Ports/JavaSE/src/com/codename1/impl/javase/FoldRenderer.java @@ -0,0 +1,827 @@ +/* + * Copyright (c) 2026, Codename One and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Codename One designates this + * particular file as subject to the "Classpath" exception as provided + * by Codename One in the LICENSE file that accompanied this code. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Codename One through http://www.codenameone.com/ if + * you need additional information or have any questions. + */ +package com.codename1.impl.javase; + +import com.codename1.gpu.Camera; +import com.codename1.gpu.GraphicsDevice; +import com.codename1.gpu.Light; +import com.codename1.gpu.Material; +import com.codename1.gpu.Mesh; +import com.codename1.gpu.PrimitiveType; +import com.codename1.gpu.RenderState; +import com.codename1.gpu.Texture; +import com.codename1.gpu.VertexBuffer; +import com.codename1.gpu.VertexFormat; +import java.awt.AlphaComposite; +import java.awt.Color; +import java.awt.Graphics2D; +import java.awt.Rectangle; +import java.awt.RenderingHints; +import java.awt.image.BufferedImage; +import java.awt.image.DataBufferInt; +import java.util.ArrayList; +import java.util.List; + +/// Draws a simulator skin as a physical device, in 3D, with the +/// `com.codename1.gpu` API: folded part way, or turning between portrait and +/// landscape. +/// +/// The device (skin with the live screen composited in) is a mesh, not a +/// picture: each half is a slab whose outline follows the skin's rounded +/// corners, with a front textured by the skin, a back textured by the cover +/// display, metal side walls, and a spine that wraps the hinge as the halves +/// part. The first half (left of a vertical hinge, above a horizontal one) +/// is the reference; the second swings about the hinge toward the viewer and +/// over the first, so at 0 degrees its back, the cover display, lies on top. +/// +/// What makes the motion read on a flat monitor, all of it faded in by how +/// far the device has left its resting pose, so a flat device renders +/// exactly as the flat skin: +/// +/// - the whole device turns as it folds, the way a reader holds a book, so +/// neither half is ever seen edge on; +/// - a key light from the upper left, so the halves and walls shade apart; +/// - the device tips toward the viewer, so its top wall shows; +/// - a soft shadow under the device, cast from its own silhouette. +/// +/// It renders through a `Target`: the GPU (`JavaSEJoglFoldTarget`, an +/// offscreen OpenGL framebuffer read back each frame) when JOGL loads, or the +/// software rasterizer (`JavaSESoftwareDevice`) otherwise and headless. Both +/// hand back a `BufferedImage`, which the Swing canvas composites and the +/// screen recorder captures. +final class FoldRenderer { + + private static final float FOV_DEGREES = 30f; + private static final double MAX_TILT = Math.toRadians(9); + private static final double ROTATION_TILT = Math.toRadians(14); + /// The thickness of one half as a share of the shorter side of the skin. + private static final float THICKNESS = 0.026f; + private static final int CORNER_SEGMENTS = 10; + private static final int SPINE_SEGMENTS = 12; + private static final int WALL_COLOR = 0xff9c9ca4; + + /// Where a frame is drawn: owns a device, makes it current, runs the + /// scene on it and returns the frame. + interface Target { + BufferedImage render(int width, int height, Painter painter); + + /// True for a GPU, which renders full size at animation rates. + boolean isAccelerated(); + } + + interface Painter { + void paint(GraphicsDevice device); + } + + /// The software rasterizer, drawing straight into an image. + static final class SoftwareTarget implements Target { + private final JavaSESoftwareDevice soft = new JavaSESoftwareDevice(); + + public BufferedImage render(int width, int height, Painter painter) { + soft.resize(width, height); + painter.paint(soft); + return soft.getImage(); + } + + public boolean isAccelerated() { + return false; + } + } + + private final Target target; + /// The device of the frame being painted. + private GraphicsDevice device; + /// What this frame created on the device, released when it is done. + private final List frameTextures = new ArrayList(); + private final List frameBuffers = new ArrayList(); + private final Camera camera = new Camera(); + private final Light light = new Light(); + private BufferedImage shadowSmall; + private int radiusFor = -1; + private float radius; + + // Per frame state shared by the vertex transform. + private int w; + private int h; + private float split; + private boolean verticalAxis; + private double phi; + /// The turn of the whole device about the hinge. + private double bodyTurn; + /// The turn of the whole device in the screen plane (rotation). + private double roll; + /// The tip of the whole device toward the viewer about the screen x axis. + private double tilt; + /// The uniform scale that keeps the moved device inside the frame. + private float fit = 1; + + FoldRenderer() { + this(new SoftwareTarget()); + } + + FoldRenderer(Target target) { + this.target = target; + } + + boolean isAccelerated() { + return target.isAccelerated(); + } + + /// Renders `front` (the open device, `W x H`) folded to `angle` degrees. + /// + /// - `back`: the image for the outside of the swinging panel (the cover + /// display with its skin), or null for a plain dark back. + /// - `hinge`: the hinge in `front` pixels; null folds at the centre. + /// - `verticalAxis`: true when the hinge runs top to bottom. + /// + /// Returns an image the size of `front`, transparent around the device. + /// The returned image is reused by the next call. + BufferedImage render(BufferedImage front, BufferedImage back, Rectangle hinge, + boolean verticalAxis, double angle) { + double clamped = Math.max(0, Math.min(180, angle)); + // How far into the fold, 0 flat .. 1 at half open and beyond; every + // effect is scaled by it, eased so none of them starts abruptly. + // They fade out again over the last 45 degrees of closing: a closed + // device faces the viewer squarely, and its cover must show in its + // own colors. + double t = Math.min(1, (180 - clamped) / 90.0) * Math.min(1, clamped / 45.0); + float fold = (float) (t * t * (3 - 2 * t)); + phi = Math.toRadians(180 - clamped); + // Back toward facing the viewer as it closes, where the cover shows. + bodyTurn = -phi / 2 * Math.min(1, clamped / 90.0); + roll = 0; + tilt = MAX_TILT * fold; + return draw(front, back, hinge, verticalAxis, fold, front.getWidth(), front.getHeight()); + } + + /// Renders the flat device `front` turned by `rollDegrees` in the screen + /// plane (negative is clockwise) and lifted toward the viewer by `lift` + /// (0 .. 1), centered in a `outW x outH` image: one frame of the device + /// rotating between portrait and landscape. + BufferedImage renderRotation(BufferedImage front, double rollDegrees, double lift, int outW, int outH) { + return renderRotation(front, null, null, true, 180, rollDegrees, lift, outW, outH); + } + + /// As above for a device held folded to `angle` (see `render`), which + /// turns as it is, still folded, rather than snapping flat first. + BufferedImage renderRotation(BufferedImage front, BufferedImage back, Rectangle hinge, + boolean verticalAxis, double angle, double rollDegrees, double lift, int outW, int outH) { + float l = (float) Math.max(0, Math.min(1, lift)); + double clamped = Math.max(0, Math.min(180, angle)); + double t = Math.min(1, (180 - clamped) / 90.0) * Math.min(1, clamped / 45.0); + float fold = (float) (t * t * (3 - 2 * t)); + phi = Math.toRadians(180 - clamped); + bodyTurn = -phi / 2 * Math.min(1, clamped / 90.0); + roll = Math.toRadians(rollDegrees); + tilt = Math.max(MAX_TILT * fold, ROTATION_TILT * l); + return draw(front, back, hinge, verticalAxis, Math.max(fold, l), outW, outH); + } + + private BufferedImage draw(BufferedImage front, BufferedImage back, Rectangle hinge, + boolean vertical, float effect, int outW, int outH) { + w = front.getWidth(); + h = front.getHeight(); + this.verticalAxis = vertical; + if (vertical) { + split = hinge != null ? hinge.x + hinge.width / 2f : w / 2f; + } else { + split = hinge != null ? hinge.y + hinge.height / 2f : h / 2f; + } + updateRadius(front); + + float dist = (float) (outH / 2f / Math.tan(Math.toRadians(FOV_DEGREES / 2))); + camera.setPerspective(FOV_DEGREES, dist * 0.05f, dist * 4f) + .setAspect(outW / (float) outH) + .setPosition(0, 0, dist) + .setTarget(0, 0, 0) + .setUp(0, 1, 0); + + // Head on and neutral at rest (a facing panel is lit at exactly + // 1.0: diffuse 0.75 + ambient 0.25), a key light from the upper left + // with deeper shadows as the device moves. + light.setDirection(0.45f * effect, -0.35f * effect, -1); + int diffuse = 0xc0 + Math.round(0x18 * effect); + int ambient = 0x40 - Math.round(0x06 * effect); + light.setColor(0xff000000 | diffuse << 16 | diffuse << 8 | diffuse); + light.setAmbientColor(0xff000000 | ambient << 16 | ambient << 8 | ambient); + + float thick = Math.min(w, h) * THICKNESS; + fit = 1; + fit = fitScale(thick, dist, outW, outH) * (1 - 0.07f * effect); + + final float thickness = thick; + final BufferedImage frontImage = front; + final BufferedImage backImage = back; + BufferedImage img = target.render(outW, outH, new Painter() { + public void paint(GraphicsDevice d) { + device = d; + try { + d.setLight(light); + d.setCamera(camera); + d.clear(0x00000000, true, true); + paintScene(frontImage, backImage, thickness); + } finally { + for (Texture t : frameTextures) { + d.dispose(t); + } + for (VertexBuffer b : frameBuffers) { + d.dispose(b); + } + frameTextures.clear(); + frameBuffers.clear(); + device = null; + } + } + }); + return effect > 0.01f ? withShadow(img, effect, Math.min(outW, outH)) : img; + } + + private Texture texture(int tw, int th, int[] argb) { + Texture t = device.createTexture(tw, th, argb); + frameTextures.add(t); + return t; + } + + private void paintScene(BufferedImage front, BufferedImage back, float thick) { + Texture frontTex = texture(w, h, pixels(front)); + frontTex.setFilter(Texture.Filter.LINEAR); + // The displays are emissive: never shaded, so the screen keeps its + // real colors at every angle and stays usable while folded. Only + // the body (walls, plain backs) takes the light. + Material frontMat = unlit(material(frontTex, 0xffffffff, true)); + Material wallMat = material(null, WALL_COLOR, false); + Material backMat; + if (back != null) { + Texture backTex = texture(back.getWidth(), back.getHeight(), pixels(back)); + backTex.setFilter(Texture.Filter.LINEAR); + backMat = unlit(material(backTex, 0xffffffff, true)); + } else { + backMat = null; + } + + float[] fixedR; + float[] swingR; + if (verticalAxis) { + fixedR = new float[]{0, 0, split, h}; + swingR = new float[]{split, 0, w, h}; + } else { + fixedR = new float[]{0, 0, w, split}; + swingR = new float[]{0, split, w, h}; + } + if (backMat == null) { + backMat = backPlateMaterial(swingR, false); + } + List fixedOutline = outline(fixedR, false); + List swingOutline = outline(swingR, true); + // Opaque geometry first, so the blended faces find it in the depth + // buffer: the walls, and the spine once the halves part. + walls(wallMat, fixedOutline, false, thick); + walls(wallMat, swingOutline, true, thick); + if (phi > 0.001) { + spine(wallMat, thick); + } + face(frontMat, fixedOutline, false, true, 0); + face(frontMat, swingOutline, true, true, 0); + plateFace = back == null; + face(backMat, swingOutline, true, false, thick); + plateFace = false; + if (phi > 0.001) { + // The fixed half's back only shows once the device turns. + plateFace = true; + face(backPlateMaterial(fixedR, true), fixedOutline, false, false, thick); + plateFace = false; + } + } + + /// The plain back of each half, without and with the camera island. + private final BufferedImage[] backPlates = new BufferedImage[2]; + + /// The back of a half without a cover display: dark anodized glass with + /// a soft sheen, and the camera island on the half that carries it. + private Material backPlateMaterial(float[] r, boolean camera) { + int bw = Math.max(8, Math.round((r[2] - r[0]) / 2)); + int bh = Math.max(8, Math.round((r[3] - r[1]) / 2)); + int k = camera ? 1 : 0; + BufferedImage backPlate = backPlates[k]; + if (backPlate == null || backPlate.getWidth() != bw || backPlate.getHeight() != bh) { + backPlate = paintBackPlate(bw, bh, camera, radius / 2); + backPlates[k] = backPlate; + } + Texture t = texture(bw, bh, pixels(backPlate)); + t.setFilter(Texture.Filter.LINEAR); + return material(t, 0xffffffff, false); + } + + static BufferedImage paintBackPlate(int bw, int bh, boolean camera, float cornerRadius) { + BufferedImage img = new BufferedImage(bw, bh, BufferedImage.TYPE_INT_ARGB); + java.awt.Graphics2D g = img.createGraphics(); + g.setRenderingHint(java.awt.RenderingHints.KEY_ANTIALIASING, java.awt.RenderingHints.VALUE_ANTIALIAS_ON); + g.setPaint(new java.awt.GradientPaint(0, 0, new java.awt.Color(0x45454c), bw, bh, + new java.awt.Color(0x1f1f23))); + g.fillRect(0, 0, bw, bh); + // A diagonal sheen, as a frosted back catches a key light. + g.setPaint(new java.awt.GradientPaint(bw * 0.2f, 0, new java.awt.Color(255, 255, 255, 0), + bw * 0.55f, bh * 0.35f, new java.awt.Color(255, 255, 255, 26), true)); + g.fillRect(0, 0, bw, bh); + if (camera) { + // Seen from behind the outer edge is on the right of the image. + float s = Math.min(bw, bh) * 0.42f; + float m = Math.max(4, cornerRadius * 0.45f); + float x = bw - m - s; + float y = m; + g.setColor(new java.awt.Color(0x2c2c31)); + g.fill(new java.awt.geom.RoundRectangle2D.Float(x, y, s, s, s * 0.36f, s * 0.36f)); + g.setColor(new java.awt.Color(255, 255, 255, 40)); + g.draw(new java.awt.geom.RoundRectangle2D.Float(x, y, s, s, s * 0.36f, s * 0.36f)); + float lens = s * 0.36f; + float[][] at = {{0.27f, 0.27f}, {0.27f, 0.73f}, {0.73f, 0.5f}}; + for (float[] a : at) { + float cx = x + s * a[0]; + float cy = y + s * a[1]; + g.setColor(new java.awt.Color(0x0c0c0e)); + g.fill(new java.awt.geom.Ellipse2D.Float(cx - lens / 2, cy - lens / 2, lens, lens)); + g.setColor(new java.awt.Color(0x5a5a66)); + g.draw(new java.awt.geom.Ellipse2D.Float(cx - lens / 2, cy - lens / 2, lens, lens)); + g.setColor(new java.awt.Color(0x1c2a4a)); + float in = lens * 0.45f; + g.fill(new java.awt.geom.Ellipse2D.Float(cx - in / 2, cy - in / 2, in, in)); + g.setColor(new java.awt.Color(255, 255, 255, 120)); + float hi = lens * 0.12f; + g.fill(new java.awt.geom.Ellipse2D.Float(cx - in / 4, cy - in / 4, hi, hi)); + } + g.setColor(new java.awt.Color(0xe8e0c8)); + float fl = s * 0.1f; + g.fill(new java.awt.geom.Ellipse2D.Float(x + s * 0.73f - fl / 2, y + s * 0.2f - fl / 2, fl, fl)); + } + g.dispose(); + return img; + } + + private static int[] pixels(BufferedImage img) { + if (img.getType() == BufferedImage.TYPE_INT_ARGB) { + return ((DataBufferInt) img.getRaster().getDataBuffer()).getData(); + } + return img.getRGB(0, 0, img.getWidth(), img.getHeight(), null, 0, img.getWidth()); + } + + /// The body's corner radius, read from the skin's own alpha along the + /// diagonal of its top left corner: the first opaque pixel lies + /// r (1 - 1/sqrt 2) in from the corner. + private void updateRadius(BufferedImage front) { + int key = front.getWidth() * 31 + front.getHeight(); + if (key == radiusFor) { + return; + } + radiusFor = key; + int max = Math.min(front.getWidth(), front.getHeight()) / 4; + int d = 0; + while (d < max && (front.getRGB(d, d) >>> 24) < 128) { + d++; + } + radius = Math.min(max, d / 0.2929f); + } + + /// The uniform scale at which every corner of the device, moved, still + /// projects inside the frame: x d / (d - z) for a point at depth z. + private float fitScale(float thick, float dist, int outW, int outH) { + float best = 1; + float[][] rects = {{0, 0, w, h}}; + float[] zs = {0, -thick}; + for (float[] r : rects) { + for (int i = 0; i < 4; i++) { + float px = (i & 1) == 0 ? r[0] : r[2]; + float py = (i & 2) == 0 ? r[1] : r[3]; + for (int k = 0; k < zs.length; k++) { + float[] p = transform(px, py, zs[k], onSwing(px, py)); + float ax = outW / 2f * dist / (Math.abs(p[0]) * dist + Math.max(0, p[2]) * outW / 2f); + float ay = outH / 2f * dist / (Math.abs(p[1]) * dist + Math.max(0, p[2]) * outH / 2f); + best = Math.min(best, Math.min(ax, ay)); + } + } + } + return best; + } + + private boolean onSwing(float px, float py) { + return verticalAxis ? px > split : py > split; + } + + private static Material unlit(Material lit) { + Material m = new Material(Material.Type.UNLIT); + m.setColor(lit.getColor()); + if (lit.getTexture() != null) { + m.setTexture(lit.getTexture()); + } + m.setRenderState(lit.getRenderState()); + return m; + } + + private Material material(Texture tex, int color, boolean blended) { + Material m = new Material(Material.Type.LAMBERT); + m.setColor(color); + if (tex != null) { + m.setTexture(tex); + } + RenderState rs = blended ? RenderState.transparent().setDepthWrite(true) : RenderState.opaque(); + m.setRenderState(rs.setCullMode(RenderState.CullMode.BACK)); + return m; + } + + /// The outline of one half in front pixels, counter clockwise on screen + /// (y down): rounded at the body's corners, square at the hinge. Each + /// point carries its outward normal in the screen plane, `{x, y, nx, ny}` + /// with ny in pixel direction; the edge along the hinge is the one from + /// the last point back to the first. + private List outline(float[] r, boolean swing) { + float x0 = r[0]; + float y0 = r[1]; + float x1 = r[2]; + float y1 = r[3]; + float cr = Math.min(radius, Math.min(x1 - x0, y1 - y0) / 2f); + // Corners in order top-left, bottom-left, bottom-right, top-right + // (counter clockwise with y down), each rounded unless on the hinge. + boolean[] round; + if (verticalAxis) { + round = swing ? new boolean[]{false, false, true, true} : new boolean[]{true, true, false, false}; + } else { + round = swing ? new boolean[]{false, true, true, false} : new boolean[]{true, false, false, true}; + } + float[][] corner = {{x0, y0}, {x0, y1}, {x1, y1}, {x1, y0}}; + // Arc centres step inward from each corner; start angles (y down). + float[][] centre = {{x0 + cr, y0 + cr}, {x0 + cr, y1 - cr}, {x1 - cr, y1 - cr}, {x1 - cr, y0 + cr}}; + double[] start = {Math.PI * 1.5, Math.PI, Math.PI * 0.5, 0}; + // The hinge edge closes the loop, so start right after it. + int first; + if (verticalAxis) { + first = swing ? 1 : 3; + } else { + first = swing ? 0 : 2; + } + List out = new ArrayList(); + for (int k = 0; k < 4; k++) { + int c = (first + k) % 4; + if (round[c]) { + for (int s = 0; s <= CORNER_SEGMENTS; s++) { + // Counter clockwise on screen is decreasing angle with y down. + double a = start[c] - s * (Math.PI / 2) / CORNER_SEGMENTS; + float nx = (float) Math.cos(a); + float ny = (float) Math.sin(a); + out.add(new float[]{centre[c][0] + nx * cr, centre[c][1] + ny * cr, nx, ny}); + } + } else { + // A square hinge corner takes the normal of its other edge. + float nx = 0; + float ny = 0; + if (verticalAxis) { + ny = c == 0 || c == 3 ? -1 : 1; + } else { + nx = c == 0 || c == 1 ? -1 : 1; + } + out.add(new float[]{corner[c][0], corner[c][1], nx, ny}); + } + } + return out; + } + + /// The front (facing +z at rest, z = 0) or back (facing -z, at depth + /// -`depth`) of one half, a fan from its centre. The front samples its + /// own part of the front image; the back the whole back image, mirrored + /// along the folding axis so it reads the right way round once the half + /// has turned over onto the other. + /// True while drawing a plain back plate, which has square texture + /// corners and so keeps the rounded outline as geometry. + private boolean plateFace; + + private void face(Material mat, List ol, boolean swing, boolean front, float depth) { + float minX = Float.MAX_VALUE; + float minY = Float.MAX_VALUE; + float maxX = -Float.MAX_VALUE; + float maxY = -Float.MAX_VALUE; + for (float[] p : ol) { + minX = Math.min(minX, p[0]); + minY = Math.min(minY, p[1]); + maxX = Math.max(maxX, p[0]); + maxY = Math.max(maxY, p[1]); + } + float z = front ? 0 : -depth; + float nz = front ? 1 : -1; + if (mat.getTexture() == null || plateFace) { + int n = ol.size(); + float[][] c = new float[n + 1][]; + for (int i = 0; i < n; i++) { + c[i] = vertex(ol.get(i)[0], ol.get(i)[1], z, front, minX, minY, maxX, maxY); + } + c[n] = vertex((minX + maxX) / 2, (minY + maxY) / 2, z, front, minX, minY, maxX, maxY); + int[] order = new int[n * 3]; + for (int i = 0; i < n; i++) { + int j = (i + 1) % n; + order[i * 3] = n; + order[i * 3 + 1] = front ? i : j; + order[i * 3 + 2] = front ? j : i; + } + emit(mat, c, order, null, 0, 0, nz, swing); + return; + } + // One quad over the half's bounds, not a fan along its rounded + // outline: the corners are the texture's own transparency (the skin + // art is rounded), and a fan of forty sliver triangles cost the + // rasterizer many times the face's area every frame. + float[][] c = { + vertex(minX, minY, z, front, minX, minY, maxX, maxY), + vertex(minX, maxY, z, front, minX, minY, maxX, maxY), + vertex(maxX, maxY, z, front, minX, minY, maxX, maxY), + vertex(maxX, minY, z, front, minX, minY, maxX, maxY) + }; + // Counter clockwise on screen (y down) for the front; the back winds + // the other way so culling keeps it only when turned over. + int[] order = front ? new int[]{0, 1, 2, 0, 2, 3} : new int[]{0, 2, 1, 0, 3, 2}; + emit(mat, c, order, null, 0, 0, nz, swing); + } + + private float[] vertex(float x, float y, float z, boolean front, float minX, float minY, + float maxX, float maxY) { + float u; + float v; + if (front) { + u = x / w; + v = y / h; + } else if (verticalAxis) { + u = 1 - (x - minX) / (maxX - minX); + v = (y - minY) / (maxY - minY); + } else { + u = (x - minX) / (maxX - minX); + v = 1 - (y - minY) / (maxY - minY); + } + return new float[]{x, y, z, u, v}; + } + + /// The side walls of one half: a band between the front outline at z = 0 + /// and the back at z = -thick, along every edge but the hinge, with + /// smooth normals so the rounded corners catch the light like metal. + private void walls(Material mat, List ol, boolean swing, float thick) { + int n = ol.size(); + int segs = n - 1; + float[][] c = new float[segs * 4][]; + float[][] normals = new float[segs * 4][]; + int[] order = new int[segs * 12]; + for (int i = 0; i < segs; i++) { + float[] a = ol.get(i); + float[] b = ol.get(i + 1); + int o = i * 4; + c[o] = new float[]{a[0], a[1], 0, 0, 0}; + c[o + 1] = new float[]{a[0], a[1], -thick, 0, 0}; + c[o + 2] = new float[]{b[0], b[1], -thick, 0, 0}; + c[o + 3] = new float[]{b[0], b[1], 0, 0, 0}; + // Pixel y runs down, world y up. + normals[o] = new float[]{a[2], -a[3], 0}; + normals[o + 1] = normals[o]; + normals[o + 2] = new float[]{b[2], -b[3], 0}; + normals[o + 3] = normals[o + 2]; + int q = i * 12; + int[] tri = {o, o + 1, o + 2, o, o + 2, o + 3}; + for (int k = 0; k < 6; k++) { + // Both windings: a wall is seen from outside on whichever + // side it lies, and the far ones hide behind the faces. + order[q + k] = tri[k]; + order[q + 6 + k] = tri[5 - k]; + } + } + emit(mat, c, order, normals, 0, 0, 0, swing); + } + + /// The spine: a half cylinder about the hinge line, from the fixed + /// half's back edge to the swung half's, so the outside of the fold + /// stays closed as the halves part. + private void spine(Material mat, float thick) { + float len0 = 0; + float len1 = verticalAxis ? h : w; + int n = SPINE_SEGMENTS; + float[][] c = new float[(n + 1) * 2][]; + float[][] normals = new float[(n + 1) * 2][]; + for (int s = 0; s <= n; s++) { + double a = phi * s / n; + float d = (float) (thick * Math.sin(a)); + float z = (float) (-thick * Math.cos(a)); + // Outward: away from the hinge line, in hinge-local terms. + float nd = (float) Math.sin(a); + float nz = (float) -Math.cos(a); + if (verticalAxis) { + c[s * 2] = new float[]{split + d, len0, z, 0, 0}; + c[s * 2 + 1] = new float[]{split + d, len1, z, 0, 0}; + normals[s * 2] = new float[]{nd, 0, nz}; + } else { + c[s * 2] = new float[]{len0, split + d, z, 0, 0}; + c[s * 2 + 1] = new float[]{len1, split + d, z, 0, 0}; + normals[s * 2] = new float[]{0, -nd, nz}; + } + normals[s * 2 + 1] = normals[s * 2]; + } + int[] order = new int[n * 12]; + for (int s = 0; s < n; s++) { + int a = s * 2; + int q = s * 12; + int[] tri = {a, a + 1, a + 3, a, a + 3, a + 2}; + for (int k = 0; k < 6; k++) { + order[q + k] = tri[k]; + // Both windings: which one faces out depends on the axis. + order[q + 6 + k] = tri[5 - k]; + } + } + // Not swung again: its points are already placed about the hinge. + emit(mat, c, order, normals, 0, 0, 0, false); + } + + /// Transforms one point given in front pixels (y down) plus depth into + /// world space (origin at the device centre, y up): the swing about the + /// hinge, the turn of the whole device, its roll in the screen plane and + /// its tip toward the viewer, then the fit. + private float[] transform(float x, float y, float z, boolean swing) { + float[] p = {x - w / 2f, h / 2f - y, z}; + float[] n = {0, 0, 0}; + move(p, n, swing); + p[0] *= fit; + p[1] *= fit; + p[2] *= fit; + return p; + } + + private void move(float[] p, float[] n, boolean swing) { + if (swing) { + hinge(p, n, (float) Math.cos(phi), (float) Math.sin(phi)); + } + if (bodyTurn != 0) { + hinge(p, n, (float) Math.cos(bodyTurn), (float) Math.sin(bodyTurn)); + } + if (roll != 0) { + float c = (float) Math.cos(roll); + float s = (float) Math.sin(roll); + float x = p[0] * c - p[1] * s; + p[1] = p[0] * s + p[1] * c; + p[0] = x; + float nx = n[0] * c - n[1] * s; + n[1] = n[0] * s + n[1] * c; + n[0] = nx; + } + if (tilt != 0) { + float c = (float) Math.cos(tilt); + float s = (float) Math.sin(tilt); + float y = p[1] * c - p[2] * s; + p[2] = p[1] * s + p[2] * c; + p[1] = y; + float ny = n[1] * c - n[2] * s; + n[2] = n[1] * s + n[2] * c; + n[1] = ny; + } + } + + private void emit(Material mat, float[][] c, int[] order, float[][] normals, float nx0, float ny0, + float nz0, boolean swing) { + float[] data = new float[order.length * 8]; + for (int i = 0; i < order.length; i++) { + float[] v = c[order[i]]; + float[] p = {v[0] - w / 2f, h / 2f - v[1], v[2]}; + float[] nn = normals != null ? normals[order[i]] : null; + float[] n = nn != null ? new float[]{nn[0], nn[1], nn[2]} : new float[]{nx0, ny0, nz0}; + move(p, n, swing); + int o = i * 8; + data[o] = p[0] * fit; + data[o + 1] = p[1] * fit; + data[o + 2] = p[2] * fit; + data[o + 3] = n[0]; + data[o + 4] = n[1]; + data[o + 5] = n[2]; + data[o + 6] = v[3]; + data[o + 7] = v[4]; + } + VertexBuffer vb = new VertexBuffer(VertexFormat.POSITION_NORMAL_TEXCOORD, order.length); + vb.setData(data); + frameBuffers.add(vb); + device.draw(new Mesh(vb, PrimitiveType.TRIANGLES), mat, null); + } + + /// Rotates a point and its normal about the hinge line, so that a + /// positive angle swings the second half toward the viewer (+z). + private void hinge(float[] p, float[] n, float cos, float sin) { + if (verticalAxis) { + float hx = split - w / 2f; + float dx = p[0] - hx; + float x = hx + dx * cos - p[2] * sin; + p[2] = dx * sin + p[2] * cos; + p[0] = x; + float nx = n[0] * cos - n[2] * sin; + n[2] = n[0] * sin + n[2] * cos; + n[0] = nx; + } else { + float hy = h / 2f - split; + float dy = p[1] - hy; + float y = hy + dy * cos + p[2] * sin; + p[2] = -dy * sin + p[2] * cos; + p[1] = y; + float ny = n[1] * cos + n[2] * sin; + n[2] = -n[1] * sin + n[2] * cos; + n[1] = ny; + } + } + + /// A soft shadow under the device, cast from its own silhouette: the + /// rendered alpha, shrunk eight times and stretched back (which blurs + /// it), offset away from the key light and drawn beneath the device. + private BufferedImage withShadow(BufferedImage img, float fold, float span) { + int fw = img.getWidth(); + int fh = img.getHeight(); + int sw = Math.max(1, fw / 8); + int sh = Math.max(1, fh / 8); + if (shadowSmall == null || shadowSmall.getWidth() != sw || shadowSmall.getHeight() != sh) { + shadowSmall = new BufferedImage(sw, sh, BufferedImage.TYPE_INT_ARGB); + } + Graphics2D sg = shadowSmall.createGraphics(); + sg.setComposite(AlphaComposite.Clear); + sg.fillRect(0, 0, sw, sh); + sg.setComposite(AlphaComposite.SrcOver); + sg.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); + sg.drawImage(img, 0, 0, sw, sh, null); + // Keep the coverage, paint it black. + sg.setComposite(AlphaComposite.SrcIn); + sg.setColor(Color.BLACK); + sg.fillRect(0, 0, sw, sh); + sg.dispose(); + // Sampled down, a diagonal silhouette edge comes out stepped; two + // box passes turn the steps into a penumbra before it is stretched. + boxBlur(shadowSmall); + boxBlur(shadowSmall); + // A new image every frame: the one returned is kept by the window + // and read by the recorder on another thread, and a reused buffer + // cleared for the next frame showed up there with its shadow missing. + BufferedImage composite = new BufferedImage(fw, fh, BufferedImage.TYPE_INT_ARGB); + Graphics2D g = composite.createGraphics(); + g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); + g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.45f * fold)); + // Placed with sub-pixel precision: offsets rounded to whole pixels, + // stretched from a mask an eighth the size, made the shadow step and + // shimmer while the hinge moved. + double ox = span * 0.025 * fold; + double oy = span * 0.03 * fold; + double grow = span * 0.01 * fold; + java.awt.geom.AffineTransform at = new java.awt.geom.AffineTransform(); + at.translate(ox - grow, oy - grow); + at.scale((fw + grow * 2) / sw, (fh + grow * 2) / sh); + g.drawImage(shadowSmall, at, null); + g.setComposite(AlphaComposite.SrcOver); + g.drawImage(img, 0, 0, null); + g.dispose(); + return composite; + } + + /// A 5x5 box blur of the alpha channel, in place (the colour is black). + private static void boxBlur(BufferedImage img) { + int iw = img.getWidth(); + int ih = img.getHeight(); + int[] a = new int[iw * ih]; + int[] px = img.getRGB(0, 0, iw, ih, null, 0, iw); + for (int i = 0; i < a.length; i++) { + a[i] = px[i] >>> 24; + } + int[] tmp = new int[a.length]; + for (int y = 0; y < ih; y++) { + for (int x = 0; x < iw; x++) { + int sum = 0; + for (int k = -2; k <= 2; k++) { + int xx = Math.max(0, Math.min(iw - 1, x + k)); + sum += a[y * iw + xx]; + } + tmp[y * iw + x] = sum / 5; + } + } + for (int y = 0; y < ih; y++) { + for (int x = 0; x < iw; x++) { + int sum = 0; + for (int k = -2; k <= 2; k++) { + int yy = Math.max(0, Math.min(ih - 1, y + k)); + sum += tmp[yy * iw + x]; + } + px[y * iw + x] = (sum / 5) << 24; + } + } + img.setRGB(0, 0, iw, ih, px, 0, iw); + } +} diff --git a/Ports/JavaSE/src/com/codename1/impl/javase/FoldScript.java b/Ports/JavaSE/src/com/codename1/impl/javase/FoldScript.java new file mode 100644 index 00000000000..7de1d00dd3c --- /dev/null +++ b/Ports/JavaSE/src/com/codename1/impl/javase/FoldScript.java @@ -0,0 +1,207 @@ +/* + * Copyright (c) 2026, Codename One and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Codename One designates this + * particular file as subject to the "Classpath" exception as provided + * by Codename One in the LICENSE file that accompanied this code. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Codename One through http://www.codenameone.com/ if + * you need additional information or have any questions. + */ +package com.codename1.impl.javase; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.Comparator; +import java.util.List; +import java.util.Map; + +/// A timed sequence of fold changes, played against a foldable skin while a +/// recording runs, so one call produces a repeatable promo clip: +/// +/// ```json +/// [{"at": 1500, "posture": "half"}, +/// {"at": 4000, "posture": "closed"}, +/// {"at": 7000, "angle": 180, "animateMs": 1200}, +/// {"at": 9000, "orientation": "landscape"}] +/// ``` +/// +/// `at` is milliseconds from the start; each step names a `posture` (flat, +/// half, closed) or an `angle`, and optionally `animateMs`, or turns the +/// device to an `orientation` (portrait, landscape). +final class FoldScript { + + interface Target { + void fold(String posture, int angle, long animateMs); + + void rotate(boolean portrait); + } + + static final class Step { + final long at; + final String posture; + final int angle; + final long animateMs; + /// "portrait" or "landscape" for a rotation step, otherwise null. + final String orientation; + + Step(long at, String posture, int angle, long animateMs) { + this(at, posture, angle, animateMs, null); + } + + Step(long at, String posture, int angle, long animateMs, String orientation) { + this.at = at; + this.orientation = orientation; + this.posture = posture; + this.angle = angle; + this.animateMs = animateMs; + } + } + + private final List steps; + private Thread thread; + private boolean cancelled; + + private FoldScript(List steps) { + this.steps = steps; + } + + /// Parses the JSON list (as `JSONParser` decodes it). Throws + /// IllegalArgumentException naming the first bad step. + static FoldScript parse(Object json) { + if (!(json instanceof List)) { + throw new IllegalArgumentException("script must be a list of steps"); + } + List out = new ArrayList(); + int index = 0; + for (Object o : (List) json) { + if (!(o instanceof Map)) { + throw new IllegalArgumentException("script step " + index + " is not an object"); + } + Map m = (Map) o; + long at = number(m.get("at"), -1); + if (at < 0) { + throw new IllegalArgumentException("script step " + index + " needs a non-negative 'at'"); + } + Object p = m.get("posture"); + String posture = p instanceof String ? (String) p : null; + int angle = (int) number(m.get("angle"), -1); + Object or = m.get("orientation"); + String orientation = or instanceof String ? (String) or : null; + if (orientation != null && !"portrait".equals(orientation) && !"landscape".equals(orientation)) { + throw new IllegalArgumentException("script step " + index + ": unknown orientation " + orientation); + } + if (posture == null && angle < 0 && orientation == null) { + throw new IllegalArgumentException("script step " + index + + " needs a 'posture', an 'angle' or an 'orientation'"); + } + if (posture != null && !FoldableSkin.POSTURE_FLAT.equals(posture) + && !FoldableSkin.POSTURE_HALF.equals(posture) + && !FoldableSkin.POSTURE_CLOSED.equals(posture)) { + throw new IllegalArgumentException("script step " + index + ": unknown posture " + posture); + } + out.add(new Step(at, posture, angle, number(m.get("animateMs"), -1), orientation)); + index++; + } + Collections.sort(out, new Comparator() { + public int compare(Step a, Step b) { + return a.at < b.at ? -1 : (a.at > b.at ? 1 : 0); + } + }); + return new FoldScript(out); + } + + private static long number(Object o, long def) { + return o instanceof Number ? ((Number) o).longValue() : def; + } + + /// True when a step folds rather than only rotates. + boolean foldsTheDevice() { + for (Step s : steps) { + if (s.orientation == null) { + return true; + } + } + return false; + } + + List getSteps() { + return Collections.unmodifiableList(steps); + } + + /// Plays the steps on a daemon thread, timed from now. + synchronized void start(final Target target) { + startWithTail(target, null, 0); + } + + /// As `start`, then runs `done` (on the script's thread) once the last + /// step has played and `tailMs` more have passed, unless cancelled. + synchronized void startWithTail(final Target target, final Runnable done, final long tailMs) { + final long start = AnimationClock.now(); + thread = new Thread(new Runnable() { + public void run() { + for (Step s : steps) { + // Polled rather than slept in one go: during a lockstep + // recording the clock follows the video, not the wall. + if (!waitUntil(start + s.at)) { + return; + } + if (isCancelled()) { + return; + } + if (s.orientation != null) { + target.rotate("portrait".equals(s.orientation)); + } else { + target.fold(s.posture, s.angle, s.animateMs); + } + } + if (done != null) { + if (!waitUntil(AnimationClock.now() + Math.max(0, tailMs))) { + return; + } + if (!isCancelled()) { + done.run(); + } + } + } + }, "FoldScript"); + thread.setDaemon(true); + thread.start(); + } + + /// Waits until the animation clock reaches `at`; false if interrupted. + private static boolean waitUntil(long at) { + try { + long wait; + while ((wait = at - AnimationClock.now()) > 0) { + Thread.sleep(Math.min(wait, 5)); + } + return true; + } catch (InterruptedException e) { + return false; + } + } + + synchronized boolean isCancelled() { + return cancelled; + } + + synchronized void cancel() { + cancelled = true; + if (thread != null) { + thread.interrupt(); + } + } +} diff --git a/Ports/JavaSE/src/com/codename1/impl/javase/FoldableSkin.java b/Ports/JavaSE/src/com/codename1/impl/javase/FoldableSkin.java new file mode 100644 index 00000000000..623f0e41452 --- /dev/null +++ b/Ports/JavaSE/src/com/codename1/impl/javase/FoldableSkin.java @@ -0,0 +1,275 @@ +/* + * Copyright (c) 2026, Codename One and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Codename One designates this + * particular file as subject to the "Classpath" exception as provided + * by Codename One in the LICENSE file that accompanied this code. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Codename One through http://www.codenameone.com/ if + * you need additional information or have any questions. + */ +package com.codename1.impl.javase; + +import com.codename1.io.Properties; +import com.codename1.ui.DevicePosture; +import java.awt.Rectangle; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +/// The foldable description of a simulator skin, read from the optional +/// `foldable*` keys of `skin.properties`. A skin without `foldable=true` +/// yields no instance at all, so every legacy skin behaves exactly as before. +/// +/// Keys (all coordinates in skin image pixels): +/// +/// - `foldable=true` +/// - `foldOrientation=vertical|horizontal` - a book fold (hinge runs top to +/// bottom) or a flip fold (hinge runs left to right). Default vertical. +/// - `foldType=continuous|dualScreen` - one flexible panel, or two panels +/// with a physical gap between them. Default continuous. +/// - `hingePortrait=x,y,w,h` and `hingeLandscape=x,y,w,h` - the hinge in the +/// portrait and landscape skin images. A continuous fold may give a zero +/// width (vertical) or zero height (horizontal); the crease is then the +/// centre line of the screen when the key is absent. +/// - `foldPostures=flat,half,closed` - the postures the device supports, in +/// the order the simulator menu lists them. Default all three. +/// - `crease=true` - draw a faint crease on the flat inner screen. +/// - `coverPortraitScreenX/Y/Width/Height`, `coverLandscapeScreenX/...` and +/// `coverPpi` - the cover (outer) display, in the `cover.skin` and +/// `cover.skin_l` images. A device with no cover display omits them, and +/// its closed posture then shows the back of the device with a dark screen. +/// - `coverCutouts` / `coverSafePortraitX...` - as the inner keys, prefixed. +final class FoldableSkin { + + /// Below this hinge angle a device reads as closed, above + /// `FLAT_THRESHOLD` as flat, in between as half opened. These are the + /// thresholds Jetpack WindowManager reports postures with. + static final int CLOSED_THRESHOLD = 30; + static final int FLAT_THRESHOLD = 150; + + static final String POSTURE_FLAT = "flat"; + static final String POSTURE_HALF = "half"; + static final String POSTURE_CLOSED = "closed"; + + private final boolean vertical; + private final boolean dualScreen; + private final boolean crease; + private final Rectangle hingePortrait; + private final Rectangle hingeLandscape; + private final List postures; + private final Rectangle coverPortraitScreen; + private final Rectangle coverLandscapeScreen; + private final double coverPpi; + + private FoldableSkin(Properties p) { + vertical = !"horizontal".equalsIgnoreCase(p.getProperty("foldOrientation", "vertical").trim()); + dualScreen = "dualScreen".equalsIgnoreCase(p.getProperty("foldType", "continuous").trim()); + crease = "true".equalsIgnoreCase(p.getProperty("crease", "false").trim()); + hingePortrait = parseRect(p.getProperty("hingePortrait")); + hingeLandscape = parseRect(p.getProperty("hingeLandscape")); + List list = new ArrayList(); + String spec = p.getProperty("foldPostures", POSTURE_FLAT + "," + POSTURE_HALF + "," + POSTURE_CLOSED); + for (String s : spec.split(",")) { + String name = s.trim(); + if ((POSTURE_FLAT.equalsIgnoreCase(name) || POSTURE_HALF.equalsIgnoreCase(name) + || POSTURE_CLOSED.equalsIgnoreCase(name)) && !containsIgnoreCase(list, name)) { + list.add(asciiLower(name)); + } + } + if (list.isEmpty()) { + list.add(POSTURE_FLAT); + } + postures = Collections.unmodifiableList(list); + coverPortraitScreen = parseScreen(p, "coverPortraitScreen"); + coverLandscapeScreen = parseScreen(p, "coverLandscapeScreen"); + double ppi = 0; + String ppiSpec = p.getProperty("coverPpi"); + if (ppiSpec != null) { + try { + ppi = Double.parseDouble(ppiSpec.trim()); + } catch (NumberFormatException err) { + ppi = 0; + } + } + coverPpi = ppi; + } + + /// Returns the foldable description of a skin, or null when the skin is + /// not foldable. + static FoldableSkin parse(Properties p) { + if (p == null || !"true".equalsIgnoreCase(p.getProperty("foldable", "false").trim())) { + return null; + } + return new FoldableSkin(p); + } + + boolean isVertical() { + return vertical; + } + + int getFoldOrientation() { + return vertical ? DevicePosture.FOLD_ORIENTATION_VERTICAL : DevicePosture.FOLD_ORIENTATION_HORIZONTAL; + } + + boolean isDualScreen() { + return dualScreen; + } + + boolean hasCrease() { + return crease; + } + + List getPostures() { + return postures; + } + + boolean supports(String posture) { + return postures.contains(posture); + } + + boolean hasCover() { + return coverPortraitScreen != null; + } + + boolean hasCoverLandscape() { + return coverLandscapeScreen != null; + } + + /// The cover display in the cover skin image, or null without a cover. + Rectangle getCoverScreen(boolean portrait) { + Rectangle r = portrait || coverLandscapeScreen == null ? coverPortraitScreen : coverLandscapeScreen; + return r == null ? null : new Rectangle(r); + } + + double getCoverPpi() { + return coverPpi; + } + + /// The hinge in skin image coordinates, or null when the skin gives + /// none for that orientation. + Rectangle getHinge(boolean portrait) { + Rectangle r = portrait || hingeLandscape == null ? hingePortrait : hingeLandscape; + return r == null ? null : new Rectangle(r); + } + + /// The posture a hinge angle reads as, a `DevicePosture` POSTURE_ constant. + static int postureForAngle(int angle) { + if (angle < CLOSED_THRESHOLD) { + return DevicePosture.POSTURE_CLOSED; + } + if (angle > FLAT_THRESHOLD) { + return DevicePosture.POSTURE_FLAT; + } + return DevicePosture.POSTURE_HALF_OPENED; + } + + /// The resting hinge angle of a named posture. + static int angleForPosture(String posture) { + if (POSTURE_CLOSED.equals(posture)) { + return 0; + } + if (POSTURE_HALF.equals(posture)) { + return 90; + } + return 180; + } + + /// The fold in display coordinates, as `DevicePosture.getFoldBounds` + /// answers it. `screen` is the inner screen rectangle in skin pixels and + /// `displayWidth`/`displayHeight` the logical display size. A dual + /// screen device answers its real gap; a continuous one a two pixel line + /// at the hinge (the centre without a hinge key). + Rectangle foldBounds(boolean portrait, Rectangle screen, int displayWidth, int displayHeight) { + Rectangle hinge = getHinge(portrait); + boolean alongX = vertical == portrait; + if (hinge == null || screen == null || screen.width <= 0 || screen.height <= 0) { + return alongX + ? new Rectangle(displayWidth / 2 - 1, 0, 2, displayHeight) + : new Rectangle(0, displayHeight / 2 - 1, displayWidth, 2); + } + if (alongX) { + double sx = displayWidth / (double) screen.width; + int x = (int) Math.round((hinge.x - screen.x) * sx); + int w = dualScreen ? Math.max(2, (int) Math.round(hinge.width * sx)) : 2; + if (!dualScreen) { + x = (int) Math.round((hinge.x + hinge.width / 2.0 - screen.x) * sx) - 1; + } + return new Rectangle(clamp(x, 0, displayWidth - w), 0, w, displayHeight); + } + double sy = displayHeight / (double) screen.height; + int y = (int) Math.round((hinge.y - screen.y) * sy); + int h = dualScreen ? Math.max(2, (int) Math.round(hinge.height * sy)) : 2; + if (!dualScreen) { + y = (int) Math.round((hinge.y + hinge.height / 2.0 - screen.y) * sy) - 1; + } + return new Rectangle(0, clamp(y, 0, displayHeight - h), displayWidth, h); + } + + private static int clamp(int v, int min, int max) { + return v < min ? min : (v > max ? max : v); + } + + private static Rectangle parseScreen(Properties p, String prefix) { + String x = p.getProperty(prefix + "X"); + String y = p.getProperty(prefix + "Y"); + String w = p.getProperty(prefix + "Width"); + String h = p.getProperty(prefix + "Height"); + if (x == null || y == null || w == null || h == null) { + return null; + } + return parseRect(x + "," + y + "," + w + "," + h); + } + + static Rectangle parseRect(String spec) { + if (spec == null) { + return null; + } + String[] parts = spec.trim().split(","); + if (parts.length != 4) { + return null; + } + try { + int w = Integer.parseInt(parts[2].trim()); + int h = Integer.parseInt(parts[3].trim()); + if (w < 0 || h < 0) { + return null; + } + return new Rectangle(Integer.parseInt(parts[0].trim()), Integer.parseInt(parts[1].trim()), w, h); + } catch (NumberFormatException err) { + return null; + } + } + + private static boolean containsIgnoreCase(List list, String s) { + for (String e : list) { + if (e.equalsIgnoreCase(s)) { + return true; + } + } + return false; + } + + // Posture names are ASCII by specification; String.toLowerCase is locale + // sensitive (CLAUDE.md, "Never case-fold a protocol token"). + private static String asciiLower(String s) { + StringBuilder b = new StringBuilder(s.length()); + for (int i = 0; i < s.length(); i++) { + char c = s.charAt(i); + b.append(c >= 'A' && c <= 'Z' ? (char) (c + 32) : c); + } + return b.toString(); + } +} diff --git a/Ports/JavaSE/src/com/codename1/impl/javase/JavaSEGLDevice.java b/Ports/JavaSE/src/com/codename1/impl/javase/JavaSEGLDevice.java index 6c420f90a48..5a8ed7ea918 100644 --- a/Ports/JavaSE/src/com/codename1/impl/javase/JavaSEGLDevice.java +++ b/Ports/JavaSE/src/com/codename1/impl/javase/JavaSEGLDevice.java @@ -153,6 +153,10 @@ public Texture createTexture(Image image) { return createTexture(image.getWidth(), image.getHeight(), image.getRGB()); } + /// Scratch space for texture uploads, reused across calls. + private int[] uploadInts; + private ByteBuffer uploadBuffer; + public Texture createTexture(int w, int h, int[] argb) { Texture t = new Texture(w, h); int[] ids = new int[1]; @@ -160,17 +164,31 @@ public Texture createTexture(int w, int h, int[] argb) { int id = ids[0]; gl.glBindTexture(GL2ES2.GL_TEXTURE_2D, id); - ByteBuffer pixels = ByteBuffer.allocateDirect(w * h * 4); - pixels.order(ByteOrder.nativeOrder()); + // RGBA bytes, written a whole pixel at a time into a reused buffer: + // a byte at a time into a fresh direct buffer cost tens of + // milliseconds for a retina-sized texture uploaded every frame. int count = w * h; + if (uploadInts == null || uploadInts.length < count) { + uploadInts = new int[count]; + } + int[] rgba = uploadInts; + boolean little = ByteOrder.nativeOrder() == ByteOrder.LITTLE_ENDIAN; for (int i = 0; i < count; i++) { int c = argb[i]; - pixels.put((byte) ((c >> 16) & 0xff)); - pixels.put((byte) ((c >> 8) & 0xff)); - pixels.put((byte) (c & 0xff)); - pixels.put((byte) ((c >>> 24) & 0xff)); - } + int a = c >>> 24; + int r = (c >> 16) & 0xff; + int g = (c >> 8) & 0xff; + int b = c & 0xff; + rgba[i] = little ? (a << 24) | (b << 16) | (g << 8) | r : (r << 24) | (g << 16) | (b << 8) | a; + } + if (uploadBuffer == null || uploadBuffer.capacity() < count * 4) { + uploadBuffer = ByteBuffer.allocateDirect(count * 4).order(ByteOrder.nativeOrder()); + } + ByteBuffer pixels = uploadBuffer; + pixels.clear(); + pixels.asIntBuffer().put(rgba, 0, count); pixels.position(0); + pixels.limit(count * 4); gl.glTexImage2D(GL2ES2.GL_TEXTURE_2D, 0, GL2ES2.GL_RGBA, w, h, 0, GL2ES2.GL_RGBA, GL2ES2.GL_UNSIGNED_BYTE, pixels); gl.glBindTexture(GL2ES2.GL_TEXTURE_2D, 0); diff --git a/Ports/JavaSE/src/com/codename1/impl/javase/JavaSEJoglFoldTarget.java b/Ports/JavaSE/src/com/codename1/impl/javase/JavaSEJoglFoldTarget.java new file mode 100644 index 00000000000..91db066a2c1 --- /dev/null +++ b/Ports/JavaSE/src/com/codename1/impl/javase/JavaSEJoglFoldTarget.java @@ -0,0 +1,126 @@ +/* + * Copyright (c) 2026, Codename One and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Codename One designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the LICENSE file that accompanied this code. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Codename One through http://www.codenameone.com/ if you + * need additional information or have any questions. + */ +package com.codename1.impl.javase; + + +import com.jogamp.opengl.GLAutoDrawable; +import com.jogamp.opengl.GLCapabilities; +import com.jogamp.opengl.GLDrawableFactory; +import com.jogamp.opengl.GLEventListener; +import com.jogamp.opengl.GLOffscreenAutoDrawable; +import com.jogamp.opengl.GLProfile; +import com.jogamp.opengl.util.awt.AWTGLReadBufferUtil; +import java.awt.image.BufferedImage; + +/// The fold renderer's GPU target: an offscreen OpenGL framebuffer driven +/// through `JavaSEGLDevice`, read back into an image after every frame. +/// +/// A frame of the folding device is two textured panels, a few walls and a +/// spine; the software rasterizer spent a quarter of a second on one at +/// retina size, the GPU a few milliseconds (most of it the read back). +/// +/// Like `JavaSEJoglSurface` it touches JOGL, so the port loads it +/// reflectively and keeps the software target when it is missing or GL is +/// unusable. Frames are rendered synchronously on the calling thread. +final class JavaSEJoglFoldTarget implements FoldRenderer.Target { + private final JavaSEGLDevice device = new JavaSEGLDevice(); + private final GLOffscreenAutoDrawable drawable; + private final AWTGLReadBufferUtil readback; + private FoldRenderer.Painter pending; + private BufferedImage frame; + private RuntimeException failure; + private int lastW = 1; + private int lastH = 1; + + JavaSEJoglFoldTarget() { + // GL2 for the same reason JavaSEJoglSurface uses it: a 2.1 + // compatibility context on macOS accepts the shared shaders. + GLProfile profile = GLProfile.get(GLProfile.GL2); + GLCapabilities caps = new GLCapabilities(profile); + caps.setDepthBits(24); + caps.setAlphaBits(8); + caps.setOnscreen(false); + caps.setFBO(true); + drawable = GLDrawableFactory.getFactory(profile).createOffscreenAutoDrawable(null, caps, null, 1, 1); + drawable.addGLEventListener(new GLEventListener() { + public void init(GLAutoDrawable d) { + device.setGL(d.getGL().getGL2ES2()); + } + + public void reshape(GLAutoDrawable d, int x, int y, int w, int h) { + device.setGL(d.getGL().getGL2ES2()); + } + + public void display(GLAutoDrawable d) { + FoldRenderer.Painter p = pending; + if (p == null) { + return; + } + device.setGL(d.getGL().getGL2ES2()); + try { + device.setViewport(0, 0, lastW, lastH); + p.paint(device); + frame = readback.readPixelsToBufferedImage(d.getGL(), true); + } catch (RuntimeException e) { + failure = e; + } + } + + public void dispose(GLAutoDrawable d) { + device.setGL(d.getGL().getGL2ES2()); + device.disposePrograms(); + } + }); + readback = new AWTGLReadBufferUtil(profile, true); + // Realize the context now, so an unusable GL fails here and the port + // keeps the software target, never in the middle of a fold. + drawable.display(); + } + + public synchronized BufferedImage render(int width, int height, FoldRenderer.Painter painter) { + if (width != lastW || height != lastH) { + drawable.setSurfaceSize(width, height); + lastW = width; + lastH = height; + } + pending = painter; + failure = null; + frame = null; + try { + drawable.display(); + } finally { + pending = null; + } + if (failure != null) { + throw failure; + } + if (frame == null) { + throw new IllegalStateException("The GL fold frame was not rendered"); + } + return frame; + } + + public boolean isAccelerated() { + return true; + } +} diff --git a/Ports/JavaSE/src/com/codename1/impl/javase/JavaSEPort.java b/Ports/JavaSE/src/com/codename1/impl/javase/JavaSEPort.java index b55b4575d5e..f7d8ee946de 100644 --- a/Ports/JavaSE/src/com/codename1/impl/javase/JavaSEPort.java +++ b/Ports/JavaSE/src/com/codename1/impl/javase/JavaSEPort.java @@ -952,6 +952,9 @@ public static void setInvokePointerHover(boolean aInvokePointerHover) { private static final String[] BUNDLED_WATCH_SKINS = { "/AppleWatch45mm.skin", "/AppleWatch41mm.skin", "/WearRound.skin", "/WearSquare.skin" }; + /// Foldable skins shipped on the classpath (tools/foldable-skins), offered + /// in the skin menu beside the watch skins for the same reason. + private static final String[] BUNDLED_FOLDABLE_SKINS = {"/iPhoneDuo.skin"}; /// Adds the bundled watch skins that are actually on the classpath to the skin menu's list. /// @@ -969,6 +972,11 @@ private static void registerBundledWatchSkins(Preferences pref) { } merged = SkinPath.append(merged, skin); } + for (String skin : BUNDLED_FOLDABLE_SKINS) { + if (JavaSEPort.class.getResource(skin) != null) { + merged = SkinPath.append(merged, skin); + } + } if (!merged.equals(skinNames)) { pref.put("skins", merged); } @@ -3776,6 +3784,18 @@ public boolean isMetaKeyDown() { private static int simulatedPosture = com.codename1.ui.DevicePosture.POSTURE_FLAT; private static int simulatedFoldOrientation = com.codename1.ui.DevicePosture.FOLD_ORIENTATION_VERTICAL; private static int simulatedHingeAngle = 180; + // The loaded skin's foldable description, or null for an ordinary skin. + private static FoldableSkin foldableSkin; + + static String postureName(int posture) { + if (posture == com.codename1.ui.DevicePosture.POSTURE_CLOSED) { + return FoldableSkin.POSTURE_CLOSED; + } + if (posture == com.codename1.ui.DevicePosture.POSTURE_HALF_OPENED) { + return FoldableSkin.POSTURE_HALF; + } + return FoldableSkin.POSTURE_FLAT; + } /// Enables or disables foldable device simulation in the desktop simulator. public static void setSimulatedFoldable(boolean foldable) { @@ -3785,6 +3805,12 @@ public static void setSimulatedFoldable(boolean foldable) { /// Sets the simulated fold posture (a `com.codename1.ui.DevicePosture` POSTURE_* constant). public static void setSimulatedPosture(int posture) { + if (foldableSkin != null && instance != null) { + // A foldable skin folds for real: the hinge animates and the + // display moves to the cover screen when closed. + instance.setFoldStateAndWait(postureName(posture), -1, -1); + return; + } simulatedPosture = posture; Display.getInstance().postureChanged(); } @@ -3797,10 +3823,744 @@ public static void setSimulatedFoldOrientation(int orientation) { /// Sets the simulated hinge angle in degrees (0 closed, 180 flat). public static void setSimulatedHingeAngle(int angle) { + if (foldableSkin != null && instance != null) { + instance.setFoldStateAndWait(null, Math.max(0, angle), -1); + return; + } simulatedHingeAngle = angle; Display.getInstance().postureChanged(); } + // ---- Foldable skins ------------------------------------------------- + // A skin with `foldable=true` (see FoldableSkin) is folded by a + // FoldController, drawn by a FoldRenderer while it is not flat, and swaps + // the whole display to its cover screen while fully closed. + + private BufferedImage coverPortraitSkin; + private BufferedImage coverLandscapeSkin; + private java.util.List coverCutoutsPortrait; + /// The inner display's skin state while the cover display is shown, or + /// null while the inner display is shown. + private Object[] innerSkinState; + private FoldController foldController; + private FoldRenderer foldRenderer; + private javax.swing.Timer foldTimer; + private BufferedImage foldComposite; + private BufferedImage coverComposite; + private boolean coverCompositePortrait; + private long foldAnimationMs = FoldController.DEFAULT_DURATION_MS; + + /// The loaded skin's foldable description, or null. + FoldableSkin getFoldableSkin() { + return foldableSkin; + } + + synchronized FoldController getFoldController() { + if (foldController == null) { + foldController = new FoldController(new FoldController.Host() { + public void angleChanged(int angle, boolean animating) { + simulatedHingeAngle = angle; + if (canvas != null) { + canvas.repaint(); + } + } + + public void postureChanged(int posture) { + simulatedPosture = posture; + Display d = Display.getInstance(); + if (d.isInitialized()) { + d.postureChanged(); + } + } + + public void coverChanged(boolean closed) { + showCoverDisplay(closed); + } + }); + } + return foldController; + } + + /// Folds the loaded foldable skin to a named posture (`flat`, `half`, + /// `closed`) or to `angle` degrees when it is not negative. Animates over + /// `animateMs` milliseconds (negative: the simulator default, 0: at once). + /// Must be called on the AWT event thread. + public void setFoldState(String posture, int angle, long animateMs) { + if (foldableSkin == null) { + throw new IllegalStateException("The current skin is not foldable"); + } + int target = angle >= 0 ? Math.min(180, angle) : FoldableSkin.angleForPosture(posture); + long ms = animateMs < 0 ? foldAnimationMs : animateMs; + FoldController fc = getFoldController(); + fc.moveTo(target, ms, AnimationClock.now()); + if (fc.isAnimating()) { + startFoldAnimation(); + } else if (canvas != null) { + canvas.repaint(); + } + } + + /// The same as setFoldState, from any thread; blocks until applied. + public void setFoldStateAndWait(final String posture, final int angle, final long animateMs) { + runOnAwtAndWait(new Runnable() { + public void run() { + setFoldState(posture, angle, animateMs); + } + }); + } + + private static void runOnAwtAndWait(Runnable r) { + if (EventQueue.isDispatchThread()) { + r.run(); + return; + } + try { + EventQueue.invokeAndWait(r); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + } catch (java.lang.reflect.InvocationTargetException e) { + Throwable c = e.getCause(); + if (c instanceof RuntimeException) { + throw (RuntimeException) c; + } + throw new IllegalStateException(c); + } + } + + private void startFoldAnimation() { + if (foldTimer == null) { + foldTimer = new javax.swing.Timer(16, new ActionListener() { + public void actionPerformed(ActionEvent e) { + FoldController fc = foldController; + if (fc == null || !fc.step(AnimationClock.now())) { + stopFoldAnimation(); + } + } + }); + foldTimer.setCoalesce(true); + } + foldTimer.start(); + } + + private void stopFoldAnimation() { + if (foldTimer != null) { + foldTimer.stop(); + } + } + + /// True while the canvas draws the device folded in 3D instead of flat. + boolean isFoldVisualActive() { + FoldController fc = foldController; + return foldableSkin != null && innerSkinState == null && fc != null + && (fc.isAnimating() || fc.getExactAngle() < 179.5); + } + + /// Pointer input is dropped while the hinge moves: the screen is not + /// where the flat hit test thinks it is. At rest part way open it is + /// delivered as if flat, which the fixed half matches exactly. + boolean isFoldInputBlocked() { + FoldController fc = foldController; + return isRotationAnimating() || foldableSkin != null && fc != null && fc.isAnimating(); + } + + // ---- 3D rotation ---------------------------------------------------- + // Turning between portrait and landscape is shown as the device itself + // turning: the last portrait (or landscape) frame, as a solid device, + // rolls a quarter turn while lifted toward the viewer, then cross fades + // into the application's first frame in the new orientation. + + static final long ROTATION_MS = 650; + private BufferedImage rotationFrom; + private long rotationStart; + private boolean rotationClockwise; + /// The scale the turn renders at, relative to skin pixels. + private double rotationScale = 1; + /// The hinge angle the turn started at (180 when flat), and the fold + /// it is drawn with when less. + private double rotationFoldAngle = 180; + private boolean rotationVertical; + private Rectangle rotationHinge; + private BufferedImage rotationBack; + private javax.swing.Timer rotationTimer; + /// The last frame the canvas drew in 3D and its zoom, reused by the + /// recorder so a frame is never rendered twice. + private BufferedImage lastAnimatedFrame; + private double lastAnimatedZoom = 1; + + boolean isRotationAnimating() { + return rotationFrom != null && AnimationClock.now() - rotationStart < ROTATION_MS; + } + + /// The longest side, in raster pixels, the device is rendered at in 3D + /// while it moves. A retina window would otherwise rasterize 2600 pixel + /// frames in software and fall to a handful a second; moving, a softer + /// frame scaled up by the window is invisible, a dropped one is not. + /// At rest (a device left half open) it renders sharp, at full size. + static final int MOTION_MAX_SIDE = 1100; + + /// The fold renderer on the GPU when JOGL loads, else in software. + /// `-Dcn1.fold.software=true` forces software. + private static FoldRenderer createFoldRenderer() { + if (!Boolean.getBoolean("cn1.fold.software")) { + try { + Class c = Class.forName("com.codename1.impl.javase.JavaSEJoglFoldTarget"); + java.lang.reflect.Constructor ctor = c.getDeclaredConstructor(); + ctor.setAccessible(true); + return new FoldRenderer((FoldRenderer.Target) ctor.newInstance()); + } catch (Throwable t) { + Throwable cause = t instanceof java.lang.reflect.InvocationTargetException + && t.getCause() != null ? t.getCause() : t; + System.out.println("Simulator fold: GPU unavailable, rendering in software (" + cause + ")"); + } + } + return new FoldRenderer(); + } + + double motionScale(double zoom, boolean moving) { + BufferedImage skin = getSkin(); + FoldRenderer r; + synchronized (this) { + if (foldRenderer == null) { + foldRenderer = createFoldRenderer(); + } + r = foldRenderer; + } + // Capped on the GPU too: a retina-sized fold still cost ~60 ms a + // frame there, mostly texture upload and readback, against ~13 ms at + // this size. The resting device is always drawn at full resolution. + if (!moving || skin == null || r == null) { + return 1; + } + return Math.min(1, MOTION_MAX_SIDE / (Math.max(skin.getWidth(), skin.getHeight()) * zoom)); + } + + /// The device as drawn now (skin over screen) at `scale`, or null. + private BufferedImage snapshotSkinFrame(double scale) { + BufferedImage skin = getSkin(); + Rectangle sc = getScreenCoordinates(); + if (skin == null || sc == null || canvas == null) { + return null; + } + BufferedImage screen = canvas.presentedFrame(); + if (screen == null) { + screen = canvas.buffer; + } + if (screen == null) { + return null; + } + int w = Math.max(1, (int) (skin.getWidth() * scale)); + int h = Math.max(1, (int) (skin.getHeight() * scale)); + BufferedImage out = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB); + Graphics2D g = out.createGraphics(); + g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); + g.drawImage(screen, (int) (sc.x * scale), (int) (sc.y * scale), + (int) (sc.width * scale), (int) (sc.height * scale), null); + g.drawImage(skin, 0, 0, w, h, null); + g.dispose(); + return out; + } + + /// Called before the orientation flips: keeps the outgoing frame. + private void beginRotationAnimation() { + rotationFrom = null; + if (foldAnimationMs <= 0) { + return; + } + rotationScale = zoomLevel * motionScale(zoomLevel, true); + rotationFrom = snapshotSkinFrame(rotationScale); + // Landscape skins are the portrait art turned clockwise. + rotationClockwise = portrait; + // A device held part way folded turns folded. + rotationFoldAngle = 180; + rotationHinge = null; + rotationBack = null; + if (foldableSkin != null && foldController != null && innerSkinState == null) { + double a = foldController.getExactAngle(); + Rectangle sc = getScreenCoordinates(); + if (a < 179.5 && sc != null) { + rotationFoldAngle = a; + rotationVertical = foldableSkin.isVertical() == portrait; + Rectangle hinge = foldableSkin.getHinge(portrait); + if (hinge == null) { + hinge = rotationVertical + ? new Rectangle(sc.x + sc.width / 2, sc.y, 0, sc.height) + : new Rectangle(sc.x, sc.y + sc.height / 2, sc.width, 0); + } + rotationHinge = new Rectangle((int) (hinge.x * rotationScale), (int) (hinge.y * rotationScale), + (int) (hinge.width * rotationScale), (int) (hinge.height * rotationScale)); + rotationBack = coverBackImage(); + } + } + } + + /// Called after the orientation flipped and the canvas took its new size. + private void startRotationAnimation() { + if (rotationFrom == null) { + return; + } + rotationStart = AnimationClock.now(); + installRotationOverlay(); + if (rotationTimer == null) { + rotationTimer = new javax.swing.Timer(16, new ActionListener() { + public void actionPerformed(ActionEvent e) { + if (!isRotationAnimating()) { + rotationTimer.stop(); + rotationFrom = null; + lastAnimatedFrame = null; + removeRotationOverlay(); + } else if (rotationOverlay != null && rotationOverlay.getParent() != null) { + rotationOverlay.repaint(); + } + if (canvas != null) { + canvas.repaint(); + } + } + }); + rotationTimer.setCoalesce(true); + } + rotationTimer.start(); + } + + /// Draws the turning device above the whole window. The canvas has + /// already taken the new orientation's shape, so inside it the device + /// would have to shrink to fit at the start of the turn (or the end); + /// over the window it keeps its size and only the window clips it. + private javax.swing.JComponent rotationOverlay; + + private void installRotationOverlay() { + if (canvas == null) { + return; + } + javax.swing.JRootPane rp = SwingUtilities.getRootPane(canvas); + if (rp == null) { + return; + } + javax.swing.JLayeredPane lp = rp.getLayeredPane(); + if (rotationOverlay == null) { + rotationOverlay = new javax.swing.JComponent() { + @Override + protected void paintComponent(java.awt.Graphics g) { + paintRotationOverlay((Graphics2D) g, this); + } + }; + rotationOverlay.setOpaque(false); + } + if (rotationOverlay.getParent() != lp) { + lp.add(rotationOverlay, javax.swing.JLayeredPane.DRAG_LAYER); + } + rotationOverlay.setBounds(0, 0, lp.getWidth(), lp.getHeight()); + rotationOverlay.repaint(); + } + + private void removeRotationOverlay() { + javax.swing.JComponent o = rotationOverlay; + if (o != null && o.getParent() != null) { + java.awt.Container p = o.getParent(); + p.remove(o); + p.repaint(); + } + } + + /// The device in its new orientation, as the turn fades into it: folded + /// like the turning device when it was folded. + private BufferedImage rotationIncoming() { + if (rotationFoldAngle < 179.5 && foldableSkin != null && canvas != null) { + BufferedImage scr = canvas.presentedFrame(); + if (scr == null) { + scr = canvas.buffer; + } + if (scr != null) { + BufferedImage f = renderFoldedSkin(scr, rotationScale); + if (f != null) { + return f; + } + } + } + return snapshotSkinFrame(rotationScale); + } + + /// True while the overlay, not the canvas, draws the device. + boolean isRotationOnOverlay() { + javax.swing.JComponent o = rotationOverlay; + return o != null && o.getParent() != null && isRotationAnimating(); + } + + private void paintRotationOverlay(Graphics2D g, javax.swing.JComponent overlay) { + BufferedImage skin = getSkin(); + if (canvas == null || skin == null || !isRotationAnimating()) { + return; + } + double cs = canvas.canvasScale(); + int side = (int) Math.ceil(Math.max(skin.getWidth(), skin.getHeight()) * zoomLevel); + int small = (int) Math.ceil(Math.max(skin.getWidth(), skin.getHeight()) * rotationScale); + BufferedImage turned = renderRotationFrame(rotationIncoming(), small, small); + if (turned == null) { + return; + } + lastAnimatedFrame = turned; + lastAnimatedZoom = rotationScale; + java.awt.Point origin = SwingUtilities.convertPoint(canvas, 0, 0, overlay); + // The canvas draws the skin at (x, y) * zoom in raster pixels. + double cx = origin.x * cs + (canvas.x + skin.getWidth() / 2.0) * zoomLevel; + double cy = origin.y * cs + (canvas.y + skin.getHeight() / 2.0) * zoomLevel; + AffineTransform t = g.getTransform(); + g.scale(1 / cs, 1 / cs); + g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); + g.drawImage(turned, (int) Math.round(cx - side / 2.0), (int) Math.round(cy - side / 2.0), side, side, null); + g.setTransform(t); + } + + /// One frame of the rotation, `incoming` (the new orientation, already + /// composited at `scale`) faded in over its last third. + BufferedImage renderRotationFrame(BufferedImage incoming) { + return incoming == null ? null + : renderRotationFrame(incoming, incoming.getWidth(), incoming.getHeight()); + } + + /// As above, centered in an `outW x outH` frame: a square as large as + /// the device's longer side holds it at its own size in both + /// orientations, so it neither shrinks nor clips as it turns. + BufferedImage renderRotationFrame(BufferedImage incoming, int outW, int outH) { + BufferedImage from = rotationFrom; + if (from == null || incoming == null) { + return null; + } + double f = Math.min(1, (AnimationClock.now() - rotationStart) / (double) ROTATION_MS); + double e = f * f * f * (f * (f * 6 - 15) + 10); + BufferedImage out; + synchronized (this) { + if (foldRenderer == null) { + foldRenderer = createFoldRenderer(); + } + double turn = rotationClockwise ? -90 : 90; + double lift = Math.sin(Math.PI * e); + BufferedImage r; + if (e < 0.5) { + r = foldRenderer.renderRotation(from, rotationBack, rotationHinge, rotationVertical, + rotationFoldAngle, turn * e, lift, outW, outH); + } else { + // Past half way the device shows the new orientation's + // content, turned back by what is left of the turn, the way + // a phone lays itself out again mid-turn. At exactly half + // way both outlines lie at 45 degrees and coincide, so the + // swap is a change of content, never a jump; a cross fade + // over the still-turning device showed both at once. + BufferedImage back = rotationBack; + Rectangle hinge = null; + boolean vertical = true; + if (rotationFoldAngle < 179.5 && foldableSkin != null) { + vertical = foldableSkin.isVertical() == portrait; + Rectangle sc = getScreenCoordinates(); + Rectangle hg = foldableSkin.getHinge(portrait); + if (hg == null && sc != null) { + hg = vertical ? new Rectangle(sc.x + sc.width / 2, sc.y, 0, sc.height) + : new Rectangle(sc.x, sc.y + sc.height / 2, sc.width, 0); + } + if (hg != null) { + hinge = new Rectangle((int) (hg.x * rotationScale), (int) (hg.y * rotationScale), + (int) (hg.width * rotationScale), (int) (hg.height * rotationScale)); + } + back = coverBackImage(); + } + BufferedImage flat = hinge != null ? snapshotSkinFrame(rotationScale) : incoming; + r = foldRenderer.renderRotation(flat != null ? flat : incoming, back, hinge, vertical, + rotationFoldAngle, turn * (e - 1), lift, outW, outH); + } + out = new BufferedImage(r.getWidth(), r.getHeight(), BufferedImage.TYPE_INT_ARGB); + Graphics2D g = out.createGraphics(); + g.drawImage(r, 0, 0, null); + g.dispose(); + } + return out; + } + + /// Composites `screen` (already at `scale`) into the skin and renders the + /// result folded at the current angle. Returns null when there is + /// nothing to fold. + BufferedImage renderFoldedSkin(BufferedImage screen, double scale) { + BufferedImage skin = getSkin(); + Rectangle sc = getScreenCoordinates(); + if (skin == null || sc == null || foldableSkin == null) { + return null; + } + int w = Math.max(1, (int) (skin.getWidth() * scale)); + int h = Math.max(1, (int) (skin.getHeight() * scale)); + if (foldComposite == null || foldComposite.getWidth() != w || foldComposite.getHeight() != h) { + foldComposite = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB); + } + Graphics2D g = foldComposite.createGraphics(); + g.setComposite(java.awt.AlphaComposite.Clear); + g.fillRect(0, 0, w, h); + g.setComposite(java.awt.AlphaComposite.SrcOver); + g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); + g.drawImage(screen, (int) (sc.x * scale), (int) (sc.y * scale), + (int) (sc.width * scale), (int) (sc.height * scale), null); + g.drawImage(skin, 0, 0, w, h, null); + g.dispose(); + boolean verticalAxis = foldableSkin.isVertical() == portrait; + Rectangle hinge = foldableSkin.getHinge(portrait); + if (hinge == null) { + hinge = verticalAxis + ? new Rectangle(sc.x + sc.width / 2, sc.y, 0, sc.height) + : new Rectangle(sc.x, sc.y + sc.height / 2, sc.width, 0); + } + Rectangle scaledHinge = new Rectangle((int) (hinge.x * scale), (int) (hinge.y * scale), + (int) (hinge.width * scale), (int) (hinge.height * scale)); + synchronized (this) { + if (foldRenderer == null) { + foldRenderer = createFoldRenderer(); + } + BufferedImage out = foldRenderer.render(foldComposite, coverBackImage(), scaledHinge, + verticalAxis, foldController.getExactAngle()); + // The renderer reuses its image; hand out a copy so the caller + // (the canvas, or the recorder on its own thread) keeps a stable frame. + BufferedImage copy = new BufferedImage(out.getWidth(), out.getHeight(), BufferedImage.TYPE_INT_ARGB); + Graphics2D cg = copy.createGraphics(); + cg.drawImage(out, 0, 0, null); + cg.dispose(); + return copy; + } + } + + /// The outside of the swinging panel: the cover skin with a dark screen. + private BufferedImage coverBackImage() { + if (foldableSkin == null || !foldableSkin.hasCover() || coverPortraitSkin == null) { + return null; + } + if (coverComposite == null || coverCompositePortrait != portrait) { + BufferedImage img = coverSkinImage(portrait); + Rectangle cs = coverScreen(portrait); + BufferedImage out = new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_INT_ARGB); + Graphics2D g = out.createGraphics(); + g.setColor(new Color(0x101014)); + g.fillRect(cs.x, cs.y, cs.width, cs.height); + g.drawImage(img, 0, 0, null); + g.dispose(); + coverComposite = out; + coverCompositePortrait = portrait; + } + return coverComposite; + } + + private BufferedImage coverSkinImage(boolean portraitValue) { + if (portraitValue || coverLandscapeSkin != null) { + return portraitValue ? coverPortraitSkin : coverLandscapeSkin; + } + return rotateImageClockwise(coverPortraitSkin); + } + + /// The cover display in its skin image. Without landscape keys the + /// portrait rectangle is turned with the image, clockwise as every + /// landscape skin is (see rotateCutoutsForLandscape): x' = H - y - h, y' = x. + private Rectangle coverScreen(boolean portraitValue) { + Rectangle ps = foldableSkin.getCoverScreen(true); + if (portraitValue) { + return ps; + } + if (foldableSkin.hasCoverLandscape()) { + return foldableSkin.getCoverScreen(false); + } + return new Rectangle(coverPortraitSkin.getHeight() - ps.y - ps.height, ps.x, ps.height, ps.width); + } + + private static BufferedImage rotateImageClockwise(BufferedImage src) { + BufferedImage out = new BufferedImage(src.getHeight(), src.getWidth(), BufferedImage.TYPE_INT_ARGB); + Graphics2D g = out.createGraphics(); + g.translate(src.getHeight(), 0); + g.rotate(Math.PI / 2); + g.drawImage(src, 0, 0, null); + g.dispose(); + return out; + } + + /// Moves the whole display to the cover screen (true) or back to the + /// inner one: skin images, screen rectangles, density, safe area and + /// cutouts, then the same relayout and size change a rotation does. + /// The safe area of a cover display: the screen minus a band along + /// whichever edge the cutouts sit nearest, plus a small margin. Null + /// without cutouts, which reads as the whole screen. + static Rectangle coverSafeArea(java.util.List cutouts, int w, int h) { + if (cutouts == null || cutouts.isEmpty()) { + return null; + } + int top = 0; + int left = 0; + int right = 0; + int bottom = 0; + for (Rectangle c : cutouts) { + int dTop = c.y; + int dBottom = h - c.y - c.height; + int dLeft = c.x; + int dRight = w - c.x - c.width; + int min = Math.min(Math.min(dTop, dBottom), Math.min(dLeft, dRight)); + int margin = Math.max(c.width, c.height) / 2; + if (min == dTop) { + top = Math.max(top, c.y + c.height + margin); + } else if (min == dBottom) { + bottom = Math.max(bottom, h - c.y + margin); + } else if (min == dLeft) { + left = Math.max(left, c.x + c.width + margin); + } else { + right = Math.max(right, w - c.x + margin); + } + } + return new Rectangle(left, top, Math.max(1, w - left - right), Math.max(1, h - top - bottom)); + } + + private void showCoverDisplay(boolean cover) { + if (foldableSkin == null || !foldableSkin.hasCover() || coverPortraitSkin == null + || cover == (innerSkinState != null)) { + return; + } + if (cover) { + innerSkinState = new Object[]{portraitSkin, landscapeSkin, portraitScreenCoordinates, + landscapeScreenCoordinates, safeAreaPortrait, safeAreaLandscape, cutoutsPortrait, + cutoutsLandscape, pixelMilliRatio, portraitSkinHotspots, landscapeSkinHotspots, + Boolean.valueOf(roundedSkin)}; + portraitSkin = coverSkinImage(true); + landscapeSkin = coverSkinImage(false); + portraitScreenCoordinates = coverScreen(true); + landscapeScreenCoordinates = coverScreen(false); + cutoutsPortrait = coverCutoutsPortrait; + cutoutsLandscape = coverCutoutsPortrait == null ? null + : rotateCutoutsForLandscape(coverCutoutsPortrait, portraitScreenCoordinates.height); + // The cover display has its own camera; without a safe area the + // app draws its title straight under it. + safeAreaPortrait = coverSafeArea(cutoutsPortrait, portraitScreenCoordinates.width, + portraitScreenCoordinates.height); + safeAreaLandscape = coverSafeArea(cutoutsLandscape, landscapeScreenCoordinates.width, + landscapeScreenCoordinates.height); + if (foldableSkin.getCoverPpi() > 0) { + pixelMilliRatio = foldableSkin.getCoverPpi() / 25.4; + } + portraitSkinHotspots = new HashMap(); + landscapeSkinHotspots = new HashMap(); + // A round-screen skin paints its frame over the UI, which is what + // rounds the screen corners; the cover keeps that, with a whole + // screen safe area when it has no cutouts. + if (roundedSkin) { + if (safeAreaPortrait == null) { + safeAreaPortrait = new Rectangle(0, 0, portraitScreenCoordinates.width, + portraitScreenCoordinates.height); + } + if (safeAreaLandscape == null) { + safeAreaLandscape = new Rectangle(0, 0, landscapeScreenCoordinates.width, + landscapeScreenCoordinates.height); + } + } + } else { + Object[] s = innerSkinState; + innerSkinState = null; + portraitSkin = (BufferedImage) s[0]; + landscapeSkin = (BufferedImage) s[1]; + portraitScreenCoordinates = (Rectangle) s[2]; + landscapeScreenCoordinates = (Rectangle) s[3]; + safeAreaPortrait = (Rectangle) s[4]; + safeAreaLandscape = (Rectangle) s[5]; + cutoutsPortrait = castRectangles(s[6]); + cutoutsLandscape = castRectangles(s[7]); + pixelMilliRatio = (Double) s[8]; + portraitSkinHotspots = castHotspots(s[9]); + landscapeSkinHotspots = castHotspots(s[10]); + roundedSkin = ((Boolean) s[11]).booleanValue(); + } + dismissSimulatedKeyboard(); + relayoutSkinCanvas(); + } + + @SuppressWarnings("unchecked") + private static java.util.List castRectangles(Object o) { + return (java.util.List) o; + } + + @SuppressWarnings("unchecked") + private static Map castHotspots(Object o) { + return (Map) o; + } + + /// Resizes the canvas and window to the current skin and tells the app + /// its display changed, as the Rotate menu item does. + private void relayoutSkinCanvas() { + if (canvas == null || getSkin() == null) { + return; + } + Container parent = canvas.getParent(); + if (parent == null) { + canvas.repaint(); + JavaSEPort.this.sizeChanged(getScreenCoordinates().width, getScreenCoordinates().height); + return; + } + parent.remove(canvas); + java.awt.Dimension displaySize = computeSkinDisplaySize(); + canvas.setForcedSize(displaySize); + if (appFrame == null && window != null) { + window.setSize(displaySize); + } + java.awt.Container top = ((JComponent) parent).getTopLevelAncestor(); + if (top != null) { + top.revalidate(); + top.repaint(); + } + parent.add(BorderLayout.CENTER, canvas); + if (appFrame == null && window != null) { + window.pack(); + } + JavaSEPort.this.sizeChanged(getScreenCoordinates().width, getScreenCoordinates().height); + } + + /// A faint crease down the flat inner screen, for skins that ask for one. + private void paintFoldCrease(Graphics2D g, int x, int y) { + if (foldableSkin == null || !foldableSkin.hasCrease() || innerSkinState != null + || foldableSkin.isDualScreen()) { + return; + } + Rectangle sc = getScreenCoordinates(); + boolean verticalAxis = foldableSkin.isVertical() == portrait; + Rectangle hinge = foldableSkin.getHinge(portrait); + double c = verticalAxis + ? (hinge != null ? hinge.x + hinge.width / 2.0 : sc.x + sc.width / 2.0) + : (hinge != null ? hinge.y + hinge.height / 2.0 : sc.y + sc.height / 2.0); + int band = Math.max(2, (int) (6 * zoomLevel)); + Graphics2D g2 = (Graphics2D) g.create(); + try { + for (int i = -band; i <= band; i++) { + int a = (int) (22 * (1 - Math.abs(i) / (double) (band + 1))); + g2.setColor(new Color(0, 0, 0, a)); + if (verticalAxis) { + int px = (int) ((c + x) * zoomLevel) + i; + g2.drawLine(px, (int) ((sc.y + y) * zoomLevel), px, (int) ((sc.y + sc.height + y) * zoomLevel)); + } else { + int py = (int) ((c + y) * zoomLevel) + i; + g2.drawLine((int) ((sc.x + x) * zoomLevel), py, (int) ((sc.x + sc.width + x) * zoomLevel), py); + } + } + } finally { + g2.dispose(); + } + } + + /// The size every recorded frame of a foldable skin has: large enough for + /// the inner and the cover display, so a recording that folds keeps one + /// video size (a video stream cannot change size part way). + java.awt.Dimension foldableRecordingSize(com.codename1.impl.javase.record.RecordingOptions.Frame frame) { + if (foldableSkin == null || !foldableSkin.hasCover() || coverPortraitSkin == null) { + return null; + } + boolean skinFrame = frame == com.codename1.impl.javase.record.RecordingOptions.Frame.SKIN; + BufferedImage innerSkin = innerSkinState != null + ? (BufferedImage) innerSkinState[portrait ? 0 : 1] : getSkin(); + Rectangle innerScreen = innerSkinState != null + ? (Rectangle) innerSkinState[portrait ? 2 : 3] : getScreenCoordinates(); + BufferedImage coverSkin = coverSkinImage(portrait); + Rectangle coverScr = coverScreen(portrait); + if (innerSkin == null || innerScreen == null) { + return null; + } + int w = skinFrame ? Math.max(innerSkin.getWidth(), coverSkin.getWidth()) : Math.max(innerScreen.width, coverScr.width); + int h = skinFrame ? Math.max(innerSkin.getHeight(), coverSkin.getHeight()) : Math.max(innerScreen.height, coverScr.height); + return new java.awt.Dimension(w, h); + } + @Override public boolean isFoldable() { return simulatedFoldable; @@ -3823,6 +4583,10 @@ public int getFoldOrientation() { @Override public boolean isPostureSeparating() { + if (foldableSkin != null && foldableSkin.isDualScreen()) { + // Two panels with a gap between them always separate the content. + return simulatedPosture != com.codename1.ui.DevicePosture.POSTURE_CLOSED; + } return simulatedFoldable && simulatedPosture == com.codename1.ui.DevicePosture.POSTURE_HALF_OPENED; } @@ -3833,6 +4597,14 @@ public com.codename1.ui.geom.Rectangle getFoldBounds(com.codename1.ui.geom.Recta } int w = getDisplayWidthImpl(); int h = getDisplayHeightImpl(); + if (foldableSkin != null) { + if (simulatedPosture == com.codename1.ui.DevicePosture.POSTURE_CLOSED) { + // Closed, the app is on the cover display, which has no fold. + return null; + } + java.awt.Rectangle b = foldableSkin.foldBounds(portrait, getScreenCoordinates(), w, h); + return new com.codename1.ui.geom.Rectangle(b.x, b.y, b.width, b.height); + } if (simulatedFoldOrientation == com.codename1.ui.DevicePosture.FOLD_ORIENTATION_VERTICAL) { return new com.codename1.ui.geom.Rectangle(w / 2 - 1, 0, 2, h); } @@ -4625,10 +5397,48 @@ private boolean drawScreenBuffer(java.awt.Graphics g) { bg.dispose(); } + boolean foldedPaint = false; if (isEnabled()) { - g.drawImage(composeSimulatorOverlays(accessibilityFilteredImage(buffer), false), - (int) ((getScreenCoordinates().getX() + x) * zoomLevel), - (int) ((getScreenCoordinates().getY() + y) * zoomLevel), this); + BufferedImage presented = composeSimulatorOverlays(accessibilityFilteredImage(buffer), false); + if (isRotationOnOverlay()) { + // The overlay draws the device; the canvas shows only its backdrop. + java.awt.Container bgOwner = getParent(); + g.setColor(bgOwner != null ? bgOwner.getBackground() : Color.GRAY); + g.fillRect(0, 0, (int) Math.ceil(getWidth() * canvasScale()) + 1, + (int) Math.ceil(getHeight() * canvasScale()) + 1); + foldedPaint = true; + } else if (isRotationAnimating()) { + BufferedImage turned = renderRotationFrame(rotationIncoming()); + if (turned != null) { + double up = zoomLevel / rotationScale; + g.drawImage(turned, (int) (x * zoomLevel), (int) (y * zoomLevel), + (int) (turned.getWidth() * up), (int) (turned.getHeight() * up), null); + lastAnimatedFrame = turned; + lastAnimatedZoom = rotationScale; + foldedPaint = true; + } + } + if (!foldedPaint && isFoldVisualActive()) { + // The skin and screen are drawn together, folded in 3D. + double q = motionScale(zoomLevel, isFoldInputBlocked()); + BufferedImage folded = renderFoldedSkin(presented, zoomLevel * q); + lastAnimatedFrame = folded; + lastAnimatedZoom = zoomLevel * q; + if (folded != null) { + BufferedImage sk = getSkin(); + ((Graphics2D) g).setRenderingHint(RenderingHints.KEY_INTERPOLATION, + RenderingHints.VALUE_INTERPOLATION_BILINEAR); + g.drawImage(folded, (int) (x * zoomLevel), (int) (y * zoomLevel), + (int) (sk.getWidth() * zoomLevel), (int) (sk.getHeight() * zoomLevel), null); + foldedPaint = true; + } + } + if (!foldedPaint) { + g.drawImage(presented, + (int) ((getScreenCoordinates().getX() + x) * zoomLevel), + (int) ((getScreenCoordinates().getY() + y) * zoomLevel), this); + paintFoldCrease((Graphics2D) g, x, y); + } } else { g.setColor(Color.WHITE); g.fillRect(x + (int) (getSkin().getWidth() * zoomLevel), y, getWidth(), getHeight()); @@ -4646,8 +5456,10 @@ private boolean drawScreenBuffer(java.awt.Graphics g) { } //updateGraphicsScale(g); BufferedImage skin = getSkin(); - g.drawImage(skin, (int) (x * zoomLevel), (int) (( y) * zoomLevel), - (int)(skin.getWidth() * zoomLevel), (int)(skin.getHeight() * zoomLevel), null); + if (!foldedPaint) { + g.drawImage(skin, (int) (x * zoomLevel), (int) (( y) * zoomLevel), + (int)(skin.getWidth() * zoomLevel), (int)(skin.getHeight() * zoomLevel), null); + } if (zoomLevel != 1) { AffineTransform t3 = ((Graphics2D)g).getTransform(); @@ -5097,6 +5909,10 @@ java.awt.Point screenToCanvas(int sx, int sy) { /// that follow are swallowed with it. private boolean pointerOnCutout; public void mousePressed(MouseEvent e) { + if (isFoldInputBlocked()) { + e.consume(); + return; + } if (e.isPopupTrigger()) { if (showContextMenu(e)) { return; @@ -5190,6 +6006,10 @@ public void mousePressed(MouseEvent e) { } public void mouseReleased(MouseEvent e) { + if (isFoldInputBlocked()) { + e.consume(); + return; + } if (e.isPopupTrigger()) { if (showContextMenu(e)) { @@ -5276,6 +6096,10 @@ public void run() { } } public void mouseDragged(MouseEvent e) { + if (isFoldInputBlocked()) { + e.consume(); + return; + } e.consume(); if (pointerOnCutout) { return; @@ -6128,6 +6952,11 @@ private void loadSkinFile(InputStream skin, final JFrame frm) { ZipInputStream z = new ZipInputStream(skin); ZipEntry e = z.getNextEntry(); final Properties props = new Properties(); + coverPortraitSkin = null; + coverLandscapeSkin = null; + innerSkinState = null; + coverComposite = null; + stopFoldAnimation(); BufferedImage map = null; BufferedImage landscapeMap = null; @@ -6163,6 +6992,16 @@ private void loadSkinFile(InputStream skin, final JFrame frm) { e = z.getNextEntry(); continue; } + if (name.equals("cover.png")) { + coverPortraitSkin = ImageIO.read(z); + e = z.getNextEntry(); + continue; + } + if (name.equals("cover_l.png")) { + coverLandscapeSkin = ImageIO.read(z); + e = z.getNextEntry(); + continue; + } if (name.equals("skin_map.png")) { map = ImageIO.read(z); e = z.getNextEntry(); @@ -6239,6 +7078,17 @@ private void loadSkinFile(InputStream skin, final JFrame frm) { landscapeSkinHotspots = new HashMap(); landscapeScreenCoordinates = new Rectangle(); boolean roundScreen = props.getProperty("roundScreen", "false").equalsIgnoreCase("true"); + foldableSkin = FoldableSkin.parse(props); + coverCutoutsPortrait = parseCutouts(props.getProperty("coverCutouts")); + if (foldableSkin != null) { + // A foldable skin always opens flat: the inner display is the + // one the rest of this method lays out. + simulatedFoldable = true; + simulatedFoldOrientation = foldableSkin.getFoldOrientation(); + simulatedPosture = com.codename1.ui.DevicePosture.POSTURE_FLAT; + simulatedHingeAngle = 180; + getFoldController().reset(180); + } // Every field below describes THIS skin. Clear them up front: // they used to be assigned only on the branches that had a value // for them, so loading a legacy skin after a skin designer one @@ -7204,6 +8054,7 @@ private void update() { @Override public void actionPerformed(ActionEvent e) { + beginRotationAnimation(); setPortrait(portraitValue); Container parent = canvas.getParent(); @@ -7225,6 +8076,7 @@ public void actionPerformed(ActionEvent e) { //zoomLevel = 1; JavaSEPort.this.sizeChanged(getScreenCoordinates().width, getScreenCoordinates().height); + startRotationAnimation(); } @@ -7632,6 +8484,7 @@ public void itemStateChanged(ItemEvent e) { rotateMenu.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent ae) { + beginRotationAnimation(); setPortrait(!portrait); Container parent = canvas.getParent(); parent.remove(canvas); @@ -7648,6 +8501,7 @@ public void actionPerformed(ActionEvent ae) { window.pack(); } JavaSEPort.this.sizeChanged(getScreenCoordinates().width, getScreenCoordinates().height); + startRotationAnimation(); } }); if (appFrame == null) deviceMenu.add(rotateMenu); @@ -10966,7 +11820,117 @@ public void actionPerformed(ActionEvent ae) { /// posture and fold orientation, and set the hinge angle. Selections are persisted and applied /// through the JavaSEPort simulated-posture state, which fires the same posture-change events an /// app would receive on a real foldable device. + private static String postureLabel(String posture) { + if (FoldableSkin.POSTURE_CLOSED.equals(posture)) { + return "Closed"; + } + if (FoldableSkin.POSTURE_HALF.equals(posture)) { + return "Half-opened"; + } + return "Flat (open)"; + } + + /// The Foldable menu for a foldable skin: the skin's own postures, a live + /// hinge slider, and whether changes animate. The skin decides the fold + /// orientation, so there is no orientation choice here. + private JMenu installFoldableSkinMenu(final Preferences pref) { + JMenu foldableMenu = new JMenu("Foldable"); + foldAnimationMs = pref.getBoolean("FoldableSim.animate", true) ? FoldController.DEFAULT_DURATION_MS : 0; + ButtonGroup group = new ButtonGroup(); + String current = postureName(simulatedPosture); + for (final String posture : foldableSkin.getPostures()) { + JRadioButtonMenuItem item = new JRadioButtonMenuItem(postureLabel(posture), posture.equals(current)); + item.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent ae) { + setFoldState(posture, -1, -1); + } + }); + group.add(item); + foldableMenu.add(item); + } + foldableMenu.addSeparator(); + JMenuItem hingeItem = new JMenuItem("Hinge angle..."); + hingeItem.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent ae) { + final JSlider slider = new JSlider(0, 180, getFoldController().getTargetAngle()); + slider.setMajorTickSpacing(30); + slider.setPaintTicks(true); + slider.setPaintLabels(true); + slider.addChangeListener(new javax.swing.event.ChangeListener() { + public void stateChanged(javax.swing.event.ChangeEvent e) { + // Follows the knob at once; the hinge tracks the hand. + setFoldState(null, slider.getValue(), 0); + } + }); + JOptionPane.showMessageDialog(canvas, slider, "Hinge angle (0 closed - 180 flat)", + JOptionPane.PLAIN_MESSAGE); + } + }); + foldableMenu.add(hingeItem); + final JCheckBoxMenuItem animate = new JCheckBoxMenuItem("Animate fold", foldAnimationMs > 0); + animate.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent ae) { + pref.putBoolean("FoldableSim.animate", animate.isSelected()); + foldAnimationMs = animate.isSelected() ? FoldController.DEFAULT_DURATION_MS : 0; + } + }); + foldableMenu.add(animate); + return foldableMenu; + } + + /// Toolbar button beside the rotate buttons: steps a foldable skin to its + /// next posture (flat, half-opened, closed, and around again). + public class FoldAction extends AbstractAction implements AppFrame.UpdatableUI { + public FoldAction() { + super("", foldActionIcon()); + putValue(SHORT_DESCRIPTION, "Fold or unfold the device"); + putValue(com.codename1.impl.javase.simulator.AppPanel.HIDE_WHEN_DISABLED, Boolean.TRUE); + update(); + } + + private void update() { + setEnabled(foldableSkin != null); + } + + @Override + public void actionPerformed(ActionEvent e) { + if (foldableSkin == null) { + return; + } + java.util.List postures = foldableSkin.getPostures(); + String current = postureName(FoldableSkin.postureForAngle(getFoldController().getTargetAngle())); + int i = postures.indexOf(current); + setFoldState(postures.get((i + 1) % postures.size()), -1, -1); + } + + @Override + public void onUpdateAppFrameUI(AppFrame frame) { + update(); + } + } + + /// A small drawn book-fold glyph, so the button needs no image resource. + private static javax.swing.Icon foldActionIcon() { + BufferedImage img = new BufferedImage(18, 18, BufferedImage.TYPE_INT_ARGB); + Graphics2D g = img.createGraphics(); + g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); + g.setColor(new Color(0x606060)); + g.setStroke(new java.awt.BasicStroke(1.5f)); + g.drawRect(2, 3, 6, 12); + int[] xs = {9, 16, 16, 9}; + int[] ys = {3, 1, 13, 15}; + g.drawPolygon(xs, ys, 4); + g.dispose(); + return new ImageIcon(img); + } + private JMenu installFoldableSimulationMenu(JMenu simulateMenu, final Preferences pref) { + if (foldableSkin != null) { + return installFoldableSkinMenu(pref); + } JMenu foldableMenu = new JMenu("Foldable"); // Apply any persisted selections to the simulator state up front. @@ -11826,6 +12790,9 @@ protected void decoratePanelWindow(AppPanel panel, Window window) { RotateAction landscapeAction = new RotateAction(false); canvasPanel.addAction(portraitAction); canvasPanel.addAction(landscapeAction); + FoldAction foldAction = new FoldAction(); + canvasPanel.addAction(foldAction); + appFrame.registerUpdateCallback(foldAction); ZoomAction zoomIn = new ZoomAction(true); ZoomAction zoomOut = new ZoomAction(false); canvasPanel.addAction(zoomIn); @@ -12092,7 +13059,14 @@ public void componentHidden(ComponentEvent e) { // It stays focusable, so a developer who clicks into it can still type. window.setAutoRequestFocus(false); } - window.setVisible(true); + if (Boolean.getBoolean("cn1.simulator.hidden")) { + // Laid out and displayable, so the app renders and every + // capture works, but never shown. + window.addNotify(); + window.validate(); + } else { + window.setVisible(true); + } } if (useNativeInput) { Display.getInstance().setDefaultVirtualKeyboard(null); @@ -12734,15 +13708,63 @@ public com.codename1.impl.javase.record.TouchOverlay getTouchOverlay() { return touchOverlay; } + /// What the recorder captures from. In a lockstep recording it also + /// moves the fold and the turn to each frame's moment and repaints them + /// there before the capture, so the captured 3D frame is the one for that + /// moment rather than whatever the window last managed to draw. + private final class RecorderSource implements com.codename1.impl.javase.record.SimulatorRecorder.FrameSource, + com.codename1.impl.javase.record.SimulatorRecorder.Timeline { + public BufferedImage captureFrame(com.codename1.impl.javase.record.RecordingOptions.Frame frame) { + return captureSimulatorFrame(frame); + } + + public void advanceTo(final long epochMs) { + AnimationClock.pin(epochMs); + runOnAwtAndWait(new Runnable() { + public void run() { + FoldController fc = foldController; + if (fc != null && fc.isAnimating() && !fc.step(epochMs)) { + stopFoldAnimation(); + } + if (canvas == null || !(isRotationAnimating() || isFoldVisualActive())) { + return; + } + // Rendered here, not by painting the window: a hidden + // simulator never paints, and the window keeps its own pace. + BufferedImage skin = getSkin(); + if (skin == null) { + return; + } + if (isRotationAnimating()) { + int small = (int) Math.ceil(Math.max(skin.getWidth(), skin.getHeight()) * rotationScale); + BufferedImage turned = renderRotationFrame(rotationIncoming(), small, small); + if (turned != null) { + lastAnimatedFrame = turned; + lastAnimatedZoom = rotationScale; + } + } else if (canvas.buffer != null) { + BufferedImage presented = composeSimulatorOverlays(accessibilityFilteredImage(canvas.buffer), false); + double q = motionScale(zoomLevel, isFoldInputBlocked()); + BufferedImage folded = renderFoldedSkin(presented, zoomLevel * q); + if (folded != null) { + lastAnimatedFrame = folded; + lastAnimatedZoom = zoomLevel * q; + } + } + } + }); + } + + public void release() { + AnimationClock.release(); + } + } + /// The screen recorder, created on first use. public synchronized com.codename1.impl.javase.record.SimulatorRecorder getSimulatorRecorder() { if (simulatorRecorder == null) { simulatorRecorder = new com.codename1.impl.javase.record.SimulatorRecorder( - new com.codename1.impl.javase.record.SimulatorRecorder.FrameSource() { - public BufferedImage captureFrame(com.codename1.impl.javase.record.RecordingOptions.Frame frame) { - return captureSimulatorFrame(frame); - } - }); + new RecorderSource()); } return simulatorRecorder; } @@ -12947,6 +13969,216 @@ public void actionPerformed(ActionEvent e) { /// A frame for the recorder: the screen with its overlays, and for /// [com.codename1.impl.javase.record.RecordingOptions.Frame#SKIN] the device skin /// around it, at the skin's own resolution. + /// A frame of a foldable skin: folded in 3D while the hinge is not flat, + /// otherwise the flat inner or cover display. + private BufferedImage captureFoldableFrame(com.codename1.impl.javase.record.RecordingOptions.Frame frame, + BufferedImage screen, Rectangle sc) { + boolean skinFrame = frame == com.codename1.impl.javase.record.RecordingOptions.Frame.SKIN; + if (skinFrame && isFoldVisualActive()) { + // renderFoldedSkin scales the screen into its rectangle itself. + BufferedImage folded = renderFoldedSkin(screen, 1.0); + if (folded != null) { + return folded; + } + } + BufferedImage skin = getSkin(); + if (skinFrame && skin != null) { + BufferedImage out = new BufferedImage(skin.getWidth(), skin.getHeight(), BufferedImage.TYPE_INT_ARGB); + Graphics2D g = out.createGraphics(); + g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); + g.drawImage(screen, sc.x, sc.y, sc.width, sc.height, null); + g.drawImage(skin, 0, 0, null); + g.dispose(); + return out; + } + BufferedImage out = new BufferedImage(sc.width, sc.height, BufferedImage.TYPE_INT_ARGB); + Graphics2D g = out.createGraphics(); + g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); + g.drawImage(screen, 0, 0, sc.width, sc.height, null); + g.dispose(); + return out; + } + + /// `img` centered on the recording backdrop, a `w x h` frame (shrunk to fit when larger). + static BufferedImage centerOn(BufferedImage img, int w, int h) { + if (img.getWidth() == w && img.getHeight() == h) { + return img; + } + BufferedImage out = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB); + Graphics2D g = out.createGraphics(); + com.codename1.impl.javase.record.SimulatorRecorder.paintBackdrop(g, w, h); + g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); + double s = Math.min(1.0, Math.min(w / (double) img.getWidth(), h / (double) img.getHeight())); + int dw = (int) Math.round(img.getWidth() * s); + int dh = (int) Math.round(img.getHeight() * s); + g.drawImage(img, (w - dw) / 2, (h - dh) / 2, dw, dh, null); + g.dispose(); + return out; + } + + /// The steps of the device tour: every posture the skin supports, in + /// portrait and in landscape, or just a turn there and back for a skin + /// that does not fold. Returns the JSON list FoldScript parses. + java.util.List deviceTourSteps() { + java.util.List steps = new java.util.ArrayList(); + long at = 1200; + FoldableSkin fs = foldableSkin; + String[] order = fs == null ? new String[0] + : new String[]{FoldableSkin.POSTURE_CLOSED, FoldableSkin.POSTURE_FLAT, FoldableSkin.POSTURE_HALF}; + java.util.List postures = new java.util.ArrayList(); + for (String p : order) { + if (fs.supports(p)) { + postures.add(p); + } + } + for (String p : postures) { + steps.add(tourStep(at, "posture", p)); + at += 2400; + } + steps.add(tourStep(at, "orientation", portrait ? "landscape" : "portrait")); + at += 2600; + for (int i = postures.size() - 1; i >= 0; i--) { + String p = postures.get(i); + if (!FoldableSkin.POSTURE_CLOSED.equals(p)) { + steps.add(tourStep(at, "posture", p)); + at += 2400; + } + } + if (postures.contains(FoldableSkin.POSTURE_FLAT)) { + steps.add(tourStep(at, "posture", FoldableSkin.POSTURE_FLAT)); + at += 1800; + } + steps.add(tourStep(at, "orientation", portrait ? "portrait" : "landscape")); + return steps; + } + + private static java.util.Map tourStep(long at, String key, String value) { + java.util.Map m = new java.util.LinkedHashMap(); + m.put("at", Long.valueOf(at)); + m.put(key, value); + if ("posture".equals(key)) { + m.put("animateMs", Long.valueOf(900)); + } + return m; + } + + /// Records the device tour to a video in the default recordings folder: + /// a staged skin recording, with no touch indicators, of the steps above, + /// stopped by itself once the last turn has settled. + void recordDeviceTour() { + final com.codename1.impl.javase.record.SimulatorRecorder rec = getSimulatorRecorder(); + if (rec.isRecording()) { + JOptionPane.showMessageDialog(window, "A recording is already running.", "Device Tour", + JOptionPane.INFORMATION_MESSAGE); + return; + } + if (getSkin() == null || (rotateMenuItem == null || !rotateMenuItem.isEnabled()) && foldableSkin == null) { + JOptionPane.showMessageDialog(window, "The device tour needs a device skin.", "Device Tour", + JOptionPane.INFORMATION_MESSAGE); + return; + } + final long savedAnimation = foldAnimationMs; + if (foldAnimationMs <= 0) { + foldAnimationMs = FoldController.DEFAULT_DURATION_MS; + } + try { + com.codename1.impl.javase.record.RecordingOptions o = new com.codename1.impl.javase.record.RecordingOptions(); + o.setFrame(com.codename1.impl.javase.record.RecordingOptions.Frame.SKIN); + o.setFormat("mp4"); + o.setFps(60); + o.setLockstep(true); + o.setTouches(com.codename1.impl.javase.record.TouchOverlay.Style.NONE); + touchOverlay.setStyle(o.getTouches()); + rec.start(o); + } catch (Exception ex) { + foldAnimationMs = savedAnimation; + Log.e(ex); + JOptionPane.showMessageDialog(window, ex.getMessage(), "Device Tour", JOptionPane.ERROR_MESSAGE); + return; + } + FoldScript script = FoldScript.parse(deviceTourSteps()); + script.startWithTail(new FoldScript.Target() { + public void fold(String posture, int angle, long animateMs) { + if (foldableSkin != null) { + setFoldStateAndWait(posture, angle, animateMs); + } + } + + public void rotate(boolean p) { + rotateSimulator(p); + } + }, new Runnable() { + public void run() { + // stop() joins the lockstep capture thread, which waits on + // the EDT every frame (advanceTo renders there), so stopping + // on the EDT would deadlock both. Stop on a worker; only the + // dialog goes to the EDT. + Thread stopper = new Thread(new Runnable() { + public void run() { + String msg; + int type; + try { + com.codename1.impl.javase.record.SimulatorRecorder.Result r = rec.stop(); + msg = "Saved " + r.file.getAbsolutePath() + (r.note != null ? "\n" + r.note : ""); + type = JOptionPane.INFORMATION_MESSAGE; + } catch (Exception ex) { + Log.e(ex); + msg = String.valueOf(ex.getMessage()); + type = JOptionPane.ERROR_MESSAGE; + } + final String fmsg = msg; + final int ftype = type; + EventQueue.invokeLater(new Runnable() { + public void run() { + foldAnimationMs = savedAnimation; + JOptionPane.showMessageDialog(window, fmsg, "Device Tour", ftype); + } + }); + } + }, "DeviceTourStop"); + stopper.setDaemon(true); + stopper.start(); + } + }, 1800); + } + + /// The longest a staged recording frame gets; a 2.6k pixel skin would + /// otherwise make a 5k video nobody can play smoothly. + static final int STAGE_MAX_HEIGHT = 1440; + + /// A skin recording is staged: every frame is a 16:9 backdrop with the + /// device centered at one constant size, tall enough for the device's + /// longer side. Folding, moving to the cover display and turning between + /// portrait and landscape then never rescale or letterbox the device, + /// which a video stream (one size from its first frame) otherwise must. + /// `scale` is the size `img` was drawn at relative to skin pixels. + BufferedImage stageFrame(BufferedImage img, double scale) { + if (img == null) { + return null; + } + BufferedImage skin = getSkin(); + int longest = Math.max(skin.getWidth(), skin.getHeight()); + java.awt.Dimension fold = foldableRecordingSize(com.codename1.impl.javase.record.RecordingOptions.Frame.SKIN); + if (fold != null) { + longest = Math.max(longest, Math.max(fold.width, fold.height)); + } + double stageH = longest * 1.12; + double k = Math.min(1.0, STAGE_MAX_HEIGHT / stageH); + int h = 2 * (int) Math.round(stageH * k / 2); + int w = 2 * (int) Math.round(stageH * k * 16 / 9 / 2); + BufferedImage out = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB); + Graphics2D g = out.createGraphics(); + com.codename1.impl.javase.record.SimulatorRecorder.paintBackdrop(g, w, h); + g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); + g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY); + double s = k / scale; + int dw = (int) Math.round(img.getWidth() * s); + int dh = (int) Math.round(img.getHeight() * s); + g.drawImage(img, (w - dw) / 2, (h - dh) / 2, dw, dh, null); + g.dispose(); + return out; + } + BufferedImage captureSimulatorFrame(com.codename1.impl.javase.record.RecordingOptions.Frame frame) { if (canvas == null) { return null; @@ -12963,7 +14195,18 @@ BufferedImage captureSimulatorFrame(com.codename1.impl.javase.record.RecordingOp if (sc == null) { return screen; } - if (frame == com.codename1.impl.javase.record.RecordingOptions.Frame.SKIN && getSkin() != null) { + boolean skinFrame = frame == com.codename1.impl.javase.record.RecordingOptions.Frame.SKIN; + BufferedImage animated = lastAnimatedFrame; + if (skinFrame && animated != null && (isRotationAnimating() || isFoldVisualActive())) { + // The canvas already rendered this motion in 3D; rendering it + // again here at full resolution, on this thread, held the canvas + // up and dropped frames from both. + return stageFrame(animated, lastAnimatedZoom); + } + if (skinFrame && getSkin() != null) { + if (foldableSkin != null) { + return stageFrame(captureFoldableFrame(frame, screen, sc), 1); + } BufferedImage skin = getSkin(); BufferedImage out = new BufferedImage(skin.getWidth(), skin.getHeight(), BufferedImage.TYPE_INT_ARGB); Graphics2D g = out.createGraphics(); @@ -12971,7 +14214,12 @@ BufferedImage captureSimulatorFrame(com.codename1.impl.javase.record.RecordingOp g.drawImage(screen, sc.x, sc.y, sc.width, sc.height, null); g.drawImage(skin, 0, 0, null); g.dispose(); - return out; + return stageFrame(out, 1); + } + java.awt.Dimension foldSize = foldableRecordingSize(frame); + if (foldSize != null || foldableSkin != null) { + BufferedImage f = captureFoldableFrame(frame, screen, sc); + return foldSize == null ? f : centerOn(f, foldSize.width, foldSize.height); } if (screen.getWidth() != sc.width) { BufferedImage out = new BufferedImage(sc.width, sc.height, BufferedImage.TYPE_INT_ARGB); @@ -13285,6 +14533,14 @@ public void actionPerformed(ActionEvent e) { } }); simulateMenu.add(record); + final JMenuItem tour = new JMenuItem("Record Device Tour..."); + tour.setToolTipText("Records a short video of the application as the device folds, unfolds and turns"); + tour.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + recordDeviceTour(); + } + }); + simulateMenu.add(tour); // A recording started over MCP, or stopped at its time limit, changes the // state behind the label's back. simulateMenu.addMenuListener(new javax.swing.event.MenuListener() { diff --git a/Ports/JavaSE/src/com/codename1/impl/javase/JavaSESoftwareDevice.java b/Ports/JavaSE/src/com/codename1/impl/javase/JavaSESoftwareDevice.java index 1dce55fed1a..5f431371bb9 100644 --- a/Ports/JavaSE/src/com/codename1/impl/javase/JavaSESoftwareDevice.java +++ b/Ports/JavaSE/src/com/codename1/impl/javase/JavaSESoftwareDevice.java @@ -351,7 +351,29 @@ private void tri(int i0, int i1, int i2, Material material, float[] model, for (int y = minY; y < maxY; y++) { float py = y + 0.5f; - for (int x = minX; x < maxX; x++) { + // Only the run of this row inside the triangle is visited: a + // thin or diagonal triangle's bounding box is mostly outside it, + // and testing every pixel of it dominated whole frames. The run + // is widened a pixel each side; the per-pixel test stays exact. + float lo = minX; + float hi = maxX; + for (int e = 0; e < 3; e++) { + int ea = e == 0 ? 1 : (e == 1 ? 2 : 0); + int eb = e == 0 ? 2 : (e == 1 ? 0 : 1); + // weight(px) = c + k px, kept where >= 0 + float k = -(sy[eb] - sy[ea]) * invArea; + float c = ((sx[eb] - sx[ea]) * (py - sy[ea]) + (sy[eb] - sy[ea]) * sx[ea]) * invArea; + if (k > 1e-9f) { + lo = Math.max(lo, -c / k - 1.5f); + } else if (k < -1e-9f) { + hi = Math.min(hi, -c / k + 1.5f); + } else if (c < 0) { + hi = lo - 1; + } + } + int xStart = Math.max(minX, (int) Math.floor(lo)); + int xEnd = Math.min(maxX, (int) Math.ceil(hi)); + for (int x = xStart; x < xEnd; x++) { float px = x + 0.5f; float w0 = edge(sx[1], sy[1], sx[2], sy[2], px, py) * invArea; float w1 = edge(sx[2], sy[2], sx[0], sy[0], px, py) * invArea; @@ -621,10 +643,27 @@ private static int blendPixel(int dst, int src, boolean additive, float srcAlpha | clamp255((int) (db + sb * sa + 0.5f)); } float ia = 1.0f - sa; - int rr = clamp255((int) (sr * sa + dr * ia + 0.5f)); - int rg = clamp255((int) (sg * sa + dg * ia + 0.5f)); - int rb = clamp255((int) (sb * sa + db * ia + 0.5f)); - return (0xff << 24) | (rr << 16) | (rg << 8) | rb; + int dai = dst >>> 24; + if (dai == 0xff) { + int rr = clamp255((int) (sr * sa + dr * ia + 0.5f)); + int rg = clamp255((int) (sg * sa + dg * ia + 0.5f)); + int rb = clamp255((int) (sb * sa + db * ia + 0.5f)); + return (0xff << 24) | (rr << 16) | (rg << 8) | rb; + } + // Source over a destination that is itself translucent (a frame + // cleared to transparent, rendered for compositing elsewhere): the + // coverage adds up instead of turning opaque, or every soft edge + // and transparent texel became a dark fringe over the backdrop. + float da = dai / 255.0f * ia; + float oa = sa + da; + if (oa <= 0.0f) { + return 0; + } + float inv = 1.0f / oa; + int rr = clamp255((int) ((sr * sa + dr * da) * inv + 0.5f)); + int rg = clamp255((int) ((sg * sa + dg * da) * inv + 0.5f)); + int rb = clamp255((int) ((sb * sa + db * da) * inv + 0.5f)); + return (clamp255((int) (oa * 255.0f + 0.5f)) << 24) | (rr << 16) | (rg << 8) | rb; } private static float channel(int argb, int shift) { diff --git a/Ports/JavaSE/src/com/codename1/impl/javase/Simulator.java b/Ports/JavaSE/src/com/codename1/impl/javase/Simulator.java index 8592357818d..dca953f37ca 100644 --- a/Ports/JavaSE/src/com/codename1/impl/javase/Simulator.java +++ b/Ports/JavaSE/src/com/codename1/impl/javase/Simulator.java @@ -141,6 +141,15 @@ public static void main(final String[] argv) throws Exception { System.setProperty("cn1.agent.background", "true"); } + if ("1".equals(System.getenv("CN1_SIMULATOR_HIDDEN")) || Boolean.getBoolean("cn1.simulator.hidden")) { + // No window at all: the simulator lays its window out and renders + // into it without ever showing it, for an agent recording a demo + // video or taking screenshots while the developer works. + System.setProperty("apple.awt.UIElement", "true"); + System.setProperty("cn1.simulator.hidden", "true"); + System.setProperty("cn1.agent.background", "true"); + } + String classPathStr = System.getProperty("java.class.path"); if (System.getProperty("cn1.class.path") != null) { classPathStr += File.pathSeparator + System.getProperty("cn1.class.path"); diff --git a/Ports/JavaSE/src/com/codename1/impl/javase/SimulatorMcpTools.java b/Ports/JavaSE/src/com/codename1/impl/javase/SimulatorMcpTools.java index 3e7f9163ac5..1019cbacc9d 100644 --- a/Ports/JavaSE/src/com/codename1/impl/javase/SimulatorMcpTools.java +++ b/Ports/JavaSE/src/com/codename1/impl/javase/SimulatorMcpTools.java @@ -221,6 +221,31 @@ public String invoke(String argumentsJson) throws Exception { } })); + server.addTool(new Tool("sim_fold", + "Folds a foldable skin (one whose skin.properties says foldable=true) to a posture " + + "- flat, half or closed - or to a hinge angle in degrees (0 closed, 180 flat). " + + "The device is drawn folding in 3D and the app receives the posture events " + + "as the hinge crosses each threshold; closed moves the app to the cover " + + "display. animateMs: transition length (default the simulator's, 0 = at once).", + "{\"type\":\"object\",\"properties\":{" + + "\"posture\":{\"type\":\"string\",\"enum\":[\"flat\",\"half\",\"closed\"]}," + + "\"angle\":{\"type\":\"integer\"},\"animateMs\":{\"type\":\"integer\"}}}", + new ToolHandler() { + public String invoke(String argumentsJson) throws Exception { + Map a = parse(argumentsJson); + Object p = a.get("posture"); + int angle = a.get("angle") instanceof Number ? ((Number) a.get("angle")).intValue() : -1; + if (!(p instanceof String) && angle < 0) { + throw new IllegalArgumentException("Give a posture or an angle"); + } + long ms = a.get("animateMs") instanceof Number ? ((Number) a.get("animateMs")).longValue() : -1; + port.setFoldStateAndWait(p instanceof String ? (String) p : null, angle, ms); + FoldableSkin f = port.getFoldableSkin(); + return ok("targetAngle", port.getFoldController().getTargetAngle(), + "postures", f.getPostures().size()); + } + })); + server.addTool(new Tool("sim_touch_indicators", "Configures how touches are drawn. style: none, dot or ripple. live: also draw them " + "in the simulator window (recordings always get them unless style is none). " @@ -275,7 +300,12 @@ public String invoke(String argumentsJson) throws Exception { + "application plays - sound effects, game sounds, music), microphone, " + "app_and_microphone (for narration) or none. maxSeconds stops a " + "forgotten recording (default 600). path defaults to " - + "~/CodenameOneRecordings. Needs ffmpeg; without it PNG frames are written.", + + "~/CodenameOneRecordings. Needs ffmpeg; without it PNG frames are written. " + + "script: timed steps played during the recording, " + + "[{at: ms, posture: flat|half|closed | angle: degrees, animateMs | " + + "orientation: portrait|landscape}] (postures and angles need a foldable skin). " + + "A skin recording is staged: the device keeps one size on a 16:9 backdrop " + + "through folds, the cover display and rotations.", "{\"type\":\"object\",\"properties\":{" + "\"path\":{\"type\":\"string\"}," + "\"format\":{\"type\":\"string\",\"enum\":[\"mp4\",\"mov\",\"webm\",\"gif\"]}," @@ -285,7 +315,11 @@ public String invoke(String argumentsJson) throws Exception { + "\"touchTrail\":{\"type\":\"boolean\"},\"touchColor\":{\"type\":\"string\"}," + "\"touchRadius\":{\"type\":\"integer\"},\"tapSound\":{\"type\":\"boolean\"}," + "\"audio\":{\"type\":\"string\",\"enum\":[\"app\",\"microphone\",\"app_and_microphone\",\"none\"]}," - + "\"maxSeconds\":{\"type\":\"integer\"}}}", + + "\"maxSeconds\":{\"type\":\"integer\"}," + + "\"script\":{\"type\":\"array\",\"items\":{\"type\":\"object\",\"properties\":{" + + "\"at\":{\"type\":\"integer\"},\"posture\":{\"type\":\"string\"}," + + "\"angle\":{\"type\":\"integer\"},\"animateMs\":{\"type\":\"integer\"}," + + "\"orientation\":{\"type\":\"string\",\"enum\":[\"portrait\",\"landscape\"]}}}}}}", new ToolHandler() { public String invoke(String argumentsJson) throws Exception { RecordingOptions o = new RecordingOptions(); @@ -295,8 +329,21 @@ public String invoke(String argumentsJson) throws Exception { t.setShowTrail(o.isTouchTrail()); t.setColor(o.getTouchColor()); t.setRadius(o.getTouchRadius()); + Map args = parse(argumentsJson); + FoldScript script = null; + if (args.get("script") != null) { + script = FoldScript.parse(args.get("script")); + if (port.getFoldableSkin() == null && script.foldsTheDevice()) { + throw new IllegalArgumentException("fold steps need a foldable skin"); + } + } + if (script != null) { + // A scripted clip renders every frame for its moment. + o.setLockstep(true); + } SimulatorRecorder rec = port.getSimulatorRecorder(); rec.start(o); + startFoldScript(script); Map out = new LinkedHashMap(); out.put("recording", Boolean.TRUE); out.put("path", rec.getOutput().getAbsolutePath()); @@ -315,7 +362,13 @@ public String invoke(String argumentsJson) throws IOException { SimulatorRecorder.Result r; boolean auto = false; if (rec.isRecording()) { - r = rec.stop(); + // The script keeps playing while a lockstep video + // catches up; it is cancelled only after that. + try { + r = rec.stopAtWallClock(); + } finally { + startFoldScript(null); + } } else if (rec.getLastError() != null) { // It already ended on its own, with an error. throw rec.getLastError(); @@ -326,6 +379,7 @@ public String invoke(String argumentsJson) throws IOException { } else { throw new IllegalStateException("Not recording"); } + startFoldScript(null); Map out = new LinkedHashMap(); if (auto) { out.put("autoStopped", Boolean.TRUE); @@ -486,6 +540,27 @@ public void run() { } } + /// The fold script of the running recording, cancelled when it stops. + private FoldScript foldScript; + + private synchronized void startFoldScript(FoldScript script) { + if (foldScript != null) { + foldScript.cancel(); + } + foldScript = script; + if (script != null) { + script.start(new FoldScript.Target() { + public void fold(String posture, int angle, long animateMs) { + port.setFoldStateAndWait(posture, angle, animateMs); + } + + public void rotate(boolean portrait) { + port.rotateSimulator(portrait); + } + }); + } + } + private static Map parse(String json) throws IOException { if (json == null || json.trim().length() == 0) { return new LinkedHashMap(); diff --git a/Ports/JavaSE/src/com/codename1/impl/javase/record/RecordingOptions.java b/Ports/JavaSE/src/com/codename1/impl/javase/record/RecordingOptions.java index 683f13d5f84..6ea31ef5b30 100644 --- a/Ports/JavaSE/src/com/codename1/impl/javase/record/RecordingOptions.java +++ b/Ports/JavaSE/src/com/codename1/impl/javase/record/RecordingOptions.java @@ -72,6 +72,7 @@ boolean includesMicrophone() { private boolean tapSound; private Audio audio = Audio.APP; private int maxSeconds = 600; + private boolean lockstep; public String getPath() { return path; @@ -173,6 +174,19 @@ public void setAudio(Audio audio) { this.audio = audio == null ? Audio.NONE : audio; } + /// True when every video frame is rendered for its own moment of the + /// timeline instead of grabbed from the live window. Simulator + /// animations (a fold, a turn) then advance exactly one frame interval + /// per video frame, so the video is smooth however slowly a frame + /// renders. Scripted recordings use it. + public boolean isLockstep() { + return lockstep; + } + + public void setLockstep(boolean lockstep) { + this.lockstep = lockstep; + } + public int getMaxSeconds() { return maxSeconds; } diff --git a/Ports/JavaSE/src/com/codename1/impl/javase/record/SimulatorRecorder.java b/Ports/JavaSE/src/com/codename1/impl/javase/record/SimulatorRecorder.java index 8e82e162bc9..46ae5a92f29 100644 --- a/Ports/JavaSE/src/com/codename1/impl/javase/record/SimulatorRecorder.java +++ b/Ports/JavaSE/src/com/codename1/impl/javase/record/SimulatorRecorder.java @@ -73,6 +73,16 @@ public interface FrameSource { BufferedImage captureFrame(RecordingOptions.Frame frame); } + /// A source that can move its animations to a given moment, for + /// lockstep recordings. `advanceTo` is called before each capture with + /// the frame's time on the wall clock's scale; `release` once the + /// recording ends, to hand the animations back to the wall clock. + public interface Timeline { + void advanceTo(long epochMs); + + void release(); + } + /// What a stopped recording produced. public static final class Result { public final File file; @@ -304,6 +314,10 @@ public synchronized void caption(String text, long durationMs) { } private void captureLoop() { + if (options.isLockstep() && source instanceof Timeline) { + lockstepLoop((Timeline) source); + return; + } long frameMs = 1000L / options.getFps(); long maxMs = options.getMaxSeconds() * 1000L; try { @@ -340,6 +354,71 @@ private void captureLoop() { } } + /// A lockstep recording renders slower than real time, so a stop asked + /// for now still has video to make: it covers as long as the wall clock + /// has run since the start, the same as a live recording would. + private void finishTimeline() { + long target; + int fps; + synchronized (this) { + if (!running || !options.isLockstep() || Thread.currentThread() == thread) { + return; + } + target = System.currentTimeMillis() - startTime; + fps = options.getFps(); + } + try { + while (true) { + synchronized (this) { + if (!running || (long) frameCount * 1000L / fps >= target) { + return; + } + } + Thread.sleep(20); + } + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + } + } + + /// One frame per interval of the timeline, each rendered for its own + /// moment: never a repeat, never a skip. When rendering is faster than + /// real time it waits for the wall clock, so the app keeps its pace. + private void lockstepLoop(Timeline timeline) { + int fps = options.getFps(); + long maxMs = options.getMaxSeconds() * 1000L; + try { + while (isRecording()) { + int index; + synchronized (this) { + index = frameCount; + } + long at = (long) index * 1000L / fps; + if (at >= maxMs) { + autoStop(); + return; + } + long ahead = at - (System.currentTimeMillis() - startTime); + if (ahead > 0) { + Thread.sleep(ahead); + } + timeline.advanceTo(startTime + at); + BufferedImage img = source.captureFrame(options.getFrame()); + if (img != null) { + compose(img, at); + } + synchronized (this) { + frameCount++; + } + queue.put(new Frame(pixels(), index)); + } + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + } finally { + timeline.release(); + } + } + /// Starts the threads that drain the queue: one for ffmpeg, which needs its /// frames in order, and one per spare core for PNG files, which do not. private void startWriters() { @@ -440,10 +519,33 @@ public void run() { }, "SimulatorRecorderAutoStop").start(); } + /// The light studio backdrop a recording shows around the device. + public static void paintBackdrop(Graphics2D g, int w, int h) { + g.setPaint(new java.awt.GradientPaint(0, 0, new java.awt.Color(0xf5f5f7), 0, h, + new java.awt.Color(0xdedee3))); + g.fillRect(0, 0, w, h); + g.setPaint(null); + } + private void compose(BufferedImage img, long now) { Graphics2D g = scaled.createGraphics(); g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); - g.drawImage(img, 0, 0, width, height, null); + // Every frame starts from the backdrop: a skin frame is transparent + // around the device, and drawn straight over the last frame its + // corners kept whatever was there (black, or a smear of the device + // as it moved). + paintBackdrop(g, width, height); + if (img.getWidth() * height == img.getHeight() * width) { + g.drawImage(img, 0, 0, width, height, null); + } else { + // A frame whose shape changed since the first (a foldable skin + // moving to its cover display, a rotation) is letterboxed, never + // stretched: a video stream keeps the size it started with. + double s = Math.min(width / (double) img.getWidth(), height / (double) img.getHeight()); + int dw = (int) Math.round(img.getWidth() * s); + int dh = (int) Math.round(img.getHeight() * s); + g.drawImage(img, (width - dw) / 2, (height - dh) / 2, dw, dh, null); + } Caption[] active; synchronized (this) { active = captions.toArray(new Caption[captions.size()]); @@ -549,8 +651,39 @@ public void run() { } } + /// As `stop`, but a lockstep recording first renders on until its video + /// is as long as the wall clock has run since the start: that recording + /// renders slower than real time, and a caller timing the stop by the + /// wall clock expects the video to cover that span. A recording ended + /// by a script on the animation clock calls `stop` instead. + public Result stopAtWallClock() throws IOException { + finishTimeline(); + return stop(); + } + /// Stops recording and finishes the file. Throws when encoding failed at any /// point during the recording. + private static void joinWhileDispatching(final Thread t) { + final java.awt.SecondaryLoop loop = java.awt.Toolkit.getDefaultToolkit() + .getSystemEventQueue().createSecondaryLoop(); + Thread waiter = new Thread(new Runnable() { + public void run() { + while (t.isAlive()) { + try { + t.join(); + } catch (InterruptedException e) { + // keep waiting; the loop must be exited + } + } + loop.exit(); + } + }, "RecorderStopJoin"); + waiter.setDaemon(true); + waiter.start(); + // Returns at once if exit() already ran (the thread had ended). + loop.enter(); + } + public Result stop() throws IOException { synchronized (stopLock) { Thread t; @@ -568,6 +701,13 @@ public Result stop() throws IOException { try { // No timeout: the writer below must never be closed while the // capture thread may still be writing a frame into it. + if (t != null && t != Thread.currentThread() && java.awt.EventQueue.isDispatchThread()) { + // A lockstep capture thread renders each frame on the EDT + // (invokeAndWait), so a plain join here would deadlock. + // A secondary loop keeps dispatching events until the + // capture thread has ended. + joinWhileDispatching(t); + } if (t != null && t != Thread.currentThread()) { boolean interrupted = false; while (t.isAlive()) { diff --git a/Ports/JavaSE/src/com/codename1/impl/javase/simulator/AppPanel.java b/Ports/JavaSE/src/com/codename1/impl/javase/simulator/AppPanel.java index c8cc7c8bec5..2eb5a2b75d8 100644 --- a/Ports/JavaSE/src/com/codename1/impl/javase/simulator/AppPanel.java +++ b/Ports/JavaSE/src/com/codename1/impl/javase/simulator/AppPanel.java @@ -291,6 +291,10 @@ public AppFrame getAppFrame() { } + /// Action key: when true, the action's toolbar button is shown only while + /// the action is enabled. + public static final String HIDE_WHEN_DISABLED = "cn1.hideWhenDisabled"; + public void addAction(Action action) { if (action instanceof SeparatorAction) { leftToolBar.addSeparator(); @@ -302,6 +306,20 @@ public void addAction(Action action) { if (action instanceof CompanionMenuAction) { btn.setBorder(new EmptyBorder(5, 0, 5, 10)); } + if (Boolean.TRUE.equals(action.getValue(HIDE_WHEN_DISABLED))) { + // A button only some skins use (fold) is hidden rather than + // greyed, so the toolbar of every other skin is unchanged. + final JButton hideable = btn; + hideable.setVisible(action.isEnabled()); + action.addPropertyChangeListener(new java.beans.PropertyChangeListener() { + public void propertyChange(java.beans.PropertyChangeEvent e) { + if ("enabled".equals(e.getPropertyName())) { + hideable.setVisible(Boolean.TRUE.equals(e.getNewValue())); + leftToolBar.revalidate(); + } + } + }); + } } } diff --git a/docs/developer-guide/Device-Input-And-Form-Factors.asciidoc b/docs/developer-guide/Device-Input-And-Form-Factors.asciidoc index 64b56d06f16..21bbec0aaa9 100644 --- a/docs/developer-guide/Device-Input-And-Form-Factors.asciidoc +++ b/docs/developer-guide/Device-Input-And-Form-Factors.asciidoc @@ -155,6 +155,42 @@ mode, then pick a posture (flat, half-opened or closed), the fold orientation (v horizontal) and the hinge angle. The running app receives the same posture changes and listener callbacks it would on a real foldable device. +A foldable skin goes further. The bundled *iPhone Duo* skin, or any skin built with the Skin +Designer's *Fold* tab, describes the hinge and the cover display, so the menu offers that device's +postures and a hinge-angle slider. A posture change animates the device folding in 3D. Closing it +switches the app to the cover display, with that display's size, density and safe area, exactly +as the hardware does. The posture events arrive while the fold moves: half opened below 150 +degrees, closed below 30, the thresholds Android's Jetpack WindowManager uses. The fold button +beside the rotate buttons in the simulator toolbar steps through the postures, and *Animate fold* +in the menu turns the animation off when you only want the end state. + +Rotating the device animates too, on every skin: the device lifts, turns a quarter and settles +into the new orientation, still folded if it was. + +*Simulate -> Record Device Tour...* turns this into a demo video in one step. It records your app +while the device closes, opens, half folds, turns to landscape and back, then stops by itself and +names the saved MP4 (in `~/CodenameOneRecordings`). A skin that doesn't fold gets the turn alone. + +The tour and any scripted recording render every video frame for its own moment of the animation, +at 60 frames a second, so the video plays without stutter however long a frame takes to draw. While such a +recording runs, the simulator window plays the motion in slow motion; that's expected. + +To record without any window on screen, start the simulator hidden: `mvn cn1:agent +-Dcn1.agent.hidden=true` (Gradle: `./gradlew cn1Agent -Pcn1.agent.hidden=true`), or set +`CN1_SIMULATOR_HIDDEN=1` in the environment. It renders exactly as it does with a window, +so screenshots, recordings and the MCP tools all work while you keep using the machine. + +Every skin-framed recording is staged: a 16:9 backdrop with the device centered at one constant +size, so folding, moving to the cover display and rotating never rescale it. To script your own +clip, the simulator's MCP tools take the fold too: `sim_fold` moves to a posture (`posture` is +`flat`, `half` or `closed`) or a hinge `angle`, over `animateMs` milliseconds, and +`sim_record_start` accepts a `script`, a list of steps such as +`{"at": 2000, "posture": "half", "animateMs": 900}` or `{"at": 6000, "orientation": "landscape"}` +that it plays from the start of the recording. + +TIP: The simulator honors the skin's safe area, so the title stays clear of the camera when the +form uses a `Toolbar`, as new projects do by default. + Foldable support on Android is opt-in so it never adds weight to apps that don't use it. Enable it with the `android.foldableSupport=true` build hint (the version can be overridden with `android.windowVersion`). Note that the hinge *angle* isn't available on Android, so diff --git a/docs/developer-guide/Skin-Designer.asciidoc b/docs/developer-guide/Skin-Designer.asciidoc index fc05170081d..9725869c839 100644 --- a/docs/developer-guide/Skin-Designer.asciidoc +++ b/docs/developer-guide/Skin-Designer.asciidoc @@ -158,6 +158,38 @@ written into `skin.properties`: insets it writes into `skin.properties`, so the same 47 is correct on every notched iPhone regardless of resolution. +[[skin-designer-fold-tab]] +==== Fold tab + +The Fold tab turns the skin into a foldable. Picking a foldable device from the catalog, such as +the iPhone Duo, fills it in. Where the manufacturer hasn't published a resolution, the catalog's +value is an estimate and the tab says so. + +* *Foldable device* - writes the fold keys at all; off for every other skin. +* *Book fold* - the hinge runs top to bottom (on), or left to right for a flip phone (off). +* *Two screens with a gap* - a device with two separate panels. The gap is what + `DevicePosture.getFoldBounds()` reports, and the device always reads as separating. +* *Crease on the flat screen* - a faint line where a single flexible panel folds. +* *Postures* - whether the device can rest half opened and closed. Flat is always supported. +* *Cover display* - the outer screen in pixels plus its PPI. Zero width means no cover display, + and the closed device then shows its back. +* *Preview* - flat, half and closed sketches in the editor's preview. The simulator renders the + real fold in 3D. + +The generated skin carries two more images, `cover.png` and `cover_l.png`, the cover display in +portrait and landscape, and these `skin.properties` keys: + +* `foldable=true`, `foldOrientation` (`vertical` or `horizontal`), `foldType` (`continuous` or + `dualScreen`), `foldPostures` (for example `flat,half,closed`) and `crease`. +* `hingePortrait` and `hingeLandscape` - the hinge as `x,y,width,height` in the skin image. A + continuous fold has zero width (book) or zero height (flip). +* `coverPortraitScreenX/Y/Width/Height`, `coverLandscapeScreenX/Y/Width/Height` and `coverPpi` - + the cover display in the cover images. `coverCutouts` lists its cutouts as `cutouts` does for + the inner display. + +Landscape rectangles are the portrait ones rotated clockwise, the way the simulator rotates the +skin. A skin without `foldable=true` behaves as it always has. + === Stage 4 — finish and download image::img/skin-designer/skin-designer-stage-4-done.png["Done page showing the green check, summary of the generated skin, and a Download skin button",role="text-center"] @@ -176,6 +208,8 @@ Clicking *Finish* in the editor's footer: . Writes `skin.properties` with `roundScreen=true`, `displayX/Y/Width/Height`, `safePortrait*` / `safeLandscape*`, `cutouts`, the platform name, override names, fonts, and PPI. +. For a foldable, renders the cover display images and writes the fold keys described under + <>. Clicking *Download skin* on the done page hands the file to the browser's download dialog. The button is the only reliable trigger diff --git a/maven/codenameone-gradle-plugin/src/main/java/com/codename1/gradle/AppSupport.java b/maven/codenameone-gradle-plugin/src/main/java/com/codename1/gradle/AppSupport.java index 8cff114d041..962e83a933a 100644 --- a/maven/codenameone-gradle-plugin/src/main/java/com/codename1/gradle/AppSupport.java +++ b/maven/codenameone-gradle-plugin/src/main/java/com/codename1/gradle/AppSupport.java @@ -1482,6 +1482,11 @@ public void execute(org.gradle.api.Task task) { JavaExec exec = (JavaExec) task; exec.environment("CN1_MCP_PORT", String.valueOf(port)); exec.environment("CN1_AGENT_BACKGROUND", "1"); + // -Pcn1.agent.hidden=true: no window at all, everything still renders. + Object hiddenProp = task.getProject().findProperty("cn1.agent.hidden"); + if (hiddenProp != null && "true".equalsIgnoreCase(String.valueOf(hiddenProp).trim())) { + exec.environment("CN1_SIMULATOR_HIDDEN", "1"); + } task.getLogger().lifecycle("[cn1.mcp] agent mode: the simulator will serve MCP on 127.0.0.1:" + port); } diff --git a/maven/codenameone-maven-plugin/src/main/java/com/codename1/maven/CN1AgentMojo.java b/maven/codenameone-maven-plugin/src/main/java/com/codename1/maven/CN1AgentMojo.java index 88718ba1dff..599f08a3aef 100644 --- a/maven/codenameone-maven-plugin/src/main/java/com/codename1/maven/CN1AgentMojo.java +++ b/maven/codenameone-maven-plugin/src/main/java/com/codename1/maven/CN1AgentMojo.java @@ -52,6 +52,16 @@ public class CN1AgentMojo extends CN1RunMojo { @Parameter(property = "cn1.mcp.port", defaultValue = "8765") private int mcpPort; + /** + * Runs the simulator with no window at all: it lays out and renders as + * usual, so screenshots, recordings and every MCP tool work, but nothing + * appears on screen. For an agent making a demo video or screenshots + * while the developer keeps working. {@code CN1_SIMULATOR_HIDDEN=1} in + * the environment does the same. + */ + @Parameter(property = "cn1.agent.hidden", defaultValue = "false") + private boolean hidden; + /** Read by the shutdown hook as well as this thread. */ private final AtomicReference xvfbRef = new AtomicReference<>(); @@ -110,6 +120,9 @@ protected void configureSimulatorRequest(InvocationRequest request) throws MojoE writePortFile(); request.addShellEnvironment("CN1_MCP_PORT", String.valueOf(mcpPort)); request.addShellEnvironment("CN1_AGENT_BACKGROUND", "1"); + if (hidden) { + request.addShellEnvironment("CN1_SIMULATOR_HIDDEN", "1"); + } String os = System.getProperty("os.name", "").toLowerCase(Locale.ROOT); boolean linux = os.contains("linux"); String display = System.getenv("DISPLAY"); diff --git a/maven/javase/pom.xml b/maven/javase/pom.xml index e10ab67f51f..f4ec59b27ac 100644 --- a/maven/javase/pom.xml +++ b/maven/javase/pom.xml @@ -381,6 +381,23 @@ after this task runs. --> + + Generating foldable skins + + + + + + + + +