Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local utils = require("utils")
- return function(basalt, parentFrame, cookbook)
- local frame = parentFrame:addFrame()
- local editing = nil
- local pattern = {}
- local itemLabel = label(frame, "Enter item ID", 3, 2)
- local itemInput = frame
- :addInput()
- :setPosition(3, 3)
- :setSize(34, 1)
- :setDefaultText("minecraft: ...")
- local itemList = frame
- :addList()
- :setPosition(3, 5)
- :setSize(34, 10)
- :setBackground(colors.black)
- :setSelectionColor(colors.white)
- :addItem("<none>", colors.black, colors.white)
- local usedColors = {}
- function addListItem(itemID)
- local count = itemList:getItemCount()
- if (count == 10) then
- toast(frame, "Maximum items exceeded.", colors.red)
- return 0
- end
- local i = (count - 1) % colorCount + 1
- while usedColors[i] do
- i = i + 1
- end
- usedColors[i] = true
- local currentColor = indexToColor[i]
- itemList:addItem(itemID, colors.black, currentColor)
- itemList:selectItem(count + 1)
- return currentColor
- end
- -- TODO: selection not updated when switching focus to grid immediately
- itemInput:onLoseFocus(
- function()
- local itemName = itemInput:getValue()
- if(#itemName > 0) then
- addListItem(itemName)
- itemInput:setValue("")
- end
- end)
- local gridIndex = 0
- local gridSelector = {}
- for pos = 1, 9 do
- xOffset = (pos-1)%3*4
- yOffset = math.floor((pos-1)/3)*3
- gridSelector[pos] = frame:addPane()
- :setSize(3, 2)
- :setPosition(39 + xOffset, 3 + yOffset)
- :onClick(
- function()
- local itemIndex = itemList:getItemIndex()
- if (itemIndex == nil) then return end
- if (itemIndex == 1) then
- gridSelector[pos]:setBackground(colors.black)
- pattern[pos] = nil
- return
- end
- local item = itemList:getItem(itemIndex)
- pattern[pos] = item.text
- gridSelector[pos]:setBackground(item.fgCol)
- end
- )
- end
- local recipeLabel = label(frame, "Enter result ID", 3, 16)
- label(frame, "Qty", 34, 16)
- local recipeNameInput = frame
- :addInput()
- :setPosition(3, 17)
- :setSize(30, 1)
- :setDefaultText("minecraft: ...")
- local recipeCountInput = frame
- :addInput()
- :setPosition(34, 17)
- :setSize(3, 1)
- :setDefaultText("1")
- :setInputType("number")
- :setInputLimit(2)
- function clearGrid()
- for pos=1, 9 do gridSelector[pos]:setBackground(colors.black) end
- pattern = {}
- usedColors = {}
- end
- function clearAll()
- clearGrid()
- itemList:clear():addItem("<none>", colors.black, colors.white)
- recipeNameInput:setValue("")
- recipeCountInput:setValue("")
- editing = false
- removeStatus()
- end
- local clearGridButton = label(frame, " Clear ", 3, 17)
- :setPosition(40, 12)
- :setBackground(colors.gray)
- :setForeground(colors.red)
- :onClick(clearGrid)
- local clearAllButton = label(frame, " Clear all ", 3, 17)
- :setPosition(39, 14)
- :setBackground(colors.gray)
- :setForeground(colors.red)
- :onClick(clearAll)
- local commitButton = label(frame, " Save recipe ", 3, 17)
- :setPosition(38, 17)
- :setBackground(colors.gray)
- :setForeground(colors.green)
- :onClick(function()
- local recipeName = recipeNameInput:getValue()
- if (#recipeName == 0) then
- toast(frame, "Enter ID of resulting item.", colors.red)
- else
- local count = tonumber("0"..recipeCountInput:getValue())
- cookbook[recipeName] = {['pattern'] = pattern, ['count'] = count}
- if (editing) then
- cookbook[editing] = nil
- editing = recipeName
- end
- saveCookbook(cookbook)
- toast(frame, "Recipe saved successfully.", colors.blue)
- end
- end)
- function onKeyHandler(self, event, key)
- if (key == keys.delete and itemList:isFocused()) then
- local removeIndex = itemList:getItemIndex()
- if (removeIndex == nil or removeIndex == 1) then return end
- local removeItem = itemList:getItem(removeIndex)
- usedColors[colorToIndex[removeItem.fgCol]] = nil
- itemList:removeItem(removeIndex)
- for pos = 1, 9 do
- if (recipe[pos] == removeItem.text) then
- gridSelector[pos]:setBackground(colors.black)
- recipe[pos] = nil
- end
- end
- -- Select nearest item in list
- if (removeIndex - 1 == itemList:getItemCount()) then removeIndex = removeIndex - 1 end
- itemList:selectItem(removeIndex)
- end
- end
- frame:onKey(onKeyHandler)
- function populate(newItem, newRecipe)
- editing = newItem
- status(frame, "Editing", colors.blue)
- clearAll()
- recipeNameInput:setValue(newItem)
- recipeCountInput:setValue(newRecipe.count)
- pattern = deepCopy(newRecipe.pattern)
- itemColors = {}
- for pos, item in pairs(pattern) do
- if itemColors[item] == nil then
- itemColors[item] = addListItem(item)
- end
- gridSelector[pos]:setBackground(itemColors[item])
- end
- end
- return frame, populate
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement