Skip to content

Commit

Permalink
setted to round all place numbers, don't get location errors with flo…
Browse files Browse the repository at this point in the history
…at numbers
  • Loading branch information
Raisess committed Nov 4, 2020
1 parent 9a83d4c commit a1bc63b
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 11 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "term-display",
"version": "1.0.9",
"version": "1.1.0",
"description": "Terminal display lib.",
"main": "./dist/Display.js",
"scripts": {
Expand Down
14 changes: 7 additions & 7 deletions src/Display.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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,
Expand All @@ -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;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/data/bgColors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ export const bgColors: any = {
brMagenta: 105,
brWhite: 107,
brBlack: 100
}
};

2 changes: 1 addition & 1 deletion src/modules/validatePlace.ts
Original file line number Diff line number Diff line change
@@ -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}!`);
}
Expand Down
2 changes: 1 addition & 1 deletion src/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down

0 comments on commit a1bc63b

Please sign in to comment.