Android port (Adreno): Vulkan backend, GameActivity app, touch HUD, lifecycle, minimap quest goals
- host/android: Vulkan replay backend (nvn_vk.c) with pipeline/descriptor caching, ADPF, AChoreographer present link; Linux compat layer; Makefile.android (headless dmhost + APK lib) - android/app: GameActivity shell without Gradle (build_apk.sh), AAudio output, touch HUD port (touch_app.c + TouchOverlay.java, three layers: body / joystick / minimap goals), lifecycle save-on-background like iOS, art_from_ipa.py for touch / HUD / mapgoal art - touch: shared touch controls moved from ios/Sources to dm/touch - patch_nso.py Android ruleset, dmsh_vk.py shader pack, DM_TOUCH_PRODUCT defaults - hle_audio: AAudio resume waits through STARTING and reopens once on failure - .gitignore: local resources/ (original game data) never committed Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
5bc5a01087
commit
4d91f80bae
@@ -0,0 +1,17 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="org.d2rmobile.app"
|
||||
android:versionCode="1" android:versionName="0.1">
|
||||
<uses-sdk android:minSdkVersion="29" android:targetSdkVersion="33" />
|
||||
<uses-permission android:name="android.permission.VIBRATE" />
|
||||
<uses-feature android:name="android.hardware.vulkan.version" android:version="0x401000" android:required="true" />
|
||||
<!-- debuggable:run-as 往私有目录放游戏数据、读 run.log | debuggable: run-as to put game data into the private directory and read run.log -->
|
||||
<application android:label="D2R Mobile" android:icon="@mipmap/ic_launcher" android:roundIcon="@mipmap/ic_launcher" android:debuggable="true" android:allowBackup="false" android:hasCode="true"
|
||||
android:extractNativeLibs="true" android:isGame="true" android:appCategory="game"
|
||||
android:theme="@android:style/Theme.Material.NoActionBar.Fullscreen">
|
||||
<activity android:name=".GameActivity" android:exported="true" android:screenOrientation="sensorLandscape"
|
||||
android:configChanges="orientation|screenSize|screenLayout|smallestScreenSize|keyboard|keyboardHidden|navigation|uiMode|density"
|
||||
android:launchMode="singleTask" android:resizeableActivity="false">
|
||||
<intent-filter><action android:name="android.intent.action.MAIN" /><category android:name="android.intent.category.LAUNCHER" /></intent-filter>
|
||||
</activity>
|
||||
</application>
|
||||
</manifest>
|
||||
@@ -0,0 +1,35 @@
|
||||
#!/usr/bin/env python3
|
||||
# 没有完整原版 RomFS(缺 build_manifest.psv)时,从现成 iOS 安装包解出的 DmIOS.app 取触屏素材:TouchArt PNG、hud_v4_ui、mapgoal、dm_touch_native_art.c。
|
||||
# 每张 PNG 与技能索引都按仓库里冻结的 native_art_manifest.json 核过哈希(同 ios/tools/native_art/generate.py 的产物)。
|
||||
# 用法:art_from_ipa.py <解开的 Payload/DmIOS.app> <dm 目录>
|
||||
# Without a full original RomFS (no build_manifest.psv), take the touch art from an unpacked iOS build (DmIOS.app): TouchArt PNGs, hud_v4_ui, mapgoal, dm_touch_native_art.c.
|
||||
# Every PNG and the skill index are hash-checked against the frozen native_art_manifest.json (same output as ios/tools/native_art/generate.py).
|
||||
# Usage: art_from_ipa.py <unpacked Payload/DmIOS.app> <dm directory>
|
||||
import hashlib, json, shutil, sys
|
||||
from pathlib import Path
|
||||
app = Path(sys.argv[1]); dm = Path(sys.argv[2]); ios = dm / 'ios'; nat = ios / 'tools/native_art'
|
||||
sha = lambda b: hashlib.sha256(b).hexdigest()
|
||||
mpath = ios / 'Sources/TouchArt/native_art_manifest.json'; man = json.loads(mpath.read_text())
|
||||
art = ios / 'Sources/TouchArt'; atl = {}
|
||||
for a in man['assets']:
|
||||
b = (app / a['asset']).read_bytes()
|
||||
assert sha(b) == a['decoded_png_sha256'], a['asset']
|
||||
(art / a['asset']).write_bytes(b); atl[a['asset']] = a
|
||||
rows = [[int(x) for x in l.split('\t')[1:4]] for l in open(app / 'dm/derived.tsv') if l.startswith('TAROW')]
|
||||
assert sha(json.dumps(rows, sort_keys=True, separators=(',', ':')).encode()) == man['descriptor_index_sha256']
|
||||
out = ['/* Generated from frozen official Skills/Skilldesc; do not hand-edit rows. */', '#include "dm_touch_native_art.h"', '#include "../host/dm_input.h"', '#include <stddef.h>',
|
||||
'const dm_ta_atlas dm_ta_atlases[DM_TA_ATLASES]={']
|
||||
for i in range(9):
|
||||
a = atl['dm_native_skill_%d.png' % i]; out.append(' {"%s",%d,%d,%d,%d,%d},' % (a['asset'][:-4], *a['atlas_px'], a['frame_width'], a['frame_pitch'], a['frame_count']))
|
||||
out += ['};', f'const char dm_ta_manifest_sha[]="{sha(mpath.read_bytes())}";', f'const char dm_ta_geometry_sha[]="{sha((ios / "docs/native_art_v1_geometry.json").read_bytes())}";',
|
||||
f'const char dm_ta_binding_sha[]="{sha((ios / "docs/touch_layout_spec_native_binding_v5.json").read_bytes())}";', 'static const struct { uint32_t id; int atlas,frame; } rows[]={']
|
||||
out += [' {%d,%d,%d},' % tuple(r) for r in rows] + ['};', (nat / 'implementation.c.inc').read_text()]
|
||||
(dm / 'touch/dm_touch_native_art.c').write_text('\n'.join(out) + '\n')
|
||||
hud = ios / 'Gen/assets/NativeHUD/v4/hud_v4_ui'
|
||||
shutil.rmtree(hud, ignore_errors=True); shutil.copytree(app / 'hud_v4_ui', hud)
|
||||
goal = ios / 'Gen/assets/NativeHUD/mapgoal' # 小地图任务终点(gen_map_goal_art.py 的产物,按它的 mapgoal.json 核齐) | minimap quest goals (gen_map_goal_art.py output, checked complete against its mapgoal.json)
|
||||
shutil.rmtree(goal, ignore_errors=True); shutil.copytree(app / 'mapgoal', goal)
|
||||
gm = json.loads((goal / 'mapgoal.json').read_text())
|
||||
need = ['cross.png'] + ['sym_%d.png' % i for i in range(gm['symbol_frames'])]
|
||||
assert all((goal / f).is_file() for f in need), 'mapgoal incomplete'
|
||||
print(len(rows), 'rows,', len(atl), 'atlases,', len(list(hud.iterdir())), 'hud files,', len(list(goal.iterdir())), 'mapgoal files')
|
||||
@@ -0,0 +1,51 @@
|
||||
#!/bin/sh
|
||||
# Package build-android/app/libd2r.so + GameActivity into a debug-signed APK, without Gradle.
|
||||
# make -f Makefile.android APP=1 build-android/app/libd2r.so && sh android/app/build_apk.sh
|
||||
# Env: ANDROID_SDK (default ~/Library/Android/sdk), BUILD_TOOLS (35.0.0), PLATFORM (android-33),
|
||||
# JAVA_HOME (default: Android Studio's bundled JBR), KEYSTORE (default ~/.android/debug.keystore).
|
||||
set -eu
|
||||
here=$(cd "$(dirname "$0")" && pwd)
|
||||
host=$(cd "$here/../.." && pwd)
|
||||
out=$host/build-android/app
|
||||
sdk=${ANDROID_SDK:-$HOME/Library/Android/sdk}
|
||||
bt=$sdk/build-tools/${BUILD_TOOLS:-35.0.0}
|
||||
jar=$sdk/platforms/${PLATFORM:-android-33}/android.jar
|
||||
jh=${JAVA_HOME:-"/Applications/Android Studio.app/Contents/jbr/Contents/Home"}
|
||||
ks=${KEYSTORE:-$HOME/.android/debug.keystore}
|
||||
so=$out/libd2r.so
|
||||
|
||||
[ -f "$so" ] || { echo "missing $so (build with APP=1 first)" >&2; exit 1; }
|
||||
[ -f "$jar" ] || { echo "missing $jar" >&2; exit 1; }
|
||||
[ -x "$jh/bin/javac" ] || { echo "no javac at $jh (set JAVA_HOME to JDK 11+)" >&2; exit 1; }
|
||||
|
||||
w=$out/apk
|
||||
rm -rf "$w"; mkdir -p "$w/classes" "$w/dex" "$w/lib/arm64-v8a"
|
||||
|
||||
"$jh/bin/javac" -nowarn --release 11 -classpath "$jar" -d "$w/classes" \
|
||||
$(find "$here/java" -name '*.java')
|
||||
PATH="$jh/bin:$PATH" "$bt/d8" --release --min-api 29 --lib "$jar" --output "$w/dex" \
|
||||
$(find "$w/classes" -name '*.class')
|
||||
|
||||
# 触屏 HUD 素材(从玩家自己的游戏文件生成,不进仓库):TouchArt/ = ios/tools/native_art/generate.py,hud_v4_ui/ = ios/tools/gen_hud_v4.py,mapgoal/ = ios/tools/gen_map_goal_art.py
|
||||
# Touch HUD assets (generated from the player's own game files, not in the repo): TouchArt/ = ios/tools/native_art/generate.py, hud_v4_ui/ = ios/tools/gen_hud_v4.py, mapgoal/ = ios/tools/gen_map_goal_art.py
|
||||
ios=$host/../ios
|
||||
mkdir -p "$w/assets/TouchArt"
|
||||
cp "$ios"/Sources/TouchArt/*.png "$w/assets/TouchArt/" 2>/dev/null || echo "warning: no TouchArt pngs (run ios/tools/gen_assets.py)" >&2
|
||||
if [ -d "$ios/Gen/assets/NativeHUD/v4/hud_v4_ui" ]; then cp -R "$ios/Gen/assets/NativeHUD/v4/hud_v4_ui" "$w/assets/"
|
||||
else echo "warning: no hud_v4_ui (run ios/tools/gen_assets.py)" >&2; fi
|
||||
if [ -d "$ios/Gen/assets/NativeHUD/mapgoal" ]; then rm -rf "$w/assets/mapgoal"; cp -R "$ios/Gen/assets/NativeHUD/mapgoal" "$w/assets/mapgoal" # 小地图任务终点 = ios/tools/gen_map_goal_art.py | minimap quest goals = ios/tools/gen_map_goal_art.py
|
||||
else echo "warning: no mapgoal (run ios/tools/gen_assets.py)" >&2; fi
|
||||
# 资源只有启动图标(res/:自适应图标,图同 iOS AppIcon1024.png)| The only resource is the launcher icon (res/: adaptive icon, same art as iOS AppIcon1024.png)
|
||||
"$bt/aapt2" compile --dir "$here/res" -o "$w/res.zip"
|
||||
"$bt/aapt2" link -o "$w/base.apk" --manifest "$here/AndroidManifest.xml" -I "$jar" \
|
||||
--min-sdk-version 29 --target-sdk-version 33 -A "$w/assets" -R "$w/res.zip" --auto-add-overlay
|
||||
cp "$so" "$w/lib/arm64-v8a/libd2r.so"
|
||||
(cd "$w/dex" && zip -q -X "$w/base.apk" classes.dex)
|
||||
# Native libs stored uncompressed so they stay page-aligned for zipalign -P.
|
||||
(cd "$w" && zip -q -X -0 base.apk lib/arm64-v8a/libd2r.so)
|
||||
|
||||
"$bt/zipalign" -f -P 16 4 "$w/base.apk" "$w/aligned.apk"
|
||||
PATH="$jh/bin:$PATH" "$bt/apksigner" sign --ks "$ks" --ks-pass pass:android \
|
||||
--ks-key-alias androiddebugkey --key-pass pass:android \
|
||||
--out "$out/d2r.apk" "$w/aligned.apk"
|
||||
echo "$out/d2r.apk"
|
||||
@@ -0,0 +1,236 @@
|
||||
// Android App 前端(libd2r.so,由 GameActivity 加载):替换无头的 front_headless.c。
|
||||
// 宿主照常从 main() 起跑(在 JNI 起的线程里),这里给它窗口、输入、软键盘和前后台切换。
|
||||
// 游戏数据放在 App 私有目录 files/d2r/(dm/ 模块、romfs/、romfs_overlay/、rt64/ 着色器包、save/、temp/);
|
||||
// files/d2r/env.txt 每行 KEY=VALUE 在起跑前 setenv(调试开关,如 DM_VK_VRS=2x2)。日志写 files/d2r/run.log。
|
||||
// Android app front end (libd2r.so, loaded by GameActivity): replaces the headless front_headless.c.
|
||||
// The host starts from main() as usual (on a thread started over JNI); this file gives it the window, input, software keyboard and foreground / background switching.
|
||||
// Game data lives in the app's private directory files/d2r/ (dm/ modules, romfs/, romfs_overlay/, rt64/ shader pack, save/, temp/);
|
||||
// each KEY=VALUE line of files/d2r/env.txt is setenv'd before start (debug switches such as DM_VK_VRS=2x2). The log is written to files/d2r/run.log.
|
||||
#include "../../dm.h"
|
||||
#include "../../hle.h"
|
||||
#include "../../front.h"
|
||||
#include "../../dm_input.h"
|
||||
#include "../../dm_padmap.h"
|
||||
#include "../../dm_cursor.h"
|
||||
#include "../vk_platform.h"
|
||||
#include <jni.h>
|
||||
#include <android/native_window_jni.h>
|
||||
#include <android/log.h>
|
||||
#include <fcntl.h>
|
||||
#include <math.h>
|
||||
#include <signal.h>
|
||||
#include <stdatomic.h>
|
||||
#include <string.h>
|
||||
#include <sys/stat.h>
|
||||
#include <unistd.h>
|
||||
|
||||
int main(int argc, char **argv);
|
||||
|
||||
static char g_root[512];
|
||||
static unsigned g_screen_w, g_screen_h;
|
||||
static _Atomic int g_started, g_window;
|
||||
|
||||
static void path_of(char *out, size_t n, const char *leaf) { snprintf(out, n, "%s/%s", g_root, leaf); }
|
||||
static int exists(const char *p) { struct stat st; return !stat(p, &st); }
|
||||
|
||||
// env.txt:KEY=VALUE 每行一条,# 开头为注释 | env.txt: one KEY=VALUE per line, lines starting with # are comments
|
||||
static void env_file(void) {
|
||||
char p[600]; path_of(p, sizeof p, "env.txt"); FILE *f = fopen(p, "r"); if (!f) return;
|
||||
char line[1024];
|
||||
while (fgets(line, sizeof line, f)) {
|
||||
line[strcspn(line, "\r\n")] = 0; char *eq = strchr(line, '=');
|
||||
if (line[0] == '#' || !eq) continue;
|
||||
*eq = 0; setenv(line, eq + 1, 1); LOG("app: env %s=%s", line, eq + 1);
|
||||
}
|
||||
fclose(f);
|
||||
}
|
||||
|
||||
static void *host_thread(void *unused) {
|
||||
(void)unused;
|
||||
char dm[600]; path_of(dm, sizeof dm, "dm");
|
||||
char *argv[] = {"dmhost", dm, "main", "sdk", NULL};
|
||||
int rc = main(4, argv);
|
||||
LOG("app: 宿主 main 返回 %d | host main returned %d", rc, rc);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// ---- front.h ----
|
||||
// 客体在 64 MB 栈的线程里跑(与无头前端、iOS 壳一致)| The guest runs on a thread with a 64 MB stack (same as the headless front end and the iOS shell)
|
||||
static void *guest_thread(void *p) {
|
||||
static char alt[65536]; // 信号备用栈每线程一份:客体栈溢出时崩溃打印也要能跑 | per-thread alternate signal stack, so the crash printout runs even on guest stack overflow
|
||||
stack_t ss = {.ss_sp = alt, .ss_size = sizeof alt}; sigaltstack(&ss, NULL);
|
||||
sigset_t all; sigfillset(&all); pthread_sigmask(SIG_UNBLOCK, &all, NULL); // JNI 线程继承了 ART 的信号屏蔽字 | the JNI thread inherited ART's signal mask
|
||||
void touch_app_guest_start(void); touch_app_guest_start(); // 触屏 HUD(touch_app.c)| touch HUD (touch_app.c)
|
||||
((void (*)(void))p)(); return NULL;
|
||||
}
|
||||
void front_run(void (*guest)(void)) {
|
||||
pthread_attr_t a; pthread_t t;
|
||||
pthread_attr_init(&a); pthread_attr_setstacksize(&a, 64 << 20);
|
||||
if (pthread_create(&t, &a, guest_thread, (void *)guest)) die("建客体线程失败");
|
||||
pthread_join(t, NULL);
|
||||
}
|
||||
// 只当「有窗口」标志用(hle_swkbd 据此决定弹不弹输入框)| Used only as a "has a window" flag (hle_swkbd decides from it whether to show the input box)
|
||||
void *front_metal_layer(void) { return (void *)(uintptr_t)1; }
|
||||
// 主机档:原版高度 900 × 屏幕比例(宽取 8 的倍数,≤ 4096);掌机档 720 同比。比 16:9 窄的屏按 16:9。DM_GAME_RES=宽x高 覆盖
|
||||
// Docked profile: the original height 900 × the screen aspect ratio (width rounded to a multiple of 8, ≤ 4096); handheld uses 720 the same way. Screens narrower than 16:9 use 16:9. DM_GAME_RES=WxH overrides
|
||||
uint64_t front_game_res(int docked) {
|
||||
unsigned w, h; const char *e = getenv("DM_GAME_RES");
|
||||
if (e && sscanf(e, "%ux%u", &w, &h) == 2) return docked ? (uint64_t)h << 32 | w : (uint64_t)(h * 4 / 5) << 32 | (w * 4 / 5 + 3) / 4 * 4;
|
||||
double aspect = g_screen_w && g_screen_h ? (double)g_screen_w / g_screen_h : 16.0 / 9;
|
||||
if (aspect < 16.0 / 9) aspect = 16.0 / 9;
|
||||
uint64_t hh = docked ? 900 : 720, ww = (uint64_t)llround(hh * aspect / 8) * 8;
|
||||
if (ww > 4096) ww = 4096;
|
||||
return hh << 32 | ww;
|
||||
}
|
||||
|
||||
// 软键盘:客体线程挂一条请求并阻塞;Java 界面线程轮询 nativeTextRequest 弹框,结果经 nativeTextResult 交回
|
||||
// Software keyboard: the guest thread posts a request and blocks; the Java UI thread polls nativeTextRequest to show the dialog and returns the result via nativeTextResult
|
||||
static pthread_mutex_t text_mu = PTHREAD_MUTEX_INITIALIZER; static pthread_cond_t text_cv = PTHREAD_COND_INITIALIZER;
|
||||
static struct { int pending, done, ok, max_chars; const char *title, *sub, *guide, *initial; char result[1024]; } g_text;
|
||||
int front_text_input(const char *title, const char *sub, const char *guide, const char *initial, int max_chars, char *out, int out_cap) {
|
||||
pthread_mutex_lock(&text_mu);
|
||||
g_text.title = title ? title : ""; g_text.sub = sub ? sub : ""; g_text.guide = guide ? guide : ""; g_text.initial = initial ? initial : "";
|
||||
g_text.max_chars = max_chars; g_text.done = 0; g_text.pending = 1;
|
||||
while (!g_text.done) pthread_cond_wait(&text_cv, &text_mu);
|
||||
int ok = g_text.ok; if (ok) snprintf(out, (size_t)out_cap, "%s", g_text.result);
|
||||
pthread_mutex_unlock(&text_mu);
|
||||
return ok;
|
||||
}
|
||||
|
||||
// ---- JNI ----
|
||||
#define JNI(name) JNICALL Java_org_d2rmobile_app_GameActivity_##name
|
||||
|
||||
// root = files/d2r;返回 0 = 已起跑,否则错误码(缺文件等)| root = files/d2r; returns 0 = started, otherwise an error code (missing files etc.)
|
||||
JNIEXPORT jint JNI(nativeStart)(JNIEnv *env, jclass cls, jstring root, jint screen_w, jint screen_h) {
|
||||
(void)cls;
|
||||
if (atomic_exchange(&g_started, 1)) return 0;
|
||||
const char *r = (*env)->GetStringUTFChars(env, root, NULL); snprintf(g_root, sizeof g_root, "%s", r); (*env)->ReleaseStringUTFChars(env, root, r);
|
||||
g_screen_w = (unsigned)(screen_w > screen_h ? screen_w : screen_h); g_screen_h = (unsigned)(screen_w > screen_h ? screen_h : screen_w);
|
||||
char p[600];
|
||||
path_of(p, sizeof p, "run.log"); int fd = open(p, O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 0644);
|
||||
if (fd >= 0) { dup2(fd, 2); close(fd); setvbuf(stderr, NULL, _IONBF, 0); }
|
||||
if (chdir(g_root)) { __android_log_print(ANDROID_LOG_ERROR, "d2r", "chdir %s failed", g_root); return 2; }
|
||||
path_of(p, sizeof p, "dm/main.patched.bin"); if (!exists(p)) return 3;
|
||||
path_of(p, sizeof p, "romfs"); if (!exists(p)) return 4;
|
||||
path_of(p, sizeof p, "rt64"); if (!exists(p)) return 5;
|
||||
// 缺省值:命令行版 run.sh 的那几项;env.txt 可覆盖 | Defaults: the items of the command-line run.sh; env.txt may override
|
||||
setenv("DM_ROOT", g_root, 1);
|
||||
path_of(p, sizeof p, "romfs"); setenv("DM_ROMFS", p, 1);
|
||||
path_of(p, sizeof p, "romfs_overlay"); if (exists(p)) setenv("DM_ROMFS_OVERLAY", p, 1);
|
||||
path_of(p, sizeof p, "save"); mkdir(p, 0755); setenv("DM_SAVE", p, 1);
|
||||
path_of(p, sizeof p, "temp"); mkdir(p, 0755); setenv("DM_TEMP", p, 1);
|
||||
path_of(p, sizeof p, "rt64"); setenv("DM_VK_SHADERS", p, 1);
|
||||
// App 总有窗口,只是熄屏启动时第一次出图前还没拿到:出图间隔模式不能靠那一刻「有没有窗口」定(否则锁成 game 模式的间隔 2 = 30 帧)
|
||||
// The app always has a window, it just may not have one yet before the first present when started with the screen off: the present interval mode must not be decided by "is there a window" at that moment (that locks game mode, interval 2 = 30 fps)
|
||||
setenv("DM_PRESENT_INTERVAL", "display", 1);
|
||||
env_file();
|
||||
LOG("app: 起跑 root=%s screen=%ux%u game=%llux%llu", g_root, g_screen_w, g_screen_h,
|
||||
(unsigned long long)(uint32_t)front_game_res(1), (unsigned long long)(front_game_res(1) >> 32));
|
||||
dm_vk_hold_without_window(1);
|
||||
pthread_attr_t a; pthread_t t;
|
||||
pthread_attr_init(&a); pthread_attr_setstacksize(&a, 8 << 20);
|
||||
if (pthread_create(&t, &a, host_thread, NULL)) return 6;
|
||||
pthread_detach(t);
|
||||
return 0;
|
||||
}
|
||||
|
||||
// surface = NULL:窗口没了(切后台 / 销毁),出图阻塞到窗口回来 | surface = NULL: the window is gone (background / destroyed), present blocks until it returns
|
||||
JNIEXPORT void JNI(nativeSurface)(JNIEnv *env, jclass cls, jobject surface, jfloat hz) {
|
||||
(void)cls;
|
||||
ANativeWindow *w = surface ? ANativeWindow_fromSurface(env, surface) : NULL;
|
||||
if (hz > 0) dm_vk_set_display_hz((int)(hz + 0.5f));
|
||||
dm_vk_set_window(w); // 后端自己 acquire 一份 | the backend acquires its own reference
|
||||
if (w) ANativeWindow_release(w);
|
||||
atomic_store(&g_window, w != NULL);
|
||||
LOG("app: surface %s", w ? "attached" : "detached");
|
||||
}
|
||||
|
||||
// ---- 前后台(同 iOS front_ios_focus_event)| foreground / background (same as iOS front_ios_focus_event) ----
|
||||
// 进后台(onStop):局内先请游戏存一次盘、等提交完成再挂起。等的时候游戏主循环照常跑,窗口可能已经没了,所以临时不让出图因「没窗口」阻塞
|
||||
// (没窗口就照常回放、不上屏,同 adb 无头);等到「出现过存档提交、之后 0.2 秒没有新提交」(角色与共享箱分两次),最多 2.5 秒。
|
||||
// 之后 dm_gfx_suspend(1) 挡住后续提交 / 出图——必须在恢复「没窗口阻塞」之前:出图若已停在等窗口,挂起会等它退出而卡死
|
||||
// Entering background (onStop): in a run, first ask the game to save once and wait for the commit before suspending. While waiting the game main loop runs as usual and the window may already be gone, so presents temporarily do not block on "no window"
|
||||
// (with no window they replay as usual without presenting, like adb headless); wait until "a save commit has appeared and no new commit for 0.2 s after it" (character and shared stash commit separately), at most 2.5 s.
|
||||
// Then dm_gfx_suspend(1) blocks later submits / presents -- this must come before "block without window" is restored: if a present were already parked waiting for a window, the suspend would wait for it to exit and deadlock
|
||||
static void background_wait_save(void) {
|
||||
int dm_ingame_now(void); uint64_t dm_fs_commit_count(void); int dm_request_save(void);
|
||||
if (!dm_ingame_now() || !dm_request_save()) return; // 让游戏下一拍走它自己的定时存盘(diag.c) | let the game run its own timed save on the next tick (diag.c)
|
||||
uint64_t c0 = dm_fs_commit_count(), t0 = now_ns(), last = c0, last_t = 0;
|
||||
for (;;) {
|
||||
usleep(20000);
|
||||
uint64_t c = dm_fs_commit_count(), t = now_ns();
|
||||
if (c != last) { last = c; last_t = t; }
|
||||
if (last_t && t - last_t >= 200000000ull) break;
|
||||
if (t - t0 >= 2500000000ull) break;
|
||||
}
|
||||
LOG("app: 进后台等游戏存盘 %.0f ms,期间存档提交 %llu 次%s", (now_ns() - t0) / 1e6, (unsigned long long)(last - c0), last == c0 ? "【红】(2.5 秒内没等到)" : "");
|
||||
}
|
||||
// 失活(onPause)也请游戏存一次盘、不等:多任务界面里直接划掉进程时未必走得到 onStop 的存盘;30 秒内只请一次
|
||||
// Pausing (onPause) also asks the game to save once without waiting: swiping the process away in the recents screen may not reach the onStop save; at most once per 30 s
|
||||
static void resign_save(void) {
|
||||
int dm_ingame_now(void); int dm_request_save(void);
|
||||
static uint64_t last; uint64_t now = now_ns();
|
||||
if (!dm_ingame_now() || (last && now - last < 30000000000ull)) return;
|
||||
if (dm_request_save()) { last = now; LOG("app: 失活,请游戏下一拍存一次盘(不等)"); }
|
||||
}
|
||||
// ev:0 onPause(失活)、1 onStop(进后台)、2 onStart(回前台)、3 onResume(激活)。在 UI 线程调;1 最多挡 2.5 秒
|
||||
// ev: 0 onPause (resign active), 1 onStop (background), 2 onStart (foreground), 3 onResume (active). Called on the UI thread; 1 blocks for at most 2.5 s
|
||||
JNIEXPORT void JNI(nativeLifecycle)(JNIEnv *env, jclass cls, jint ev) {
|
||||
(void)env; (void)cls;
|
||||
static const char *k[] = {"onPause", "onStop", "onStart", "onResume"};
|
||||
static int bg; // 进过后台、还没回前台(第一次 onStart 没有对应的 onStop) | went to background and not back yet (the first onStart has no matching onStop)
|
||||
if (ev < 0 || ev > 3 || !atomic_load(&g_started)) return;
|
||||
{ uint64_t guest_ns(void); LOG("app: %s(客体时钟 %.3f s)", k[ev], guest_ns() / 1e9); }
|
||||
void guest_clock_freeze_for(int reason, int on); void dm_ui_lifecycle(int background); void dm_3d_scale_resumed(void);
|
||||
switch (ev) {
|
||||
case 0: dm_focus_set(2); resign_save(); break; // 触点、手柄键由 Java 端先清 | touches and controller keys are cleared on the Java side first
|
||||
// 客体时钟一并冻结(等效 Switch 挂起):否则回来时游戏内时间一下跳过整段后台时长 | the guest clock is frozen as well (like Switch suspend): otherwise in-game time jumps over the whole background period on return
|
||||
case 1: dm_focus_set(3); dm_ui_lifecycle(1); dm_audio_suspend(1);
|
||||
dm_vk_hold_without_window(0); background_wait_save(); dm_gfx_suspend(1); dm_vk_hold_without_window(1);
|
||||
guest_clock_freeze_for(1, 1); bg = 1; break;
|
||||
case 2: if (!bg) break; bg = 0;
|
||||
dm_ui_lifecycle(0); dm_3d_scale_resumed(); dm_gfx_suspend(0); guest_clock_freeze_for(1, 0); dm_audio_suspend(0); dm_focus_set(2); break; // 窗口未回来前出图仍停在等窗口 | until the window returns, presents still park waiting for it
|
||||
case 3: dm_focus_set(1); break;
|
||||
}
|
||||
}
|
||||
|
||||
// 游戏已出的帧数,Java 端据此算 FPS 标签 | frames the game has presented, for the Java FPS label
|
||||
JNIEXPORT jlong JNI(nativeFrames)(JNIEnv *env, jclass cls) {
|
||||
(void)env; (void)cls;
|
||||
extern uint64_t mtl_present_count(void);
|
||||
return (jlong)mtl_present_count();
|
||||
}
|
||||
|
||||
// 触点由 touch_app.c 的 nativeTouchEvent 接 | touches are taken by nativeTouchEvent in touch_app.c
|
||||
|
||||
// 实体手柄:face 位 0–3 = 下 / 右 / 左 / 上面键(按位置),其余按钮已是 DM_BTN_* 位;摇杆 ±1 浮点
|
||||
// Physical controller: face bits 0–3 = bottom / right / left / top face buttons (by position), other buttons already as DM_BTN_* bits; sticks as ±1 floats
|
||||
JNIEXPORT void JNI(nativePad)(JNIEnv *env, jclass cls, jint face, jint other, jboolean nintendo, jfloat lx, jfloat ly, jfloat rx, jfloat ry) {
|
||||
(void)env; (void)cls;
|
||||
uint64_t b = dm_padmap_face(nintendo ? DM_PADKIND_NINTENDO : DM_PADKIND_XBOX, face & 1, face >> 1 & 1, face >> 2 & 1, face >> 3 & 1) | (uint32_t)other;
|
||||
// Android 的 Y 轴向下为正,nn::hid 向上为正 | Android's Y axis is positive downwards, nn::hid's is positive upwards
|
||||
void touch_app_pad(uint64_t, int32_t, int32_t, int32_t, int32_t);
|
||||
touch_app_pad(b, (int32_t)(lx * DM_STICK_MAX), (int32_t)(-ly * DM_STICK_MAX), (int32_t)(rx * DM_STICK_MAX), (int32_t)(-ry * DM_STICK_MAX));
|
||||
}
|
||||
|
||||
// 软键盘请求:没有就返回 null;有则 [标题, 说明, 提示, 初值, 最大字数] | Software keyboard request: null if none, otherwise [title, subtitle, hint, initial, max chars]
|
||||
JNIEXPORT jobjectArray JNI(nativeTextRequest)(JNIEnv *env, jclass cls) {
|
||||
(void)cls; jobjectArray a = NULL;
|
||||
pthread_mutex_lock(&text_mu);
|
||||
if (g_text.pending) {
|
||||
g_text.pending = 0; char max[16]; snprintf(max, sizeof max, "%d", g_text.max_chars);
|
||||
const char *v[5] = {g_text.title, g_text.sub, g_text.guide, g_text.initial, max};
|
||||
a = (*env)->NewObjectArray(env, 5, (*env)->FindClass(env, "java/lang/String"), NULL);
|
||||
for (int i = 0; i < 5; i++) (*env)->SetObjectArrayElement(env, a, i, (*env)->NewStringUTF(env, v[i]));
|
||||
}
|
||||
pthread_mutex_unlock(&text_mu);
|
||||
return a;
|
||||
}
|
||||
JNIEXPORT void JNI(nativeTextResult)(JNIEnv *env, jclass cls, jboolean ok, jstring text) {
|
||||
(void)cls;
|
||||
pthread_mutex_lock(&text_mu);
|
||||
g_text.ok = ok && text; g_text.result[0] = 0;
|
||||
if (g_text.ok) { const char *s = (*env)->GetStringUTFChars(env, text, NULL); snprintf(g_text.result, sizeof g_text.result, "%s", s); (*env)->ReleaseStringUTFChars(env, text, s); }
|
||||
g_text.done = 1; pthread_cond_broadcast(&text_cv);
|
||||
pthread_mutex_unlock(&text_mu);
|
||||
}
|
||||
@@ -0,0 +1,286 @@
|
||||
// 全屏横屏 Activity:SurfaceView 交给 Vulkan 后端出图,上面盖一层触屏 HUD(TouchOverlay);触点 / 手柄 / 软键盘转给 libd2r.so(front_app.c、touch_app.c)。
|
||||
// Fullscreen landscape Activity: the SurfaceView goes to the Vulkan backend for presenting, with the touch HUD (TouchOverlay) on top; touches / controller / software keyboard go to libd2r.so (front_app.c, touch_app.c).
|
||||
package org.d2rmobile.app;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.app.AlertDialog;
|
||||
import android.os.Bundle;
|
||||
import android.os.VibrationEffect;
|
||||
import android.os.Vibrator;
|
||||
import android.view.Choreographer;
|
||||
import android.view.DisplayCutout;
|
||||
import android.view.WindowInsets;
|
||||
import android.widget.FrameLayout;
|
||||
import android.os.Handler;
|
||||
import android.os.Looper;
|
||||
import android.text.InputFilter;
|
||||
import android.util.DisplayMetrics;
|
||||
import android.view.InputDevice;
|
||||
import android.view.KeyEvent;
|
||||
import android.view.MotionEvent;
|
||||
import android.view.SurfaceHolder;
|
||||
import android.view.SurfaceView;
|
||||
import android.view.View;
|
||||
import android.view.WindowManager;
|
||||
import android.widget.EditText;
|
||||
import android.widget.TextView;
|
||||
import java.io.File;
|
||||
|
||||
public final class GameActivity extends Activity implements SurfaceHolder.Callback {
|
||||
private static native int nativeStart(String root, int screenW, int screenH);
|
||||
private static native void nativeSurface(Object surface, float hz);
|
||||
// 触屏 HUD(touch_app.c),坐标单位 dp | touch HUD (touch_app.c), coordinates in dp
|
||||
private static native void nativeLayout(float w, float h, float safeL, float safeR);
|
||||
private static native void nativeTouchEvent(int action, int pid, int[] pids, float[] xs, float[] ys, int n);
|
||||
private static native int nativeTick();
|
||||
static native Object[] nativeDraw(int part);
|
||||
private static native void nativePad(int face, int other, boolean nintendo, float lx, float ly, float rx, float ry);
|
||||
private static native long nativeFrames();
|
||||
private static native String[] nativeTextRequest();
|
||||
private static native void nativeTextResult(boolean ok, String text);
|
||||
// 前后台:0 onPause、1 onStop(局内等存盘,最多 2.5 s)、2 onStart、3 onResume(front_app.c)| foreground / background: 0 onPause, 1 onStop (waits for the save in a run, at most 2.5 s), 2 onStart, 3 onResume (front_app.c)
|
||||
private static native void nativeLifecycle(int ev);
|
||||
|
||||
// nn::hid 按钮位(dm_input.h)| nn::hid button bits (dm_input.h)
|
||||
private static final int LS = 1 << 4, RS = 1 << 5, L = 1 << 6, R = 1 << 7, ZL = 1 << 8, ZR = 1 << 9,
|
||||
PLUS = 1 << 10, MINUS = 1 << 11, LEFT = 1 << 12, UP = 1 << 13, RIGHT = 1 << 14, DOWN = 1 << 15;
|
||||
|
||||
private final Handler ui = new Handler(Looper.getMainLooper());
|
||||
private SurfaceView view;
|
||||
private boolean started;
|
||||
private TouchOverlay overlay, joy, goals;
|
||||
private float safeL, safeR;
|
||||
private Vibrator vibrator;
|
||||
private final int[] tPids = new int[32]; private final float[] tXs = new float[32], tYs = new float[32];
|
||||
private int padFace, padKeys, padHat;
|
||||
private float lx, ly, rx, ry, lt, rt;
|
||||
private boolean nintendo;
|
||||
private AlertDialog textDialog;
|
||||
// 左上角 FPS 标签:每 1 s 按游戏出帧数更新 | top-left FPS label: refreshed every 1 s from the game's presented frame count
|
||||
private TextView fps;
|
||||
private long fpsFrames, fpsTime;
|
||||
|
||||
/* 游戏自己按 60 Hz(省电 30 Hz)出帧;120 Hz 面板上软件时钟和垂直同步拍频,帧落在 1/2/3 个 vsync 上抖动,所以把屏幕钉在同分辨率的 60 Hz 模式
|
||||
* The game paces itself at 60 Hz (30 Hz in eco); on a 120 Hz panel the software clock beats against vsync and frames land on 1/2/3 vsyncs, so pin the panel to the same-resolution 60 Hz mode */
|
||||
private void pin60Hz() {
|
||||
if (android.os.Build.VERSION.SDK_INT < 23) return;
|
||||
android.view.Display d = getWindowManager().getDefaultDisplay();
|
||||
android.view.Display.Mode cur = d.getMode(), best = null;
|
||||
for (android.view.Display.Mode m : d.getSupportedModes()) {
|
||||
if (m.getPhysicalWidth() != cur.getPhysicalWidth() || m.getPhysicalHeight() != cur.getPhysicalHeight()) continue;
|
||||
if (Math.abs(m.getRefreshRate() - 60f) < 1f) { best = m; break; }
|
||||
}
|
||||
if (best == null) return;
|
||||
WindowManager.LayoutParams lp = getWindow().getAttributes();
|
||||
lp.preferredDisplayModeId = best.getModeId();
|
||||
getWindow().setAttributes(lp);
|
||||
}
|
||||
|
||||
@Override protected void onCreate(Bundle state) {
|
||||
super.onCreate(state);
|
||||
// 库的构造函数要按 DM_ROOT 找 dm/derived.tsv(经验表等),所以先设环境变量再加载 | the library's constructors locate dm/derived.tsv (EXP table etc.) via DM_ROOT, so set it before loading
|
||||
try { android.system.Os.setenv("DM_ROOT", new File(getFilesDir(), "d2r").getAbsolutePath(), true); } catch (Exception e) {}
|
||||
System.loadLibrary("d2r");
|
||||
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
|
||||
if (android.os.Build.VERSION.SDK_INT >= 28)
|
||||
getWindow().getAttributes().layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES;
|
||||
pin60Hz();
|
||||
view = new SurfaceView(this);
|
||||
view.getHolder().addCallback(this);
|
||||
view.setFocusable(true); view.setFocusableInTouchMode(true);
|
||||
// 主体层缓存成 GPU 纹理:手指在摇杆上动时只重画摇杆层(见 touch_app.c op_list) | the body layer is cached as a GPU texture: while a finger moves on the stick only the joystick layer is redrawn (see touch_app.c op_list)
|
||||
overlay = new TouchOverlay(this, 0); overlay.setLayerType(View.LAYER_TYPE_HARDWARE, null);
|
||||
joy = new TouchOverlay(this, 1);
|
||||
goals = new TouchOverlay(this, 2); // 小地图任务终点:跟着玩家走每帧变,单独一层不牵动主体层 | minimap quest goals: change every frame as the player walks, own layer so the body layer is untouched
|
||||
FrameLayout root = new FrameLayout(this);
|
||||
root.addView(view, new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT));
|
||||
root.addView(goals, new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT));
|
||||
root.addView(overlay, new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT));
|
||||
root.addView(joy, new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT));
|
||||
fps = new TextView(this);
|
||||
fps.setTextSize(12); fps.setTextColor(0xffe9d9ba); fps.setBackgroundColor(0x80000000);
|
||||
fps.setTypeface(android.graphics.Typeface.MONOSPACE); fps.setPadding(12, 4, 12, 4);
|
||||
root.addView(fps, new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT));
|
||||
setContentView(root);
|
||||
// 刘海 / 挖孔:横屏左右的安全边距交给布局(同 iOS safeAreaInsets)| cutout / punch hole: the landscape left / right safe insets go to the layout (like iOS safeAreaInsets)
|
||||
root.setOnApplyWindowInsetsListener((v, in) -> {
|
||||
DisplayCutout cut = in.getDisplayCutout();
|
||||
safeL = cut != null ? cut.getSafeInsetLeft() : 0; safeR = cut != null ? cut.getSafeInsetRight() : 0;
|
||||
fps.setTranslationX(safeL);
|
||||
layoutHud(); return in;
|
||||
});
|
||||
overlay.addOnLayoutChangeListener((v, l, t, r, b, ol, ot, or, ob) -> layoutHud());
|
||||
vibrator = getSystemService(Vibrator.class);
|
||||
immersive();
|
||||
ui.post(textPoll);
|
||||
}
|
||||
private void immersive() {
|
||||
getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_FULLSCREEN | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION |
|
||||
View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION |
|
||||
View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
|
||||
}
|
||||
@Override public void onWindowFocusChanged(boolean focus) { super.onWindowFocusChanged(focus); if (focus) immersive(); }
|
||||
|
||||
private void fail(String text) {
|
||||
TextView t = new TextView(this); t.setText(text); t.setTextSize(18); t.setPadding(48, 48, 48, 48);
|
||||
t.setTextColor(0xffe9d9ba); t.setBackgroundColor(0xff0d0d0f); setContentView(t);
|
||||
}
|
||||
|
||||
// ---- Surface ----
|
||||
@Override public void surfaceCreated(SurfaceHolder h) {}
|
||||
@Override public void surfaceChanged(SurfaceHolder h, int format, int w, int hh) {
|
||||
if (!started) {
|
||||
DisplayMetrics m = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getRealMetrics(m);
|
||||
File root = new File(getFilesDir(), "d2r");
|
||||
int rc = nativeStart(root.getAbsolutePath(), m.widthPixels, m.heightPixels);
|
||||
if (rc != 0) { fail("游戏文件不全(错误 " + rc + ")\n请把 dm/、romfs/、rt64/ 放到\n" + root.getAbsolutePath()); return; }
|
||||
started = true;
|
||||
layoutHud();
|
||||
Choreographer.getInstance().postFrameCallback(frame);
|
||||
}
|
||||
// 同步交出窗口:surfaceDestroyed 返回前后端必须已放手,这里对称处理 | Hand the window over synchronously: the backend must let go before surfaceDestroyed returns, handled symmetrically here
|
||||
nativeSurface(h.getSurface(), getWindowManager().getDefaultDisplay().getRefreshRate());
|
||||
}
|
||||
@Override public void surfaceDestroyed(SurfaceHolder h) {
|
||||
if (started) nativeSurface(null, 0);
|
||||
cancelTouches();
|
||||
}
|
||||
|
||||
// ---- 触屏 HUD | touch HUD ----
|
||||
private void layoutHud() {
|
||||
if (!started || overlay.getWidth() <= 0) return;
|
||||
float d = getResources().getDisplayMetrics().density;
|
||||
nativeLayout(overlay.getWidth() / d, overlay.getHeight() / d, safeL / d, safeR / d);
|
||||
overlay.invalidate(); joy.invalidate(); goals.invalidate();
|
||||
}
|
||||
// 每帧:推进 HUD 状态机(长按、松手脉冲、腰带 …),要重画才 invalidate;掉落震动按档位 | per frame: advance the HUD state machine (long press, release pulse, belt …), invalidate only when a redraw is needed; drop haptics by level
|
||||
private final Choreographer.FrameCallback frame = new Choreographer.FrameCallback() { public void doFrame(long t) {
|
||||
if (!started) return;
|
||||
int r = nativeTick();
|
||||
if ((r & 1) != 0) overlay.invalidate();
|
||||
if ((r & 2) != 0) joy.invalidate();
|
||||
if ((r & 4) != 0) goals.invalidate();
|
||||
if (t - fpsTime >= 1_000_000_000L) {
|
||||
long n = nativeFrames();
|
||||
if (fpsTime != 0) fps.setText(String.format(java.util.Locale.ROOT, "%.1f FPS", (n - fpsFrames) * 1e9 / (t - fpsTime)));
|
||||
fpsFrames = n; fpsTime = t;
|
||||
}
|
||||
int hap = r >> 4 & 3;
|
||||
if (hap != 0 && vibrator != null && vibrator.hasVibrator())
|
||||
vibrator.vibrate(VibrationEffect.createPredefined(hap == 1 ? VibrationEffect.EFFECT_HEAVY_CLICK : hap == 2 ? VibrationEffect.EFFECT_CLICK : VibrationEffect.EFFECT_TICK));
|
||||
Choreographer.getInstance().postFrameCallback(this);
|
||||
}};
|
||||
private void cancelTouches() { if (started) nativeTouchEvent(3, -1, null, null, null, 0); }
|
||||
@Override public boolean onTouchEvent(MotionEvent e) {
|
||||
if (!started) return true;
|
||||
int act = e.getActionMasked(), action;
|
||||
switch (act) {
|
||||
case MotionEvent.ACTION_DOWN: case MotionEvent.ACTION_POINTER_DOWN: action = 0; break;
|
||||
case MotionEvent.ACTION_MOVE: action = 1; break;
|
||||
case MotionEvent.ACTION_UP: case MotionEvent.ACTION_POINTER_UP: action = 2; break;
|
||||
case MotionEvent.ACTION_CANCEL: action = 3; break;
|
||||
default: return true;
|
||||
}
|
||||
float d = getResources().getDisplayMetrics().density;
|
||||
int n = Math.min(e.getPointerCount(), tPids.length);
|
||||
for (int i = 0; i < n; i++) { tPids[i] = e.getPointerId(i); tXs[i] = e.getX(i) / d; tYs[i] = e.getY(i) / d; }
|
||||
int pid = action == 0 || action == 2 ? e.getPointerId(e.getActionIndex()) : -1;
|
||||
nativeTouchEvent(action, pid, tPids, tXs, tYs, n); // 重画交给同一帧随后的 doFrame(nativeTick 比对指令表) | the redraw is left to the doFrame later in the same frame (nativeTick compares the draw lists)
|
||||
return true;
|
||||
}
|
||||
|
||||
// ---- 实体手柄 | physical controller ----
|
||||
private static boolean isPad(int source) {
|
||||
return (source & InputDevice.SOURCE_GAMEPAD) == InputDevice.SOURCE_GAMEPAD || (source & InputDevice.SOURCE_JOYSTICK) == InputDevice.SOURCE_JOYSTICK;
|
||||
}
|
||||
private void padPublish() {
|
||||
int other = padKeys | padHat | (lt > 0.5f ? ZL : 0) | (rt > 0.5f ? ZR : 0);
|
||||
nativePad(padFace, other, nintendo, lx, ly, rx, ry);
|
||||
}
|
||||
private static float dead(float v) { return Math.abs(v) < 0.12f ? 0 : v; }
|
||||
@Override public boolean dispatchKeyEvent(KeyEvent e) {
|
||||
int code = e.getKeyCode();
|
||||
if (code == KeyEvent.KEYCODE_BACK && !isPad(e.getSource())) return true; // 系统返回键不退出游戏 | the system back key does not quit the game
|
||||
if (!started || !isPad(e.getSource())) return super.dispatchKeyEvent(e);
|
||||
int face = 0, key = 0;
|
||||
switch (code) {
|
||||
case KeyEvent.KEYCODE_BUTTON_A: face = 1; break; // 下 | bottom
|
||||
case KeyEvent.KEYCODE_BUTTON_B: face = 2; break; // 右 | right
|
||||
case KeyEvent.KEYCODE_BUTTON_X: face = 4; break; // 左 | left
|
||||
case KeyEvent.KEYCODE_BUTTON_Y: face = 8; break; // 上 | top
|
||||
case KeyEvent.KEYCODE_BUTTON_L1: key = L; break;
|
||||
case KeyEvent.KEYCODE_BUTTON_R1: key = R; break;
|
||||
case KeyEvent.KEYCODE_BUTTON_L2: key = ZL; break;
|
||||
case KeyEvent.KEYCODE_BUTTON_R2: key = ZR; break;
|
||||
case KeyEvent.KEYCODE_BUTTON_THUMBL: key = LS; break;
|
||||
case KeyEvent.KEYCODE_BUTTON_THUMBR: key = RS; break;
|
||||
case KeyEvent.KEYCODE_BUTTON_START: key = PLUS; break;
|
||||
case KeyEvent.KEYCODE_BUTTON_SELECT: case KeyEvent.KEYCODE_BACK: key = MINUS; break;
|
||||
case KeyEvent.KEYCODE_DPAD_UP: key = UP; break;
|
||||
case KeyEvent.KEYCODE_DPAD_DOWN: key = DOWN; break;
|
||||
case KeyEvent.KEYCODE_DPAD_LEFT: key = LEFT; break;
|
||||
case KeyEvent.KEYCODE_DPAD_RIGHT: key = RIGHT; break;
|
||||
default: return super.dispatchKeyEvent(e);
|
||||
}
|
||||
InputDevice d = e.getDevice(); nintendo = d != null && d.getName() != null && d.getName().toLowerCase().matches(".*(nintendo|switch|joy-con).*");
|
||||
boolean down = e.getAction() == KeyEvent.ACTION_DOWN;
|
||||
if (face != 0) padFace = down ? padFace | face : padFace & ~face;
|
||||
else padKeys = down ? padKeys | key : padKeys & ~key;
|
||||
padPublish();
|
||||
return true;
|
||||
}
|
||||
@Override public boolean dispatchGenericMotionEvent(MotionEvent e) {
|
||||
if (!started || !isPad(e.getSource()) || e.getAction() != MotionEvent.ACTION_MOVE) return super.dispatchGenericMotionEvent(e);
|
||||
lx = dead(e.getAxisValue(MotionEvent.AXIS_X)); ly = dead(e.getAxisValue(MotionEvent.AXIS_Y));
|
||||
rx = dead(e.getAxisValue(MotionEvent.AXIS_Z)); ry = dead(e.getAxisValue(MotionEvent.AXIS_RZ));
|
||||
lt = Math.max(e.getAxisValue(MotionEvent.AXIS_LTRIGGER), e.getAxisValue(MotionEvent.AXIS_BRAKE));
|
||||
rt = Math.max(e.getAxisValue(MotionEvent.AXIS_RTRIGGER), e.getAxisValue(MotionEvent.AXIS_GAS));
|
||||
float hx = e.getAxisValue(MotionEvent.AXIS_HAT_X), hy = e.getAxisValue(MotionEvent.AXIS_HAT_Y);
|
||||
padHat = (hx < -0.5f ? LEFT : 0) | (hx > 0.5f ? RIGHT : 0) | (hy < -0.5f ? UP : 0) | (hy > 0.5f ? DOWN : 0);
|
||||
padPublish();
|
||||
return true;
|
||||
}
|
||||
|
||||
// ---- 软键盘 | software keyboard ----
|
||||
private final Runnable textPoll = new Runnable() { public void run() {
|
||||
if (started && textDialog == null) {
|
||||
String[] r = nativeTextRequest();
|
||||
if (r != null) showText(r);
|
||||
}
|
||||
ui.postDelayed(this, 250);
|
||||
}};
|
||||
private void showText(String[] r) {
|
||||
final EditText edit = new EditText(this);
|
||||
edit.setSingleLine(true); edit.setText(r[3]); edit.setSelection(edit.getText().length());
|
||||
if (!r[2].isEmpty()) edit.setHint(r[2]);
|
||||
int max = Integer.parseInt(r[4]); if (max > 0) edit.setFilters(new InputFilter[]{new InputFilter.LengthFilter(max)});
|
||||
String msg = r[1] + (!r[1].isEmpty() && !r[2].isEmpty() ? "\n" : "") + r[2];
|
||||
textDialog = new AlertDialog.Builder(this).setTitle(r[0].isEmpty() ? "输入" : r[0]).setMessage(msg.isEmpty() ? null : msg).setView(edit)
|
||||
.setPositiveButton(android.R.string.ok, (d, w) -> { nativeTextResult(true, edit.getText().toString()); textDialog = null; })
|
||||
.setNegativeButton(android.R.string.cancel, (d, w) -> { nativeTextResult(false, null); textDialog = null; })
|
||||
.setOnCancelListener(d -> { nativeTextResult(false, null); textDialog = null; })
|
||||
.create();
|
||||
textDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
|
||||
textDialog.show();
|
||||
}
|
||||
|
||||
// ---- 前后台 | foreground / background ----
|
||||
// 切走时按着的触点和手柄键一并清零:在后台松开收不到「松开」| touches and controller keys held while switching away are cleared: a release in the background never arrives
|
||||
@Override protected void onPause() {
|
||||
super.onPause();
|
||||
if (!started) return;
|
||||
cancelTouches();
|
||||
padFace = padKeys = padHat = 0; lx = ly = rx = ry = lt = rt = 0; padPublish();
|
||||
nativeLifecycle(0);
|
||||
}
|
||||
@Override protected void onStop() { super.onStop(); if (started) nativeLifecycle(1); }
|
||||
@Override protected void onStart() { super.onStart(); if (started) nativeLifecycle(2); }
|
||||
@Override protected void onResume() { super.onResume(); if (started) nativeLifecycle(3); }
|
||||
|
||||
@Override protected void onDestroy() {
|
||||
ui.removeCallbacks(textPoll);
|
||||
super.onDestroy();
|
||||
// 宿主不支持在同一进程里重新起跑:Activity 销毁就结束进程 | The host cannot restart in the same process: end the process when the Activity is destroyed
|
||||
if (isFinishing()) android.os.Process.killProcess(android.os.Process.myPid());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,179 @@
|
||||
// 局内触屏 HUD 叠加层:照 touch_app.c 每帧给的绘制指令表画(与 iOS DmTouchOverlay.m drawRect 一一对应);坐标单位 dp。
|
||||
// 素材取自 APK assets:TouchArt/(原版技能图集等)与 hud_v4_ui/(底部 UI v4,gen_hud_v4.py 生成)。本视图不接触摸,触摸由 Activity 交给 nativeTouchEvent。
|
||||
// In-run touch HUD overlay: draws from the per-frame draw list given by touch_app.c (one-to-one with iOS DmTouchOverlay.m drawRect); units are dp.
|
||||
// Assets come from the APK assets: TouchArt/ (original game skill atlases etc.) and hud_v4_ui/ (bottom UI v4, generated by gen_hud_v4.py). This view takes no touches; the Activity hands them to nativeTouchEvent.
|
||||
package org.d2rmobile.app;
|
||||
|
||||
import android.content.Context;
|
||||
import android.graphics.Bitmap;
|
||||
import android.graphics.BitmapFactory;
|
||||
import android.graphics.BlendMode;
|
||||
import android.graphics.Canvas;
|
||||
import android.graphics.Color;
|
||||
import android.graphics.Paint;
|
||||
import android.graphics.Rect;
|
||||
import android.graphics.RectF;
|
||||
import android.graphics.Typeface;
|
||||
import android.util.Log;
|
||||
import android.view.View;
|
||||
import java.io.InputStream;
|
||||
import java.util.HashMap;
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONObject;
|
||||
|
||||
final class TouchOverlay extends View {
|
||||
// 指令码:与 touch_app.c 的 OP_* 一致 | op codes: same as OP_* in touch_app.c
|
||||
private static final int FILL_ELLIPSE = 1, STROKE_ELLIPSE = 2, LINE = 3, ARC = 4, IMAGE = 5, TEXT = 6, FILL_RECT = 7, V4 = 8, V4TEXT = 9, LABEL = 10, COVER = 11;
|
||||
|
||||
private final float density;
|
||||
private final int part; // 0 = HUD 主体,1 = 摇杆,2 = 小地图任务终点(touch_app.c draw_overlay) | 0 = HUD body, 1 = joystick, 2 = minimap quest goals (touch_app.c draw_overlay)
|
||||
private final Paint fill = new Paint(Paint.ANTI_ALIAS_FLAG), stroke = new Paint(Paint.ANTI_ALIAS_FLAG),
|
||||
img = new Paint(Paint.FILTER_BITMAP_FLAG), text = new Paint(Paint.ANTI_ALIAS_FLAG);
|
||||
private final HashMap<String, Bitmap> bitmaps = new HashMap<>();
|
||||
private final RectF rf = new RectF(); private final Rect src = new Rect();
|
||||
private JSONObject ui; private boolean uiLoaded;
|
||||
|
||||
TouchOverlay(Context c, int part) {
|
||||
super(c);
|
||||
this.part = part;
|
||||
density = getResources().getDisplayMetrics().density;
|
||||
fill.setStyle(Paint.Style.FILL); stroke.setStyle(Paint.Style.STROKE);
|
||||
text.setTypeface(Typeface.create(Typeface.SERIF, Typeface.NORMAL)); text.setTextAlign(Paint.Align.CENTER);
|
||||
setWillNotDraw(false); setClickable(false); setFocusable(false);
|
||||
}
|
||||
|
||||
private Bitmap bitmap(String path) {
|
||||
if (bitmaps.containsKey(path)) return bitmaps.get(path);
|
||||
Bitmap b = null;
|
||||
try (InputStream in = getContext().getAssets().open(path)) {
|
||||
BitmapFactory.Options o = new BitmapFactory.Options(); o.inScaled = false; o.inPremultiplied = true;
|
||||
b = BitmapFactory.decodeStream(in, null, o);
|
||||
} catch (Exception e) { Log.w("d2r", "触屏 HUD:缺素材 " + path); }
|
||||
bitmaps.put(path, b); return b; // 缺的也记下,不每帧重试 | misses are remembered too, no per-frame retry
|
||||
}
|
||||
private JSONObject ui() {
|
||||
if (!uiLoaded) { uiLoaded = true;
|
||||
try (InputStream in = getContext().getAssets().open("hud_v4_ui/ui.json")) {
|
||||
ui = new JSONObject(new String(in.readAllBytes(), "UTF-8"));
|
||||
} catch (Exception e) { Log.e("d2r", "【红】底部 UI v4:APK 内缺 hud_v4_ui/ui.json"); } }
|
||||
return ui;
|
||||
}
|
||||
private static int rgba(float r, float g, float b, float a) {
|
||||
return Color.argb(Math.round(Math.max(0, Math.min(1, a)) * 255), Math.round(r * 255), Math.round(g * 255), Math.round(b * 255));
|
||||
}
|
||||
|
||||
@Override protected void onDraw(Canvas c) {
|
||||
Object[] d = GameActivity.nativeDraw(part); if (d == null) return;
|
||||
float[] o = (float[]) d[0]; String[] s = (String[]) d[1];
|
||||
c.save(); c.scale(density, density);
|
||||
for (int p = 0; p + 2 <= o.length; ) {
|
||||
int code = (int) o[p], n = (int) o[p + 1], a = p + 2; p = a + n;
|
||||
switch (code) {
|
||||
case FILL_ELLIPSE: fill.setColor(rgba(o[a + 4], o[a + 5], o[a + 6], o[a + 7])); rf.set(o[a], o[a + 1], o[a] + o[a + 2], o[a + 1] + o[a + 3]); c.drawOval(rf, fill); break;
|
||||
case STROKE_ELLIPSE: stroke.setStrokeWidth(o[a + 4]); stroke.setColor(rgba(o[a + 5], o[a + 6], o[a + 7], o[a + 8])); rf.set(o[a], o[a + 1], o[a] + o[a + 2], o[a + 1] + o[a + 3]); c.drawOval(rf, stroke); break;
|
||||
case LINE: stroke.setStrokeWidth(o[a + 4]); stroke.setStrokeCap(Paint.Cap.BUTT); stroke.setColor(rgba(o[a + 5], o[a + 6], o[a + 7], o[a + 8])); c.drawLine(o[a], o[a + 1], o[a + 2], o[a + 3], stroke); break;
|
||||
case ARC: { float r = o[a + 2]; stroke.setStrokeWidth(o[a + 5]); stroke.setColor(rgba(o[a + 6], o[a + 7], o[a + 8], o[a + 9]));
|
||||
rf.set(o[a] - r, o[a + 1] - r, o[a] + r, o[a + 1] + r);
|
||||
c.drawArc(rf, (float) Math.toDegrees(o[a + 3]), (float) Math.toDegrees(o[a + 4] - o[a + 3]), false, stroke); break; }
|
||||
case FILL_RECT: fill.setColor(rgba(o[a + 4], o[a + 5], o[a + 6], o[a + 7])); c.drawRect(o[a], o[a + 1], o[a] + o[a + 2], o[a + 1] + o[a + 3], fill); break;
|
||||
case IMAGE: {
|
||||
Bitmap b = o[a] >= 0 ? bitmap(s[(int) o[a]]) : null;
|
||||
if (b == null && o[a + 1] >= 0) b = bitmap(s[(int) o[a + 1]]);
|
||||
if (b == null) break;
|
||||
if (o[a + 4] > 0) src.set((int) o[a + 2], (int) o[a + 3], (int) (o[a + 2] + o[a + 4]), (int) (o[a + 3] + o[a + 5]));
|
||||
else src.set(0, 0, b.getWidth(), b.getHeight());
|
||||
rf.set(o[a + 6], o[a + 7], o[a + 6] + o[a + 8], o[a + 7] + o[a + 9]);
|
||||
img.setAlpha(Math.round(Math.max(0, Math.min(1, o[a + 10])) * 255));
|
||||
img.setBlendMode(o[a + 11] == 1 ? BlendMode.LUMINOSITY : null);
|
||||
c.drawBitmap(b, src, rf, img); img.setBlendMode(null); break; }
|
||||
case TEXT: { // 矩形内水平居中、顶对齐(同 NSString drawInRect)| centered horizontally, top-aligned in the rect (like NSString drawInRect)
|
||||
text.setTextSize(o[a + 5]); text.setColor(rgba(.85f, .79f, .65f, o[a + 6]));
|
||||
c.drawText(s[(int) o[a]], o[a + 1] + o[a + 3] / 2, o[a + 2] - text.getFontMetrics().ascent, text); break; }
|
||||
case LABEL: {
|
||||
Bitmap b = bitmap(s[(int) o[a]]);
|
||||
if (b == null && n > 4 && o[a + 4] >= 0) b = bitmap(s[(int) o[a + 4]]); // 可选第 5 项 = 备用图 | optional 5th value = fallback image
|
||||
if (b == null) break;
|
||||
float h = o[a + 3], w = b.getWidth() * h / b.getHeight();
|
||||
rf.set(o[a + 1] - w / 2, o[a + 2] - h / 2, o[a + 1] + w / 2, o[a + 2] + h / 2); img.setAlpha(255); c.drawBitmap(b, null, rf, img); break; }
|
||||
case V4TEXT: { JSONObject u = ui(); if (u == null) break;
|
||||
float hh = getHeight() / density, ww = getWidth() / density;
|
||||
v4text(c, s[(int) o[a]], u.optJSONObject("badge").optJSONObject("glyphs"), o[a + 1], o[a + 2], (int) o[a + 3], ww, hh / 1320f); break; }
|
||||
case V4: drawV4(c, o, a, s); break;
|
||||
case COVER: fill.setColor(Color.BLACK); c.drawRect(0, 0, o[a], o[a + 1], fill); break;
|
||||
}
|
||||
}
|
||||
c.restore();
|
||||
}
|
||||
|
||||
// ---- 底部 UI v4(同 DmTouchOverlay.m drawV4):几何单位 = 2868×1320 效果图像素,x = (像素 − 1434) × u + 宽/2,y = 像素 × u,u = 高/1320 ----
|
||||
// ---- Bottom UI v4 (same as DmTouchOverlay.m drawV4): geometry unit = pixels of the 2868×1320 mockup, x = (px − 1434) × u + width/2, y = px × u, u = height/1320 ----
|
||||
private void v4text(Canvas c, String str, JSONObject glyphs, float x, float baseline, int align, float W, float u) {
|
||||
if (glyphs == null) return;
|
||||
float adv = 0; for (int i = 0; i < str.length(); i++) { JSONObject g = glyphs.optJSONObject(str.substring(i, i + 1)); if (g != null) adv += (float) g.optDouble("adv"); }
|
||||
float pen = align == 1 ? x - adv : x - adv / 2;
|
||||
img.setAlpha(255);
|
||||
for (int i = 0; i < str.length(); i++) {
|
||||
JSONObject g = glyphs.optJSONObject(str.substring(i, i + 1)); if (g == null) continue;
|
||||
String f = g.optString("file", null); Bitmap b = f != null && !g.isNull("file") ? bitmap("hud_v4_ui/" + f) : null;
|
||||
if (b != null) { float l = (pen + (float) g.optDouble("dx") - 1434) * u + W / 2, t = (baseline + (float) g.optDouble("dy")) * u;
|
||||
rf.set(l, t, l + b.getWidth() * u, t + b.getHeight() * u); c.drawBitmap(b, null, rf, img); }
|
||||
pen += (float) g.optDouble("adv");
|
||||
}
|
||||
}
|
||||
private float X(float px, float u, float W) { return (px - 1434) * u + W / 2; }
|
||||
private void blit(Canvas c, Bitmap b, float l, float t, float w, float h, float alpha) {
|
||||
if (b == null) return; rf.set(l, t, l + w, t + h); img.setAlpha(Math.round(alpha * 255)); c.drawBitmap(b, null, rf, img); img.setAlpha(255);
|
||||
}
|
||||
private void digit(Canvas c, int n, float cx, float cy, float alpha, float u, float W) {
|
||||
Bitmap d = bitmap("hud_v4_ui/diff_" + n + ".png"); if (d == null) return;
|
||||
blit(c, d, X(cx, u, W) - d.getWidth() * u / 2, cy * u - d.getHeight() * u / 2, d.getWidth() * u, d.getHeight() * u, alpha);
|
||||
}
|
||||
private static int rgb(JSONArray a) { return Color.rgb(a.optInt(0), a.optInt(1), a.optInt(2)); }
|
||||
// v:life, mana, lv(字符串表下标), 十字按住, 有点数, 任务未读, 经验‰, 腰带 ×4, 难度档, 选择条展开
|
||||
// v: life, mana, lv (string table indices), cross held, has points, quest unread, exp ‰, belt ×4, difficulty level, selector expanded
|
||||
private void drawV4(Canvas c, float[] o, int a, String[] s) {
|
||||
JSONObject ui = ui(); if (ui == null) return;
|
||||
try {
|
||||
float W = getWidth() / density, u = getHeight() / density / 1320f;
|
||||
boolean held = o[a + 3] != 0, points = o[a + 4] != 0, alert = points || o[a + 5] != 0;
|
||||
float exp = o[a + 6]; int players = (int) o[a + 11]; boolean diffOpen = o[a + 12] != 0;
|
||||
Bitmap full = bitmap("hud_v4_ui/base_full.png"); JSONObject bo = ui.getJSONObject("base");
|
||||
if (full != null) blit(c, full, X((float) bo.getDouble("x"), u, W), (float) bo.getDouble("y") * u, full.getWidth() * u, full.getHeight() * u, 1);
|
||||
JSONArray slots = ui.getJSONArray("slots"), s6 = slots.getJSONArray(5);
|
||||
Bitmap btn = bitmap(alert ? (held ? "hud_v4_ui/lvl_1.png" : "hud_v4_ui/lvl_0.png") : (held ? "hud_v4_ui/lvl_d1.png" : "hud_v4_ui/lvl_d0.png"));
|
||||
float bl = X((float) s6.getDouble(0), u, W), bt = (float) s6.getDouble(1) * u, bw = (float) (s6.getDouble(2) - s6.getDouble(0)) * u, bh = (float) (s6.getDouble(3) - s6.getDouble(1)) * u;
|
||||
if (btn != null) {
|
||||
if (points) { img.setShadowLayer(10 * u, 0, 0, Color.argb(242, 255, 209, 89)); blit(c, btn, bl, bt, bw, bh, 1); img.clearShadowLayer(); }
|
||||
blit(c, btn, bl, bt, bw, bh, 1);
|
||||
}
|
||||
JSONObject e = ui.getJSONObject("exp");
|
||||
float x0 = (float) e.getDouble("x0"), x1 = (float) e.getDouble("x1"), y = (float) e.getDouble("y"), eh = (float) e.getDouble("h");
|
||||
fill.setColor(rgb(e.getJSONArray("track"))); c.drawRect(X(x0, u, W), y * u, X(x0, u, W) + (x1 - x0) * u, (y + eh) * u, fill);
|
||||
stroke.setColor(rgb(e.getJSONArray("edge"))); stroke.setStrokeWidth(u);
|
||||
c.drawRect(X(x0, u, W) + u / 2, y * u + u / 2, X(x0, u, W) + u / 2 + (x1 - x0 - 1) * u, y * u + u / 2 + (eh - 1) * u, stroke);
|
||||
fill.setColor(rgb(e.getJSONArray("fill"))); c.drawRect(X(x0 + 1, u, W), (y + 1) * u, X(x0 + 1, u, W) + (x1 - x0 - 2) * u * exp / 1000f, (y + eh - 1) * u, fill);
|
||||
int ticks = e.getInt("ticks"); fill.setColor(rgb(e.getJSONArray("track")));
|
||||
for (int t = 1; t < ticks; t++) { float tx = X(x0 + Math.round((x1 - x0) * t / (double) ticks), u, W); c.drawRect(tx, (y + 1) * u, tx + u, (y + eh - 1) * u, fill); }
|
||||
JSONObject vt = ui.getJSONObject("vital");
|
||||
v4text(c, s[(int) o[a]], vt.getJSONObject("glyphs"), (float) vt.getDouble("left_x"), (float) vt.getDouble("baseline"), 0, W, u);
|
||||
v4text(c, s[(int) o[a + 1]], vt.getJSONObject("glyphs"), (float) vt.getDouble("right_x"), (float) vt.getDouble("baseline"), 0, W, u);
|
||||
JSONObject lv = ui.getJSONObject("lv");
|
||||
v4text(c, s[(int) o[a + 2]], lv.getJSONObject("glyphs"), (float) lv.getDouble("x"), (float) lv.getDouble("baseline"), 0, W, u);
|
||||
JSONArray in = ui.getJSONArray("inner"); JSONObject bd = ui.getJSONObject("badge");
|
||||
for (int i = 1; i <= 4; i++) { int cnt = (int) o[a + 6 + i]; if (cnt == 0) continue;
|
||||
JSONArray si = slots.getJSONArray(i);
|
||||
v4text(c, Integer.toString(cnt), bd.getJSONObject("glyphs"), (float) (si.getDouble(0) + in.getDouble(2) + bd.getDouble("dx")), (float) (si.getDouble(1) + in.getDouble(3) + bd.getDouble("dy")), 1, W, u); }
|
||||
float icx = (float) (in.getDouble(0) + in.getDouble(2) + 1) / 2, icy = (float) (in.getDouble(1) + in.getDouble(3) + 1) / 2;
|
||||
JSONArray s0 = slots.getJSONArray(0);
|
||||
if (players >= 1 && players <= 8) digit(c, players, (float) s0.getDouble(0) + icx, (float) s0.getDouble(1) + icy, 1, u, W);
|
||||
if (diffOpen) {
|
||||
JSONObject pk = ui.getJSONObject("pick"); Bitmap tile = bitmap("hud_v4_ui/slot.png");
|
||||
for (int i = 0; i < 8; i++) {
|
||||
float cx = (float) (pk.getDouble("center_x") + (i - 3.5) * pk.getDouble("pitch")), cy = (float) pk.getDouble("y"), al = i + 1 == players ? 1 : .82f;
|
||||
if (tile != null) blit(c, tile, X(cx - tile.getWidth() / 2f, u, W), (cy - tile.getHeight() / 2f) * u, tile.getWidth() * u, tile.getHeight() * u, al);
|
||||
digit(c, i + 1, cx, cy, al, u, W);
|
||||
}
|
||||
}
|
||||
} catch (Exception ex) { Log.e("d2r", "底部 UI v4 绘制失败:" + ex); }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<!-- 自适应图标:图在 108dp 画布中缩到 72dp,各家圆形 / 圆角遮罩都不裁到图(iOS 同一张 AppIcon1024.png)| Adaptive icon: the art is scaled to 72dp inside the 108dp canvas so no launcher mask crops it (same AppIcon1024.png as iOS) -->
|
||||
<adaptive-icon xmlns:android="http://schemas.android.com/apk/res/android">
|
||||
<background android:drawable="@android:color/black" />
|
||||
<foreground android:drawable="@mipmap/ic_launcher_fg" />
|
||||
</adaptive-icon>
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 69 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 120 KiB |
@@ -0,0 +1,702 @@
|
||||
// Android 局内触屏操作层:front_ios.m 里 DmMetalView(手势 → 手势层 dm_touch_controls → 宿主)与 DmTouchOverlay(画操作层)的移植。
|
||||
// 坐标一律用 dp(≈ UIKit 点):Java 传视图尺寸、安全边距与触点时已除以 density。
|
||||
// 线程:触点、每帧 tick、绘制都在 Java 界面线程;界面消息(touch_ui_message)在客体线程,只写原子量,界面线程 tick 时取用
|
||||
// (iOS 这里用 dispatch_async 切主线程)。绘制不直接碰 Canvas:C 产出一张绘制指令表(浮点数组 + 字符串表),Java 照表画。
|
||||
// 没移植:秘境任务条、货架拖货跟手图、测试脚本触点、操作层截图。
|
||||
// Android in-run touch layer: port of DmMetalView (gestures → gesture layer dm_touch_controls → host) and DmTouchOverlay (draws the layer) from front_ios.m.
|
||||
// All coordinates are dp (≈ UIKit points): Java divides the view size, safe insets and touch points by density before passing them.
|
||||
// Threads: touches, the per-frame tick and drawing all run on the Java UI thread; UI messages (touch_ui_message) arrive on the guest thread and only write
|
||||
// atomics that the UI-thread tick picks up (iOS uses dispatch_async to the main thread here). Drawing does not touch a Canvas: C emits a draw list
|
||||
// (float array + string table) that Java executes.
|
||||
// Not ported: Rift objective bar, the shelf-drag ghost image, scripted test touches, overlay capture.
|
||||
#include "../../dm.h"
|
||||
#include "../../front.h"
|
||||
#include "../../hle.h"
|
||||
#include "../../dm_input.h"
|
||||
#include "../../dm_cursor.h"
|
||||
#include "../../dm_hud.h"
|
||||
#include "../../dm_world.h"
|
||||
#include "../../dm_uiscreen.h"
|
||||
#include "../../dm_actions.h"
|
||||
#include "../../dm_i18n.h"
|
||||
#include "../../dm_custom.h"
|
||||
#include "../../../touch/dm_touch_controls.h"
|
||||
#include "../../../touch/dm_touch_game_state.h"
|
||||
#include "../../../touch/dm_touch_native_art.h"
|
||||
#include <jni.h>
|
||||
#include <math.h>
|
||||
#include <stdatomic.h>
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
|
||||
void dm_tp_request(void);
|
||||
int dm_drop_haptic_take(void);
|
||||
int dm_padmode_busy(void);
|
||||
void dm_padmode_request(int pad);
|
||||
uint32_t dm_padmode_rebuilds(void);
|
||||
int dm_swapskill_slot(int, int *);
|
||||
int dm_freetp_zr_free(int zl);
|
||||
void dm_ui_key_pulse_alone(uint64_t, int);
|
||||
void uimsg_subscribe(void (*)(const char *, const char *, const char *));
|
||||
|
||||
static double now_s(void) { struct timespec t; clock_gettime(CLOCK_MONOTONIC, &t); return t.tv_sec + t.tv_nsec * 1e-9; }
|
||||
#define MIN(a, b) ((a) < (b) ? (a) : (b))
|
||||
#define MAX(a, b) ((a) > (b) ? (a) : (b))
|
||||
|
||||
// ---- 客体线程 → 界面线程 | guest thread → UI thread ----
|
||||
static _Atomic int g_touch_frontend = 1, g_touch_navigation = 1, g_hud_blocked, g_cover;
|
||||
static _Atomic int g_nav_pub;
|
||||
int dm_front_navigation(void) { return atomic_load(&g_nav_pub); } // 面板 / 读取画面 / 过场开着:手柄 ZR 只在游戏态接管 | a panel / loading screen / cutscene is open: the pad's ZR is taken over only in game state
|
||||
int dm_touch_frontend(void) { return atomic_load(&g_touch_frontend); }
|
||||
static void restart_cover_hook(int on) { atomic_store(&g_cover, on); } // 0 撤 / 1 黑屏 / 2 透明但吞触点 | 0 remove / 1 black / 2 transparent but swallows touches
|
||||
|
||||
// ---- 触摸记录:Android 指针 id 会复用,iOS 的 UITouch 身份换成记录下标 | touch records: Android pointer ids get reused, records stand in for UITouch identity ----
|
||||
enum { NREC = 32 };
|
||||
static struct { int used, pid, done; float x, y; } R[NREC];
|
||||
static int pid_rec(int pid) { for (int i = 0; i < NREC; i++) if (R[i].used && !R[i].done && R[i].pid == pid) return i; return -1; }
|
||||
|
||||
// ---- DmMetalView 状态 | DmMetalView state ----
|
||||
static dm_tc_state C;
|
||||
static float W, H; // 视图尺寸(dp) | view size (dp)
|
||||
static int g_slot[16]; // 手指槽 → 记录;-1 = 空 | finger slot → record; -1 = empty
|
||||
static uint32_t g_swallow; // 手柄模式 / 切换中 / 遮罩下落下的手指(记录位图) | fingers that landed in pad mode / mid-switch / under the cover (record bitmap)
|
||||
static int g_pad, g_fe = 1, g_nav = -1, g_blocked_applied, g_dialog_only, g_hud_blocked_v;
|
||||
static int ui_finger = -1, map_t = -1, mapback_t = -1, bind_t = -1, pend_end = -1;
|
||||
static float fe_ox, fe_oy, map_x0, map_y0, bind_x0, bind_y0; static int fe_drag, ui_drag_on;
|
||||
static double map_t0, pend_time, pend_wall; static int pend_fire, final_asked;
|
||||
static uint64_t bind_key; static uint32_t sel_id; static double stat_next, tip_until, add_at;
|
||||
static unsigned map_world_seq; static int map_world_asked; static float map_wx, map_wy;
|
||||
static int pulse_ticking; static uint64_t pulse_epoch, pulse_s0; static double pulse_t0, pulse_seen, pulse_free;
|
||||
static unsigned item_seq, rel_seq; static int rel_asked, item_asked, item_kind;
|
||||
static double labels_at; static int labels_done;
|
||||
static dm_hud_info g_hud; static dm_tg_snapshot g_native;
|
||||
static int g_dirty = 1, g_haptic;
|
||||
|
||||
static int slot_of_rec(int t) { for (int i = 0; i < 16; i++) if (g_slot[i] == t) return i; return -1; }
|
||||
static int rec_referenced(int t) { return slot_of_rec(t) >= 0 || t == ui_finger || t == map_t || t == mapback_t || t == bind_t || t == pend_end || (g_swallow >> t & 1); }
|
||||
static void gc_recs(void) { for (int i = 0; i < NREC; i++) if (R[i].used && R[i].done && !rec_referenced(i)) R[i].used = 0; }
|
||||
|
||||
static void push(void) {
|
||||
dm_touch ts[16]; int n = 0;
|
||||
if (W > 0 && H > 0) for (int i = 0; i < 16; i++) if (g_slot[i] >= 0) {
|
||||
int t = g_slot[i]; ts[n++] = (dm_touch){(uint32_t)i, fminf(fmaxf(R[t].x / W, 0), .99999f), fminf(fmaxf(R[t].y / H, 0), .99999f)};
|
||||
}
|
||||
dm_touch_set(ts, n);
|
||||
if (C.pointer_valid) dm_cursor_set_screen(C.pointer_x, C.pointer_y, W, H); else dm_cursor_clear();
|
||||
dm_input_set(C.buttons, C.lx, C.ly, C.rx, C.ry);
|
||||
if (C.belt_request) { int br = C.belt_request; C.belt_request = 0; if (br == 1 || br == 2) dm_belt_expand(br == 1); else dm_belt_use(br & 0xf, br >> 4 & 0xf); }
|
||||
if (C.diff_request) { dm_hud_request_players(C.diff_request); C.diff_request = 0; }
|
||||
g_dirty = 1;
|
||||
}
|
||||
static void cancel_controls(void) {
|
||||
pend_end = -1; final_asked = 0;
|
||||
for (int i = 0; i < 16; i++) g_slot[i] = -1;
|
||||
dm_tc_cancel_all(&C); push();
|
||||
if (ui_drag_on) { ui_drag_on = 0; dm_ui_drag_end(); }
|
||||
map_t = mapback_t = bind_t = -1;
|
||||
}
|
||||
static void set_pad_mode(int pad) {
|
||||
g_pad = pad;
|
||||
if (pad) { tip_until = -2; dm_tc_cancel_all(&C); push(); }
|
||||
g_dirty = 1;
|
||||
}
|
||||
static void set_frontend(int fe) {
|
||||
g_fe = fe;
|
||||
if (fe) { pend_end = -1; final_asked = 0; for (int i = 0; i < 16; i++) g_slot[i] = -1; dm_tc_cancel_all(&C); push(); }
|
||||
g_dirty = 1;
|
||||
}
|
||||
static void hud_blocked(int n) { g_hud_blocked_v = n > 0; g_dialog_only = n < 0; dm_tc_set_joy_block(&C, n < 0); g_dirty = 1; }
|
||||
static void navigation_mode(int n) {
|
||||
if (g_nav == n) return;
|
||||
dm_tc_set_navigation_mode(&C, n); g_nav = n; atomic_store(&g_nav_pub, n);
|
||||
static int pad = -1; if (pad < 0) { const char *e = getenv("DM_TOUCH_NAV_PAD"); pad = e && e[0] == '1'; }
|
||||
uint32_t mask = n ? (dm_tc_nav_mask(pad && n != 2) | (n == 3 ? 1u << DM_TC_MENU : 0)) : (((1u << DM_TC_COUNT) - 1) & ~(1u << DM_TC_MAP));
|
||||
dm_tc_enable_targets(&C, mask); push();
|
||||
// 进局后第一次回游戏态:替玩家按一次 L 打开物品名标签(同 iOS itemLabelsOn,1.5 秒后) | first return to game state: press L once for the player to turn item labels on (as iOS itemLabelsOn, after 1.5 s)
|
||||
if (!n && !labels_done && !labels_at) { const char *e = getenv("DM_TOUCH_DEFAULTS"); if (!(e && !strcmp(e, "0"))) labels_at = now_s() + 1.5; }
|
||||
if (n && dm_ui_map_is_full()) { dm_ui_map_full(0); LOG("前端:开了面板,全屏地图先收回小地图"); }
|
||||
}
|
||||
// 客体线程发布的状态落到界面线程 | apply the state published by the guest thread on the UI thread
|
||||
static void apply_pending(void) {
|
||||
int fe = atomic_load(&g_touch_frontend); if (fe != g_fe) set_frontend(fe);
|
||||
int hb = atomic_load(&g_hud_blocked); if (hb != g_blocked_applied) { g_blocked_applied = hb; hud_blocked(hb); }
|
||||
navigation_mode(atomic_load(&g_touch_navigation));
|
||||
}
|
||||
|
||||
// ---- 界面消息(客体线程):同 iOS hud_stack_message / touch_ui_message | UI messages (guest thread): as iOS hud_stack_message / touch_ui_message ----
|
||||
enum { HUD_STACK_MAX = 64 };
|
||||
static struct { char name[64]; int open; } g_hud_stack[HUD_STACK_MAX];
|
||||
static void hud_stack_message(const char *act, const char *arg) {
|
||||
if (!strcmp(act, "UnloadPanel") && !strcmp(arg, "LoadScreenPanel")) {
|
||||
for (int i = 0; i < HUD_STACK_MAX; i++)
|
||||
if (!(g_hud_stack[i].open > 0 && strcmp(g_hud_stack[i].name, "LoadScreenPanel") && dm_ui_panel_shown(g_hud_stack[i].name))) memset(&g_hud_stack[i], 0, sizeof g_hud_stack[i]);
|
||||
} else if (!strcmp(act, "UnloadAll")) memset(g_hud_stack, 0, sizeof g_hud_stack);
|
||||
else {
|
||||
int tog = !strcmp(act, "TogglePanel");
|
||||
int open = !strcmp(act, "OpenPanel") || !strcmp(act, "OpenExclusivePanel"), close = !strcmp(act, "ClosePanel") || !strcmp(act, "UnloadPanel");
|
||||
if (!open && !close && !tog) return;
|
||||
if (dm_ui_panel_resident(arg)) return;
|
||||
int slot = -1; for (int i = 0; i < HUD_STACK_MAX; i++) if (!strcmp(g_hud_stack[i].name, arg)) { slot = i; break; }
|
||||
if (slot < 0) for (int i = 0; i < HUD_STACK_MAX; i++) if (!g_hud_stack[i].name[0]) { slot = i; snprintf(g_hud_stack[i].name, sizeof g_hud_stack[i].name, "%s", arg); break; }
|
||||
if (slot < 0) return;
|
||||
if (tog) { open = g_hud_stack[slot].open <= 0; close = !open; }
|
||||
if (close && g_hud_stack[slot].open <= 0) return;
|
||||
if (open && g_hud_stack[slot].open > 0) return;
|
||||
g_hud_stack[slot].open = open ? 1 : 0;
|
||||
fprintf(stderr, "[hud] 面板栈 %s %s → %d\n", act, arg, g_hud_stack[slot].open);
|
||||
}
|
||||
int n = 0, dlg = 0;
|
||||
for (int i = 0; i < HUD_STACK_MAX; i++) {
|
||||
if (!strcmp(g_hud_stack[i].name, "NpcDialogPanel") || !strcmp(g_hud_stack[i].name, "ScrollSpeechPanel")) dlg |= g_hud_stack[i].open > 0;
|
||||
else n += g_hud_stack[i].open;
|
||||
}
|
||||
if (!n && dlg) n = -1;
|
||||
atomic_store(&g_hud_blocked, n);
|
||||
}
|
||||
static void touch_ui_message(const char *cat, const char *act, const char *arg) {
|
||||
if (!strcmp(cat, "PanelManager")) hud_stack_message(act, arg);
|
||||
static dm_tc_scene scene; static uint64_t scene_epoch = 1, world_epoch = 1; static int prev_mode = 1;
|
||||
dm_tc_scene before = scene;
|
||||
dm_tc_set_panel_state(dm_ui_panel_state());
|
||||
int mode = dm_tc_navigation(&scene, cat, act, arg);
|
||||
if (scene.game && atomic_load(&g_hud_blocked) > 0) mode = 1;
|
||||
if (before.game != scene.game) ++world_epoch;
|
||||
{ static uint32_t rb0; uint32_t rb = dm_padmode_rebuilds(); if (rb != rb0) { rb0 = rb; ++world_epoch; } }
|
||||
if (mode != prev_mode) ++scene_epoch;
|
||||
prev_mode = mode;
|
||||
dm_touch_game_set_scene(!mode, scene_epoch, world_epoch);
|
||||
atomic_store(&g_touch_frontend, !scene.game);
|
||||
int nmode = mode ? (dm_tc_scene_dead() ? 2 : dm_tc_scene_dialog_only(&scene) && atomic_load(&g_hud_blocked) <= 0 ? 3 : 1) : 0;
|
||||
atomic_store(&g_touch_navigation, nmode);
|
||||
dm_uiscreen_set_panel(nmode == 1);
|
||||
}
|
||||
// front_run 在客体线程开头调 | called by front_run at the start of the guest thread
|
||||
void touch_app_guest_start(void) {
|
||||
extern void (*g_dm_restart_cover_hook)(int); g_dm_restart_cover_hook = restart_cover_hook;
|
||||
uimsg_subscribe(touch_ui_message);
|
||||
dm_touch_game_live_check(1);
|
||||
if (!dm_touch_game_attach()) LOG("【红】技能绑定读取没挂上(产物没有观测门 OBS_BINDING_GETTER,或 main 模块不符):技能键图标 / 回城键绑定不工作");
|
||||
}
|
||||
|
||||
// ---- 手柄模式(iOS dm_gamepad.m 的 apply / set_mode,Android 只有一路合并后的手柄) | pad mode (iOS dm_gamepad.m apply / set_mode; Android has one merged pad) ----
|
||||
static int g_need_center;
|
||||
static void pad_set_mode(int pad) {
|
||||
if (g_pad == pad) return;
|
||||
dm_padmode_request(pad); LOG("手柄:%s", pad ? "手柄有输入 → 手柄模式" : "碰了屏幕 → 触屏模式");
|
||||
set_pad_mode(pad);
|
||||
}
|
||||
static int stick_on(int was, int32_t x, int32_t y) { double m = hypot(x, y) / DM_STICK_MAX; return was ? m >= 0.15 : m >= 0.25; }
|
||||
static uint64_t pad_b; static int32_t pad_ax[4];
|
||||
static void pad_apply(void) {
|
||||
uint64_t b = pad_b; int32_t ax[4] = {pad_ax[0], pad_ax[1], pad_ax[2], pad_ax[3]};
|
||||
static int on[2]; static uint64_t prev_b;
|
||||
on[0] = stick_on(on[0], ax[0], ax[1]); on[1] = stick_on(on[1], ax[2], ax[3]);
|
||||
if (!on[0] && !on[1]) g_need_center = 0;
|
||||
if ((b & ~prev_b) || ((on[0] || on[1]) && !g_need_center)) pad_set_mode(1);
|
||||
prev_b = b;
|
||||
if (!g_pad || !on[0]) ax[0] = ax[1] = 0;
|
||||
if (!g_pad || !on[1]) ax[2] = ax[3] = 0;
|
||||
{ static uint64_t prev; static int mine;
|
||||
if ((b & DM_BTN_ZR) && !(prev & DM_BTN_ZR)) { mine = !dm_front_navigation() && dm_freetp_zr_free((b & DM_BTN_ZL) != 0); if (mine) { dm_tp_request(); LOG("手柄:ZR(这一格空着)→ 免费回城"); } }
|
||||
prev = b; if (mine) b &= ~(uint64_t)DM_BTN_ZR; if (!(b & DM_BTN_ZR) && !(prev & DM_BTN_ZR)) mine = 0; }
|
||||
dm_pad_set(b, ax[0], ax[1], ax[2], ax[3]);
|
||||
}
|
||||
void touch_app_pad(uint64_t b, int32_t lx, int32_t ly, int32_t rx, int32_t ry) { pad_b = b; pad_ax[0] = lx; pad_ax[1] = ly; pad_ax[2] = rx; pad_ax[3] = ry; pad_apply(); }
|
||||
static void gamepad_touched(void) { if (g_pad) { g_need_center = 1; pad_set_mode(0); pad_apply(); } }
|
||||
|
||||
// ---- 手势 | gestures ----
|
||||
static uint64_t skill_key_at(float x, float y, int *empty) {
|
||||
int i = dm_tc_hit(&C.layout, x, y);
|
||||
if (i >= DM_TC_SKILL1 && i <= DM_TC_SKILL4 && (C.enabled_targets & (1u << i))) {
|
||||
const dm_tc_target *t = &C.layout.target[i];
|
||||
dm_ta_glyph g = dm_ta_current_set(&g_native, t->button, now_ns(), C.skill_set); *empty = g.status == DM_TA_EMPTY;
|
||||
return t->button | (C.skill_set ? DM_BTN_ZL : 0);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
static int in_minimap(float x, float y) {
|
||||
float r[4]; if (g_nav || W <= 0 || !dm_ui_minimap_rect(r)) return 0;
|
||||
if (dm_tc_hit(&C.layout, x, y) >= 0) return 0;
|
||||
float x0 = r[0] * W, y0 = r[1] * H, w = (r[2] - r[0]) * W, h = (r[3] - r[1]) * H, band = h / 4; if (band < 30) band = MIN(30, h * 0.4f);
|
||||
return x >= x0 && x < x0 + w && y >= y0 && y < y0 + band;
|
||||
}
|
||||
static void refresh_portal_binding(void) {
|
||||
{ dm_hud_info h; dm_hud_get(&h); h.x = h.y = 0; if (memcmp(&h, &g_hud, sizeof h)) { g_hud = h; g_dirty = 1; } }
|
||||
dm_tg_snapshot s = {0}; dm_touch_game_snapshot(&s);
|
||||
if (memcmp(&s.raw_skill_id, &g_native.raw_skill_id, sizeof s.raw_skill_id) || memcmp(&s.mapping, &g_native.mapping, sizeof s.mapping) || s.valid != g_native.valid) g_dirty = 1;
|
||||
g_native = s;
|
||||
if (g_nav || !s.supported) return;
|
||||
uint64_t mask = 0; // 回城键不跟原生绑定(同 iOS) | the portal key does not follow the native binding (as iOS)
|
||||
if (C.layout.target[DM_TC_PORTAL].button != mask || C.portal_binding_identity != s.binding_identity || C.portal_scene_epoch != s.scene_epoch) {
|
||||
dm_tc_set_portal_binding(&C, mask, s.binding_identity, s.scene_epoch); push();
|
||||
}
|
||||
}
|
||||
static void bind_tap_sync(void) {
|
||||
float nx = 0, ny = 0;
|
||||
{ static int was; int st = dm_ui_bind_active(); if (st == 2 && was != 2 && W > 0) { C.pointer_valid = 1; C.pointer_x = 0.155f * W; C.pointer_y = 0.5f * H; } was = st; }
|
||||
int hk = dm_ui_bind_skill_hit(&nx, &ny); C.tap_button = 0; C.tap_move = 0;
|
||||
if (!hk) sel_id = 0;
|
||||
if (hk) { C.tap_x = nx * W; C.tap_y = ny * H;
|
||||
if (hk == 1 && dm_ui_bind_active() == 2) C.tap_button = DM_BTN_Y;
|
||||
else if (!dm_ui_bind_active()) C.tap_move = 1; }
|
||||
}
|
||||
static void ui_touch(const int *ts, int n, int ph) {
|
||||
if (W <= 0 || H <= 0) return;
|
||||
for (int k = 0; k < n; k++) {
|
||||
int t = ts[k];
|
||||
if (ph == 0 && ui_finger < 0) { ui_finger = t; fe_ox = R[t].x; fe_oy = R[t].y; fe_drag = 0; }
|
||||
if (t != ui_finger) continue;
|
||||
float px = R[t].x, py = R[t].y, dy = fabsf(py - fe_oy);
|
||||
if (ph == 1 && !fe_drag && !ui_drag_on && dy > 10 && dy > fabsf(px - fe_ox) && dm_ui_fe_drag_ok()) {
|
||||
fe_drag = 1; ui_drag_on = 1; dm_ui_touch(3, 0, 0); dm_ui_drag_begin(1, fe_ox / W, fe_oy / H, 0);
|
||||
}
|
||||
if (fe_drag) { if (ph == 1) dm_ui_drag_move(px / W, py / H); else if (ph == 2) { fe_drag = 0; ui_drag_on = 0; dm_ui_drag_end(); ui_finger = -1; } continue; }
|
||||
dm_ui_touch(ph, px / W, py / H);
|
||||
if (ph == 2) ui_finger = -1;
|
||||
}
|
||||
}
|
||||
static void pulse_tick(void);
|
||||
static void arm_pulse_end(void) {
|
||||
if (pulse_ticking || (!C.pulse_button && !dm_tc_pulse_pending(&C))) return;
|
||||
pulse_ticking = 1; pulse_epoch = 0; pulse_tick();
|
||||
}
|
||||
static void touches_began(const int *ts, int n) {
|
||||
{ int was = g_pad; gamepad_touched();
|
||||
if (was || dm_padmode_busy() || atomic_load(&g_cover)) { for (int k = 0; k < n; k++) g_swallow |= 1u << ts[k]; if (was) LOG("前端:手柄模式下碰屏 → 只切回触屏模式,这一下不执行"); return; } }
|
||||
if (g_fe) { ui_touch(ts, n, 0); return; }
|
||||
refresh_portal_binding();
|
||||
void finish_touches(const int *, int, int);
|
||||
if (pend_end >= 0) { int pt = pend_end; pend_fire = 1; finish_touches(&pt, 1, 0); pend_fire = 0; pend_end = -1; final_asked = 0; }
|
||||
double now = now_s();
|
||||
for (int k = 0; k < n; k++) {
|
||||
int t = ts[k]; float px = R[t].x, py = R[t].y;
|
||||
{ int ex = 0; C.belt_rows = dm_belt_rows(&ex); C.belt_open = ex;
|
||||
if (dm_tc_belt_outside(&C, px, py)) { dm_belt_expand(0); LOG("前端:腰带展开着点别处 (%.0f,%.0f) → 只收腰带", px, py); continue; } }
|
||||
if (!g_nav && bind_t < 0 && !dm_ui_bind_active()) { int em = 0; uint64_t bk = skill_key_at(px, py, &em); if (bk && em) { bind_t = t; bind_key = bk; bind_x0 = px; bind_y0 = py; map_t0 = now; continue; } }
|
||||
if (map_t < 0 && in_minimap(px, py)) { map_t = t; map_x0 = px; map_y0 = py; map_t0 = now; continue; }
|
||||
for (int i = 0; i < 16; i++) if (g_slot[i] < 0) {
|
||||
g_slot[i] = t; { int ex = 0; C.belt_rows = dm_belt_rows(&ex); C.belt_open = ex; } dm_tc_begin_at(&C, i, px, py, now);
|
||||
if (!g_nav && C.finger[i].target == DM_TC_PORTAL) { dm_tp_request(); LOG("前端:点回城钮 → 免费回城"); }
|
||||
if (!g_nav && mapback_t < 0 && C.finger[i].target == DM_TC_NONE && dm_ui_map_is_full()) { mapback_t = t; map_x0 = px; map_y0 = py; map_t0 = now; }
|
||||
if (C.navigation_mode && C.pointer_slot == i && W > 0) item_seq = dm_ui_item_query(px / W, py / H);
|
||||
item_asked = 1; item_kind = -1; // iOS 原样:这两句不在 if 里 | as on iOS: these two are outside the if
|
||||
break;
|
||||
}
|
||||
}
|
||||
push();
|
||||
}
|
||||
static void touches_moved(const int *ts0, int n0) {
|
||||
int ts[NREC], n = 0; for (int k = 0; k < n0; k++) if (!(g_swallow >> ts0[k] & 1)) ts[n++] = ts0[k];
|
||||
if (!n) return;
|
||||
if (g_fe) { ui_touch(ts, n, 1); return; }
|
||||
if (ui_finger >= 0) { int m = 0, hit = 0; for (int k = 0; k < n; k++) { if (ts[k] == ui_finger) hit = 1; else ts[m++] = ts[k]; }
|
||||
if (hit) { int u = ui_finger; ui_touch(&u, 1, 1); n = m; if (!n) return; } }
|
||||
double now = now_s(); float sc = dm_tc_scale_for_size(W, H);
|
||||
for (int k = 0; k < n; k++) {
|
||||
int t = ts[k], i = slot_of_rec(t); if (i < 0) continue;
|
||||
float px = R[t].x, py = R[t].y;
|
||||
dm_tc_move_at(&C, i, px, py, now);
|
||||
if (C.navigation_mode && C.pointer_slot == i && C.finger[i].ui_drag && W > 0) { dm_tc_finger *f = &C.finger[i];
|
||||
if (!ui_drag_on) { ui_drag_on = 1; dm_ui_drag_lift(DM_TC_POINTER_LIFT_PT * sc / H); dm_ui_drag_begin(f->ui_drag, f->aim_origin_x / W, f->aim_origin_y / H, 44.0f / H); }
|
||||
dm_ui_drag_move(px / W, py / H); }
|
||||
if (C.navigation_mode && C.pointer_slot == i && C.finger[i].pointer_dragged && W > 0) { rel_seq = dm_ui_drop_query(px / W, (py - DM_TC_POINTER_LIFT_PT * sc) / H, py / H); rel_asked = 1; }
|
||||
}
|
||||
push();
|
||||
arm_pulse_end();
|
||||
}
|
||||
// cancelled:0 = 抬起,1 = 系统取消 | cancelled: 0 = lifted, 1 = cancelled by the system
|
||||
void finish_touches(const int *ts0, int n0, int cancelled) {
|
||||
int ts[NREC], n = 0;
|
||||
for (int k = 0; k < n0; k++) { if (g_swallow >> ts0[k] & 1) g_swallow &= ~(1u << ts0[k]); else ts[n++] = ts0[k]; }
|
||||
if (ui_finger >= 0) { int m = 0, hit = 0; for (int k = 0; k < n; k++) { if (ts[k] == ui_finger) hit = 1; else ts[m++] = ts[k]; }
|
||||
if (hit) { int u = ui_finger; ui_touch(&u, 1, 2); n = m; } }
|
||||
if (!n) return;
|
||||
double now = now_s(); float sc = dm_tc_scale_for_size(W, H);
|
||||
for (int k = 0; k < n; k++) {
|
||||
int t = ts[k]; float px = R[t].x, py = R[t].y;
|
||||
if (t == map_t) { map_t = -1;
|
||||
if (!cancelled && !g_nav && hypotf(px - map_x0, py - map_y0) <= 14 && now - map_t0 < 0.6) {
|
||||
if (dm_ui_map_is_full()) { dm_ui_map_full(0); LOG("前端:点小地图那一块 (%.0f,%.0f) → 回小地图", px, py); }
|
||||
else if (W > 0) { map_world_seq = dm_world_tap(px / W, py / H); map_world_asked = 1; map_wx = px; map_wy = py; } }
|
||||
continue; }
|
||||
if (t == bind_t) { bind_t = -1;
|
||||
if (!cancelled && !g_nav && hypotf(px - bind_x0, py - bind_y0) <= 14) { dm_ui_bind_begin(bind_key); LOG("前端:点空技能位 (%.0f,%.0f) → 给键 %#llx 选技能", px, py, (unsigned long long)bind_key); }
|
||||
continue; }
|
||||
if (t == mapback_t) { mapback_t = -1;
|
||||
if (!cancelled && !g_nav && dm_ui_map_is_full() && hypotf(px - map_x0, py - map_y0) <= 14 && now - map_t0 < 0.6) { dm_ui_map_full(0); LOG("前端:全屏地图开着,点空处 (%.0f,%.0f) → 回小地图", px, py); } }
|
||||
if (!pend_fire && pend_end < 0 && !cancelled && C.navigation_mode && C.pointer_slot >= 0 && g_slot[C.pointer_slot] == t && W > 0) {
|
||||
dm_tc_finger *pf = &C.finger[C.pointer_slot]; int wait = 0;
|
||||
if (item_asked && dm_ui_item_result(item_seq) < 0) wait = 1;
|
||||
else if (pf->pointer_dragged) {
|
||||
dm_tc_move_at(&C, C.pointer_slot, px, py, now);
|
||||
rel_seq = dm_ui_drop_query(px / W, (py - DM_TC_POINTER_LIFT_PT * sc) / H, py / H); rel_asked = 1; final_asked = 1; wait = 1; }
|
||||
if (wait) { pend_end = t; pend_time = now; pend_wall = now; continue; }
|
||||
}
|
||||
int i = slot_of_rec(t); if (i < 0) continue;
|
||||
if (cancelled) { dm_tc_finger *cf = &C.finger[i]; int drag = C.navigation_mode && C.pointer_slot == i && (cf->pointer_dragged || cf->shop_drag);
|
||||
if (ui_drag_on && C.pointer_slot == i) { ui_drag_on = 0; dm_ui_drag_end(); }
|
||||
if (drag) { int shop = cf->shop_drag; if (!shop) dm_tc_pointer_release(&C, 0); dm_tc_end_at(&C, i, now); LOG("前端:拖动中被系统取消触摸 → %s", shop ? "不买不取" : "放回原处"); }
|
||||
else dm_tc_cancel_finger(&C, i);
|
||||
if (C.pointer_slot < 0) { rel_asked = 0; item_asked = 0; }
|
||||
g_slot[i] = -1; continue; }
|
||||
if (C.navigation_mode && C.pointer_slot == i && C.finger[i].shop_drag) {
|
||||
int rk = rel_asked ? dm_ui_item_result(rel_seq) : 0; if (rk < 0) rk = dm_ui_drop_last();
|
||||
if (rk == 5 && !dm_ui_drop_bag()) LOG("前端:%s拖出后松在背包格以外的物品区 → 不%s", item_kind == 8 ? "一叠" : "货架上的货", item_kind == 8 ? "取" : "买");
|
||||
else if ((rk == 5 || rk == 11) && W > 0) { dm_tc_finger *sf = &C.finger[i]; float ax = sf->aim_origin_x / W, ay = sf->aim_origin_y / H;
|
||||
if (rk == 11) { dm_ui_stack_cube_at(ax, ay); LOG("前端:高级仓库里的一叠拖到魔盒格上松手 → 1 件进魔盒"); }
|
||||
else if (item_kind == 8) { dm_ui_stack_take_at(ax, ay); LOG("前端:高级仓库里的一叠拖到背包松手 → 取 1 件"); }
|
||||
else { LOG("前端:货架上的货拖到背包松手 → 买"); dm_ui_shop_buy_at(ax, ay); }
|
||||
C.pointer_x = sf->aim_origin_x; C.pointer_y = sf->aim_origin_y; }
|
||||
} else if (C.navigation_mode && C.pointer_slot == i && C.finger[i].pointer_dragged && rel_asked) {
|
||||
int rr = dm_ui_item_result(rel_seq); if (rr < 0) rr = dm_ui_drop_last(); if (rr < 0) rr = 0; float dx, dy;
|
||||
if ((rr == 5 && dm_ui_drop_point(&dx, &dy)) || (rr == 0 && dm_ui_origin_point(&dx, &dy))) dm_tc_pointer_release_at(&C, dx * W, dy * H);
|
||||
else { if (rr == 7) { dm_shop_sell_cursor(); rr = 0; } dm_tc_pointer_release(&C, rr < 0 ? 1 : rr); }
|
||||
}
|
||||
rel_asked = 0;
|
||||
if (C.navigation_mode && C.pointer_slot == i && item_asked) { item_asked = 0; dm_tc_pointer_item(&C, dm_ui_item_result(item_seq)); bind_tap_sync(); }
|
||||
unsigned wt = C.world_tap_seq, mc = C.mouse_click_seq, tt = C.tab_tap_seq, us = C.use_seq, ids = C.ident_seq, sts = C.stack_take_seq, lps = C.long_press_seq, its = C.item_tap_seq;
|
||||
if (ui_drag_on && C.pointer_slot == i) { ui_drag_on = 0; dm_ui_drag_end(); }
|
||||
int was_ptr = C.pointer_slot == i; unsigned tm = C.tap_move_seq;
|
||||
dm_tc_end_at(&C, i, pend_fire ? pend_time : now); g_slot[i] = -1;
|
||||
if (C.tap_move_seq != tm) { uint32_t hid = dm_ui_hit_id();
|
||||
if (hid && hid == sel_id) { if (hid <= 4) { dm_stat_add((int)hid - 1, 1); LOG("前端:再点同一个「+」(属性 %u)→ 加 1 点", hid - 1); } else add_at = C.last_time_s + 0.3; }
|
||||
else { sel_id = hid; LOG("前端:点加点类按钮 → 选中(标识 %#x)", hid); } }
|
||||
if (C.tab_tap_seq != tt) dm_ui_tab_go();
|
||||
if (C.long_press_seq != lps) dm_ui_long_press();
|
||||
if (C.item_tap_seq != its) dm_ui_item_tap();
|
||||
if (C.stack_take_seq != sts) { dm_stash_take(1); LOG("前端:双击高级仓库里的一叠 → 取 1 件"); }
|
||||
if (C.ident_seq != ids) dm_ui_identify_hovered();
|
||||
if (C.use_seq != us) dm_ui_use_item();
|
||||
if (C.world_tap_seq != wt && W > 0) { float wx = C.world_tap_x / W, wy = C.world_tap_y / H; dm_ui_world_tap(wx, wy, wx, wy); }
|
||||
if (was_ptr && C.navigation_mode) tip_until = C.tap_move_seq != tm ? -1 : C.last_time_s + (C.mouse_click_seq != mc ? 0.05 : 2.8);
|
||||
if (C.mouse_click_seq != mc && W > 0) { float nx = C.mouse_click_x / W, ny = C.mouse_click_y / H; dm_ui_touch(0, nx, ny); dm_ui_touch(2, nx, ny); }
|
||||
}
|
||||
push();
|
||||
arm_pulse_end();
|
||||
}
|
||||
// 松手施放的脉冲:游戏真取走了(输入采样数前进、光标不扣着)才收回;iOS 用 120 Hz 定时器,这里每帧 tick 一次
|
||||
// Release-to-cast pulse: released only once the game has really taken it (input samples advanced, cursor not holding it); iOS uses a 120 Hz timer, here once per frame tick
|
||||
static void pulse_tick(void) {
|
||||
uint64_t n = dm_input_samples(); double now = now_s();
|
||||
if (C.pulse_button) {
|
||||
if (pulse_epoch != C.pulse_epoch) { pulse_epoch = C.pulse_epoch; pulse_s0 = n; pulse_t0 = now; pulse_seen = 0; pulse_free = 0; }
|
||||
if (!pulse_seen && n > pulse_s0) pulse_seen = now;
|
||||
if (pulse_seen && !pulse_free && !dm_cursor_pending()) pulse_free = now;
|
||||
double hold = C.pulse_hold_s;
|
||||
int taken = pulse_seen && now - pulse_seen >= 0.12 && n - pulse_s0 >= 3 && !dm_cursor_pending() && (hold <= 0 || (pulse_free && now - pulse_free >= hold));
|
||||
if (taken || now - pulse_t0 > 2.0 + hold) {
|
||||
if (!taken) LOG("[touch] 【红】脉冲 2 秒内游戏没取走(取了 %llu 次,扣着 %d 下),强制收回", (unsigned long long)(n - pulse_s0), dm_cursor_pending());
|
||||
if (dm_tc_pulse_end(&C, pulse_epoch)) push();
|
||||
pulse_s0 = n; pulse_t0 = now;
|
||||
}
|
||||
} else if (dm_tc_pulse_pending(&C)) {
|
||||
if (n - pulse_s0 >= 2 || now - pulse_t0 > 0.5) { if (dm_tc_pulse_resume(&C)) push(); }
|
||||
} else pulse_ticking = 0;
|
||||
}
|
||||
static void tick(double now) {
|
||||
apply_pending();
|
||||
int hide = g_fe || g_pad || dm_padmode_busy();
|
||||
{ static int lh = -1; if (hide != lh) { lh = hide; g_dirty = 1; } }
|
||||
{ int t = dm_drop_haptic_take(); if (t >= 1 && t <= 3) { g_haptic = t; LOG("掉落震动:前端震第 %d 档", t); } }
|
||||
if (labels_at && now >= labels_at) { labels_at = 0; if (!g_nav && !labels_done) { labels_done = 1; dm_ui_key_pulse_alone(DM_BTN_L, 9); } }
|
||||
dm_tc_set_tap_box(dm_ui_tap_box()); dm_tc_set_cur_stack(dm_ui_cur_stack_id()); dm_tc_set_hover_unid(dm_ui_hover_unid());
|
||||
{ dm_hud_info hi; dm_hud_get(&hi); dm_tc_set_holding(&C, hi.valid && hi.holding && !dm_ui_targeting()); }
|
||||
if (item_asked && C.navigation_mode && C.pointer_slot >= 0) { int ir = dm_ui_item_result(item_seq); if (ir >= 0) { item_asked = 0; item_kind = ir; dm_tc_pointer_item(&C, ir); } }
|
||||
if (pend_end >= 0) {
|
||||
int ready = !(item_asked && dm_ui_item_result(item_seq) < 0) && !(final_asked && dm_ui_item_result(rel_seq) < 0);
|
||||
if (ready || now - pend_wall > 0.25) { int pt = pend_end;
|
||||
pend_fire = 1; finish_touches(&pt, 1, 0); pend_fire = 0; pend_end = -1; final_asked = 0; }
|
||||
}
|
||||
bind_tap_sync();
|
||||
float sc = dm_tc_scale_for_size(W, H);
|
||||
if (C.navigation_mode && !dm_ui_bind_active() && !item_asked && C.pointer_slot >= 0 && C.pointer_slot < DM_TC_MAX_TOUCH) {
|
||||
dm_tc_finger *f = &C.finger[C.pointer_slot]; float nx = 0, ny = 0; uint32_t hid = dm_ui_hit_id();
|
||||
if (f->active && !f->pointer_dragged && !f->ui_drag && now - f->start_time_s >= 0.5 && hypotf(f->x - f->aim_origin_x, f->y - f->aim_origin_y) <= 10 * sc && dm_ui_bind_skill_hit(&nx, &ny) == 2 && hid >= 1 && hid <= 4) {
|
||||
if (!f->pointer_long) { f->pointer_long = 1; stat_next = now; sel_id = hid; C.pointer_valid = 1; C.pointer_x = nx * W; C.pointer_y = ny * H; LOG("前端:按住「+」(属性 %u)→ 连续加点", hid - 1); }
|
||||
if (now >= stat_next) { stat_next = now + 0.15; dm_stat_add((int)hid - 1, 1); } }
|
||||
}
|
||||
if (C.navigation_mode && !dm_ui_bind_active() && !item_asked && C.pointer_slot >= 0 && C.pointer_slot < DM_TC_MAX_TOUCH) {
|
||||
dm_tc_finger *f = &C.finger[C.pointer_slot]; float nx = 0, ny = 0;
|
||||
if (pend_end < 0 && f->active && !f->pointer_dragged && !f->pointer_long && now - f->start_time_s >= 0.5 && hypotf(f->x - f->aim_origin_x, f->y - f->aim_origin_y) <= 10 * sc && dm_ui_bind_skill_hit(&nx, &ny) == 1) {
|
||||
dm_tc_pointer_press(&C, nx * W, ny * H, DM_BTN_Y); LOG("前端:技能树里长按技能 (%.0f,%.0f) → 开绑定框", nx * W, ny * H); }
|
||||
}
|
||||
if (map_world_asked) { int wr = dm_world_tap_result(map_world_seq);
|
||||
if (wr >= 0) { map_world_asked = 0; if (wr == 0 && !g_nav) { dm_ui_map_full(1); LOG("前端:点小地图那一块 (%.0f,%.0f) → 全屏地图", map_wx, map_wy); }
|
||||
else LOG("前端:点小地图那一块 (%.0f,%.0f) → 底下有世界目标(类 %d),交互、不切地图", map_wx, map_wy, wr); } }
|
||||
if (add_at > 0 && now >= add_at) { dm_points_info pi; dm_points_get(&pi);
|
||||
if (pi.tree_open && pi.sel_skill >= 0 && !dm_cursor_pending()) { add_at = 0; if (pi.sel_block == 0 || pi.sel_block == 5) dm_skill_add_selected(); else LOG("前端:再点同一个技能 → 不能加(原因 %d),不发", pi.sel_block); }
|
||||
else if (now > add_at + 1.0 || !pi.tree_open) add_at = 0; }
|
||||
if (!C.navigation_mode || g_pad) tip_until = -2;
|
||||
else if (C.pointer_slot >= 0) tip_until = -1;
|
||||
else if (tip_until == -2) { if (dm_ui_free_cursor()) tip_until = now + 0.3; }
|
||||
else if (tip_until > 0 && now >= tip_until && !C.holding && !dm_ui_bind_active() && !dm_ui_touch_active() && !C.pulse_button && !dm_tc_pulse_pending(&C) && !dm_cursor_pending()) {
|
||||
C.pointer_valid = 1; C.pointer_x = 0.02f * W; C.pointer_y = 0.5f * H; tip_until = 0; }
|
||||
uint64_t tb = C.buttons;
|
||||
dm_tc_tick(&C, now);
|
||||
{ int fg = 0; for (int i = 0; i < DM_TC_MAX_TOUCH; i++) fg |= C.finger[i].active; dm_ui_hand_idle(!fg && !C.pulse_button && !dm_tc_pulse_pending(&C) && !dm_cursor_pending() && !dm_ui_touch_active()); }
|
||||
if (C.buttons != tb && !C.pulse_button) push();
|
||||
else if (C.belt_request && !C.pulse_button) push();
|
||||
if (C.pulse_button && !pulse_ticking) { push(); arm_pulse_end(); }
|
||||
if (C.pointer_valid) dm_cursor_set_screen(C.pointer_x, C.pointer_y, W, H);
|
||||
else if (C.navigation_mode) dm_cursor_clear();
|
||||
if (pulse_ticking) pulse_tick();
|
||||
refresh_portal_binding();
|
||||
{ static uint64_t lb; static int32_t llx, lly; if (C.buttons != lb || C.lx != llx || C.ly != lly) { lb = C.buttons; llx = C.lx; lly = C.ly; g_dirty = 1; } }
|
||||
}
|
||||
|
||||
// ---- 绘制指令表(Java TouchOverlay 照表画;与 DmTouchOverlay.m drawRect 一一对应) | draw list (executed by Java TouchOverlay; mirrors DmTouchOverlay.m drawRect) ----
|
||||
enum { OP_FILL_ELLIPSE = 1, OP_STROKE_ELLIPSE, OP_LINE, OP_ARC, OP_IMAGE, OP_TEXT, OP_FILL_RECT, OP_V4, OP_V4TEXT, OP_LABEL, OP_COVER };
|
||||
// 三层:0 = HUD 主体,1 = 摇杆,2 = 小地图任务终点(draw_goals)。手指在摇杆上动时只有摇杆层变,主体层(GPU 缓存成一张纹理)不重画——
|
||||
// 整层 HUD 每帧在 HWUI 里重画要占 GPU 6–11 ms,和游戏抢同一块 GPU,跑动中转向(手指一直在动)就掉帧
|
||||
// Three layers: 0 = HUD body, 1 = joystick, 2 = minimap quest goals (draw_goals). While a finger moves on the stick only the joystick layer changes, and the body layer (cached on the GPU as one texture) is not redrawn --
|
||||
// redrawing the whole HUD in HWUI every frame cost 6-11 ms of GPU, competing with the game for the same GPU, so turning while running (finger always moving) dropped frames
|
||||
typedef struct { float ops[8192]; int n; char str[96][64]; int nstr; } op_list;
|
||||
static op_list g_drawn[3], g_probe; static op_list *g_ol = &g_drawn[0];
|
||||
static void op(int code, int n, const float *v) {
|
||||
if (g_ol->n + n + 2 > (int)(sizeof g_ol->ops / sizeof *g_ol->ops)) return;
|
||||
g_ol->ops[g_ol->n++] = (float)code; g_ol->ops[g_ol->n++] = (float)n; memcpy(&g_ol->ops[g_ol->n], v, n * sizeof *v); g_ol->n += n;
|
||||
}
|
||||
static float str(const char *s) {
|
||||
for (int i = 0; i < g_ol->nstr; i++) if (!strcmp(g_ol->str[i], s)) return (float)i;
|
||||
if (g_ol->nstr >= 96) return -1;
|
||||
snprintf(g_ol->str[g_ol->nstr], sizeof g_ol->str[0], "%s", s); return (float)g_ol->nstr++;
|
||||
}
|
||||
static int same_list(const op_list *a, const op_list *b) {
|
||||
if (a->n != b->n || a->nstr != b->nstr || memcmp(a->ops, b->ops, a->n * sizeof *a->ops)) return 0;
|
||||
for (int i = 0; i < a->nstr; i++) if (strcmp(a->str[i], b->str[i])) return 0;
|
||||
return 1;
|
||||
}
|
||||
#define OPV(code, ...) do { const float v_[] = {__VA_ARGS__}; op(code, (int)(sizeof v_ / sizeof *v_), v_); } while (0)
|
||||
static void fill_ellipse(float x, float y, float w, float h, float r, float g, float b, float a) { OPV(OP_FILL_ELLIPSE, x, y, w, h, r, g, b, a); }
|
||||
static void stroke_ellipse(float x, float y, float w, float h, float lw, float r, float g, float b, float a) { OPV(OP_STROKE_ELLIPSE, x, y, w, h, lw, r, g, b, a); }
|
||||
static void caption(const char *s, float x, float y, float w, float h, float size, float alpha) { OPV(OP_TEXT, str(s), x, y, w, h, size, alpha); }
|
||||
// 图:素材路径(备用路径,-1 无),裁切框(sw ≤ 0 = 整张),目标框,不透明度,混合(0 正常 / 1 灰度) | image: asset path (fallback path, -1 none), crop (sw ≤ 0 = whole), destination, alpha, blend (0 normal / 1 luminosity)
|
||||
static void image(const char *a, const char *fb, float sx, float sy, float sw, float sh, float dx, float dy, float dw, float dh, float alpha, int blend) {
|
||||
OPV(OP_IMAGE, str(a), fb ? str(fb) : -1, sx, sy, sw, sh, dx, dy, dw, dh, alpha, (float)blend);
|
||||
}
|
||||
static dm_ta_glyph shown_glyph(uint64_t button, uint64_t now, int set) {
|
||||
dm_ta_glyph g = dm_ta_current_set(&g_native, button, now, set);
|
||||
if (g.status == DM_TA_UNKNOWN && g_native.captured_ns && now > g_native.captured_ns && now - g_native.captured_ns > DM_TG_MAX_AGE_NS) g = dm_ta_current_set(&g_native, button, g_native.captured_ns, set);
|
||||
return g;
|
||||
}
|
||||
// 技能图标的裁切框;0 = 没有 | skill icon crop rect; 0 = none
|
||||
static int skill_frame(dm_ta_glyph g, int held, dm_ta_frame *f, char *path, char *sil, size_t n) {
|
||||
if (g.status != DM_TA_GLYPH) return 0;
|
||||
if (!dm_ta_frame_at(g.atlas, held ? g.pressed_frame : g.normal_frame, f)) return 0;
|
||||
snprintf(path, n, "TouchArt/%s.png", dm_ta_atlases[g.atlas].asset); snprintf(sil, n, "hud_v4_ui/sil_%s.png", dm_ta_atlases[g.atlas].asset);
|
||||
return 1;
|
||||
}
|
||||
#define DM_SKILL_IDLE_ALPHA .6f
|
||||
static void draw_shell(float x, float y, float rr, int held) {
|
||||
float lw = MAX(1.5f, rr * 0.075f), fr = held ? 1 : DM_SKILL_IDLE_ALPHA, d = rr * 2;
|
||||
fill_ellipse(x - rr, y - rr, d, d, .086f, .067f, .047f, held ? .92f : .80f * fr);
|
||||
stroke_ellipse(x - rr + lw / 2, y - rr + lw / 2, d - lw, d - lw, lw, .60f, .47f, .28f, held ? 1 : .9f * fr);
|
||||
stroke_ellipse(x - rr + lw * 1.3f, y - rr + lw * 1.3f, d - lw * 2.6f, d - lw * 2.6f, lw * .5f, .12f, .09f, .05f, .9f * fr);
|
||||
}
|
||||
// 小地图任务终点(同 front_ios.m mapGoalSync;宿主 dm_world.c 找终点、dm_cursor.c 给地图几何):七个墓的真墓口 = 塔拉夏符号,其余 = 原版名牌 + 灰十字;
|
||||
// 在地图可见范围外 = 只写名字,沿 玩家点 → 终点 的方向贴边(全屏地图让开右下技能区)。世界格 (x, y) 在 玩家点 + ((Δx−Δy)×1.6, (Δx+Δy)×0.8) × 每格像素。
|
||||
// 画面 = 整个视图(Android 不留边)。面板开着不画;手柄模式照画(同 iOS)。素材 assets/mapgoal/(art_from_ipa.py 从 iOS 包取)
|
||||
// Minimap quest goals (same as front_ios.m mapGoalSync; the host's dm_world.c finds the goals, dm_cursor.c gives the map geometry): the real tomb of the seven = the Tal Rasha symbol, others = the original nameplate + grey cross;
|
||||
// outside the visible map range = name only, clamped to the edge along player point → goal (the fullscreen map avoids the bottom-right skill area). World cell (x, y) is at player point + ((Δx−Δy)×1.6, (Δx+Δy)×0.8) × pixels per cell.
|
||||
// The picture = the whole view (no letterbox on Android). Not drawn while a panel is open; drawn in pad mode too (as on iOS). Art in assets/mapgoal/ (taken from the iOS bundle by art_from_ipa.py)
|
||||
static void draw_goals(void) {
|
||||
static int old = -1; if (old < 0) old = dm_custom_get("item_notes") != 0; // 旧译名开关重启生效 | the old-names switch takes effect on restart
|
||||
dm_map_goal_view v; float cell = 0; int mode = dm_ui_map_geom(&cell), n = dm_map_goals(&v), rw = 0, rh = 0; dm_screen_real(&rw, &rh);
|
||||
float mm[4]; int mini = dm_ui_minimap_rect(mm);
|
||||
int show = mode && n > 0 && !g_nav && !g_fe && rw > 0 && cell > 0 && (mode == 2 || mini);
|
||||
static int lm = -1, ln = -1, lc; int m2 = show ? mode : 0;
|
||||
if ((m2 != lm || n != ln) && lc < 40) { lm = m2; ln = n; lc++; LOG("小地图终点:%s %d 个(地图 %d,每格 %.2f 像素 = %.2f dp)", show ? "画" : "不画", n, mode, cell, cell * W / (rw > 0 ? rw : 1)); }
|
||||
if (!show) return;
|
||||
if (n > 12) n = 12;
|
||||
float c = cell * W / rw, lh = H * .0236f, sh = H * .085f * (mode == 1 ? .6f : 1), cw = lh * 1.4f, ch = cw * 18 / 34;
|
||||
float ox, oy, ax0, ay0, ax1, ay1; // 玩家点,可见范围 | player point, visible area
|
||||
if (mode == 2) { ox = v.sx * W; oy = v.sy * H; ax0 = W * .04f; ay0 = H * .06f; ax1 = ax0 + W * .92f; ay1 = ay0 + H * .72f; }
|
||||
else { ax0 = mm[0] * W; ay0 = mm[1] * H; ax1 = mm[2] * W; ay1 = mm[3] * H; ox = (ax0 + ax1) / 2; oy = (ay0 + ay1) / 2;
|
||||
float iw = (ax1 - ax0) * .06f, ih = (ay1 - ay0) * .08f; ax0 += iw; ax1 -= iw; ay0 += ih; ay1 -= ih; }
|
||||
for (int i = 0; i < n; i++) {
|
||||
int dx = v.x[i] - v.px, dy = v.y[i] - v.py; float px = ox + (dx - dy) * 1.6f * c, py = oy + (dx + dy) * .8f * c;
|
||||
int skill = mode == 2 && px > W * .72f && py > H * .45f; // 全屏地图右下是技能区 | the bottom right of the fullscreen map is the skill area
|
||||
int inside = px >= ax0 && px < ax1 && py >= ay0 && py < ay1 && !skill, art = v.art[i], sym = art >= 64;
|
||||
char lab[48] = "", fb[48] = "", mark[48] = "";
|
||||
if (inside && sym) snprintf(mark, sizeof mark, "mapgoal/sym_%d.png", art - 64);
|
||||
else { int id = sym ? 0 : art; snprintf(fb, sizeof fb, "mapgoal/label_%d.png", id);
|
||||
if (!dm_lang_zh()) snprintf(lab, sizeof lab, "mapgoal/label_%d_en.png", id); // 简中 / 繁体用中文名牌,其余 enUS;旧译名只跟简中 | zh uses the Chinese label, others enUS; old names only for zhCN
|
||||
else if (old && dm_lang_index() == 8) snprintf(lab, sizeof lab, "mapgoal/label_%d_old.png", id);
|
||||
else snprintf(lab, sizeof lab, "%s", fb);
|
||||
if (inside) snprintf(mark, sizeof mark, "mapgoal/cross.png"); }
|
||||
if (!inside) { // 画面外:收到可见范围边上,只写名字 | off-screen: clamp to the edge of the visible range, name only
|
||||
float tx = px > ox ? (ax1 - ox) / (px - ox) : px < ox ? (ax0 - ox) / (px - ox) : 1e9f, ty = py > oy ? (ay1 - oy) / (py - oy) : py < oy ? (ay0 - oy) / (py - oy) : 1e9f, t = MIN(tx, ty);
|
||||
px = ox + (px - ox) * t; py = oy + (py - oy) * t;
|
||||
if (mode == 2 && px > W * .72f && py > H * .45f) py = H * .45f; }
|
||||
if (lab[0]) OPV(OP_LABEL, str(lab), px, mark[0] ? py - ch / 2 - 2 - lh / 2 : py, lh, strcmp(lab, fb) ? str(fb) : -1);
|
||||
if (mark[0]) { if (sym) OPV(OP_LABEL, str(mark), px, py, sh); else image(mark, NULL, 0, 0, 0, 0, px - cw / 2, py - ch / 2, cw, ch, 1, 0); }
|
||||
}
|
||||
}
|
||||
static void draw_overlay(int part) {
|
||||
g_ol->n = 0; g_ol->nstr = 0;
|
||||
int cover = atomic_load(&g_cover);
|
||||
if (cover == 1) { if (!part) OPV(OP_COVER, W, H); return; }
|
||||
if (part == 2) { draw_goals(); return; }
|
||||
if (g_fe || g_pad || dm_padmode_busy()) return;
|
||||
const dm_tc_state *s = &C; const dm_tc_layout *l = &s->layout;
|
||||
float k = l->joy_radius / 60; if (k <= 0) return;
|
||||
if (part) {
|
||||
if ((g_hud.valid && g_hud.dead) || s->navigation_mode) return;
|
||||
int moving = s->joy_slot >= 0;
|
||||
float ox = moving ? s->joy_origin_x : l->joy_x, oy = moving ? s->joy_origin_y : l->joy_y, radius = l->joy_radius;
|
||||
stroke_ellipse(ox - radius, oy - radius, radius * 2, radius * 2, k, .66f, .53f, .32f, moving ? .32f : .12f);
|
||||
float dx = s->lx / 32767.0f * radius, dy = -s->ly / 32767.0f * radius;
|
||||
image("TouchArt/dm_touch_joy.png", NULL, 0, 0, 0, 0, ox + dx - 23 * k, oy + dy - 23 * k, 46 * k, 46 * k, moving ? .8f : .18f, 0);
|
||||
if (moving && (s->lx || s->ly)) { float ang = atan2f(dy, dx); OPV(OP_ARC, ox, oy, radius + 3 * k, ang - (float)M_PI / 6, ang + (float)M_PI / 6, 2 * k, .66f, .53f, .32f, .7f); }
|
||||
return;
|
||||
}
|
||||
int nav = g_nav != 0; uint64_t host_now = now_ns();
|
||||
const char *labels[DM_TC_COUNT] = {"主技能", "技能一", "技能二", "技能三", "技能四", "回城", "药剂", "药剂", "药剂", "药剂", "功能", "地图"};
|
||||
if (nav) { labels[0] = "确认/交换"; labels[1] = "返回"; labels[2] = "操作"; labels[3] = "绑定"; labels[4] = "下一页"; }
|
||||
for (int i = 0; i < DM_TC_COUNT; i++) {
|
||||
if (!(s->enabled_targets & (1u << i))) continue;
|
||||
if (!nav && i > DM_TC_PORTAL && i <= DM_TC_MENU) continue;
|
||||
const dm_tc_target *t = &l->target[i]; float bx = t->x - t->r, by = t->y - t->r, bw = t->r * 2;
|
||||
int held = t->button != 0 && (s->buttons & t->button) == t->button;
|
||||
if (i == DM_TC_PORTAL) for (int f = 0; f < DM_TC_MAX_TOUCH; f++) held |= s->finger[f].active && s->finger[f].target == DM_TC_PORTAL;
|
||||
float opacity = held ? .6f : .28f;
|
||||
if (!nav && i == DM_TC_MAP && s->map_cancel_active) { OPV(OP_FILL_RECT, bx, by, bw, bw, .55f, .05f, .04f, .36f); labels[i] = "取消"; }
|
||||
int skill_key = !nav && (i <= DM_TC_SKILL4 || i == DM_TC_PORTAL);
|
||||
if (skill_key) draw_shell(t->x, t->y, t->r, held);
|
||||
else image("TouchArt/dm_native_circle.png", NULL, 0, 0, 0, 0, bx, by, bw, bw, opacity, 0);
|
||||
int have_icon = 0, show_caption = 1, ss_kind = 0, ss_charges = 0; dm_ta_frame fr; char ip[96], sp[96]; float iw = 0, ih = 0;
|
||||
if (!nav && i <= DM_TC_SKILL4) {
|
||||
dm_ta_glyph g = shown_glyph(t->button, host_now, s->skill_set);
|
||||
if (g.status != DM_TA_EMPTY && g.status != DM_TA_UNKNOWN) ss_kind = dm_swapskill_slot(g.slot, &ss_charges);
|
||||
have_icon = skill_frame(g, held, &fr, ip, sp, sizeof ip);
|
||||
show_caption = g.status != DM_TA_EMPTY && !have_icon;
|
||||
} else if (!nav && i == DM_TC_PORTAL) have_icon = skill_frame(dm_ta_skill(359), held, &fr, ip, sp, sizeof ip);
|
||||
else if (!nav && (i == DM_TC_MENU || i == DM_TC_MAP)) {
|
||||
have_icon = 1; fr = (dm_ta_frame){held ? 36 : 0, 0, 36, 36};
|
||||
snprintf(ip, sizeof ip, "TouchArt/%s", i == DM_TC_MAP ? "dm_native_map.png" : "dm_native_menu.png"); sp[0] = 0;
|
||||
}
|
||||
if (!nav && i == DM_TC_MAP && s->map_cancel_active) { have_icon = 0; show_caption = 1; }
|
||||
if (have_icon) { iw = (float)fr.width; ih = (float)fr.height; }
|
||||
if (skill_key && !have_icon && (!show_caption || i == DM_TC_MAIN)) { // 空格:十字 | empty slot: a cross
|
||||
float a_ = t->r * .30f, w_ = MAX(1.5f, t->r * .085f), al = held ? 1 : .9f;
|
||||
OPV(OP_LINE, t->x - a_, t->y, t->x + a_, t->y, w_, .60f, .47f, .28f, al);
|
||||
OPV(OP_LINE, t->x, t->y - a_, t->x, t->y + a_, w_, .60f, .47f, .28f, al);
|
||||
continue;
|
||||
}
|
||||
if (skill_key) {
|
||||
int bare = have_icon, spent = (ss_kind & 2) && ss_charges <= 0;
|
||||
if (have_icon) { float size = bw * (bare ? .64f : .56f), fit = size / MAX(iw, ih);
|
||||
float rx_ = t->x - iw * fit / 2, ry_ = t->y - (bare ? 0 : t->r * .12f) - ih * fit / 2;
|
||||
if (spent) { fill_ellipse(bx + t->r * .14f, by + t->r * .14f, bw - t->r * .28f, bw - t->r * .28f, .30f, .30f, .30f, 1);
|
||||
image(sp, ip, fr.x, fr.y, fr.width, fr.height, rx_, ry_, iw * fit, ih * fit, .55f, 1);
|
||||
fill_ellipse(bx, by, bw, bw, 0, 0, 0, .35f); }
|
||||
else image(sp[0] ? sp : ip, sp[0] ? ip : NULL, fr.x, fr.y, fr.width, fr.height, rx_, ry_, iw * fit, ih * fit, held ? 1 : .92f, 0); }
|
||||
if (ss_kind & 1) { float br = t->r * .30f, bx2 = t->x + t->r * .74f, by2 = t->y - t->r * .74f, in = MAX(1, br * .08f);
|
||||
fill_ellipse(bx2 - br, by2 - br, br * 2, br * 2, .11f, .094f, .078f, .95f);
|
||||
stroke_ellipse(bx2 - br + in, by2 - br + in, br * 2 - in * 2, br * 2 - in * 2, MAX(1, br * .14f), .63f, .50f, .27f, 1);
|
||||
float ts = br * 1.15f; caption("Ⅱ", bx2 - br, by2 - ts * .62f, br * 2, ts * 1.4f, ts, 1); }
|
||||
if (ss_kind & 2) { char cs[16]; snprintf(cs, sizeof cs, "%d", ss_charges < 0 ? 0 : ss_charges); float u = H / 1320.0f;
|
||||
if (u > 0) OPV(OP_V4TEXT, str(cs), (t->x + t->r * .66f - W / 2) / u + 1434, (t->y + t->r * .42f) / u, 1); }
|
||||
if (bare) continue;
|
||||
char ln[48]; if (i == DM_TC_MAIN) snprintf(ln, sizeof ln, "hud_v4_ui/lbl_main.png"); else snprintf(ln, sizeof ln, "hud_v4_ui/lbl_f%d.png", i - DM_TC_SKILL1 + 1 + (s->skill_set ? 4 : 0));
|
||||
OPV(OP_LABEL, str(ln), t->x, t->y + t->r * .50f, t->r * (i == DM_TC_MAIN ? .30f : .36f));
|
||||
continue;
|
||||
}
|
||||
if (have_icon) { float size = bw * (i == DM_TC_MAIN ? .55f : .62f), fit = size / MAX(iw, ih);
|
||||
image(ip, NULL, fr.x, fr.y, fr.width, fr.height, t->x - iw * fit / 2, t->y - ih * fit / 2, iw * fit, ih * fit, held ? 1 : .82f, 0); show_caption = 0; }
|
||||
float ts = (i == DM_TC_MAIN ? 13 : 10) * k;
|
||||
if (show_caption) caption(labels[i], bx, t->y - ts * .6f, bw, ts * 1.6f, ts, held ? 1 : .84f);
|
||||
if (i >= DM_TC_BELT1 && i <= DM_TC_BELT4) { char d[4]; snprintf(d, sizeof d, "%d", i - DM_TC_BELT1 + 1); caption(d, t->x - 10 * k, t->y + 8 * k, 20 * k, 12 * k, 8 * k, .65f); }
|
||||
}
|
||||
if (!nav && l->swap.r > 0) {
|
||||
int sheld = 0; for (int f = 0; f < DM_TC_MAX_TOUCH; f++) sheld |= s->finger[f].active && hypotf(s->finger[f].x - l->swap.x, s->finger[f].y - l->swap.y) <= l->swap.r;
|
||||
draw_shell(l->swap.x, l->swap.y, l->swap.r, sheld);
|
||||
float ts = 14 * k; caption(s->skill_set ? "Ⅱ" : "Ⅰ", l->swap.x - l->swap.r, l->swap.y - ts * .8f, l->swap.r * 2, ts * 1.6f, ts, 1);
|
||||
}
|
||||
if (g_hud.valid && g_hud.dead) return;
|
||||
if (s->navigation_mode) return;
|
||||
// 底部 UI v4:可见性在这里判,几何与字形 Java 按 hud_v4_ui/ui.json 画 | bottom UI v4: visibility decided here, geometry and glyphs drawn by Java from hud_v4_ui/ui.json
|
||||
const dm_hud_info *h = &g_hud;
|
||||
if (dm_hud_bar_visible(h, nav && !g_dialog_only, g_hud_blocked_v)) {
|
||||
char life[64], mana[64], lv[48];
|
||||
snprintf(life, sizeof life, dm_ui("dmHudLife"), h->hp, h->hp_max); snprintf(mana, sizeof mana, dm_ui("dmHudMana"), h->mp, h->mp_max);
|
||||
snprintf(lv, sizeof lv, "Lv %d · %d%%", h->level, h->exp_permille / 10);
|
||||
OPV(OP_V4, str(life), str(mana), str(lv), (s->buttons & DM_BTN_PLUS) ? 1 : 0, h->points > 0 ? 1 : 0, h->quest_unread ? 1 : 0, (float)h->exp_permille,
|
||||
(float)h->belt[0], (float)h->belt[1], (float)h->belt[2], (float)h->belt[3], (float)h->players, s->diff_open ? 1 : 0);
|
||||
}
|
||||
}
|
||||
|
||||
// ---- JNI(GameActivity 界面线程) | JNI (GameActivity UI thread) ----
|
||||
#define JNI(name) JNICALL Java_org_d2rmobile_app_GameActivity_##name
|
||||
JNIEXPORT void JNI(nativeLayout)(JNIEnv *env, jclass cls, jfloat w, jfloat h, jfloat safe_l, jfloat safe_r) {
|
||||
(void)env; (void)cls;
|
||||
static int inited; static float sl = -1, sr = -1;
|
||||
if (!inited) {
|
||||
inited = 1; for (int i = 0; i < 16; i++) g_slot[i] = -1;
|
||||
dm_tc_set_tall_scale(1); dm_tc_init(&C, w, h); C.mouse_click_mode = 1; W = w; H = h;
|
||||
g_nav = -1; navigation_mode(atomic_load(&g_touch_navigation));
|
||||
}
|
||||
int safe_changed = safe_l != sl || safe_r != sr;
|
||||
if (safe_changed) { sl = safe_l; sr = safe_r; dm_tc_set_safe_insets(safe_l, safe_r); }
|
||||
dm_tc_set_edge_bottom(0);
|
||||
if (h > 0) { dm_ui_safe_left((int)lroundf(safe_l / h * 2160)); dm_ui_safe_right((int)lroundf(safe_r / h * 2160)); }
|
||||
if (safe_changed || C.layout.width != w || C.layout.height != h) { W = w; H = h; cancel_controls(); dm_tc_resize(&C, w, h); }
|
||||
LOG("触屏:视图 %.0fx%.0f dp,安全边距 左 %.0f 右 %.0f", w, h, safe_l, safe_r);
|
||||
g_dirty = 1;
|
||||
}
|
||||
// action:0 按下(pid 为新手指)/ 1 移动 / 2 抬起(pid)/ 3 取消(全部);pids / xs / ys 为这一刻全部手指(dp)
|
||||
// action: 0 down (pid is the new finger) / 1 move / 2 up (pid) / 3 cancel (all); pids / xs / ys are all fingers at this moment (dp)
|
||||
JNIEXPORT void JNI(nativeTouchEvent)(JNIEnv *env, jclass cls, jint action, jint pid, jintArray pids, jfloatArray xs, jfloatArray ys, jint n) {
|
||||
(void)cls;
|
||||
if (W <= 0) return;
|
||||
jint id[NREC]; jfloat x[NREC], y[NREC]; if (n > NREC) n = NREC;
|
||||
if (n > 0) { (*env)->GetIntArrayRegion(env, pids, 0, n, id); (*env)->GetFloatArrayRegion(env, xs, 0, n, x); (*env)->GetFloatArrayRegion(env, ys, 0, n, y); }
|
||||
apply_pending();
|
||||
int ts[NREC], m = 0;
|
||||
for (int i = 0; i < n; i++) { int t = pid_rec(id[i]); if (t >= 0) { R[t].x = x[i]; R[t].y = y[i]; } }
|
||||
if (action == 0) {
|
||||
if (pid_rec(pid) >= 0) return;
|
||||
int t = -1; for (int i = 0; i < NREC; i++) if (!R[i].used) { t = i; break; }
|
||||
if (t < 0) { gc_recs(); return; }
|
||||
R[t].used = 1; R[t].done = 0; R[t].pid = pid; R[t].x = R[t].y = 0;
|
||||
for (int i = 0; i < n; i++) if (id[i] == pid) { R[t].x = x[i]; R[t].y = y[i]; }
|
||||
ts[m++] = t; touches_began(ts, m);
|
||||
} else if (action == 1) {
|
||||
for (int i = 0; i < NREC; i++) if (R[i].used && !R[i].done) ts[m++] = i;
|
||||
if (m) touches_moved(ts, m);
|
||||
} else if (action == 2) {
|
||||
int t = pid_rec(pid); if (t < 0) return;
|
||||
R[t].done = 1; ts[m++] = t; finish_touches(ts, m, 0);
|
||||
} else {
|
||||
for (int i = 0; i < NREC; i++) if (R[i].used && !R[i].done) { R[i].done = 1; ts[m++] = i; }
|
||||
if (m) finish_touches(ts, m, 1);
|
||||
}
|
||||
gc_recs();
|
||||
}
|
||||
// 每帧(Choreographer):返回位 0 / 1 / 2 = 主体层 / 摇杆层 / 终点层要重画,位 4–5 = 掉落震动档(1 重 / 2 中 / 3 轻),位 8–9 = 重开遮罩(1 黑屏 / 2 透明)
|
||||
// Per frame (Choreographer): bits 0 / 1 / 2 = body / joystick / goals layer needs a redraw, bits 4–5 = drop haptic level (1 heavy / 2 medium / 3 light), bits 8–9 = restart cover (1 black / 2 transparent)
|
||||
JNIEXPORT jint JNI(nativeTick)(JNIEnv *env, jclass cls) {
|
||||
(void)env; (void)cls;
|
||||
if (W <= 0) return 0;
|
||||
tick(now_s());
|
||||
static int lc; int cover = atomic_load(&g_cover); if (cover != lc) { lc = cover; g_dirty = 1; if (cover) cancel_controls(); }
|
||||
// 位 0 / 1 = 主体层 / 摇杆层要重画:重算一遍指令表,和上次画出来的不同才算 | bits 0 / 1 = body / joystick layer needs a redraw: rebuild the draw list, and only count it if it differs from what was last drawn
|
||||
int r = (g_haptic & 3) << 4 | (cover & 3) << 8;
|
||||
if (g_dirty) for (int part = 0; part < 2; part++) { g_ol = &g_probe; draw_overlay(part); if (!same_list(&g_probe, &g_drawn[part])) r |= 1 << part; }
|
||||
{ g_ol = &g_probe; draw_overlay(2); if (!same_list(&g_probe, &g_drawn[2])) r |= 4; } // 终点层跟着玩家走,每帧都比 | the goals layer follows the player, compared every frame
|
||||
g_dirty = 0; g_haptic = 0;
|
||||
return r;
|
||||
}
|
||||
// 返回 [float[] 指令, String[] 字符串表] | returns [float[] ops, String[] strings]
|
||||
JNIEXPORT jobjectArray JNI(nativeDraw)(JNIEnv *env, jclass cls, jint part) {
|
||||
(void)cls;
|
||||
if (part < 0 || part > 2) part = 0;
|
||||
g_ol = &g_drawn[part]; draw_overlay(part);
|
||||
jfloatArray f = (*env)->NewFloatArray(env, g_ol->n); if (g_ol->n) (*env)->SetFloatArrayRegion(env, f, 0, g_ol->n, g_ol->ops);
|
||||
jclass sc = (*env)->FindClass(env, "java/lang/String");
|
||||
jobjectArray sa = (*env)->NewObjectArray(env, g_ol->nstr, sc, NULL);
|
||||
for (int i = 0; i < g_ol->nstr; i++) { jstring js = (*env)->NewStringUTF(env, g_ol->str[i]); (*env)->SetObjectArrayElement(env, sa, i, js); (*env)->DeleteLocalRef(env, js); }
|
||||
jobjectArray out = (*env)->NewObjectArray(env, 2, (*env)->FindClass(env, "java/lang/Object"), NULL);
|
||||
(*env)->SetObjectArrayElement(env, out, 0, f); (*env)->SetObjectArrayElement(env, out, 1, sa);
|
||||
return out;
|
||||
}
|
||||
Reference in New Issue
Block a user