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");
const Display2 = require("./Display2");
const { width: viewportW, height: viewportH } = require("Dimensions").get("window");
const Tests = React.createClass({
render: function() {
class Tests extends React.Component {
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 =
<HelloGL width={64} height={64} />;
......@@ -48,7 +60,7 @@ const Tests = React.createClass({
</Layer>;
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}>
<Add width={viewportW/2} height={viewportH/2}>
{txt}
......@@ -68,6 +80,6 @@ const Tests = React.createClass({
</Display2>
</View>;
}
});
}
AppRegistry.registerComponent("Tests", () => Tests);
......@@ -8,8 +8,10 @@
@property (nonatomic) NSNumber *nbContentTextures;
@property (nonatomic) NSNumber *renderId;
@property (nonatomic) NSArray *imagesToPreload;
@property (nonatomic, assign) BOOL onProgress;
@property (nonatomic, assign) BOOL onLoad;
- (instancetype)initWithBridge:(RCTBridge *)bridge
withContext:(EAGLContext*)context;
withContext:(EAGLContext *)context;
@end
......@@ -2,6 +2,7 @@
#import "RCTBridge.h"
#import "RCTUtils.h"
#import "RCTConvert.h"
#import "RCTEventDispatcher.h"
#import "RCTLog.h"
#import "GLCanvas.h"
#import "GLShader.h"
......@@ -9,6 +10,7 @@
#import "GLTexture.h"
#import "GLImage.h"
#import "GLRenderData.h"
#import "UIView+React.h"
// For reference, see implementation of gl-shader's GLCanvas
......@@ -32,7 +34,7 @@
}
- (instancetype)initWithBridge:(RCTBridge *)bridge
withContext:(EAGLContext*)context
withContext:(EAGLContext *)context
{
if ((self = [super init])) {
_bridge = bridge;
......@@ -50,11 +52,35 @@ RCT_NOT_IMPLEMENTED(-init)
{
if (_preloadingDone) return;
if ([imagesToPreload count] == 0) {
[self dispatchOnLoad];
_preloadingDone = true;
}
else {
_preloadingDone = false;
}
_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
{
_opaque = opaque;
......@@ -203,20 +229,26 @@ NSString* srcResource (id res)
}
}
- (bool)allPreloaded
- (int)countPreloaded
{
int nb = 0;
for (id toload in _imagesToPreload) {
if (![_preloaded containsObject:srcResource(toload)])
return false;
if ([_preloaded containsObject:srcResource(toload)])
nb++;
}
return true;
return nb;
}
- (void)onImageLoad:(NSString *)loaded
{
if (!_preloadingDone) {
[_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;
[self requestSyncData];
}
......
......@@ -23,6 +23,8 @@ RCT_EXPORT_VIEW_PROPERTY(opaque, BOOL);
RCT_EXPORT_VIEW_PROPERTY(data, GLData);
RCT_EXPORT_VIEW_PROPERTY(renderId, NSNumber);
RCT_EXPORT_VIEW_PROPERTY(imagesToPreload, NSArray);
RCT_EXPORT_VIEW_PROPERTY(onLoad, BOOL);
RCT_EXPORT_VIEW_PROPERTY(onProgress, BOOL);
- (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