Adding communication with iPad app.

This commit is contained in:
Nick Winter 2014-09-06 19:50:31 -07:00
parent 8de75024fc
commit 2f07c3e8d3
13 changed files with 188 additions and 68 deletions

View file

@ -37,6 +37,8 @@ preload = (arrayOfImages) ->
Application = initialize: -> Application = initialize: ->
Router = require('Router') Router = require('Router')
@isProduction = -> document.location.href.search('codecombat.com') isnt -1 @isProduction = -> document.location.href.search('codecombat.com') isnt -1
@isIPadApp = webkit?.messageHandlers? and navigator.userAgent?.indexOf('iPad') isnt -1
$('body').addClass 'ipad' if @isIPadApp
@tracker = new Tracker() @tracker = new Tracker()
@facebookHandler = new FacebookHandler() @facebookHandler = new FacebookHandler()
@gplusHandler = new GPlusHandler() @gplusHandler = new GPlusHandler()

View file

@ -21,20 +21,16 @@ definitionSchemas =
init = -> init = ->
watchForErrors() watchForErrors()
setUpIOSLogging()
path = document.location.pathname path = document.location.pathname
testing = path.startsWith '/test' testing = path.startsWith '/test'
demoing = path.startsWith '/demo' demoing = path.startsWith '/demo'
initializeServices() unless testing or demoing initializeServices() unless testing or demoing
setUpBackboneMediator()
# Set up Backbone.Mediator schemas
setUpDefinitions()
setUpChannels()
Backbone.Mediator.setValidationEnabled document.location.href.search(/codecombat.com/) is -1
app.initialize() app.initialize()
Backbone.history.start({ pushState: true }) Backbone.history.start({ pushState: true })
handleNormalUrls() handleNormalUrls()
setUpMoment() # Set up i18n for moment setUpMoment() # Set up i18n for moment
treemaExt = require 'treema-ext' treemaExt = require 'treema-ext'
treemaExt.setup() treemaExt.setup()
@ -59,13 +55,15 @@ handleNormalUrls = ->
return false return false
setUpChannels = -> setUpBackboneMediator = ->
for channel of channelSchemas Backbone.Mediator.addDefSchemas schemas for definition, schemas of definitionSchemas
Backbone.Mediator.addChannelSchemas channelSchemas[channel] Backbone.Mediator.addChannelSchemas schemas for channel, schemas of channelSchemas
Backbone.Mediator.setValidationEnabled document.location.href.search(/codecombat.com/) is -1
setUpDefinitions = -> if webkit?.messageHandlers
for definition of definitionSchemas originalPublish = Backbone.Mediator.publish
Backbone.Mediator.addDefSchemas definitionSchemas[definition] Backbone.Mediator.publish = ->
originalPublish.apply Backbone.Mediator, arguments
webkit.messageHandlers.backboneEventHandler?.postMessage channel: arguments[0], event: serializeForIOS(arguments[1] ? {})
setUpMoment = -> setUpMoment = ->
{me} = require 'lib/auth' {me} = require 'lib/auth'
@ -94,11 +92,53 @@ watchForErrors = ->
return if currentErrors >= 3 return if currentErrors >= 3
return unless me.isAdmin() or document.location.href.search(/codecombat.com/) is -1 or document.location.href.search(/\/editor\//) isnt -1 return unless me.isAdmin() or document.location.href.search(/codecombat.com/) is -1 or document.location.href.search(/\/editor\//) isnt -1
++currentErrors ++currentErrors
msg = "Error: #{msg}<br>Check the JS console for more." message = "Error: #{msg}<br>Check the JS console for more."
#msg += "\nLine: #{line}" if line? #msg += "\nLine: #{line}" if line?
#msg += "\nColumn: #{col}" if col? #msg += "\nColumn: #{col}" if col?
#msg += "\nError: #{error}" if error? #msg += "\nError: #{error}" if error?
#msg += "\nStack: #{stack}" if stack = error?.stack #msg += "\nStack: #{stack}" if stack = error?.stack
noty text: msg, layout: 'topCenter', type: 'error', killer: false, timeout: 5000, dismissQueue: true, maxVisible: 3, callback: {onClose: -> --currentErrors} noty text: message, layout: 'topCenter', type: 'error', killer: false, timeout: 5000, dismissQueue: true, maxVisible: 3, callback: {onClose: -> --currentErrors}
Backbone.Mediator.publish 'application:error', message: msg # For iOS app
setUpIOSLogging = ->
return unless webkit?.messageHandlers
for level in ['debug', 'log', 'info', 'warn', 'error']
do (level) ->
originalLog = console[level]
console[level] = ->
originalLog.apply console, arguments
try
webkit?.messageHandlers?.consoleLogHandler?.postMessage level: level, arguments: (a?.toString?() ? ('' + a) for a in arguments)
catch e
webkit?.messageHandlers?.consoleLogHandler?.postMessage level: level, arguments: ['could not post log: ' + e]
# This is so hacky... hopefully it's restrictive enough to not be slow.
# We could also keep a list of events we are actually subscribed for and only try to send those over.
seen = null
window.serializeForIOS = serializeForIOS = (obj, depth=3) ->
return {} unless depth
root = not seen?
seen ?= []
clone = {}
keysHandled = 0
for own key, value of obj
continue if ++keysHandled > 20
if not value
clone[key] = value
else if value is window or value.firstElementChild or value.preventDefault
null # Don't include these things
else if value in seen
null # No circular references
else if _.isArray value
clone[key] = (serializeForIOS(child, depth - 1) for child in value)
seen.push value
else if _.isObject value
value = value.attributes if value.id and value.attributes
clone[key] = serializeForIOS value, depth - 1
seen.push value
else
clone[key] = value
seen = null if root
clone
$ -> init() $ -> init()

View file

@ -509,7 +509,8 @@ module.exports = Surface = class Surface extends CocoClass
## newHeight = Math.min 589, newHeight ## newHeight = Math.min 589, newHeight
#@canvas.width newWidth #@canvas.width newWidth
#@canvas.height newHeight #@canvas.height newHeight
@canvas.attr width: newWidth, height: newHeight scaleFactor = if application.isIPadApp then 2 else 1 # Retina
@canvas.attr width: newWidth * scaleFactor, height: newHeight * scaleFactor
@stage.scaleX *= newWidth / oldWidth @stage.scaleX *= newWidth / oldWidth
@stage.scaleY *= newHeight / oldHeight @stage.scaleY *= newHeight / oldHeight
@camera.onResize newWidth, newHeight @camera.onResize newWidth, newHeight
@ -672,4 +673,3 @@ module.exports = Surface = class Surface extends CocoClass
clearTimeout @surfacePauseTimeout if @surfacePauseTimeout clearTimeout @surfacePauseTimeout if @surfacePauseTimeout
clearTimeout @surfaceZoomPauseTimeout if @surfaceZoomPauseTimeout clearTimeout @surfaceZoomPauseTimeout if @surfaceZoomPauseTimeout
super() super()

View file

@ -195,6 +195,7 @@ module.exports = class SuperModel extends Backbone.Model
@progress = newProg @progress = newProg
@trigger('update-progress', @progress) @trigger('update-progress', @progress)
@trigger('loaded-all') if @finished() @trigger('loaded-all') if @finished()
Backbone.Mediator.publish 'supermodel:load-progress-changed', progress: @progress
setMaxProgress: (@maxProgress) -> setMaxProgress: (@maxProgress) ->
resetProgress: -> @progress = 0 resetProgress: -> @progress = 0

View file

@ -4,6 +4,10 @@ module.exports =
'application:idle-changed': c.object {}, 'application:idle-changed': c.object {},
idle: {type: 'boolean'} idle: {type: 'boolean'}
'application:error': c.object {},
message: {type: 'string'}
stack: {type: 'string'}
'audio-player:loaded': c.object {required: ['sender']}, 'audio-player:loaded': c.object {required: ['sender']},
sender: {type: 'object'} sender: {type: 'object'}
@ -31,3 +35,6 @@ module.exports =
'ladder:game-submitted': c.object {required: ['session', 'level']}, 'ladder:game-submitted': c.object {required: ['session', 'level']},
session: {type: 'object'} session: {type: 'object'}
level: {type: 'object'} level: {type: 'object'}
'supermodel:load-progress-changed': c.object {required: ['progress']},
progress: {type: 'number', minimum: 0, maximum: 1}

View file

@ -109,3 +109,5 @@ module.exports =
'tome:toggle-maximize': c.object {title: 'Toggle Maximize', description: 'Published when we want to make the Tome take up most of the screen'} 'tome:toggle-maximize': c.object {title: 'Toggle Maximize', description: 'Published when we want to make the Tome take up most of the screen'}
'tome:maximize-toggled': c.object {title: 'Maximize Toggled', description: 'Published when the Tome has changed maximize/minimize state.'} 'tome:maximize-toggled': c.object {title: 'Maximize Toggled', description: 'Published when the Tome has changed maximize/minimize state.'}
'tome:select-primary-sprite': c.object {title: 'Select Primary Sprite', description: 'Published to get the most important sprite\'s code selected.'}

View file

@ -210,3 +210,56 @@ html.fullscreen-editor
width: 95% width: 95%
height: 100% height: 100%
right: 0 right: 0
body.ipad #level-view
// Full-width Surface, preserving aspect ratio.
height: 1024px * (589 / 924)
overflow: hidden
#code-area, .footer, #thang-hud
display: none
#control-bar-view
position: absolute
background: transparent
z-index: 1
.home
i
@include opacity(1)
.home-text
display: none
.title
left: 20%
width: 60%
text-align: center
color: white
a, .editor-dash
display: none
#level-chat-view
bottom: 40px
#playback-view
background-color: transparent
z-index: 3
bottom: 30px
margin-bottom: -30px
button
background-color: #333
.scrubber .progress
background-color: rgba(255, 255, 255, 0.4)
#canvas-wrapper, #control-bar-view, #playback-view, #thang-hud
width: 100%
#canvas-wrapper
height: 653px
canvas#surface
margin: 0 auto
overflow: hidden

View file

@ -2,11 +2,11 @@ h4.home
a(href=homeLink || "/") a(href=homeLink || "/")
i.icon-home.icon-white i.icon-home.icon-white
span(data-i18n="play_level.home") Home span(data-i18n="play_level.home").home-text Home
h4.title h4.title
| #{worldName} | #{worldName}
span.spl.spr - span.spl.spr.editor-dash -
a(href=editorLink, data-i18n="nav.editor", title="Open " + worldName + " in the Level Editor") Editor a(href=editorLink, data-i18n="nav.editor", title="Open " + worldName + " in the Level Editor") Editor
if multiplayerSession if multiplayerSession
- found = false - found = false

View file

@ -319,6 +319,10 @@ module.exports = class CocoView extends Backbone.View
isMac: -> isMac: ->
navigator.platform.toUpperCase().indexOf('MAC') isnt -1 navigator.platform.toUpperCase().indexOf('MAC') isnt -1
isIPadApp: ->
return @_isIPadApp if @_isIPadApp?
return @_isIPadApp = webkit?.messageHandlers? and navigator.userAgent?.indexOf('iPad') isnt -1
initSlider: ($el, startValue, changeCallback) -> initSlider: ($el, startValue, changeCallback) ->
slider = $el.slider({animate: 'fast'}) slider = $el.slider({animate: 'fast'})
slider.slider('value', startValue) slider.slider('value', startValue)

View file

@ -131,6 +131,7 @@ module.exports = class PlayLevelView extends RootView
updateProgress: (progress) -> updateProgress: (progress) ->
super(progress) super(progress)
return if @seenDocs return if @seenDocs
return if @isIPadApp()
return unless @levelLoader.session.loaded and @levelLoader.level.loaded return unless @levelLoader.session.loaded and @levelLoader.level.loaded
return unless showFrequency = @levelLoader.level.get('showsGuide') return unless showFrequency = @levelLoader.level.get('showsGuide')
session = @levelLoader.session session = @levelLoader.session
@ -173,10 +174,11 @@ module.exports = class PlayLevelView extends RootView
@insertSubView @loadingView = new LevelLoadingView {} @insertSubView @loadingView = new LevelLoadingView {}
@$el.find('#level-done-button').hide() @$el.find('#level-done-button').hide()
$('body').addClass('is-playing') $('body').addClass('is-playing')
$('body').bind('touchmove', false) if @isIPadApp()
afterInsert: -> afterInsert: ->
super() super()
@showWizardSettingsModal() if not me.get('name') @showWizardSettingsModal() if not me.get('name') and not @isIPadApp()
# Partially Loaded Setup #################################################### # Partially Loaded Setup ####################################################
@ -259,7 +261,7 @@ module.exports = class PlayLevelView extends RootView
@insertSubView new ChatView levelID: @levelID, sessionID: @session.id, session: @session @insertSubView new ChatView levelID: @levelID, sessionID: @session.id, session: @session
worldName = utils.i18n @level.attributes, 'name' worldName = utils.i18n @level.attributes, 'name'
@controlBar = @insertSubView new ControlBarView {worldName: worldName, session: @session, level: @level, supermodel: @supermodel, playableTeams: @world.playableTeams} @controlBar = @insertSubView new ControlBarView {worldName: worldName, session: @session, level: @level, supermodel: @supermodel, playableTeams: @world.playableTeams}
#Backbone.Mediator.publish('level:set-debug', debug: true) if me.displayName() is 'Nick' Backbone.Mediator.publish('level:set-debug', debug: true) if @isIPadApp() # if me.displayName() is 'Nick'
initVolume: -> initVolume: ->
volume = me.get('volume') volume = me.get('volume')
@ -343,6 +345,8 @@ module.exports = class PlayLevelView extends RootView
if state.selected if state.selected
# TODO: Should also restore selected spell here by saving spellName # TODO: Should also restore selected spell here by saving spellName
Backbone.Mediator.publish 'level:select-sprite', thangID: state.selected, spellName: null Backbone.Mediator.publish 'level:select-sprite', thangID: state.selected, spellName: null
else if @isIPadApp()
Backbone.Mediator.publish 'tome:select-primary-sprite', {}
if state.playing? if state.playing?
Backbone.Mediator.publish 'level:set-playing', playing: state.playing Backbone.Mediator.publish 'level:set-playing', playing: state.playing

View file

@ -80,11 +80,14 @@ module.exports = class ThangListEntryView extends CocoView
score += 9001 * _.size(s.thangs) score += 9001 * _.size(s.thangs)
score score
onClick: (e) -> select: ->
return unless @controlsEnabled
@sortSpells() @sortSpells()
Backbone.Mediator.publish 'level:select-sprite', thangID: @thang.id, spellName: @spells[0]?.name Backbone.Mediator.publish 'level:select-sprite', thangID: @thang.id, spellName: @spells[0]?.name
onClick: (e) ->
return unless @controlsEnabled
@select()
onMouseEnter: (e) -> onMouseEnter: (e) ->
return unless @controlsEnabled and @spells.length return unless @controlsEnabled and @spells.length
@clearTimeouts() @clearTimeouts()

View file

@ -11,7 +11,8 @@ module.exports = class ThangListView extends CocoView
id: 'thang-list-view' id: 'thang-list-view'
template: template template: template
subscriptions: {} subscriptions:
'tome:select-primary-sprite': 'onSelectPrimarySprite'
constructor: (options) -> constructor: (options) ->
super options super options
@ -89,6 +90,9 @@ module.exports = class ThangListView extends CocoView
@sortThangs() @sortThangs()
@addThangListEntries() @addThangListEntries()
onSelectPrimarySprite: (e) ->
@entries[0]?.select()
destroy: -> destroy: ->
entry.destroy() for entry in @entries entry.destroy() for entry in @entries
super() super()