Commit 6ccfab2c authored by Gaëtan Renaudeau's avatar Gaëtan Renaudeau

add onLoad and onProgress events

parent 66ea4f47
...@@ -13,8 +13,20 @@ const HelloGL = require("./HelloGL"); ...@@ -13,8 +13,20 @@ const HelloGL = require("./HelloGL");
const Display2 = require("./Display2"); const Display2 = require("./Display2");
const { width: viewportW, height: viewportH } = require("Dimensions").get("window"); const { width: viewportW, height: viewportH } = require("Dimensions").get("window");
const Tests = React.createClass({ class Tests extends React.Component {
render: function() {
constructor (props) {
super(props);
this.onLoad = this.onLoad.bind(this);
this.onProgress = this.onProgress.bind(this);
}
onLoad () {
console.log("LOADED");
}
onProgress ({nativeEvent: { progress, loaded, total }}) {
console.log("PROGRESS", progress, loaded, total);
}
render () {
const helloGL = const helloGL =
<HelloGL width={64} height={64} />; <HelloGL width={64} height={64} />;
...@@ -48,7 +60,7 @@ const Tests = React.createClass({ ...@@ -48,7 +60,7 @@ const Tests = React.createClass({
</Layer>; </Layer>;
return <View style={{ backgroundColor: "#000" }}> return <View style={{ backgroundColor: "#000" }}>
<Display2 width={viewportW} height={viewportH} vertical preload> <Display2 width={viewportW} height={viewportH} vertical preload onLoad={this.onLoad} onProgress={this.onProgress}>
<Display2 width={viewportW} height={viewportH/2}> <Display2 width={viewportW} height={viewportH/2}>
<Add width={viewportW/2} height={viewportH/2}> <Add width={viewportW/2} height={viewportH/2}>
{txt} {txt}
...@@ -68,6 +80,6 @@ const Tests = React.createClass({ ...@@ -68,6 +80,6 @@ const Tests = React.createClass({
</Display2> </Display2>
</View>; </View>;
} }
}); }
AppRegistry.registerComponent("Tests", () => Tests); AppRegistry.registerComponent("Tests", () => Tests);
...@@ -8,8 +8,10 @@ ...@@ -8,8 +8,10 @@
@property (nonatomic) NSNumber *nbContentTextures; @property (nonatomic) NSNumber *nbContentTextures;
@property (nonatomic) NSNumber *renderId; @property (nonatomic) NSNumber *renderId;
@property (nonatomic) NSArray *imagesToPreload; @property (nonatomic) NSArray *imagesToPreload;
@property (nonatomic, assign) BOOL onProgress;
@property (nonatomic, assign) BOOL onLoad;
- (instancetype)initWithBridge:(RCTBridge *)bridge - (instancetype)initWithBridge:(RCTBridge *)bridge
withContext:(EAGLContext*)context; withContext:(EAGLContext *)context;
@end @end
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
#import "RCTBridge.h" #import "RCTBridge.h"
#import "RCTUtils.h" #import "RCTUtils.h"
#import "RCTConvert.h" #import "RCTConvert.h"
#import "RCTEventDispatcher.h"
#import "RCTLog.h" #import "RCTLog.h"
#import "GLCanvas.h" #import "GLCanvas.h"
#import "GLShader.h" #import "GLShader.h"
...@@ -9,6 +10,7 @@ ...@@ -9,6 +10,7 @@
#import "GLTexture.h" #import "GLTexture.h"
#import "GLImage.h" #import "GLImage.h"
#import "GLRenderData.h" #import "GLRenderData.h"
#import "UIView+React.h"
// For reference, see implementation of gl-shader's GLCanvas // For reference, see implementation of gl-shader's GLCanvas
...@@ -32,7 +34,7 @@ ...@@ -32,7 +34,7 @@
} }
- (instancetype)initWithBridge:(RCTBridge *)bridge - (instancetype)initWithBridge:(RCTBridge *)bridge
withContext:(EAGLContext*)context withContext:(EAGLContext *)context
{ {
if ((self = [super init])) { if ((self = [super init])) {
_bridge = bridge; _bridge = bridge;
...@@ -50,11 +52,35 @@ RCT_NOT_IMPLEMENTED(-init) ...@@ -50,11 +52,35 @@ RCT_NOT_IMPLEMENTED(-init)
{ {
if (_preloadingDone) return; if (_preloadingDone) return;
if ([imagesToPreload count] == 0) { if ([imagesToPreload count] == 0) {
[self dispatchOnLoad];
_preloadingDone = true; _preloadingDone = true;
} }
else {
_preloadingDone = false;
}
_imagesToPreload = imagesToPreload; _imagesToPreload = imagesToPreload;
} }
- (void)dispatchOnLoad
{
if (_onLoad) {
[_bridge.eventDispatcher sendInputEventWithName:@"load" body:@{ @"target": self.reactTag }];
}
}
- (void)dispatchOnProgress: (double)progress withLoaded:(int)loaded withTotal:(int)total
{
if (_onProgress) {
NSDictionary *event =
@{
@"target": self.reactTag,
@"progress": @(progress),
@"loaded": @(loaded),
@"total": @(total) };
[_bridge.eventDispatcher sendInputEventWithName:@"progress" body:event];
}
}
- (void)setOpaque:(BOOL)opaque - (void)setOpaque:(BOOL)opaque
{ {
_opaque = opaque; _opaque = opaque;
...@@ -203,20 +229,26 @@ NSString* srcResource (id res) ...@@ -203,20 +229,26 @@ NSString* srcResource (id res)
} }
} }
- (bool)allPreloaded - (int)countPreloaded
{ {
int nb = 0;
for (id toload in _imagesToPreload) { for (id toload in _imagesToPreload) {
if (![_preloaded containsObject:srcResource(toload)]) if ([_preloaded containsObject:srcResource(toload)])
return false; nb++;
} }
return true; return nb;
} }
- (void)onImageLoad:(NSString *)loaded - (void)onImageLoad:(NSString *)loaded
{ {
if (!_preloadingDone) { if (!_preloadingDone) {
[_preloaded addObject:loaded]; [_preloaded addObject:loaded];
if ([self allPreloaded]) { int count = [self countPreloaded];
int total = (int) [_imagesToPreload count];
double progress = ((double) count) / ((double) total);
[self dispatchOnProgress:progress withLoaded:count withTotal:total];
if (count == total) {
[self dispatchOnLoad];
_preloadingDone = true; _preloadingDone = true;
[self requestSyncData]; [self requestSyncData];
} }
......
...@@ -23,6 +23,8 @@ RCT_EXPORT_VIEW_PROPERTY(opaque, BOOL); ...@@ -23,6 +23,8 @@ RCT_EXPORT_VIEW_PROPERTY(opaque, BOOL);
RCT_EXPORT_VIEW_PROPERTY(data, GLData); RCT_EXPORT_VIEW_PROPERTY(data, GLData);
RCT_EXPORT_VIEW_PROPERTY(renderId, NSNumber); RCT_EXPORT_VIEW_PROPERTY(renderId, NSNumber);
RCT_EXPORT_VIEW_PROPERTY(imagesToPreload, NSArray); RCT_EXPORT_VIEW_PROPERTY(imagesToPreload, NSArray);
RCT_EXPORT_VIEW_PROPERTY(onLoad, BOOL);
RCT_EXPORT_VIEW_PROPERTY(onProgress, BOOL);
- (UIView *)view - (UIView *)view
{ {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment