summaryrefslogtreecommitdiff
path: root/assets/javascripts/rectangles/map/ui.js
blob: f612fe2b380deaa2b414dc0e28eabc1e3629886f (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
map.ui = new function(){

	var base = this
	base.el = document.querySelector("#map")

	base.creating = base.dragging = false

	base.mouse = new mouse({
		el: base.el,
		down: down,
		move: move,
		drag: drag,
		up: up,
	})

	base.wheel = new wheel({
		el: base.el,
		update: function(e, val, delta){
			// do something with val
		},
	})
	
	//
	
	function down (e, cursor){
		cursor.x.div(map.zoom).add(  map.center.a + map.bounds.a/2 )
		cursor.y.div(map.zoom).add( -map.center.b - map.bounds.b/2 )

		var intersects = clipper.rooms.filter(function(r){
			return r.focused = r.rect.contains(cursor.x.a, cursor.y.a)
		})

		if (intersects.length){
			base.dragging = intersects[0]
		}
		else {
			base.creating = true
		}

		if (e.shiftKey && base.dragging) {
			base.dragging.rect.quantize(10)
		}
	}

	function move (e, cursor) {
		cursor.x.div(map.zoom).add(  map.center.a + map.bounds.a/2 )
		cursor.y.div(map.zoom).add( -map.center.b - map.bounds.b/2 )
	}

	function drag (e, cursor) {
		cursor.x.b = (cursor.x.b / map.zoom) + map.center.a + map.bounds.a/2
		cursor.y.b = (cursor.y.b / map.zoom) - map.center.b - map.bounds.b/2

		if (base.dragging) {
			base.dragging.rect.translation.a = cursor.x.magnitude()
			base.dragging.rect.translation.b = cursor.y.magnitude()
		}
	}

	function up (e, cursor, new_cursor) {

		new_cursor.x.div(map.zoom).add(  map.center.a + map.bounds.a/2 )
		new_cursor.y.div(map.zoom).add( -map.center.b - map.bounds.b/2 )

		if (base.creating) {
			if (cursor.height() != 0 && cursor.width() != 0) {
				cursor.x.abs()
				cursor.y.abs()
				clipper.add_room( cursor )
			}
		}
		
		if (base.dragging) {
			base.dragging.rect.translate()
		}

		base.creating = base.dragging = false
	}
}