Skip to content
Snippets Groups Projects
AppState.js 5.89 KiB
Newer Older
Amanda Ghassaei's avatar
Amanda Ghassaei committed
/**
 * Created by aghassaei on 1/29/15.
 */

//a class to store global app state, model for navbar and menu wrapper
Amanda Ghassaei's avatar
Amanda Ghassaei committed
//never deallocated
Amanda Ghassaei's avatar
Amanda Ghassaei committed

AppState = Backbone.Model.extend({

    defaults: {
Amanda Ghassaei's avatar
Amanda Ghassaei committed
        currentNav:"navDesign",// design, sim, assemble
Amanda Ghassaei's avatar
Amanda Ghassaei committed
        currentTab:"lattice",

        //last tab that one open in each of the main menus
        lastDesignTab: "lattice",
        lastSimulationTab: "physics",
        lastAssembleTab: "assembler",

Amanda Ghassaei's avatar
Amanda Ghassaei committed
        menuWrapper: null,
Amanda Ghassaei's avatar
Amanda Ghassaei committed

        allCellTypes: {octa:"Octahedron", cube:"Cube"},
        allConnectionTypes: {
            octa: {face:"Face", freeformFace:"Freeform Face", edge:"Edge", edgeRot:"Rotated Edge", vertex:"Vertex"},
Amanda Ghassaei's avatar
Amanda Ghassaei committed
            cube: {face:"Face"}
        },
        allPartTypes:{
            octa:{
Amanda Ghassaei's avatar
Amanda Ghassaei committed
                face: {triangle:"Triangle", default:"Default"},
Amanda Ghassaei's avatar
Amanda Ghassaei committed
                freeformFace: {triangle:"Triangle", default:"Default"},
Amanda Ghassaei's avatar
Amanda Ghassaei committed
                edge: {triangle:"Triangle", default:"Default"},
                edgeRot: {triangle:"Triangle", default:"Default"},
                vertex: {square:"Square", xShape:"X", default:"Default"}
Amanda Ghassaei's avatar
Amanda Ghassaei committed
            },
            cube:{
                face: null
            }
        },
Amanda Ghassaei's avatar
Amanda Ghassaei committed
        menuIsVisible: true,

        //key bindings
        shift: false,
        deleteMode: false,
        extrudeMode: false
    },

    initialize: function(options){
Amanda Ghassaei's avatar
Amanda Ghassaei committed

        _.bindAll(this, "_handleKeyStroke");

        //bind events
        $(document).bind('keydown', {state:true}, this._handleKeyStroke);
        $(document).bind('keyup', {state:false}, this._handleKeyStroke);
Amanda Ghassaei's avatar
Amanda Ghassaei committed

Amanda Ghassaei's avatar
Amanda Ghassaei committed
        this.listenTo(this, "change:currentTab", this._storeTab);
        this.listenTo(this, "change:currentNav", this._updateCurrentTabForNav);
Amanda Ghassaei's avatar
Amanda Ghassaei committed
        this.listenTo(this, "change:currentTab", this._updateCellMode);

Amanda Ghassaei's avatar
Amanda Ghassaei committed
        this.lattice = options.lattice;//this doesn't need to be tracked for changes
Amanda Ghassaei's avatar
Amanda Ghassaei committed
        this.downKeys = {};//track keypresses to prevent repeat keystrokeson hold
Amanda Ghassaei's avatar
Amanda Ghassaei committed
        this.set("menuWrapper", new MenuWrapper({model: this}));
Amanda Ghassaei's avatar
Amanda Ghassaei committed
    },

Amanda Ghassaei's avatar
Amanda Ghassaei committed

    ///////////////////////////////////////////////////////////////////////////////
    /////////////////////EVENTS////////////////////////////////////////////////////
    ///////////////////////////////////////////////////////////////////////////////


Amanda Ghassaei's avatar
Amanda Ghassaei committed
    _storeTab: function(){
        var currentNav = this.get("currentNav");
        var currentTab = this.get("currentTab");
        if (currentNav == "navDesign") this.set("lastDesignTab", currentTab);
        else if (currentNav == "navSim") this.set("lastSimulationTab", currentTab);
        else if (currentNav == "navAssemble") this.set("lastAssembleTab", currentTab);
    },

Amanda Ghassaei's avatar
Amanda Ghassaei committed
    _updateCellMode: function(){
Amanda Ghassaei's avatar
Amanda Ghassaei committed
        var currentTab = this.get("currentTab");
        if (currentTab == "lattice") this.lattice.set("cellMode", "cell");
        else if (currentTab == "import") this.lattice.set("cellMode", "cell");
        else if (currentTab == "sketch") this.lattice.set("cellMode", "cell");
        else if (currentTab == "part") this.lattice.set("cellMode", "part");
Amanda Ghassaei's avatar
Amanda Ghassaei committed
    },

    //update to last tab open in that section
    _updateCurrentTabForNav: function(){
        var navSelection = this.get("currentNav");
        if (navSelection == "navDesign") this.set("currentTab",
            this.get("lastDesignTab"), {silent:true});
        else if (navSelection == "navSim") this.set("currentTab",
            this.get("lastSimulationTab"), {silent:true});
        else if (navSelection == "navAssemble") this.set("currentTab",
            this.get("lastAssembleTab"), {silent:true});
Amanda Ghassaei's avatar
Amanda Ghassaei committed
        this._updateCellMode();//a little bit hacky, this updates the cell mode, but holds off on updating the menus til the animation has happened
Amanda Ghassaei's avatar
Amanda Ghassaei committed
    },

    ///////////////////////////////////////////////////////////////////////////////
    /////////////////////KEY BINDINGS//////////////////////////////////////////////
    ///////////////////////////////////////////////////////////////////////////////

    _handleKeyStroke: function(e){//receives keyup and keydown

Amanda Ghassaei's avatar
Amanda Ghassaei committed
        if ($("input").is(':focus')) return;//we are typing in an input

Amanda Ghassaei's avatar
Amanda Ghassaei committed
        var state = e.data.state;
        var currentTab = this.get("currentTab");

Amanda Ghassaei's avatar
Amanda Ghassaei committed
        if (e.ctrlKey || e.metaKey){

        }else if (state) {
Amanda Ghassaei's avatar
Amanda Ghassaei committed
            if (this.downKeys[e.keyCode]) return;
            this.downKeys[e.keyCode] = true;
Amanda Ghassaei's avatar
Amanda Ghassaei committed
        } else this.downKeys[e.keyCode] = false;

Amanda Ghassaei's avatar
Amanda Ghassaei committed
//        console.log(e);
//        console.log(e.keyCode);
Amanda Ghassaei's avatar
Amanda Ghassaei committed
        switch(e.keyCode){
            case 16://shift
                this.set("shift", state);
                break;
Amanda Ghassaei's avatar
Amanda Ghassaei committed
            case 68://d delete mode
Amanda Ghassaei's avatar
Amanda Ghassaei committed
                if (this.get("cellMode") == "cell") this.set("deleteMode", state);//only for cell mode
Amanda Ghassaei's avatar
Amanda Ghassaei committed
                else this.set("deleteMode", false);
                break;
            case 69://e
//                if (currentTab != "sketch") return;
                this.set("extrudeMode", state);
                break;
Amanda Ghassaei's avatar
Amanda Ghassaei committed
            case 80://p part mode
Amanda Ghassaei's avatar
Amanda Ghassaei committed
                var cellMode = this.lattice.get("cellMode");
                if (cellMode == "part") this.lattice.set("cellMode", "cell");
                else if (cellMode == "cell") this.lattice.set("cellMode", "part");
                break;
            case 73://i inverse mode
                this.lattice.set("inverseMode", !this.lattice.get("inverseMode"));
Amanda Ghassaei's avatar
Amanda Ghassaei committed
                break;
Amanda Ghassaei's avatar
Amanda Ghassaei committed
            case 83://s save
                if (e.ctrlKey || e.metaKey){//command
Amanda Ghassaei's avatar
Amanda Ghassaei committed
                    e.preventDefault();
Amanda Ghassaei's avatar
Amanda Ghassaei committed
                    if (e.shiftKey){
                        $("#saveAsModel").modal("show");
                    } else {
                        dmaGlobals.lattice.saveAsJSON();
                    }
                }
                break;
            case 79://o open
                if (e.ctrlKey || e.metaKey){//command
Amanda Ghassaei's avatar
Amanda Ghassaei committed
                    e.preventDefault();
                    $("#jsonInput").click();
                }
Amanda Ghassaei's avatar
Amanda Ghassaei committed
                break;
Amanda Ghassaei's avatar
Amanda Ghassaei committed
            default:
                break;
        }
    }

});