|
@@ -4,8 +4,8 @@ module BABYLON.GUI {
|
|
export class InputText extends Control implements IFocusableControl {
|
|
export class InputText extends Control implements IFocusableControl {
|
|
private _text = "";
|
|
private _text = "";
|
|
private _placeholderText = "";
|
|
private _placeholderText = "";
|
|
- private _background = "black";
|
|
|
|
- private _focusedBackground = "black";
|
|
|
|
|
|
+ private _background = "#222222";
|
|
|
|
+ private _focusedBackground = "#000000";
|
|
private _placeholderColor = "gray";
|
|
private _placeholderColor = "gray";
|
|
private _thickness = 1;
|
|
private _thickness = 1;
|
|
private _margin = new ValueAndUnit(10, ValueAndUnit.UNITMODE_PIXEL);
|
|
private _margin = new ValueAndUnit(10, ValueAndUnit.UNITMODE_PIXEL);
|
|
@@ -16,7 +16,9 @@ module BABYLON.GUI {
|
|
private _blinkIsEven = false;
|
|
private _blinkIsEven = false;
|
|
private _cursorOffset = 0;
|
|
private _cursorOffset = 0;
|
|
private _scrollLeft: number;
|
|
private _scrollLeft: number;
|
|
-
|
|
|
|
|
|
+ private _textWidth: number;
|
|
|
|
+ private _clickedCoordinate: number;
|
|
|
|
+
|
|
public promptMessage = "Please enter text:";
|
|
public promptMessage = "Please enter text:";
|
|
|
|
|
|
public onTextChangedObservable = new Observable<InputText>();
|
|
public onTextChangedObservable = new Observable<InputText>();
|
|
@@ -305,10 +307,10 @@ module BABYLON.GUI {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
- let textWidth = context.measureText(text).width;
|
|
|
|
|
|
+ this._textWidth = context.measureText(text).width;
|
|
let marginWidth = this._margin.getValueInPixel(this._host, parentMeasure.width) * 2;
|
|
let marginWidth = this._margin.getValueInPixel(this._host, parentMeasure.width) * 2;
|
|
if (this._autoStretchWidth) {
|
|
if (this._autoStretchWidth) {
|
|
- this.width = Math.min(this._maxWidth.getValueInPixel(this._host, parentMeasure.width), textWidth + marginWidth) + "px";
|
|
|
|
|
|
+ this.width = Math.min(this._maxWidth.getValueInPixel(this._host, parentMeasure.width), this._textWidth + marginWidth) + "px";
|
|
}
|
|
}
|
|
|
|
|
|
let rootY = this._fontOffset.ascent + (this._currentMeasure.height - this._fontOffset.height) / 2;
|
|
let rootY = this._fontOffset.ascent + (this._currentMeasure.height - this._fontOffset.height) / 2;
|
|
@@ -318,8 +320,8 @@ module BABYLON.GUI {
|
|
context.rect(clipTextLeft, this._currentMeasure.top + (this._currentMeasure.height - this._fontOffset.height) / 2, availableWidth + 2, this._currentMeasure.height);
|
|
context.rect(clipTextLeft, this._currentMeasure.top + (this._currentMeasure.height - this._fontOffset.height) / 2, availableWidth + 2, this._currentMeasure.height);
|
|
context.clip();
|
|
context.clip();
|
|
|
|
|
|
- if (this._isFocused && textWidth > availableWidth) {
|
|
|
|
- let textLeft = clipTextLeft - textWidth + availableWidth;
|
|
|
|
|
|
+ if (this._isFocused && this._textWidth > availableWidth) {
|
|
|
|
+ let textLeft = clipTextLeft - this._textWidth + availableWidth;
|
|
if (!this._scrollLeft) {
|
|
if (!this._scrollLeft) {
|
|
this._scrollLeft = textLeft;
|
|
this._scrollLeft = textLeft;
|
|
}
|
|
}
|
|
@@ -331,10 +333,37 @@ module BABYLON.GUI {
|
|
|
|
|
|
// Cursor
|
|
// Cursor
|
|
if (this._isFocused) {
|
|
if (this._isFocused) {
|
|
|
|
+
|
|
|
|
+ // Need to move cursor
|
|
|
|
+ if (this._clickedCoordinate) {
|
|
|
|
+ var rightPosition = this._scrollLeft + this._textWidth;
|
|
|
|
+ var absoluteCursorPosition = rightPosition - this._clickedCoordinate;
|
|
|
|
+ var currentSize = 0;
|
|
|
|
+ this._cursorOffset = 0;
|
|
|
|
+ var previousDist = 0;
|
|
|
|
+ do {
|
|
|
|
+ if (this._cursorOffset) {
|
|
|
|
+ previousDist = Math.abs(absoluteCursorPosition - currentSize);
|
|
|
|
+ }
|
|
|
|
+ this._cursorOffset++;
|
|
|
|
+ currentSize = context.measureText(text.substr(text.length - this._cursorOffset, this._cursorOffset)).width;
|
|
|
|
+
|
|
|
|
+ } while(currentSize < absoluteCursorPosition);
|
|
|
|
+
|
|
|
|
+ // Find closest move
|
|
|
|
+ if (Math.abs(absoluteCursorPosition - currentSize) > previousDist) {
|
|
|
|
+ this._cursorOffset--;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ this._blinkIsEven = false;
|
|
|
|
+ this._clickedCoordinate = null;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // Render cursor
|
|
if (!this._blinkIsEven) {
|
|
if (!this._blinkIsEven) {
|
|
let cursorOffsetText = this.text.substr(this._text.length - this._cursorOffset);
|
|
let cursorOffsetText = this.text.substr(this._text.length - this._cursorOffset);
|
|
let cursorOffsetWidth = context.measureText(cursorOffsetText).width;
|
|
let cursorOffsetWidth = context.measureText(cursorOffsetText).width;
|
|
- let cursorLeft = this._scrollLeft + textWidth - cursorOffsetWidth;
|
|
|
|
|
|
+ let cursorLeft = this._scrollLeft + this._textWidth - cursorOffsetWidth;
|
|
|
|
|
|
if (cursorLeft < clipTextLeft) {
|
|
if (cursorLeft < clipTextLeft) {
|
|
this._scrollLeft += (clipTextLeft - cursorLeft);
|
|
this._scrollLeft += (clipTextLeft - cursorLeft);
|
|
@@ -376,6 +405,13 @@ module BABYLON.GUI {
|
|
return false;
|
|
return false;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ this._clickedCoordinate = coordinates.x;
|
|
|
|
+ if (this._host.focusedControl === this) {
|
|
|
|
+ // Move cursor
|
|
|
|
+ clearTimeout(this._blinkTimeout);
|
|
|
|
+ this._markAsDirty();
|
|
|
|
+ return true;
|
|
|
|
+ }
|
|
this._host.focusedControl = this;
|
|
this._host.focusedControl = this;
|
|
|
|
|
|
return true;
|
|
return true;
|