Skip to content

Commit

Permalink
feat: expose Rive RendererType to choose renderer
Browse files Browse the repository at this point in the history
  • Loading branch information
HayesGordon committed May 31, 2024
1 parent 092813a commit 88cac53
Show file tree
Hide file tree
Showing 11 changed files with 178 additions and 8 deletions.
35 changes: 35 additions & 0 deletions RNRiveRendererType.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
//
// RNRiveRendererType.swift
// rive-react-native
//
// Created by Peter G Hayes on 31/05/2024.
//

import Foundation
import RiveRuntime

enum RNRiveRendererType: String {
case Rive = "riveRenderer"
case Skia = "skiaRenderer"
case CoreGraphics = "cgRenderer"

static func mapToRNRiveRendererType(value: String) -> RNRiveRendererType {
if let rnEnum = RNRiveRendererType(rawValue: value) {
return rnEnum
} else {
fatalError("Unsupported renderer type: \(value)")
}
}

static func mapToRendererType(rnRendererType: RNRiveRendererType) -> RendererType {
switch rnRendererType {
case .Rive:
return RendererType.riveRenderer
case .Skia:
return RendererType.skiaRenderer
case .CoreGraphics:
return RendererType.cgRenderer
}
}
}

24 changes: 24 additions & 0 deletions RiveReactNativeRendererModule.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//
// RiveReactNativeRendererModule.swift
// rive-react-native
//
// Created by Peter G Hayes on 31/05/2024.
//

import Foundation
import RiveRuntime

@objc(RiveReactNativeRendererModule)
class RiveReactNativeRendererModule: NSObject {

@objc(defaultRenderer:androidRenderer:)
func defaultRenderer(_ iosRenderer: String, androidRenderer: String) -> Void {
let rnRendererType = RNRiveRendererType.mapToRNRiveRendererType(value: iosRenderer)
RenderContextManager.shared().defaultRenderer = RNRiveRendererType.mapToRendererType(rnRendererType: rnRendererType)
}
// Required to register the module with React Native
@objc static func requiresMainQueueSetup() -> Bool {
return true
}

}
14 changes: 14 additions & 0 deletions RiveReactNativeRendererModuleBridge.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//
// RiveReactNativeRendererModuleBridge.m
// rive-react-native
//
// Created by Peter G Hayes on 31/05/2024.
//

#import <React/RCTBridgeModule.h>

@interface RCT_EXTERN_MODULE(RiveReactNativeRendererModule, NSObject)

RCT_EXTERN_METHOD(defaultRenderer:(NSString *)iosRenderer androidRenderer:(NSString *)androidRenderer)

@end
28 changes: 28 additions & 0 deletions android/src/main/java/com/rivereactnative/RNRiveRendererType.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.rivereactnative

import app.rive.runtime.kotlin.core.RendererType

