summaryrefslogtreecommitdiff
path: root/assets/javascripts/rectangles/engine/scenery/image/resize.js
blob: 52d61bd80374fb9f5467613cfa265fdb4cebe76c (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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
Scenery.resize = new function(){

	var base = this
	
	var obj
	var x, y, z, bounds
	var dragging = false
	
	var dots = [], dot

	base.init = function(){
		base.build()
		base.bind()
	}
	
	// create 9 dots at the corners of the div
	base.build = function(){
		[ TOP,
			TOP_RIGHT,
			RIGHT,
			BOTTOM_RIGHT,
			BOTTOM,
			BOTTOM_LEFT,
			LEFT,
			TOP_LEFT ].forEach(base.build_dot)
	}
	
	// generate a dot element
	base.build_dot = function(side) {
		var dot = new MX.Object3D('.dot')
		dot.width = 10
		dot.height = 10
		dot.side = side
		$(dot.el).on({
			mouseenter: function(){ base.hovering = true },
			mouseleave: function(){ base.hovering = false },
		})
		dots.push(dot)
	}

	base.add_dots = function(){
		dots.forEach(function(dot){
			scene.add(dot)
		})
	}
	
	// rotate the dots as appropriate
	base.rotate_dots = function(){
		rotationY = wall_rotation[obj.wall.side]
		dots.forEach(function(dot){
			dot.rotationY = rotationY
		})
	}
	
	// move all the dots to the object's current position
	base.move_dots = function(){		
		x = obj.mx_img.x + sin(rotationY) * 3
		y = obj.mx_img.y
		z = obj.mx_img.z - cos(rotationY) * 3

		dots.forEach(function(dot){
			base.move_dot(dot)
		})
	}
		
	// move a dot .. to the initial position of the image
	base.move_dot = function(dot){
		dot.x = x
		dot.y = y
		dot.z = z
		
		if (dot.side & TOP) {
			dot.y += obj.img.height / 2
		}
		if (dot.side & BOTTOM) {
			dot.y -= obj.img.height / 2
		}
		if (dot.side & LEFT) {
			dot.x -= cos(rotationY) * obj.img.width / 2
			dot.z -= sin(rotationY) * obj.img.width / 2
		}
		if (dot.side & RIGHT) {
			dot.x += cos(rotationY) * obj.img.width / 2
			dot.z += sin(rotationY) * obj.img.width / 2
		}
	}

	// pick a new object to focus on and show the dots
	base.show = function(new_object) {
		if (obj === new_object) return
		
		obj = new_object
		
		base.add_dots()
		base.rotate_dots()
		base.move_dots()
	}

	// dismiss the dots on blur
	var dotsHideTimeout;
	base.defer_hide = function(){
		clearTimeout(dotsHideTimeout)

		dotsHideTimeout = setTimeout(function(){
			if (Scenery.image.hovering || Scenery.resize.hovering || Scenery.mouse.down) return
			Scenery.resize.hide()
		}, dot_hide_delay)
	}
	base.hide = function () {
		obj = null
		dots.forEach(function(dot){
			scene.remove(dot)
		})
	}
	
	base.bind = function(){
		dots.forEach(function(dot){
			Scenery.mouse.bind_el(dot.el)
			$(dot.el).bind({
				mouseenter: function(e){
					console.log('entered a dot')
					Scenery.resize.hovering = true 
				},
				mouseleave: function(e){
					console.log('left a dot')
					Scenery.resize.hovering = false
					base.defer_hide()
				}
			})
		})
		Scenery.mouse.on("drag", drag)
		Scenery.mouse.on("up", up)
	}

	this.unbind = function(){
		dots.forEach(function(dot){
			Scenery.mouse.unbind_el(dot.el)
		})
		Scenery.mouse.off("drag", drag)
		Scenery.mouse.off("up", up)
	}
	
	function down (e, cursor){
		dragging = true
		bounds = obj.wall.bounds_for(img)
		document.body.classList.add("dragging")
	}

	function drag (e, cursor){
		if (! dragging) return
		// cursor.x.magnitude()*cursor_amp
	}

	function up (e, cursor){
		dragging = false
		document.body.classList.remove("dragging")
	}

}