Skip to content

Commit

Permalink
feat(simulation): aspect resolution support
Browse files Browse the repository at this point in the history
* #13
* #4

Signed-off-by: Richard Lea <[email protected]>
  • Loading branch information
chigix committed Jun 29, 2019
1 parent c290cfb commit f3260f4
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 28 deletions.
4 changes: 0 additions & 4 deletions src/app/modules/simulation/resolutions.ts

This file was deleted.

3 changes: 2 additions & 1 deletion src/app/modules/simulation/simulation-outlet.component.html
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
<div class="main-layout game-classroom" [style.height.px]="resolution.height">
<div class="main-layout game-classroom" [style.height]="resolution.height"
[style.width]="resolution.width">
<ng-template scene-host></ng-template>
</div>
9 changes: 9 additions & 0 deletions src/app/modules/simulation/simulation-outlet.component.scss
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
:host {
display: flex;
background-color: rgba(0, 0, 0, 0.9);
width: 100vw;
height: 100vh;
align-items: center;
justify-content: center;
}

div.main-layout {
width: 100%;
position: relative;
Expand Down
64 changes: 41 additions & 23 deletions src/app/modules/simulation/simulation-outlet.component.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
import {
Component, OnInit, ComponentFactoryResolver,
ViewChild, ComponentRef,
} from '@angular/core';
import { BreakpointObserver, Breakpoints } from '@angular/cdk/layout';
import { filter } from 'rxjs/operators';
import { Component, OnInit, ViewChild, ComponentRef, HostListener } from '@angular/core';
import { first, filter, map } from 'rxjs/operators';
import {
SimulationServiceBase, StaticSessionService,
} from 'app/renpi/services';

import { SimulationService } from './simulation.service';
import { SceneHostDirective } from './scene-host.directive';
import { Resolution } from './resolutions';

@Component({
selector: 'ren-simulation',
Expand All @@ -22,31 +17,54 @@ export class SimulationOutletComponent implements OnInit {

@ViewChild(SceneHostDirective) sceneHost: SceneHostDirective;

private currentScene: ComponentRef<any>;

resolution: Resolution;
resolution = { width: '100vw', height: '100vh' };

constructor(
private breakpointObserver: BreakpointObserver,
private simulationService: SimulationServiceBase,
public staticSessionService: StaticSessionService,
) {
this.breakpointObserver.observe([
Breakpoints.XSmall, Breakpoints.Small, Breakpoints.Medium])
.pipe(filter(result => result.matches))
.subscribe(result => this.resolution = { width: 1066, height: 600 });
this.breakpointObserver.observe(Breakpoints.Large)
.pipe(filter(result => result.matches))
.subscribe(result => this.resolution = { width: 1280, height: 720 });
this.breakpointObserver.observe(Breakpoints.XLarge)
.pipe(filter(result => result.matches))
.subscribe(result => this.resolution = { width: 1920, height: 1080 });
}
) { }

ngOnInit() {
if (this.simulationService instanceof SimulationService) {
this.simulationService.setOutlet(this);
}
this.onResize({ currentTarget: window });
}

@HostListener('window:resize', ['$event'])
onResize(event?: { currentTarget: { innerWidth: number, innerHeight: number } }) {
this.simulationService.initObserve.pipe(
first(), map(init => init.screenAspect), filter(aspect => !!aspect)
).subscribe(aspect => {
if (aspect.height === aspect.width) {
if (event.currentTarget.innerHeight > event.currentTarget.innerWidth) {
this.resolution.height = '100vw';
this.resolution.width = '100vw';
} else {
this.resolution.height = '100vh';
this.resolution.width = '100vh';
}
} else if ((aspect.height - aspect.width) *
(event.currentTarget.innerHeight - event.currentTarget.innerWidth) < 0) {
if (event.currentTarget.innerHeight > event.currentTarget.innerWidth) {
this.resolution.height = (aspect.height * event.currentTarget.innerWidth / aspect.width) + 'px';
this.resolution.width = '100vw';
} else {
this.resolution.height = '100vh';
this.resolution.width = (aspect.width * event.currentTarget.innerHeight / aspect.height) + 'px';
}
} else {
const widthRate = event.currentTarget.innerWidth / aspect.width;
const heightRate = event.currentTarget.innerHeight / aspect.height;
if (widthRate < heightRate) {
this.resolution.height = (aspect.height * widthRate) + 'px';
this.resolution.width = '100vw';
} else {
this.resolution.height = '100vh';
this.resolution.width = (aspect.width * heightRate) + 'px';
}
}
});
}

}
3 changes: 3 additions & 0 deletions src/app/modules/simulation/simulation.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,9 @@ export class SimulationService implements OnDestroy, SimulationServiceBase {
name: 'ren:contextName',
title: 'ren:contextTitle',
entryScene: 'ren:nextScene',
screenAspect: 'ren:screenAspect',
width: 'ren:screenAspectWidth',
height: 'ren:screenAspectHeight',
version: 'http://schema.org/version',
interfaceVersion: 'http://schema.org/schemaVersion',
});
Expand Down

0 comments on commit f3260f4

Please sign in to comment.