Improve inventory
This commit is contained in:
parent
ee5ab45cfb
commit
27c6a8bcd0
12 changed files with 99 additions and 17 deletions
|
@ -1,5 +1,6 @@
|
|||
import * as gui from './index.mjs';
|
||||
import GuiElement from './element.mjs';
|
||||
import {context} from '../graphics/index.mjs';
|
||||
|
||||
export default class GuiText extends GuiElement {
|
||||
constructor(text = '', x, y, w, h, {
|
||||
|
@ -14,8 +15,33 @@ export default class GuiText extends GuiElement {
|
|||
this.type = 'text';
|
||||
this.color = color;
|
||||
this.text = text;
|
||||
this.font = size + 'pt Consolas';
|
||||
this.spacing = size * 1.2;
|
||||
this.font = size + 'px Consolas';
|
||||
this.align = align;
|
||||
this.valign = valign;
|
||||
}
|
||||
|
||||
splitLines() {
|
||||
// Not very robust, but good enough for now. Mebe fix later?
|
||||
context.font = this.font;
|
||||
let maxLineLength = (this.w / context.measureText('A').width) | 0;
|
||||
maxLineLength = Math.max(maxLineLength, 1);
|
||||
|
||||
let lines = [];
|
||||
let currentLine = '';
|
||||
|
||||
this.text.split('\n').forEach(l => {
|
||||
currentLine = '';
|
||||
l.split(' ').forEach(word => {
|
||||
if (word.length + currentLine.length > maxLineLength) {
|
||||
lines.push(currentLine.slice(0, -1));
|
||||
currentLine = '';
|
||||
}
|
||||
currentLine += word + ' ';
|
||||
});
|
||||
lines.push(currentLine.slice(0, -1));
|
||||
});
|
||||
|
||||
this.text = lines.join('\n');
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue