Guest User

Untitled

a guest
Jul 20th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.81 KB | None | 0 0
  1. export class MyApp {
  2. @ViewChild("myNav") nav: NavController;
  3.  
  4. rootPage: any = LearnPage;
  5.  
  6. pages: Array<{title: string, component: any}>;
  7.  
  8. constructor(public platform: Platform, public statusBar: StatusBar, public splashScreen: SplashScreen) {
  9. this.initializeApp();
  10.  
  11. this.pages = [
  12. { title: 'Learn', component: LearnPage },
  13. { title: 'Work', component: WorkPage},
  14. { title: 'Play', component: PlayPage}
  15. ];
  16.  
  17. }
  18.  
  19. initializeApp() {
  20. this.platform.ready().then(() => {
  21. this.statusBar.styleDefault();
  22. this.splashScreen.hide();
  23. });
  24. }
  25.  
  26. openPage(page) {
  27. this.nav.setRoot(page.component);
  28. }
  29. }
  30.  
  31. <ion-menu [content]="content">
  32. <ion-header>
  33. <ion-toolbar>
  34. <ion-title>Menu</ion-title>
  35. </ion-toolbar>
  36. </ion-header>
  37.  
  38. <ion-content>
  39. <ion-list>
  40. <button menuClose ion-item *ngFor="let p of pages" (click)="openPage(p)">
  41. {{p.title}}
  42. </button>
  43. </ion-list>
  44. </ion-content>
  45.  
  46. <ion-header>
  47. <ion-navbar>
  48. <button ion-button menuToggle>
  49. <ion-icon name="menu"></ion-icon>
  50. </button>
  51. <ion-title>Learn</ion-title>
  52. </ion-navbar>
  53. </ion-header>
  54.  
  55.  
  56. <ion-content>
  57. <div class="padding-default">
  58. <grid class="canvas-disable"></grid>
  59. </div>
  60. <ion-item no-lines class="padding-default">
  61. <ion-select class="default-select" [(ngModel)]="number" (ionChange)="onChange()">
  62. <ion-option *ngFor="let number of numbers" [value]="number">{{number}} {{shape}}</ion-option>
  63. </ion-select>
  64. </ion-item>
  65. </ion-content>
  66.  
  67. export class LearnPage {
  68. @ViewChild(Select) select: Select;
  69.  
  70. shape: string = shapeNames.SQUARE;
  71. number: number = 1;
  72. numbers: number[] = Array.from(Array(100),(x,i)=>i+1);
  73.  
  74. constructor(public events: Events, public eventNames: EventNames) {
  75.  
  76. }
  77.  
  78. ngAfterViewInit() {
  79. this.events.publish(this.eventNames.GRID_COUNT, this.number, this.shape);
  80. }
  81.  
  82. onChange() {
  83. this.events.publish(this.eventNames.GRID_COUNT, this.number, this.shape);
  84. }
  85. }
  86.  
  87. <div #rendererContainer></div>
  88.  
  89. export class GridComponent {
  90. @ViewChild('rendererContainer') rendererContainer: ElementRef;
  91.  
  92. canvasWidth: any;
  93. grid: Shape[][];
  94. color: number = 0xff0000;
  95.  
  96. renderer = new THREE.WebGLRenderer();
  97. scene = null;
  98. camera = null;
  99. mesh = null;
  100.  
  101. constructor(public events: Events, public eventNames: EventNames) {
  102. this.canvasWidth = window.innerWidth * 86 / 100;
  103. this.grid = new Array<Array<Shape>>();
  104. for (let y = 0; y < 10; y++) {
  105. let row: Shape[] = new Array<Shape>();
  106. for (let x = 0; x < 10; x++) {
  107. row.push(new Shape(y * 10 + x));
  108. }
  109. this.grid.push(row);
  110. }
  111.  
  112. this.scene = new THREE.Scene();
  113. this.camera = new THREE.OrthographicCamera(0, this.canvasWidth, 0, this.canvasWidth, 1, 1000);
  114. this.drawGrid();
  115. this.events.subscribe(this.eventNames.GRID_COUNT, (x, s) => { this.count(x, s); });
  116. console.log("Suscribe");
  117. }
  118.  
  119. ngAfterViewInit() {
  120. this.renderer.setSize(this.canvasWidth, this.canvasWidth);
  121. this.rendererContainer.nativeElement.appendChild(this.renderer.domElement);
  122. this.renderer.render(this.scene, this.camera);
  123. }
  124.  
  125. ngOnDestroy() {
  126. console.log("Grid ngOnDestroy");
  127. this.events.unsubscribe(this.eventNames.GRID_COUNT);
  128. }
  129.  
  130. drawGrid() {
  131. let size = this.canvasWidth + 1;
  132. let divisions = 10;
  133. let gridHelper = new THREE.GridHelper(size, divisions, 0x000000, 0x000000);
  134. gridHelper.rotation.x = Math.PI / 2;
  135. gridHelper.position.z = -900;
  136. gridHelper.position.x = this.canvasWidth / 2;
  137. gridHelper.position.y = this.canvasWidth / 2;
  138. this.scene.add(gridHelper);
  139.  
  140. let material = new THREE.MeshBasicMaterial({ color: 0xffffff, side: THREE.DoubleSide });
  141. let plane = new THREE.Mesh(new THREE.PlaneGeometry(this.canvasWidth, this.canvasWidth), material);
  142. plane.position.z = -1000;
  143. plane.position.x = this.canvasWidth / 2;
  144. plane.position.y = this.canvasWidth / 2;
  145. this.scene.add(plane);
  146. }
  147.  
  148. drawMesh(shape: Shape, x, y) {
  149. let geometry = new THREE.BoxGeometry(this.canvasWidth / 11, this.canvasWidth / 11, this.canvasWidth / 11);
  150. let material = new THREE.MeshBasicMaterial({ color: this.color });
  151. this.mesh = new THREE.Mesh(geometry, material);
  152. this.mesh.position.z = -100;
  153. this.mesh.position.x = x * this.canvasWidth / 10 + this.canvasWidth / 20;
  154. this.mesh.position.y = y * this.canvasWidth / 10 + this.canvasWidth / 20;
  155. this.mesh.name = shape.id
  156. shape.mesh = this.mesh;
  157. this.scene.add(this.mesh);
  158. this.renderer.render(this.scene, this.camera);
  159. shape.filled = !shape.filled;
  160. }
  161.  
  162. count(x: number, s: string) {
  163. console.log("Count event received");
  164. while (this.scene.children.length > 0) {
  165. this.scene.remove(this.scene.children[0]);
  166. }
  167. this.drawGrid();
  168. for (let i = 0; i < x; i++) {
  169. this.drawMesh(this.grid[i % 10][Math.floor(i / 10)], i % 10, Math.floor(i / 10));
  170. }
  171. }
  172. }
Add Comment
Please, Sign In to add comment