-
Notifications
You must be signed in to change notification settings - Fork 4
/
app.coffee
95 lines (75 loc) · 2.77 KB
/
app.coffee
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
# Contains code that makes use of jquery to interact with the user, manage the UI and start the application.
$(document).ready ->
window.renderer = new Boids2DRenderer($("canvas")[0])
window.simulation = new Boids(window.renderer)
window.simulation.start()
displayOnButtonClick("#options-button", "#options", "#info")
displayOnButtonClick("header", "#info", "#options")
$("header").hover( ->
$("#instruction").slideDown(500)
, ->
$("#instruction").slideUp(500)
)
$("#play").click ->
window.simulation.start()
setActiveButton("play")
$("#pause").click ->
window.simulation.pause()
setActiveButton("pause")
$("#stop").click ->
window.simulation.stop()
setActiveButton("stop")
window.goalSet = false
$("#sim").click (e) ->
if window.goalSet
window.simulation.unsetGoal()
window.goalSet = false
else
window.simulation.setGoal(e.pageX, e.pageY)
window.goalSet = true
$("#sim").mousemove (e) ->
window.simulation.setGoal(e.pageX, e.pageY) if window.clicking
initializeCheckboxes()
initializeSliders()
$(window).resize -> window.renderer.handleResize()
displayOnButtonClick = (button, div, hide) ->
$("#{button}").click ->
if hide
$("#{hide}").slideUp(1000) if $("#{hide}").is(":visible")
if $("#{div}").is(':visible')
$("#{div}").slideUp(1000)
else
$("#{div}").slideDown(1000)
setActiveButton = (btn) ->
$("#play").removeClass("active")
$("#stop").removeClass("active")
$("#pause").removeClass("active")
$("##{btn}").addClass("active")
initializeSliders = ->
$("#boidsNumber > .slider").slider sliderArguments('boidsNumber', 10, 500, 10)
$("#flockSize > .slider").slider sliderArguments('flockSize', 5, 500, 5)
options = ["simulationSpeed", "acceleration", "perceivedCenterWeight", "perceivedVelocityWeight", "collisionAvoidanceWeight", "stayInBoundsWeight"]
for option in options
$("##{option} > .slider").slider sliderArguments(option)
sliderArguments = (option, min = 0, max = 20, step = 1) ->
value = window.simulation.get(option)
setOption(option, value)
value: value
min: min
max: max
step: step
slide: (event, ui) -> setOption(option, ui.value)
setOption = (option, value) ->
window.simulation.set(option, value)
$("##{option} > .value").html(value)
if value == 0
$("##{option} > .value").addClass('zero')
else
$("##{option} > .value").removeClass('zero')
initializeCheckboxes = ->
options = ["showAveragePosition", "showVelocityVectors"]
for option in options
console.log option
console.log window.renderer.get(option)
$("##{option} > input").prop("checked", window.renderer.get(option))
$("##{option} > input").change -> window.renderer.set($(this).parent().attr('id'), $(this).is(":checked"))