-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
159 additions
and
13 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<script setup lang="ts"> | ||
import { ref } from 'vue' | ||
import { getJoysticks, Joystick, JoystickArea } from '@manapotion/vue' | ||
const props = defineProps<{ mode: 'follow' | 'origin' }>() | ||
const joystickCurrent = ref<HTMLDivElement | null>(null) | ||
const joystickOrigin = ref<HTMLDivElement | null>(null) | ||
const joystickFollow = ref<HTMLDivElement | null>(null) | ||
const handleStart = (joystick: Joystick) => { | ||
joystickCurrent.value!.style.transform = `translate(${joystick.current.x}px, ${-joystick.current.y!}px)` | ||
joystickOrigin.value!.style.transform = `translate(${joystick.origin.x}px, ${-joystick.origin.y!}px)` | ||
joystickFollow.value!.style.transform = `translate(${joystick.follow.x}px, ${-joystick.follow.y!}px)` | ||
joystickCurrent.value!.style.opacity = '1' | ||
props.mode === 'follow' && (joystickFollow.value!.style.opacity = '1') | ||
joystickOrigin.value!.style.opacity = '1' | ||
} | ||
const handleEnd = () => { | ||
joystickCurrent.value!.style.opacity = '0' | ||
joystickFollow.value!.style.opacity = '0' | ||
joystickOrigin.value!.style.opacity = '0' | ||
} | ||
const handleMove = (joystick: Joystick) => { | ||
joystickCurrent.value!.style.transform = `translate(${joystick.current.x}px, ${-joystick.current.y!}px)` | ||
joystickOrigin.value!.style.transform = `translate(${joystick.origin.x}px, ${-joystick.origin.y!}px)` | ||
joystickFollow.value!.style.transform = `translate(${joystick.follow.x}px, ${-joystick.follow.y!}px)` | ||
} | ||
</script> | ||
|
||
<template> | ||
<JoystickArea | ||
:joystick="getJoysticks().movement" | ||
:mode="props.mode" | ||
:container-props="{ class: `relative z-10 h-48 w-64 rounded-md border border-slate-500 ` }" | ||
@start="handleStart" | ||
@move="handleMove" | ||
@end="handleEnd" | ||
> | ||
<div | ||
ref="joystickCurrent" | ||
class="pointer-events-none absolute -bottom-6 -left-6 size-12 rounded-full bg-red-500 opacity-0 transition-opacity" | ||
/> | ||
<div | ||
ref="joystickOrigin" | ||
class="pointer-events-none absolute -bottom-6 -left-6 size-12 rounded-full bg-blue-500 opacity-0 transition-opacity" | ||
/> | ||
<div | ||
ref="joystickFollow" | ||
class="pointer-events-none absolute -bottom-6 -left-6 size-12 rounded-full bg-green-500 opacity-0 transition-opacity" | ||
/> | ||
</JoystickArea> | ||
</template> |
This file contains 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 |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import { defineComponent, h, onMounted, onUnmounted, ref, watch } from 'vue' | ||
|
||
import { Joystick, mountJoystickArea } from '@manapotion/core' | ||
|
||
export const JoystickArea = defineComponent({ | ||
name: 'JoystickArea', | ||
props: { | ||
mode: { | ||
type: String as () => 'follow' | 'origin', | ||
default: 'follow', | ||
}, | ||
joystick: { | ||
type: Object as () => Joystick, | ||
required: true, | ||
}, | ||
// eslint-disable-next-line vue/require-default-prop | ||
maxFollowDistance: { type: Number }, | ||
// eslint-disable-next-line vue/require-default-prop | ||
maxOriginDistance: { type: Number }, | ||
// eslint-disable-next-line vue/require-default-prop | ||
onEnd: { type: Function }, | ||
// eslint-disable-next-line vue/require-default-prop | ||
onMove: { type: Function }, | ||
// eslint-disable-next-line vue/require-default-prop | ||
onStart: { type: Function }, | ||
// eslint-disable-next-line vue/require-default-prop | ||
containerProps: { type: Object }, | ||
}, | ||
setup(props, { slots, attrs }) { | ||
const localRef = ref<HTMLDivElement | null>(null) | ||
|
||
onMounted(() => { | ||
let unsub = mountJoystickArea({ | ||
mode: props.mode, | ||
joystick: props.joystick, | ||
maxFollowDistance: props.maxFollowDistance, | ||
maxOriginDistance: props.maxOriginDistance, | ||
onEnd: props.onEnd as (joystick: Joystick) => void, | ||
onMove: props.onMove as (joystick: Joystick) => void, | ||
onStart: props.onStart as (joystick: Joystick) => void, | ||
element: localRef.value!, | ||
}) | ||
// TODO: watch for changes in other props? joystick, maxFollowDistance, maxOriginDistance | ||
watch( | ||
() => props.mode, | ||
newMode => { | ||
unsub() | ||
unsub = mountJoystickArea({ | ||
mode: newMode, | ||
joystick: props.joystick, | ||
maxFollowDistance: props.maxFollowDistance, | ||
maxOriginDistance: props.maxOriginDistance, | ||
onEnd: props.onEnd as (joystick: Joystick) => void, | ||
onMove: props.onMove as (joystick: Joystick) => void, | ||
onStart: props.onStart as (joystick: Joystick) => void, | ||
element: localRef.value!, | ||
}) | ||
}, | ||
) | ||
onUnmounted(() => { | ||
unsub() | ||
}) | ||
}) | ||
|
||
return () => | ||
h( | ||
'div', | ||
{ | ||
...props.containerProps, | ||
ref: localRef, | ||
...attrs, | ||
}, | ||
slots.default ? slots.default() : [], | ||
) | ||
}, | ||
}) |
This file contains 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