summaryrefslogtreecommitdiff
path: root/public/assets/javascripts/rectangles/engine/map/ui_ortho.js
blob: adcc0787ada1e87c19276bebfc0c4ee842da6221 (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
Map.UI = Map.UI || {}
Map.UI.Ortho = function(map){

	var base = this

	base.creating = base.dragging = base.resizing = false

	base.mouse = new mouse({
		el: map.el,
		down: function(e, cursor){
			cursor.x.div(map.dimensions.a).add(0.5).mul(map.dimensions.a / map.zoom).add(map.center.a)
			cursor.y.div(map.dimensions.b).sub(0.5).mul(map.dimensions.b / map.zoom).sub(map.center.b)

      if (e.ctrlKey || e.which === 3) {
        if (placing) {
          // close polyline or cancel
          return
        }
        cursor.quantize(1/map.zoom)
        map.center.a = cursor.x.a
        map.center.b = -cursor.y.a
        cursor.x.b = cursor.x.a
        cursor.y.b = cursor.y.a
        base.mouse.down = false
        e.preventDefault()
        e.stopPropagation()
        return
      }

			// compare to initial point
			var p = new vec2( cursor.x.a, cursor.y.a )
			if (placing) {
				if (points.length > 2 && points[0].distanceTo( p ) < 10/map.zoom) {
					points.push( points[0].clone() )
					placing = false
					add_mx_polyline(points)
				}
				else {
					points.push( p )
          mx_points.push( add_mx_point(p) )
				}
			}
			else {
				placing = true
				points = []
				points.push( p )
        mx_points.push( add_mx_point(p) )
			}
		},
		move: function(e, cursor){
			cursor.x.div(map.dimensions.a).add(0.5).mul(map.dimensions.a / map.zoom).add(map.center.a)
			cursor.y.div(map.dimensions.b).sub(0.5).mul(map.dimensions.b / map.zoom).sub(map.center.b)
			last_point = new vec2( cursor.x.a, cursor.y.a )
			if (placing && points.length > 1 && points[0].distanceTo( last_point ) < 10/map.zoom) {
				document.body.style.cursor = "pointer"
				last_point.assign(points[0])
				cursor.x.a = cursor.x.b = last_point.a
				cursor.y.a = cursor.y.b =last_point.b
			}
			else {
				document.body.style.cursor = "crosshair"
			}
		},
		drag: function(e, cursor){
			cursor.x.b = ((cursor.x.b/map.dimensions.a)+0.5) * map.dimensions.a / map.zoom + map.center.a
			cursor.y.b = ((cursor.y.b/map.dimensions.b)-0.5) * map.dimensions.b / map.zoom - map.center.b
		},
		up: function(e, cursor, new_cursor){
			new_cursor.x.div(map.dimensions.a).add(0.5).mul(map.dimensions.a / map.zoom).add(map.center.a)
			new_cursor.y.div(map.dimensions.b).sub(0.5).mul(map.dimensions.b / map.zoom).sub(map.center.b)
		}
	})

	base.wheel = new wheel({
		el: map.el,
		update: mousewheel,
	})

	function mousewheel (e, deltaY, deltaX){
		map.set_zoom(map.zoom_exponent - deltaY/20)
	}
}