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

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