Allow item stacks as input for recipes

* Create the SlotController and refactor it into the various places
  where a crafting grid or crafting output is displayed
* Update the Recipe model to allow each "input" stack to retain the
  count indicated in the data file. This required changing how a few
  other classes interact with it in order for them to get the full
  stack back again for each input (instead of just getting the item
  slug)
* Update the ModVersionParser to allow for full stacks to be given
  as input and refactored common code out for handling input/output/
  tools stacks
This commit is contained in:
Andrew Miner
2015-04-07 19:21:02 -07:00
parent 2ba4dca95b
commit 144aa1bf32
17 changed files with 244 additions and 3426 deletions
@@ -6,9 +6,10 @@ All rights reserved.
###
BaseController = require './base_controller'
ImageLoader = require './image_loader'
SlotController = require './slot_controller'
{Duration} = require '../constants'
{Event} = require '../constants'
ImageLoader = require './image_loader'
########################################################################################################################
@@ -17,58 +18,29 @@ module.exports = class CraftingGridController extends BaseController
constructor: (options={})->
if not options.imageLoader? then throw new Error 'options.imageLoader is required'
if not options.modPack? then throw new Error 'options.modPack is required'
# options.model should be a recipe
options.templateName = 'crafting_grid'
super options
@_imageLoader = options.imageLoader
@_modPack = options.modPack
@_slotCount = 9
@imageLoader = options.imageLoader
@modPack = options.modPack
@_modPack.on Event.change, => @tryRefresh()
@modPack.on Event.change, => @tryRefresh()
# BaseController Methods #######################################################################
onDidRender: ->
@slots = []
for el in @$('td')
$el = $(el)
@slots.push a:$el.find('a'), img:$el.find('img')
@slotControllers = []
for el in @$('.view__slot')
controller = new SlotController el:el, imageLoader:@imageLoader, modPack:@modPack
controller.render()
@slotControllers.push controller
super
refresh: ->
for index in [0...@slots.length]
slot = @slots[index]
slot.a.addClass 'empty'
slot.a.removeAttr 'href'
slot.img.attr 'src', '/images/empty.png'
slot.img.removeAttr 'alt'
display = @_getItemDisplayAt index
if display?
slot.a.removeClass 'empty'
slot.a.attr 'href', display.itemUrl
slot.a.attr 'title', display.itemName
@_imageLoader.load display.iconUrl, slot.img
slot.img.attr 'alt', display.itemName
for i in [0...@slotControllers.length]
controller = @slotControllers[i]
controller.model = @model?.getStackAtSlot(i)
@$el.tooltip show:{delay:Duration.snap, duration:Duration.fast}
super
# Backbone.View Overrides ######################################################################
events: ->
return _.extend super,
'click td a': 'routeLinkClick'
# Private Methods ##############################################################################
_getItemDisplayAt: (slot)->
if slot >= @_slotCount then throw new Error "slot (#{slot}) must be less than #{@_slotCount}"
return null unless @model?
itemSlug = @model.getItemSlugAt slot
return null unless itemSlug?
itemDisplay = @_modPack.findItemDisplay itemSlug
return itemDisplay
@@ -30,8 +30,8 @@ module.exports = class FullRecipeController extends BaseController
onDidRender: ->
@gridController = @addChild CraftingGridController, '.view__crafting_grid',
modPack: @modPack
imageLoader: @imageLoader
modPack: @modPack
@inputController = @addChild InventoryTableController, '.input .view__inventory_table',
editable: false
@@ -72,7 +72,7 @@ module.exports = class FullRecipeController extends BaseController
inputs.clear()
if @model?
for stack in @model.input
@model.eachInputStack (stack)->
inputs.add stack.itemSlug, stack.quantity
@@ -8,6 +8,7 @@ All rights reserved.
BaseController = require './base_controller'
CraftingGridController = require './crafting_grid_controller'
ImageLoader = require './image_loader'
SlotController = require './slot_controller'
{Duration} = require '../constants'
{StringBuilder} = require 'crafting-guide-common'
@@ -31,6 +32,10 @@ module.exports = class MinimalRecipeController extends BaseController
modPack: @modPack
imageLoader: @imageLoader
@outputSlotController = @addChild SlotController, '.output.view__slot',
imageLoader: @imageLoader
modPack: @modPack
@$outputImg = @$('.output img')
@$outputLink = @$('.output a')
@$outputQuantity = @$('.quantity')
@@ -39,25 +44,8 @@ module.exports = class MinimalRecipeController extends BaseController
refresh: ->
@gridController.model = @model
@$outputImg.attr 'src', '/images/empty.png'
@$outputImg.removeAttr 'alt'
@$outputLink.removeAttr 'href'
@$outputQuantity.html ''
if @model?
outputStack = @model.output[0]
if outputStack?
display = @modPack.findItemDisplay outputStack.itemSlug
@$outputLink.attr 'href', display.itemUrl
@$outputLink.attr 'title', display.itemName
@$outputImg.attr 'alt', display.itemName
@$outputQuantity.html outputStack.quantity if outputStack.quantity > 1
@imageLoader.load display.iconUrl, @$outputImg
@outputSlotController.model = @model?.output?[0]
@$el.tooltip show:{delay:Duration.snap, duration:Duration.fast}
@_refreshTools()
super
@@ -0,0 +1,58 @@
###
Crafting Guide - slot_controller.coffee
Copyright (c) 2015 by Redwood Labs
All rights reserved.
###
BaseController = require './base_controller'
########################################################################################################################
module.exports = class SlotController extends BaseController
constructor: (options={})->
if not options.imageLoader? then throw new Error 'options.imageLoader is required'
if not options.modPack? then throw new Error 'options.modPack is required'
# options.model should be a Stack
options.templateName = 'slot'
options.useAnimations = false
super options
@imageLoader = options.imageLoader
@modPack = options.modPack
# BaseController Overrides #####################################################################
onDidRender: ->
@$link = @$('a')
@$image = @$('img')
@$quantity = @$('.quantity')
super
refresh: ->
if @model?
display = @modPack.findItemDisplay @model.itemSlug
@$link.attr 'href', display.itemUrl
@imageLoader.load display.iconUrl, @$image
@show @$image
if @model.quantity > 1
@$quantity.html @model.quantity
else
@$quantity.html ''
else
@$link.removeAttr 'href'
@$quantity.html ''
@$image.removeAttr 'src'
@hide @$image
super
# Backbone.View Overrides ######################################################################
events: ->
return _.extend super,
'click a': 'routeLinkClick'