<commit_before>/**
 Copyright (C) 2013 by <NAME>

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

 http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
 */
/// <reference path="./point.ts"/>
/// <reference path="../../types/jquery/jquery.d.ts"/>
/// <reference path="./scene/sceneView.ts"/>
module pow2 {
   export enum KeyCode {
      UP = 38,
      DOWN = 40,
      LEFT = 37,
      RIGHT = 39,
      BACKSPACE = 8,
      COMMA = 188,
      DELETE = 46,
      END = 35,
      ENTER = 13,
      ESCAPE = 27,
      HOME = 36,
      SPACE = 32,
      TAB = 9
   }

   export interface CanvasMouseCoords {
      point:Point; // Point on the canvas in pixels.
      world:Point; // Point in the world, accounting for camera scale and offset.
   }

   export interface NamedMouseElement extends CanvasMouseCoords {
      name:string;
      view:SceneView;
   }

   export class Input {
      _keysDown:Object = {};
      _mouseElements:NamedMouseElement[] = [];

      static mouseOnView(ev:MouseEvent,view:pow2.SceneView,coords?:CanvasMouseCoords) {
         var result:CanvasMouseCoords = coords || {
            point: new pow2.Point(),
            world: new pow2.Point()
         };
         var canoffset = $(ev.srcElement).offset();
         var x = ev.clientX + document.body.scrollLeft + document.documentElement.scrollLeft - Math.floor(canoffset.left);
         var y = ev.clientY + document.body.scrollTop + document.documentElement.scrollTop - Math.floor(canoffset.top);
         result.point.set(x,y);
         // Generate world mouse position
         var worldMouse = view.screenToWorld(result.point,view.cameraScale).add(view.camera.point).round();
         result.world.set(worldMouse.x,worldMouse.y);
         return result;
      }


      constructor() {
         window.addEventListener(<string>"keydown", (ev:KeyboardEvent) => {
            this._keysDown[ev.which] = true;
         });
         window.addEventListener(<string>'keyup', (ev:KeyboardEvent) => {
            this._keysDown[ev.which] = false;
         });
         var hooks = this._mouseElements;
         window.addEventListener(<string>'mousemove', (ev:MouseEvent) => {
            _.each(hooks,(hook:NamedMouseElement) => {
               if(ev.srcElement === hook.view.canvas){
                  Input.mouseOnView(ev,hook.view,hook);
               }
               else {
                  hook.point.set(-1,-1);
                  hook.world.set(-1,-1);
               }
               return false;
            });
         });
      }

      mouseHook(view:SceneView,name:string):NamedMouseElement{
         var hooks = <NamedMouseElement[]>_.where(this._mouseElements,{name:name});
         if(hooks.length > 0){
            return hooks[0];
         }
         var result:NamedMouseElement = {
            name:name,
            view:view,
            point: new Point(-1,-1),
            world: new Point(-1,-1)
         };
         this._mouseElements.push(result);
         return result;<commit_msg>refactor(input): use for loop for better profiles<commit_after>/**
 Copyright (C) 2013 by <NAME>

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

 http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
 */
/// <reference path="./point.ts"/>
/// <reference path="../../types/jquery/jquery.d.ts"/>
/// <reference path="./scene/sceneView.ts"/>
module pow2 {
   export enum KeyCode {
      UP = 38,
      DOWN = 40,
      LEFT = 37,
      RIGHT = 39,
      BACKSPACE = 8,
      COMMA = 188,
      DELETE = 46,
      END = 35,
      ENTER = 13,
      ESCAPE = 27,
      HOME = 36,
      SPACE = 32,
      TAB = 9
   }

   export interface CanvasMouseCoords {
      point:Point; // Point on the canvas in pixels.
      world:Point; // Point in the world, accounting for camera scale and offset.
   }

   export interface NamedMouseElement extends CanvasMouseCoords {
      name:string;
      view:SceneView;
   }

   export class Input {
      _keysDown:Object = {};
      _mouseElements:NamedMouseElement[] = [];

      static mouseOnView(ev:MouseEvent,view:pow2.SceneView,coords?:CanvasMouseCoords) {
         var result:CanvasMouseCoords = coords || {
            point: new pow2.Point(),
            world: new pow2.Point()
         };
         var canoffset = $(ev.srcElement).offset();
         var x = ev.clientX + document.body.scrollLeft + document.documentElement.scrollLeft - Math.floor(canoffset.left);
         var y = ev.clientY + document.body.scrollTop + document.documentElement.scrollTop - Math.floor(canoffset.top);
         result.point.set(x,y);
         // Generate world mouse position
         var worldMouse = view.screenToWorld(result.point,view.cameraScale).add(view.camera.point).round();
         result.world.set(worldMouse.x,worldMouse.y);
         return result;
      }


      constructor() {
         window.addEventListener(<string>"keydown", (ev:KeyboardEvent) => {
            this._keysDown[ev.which] = true;
         });
         window.addEventListener(<string>'keyup', (ev:KeyboardEvent) => {
            this._keysDown[ev.which] = false;
         });
         var hooks = this._mouseElements;
         window.addEventListener(<string>'mousemove', (ev:MouseEvent) => {
            var l:number = hooks.length;
            for(var i = 0; i < l; i++){
               var hook:NamedMouseElement = hooks[i];
               if(ev.srcElement === hook.view.canvas){
                  Input.mouseOnView(ev,hook.view,hook);
               }
               else {
                  hook.point.set(-1,-1);
                  hook.world.set(-1,-1);
               }
            }
         });
      }

      mouseHook(view:SceneView,name:string):NamedMouseElement{
         var hooks = <NamedMouseElement[]>_.where(this._mouseElements,{name:name});
         if(hooks.length > 0){
            return hooks[0];
         }
         var result:NamedMouseElement = {
            name:name,
            view:view,
            point: new Point(-1,-1),
            world: new Point(-1,-1)
         };
         this._mouseElements.push(result);
         return result;
{'fraction_non_alphanumeric': 0.11964208399480444, 'fraction_numerical': 0.013710492134507144, 'mean_word_length': 2.1414324569356302, 'pattern_counts': {'":': 0, '<': 20, '<?xml version=': 0, '>': 28, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4513123', 'n_tokens_mistral': 1997, 'n_tokens_neox': 1889, 'n_words': 484}