-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSpace.js
453 lines (408 loc) · 15.2 KB
/
Space.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
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
import React from 'react';
import {
Platform,
StyleSheet,
Text,
View,
PanResponder,
Animated,
TextInput,
PixelRatio
} from 'react-native';
import SpaceStyles from "./SpaceStyle";
export default class Space extends React.Component {
constructor(props) {
super(props);
this.state = {
x:this.props.x || 0,
y:this.props.y || 0,
width: 0,
height: 0,
rows: [],
lines: [],
pan:new Animated.ValueXY(),
d:new Animated.ValueXY({x:this.props.x||0, y:this.props.y||0}),
unitsize : this.props.unitsize || 64,
memoryComps:[],
activeComps:[],
_compId:0,
zoomFactor:new Animated.Value(1)
}
this.handleInit = this.handleInit.bind(this);
this.handleMove = this.handleMove.bind(this);
this._getBoundary = this._getBoundary.bind(this);
this._initComponents = this._initComponents.bind(this);
this._fixComponent = this._fixComponent.bind(this);
this.getPosition = this.getPosition.bind(this);
this.addItem = this.addItem.bind(this);
}
/* MOUNT : Gets touch events */
componentWillMount() {
this.state.d.addListener((value) => this.handleMove(value));
this._panResponder = PanResponder.create({
/*onStartShouldSetPanResponderCapture: (evt, gestureState) => true,
onStartShouldSetPanResponder: (e, gesture) => true,*/
onMoveShouldSetPanResponderCapture: (evt, gestureState) => true,
onMoveShouldSetPanResponder: (e, gesture) => true,
onPanResponderMove: (e, gesture) => {
if (this.state.zoomFactor.__getValue()===1) {
var x = this.state.x;
var y = this.state.y;
var newx = x-gesture.dx;
var newy = y-gesture.dy;
var newgx = -newx;
var newgy = -newy;
if (newgx<-this.state.unitsize || newgx>=this.state.unitsize) {
newgx = (Math.floor(newx/this.state.unitsize)-newx/this.state.unitsize)*this.state.unitsize+this.state.unitsize*2.0;
}
if (gesture.dy<-this.state.unitsize || gesture.dy>=this.state.unitsize) {
newgy = (Math.floor(newy/this.state.unitsize)-newy/this.state.unitsize)*this.state.unitsize+this.state.unitsize*2.0;
}
Animated.event([
null,
{ dx: this.state.pan.x, dy: this.state.pan.y, wholedx:this.state.d.x, wholedy:this.state.d.y },
])(e, {dx:newgx, dy:newgy, wholedx:newx, wholedy:newy});
}
/*var x = this.state.x;
var y = this.state.y;
this.setState({x:x-gesture.dx, y:y-gesture.dy});*/
return true
},
onPanResponderRelease: () => {
this.setState({x: this.state.d.x.__getValue(), y:this.state.d.y.__getValue() })
}
})
}
/* INIT : already rendered, gets informations when rendered*/
handleInit(event) {
var xwidth = event.nativeEvent.layout.width;
var yheight = event.nativeEvent.layout.height;
var xpos = this.props.x ;
var ypos = this.props.y ;
if (this.props.onInitial) {
if (typeof this.props.onInitial == 'function') {
try {
var b = this._getBoundary(xpos,ypos,xpos+xwidth, ypos+yheight);
var l = this._initComponents(b);
} catch (e) {
console.error(e);
var l = []
}
}
} else {
var l = []
}
/* Calculate the grid's position */
var numberoflines = Math.floor(xwidth/this.state.unitsize)+4;
var lines = [];
for (i=0; i<numberoflines; i++ ) {
var linepos = (i-2)*this.state.unitsize-this.state.unitsize - this.state.unitsize/2;
lines.push({x: linepos, id:i});
};
var numberofrows = Math.floor(yheight/this.state.unitsize)+4;
var rows = [];
for (i=0; i<numberofrows; i++ ) {
rows.push({y: (i-2)*this.state.unitsize-this.state.unitsize-this.state.unitsize/2, id:i});
};
this.setState({
x:xpos-xwidth/2,
y:ypos-yheight/2,
width: xwidth,
height: yheight,
rows: rows,
lines:lines,
d:new Animated.ValueXY({x:xpos-xwidth/2, y:ypos-yheight/2}), // change d position to the right one initially
})
}
/* EVENT : triggers update event */
async handleMove(e) {
/*this.setState({x:e.x, y:e.y})*/
boundarybox = this._getBoundary(e.x, e.y, e.x+this.state.width, e.y+this.state.height);
/*TODO : ADD ERROR HANDLING */
if (typeof this.props.onUpdate == 'function') {
try {
this.props.onUpdate(boundarybox);
} catch (e) {
console.log(e);
}
}
}
/* COMPONENT'S FUNCTIONS : using 'ref' to get/post information */
getPosition() {
return {x: this.state.x+this.state.width/2, y: this.state.y+this.state.height/2}
}
addItem(item){
var newitem = this._fixComponent(item);
var newmemorycomps = this.state.memoryComps;
var newactivecomps = this.state.activeComps;
var uids=[];
for (var i = 0; i < newmemorycomps.length; i++) {
if (newmemorycomps.uid) {
uids.push(newmemorycomps.uid);
}
}
if (uids.indexOf(newitem.uid)<0) {
newmemorycomps.push(newitem);
newactivecomps.push(newitem);
this.setState({memoryComps:newmemorycomps, activeComps:newactivecomps})
return {
"status":"ok",
}
} else {
return {
"status":"error",
"message":"Item already exists"
}
}
}
removeItem(uid) {
var newmemorycomps = [];
var newactivecomps = [];
for (var i = 0; i < this.state.memoryComps.length; i++) {
if (this.state.memoryComps[i].uid !== uid) {
newmemorycomps.push(this.state.memoryComps[i]);
newactivecomps.push(this.state.memoryComps[i]);
}
}
this.setState({memoryComps:newmemorycomps, activeComps:newactivecomps})
return {
"status":"removed",
}
}
Zoom(val){
Animated.timing(
this.state.zoomFactor,
{
toValue: val,
duration: 400,
}
).start();
}
deZoom(){
Animated.timing(
this.state.zoomFactor,
{
toValue: 1,
duration: 300,
}
).start();
}
/* INTERNAL INFO : boundary box */
_getBoundary(left, top, right, bottom) {
const bmargin = this.props.boundaryMargin || 64;
return {
left:left-bmargin,
top:top-bmargin,
right:right+bmargin,
bottom:bottom+bmargin
}
}
/* INTERNAL ACTION : fix a new component */
_fixComponent(c) {
var newc = c;
if (!c.height) {
newc.height = 100
}
if (!c.width) {
newc.width = 100
}
if (!c.x) {
newc.x = 100
}
if (!c.y) {
newc.y = 100
}
if (!c.uid) {
newc.uid = 0;
console.warn('Space : item should have a unique ID, please ad a `uid` parameter to your item ')
}
if (!c.component) {
newc.component = <ComponentTest1/> // TODO : REPLACE WITH PLACEHOLDER COMP, NOT TESTING COMP
}
newc.x = Number(newc.x);
newc.y = Number(newc.y);
newc.width = Number(newc.width);
newc.height = Number(newc.height);
return newc;
}
/* INTERNAL ACTION: select components that should be rendered */
selectActiveComponents() {
// TODO
}
/* INTERNAL ACTION : convert initial components to the right stuff [especially add a unique id] */
async _initComponents(boundary) {
var clist = await this.props.onInitial(boundary);
var nclist = [];
for (var i = 0; i < clist.length; i++) {
var n = clist[i];
var nfix = this._fixComponent(n);
nclist.push(nfix);
}
this.setState({
activeComps:nclist,
memoryComps:nclist
})
return nclist;
}
/* SPECIFIC INFO : gets the grid's line's styles [needs a cleaner aproach?] */
getLineStyle(i,linesize, rowsize) {
const pixelvalue = PixelRatio.getPixelSizeForLayoutSize(this.state.unitsize);
if (linesize===0) {
if ( rowsize<this.state.height/2 ) {
var diff = Animated.add(this.state.height/2, Animated.multiply(-1, rowsize));
var diffmult = Animated.multiply(this.state.zoomFactor, diff);
var zoomAdd1 = Animated.add(diff, Animated.multiply(-1, diffmult));
// var zoomAdd1 = Animated.multiply(Animated.multiply(this.state.zoomFactor, -this.state.unitsize), -(rowsize-this.state.width/2)/pixelvalue );
} else if ( rowsize>=this.state.height/2 ) {
var diff = Animated.add(rowsize, Animated.multiply(-1, this.state.height/2));
var diffmult = Animated.multiply(this.state.zoomFactor, diff);
var zoomAdd1 = Animated.add(diffmult, Animated.multiply(-1, diff));
// var zoomAdd1 = Animated.multiply(Animated.multiply(this.state.zoomFactor, this.state.unitsize), (rowsize-this.state.width/2)/pixelvalue );
}
var top = rowsize;
var left = 0;
var width = "100%";
var height = 1;
var translateX = 0;
var translateY = Animated.add(this.state.pan.y, zoomAdd1);
} else if (rowsize===0) {
if (linesize<this.state.width/2 ) {
var diff = Animated.add(this.state.width/2, Animated.multiply(-1, linesize));
var diffmult = Animated.multiply(this.state.zoomFactor, diff);
var zoomAdd2 = Animated.add(diff, Animated.multiply(-1, diffmult));
// var zoomAdd2 = Animated.multiply(Animated.multiply(this.state.zoomFactor, -this.state.unitsize), -(linesize-this.state.width/2)/pixelvalue );
} else if (linesize>this.state.width/2 ) {
var diff = Animated.add(linesize, Animated.multiply(-1, this.state.width/2));
var diffmult = Animated.multiply(this.state.zoomFactor, diff);
var zoomAdd2 = Animated.add(diffmult, Animated.multiply(-1, diff));
//var zoomAdd2 = Animated.multiply(Animated.multiply(this.state.zoomFactor, this.state.unitsize), (linesize-this.state.width/2)/pixelvalue );
}
var top = 0;
var left = linesize;
var height = "100%";
var width = 1;
var translateX = Animated.add(this.state.pan.x, zoomAdd2);
var translateY = 0;
}
if (this.props.theme) {
var style=this.props.theme;
} else {
var style = SpaceStyles;
}
return [
SpaceStyles._spaceLine,
{
backgroundColor:this.props.lineColor || "#9EADFF",
width:width,
height:height,
top: top,
left:left,
transform: [
{
translateX: translateX
},
{
translateY: translateY
}
]
}
];
}
render() {
/* CREATE ITEM LIST */
if (this.state.activeComps.length>0) {
var uids = []
var items = this.state.activeComps.map((item) => {
if (uids.indexOf(item.uid)<0) {
uids.push(item.uid);
var baseTopPosition = Animated.add(Animated.add(Animated.multiply(this.state.d.y,-1), item.y), Animated.multiply(-1, item.height/2));
if (baseTopPosition.__getValue()+item.height/2 < (this.state.height/2)) {
var diff = Animated.add(this.state.height/2, Animated.multiply(-1, baseTopPosition));
var diffmult = Animated.multiply(this.state.zoomFactor, diff);
var zoomAdd1 = Animated.add(diffmult, Animated.multiply(-1, diff));
// var addition = Animated.multiply(Animated.multiply( -pixelUnitValue , this.state.zoomFactor), Animated.multiply( Animated.divide(Animated.add(baseTopPosition, -this.state.height/2), pixelUnitValue ), -1) );
/*Animated.multiply(Animated.multiply(this.state.zoomFactor, -this.state.unitsize), -(linesize-this.state.width/2)/pixelvalue );*/
} else {
var diff = Animated.add(this.state.height/2, Animated.multiply(-1, baseTopPosition));
var diffmult = Animated.multiply(this.state.zoomFactor, diff);
var zoomAdd1 = Animated.add(diffmult, Animated.multiply(-1, diff));
// var addition = Animated.multiply(Animated.multiply( pixelUnitValue , this.state.zoomFactor), Animated.divide(Animated.add(baseTopPosition, -this.state.height/2), pixelUnitValue ) );
}
var topPosition = Animated.add(baseTopPosition, Animated.multiply(-1, zoomAdd1));
var baseLeftPosition = Animated.add(Animated.add(Animated.multiply(this.state.d.x,-1), item.x), Animated.multiply(-1, item.width/2));
if (baseLeftPosition.__getValue()+item.width/2 < this.state.width/2) {
var diff = Animated.add(baseLeftPosition, Animated.multiply(-1, this.state.width/2));
var diffmult = Animated.multiply(this.state.zoomFactor, diff);
var zoomAdd2 = Animated.add(diff, Animated.multiply(-1, diffmult));
//var addition2 = Animated.multiply(Animated.multiply( -pixelUnitValue , this.state.zoomFactor), Animated.multiply( Animated.divide(Animated.add(baseLeftPosition, -this.state.width/2), pixelUnitValue ), -1) );
} else {
var diff = Animated.add(this.state.width/2, Animated.multiply(-1, baseLeftPosition));
var diffmult = Animated.multiply(this.state.zoomFactor, diff);
var zoomAdd2 = Animated.add(diffmult, Animated.multiply(-1, diff));
//var addition2 = Animated.multiply(Animated.multiply( pixelUnitValue , this.state.zoomFactor), Animated.divide(Animated.add(baseLeftPosition, -this.state.width/2), pixelUnitValue ) );
}
var leftPosition =Animated.add(baseLeftPosition, Animated.multiply(-1, zoomAdd2));
return (
(<Animated.View
style={{position:"absolute",
top:topPosition,
left:leftPosition,
width:item.width,
height:item.height,
transform: [
{scaleX:this.state.zoomFactor},
{scaleY:this.state.zoomFactor}
]
}}
key={item.uid}
>
{item.component}
</Animated.View>)
)
}
});
} else {
var items = [];
}
return (
<View onLayout={(e) => this.handleInit(e)} style={[SpaceStyles._spaceView,{backgroundColor:this.props.backgroundColor || "#1E59E9"} ]} {...this._panResponder.panHandlers} >
{
this.state.lines &&
this.state.lines.map(line => <Animated.View style={this.getLineStyle(line.id, line.x, 0)} key={line.id}></Animated.View>)
}
{
this.state.rows &&
this.state.rows.map(row => <Animated.View style={this.getLineStyle(row.id, 0,row.y)} key={row.id}></Animated.View>)
}
{items}
</View>
)
}
};
export class ComponentTest1 extends React.Component {
render() {
return (
<View style={SpaceStyles.testView}>
<Text style={SpaceStyles.testText}>This is a test component</Text>
<TextInput value="Write here" style={SpaceStyles.testInput}></TextInput>
</View>
)
}
};
export class ComponentNotFound extends React.Component {
render() {
return (
<Text style={SpaceStyles.componentError}> Error : component not found</Text>
)
}
};
export class ComponentPoint extends React.Component {
render() {
return (
<View style={{backgroundColor:'rgba(255,25,25,0.5)', width:64, height:48}} >
<Text style={SpaceStyles.componentError}> X : {this.props.x}</Text>
<Text style={SpaceStyles.componentError}> Y : {this.props.y}</Text>
</View>
)
}
};