Advertisement
Guest User

hybrid

a guest
Sep 20th, 2019
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.60 KB | None | 0 0
  1. import React from 'react';
  2. import {
  3. NavigationParams,
  4. NavigationScreenProp,
  5. NavigationState,
  6. } from 'react-navigation';
  7. import {
  8. View,
  9. TouchableOpacity,
  10. Text,
  11. StyleSheet,
  12. Dimensions,
  13. RCTNativeAppEventEmitter,
  14. Button,
  15. } from 'react-native';
  16. import Orientation from 'react-native-orientation';
  17.  
  18. export interface Props {
  19. navigation: NavigationScreenProp<NavigationState, NavigationParams>;
  20. orientation: string;
  21. count: number;
  22. boardSizeNumber: number;
  23. }
  24.  
  25. interface NavigationParams {
  26. boardSize: string;
  27. }
  28.  
  29. const numbers = [
  30. {index: 0, value: '1'},
  31. {index: 1, value: '2'},
  32. {index: 2, value: '3'},
  33. {index: 3, value: '4'},
  34. {index: 4, value: '5'},
  35. {index: 5, value: '6'},
  36. {index: 6, value: '7'},
  37. {index: 7, value: '8'},
  38. {index: 8, value: '9'},
  39. {index: 9, value: '10'},
  40. {index: 10, value: '11'},
  41. {index: 11, value: '12'},
  42. {index: 12, value: '13'},
  43. {index: 13, value: '14'},
  44. {index: 14, value: '15'},
  45. {index: 15, value: ' '},
  46. ];
  47.  
  48. export class GameScreen extends React.Component<Props> {
  49. static navigationOptions = {title: 'Game Screen'};
  50.  
  51. constructor(props: Props) {
  52. super(props);
  53.  
  54. this.state = {
  55. count: 0,
  56. screenOrientationIsPortrait: Boolean,
  57. boardSizeNumber: parseInt(this.props.navigation.state.params!.boardSize),
  58. };
  59. }
  60.  
  61. componentWillMount() {
  62. // The getOrientation method is async. It happens sometimes that
  63. // you need the orientation at the moment the JS runtime starts running on device.
  64. // `getInitialOrientation` returns directly because its a constant set at the
  65. // beginning of the JS runtime.
  66.  
  67. const initial = Orientation.getInitialOrientation();
  68. if (initial === 'PORTRAIT') {
  69. // do something
  70. this.setState({
  71. screenOrientationIsPortrait: true,
  72. });
  73. } else {
  74. // do something else
  75. this.setState({
  76. screenOrientationIsPortrait: false,
  77. });
  78. }
  79. }
  80.  
  81. componentDidMount() {
  82. // this locks the view to Portrait Mode
  83. //Orientation.lockToPortrait();
  84.  
  85. // this locks the view to Landscape Mode
  86. // Orientation.lockToLandscape();
  87.  
  88. // this unlocks any previous locks to all Orientations
  89. // Orientation.unlockAllOrientations();
  90.  
  91. Orientation.addOrientationListener(this._orientationDidChange);
  92. }
  93.  
  94. _orientationDidChange = orientation => {
  95. if (orientation === 'LANDSCAPE') {
  96. // do something with landscape layout
  97. this.setState({
  98. screenOrientationIsPortrait: false,
  99. });
  100. } else {
  101. // do something with portrait layout
  102. this.setState({
  103. screenOrientationIsPortrait: true,
  104. });
  105. }
  106. };
  107.  
  108. componentWillUnmount() {
  109. Orientation.getOrientation((err, orientation) => {
  110. console.log(`Current Device Orientation: ${orientation}`);
  111. });
  112.  
  113. // Remember to remove listener
  114. Orientation.removeOrientationListener(this._orientationDidChange);
  115. }
  116.  
  117. onPress = () => {
  118. this.setState({
  119. count: this.state.count + 1,
  120. });
  121. };
  122.  
  123. render() {
  124. const {navigate} = this.props.navigation;
  125.  
  126. return (
  127. <View
  128. style={
  129. this.state.screenOrientationIsPortrait === true
  130. ? this.styles.masterContainer
  131. : this.styles.masterContainerLandscape
  132. }>
  133. <View style={{flexGrow: 1, flex: 1}}>
  134. <Text
  135. style={{
  136. textAlign: 'center',
  137. textAlignVertical: 'center',
  138. height: '100%',
  139. fontSize: 30,
  140. }}>
  141. {}
  142. Moves: {this.state.count}
  143. </Text>
  144. </View>
  145.  
  146. <View style={{flexGrow: 1, flex: 1}}>
  147. <Text
  148. style={{
  149. textAlign: 'center',
  150. textAlignVertical: 'center',
  151. height: '100%',
  152. fontSize: 30,
  153. }}>
  154. Time: 1:30
  155. {this.state.screenOrientationIsPortrait === true
  156. ? 'Portrait'
  157. : 'Landscape'}
  158. </Text>
  159. </View>
  160.  
  161. <View style={this.styles.gameContainer}>
  162. <View style={{flexDirection: 'row', flex: 1}}>
  163. <View style={this.styles.gameButtonView}>
  164. <TouchableOpacity
  165. onPress={this.onPress}
  166. style={this.styles.gameButtonTouchableOpacity}>
  167. <Text style={this.styles.gameButtonText}>
  168. {numbers.find(v => v.index === 0).value}
  169. </Text>
  170. </TouchableOpacity>
  171. </View>
  172. <View style={this.styles.gameButtonView}>
  173. <TouchableOpacity
  174. onPress={this.onPress}
  175. style={this.styles.gameButtonTouchableOpacity}>
  176. <Text style={this.styles.gameButtonText}>
  177. {numbers.find(v => v.index === 1).value}
  178. </Text>
  179. </TouchableOpacity>
  180. </View>
  181. <View style={this.styles.gameButtonView}>
  182. <TouchableOpacity
  183. onPress={this.onPress}
  184. style={this.styles.gameButtonTouchableOpacity}>
  185. <Text style={this.styles.gameButtonText}>
  186. {numbers.find(v => v.index === 2).value}
  187. </Text>
  188. </TouchableOpacity>
  189. </View>
  190. <View style={this.styles.gameButtonView}>
  191. <TouchableOpacity
  192. onPress={this.onPress}
  193. style={this.styles.gameButtonTouchableOpacity}>
  194. <Text style={this.styles.gameButtonText}>
  195. {numbers.find(v => v.index === 3).value}
  196. </Text>
  197. </TouchableOpacity>
  198. </View>
  199. </View>
  200.  
  201. <View style={{flexDirection: 'row', flex: 1}}>
  202. <View style={this.styles.gameButtonView}>
  203. <TouchableOpacity
  204. onPress={this.onPress}
  205. style={this.styles.gameButtonTouchableOpacity}>
  206. <Text style={this.styles.gameButtonText}>
  207. {numbers.find(v => v.index === 4).value}
  208. </Text>
  209. </TouchableOpacity>
  210. </View>
  211. <View style={this.styles.gameButtonView}>
  212. <TouchableOpacity
  213. onPress={this.onPress}
  214. style={this.styles.gameButtonTouchableOpacity}>
  215. <Text style={this.styles.gameButtonText}>
  216. {numbers.find(v => v.index === 5).value}
  217. </Text>
  218. </TouchableOpacity>
  219. </View>
  220. <View style={this.styles.gameButtonView}>
  221. <TouchableOpacity
  222. onPress={this.onPress}
  223. style={this.styles.gameButtonTouchableOpacity}>
  224. <Text style={this.styles.gameButtonText}>
  225. {numbers.find(v => v.index === 6).value}
  226. </Text>
  227. </TouchableOpacity>
  228. </View>
  229. <View style={this.styles.gameButtonView}>
  230. <TouchableOpacity
  231. onPress={this.onPress}
  232. style={this.styles.gameButtonTouchableOpacity}>
  233. <Text style={this.styles.gameButtonText}>
  234. {numbers.find(v => v.index === 7).value}
  235. </Text>
  236. </TouchableOpacity>
  237. </View>
  238. </View>
  239.  
  240. <View style={{flexDirection: 'row', flex: 1}}>
  241. <View style={this.styles.gameButtonView}>
  242. <TouchableOpacity
  243. onPress={this.onPress}
  244. style={this.styles.gameButtonTouchableOpacity}>
  245. <Text style={this.styles.gameButtonText}>
  246. {numbers.find(v => v.index === 8).value}
  247. </Text>
  248. </TouchableOpacity>
  249. </View>
  250. <View style={this.styles.gameButtonView}>
  251. <TouchableOpacity
  252. onPress={this.onPress}
  253. style={this.styles.gameButtonTouchableOpacity}>
  254. <Text style={this.styles.gameButtonText}>
  255. {numbers.find(v => v.index === 9).value}
  256. </Text>
  257. </TouchableOpacity>
  258. </View>
  259. <View style={this.styles.gameButtonView}>
  260. <TouchableOpacity
  261. onPress={this.onPress}
  262. style={this.styles.gameButtonTouchableOpacity}>
  263. <Text style={this.styles.gameButtonText}>
  264. {numbers.find(v => v.index === 10).value}
  265. </Text>
  266. </TouchableOpacity>
  267. </View>
  268. <View style={this.styles.gameButtonView}>
  269. <TouchableOpacity
  270. onPress={this.onPress}
  271. style={this.styles.gameButtonTouchableOpacity}>
  272. <Text style={this.styles.gameButtonText}>
  273. {numbers.find(v => v.index === 11).value}
  274. </Text>
  275. </TouchableOpacity>
  276. </View>
  277. </View>
  278.  
  279. <View style={{flexDirection: 'row', flex: 1}}>
  280. <View style={this.styles.gameButtonView}>
  281. <TouchableOpacity
  282. onPress={this.onPress}
  283. style={this.styles.gameButtonTouchableOpacity}>
  284. <Text style={this.styles.gameButtonText}>
  285. {numbers.find(v => v.index === 12).value}
  286. </Text>
  287. </TouchableOpacity>
  288. </View>
  289. <View style={this.styles.gameButtonView}>
  290. <TouchableOpacity
  291. onPress={this.onPress}
  292. style={this.styles.gameButtonTouchableOpacity}>
  293. <Text style={this.styles.gameButtonText}>
  294. {numbers.find(v => v.index === 13).value}
  295. </Text>
  296. </TouchableOpacity>
  297. </View>
  298. <View style={this.styles.gameButtonView}>
  299. <TouchableOpacity
  300. onPress={this.onPress}
  301. style={this.styles.gameButtonTouchableOpacity}>
  302. <Text style={this.styles.gameButtonText}>
  303. {numbers.find(v => v.index === 14).value}
  304. </Text>
  305. </TouchableOpacity>
  306. </View>
  307. <View style={this.styles.gameButtonView}>
  308. <TouchableOpacity
  309. onPress={this.onPress}
  310. style={this.styles.gameButtonTouchableOpacityEmpty}>
  311. <Text>{numbers.find(v => v.index === 15).value}</Text>
  312. </TouchableOpacity>
  313. </View>
  314. </View>
  315. </View>
  316. </View>
  317. );
  318. }
  319.  
  320. styles = StyleSheet.create({
  321. masterContainer: {
  322. flexDirection: 'column',
  323. width: '100%',
  324. height: '100%',
  325. },
  326. masterContainerLandscape: {
  327. flexDirection: 'row',
  328. width: '100%',
  329. height: '100%',
  330. },
  331. gameContainer: {
  332. flexGrow: 2,
  333. flex: 1,
  334. flexDirection: 'column',
  335. padding: 5,
  336. },
  337. gameContainerLandscape: {
  338. flexGrow: 3,
  339. flex: 1,
  340. flexDirection: 'column',
  341. padding: 5,
  342. },
  343. gameButtonView: {
  344. backgroundColor: '#1a1a1a',
  345. borderColor: '#000',
  346. borderWidth: 2,
  347. flex: 1,
  348. flexGrow: 1,
  349. },
  350.  
  351. gameButtonTouchableOpacity: {
  352. height: '100%',
  353. alignItems: 'center',
  354. backgroundColor: '#262626',
  355. },
  356.  
  357. gameButtonTouchableOpacityEmpty: {
  358. height: '100%',
  359. alignItems: 'center',
  360. backgroundColor: '#fff',
  361. },
  362.  
  363. gameButtonText: {
  364. height: '100%',
  365. color: '#fff',
  366. textAlign: 'center',
  367. textAlignVertical: 'center',
  368. fontSize: 30,
  369. },
  370. });
  371. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement