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

	var x, y, z, position, dimension, bounds
	var rotationY
	var dragging = false, altPressed = false, shiftPressed = false, moved = false
	var oldState
	var height, width

	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
		}
		altPressed = (e.altKey && ! base.billboard)
    shiftPressed = e.shiftKey
		dragging = true
		moved = false
		x = base.mx.x
		y = base.mx.y
		z = base.mx.z
		rotationY = base.mx.rotationY
		height = base.mx.height * base.mx.scale
		
		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
    if (shiftPressed) {
      base.mx.y = clamp( y + delta.b, height/2 + sculpture_distance_from_floor, Infinity )
    }
    else if (altPressed) {
      base.mx.rotationY = mod( rotationY + delta.a / (window.innerWidth / 2) , TWO_PI )
      Sculpture.resize.move_dots()
    }
    else {
      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()
			base.updateOutline()
		}
	}

	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

}