feat: 控制指令消息改为protobuf二进制
This commit is contained in:
@@ -1,57 +1,59 @@
|
||||
import 'dart:convert';
|
||||
import 'package:fixnum/fixnum.dart';
|
||||
import 'package:webrtc_controller_flutter/proto/control_message.pb.dart';
|
||||
|
||||
/// 控制指令构造工具,对应 Android 端 RemoteTouchView 的指令格式。
|
||||
///
|
||||
/// 指令通过 DataChannel 以 JSON 字符串发送,被控端(WebRTCControlled)
|
||||
/// 的 InputCommandHandler 负责解析执行。
|
||||
/// 指令通过 DataChannel 以 protobuf 二进制([ControlMessage])发送,
|
||||
/// 被控端(WebRTCControlled)的 InputCommandHandler 负责解析执行。
|
||||
class ControlCommands {
|
||||
ControlCommands._();
|
||||
|
||||
/// 单击指令。坐标 x/y 为相对于屏幕的百分比(0.0 ~ 1.0)。
|
||||
static String touch(double x, double y) => jsonEncode({
|
||||
'action': 'TOUCH',
|
||||
'x': x,
|
||||
'y': y,
|
||||
});
|
||||
static ControlMessage touch(double x, double y) => ControlMessage(
|
||||
action: Action.TOUCH,
|
||||
x: x,
|
||||
y: y,
|
||||
);
|
||||
|
||||
/// 滑动指令。坐标为相对百分比,duration 为毫秒。
|
||||
static String swipe(
|
||||
static ControlMessage swipe(
|
||||
double x1,
|
||||
double y1,
|
||||
double x2,
|
||||
double y2,
|
||||
int durationMs,
|
||||
) =>
|
||||
jsonEncode({
|
||||
'action': 'SWIPE',
|
||||
'x1': x1,
|
||||
'y1': y1,
|
||||
'x2': x2,
|
||||
'y2': y2,
|
||||
'duration': durationMs,
|
||||
});
|
||||
ControlMessage(
|
||||
action: Action.SWIPE,
|
||||
x1: x1,
|
||||
y1: y1,
|
||||
x2: x2,
|
||||
y2: y2,
|
||||
duration: Int64(durationMs),
|
||||
);
|
||||
|
||||
/// 按键指令。action: 0=按下, 1=抬起(对应 Android KeyEvent.ACTION_DOWN/UP)。
|
||||
static String key(int keyCode, int action) => jsonEncode({
|
||||
'action': 'KEY',
|
||||
'keyCode': keyCode,
|
||||
'keyAction': action,
|
||||
});
|
||||
static ControlMessage key(int keyCode, int action) => ControlMessage(
|
||||
action: Action.KEY,
|
||||
keyCode: keyCode,
|
||||
keyAction: action,
|
||||
);
|
||||
|
||||
/// 长按指令。坐标 x/y 为相对于屏幕的百分比(0.0 ~ 1.0)。
|
||||
/// 对应 Android 端 RemoteTouchView.createLongPressCommand。
|
||||
static String longPress(double x, double y) => jsonEncode({
|
||||
'action': 'LONG_PRESS',
|
||||
'x': x,
|
||||
'y': y,
|
||||
});
|
||||
static ControlMessage longPress(double x, double y) => ControlMessage(
|
||||
action: Action.LONG_PRESS,
|
||||
x: x,
|
||||
y: y,
|
||||
);
|
||||
|
||||
/// 原始 MotionEvent 指令,用于实现“实时跟手”。
|
||||
/// action: 0=DOWN, 1=UP, 2=MOVE(对应 Android MotionEvent.ACTION_XXX)。
|
||||
static String motionEvent(int action, double x, double y) => jsonEncode({
|
||||
'action': 'MOTION_EVENT',
|
||||
'motionAction': action,
|
||||
'x': x,
|
||||
'y': y,
|
||||
});
|
||||
static ControlMessage motionEvent(int action, double x, double y) =>
|
||||
ControlMessage(
|
||||
action: Action.MOTION_EVENT,
|
||||
motionAction: action,
|
||||
x: x,
|
||||
y: y,
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user