home *** CD-ROM | disk | FTP | other *** search
/ Champak 48 / cdrom_image.iso / Games / alex_trax.swf / scripts / __Packages / com / neodelight / std / Inputs.as < prev    next >
Encoding:
Text File  |  2007-10-01  |  11.7 KB  |  370 lines

  1. class com.neodelight.std.Inputs
  2. {
  3.    static var hookMc;
  4.    static var vMouseA;
  5.    static var vMouseVMax;
  6.    static var vMouseXMin;
  7.    static var vMouseXMax;
  8.    static var vMouseYMin;
  9.    static var vMouseYMax;
  10.    static var inputs = new Object();
  11.    static var listeners = new Array();
  12.    static var locked = false;
  13.    static var vKeysDown = new Object();
  14.    static var vKeysReset = new Array();
  15.    static var vMouse = false;
  16.    static var vMouseKeys = new Object();
  17.    static var vMousePos = new com.neodelight.std.Vector();
  18.    static var vMouseV = new com.neodelight.std.Vector(0,0);
  19.    static var vMouseReal = new com.neodelight.std.Vector();
  20.    static var inited = false;
  21.    static var KEY_BACKSPACE = 8;
  22.    static var KEY_CAPSLOCK = 20;
  23.    static var KEY_CONTROL = 17;
  24.    static var KEY_DELETEKEY = 46;
  25.    static var KEY_DOWN = 40;
  26.    static var KEY_END = 35;
  27.    static var KEY_ENTER = 13;
  28.    static var KEY_ESCAPE = 27;
  29.    static var KEY_HOME = 36;
  30.    static var KEY_INSERT = 45;
  31.    static var KEY_LEFT = 37;
  32.    static var KEY_PGDN = 34;
  33.    static var KEY_PGUP = 33;
  34.    static var KEY_RIGHT = 39;
  35.    static var KEY_SHIFT = 16;
  36.    static var KEY_SPACE = 32;
  37.    static var KEY_TAB = 9;
  38.    static var KEY_UP = 38;
  39.    static var JOY_UP = 400;
  40.    static var JOY_DOWN = 401;
  41.    static var JOY_LEFT = 402;
  42.    static var JOY_RIGHT = 403;
  43.    static var JOY_BUTTON0 = 404;
  44.    static var JOY_BUTTON1 = 405;
  45.    static var MOUSE_BUTTON = 406;
  46.    function Inputs()
  47.    {
  48.    }
  49.    static function init()
  50.    {
  51.       if(!com.neodelight.std.Inputs.inited)
  52.       {
  53.          com.neodelight.std.Inputs.inited = true;
  54.          var _loc6_ = com.neodelight.std.Blueprint.getBlueprint("inputs");
  55.          if(_loc6_)
  56.          {
  57.             var _loc3_ = _loc6_.inputsKeys;
  58.             if(_loc3_)
  59.             {
  60.                var _loc2_ = _loc3_.length;
  61.                while(true)
  62.                {
  63.                   _loc2_;
  64.                   if(!_loc2_--)
  65.                   {
  66.                      break;
  67.                   }
  68.                   com.neodelight.std.Inputs.define(_loc3_[_loc2_][0],_loc3_[_loc2_][1]);
  69.                }
  70.             }
  71.          }
  72.          Mouse.addListener(com.neodelight.std.Inputs);
  73.       }
  74.       if(!com.neodelight.std.Inputs.hookMc)
  75.       {
  76.          var _loc0_ = null;
  77.          var _loc5_ = com.neodelight.std.Inputs.hookMc = _root.createEmptyMovieClip(com.neodelight.std.Unique.getKey(),_root.getNextHighestDepth());
  78.          _loc5_.onEnterFrame = function()
  79.          {
  80.             com.neodelight.std.Inputs.updateInputs();
  81.             if(com.neodelight.std.Inputs.isDown("mouse_click"))
  82.             {
  83.                com.neodelight.std.Inputs.hookMc.onMouseDown();
  84.             }
  85.             if(com.neodelight.std.Inputs.isTriggeredUp("mouse_click"))
  86.             {
  87.                com.neodelight.std.Inputs.hookMc.onMouseUp();
  88.             }
  89.          };
  90.          _loc5_.onMouseDown = function()
  91.          {
  92.             var _loc2_ = com.neodelight.std.Inputs.listeners;
  93.             var _loc1_ = _loc2_.length;
  94.             while(true)
  95.             {
  96.                _loc1_;
  97.                if(!_loc1_--)
  98.                {
  99.                   break;
  100.                }
  101.                if(_loc2_[_loc1_].onVMouseDown)
  102.                {
  103.                   _loc2_[_loc1_].onVMouseDown();
  104.                }
  105.             }
  106.          };
  107.          _loc5_.onMouseUp = function()
  108.          {
  109.             var _loc2_ = com.neodelight.std.Inputs.listeners;
  110.             var _loc1_ = _loc2_.length;
  111.             while(true)
  112.             {
  113.                _loc1_;
  114.                if(!_loc1_--)
  115.                {
  116.                   break;
  117.                }
  118.                if(_loc2_[_loc1_].onVMouseUp)
  119.                {
  120.                   _loc2_[_loc1_].onVMouseUp();
  121.                }
  122.             }
  123.          };
  124.       }
  125.    }
  126.    static function define(id, key)
  127.    {
  128.       com.neodelight.std.Inputs.init();
  129.       trace("define: \"" + key + "\" as \"" + id + "\"");
  130.       switch(typeof key)
  131.       {
  132.          case "string":
  133.             var _loc2_ = String(key).length;
  134.             while(true)
  135.             {
  136.                _loc2_;
  137.                if(!_loc2_--)
  138.                {
  139.                   break;
  140.                }
  141.                com.neodelight.std.Inputs.define(id,String(key).charCodeAt(_loc2_));
  142.             }
  143.             break;
  144.          case "number":
  145.             if(!com.neodelight.std.Inputs.inputs[id])
  146.             {
  147.                com.neodelight.std.Inputs.inputs[id] = {isDown:false,triggerDown:false,triggerUp:false,keys:new Array()};
  148.             }
  149.             com.neodelight.std.Inputs.inputs[id].keys.push(key);
  150.             break;
  151.          default:
  152.             _global.bin.syslog("Inputs","define(): unknown variable type: " + typeof key,2);
  153.       }
  154.    }
  155.    static function setVirtual(id, isDown)
  156.    {
  157.       com.neodelight.std.Inputs.vKeysDown[id] = isDown;
  158.    }
  159.    static function triggerVirtual(id, sustain)
  160.    {
  161.       com.neodelight.std.Inputs.vKeysDown[id] = true;
  162.       com.neodelight.std.Inputs.vKeysReset.push({id:id,sustain:int(sustain)});
  163.    }
  164.    static function addListener(listener)
  165.    {
  166.       com.neodelight.std.Inputs.init();
  167.       var _loc2_ = com.neodelight.std.Inputs.listeners;
  168.       var _loc3_ = false;
  169.       var _loc1_ = _loc2_.length;
  170.       while(true)
  171.       {
  172.          _loc1_;
  173.          if(!_loc1_--)
  174.          {
  175.             break;
  176.          }
  177.          if(_loc2_[_loc1_] == listener)
  178.          {
  179.             _loc3_ = true;
  180.             break;
  181.          }
  182.       }
  183.       if(!_loc3_)
  184.       {
  185.          _loc2_.push(listener);
  186.       }
  187.    }
  188.    static function removeListener(listener)
  189.    {
  190.       var _loc2_ = com.neodelight.std.Inputs.listeners;
  191.       var _loc4_ = false;
  192.       var _loc1_ = _loc2_.length;
  193.       while(true)
  194.       {
  195.          _loc1_;
  196.          if(!_loc1_--)
  197.          {
  198.             break;
  199.          }
  200.          if(_loc2_[_loc1_] == listener)
  201.          {
  202.             _loc2_.splice(_loc1_,1);
  203.             break;
  204.          }
  205.       }
  206.    }
  207.    static function updateInputs()
  208.    {
  209.       var _loc9_ = com.neodelight.std.Inputs.inputs;
  210.       var _loc7_ = com.neodelight.std.Inputs.vKeysReset;
  211.       var _loc8_ = com.neodelight.std.Inputs.vKeysDown;
  212.       for(var _loc10_ in _loc9_)
  213.       {
  214.          var _loc3_ = _loc9_[_loc10_];
  215.          var _loc2_ = false;
  216.          var _loc5_ = _loc3_.keys;
  217.          var _loc4_ = _loc5_.length;
  218.          while(true)
  219.          {
  220.             _loc4_;
  221.             if(!_loc4_--)
  222.             {
  223.                break;
  224.             }
  225.             var _loc1_ = _loc5_[_loc4_];
  226.             if(_loc1_ < 256)
  227.             {
  228.                if(Key.isDown(_loc1_))
  229.                {
  230.                   _loc2_ = true;
  231.                   break;
  232.                }
  233.             }
  234.             else if(_loc8_[_loc1_])
  235.             {
  236.                _loc2_ = true;
  237.                break;
  238.             }
  239.          }
  240.          _loc3_.triggerDown = !_loc3_.isDown && _loc2_;
  241.          _loc3_.triggerUp = _loc3_.isDown && !_loc2_;
  242.          _loc3_.isDown = _loc2_;
  243.       }
  244.       var _loc6_ = _loc7_.length;
  245.       while(true)
  246.       {
  247.          _loc6_;
  248.          if(!_loc6_--)
  249.          {
  250.             break;
  251.          }
  252.          if(--_loc7_[_loc6_].sustain <= 0)
  253.          {
  254.             _loc8_[Number(_loc7_[_loc6_].id)] = false;
  255.             _loc7_.splice(_loc6_,1);
  256.          }
  257.       }
  258.    }
  259.    static function isDown(id)
  260.    {
  261.       return !com.neodelight.std.Inputs.locked && com.neodelight.std.Inputs.inputs[id].isDown;
  262.    }
  263.    static function isTriggeredDown(id)
  264.    {
  265.       return !com.neodelight.std.Inputs.locked && com.neodelight.std.Inputs.inputs[id].triggerDown;
  266.    }
  267.    static function isTriggeredUp(id)
  268.    {
  269.       return !com.neodelight.std.Inputs.locked && com.neodelight.std.Inputs.inputs[id].triggerUp;
  270.    }
  271.    static function getMousePos(mc)
  272.    {
  273.       var _loc6_ = undefined;
  274.       if(com.neodelight.std.Inputs.vMouse)
  275.       {
  276.          var _loc2_ = com.neodelight.std.Inputs.vMouseV;
  277.          var _loc3_ = com.neodelight.std.Inputs.vMousePos;
  278.          var _loc4_ = com.neodelight.std.Inputs.vMouseReal;
  279.          if(_loc4_.x != int(_root._xmouse) || _loc4_.y != int(_root._ymouse))
  280.          {
  281.             _loc4_.x = _loc3_.x = int(_root._xmouse);
  282.             _loc4_.y = _loc3_.y = int(_root._ymouse);
  283.          }
  284.          else
  285.          {
  286.             if(com.neodelight.std.Inputs.isDown("mouse_right"))
  287.             {
  288.                _loc2_.x = Math.max(0,_loc2_.x + com.neodelight.std.Inputs.vMouseA);
  289.             }
  290.             else if(com.neodelight.std.Inputs.isDown("mouse_left"))
  291.             {
  292.                _loc2_.x = Math.min(0,_loc2_.x - com.neodelight.std.Inputs.vMouseA);
  293.             }
  294.             else
  295.             {
  296.                _loc2_.x = 0;
  297.             }
  298.             if(com.neodelight.std.Inputs.isDown("mouse_down"))
  299.             {
  300.                _loc2_.y = Math.max(0,_loc2_.y + com.neodelight.std.Inputs.vMouseA);
  301.             }
  302.             else if(com.neodelight.std.Inputs.isDown("mouse_up"))
  303.             {
  304.                _loc2_.y = Math.min(0,_loc2_.y - com.neodelight.std.Inputs.vMouseA);
  305.             }
  306.             else
  307.             {
  308.                _loc2_.y = 0;
  309.             }
  310.             if(_loc2_.x != 0 || _loc2_.y != 0)
  311.             {
  312.                var _loc5_ = Math.min(com.neodelight.std.Inputs.vMouseVMax,Math.max(Math.abs(_loc2_.x),Math.abs(_loc2_.y)));
  313.                if(_loc2_.x != 0)
  314.                {
  315.                   if(_loc2_.x > 0)
  316.                   {
  317.                      _loc2_.x = _loc5_;
  318.                   }
  319.                   else
  320.                   {
  321.                      _loc2_.x = _loc5_ * -1;
  322.                   }
  323.                }
  324.                if(_loc2_.y != 0)
  325.                {
  326.                   if(_loc2_.y > 0)
  327.                   {
  328.                      _loc2_.y = _loc5_;
  329.                   }
  330.                   else
  331.                   {
  332.                      _loc2_.y = _loc5_ * -1;
  333.                   }
  334.                }
  335.             }
  336.             _loc3_.x = Math.max(com.neodelight.std.Inputs.vMouseXMin,Math.min(com.neodelight.std.Inputs.vMouseXMax,_loc3_.x + _loc2_.x));
  337.             _loc3_.y = Math.max(com.neodelight.std.Inputs.vMouseYMin,Math.min(com.neodelight.std.Inputs.vMouseYMax,_loc3_.y + _loc2_.y));
  338.          }
  339.          _loc6_ = new com.neodelight.std.Vector(_loc3_.x,_loc3_.y);
  340.          mc.globalToLocal(_loc6_);
  341.       }
  342.       else
  343.       {
  344.          _loc6_ = new com.neodelight.std.Vector(mc._xmouse,mc._ymouse);
  345.       }
  346.       return _loc6_;
  347.    }
  348.    static function initVMouse(cfg)
  349.    {
  350.       com.neodelight.std.Inputs.vMouseReal.x = com.neodelight.std.Inputs.vMousePos.x = int(_root._xmouse);
  351.       com.neodelight.std.Inputs.vMouseReal.y = com.neodelight.std.Inputs.vMousePos.y = int(_root._ymouse);
  352.       com.neodelight.std.Inputs.vMouseA = !cfg.acc ? 1 : com.neodelight.std.XMath.toNumber(cfg.acc);
  353.       com.neodelight.std.Inputs.vMouseVMax = !cfg.vMax ? 10 : com.neodelight.std.XMath.toNumber(cfg.vMax);
  354.       com.neodelight.std.Inputs.vMouseXMin = !cfg.xMin ? 0 : com.neodelight.std.XMath.toNumber(cfg.xMin);
  355.       com.neodelight.std.Inputs.vMouseXMax = !cfg.xMax ? 550 : com.neodelight.std.XMath.toNumber(cfg.xMax);
  356.       com.neodelight.std.Inputs.vMouseYMin = !cfg.yMin ? 0 : com.neodelight.std.XMath.toNumber(cfg.yMin);
  357.       com.neodelight.std.Inputs.vMouseYMax = !cfg.yMax ? 400 : com.neodelight.std.XMath.toNumber(cfg.yMax);
  358.       com.neodelight.std.Inputs.vMouse = true;
  359.       com.neodelight.std.Inputs.init();
  360.    }
  361.    static function onMouseDown()
  362.    {
  363.       com.neodelight.std.Inputs.setVirtual(com.neodelight.std.Inputs.MOUSE_BUTTON,true);
  364.    }
  365.    static function onMouseUp()
  366.    {
  367.       com.neodelight.std.Inputs.setVirtual(com.neodelight.std.Inputs.MOUSE_BUTTON,false);
  368.    }
  369. }
  370.