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


LatticeMenuView = Backbone.View.extend({

    el: "#menuContent",

    events: {
        "click #latticeMenuClearCells":                 "_clearCells",
        "change #latticeScale":                         "_changeScale",
        "click .cellType":                              "_changeCellType",
        "click .connectionType":                        "_changeConnectionType",
Amanda Ghassaei's avatar
Amanda Ghassaei committed
        "slide #scaleSlider":                           "_sliderDidSlide",
Amanda Ghassaei's avatar
Amanda Ghassaei committed
        "slideStop #scaleSlider":                       "_changeScaleSlider",
        "change #preserveCells":                        "_changePreserveCells",
        "change #showInverse":                          "_showInverseCells"
Amanda Ghassaei's avatar
Amanda Ghassaei committed
    },


    initialize: function(options){

Amanda Ghassaei's avatar
Amanda Ghassaei committed
        this.lattice = options.lattice;
Amanda Ghassaei's avatar
Amanda Ghassaei committed

        _.bindAll(this, "render");
//        var self = this;
//        this.listenTo(this.model, "change", function(){
//            var change = false;
//            _.each(_.keys(self.model.changedAttributes()), function(attribute){
//                if (attribute != "currentNav" && attribute != "currentTab") change = true;
//            });
//            if (change) this.render();
//        });
        this.listenTo(this.lattice, "change", this.render);
Amanda Ghassaei's avatar
Amanda Ghassaei committed
    },

    _clearCells: function(e){
        e.preventDefault();
Amanda Ghassaei's avatar
Amanda Ghassaei committed
        this.lattice.clearCells();
Amanda Ghassaei's avatar
Amanda Ghassaei committed
    },

    _changeScale: function(e){
        e.preventDefault();
        var val = parseFloat($(e.target).val());
        if (isNaN(val)) return;
Amanda Ghassaei's avatar
Amanda Ghassaei committed
        this.lattice.set("scale", val);
Amanda Ghassaei's avatar
Amanda Ghassaei committed
    },

Amanda Ghassaei's avatar
Amanda Ghassaei committed
    _sliderDidSlide: function(e){
Amanda Ghassaei's avatar
Amanda Ghassaei committed
        var scale = $(e.target)[0].value;
Amanda Ghassaei's avatar
Amanda Ghassaei committed
        this.lattice.previewScaleChange(scale);//does not trigger lattice change event - no rerendering of ui
Amanda Ghassaei's avatar
Amanda Ghassaei committed
        $("#latticeScale").val(scale);
        dmaGlobals.three.render();
Amanda Ghassaei's avatar
Amanda Ghassaei committed
    },

Amanda Ghassaei's avatar
Amanda Ghassaei committed
    _changeScaleSlider: function(e){
Amanda Ghassaei's avatar
Amanda Ghassaei committed
        this.lattice.set("scale", $(e.target)[0].value);
Amanda Ghassaei's avatar
Amanda Ghassaei committed
    },
Amanda Ghassaei's avatar
Amanda Ghassaei committed

    _changeCellType: function(e){
        e.preventDefault();
        var cellType = $(e.target).data("type");

        //reset everything to defaults silently
        if (cellType != this.lattice.get("cellType")){
            this._setAppStateToDefaultsSilently(cellType);
        this.lattice.set("cellType", cellType);
    _changeConnectionType: function(e){
        e.preventDefault();
        var connectionType = $(e.target).data("type");
        if (connectionType != this.lattice.get("connectionType")){
            this._setAppStateToDefaultsSilently(this.lattice.get("cellType"), connectionType);
        this.lattice.set("connectionType", connectionType);
    },

    _setAppStateToDefaultsSilently: function(newCellType, newConnectionType){
        if (newCellType == "cube") {
            if (!newConnectionType){
                newConnectionType = "face";
                this.lattice.set("connectionType", newConnectionType, {silent:true});
            }
            this.model.set("connectionType", newConnectionType, {silent:true});
            if (newConnectionType == "face"){
                this.lattice.set("partType", null, {silent:true});
            }
        }
        else if (newCellType == "octa") {
            if (!newConnectionType){
                newConnectionType = "face";
                this.lattice.set("connectionType", newConnectionType, {silent:true});
            }
            if (newConnectionType == "face"){
                this.lattice.set("partType", "triangle", {silent:true});
            } else if (newConnectionType == "edge"){
                this.lattice.set("partType", "triangle", {silent:true});
            } else if (newConnectionType == "vertex"){
                this.lattice.set("partType", "square", {silent:true});
Amanda Ghassaei's avatar
Amanda Ghassaei committed
    _changePreserveCells: function(e){
        this.lattice.set("shouldPreserveCells", $(e.target).prop("checked"));
    },

    _showInverseCells: function(e){
        this.lattice.set("inverseMode", $(e.target).prop("checked"));
Amanda Ghassaei's avatar
Amanda Ghassaei committed
    render: function(){
Amanda Ghassaei's avatar
Amanda Ghassaei committed
        if (this.model.get("currentTab") != "lattice") return;
        this.$el.html(this.template(_.extend(this.model.attributes, this.lattice.attributes)));
Amanda Ghassaei's avatar
Amanda Ghassaei committed

        $('#scaleSlider').slider({
            formatter: function(value) {
                return value;
            }
        });
Amanda Ghassaei's avatar
Amanda Ghassaei committed
    },

    template: _.template('\
        Cell Type:   \
            <div class="btn-group">\
                <button data-toggle="dropdown" class="btn dropdown-toggle" type="button"><%= allCellTypes[cellType] %><span class="caret"></span></button>\
                <ul role="menu" class="dropdown-menu">\
                    <% _.each(_.keys(allCellTypes), function(key){ %>\
                        <li><a class="cellType" data-type="<%= key %>" href="#"><%= allCellTypes[key] %></a></li>\
                    <% }); %>\
                </ul>\
            </div><br/><br/>\
        Cell Connection:&nbsp;&nbsp;\
            <div class="btn-group">\
                <button data-toggle="dropdown" class="btn dropdown-toggle" type="button"><%= allConnectionTypes[cellType][connectionType] %>-Connected<span class="caret"></span></button>\
                <ul role="menu" class="dropdown-menu">\
                    <% _.each(_.keys(allConnectionTypes[cellType]), function(key){ %>\
                        <li><a class="connectionType" data-type="<%= key %>" href="#"><%= allConnectionTypes[cellType][key] %></a></li>\
                    <% }); %>\
                </ul>\
Amanda Ghassaei's avatar
Amanda Ghassaei committed
            </div><br/>\
        <% if (connectionType != "freeformFace") { %>\
Amanda Ghassaei's avatar
Amanda Ghassaei committed
        <label class="checkbox">\
            <input type="checkbox" <% if (shouldPreserveCells) { %> checked="checked" <% } %> value="" id="preserveCells" data-toggle="checkbox" class="custom-checkbox"><span class="icons"><span class="icon-unchecked"></span><span class="icon-checked"></span></span>\
            Preserve cells on lattice change\
        </label>\
        <% } %>\
        <br/><br/>\
Amanda Ghassaei's avatar
Amanda Ghassaei committed
        <label class="checkbox">\
            <input type="checkbox"  <% if (inverseMode) { %> checked="checked" <% } %> value="" id="showInverse" data-toggle="checkbox" class="custom-checkbox"><span class="icons"><span class="icon-unchecked"></span><span class="icon-checked"></span></span>\
Amanda Ghassaei's avatar
Amanda Ghassaei committed
            Show Inverse Geometry (hold "i" key)\
Amanda Ghassaei's avatar
Amanda Ghassaei committed
        </label><br/><br/>\
Amanda Ghassaei's avatar
Amanda Ghassaei committed
        Scale:&nbsp;&nbsp;<input id="scaleSlider" data-slider-id="ex1Slider" type="text" data-slider-min="1" data-slider-max="100" data-slider-step="0.1" data-slider-value="<%= scale %>"/>\
Amanda Ghassaei's avatar
Amanda Ghassaei committed
        <br/><input id="latticeScale" value="<%= scale %>" placeholder="enter scale" class="form-control" type="text"><br/>\
        Num Cells:&nbsp;&nbsp;<%= numCells %><br/>\
Amanda Ghassaei's avatar
Amanda Ghassaei committed
        <br/>\
        <a href="#" id="latticeMenuClearCells" class=" btn btn-block btn-lg btn-default">Clear All Cells</a><br/>\
Amanda Ghassaei's avatar
Amanda Ghassaei committed
        hint: click to create cells, shift+drag to create a lot of cells, d+click to delete cells<br/>\
        hold "p" key to see parts\
Amanda Ghassaei's avatar
Amanda Ghassaei committed
        ')

Amanda Ghassaei's avatar
Amanda Ghassaei committed
//    <label class="checkbox" for="invertGeo">\
//            <input type="checkbox" checked="checked" value="" id="invertGeo" data-toggle="checkbox" class="custom-checkbox"><span class="icons"><span class="icon-unchecked"></span><span class="icon-checked"></span></span>\
//            Show Inverse Geometry\
//        </label>\

Amanda Ghassaei's avatar
Amanda Ghassaei committed
});