-
Notifications
You must be signed in to change notification settings - Fork 94
/
ReactPixiFiberComponent.js
193 lines (177 loc) · 6.33 KB
/
ReactPixiFiberComponent.js
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
// Based on: https://github.com/facebook/react/blob/27535e7bfcb63e8a4d65f273311e380b4ca12eff/packages/react-dom/src/client/ReactDOMFiberComponent.js
import invariant from "fbjs/lib/invariant";
import * as PIXI from "pixi.js";
import { CHILDREN } from "./props";
import { TYPES } from "./types";
import { createInjectedTypeInstance, isInjectedType } from "./inject";
import { setValueForProperty } from "./PixiPropertyOperations";
export function createInstance(type, props, rootContainer, hostContext, internalHandle) {
let instance;
switch (type) {
case TYPES.BITMAP_TEXT:
const style =
typeof props.style !== "undefined"
? props.style
: {
align: props.align,
font: props.font,
tint: props.tint,
};
try {
instance = new PIXI.extras.BitmapText(props.text, style);
} catch (e) {
instance = new PIXI.BitmapText(props.text, style);
}
break;
case TYPES.CONTAINER:
instance = new PIXI.Container();
break;
case TYPES.GRAPHICS:
instance = new PIXI.Graphics();
break;
case TYPES.NINE_SLICE_PLANE:
try {
instance = new PIXI.mesh.NineSlicePlane(
props.texture,
props.leftWidth,
props.topHeight,
props.rightWidth,
props.bottomHeight
);
} catch (e) {
instance = new PIXI.NineSlicePlane(
props.texture,
props.leftWidth,
props.topHeight,
props.rightWidth,
props.bottomHeight
);
}
break;
case TYPES.PARTICLE_CONTAINER:
try {
instance = new PIXI.particles.ParticleContainer(
props.maxSize,
props.properties,
props.batchSize,
props.autoResize
);
} catch (e) {
instance = new PIXI.ParticleContainer(props.maxSize, props.properties, props.batchSize, props.autoResize);
}
break;
case TYPES.SPRITE:
instance = new PIXI.Sprite(props.texture);
break;
case TYPES.TEXT:
instance = new PIXI.Text(props.text, props.style, props.canvas);
break;
case TYPES.TILING_SPRITE:
try {
instance = new PIXI.extras.TilingSprite(props.texture, props.width, props.height);
} catch (e) {
instance = new PIXI.TilingSprite(props.texture, props.width, props.height);
}
break;
default:
instance = createInjectedTypeInstance(
type,
props,
rootContainer,
hostContext,
internalHandle,
applyDisplayObjectProps
);
break;
}
invariant(instance, "ReactPixiFiber does not support the type: `%s`.", type);
return instance;
}
export function setInitialCustomComponentProperties(type, instance, rawProps, rootContainer, hostContext) {
instance._customApplyProps(instance, undefined, rawProps);
}
export function setInitialPixiProperties(type, instance, rawProps, rootContainer, hostContext) {
for (const propKey in rawProps) {
if (!rawProps.hasOwnProperty(propKey)) {
continue;
}
const nextProp = rawProps[propKey];
if (propKey === CHILDREN) {
// Noop. Text children not supported
} else {
setValueForProperty(type, instance, propKey, nextProp);
}
}
}
export function setInitialProperties(type, instance, rawProps, rootContainer, hostContext) {
// injected types with customApplyProps need to have full control over passed props
if (isInjectedType(type) && typeof instance._customApplyProps === "function") {
setInitialCustomComponentProperties(type, instance, rawProps, rootContainer, hostContext);
return;
}
setInitialPixiProperties(type, instance, rawProps, rootContainer, hostContext);
}
// Calculate the diff between the two objects.
// See: https://github.com/facebook/react/blob/97e2911/packages/react-dom/src/client/ReactDOMFiberComponent.js#L546
export function diffProperties(type, instance, lastRawProps, nextRawProps) {
let updatePayload = null;
let lastProps = lastRawProps;
let nextProps = nextRawProps;
let propKey;
for (propKey in lastProps) {
if (nextProps.hasOwnProperty(propKey) || !lastProps.hasOwnProperty(propKey)) {
continue;
}
if (propKey === CHILDREN) {
// Noop. Text children not supported
} else {
// For all other deleted properties we add it to the queue. We use
// the whitelist in the commit phase instead.
(updatePayload = updatePayload || []).push(propKey, null);
}
}
for (propKey in nextProps) {
const nextProp = nextProps[propKey];
const lastProp = lastProps != null ? lastProps[propKey] : undefined;
if (!nextProps.hasOwnProperty(propKey) || nextProp === lastProp) {
continue;
}
if (propKey === CHILDREN) {
// Noop. Text children not supported
} else {
// For any other property we always add it to the queue and then we
// filter it out using the whitelist during the commit.
(updatePayload = updatePayload || []).push(propKey, nextProp);
}
}
return updatePayload;
}
export function applyDisplayObjectProps(type, instance, oldProps, newProps) {
const updatePayload = diffProperties(type, instance, oldProps, newProps);
if (updatePayload !== null) {
updatePixiProperties(type, instance, updatePayload);
}
}
export function updateCustomComponentProperties(type, instance, updatePayload, prevProps, nextProps, internalHandle) {
instance._customApplyProps(instance, prevProps, nextProps);
}
export function updatePixiProperties(type, instance, updatePayload, prevProps, nextProps, internalHandle) {
for (let i = 0; i < updatePayload.length; i += 2) {
const propKey = updatePayload[i];
const propValue = updatePayload[i + 1];
if (propKey === CHILDREN) {
// Noop. Text children not supported
} else {
setValueForProperty(type, instance, propKey, propValue, internalHandle);
}
}
}
// Apply the diff.
export function updateProperties(type, instance, updatePayload, prevProps, nextProps, internalHandle) {
// injected types with customApplyProps need to have full control over passed props
if (isInjectedType(type) && typeof instance._customApplyProps === "function") {
updateCustomComponentProperties(type, instance, updatePayload, prevProps, nextProps, internalHandle);
return;
}
updatePixiProperties(type, instance, updatePayload, prevProps, nextProps, internalHandle);
}