Skip to content

Instantly share code, notes, and snippets.

@GirlBossRush
Created May 17, 2012 01:38
Show Gist options
  • Save GirlBossRush/2715534 to your computer and use it in GitHub Desktop.
Save GirlBossRush/2715534 to your computer and use it in GitHub Desktop.
# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://jashkenas.github.com/coffee-script/
# A word of warning. I'm aware that this is a bit of mess. Development is comprimise; tread lightly.
# The state should only be used for shared data that must be received.
stateDelgator = (state) ->
# Deal with any changes
$.each state.addedKeys, (index, key) ->
console.log("Added keys", index, key)
console.log("Executing #{key.key}(#{key.value})")
eval("#{key.key}(#{key.value})")
# The message system is useful for trival events like mouse movements.
messageDelgator = (message) ->
$.each message, (key, value) ->
console.log("Message received", key, value)
console.log("Executing #{key.key}(#{key.value})")
eval("#{key.key}(#{key.value})")
getQuestion = (scenario_id, text_language, answer_language) ->
$.getJSON "/scenarios/#{scenario_id}/questions/random", {text_language, answer_language},
(data) ->
$("#message").html("<p>#{data["text"]}</p>")
updatePropPosition = (prop, x, y) ->
position = $("##{prop}").offset()
$("#message").html("<p>#{prop} X: #{position.left} Y: #{position.top}</p>")
getScenario = (scenario_id) ->
# Clear the area
$(".prop-container").html('')
$("#scenario-tools").html('')
# Load scene
$.getJSON ("/scenarios/#{scenario_id}/show_scene"), {},
(data) ->
# Push out the old scene
$("#scene").hide("slide", { direction: "left" }, 1000)
$("#scene").html("<img src=#{data["image"]["url"]} id='scene-image' />")
$("#scene-image").load () ->
# TODO: An odd jerk shows up at the end of the animation. Possibly margin related?
$("#scene").show "slide", { direction: "right" }, 1000, ()->
# Load Props.
$(".prop-container").html('')
$.getJSON ("/scenarios/#{scenario_id}/props/"), {},
(data) ->
$.each data, (index, prop) ->
# We append the '-prop' suffix to avoid ID clashing
$(".prop-container").append("<img src='#{prop.image.url}' id='#{prop.name}-prop' class='prop' style='top:#{prop.y}px; left:#{prop.x}px;'/>")
$("##{prop.name}-prop").load () ->
$("##{prop.name}-prop").show("bounce", {times: 0}, "fast")
$("##{prop.name}-prop").draggable true,
containment: "parent",
drag: (event, ui) ->
console.log(event,ui, ui.helper[0].id)
updatePropPosition(ui.helper[0].id, ui.position.left, ui.position.top)
$("#scenario-tools").append("<li><a id='random-question' href='#load-question'>Random Question</a></li>")
$("#scenario-tools").append("<li class='divider-vertical'></li>")
# Load available masks.
$.getJSON ("/scenarios/#{scenario_id}/masks/"), {},
(data) ->
$.each data, (index, mask) ->
$("#scenario-tools").append("<li><a id='load-mask' href='#load-mask' data-id=#{mask["id"]}>Load #{mask["name"]}</a></li>")
# Pull back the curtains and start the show
if ($(".inner-curtain").is(":visible"))
$(".left-curtain").hide("slide", { direction: "left" }, 2000)
$(".right-curtain").hide("slide", { direction: "right" }, 2000)
$(".infotainment .exclamation #text").html("<span>Loaded!</span>")
loadMask = (scenario_id, mask_id) ->
console.log("loading mask")
$.getJSON ("/scenarios/#{scenario_id}/masks/#{mask_id}"), {},
(data) ->
console.log("data fetched", data)
window.overlay = null
image = gapi.hangout.av.effects.createImageResource(data.image.url)
console.log("Image resource", image)
window.overlay = image.createFaceTrackingOverlay(
'trackingFeature': data.track
'rotateWithFace': data.rotate_with_face
'scaleWithFace': data.scale_with_face
'scale': data.scale
'rotation': data.rotation
'offset':
'x': data.x_offset
'y': data.y_offset)
console.log("overlay", overlay)
window.overlay.setVisible(true)
$ ->
gapi.hangout.onApiReady.add ()->
gapi.hangout.data.onStateChanged.add (data) ->
console.log("Change!", data)
stateDelgator(data)
gapi.hangout.data.onMessageReceived.add (data) ->
console.log("Message!", data)
messageDelgator(data)
$("#load-scenario").click ()->
window.scenario_id = $(this).data("id")
scenario_id = $(this).data("id")
gapi.hangout.data.setValue("getScenario", scenario_id.toString())
$(document).on "click", "#random-question",
() ->
getQuestion(window.scenario_id,"English", "English")
$(document).on "click", "#load-mask",
() ->
loadMask(window.scenario_id, $(this).data("id"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment