From a1bc63b732cee3284502262320ea0a867a838db7 Mon Sep 17 00:00:00 2001 From: Raisess Date: Wed, 4 Nov 2020 13:31:17 -0300 Subject: [PATCH] setted to round all place numbers, don't get location errors with float numbers --- package.json | 2 +- src/Display.ts | 14 +++++++------- src/data/bgColors.ts | 2 +- src/modules/validatePlace.ts | 2 +- src/test.ts | 2 +- 5 files changed, 11 insertions(+), 11 deletions(-) diff --git a/package.json b/package.json index aa61e5a..44aa8d9 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "term-display", - "version": "1.0.9", + "version": "1.1.0", "description": "Terminal display lib.", "main": "./dist/Display.js", "scripts": { diff --git a/src/Display.ts b/src/Display.ts index 98659b0..d953cd0 100644 --- a/src/Display.ts +++ b/src/Display.ts @@ -41,8 +41,8 @@ export default class Display { * } */ constructor(size: IPlace, whiteSpace?: string) { - this.size.x = size.x + 1; - this.size.y = size.y + 1; + this.size.x = Math.round(size.x) + 1; + this.size.y = Math.round(size.y) + 1; this.whiteSpace = whiteSpace ? whiteSpace : this.whiteSpace; this.clear(); // Creates the window. @@ -111,17 +111,17 @@ export default class Display { if (validatePlace(place, this.size)) { if (value.length > 1) { // Check if is more than 1 pixel. for (let i: number = 0; i < value.length; i++) { - this.display[place.y][place.x + i] = generateDisplayablePixel(this.currentBgColor, color_, value[i]); + this.display[Math.round(place.y)][Math.round(place.x) + i] = generateDisplayablePixel(this.currentBgColor, color_, value[i]); } } else { - this.display[place.y][place.x] = generateDisplayablePixel(this.currentBgColor, color_, value); + this.display[Math.round(place.y)][Math.round(place.x)] = generateDisplayablePixel(this.currentBgColor, color_, value); } if (!noSave) { this.pixels.push({ place: { - x: place.x, - y: place.y + x: Math.round(place.x), + y: Math.round(place.y) }, idx: this.pixels.length, value: value, @@ -147,7 +147,7 @@ export default class Display { if (validatePlace(place, this.size)) { for (let pixel of this.pixels) { if(pixel) { // When clear, pixel is setted to undefined. - if (place.x === pixel.place.x && place.y === pixel.place.y) { + if (Math.round(place.x) === pixel.place.x && Math.round(place.y) === pixel.place.y) { return pixel; } } diff --git a/src/data/bgColors.ts b/src/data/bgColors.ts index da23ff4..2433556 100644 --- a/src/data/bgColors.ts +++ b/src/data/bgColors.ts @@ -15,5 +15,5 @@ export const bgColors: any = { brMagenta: 105, brWhite: 107, brBlack: 100 -} +}; diff --git a/src/modules/validatePlace.ts b/src/modules/validatePlace.ts index 180765b..686a00b 100644 --- a/src/modules/validatePlace.ts +++ b/src/modules/validatePlace.ts @@ -1,7 +1,7 @@ import { IPlace } from "../interfaces/IPlace"; export default function validatePlace(place: IPlace, size: IPlace): boolean | void { - if (place.y <= size.y && place.x <= size.x) return true; + if (Math.round(place.y) <= size.y && Math.round(place.x) <= size.x) return true; throw new Error(`Invalid pixel location ${place.x}:${place.y}!`); } diff --git a/src/test.ts b/src/test.ts index 2e93318..696bb2a 100644 --- a/src/test.ts +++ b/src/test.ts @@ -12,7 +12,7 @@ const display: Display = new Display({ x: window.width, y: window.height }, ".") const title: string = "center title asdfghjkl"; display.setPixel({ - x: (Math.round(window.width / 2)) - (Math.round(title.length / 2)), + x: (window.width / 2) - (title.length / 2), y: 1 }, title.toUpperCase(), COLOR.yellow);