summaryrefslogtreecommitdiff
path: root/public/assets/javascripts/rectangles/models/floor.js
diff options
context:
space:
mode:
Diffstat (limited to 'public/assets/javascripts/rectangles/models/floor.js')
-rw-r--r--public/assets/javascripts/rectangles/models/floor.js146
1 files changed, 146 insertions, 0 deletions
diff --git a/public/assets/javascripts/rectangles/models/floor.js b/public/assets/javascripts/rectangles/models/floor.js
new file mode 100644
index 0000000..ee264c7
--- /dev/null
+++ b/public/assets/javascripts/rectangles/models/floor.js
@@ -0,0 +1,146 @@
+(function(){
+
+ var vec2, Rect, sort
+ if ('window' in this) {
+ vec2 = window.vec2
+ Rect = window.Rect
+ sort = window.sort
+ }
+ else {
+ vec2 = require('./vec2')
+ Rect = require('./rect')
+ UidGenerator = require('../util/uid')
+ }
+
+ var Floor = function(opt){
+ this.id = opt.id
+ this.side = opt.side
+ this.mx = opt.mx
+ }
+
+ Floor.prototype.serialize = function(){
+ return {
+ id: this.id,
+ background: this.background,
+ }
+ }
+
+ Floor.prototype.deserialize = function(data){
+ this.wallpaper( data.background )
+ }
+
+ Floor.prototype.bind = function(){
+ var base = this
+ base.$els = $( this.mx.map(function(mx){ return mx.el }) )
+ console.log("HELLO")
+ this.mx.forEach(function(mx, index){
+ $(mx.el).bind({
+ mousedown: function(e){
+ console.log("clicked on", base.id)
+ if (Scenery.nextWallpaper) {
+ var oldState = base.serialize()
+ base.wallpaper(Scenery.nextWallpaper)
+ Scenery.nextWallpaper = null
+
+ UndoStack.push({
+ type: 'update-wallpaper',
+ undo: oldState,
+ redo: base.serialize(),
+ })
+
+ // TODO: watch individual scenery object here
+ Minotaur.watch( app.router.editorView.settings )
+ }
+ }
+ })
+ })
+
+ // flip the mx order
+ var shouldFlip = this.side & (CEILING)
+ if (! shouldFlip) {
+ this.mx.reverse()
+ }
+ }
+
+ Floor.prototype.color = function(color){
+ this.$els.css("background-color", color)
+ }
+
+ Floor.prototype.wallpaper = function(background){
+ if (! background) {
+ background = { src: "none" }
+ }
+ else if (typeof background == "string") {
+ background = { src: background }
+ }
+ else if (! background.src) {
+ background = { src: "none" }
+ }
+ background.x = background.x || 0
+ background.y = background.y || 0
+ background.scale = background.scale || 1
+
+ this.background = background
+ this.background.src = this.background.src.replace("url(","").replace(")","")
+
+ if (this.background.src == "none") {
+ this.wallpaperLoad(this.background.src)
+ return
+ }
+
+ var img = new Image ()
+ img.onload = function(){
+ this.backgroundImage = img
+ this.wallpaperLoad(this.background.src)
+ this.wallpaperPosition(background)
+ }.bind(this)
+ img.src = this.background.src
+ img.complete && img.onload()
+ }
+
+ Floor.prototype.wallpaperLoad = function(url){
+ if (url !== "none") {
+ url = "url(" + url + ")"
+ }
+ this.mx.forEach(function(mx){
+ mx.el.style.backgroundImage = url
+ })
+ }
+
+ Floor.prototype.wallpaperPosition = function(background){
+ if (this.background.src == "none") { return }
+ this.background.x = background.x || this.background.x
+ this.background.y = background.y || this.background.y
+ this.background.scale = background.scale || this.background.scale || 1
+
+ var shouldFlip = this.side & (CEILING)
+
+ var mx, dx, dy
+ var w = Math.round( this.backgroundImage.naturalWidth * this.background.scale )
+ var h = Math.round( this.backgroundImage.naturalHeight * this.background.scale )
+
+ this.surface.faces.forEach(function(face, i){
+
+ if (shouldFlip) {
+ mx = this.mx[this.mx.length-1-i]
+ dx = Math.round( this.background.x + face.x.a )
+ dy = Math.round( this.background.y + face.y.b )
+ }
+ else {
+ mx = this.mx[i]
+ dx = Math.round( this.background.x - face.x.b )
+ dy = Math.round( this.background.y + face.y.b )
+ }
+
+ mx.el.style.backgroundPosition = dx + 'px ' + dy + 'px'
+ mx.el.style.backgroundSize = w + 'px ' + h + 'px'
+ }.bind(this))
+ }
+
+ if ('window' in this) {
+ window.Floor = Floor
+ }
+ else {
+ module.exports = Floor
+ }
+})()