Skip to content

Commit 844be5a

Browse files
committed
Fix screenshot taking
Screenshot taking wasn't working due to a react "this" issue. In JavaScript, class methods are not bound to the instance like an arrow function is. So, when a user of ViroARSceneNavigator tries to use these functions, the "this" instance didn't have all of the React properties that make React Native's findNodeHandle work correctly. Changing functions like: async _takeScreenshot(fileName, saveToCameraRoll) { // ... } To _takeScreenshot = (filename, saveToCameraRoll) => { // ... } Fixes the issue. Also adds more documentation to the ViroARSceneNavigator.
1 parent 9ed22a0 commit 844be5a

10 files changed

+492
-426
lines changed

components/AR/ViroARSceneNavigator.tsx

+226-169
Large diffs are not rendered by default.

components/Viro3DSceneNavigator.tsx

+14-15
Original file line numberDiff line numberDiff line change
@@ -83,19 +83,6 @@ type State = {
8383
export class Viro3DSceneNavigator extends React.Component<Props, State> {
8484
_component: ViroNativeRef = null;
8585

86-
sceneNavigator = {
87-
push: this.push,
88-
pop: this.pop,
89-
popN: this.popN,
90-
jump: this.jump,
91-
replace: this.replace,
92-
// exitViro: this.exitViro, TODO: this was unused
93-
recenterTracking: this._recenterTracking,
94-
project: this._project,
95-
unproject: this._unproject,
96-
viroAppProps: {} as any,
97-
};
98-
9986
/**
10087
* Called from native when either the user physically decides to exit vr (hits
10188
* the "X" buton).
@@ -349,8 +336,7 @@ export class Viro3DSceneNavigator extends React.Component<Props, State> {
349336
* (counts equals 0), we then remove that scene from sceneDictionary.
350337
*/
351338
decrementReferenceForLastNScenes(n: number) {
352-
var sceneHistory = this.state.sceneHistory;
353-
var sceneDictionary = this.state.sceneDictionary;
339+
const { sceneHistory, sceneDictionary } = this.state;
354340

355341
// Now update and release any reference counts
356342
for (var i = 1; i <= n; i++) {
@@ -473,6 +459,19 @@ export class Viro3DSceneNavigator extends React.Component<Props, State> {
473459
);
474460
}
475461

462+
sceneNavigator = {
463+
push: this.push,
464+
pop: this.pop,
465+
popN: this.popN,
466+
jump: this.jump,
467+
replace: this.replace,
468+
// exitViro: this.exitViro, TODO: this was unused
469+
recenterTracking: this._recenterTracking,
470+
project: this._project,
471+
unproject: this._unproject,
472+
viroAppProps: {} as any,
473+
};
474+
476475
render() {
477476
// Uncomment this line to check for misnamed props
478477
//checkMisnamedProps("Viro3DSceneNavigator", this.props);

components/ViroVRSceneNavigator.tsx

+13-12
Original file line numberDiff line numberDiff line change
@@ -84,18 +84,6 @@ type Props = ViewProps & {
8484
* ViroVRSceneNavigator is used to transition between multiple scenes.
8585
*/
8686
export class ViroVRSceneNavigator extends React.Component<Props, State> {
87-
sceneNavigator = {
88-
push: this.push,
89-
pop: this.pop,
90-
popN: this.popN,
91-
jump: this.jump,
92-
replace: this.replace,
93-
// exitViro: this.exitViro, // not defined?
94-
project: this._project,
95-
unproject: this._unproject,
96-
recenterTracking: this._recenterTracking,
97-
viroAppProps: {} as any,
98-
};
9987
_component: ViroNativeRef = null;
10088

10189
/**
@@ -472,6 +460,19 @@ export class ViroVRSceneNavigator extends React.Component<Props, State> {
472460
return views;
473461
}
474462

463+
sceneNavigator = {
464+
push: this.push,
465+
pop: this.pop,
466+
popN: this.popN,
467+
jump: this.jump,
468+
replace: this.replace,
469+
// exitViro: this.exitViro, // not defined?
470+
project: this._project,
471+
unproject: this._unproject,
472+
recenterTracking: this._recenterTracking,
473+
viroAppProps: {} as any,
474+
};
475+
475476
render() {
476477
var items = this._renderSceneStackItems();
477478

dist/components/AR/ViroARSceneNavigator.d.ts

+61-49
Original file line numberDiff line numberDiff line change
@@ -53,38 +53,24 @@ type State = {
5353
*/
5454
export declare class ViroARSceneNavigator extends React.Component<Props, State> {
5555
_component: ViroNativeRef;
56-
arSceneNavigator: {
57-
push: (param1?: ViroScene | string, param2?: ViroScene) => void;
58-
pop: () => void;
59-
popN: (n: number) => void;
60-
jump: (param1?: ViroScene | string, param2?: ViroScene) => void;
61-
replace: (param1?: ViroScene | string, param2?: ViroScene) => void;
62-
startVideoRecording: (fileName: string, saveToCameraRoll: boolean, onError: (errorCode: number) => void) => void;
63-
stopVideoRecording: () => Promise<any>;
64-
takeScreenshot: (fileName: string, saveToCameraRoll: boolean) => Promise<any>;
65-
resetARSession: (resetTracking: any, removeAnchors: any) => void;
66-
setWorldOrigin: (worldOrigin: ViroWorldOrigin) => void;
67-
project: (point: Viro3DPoint) => Promise<any>;
68-
unproject: (point: Viro3DPoint) => Promise<any>;
69-
viroAppProps: any;
70-
};
71-
sceneNavigator: {
72-
push: (param1?: ViroScene | string, param2?: ViroScene) => void;
73-
pop: () => void;
74-
popN: (n: number) => void;
75-
jump: (param1?: ViroScene | string, param2?: ViroScene) => void;
76-
replace: (param1?: ViroScene | string, param2?: ViroScene) => void;
77-
startVideoRecording: (fileName: string, saveToCameraRoll: boolean, onError: (errorCode: number) => void) => void;
78-
stopVideoRecording: () => Promise<any>;
79-
takeScreenshot: (fileName: string, saveToCameraRoll: boolean) => Promise<any>;
80-
resetARSession: (resetTracking: any, removeAnchors: any) => void;
81-
setWorldOrigin: (worldOrigin: ViroWorldOrigin) => void;
82-
project: (point: Viro3DPoint) => Promise<any>;
83-
unproject: (point: Viro3DPoint) => Promise<any>;
84-
viroAppProps: any;
85-
};
8656
constructor(props: Props);
87-
getRandomTag(): string;
57+
private _startVideoRecording;
58+
/**
59+
* Stops recording the video of the Viro Renderer.
60+
*
61+
* returns Object w/ success, url and errorCode keys.
62+
* @returns Promise that resolves when the video has stopped recording.
63+
*/
64+
private _stopVideoRecording;
65+
/**
66+
* Takes a screenshot of the Viro renderer
67+
* @param fileName - name of the file (without extension)
68+
* @param saveToCameraRoll - whether or not the file should also be saved to the camera roll
69+
* returns Object w/ success, url and errorCode keys.
70+
*/
71+
private _takeScreenshot;
72+
_project(point: Viro3DPoint): Promise<any>;
73+
getRandomTag: () => string;
8874
/**
8975
* Pushes a scene and reference it with the given key if provided.
9076
* If the scene has been previously pushed, we simply show the scene again.
@@ -99,7 +85,7 @@ export declare class ViroARSceneNavigator extends React.Component<Props, State>
9985
*
10086
* @todo: use Typescript function overloading rather than this inaccurate solution
10187
*/
102-
push(param1?: ViroScene | string, param2?: ViroScene): void;
88+
push: (param1?: ViroScene | string, param2?: ViroScene) => void;
10389
/**
10490
* Replace the top scene in the stack with the given scene. The remainder of the back
10591
* history is kept in the same order as before.
@@ -111,7 +97,7 @@ export declare class ViroARSceneNavigator extends React.Component<Props, State>
11197
*
11298
* @todo: use Typescript function overloading rather than this inaccurate solution
11399
*/
114-
replace(param1?: ViroScene | string, param2?: ViroScene): void;
100+
replace: (param1?: ViroScene | string, param2?: ViroScene) => void;
115101
/**
116102
* Jumps to a given scene that had been previously pushed. If the scene was not pushed, we
117103
* then push and jump to it. The back history is re-ordered such that jumped to scenes are
@@ -124,44 +110,70 @@ export declare class ViroARSceneNavigator extends React.Component<Props, State>
124110
*
125111
* @todo: use Typescript function overloading rather than this inaccurate solution
126112
*/
127-
jump(param1?: ViroScene | string, param2?: ViroScene): void;
128-
pop(): void;
129-
popN(n: number): void;
113+
jump: (param1?: ViroScene | string, param2?: ViroScene) => void;
114+
pop: () => void;
115+
popN: (n: number) => void;
130116
/**
131117
* Increments the reference count for a scene within sceneDictionary that is
132118
* mapped to the given sceneKey. If no scenes are found / mapped, we create
133119
* one, initialize it with a reference count of 1, and store it within the
134120
* sceneDictionary for future reference.
135121
*/
136-
incrementSceneReference(scene: ViroScene, sceneKey: string, limitOne: boolean): void;
122+
incrementSceneReference: (scene: ViroScene, sceneKey: string, limitOne: boolean) => void;
137123
/**
138124
* Decrements the reference count for the last N scenes within
139125
* the sceneHistory by 1. If nothing else references that given scene
140126
* (counts equals 0), we then remove that scene from sceneDictionary.
141127
*/
142-
decrementReferenceForLastNScenes(n: number): void;
128+
decrementReferenceForLastNScenes: (n: number) => void;
143129
/**
144130
* Adds the given sceneKey to the sceneHistory and updates the currentSceneIndex to point
145131
* to the scene on the top of the history stack (the most recent scene).
146132
*/
147-
addToHistory(sceneKey: string): void;
133+
addToHistory: (sceneKey: string) => void;
148134
/**
149135
* Instead of preserving history, we find the last pushed sceneKey within the history stack
150136
* matching the given sceneKey and re-order it to the front. We then update the
151137
* currentSceneIndex to point to the scene on the top of the history stack
152138
* (the most recent scene).
153139
*/
154-
reorderHistory(sceneKey: string): void;
140+
reorderHistory: (sceneKey: string) => void;
155141
popHistoryByN(n: number): void;
156-
getSceneIndex(sceneTag: string): number;
157-
_startVideoRecording(fileName: string, saveToCameraRoll: boolean, onError: (errorCode: number) => void): void;
158-
_stopVideoRecording(): Promise<any>;
159-
_takeScreenshot(fileName: string, saveToCameraRoll: boolean): Promise<any>;
160-
_project(point: Viro3DPoint): Promise<any>;
161-
_unproject(point: Viro3DPoint): Promise<any>;
162-
_resetARSession(resetTracking: any, removeAnchors: any): void;
163-
_setWorldOrigin(worldOrigin: ViroWorldOrigin): void;
164-
_renderSceneStackItems(): JSX.Element[];
142+
getSceneIndex: (sceneTag: string) => number;
143+
private _unproject;
144+
private _resetARSession;
145+
private _setWorldOrigin;
146+
private _renderSceneStackItems;
147+
arSceneNavigator: {
148+
push: (param1?: ViroScene | string, param2?: ViroScene) => void;
149+
pop: () => void;
150+
popN: (n: number) => void;
151+
jump: (param1?: ViroScene | string, param2?: ViroScene) => void;
152+
replace: (param1?: ViroScene | string, param2?: ViroScene) => void;
153+
startVideoRecording: (fileName: string, saveToCameraRoll: boolean, onError: (errorCode: number) => void) => void;
154+
stopVideoRecording: () => Promise<any>;
155+
takeScreenshot: (fileName: string, saveToCameraRoll: boolean) => Promise<any>;
156+
resetARSession: (resetTracking: any, removeAnchors: any) => void;
157+
setWorldOrigin: (worldOrigin: ViroWorldOrigin) => void;
158+
project: (point: Viro3DPoint) => Promise<any>;
159+
unproject: (point: Viro3DPoint) => Promise<any>;
160+
viroAppProps: any;
161+
};
162+
sceneNavigator: {
163+
push: (param1?: ViroScene | string, param2?: ViroScene) => void;
164+
pop: () => void;
165+
popN: (n: number) => void;
166+
jump: (param1?: ViroScene | string, param2?: ViroScene) => void;
167+
replace: (param1?: ViroScene | string, param2?: ViroScene) => void;
168+
startVideoRecording: (fileName: string, saveToCameraRoll: boolean, onError: (errorCode: number) => void) => void;
169+
stopVideoRecording: () => Promise<any>;
170+
takeScreenshot: (fileName: string, saveToCameraRoll: boolean) => Promise<any>;
171+
resetARSession: (resetTracking: any, removeAnchors: any) => void;
172+
setWorldOrigin: (worldOrigin: ViroWorldOrigin) => void;
173+
project: (point: Viro3DPoint) => Promise<any>;
174+
unproject: (point: Viro3DPoint) => Promise<any>;
175+
viroAppProps: any;
176+
};
165177
render(): JSX.Element;
166178
}
167179
export {};

0 commit comments

Comments
 (0)