-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathInputControlsView.kt
More file actions
523 lines (443 loc) · 16.9 KB
/
Copy pathInputControlsView.kt
File metadata and controls
523 lines (443 loc) · 16.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
package org.github.ewt45.winemulator.inputcontrols
import android.annotation.SuppressLint
import android.content.Context
import android.graphics.*
import android.os.VibrationEffect
import android.os.Vibrator
import android.view.MotionEvent
import android.view.View
import android.widget.FrameLayout
import org.github.ewt45.winemulator.inputcontrols.ControlElement.Shape
import org.github.ewt45.winemulator.inputcontrols.ControlElement.Type
import kotlin.math.*
/**
* View for rendering and interacting with input controls
*/
@SuppressLint("ViewConstructor")
class InputControlsView(
context: Context,
private var editMode: Boolean = false
) : View(context) {
var inputEventHandler: InputEventHandler? = null
var profile: ControlsProfile? = null
private set
var showTouchscreenControls = true
var overlayOpacity = 0.4f
var touchpadView: TouchpadView? = null
val snappingSize: Int
get() = if (width > 0) maxOf(width, height) / 100 else 10
val maxWidth: Int
get() = if (snappingSize > 0) (width.toFloat() / snappingSize).roundToInt() * snappingSize else width
val maxHeight: Int
get() = if (snappingSize > 0) (height.toFloat() / snappingSize).roundToInt() * snappingSize else height
private var selectedElement: ControlElement? = null
private var moveCursor = false
private var offsetX = 0f
private var offsetY = 0f
private val cursor = Point()
private var pendingProfileReload = false // 标记是否需要在新尺寸测量后重新加载配置
private val paint = Paint(Paint.ANTI_ALIAS_FLAG)
private val path = Path()
private var readyToDraw = false
private var vibrator: Vibrator? = null
private var vibrationEffect: VibrationEffect? = null
private var rangeScroller: RangeScroller? = null
private var currentElementForScroller: ControlElement? = null
// Icon cache
private val icons = arrayOfNulls<Bitmap>(17)
// 用于检测尺寸变化,重新加载元素坐标
private var lastMaxWidth = 0
private var lastMaxHeight = 0
init {
// 默认可点击可聚焦,但会根据 showTouchscreenControls 动态调整
setClickable(true)
setFocusable(true)
isFocusableInTouchMode = true
setBackgroundColor(Color.TRANSPARENT)
layoutParams = FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT)
@Suppress("DEPRECATION")
try {
vibrator = context.getSystemService(Context.VIBRATOR_SERVICE) as? Vibrator
vibrationEffect = VibrationEffect.createOneShot(50, VibrationEffect.DEFAULT_AMPLITUDE)
} catch (e: Exception) {
vibrator = null
}
}
/**
* 设置是否显示虚拟按键,同时调整视图的点击和聚焦状态
*/
@JvmName("setControlsVisible")
fun setControlsVisible(show: Boolean) {
showTouchscreenControls = show
// 当不显示虚拟按键时,禁用所有交互,确保不拦截触摸事件
isClickable = false
isFocusable = false
isFocusableInTouchMode = false
// 刷新视图以更新绘制
invalidate()
}
override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
super.onSizeChanged(w, h, oldw, oldh)
// 当视图尺寸变化时,重新加载元素以重新计算坐标
// 这处理了屏幕旋转和分辨率变化的情况
if (w > 0 && h > 0) {
// 如果有待加载的配置,先加载配置
if (pendingProfileReload && profile != null) {
pendingProfileReload = false
reloadElements()
}
// 无论尺寸是否变化,都重新加载元素以确保坐标正确
else if (profile != null) {
reloadElements()
}
}
}
override fun onConfigurationChanged(newConfig: android.content.res.Configuration) {
super.onConfigurationChanged(newConfig)
// 当屏幕方向变化时,重新加载元素以重新计算坐标
if (profile != null && width > 0 && height > 0) {
reloadElements()
}
}
private fun reloadElements() {
if (profile != null) {
// 保存当前选中的元素(如果有)
val selected = selectedElement
// 重新加载元素(会更新所有元素的坐标)
profile!!.loadElements(this)
// 恢复选中状态
if (selected != null) {
val newSelected = profile!!.getElements().find { it == selected }
selectElement(newSelected)
}
}
invalidate()
}
fun setEditMode(mode: Boolean) {
editMode = mode
}
fun setProfile(profile: ControlsProfile?) {
this.profile = profile
deselectAllElements()
// 立即加载元素,但可能视图尚未测量
if (width > 0 && height > 0) {
pendingProfileReload = false
reloadElements()
} else {
// 视图尚未测量,标记待加载,下次 onSizeChanged 时加载
pendingProfileReload = true
}
}
fun getSelectedElement(): ControlElement? = selectedElement
fun addElement(): Boolean {
if (editMode && profile != null) {
val element = ControlElement(this)
element.x = cursor.x
element.y = cursor.y
element.initDefaultBindings()
profile!!.addElement(element)
profile!!.save()
selectElement(element)
return true
}
return false
}
fun removeElement(): Boolean {
if (editMode && selectedElement != null && profile != null) {
profile!!.removeElement(selectedElement!!)
selectedElement = null
profile!!.save()
invalidate()
return true
}
return false
}
fun getRangeScroller(): RangeScroller? = rangeScroller
private fun deselectAllElements() {
selectedElement = null
profile?.getElements()?.forEach { it.isSelected = false }
}
private fun selectElement(element: ControlElement?) {
deselectAllElements()
if (element != null) {
selectedElement = element
element.isSelected = true
}
invalidate()
}
fun handleInputEvent(binding: Binding, isDown: Boolean, value: Float = 0f) {
when {
binding.isGamepad -> {
// Gamepad events handled separately
}
binding.isMouse -> {
when {
binding.isMouseMove() -> {
// 处理鼠标移动
val dx = when (binding) {
Binding.MOUSE_MOVE_LEFT -> -10
Binding.MOUSE_MOVE_RIGHT -> 10
else -> 0
}
val dy = when (binding) {
Binding.MOUSE_MOVE_UP -> -10
Binding.MOUSE_MOVE_DOWN -> 10
else -> 0
}
if (isDown && (dx != 0 || dy != 0)) {
inputEventHandler?.onPointerMove(dx, dy)
}
}
else -> {
// 处理鼠标按钮事件,使用 getPointerButton 方法
binding.getPointerButton()?.let { button ->
inputEventHandler?.onPointerButton(button, isDown)
}
}
}
}
binding.isKeyboard -> {
inputEventHandler?.onKeyEvent(binding.keycode, isDown)
}
}
}
fun injectPointerMove(dx: Int, dy: Int) {
inputEventHandler?.onPointerMove(dx, dy)
}
override fun onDraw(canvas: Canvas) {
val w = width
val h = height
if (w == 0 || h == 0) {
readyToDraw = false
return
}
readyToDraw = true
if (editMode) {
drawGrid(canvas)
drawCursor(canvas)
}
if (profile != null) {
if (!profile!!.isElementsLoaded()) {
reloadElements()
}
if (showTouchscreenControls) {
profile!!.getElements().forEach { element ->
element.draw(canvas)
}
}
}
super.onDraw(canvas)
}
private fun drawGrid(canvas: Canvas) {
paint.style = Paint.Style.FILL
paint.strokeWidth = snappingSize * 0.0625f
paint.color = Color.BLACK
canvas.drawColor(Color.BLACK)
paint.isAntiAlias = false
paint.color = Color.rgb(48, 48, 48)
val w = maxWidth
val h = maxHeight
var i = 0
while (i <= w) {
canvas.drawLine(i.toFloat(), 0f, i.toFloat(), h.toFloat(), paint)
i += snappingSize
}
i = 0
while (i <= h) {
canvas.drawLine(0f, i.toFloat(), w.toFloat(), i.toFloat(), paint)
i += snappingSize
}
val cx = roundTo(w * 0.5f, snappingSize.toFloat())
val cy = roundTo(h * 0.5f, snappingSize.toFloat())
paint.color = Color.rgb(66, 66, 66)
i = 0
while (i <= w) {
canvas.drawLine(cx, i.toFloat(), cx, (i + snappingSize).toFloat(), paint)
i += snappingSize * 2
}
i = 0
while (i <= h) {
canvas.drawLine(i.toFloat(), cy, (i + snappingSize).toFloat(), cy, paint)
i += snappingSize * 2
}
paint.isAntiAlias = true
}
private fun drawCursor(canvas: Canvas) {
paint.style = Paint.Style.FILL
paint.strokeWidth = snappingSize * 0.0625f
paint.color = Color.rgb(198, 40, 40)
paint.isAntiAlias = false
canvas.drawLine(0f, cursor.y.toFloat(), maxWidth.toFloat(), cursor.y.toFloat(), paint)
canvas.drawLine(cursor.x.toFloat(), 0f, cursor.x.toFloat(), maxHeight.toFloat(), paint)
paint.isAntiAlias = true
}
fun getPaint(): Paint = paint
fun getPath(): Path = path
fun getColorFilter(): ColorFilter {
return PorterDuffColorFilter(0xFFFFFFFF.toInt(), PorterDuff.Mode.SRC_IN)
}
fun getPrimaryColor(): Int = Color.argb((overlayOpacity * 255).toInt(), 255, 255, 255)
fun getSecondaryColor(): Int = Color.argb((overlayOpacity * 255).toInt(), 2, 119, 189)
fun getIcon(id: Byte): Bitmap? {
if (icons[id.toInt()] == null) {
try {
context.assets.open("inputcontrols/icons/$id.png").use { inputStream ->
icons[id.toInt()] = BitmapFactory.decodeStream(inputStream)
}
} catch (e: Exception) {
// Icon not found
}
}
return icons[id.toInt()]
}
private fun roundTo(value: Float, rounding: Float): Float {
return (value / rounding).roundToInt() * rounding
}
override fun onTouchEvent(event: MotionEvent): Boolean {
if (editMode && readyToDraw) {
when (event.action) {
MotionEvent.ACTION_DOWN -> {
val x = event.x
val y = event.y
val element = intersectElement(x, y)
moveCursor = true
if (element != null) {
offsetX = x - element.x
offsetY = y - element.y
moveCursor = false
}
selectElement(element)
}
MotionEvent.ACTION_MOVE -> {
if (selectedElement != null) {
selectedElement!!.x = roundTo(event.x - offsetX, snappingSize.toFloat()).toInt()
selectedElement!!.y = roundTo(event.y - offsetY, snappingSize.toFloat()).toInt()
invalidate()
}
}
MotionEvent.ACTION_UP -> {
if (selectedElement != null && profile != null) {
profile!!.save()
}
if (moveCursor) {
cursor.x = roundTo(event.x, snappingSize.toFloat()).toInt()
cursor.y = roundTo(event.y, snappingSize.toFloat()).toInt()
}
invalidate()
}
}
return true
}
// 非编辑模式下,只有当 showTouchscreenControls 为 true 时才处理触摸事件
if (!editMode && profile != null && showTouchscreenControls) {
val actionIndex = event.actionIndex
val pointerId = event.getPointerId(actionIndex)
val actionMasked = event.actionMasked
var handled = false
when (actionMasked) {
MotionEvent.ACTION_DOWN, MotionEvent.ACTION_POINTER_DOWN -> {
val x = event.getX(actionIndex)
val y = event.getY(actionIndex)
for (element in profile!!.getElements()) {
if (element.handleTouchDown(pointerId, x, y)) {
vibrator?.vibrate(vibrationEffect)
handled = true
break
}
}
if (!handled) {
// 让 touchpadView 处理(如果存在),但不一定消费事件
touchpadView?.onTouchEvent(event)
}
}
MotionEvent.ACTION_MOVE -> {
for (i in 0 until event.pointerCount) {
val x = event.getX(i)
val y = event.getY(i)
val id = event.getPointerId(i)
for (element in profile!!.getElements()) {
if (element.handleTouchMove(id, x, y)) {
handled = true
break
}
}
}
if (!handled) {
touchpadView?.onTouchEvent(event)
}
}
MotionEvent.ACTION_UP, MotionEvent.ACTION_POINTER_UP, MotionEvent.ACTION_CANCEL -> {
for (element in profile!!.getElements()) {
if (element.handleTouchUp(pointerId)) {
handled = true
}
}
if (!handled) {
touchpadView?.onTouchEvent(event)
}
}
}
return handled
}
// 当 showTouchscreenControls 为 false 或 profile 为 null 时,不处理触摸事件,让事件传递给下层
return false
}
private fun intersectElement(x: Float, y: Float): ControlElement? {
profile?.getElements()?.forEach { element ->
if (element.containsPoint(x, y)) return element
}
return null
}
}
/**
* Touchpad view for mouse simulation
*/
@SuppressLint("ViewConstructor")
class TouchpadView(context: Context) : View(context) {
var isPointerButtonLeftEnabled = true
private set
private var swapMouseButtons = false
private var simTouchScreen = false
private var lastX = 0f
private var lastY = 0f
var inputEventHandler: InputEventHandler? = null
companion object {
const val CURSOR_ACCELERATION = 2f
const val CURSOR_ACCELERATION_THRESHOLD = 4f
}
fun setPointerButtonLeftEnabled(enabled: Boolean) {
isPointerButtonLeftEnabled = enabled
}
fun setSwapMouseButtons() {
swapMouseButtons = !swapMouseButtons
}
fun setSimTouchScreen() {
simTouchScreen = !simTouchScreen
}
fun computeDeltaPoint(oldX: Float, oldY: Float, newX: Float, newY: Float): FloatArray {
return floatArrayOf(newX - oldX, newY - oldY)
}
override fun onTouchEvent(event: MotionEvent): Boolean {
when (event.action) {
MotionEvent.ACTION_DOWN -> {
lastX = event.x
lastY = event.y
}
MotionEvent.ACTION_MOVE -> {
val dx = event.x - lastX
val dy = event.y - lastY
if (abs(dx) > CURSOR_ACCELERATION_THRESHOLD || abs(dy) > CURSOR_ACCELERATION_THRESHOLD) {
inputEventHandler?.onPointerMove(
(dx * CURSOR_ACCELERATION).toInt(),
(dy * CURSOR_ACCELERATION).toInt()
)
} else {
inputEventHandler?.onPointerMove(dx.toInt(), dy.toInt())
}
lastX = event.x
lastY = event.y
}
}
return true
}
}