From 28d2d49b1cd13e4a0903c2ae85a2395f56202df4 Mon Sep 17 00:00:00 2001 From: Lewis Hemens Date: Thu, 9 Jan 2014 20:18:46 +0000 Subject: [PATCH] Refactor namespace to be less verbose --- compile.sh | 2 +- js-lib/common.js | 15 +++++++++++++-- js-lib/controller.js | 25 +++++++++++++------------ js-lib/launch.js | 21 ++++++++++++--------- js-lib/state.js | 20 ++++++++++---------- js-lib/view.js | 18 +++++++++--------- 6 files changed, 58 insertions(+), 43 deletions(-) diff --git a/compile.sh b/compile.sh index c5f720e..9c1e558 100755 --- a/compile.sh +++ b/compile.sh @@ -1 +1 @@ -../closure-library/closure/bin/build/closurebuilder.py --root=../closure-library/ --root=js-lib/ --namespace="asciiflow.launch" --compiler_flags="--warning_level=VERBOSE" --compiler_flags="--formatting=PRETTY_PRINT" --compiler_flags="--compilation_level=ADVANCED_OPTIMIZATIONS" --compiler_flags="--externs=jquery-1.9-externs.js" --output_mode=compiled --compiler_jar=closure-compiler.jar > js-compiled.js +../closure-library/closure/bin/build/closurebuilder.py --root=../closure-library/ --root=js-lib/ --namespace="ascii.launch" --compiler_flags="--warning_level=VERBOSE" --compiler_flags="--formatting=PRETTY_PRINT" --compiler_flags="--compilation_level=ADVANCED_OPTIMIZATIONS" --compiler_flags="--externs=jquery-1.9-externs.js" --output_mode=compiled --compiler_jar=closure-compiler.jar > js-compiled.js diff --git a/js-lib/common.js b/js-lib/common.js index d825640..fc24c29 100644 --- a/js-lib/common.js +++ b/js-lib/common.js @@ -1,12 +1,23 @@ /** * Common classes. */ -goog.provide('asciiflow.common'); +goog.provide('ascii.Position'); /** * @constructor */ -asciiflow.common.Position = function(x, y) { +ascii.Position = function(x, y) { /** type {Number} */ this.x = x; /** type {Number} */ this.y = y; }; + +goog.provide('ascii.Blah'); + +/** + * @constructor + */ +ascii.Blah = function(x, y) { + /** type {Number} */ this.x = x; + /** type {Number} */ this.y = y; +}; + diff --git a/js-lib/controller.js b/js-lib/controller.js index d2a2600..624d567 100644 --- a/js-lib/controller.js +++ b/js-lib/controller.js @@ -2,46 +2,47 @@ * Handles user input events and modifies state. * Core logic comes through this class. */ -goog.provide('asciiflow.Controller'); +goog.provide('ascii.Controller'); -goog.require('asciiflow.View'); +goog.require('ascii.Position'); +goog.require('ascii.View'); /** * @constructor */ -asciiflow.Controller = function(view) { - /** type {asciiflow.View} */ +ascii.Controller = function(view) { + /** type {ascii.View} */ this.view = view; - /** type {asciiflow.common.Position} */ + /** type {ascii.Position} */ this.clickPosition; this.installDesktopBindings(); }; -asciiflow.Controller.prototype.handlePress = function(x, y) { - this.clickPosition = new asciiflow.common.Position(x, y); +ascii.Controller.prototype.handlePress = function(x, y) { + this.clickPosition = new ascii.Position(x, y); }; -asciiflow.Controller.prototype.handleMove = function(x, y) { +ascii.Controller.prototype.handleMove = function(x, y) { if (this.clickPosition) { // Drag has started. this.view.offset.x -= (x - this.clickPosition.x)/this.view.zoom; this.view.offset.y -= (y - this.clickPosition.y)/this.view.zoom; - this.clickPosition = new asciiflow.common.Position(x, y); + this.clickPosition = new ascii.Position(x, y); } }; -asciiflow.Controller.prototype.handleRelease = function(x, y) { +ascii.Controller.prototype.handleRelease = function(x, y) { this.clickPosition = null; }; -asciiflow.Controller.prototype.handleZoom = function(delta) { +ascii.Controller.prototype.handleZoom = function(delta) { this.view.zoom *= delta > 0 ? 1.1 : 0.9; this.view.zoom = Math.max(Math.min(this.view.zoom, 5), 0.2); }; -asciiflow.Controller.prototype.installDesktopBindings = function() { +ascii.Controller.prototype.installDesktopBindings = function() { var controller = this; $(this.view.canvas).bind('mousewheel', function(e) { controller.handleZoom(e.originalEvent.wheelDelta); diff --git a/js-lib/launch.js b/js-lib/launch.js index 4c4faef..13b9912 100644 --- a/js-lib/launch.js +++ b/js-lib/launch.js @@ -1,17 +1,20 @@ /** * Application main entry point. */ -goog.provide('asciiflow.launch'); +goog.provide('ascii.launch'); -goog.require('asciiflow.Controller'); -goog.require('asciiflow.State'); -goog.require('asciiflow.View'); +goog.require('ascii.Controller'); +goog.require('ascii.State'); +goog.require('ascii.View'); -asciiflow.launch = function() { - var state = new asciiflow.State(); - var view = new asciiflow.View(state); - var controller = new asciiflow.Controller(view); +/** + * @private + */ +ascii.launch = function() { + var state = new ascii.State(); + var view = new ascii.View(state); + var controller = new ascii.Controller(view); view.animate(); }; -asciiflow.launch(); +ascii.launch(); diff --git a/js-lib/state.js b/js-lib/state.js index f50d98d..7036edb 100644 --- a/js-lib/state.js +++ b/js-lib/state.js @@ -1,31 +1,31 @@ /** * Classes holding the state of the ascii-diagram. */ -goog.provide('asciiflow.State'); +goog.provide('ascii.State'); /** * @constructor */ -asciiflow.Cell = function() { +ascii.Cell = function() { /** @type {string|null} */ // Uses the string "#" for lines. this.value = null; }; -asciiflow.Cell.prototype.setValue = function(value) { +ascii.Cell.prototype.setValue = function(value) { this.value = value; }; /** * @constructor */ -asciiflow.State = function() { - /** @type {Array.>} */ - this.cells = new Array(asciiflow.State.MAX_SIZE); +ascii.State = function() { + /** @type {Array.>} */ + this.cells = new Array(ascii.State.MAX_SIZE); for (var i = 0; i < this.cells.length; i++) { - this.cells[i] = new Array(asciiflow.State.MAX_SIZE); + this.cells[i] = new Array(ascii.State.MAX_SIZE); for (var j = 0; j < this.cells[i].length; j++) { - this.cells[i][j] = new asciiflow.Cell(); + this.cells[i][j] = new ascii.Cell(); // Hack: Just fill image with random stuff for now. if ((i % 10 == 0) && (j % 10 == 0)) { var jstr = ("" + j); @@ -35,7 +35,7 @@ asciiflow.State = function() { } }; -/** @const */ asciiflow.State.MAX_SIZE = 1000; +/** @const */ ascii.State.MAX_SIZE = 1000; -asciiflow.State.prototype.blah = function() { +ascii.State.prototype.blah = function() { }; diff --git a/js-lib/view.js b/js-lib/view.js index 31e3e18..247eecc 100644 --- a/js-lib/view.js +++ b/js-lib/view.js @@ -1,34 +1,34 @@ /** * Functions relating to view operations and management of the screen. */ -goog.provide('asciiflow.View'); +goog.provide('ascii.View'); -goog.require('asciiflow.common'); +goog.require('ascii.Position'); /** * @constructor */ -asciiflow.View = function(state) { +ascii.View = function(state) { /** type {Element} */ this.canvas = document.getElementById('ascii-canvas'); /** type {Object} */ this.context = this.canvas.getContext('2d'); /** type {number} */ this.zoom = 1; - /** type {asciiflow.common.Position} */ this.offset = new asciiflow.common.Position(0, 0); - /** type {asciiflow.State} */ this.state = state; + /** type {ascii.Position} */ this.offset = new ascii.Position(0, 0); + /** type {ascii.State} */ this.state = state; this.resizeCanvas(); }; -asciiflow.View.prototype.resizeCanvas = function() { +ascii.View.prototype.resizeCanvas = function() { this.canvas.width = document.documentElement.clientWidth; this.canvas.height = document.documentElement.clientHeight; }; -asciiflow.View.prototype.animate = function() { +ascii.View.prototype.animate = function() { this.render(); var view = this; window.requestAnimationFrame(function() { view.animate(); }); }; -asciiflow.View.prototype.render = function() { +ascii.View.prototype.render = function() { this.context.setTransform(1, 0, 0, 1, 0, 0); // Clear the visible area. this.context.clearRect(0, 0, this.canvas.width, this.canvas.height); @@ -49,7 +49,7 @@ asciiflow.View.prototype.render = function() { /** * Given a screen coordinate, find the integer cell position that it relates to. */ -asciiflow.View.prototype.getCell = function(x, y) { +ascii.View.prototype.getCell = function(x, y) { };