index.js 9.94 KB
Newer Older
1
require("gl-react/react-native");
2

Gaëtan Renaudeau's avatar
Gaëtan Renaudeau committed
3 4 5 6 7 8 9 10
const React = require("react-native");
const {
  StyleSheet,
  Text,
  View,
  Image,
  TextInput,
  Component,
11
  TouchableOpacity
Gaëtan Renaudeau's avatar
Gaëtan Renaudeau committed
12 13
} = React;

14 15
const { Surface } = require("gl-react-native");

Gaëtan Renaudeau's avatar
Gaëtan Renaudeau committed
16 17 18 19 20 21 22 23 24
const {
  mdl: {
    Progress,
    Slider,
    Switch
  },
  MKButton,
} = require("react-native-material-kit");

25 26
const RNFS = require("react-native-fs");

Gaëtan Renaudeau's avatar
Gaëtan Renaudeau committed
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
const HelloGL = require("./HelloGL");
const Saturation = require("./Saturation");
const HueRotate = require("./HueRotate");
const PieProgress = require("./PieProgress");
const OneFingerResponse = require("./OneFingerResponse");
const AnimatedHelloGL = require("./AnimatedHelloGL");
const Blur = require("./Blur");
const Button = require("./Button");

class Demo extends Component {
  render () {
    const { title, children } = this.props;
    return <View>
      <Text style={styles.demoTitle}>{title}</Text>
      <View style={styles.demo}>
        {children}
      </View>
    </View>;
  }
}

class Demos extends Component {
  render () {
    const { children, onChange, value } = this.props;
    return <View>
      <View style={styles.nav}>
        {React.Children.map(children, (demo, i) =>
          <MKButton
            style={{ flex: 1, padding: 10 }}
            onPress={() => onChange(i)}>
            <Text pointerEvents="none"
              style={{
                textAlign: "center",
                color: i!==value ? "#123" : "#69c",
                fontWeight: "bold"
              }}>
              {""+(i+1)}
            </Text>
          </MKButton>)}
      </View>
      <View style={styles.demos}>
        {children[value]}
      </View>
  </View>;
  }
}

class Simple extends Component {
  constructor (props) {
    super(props);
    this.state = {
      current: 0,
      saturationFactor: 1,
      hue: 0,
      progress: 0,
      factor: 0,
      text: "and I will return leading the pack",
      switch1: false,
      switch2: false,
      switch3: false,
87 88
      captured: null,
      captureConfig: null
Gaëtan Renaudeau's avatar
Gaëtan Renaudeau committed
89 90
    };
    this.onCapture1 = this.onCapture1.bind(this);
91 92 93 94 95 96 97 98 99

    /*
    // Crazy stress mode
    const self = this;
    setTimeout(function loop () {
      setTimeout(loop, 100 * Math.random());
      self.setState({ current: Math.floor(8 * Math.random()) });
    }, 100);
    */
Gaëtan Renaudeau's avatar
Gaëtan Renaudeau committed
100 101 102
  }

  onCapture1 () {
103 104 105 106 107 108 109 110 111 112 113
    const captureConfig = {
      quality: Math.round((Math.random() * 100))/100,
      type: Math.random() < 0.5 ? "jpg": "png",
      format: Math.random() < 0.5 ? "base64" : "file"
    };
    if (captureConfig.format === "file") {
      captureConfig.filePath = RNFS.DocumentDirectoryPath+"/hellogl_capture.png";
    }
    this.refs.helloGL
    .captureFrame(captureConfig)
    .then(captured => this.setState({ captured, captureConfig }));
Gaëtan Renaudeau's avatar
Gaëtan Renaudeau committed
114 115 116 117 118 119 120 121 122 123 124 125 126
  }

