-
Notifications
You must be signed in to change notification settings - Fork 82
增加摇杆左半屏移动功能 #546
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
LanRhyme
wants to merge
6
commits into
ZalithLauncher:main
Choose a base branch
from
LanRhyme:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
增加摇杆左半屏移动功能 #546
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
08c7bbb
feat(joystick): ✨️摇杆左半屏移动功能
LanRhyme fabc459
fix(joystick): 🪲️修复摇杆层冲突
LanRhyme 5c21a3d
fix(joystick): 🪲️调整摇杆图层位置并分离输入与渲染
LanRhyme f748b34
fix(joystick): 🪲️修复在RTL布局下摇杆渲染位置错误的问题
LanRhyme 82e40b2
feat(joystick): 添加将左半屏摇杆组件改为半屏摇杆组件,实现左右半屏切换
LanRhyme ea8782b
feat(joystick): 扩展半屏摇杆功能支持左右半屏选择
LanRhyme File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -174,6 +174,139 @@ fun StyleableJoystick( | |
| ) | ||
| } | ||
|
|
||
| /** | ||
| * 无状态的摇杆样式组件,由外部提供摇杆偏移位置 | ||
| */ | ||
| @Composable | ||
| fun StatelessStyleableJoystick( | ||
| modifier: Modifier = Modifier, | ||
| isDarkTheme: Boolean = isLauncherInDarkTheme(), | ||
| style: ObservableJoystickStyle, | ||
| size: Dp = 120.dp, | ||
| joystickOffset: () -> Offset, // 摇杆相对于中心的偏移 | ||
| isLocked: Boolean = false, | ||
| internalCanLock: Boolean = false | ||
| ) { | ||
| val theme = if (isDarkTheme) style.darkStyle else style.lightStyle | ||
|
|
||
| val backgroundShape = remember(theme.backgroundShape) { | ||
| RoundedCornerShape(percent = theme.backgroundShape) | ||
| } | ||
|
|
||
| val joystickShape = remember(theme.joystickShape) { | ||
| RoundedCornerShape(percent = theme.joystickShape) | ||
| } | ||
|
|
||
| val borderWidthRatio = remember(theme.borderWidthRatio) { | ||
| (theme.borderWidthRatio.toFloat() / 100f).coerceIn(0.0f, 0.5f) | ||
| } | ||
|
|
||
| StatelessJoystick( | ||
| modifier = modifier, | ||
| alpha = theme.alpha, | ||
| backgroundColor = theme.backgroundColor, | ||
| joystickColor = theme.joystickColor, | ||
| joystickCanLockColor = theme.joystickCanLockColor, | ||
| joystickLockedColor = theme.joystickLockedColor, | ||
| lockMarkColor = theme.lockMarkColor, | ||
| borderColor = theme.borderColor, | ||
| borderWidthRatio = borderWidthRatio, | ||
| backgroundShape = backgroundShape, | ||
| joystickShape = joystickShape, | ||
| size = size, | ||
| joystickSize = theme.joystickSize, | ||
| joystickOffset = joystickOffset, | ||
| isLocked = isLocked, | ||
| internalCanLock = internalCanLock | ||
| ) | ||
| } | ||
|
|
||
| /** | ||
| * 无状态移动摇杆控件 | ||
| */ | ||
| @Composable | ||
| fun StatelessJoystick( | ||
| modifier: Modifier = Modifier, | ||
| @FloatRange(from = 0.0, to = 1.0) | ||
| alpha: Float = 1.0f, | ||
| backgroundColor: Color = Color.Black.copy(alpha = 0.5f), | ||
| joystickColor: Color = Color.White.copy(alpha = 0.5f), | ||
| joystickCanLockColor: Color = Color.Yellow.copy(alpha = 0.5f), | ||
| joystickLockedColor: Color = Color.Green.copy(alpha = 0.5f), | ||
| lockMarkColor: Color = Color.White, | ||
| borderColor: Color = Color.White, | ||
| @FloatRange(from = 0.0, to = 0.5) | ||
| borderWidthRatio: Float = 0f, | ||
| backgroundShape: Shape = CircleShape, | ||
| joystickShape: Shape = CircleShape, | ||
| size: Dp = 120.dp, | ||
| @FloatRange(from = 0.0, to = 1.0) | ||
| joystickSize: Float = 0.5f, | ||
| joystickOffset: () -> Offset, | ||
| isLocked: Boolean = false, | ||
| internalCanLock: Boolean = false | ||
| ) { | ||
| val density = LocalDensity.current | ||
| val layoutDirection = LocalLayoutDirection.current | ||
|
|
||
| // 颜色处理 | ||
| val currentBackgroundColor = remember(backgroundColor, alpha) { backgroundColor.applyAlpha(alpha) } | ||
| val currentJoystickColor = remember(joystickColor, alpha) { joystickColor.applyAlpha(alpha) } | ||
| val currentJoystickCanLockColor = remember(joystickCanLockColor, alpha) { joystickCanLockColor.applyAlpha(alpha) } | ||
| val currentJoystickLockedColor = remember(joystickLockedColor, alpha) { joystickLockedColor.applyAlpha(alpha) } | ||
| val currentLockMarkColor = remember(lockMarkColor, alpha) { lockMarkColor.applyAlpha(alpha) } | ||
| val currentBorderColor = remember(borderColor, alpha) { borderColor.applyAlpha(alpha) } | ||
|
|
||
| // 计算尺寸 | ||
| val backgroundSizePx = with(density) { size.toPx() } | ||
| val joystickSizePx = backgroundSizePx * joystickSize.coerceIn(0.0f, 1.0f) | ||
| val centerPoint = Offset(backgroundSizePx / 2, backgroundSizePx / 2) | ||
| val lockPosition = Offset(centerPoint.x, 0f) | ||
|
|
||
| // 计算摇杆当前的绝对位置供 Canvas 绘制 | ||
| // 延迟到 Draw 阶段计算,避免重组 | ||
| // val joystickPosition = centerPoint + joystickOffset | ||
|
|
||
| Canvas( | ||
| modifier = modifier | ||
| .size(size) | ||
| ) { | ||
| val minSide = minOf([email protected], [email protected]) | ||
|
|
||
| // 背景层 | ||
| drawBackgroundLayer( | ||
| layoutDirection = layoutDirection, | ||
| size = [email protected], | ||
| shape = backgroundShape, | ||
| backgroundColor = currentBackgroundColor, | ||
| borderColor = currentBorderColor, | ||
| borderWidthPx = (minSide * borderWidthRatio).coerceAtLeast(0f) | ||
| ) | ||
|
|
||
| // 摇杆层 | ||
| drawJoystick( | ||
| layoutDirection = layoutDirection, | ||
| color = when { | ||
| isLocked -> currentJoystickLockedColor | ||
| internalCanLock -> currentJoystickCanLockColor | ||
| else -> currentJoystickColor | ||
| }, | ||
| center = centerPoint + joystickOffset(), | ||
| size = joystickSizePx, | ||
| shape = joystickShape | ||
| ) | ||
|
|
||
| // 锁定标记 | ||
| if (isLocked) { | ||
| drawCircle( | ||
| color = currentLockMarkColor, | ||
| center = lockPosition, | ||
| radius = 4f | ||
| ) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * 移动摇杆控件 | ||
| * @param alpha 整体不透明度 | ||
|
|
@@ -534,7 +667,7 @@ private fun DrawScope.drawJoystick( | |
| } | ||
| } | ||
|
|
||
| private fun updateJoystickPosition( | ||
| internal fun updateJoystickPosition( | ||
| newPosition: Offset, | ||
| minX: Float, | ||
| maxX: Float, | ||
|
|
@@ -551,7 +684,7 @@ private fun updateJoystickPosition( | |
| /** | ||
| * 计算摇杆方向 | ||
| */ | ||
| private fun calculateDirection( | ||
| internal fun calculateDirection( | ||
LanRhyme marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| joystickPosition: Offset, | ||
| backgroundCenter: Offset, | ||
| deadZoneRadius: Float | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.