split layout.ts

This commit is contained in:
Eon S. Jeon 2018-12-08 01:46:38 +09:00
parent a82f86fdc2
commit 687943f10c
5 changed files with 194 additions and 118 deletions

28
src/layouts/ilayout.ts Normal file
View File

@ -0,0 +1,28 @@
// Copyright (c) 2018 Eon S. Jeon <esjeon@hyunmu.am>
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
interface ILayout {
apply(tiles: Tile[], area: Rect): void;
handleUserInput(input: UserInput, data?: any): boolean;
/* if true, layout completely overrides the default behavior */
isEnabled(): boolean;
}

View File

@ -0,0 +1,33 @@
// Copyright (c) 2018 Eon S. Jeon <esjeon@hyunmu.am>
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
class MonocleLayout implements ILayout {
public apply = (tiles: Tile[], area: Rect): void => {
tiles.forEach((tile) => tile.geometry.copyFrom(area));
}
public handleUserInput(input: UserInput) {
return false;
}
public isEnabled(): boolean {
return Config.enableMonocleLayout;
}
}

View File

@ -0,0 +1,68 @@
// Copyright (c) 2018 Eon S. Jeon <esjeon@hyunmu.am>
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
class SpreadLayout implements ILayout {
private space: number; /* in ratio */
constructor() {
this.space = 0.07;
}
public apply = (tiles: Tile[], area: Rect): void => {
let numTiles = tiles.length;
const spaceWidth = Math.floor(area.width * this.space);
let cardWidth = area.width - (spaceWidth * (numTiles - 1));
// TODO: define arbitrary constants
const miniumCardWidth = area.width * 0.40;
while (cardWidth < miniumCardWidth) {
cardWidth += spaceWidth;
numTiles -= 1;
}
for (let i = 0; i < tiles.length; i++)
tiles[i].geometry.set(
area.x + ((i < numTiles) ? spaceWidth * (numTiles - i - 1) : 0),
area.y,
cardWidth,
area.height,
);
}
public handleUserInput(input: UserInput) {
switch (input) {
case UserInput.Decrease:
// TODO: define arbitrary constants
this.space = Math.max(0.04, this.space - 0.01);
break;
case UserInput.Increase:
// TODO: define arbitrary constants
this.space = Math.min(0.10, this.space + 0.01);
break;
default:
return false;
}
return true;
}
public isEnabled(): boolean {
return Config.enableSpreadLayout;
}
}

View File

@ -0,0 +1,65 @@
// Copyright (c) 2018 Eon S. Jeon <esjeon@hyunmu.am>
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
class StairLayout implements ILayout {
private space: number; /* in PIXELS */
constructor() {
this.space = 24;
}
public apply = (tiles: Tile[], area: Rect): void => {
const len = tiles.length;
const space = this.space;
// TODO: limit the maximum number of staired windows.
for (let i = 0; i < len; i++) {
const dx = space * (len - i - 1);
const dy = space * i;
tiles[i].geometry.set(
area.x + dx,
area.y + dy,
area.width - dx,
area.height - dy,
);
}
}
public handleUserInput(input: UserInput) {
switch (input) {
case UserInput.Decrease:
// TODO: define arbitrary constants
this.space = Math.max(16, this.space - 8);
break;
case UserInput.Increase:
// TODO: define arbitrary constants
this.space = Math.min(160, this.space + 8);
break;
default:
return false;
}
return true;
}
public isEnabled(): boolean {
return Config.enableStairLayout;
}
}

View File

@ -18,15 +18,6 @@
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
interface ILayout {
apply(tiles: Tile[], area: Rect): void;
handleUserInput(input: UserInput, data?: any): boolean;
/* if true, layout completely overrides the default behavior */
isEnabled(): boolean;
}
class TileLayout implements ILayout {
private numMaster: number;
private masterRatio: number; /* in ratio */
@ -107,112 +98,3 @@ class TileLayout implements ILayout {
return Config.enableTileLayout;
}
}
class MonocleLayout implements ILayout {
public apply = (tiles: Tile[], area: Rect): void => {
tiles.forEach((tile) => tile.geometry.copyFrom(area));
}
public handleUserInput(input: UserInput) {
return false;
}
public isEnabled(): boolean {
return Config.enableMonocleLayout;
}
}
class SpreadLayout implements ILayout {
private space: number; /* in ratio */
constructor() {
this.space = 0.07;
}
public apply = (tiles: Tile[], area: Rect): void => {
let numTiles = tiles.length;
const spaceWidth = Math.floor(area.width * this.space);
let cardWidth = area.width - (spaceWidth * (numTiles - 1));
// TODO: define arbitrary constants
const miniumCardWidth = area.width * 0.40;
while (cardWidth < miniumCardWidth) {
cardWidth += spaceWidth;
numTiles -= 1;
}
for (let i = 0; i < tiles.length; i++)
tiles[i].geometry.set(
area.x + ((i < numTiles) ? spaceWidth * (numTiles - i - 1) : 0),
area.y,
cardWidth,
area.height,
);
}
public handleUserInput(input: UserInput) {
switch (input) {
case UserInput.Decrease:
// TODO: define arbitrary constants
this.space = Math.max(0.04, this.space - 0.01);
break;
case UserInput.Increase:
// TODO: define arbitrary constants
this.space = Math.min(0.10, this.space + 0.01);
break;
default:
return false;
}
return true;
}
public isEnabled(): boolean {
return Config.enableSpreadLayout;
}
}
class StairLayout implements ILayout {
private space: number; /* in PIXELS */
constructor() {
this.space = 24;
}
public apply = (tiles: Tile[], area: Rect): void => {
const len = tiles.length;
const space = this.space;
// TODO: limit the maximum number of staired windows.
for (let i = 0; i < len; i++) {
const dx = space * (len - i - 1);
const dy = space * i;
tiles[i].geometry.set(
area.x + dx,
area.y + dy,
area.width - dx,
area.height - dy,
);
}
}
public handleUserInput(input: UserInput) {
switch (input) {
case UserInput.Decrease:
// TODO: define arbitrary constants
this.space = Math.max(16, this.space - 8);
break;
case UserInput.Increase:
// TODO: define arbitrary constants
this.space = Math.min(160, this.space + 8);
break;
default:
return false;
}
return true;
}
public isEnabled(): boolean {
return Config.enableStairLayout;
}
}