Advertisement
loloof64

Bounded Dnd in react native : 2nd try

Oct 24th, 2017
357
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. import React, { Component } from 'react';
  3. import { StyleSheet, View, Animated, PanResponder, UIManager, findNodeHandle } from 'react-native';
  4. import _ from 'underscore';
  5.  
  6. class Square {
  7.     constructor(value, x, y, cellsSize) {
  8.         this.value = value;
  9.         this.pan = new Animated.ValueXY({ x, y });
  10.         this.cellsSize = cellsSize;
  11.         this.minXY = this.cellsSize * (0.5);
  12.         this.maxXY = this.cellsSize * (1.5);
  13.         this.constrainedX = this.pan.x.interpolate({
  14.             inputRange: [this.minXY, this.maxXY],
  15.             outputRange: [this.minXY, this.maxXY],
  16.             extrapolate: 'clamp',
  17.         });
  18.         this.constrainedY = this.pan.y.interpolate({
  19.             inputRange: [this.minXY, this.maxXY],
  20.             outputRange: [this.minXY, this.maxXY],
  21.             extrapolate: 'clamp',
  22.         });
  23.         this.panResponder = this._buildPanResponder();
  24.     }
  25.  
  26.     get valueString() {
  27.         return this.value;
  28.     }
  29.  
  30.     get panRef() {
  31.         return this.pan;
  32.     }
  33.  
  34.     get panResponderRef() {
  35.         return this.panResponder;
  36.     }
  37.  
  38.     _buildPanResponder() {
  39.         return PanResponder.create({
  40.             onStartShouldSetPanResponder: () => true,
  41.             onPanResponderGrant: (event, gestureState) => {
  42.                 this.pan.setOffset({ x: this.pan.x._value, y: this.pan.y._value });
  43.             },
  44.             onPanResponderMove: (event, gestureState) => {
  45.                 this.pan.setValue({ x: gestureState.dx, y: gestureState.dy });
  46.             },
  47.             onPanResponderRelease: (event, gesture) => {
  48.                 this.pan.flattenOffset();
  49.             }
  50.         });
  51.     }
  52. }
  53.  
  54. export default class TestComponent extends Component {
  55.  
  56.     constructor(props) {
  57.         super(props);
  58.  
  59.         this.cellsSize = 100;
  60.  
  61.         this.squares = [
  62.             [undefined, new Square('red', this.cellsSize * (1.5), this.cellsSize * (0.5), this.cellsSize)],
  63.             [new Square('green', this.cellsSize * (0.5), this.cellsSize * (1.5), this.cellsSize), new Square('blue', this.cellsSize * (1.5), this.cellsSize * (1.5), this.cellsSize)]
  64.         ];
  65.     }
  66.  
  67.     renderACoin(value, file, rank) {
  68.         if (value) {
  69.             let style;
  70.             switch (value.valueString) {
  71.                 case 'red': style = styles.redCoin; break;
  72.                 case 'green': style = styles.greenCoin; break;
  73.                 case 'blue': style = styles.blueCoin; break;
  74.             }
  75.  
  76.             const coordsStyle = {
  77.                 top: this.cellsSize * (0.5 + rank),
  78.                 left: this.cellsSize * (0.5 + file),
  79.             }
  80.  
  81.             const randomKey = parseInt(Math.random() * 1000000).toString()
  82.  
  83.             return (
  84.                 <Animated.View key={randomKey} style={StyleSheet.flatten([style, coordsStyle, { left: this.constrainedX, top: this.constrainedY }])}
  85.                     {...value.panResponderRef.panHandlers}
  86.                 />
  87.             );
  88.         }
  89.     }
  90.  
  91.     renderAllCoins() {
  92.         return _.map(this.squares, (currRank, rankIndex) => {
  93.             return _.map(currRank, (cell, fileIndex) => {
  94.                 return this.renderACoin(this.squares[rankIndex][fileIndex], fileIndex, rankIndex);
  95.             });
  96.         });
  97.     }
  98.  
  99.     render() {
  100.  
  101.         return (
  102.             <View style={styles.topLevel}>
  103.                 <View style={StyleSheet.flatten([styles.board])}
  104.                     ref="boardRoot"
  105.                 >
  106.                     <View style={StyleSheet.flatten([styles.whiteCell, {
  107.                         left: 50,
  108.                         top: 50,
  109.                     }])} />
  110.                     <View style={StyleSheet.flatten([styles.blackCell, {
  111.                         left: 150,
  112.                         top: 50,
  113.                     }])} />
  114.                     <View style={StyleSheet.flatten([styles.blackCell, {
  115.                         left: 50,
  116.                         top: 150,
  117.                     }])} />
  118.                     <View style={StyleSheet.flatten([styles.whiteCell, {
  119.                         left: 150,
  120.                         top: 150,
  121.                     }])} />
  122.  
  123.                     {this.renderAllCoins()}
  124.  
  125.                 </View>
  126.             </View>
  127.         );
  128.     }
  129. }
  130.  
  131. const styles = StyleSheet.create({
  132.     topLevel: {
  133.         backgroundColor: "#CCFFCC",
  134.         flex: 1,
  135.         justifyContent: 'center',
  136.         alignItems: 'center',
  137.         flexDirection: 'row',
  138.     },
  139.     board: {
  140.         width: 300,
  141.         height: 300,
  142.         backgroundColor: "#FFCCFF",
  143.     },
  144.     whiteCell: {
  145.         width: 100,
  146.         height: 100,
  147.         backgroundColor: '#FFAA22',
  148.         position: 'absolute',
  149.     },
  150.     blackCell: {
  151.         width: 100,
  152.         height: 100,
  153.         backgroundColor: '#221122',
  154.         position: 'absolute',
  155.     },
  156.     greenCoin: {
  157.         width: 100,
  158.         height: 100,
  159.         position: 'absolute',
  160.         backgroundColor: '#23CC12',
  161.         borderRadius: 50,
  162.     },
  163.     redCoin: {
  164.         width: 100,
  165.         height: 100,
  166.         position: 'absolute',
  167.         backgroundColor: '#FF0000',
  168.         borderRadius: 50,
  169.     },
  170.     blueCoin: {
  171.         width: 100,
  172.         height: 100,
  173.         position: 'absolute',
  174.         backgroundColor: '#0000FF',
  175.         borderRadius: 50,
  176.     },
  177. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement