Cromon

Untitled

Sep 3rd, 2013
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var TextBox = function (position) {
  2.     UI.makeControl(this);
  3.     var local = this;
  4.  
  5.     this.curText = "";
  6.     this.visibleText = "";
  7.     this.visibleTextStart = 0;
  8.     this.texQuad = new Quad({ });
  9.     this.texQuad.setSize(256, 32);
  10.     this.texQuad.setTexture("Interface\\ChatFrame\\UI-ChatInputBorder.blp");
  11.     this.caretPos = 0;
  12.     this.caretQuad = new Quad({});
  13.     this.caretQuad.setSize(2, 15);
  14.     this.caretQuad.setPosition(0, 0);
  15.     this.prevCaretStr = "";
  16.     this.prevCaretLen = 0.0;
  17.     this.isCaretVisible = true;
  18.     this.caretEvent = new TimedEvent({ This: local, callback: (function(scope) { return function() { scope.caretUpdate(); };})(local), interval: 1000 });
  19.     this.caretEvent.enable();
  20.  
  21.     this.curPosition = [0, 0];
  22.  
  23.     if (typeof position !== "undefined") {
  24.         this.setPosition(position);
  25.         this.curPosition = { x: position.x, y: position.y };
  26.     }
  27. }
  28.  
  29. TextBox.prototype.caretUpdate = function (arg) {
  30.    
  31. }
  32.  
  33. TextBox.prototype.onDraw = function () {
  34.     this.texQuad.draw();
  35.  
  36.     if (this.visibleText.length > 0) {
  37.         UI.drawString(this.visibleText, this.curPosition.x + 10, this.curPosition.y + 8);
  38.     }
  39.  
  40.     this.caretQuad.setPosition(this.curPosition.x + 10 - 1 + this.prevCaretLen, this.curPosition.y + 8);
  41.     this.caretQuad.draw();
  42. }
  43.  
  44. TextBox.prototype.onMessage = function (message) {
  45.     if (message.type === MessageType.CharPressed) {
  46.         var chr = message.character[0];
  47.         var chrCode = message.character.charCodeAt(0);
  48.  
  49.         if (Char.isWhitespace(chr) === true && chr !== ' ') {
  50.             return;
  51.         }
  52.  
  53.         var modifiers = Char.listModifiers(chr);
  54.         if ((modifiers.control == true || modifiers.alt == true || chrCode < 32) && chr != '\b') {
  55.             this.handleSpecialCharacter(chrCode, modifiers.control, modifiers.alt);
  56.             return;
  57.         }
  58.  
  59.         if (chrCode < 32 && chr != '\b') {
  60.             return;
  61.         }
  62.  
  63.         switch (chr) {
  64.             case '\b':
  65.                 this.removeCharacter();
  66.                 break;
  67.  
  68.             default:
  69.                 this.addCharacter(chr);
  70.                 break;
  71.         }
  72.  
  73.     } else if (message.type === MessageType.VirtualKeyDown) {
  74.         var code = message.code;
  75.  
  76.         switch (code) {
  77.             case 0x23:
  78.                 this.setCaretExtreme(false);
  79.                 break;
  80.  
  81.             case 0x24:
  82.                 this.setCaretExtreme(true);
  83.                 break;
  84.  
  85.             case 0x25:
  86.                 this.moveCaret(true);
  87.                 break;
  88.  
  89.             case 0x27:
  90.                 this.moveCaret(false);
  91.                 break;
  92.  
  93.             case 0x2E:
  94.                 {
  95.                     if (this.caretPos < this.curText.length) {
  96.                         var startStr = "";
  97.                         if (this.caretPos > 0) {
  98.                             startStr = this.curText.substr(0, this.caretPos);
  99.                         }
  100.  
  101.                         var endStr = "";
  102.  
  103.                         if (this.caretPos < this.curText.length) {
  104.                             endStr = this.curText.substr(this.caretPos + 1, (this.curText.length - this.caretPos - 1));
  105.                         }
  106.  
  107.                         this.curText = startStr + endStr;
  108.                         this.updateCaret();
  109.                     }
  110.                 }
  111.                 break;
  112.  
  113.             default:
  114.                 break;
  115.         }
  116.     }
  117. }
  118.  
  119. TextBox.prototype.setCaretExtreme = function (left) {
  120.     if (this.curText.length === 0) {
  121.         return;
  122.     }
  123.  
  124.     if (left === true && this.caretPos !== 0) {
  125.         this.caretPos = 0;
  126.         this.visibleText = "";
  127.         this.visibleTextStart = 0;
  128.  
  129.         while (UI.measureString(this.visibleText) < 230 && this.visibleText.length < this.curText.length) {
  130.             this.visibleText = this.curText.substr(0, this.visibleText.length + 1);
  131.         }
  132.  
  133.         this.updateCaret();
  134.     } else if (left === false && this.caretPos !== this.curText.length) {
  135.         this.caretPos = this.curText.length;
  136.         this.visibleTextStart = this.curText.length - 1;
  137.         this.visibleText = this.curText.substr(this.visibleTextStart, 1);
  138.         while (UI.measureString(this.visibleText) < 230 && this.visibleTextStart > 0) {
  139.             --this.visibleTextStart;
  140.             this.visibleText = this.curText.substr(this.visibleTextStart, this.curText.length - this.visibleTextStart);
  141.         }
  142.  
  143.         this.updateCaret();
  144.     }
  145. }
  146.  
  147. TextBox.prototype.handleSpecialCharacter = function (char, ctrl, alt) {
  148.     if (char === 3) {
  149.         Debug.print("__missing_implementation__: CTRL + C");
  150.     } else if (char === 22) {
  151.         var text = System.getClipboardText();
  152.         for (var i = 0; i < text.length; ++i) {
  153.             this.addCharacter(text[i]);
  154.         }
  155.     }
  156. }
  157.  
  158. TextBox.prototype.moveCaret = function (left) {
  159.     if (left === true) {
  160.         this.caretPos -= 1;
  161.         if (this.caretPos < 0) {
  162.             this.caretPos = 0;
  163.             return;
  164.         }
  165.  
  166.         if (this.caretPos === this.visibleTextStart) {
  167.             this.visibleTextStart -= 10;
  168.             if (this.visibleTextStart < 0) {
  169.                 this.visibleTextStart = 0;
  170.             }
  171.  
  172.             this.visibleText = this.curText.substr(this.visibleTextStart, this.visibleText.length + 15);
  173.             while (UI.measureString(this.visibleText) > 230) {
  174.                 this.visibleText = this.visibleText.substr(0, this.visibleText.length - 1);
  175.             }
  176.         }
  177.     } else {
  178.         this.caretPos += 1;
  179.         if (this.caretPos > this.curText.length) {
  180.             this.caretPos = this.curText.length;
  181.             return;
  182.         }
  183.  
  184.         if (this.caretPos >= this.visibleTextStart + this.visibleText.length) {
  185.             var add = Math.min(this.curText.length - this.visibleTextStart - this.visibleText.length, 10);
  186.             var endIndex = this.visibleTextStart + this.visibleText.length + add;
  187.  
  188.             this.visibleText = this.curText.substr(this.visibleTextStart, endIndex - this.visibleTextStart);
  189.  
  190.             while (UI.measureString(this.visibleText) > 230) {
  191.                 this.visibleTextStart += 1;
  192.                 this.visibleText = this.curText.substr(this.visibleTextStart, endIndex - this.visibleTextStart);
  193.             }
  194.         }
  195.     }
  196.  
  197.     this.updateCaret();
  198. }
  199.  
  200. TextBox.prototype.addCharacter = function (chr) {
  201.     var startStr = "";
  202.     if (this.caretPos > 0) {
  203.         startStr = this.curText.substr(0, this.caretPos);
  204.     }
  205.  
  206.     var endStr = "";
  207.  
  208.     if (this.caretPos < this.curText.length) {
  209.         endStr = this.curText.substr(this.caretPos, (this.curText.length - this.caretPos));
  210.     }
  211.  
  212.     this.curText = startStr + chr + endStr;
  213.     this.caretPos += 1;
  214.  
  215.     var curTotalLength = UI.measureString(this.curText);
  216.     if (curTotalLength <= 230) {
  217.         this.visibleText = this.curText;
  218.         this.visibleTextStart = 0;
  219.     } else {
  220.         if (this.caretPos === this.curText.length) {
  221.             this.visibleText += chr;
  222.             while (UI.measureString(this.visibleText) > 230) {
  223.                 this.visibleText = this.visibleText.substr(1, this.visibleText.length - 1);
  224.                 this.visibleTextStart += 1;
  225.             }
  226.         } else if ((this.caretPos - this.visibleTextStart) >= this.visibleText.length) {
  227.             var add = Math.min(this.curText.length - this.visibleTextStart - this.visibleText.length, 10);
  228.             var endIndex = this.visibleTextStart + this.visibleText.length + add;
  229.  
  230.             this.visibleText = this.curText.substr(this.visibleTextStart, endIndex - this.visibleTextStart);
  231.  
  232.             while (UI.measureString(this.visibleText) > 230) {
  233.                 this.visibleTextStart += 1;
  234.                 this.visibleText = this.curText.substr(this.visibleTextStart, endIndex - this.visibleTextStart);
  235.             }
  236.         } else {
  237.             this.visibleText = this.curText.substr(this.visibleTextStart, Math.min(this.curText.length - this.visibleTextStart, this.visibleText.length + 5));
  238.  
  239.             while (UI.measureString(this.visibleText) > 230) {
  240.                 this.visibleText = this.visibleText.substr(0, this.visibleText.length - 1);
  241.             }
  242.         }
  243.     }
  244.  
  245.     this.updateCaret();
  246. }
  247.  
  248. TextBox.prototype.removeCharacter = function () {
  249.     if (this.caretPos > this.curText.length || this.caretPos <= 0) {
  250.         return;
  251.     }
  252.  
  253.     var startStr = "";
  254.     if (this.caretPos > 1) {
  255.         startStr = this.curText.substr(0, this.caretPos - 1);
  256.     }
  257.  
  258.     var endStr = "";
  259.  
  260.     if (this.caretPos < this.curText.length) {
  261.         endStr = this.curText.substr(this.caretPos, (this.curText.length - this.caretPos));
  262.     }
  263.    
  264.     var oldText = this.curText;
  265.  
  266.     this.curText = startStr + endStr;
  267.     this.caretPos -= 1;
  268.  
  269.     if (this.caretPos == 0) {
  270.         this.visibleText = "";
  271.         this.visibleTextStart = 0;
  272.  
  273.         while (UI.measureString(this.visibleText) < 230 && this.visibleText.length < this.curText.length) {
  274.             this.visibleText = this.curText.substr(0, this.visibleText.length + 1);
  275.         }
  276.     } else if (this.caretPos === this.visibleTextStart) {
  277.         this.visibleTextStart -= 10;
  278.         if (this.visibleTextStart <= 0) {
  279.             this.visibleTextStart = 0;
  280.         }
  281.  
  282.         this.visibleText = this.curText.substr(this.visibleTextStart, Math.min(this.curText.length - this.visibleTextStart, this.visibleText.length + 15));
  283.         while (UI.measureString(this.visibleText) > 230) {
  284.             this.visibleText = this.visibleText.substr(0, this.visibleText.length - 1);
  285.         }
  286.     } else {
  287.         this.visibleTextStart -= 1;
  288.         if(this.visibleTextStart <= 0) {
  289.             this.visibleTextStart = 0;
  290.         }
  291.  
  292.         this.visibleText = this.curText.substr(this.visibleTextStart, Math.min(this.curText.length - this.visibleTextStart, this.visibleText.length + 15));
  293.         while (UI.measureString(this.visibleText) > 230) {
  294.             this.visibleText = this.visibleText.substr(0, this.visibleText.length - 1);
  295.         }
  296.     }
  297.  
  298.     this.updateCaret();
  299. }
  300.  
  301. TextBox.prototype.updateCaret = function () {
  302.     if(this.caretPos === 0 || this.curText.length === 0) {
  303.         this.prevCaretStr = "";
  304.         this.prevCaretLen = 0.0;
  305.         return;
  306.     }
  307.  
  308.     var caretOffset = this.caretPos - this.visibleTextStart;
  309.  
  310.     this.prevCaretStr = this.curText.substr(this.visibleTextStart, caretOffset);
  311.     this.prevCaretLen = UI.measureString(this.prevCaretStr);
  312. }
  313.  
  314. TextBox.prototype.updateVisibleText = function () {
  315.     var curTotalLength = UI.measureString(this.curText);
  316.     if (curTotalLength < 230) {
  317.         this.visibleText = this.curText;
  318.         this.visibleTextStart = 0;
  319.         return;
  320.     }
  321.  
  322.  
  323. }
  324.  
  325. TextBox.prototype.setPosition = function (pos) {
  326.     if (typeof pos === "undefined") {
  327.         return;
  328.     }
  329.  
  330.     if (typeof pos.x === "undefined" || typeof pos.y === "undefined") {
  331.         return;
  332.     }
  333.  
  334.     this.curPosition = { x: pos.x, y: pos.y };
  335.  
  336.     this.texQuad.setPosition(pos.x, pos.y);
  337. }
Advertisement
Add Comment
Please, Sign In to add comment