Older systems: iOS 18+ / macOS 15+ via the older-API build; floating left stick; runeword required level; Mac keyboard, mouse and docs; clearer older-device dialog

This commit is contained in:
jhaizhou
2026-10-14 01:49:25 +08:00
parent 2eb5758771
commit d9e95c3381
52 changed files with 1325 additions and 274 deletions
+43 -3
View File
@@ -20,6 +20,7 @@ __attribute__((constructor)) static void ui_shift_reg(void) { mtl_set_ui_rect_fn
void dm_touch_host_live_enable(void); void dm_touch_host_live(int op, float nx, float ny); // touch_mac.c
static int g_live; // App 形态(或 DM_TOUCH_LIVE=1):鼠标进我们的触屏状态机;否则照旧当原版触摸屏的一个触点 | App form (or DM_TOUCH_LIVE=1): the mouse goes into our touch state machine; otherwise it stays a single touch point of the original touch screen as before
static CAMetalLayer *g_layer;
static NSWindow *g_win;
static NSView *g_view;
// 窗口不是 16:9(拉伸窗口、全屏到 16:10 的屏):游戏画面保持 16:9 居中,四周留黑边;鼠标位置按游戏画面这一块算
// The window is not 16:9 (stretched window, full screen on a 16:10 display): the game picture stays 16:9 and centred with black bars around it; the mouse position is computed against the game picture
@@ -50,6 +51,7 @@ static void app_quit(const char *why) {
}
if (last != c0 && asked) LOG("窗口:退出前等游戏存盘 %.0f ms,期间存档提交 %llu 次%s", (now_ns() - t0) / 1e6, (unsigned long long)(last - c0), paused ? "(原先停在暂停菜单 / 面板,已放开)" : "");
else LOG("【红】窗口:未存盘退出——3 秒内没等到存档提交(%s%s)", asked ? "已请游戏存盘" : "一直没到能请存盘的状态(读取画面?)", paused ? ",原先暂停着、已放开" : "");
{ int dm_cloud_flush_wait(double sec); dm_cloud_flush_wait(10); } // 存盘之后把这一局传上 iCloud 再退(没开同步时立即返回) | after saving, upload this session to iCloud before quitting (returns immediately when sync is off)
dm_host_shutdown(why);
}
static const char *ui(const char *k) { const char *dm_ui(const char *key); return dm_ui(k); }
@@ -84,7 +86,10 @@ static int key_bit(unsigned short code) {
case 38: return 0; case 40: return 1; case 32: return 2; case 34: return 3; // J K U I → A B X Y
case 3: return 4; case 5: return 5; // F G → 按摇杆 | F G → press stick
case 12: return 6; case 14: return 7; case 6: return 8; case 8: return 9; // Q E Z C → L R ZL ZR
case 36: return 10; case 48: return 11; // 回车 Tab → + − | Return Tab → + −
case 36: return g_live ? 40 : 10; case 48: return 11; // 回车 Tab → + −(App 形态:回车交给 touch_mac.c 按当前页决定是 + 还是确认,见那里) | Return Tab -> + - (App form: Return is handed to touch_mac.c, which decides by the current page whether it is + or confirm, see there)
case 24: case 69: return 10; // = 键(+ 那个键)、小键盘 + → + | the = key (the one with +) and keypad + -> +
case 49: return 0; // 空格 → A(确认) | Space -> A (confirm)
case 53: return g_live ? 41 : -1; // Esc → 返回(App 形态;局内没有面板时是开功能页,touch_mac.c) | Esc -> back (App form; with no panel open in game it opens the function page, touch_mac.c)
case 123: return 12; case 126: return 13; case 124: return 14; case 125: return 15; // ← ↑ → ↓
}
return -1;
@@ -113,7 +118,7 @@ void front_run(void (*guest)(void)) {
NSWindow *win = [[NSWindow alloc] initWithContentRect:r
styleMask:NSWindowStyleMaskTitled | NSWindowStyleMaskClosable | NSWindowStyleMaskResizable
backing:NSBackingStoreBuffered defer:NO];
win.title = @"D2R Mobile";
win.title = @"D2R Mobile"; win.restorable = NO; g_win = win;
NSView *v = win.contentView; g_view = v;
v.wantsLayer = YES; v.layer.backgroundColor = CGColorGetConstantColor(kCGColorBlack);
win.collectionBehavior = NSWindowCollectionBehaviorFullScreenPrimary; win.contentMinSize = NSMakeSize(640, 360);
@@ -173,6 +178,36 @@ void front_run(void (*guest)(void)) {
{ const char *e; // 测试(没人点窗口时用):DM_TEST_WINSIZE=宽x高 设内容区大小;DM_TEST_FULLSCREEN_AT=<秒> 进全屏;DM_TEST_QUIT_AT=<秒> 走一次退出 | Tests (for when nobody is there to click the window): DM_TEST_WINSIZE=WxH sets the content size; DM_TEST_FULLSCREEN_AT=<seconds> enters full screen; DM_TEST_QUIT_AT=<seconds> goes through one quit
int w, h; if ((e = getenv("DM_TEST_WINSIZE")) && sscanf(e, "%dx%d", &w, &h) == 2) { [win setContentSize:NSMakeSize(w, h)]; layout_game(); }
if ((e = getenv("DM_TEST_FULLSCREEN_AT"))) dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(atof(e) * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ [win toggleFullScreen:nil]; });
// DM_TEST_NSMOUSE=<文件>:合成真正的系统鼠标事件(NSEvent)投进 App 的事件队列,走与真人点击同一条分发路(事件监视器 → 换算 → 状态机),不经测试入口。
// 每行「秒 down|move|up x,y」,x,y 是内容区里的点(左上角为原点;窗口缺省 1280×720),时刻自窗口建好起算
// DM_TEST_NSMOUSE=<file>: synthesise real system mouse events (NSEvent) and post them to the App's event queue, going through the same dispatch path as a person clicking (event monitor -> conversion -> state machine), not through the test entry.
// Each line is "<seconds> down|move|up x,y", where x,y is a point in the content area (origin at top-left; the window defaults to 1280x720), and time counts from when the window is created
if ((e = getenv("DM_TEST_NSMOUSE"))) { NSString *txt = [NSString stringWithContentsOfFile:@(e) encoding:NSUTF8StringEncoding error:nil]; NSTimeInterval t0 = NSProcessInfo.processInfo.systemUptime; int n = 0;
dispatch_queue_t tq = dispatch_queue_create("dm.test.nsevent", DISPATCH_QUEUE_SERIAL); // 不排在主队列上:我们自己的弹框是在主队列的一个块里跑模态循环的,那期间主队列上别的块不执行,事件就投不进弹框 | not queued on the main queue: our own dialog runs its modal loop inside a block on the main queue, during which other blocks on the main queue do not execute, so events could not be delivered into the dialog
for (NSString *ln in [txt componentsSeparatedByString:@"\n"]) { double sec, x = 0, y = 0; char op[8];
if (sscanf(ln.UTF8String, "%lf %7s %lf,%lf", &sec, op, &x, &y) < 2 || ln.UTF8String[0] == '#') continue; n++;
if (!strcmp(op, "key") || !strcmp(op, "type")) { // 「秒 key <键码>」:合成一次真正的键盘按下 / 0.08 秒后松开(键码 = 苹果的虚拟键码,如回车 36、Tab 48、J 38);「秒 type <字>」:逐字打字。 | "<seconds> key <key code>": synthesise a real key press / release 0.08 s later (key code = Apple virtual key code, e.g. Return 36, Tab 48, J 38); "<seconds> type <text>": type character by character.
// 都投给当时的键窗口(真键盘就是这样:弹着输入框时进输入框),并记一行键窗口是不是游戏窗口
// All are posted to the key window at that moment (which is what a real keyboard does: into the input field while it is shown), and one line records whether the key window is the game window
int ty = !strcmp(op, "type"); char word[64] = ""; if (ty) sscanf(ln.UTF8String, "%*lf %*7s %63s", word); NSString *chars = ty ? @(word) : (x == 36 ? @"\r" : @"");
if (ty) { // 打字:在主线程的运行循环里执行(模态框开着时也走——主队列那时不走),记下第一响应者是谁,再把字经响应链送进去(等于输入法提交了这些字) | typing: executed in the main thread's run loop (which also runs while a modal dialog is open - the main queue does not), noting who the first responder is, then sending the text through the responder chain (as if an input method committed it)
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(sec * NSEC_PER_SEC)), tq, ^{ CFRunLoopPerformBlock(CFRunLoopGetMain(), kCFRunLoopCommonModes, ^{
NSWindow *kw = NSApp.keyWindow; id fr = kw.firstResponder; BOOL ok = [NSApp sendAction:@selector(insertText:) to:nil from:chars];
LOG("DM_TEST_NSMOUSE:%.2f 秒打字「%s」(键窗口%s,第一响应者 %s,%s)", NSProcessInfo.processInfo.systemUptime - t0, chars.UTF8String, kw == win ? "是游戏窗口" : kw ? "是弹框" : "没有", fr ? NSStringFromClass([fr class]).UTF8String : "无", ok ? "有人收" : "没人收"); });
CFRunLoopWakeUp(CFRunLoopGetMain()); });
continue; }
for (NSUInteger ci = 0; ci < (ty ? chars.length : 1); ci++) { NSString *one = ty ? [chars substringWithRange:NSMakeRange(ci, 1)] : chars; double at = sec + ci * 0.15;
for (int dn = 1; dn >= 0; dn--) dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)((at + (dn ? 0 : 0.08)) * NSEC_PER_SEC)), tq, ^{ NSWindow *kw = NSApp.keyWindow;
if (dn) LOG("DM_TEST_NSMOUSE:%.2f 秒投 键盘 %s%s(键窗口%s)", NSProcessInfo.processInfo.systemUptime - t0, ty ? "字 " : "键码 ", ty ? one.UTF8String : [NSString stringWithFormat:@"%d", (int)x].UTF8String, kw == win ? "是游戏窗口" : kw ? "是别的窗口(弹框)" : "没有:App 里没有键窗口");
[NSApp postEvent:[NSEvent keyEventWithType:dn ? NSEventTypeKeyDown : NSEventTypeKeyUp location:NSZeroPoint modifierFlags:0 timestamp:NSProcessInfo.processInfo.systemUptime windowNumber:(kw ?: win).windowNumber context:nil characters:one charactersIgnoringModifiers:one isARepeat:NO keyCode:ty ? 0 : (unsigned short)x] atStart:NO]; }); }
continue; }
NSEventType ty = !strcmp(op, "down") ? NSEventTypeLeftMouseDown : !strcmp(op, "up") ? NSEventTypeLeftMouseUp : NSEventTypeLeftMouseDragged; int has = !strcmp(op, "up") ? 0 : 1;
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(sec * NSEC_PER_SEC)), tq, ^{ static NSPoint last;
if (has) last = NSMakePoint(x, v.bounds.size.height - y); // 内容区的点 → 窗口坐标(原点左下) | point in the content area -> window coordinates (origin at bottom-left)
NSEvent *ev = [NSEvent mouseEventWithType:ty location:[v convertPoint:last toView:nil] modifierFlags:0 timestamp:NSProcessInfo.processInfo.systemUptime windowNumber:win.windowNumber context:nil eventNumber:0 clickCount:1 pressure:ty == NSEventTypeLeftMouseUp ? 0 : 1];
LOG("DM_TEST_NSMOUSE:%.2f 秒投 %s 内容区 (%.0f,%.0f)(窗口%s是主窗口,App %s在前台,背板缩放 %.1f)", NSProcessInfo.processInfo.systemUptime - t0, ty == NSEventTypeLeftMouseDown ? "按下" : ty == NSEventTypeLeftMouseUp ? "抬起" : "拖动", last.x, v.bounds.size.height - last.y, win.isKeyWindow ? "" : "不", NSApp.isActive ? "" : "不", win.backingScaleFactor);
[NSApp postEvent:ev atStart:NO]; }); }
LOG("DM_TEST_NSMOUSE:%d 条排好", n); }
if ((e = getenv("DM_TEST_QUIT_AT"))) dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(atof(e) * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ app_quit("DM_TEST_QUIT_AT"); }); }
// 生命周期(hle.h / dm.h):失焦 → 2、拿回焦点 → 1;最小化 → 3 + 音频暂停 + GPU 挂起,还原 → 恢复(随后拿回焦点 → 1)
// Lifecycle (hle.h / dm.h): focus lost → 2, focus regained → 1; minimized → 3 + audio paused + GPU suspended, restored → resume (followed by focus regained → 1)
@@ -205,8 +240,13 @@ int front_text_input(const char *title, const char *sub, const char *guide, cons
NSTextField *f = [[NSTextField alloc] initWithFrame:NSMakeRect(0, 0, 300, 24)];
f.stringValue = initial ? @(initial) : @"";
if (guide && *guide) f.placeholderString = @(guide);
a.accessoryView = f; a.window.initialFirstResponder = f;
a.accessoryView = f; [a layout]; a.window.initialFirstResponder = f;
f.target = a.buttons.firstObject; f.action = @selector(performClick:); // 在输入框里按回车 = 点「确定」(明确接上,不靠系统把回车转给缺省按钮) | pressing Return in the input field = clicking "OK" (wired explicitly, not relying on the system forwarding Return to the default button)
// 弹框出来后光标要在输入框里:只指定「初始第一响应者」不生效(实测弹出后第一响应者是弹框自己,要先点一下输入框才能打字),等模态循环转起来再显式指定一次
// The cursor must be in the input field once the dialog appears: specifying only the "initial first responder" does not work (measured: after it appears the first responder is the dialog itself, and the field has to be clicked before typing), so it is specified explicitly once more after the modal loop starts running
CFRunLoopPerformBlock(CFRunLoopGetMain(), kCFRunLoopCommonModes, ^{ [a.window makeFirstResponder:f]; });
r = [a runModal] == NSAlertFirstButtonReturn;
[g_win makeKeyAndOrderFront:nil]; // 弹框关掉后把游戏窗口显式设回键窗口(系统通常自己会还,保险) | after the dialog closes, explicitly make the game window key again (the system usually returns it by itself; this is for safety)
text = f.stringValue;
});
if (!r) return 0;