  render () {
    const {
      current,
      saturationFactor,
      hue,
      text,
      progress,
      factor,
      switch1,
      switch2,
      switch3,
127 128
      captured,
      captureConfig
Gaëtan Renaudeau's avatar
Gaëtan Renaudeau committed
129 130 131 132
    } = this.state;

    return <View style={styles.container}>
      <Text style={styles.title}>
133
        gl-react-native > Simple
Gaëtan Renaudeau's avatar
Gaëtan Renaudeau committed
134 135 136
      </Text>

      <Demos onChange={current => this.setState({ current })} value={current}>
137
        <Demo id={1} title="1. Hello GL">
138 139 140
          <Surface width={256} height={171} ref="helloGL">
            <HelloGL />
          </Surface>
Gaëtan Renaudeau's avatar
Gaëtan Renaudeau committed
141 142
          <View style={{ paddingTop: 20, alignItems: "center", flexDirection: "row" }}>
            <Button onPress={this.onCapture1}>captureFrame()</Button>
143 144 145 146
            {captured &&
              <Image source={{ uri: captured }}
                style={{ marginLeft: 20, width: 51, height: 34 }}
              /> }
Gaëtan Renaudeau's avatar
Gaëtan Renaudeau committed
147
          </View>
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
          {captureConfig &&
            <View style={{ paddingTop: 20, alignItems: "center", flexDirection: "row", justifyContent: "space-between" }}>
              <Text style={{ fontSize: 10 }}>
                format={captureConfig.format}
              </Text>
              <Text style={{ fontSize: 10 }}>
                type={captureConfig.type}
              </Text>
              <Text style={{ fontSize: 10 }}>
                quality={captureConfig.quality+""}
              </Text>
            </View>
          }
          {captured &&
            <Text numberOfLines={1} style={{ marginTop: 10, fontSize: 10, color: "#aaa" }}>
              {captured.slice(0, 100)}
            </Text> }
Gaëtan Renaudeau's avatar
Gaëtan Renaudeau committed
165 166
        </Demo>

167
        <Demo id={2} title="2. Saturate an Image">
168 169
          <Surface width={256} height={171}>
            <Saturation
Gaëtan Renaudeau's avatar
Gaëtan Renaudeau committed
170 171 172
              factor={saturationFactor}
              image={{ uri: "http://i.imgur.com/iPKTONG.jpg" }}
            />
173
          </Surface>
Gaëtan Renaudeau's avatar
Gaëtan Renaudeau committed
174 175 176 177 178 179 180
          <Slider
            max={8}
            onChange={saturationFactor => this.setState({ saturationFactor })}
          />
        </Demo>

        <Demo id={3} current={current} title="3. Hue Rotate on Text+Image">
181 182 183 184 185 186 187
          <Surface autoRedraw width={256} height={180}>
            <HueRotate hue={hue}>
              <Image style={{ width: 256, height: 244 }} source={{ uri: "http://i.imgur.com/qVxHrkY.jpg" }}/>
              <Text style={styles.demoText1}>Throw me to the wolves</Text>
              <Text style={styles.demoText2}>{text}</Text>
            </HueRotate>
          </Surface>
Gaëtan Renaudeau's avatar
Gaëtan Renaudeau committed
188 189 190 191 192 193 194 195 196 197 198 199 200
          <Slider
            max={2 * Math.PI}
            onChange={hue => this.setState({ hue })}
          />
          <TextInput
            style={{ height: 40, borderColor: "#aaa", borderWidth: 1 }}
            onChangeText={text => this.setState({ text })}
            value={text}
          />
        </Demo>

        <Demo id={4} current={current} title="4. Progress Indicator">
          <View style={{ position: "relative", width: 256, height: 180 }}>
201 202 203 204 205 206 207 208 209 210 211 212 213
            <TouchableOpacity>
              <Image source={{ uri: "http://i.imgur.com/qM9BHCy.jpg" }}
                style={{
                  width: 256,
                  height: 180,
                  position: "absolute",
                  top: 0,
                  left: 0
                }}
              />
            </TouchableOpacity>
            <View pointerEvents="box-none" style={{ position: "absolute", top: 0, left: 0, backgroundColor: "transparent" }}>
              <Surface width={256} height={180} opaque={false} eventsThrough>
214 215
                <PieProgress progress={progress} width={256} height={180} />
              </Surface>
Gaëtan Renaudeau's avatar
Gaëtan Renaudeau committed
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
            </View>
          </View>
          <Slider
            max={1}
            onChange={progress => this.setState({ progress })}
          />
        </Demo>

        <Demo id={5} current={current} title="5. Touch Responsive">
          <OneFingerResponse
            width={256}
            height={180}
          />
        </Demo>

        <Demo id={6} current={current} title="6. Animation">
          <AnimatedHelloGL
            width={256}
            height={180}
          />
        </Demo>

        <Demo id={7} current={current} title="7. Blur (2-pass)">
239 240 241 242 243
          <Surface preload width={256} height={180}>
            <Blur width={256} height={180} factor={factor + 1}>
              http://i.imgur.com/3On9QEu.jpg
            </Blur>
          </Surface>
Gaëtan Renaudeau's avatar
Gaëtan Renaudeau committed
244 245 246 247 248 249
          <Slider
            max={2}
            onChange={factor => this.setState({ factor })} />
        </Demo>

        <Demo id={8} current={current} title="8. Blur+Hue over UI">
250
          <Surface
Gaëtan Renaudeau's avatar
Gaëtan Renaudeau committed
251 252 253 254 255
            width={256}
            height={160}
            autoRedraw
            eventsThrough
            visibleContent>
256
            <HueRotate hue={-switch1 + 2 * switch2 + 4 * switch3}>
Gaëtan Renaudeau's avatar
Gaëtan Renaudeau committed
257 258 259
              <Blur
                width={256}
                height={160}
260 261 262 263 264
                factor={factor}>
                <Blur
                  width={256}
                  height={160}
                  factor={factor/2}>
265
                  <View style={{ width: 256, height: 160, padding: 10, backgroundColor: "#f9f9f9" }}>
266 267 268 269 270 271 272 273 274 275 276 277 278 279
                    <Slider
                      style={{ height: 80 }}
                      max={2}
                      onChange={factor => this.setState({ factor })}
                    />
                  <View style={{ height: 60, flexDirection: "row", alignItems: "center" }}>
                    <Switch style={{flex:1}} checked={switch1} onCheckedChange={({checked:switch1}) => this.setState({ switch1 })} />
                    <Switch style={{flex:1}} checked={switch2} onCheckedChange={({checked:switch2}) => this.setState({ switch2 })} />
                    <Switch style={{flex:1}} checked={switch3} onCheckedChange={({checked:switch3}) => this.setState({ switch3 })} />
                  </View>
                  <Progress progress={factor} style={{height: 10, marginTop: 8, flex:1}} />

                  </View>
                </Blur>
Gaëtan Renaudeau's avatar
Gaëtan Renaudeau committed
280
              </Blur>
281 282
            </HueRotate>
          </Surface>
283
          <Text>Note: This is highly experimental and not yet performant enough.</Text>
Gaëtan Renaudeau's avatar
Gaëtan Renaudeau committed
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
        </Demo>

        <Demo id={9} current={current} title="9. Texture from array">
          <Text>Not Supported Yet</Text>
        </Demo>

      </Demos>
    </View>;
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: "#F9F9F9",
  },
  title: {
    fontSize: 20,
    textAlign: "center",
    margin: 5,
    marginBottom: 20,
    fontWeight: "bold"
  },
  nav: {
    flexDirection: "row",
    marginBottom: 20
  },
  demos: {
    flex: 1,
    justifyContent: "center",
    marginLeft: 40,
    width: 276,
    marginBottom: 40,
  },
  demoTitle: {
    marginBottom: 16,
    fontStyle: "italic",
    alignSelf: "flex-start",
    color: "#999",
    fontWeight: "300",
    fontSize: 20,
  },
  demo: {
    marginBottom: 64,
    marginLeft: 20,
  },
  demoText1: {
    position: "absolute",
    top: 0,
    left: 0,
    width: 256,
    textAlign: "center",
    color: "#f16",
    backgroundColor: "transparent",
    fontWeight: "400",
    fontSize: 24,
    letterSpacing: 0
  },
  demoText2: {
    position: "absolute",
    bottom: 4,
    left: 0,
    width: 256,
    textAlign: "center",
    color: "#7bf",
    backgroundColor: "transparent",
    fontWeight: "300",
    fontSize: 32,
    letterSpacing: -1
  },
});

module.exports = Simple;