Application Android et IOS pour l'amicale des élèves
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Mascot.js 7.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. // @flow
  2. import * as React from 'react';
  3. import * as Animatable from 'react-native-animatable';
  4. import {Image, TouchableWithoutFeedback, View} from 'react-native';
  5. import type {ViewStyle} from 'react-native/Libraries/StyleSheet/StyleSheet';
  6. export type AnimatableViewRefType = {current: null | Animatable.View};
  7. type PropsType = {
  8. emotion?: number,
  9. animated?: boolean,
  10. style?: ViewStyle | null,
  11. entryAnimation?: Animatable.AnimatableProperties | null,
  12. loopAnimation?: Animatable.AnimatableProperties | null,
  13. onPress?: null | ((viewRef: AnimatableViewRefType) => void),
  14. onLongPress?: null | ((viewRef: AnimatableViewRefType) => void),
  15. };
  16. type StateType = {
  17. currentEmotion: number,
  18. };
  19. const MASCOT_IMAGE = require('../../../assets/mascot/mascot.png');
  20. const MASCOT_EYES_NORMAL = require('../../../assets/mascot/mascot_eyes_normal.png');
  21. const MASCOT_EYES_GIRLY = require('../../../assets/mascot/mascot_eyes_girly.png');
  22. const MASCOT_EYES_CUTE = require('../../../assets/mascot/mascot_eyes_cute.png');
  23. const MASCOT_EYES_WINK = require('../../../assets/mascot/mascot_eyes_wink.png');
  24. const MASCOT_EYES_HEART = require('../../../assets/mascot/mascot_eyes_heart.png');
  25. const MASCOT_EYES_ANGRY = require('../../../assets/mascot/mascot_eyes_angry.png');
  26. const MASCOT_GLASSES = require('../../../assets/mascot/mascot_glasses.png');
  27. const MASCOT_SUNGLASSES = require('../../../assets/mascot/mascot_sunglasses.png');
  28. export const EYE_STYLE = {
  29. NORMAL: 0,
  30. GIRLY: 2,
  31. CUTE: 3,
  32. WINK: 4,
  33. HEART: 5,
  34. ANGRY: 6,
  35. };
  36. const GLASSES_STYLE = {
  37. NORMAL: 0,
  38. COOl: 1,
  39. };
  40. export const MASCOT_STYLE = {
  41. NORMAL: 0,
  42. HAPPY: 1,
  43. GIRLY: 2,
  44. WINK: 3,
  45. CUTE: 4,
  46. INTELLO: 5,
  47. LOVE: 6,
  48. COOL: 7,
  49. ANGRY: 8,
  50. RANDOM: 999,
  51. };
  52. class Mascot extends React.Component<PropsType, StateType> {
  53. static defaultProps = {
  54. emotion: MASCOT_STYLE.NORMAL,
  55. animated: false,
  56. style: null,
  57. entryAnimation: {
  58. useNativeDriver: true,
  59. animation: 'rubberBand',
  60. duration: 2000,
  61. },
  62. loopAnimation: {
  63. useNativeDriver: true,
  64. animation: 'swing',
  65. duration: 2000,
  66. iterationDelay: 250,
  67. iterationCount: 'infinite',
  68. },
  69. onPress: null,
  70. onLongPress: null,
  71. };
  72. viewRef: AnimatableViewRefType;
  73. eyeList: {[key: number]: number | string};
  74. glassesList: {[key: number]: number | string};
  75. onPress: (viewRef: AnimatableViewRefType) => void;
  76. onLongPress: (viewRef: AnimatableViewRefType) => void;
  77. initialEmotion: number;
  78. constructor(props: PropsType) {
  79. super(props);
  80. this.viewRef = React.createRef();
  81. this.eyeList = {};
  82. this.glassesList = {};
  83. this.eyeList[EYE_STYLE.NORMAL] = MASCOT_EYES_NORMAL;
  84. this.eyeList[EYE_STYLE.GIRLY] = MASCOT_EYES_GIRLY;
  85. this.eyeList[EYE_STYLE.CUTE] = MASCOT_EYES_CUTE;
  86. this.eyeList[EYE_STYLE.WINK] = MASCOT_EYES_WINK;
  87. this.eyeList[EYE_STYLE.HEART] = MASCOT_EYES_HEART;
  88. this.eyeList[EYE_STYLE.ANGRY] = MASCOT_EYES_ANGRY;
  89. this.glassesList[GLASSES_STYLE.NORMAL] = MASCOT_GLASSES;
  90. this.glassesList[GLASSES_STYLE.COOl] = MASCOT_SUNGLASSES;
  91. this.initialEmotion =
  92. props.emotion != null ? props.emotion : Mascot.defaultProps.emotion;
  93. if (this.initialEmotion === MASCOT_STYLE.RANDOM)
  94. this.initialEmotion = Math.floor(Math.random() * MASCOT_STYLE.ANGRY) + 1;
  95. this.state = {
  96. currentEmotion: this.initialEmotion,
  97. };
  98. if (props.onPress == null) {
  99. this.onPress = (viewRef: AnimatableViewRefType) => {
  100. const ref = viewRef.current;
  101. if (ref != null) {
  102. this.setState({currentEmotion: MASCOT_STYLE.LOVE});
  103. ref.rubberBand(1500).then(() => {
  104. this.setState({currentEmotion: this.initialEmotion});
  105. });
  106. }
  107. };
  108. } else this.onPress = props.onPress;
  109. if (props.onLongPress == null) {
  110. this.onLongPress = (viewRef: AnimatableViewRefType) => {
  111. const ref = viewRef.current;
  112. if (ref != null) {
  113. this.setState({currentEmotion: MASCOT_STYLE.ANGRY});
  114. ref.tada(1000).then(() => {
  115. this.setState({currentEmotion: this.initialEmotion});
  116. });
  117. }
  118. };
  119. } else this.onLongPress = props.onLongPress;
  120. }
  121. getGlasses(style: number): React.Node {
  122. const glasses = this.glassesList[style];
  123. return (
  124. <Image
  125. key="glasses"
  126. source={
  127. glasses != null ? glasses : this.glassesList[GLASSES_STYLE.NORMAL]
  128. }
  129. style={{
  130. position: 'absolute',
  131. top: '15%',
  132. left: 0,
  133. width: '100%',
  134. height: '100%',
  135. }}
  136. />
  137. );
  138. }
  139. getEye(
  140. style: number,
  141. isRight: boolean,
  142. rotation: string = '0deg',
  143. ): React.Node {
  144. const eye = this.eyeList[style];
  145. return (
  146. <Image
  147. key={isRight ? 'right' : 'left'}
  148. source={eye != null ? eye : this.eyeList[EYE_STYLE.NORMAL]}
  149. style={{
  150. position: 'absolute',
  151. top: '15%',
  152. left: isRight ? '-11%' : '11%',
  153. width: '100%',
  154. height: '100%',
  155. transform: [{rotateY: rotation}],
  156. }}
  157. />
  158. );
  159. }
  160. getEyes(emotion: number): React.Node {
  161. const final = [];
  162. final.push(
  163. <View
  164. key="container"
  165. style={{
  166. position: 'absolute',
  167. width: '100%',
  168. height: '100%',
  169. }}
  170. />,
  171. );
  172. if (emotion === MASCOT_STYLE.CUTE) {
  173. final.push(this.getEye(EYE_STYLE.CUTE, true));
  174. final.push(this.getEye(EYE_STYLE.CUTE, false));
  175. } else if (emotion === MASCOT_STYLE.GIRLY) {
  176. final.push(this.getEye(EYE_STYLE.GIRLY, true));
  177. final.push(this.getEye(EYE_STYLE.GIRLY, false));
  178. } else if (emotion === MASCOT_STYLE.HAPPY) {
  179. final.push(this.getEye(EYE_STYLE.WINK, true));
  180. final.push(this.getEye(EYE_STYLE.WINK, false));
  181. } else if (emotion === MASCOT_STYLE.WINK) {
  182. final.push(this.getEye(EYE_STYLE.WINK, true));
  183. final.push(this.getEye(EYE_STYLE.NORMAL, false));
  184. } else if (emotion === MASCOT_STYLE.LOVE) {
  185. final.push(this.getEye(EYE_STYLE.HEART, true));
  186. final.push(this.getEye(EYE_STYLE.HEART, false));
  187. } else if (emotion === MASCOT_STYLE.ANGRY) {
  188. final.push(this.getEye(EYE_STYLE.ANGRY, true));
  189. final.push(this.getEye(EYE_STYLE.ANGRY, false, '180deg'));
  190. } else if (emotion === MASCOT_STYLE.COOL) {
  191. final.push(this.getGlasses(GLASSES_STYLE.COOl));
  192. } else {
  193. final.push(this.getEye(EYE_STYLE.NORMAL, true));
  194. final.push(this.getEye(EYE_STYLE.NORMAL, false));
  195. }
  196. if (emotion === MASCOT_STYLE.INTELLO) {
  197. // Needs to have normal eyes behind the glasses
  198. final.push(this.getGlasses(GLASSES_STYLE.NORMAL));
  199. }
  200. final.push(<View key="container2" />);
  201. return final;
  202. }
  203. render(): React.Node {
  204. const {props, state} = this;
  205. const entryAnimation = props.animated ? props.entryAnimation : null;
  206. const loopAnimation = props.animated ? props.loopAnimation : null;
  207. return (
  208. <Animatable.View
  209. style={{
  210. aspectRatio: 1,
  211. ...props.style,
  212. }}
  213. // eslint-disable-next-line react/jsx-props-no-spreading
  214. {...entryAnimation}>
  215. <TouchableWithoutFeedback
  216. onPress={() => {
  217. this.onPress(this.viewRef);
  218. }}
  219. onLongPress={() => {
  220. this.onLongPress(this.viewRef);
  221. }}>
  222. <Animatable.View ref={this.viewRef}>
  223. <Animatable.View
  224. // eslint-disable-next-line react/jsx-props-no-spreading
  225. {...loopAnimation}>
  226. <Image
  227. source={MASCOT_IMAGE}
  228. style={{
  229. width: '100%',
  230. height: '100%',
  231. }}
  232. />
  233. {this.getEyes(state.currentEmotion)}
  234. </Animatable.View>
  235. </Animatable.View>
  236. </TouchableWithoutFeedback>
  237. </Animatable.View>
  238. );
  239. }
  240. }
  241. export default Mascot;