enum class RNRiveRendererType(private val rendererTypeName: String) {
Rive("riveRenderer"),
Skia("skiaRenderer"),
Canvas("canvasRenderer");

override fun toString(): String {
return rendererTypeName
}

companion object {
fun mapToRNRiveRendererType(rendererType: String): RNRiveRendererType {
return values().first { it.rendererTypeName == rendererType }
}

fun mapToRiveRendererType(rnRendererType: RNRiveRendererType): RendererType {
return when (rnRendererType) {
Rive -> RendererType.Rive
Skia -> RendererType.Skia
Canvas -> RendererType.Canvas
}
}
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import com.facebook.react.uimanager.ViewManager


class RiveReactNativePackage : ReactPackage {
override fun createNativeModules(reactContext: ReactApplicationContext): List<NativeModule> {
return emptyList()
}
override fun createNativeModules(
reactContext: ReactApplicationContext
): MutableList<NativeModule> = listOf(RiveReactNativeRendererModule(reactContext)).toMutableList()

override fun createViewManagers(reactContext: ReactApplicationContext): List<ViewManager<*, *>> {
return listOf(RiveReactNativeViewManager())
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.rivereactnative

import com.facebook.react.bridge.ReactApplicationContext
import com.facebook.react.bridge.ReactContextBaseJavaModule
import com.facebook.react.bridge.ReactMethod
import app.rive.runtime.kotlin.core.Rive

class RiveReactNativeRendererModule(reactContext: ReactApplicationContext) : ReactContextBaseJavaModule(reactContext) {
override fun getName() = "RiveReactNativeRendererModule"

@ReactMethod fun defaultRenderer(iosRenderer: String, androidRenderer: String) {
val rnRendererType = RNRiveRendererType.mapToRNRiveRendererType(androidRenderer);
val rendererType = RNRiveRendererType.mapToRiveRendererType(rnRendererType);
Rive.init(reactApplicationContext, defaultRenderer = rendererType)
}
}
4 changes: 2 additions & 2 deletions example/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { AppRegistry } from 'react-native';
import App from './src/App';
import { name as appName } from './app.json';
import Main from './src/App';

AppRegistry.registerComponent(appName, () => App);
AppRegistry.registerComponent(appName, () => Main);
29 changes: 27 additions & 2 deletions example/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as React from 'react';
import React, { useEffect } from 'react';
import { createStackNavigator } from '@react-navigation/stack';
import { NavigationContainer } from '@react-navigation/native';
import { SafeAreaProvider } from 'react-native-safe-area-context';
Expand All @@ -17,9 +17,34 @@ import DynamicText from './DynamicText';
import NestedInputs from './NestedInputs';
import Events from './Events';

import {
RiveRenderer,
RiveRendererAndroid,
RiveRendererIOS,
} from 'rive-react-native';

const Stack = createStackNavigator();

export default function App() {
export default function Main() {
// Configure the defualt renderer to use for both iOS and Android.
// For more information: https://rive.app/community/doc/overview/docD20dU9Rod
//
// This is optional. The current defaults are:
// - iOS: Skia
// - Android: Skia
// In the future the default will be the Rive Renderer (RiveRendererIOS.Rive and RiveRendererAndroid.Rive)
//
useEffect(() => {
RiveRenderer.defaultRenderer(
RiveRendererIOS.Rive,
RiveRendererAndroid.Skia
);
}, []);

return <App />;
}

function App() {
return (
<SafeAreaProvider>
<NavigationContainer>
Expand Down
7 changes: 7 additions & 0 deletions src/Rive.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
TouchableWithoutFeedback,
GestureResponderEvent,
StyleProp,
NativeModules,
} from 'react-native';
import {
RiveRef,
Expand All @@ -19,11 +20,17 @@ import {
ViewManagerMethod,
RiveGeneralEvent,
RiveOpenUrlEvent,
RiveRendererInterface,
} from './types';
import { convertErrorFromNativeToRN, XOR } from './helpers';

import { Alignment, Fit } from './types';

const { RiveReactNativeRendererModule } = NativeModules;

export const RiveRenderer =
RiveReactNativeRendererModule as RiveRendererInterface;

type RiveProps = {
onPlay?: (
event: NativeSyntheticEvent<{
Expand Down
4 changes: 3 additions & 1 deletion src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
export * from './types';
import Rive from './Rive';
import Rive, { RiveRenderer } from './Rive';

export { RiveRenderer };

export default Rive;
19 changes: 19 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,18 @@ export enum Direction {
Forwards = 'forwards',
}

export enum RiveRendererIOS {
Rive = 'riveRenderer',
Skia = 'skiaRenderer',
CoreGraphics = 'cgRenderer',
}

export enum RiveRendererAndroid {
Rive = 'riveRenderer',
Skia = 'skiaRenderer',
Canvas = 'canvasRenderer',
}

export enum RNRiveErrorType {
FileNotFound = 'FileNotFound',
UnsupportedRuntimeVersion = 'UnsupportedRuntimeVersion',
Expand Down Expand Up @@ -101,3 +113,10 @@ export interface RiveOpenUrlEvent extends RiveEvent {
url?: string;
target?: string;
}

export interface RiveRendererInterface {
defaultRenderer(
iosRenderer: RiveRendererIOS,
androidRenderer: RiveRendererAndroid
): void;
}

0 comments on commit 88cac53

Please sign in to comment.