var Panel = require('./Panel'), ui = require('../util/ui'), Handlebars = require('hbsfy/runtime'); //require templates var panelHtml = require('../hbs/scene/panel.hbs'), detailsHtml = require('../hbs/scene/details.hbs'), treeHtml = require('../hbs/scene/tree.hbs'), _cache = {}, _id = 0; Handlebars.registerPartial('sceneDetails', detailsHtml); Handlebars.registerPartial('sceneTree', treeHtml); Handlebars.registerHelper('typeString', typeToString); Handlebars.registerHelper('listItemOpen', listItemOpen); function Scene(game, parent) { Panel.call(this, game, parent); this.name = 'scene'; this.title = 'Scene Tree'; this._tree = null; this.tree = null; this.details = null; this.refresh = null; this.selected = null; } Scene.prototype = Object.create(Panel.prototype); Scene.prototype.constructor = Scene; module.exports = Scene; Scene.prototype.createPanelElement = function () { Panel.prototype.createPanelElement.call(this); this._panel.innerHTML = panelHtml(this.game.stage); this.tree = this._panel.querySelector('.sidebar'); this.details = this._panel.querySelector('.details'); this.refresh = this._panel.querySelector('.refresh'); ui.on(this.tree, 'click', 'li', this._onLiClick.bind(this)); ui.on(this.refresh, 'click', this._onRefreshClick.bind(this)); this.bmd = this.game.add.bitmapData(512, 256); return this._panel; }; Scene.prototype.rebuildTree = function () { ui.empty(this.tree); _cache = {}; this.tree.innerHTML = treeHtml(this.game.stage); this.select(this.tree.querySelector('li:first-child')); ui.addClass(this.selected, 'expanded'); this.reloadDetails(); }; Scene.prototype.reloadDetails = function () { var id = this.selected.dataset.id; var obj = _cache[id]; this.details.innerHTML = detailsHtml(obj); if (obj.texture) { this.details.appendChild(this.bmd.canvas); var w = Math.min(512, obj.width); var h = Math.min(256, obj.height); this.bmd.clear(); this.bmd.resize(w, h); try { this.bmd.draw(obj, 0, 0, w, h); } catch (e) {}; } }; Scene.prototype.select = function (li) { if (this.selected) { ui.removeClass(this.selected, 'selected'); } this.selected = li; ui.addClass(this.selected, 'selected'); }; Scene.prototype.show = function () { this.rebuildTree(); Panel.prototype.show.call(this); }; Scene.prototype.destroy = function () { Panel.prototype.destroy.call(this); this.tree = null; this.details = null; this.refresh = null; }; Scene.prototype._onLiClick = function (e) { e.stopPropagation(); this.select(e.delegateTarget); ui.toggleClass(e.delegateTarget, 'expanded'); this.reloadDetails(); }; Scene.prototype._onRefreshClick = function (e) { e.preventDefault(); e.stopPropagation(); this.rebuildTree(); }; function listItemOpen () { _cache[++_id] = this; return new Handlebars.SafeString( '