-
Notifications
You must be signed in to change notification settings - Fork 1
/
ui.js
103 lines (89 loc) · 2.5 KB
/
ui.js
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
function UI() {
this.ui = null
this.selected = null
this.itemSelected = false
this.itemHovered = false
this.mouseDown = false
this.lastMousePos = null
this.run = function(crabs) {
if (this.itemSelected) {
if (crabs.physics.masses[this.selected]) {
var physicsPos = crabs.renderer.s2p(crabs, this.lastMousePos)
crabs.physics.masses[this.selected].pos = physicsPos
}
}
}
this.resize = function(c) {
}
this.mousemove = function(crabs, e) {
var pt = new Vec(e.clientX, e.clientY)
this.lastMousePos = pt
var physicsPos = crabs.renderer.s2p(crabs, pt)
// if one isn't already selected, find the closest object
if (!this.itemSelected) {
var me = this
var physics = crabs.physics
var distance = 50.0 // should be fine until people start having 100K screens
var foundId = ''
crabs.ids.forEach(function(id) {
if (physics.masses[id]) {
var dist = Vec.mag(Vec.sub(physicsPos, physics.masses[id].pos))
if (dist < distance) {
distance = dist
foundId = id
}
} else if (physics.springs[id]) {
var aPos = physics.masses[physics.springs[id].ida].pos
var bPos = physics.masses[physics.springs[id].idb].pos
var center = Vec.scale(0.5, Vec.add(aPos, bPos))
var dist = Vec.mag(Vec.sub(physicsPos, center))
if (dist < distance) {
distance = dist
foundId = id
}
}
})
this.selected = foundId
} else {
if (crabs.physics.masses[this.selected]) {
crabs.physics.masses[this.selected].pos = crabs.renderer.s2p(crabs, pt)
crabs.physics.masses[this.selected].vel = Vec.zero()
}
}
if (crabs.paused) {
crabs.renderer.run(crabs)
}
}
this.mousedown = function(crabs, e) {
this.mouseDown = true
if (this.selected != '') {
this.itemSelected = true
}
}
this.mouseup = function(crabs, e) {
this.mouseDown = false
this.itemSelected = false
}
this.keypress = function(crabs, e) {
if (e.key == 'p' || e.key == 'P') {
crabs.paused = !crabs.paused
if (!crabs.paused) {
crabs.go()
}
}
}
}
function TestUI() {
this.mousemove = function(c, e) {
// log(log.DEBUG, 'mouse move')
// console.log(e)
}
this.click = function(c, e) {
log(log.DEBUG, 'click')
console.log(e)
}
this.keypress = function(c, e) {
log(log.DEBUG, 'keypress')
console.log(e)
}
}