<commit_before>/*

    Todo:
        
        Disable relevant ui functionality when worker is running, e.g., 
        disallow frame-changing.
        
        Provide a quiet run button that doesn't do any rendering until
        the worker is finished. For efficient brownie construction.
        
        Add a function that allows the worker program to indicate how far
        along execution is, and use that to fill out a progress bar.

        Fix the issue with callbacks in loadExamplesIndex and onProgramOpenButton.

        Store the state of the editor better - save state, program name, etc.

*/

(function() {

    "use strict";

    // Globals.
    // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = 

    var scene, sceneCanvas, fps;
    var frames = [], frame = 0;
    var worker;
    var editor;
    var currentProgramName = "untitled";
    var queue = new QueueBug();

    // General.
    // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = 

    window.onload = function() {
        initializeWorker();
        initializeEditor();
        initializeScene();
        loadExamplesIndex();
        initializeSaves();
        setCurrentProgramName(currentProgramName);
        initializeMenuEvents();
        reflow();
        animate();
        fadeOutCover();
    }

    function initializeMenuEvents() {
        // Program
        document.getElementById("new-button").addEventListener("click", onProgramNewButton, false);
        document.getElementById("open-button").addEventListener("click", onProgramOpenButton, false);
        document.getElementById("save-button").addEventListener("click", onProgramSaveButton, false);
        document.getElementById("modal-save-button").addEventListener("click", onProgramSaveAsModalSaveButton, false);

        // Model
        document.getElementById("clear-button").addEventListener("click", onModelClearButton, false);
        document.getElementById("export-voxels-button").addEventListener("click", onModelExportVoxelsButton, false);
        document.getElementById("export-voxels-saveas-button").addEventListener("click", onModelExportVoxelsSaveButton, false);

        // View
        document.getElementById("center-button").addEventListener("click", onViewCenterButton, false);
        document.getElementById("view-get-camera-button").addEventListener("click", onViewGetCameraButton, false);

        // Menubar
        document.getElementById("run-button").addEventListener("click", onRunButton, false);

        // Frames
        document.getElementById("frame-step-left-button").addEventListener("click", onFrameStepLeftButton, false);
        document.getElementById("frame-step-right-button").addEventListener("click", onFrameStepRightButton, false);
    }

    function loadExamplesIndex() {
        $.getJSON("examples/index.json", function(index) {
            var div = document.getElementById("examples-modal-body");
            var content = "";
            for (var i in index.examples) {
                var example = index.examples[i];
                content += sprintf("<span id='example-select-%s' class='pseudolink'>%s</span><br>", example, example);
            }
            div.innerHTML = content;
            for (var i in index.examples) {
                var example = index.examples[i];
                (function(e) {
                    // Dont do this - these eventlisteners might hang around. Should just have a list of names and an open button.
                    document.getElementById(sprintf("example-select-%s", e)).addEventListener("click", function() {
                        $.get(sprintf("examples/%s.js", e), function(data) {
                            editor.setValue(data, -1);
                            setCurrentProgramName("untitled");
                            $("#examples-modal").modal("hide");
                        }, "text");
                    }, false);
                })(example);
            }
        });
    }

    function fadeOutCover() {
        setTimeout(function() {
            $("#cover").fadeOut(500);
        }, 0);
    }

    function reflow() {
        var menu = document.getElementById("menu");
        var edit = document.getElementById("editor");
        edit.style.top = menu.offsetHeight + "px";
        scene.setSize(window.innerWidth / 2, window.innerHeight);
    }

    function animate() {
        handleQueue();
        handleKeys();
        scene.light.position = scene.camera.position.clone();
        scene.render();
        requestAnimationFrame(animate);
    }

    // Program
    // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = 

    function saveProgram(programName, programText) {
        var programs = getPrograms();
        programs[programName] = programText;
        setPrograms(programs);
        document.getElementById("current-program-name").style.color = "gray";
    }

    function openProgram(programName) {
        editor.setValue(getPrograms()[programName], -1);
    }

    function getPrograms() {
        return JSON.parse(localStorage.programs);
    }

    function setPrograms(programs) {
        localStorage.programs = JSON.stringify(programs);
    }

    function setCurrentProgramName(programName) {
        currentProgramName = programName;
        document.getElementById("current-program-name").innerHTML = programName;
    }

    function initializeSaves() {
        if (localStorage.programs === undefined) {
            localStorage.programs = JSON.stringify({});
        }
    }

    // Mouse
    // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = 

    function onMouseDown(e) {
        PointerLock.requestFor(sceneCanvas);
    }

    function onMouseWheel(e) {
    }

    function onMouseMove(e) {
        if (isPointerLocked()) {
            var dx = e.webkitMovementX;
            var dy = e.webkitMovementY;
            fps.pitch -= dy * 0.001;
            fps.yaw += dx * 0.001;
            updateCamera();
        }
    }

    // Keyboard
    // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = 

    function keyDown(key) {<commit_msg>make fps cam work on nonwebkit browsers<commit_after>/*

    Todo:
        
        Disable relevant ui functionality when worker is running, e.g., 
        disallow frame-changing.
        
        Provide a quiet run button that doesn't do any rendering until
        the worker is finished. For efficient brownie construction.
        
        Add a function that allows the worker program to indicate how far
        along execution is, and use that to fill out a progress bar.

        Fix the issue with callbacks in loadExamplesIndex and onProgramOpenButton.

        Store the state of the editor better - save state, program name, etc.

*/

(function() {

    "use strict";

    // Globals.
    // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = 

    var scene, sceneCanvas, fps;
    var frames = [], frame = 0;
    var worker;
    var editor;
    var currentProgramName = "untitled";
    var queue = new QueueBug();

    // General.
    // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = 

    window.onload = function() {
        initializeWorker();
        initializeEditor();
        initializeScene();
        loadExamplesIndex();
        initializeSaves();
        setCurrentProgramName(currentProgramName);
        initializeMenuEvents();
        reflow();
        animate();
        fadeOutCover();
    }

    function initializeMenuEvents() {
        // Program
        document.getElementById("new-button").addEventListener("click", onProgramNewButton, false);
        document.getElementById("open-button").addEventListener("click", onProgramOpenButton, false);
        document.getElementById("save-button").addEventListener("click", onProgramSaveButton, false);
        document.getElementById("modal-save-button").addEventListener("click", onProgramSaveAsModalSaveButton, false);

        // Model
        document.getElementById("clear-button").addEventListener("click", onModelClearButton, false);
        document.getElementById("export-voxels-button").addEventListener("click", onModelExportVoxelsButton, false);
        document.getElementById("export-voxels-saveas-button").addEventListener("click", onModelExportVoxelsSaveButton, false);

        // View
        document.getElementById("center-button").addEventListener("click", onViewCenterButton, false);
        document.getElementById("view-get-camera-button").addEventListener("click", onViewGetCameraButton, false);

        // Menubar
        document.getElementById("run-button").addEventListener("click", onRunButton, false);

        // Frames
        document.getElementById("frame-step-left-button").addEventListener("click", onFrameStepLeftButton, false);
        document.getElementById("frame-step-right-button").addEventListener("click", onFrameStepRightButton, false);
    }

    function loadExamplesIndex() {
        $.getJSON("examples/index.json", function(index) {
            var div = document.getElementById("examples-modal-body");
            var content = "";
            for (var i in index.examples) {
                var example = index.examples[i];
                content += sprintf("<span id='example-select-%s' class='pseudolink'>%s</span><br>", example, example);
            }
            div.innerHTML = content;
            for (var i in index.examples) {
                var example = index.examples[i];
                (function(e) {
                    // Dont do this - these eventlisteners might hang around. Should just have a list of names and an open button.
                    document.getElementById(sprintf("example-select-%s", e)).addEventListener("click", function() {
                        $.get(sprintf("examples/%s.js", e), function(data) {
                            editor.setValue(data, -1);
                            setCurrentProgramName("untitled");
                            $("#examples-modal").modal("hide");
                        }, "text");
                    }, false);
                })(example);
            }
        });
    }

    function fadeOutCover() {
        setTimeout(function() {
            $("#cover").fadeOut(500);
        }, 0);
    }

    function reflow() {
        var menu = document.getElementById("menu");
        var edit = document.getElementById("editor");
        edit.style.top = menu.offsetHeight + "px";
        scene.setSize(window.innerWidth / 2, window.innerHeight);
    }

    function animate() {
        handleQueue();
        handleKeys();
        scene.light.position = scene.camera.position.clone();
        scene.render();
        requestAnimationFrame(animate);
    }

    // Program
    // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = 

    function saveProgram(programName, programText) {
        var programs = getPrograms();
        programs[programName] = programText;
        setPrograms(programs);
        document.getElementById("current-program-name").style.color = "gray";
    }

    function openProgram(programName) {
        editor.setValue(getPrograms()[programName], -1);
    }

    function getPrograms() {
        return JSON.parse(localStorage.programs);
    }

    function setPrograms(programs) {
        localStorage.programs = JSON.stringify(programs);
    }

    function setCurrentProgramName(programName) {
        currentProgramName = programName;
        document.getElementById("current-program-name").innerHTML = programName;
    }

    function initializeSaves() {
        if (localStorage.programs === undefined) {
            localStorage.programs = JSON.stringify({});
        }
    }

    // Mouse
    // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = 

    function onMouseDown(e) {
        PointerLock.requestFor(sceneCanvas);
    }

    function onMouseWheel(e) {
    }

    function onMouseMove(e) {
        if (isPointerLocked()) {
            var dx = e.webkitMovementX || e.mozMovementX || e.movementX || 0;
            var dy = e.webkitMovementY || e.mozMovementY || e.movementY || 0;
            fps.pitch -= dy * 0.001;
            fps.yaw += dx * 0.001;
            updateCamera();
        }
    }

    // Keyboard
    // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = 

    function keyDown(key) {
{'fraction_non_alphanumeric': 0.14431347562918126, 'fraction_numerical': 0.00270786874800892, 'mean_word_length': 2.1494858289440684, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 60, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3546889', 'n_tokens_mistral': 3461, 'n_tokens_neox': 3312, 'n_words': 636}