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

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

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

	this.unbind = function(){
	  base.focused = false
		Sculpture.mouse.unbind_el(base.mx.el)
		Sculpture.mouse.off("down", down)
		Sculpture.mouse.off("drag", drag)
		Sculpture.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
		
		oldState = base.serialize()
		document.body.classList.add("dragging")
	}
	
	function drag (e, cursor){
		if (! dragging) return
		
		moved = true

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

    // here we need to move the element based on the XY coords, as 
		// base.mx.y = position.b + delta.b + dimension.b / 2
    base.mx.x = x + delta.a * cos(cam.rotationY) - delta.b * sin(cam.rotationY)
    base.mx.z = z + delta.a * sin(cam.rotationY) + delta.b * cos(cam.rotationY)

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

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

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

		dragging = moved = false
    oldState = null
		document.body.classList.remove("dragging")
  }
	
	return this

}