Play your own Switch games on iPhone/iPad — one-click macOS app, no terminal. | 把 Switch 游戏装进 iPhone:图形界面一键构建,不用命令行。Bring your own game.
Swift apple-siliconcustom-formathomebrewiosipaipadipadosiphonemetalnative-portnintendo-switchnxnativeromfsswitch-games
DmGamepad H2: a menu pad for the inventory
LNPANDY65d0d42d03db
diff --git a/dm/ios/Sources/DmGamepad.m b/dm/ios/Sources/DmGamepad.mindex 9de8efcb..2f2265b9 100644--- a/dm/ios/Sources/DmGamepad.m+++ b/dm/ios/Sources/DmGamepad.m@@ -57,6 +57,15 @@ static const CGFloat KH_FACE_X = 118, KH_FACE_Y = 88,KH_A_DX = 40, KH_A_DY = 88,KH_ZR_DX = -4, KH_ZR_DY = -108,KH_R_DX = 34, KH_R_DY = 148;+// The menu pad: a handle on the left edge mid-height expands a small D-pad (menu navigation) with+// the two bumpers beside it — the keys menus want (D-pad right opens the inventory, L / R page+// through it) while costing nothing in combat. It hides again 4 s after the last touch on it.+static const CGFloat KH_MENU_HANDLE_X = 6, KH_MENU_HANDLE_Y = 0.42, KH_MENU_HANDLE_R = 22,+ KH_MENU_X = 64, KH_MENU_Y = 0.40, // the D-pad centre: from the left edge, fraction of the height+ KH_MENU_R = 40, KH_MENU_ARM = 17, KH_MENU_IN = 12,+ KH_MENU_L_DX = 116, KH_MENU_L_DY = -62, // L: inward and above the D-pad+ KH_MENU_R_DX = 116, KH_MENU_R_DY = 62, // R: inward and below+ KH_MENU_HIDE_S = 4.0;// Floating stick: the start zone's right bound (fraction of the width) and upper bound (fraction of// the height from the bottom); the base radius, full-tilt fraction, dead zone, the resume window for// a system-cancelled stick finger, its grab factor, and how long the Ω latch leads the attack key.@@ -149,6 +158,11 @@ static __weak DmGamepad *g_view;double _h2omegaSince; // when the latch went down (the lead time runs from it)double _h2keyDown[KH_N]; // when each key's finger went down (0 = up; Ω needs the duration)uint64_t _h2bits; // the combat-key bits as published at the last recompute+ // ---- Hades II menu pad ----+ BOOL _h2menu; // the menu pad is expanded+ CAShapeLayer *_h2menuKey[6]; CATextLayer *_h2menuLabel[6]; // up / down / left / right / L / R+ CAShapeLayer *_h2menuHandle; CATextLayer *_h2menuHandleLabel;+ NSDate *_h2menuTouch; // the last touch on the menu pad (auto-hide after KH_MENU_HIDE_S)}static UIColor *k_idle, *k_on, *k_line, *k_ringFill, *k_text, *k_textOn;@@ -211,6 +225,15 @@ static BOOL fullOnly(int b) { return b == B_LS || b == B_RS || b >= B_LEFT; }_h2knob = shape(_controls);CAShapeLayer *h2in = [CAShapeLayer layer]; h2in.fillColor = nil; h2in.strokeColor = k_line.CGColor; h2in.lineWidth = 1.5;[_h2knob addSublayer:h2in];+ // The menu pad: a handle on the left edge expands a small D-pad + L / R for the menus.+ _h2menuHandle = shape(_controls);+ _h2menuHandleLabel = label(_h2menuHandle, @"☰");+ static NSString *const h2menuNames[6] = {@"↑", @"↓", @"←", @"→", @"L", @"R"};+ for (int i = 0; i < 6; i++) {+ _h2menuKey[i] = shape(_controls); _h2menuKey[i].hidden = YES;+ _h2menuLabel[i] = label(_h2menuKey[i], h2menuNames[i]);+ }+ _h2menuHandle.hidden = YES; // only the H2 layout shows it[self applyLayout];NSNotificationCenter *nc = NSNotificationCenter.defaultCenter;[nc addObserver:self selector:@selector(padsChanged) name:GCControllerDidConnectNotification object:nil];@@ -225,6 +248,9 @@ static BOOL fullOnly(int b) { return b == B_LS || b == B_RS || b >= B_LEFT; }_ring[1].hidden = _knob[1].hidden = _h2 || !_full;_ring[0].hidden = _knob[0].hidden = _h2; // the H2 stick has its own floating base layersfor (int i = 0; i < KH_N; i++) _h2key[i].hidden = !_h2;+ _h2menuHandle.hidden = !_h2; // the handle always shows in H2; the pad expands on tap+ if (!_h2) _h2menu = NO;+ for (int i = 0; i < 6; i++) _h2menuKey[i].hidden = !_h2 || !_h2menu;_swapLabel.string = _h2 ? @"H2" : _full ? @"3D" : @"2D";[CATransaction commit];}@@ -289,6 +315,33 @@ static BOOL isClick(int b) { return b == B_LS || b == B_RS; }if (dist(p, [self h2keyCenter:i]) <= r) return i; }return -1;}+// ---- Hades II menu pad geometry ----+- (CGPoint)h2menuHandleCenter {+ CGFloat u = self.h2u;+ return CGPointMake(CGRectGetMinX(self.safe) + KH_MENU_HANDLE_X * u, self.bounds.size.height * KH_MENU_HANDLE_Y);+}+- (CGFloat)h2menuHandleR { return KH_MENU_HANDLE_R * self.h2u; }+- (CGPoint)h2menuCenter {+ CGFloat u = self.h2u;+ return CGPointMake(CGRectGetMinX(self.safe) + KH_MENU_X * u, self.bounds.size.height * KH_MENU_Y);+}+- (CGFloat)h2menuR { return KH_MENU_R * self.h2u; }+- (CGPoint)h2menuBumper:(int)i { // 4 = L (above), 5 = R (below)+ CGPoint c = self.h2menuCenter; CGFloat u = self.h2u;+ CGFloat dy = i == 4 ? KH_MENU_L_DY : KH_MENU_R_DY;+ return CGPointMake(c.x + KH_MENU_L_DX * u, c.y + dy * u);+}+- (int)h2menuAt:(CGPoint)p { // 0 up, 1 down, 2 left, 3 right, 4 L, 5 R; -1 none+ CGFloat r = self.h2menuR * 1.15;+ for (int i = 0; i < 4; i++) {+ static const CGFloat an[4] = {-M_PI_2, M_PI_2, M_PI, 0};+ CGPoint c = CGPointMake(self.h2menuCenter.x + self.h2menuR * cos(an[i]), self.h2menuCenter.y + self.h2menuR * sin(an[i]));+ if (dist(p, c) <= self.h2menuR * 0.62) return i;+ }+ for (int i = 4; i < 6; i++) if (dist(p, [self h2menuBumper:i]) <= self.h2menuR * 0.55) return i;+ return -1;+}+- (BOOL)h2inMenuHandle:(CGPoint)p { return dist(p, [self h2menuHandleCenter]) <= [self h2menuHandleR] * 1.5; }- (BOOL)h2inStickZone:(CGPoint)p { // the lower-left zone the floating stick starts inreturn p.x >= 0 && p.y <= self.bounds.size.height&& p.x < self.bounds.size.width * KH_ZONE_R@@ -357,8 +410,17 @@ static CGFloat dist(CGPoint a, CGPoint b) { return hypot(a.x - b.x, a.y - b.y);// a quick tap-Ω-attack still arrives in the game's own "hold ZR first" order. Press durations// pass through untouched (the Ω moves are holds; the hex's hold-and-release works as is).uint64_t h2 = 0;+ static const int h2menuBtn[6] = {B_UP, B_DOWN, B_LEFT, B_RIGHT, B_L, B_R};for (UITouch *t in _owner) { uint64_t v = [_owner objectForKey:t].unsignedLongLongValue;- if ((v & 0xff) == C_H2KEY) h2 |= 1ull << k_h2[(int)(v >> 8)].b; }+ if ((v & 0xff) != C_H2KEY) continue;+ int tag = (int)(v >> 8);+ if (tag < KH_N) h2 |= 1ull << k_h2[tag].b;+ else if (tag >= KH_N + 16) h2 |= 1ull << h2menuBtn[tag - KH_N - 16];+ /* the handle's tag (KH_N + 8) holds no key: the toggle happened at touch-down */ }+ // the menu pad hides itself when nothing has touched it for a while+ if (_h2menu && _h2menuTouch && [_h2menuTouch timeIntervalSinceNow] < -KH_MENU_HIDE_S) {+ _h2menu = NO; [self applyLayout];+ }BOOL h2changed = h2 != _h2bits;_h2bits = h2;// The H2 floating stick: axis values from the finger's offset to the base centre (the same@@ -415,10 +477,23 @@ static CGFloat dist(CGPoint a, CGPoint b) { return hypot(a.x - b.x, a.y - b.y);BOOL game = NO;for (UITouch *t in ts) {CGPoint p = [t locationInView:self];- // locked keys first (shoulders, − / +, the layout button), then H2 keys, then the stick zone+ // locked keys first (shoulders, − / +, the layout button), then H2 keys, then the menu pad, then the stick zoneint hit = [self btnAt:p];if (hit >= 0) { [_owner setObject:@(C_BTN | (uint64_t)hit << 8) forKey:t]; continue; }if ([self inSwap:p]) { [_owner setObject:@(C_SWAP) forKey:t]; continue; }+ if ([self h2inMenuHandle:p]) { // the handle toggles the menu pad on touch-down+ _h2menu = !_h2menu; _h2menuTouch = [NSDate date]; [self applyLayout];+ [_owner setObject:@(C_H2KEY | (uint64_t)(KH_N + 8) << 8) forKey:t]; // holds no key+ continue;+ }+ if (_h2menu) {+ int m = [self h2menuAt:p];+ if (m >= 0) {+ [_owner setObject:@(C_H2KEY | (uint64_t)(KH_N + 16 + m) << 8) forKey:t];+ _h2menuTouch = [NSDate date];+ continue;+ }+ }int k = [self h2keyAt:p];if (k >= 0) {[_owner setObject:@(C_H2KEY | (uint64_t)k << 8) forKey:t];@@ -497,7 +572,14 @@ static CGFloat dist(CGPoint a, CGPoint b) { return hypot(a.x - b.x, a.y - b.y);if ((v & 0xff) == C_STICK && (v >> 8) == 2 && t == _h2stick) { // the floating stick: record for the resume_h2stick = nil; _h2lostOrigin = _h2origin; _h2lostS = [NSDate timeIntervalSinceReferenceDate];}- if ((v & 0xff) == C_H2KEY) { int k = (int)(v >> 8); _h2keyDown[k] = 0; if (k_h2[k].b == B_ZR) _h2omega = NO; }+ if ((v & 0xff) == C_H2KEY) {+ int tag = (int)(v >> 8);+ if (tag < KH_N) { _h2keyDown[tag] = 0; if (k_h2[tag].b == B_ZR) _h2omega = NO; }+ else if (tag >= KH_N + 16 && ended) { // a menu key: touch the auto-hide clock+ _h2menuTouch = [NSDate date];+ }+ // the handle's toggle happens in recompute (it sees the release through the missing bit)+ }if ((v & 0xff) == C_GAME && (v >> 8) < 16) { _game[v >> 8] = nil; game = YES; }[_owner removeObjectForKey:t]; [_faceOf removeObjectForKey:t]; for (int i = 0; i < 2; i++) if (t == _stick[i]) _stick[i] = nil;}@@ -508,7 +590,7 @@ static CGFloat dist(CGPoint a, CGPoint b) { return hypot(a.x - b.x, a.y - b.y);- (void)touchesEnded:(NSSet<UITouch *> *)ts withEvent:(UIEvent *)e { [self endTouches:ts tapped:YES]; }- (void)touchesCancelled:(NSSet<UITouch *> *)ts withEvent:(UIEvent *)e { [self endTouches:ts tapped:NO]; }- (void)reset { [_owner removeAllObjects]; [_faceOf removeAllObjects]; _stick[0] = _stick[1] = nil; for (int i = 0; i < 16; i++) _game[i] = nil;- _h2stick = nil; _h2omega = NO; for (int i = 0; i < KH_N; i++) _h2keyDown[i] = 0; _h2lostS = 0; [self pushGame]; [self recompute]; }+ _h2stick = nil; _h2omega = NO; for (int i = 0; i < KH_N; i++) _h2keyDown[i] = 0; _h2lostS = 0; _h2menu = NO; [self pushGame]; [self recompute]; }// ---- Shapes ----static CGPathRef circlePath(CGPoint c, CGFloat r) { return CGPathCreateWithEllipseInRect(CGRectMake(c.x - r, c.y - r, 2 * r, 2 * r), NULL); }@@ -571,6 +653,23 @@ static void placeLabel(CATextLayer *t, CGPoint c, CGFloat fs) {placeLabel(_h2label[i], c, r * 0.75);}}+ // Hades II menu pad: the handle on the left edge; the D-pad arms and the two bumpers+ {+ CGPoint hc = [self h2menuHandleCenter]; CGFloat hr = [self h2menuHandleR];+ setPath(_h2menuHandle, circlePath(hc, hr)); placeLabel(_h2menuHandleLabel, hc, hr * 0.9);+ CGPoint c = [self h2menuCenter]; CGFloat r = self.h2menuR, arm = KH_MENU_ARM * self.h2u, tip = KH_MENU_IN * self.h2u;+ static const struct { int i, b; CGFloat angle; } arms[] = {{0, B_UP, 0}, {1, B_DOWN, M_PI}, {2, B_LEFT, -M_PI_2}, {3, B_RIGHT, M_PI_2}};+ for (int i = 0; i < 4; i++) {+ setPath(_h2menuKey[arms[i].i], armPath(c, r, arm, tip, arms[i].angle, 5 * self.h2u));+ CGPoint k = CGPointMake(c.x + r * cos(arms[i].angle), c.y + r * sin(arms[i].angle));+ placeLabel(_h2menuLabel[arms[i].i], k, 15 * self.h2u);+ }+ for (int i = 4; i < 6; i++) {+ CGPoint bc = [self h2menuBumper:i];+ setPath(_h2menuKey[i], circlePath(bc, r * 0.52));+ placeLabel(_h2menuLabel[i], bc, 16 * self.h2u);+ }+ }for (int i = 0; i < 2; i++) {setPath(_ring[i], circlePath([self stickHome:i], [self stickR:i]));CGFloat k = (i ? K_RKNOB_R : K_KNOB_R) * u; _knob[i].bounds = CGRectMake(0, 0, 2 * k, 2 * k);@@ -594,6 +693,16 @@ static void placeLabel(CATextLayer *t, CGPoint c, CGFloat fs) {_h2key[i].fillColor = (on || armed ? k_on : k_idle).CGColor;if (_h2label[i]) _h2label[i].foregroundColor = (on || armed ? k_textOn : k_text).CGColor;}+ // the menu pad: handle always visible in H2; the expanded keys light on press+ _h2menuHandle.fillColor = (_h2menu ? k_on : k_idle).CGColor; _h2menuHandleLabel.foregroundColor = (_h2menu ? k_textOn : k_text).CGColor;+ {+ static const int h2menuBtn[6] = {B_UP, B_DOWN, B_LEFT, B_RIGHT, B_L, B_R};+ for (int i = 0; i < 6; i++) {+ BOOL on = (_h2bits >> h2menuBtn[i]) & 1;+ _h2menuKey[i].fillColor = (on ? k_on : k_idle).CGColor;+ _h2menuLabel[i].foregroundColor = (on ? k_textOn : k_text).CGColor;+ }+ }for (int i = 0; i < 2; i++) {CGPoint h = [self stickHome:i], o = [self knobOffset:i];_knob[i].position = CGPointMake(h.x + o.x, h.y + o.y); _knob[i].fillColor = (_stick[i] ? k_on : k_idle).CGColor;