Advertisement
biud436

Window_KorNameEdit

Nov 1st, 2015
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*:
  2.  * Window_KorNameEdit.js
  3.  * @plugindesc 한글 이름 입력 플러그인
  4.  * @author 러닝은빛(biud436)
  5.  * @since 2015.10.19
  6.  * @version 1.2
  7.  *
  8.  * @param windowWidth
  9.  * @desc 윈도우의 폭입니다
  10.  * @default 580
  11.  *
  12.  */
  13.  
  14. function TextBox() {
  15.   this.initialize.apply(this, arguments);
  16. };
  17.  
  18. (function() {
  19.  
  20.   var parameters = PluginManager.parameters('Window_KorNameEdit');
  21.   var __windowWidth = Number(parameters['windowWidth'] || 580);
  22.  
  23.   TextBox.prototype.initialize = function(_editWindow)  {
  24.     this._editWindow = _editWindow;
  25.     this.createTextBox();
  26.     this.getFocus();
  27.   };
  28.  
  29.   TextBox.prototype.createTextBox = function() {
  30.     this._textBox = document.createElement('input');
  31.     this._textBox.type = "text"
  32.     this._textBox.id = "textBox"
  33.     this._textBox.style.opacity = 0;
  34.     this._textBox.onkeydown = this.onKeyDown.bind(this);
  35.     document.body.appendChild(this._textBox);  
  36.   };
  37.  
  38.   TextBox.prototype.setEvent = function(func) {
  39.     this._textBox.onchange = func;
  40.   };   
  41.    
  42.   TextBox.prototype.terminateTextBox = function() {
  43.     document.body.removeChild(this._textBox);
  44.   };  
  45.  
  46.   TextBox.prototype.onKeyDown = function(e) {
  47.  
  48.     this.getFocus();
  49.  
  50.     switch(e.keyCode) {
  51.     case 8:
  52.       if (this.getTextLength() > 0) {
  53.         this.backSpace();
  54.       }
  55.       break;  
  56.     case 13:
  57.       if(this.getTextLength() <= 0) {
  58.         e.preventDefault();
  59.       }
  60.       break;
  61.     case 229:
  62.       break;
  63.     }    
  64.   }
  65.  
  66.   Window_NameEdit.prototype.charWidth = function () {
  67.     var text = '\uAC00';
  68.     return this.textWidth(text)
  69.   };  
  70.  
  71.   TextBox.prototype.getTextLength = function() {
  72.     return this._textBox.value.length;
  73.   };
  74.  
  75.   TextBox.prototype.getMaxLength = function() {
  76.     return this._editWindow._maxLength;
  77.   };
  78.  
  79.   TextBox.prototype.backSpace = function() {
  80.     this._editWindow._name = this._editWindow._name.slice(0, this._textBox.value.length - 1);
  81.     this._editWindow._index = this._textBox.value.length - 1;
  82.     this._textBox.value = this._editWindow._name;
  83.     this._editWindow.refresh();  
  84.   };
  85.  
  86.   TextBox.prototype.refreshNameEdit = function()  {
  87.     this._editWindow._name = this._textBox.value.toString();
  88.     this._editWindow._index = this._textBox.value.length || 0;
  89.     this._editWindow.refresh();
  90.   };
  91.  
  92.   TextBox.prototype.update = function() {      
  93.     if(this.getTextLength() <= this._editWindow._maxLength) {
  94.       this.refreshNameEdit();
  95.     }
  96.   };
  97.  
  98.   Window_NameEdit.prototype.windowWidth = function () {
  99.     return __windowWidth;
  100.   };  
  101.  
  102.   Window_NameEdit.prototype.drawChar = function (index) {
  103.     var rect = this.itemRect(index);
  104.     this.resetTextColor();
  105.     this.drawText(this._name[index] || '', rect.x, rect.y)
  106.   };  
  107.  
  108.   TextBox.prototype.getFocus = function() {
  109.     this._textBox.focus();
  110.   };
  111.  
  112.   TextBox.prototype.terminate =  function() {
  113.     this.terminateTextBox();
  114.   };
  115.  
  116. })();
  117.  
  118. (function () {
  119.  
  120.   Scene_Name.prototype.create = function () {
  121.     Scene_MenuBase.prototype.create.call(this);
  122.     this._actor = $gameActors.actor(this._actorId);
  123.     this.createEditWindow();
  124.     this.createTextBox();
  125.     this._textBox.setEvent( this.onInputOk.bind(this) );
  126.   };  
  127.  
  128.   Scene_Name.prototype.createTextBox =  function() {
  129.     this._textBox = new TextBox(this._editWindow);
  130.   }
  131.  
  132.   Scene_Name.prototype.update = function() {
  133.  
  134.     this._textBox.getFocus();
  135.     this._textBox.update();
  136.     Scene_MenuBase.prototype.update.call(this);
  137.   }
  138.  
  139.   Scene_Name.prototype.terminate = function() {
  140.     Scene_MenuBase.prototype.terminate.call(this);
  141.     this._textBox.terminate();
  142.   }
  143.  
  144. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement