summaryrefslogtreecommitdiff
path: root/public/assets/javascripts/rectangles/engine/scenery/_scenery.js
blob: 4cf5b061f90b7c61f7257cbdc7195df0ee58a2b6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
var Scenery = new function(){
	
	var base = this;

	base.list = {}
	base.nextMedia = null
	
	base.mouse = new mouse ({ use_offset: false })
	
	base.init = function(){
		base.resize.init()
	}

	base.add = function(opt){
		var scene_media
		switch (opt.media.type) {
			case 'image':
				scene_media = new Scenery.types.image (opt)
				break
			
			case 'video':
			case 'youtube':
			case 'vimeo':
				scene_media = new Scenery.types.video (opt)
				break
		}
		base.list[scene_media.id] = scene_media
		return scene_media
	}
	
	base.addNextToWall = function(opt){
		opt.media = base.nextMedia
		opt.index = opt.index || 0
		var scene_media = base.add(opt)
		
		// test if scenery was placed here
		if (! scene_media.bounds) {
			base.remove( scene_media.id )
			return null
		}
		else {
			base.nextMedia = null
			return scene_media
		}
	}

	base.find = function(id){
		return base.list[id] || null
	}
		
	base.remove = function(id){
		var scene_media = base.list[id]
		delete base.list[id]
		scene_media && scene_media.destroy()
	}
	
	base.removeAll = function(){
		base.forEach(function(scene_media){
			base.remove(scene_media.id)
		})
	}

	base.uid = new UidGenerator(base.list)
	
	base.forEach = function(f){
		return base.values().forEach(f)
	}
	
	base.map = function(f){
		return base.values().map(f)
	}
	
	base.values = function(){
		return _.values(base.list)
	}
	
	base.serialize = function(){
		var scenery = base.map(function(media){
			return media.serialize()
		})
		return scenery
	}

	base.deserialize = function(scenery_data){
		scenery_data.forEach(function(data){
			var wall = Walls.list[data.wall_id] || Walls.first()
			var scene_media = base.add({
			  data: data,
				wall: wall,
				media: data.media,
				id: data.id
			})
		})
	}

	return base
}

Scenery.types = {}