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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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. type Props = {
  7. style?: ViewStyle,
  8. emotion: number,
  9. animated: boolean,
  10. entryAnimation: Animatable.AnimatableProperties | null,
  11. loopAnimation: Animatable.AnimatableProperties | null,
  12. onPress?: (viewRef: AnimatableViewRef) => null,
  13. onLongPress?: (viewRef: AnimatableViewRef) => null,
  14. }
  15. type State = {
  16. currentEmotion: number,
  17. }
  18. export type AnimatableViewRef = {current: null | Animatable.View};
  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<Props, State> {
  53. static defaultProps = {
  54. animated: false,
  55. entryAnimation: {
  56. useNativeDriver: true,
  57. animation: "rubberBand",
  58. duration: 2000,
  59. },
  60. loopAnimation: {
  61. useNativeDriver: true,
  62. animation: "swing",
  63. duration: 2000,
  64. iterationDelay: 250,
  65. iterationCount: "infinite",
  66. },
  67. clickAnimation: {
  68. useNativeDriver: true,
  69. animation: "rubberBand",
  70. duration: 2000,
  71. },
  72. }
  73. viewRef: AnimatableViewRef;
  74. eyeList: { [key: number]: number | string };
  75. glassesList: { [key: number]: number | string };
  76. onPress: (viewRef: AnimatableViewRef) => null;
  77. onLongPress: (viewRef: AnimatableViewRef) => null;
  78. initialEmotion: number;
  79. constructor(props: Props) {
  80. super(props);
  81. this.viewRef = React.createRef();
  82. this.eyeList = {};
  83. this.glassesList = {};
  84. this.eyeList[EYE_STYLE.NORMAL] = MASCOT_EYES_NORMAL;
  85. this.eyeList[EYE_STYLE.GIRLY] = MASCOT_EYES_GIRLY;
  86. this.eyeList[EYE_STYLE.CUTE] = MASCOT_EYES_CUTE;
  87. this.eyeList[EYE_STYLE.WINK] = MASCOT_EYES_WINK;
  88. this.eyeList[EYE_STYLE.HEART] = MASCOT_EYES_HEART;
  89. this.eyeList[EYE_STYLE.ANGRY] = MASCOT_EYES_ANGRY;
  90. this.glassesList[GLASSES_STYLE.NORMAL] = MASCOT_GLASSES;
  91. this.glassesList[GLASSES_STYLE.COOl] = MASCOT_SUNGLASSES;
  92. this.initialEmotion = this.props.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 (this.props.onPress == null) {
  99. this.onPress = (viewRef: AnimatableViewRef) => {
  100. let 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. return null;
  108. }
  109. } else
  110. this.onPress = this.props.onPress;
  111. if (this.props.onLongPress == null) {
  112. this.onLongPress = (viewRef: AnimatableViewRef) => {
  113. let ref = viewRef.current;
  114. if (ref != null) {
  115. this.setState({currentEmotion: MASCOT_STYLE.ANGRY});
  116. ref.tada(1000).then(() => {
  117. this.setState({currentEmotion: this.initialEmotion});
  118. });
  119. }
  120. return null;
  121. }
  122. } else
  123. this.onLongPress = this.props.onLongPress;
  124. }
  125. getGlasses(style: number) {
  126. const glasses = this.glassesList[style];
  127. return <Image
  128. key={"glasses"}
  129. source={glasses != null ? glasses : this.glassesList[GLASSES_STYLE.NORMAL]}
  130. style={{
  131. position: "absolute",
  132. top: "15%",
  133. left: 0,
  134. width: "100%",
  135. height: "100%",
  136. }}
  137. />
  138. }
  139. getEye(style: number, isRight: boolean, rotation: string="0deg") {
  140. const eye = this.eyeList[style];
  141. return <Image
  142. key={isRight ? "right" : "left"}
  143. source={eye != null ? eye : this.eyeList[EYE_STYLE.NORMAL]}
  144. style={{
  145. position: "absolute",
  146. top: "15%",
  147. left: isRight ? "-11%" : "11%",
  148. width: "100%",
  149. height: "100%",
  150. transform: [{rotateY: rotation}]
  151. }}
  152. />
  153. }
  154. getEyes(emotion: number) {
  155. let final = [];
  156. final.push(<View
  157. key={"container"}
  158. style={{
  159. position: "absolute",
  160. width: "100%",
  161. height: "100%",
  162. }}/>);
  163. if (emotion === MASCOT_STYLE.CUTE) {
  164. final.push(this.getEye(EYE_STYLE.CUTE, true));
  165. final.push(this.getEye(EYE_STYLE.CUTE, false));
  166. } else if (emotion === MASCOT_STYLE.GIRLY) {
  167. final.push(this.getEye(EYE_STYLE.GIRLY, true));
  168. final.push(this.getEye(EYE_STYLE.GIRLY, false));
  169. } else if (emotion === MASCOT_STYLE.HAPPY) {
  170. final.push(this.getEye(EYE_STYLE.WINK, true));
  171. final.push(this.getEye(EYE_STYLE.WINK, false));
  172. } else if (emotion === MASCOT_STYLE.WINK) {
  173. final.push(this.getEye(EYE_STYLE.WINK, true));
  174. final.push(this.getEye(EYE_STYLE.NORMAL, false));
  175. } else if (emotion === MASCOT_STYLE.LOVE) {
  176. final.push(this.getEye(EYE_STYLE.HEART, true));
  177. final.push(this.getEye(EYE_STYLE.HEART, false));
  178. } else if (emotion === MASCOT_STYLE.ANGRY) {
  179. final.push(this.getEye(EYE_STYLE.ANGRY, true));
  180. final.push(this.getEye(EYE_STYLE.ANGRY, false, "180deg"));
  181. } else if (emotion === MASCOT_STYLE.COOL) {
  182. final.push(this.getGlasses(GLASSES_STYLE.COOl));
  183. } else {
  184. final.push(this.getEye(EYE_STYLE.NORMAL, true));
  185. final.push(this.getEye(EYE_STYLE.NORMAL, false));
  186. }
  187. if (emotion === MASCOT_STYLE.INTELLO) { // Needs to have normal eyes behind the glasses
  188. final.push(this.getGlasses(GLASSES_STYLE.NORMAL));
  189. }
  190. final.push(<View key={"container2"}/>);
  191. return final;
  192. }
  193. render() {
  194. const entryAnimation = this.props.animated ? this.props.entryAnimation : null;
  195. const loopAnimation = this.props.animated ? this.props.loopAnimation : null;
  196. return (
  197. <Animatable.View
  198. style={{
  199. aspectRatio: 1,
  200. ...this.props.style
  201. }}
  202. {...entryAnimation}
  203. >
  204. <TouchableWithoutFeedback
  205. onPress={() => this.onPress(this.viewRef)}
  206. onLongPress={() => this.onLongPress(this.viewRef)}
  207. >
  208. <Animatable.View
  209. ref={this.viewRef}
  210. >
  211. <Animatable.View
  212. {...loopAnimation}
  213. >
  214. <Image
  215. source={MASCOT_IMAGE}
  216. style={{
  217. width: "100%",
  218. height:"100%",
  219. }}
  220. />
  221. {this.getEyes(this.state.currentEmotion)}
  222. </Animatable.View>
  223. </Animatable.View>
  224. </TouchableWithoutFeedback>
  225. </Animatable.View>
  226. );
  227. }
  228. }
  229. export default Mascot;