Skip to content

Play your own Switch games on iPhone/iPad — one-click macOS app, no terminal. | 把 Switch 游戏装进 iPhone:图形界面一键构建,不用命令行。Bring your own game.

Merge origin/main into v0lumehi/engine-bump-1.1.0

v0lumehi06d4fc2041d2
diff --cc patches/0065-ios-touch-controls-size.patch
index 00000000,cce3486a..c4957671
mode 000000,100644..100644
--- a/patches/0065-ios-touch-controls-size.patch
+++ b/patches/0065-ios-touch-controls-size.patch
@@@ -1,0 -1,593 +1,596 @@@
+ From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
+ From: v0lumehi <[email protected]>
-Date: Tue, 3 Nov 2026 15:11:04 +0100
++Date: Tue, 3 Nov 2026 18:23:54 +0100
+ Subject: [PATCH] ios: touch controls size (DM_TOUCH_SIZE) on a tested geometry
+
+ The virtual gamepad's placement moves out of DmGamepad.m into
+ DmPadGeometry.h: pure functions of the view's bounds, its safe area, the
+ DmLayout split and the player's options. Drawing, the hit-test and the
+ grab read the same values, as before. The built-in size is unchanged.
+
+ DM_TOUCH_SIZE=small|medium|large scales every control (0.8, 1, 1.2). A
+ size that does not fit the screen moves toward medium in steps: every
+ drawn control stays inside the safe area (the bottom corners reach
+ halfway into the side insets, as always) and no two overlap.
+
+ dm/ios/tests/test_pad_geometry.py builds the header on the Mac with a
+ small UIKit stand-in and checks iPhone and iPad in landscape, iPad and
+ split windows in portrait, and iPhone Duo's inner display in portrait,
+ landscape and tabletop, in 2D and 3D and every size. It also pins the
+ built-in layout on iPhone Pro Max to the points it has always used.
+
+ Upstream-Status: pending (on seedds/nxnative#683)
+ Reason: Players ask for smaller or larger touch controls (#347), and the placement needs tests without a device
+ Issue: LNPANDY/NX2Apple#347
+ ---
+ dm/ios/Sources/DmGamepad.m | 118 +++++-------
+ dm/ios/Sources/DmPadGeometry.h | 176 ++++++++++++++++++
+ dm/ios/tests/pad_geometry/UIKit/UIKit.h | 11 ++
+ .../tests/pad_geometry/pad_geometry_check.m | 102 ++++++++++
+ dm/ios/tests/test_pad_geometry.py | 34 ++++
+ 5 files changed, 369 insertions(+), 72 deletions(-)
+ create mode 100644 dm/ios/Sources/DmPadGeometry.h
+ create mode 100644 dm/ios/tests/pad_geometry/UIKit/UIKit.h
+ create mode 100644 dm/ios/tests/pad_geometry/pad_geometry_check.m
+ create mode 100644 dm/ios/tests/test_pad_geometry.py
+
+ diff --git a/dm/ios/Sources/DmGamepad.m b/dm/ios/Sources/DmGamepad.m
-index 9f51749..3ea60b6 100644
++index 8a9f009..6396f99 100644
+ --- a/dm/ios/Sources/DmGamepad.m
+ +++ b/dm/ios/Sources/DmGamepad.m
-@@ -9,6 +9,7 @@
- // Touch-down ownership: locked keys first (shoulders, − / +, the layout button, L3 / R3), then whichever of the diamond, the D-pad and the left stick has the smallest distance / grab radius (the left stick's grab circle borders the D-pad),
++@@ -10,6 +10,7 @@
++ // Touch-down ownership: the menu button where it is nearer than a locked key (a view of its own, DmMenuButton), locked keys next (shoulders, − / +, L3 / R3), then whichever of the diamond, the D-pad and the left stick has the smallest distance / grab radius (the left stick's grab circle borders the D-pad),
+ // then in 3D the right stick for a touch in the right third.
+ // Everything hugs the edges and corners to leave the picture room; controls sit over the picture, one CAShapeLayer each: a press only swaps the fill, a stick only moves its knob, nothing redraws the whole screen.
+ +// Where each control sits is DmPadGeometry.h (pure, tested on the Mac); the player picks the size there through DM_TOUCH_SIZE (small, medium, large; a size that does not fit the screen moves toward medium).
+ // Touches no control takes (in 3D, those outside the right third) go to the game's touch screen when DmLayout assigns a local game viewport (pushGame; Super Mario Galaxy's pointer in handheld mode).
- // The virtual gamepad (layout button included) hides while a physical controller is connected and comes back when it is removed; unclaimed touches go to the game only when a local game viewport exists. Physical controllers keep every button (D-pad and right stick included) and map by position: bottom = B, right = A, left = Y, top = X (same positions as on a Switch controller); a Nintendo pad maps by its labels (pad_apple.h).
++ // The virtual gamepad (all but the menu button) hides while a physical controller is connected and comes back when it is removed; unclaimed touches go to the game only when a local game viewport exists. Physical controllers keep every button (D-pad and right stick included) and map by position: bottom = B, right = A, left = Y, top = X (same positions as on a Switch controller); a Nintendo pad maps by its labels (pad_apple.h).
+ // Each physical controller is a player of its own (local co-op), as on the Mac: the first one connected is player 1, together with the touch gamepad, the next player 2, and so on up to 8; see "Physical controllers" below.
-@@ -30,6 +31,7 @@
++@@ -31,6 +32,7 @@
+ #include "input_map.h"
+ #include <stdatomic.h>
+ #import "DmLayout.h"
+ +#import "DmPadGeometry.h"
++ #import "DmGameMenu.h"
+
+ enum { B_A = 0, B_B, B_X, B_Y, B_LS, B_RS, B_L, B_R, B_ZL, B_ZR, B_PLUS, B_MINUS, B_LEFT, B_UP, B_RIGHT, B_DOWN, B_N };
- enum { C_NONE = 0, C_STICK, C_DPAD, C_FACE, C_BTN, C_SWAP, C_GAME }; // which control a touch holds; C_GAME: none, the touch goes to the game's touch screen (finger slot << 8)
-@@ -106,27 +108,19 @@ static void phone_motion_start(UIView *body) {
++@@ -108,27 +110,19 @@ static void phone_motion_start(UIView *body) {
+ }];
+ }
+
+ -// ---- Layout constants (in u; x measured from the outer edge, the right half mirrors) ----
+ +// ---- Layout constants ----
+ +// Where each control is drawn (in u, x measured from the outer edge, the right half mirrors) is DmPadGeometry.h: pure functions of the bounds, the safe area,
+ +// the DmLayout split and the player's options (DM_TOUCH_SIZE), tested on the Mac (dm/ios/tests/test_pad_geometry.py). Drawing, the hit-test and the grab all read it.
+ +// The bottom-corner controls reach halfway into the side safe-area inset (in landscape there is only the rounded screen corner there; the Dynamic Island is mid-edge);
+ +// the stick's ring is as large as the whole diamond (key distance + key radius), the knob a little larger than a diamond key; the D-pad sits inward of and below the
-+// left stick, as on a Pro Controller. What stays here is how touches are taken:
+++// left stick, as on a Pro Controller. The menu button is at the top centre, level with - / +, and takes a touch within 1.6 times its radius (at least 22 points, half of a 44-point target). What stays here is how touches are taken:
+ static const CGFloat
+ - K_ZL_X = 44, K_ZL_Y = 40, K_ZL_R = 26, // ZL / ZR: centre from the outer edge and the top, radius
+ - K_L_X = 44, K_L_Y = 108, K_L_R = 24, // L / R: same column as ZL / ZR
+ - K_DROP_TIP = 1.45, // teardrop tip distance from the centre (times the radius)
+ - K_CORNER_IN = 0.5, // how far the bottom-corner controls reach into the side safe-area inset (in landscape there is only the rounded screen corner there; the Dynamic Island is mid-edge)
+ - K_FACE_X = 81, K_PAD_Y = 82, // diamond centre from the outer edge; diamond and stick centre from the bottom (B's lower edge about 5 above it)
+ - K_FACE_GAP = 50, K_FACE_R = 27, // diamond: key distance from the centre, key radius
+ - K_PILL_DX = 52, K_PILL_Y = 22, K_PILL_W = 50, K_PILL_H = 28, // − / +: centre from the screen's midline (the middle goes to the layout button) and from the top, size
-- K_SWAP_R = 15, // layout button: top centre, level with − / +
++- K_MENU_R = 15, // menu button: top centre, level with − / +; it takes a touch within 1.6 times this (at least 22 points, half of a 44-point target)
+ - K_STICK_X = 81, K_STICK_R = 77, K_KNOB_R = 30, // left stick: mirrors the diamond, the ring as large as the whole diamond (key distance + key radius), the knob a little larger than a diamond key
+ K_STICK_GRAB = 1.4, // how near the ring centre a touch-down grabs a stick (times the ring radius)
+ K_FULL = 0.8, // full tilt at this fraction of the ring radius (the knob still travels to the ring edge)
+ K_DEAD = 0.1, // stick dead zone (times the ring radius)
+ - // controls only the 3D layout has
+ - K_LOW_X = 231, K_LOW_Y = 64, // D-pad centre from the outer edge and the bottom (inward of and below the left stick, as on a Pro Controller)
+ - K_DPAD_R = 48, K_ARM_W = 17, K_ARM_IN = 11, // D-pad: arm outer edge from the centre, half-width, tip from the centre
+ K_DPAD_GRAB = 1.3, // D-pad grab radius (times the outer edge)
+ K_RSTICK_R = 60, // right stick (not drawn): radius around its centre, the touch-down point
+ - K_RSWIPE = 1.0 / 3, // right stick area: this fraction of the screen width from the right edge (not in u)
+ - K_CLICK_X = 102, K_CLICK_R = 17; // L3 / R3: centre from the outer edge (level with ZL / ZR), radius
+ + K_RSWIPE = 1.0 / 3; // right stick area: this fraction of the screen width from the right edge (not in u)
+ static const int k_btns[] = {B_ZL, B_L, B_ZR, B_R, B_MINUS, B_PLUS, B_LS, B_RS}; // keys locked at touch-down (L3 / R3 only in the 3D layout)
+ static NSString *const k_layout_key = @"DmPadLayout"; // NSUserDefaults: 0 = 2D, 1 = 3D, 2 = Tap (DM_GAME_PAD_LAYOUT's numbers)
+ int dm_opmode(void); // hle_oe.c: 0 = handheld
-@@ -153,7 +147,9 @@ @implementation DmGamepad {
++@@ -163,7 +157,9 @@ @implementation DmGamepad {
+ CALayer *_controls; // holds every control layer: hidden while a physical controller is connected (the view itself stays, for game touches)
+ BOOL _padOff; // a physical controller is connected: every touch goes to the game
+ DmLayout _layout;
+ - UITouch *_game[16]; // touches on the game's touch screen by finger slot
+ + DmPadOptions _opts; // the player's options from the environment (DM_TOUCH_SIZE)
+ + DmPadGeo _geo; CGRect _geoBounds; UIEdgeInsets _geoInsets; BOOL _geoFull, _geoValid; // where the controls are, and what it was computed for
+ + UITouch *_game[16]; // touches on the game's touch screen by finger slot
+ }
+
+ static UIColor *k_idle, *k_on, *k_line, *k_ringFill, *k_text, *k_textOn;
-@@ -173,6 +169,9 @@ @implementation DmGamepad {
++@@ -183,6 +179,9 @@ @implementation DmGamepad {
+ - (instancetype)initWithFrame:(CGRect)f {
+ if ((self = [super initWithFrame:f])) {
+ _layout = dm_layout(self.bounds, UIEdgeInsetsZero, NO, CGRectNull);
+ + const char *size = getenv("DM_TOUCH_SIZE");
+ + _opts.scale = dm_pad_size_scale(size);
+ + if (size && *size) LOG("front end: touch controls size %s (DM_TOUCH_SIZE)", _opts.scale < 1 ? "small" : _opts.scale > 1 ? "large" : "medium");
+ [self registerForTraitChanges:@[UITraitDisplayScale.class] withHandler:^(id<UITraitEnvironment> environment, UITraitCollection *previous) {
+ [(UIView *)environment setNeedsLayout];
+ }];
-@@ -234,68 +233,43 @@ - (void)nextLayout { // 2D → 3D (→ Tap in handheld mode) → 2D: release e
++@@ -259,68 +258,43 @@ - (void)setTouchFeedback:(BOOL)on {
+ - (void)arrange:(DmLayout)layout {
+ if (CGRectEqualToRect(_layout.controls, layout.controls) && CGRectEqualToRect(_layout.game, layout.game) && _layout.separated == layout.separated && _layout.topSafeOnly == layout.topSafeOnly) return;
+ [self reset]; // A finger held across a pose change must not leave a key or game touch stuck.
+ - _layout = layout;
+ + _layout = layout; _geoValid = NO;
+ [self setNeedsLayout];
+ }
+ -- (CGRect)safe { return CGRectIntersection(_layout.controls, UIEdgeInsetsInsetRect(self.bounds, dm_game_safe_insets(self.bounds, self.safeAreaInsets, _layout.topSafeOnly))); }
+ -- (CGRect)corner { if (_layout.separated) return self.safe; UIEdgeInsets i = self.safeAreaInsets; i.left *= K_CORNER_IN; i.right *= K_CORNER_IN; return UIEdgeInsetsInsetRect(self.bounds, i); } // the rectangle the bottom-corner controls use
+ -- (CGFloat)u { if (_layout.separated) { CGRect s = self.safe; return MIN(MIN(s.size.height / 430.0, s.size.width / 700.0), 1.3); } return MIN(self.bounds.size.height / 430.0, 1.3); } // 1 = 430 points tall (iPhone Pro Max landscape)
+ -static CGPoint side(CGRect s, int right, CGFloat dx, CGFloat y) { return CGPointMake(right ? CGRectGetMaxX(s) - dx : CGRectGetMinX(s) + dx, y); }
+ -- (BOOL)portraitControls { return _layout.separated && self.bounds.size.height > self.bounds.size.width; }
+ -- (CGPoint)portraitThumbCenter:(BOOL)right {
+ - CGRect s = self.safe;
+ - return CGPointMake(CGRectGetMinX(s) + s.size.width * (right ? 0.72 : 0.28),
+ - CGRectGetMinY(s) + s.size.height * 0.58);
+ -}
+ -- (CGPoint)faceCenter { CGRect s = self.corner; CGFloat u = self.u;
+ - if (self.portraitControls) return [self portraitThumbCenter:YES];
+ - CGFloat edge = _layout.separated ? self.faceGap + self.faceHitR : 0;
+ - return side(s, 1, MAX(K_FACE_X * u, edge), CGRectGetMaxY(s) - MAX(K_PAD_Y * u, edge)); }
+ -- (CGFloat)faceGap { return K_FACE_GAP * self.u; }
+ -- (CGFloat)faceR { return K_FACE_R * self.u; }
+ -- (CGFloat)faceHitR { return MAX(self.faceR * 1.25, _layout.separated ? 22 : 0); }
+ -- (CGPoint)facePos:(int)b { CGPoint c = self.faceCenter; CGFloat g = self.faceGap;
+ - switch (b) { case B_A: return CGPointMake(c.x + g, c.y); case B_B: return CGPointMake(c.x, c.y + g); case B_X: return CGPointMake(c.x, c.y - g); default: return CGPointMake(c.x - g, c.y); } }
+ -- (CGPoint)stickHome {
+ - if (self.portraitControls) {
+ - CGPoint p = [self portraitThumbCenter:NO];
+ - if (_full) p.x -= self.dpadR + 6 * self.u;
+ - return p;
+ +- (const DmPadGeo *)geo { // DmPadGeometry.h, computed again whenever the bounds, the safe area, the split or the layout changed
+ + CGRect b = self.bounds; UIEdgeInsets i = self.safeAreaInsets;
+ + if (!_geoValid || !CGRectEqualToRect(b, _geoBounds) || !UIEdgeInsetsEqualToEdgeInsets(i, _geoInsets) || _geoFull != _full) {
+ + _geo = dm_pad_geo(b, i, _layout, _full, _opts);
+ + _geoBounds = b; _geoInsets = i; _geoFull = _full; _geoValid = YES;
+ }
+ - CGRect s = self.corner;
+ - return side(s, 0, K_STICK_X * self.u, CGRectGetMaxY(s) - K_PAD_Y * self.u);
+ -}
+ + return &_geo;
+ +}
+ +- (CGFloat)u { return self.geo->u; } // 1 = 430 points tall (iPhone Pro Max landscape), times the player's size
+ +- (CGPoint)faceCenter { return self.geo->face; }
+ +- (CGFloat)faceGap { return self.geo->faceGap; }
+ +- (CGFloat)faceR { return self.geo->faceR; }
+ +- (CGFloat)faceHitR { return self.geo->faceHitR; }
+ +- (CGPoint)facePos:(int)b { return dm_pad_face_key(self.geo, b - B_A); } // B_A..B_Y are A, B, X, Y: the geometry's order
+ +- (CGPoint)stickHome { return self.geo->stick; }
+ - (CGPoint)stickCentre:(int)i { return i ? _rCentre : self.stickHome; }
+ -- (CGFloat)stickR:(int)i { return (i ? K_RSTICK_R : K_STICK_R) * self.u; }
+ +- (CGFloat)stickR:(int)i { return i ? K_RSTICK_R * self.u : self.geo->stickR; }
+ - (BOOL)inRightArea:(CGPoint)p { CGRect b = _layout.controls; return p.x >= CGRectGetMaxX(b) - b.size.width * K_RSWIPE; }
+ -- (CGPoint)dpadCenter {
+ - if (self.portraitControls) {
+ - CGPoint p = [self portraitThumbCenter:NO];
+ - p.x += [self stickR:0] + 6 * self.u;
+ - return p;
+ - }
+ - CGRect s = self.corner; CGFloat u = self.u;
+ - return side(s, 0, K_LOW_X * u, CGRectGetMaxY(s) - K_LOW_Y * u);
+ -}
+ -- (CGFloat)dpadR { return K_DPAD_R * self.u; }
+ +- (CGPoint)dpadCenter { return self.geo->dpad; }
+ +- (CGFloat)dpadR { return self.geo->dpadR; }
+ static BOOL isDrop(int b) { return b == B_ZL || b == B_L || b == B_ZR || b == B_R; }
+ static BOOL isClick(int b) { return b == B_LS || b == B_RS; }
+ -- (CGPoint)dropCenter:(int)b { CGRect s = self.safe; CGFloat u = self.u; BOOL z = b == B_ZL || b == B_ZR;
+ - CGFloat edge = _layout.separated ? 22 : 0;
+ - return side(s, b == B_ZR || b == B_R, MAX((z ? K_ZL_X : K_L_X) * u, edge), CGRectGetMinY(s) + MAX((z ? K_ZL_Y : K_L_Y) * u, edge)); }
+ -- (CGFloat)dropR:(int)b { return (b == B_ZL || b == B_ZR ? K_ZL_R : K_L_R) * self.u; }
+ -- (CGPoint)clickCenter:(int)b { CGRect s = self.safe; CGFloat u = self.u; CGFloat edge = _layout.separated ? 22 : 0;
+ - return side(s, b == B_RS, MAX(K_CLICK_X * u, edge), CGRectGetMinY(s) + MAX(K_ZL_Y * u, edge)); }
+ -- (CGRect)pillRect:(int)b { CGRect s = self.safe; CGFloat u = self.u, w = K_PILL_W * u, h = K_PILL_H * u;
+ - CGPoint c = CGPointMake(CGRectGetMidX(s) + (b == B_PLUS ? K_PILL_DX : -K_PILL_DX) * u, CGRectGetMinY(s) + MAX(K_PILL_Y * u, _layout.separated ? 14 : 0)); // − on the left, + on the right
+ - return CGRectMake(c.x - w / 2, c.y - h / 2, w, h); }
--- (CGPoint)swapCenter { CGRect s = self.safe; return CGPointMake(CGRectGetMidX(s), CGRectGetMinY(s) + MAX(K_PILL_Y * self.u, _layout.separated ? 14 : 0)); }
++-- (CGPoint)menuCenter { CGRect s = self.safe; return CGPointMake(CGRectGetMidX(s), CGRectGetMinY(s) + MAX(K_PILL_Y * self.u, _layout.separated ? 14 : 0)); }
+ +static int dropIndex(int b) { return b == B_ZL ? DM_PAD_ZL : b == B_L ? DM_PAD_L : b == B_ZR ? DM_PAD_ZR : DM_PAD_R; }
+ +- (CGPoint)dropCenter:(int)b { return self.geo->drop[dropIndex(b)]; }
+ +- (CGFloat)dropR:(int)b { return self.geo->dropR[dropIndex(b)]; }
+ +- (CGPoint)clickCenter:(int)b { return self.geo->click[b == B_RS]; }
+ +- (CGRect)pillRect:(int)b { return self.geo->pill[b == B_PLUS ? DM_PAD_PLUS : DM_PAD_MINUS]; }
-+- (CGPoint)swapCenter { return self.geo->swap; }
+++- (CGPoint)menuCenter { return self.geo->swap; }
+
+ static CGFloat dist(CGPoint a, CGPoint b) { return hypot(a.x - b.x, a.y - b.y); }
+ - (CGFloat)buttonDistance:(int)b point:(CGPoint)p {
+ CGFloat u = self.u;
+ if (isDrop(b)) return dist(p, [self dropCenter:b]) / MAX([self dropR:b] * 1.35, _layout.separated ? 22 : 0);
+ - if (isClick(b)) return dist(p, [self clickCenter:b]) / MAX(K_CLICK_R * u * 1.35, _layout.separated ? 22 : 0);
+ + if (isClick(b)) return dist(p, [self clickCenter:b]) / MAX(self.geo->clickR * 1.35, _layout.separated ? 22 : 0);
+ CGRect r = CGRectInset([self pillRect:b], -8 * u, -8 * u);
+ if (!_layout.separated && !CGRectContainsPoint(r, p)) return CGFLOAT_MAX;
+ CGFloat w = MAX(r.size.width, _layout.separated ? 28 : 0), h = MAX(r.size.height, _layout.separated ? 28 : 0);
-@@ -313,7 +287,7 @@ - (int)btnAt:(CGPoint)p { // Locked at touch-down; nearest normalized target w
++@@ -338,7 +312,7 @@ - (int)btnAt:(CGPoint)p { // Locked at touch-down; nearest normalized target w
+ }
+ return best;
+ }
--- (CGFloat)swapDistance:(CGPoint)p { return dist(p, self.swapCenter) / MAX(K_SWAP_R * self.u * 1.6, _layout.separated ? 14 : 0); }
-+- (CGFloat)swapDistance:(CGPoint)p { return dist(p, self.swapCenter) / MAX(self.geo->swapR * 1.6, _layout.separated ? 14 : 0); }
- - (BOOL)inSwap:(CGPoint)p { return [self swapDistance:p] <= 1; }
- - (uint64_t)faceAt:(CGPoint)p { // keys under the finger in the diamond (between two keys both count)
- uint64_t m = 0; CGFloat r = self.faceHitR;
-@@ -442,7 +416,7 @@ - (void)reset { [_owner removeAllObjects]; [_faceOf removeAllObjects]; _stick[0]
++-- (CGFloat)menuDistance:(CGPoint)p { return dist(p, self.menuCenter) / MAX(K_MENU_R * self.u * 1.6, 22); }
+++- (CGFloat)menuDistance:(CGPoint)p { return dist(p, self.menuCenter) / MAX(self.geo->swapR * 1.6, 22); }
++ - (BOOL)menuTakes:(CGPoint)p { // the menu button takes a touch in its hit circle unless a locked key's target is nearer (the drawn keys are hidden in Tap and with a controller)
++ CGFloat d = [self menuDistance:p]; if (d > 1) return NO;
++ if (_tap || _padOff || (_layout.separated && !CGRectContainsPoint(_layout.controls, p))) return YES;
++@@ -466,7 +440,7 @@ - (void)reset { [_owner removeAllObjects]; [_faceOf removeAllObjects]; _stick[0]
+ // ---- Shapes ----
+ static CGPathRef circlePath(CGPoint c, CGFloat r) { return CGPathCreateWithEllipseInRect(CGRectMake(c.x - r, c.y - r, 2 * r, 2 * r), NULL); }
+ static CGPathRef dropPath(CGPoint c, CGFloat r, CGFloat tipAngle, CGFloat round) { // teardrop: circle + two tangents meeting at the tip, tip slightly rounded
+ - CGFloat d = r * K_DROP_TIP, th = acos(r / d);
+ + CGFloat d = r * DM_PAD_DROP_TIP, th = acos(r / d);
+ CGPoint tip = CGPointMake(c.x + d * cos(tipAngle), c.y + d * sin(tipAngle));
+ CGPoint p1 = CGPointMake(c.x + r * cos(tipAngle + th), c.y + r * sin(tipAngle + th)), p2 = CGPointMake(c.x + r * cos(tipAngle - th), c.y + r * sin(tipAngle - th));
+ CGMutablePathRef p = CGPathCreateMutable();
-@@ -486,17 +460,17 @@ - (void)layoutSubviews {
++@@ -509,20 +483,20 @@ - (void)layoutSubviews {
+ BOOL right = b == B_ZR || b == B_R; CGPoint c = [self dropCenter:b];
+ setPath(_key[b], dropPath(c, [self dropR:b], right ? -M_PI_4 : -3 * M_PI_4, 4 * u)); placeLabel(_label[b], c, 16 * u); // tip toward the near top corner
+ } else if (isClick(b)) {
+ - CGPoint c = [self clickCenter:b]; setPath(_key[b], circlePath(c, K_CLICK_R * u)); placeLabel(_label[b], c, 13 * u);
+ + CGPoint c = [self clickCenter:b]; setPath(_key[b], circlePath(c, self.geo->clickR)); placeLabel(_label[b], c, 13 * u);
+ } else {
+ CGRect rc = [self pillRect:b];
+ setPath(_key[b], CGPathCreateWithRoundedRect(rc, rc.size.height / 2, rc.size.height / 2, NULL)); placeLabel(_label[b], CGPointMake(CGRectGetMidX(rc), CGRectGetMidY(rc)), 18 * u);
+ }
+ }
+ static const struct { int b; CGFloat angle; } arms[] = {{B_UP, 0}, {B_RIGHT, M_PI_2}, {B_DOWN, M_PI}, {B_LEFT, -M_PI_2}}; // the same arm in 90° steps
+ - for (int i = 0; i < 4; i++) setPath(_key[arms[i].b], armPath(self.dpadCenter, self.dpadR, K_ARM_W * u, K_ARM_IN * u, arms[i].angle, 6 * u));
-- CGPoint sc = self.swapCenter; setPath(_swap, circlePath(sc, K_SWAP_R * u)); placeLabel(_swapLabel, sc, 12 * u);
++- CGFloat mr = K_MENU_R * u; CGPoint mc = self.menuCenter;
+ + for (int i = 0; i < 4; i++) setPath(_key[arms[i].b], armPath(self.dpadCenter, self.dpadR, self.geo->armW, self.geo->armIn, arms[i].angle, 6 * u));
-+ CGPoint sc = self.swapCenter; setPath(_swap, circlePath(sc, self.geo->swapR)); placeLabel(_swapLabel, sc, 12 * u);
+++ CGFloat mr = self.geo->swapR; CGPoint mc = self.menuCenter;
++ _menu.frame = CGRectMake(mc.x - mr, mc.y - mr, 2 * mr, 2 * mr); _menu.layer.cornerRadius = mr;
++ _menu.layer.shadowPath = [UIBezierPath bezierPathWithOvalInRect:_menu.bounds].CGPath;
++ [_menu setImage:[UIImage systemImageNamed:@"gearshape.fill" withConfiguration:[UIImageSymbolConfiguration configurationWithPointSize:13 * u weight:UIImageSymbolWeightSemibold]] forState:UIControlStateNormal];
+ setPath(_ring, circlePath(self.stickHome, [self stickR:0]));
+ - CGFloat k = K_KNOB_R * u; _knob.bounds = CGRectMake(0, 0, 2 * k, 2 * k);
+ + CGFloat k = self.geo->knobR; _knob.bounds = CGRectMake(0, 0, 2 * k, 2 * k);
+ setPath(_knob, circlePath(CGPointMake(k, k), k));
+ CAShapeLayer *in = (CAShapeLayer *)_knob.sublayers.firstObject; CGPathRef ip = circlePath(CGPointMake(k, k), k * 0.62);
+ in.frame = _knob.bounds; in.path = ip; CGPathRelease(ip);
+ diff --git a/dm/ios/Sources/DmPadGeometry.h b/dm/ios/Sources/DmPadGeometry.h
+ new file mode 100644
-index 0000000..a03f3ec
++index 0000000..92697ef
+ --- /dev/null
+ +++ b/dm/ios/Sources/DmPadGeometry.h
+ @@ -0,0 +1,176 @@
+ +// Where the virtual gamepad's controls are drawn (DmGamepad.m), as pure functions of the view's bounds, its safe-area insets, the DmLayout split
+ +// and the player's options, so the placement is tested without a device (dm/ios/tests/test_pad_geometry.py).
+ +// Every length is in u (1 = 430 points tall, iPhone Pro Max landscape), x measured from the outer edge, the right half mirrors.
+ +// The options come from the environment (the shell's settings, Documents/env.txt): DM_TOUCH_SIZE = small | medium | large scales u.
+ +// An option never moves a control off the screen or onto another one: a size that does not fit falls back step by step to the size that does
+ +// (dm_pad_geo_fits: every drawn shape inside its rectangle, no two drawn shapes overlap).
+ +#pragma once
+ +#import "DmLayout.h"
+ +#include <math.h>
+ +#include <string.h>
+ +
+ +NS_ASSUME_NONNULL_BEGIN
+ +
+ +enum { DM_PAD_ZL = 0, DM_PAD_L, DM_PAD_ZR, DM_PAD_R }; // the order of DmPadGeo.drop
+ +enum { DM_PAD_MINUS = 0, DM_PAD_PLUS }; // the order of DmPadGeo.pill
+ +
+ +typedef struct {
+ + CGFloat scale; // DM_TOUCH_SIZE: 0.8 small, 1 medium (the built-in size), 1.2 large
+ +} DmPadOptions;
+ +
+ +typedef struct {
+ + CGFloat u; // the unit every size below is a multiple of
+ + CGRect safe, corner; // the top row lives in safe, the bottom-corner controls in corner (reaching halfway into the side insets)
+ + BOOL portrait; // separated and taller than wide: both thumbs' clusters sit in the controls region's middle band
+ + CGPoint face; CGFloat faceGap, faceR, faceHitR; // the A B X Y diamond: centre, key distance from it, key radius, hit radius
+ + CGPoint stick; CGFloat stickR, knobR; // the left stick: ring centre and radius, knob radius
+ + CGPoint dpad; CGFloat dpadR, armW, armIn; // the D-pad (3D only): centre, arm outer edge, arm half-width, arm tip from the centre
+ + CGPoint drop[4]; CGFloat dropR[4]; // ZL, L, ZR, R: teardrop centres and radii
+ + CGPoint click[2]; CGFloat clickR; // L3, R3 (3D only)
+ + CGRect pill[2]; // −, +
-+ CGPoint swap; CGFloat swapR; // the layout button
+++ CGPoint swap; CGFloat swapR; // the menu button
+ +} DmPadGeo;
+ +
+ +// Layout constants (in u), the values DmGamepad.m has always used.
+ +static const CGFloat
+ + DM_PAD_ZL_X = 44, DM_PAD_ZL_Y = 40, DM_PAD_ZL_R = 26, // ZL / ZR: centre from the outer edge and the top, radius
+ + DM_PAD_L_X = 44, DM_PAD_L_Y = 108, DM_PAD_L_R = 24, // L / R: same column as ZL / ZR
+ + DM_PAD_DROP_TIP = 1.45, // teardrop tip distance from the centre (times the radius)
+ + DM_PAD_CORNER_IN = 0.5, // how far the bottom-corner controls reach into the side safe-area inset
+ + DM_PAD_FACE_X = 81, DM_PAD_Y = 82, // diamond centre from the outer edge; diamond and stick centre from the bottom
+ + DM_PAD_FACE_GAP = 50, DM_PAD_FACE_R = 27, // diamond: key distance from the centre, key radius
+ + DM_PAD_PILL_DX = 52, DM_PAD_PILL_Y = 22, DM_PAD_PILL_W = 50, DM_PAD_PILL_H = 28, // − / +: centre from the midline and from the top, size
-+ DM_PAD_SWAP_R = 15, // layout button: top centre, level with − / +
+++ DM_PAD_SWAP_R = 15, // menu button: top centre, level with − / +
+ + DM_PAD_STICK_X = 81, DM_PAD_STICK_R = 77, DM_PAD_KNOB_R = 30, // left stick: mirrors the diamond
+ + DM_PAD_LOW_X = 231, DM_PAD_LOW_Y = 64, // D-pad centre from the outer edge and the bottom
+ + DM_PAD_DPAD_R = 48, DM_PAD_ARM_W = 17, DM_PAD_ARM_IN = 11,
+ + DM_PAD_CLICK_X = 102, DM_PAD_CLICK_R = 17; // L3 / R3: centre from the outer edge (level with ZL / ZR), radius
+ +
+ +// DM_TOUCH_SIZE: small, medium (also unset, empty or unknown) or large.
+ +static inline CGFloat dm_pad_size_scale(const char *_Nullable v) {
+ + if (v && !strcmp(v, "small")) return 0.8;
+ + if (v && !strcmp(v, "large")) return 1.2;
+ + return 1;
+ +}
+ +
+ +static inline CGPoint dm_pad_side(CGRect s, int right, CGFloat dx, CGFloat y) { return CGPointMake(right ? CGRectGetMaxX(s) - dx : CGRectGetMinX(s) + dx, y); }
+ +
+ +// The geometry at one unit, with no fitting.
+ +static inline DmPadGeo dm_pad_geo_at(CGRect bounds, UIEdgeInsets insets, DmLayout layout, BOOL full, CGFloat u) {
+ + DmPadGeo g; memset(&g, 0, sizeof g);
+ + BOOL sep = layout.separated;
+ + g.u = u;
+ + g.safe = CGRectIntersection(layout.controls, UIEdgeInsetsInsetRect(bounds, dm_game_safe_insets(bounds, insets, layout.topSafeOnly)));
+ + UIEdgeInsets ci = insets; ci.left *= DM_PAD_CORNER_IN; ci.right *= DM_PAD_CORNER_IN;
+ + g.corner = sep ? g.safe : UIEdgeInsetsInsetRect(bounds, ci);
+ + g.portrait = sep && bounds.size.height > bounds.size.width;
+ + CGRect s = g.safe, c = g.corner;
+ + CGPoint thumbL = CGPointMake(CGRectGetMinX(s) + s.size.width * 0.28, CGRectGetMinY(s) + s.size.height * 0.58);
+ + CGPoint thumbR = CGPointMake(CGRectGetMinX(s) + s.size.width * 0.72, thumbL.y);
+ + g.faceGap = DM_PAD_FACE_GAP * u; g.faceR = DM_PAD_FACE_R * u; g.faceHitR = MAX(g.faceR * 1.25, sep ? 22 : 0);
+ + g.stickR = DM_PAD_STICK_R * u; g.knobR = DM_PAD_KNOB_R * u;
+ + g.dpadR = DM_PAD_DPAD_R * u; g.armW = DM_PAD_ARM_W * u; g.armIn = DM_PAD_ARM_IN * u;
+ + if (g.portrait) {
+ + g.face = thumbR;
+ + g.stick = thumbL; if (full) g.stick.x -= g.dpadR + 6 * u;
+ + g.dpad = CGPointMake(thumbL.x + g.stickR + 6 * u, thumbL.y);
+ + } else {
+ + CGFloat edge = sep ? g.faceGap + g.faceHitR : 0;
+ + g.face = dm_pad_side(c, 1, MAX(DM_PAD_FACE_X * u, edge), CGRectGetMaxY(c) - MAX(DM_PAD_Y * u, edge));
+ + g.stick = dm_pad_side(c, 0, DM_PAD_STICK_X * u, CGRectGetMaxY(c) - DM_PAD_Y * u);
+ + g.dpad = dm_pad_side(c, 0, DM_PAD_LOW_X * u, CGRectGetMaxY(c) - DM_PAD_LOW_Y * u);
+ + }
+ + CGFloat edge = sep ? 22 : 0;
+ + for (int i = 0; i < 4; i++) {
+ + BOOL z = i == DM_PAD_ZL || i == DM_PAD_ZR;
+ + g.drop[i] = dm_pad_side(s, i >= DM_PAD_ZR, MAX((z ? DM_PAD_ZL_X : DM_PAD_L_X) * u, edge), CGRectGetMinY(s) + MAX((z ? DM_PAD_ZL_Y : DM_PAD_L_Y) * u, edge));
+ + g.dropR[i] = (z ? DM_PAD_ZL_R : DM_PAD_L_R) * u;
+ + }
+ + for (int i = 0; i < 2; i++) g.click[i] = dm_pad_side(s, i, MAX(DM_PAD_CLICK_X * u, edge), CGRectGetMinY(s) + MAX(DM_PAD_ZL_Y * u, edge));
+ + g.clickR = DM_PAD_CLICK_R * u;
+ + CGFloat top = CGRectGetMinY(s) + MAX(DM_PAD_PILL_Y * u, sep ? 14 : 0), w = DM_PAD_PILL_W * u, h = DM_PAD_PILL_H * u;
+ + for (int i = 0; i < 2; i++) {
+ + CGFloat x = CGRectGetMidX(s) + (i == DM_PAD_PLUS ? DM_PAD_PILL_DX : -DM_PAD_PILL_DX) * u; // − on the left, + on the right
+ + g.pill[i] = CGRectMake(x - w / 2, top - h / 2, w, h);
+ + }
+ + g.swap = CGPointMake(CGRectGetMidX(s), top); g.swapR = DM_PAD_SWAP_R * u;
+ + return g;
+ +}
+ +
+ +// The face key positions: A right, B bottom, X top, Y left (i = 0..3 in that order).
+ +static inline CGPoint dm_pad_face_key(const DmPadGeo *g, int i) {
+ + CGPoint c = g->face; CGFloat d = g->faceGap;
+ + switch (i) { case 0: return CGPointMake(c.x + d, c.y); case 1: return CGPointMake(c.x, c.y + d); case 2: return CGPointMake(c.x, c.y - d); default: return CGPointMake(c.x - d, c.y); }
+ +}
+ +static inline CGPoint dm_pad_drop_tip(const DmPadGeo *g, int i) {
+ + CGFloat a = i >= DM_PAD_ZR ? -M_PI_4 : -3 * M_PI_4, d = g->dropR[i] * DM_PAD_DROP_TIP; // tip toward the near top corner
+ + return CGPointMake(g->drop[i].x + d * cos(a), g->drop[i].y + d * sin(a));
+ +}
+ +
+ +// The drawn shapes as circles and rectangles, for the fit test and for tests: each with the rectangle it must stay inside.
+ +typedef struct { CGPoint c; CGFloat r; CGRect rect; BOOL isRect; CGRect within; int id; } DmPadShape;
+ +enum { DM_PAD_SHAPE_FACE = 0, DM_PAD_SHAPE_STICK = 4, DM_PAD_SHAPE_DPAD, DM_PAD_SHAPE_DROP, DM_PAD_SHAPE_TIP = 10, DM_PAD_SHAPE_CLICK = 14, DM_PAD_SHAPE_PILL = 16, DM_PAD_SHAPE_SWAP = 18 };
+ +static inline int dm_pad_shapes(const DmPadGeo *g, BOOL full, DmPadShape out[_Nonnull 20]) {
+ + int n = 0;
+ + for (int i = 0; i < 4; i++) out[n++] = (DmPadShape){dm_pad_face_key(g, i), g->faceR, CGRectNull, NO, g->corner, DM_PAD_SHAPE_FACE + i};
+ + out[n++] = (DmPadShape){g->stick, g->stickR, CGRectNull, NO, g->corner, DM_PAD_SHAPE_STICK};
+ + if (full) out[n++] = (DmPadShape){g->dpad, hypot(g->dpadR, g->armW), CGRectNull, NO, g->corner, DM_PAD_SHAPE_DPAD};
+ + for (int i = 0; i < 4; i++) {
+ + out[n++] = (DmPadShape){g->drop[i], g->dropR[i], CGRectNull, NO, g->safe, DM_PAD_SHAPE_DROP + i};
+ + out[n++] = (DmPadShape){dm_pad_drop_tip(g, i), 0, CGRectNull, NO, g->safe, DM_PAD_SHAPE_TIP + i};
+ + }
+ + if (full) for (int i = 0; i < 2; i++) out[n++] = (DmPadShape){g->click[i], g->clickR, CGRectNull, NO, g->safe, DM_PAD_SHAPE_CLICK + i};
+ + for (int i = 0; i < 2; i++) out[n++] = (DmPadShape){CGPointZero, 0, g->pill[i], YES, g->safe, DM_PAD_SHAPE_PILL + i};
+ + out[n++] = (DmPadShape){g->swap, g->swapR, CGRectNull, NO, g->safe, DM_PAD_SHAPE_SWAP};
+ + return n;
+ +}
+ +static inline BOOL dm_pad_shape_inside(const DmPadShape *s) {
+ + const CGFloat e = 0.5;
+ + CGRect b = s->isRect ? s->rect : CGRectMake(s->c.x - s->r, s->c.y - s->r, 2 * s->r, 2 * s->r);
+ + return CGRectGetMinX(b) >= CGRectGetMinX(s->within) - e && CGRectGetMaxX(b) <= CGRectGetMaxX(s->within) + e
+ + && CGRectGetMinY(b) >= CGRectGetMinY(s->within) - e && CGRectGetMaxY(b) <= CGRectGetMaxY(s->within) + e;
+ +}
+ +static inline BOOL dm_pad_shapes_overlap(const DmPadShape *a, const DmPadShape *b) {
+ + const CGFloat e = 0.5;
+ + if (a->isRect && b->isRect) return CGRectIntersectsRect(CGRectInset(a->rect, e, e), CGRectInset(b->rect, e, e));
+ + if (a->isRect || b->isRect) {
+ + const DmPadShape *r = a->isRect ? a : b, *c = a->isRect ? b : a;
+ + CGFloat nx = fmax(CGRectGetMinX(r->rect), fmin(c->c.x, CGRectGetMaxX(r->rect))), ny = fmax(CGRectGetMinY(r->rect), fmin(c->c.y, CGRectGetMaxY(r->rect)));
+ + return hypot(c->c.x - nx, c->c.y - ny) < c->r - e;
+ + }
+ + return hypot(a->c.x - b->c.x, a->c.y - b->c.y) < a->r + b->r - e;
+ +}
+ +// A teardrop's tip belongs to its own circle; the tips are points, so they only count when they land inside another shape.
+ +static inline BOOL dm_pad_same_control(const DmPadShape *a, const DmPadShape *b) {
+ + return (a->id >= DM_PAD_SHAPE_DROP && a->id < DM_PAD_SHAPE_TIP && b->id == a->id + 4) || (b->id >= DM_PAD_SHAPE_DROP && b->id < DM_PAD_SHAPE_TIP && a->id == b->id + 4);
+ +}
+ +// Every drawn shape inside its rectangle and no two overlapping.
+ +static inline BOOL dm_pad_geo_fits(const DmPadGeo *g, BOOL full) {
+ + DmPadShape s[20]; int n = dm_pad_shapes(g, full, s);
+ + for (int i = 0; i < n; i++) {
+ + if (!dm_pad_shape_inside(&s[i])) return NO;
+ + for (int j = i + 1; j < n; j++) if (!dm_pad_same_control(&s[i], &s[j]) && dm_pad_shapes_overlap(&s[i], &s[j])) return NO;
+ + }
+ + return YES;
+ +}
+ +
+ +// The unit of the built-in size: the screen height (inside the controls region when it is split off), up to 1.3.
+ +static inline CGFloat dm_pad_base_unit(CGRect bounds, UIEdgeInsets insets, DmLayout layout) {
+ + if (!layout.separated) return MIN(bounds.size.height / 430.0, 1.3);
+ + CGRect s = CGRectIntersection(layout.controls, UIEdgeInsetsInsetRect(bounds, dm_game_safe_insets(bounds, insets, layout.topSafeOnly)));
+ + return MIN(MIN(s.size.height / 430.0, s.size.width / 700.0), 1.3);
+ +}
+ +
+ +// The geometry the gamepad draws: the player's size, moved toward the built-in size in steps until it fits (a large size that does
+ +// not fit gets smaller; a small one in a split region whose minimum edges crowd it gets larger). The built-in size is never changed.
+ +static inline DmPadGeo dm_pad_geo(CGRect bounds, UIEdgeInsets insets, DmLayout layout, BOOL full, DmPadOptions o) {
+ + CGFloat u0 = dm_pad_base_unit(bounds, insets, layout), scale = o.scale > 0 ? o.scale : 1;
+ + DmPadGeo g = dm_pad_geo_at(bounds, insets, layout, full, u0 * scale);
+ + while (scale != 1 && !dm_pad_geo_fits(&g, full)) {
+ + scale = scale > 1 ? fmax(1, scale - 0.05) : fmin(1, scale + 0.05);
+ + g = dm_pad_geo_at(bounds, insets, layout, full, u0 * scale);
+ + }
+ + return g;
+ +}
+ +
+ +NS_ASSUME_NONNULL_END
+ diff --git a/dm/ios/tests/pad_geometry/UIKit/UIKit.h b/dm/ios/tests/pad_geometry/UIKit/UIKit.h
+ new file mode 100644
+ index 0000000..6643574
+ --- /dev/null
+ +++ b/dm/ios/tests/pad_geometry/UIKit/UIKit.h
+ @@ -0,0 +1,11 @@
+ +// The few UIKit names DmLayout.h and DmPadGeometry.h use, so their pure geometry builds and runs on the Mac (test_pad_geometry.py).
+ +#pragma once
+ +#import <Foundation/Foundation.h>
+ +#import <CoreGraphics/CoreGraphics.h>
+ +typedef struct { CGFloat top, left, bottom, right; } UIEdgeInsets;
+ +static const UIEdgeInsets UIEdgeInsetsZero = {0, 0, 0, 0};
+ +static inline UIEdgeInsets UIEdgeInsetsMake(CGFloat t, CGFloat l, CGFloat b, CGFloat r) { return (UIEdgeInsets){t, l, b, r}; }
+ +static inline CGRect UIEdgeInsetsInsetRect(CGRect r, UIEdgeInsets i) {
+ + r.origin.x += i.left; r.origin.y += i.top; r.size.width -= i.left + i.right; r.size.height -= i.top + i.bottom; return r;
+ +}
+ +@class UIView;
+ diff --git a/dm/ios/tests/pad_geometry/pad_geometry_check.m b/dm/ios/tests/pad_geometry/pad_geometry_check.m
+ new file mode 100644
+ index 0000000..f02eccc
+ --- /dev/null
+ +++ b/dm/ios/tests/pad_geometry/pad_geometry_check.m
+ @@ -0,0 +1,102 @@
+ +// The virtual gamepad's placement (DmPadGeometry.h) on the screens it runs on, checked on the Mac: built and run by test_pad_geometry.py.
+ +// Every line that starts with FAIL is a broken rule; the exit status is the number of them (capped).
+ +#import <UIKit/UIKit.h>
+ +#import "DmPadGeometry.h"
+ +#include <stdio.h>
+ +
+ +typedef struct { const char *name; CGRect bounds; UIEdgeInsets insets; BOOL partiallyOpen; CGRect division; } Screen;
+ +
+ +static int g_fail;
+ +#define FAIL(...) do { printf("FAIL: "); printf(__VA_ARGS__); printf("\n"); g_fail++; } while (0)
+ +#define NEAR(a, b) (fabs((a) - (b)) < 0.01)
+ +
+ +static const char *k_sizes[] = {"small", "medium", "large"};
+ +
+ +static DmPadGeo geo(const Screen *s, BOOL full, DmPadOptions o) {
+ + return dm_pad_geo(s->bounds, s->insets, dm_layout(s->bounds, s->insets, s->partiallyOpen, s->division), full, o);
+ +}
+ +
+ +static void check_fits(const Screen *s, BOOL full, const char *size, DmPadOptions o) {
+ + DmPadGeo g = geo(s, full, o);
+ + DmPadShape sh[20]; int n = dm_pad_shapes(&g, full, sh);
+ + for (int i = 0; i < n; i++) {
+ + if (!dm_pad_shape_inside(&sh[i])) FAIL("%s %s %s: shape %d leaves its rectangle", s->name, full ? "3D" : "2D", size, sh[i].id);
+ + for (int j = i + 1; j < n; j++)
+ + if (!dm_pad_same_control(&sh[i], &sh[j]) && dm_pad_shapes_overlap(&sh[i], &sh[j]))
+ + FAIL("%s %s %s: shapes %d and %d overlap", s->name, full ? "3D" : "2D", size, sh[i].id, sh[j].id);
+ + }
+ + // Nothing outside the screen at all, side insets included only up to the corner rule.
+ + CGRect b = s->bounds;
+ + for (int i = 0; i < n; i++) {
+ + CGRect r = sh[i].isRect ? sh[i].rect : CGRectMake(sh[i].c.x - sh[i].r, sh[i].c.y - sh[i].r, 2 * sh[i].r, 2 * sh[i].r);
+ + if (!CGRectContainsRect(CGRectInset(b, -0.5, -0.5), r)) FAIL("%s %s %s: shape %d is off the screen", s->name, full ? "3D" : "2D", size, sh[i].id);
+ + }
+ +}
+ +
+ +int main(void) {
+ + const Screen screens[] = {
+ + // iPhone landscape (the phone's only orientation): the Dynamic Island or notch on one side, the home indicator at the bottom.
+ + {"iPhone SE landscape", {{0, 0}, {667, 375}}, {0, 0, 0, 0}, NO, CGRectNull},
+ + {"iPhone mini landscape", {{0, 0}, {812, 375}}, {0, 50, 21, 50}, NO, CGRectNull},
+ + {"iPhone landscape", {{0, 0}, {852, 393}}, {0, 59, 21, 59}, NO, CGRectNull},
+ + {"iPhone Pro Max landscape", {{0, 0}, {932, 430}}, {0, 59, 21, 59}, NO, CGRectNull},
+ + {"iPhone 17 Pro Max landscape", {{0, 0}, {956, 440}}, {0, 62, 20, 62}, NO, CGRectNull},
+ + // iPad: every orientation; portrait splits the picture from the controls.
+ + {"iPad mini landscape", {{0, 0}, {1133, 744}}, {24, 0, 20, 0}, NO, CGRectNull},
+ + {"iPad Pro 13 landscape", {{0, 0}, {1376, 1032}}, {24, 0, 20, 0}, NO, CGRectNull},
+ + {"iPad mini portrait", {{0, 0}, {744, 1133}}, {24, 0, 20, 0}, NO, CGRectNull},
+ + {"iPad Pro 13 portrait", {{0, 0}, {1032, 1376}}, {24, 0, 20, 0}, NO, CGRectNull},
+ + // Split portrait windows the Duo harness uses, and iPhone Duo's inner display (patch 0061): portrait with the fold, tabletop half open.
+ + {"portrait 390x844", {{0, 0}, {390, 844}}, {59, 0, 34, 0}, NO, CGRectNull},
+ + {"portrait 320x568", {{0, 0}, {320, 568}}, {50, 0, 24, 0}, NO, CGRectNull},
+ + {"Duo inner portrait", {{0, 0}, {669, 951}}, {62, 0, 34, 0}, NO, {{0, 455.5}, {669, 40}}},
+ + {"Duo inner landscape", {{0, 0}, {951, 669}}, {0, 62, 20, 62}, NO, CGRectNull},
+ + {"Duo tabletop", {{0, 0}, {951, 669}}, {0, 62, 20, 62}, YES, {{0, 314.5}, {951, 40}}},
+ + };
+ + int nscreens = (int)(sizeof screens / sizeof *screens);
+ +
+ + // Option parsing.
+ + if (dm_pad_size_scale(NULL) != 1 || dm_pad_size_scale("") != 1 || dm_pad_size_scale("medium") != 1 || dm_pad_size_scale("huge") != 1) FAIL("DM_TOUCH_SIZE: medium is the default");
+ + if (!(dm_pad_size_scale("small") < 1) || !(dm_pad_size_scale("large") > 1)) FAIL("DM_TOUCH_SIZE: small is smaller and large larger");
+ +
+ + // The built-in size is the layout DmGamepad.m has always drawn: iPhone Pro Max landscape, centres in points.
+ + {
+ + DmPadGeo g = geo(&screens[3], YES, (DmPadOptions){1});
+ + if (!NEAR(g.u, 1)) FAIL("Pro Max: u %.3f, want 1", g.u);
+ + if (!NEAR(g.face.x, 932 - 29.5 - 81) || !NEAR(g.face.y, 430 - 21 - 82)) FAIL("Pro Max: diamond at %.1f %.1f", g.face.x, g.face.y);
+ + if (!NEAR(g.stick.x, 29.5 + 81) || !NEAR(g.stick.y, 430 - 21 - 82)) FAIL("Pro Max: stick at %.1f %.1f", g.stick.x, g.stick.y);
+ + if (!NEAR(g.dpad.x, 29.5 + 231) || !NEAR(g.dpad.y, 430 - 21 - 64)) FAIL("Pro Max: D-pad at %.1f %.1f", g.dpad.x, g.dpad.y);
+ + if (!NEAR(g.drop[DM_PAD_ZL].x, 59 + 44) || !NEAR(g.drop[DM_PAD_ZL].y, 40) || !NEAR(g.drop[DM_PAD_R].x, 932 - 59 - 44) || !NEAR(g.drop[DM_PAD_R].y, 108)) FAIL("Pro Max: shoulders moved");
+ + if (!NEAR(g.click[1].x, 932 - 59 - 102) || !NEAR(g.pill[DM_PAD_PLUS].origin.x, 466 + 52 - 25) || !NEAR(g.swap.x, 466) || !NEAR(g.swap.y, 22)) FAIL("Pro Max: top row moved");
+ + if (!NEAR(g.stickR, 77) || !NEAR(g.faceR, 27) || !NEAR(g.dpadR, 48)) FAIL("Pro Max: sizes changed");
+ + // Portrait split (the Duo harness's numbers): the thumbs' centres at 28 % and 72 % of the controls, 58 % down.
+ + DmLayout l = dm_layout(screens[9].bounds, screens[9].insets, NO, CGRectNull);
+ + DmPadGeo p = dm_pad_geo(screens[9].bounds, screens[9].insets, l, NO, (DmPadOptions){1});
+ + CGRect s = p.safe;
+ + if (!p.portrait || !NEAR(p.face.x, CGRectGetMinX(s) + 0.72 * s.size.width) || !NEAR(p.stick.x, CGRectGetMinX(s) + 0.28 * s.size.width)) FAIL("portrait: thumbs moved");
+ + }
+ +
+ + // Every size on every screen, 2D and 3D: inside, and no overlap.
+ + for (int i = 0; i < nscreens; i++)
+ + for (int full = 0; full < 2; full++)
+ + for (int z = 0; z < 3; z++) check_fits(&screens[i], full, k_sizes[z], (DmPadOptions){dm_pad_size_scale(k_sizes[z])});
+ +
+ + // Picture Fill (DM_PICTURE_FIT) only makes the game rectangle larger: the controls keep the full view and their places.
+ + for (int i = 0; i < nscreens; i++) {
+ + DmLayout fit = dm_layout(screens[i].bounds, screens[i].insets, screens[i].partiallyOpen, screens[i].division);
+ + DmLayout fill = dm_layout_picture(fit, screens[i].bounds, YES);
+ + DmPadGeo a = dm_pad_geo(screens[i].bounds, screens[i].insets, fit, YES, (DmPadOptions){1.2}), b = dm_pad_geo(screens[i].bounds, screens[i].insets, fill, YES, (DmPadOptions){1.2});
+ + if (!CGPointEqualToPoint(a.face, b.face) || !CGPointEqualToPoint(a.stick, b.stick) || a.u != b.u) FAIL("%s: the picture fill moved the controls", screens[i].name);
+ + }
+ +
+ + // The sizes keep their order everywhere, and on a phone in landscape large really is larger and small smaller.
+ + for (int i = 0; i < nscreens; i++)
+ + for (int full = 0; full < 2; full++) {
+ + CGFloat us = geo(&screens[i], full, (DmPadOptions){0.8}).u, um = geo(&screens[i], full, (DmPadOptions){1}).u, ul = geo(&screens[i], full, (DmPadOptions){1.2}).u;
+ + if (!(us < um) || ul < um) FAIL("%s %s: sizes out of order (%.3f %.3f %.3f)", screens[i].name, full ? "3D" : "2D", us, um, ul);
+ + if (i <= 4 && !(ul > um * 1.15)) FAIL("%s %s: large is not larger (%.3f vs %.3f)", screens[i].name, full ? "3D" : "2D", ul, um);
+ + }
+ +
+ + if (!g_fail) printf("ok: %d screens\n", nscreens);
+ + return g_fail > 100 ? 100 : g_fail;
+ +}
+ diff --git a/dm/ios/tests/test_pad_geometry.py b/dm/ios/tests/test_pad_geometry.py
+ new file mode 100644
+ index 0000000..9854dfd
+ --- /dev/null
+ +++ b/dm/ios/tests/test_pad_geometry.py
+ @@ -0,0 +1,34 @@
+ +"""The virtual gamepad's placement (dm/ios/Sources/DmPadGeometry.h), built and run on the Mac.
+ +
+ +pad_geometry/pad_geometry_check.m runs the same pure functions DmGamepad.m draws, hit-tests and grabs with, on the iPhone, iPad
+ +and iPhone Duo screens (portrait, landscape, tabletop), in the 2D and the 3D layout and every DM_TOUCH_SIZE: every control stays
+ +inside the safe area (the bottom corners reach halfway into the side insets, as always), no two drawn controls overlap, and the
+ +built-in size is the layout the gamepad has always drawn. pad_geometry/UIKit/UIKit.h stands in for the few UIKit names the
+ +headers use, so no simulator is needed.
+ +"""
+ +import shutil
+ +import subprocess
+ +import sys
+ +import tempfile
+ +import unittest
+ +from pathlib import Path
+ +
+ +IOS = Path(__file__).resolve().parents[1]
+ +HERE = Path(__file__).resolve().parent / "pad_geometry"
+ +
+ +
+ [email protected](sys.platform == "darwin" and shutil.which("clang"), "needs clang and the Mac's CoreGraphics")
+ +class PadGeometryTests(unittest.TestCase):
+ + def test_every_screen_size_and_layout(self):
+ + with tempfile.TemporaryDirectory() as tmp:
+ + exe = Path(tmp) / "pad_geometry_check"
+ + subprocess.run(["clang", "-x", "objective-c", "-fobjc-arc", "-Wall", "-Werror", "-Wno-unused-function",
+ + "-I", str(HERE), "-I", str(IOS / "Sources"), str(HERE / "pad_geometry_check.m"),
+ + "-framework", "Foundation", "-framework", "CoreGraphics", "-o", str(exe)], check=True)
+ + run = subprocess.run([str(exe)], capture_output=True, text=True)
+ + self.assertEqual(run.returncode, 0, run.stdout + run.stderr)
+ + self.assertIn("ok:", run.stdout)
+ +
+ +
+ +if __name__ == "__main__":
+ + unittest.main()
diff --cc patches/0066-ios-touch-stick-place.patch
index 00000000,9b8578c0..03e20569
mode 000000,100644..100644
--- a/patches/0066-ios-touch-stick-place.patch
+++ b/patches/0066-ios-touch-stick-place.patch
@@@ -1,0 -1,156 +1,155 @@@
+ From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
+ From: v0lumehi <[email protected]>
-Date: Tue, 3 Nov 2026 16:15:21 +0100
++Date: Tue, 3 Nov 2026 18:24:38 +0100
+ Subject: [PATCH] ios: place the left touch stick higher or further in
+ (DM_TOUCH_STICK)
+
+ Players on large phones find the left stick too low in the corner
+ (LNPANDY/NX2Apple#558). DM_TOUCH_STICK=corner|higher|inward moves it: higher
+ lifts it up to 70 u toward the shoulders, inward moves it up to 60 u toward
+ the middle and takes the D-pad of the 3D layout along, which sits inward
+ of it. The diamond, the shoulders and the top row keep their place.
+
+ The move shrinks in steps until no drawn control overlaps another and
+ everything stays inside the safe area. On a screen with no room the stick
+ stays in the corner. Drawing, the hit-test and the grab read the same
+ place. test_pad_geometry.py checks every place on every screen, size and
+ layout, that the stick really moves on the phones and that the right side
+ does not.
+
+ Upstream-Status: pending (on seedds/nxnative#683)
+ Reason: Players on large phones want the left touch stick higher or further in (#558)
+ Issue: LNPANDY/NX2Apple#558
-
+ ---
+ dm/ios/Sources/DmGamepad.m | 7 ++++--
+ dm/ios/Sources/DmPadGeometry.h | 23 +++++++++++++++++
+ .../tests/pad_geometry/pad_geometry_check.m | 25 +++++++++++++++++--
+ 3 files changed, 51 insertions(+), 4 deletions(-)
+
+ diff --git a/dm/ios/Sources/DmGamepad.m b/dm/ios/Sources/DmGamepad.m
-index 3ea60b6..e4b6f15 100644
++index 6396f99..0b36d0a 100644
+ --- a/dm/ios/Sources/DmGamepad.m
+ +++ b/dm/ios/Sources/DmGamepad.m
-@@ -9,7 +9,7 @@
- // Touch-down ownership: locked keys first (shoulders, − / +, the layout button, L3 / R3), then whichever of the diamond, the D-pad and the left stick has the smallest distance / grab radius (the left stick's grab circle borders the D-pad),
++@@ -10,7 +10,7 @@
++ // Touch-down ownership: the menu button where it is nearer than a locked key (a view of its own, DmMenuButton), locked keys next (shoulders, − / +, L3 / R3), then whichever of the diamond, the D-pad and the left stick has the smallest distance / grab radius (the left stick's grab circle borders the D-pad),
+ // then in 3D the right stick for a touch in the right third.
+ // Everything hugs the edges and corners to leave the picture room; controls sit over the picture, one CAShapeLayer each: a press only swaps the fill, a stick only moves its knob, nothing redraws the whole screen.
+ -// Where each control sits is DmPadGeometry.h (pure, tested on the Mac); the player picks the size there through DM_TOUCH_SIZE (small, medium, large; a size that does not fit the screen moves toward medium).
+ +// Where each control sits is DmPadGeometry.h (pure, tested on the Mac); the player picks the size there through DM_TOUCH_SIZE (small, medium, large; a size that does not fit the screen moves toward medium) and the left stick's place through DM_TOUCH_STICK (corner, higher, inward).
+ // Touches no control takes (in 3D, those outside the right third) go to the game's touch screen when DmLayout assigns a local game viewport (pushGame; Super Mario Galaxy's pointer in handheld mode).
- // The virtual gamepad (layout button included) hides while a physical controller is connected and comes back when it is removed; unclaimed touches go to the game only when a local game viewport exists. Physical controllers keep every button (D-pad and right stick included) and map by position: bottom = B, right = A, left = Y, top = X (same positions as on a Switch controller); a Nintendo pad maps by its labels (pad_apple.h).
++ // The virtual gamepad (all but the menu button) hides while a physical controller is connected and comes back when it is removed; unclaimed touches go to the game only when a local game viewport exists. Physical controllers keep every button (D-pad and right stick included) and map by position: bottom = B, right = A, left = Y, top = X (same positions as on a Switch controller); a Nintendo pad maps by its labels (pad_apple.h).
+ // Each physical controller is a player of its own (local co-op), as on the Mac: the first one connected is player 1, together with the touch gamepad, the next player 2, and so on up to 8; see "Physical controllers" below.
-@@ -147,7 +147,7 @@ @implementation DmGamepad {
++@@ -157,7 +157,7 @@ @implementation DmGamepad {
+ CALayer *_controls; // holds every control layer: hidden while a physical controller is connected (the view itself stays, for game touches)
+ BOOL _padOff; // a physical controller is connected: every touch goes to the game
+ DmLayout _layout;
+ - DmPadOptions _opts; // the player's options from the environment (DM_TOUCH_SIZE)
+ + DmPadOptions _opts; // the player's options from the environment (DM_TOUCH_SIZE, DM_TOUCH_STICK)
+ DmPadGeo _geo; CGRect _geoBounds; UIEdgeInsets _geoInsets; BOOL _geoFull, _geoValid; // where the controls are, and what it was computed for
+ UITouch *_game[16]; // touches on the game's touch screen by finger slot
+ }
-@@ -172,6 +172,9 @@ - (instancetype)initWithFrame:(CGRect)f {
++@@ -182,6 +182,9 @@ - (instancetype)initWithFrame:(CGRect)f {
+ const char *size = getenv("DM_TOUCH_SIZE");
+ _opts.scale = dm_pad_size_scale(size);
+ if (size && *size) LOG("front end: touch controls size %s (DM_TOUCH_SIZE)", _opts.scale < 1 ? "small" : _opts.scale > 1 ? "large" : "medium");
+ + const char *stick = getenv("DM_TOUCH_STICK");
+ + _opts.stick = dm_pad_stick_place(stick);
+ + if (stick && *stick) LOG("front end: left stick %s (DM_TOUCH_STICK)", _opts.stick == DM_PAD_STICK_HIGHER ? "higher" : _opts.stick == DM_PAD_STICK_INWARD ? "inward" : "in the corner");
+ [self registerForTraitChanges:@[UITraitDisplayScale.class] withHandler:^(id<UITraitEnvironment> environment, UITraitCollection *previous) {
+ [(UIView *)environment setNeedsLayout];
+ }];
+ diff --git a/dm/ios/Sources/DmPadGeometry.h b/dm/ios/Sources/DmPadGeometry.h
-index a03f3ec..18dc25e 100644
++index 92697ef..86083ad 100644
+ --- a/dm/ios/Sources/DmPadGeometry.h
+ +++ b/dm/ios/Sources/DmPadGeometry.h
+ @@ -2,6 +2,7 @@
+ // and the player's options, so the placement is tested without a device (dm/ios/tests/test_pad_geometry.py).
+ // Every length is in u (1 = 430 points tall, iPhone Pro Max landscape), x measured from the outer edge, the right half mirrors.
+ // The options come from the environment (the shell's settings, Documents/env.txt): DM_TOUCH_SIZE = small | medium | large scales u.
+ +// DM_TOUCH_STICK = corner | higher | inward places the left stick (inward takes the D-pad along); the right-hand controls never move.
+ // An option never moves a control off the screen or onto another one: a size that does not fit falls back step by step to the size that does
+ // (dm_pad_geo_fits: every drawn shape inside its rectangle, no two drawn shapes overlap).
+ #pragma once
+ @@ -14,8 +15,10 @@ NS_ASSUME_NONNULL_BEGIN
+ enum { DM_PAD_ZL = 0, DM_PAD_L, DM_PAD_ZR, DM_PAD_R }; // the order of DmPadGeo.drop
+ enum { DM_PAD_MINUS = 0, DM_PAD_PLUS }; // the order of DmPadGeo.pill
+
+ +enum { DM_PAD_STICK_CORNER = 0, DM_PAD_STICK_HIGHER, DM_PAD_STICK_INWARD }; // DM_TOUCH_STICK
+ typedef struct {
+ CGFloat scale; // DM_TOUCH_SIZE: 0.8 small, 1 medium (the built-in size), 1.2 large
+ + int stick; // DM_TOUCH_STICK: where the left stick sits (the D-pad moves with it when it moves inward); the right-hand controls keep their place
+ } DmPadOptions;
+
+ typedef struct {
+ @@ -53,6 +56,14 @@ static inline CGFloat dm_pad_size_scale(const char *_Nullable v) {
+ return 1;
+ }
+
+ +// DM_TOUCH_STICK: corner (also unset, empty or unknown), higher or inward.
+ +static inline int dm_pad_stick_place(const char *_Nullable v) {
+ + if (v && !strcmp(v, "higher")) return DM_PAD_STICK_HIGHER;
+ + if (v && !strcmp(v, "inward")) return DM_PAD_STICK_INWARD;
+ + return DM_PAD_STICK_CORNER;
+ +}
+ +static const CGFloat DM_PAD_STICK_UP = 70, DM_PAD_STICK_IN = 60; // how far higher / inward moves the left stick (in u), at most
+ +
+ static inline CGPoint dm_pad_side(CGRect s, int right, CGFloat dx, CGFloat y) { return CGPointMake(right ? CGRectGetMaxX(s) - dx : CGRectGetMinX(s) + dx, y); }
+
+ // The geometry at one unit, with no fitting.
+ @@ -170,6 +181,18 @@ static inline DmPadGeo dm_pad_geo(CGRect bounds, UIEdgeInsets insets, DmLayout l
+ scale = scale > 1 ? fmax(1, scale - 0.05) : fmin(1, scale + 0.05);
+ g = dm_pad_geo_at(bounds, insets, layout, full, u0 * scale);
+ }
+ + // The left stick's place: higher lifts it toward the shoulders, inward moves it (and the D-pad, which sits inward of it) toward the
+ + // middle. The move shrinks in steps until nothing overlaps; on a screen with no room the stick stays in the corner.
+ + if (o.stick != DM_PAD_STICK_CORNER) {
+ + BOOL up = o.stick == DM_PAD_STICK_HIGHER;
+ + CGPoint stick = g.stick, dpad = g.dpad;
+ + for (CGFloat d = (up ? DM_PAD_STICK_UP : DM_PAD_STICK_IN) * g.u; d > 0; d -= 2 * g.u) {
+ + if (up) g.stick = CGPointMake(stick.x, stick.y - d);
+ + else { g.stick = CGPointMake(stick.x + d, stick.y); g.dpad = CGPointMake(dpad.x + d, dpad.y); }
+ + if (dm_pad_geo_fits(&g, full)) return g;
+ + }
+ + g.stick = stick; g.dpad = dpad;
+ + }
+ return g;
+ }
+
+ diff --git a/dm/ios/tests/pad_geometry/pad_geometry_check.m b/dm/ios/tests/pad_geometry/pad_geometry_check.m
+ index f02eccc..dc17483 100644
+ --- a/dm/ios/tests/pad_geometry/pad_geometry_check.m
+ +++ b/dm/ios/tests/pad_geometry/pad_geometry_check.m
+ @@ -76,10 +76,31 @@ int main(void) {
+ if (!p.portrait || !NEAR(p.face.x, CGRectGetMinX(s) + 0.72 * s.size.width) || !NEAR(p.stick.x, CGRectGetMinX(s) + 0.28 * s.size.width)) FAIL("portrait: thumbs moved");
+ }
+
+ - // Every size on every screen, 2D and 3D: inside, and no overlap.
+ + // Every size and stick place on every screen, 2D and 3D: inside, and no overlap.
+ + static const char *places[] = {"corner", "higher", "inward"};
+ + if (dm_pad_stick_place(NULL) != DM_PAD_STICK_CORNER || dm_pad_stick_place("left") != DM_PAD_STICK_CORNER || dm_pad_stick_place("higher") != DM_PAD_STICK_HIGHER
+ + || dm_pad_stick_place("inward") != DM_PAD_STICK_INWARD) FAIL("DM_TOUCH_STICK: corner is the default");
+ for (int i = 0; i < nscreens; i++)
+ for (int full = 0; full < 2; full++)
+ - for (int z = 0; z < 3; z++) check_fits(&screens[i], full, k_sizes[z], (DmPadOptions){dm_pad_size_scale(k_sizes[z])});
+ + for (int z = 0; z < 3; z++)
+ + for (int p = 0; p < 3; p++) {
+ + char what[64]; snprintf(what, sizeof what, "%s %s", k_sizes[z], places[p]);
+ + check_fits(&screens[i], full, what, (DmPadOptions){dm_pad_size_scale(k_sizes[z]), dm_pad_stick_place(places[p])});
+ + }
+ +
+ + // The stick moves where asked on the phones, and nothing on the right moves with it.
+ + for (int i = 0; i <= 4; i++)
+ + for (int full = 0; full < 2; full++)
+ + for (int z = 0; z < 3; z++) {
+ + DmPadOptions o = {dm_pad_size_scale(k_sizes[z]), DM_PAD_STICK_CORNER};
+ + DmPadGeo c = geo(&screens[i], full, o);
+ + o.stick = DM_PAD_STICK_HIGHER; DmPadGeo h = geo(&screens[i], full, o);
+ + o.stick = DM_PAD_STICK_INWARD; DmPadGeo n = geo(&screens[i], full, o);
+ + if (!(c.stick.y - h.stick.y >= 30 * c.u) || h.stick.x != c.stick.x) FAIL("%s %s %s: higher lifts the stick only %.1f", screens[i].name, full ? "3D" : "2D", k_sizes[z], c.stick.y - h.stick.y);
+ + if (!(n.stick.x - c.stick.x >= 30 * c.u) || n.stick.y != c.stick.y) FAIL("%s %s %s: inward moves the stick only %.1f", screens[i].name, full ? "3D" : "2D", k_sizes[z], n.stick.x - c.stick.x);
+ + if (full && !NEAR(n.dpad.x - c.dpad.x, n.stick.x - c.stick.x)) FAIL("%s 3D %s: the D-pad does not move with the stick", screens[i].name, k_sizes[z]);
+ + if (!CGPointEqualToPoint(h.face, c.face) || !CGPointEqualToPoint(n.face, c.face) || !CGPointEqualToPoint(n.drop[DM_PAD_R], c.drop[DM_PAD_R])) FAIL("%s: the right side moved", screens[i].name);
+ + }
+
+ // Picture Fill (DM_PICTURE_FIT) only makes the game rectangle larger: the controls keep the full view and their places.
+ for (int i = 0; i < nscreens; i++) {
diff --cc patches/0067-ios-touch-right-stick-hint.patch
index 00000000,51770e56..661da72d
mode 000000,100644..100644
--- a/patches/0067-ios-touch-right-stick-hint.patch
+++ b/patches/0067-ios-touch-right-stick-hint.patch
@@@ -1,0 -1,182 +1,181 @@@
+ From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
+ From: v0lumehi <[email protected]>
-Date: Tue, 3 Nov 2026 16:16:34 +0100
++Date: Tue, 3 Nov 2026 18:25:07 +0100
+ Subject: [PATCH] ios: say where the 3D layout's undrawn right stick is, once
+ per launch
+
+ Players read the 3D layout as having no right stick: it answers anywhere
+ in the right third of the screen but is drawn nowhere (LNPANDY/NX2Apple#414,
+ #347, seedds/nxnative#788). The first time the 3D layout shows in a launch,
+ a few words say where it is, for two seconds, then fade. Words only, in a
+ dim capsule: a ring or a knob would read as a fixed stick to hit. A touch
+ on the right stick ends it at once, a connected controller holds it back.
+
+ DM_TOUCH_HINT carries the words, so a front end shows them in the player's
+ language. Empty turns the hint off. Without it the words are English
+ ("Right stick: drag here").
+
+ dm_pad_hint_rect places it in the right third, between the top row and
+ the diamond, inside the safe area and clear of every control, or not at
+ all when there is no room. test_pad_geometry.py checks that on every
+ screen and size, and that every phone in landscape has room for a short
+ and a long wording.
+
+ Upstream-Status: pending (on seedds/nxnative#683)
+ Reason: Players read the 3D layout as having no right stick (#414, #347)
+ Issue: LNPANDY/NX2Apple#414
-
+ ---
+ dm/ios/Sources/DmGamepad.m | 48 ++++++++++++++++++-
+ dm/ios/Sources/DmPadGeometry.h | 22 +++++++++
+ .../tests/pad_geometry/pad_geometry_check.m | 18 +++++++
+ 3 files changed, 87 insertions(+), 1 deletion(-)
+
+ diff --git a/dm/ios/Sources/DmGamepad.m b/dm/ios/Sources/DmGamepad.m
-index e4b6f15..7653b5a 100644
++index 0b36d0a..5d3d239 100644
+ --- a/dm/ios/Sources/DmGamepad.m
+ +++ b/dm/ios/Sources/DmGamepad.m
-@@ -6,6 +6,7 @@
++@@ -7,6 +7,7 @@
+ // 3D (full, Pro Controller style): everything in 2D (left stick and diamond keep their place and size), plus a D-pad (inward of and below the left stick, 8-way), a right stick that is not drawn
+ // (a touch landing in the right third of the screen on no control holds it: centred where it lands, the centre dragged along once the finger passes the stick radius) and L3 / R3 (small round keys in the top row, inward of ZL / ZR).
+ // Breath of the Wild crouches with L3 and uses the scope with R3; 3D World and Breath of the Wild turn the camera with the right stick.
+ +// The first time the 3D layout shows in a launch, a few words (DM_TOUCH_HINT) say where the undrawn right stick is, for two seconds; see "The right-stick hint".
- // Touch-down ownership: locked keys first (shoulders, − / +, the layout button, L3 / R3), then whichever of the diamond, the D-pad and the left stick has the smallest distance / grab radius (the left stick's grab circle borders the D-pad),
++ // Touch-down ownership: the menu button where it is nearer than a locked key (a view of its own, DmMenuButton), locked keys next (shoulders, − / +, L3 / R3), then whichever of the diamond, the D-pad and the left stick has the smallest distance / grab radius (the left stick's grab circle borders the D-pad),
+ // then in 3D the right stick for a touch in the right third.
+ // Everything hugs the edges and corners to leave the picture room; controls sit over the picture, one CAShapeLayer each: a press only swaps the fill, a stick only moves its knob, nothing redraws the whole screen.
-@@ -140,6 +141,7 @@ @implementation DmGamepad {
++@@ -149,6 +150,7 @@ @implementation DmGamepad {
++ NSMapTable<UITouch *, NSNumber *> *_faceOf; // which keys each touch on the diamond currently holds
+ CAShapeLayer *_key[B_N]; CATextLayer *_label[B_N]; // one layer per key
+ CAShapeLayer *_ring, *_knob; // the left stick (the right one is not drawn)
- CAShapeLayer *_swap; CATextLayer *_swapLabel; BOOL _swapHeld;
+ + CALayer *_hint; CATextLayer *_hintText; NSString *_hintWords; // the 3D layout's right-stick hint: words only, once per launch (see "The right-stick hint")
++ DmMenuButton *_menu; // opens the in-game menu (GameMenu.swift)
+ UIImpactFeedbackGenerator *_touchFeedback; // one short feedback per touch-control press edge
+ unsigned _hapticSticks;
- BOOL _stickShown; // the left stick was held at the last refresh
-@@ -202,6 +204,11 @@ - (instancetype)initWithFrame:(CGRect)f {
- if (names[b]) _label[b] = label(_key[b], names[b]); // the D-pad arms carry no label
- }
- _swap = shape(_controls); _swapLabel = label(_swap, @"");
++@@ -219,6 +221,11 @@ - (instancetype)initWithFrame:(CGRect)f {
++ __weak DmGamepad *weakSelf = self;
++ [_menu addAction:[UIAction actionWithHandler:^(UIAction *a) { (void)a; [weakSelf reset]; }] forControlEvents:UIControlEventMenuActionTriggered]; // the menu opens: release every key and touch first
++ [self addSubview:_menu];
+ + const char *hint = getenv("DM_TOUCH_HINT"); // the shell passes the player's language; empty turns the hint off
+ + _hintWords = hint ? [NSString stringWithUTF8String:hint] : @"Right stick: drag here";
+ + _hint = [CALayer layer]; _hint.backgroundColor = [UIColor colorWithWhite:0 alpha:0.5].CGColor; _hint.opacity = 0; _hint.hidden = YES;
+ + _hintText = [CATextLayer layer]; _hintText.string = _hintWords; _hintText.alignmentMode = kCAAlignmentCenter;
+ + _hintText.foregroundColor = [UIColor colorWithWhite:1 alpha:0.9].CGColor; [_hint addSublayer:_hintText]; [_controls addSublayer:_hint];
+ NSNumber *saved = [NSUserDefaults.standardUserDefaults objectForKey:k_layout_key];
+ int l = saved ? (int)saved.integerValue : dm_profile()->pad_layout;
+ if (l == 2 && dm_opmode() != 0) l = 0; // Tap only in handheld mode (env.txt's DM_OPMODE may have changed it)
-@@ -378,7 +385,7 @@ - (void)touchesBegan:(NSSet<UITouch *> *)ts withEvent:(UIEvent *)e {
++@@ -404,7 +411,7 @@ - (void)touchesBegan:(NSSet<UITouch *> *)ts withEvent:(UIEvent *)e {
+ CGFloat f = dist(p, self.faceCenter) / (self.faceGap + MAX(self.faceR * 1.6, self.faceHitR)); if (f <= best) { best = f; kind = C_FACE; }
+ if (_full) { CGFloat d = dist(p, self.dpadCenter) / MAX(self.dpadR * K_DPAD_GRAB, _layout.separated ? 22 : 0); if (d < best) { best = d; kind = C_DPAD; } }
+ if (!_stick[0]) { CGFloat s = dist(p, self.stickHome) / MAX([self stickR:0] * K_STICK_GRAB, _layout.separated ? 22 : 0); if (s < best) { best = s; kind = C_STICK; } }
+ - if (kind == C_NONE && _full && !_stick[1] && [self inRightArea:p]) { kind = C_STICK; idx = 1; _rCentre = p; } // the right stick: centred where the touch lands
+ + if (kind == C_NONE && _full && !_stick[1] && [self inRightArea:p]) { kind = C_STICK; idx = 1; _rCentre = p; [self hideHint]; } // the right stick: centred where the touch lands (the hint's job is done)
+ if (kind == C_FACE) { [_owner setObject:@(C_FACE) forKey:t]; [_faceOf setObject:@([self faceAt:p]) forKey:t]; }
+ else if (kind == C_DPAD) [_owner setObject:@(C_DPAD) forKey:t];
+ else if (kind == C_STICK) { [_owner setObject:@(C_STICK | (uint64_t)idx << 8) forKey:t]; _stick[idx] = t; _stickNow[idx] = p; }
-@@ -478,8 +485,47 @@ - (void)layoutSubviews {
++@@ -504,8 +511,47 @@ - (void)layoutSubviews {
+ CAShapeLayer *in = (CAShapeLayer *)_knob.sublayers.firstObject; CGPathRef ip = circlePath(CGPointMake(k, k), k * 0.62);
+ in.frame = _knob.bounds; in.path = ip; CGPathRelease(ip);
+ [CATransaction commit];
+ + [self placeHint];
+ [self refresh];
+ }
+ +
+ +// ---- The right-stick hint ----
+ +// The 3D layout's right stick is not drawn (it answers anywhere in the right third), and players read the layout as having none (GitHub issues #347, #414).
+ +// So the first time the 3D layout shows in a launch, a few words say where it is, for two seconds, then fade. Words only: a ring or a knob would read as
+ +// a fixed stick that has to be hit. It sits in the right third between the top row and the diamond, clear of every control (dm_pad_hint_rect); with no
+ +// room, or with a controller connected, it waits. A touch on the right stick ends it at once. DM_TOUCH_HINT carries the words (the shell's language),
+ +// empty turns it off; without it the words are English.
+ +static BOOL g_hintShown; // once per launch
+ +- (void)placeHint {
+ + if (g_hintShown && _hint.hidden) return;
+ + if (!_full || _tap || _padOff || !_hintWords.length || CGRectIsEmpty(self.bounds)) { if (!_hint.hidden) [self hideHint]; return; }
+ + CGFloat u = self.u, fs = fmin(fmax(15 * u, 13), 20);
+ + UIFont *font = [UIFont systemFontOfSize:fs weight:UIFontWeightSemibold];
+ + CGSize t = [_hintWords sizeWithAttributes:@{NSFontAttributeName: font}];
+ + CGSize box = CGSizeMake(ceil(t.width) + 2 * fs, ceil(t.height) + fs * 0.8);
+ + CGRect area = _layout.controls;
+ + CGRect r = dm_pad_hint_rect(self.geo, CGRectGetMaxX(area) - area.size.width * K_RSWIPE, box);
+ + if (CGRectIsNull(r)) { if (!_hint.hidden) [self hideHint]; return; }
+ + [CATransaction begin]; [CATransaction setDisableActions:YES];
+ + _hint.frame = r; _hint.cornerRadius = box.height / 2;
+ + CTFontRef f = CTFontCreateUIFontForLanguage(kCTFontUIFontEmphasizedSystem, fs, NULL); _hintText.font = f; _hintText.fontSize = fs; CFRelease(f);
+ + _hintText.contentsScale = MAX(self.traitCollection.displayScale, 1);
+ + _hintText.frame = CGRectMake(0, (box.height - ceil(t.height)) / 2, box.width, ceil(t.height));
+ + [CATransaction commit];
+ + if (g_hintShown) return; // already on screen: only follow the layout
+ + g_hintShown = YES;
+ + LOG("front end: right-stick hint shown");
+ + _hint.hidden = NO; _hint.opacity = 1;
+ + __weak DmGamepad *weak = self;
+ + dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ [weak hideHint]; });
+ +}
+ +- (void)hideHint {
+ + if (_hint.hidden) return;
+ + [CATransaction begin]; [CATransaction setAnimationDuration:0.4];
+ + [CATransaction setCompletionBlock:^{ self->_hint.hidden = YES; }];
+ + _hint.opacity = 0; // an implicit fade
+ + [CATransaction commit];
+ +}
+ - (void)safeAreaInsetsDidChange { [super safeAreaInsetsDidChange]; [self setNeedsLayout]; }
+ - (void)refresh { // pressed state → fill, sticks → knob position
+ uint64_t all = _faceBits | _dpadBits | _btnBits;
+ diff --git a/dm/ios/Sources/DmPadGeometry.h b/dm/ios/Sources/DmPadGeometry.h
-index 18dc25e..e6abdd0 100644
++index 86083ad..a0b915b 100644
+ --- a/dm/ios/Sources/DmPadGeometry.h
+ +++ b/dm/ios/Sources/DmPadGeometry.h
+ @@ -196,4 +196,26 @@ static inline DmPadGeo dm_pad_geo(CGRect bounds, UIEdgeInsets insets, DmLayout l
+ return g;
+ }
+
+ +// Where the 3D layout's right-stick hint goes: a few words, no shape, in the area the undrawn right stick answers in (x from areaMinX to
+ +// the right edge), between the top row and the diamond, inside the safe area and clear of every drawn control (a gap of 6 u around it).
+ +// text is the label's size including its padding. CGRectNull when there is no room: the hint is then not shown.
+ +static inline CGRect dm_pad_hint_rect(const DmPadGeo *g, CGFloat areaMinX, CGSize text) {
+ + CGRect s = g->safe; CGFloat gap = 6 * g->u;
+ + CGFloat left = MAX(areaMinX, CGRectGetMinX(s)), right = CGRectGetMaxX(s);
+ + if (right - left < text.width) { left = right - text.width; if (left < CGRectGetMinX(s)) return CGRectNull; }
+ + CGFloat top = CGRectGetMinY(s); // below the right half of the top row
+ + for (int i = DM_PAD_ZR; i <= DM_PAD_R; i++) top = MAX(top, g->drop[i].y + g->dropR[i]);
+ + top = MAX(top, g->click[1].y + g->clickR);
+ + top = MAX(top, CGRectGetMaxY(g->pill[DM_PAD_PLUS]));
+ + CGFloat bottom = g->face.y - g->faceGap - g->faceR; // above the diamond's X
+ + CGFloat x = (left + right) / 2 - text.width / 2, y = (top + bottom) / 2 - text.height / 2;
+ + x = fmax(left, fmin(x, right - text.width));
+ + CGRect r = CGRectMake(x, y, text.width, text.height);
+ + DmPadShape sh[20]; int n = dm_pad_shapes(g, YES, sh);
+ + DmPadShape me = {CGPointZero, 0, CGRectInset(r, -gap, -gap), YES, s, -1};
+ + if (!CGRectContainsRect(s, r)) return CGRectNull;
+ + for (int i = 0; i < n; i++) if (dm_pad_shapes_overlap(&me, &sh[i])) return CGRectNull;
+ + return r;
+ +}
+ +
+ NS_ASSUME_NONNULL_END
+ diff --git a/dm/ios/tests/pad_geometry/pad_geometry_check.m b/dm/ios/tests/pad_geometry/pad_geometry_check.m
+ index dc17483..7bf7e8f 100644
+ --- a/dm/ios/tests/pad_geometry/pad_geometry_check.m
+ +++ b/dm/ios/tests/pad_geometry/pad_geometry_check.m
+ @@ -118,6 +118,24 @@ int main(void) {
+ if (i <= 4 && !(ul > um * 1.15)) FAIL("%s %s: large is not larger (%.3f vs %.3f)", screens[i].name, full ? "3D" : "2D", ul, um);
+ }
+
+ + // The 3D layout's right-stick hint: in the right third, inside the safe area, clear of every control; on every phone in landscape
+ + // and every size it has room for a short and a long wording.
+ + for (int i = 0; i < nscreens; i++)
+ + for (int z = 0; z < 3; z++)
+ + for (int w = 0; w < 2; w++) {
+ + DmLayout l = dm_layout(screens[i].bounds, screens[i].insets, screens[i].partiallyOpen, screens[i].division);
+ + DmPadGeo g = dm_pad_geo(screens[i].bounds, screens[i].insets, l, YES, (DmPadOptions){dm_pad_size_scale(k_sizes[z]), 0});
+ + CGFloat areaMinX = CGRectGetMaxX(l.controls) - l.controls.size.width / 3;
+ + CGSize text = CGSizeMake(w ? fmax(300 * g.u, 280) : 150 * g.u, fmax(30 * g.u, 28)); // the long one: about 30 characters at the label's 13 to 20 points
+ + CGRect r = dm_pad_hint_rect(&g, areaMinX, text);
+ + if (CGRectIsNull(r)) { if (i <= 6 || i == 12) FAIL("%s %s: no room for the hint (%s)", screens[i].name, k_sizes[z], w ? "long" : "short"); continue; }
+ + if (CGRectGetMinX(r) < areaMinX - 0.5 && CGRectGetMaxX(r) < CGRectGetMaxX(g.safe) - 0.5) FAIL("%s %s: the hint is not at the right edge's area", screens[i].name, k_sizes[z]);
+ + if (!CGRectContainsRect(g.safe, r)) FAIL("%s %s: the hint leaves the safe area", screens[i].name, k_sizes[z]);
+ + DmPadShape sh[20]; int n = dm_pad_shapes(&g, YES, sh);
+ + DmPadShape me = {CGPointZero, 0, r, YES, g.safe, -1};
+ + for (int k = 0; k < n; k++) if (dm_pad_shapes_overlap(&me, &sh[k])) FAIL("%s %s: the hint covers shape %d", screens[i].name, k_sizes[z], sh[k].id);
+ + }
+ +
+ if (!g_fail) printf("ok: %d screens\n", nscreens);
+ return g_fail > 100 ? 100 : g_fail;
+ }
diff --cc patches/series
index 559e6bf0,447a07bd..d090d2c1
--- a/patches/series
+++ b/patches/series
@@@ -36,4 -36,10 +36,7 @@@
0058-read-pfs0-packages-missing-the-final-filename-te.patch
0059-recognize-unframed-pointer-tail-calls-before-zer.patch
0060-ios-shell-declare-italian-in-cfbundlelocalizatio.patch
-0061-ios-adapt-gameplay-to-duo-portrait-and-tabletop.patch
0062-ios-apply-controller-button-mappings-to-physical.patch
-0063-ios-picture-fit-or-fill-for-screens-that-are-not.patch
-0064-mac-host-quit-a-game-by-holding-the-controller-s.patch
+ 0065-ios-touch-controls-size.patch
+ 0066-ios-touch-stick-place.patch
+ 0067-ios-touch-right-stick-hint.patch