Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var TextBox = function (position) {
- UI.makeControl(this);
- var local = this;
- this.curText = "";
- this.visibleText = "";
- this.visibleTextStart = 0;
- this.texQuad = new Quad({ });
- this.texQuad.setSize(256, 32);
- this.texQuad.setTexture("Interface\\ChatFrame\\UI-ChatInputBorder.blp");
- this.caretPos = 0;
- this.caretQuad = new Quad({});
- this.caretQuad.setSize(2, 15);
- this.caretQuad.setPosition(0, 0);
- this.prevCaretStr = "";
- this.prevCaretLen = 0.0;
- this.isCaretVisible = true;
- this.caretEvent = new TimedEvent({ This: local, callback: (function(scope) { return function() { scope.caretUpdate(); };})(local), interval: 1000 });
- this.caretEvent.enable();
- this.curPosition = [0, 0];
- if (typeof position !== "undefined") {
- this.setPosition(position);
- this.curPosition = { x: position.x, y: position.y };
- }
- }
- TextBox.prototype.caretUpdate = function (arg) {
- }
- TextBox.prototype.onDraw = function () {
- this.texQuad.draw();
- if (this.visibleText.length > 0) {
- UI.drawString(this.visibleText, this.curPosition.x + 10, this.curPosition.y + 8);
- }
- this.caretQuad.setPosition(this.curPosition.x + 10 - 1 + this.prevCaretLen, this.curPosition.y + 8);
- this.caretQuad.draw();
- }
- TextBox.prototype.onMessage = function (message) {
- if (message.type === MessageType.CharPressed) {
- var chr = message.character[0];
- var chrCode = message.character.charCodeAt(0);
- if (Char.isWhitespace(chr) === true && chr !== ' ') {
- return;
- }
- var modifiers = Char.listModifiers(chr);
- if ((modifiers.control == true || modifiers.alt == true || chrCode < 32) && chr != '\b') {
- this.handleSpecialCharacter(chrCode, modifiers.control, modifiers.alt);
- return;
- }
- if (chrCode < 32 && chr != '\b') {
- return;
- }
- switch (chr) {
- case '\b':
- this.removeCharacter();
- break;
- default:
- this.addCharacter(chr);
- break;
- }
- } else if (message.type === MessageType.VirtualKeyDown) {
- var code = message.code;
- switch (code) {
- case 0x23:
- this.setCaretExtreme(false);
- break;
- case 0x24:
- this.setCaretExtreme(true);
- break;
- case 0x25:
- this.moveCaret(true);
- break;
- case 0x27:
- this.moveCaret(false);
- break;
- case 0x2E:
- {
- if (this.caretPos < this.curText.length) {
- var startStr = "";
- if (this.caretPos > 0) {
- startStr = this.curText.substr(0, this.caretPos);
- }
- var endStr = "";
- if (this.caretPos < this.curText.length) {
- endStr = this.curText.substr(this.caretPos + 1, (this.curText.length - this.caretPos - 1));
- }
- this.curText = startStr + endStr;
- this.updateCaret();
- }
- }
- break;
- default:
- break;
- }
- }
- }
- TextBox.prototype.setCaretExtreme = function (left) {
- if (this.curText.length === 0) {
- return;
- }
- if (left === true && this.caretPos !== 0) {
- this.caretPos = 0;
- this.visibleText = "";
- this.visibleTextStart = 0;
- while (UI.measureString(this.visibleText) < 230 && this.visibleText.length < this.curText.length) {
- this.visibleText = this.curText.substr(0, this.visibleText.length + 1);
- }
- this.updateCaret();
- } else if (left === false && this.caretPos !== this.curText.length) {
- this.caretPos = this.curText.length;
- this.visibleTextStart = this.curText.length - 1;
- this.visibleText = this.curText.substr(this.visibleTextStart, 1);
- while (UI.measureString(this.visibleText) < 230 && this.visibleTextStart > 0) {
- --this.visibleTextStart;
- this.visibleText = this.curText.substr(this.visibleTextStart, this.curText.length - this.visibleTextStart);
- }
- this.updateCaret();
- }
- }
- TextBox.prototype.handleSpecialCharacter = function (char, ctrl, alt) {
- if (char === 3) {
- Debug.print("__missing_implementation__: CTRL + C");
- } else if (char === 22) {
- var text = System.getClipboardText();
- for (var i = 0; i < text.length; ++i) {
- this.addCharacter(text[i]);
- }
- }
- }
- TextBox.prototype.moveCaret = function (left) {
- if (left === true) {
- this.caretPos -= 1;
- if (this.caretPos < 0) {
- this.caretPos = 0;
- return;
- }
- if (this.caretPos === this.visibleTextStart) {
- this.visibleTextStart -= 10;
- if (this.visibleTextStart < 0) {
- this.visibleTextStart = 0;
- }
- this.visibleText = this.curText.substr(this.visibleTextStart, this.visibleText.length + 15);
- while (UI.measureString(this.visibleText) > 230) {
- this.visibleText = this.visibleText.substr(0, this.visibleText.length - 1);
- }
- }
- } else {
- this.caretPos += 1;
- if (this.caretPos > this.curText.length) {
- this.caretPos = this.curText.length;
- return;
- }
- if (this.caretPos >= this.visibleTextStart + this.visibleText.length) {
- var add = Math.min(this.curText.length - this.visibleTextStart - this.visibleText.length, 10);
- var endIndex = this.visibleTextStart + this.visibleText.length + add;
- this.visibleText = this.curText.substr(this.visibleTextStart, endIndex - this.visibleTextStart);
- while (UI.measureString(this.visibleText) > 230) {
- this.visibleTextStart += 1;
- this.visibleText = this.curText.substr(this.visibleTextStart, endIndex - this.visibleTextStart);
- }
- }
- }
- this.updateCaret();
- }
- TextBox.prototype.addCharacter = function (chr) {
- var startStr = "";
- if (this.caretPos > 0) {
- startStr = this.curText.substr(0, this.caretPos);
- }
- var endStr = "";
- if (this.caretPos < this.curText.length) {
- endStr = this.curText.substr(this.caretPos, (this.curText.length - this.caretPos));
- }
- this.curText = startStr + chr + endStr;
- this.caretPos += 1;
- var curTotalLength = UI.measureString(this.curText);
- if (curTotalLength <= 230) {
- this.visibleText = this.curText;
- this.visibleTextStart = 0;
- } else {
- if (this.caretPos === this.curText.length) {
- this.visibleText += chr;
- while (UI.measureString(this.visibleText) > 230) {
- this.visibleText = this.visibleText.substr(1, this.visibleText.length - 1);
- this.visibleTextStart += 1;
- }
- } else if ((this.caretPos - this.visibleTextStart) >= this.visibleText.length) {
- var add = Math.min(this.curText.length - this.visibleTextStart - this.visibleText.length, 10);
- var endIndex = this.visibleTextStart + this.visibleText.length + add;
- this.visibleText = this.curText.substr(this.visibleTextStart, endIndex - this.visibleTextStart);
- while (UI.measureString(this.visibleText) > 230) {
- this.visibleTextStart += 1;
- this.visibleText = this.curText.substr(this.visibleTextStart, endIndex - this.visibleTextStart);
- }
- } else {
- this.visibleText = this.curText.substr(this.visibleTextStart, Math.min(this.curText.length - this.visibleTextStart, this.visibleText.length + 5));
- while (UI.measureString(this.visibleText) > 230) {
- this.visibleText = this.visibleText.substr(0, this.visibleText.length - 1);
- }
- }
- }
- this.updateCaret();
- }
- TextBox.prototype.removeCharacter = function () {
- if (this.caretPos > this.curText.length || this.caretPos <= 0) {
- return;
- }
- var startStr = "";
- if (this.caretPos > 1) {
- startStr = this.curText.substr(0, this.caretPos - 1);
- }
- var endStr = "";
- if (this.caretPos < this.curText.length) {
- endStr = this.curText.substr(this.caretPos, (this.curText.length - this.caretPos));
- }
- var oldText = this.curText;
- this.curText = startStr + endStr;
- this.caretPos -= 1;
- if (this.caretPos == 0) {
- this.visibleText = "";
- this.visibleTextStart = 0;
- while (UI.measureString(this.visibleText) < 230 && this.visibleText.length < this.curText.length) {
- this.visibleText = this.curText.substr(0, this.visibleText.length + 1);
- }
- } else if (this.caretPos === this.visibleTextStart) {
- this.visibleTextStart -= 10;
- if (this.visibleTextStart <= 0) {
- this.visibleTextStart = 0;
- }
- this.visibleText = this.curText.substr(this.visibleTextStart, Math.min(this.curText.length - this.visibleTextStart, this.visibleText.length + 15));
- while (UI.measureString(this.visibleText) > 230) {
- this.visibleText = this.visibleText.substr(0, this.visibleText.length - 1);
- }
- } else {
- this.visibleTextStart -= 1;
- if(this.visibleTextStart <= 0) {
- this.visibleTextStart = 0;
- }
- this.visibleText = this.curText.substr(this.visibleTextStart, Math.min(this.curText.length - this.visibleTextStart, this.visibleText.length + 15));
- while (UI.measureString(this.visibleText) > 230) {
- this.visibleText = this.visibleText.substr(0, this.visibleText.length - 1);
- }
- }
- this.updateCaret();
- }
- TextBox.prototype.updateCaret = function () {
- if(this.caretPos === 0 || this.curText.length === 0) {
- this.prevCaretStr = "";
- this.prevCaretLen = 0.0;
- return;
- }
- var caretOffset = this.caretPos - this.visibleTextStart;
- this.prevCaretStr = this.curText.substr(this.visibleTextStart, caretOffset);
- this.prevCaretLen = UI.measureString(this.prevCaretStr);
- }
- TextBox.prototype.updateVisibleText = function () {
- var curTotalLength = UI.measureString(this.curText);
- if (curTotalLength < 230) {
- this.visibleText = this.curText;
- this.visibleTextStart = 0;
- return;
- }
- }
- TextBox.prototype.setPosition = function (pos) {
- if (typeof pos === "undefined") {
- return;
- }
- if (typeof pos.x === "undefined" || typeof pos.y === "undefined") {
- return;
- }
- this.curPosition = { x: pos.x, y: pos.y };
- this.texQuad.setPosition(pos.x, pos.y);
- }
Advertisement
Add Comment
Please, Sign In to add comment