Skip to content

Commit 615234d

Browse files
committed
2.0.0: GL.Surface, GL.Node & dep on React 0.14.0
1 parent 6740866 commit 615234d

17 files changed

+306
-264
lines changed

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@
2222
"url": "https://github.com/ProjectSeptemberInc/gl-react-core/issues"
2323
},
2424
"homepage": "https://github.com/ProjectSeptemberInc/gl-react-core#readme",
25+
"peerDependencies": {
26+
"react": "^0.14.0"
27+
},
2528
"dependencies": {
2629
"babelify": "^6.3.0",
2730
"envify": "^3.4.0",

src/Node.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
const React = require("react");
2+
const { Component, PropTypes } = React;
3+
const invariant = require("invariant");
4+
5+
class Node extends Component {
6+
render () {
7+
invariant(
8+
false,
9+
"GL.Node elements can only be used as children of GL.Surface / GL.Node and should not be rendered"
10+
);
11+
}
12+
}
13+
14+
Node.isGLNode = true;
15+
16+
Node.displayName = "GL.Node";
17+
18+
Node.propTypes = {
19+
shader: PropTypes.number.isRequired,
20+
uniforms: PropTypes.object,
21+
children: PropTypes.node,
22+
width: PropTypes.number,
23+
height: PropTypes.number,
24+
preload: PropTypes.bool
25+
};
26+
27+
module.exports = Node;

src/Shaders.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
const EventEmitter = require("events");
2+
const invariant = require("invariant");
3+
4+
let _uid = 1;
5+
const names = {};
6+
const shaders = {};
7+
8+
const Shaders = {
9+
create (obj) {
10+
invariant(typeof obj === "object", "config must be an object");
11+
const result = {};
12+
for (let key in obj) {
13+
const shader = obj[key];
14+
invariant(typeof shader === "object" && typeof shader.frag === "string",
15+
"invalid shader given to Shaders.create(). A valid shader is a { frag: String }");
16+
const id = _uid ++;
17+
if (!shader.name) shader.name = key;
18+
names[id] = shader.name;
19+
shaders[id] = shader;
20+
this.emit("add", id, shader);
21+
result[key] = id;
22+
}
23+
return result;
24+
},
25+
remove (id) {
26+
invariant(id in shaders, "There is no such shader '%s'", id);
27+
delete shaders[id];
28+
delete names[id];
29+
this.emit("remove", id);
30+
},
31+
get (id) {
32+
return shaders[id];
33+
},
34+
getName (id) {
35+
return names[id];
36+
},
37+
list () {
38+
return Object.keys(names);
39+
},
40+
exists (id) {
41+
return typeof id === "number" && id >= 1 && id < _uid;
42+
},
43+
44+
...EventEmitter.prototype
45+
};
46+
47+
EventEmitter.call(Shaders);
48+
49+
module.exports = Object.freeze(Shaders);

src/Uniform.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
const React = require("react");
2+
const { Component, PropTypes } = React;
3+
const invariant = require("invariant");
4+
5+
class Uniform extends Component {
6+
render () {
7+
invariant(
8+
false,
9+
"GL.Uniform elements are for GL.Node configuration only and should not be rendered"
10+
);
11+
}
12+
}
13+
14+
Uniform.isGLUniform = true;
15+
16+
Uniform.displayName = "GL.Uniform";
17+
18+
Uniform.propTypes = {
19+
children: PropTypes.any.isRequired,
20+
name: PropTypes.string.isRequired
21+
};
22+
23+
module.exports = Uniform;

src/createComponent.js

Lines changed: 31 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,36 @@
1+
const React = require("react");
12
const invariant = require("invariant");
2-
const glViewMethods = require("./glViewMethods");
3-
4-
module.exports = function (React, View) {
5-
function createComponent (renderGLView, staticFields) {
6-
invariant(typeof renderGLView === "function",
7-
"GL.createComponent(props => glview) must have a function in parameter");
8-
class GLComponent extends React.Component {
9-
constructor (props, context) {
10-
super(props, context);
11-
glViewMethods.forEach(this._delegateMethod, this);
12-
}
13-
_delegateMethod (methodname) {
14-
const self = this;
15-
this[methodname] = function () {
16-
const glViewRef = self.refs._;
17-
invariant(glViewRef, "glView has been rendered");
18-
return glViewRef[methodname].apply(glViewRef, arguments);
19-
};
20-
}
21-
render () {
22-
const glView = renderGLView(this.props);
23-
invariant(glView && (glView.type === View || glView.type.isGLComponent),
24-
"The GL.createComponent function parameter must return a GL.View or another GL Component");
25-
return React.cloneElement(glView, { ...glView.props, ref: "_" });
26-
}
3+
4+
module.exports = function createComponent (renderGLNode, staticFields) {
5+
6+
invariant(typeof renderGLNode === "function",
7+
"GL.createComponent(props => glnode) must have a function in parameter");
8+
9+
class GLComponent extends React.Component {
10+
render () {
11+
const glNode = renderGLNode(this.props);
12+
13+
invariant(glNode && glNode.type && (glNode.type.isGLNode || glNode.type.isGLComponent),
14+
"The GL.createComponent function parameter must return a GL.Node or "+
15+
"another GL Component");
16+
17+
return glNode;
2718
}
28-
GLComponent.isGLComponent = true;
29-
GLComponent.displayName = renderGLView.name || "";
30-
if (staticFields) {
31-
invariant(typeof staticFields === "object", "second parameter of createComponent must be an object of static fields to set in the React component. (example: propTypes, displayName)");
32-
for (let key in staticFields) {
33-
GLComponent[key] = staticFields[key];
34-
}
19+
}
20+
21+
GLComponent.isGLComponent = true;
22+
23+
GLComponent.displayName = renderGLNode.name || "";
24+
25+
if (staticFields) {
26+
invariant(typeof staticFields === "object",
27+
"second parameter of createComponent must be an object of static fields "+
28+
"to set in the React component. (example: propTypes, displayName)");
29+
30+
for (let key in staticFields) {
31+
GLComponent[key] = staticFields[key];
3532
}
36-
return GLComponent;
3733
}
38-
return createComponent;
34+
35+
return GLComponent;
3936
};

src/createComponentDeprecated.js

Lines changed: 0 additions & 16 deletions
This file was deleted.

src/createShaders.js

Lines changed: 0 additions & 36 deletions
This file was deleted.
Lines changed: 47 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
1+
const React = require("react");
2+
const {
3+
Component,
4+
PropTypes
5+
} = React;
16
const invariant = require("invariant");
2-
const { fill, resolve, createBuild } = require("./data");
7+
const { fill, resolve, build } = require("./data");
8+
const findGLNodeInGLComponentChildren = require("./data/findGLNodeInGLComponentChildren");
39

410
function logResult (data, contentsVDOM) {
511
if (typeof console !== "undefined" &&
612
console.debug // eslint-disable-line
713
) {
8-
console.debug("GL.View rendered with", data, contentsVDOM); // eslint-disable-line no-console
14+
console.debug("GL.Surface rendered with", data, contentsVDOM); // eslint-disable-line no-console
915
}
1016
}
1117

12-
module.exports = function (React, Shaders, Uniform, renderVcontainer, renderVcontent, renderVGL) {
13-
const {
14-
Component,
15-
PropTypes
16-
} = React;
18+
module.exports = function (renderVcontainer, renderVcontent, renderVGL) {
1719

18-
let build; // will be set after GLView class defined.
19-
20-
class GLView extends Component {
20+
class GLSurface extends Component {
2121
constructor (props, context) {
2222
super(props, context);
2323
this._renderId = 1;
@@ -34,27 +34,38 @@ module.exports = function (React, Shaders, Uniform, renderVcontainer, renderVcon
3434
render() {
3535
const renderId = this._renderId ++;
3636
const props = this.props;
37-
const { style, width, height, children, shader, uniforms, debug, preload, opaque, visibleContent, eventsThrough, ...restProps } = props;
37+
const {
38+
style,
39+
width,
40+
height,
41+
children,
42+
debug,
43+
preload,
44+
opaque,
45+
visibleContent,
46+
eventsThrough,
47+
...restProps
48+
} = props;
3849

39-
invariant(width && height && width>0 && height>0, "width and height are required for the root GLView");
50+
const { via, childGLNode } =
51+
findGLNodeInGLComponentChildren(children);
4052

41-
const {data, contentsVDOM, imagesToPreload} =
53+
const { data, contentsVDOM, imagesToPreload } =
4254
resolve(
4355
fill(
4456
build(
45-
shader,
46-
uniforms,
57+
childGLNode,
4758
width,
4859
height,
49-
children,
50-
preload||false,
51-
[])));
60+
preload,
61+
via)));
5262

5363
if (debug) logResult(data, contentsVDOM);
5464

5565
return renderVcontainer(
5666
{ width, height, style, visibleContent, eventsThrough },
57-
contentsVDOM.map((vdom, i) => renderVcontent(data.width, data.height, i, vdom, { visibleContent })),
67+
contentsVDOM.map((vdom, i) =>
68+
renderVcontent(data.width, data.height, i, vdom, { visibleContent })),
5869
renderVGL({
5970
...restProps, // eslint-disable-line no-undef
6071
width,
@@ -71,23 +82,28 @@ module.exports = function (React, Shaders, Uniform, renderVcontainer, renderVcon
7182
}
7283
}
7384

74-
GLView.displayName = "GL.View";
75-
GLView.propTypes = {
76-
shader: PropTypes.number.isRequired,
77-
width: PropTypes.number,
78-
height: PropTypes.number,
79-
uniforms: PropTypes.object,
85+
GLSurface.displayName = "GL.Surface";
86+
87+
GLSurface.propTypes = {
88+
width: PropTypes.number.isRequired,
89+
height: PropTypes.number.isRequired,
90+
children: PropTypes.element.isRequired,
8091
opaque: PropTypes.bool,
8192
preload: PropTypes.bool,
8293
autoRedraw: PropTypes.bool,
8394
eventsThrough: PropTypes.bool,
84-
visibleContent: PropTypes.bool
85-
};
86-
GLView.defaultProps = {
87-
opaque: true
95+
visibleContent: PropTypes.bool,
96+
onLoad: PropTypes.func,
97+
onProgress: PropTypes.func
8898
};
8999

90-
build = createBuild(React, Shaders, Uniform, GLView);
100+
GLSurface.defaultProps = {
101+
opaque: true,
102+
preload: false,
103+
autoRedraw: false,
104+
eventsThrough: false,
105+
visibleContent: false
106+
};
91107

92-
return GLView;
108+
return GLSurface;
93109
};

src/createUniform.js

Lines changed: 0 additions & 21 deletions
This file was deleted.

0 commit comments

Comments
 (0)