react-native-spritestudio-ui 0.0.0-stage → 7.0.0-beta.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,3 @@
1
+ export { SpriteStudioButton, type SpriteStudioButtonProps } from './SpriteStudioButton.js';
2
+ export { SsUiStateDriver, ssUiStateOf, type SsUiClip, type SsUiClips, type SsUiSection, type SsUiState, } from './uiState.js';
3
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,kBAAkB,EAAE,KAAK,uBAAuB,EAAE,MAAM,yBAAsB,CAAA;AACvF,OAAO,EACL,eAAe,EACf,WAAW,EACX,KAAK,QAAQ,EACb,KAAK,SAAS,EACd,KAAK,WAAW,EAChB,KAAK,SAAS,GACf,MAAM,cAAW,CAAA"}
@@ -0,0 +1 @@
1
+ {"type":"module"}
@@ -0,0 +1,114 @@
1
+ import type { SpriteStudioPlayer, SsLabelTable } from 'react-native-spritestudio';
2
+ /**
3
+ * A state a SpriteStudio button shows. By default each plays from the label of
4
+ * the same name in one animation, up to the frame before the next label.
5
+ */
6
+ export type SsUiState = 'normal' | 'hover' | 'pressed' | 'disabled' | 'focused';
7
+ /**
8
+ * The one state to show when several hold at once: `disabled` over `pressed`
9
+ * over `hover` over `focused`, and `normal` when none does.
10
+ */
11
+ export declare function ssUiStateOf(flags: {
12
+ disabled?: boolean;
13
+ pressed?: boolean;
14
+ hovered?: boolean;
15
+ focused?: boolean;
16
+ }): SsUiState;
17
+ /**
18
+ * What a state plays instead of the label named after it: the section of
19
+ * `animation` that starts at `label`.
20
+ *
21
+ * With only `animation`, the whole of it. With only `label`, that label in the
22
+ * animation the states are read from. A section runs to the frame before the
23
+ * next label, or to the end of the animation.
24
+ */
25
+ export type SsUiClip = {
26
+ animation: string;
27
+ label?: string;
28
+ } | {
29
+ animation?: string;
30
+ label: string;
31
+ };
32
+ /** Per-state replacements for the labels named after the states. */
33
+ export type SsUiClips = Partial<Record<SsUiState, SsUiClip>>;
34
+ export declare const SS_REDUCED_MOTION_LABEL = "reduced_motion";
35
+ /** A section a state plays, and whether to hold its first frame rather than play it. */
36
+ export interface SsUiSection {
37
+ animation: string;
38
+ startFrame: number;
39
+ endFrame: number;
40
+ hold: boolean;
41
+ }
42
+ /**
43
+ * The section starting at `label` in `table`: to the frame before the next
44
+ * label — any label, the state names or not — or to the end of the animation.
45
+ * Null when the table has no `label`.
46
+ */
47
+ export declare function labelSectionOf(table: SsLabelTable, label: string): {
48
+ startFrame: number;
49
+ endFrame: number;
50
+ } | null;
51
+ /**
52
+ * The section `state` plays in `animation`: the first of its chain that has a
53
+ * clip in `clips` or a label of its own name, or else the whole animation.
54
+ * Null when `tableOf` knows no `animation`.
55
+ *
56
+ * Under `reducedMotion` the section is held, except where it came from
57
+ * `normal` (or the whole animation `normal` falls back to) and `animation` has a
58
+ * `reduced_motion` label: that label's section plays instead.
59
+ *
60
+ * A clip that names an animation or a label the pack does not have is handed
61
+ * to `onMissingClip`, and the state goes on as if it had none.
62
+ */
63
+ export declare function resolveUiSection(state: SsUiState, options: {
64
+ animation: string;
65
+ tableOf: (animation: string) => SsLabelTable | null;
66
+ clips?: SsUiClips;
67
+ reducedMotion?: boolean;
68
+ onMissingClip?: (state: SsUiState, clip: SsUiClip) => void;
69
+ }): SsUiSection | null;
70
+ /**
71
+ * Plays the section an {@link SsUiState} calls for on a `player` the host owns.
72
+ *
73
+ * {@link show} switches at once and plays the new section from its first
74
+ * frame, looping; the section that was playing is dropped. Under reduced
75
+ * motion it holds the section's first frame instead, stopped, so no event
76
+ * fires — except that `normal`, and the states that fall back to it, play the
77
+ * `reduced_motion` label's section when the animation has one.
78
+ *
79
+ * It takes the player's playback over: it selects animations, sets sections
80
+ * and the loop count, and plays and stops.
81
+ */
82
+ export declare class SsUiStateDriver {
83
+ readonly player: SpriteStudioPlayer;
84
+ /**
85
+ * The animation the states' labels are read from. When omitted, the one the
86
+ * player has selected the first time a state is shown, or else the pack's
87
+ * first.
88
+ */
89
+ readonly animation: string | undefined;
90
+ readonly clips: SsUiClips;
91
+ private base;
92
+ private shownOn;
93
+ private shownAnimation;
94
+ private current;
95
+ private reduced;
96
+ private readonly reported;
97
+ constructor(player: SpriteStudioPlayer, options?: {
98
+ animation?: string;
99
+ clips?: SsUiClips;
100
+ });
101
+ /** The state last shown, or null before one has been. */
102
+ get state(): SsUiState | null;
103
+ /**
104
+ * Shows `state` — or nothing before the player has loaded a pack.
105
+ *
106
+ * Showing the state already shown, with the same `reducedMotion`, does
107
+ * nothing while the player still has the pack and the animation it was
108
+ * given, so it can be called on every change the player announces. After
109
+ * another load, or a switch made elsewhere, it shows the state again.
110
+ */
111
+ show(state: SsUiState, reducedMotion?: boolean): void;
112
+ private reportMissingClip;
113
+ }
114
+ //# sourceMappingURL=uiState.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"uiState.d.ts","sourceRoot":"","sources":["../../src/uiState.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,kBAAkB,EAAE,YAAY,EAAE,MAAM,2BAA2B,CAAA;AAEjF;;;GAGG;AACH,MAAM,MAAM,SAAS,GAAG,QAAQ,GAAG,OAAO,GAAG,SAAS,GAAG,UAAU,GAAG,SAAS,CAAA;AAE/E;;;GAGG;AACH,wBAAgB,WAAW,CAAC,KAAK,EAAE;IACjC,QAAQ,CAAC,EAAE,OAAO,CAAA;IAClB,OAAO,CAAC,EAAE,OAAO,CAAA;IACjB,OAAO,CAAC,EAAE,OAAO,CAAA;IACjB,OAAO,CAAC,EAAE,OAAO,CAAA;CAClB,GAAG,SAAS,CAMZ;AAUD;;;;;;;GAOG;AACH,MAAM,MAAM,QAAQ,GAAG;IAAE,SAAS,EAAE,MAAM,CAAC;IAAC,KAAK,CAAC,EAAE,MAAM,CAAA;CAAE,GAAG;IAAE,SAAS,CAAC,EAAE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;IAC5C,OAAO,CAAC,OAAO,CAAyB;IACxC,OAAO,CAAC,OAAO,CAAQ;IACvB,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAoB;IAE7C,YACW,MAAM,EAAE,kBAAkB,EACnC,OAAO,GAAE;QAAE,SAAS,CAAC,EAAE,MAAM,CAAC;QAAC,KAAK,CAAC,EAAE,SAAS,CAAA;KAAO,EAIxD;IAED,yDAAyD;IACzD,IAAI,KAAK,IAAI,SAAS,GAAG,IAAI,CAE5B;IAED;;;;;;;OAOG;IACH,IAAI,CAAC,KAAK,EAAE,SAAS,EAAE,aAAa,UAAQ,GAAG,IAAI,CA+ClD;IAED,OAAO,CAAC,iBAAiB;CAe1B"}
package/package.json CHANGED
@@ -1,6 +1,69 @@
1
1
  {
2
2
  "name": "react-native-spritestudio-ui",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
3
+ "version": "7.0.0-beta.1",
4
+ "description": "SpriteStudio Player for React Native, as UI: a SpriteStudio animation as a button whose states play its labels, pressable on its parts.",
5
+ "license": "BSD-3-Clause",
6
+ "main": "lib/module/index.js",
7
+ "types": "lib/typescript/index.d.ts",
8
+ "module": "lib/module/index.js",
9
+ "react-native": "src/index.ts",
10
+ "source": "src/index.ts",
11
+ "files": [
12
+ "src",
13
+ "lib",
14
+ "LICENSE.ja.md"
15
+ ],
16
+ "peerDependencies": {
17
+ "react": "^19.3.0",
18
+ "react-native": "^0.88.0",
19
+ "react-native-spritestudio": "7.0.0-beta.1"
20
+ },
21
+ "devDependencies": {
22
+ "@types/react": "^19.3.0",
23
+ "@types/react-test-renderer": "^19.3.1",
24
+ "react-native-builder-bob": "^0.44.1",
25
+ "react-native-spritestudio": "7.0.0-beta.1",
26
+ "react-test-renderer": "19.3.0",
27
+ "typescript": "^7.0.2",
28
+ "vitest": "^5.1.0"
29
+ },
30
+ "repository": {
31
+ "type": "git",
32
+ "url": "git+https://github.com/cri-middleware/SSPlayerForReactNative.git",
33
+ "directory": "packages/react-native-spritestudio-ui"
34
+ },
35
+ "keywords": [
36
+ "spritestudio",
37
+ "spritestudio7",
38
+ "animation",
39
+ "2d",
40
+ "ui",
41
+ "button",
42
+ "accessibility",
43
+ "react-native",
44
+ "expo"
45
+ ],
46
+ "react-native-builder-bob": {
47
+ "source": "src",
48
+ "output": "lib",
49
+ "targets": [
50
+ [
51
+ "module",
52
+ {
53
+ "esm": true
54
+ }
55
+ ],
56
+ [
57
+ "typescript",
58
+ {
59
+ "project": "tsconfig.build.json"
60
+ }
61
+ ]
62
+ ]
63
+ },
64
+ "scripts": {
65
+ "typecheck": "tsc --noEmit -p tsconfig.test.json",
66
+ "test": "vitest run",
67
+ "build": "bob build"
68
+ }
6
69
  }
