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 8.5KB

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