Skip to content
This repository has been archived by the owner on May 2, 2022. It is now read-only.

Commit

Permalink
fix bug for GTextField:
Browse files Browse the repository at this point in the history
1, text align does not work while it's being moving.
2, TextMetrics.wordWrap from official code adds extra useless blank to the head of the first text line, see issue: pixijs/pixijs#4251, and this is the hot fix for it. I've pushed this patch to the official Guithub as a pull-request already, once they merge it, I'll clear my code again.
fix bug for GTextInput.ts
1, text align does not work.
  • Loading branch information
jcyuan committed Nov 10, 2017
1 parent d15c130 commit e00cc6d
Show file tree
Hide file tree
Showing 3 changed files with 135 additions and 9 deletions.
34 changes: 26 additions & 8 deletions src/GTextField.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ namespace fgui {

protected $style: PIXI.TextStyle;
protected $verticalAlign: VertAlignType = VertAlignType.Top;
protected $alignYOffset: number = 0;
protected $offset: PIXI.Point = new PIXI.Point();
protected $color: number;
protected $singleLine:boolean = true;

Expand Down Expand Up @@ -399,7 +399,7 @@ namespace fgui {

this.applyStyle();
this.$textField.$updateMinHeight();
let wordWrap = (!this.$widthAutoSize && !this.$singleLine && this.autoSize != AutoSizeType.Shrink);
let wordWrap = !this.$widthAutoSize && this.multipleLine;
this.$textField.width = this.$textField.style.wordWrapWidth = wordWrap ? Math.ceil(this.width) : 10000;
this.$textField.style.wordWrap = wordWrap;
this.$textField.style.breakWords = wordWrap;
Expand Down Expand Up @@ -691,10 +691,23 @@ namespace fgui {
charX += letterSpacing;
}
}

});
}

public localToGlobal(ax: number = 0, ay: number = 0, resultPoint?: PIXI.Point): PIXI.Point {
let r = super.localToGlobal(ax, ay, resultPoint);
r.x -= this.$offset.x;
r.y -= this.$offset.y;
return r;
}

public globalToLocal(ax: number = 0, ay: number = 0, resultPoint?: PIXI.Point): PIXI.Point {
let r = super.globalToLocal(ax, ay, resultPoint);
r.x -= this.$offset.x;
r.y -= this.$offset.y;
return r;
}

protected handleSizeChanged(): void {
if (this.$updatingSize)
return;
Expand Down Expand Up @@ -740,13 +753,13 @@ namespace fgui {
th *= this.displayObject.scale.y;
}
if (this.$verticalAlign == VertAlignType.Top || th == 0)
this.$alignYOffset = GTextField.GUTTER_Y;
this.$offset.y = GTextField.GUTTER_Y;
else {
let dh: number = Math.max(0, this.height - th);
if (this.$verticalAlign == VertAlignType.Middle)
this.$alignYOffset = dh * .5;
this.$offset.y = dh * .5;
else if(this.$verticalAlign == VertAlignType.Bottom)
this.$alignYOffset = dh;
this.$offset.y = dh;
}

let xPos = 0;
Expand All @@ -759,14 +772,19 @@ namespace fgui {
xPos = this.width - tw;
break;
}
this.$offset.x = xPos;

this.updatePosition();
}

this.displayObject.position.set(Math.floor(this.x + xPos), Math.floor(this.y + this.$alignYOffset));
private updatePosition():void {
this.displayObject.position.set(Math.floor(this.x + this.$offset.x), Math.floor(this.y + this.$offset.y));
}

protected handleXYChanged(): void {
super.handleXYChanged();
if (this.$displayObject)
this.$displayObject.y += this.$alignYOffset;
this.updatePosition();
}

public setupBeforeAdd(xml: utils.XmlNode): void {
Expand Down
106 changes: 106 additions & 0 deletions src/PIXI/extras/Text.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/*this class is temporarily for the bug fixing purpose only, so once PIXI releases a new version, this class will be removed */

namespace PIXI.extras {

export class Text extends PIXI.Text {

private static __init:boolean = false;

public constructor(text?:string, style?:PIXI.TextStyle, canvas?:HTMLCanvasElement) {
super(text, style, canvas);

if(!PIXI.extras.Text.__init) {
PIXI.extras.Text.__init = true;

//override
PIXI.TextMetrics.wordWrap = function(text, style, canvas)
{
if(!canvas) canvas = (PIXI.TextMetrics as any)["_canvas"];

const context = canvas.getContext('2d');

// Greedy wrapping algorithm that will wrap words as the line grows longer
// than its horizontal bounds.
let result = '';
let firstChar = text.charAt(0);
const lines = text.split('\n');
const wordWrapWidth = style.wordWrapWidth;
const characterCache:{ [char:string] : number } = {};

for (let i = 0; i < lines.length; i++)
{
let spaceLeft = wordWrapWidth;
const words = lines[i].split(' ');

for (let j = 0; j < words.length; j++)
{
const wordWidth = context.measureText(words[j]).width;

if (style.breakWords && wordWidth > wordWrapWidth)
{
// Word should be split in the middle
const characters = words[j].split('');

for (let c = 0; c < characters.length; c++)
{
const character = characters[c];
let characterWidth = characterCache[character];

if (characterWidth === undefined)
{
characterWidth = context.measureText(character).width;
characterCache[character] = characterWidth;
}

if (characterWidth > spaceLeft)
{
result += `\n${character}`;
spaceLeft = wordWrapWidth - characterWidth;
}
else
{
if (c === 0 && (j > 0 || firstChar == ' '))
{
result += ' ';
}

result += character;
spaceLeft -= characterWidth;
}
}
}
else
{
const wordWidthWithSpace = wordWidth + context.measureText(' ').width;

if (j === 0 || wordWidthWithSpace > spaceLeft)
{
// Skip printing the newline if it's the first word of the line that is
// greater than the word wrap width.
if (j > 0)
{
result += '\n';
}
result += words[j];
spaceLeft = wordWrapWidth - wordWidth;
}
else
{
spaceLeft -= wordWidthWithSpace;
result += ` ${words[j]}`;
}
}
}

if (i < lines.length - 1)
{
result += '\n';
}
}

return result;
}
}
}
}
}
4 changes: 3 additions & 1 deletion src/display/UITextField.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
///<reference path="../PIXI/extras/Text.ts" />

namespace fgui {

export class UITextField extends PIXI.Text implements IUIObject {
export class UITextField extends PIXI.extras.Text implements IUIObject {

public UIOwner:GObject;

Expand Down

0 comments on commit e00cc6d

Please sign in to comment.