@@ -0,0 +1,272 @@
1
+ import { forwardRef, useEffect, useMemo, useRef, useState, type ReactElement } from 'react'
2
+ import {
3
+ AccessibilityInfo,
4
+ Pressable,
5
+ StyleSheet,
6
+ View,
7
+ type GestureResponderEvent,
8
+ type PressableProps,
9
+ } from 'react-native'
10
+ import {
11
+ SpriteStudioView,
12
+ useSsViewPlayer,
13
+ type SpriteStudioViewProps,
14
+ type SpriteStudioViewRef,
15
+ } from 'react-native-spritestudio'
16
+
17
+ import { SsUiStateDriver, ssUiStateOf, type SsUiClips } from './uiState'
18
+
19
+ type SsButtonViewProps = Pick<
20
+ SpriteStudioViewProps,
21
+ | 'source'
22
+ | 'textures'
23
+ | 'animation'
24
+ | 'speed'
25
+ | 'fit'
26
+ | 'frame'
27
+ | 'frameScope'
28
+ | 'measureFrames'
29
+ | 'parts'
30
+ | 'onUserData'
31
+ | 'onAudio'
32
+ | 'onSignal'
33
+ | 'onLoad'
34
+ | 'onError'
35
+ | 'onAnimationFinished'
36
+ | 'onAnimationLooped'
37
+ | 'onAnimationStarted'
38
+ | 'onAnimationChanged'
39
+ | 'align'
40
+ | 'flipX'
41
+ | 'flipY'
42
+ | 'offset'
43
+ | 'scale'
44
+ | 'style'
45
+ | 'placeholder'
46
+ >
47
+
48
+ const VIEW_PROPS: Record<keyof SsButtonViewProps, true> = {
49
+ source: true,
50
+ textures: true,
51
+ animation: true,
52
+ speed: true,
53
+ fit: true,
54
+ frame: true,
55
+ frameScope: true,
56
+ measureFrames: true,
57
+ parts: true,
58
+ onUserData: true,
59
+ onAudio: true,
60
+ onSignal: true,
61
+ onLoad: true,
62
+ onError: true,
63
+ onAnimationFinished: true,
64
+ onAnimationLooped: true,
65
+ onAnimationStarted: true,
66
+ onAnimationChanged: true,
67
+ align: true,
68
+ flipX: true,
69
+ flipY: true,
70
+ offset: true,
71
+ scale: true,
72
+ style: true,
73
+ placeholder: true,
74
+ }
75
+
76
+ /**
77
+ * The props of a `<SpriteStudioView>` that still mean something on a button —
78
+ * `animation` is where the states' labels are read from — and those of a
79
+ * `<Pressable>`, which the button is. The playback props are the button's.
80
+ */
81
+ export interface SpriteStudioButtonProps
82
+ extends SsButtonViewProps,
83
+ Omit<PressableProps, 'style' | 'children'> {
84
+ /** Per-state replacements for the labels named after the states. */
85
+ clips?: SsUiClips
86
+ /**
87
+ * The parts a press counts on, by name or index: a touch that starts on
88
+ * none of their outlines is not a press. Omitted, the whole box takes one.
89
+ * Only touches are narrowed — keyboard focus and a screen reader's
90
+ * activation reach the button as before.
91
+ */
92
+ hitParts?: ReadonlyArray<string | number>
93
+ /** With no `hitParts`, whether a press counts on any part with an outline rather than on the whole box. */
94
+ hitOnArtwork?: boolean
95
+ /** Whether to hold still. Omitted, it follows the system's reduce motion setting. */
96
+ reducedMotion?: boolean
97
+ }
98
+
99
+ function ownViewProps(props: SpriteStudioButtonProps): {
100
+ view: SsButtonViewProps
101
+ rest: Record<string, unknown>
102
+ } {
103
+ const view: Record<string, unknown> = {}
104
+ const rest: Record<string, unknown> = {}
105
+ for (const [key, value] of Object.entries(props)) {
106
+ if (key in VIEW_PROPS) view[key] = value
107
+ else rest[key] = value
108
+ }
109
+ return { view: view as SsButtonViewProps, rest }
110
+ }
111
+
112
+ function useSystemReducedMotion(active: boolean): boolean | null {
113
+ const [enabled, setEnabled] = useState<boolean | null>(null)
114
+ useEffect(() => {
115
+ if (!active) return
116
+ let live = true
117
+ AccessibilityInfo.isReduceMotionEnabled().then(
118
+ (value) => {
119
+ if (live) setEnabled(value)
120
+ },
121
+ () => {
122
+ if (live) setEnabled(false)
123
+ }
124
+ )
125
+ const subscription = AccessibilityInfo.addEventListener('reduceMotionChanged', setEnabled)
126
+ return () => {
127
+ live = false
128
+ subscription.remove()
129
+ }
130
+ }, [active])
131
+ return enabled
132
+ }
133
+
134
+ interface SurfaceProps extends Omit<PressableProps, 'style' | 'children'> {
135
+ animation?: string
136
+ clips?: SsUiClips
137
+ hitParts?: ReadonlyArray<string | number>
138
+ hitOnArtwork?: boolean
139
+ reducedMotion?: boolean
140
+ }
141
+
142
+ function ButtonSurface(props: SurfaceProps): ReactElement {
143
+ const {
144
+ animation,
145
+ clips,
146
+ hitParts,
147
+ hitOnArtwork = false,
148
+ reducedMotion,
149
+ disabled,
150
+ onPressIn,
151
+ onPressOut,
152
+ onHoverIn,
153
+ onHoverOut,
154
+ onFocus,
155
+ onBlur,
156
+ ...pressable
157
+ } = props
158
+ const player = useSsViewPlayer()
159
+ const [pressed, setPressed] = useState(false)
160
+ const [hovered, setHovered] = useState(false)
161
+ const [focused, setFocused] = useState(false)
162
+ const system = useSystemReducedMotion(reducedMotion == null)
163
+ const reduced = reducedMotion ?? system
164
+
165
+ useEffect(() => {
166
+ if (disabled) setPressed(false)
167
+ }, [disabled])
168
+
169
+ const clipsKey = clips == null ? '' : JSON.stringify(clips)
170
+ const driver = useMemo(
171
+ () => (player == null ? null : new SsUiStateDriver(player, { animation, clips })),
172
+ // eslint-disable-next-line react-hooks/exhaustive-deps
173
+ [player, animation, clipsKey]
174
+ )
175
+
176
+ const state = ssUiStateOf({ disabled: disabled ?? false, pressed, hovered, focused })
177
+ const shown = useRef({ driver, state, reduced })
178
+ shown.current = { driver, state, reduced }
179
+
180
+ useEffect(() => {
181
+ if (driver == null || reduced == null) return
182
+ driver.show(state, reduced)
183
+ }, [driver, state, reduced])
184
+
185
+ useEffect(() => {
186
+ if (player == null) return
187
+ return player.addListener(() => {
188
+ const { driver: current, state: now, reduced: still } = shown.current
189
+ if (current != null && still != null) current.show(now, still)
190
+ })
191
+ }, [player])
192
+
193
+ const narrows = hitParts != null || hitOnArtwork
194
+ const refuse = (event: GestureResponderEvent): boolean => {
195
+ if (player == null) return false
196
+ const { locationX, locationY } = event.nativeEvent
197
+ return player.hitTestPart(locationX, locationY, hitParts) < 0
198
+ }
199
+
200
+ const labelled =
201
+ pressable.accessibilityLabel != null || pressable['aria-label'] != null
202
+ const accessibility = labelled
203
+ ? { accessibilityRole: 'button' as const }
204
+ : { accessible: false, importantForAccessibility: 'no-hide-descendants' as const }
205
+
206
+ return (
207
+ <View
208
+ style={StyleSheet.absoluteFill}
209
+ onStartShouldSetResponderCapture={narrows ? refuse : undefined}
210
+ >
211
+ <Pressable
212
+ {...accessibility}
213
+ {...pressable}
214
+ style={StyleSheet.absoluteFill}
215
+ disabled={disabled}
216
+ onPressIn={(event) => {
217
+ setPressed(true)
218
+ onPressIn?.(event)
219
+ }}
220
+ onPressOut={(event) => {
221
+ setPressed(false)
222
+ onPressOut?.(event)
223
+ }}
224
+ onHoverIn={(event) => {
225
+ setHovered(true)
226
+ onHoverIn?.(event)
227
+ }}
228
+ onHoverOut={(event) => {
229
+ setHovered(false)
230
+ onHoverOut?.(event)
231
+ }}
232
+ onFocus={(event) => {
233
+ setFocused(true)
234
+ onFocus?.(event)
235
+ }}
236
+ onBlur={(event) => {
237
+ setFocused(false)
238
+ onBlur?.(event)
239
+ }}
240
+ />
241
+ </View>
242
+ )
243
+ }
244
+
245
+ /**
246
+ * A `<SpriteStudioView>` that is a button: a `<Pressable>` over the view's box
247
+ * whose states each play a section of one animation.
248
+ *
249
+ * Pressed, hovered, focused and `disabled` map to `pressed`, `hover`, `focused`
250
+ * and `disabled`, one at a time in that order of precedence, and none of them
251
+ * to `normal`. Each state plays, looping, from the label of its name to the
252
+ * frame before the next label; a state without a label falls back — `pressed`
253
+ * to `hover`, the rest to `normal` — and with no labels the whole animation
254
+ * plays. Under reduced motion each state holds its first frame, and `normal`
255
+ * plays a `reduced_motion` label instead when the animation has one.
256
+ *
257
+ * Without an `accessibilityLabel` it is decoration to a screen reader; with
258
+ * one it is read as a button.
259
+ *
260
+ * It takes its player's playback over: `playing`, `loop` and `transitionMs`
261
+ * are not props here, and the player from the ref is not one to play or stop.
262
+ */
263
+ export const SpriteStudioButton = forwardRef<SpriteStudioViewRef, SpriteStudioButtonProps>(
264
+ function SpriteStudioButton(props, ref) {
265
+ const { view, rest } = ownViewProps(props)
266
+ return (
267
+ <SpriteStudioView {...view} ref={ref} playing={false}>
268
+ <ButtonSurface {...(rest as SurfaceProps)} animation={view.animation} />
269
+ </SpriteStudioView>
270
+ )
271
+ }
272
+ )
package/src/index.ts ADDED
@@ -0,0 +1,9 @@
1
+ export { SpriteStudioButton, type SpriteStudioButtonProps } from './SpriteStudioButton'
2
+ export {
3
+ SsUiStateDriver,
4
+ ssUiStateOf,
5
+ type SsUiClip,
6
+ type SsUiClips,
7
+ type SsUiSection,
8
+ type SsUiState,
9
+ } from './uiState'