summaryrefslogtreecommitdiff
path: root/public/assets/javascripts/rectangles/engine/scenery/move.js
blob: 94c628176fb5a7ed3658aecb7f79205bef8a5373 (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
Scenery.move = function(base){

	var x, y, z, position, dimension, bounds
	var dragging = false, moved = false
	var oldState

	this.bind = function(){
		Scenery.mouse.bind_el(base.mx.el)
		Scenery.mouse.on("down", down)
		Scenery.mouse.on("enter", switch_wall)
		Scenery.mouse.on("drag", drag)
		Scenery.mouse.on("up", up)
	}

	this.unbind = function(){
	  base.focused = false
		Scenery.mouse.unbind_el(base.mx.el)
		Scenery.mouse.off("down", down)
		Scenery.mouse.off("enter", switch_wall)
		Scenery.mouse.off("drag", drag)
		Scenery.mouse.off("up", up)
	}
	
	function down (e, cursor){
		if (e.target != base.mx.el && (e.target != base.mx.overlay)) return;
		if (editor.permissions.destroy) {
		  base.remove()
			return
		}
    
		// load the modal
		app.controller.pick(base)

    if (! base.focused) {
			e.clickAccepted = false
			base.focused = true
			return
    }
		if (! (editor.permissions.move || editor.permissions.resize) ) {
			e.clickAccepted = false
			return
		}
		dragging = true
		moved = false
		x = base.mx.x
		y = base.mx.y
		z = base.mx.z
		
		bounds = base.bounds
		dimension = base.dimensions
		position = base.wall.mxToPosition( base.mx, dimension )
		
		oldState = base.serialize()
		document.body.classList.add("dragging")
	}
	
	function drag (e, cursor){
		if (! dragging) return
		
		moved = true

		var flipX = base.wall.side & (FRONT | RIGHT)

		var delta = cursor.delta()
		delta.mul( cursor_amp ) // TODO: this should be proportional to your distance from the wall
		if (flipX) { delta.a *= -1 }
		delta.b *= -1

		var new_bounds = base.wall.surface.translate( bounds, dimension, position, delta )
		if (new_bounds) bounds = new_bounds
		if (flipX) { delta.a *= -1 }

		base.mx.y = position.b + delta.b + dimension.b / 2
		switch (base.wall.side) {
			case FRONT:
			case BACK:
				base.mx.x = position.a + delta.a * cos(base.wall.rotationY) + dimension.a / 2
				break
			case LEFT:
			case RIGHT:
				base.mx.z = position.a + delta.a * sin(base.wall.rotationY) + dimension.a / 2
				break
		}

		if (editor.permissions.resize) {
			Scenery.resize.move_dots()
		}
	}

	function up (e, cursor){
    if (! dragging || ! oldState) return
        
    if (moved) {
      UndoStack.push({
        type: 'update-scenery',
        undo: oldState,
        redo: base.serialize(),
      })

      // TODO: watch individual scenery object here
      Minotaur.watch( app.router.editorView.settings )
    }

		dragging = moved = false
    oldState = null
		document.body.classList.remove("dragging")
  }
	
	function switch_wall (e, target, cursor){
		if (! dragging) return
		if (target.wall.id == base.wall.id) return
		
		var old_wall_side = base.wall.side
		var wall_group = old_wall_side | target.wall.side
		var new_bounds = target.wall.surface.bounds_at_index_with_dimensions(target.index || 0, dimension)

		if (! new_bounds) return

		base.wall = target.wall
		base.bounds = bounds = new_bounds

		position.a = bounds.x.midpoint() - dimension.a / 2

		if (old_wall_side !== target.wall.side && wall_group !== FRONT_BACK && wall_group !== LEFT_RIGHT) {
			switch (old_wall_side) {
				case FRONT:
					position.a = bounds.x.a + dimension.a / 2
					break
				case BACK:
					position.a = bounds.x.b - dimension.a / 2
					break
				case LEFT:
					position.a = bounds.x.a + dimension.a / 2
					break
				case RIGHT:
					position.a = bounds.x.b - dimension.a / 2
					break
			}
		}

		var mx_delta = base.wall.positionToMx( position, dimension )
		base.mx.move(mx_delta)

		if (editor.permissions.resize) {
			Scenery.resize.rotate_dots()
 			Scenery.resize.move_dots()
		}

		cursor.x.a = cursor.x.b
		//var delta = cursor.delta()
		drag(e, cursor)

	}
	
	return this

}