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
|
Map.UI = Map.UI || {}
Map.UI.Editor = function(map){
var base = this
base.creating = base.dragging = base.resizing = false
base.mouse = new mouse({
el: map.el,
down: down,
move: move,
drag: drag,
up: up,
rightclick: rightclick,
})
base.wheel = new wheel({
el: map.el,
update: mousewheel,
})
base.permissions = new Permissions({
create: true,
move: true,
resize: true,
destroy: false,
})
//
function down (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) {
cursor.quantize(1/map.zoom)
map.center.a = cursor.x.a
map.center.b = -cursor.y.a
console.log(map.center+"")
cursor.x.b = cursor.x.a
cursor.y.b = cursor.y.a
base.mouse.down = false
e.preventDefault()
e.stopPropagation()
return
}
var intersects = Rooms.filter(function(r){
return r.focused = r.rect.contains(cursor.x.a, cursor.y.a)
})
if (intersects.length && base.permissions.destroy) {
base.mouse.down = false
Rooms.remove(intersects[0])
app.tube("builder-destroy-room", intersects[0])
return
}
else if (intersects.length && (base.permissions.move || base.permissions.resize)) {
base.dragging = intersects[0]
base.resizing = base.permissions.resize && base.dragging.rect.nearEdge(cursor.x.a, cursor.y.a, resize_margin / map.zoom)
base.dragging.rect.translation.sides = base.resizing
app.tube("builder-pick-room", intersects[0])
}
else if (base.permissions.create) {
base.creating = true
}
else if (intersects.length) {
app.tube("builder-pick-room", intersects[0])
}
if (e.shiftKey && base.dragging) {
base.dragging.rect.quantize(10/map.zoom)
}
}
function move (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)
}
function drag (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
if (base.resizing) {
var x_length = base.dragging.rect.x.length(),
y_length = base.dragging.rect.y.length()
if (base.resizing & LEFT) {
base.dragging.rect.translation.a = clamp( cursor.x.magnitude(), x_length - side_max, x_length - side_min )
}
if (base.resizing & RIGHT) {
base.dragging.rect.translation.a = clamp( cursor.x.magnitude(), side_min - x_length, side_max - x_length )
}
if (base.resizing & FRONT) {
base.dragging.rect.translation.b = clamp( cursor.y.magnitude(), y_length - side_max, y_length - side_min )
}
if (base.resizing & BACK) {
base.dragging.rect.translation.b = clamp( cursor.y.magnitude(), side_min - y_length, side_max - y_length )
}
}
else 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.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)
if (base.creating) {
if (cursor.height() > side_min && cursor.width() > side_min) {
cursor.x.abs().quantize(1)
cursor.y.abs().quantize(1)
var room = Rooms.add_with_rect( cursor )
app.tube("builder-pick-room", room)
}
}
if (base.resizing) {
base.dragging.rect.resize()
}
else if (base.dragging) {
base.dragging.rect.translate()
}
base.creating = base.dragging = base.resizing = false
}
function mousewheel (e, val, delta){
var cursor = base.mouse.cursor
var intersects = Rooms.filter(function(r){
return r.focused = r.rect.contains(cursor.x.a, cursor.y.a)
})
if (intersects.length) {
intersects[0].height = clamp( ~~(intersects[0].height - delta), height_min, height_max )
Rooms.clipper.update()
}
else {
map.set_zoom(map.zoom_exponent - delta/20)
}
}
function rightclick (e){
}
}
|