<commit_before>/*
Name: Input monitoring and library
Version: 1
Desc: Handles all keyboard input by storing results inside an array that can be
checked and cleared. This is the only design patter that allows multiple keys to
be checked at one time.

Note: Needs to be self contained keyboard events that are created and/or destroyed. Right now its
super jacked up because all the events fire to the same place. Should just let users setup their
own keyboard events with an object that can be destroyed or created.
*/

var cp = cp || {};

cp.input = {
    init: function() {
        // Creates keyboard monitoring
        window.addEventListener('keydown', this.store, true);
        window.addEventListener('keyup', this.remove, true);
        
        // Mouse logic only relative to the Canvas
        cp.core.canvas.addEventListener('mousemove', this.move, true);
        cp.core.canvas.addEventListener('mousedown', this.store, true);
        cp.core.canvas.addEventListener('mouseup', this.remove, true);
    },
    
    // Container for storing all current keys
    storage: {},
    
    // Only contains binded keys
    active: {},
    
    // Key items ready for deletion
    graveyard: [],
    
    // Returns a mouse or keyboard key depending upon the called event
    getKey: function(e) {
        // Keyboards don't have the mouse's button property, check for it
        // to verify a keyboard is firing
        if (e.button === undefined) {
            return e.keyCode;
            
        // Mouse input, get the clicked button
        } else {
            return e.button;
        }
    },
    
    // Stores the current key event in an array
    store: function(e) {
        var key = cp.input.getKey(e);
        
        // Get current status of existing keyCode (if present)
        var status = cp.input.storage[key];
        
        // Set as active only if the existing key is not already set
        if ( ! status ) {
            cp.input.storage[key] = 'active';
        }
    },
    
    // Marks the current key for deletion
    remove: function(e) {
        var key = cp.input.getKey(e);
        
        cp.input.storage[key] = 'remove';
    },
    
    // Monitors active keys and modifies them as necessary from each frame
    // Note: This loop could be better optimized
    monitor: function() {        
        // Loop through all keyboard objects and modify as necessary
        for ( var key in this.storage ) {
            // Cache active object value for comparison only, cannot be set
            var item = this.storage[key];
            
            // If the item has been recently pressed set it to the down state
            if (item === 'active') {
                this.storage[key] = 'down';
                
            // After down has been set for one frame, change it to pressed
            } else if (item === 'down') {
                this.storage[key] = 'pressed';
                
            // After up has been set, change the status to delete
            } else if (item === 'remove') {
                this.storage[key] = 'up';
                
            // After a full frame has passed, delete the item out of existence
            } else if (item === 'up') {
                delete this.storage[key];
            }
        }
    },
    
    // Binds a key with a corresponding tag/name that can be referenced
    bind: function(key, tag) {
        // Convert key name to keycode
        key = this.library[key];
        
        // Store key value for reference later
        this.active[tag] = key;
    },
    
    // Unbinds a specific key
    unbind: function(tag) {
        delete this.active[tag];
    },
    
    // Complete key binding obliteration
    unbindAll: function() {
        this.active = [];
    },
    
    // Detects if a key has been pressed for one frame
    down: function(tag) {
        // Test if key was pressed
        if (this.storage[this.active[tag]] === 'down') {
            return true;
        } else {
            return false;
        }
    },
    
    // Detects if a key has been released within the current frame
    up: function(tag) {
        // Test if key was pressed
        if (this.storage[this.active[tag]] === 'up') {
            return true;
        } else {
            return false;
        }
    },
    
    // Returns true for pressed and disregards press and release's frame based rules
    press: function(tag) {
        // Cache state of storage item
        var state = this.storage[this.active[tag]];
        
        // Return true if anything exceupt up is pressed to prevent logic overlap
        if (state &&
        state !== 'up') {
            return true;
        } else {
            return false;
        }
    },
    
    // Special function relative to the mouse
    move: function(e) {
        cp.input.mouse = {
            x: e.pageX,
            y: e.pageY
        }
    },
    
    // Archived library so you don't have to remember the keyboard event code
    library: {
        // Mouse
        'clickLeft': 0,<commit_msg>Adjusted move coordinates to be relative to Canvas and not the window.<commit_after>/*
Name: Input monitoring and library
Version: 1
Desc: Handles all keyboard input by storing results inside an array that can be
checked and cleared. This is the only design patter that allows multiple keys to
be checked at one time.

Note: Needs to be self contained keyboard events that are created and/or destroyed. Right now its
super jacked up because all the events fire to the same place. Should just let users setup their
own keyboard events with an object that can be destroyed or created.
*/

var cp = cp || {};

cp.input = {
    init: function() {
        // Creates keyboard monitoring
        window.addEventListener('keydown', this.store, true);
        window.addEventListener('keyup', this.remove, true);
        
        // Mouse logic only relative to the Canvas
        cp.core.canvas.addEventListener('mousemove', this.move, true);
        cp.core.canvas.addEventListener('mousedown', this.store, true);
        cp.core.canvas.addEventListener('mouseup', this.remove, true);
    },
    
    // Container for storing all current keys
    storage: {},
    
    // Only contains binded keys
    active: {},
    
    // Key items ready for deletion
    graveyard: [],
    
    // Returns a mouse or keyboard key depending upon the called event
    getKey: function(e) {
        // Keyboards don't have the mouse's button property, check for it
        // to verify a keyboard is firing
        if (e.button === undefined) {
            return e.keyCode;
            
        // Mouse input, get the clicked button
        } else {
            return e.button;
        }
    },
    
    // Stores the current key event in an array
    store: function(e) {
        var key = cp.input.getKey(e);
        
        // Get current status of existing keyCode (if present)
        var status = cp.input.storage[key];
        
        // Set as active only if the existing key is not already set
        if ( ! status ) {
            cp.input.storage[key] = 'active';
        }
    },
    
    // Marks the current key for deletion
    remove: function(e) {
        var key = cp.input.getKey(e);
        
        cp.input.storage[key] = 'remove';
    },
    
    // Monitors active keys and modifies them as necessary from each frame
    // Note: This loop could be better optimized
    monitor: function() {        
        // Loop through all keyboard objects and modify as necessary
        for ( var key in this.storage ) {
            // Cache active object value for comparison only, cannot be set
            var item = this.storage[key];
            
            // If the item has been recently pressed set it to the down state
            if (item === 'active') {
                this.storage[key] = 'down';
                
            // After down has been set for one frame, change it to pressed
            } else if (item === 'down') {
                this.storage[key] = 'pressed';
                
            // After up has been set, change the status to delete
            } else if (item === 'remove') {
                this.storage[key] = 'up';
                
            // After a full frame has passed, delete the item out of existence
            } else if (item === 'up') {
                delete this.storage[key];
            }
        }
    },
    
    // Binds a key with a corresponding tag/name that can be referenced
    bind: function(key, tag) {
        // Convert key name to keycode
        key = this.library[key];
        
        // Store key value for reference later
        this.active[tag] = key;
    },
    
    // Unbinds a specific key
    unbind: function(tag) {
        delete this.active[tag];
    },
    
    // Complete key binding obliteration
    unbindAll: function() {
        this.active = [];
    },
    
    // Detects if a key has been pressed for one frame
    down: function(tag) {
        // Test if key was pressed
        if (this.storage[this.active[tag]] === 'down') {
            return true;
        } else {
            return false;
        }
    },
    
    // Detects if a key has been released within the current frame
    up: function(tag) {
        // Test if key was pressed
        if (this.storage[this.active[tag]] === 'up') {
            return true;
        } else {
            return false;
        }
    },
    
    // Returns true for pressed and disregards press and release's frame based rules
    press: function(tag) {
        // Cache state of storage item
        var state = this.storage[this.active[tag]];
        
        // Return true if anything exceupt up is pressed to prevent logic overlap
        if (state &&
        state !== 'up') {
            return true;
        } else {
            return false;
        }
    },
    
    // Special function relative to the mouse
    move: function(e) {
        cp.input.mouse = {
            x: e.offsetX,
            y: e.offsetY
        }
    },
    
    // Archived library so you don't have to remember the keyboard event code
    library: {
        // Mouse
        'clickLeft': 0,
{'fraction_non_alphanumeric': 0.09194387204396036, 'fraction_numerical': 0.0003925031890884113, 'mean_word_length': 1.6898917920295593, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '4528826', 'n_tokens_mistral': 2650, 'n_tokens_neox': 2607, 'n_words': 1044}