ZiegelMan

norm3d

May 22nd, 2020
227
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 23.42 KB | None | 0 0
  1. local number = require("Number")
  2. local GUI = require("GUI")
  3. local screen = require("Screen")
  4. local color = require("Color")
  5. local system = require("System")
  6. local paths = require("Paths")
  7. local text = require("Text")
  8. local bigLetters = require("BigLetters")
  9. local filesystem = require("Filesystem")
  10.  
  11. --------------------------------------------------------------------------------
  12.  
  13. local args, options = system.parseArguments(...)
  14.  
  15. local proxies = {}
  16.  
  17. local function updateProxy(name)
  18.   proxies[name] = component.list(name)()
  19.   if proxies[name] then
  20.     proxies[name] = component.proxy(proxies[name])
  21.     return true
  22.   end
  23. end
  24.  
  25. local function print(model,num)
  26.   local proxy = proxies.printer3d
  27.   proxy.reset()
  28.  
  29.   if model.label then
  30.     proxy.setLabel(model.label)
  31.   end
  32.  
  33.   if model.tooltip then
  34.     proxy.setTooltip(model.tooltip)
  35.   end
  36.  
  37.   if model.collidable then
  38.     proxy.setCollidable(model.collidable[1], model.collidable[2])
  39.   end
  40.  
  41.   if model.lightLevel then
  42.     proxy.setLightLevel(model.lightLevel)
  43.   end
  44.  
  45.   if model.emitRedstone then
  46.     proxy.setRedstoneEmitter(model.emitRedstone)
  47.   end
  48.  
  49.   if model.buttonMode then
  50.     proxy.setButtonMode(model.buttonMode)
  51.   end
  52.  
  53.   for i = 1, #model.shapes do
  54.     local shape = model.shapes[i]
  55.     proxy.addShape(shape[1], shape[2], shape[3], shape[4], shape[5], shape[6], shape.texture or "empty", shape.state, shape.tint)
  56.   end
  57.  
  58.   local success, reason = proxy.commit(num)
  59.   if not success then
  60.     GUI.alert(localization.failedToPrint .. ": " .. reason)
  61.   end
  62. end
  63.  
  64. -- Just printing without UI
  65. if options.p then
  66.   updateProxy("printer3d")
  67.   print(filesystem.readTable(args[1]))
  68.   return
  69. end
  70. --------------------------------------------------------------------------------
  71.  
  72. local currentScriptDirectory = filesystem.path(system.getCurrentScript())
  73. local localization = system.getLocalization(currentScriptDirectory .. "Localizations/")
  74. local currentLayer = 0
  75. local model
  76. local savePath
  77. local shapeLimit = 100
  78. local viewPixelWidth, viewPixelHeight = 4, 2
  79.  
  80. local colors, hue, hueStep = {}, 0, 360 / shapeLimit
  81. for i = 1, shapeLimit do
  82.   colors[i] = color.HSBToInteger(hue, 1, 1)
  83.   hue = hue + hueStep
  84. end
  85.  
  86. local workspace, window, menu = system.addWindow(GUI.filledWindow(1, 1, 100, screen.getHeight() - 1, 0x1E1E1E))
  87. -- local workspace, window, menu = system.addWindow(GUI.filledWindow(1, 1, 92, 32, 0x1E1E1E))
  88.  
  89. --------------------------------------------------------------------------------
  90.  
  91. local toolPanel = window:addChild(GUI.panel(1, 1, 28, 1, 0x2D2D2D))
  92.  
  93. window.backgroundPanel.localX = toolPanel.width + 1
  94.  
  95. local toolLayout = window:addChild(GUI.layout(1, 1, toolPanel.width, 1, 1, 1))
  96. toolLayout:setAlignment(1, 1, GUI.ALIGNMENT_HORIZONTAL_CENTER, GUI.ALIGNMENT_VERTICAL_TOP)
  97. toolLayout:setMargin(1, 1, 0, 1)
  98.  
  99. local function addSeparator(text)
  100.   toolLayout:addChild(GUI.object(1, 1, toolLayout.width, 1)).draw = function(object)
  101.     screen.drawRectangle(object.x, object.y, object.width, 1, 0x0F0F0F, 0xE1E1E1, " ")
  102.     screen.drawText(object.x + 1, object.y, 0xE1E1E1, text)
  103.   end
  104. end
  105.  
  106. local function newButton(width, height, ...)
  107.   local button = GUI.button(1, 1, width, height, 0x3C3C3C, 0xA5A5A5, 0xE1E1E1, 0x3C3C3C, ...)
  108.   button.colors.disabled.background = 0x3C3C3C
  109.   button.colors.disabled.text = 0x5A5A5A
  110.  
  111.   return button
  112. end
  113. local function newAnButton(width, height, ...)
  114.   local button = GUI.button(1, 1, width, height, 0xE60A00, 0x303030, 0xE1E1E1, 0x3C3C3C, ...)
  115.   button.colors.disabled.background = 0x3C3C3C
  116.   button.colors.disabled.text = 0x5A5A5A
  117.  
  118.   return button
  119. end
  120.  
  121. local function addObjectsTo(layout, objects)
  122.   layout:setGridSize(#objects * 2 - 1, 1)
  123.  
  124.   for i = 1, #objects do
  125.     layout:setColumnWidth(i * 2 - 1, GUI.SIZE_POLICY_RELATIVE, 1 / #objects)
  126.  
  127.     if i < #objects then
  128.       layout:setColumnWidth(i * 2, GUI.SIZE_POLICY_ABSOLUTE, 1)
  129.     end
  130.  
  131.     layout:setPosition(i * 2 - 1, 1, layout:addChild(objects[i]))
  132.     layout:setFitting(i * 2 - 1, 1, true, false)
  133.   end
  134. end
  135.  
  136. local function addObjectsWithLayout(objects)
  137.   addObjectsTo(toolLayout:addChild(GUI.layout(1, 1, toolLayout.width - 2, 1, 1, 1)), objects)
  138. end
  139.  
  140. local function addButtons(...)
  141.   local texts, buttons = {...}, {}
  142.   for i = 1, #texts do
  143.     buttons[i] = newButton(toolLayout.width - 2, 1, texts[i])
  144.   end
  145.  
  146.   addObjectsWithLayout(buttons)
  147.  
  148.   return table.unpack(buttons)
  149. end
  150.  
  151. local function addSwitch(...)
  152.   return toolLayout:addChild(GUI.switchAndLabel(1, 1, toolLayout.width - 2, 6, 0x66DB80, 0x1E1E1E, 0xE1E1E1, 0x787878, ...)).switch
  153. end
  154.  
  155. local function addInput(...)
  156.   return toolLayout:addChild(GUI.input(1, 1, toolLayout.width - 2, 1, 0x1E1E1E, 0xA5A5A5, 0x5A5A5A, 0x1E1E1E, 0xE1E1E1, ...))
  157. end
  158.  
  159. local function addSlider(...)
  160.   return toolLayout:addChild(GUI.slider(1, 1, toolLayout.width - 2, 0x66DB80, 0x1E1E1E, 0xE1E1E1, 0x787878, ...))
  161. end
  162.  
  163. local function addComboBox(...)
  164.   return toolLayout:addChild(GUI.comboBox(1, 1, toolLayout.width - 2, 1, 0x1E1E1E, 0xA5A5A5, 0x3C3C3C, 0x696969))
  165. end
  166.  
  167. local bigContainer = toolLayout:addChild(GUI.container(1, 1, toolLayout.width, 5))
  168.  
  169. bigContainer:addChild(GUI.object(1, 1, bigContainer.width, bigContainer.height)).draw = function(object)
  170.   local text = tostring(math.floor(currentLayer))
  171.   local width = bigLetters.getTextSize(text)
  172.   bigLetters.drawText(math.floor(object.x + object.width / 2 - width / 2), object.y, 0xE1E1E1, text)
  173. end
  174.  
  175. window.actionButtons:remove()
  176. bigContainer:addChild(window.actionButtons)
  177. window.actionButtons.localY = 1
  178.  
  179. local fileItem = menu:addContextMenuItem(localization.file)
  180.  
  181. local newItem = fileItem:addItem(localization.new, false, "^N")
  182. local openItem = fileItem:addItem(localization.open, false, "^O")
  183. fileItem:addSeparator()
  184. local saveItem = fileItem:addItem(localization.save, true, "^S")
  185. local saveAsItem = fileItem:addItem(localization.saveAs, false, "^⇧S")
  186.  
  187. menu:addItem(localization.help).onTouch = function()
  188.   local container = GUI.addBackgroundContainer(workspace, true, true, localization.help)
  189.  
  190.   local textBox = container.layout:addChild(GUI.textBox(1, 1, 68, 1, nil, 0xB4B4B4, localization.helpInfo, 1, 0, 0, true, true))
  191.   textBox.eventHandler = container.panel.eventHandler
  192.  
  193.   textBox:update()
  194.   workspace:draw()
  195. end
  196.  
  197. local function updateSavePath(path)
  198.   savePath = path
  199.   saveItem.disabled = not savePath
  200. end
  201.  
  202. addSeparator(localization.elementSettings)
  203.  
  204. local cancelButton = window:addChild(newAnButton(toolLayout.width, 1, "Отмена "))
  205. local printButton = window:addChild(newButton(toolLayout.width, 3, localization.print))
  206.  
  207.  
  208. local modelList = toolLayout:addChild(GUI.list(1, 1, toolLayout.width, 3, math.floor(toolLayout.width / 2), 0, 0x1E1E1E, 0x5A5A5A, 0x1E1E1E, 0x5A5A5A, 0x2D2D2D, 0xA5A5A5))
  209. modelList:setAlignment(GUI.ALIGNMENT_HORIZONTAL_CENTER, GUI.ALIGNMENT_VERTICAL_TOP)
  210. modelList:setDirection(GUI.DIRECTION_HORIZONTAL)
  211. local disabledListItem = modelList:addItem(localization.disabled)
  212. local enabledListItem = modelList:addItem(localization.enabled)
  213.  
  214. local elementComboBox = addComboBox()
  215.  
  216. local textureInput = addInput("", localization.texture, true)
  217. local tintColorSelector = toolLayout:addChild(GUI.colorSelector(1, 1, toolLayout.width - 2, 1, 0x330040, localization.tintColor))
  218. local tintSwitch = addSwitch(localization.tintEnabled .. ":", false)
  219.  
  220. local function checkShapeState(shape)
  221.   return modelList.selectedItem == 1 and not shape.state or modelList.selectedItem == 2 and shape.state
  222. end
  223.  
  224. local addShapeButton, removeShapeButton = addButtons(localization.add, localization.remove)
  225.  
  226. addSeparator(localization.blockSettings)
  227.  
  228. local labelInput = addInput("", localization.label, true)
  229. local tooltipInput = addInput("", localization.tooltip, true)
  230. local buttonModeSwitch = addSwitch(localization.buttonMode .. ":", false)
  231. local collisionSwitch = addSwitch(localization.collidable .. ":", true)
  232. local redstoneSwitch = addSwitch(localization.emitRedstone .. ":", true)
  233.  
  234. local lightLevelSlider = addSlider(0, 15, 0, false, localization.lightLevel .. ": ", "")
  235. lightLevelSlider.height = 2
  236. lightLevelSlider.roundValues = true
  237.  
  238. local axisComboBox = addComboBox()
  239. axisComboBox:addItem(localization.xAxis)
  240. axisComboBox:addItem(localization.yAxis)
  241. axisComboBox:addItem(localization.zAxis)
  242.  
  243. local function fixShape(shape)
  244.   for i = 1, 3 do
  245.     if shape[i] > shape[i + 3] then
  246.       shape[i], shape[i + 3] = shape[i + 3], shape[i]
  247.     end
  248.   end
  249. end
  250.  
  251. local rotateButton, flipButton = addButtons(localization.rotate, localization.flip)
  252.  
  253. addSeparator("кол-во")
  254. local numofmodels = addSlider(1,32,1,false,"","")
  255. numofmodels.roundValues = true
  256.  
  257. addSeparator(localization.projectorSettings)
  258. local projectorSwitch = addSwitch(localization.projectorEnabled .. ": ", true)
  259.  
  260. local projectorScaleSlider = addSlider(0.33, 3, proxies.hologram and proxies.hologram.getScale() or 1, false, localization.scale .. ": ", "")
  261. projectorScaleSlider.onValueChanged = function()
  262.   if proxies.hologram then
  263.     proxies.hologram.setScale(projectorScaleSlider.value)
  264.   end
  265. end
  266.  
  267. local projectorOffsetSlider = addSlider(0, 1, proxies.hologram and select(2, proxies.hologram.getTranslation()) or 0, false, localization.offset .. ": ", "")
  268. projectorOffsetSlider.height = 2
  269. projectorOffsetSlider.onValueChanged = function()
  270.   if proxies.hologram then
  271.     proxies.hologram.setTranslation(0, projectorOffsetSlider.value, 0)
  272.   end
  273. end
  274.  
  275. local hologramWidgetsLayout = toolLayout:addChild(GUI.layout(1, 1, toolLayout.width - 2, 1, 1, 1))
  276.  
  277. local function updateHologramWidgets()
  278.   local objects = {}
  279.  
  280.   for i = 1, (proxies.hologram and proxies.hologram.maxDepth() == 1 and 1 or 3) or 3 do
  281.     objects[i] = GUI.colorSelector(1, 1, 1, 1, proxies.hologram and proxies.hologram.getPaletteColor(i) or 0x0, localization.color .. i)
  282.     objects[i].onColorSelected = function()
  283.       if proxies.hologram then
  284.         proxies.hologram.setPaletteColor(i, objects[i].color)
  285.         workspace:draw()
  286.       end
  287.     end
  288.   end
  289.  
  290.   hologramWidgetsLayout:removeChildren()
  291.   addObjectsTo(hologramWidgetsLayout, objects)
  292. end
  293.  
  294. local function updateProxies()
  295.   updateProxy("hologram")
  296.   updateHologramWidgets()
  297.   printButton.disabled = not updateProxy("printer3d")
  298. end
  299.  
  300. updateProxies()
  301.  
  302. local function getSelectedShapeIndex()
  303.   local item = elementComboBox:getItem(elementComboBox.selectedItem)
  304.   return item and item.shapeIndex
  305. end
  306.  
  307. local function updateHologram()
  308.   if proxies.hologram and projectorSwitch.state then
  309.     local initialX = 17
  310.     local initialY = 2
  311.     local initialZ = 33
  312.     local projectorPaletteIndex = proxies.hologram.maxDepth() > 1 and 3 or 1
  313.  
  314.     proxies.hologram.clear()
  315.  
  316.     local shapeIndex = getSelectedShapeIndex()
  317.     for i = 1, #model.shapes do
  318.       local shape = model.shapes[i]
  319.       if checkShapeState(shape) then
  320.         for x = initialX + shape[1], initialX + shape[4] - 1 do
  321.           for z = initialZ - shape[6] + 1, initialZ - shape[3] do
  322.             proxies.hologram.fill(x, z, initialY + shape[2], initialY + shape[5] - 1, projectorPaletteIndex == 3 and (i == shapeIndex and 1 or 2) or 1)
  323.           end
  324.         end
  325.       end
  326.     end
  327.  
  328.     proxies.hologram.fill(initialX - 1, initialZ - currentLayer, initialY - 1, initialY + 16, projectorPaletteIndex)
  329.     proxies.hologram.fill(initialX + 16, initialZ - currentLayer, initialY - 1, initialY + 16, projectorPaletteIndex)
  330.  
  331.     for x = initialX - 1, initialX + 16 do
  332.       proxies.hologram.set(x, initialY - 1, initialZ - currentLayer, projectorPaletteIndex)
  333.       proxies.hologram.set(x, initialY + 16, initialZ - currentLayer, projectorPaletteIndex)
  334.     end
  335.   end
  336. end
  337.  
  338. local function updateComboBoxFromModel()
  339.   elementComboBox:clear()
  340.  
  341.   for i = 1, #model.shapes do
  342.     if checkShapeState(model.shapes[i]) then
  343.       local item = elementComboBox:addItem(tostring(i))
  344.       item.shapeIndex = i
  345.       item.color = colors[i]
  346.     end
  347.   end
  348. end
  349.  
  350. local function updateAddRemoveButtonsState()
  351.   addShapeButton.disabled = #model.shapes >= shapeLimit
  352.   removeShapeButton.disabled = #model.shapes < 1 or elementComboBox:count() < 1
  353. end
  354.  
  355. local function updateWidgetsFromModel()
  356.   labelInput.text = model.label or ""
  357.   tooltipInput.text = model.tooltip or ""
  358.   buttonModeSwitch:setState(model.buttonMode)
  359.   collisionSwitch:setState(model.collidable)
  360.   redstoneSwitch.state = model.emitRedstone or false
  361.   lightLevelSlider.value = model.lightLevel or 0
  362.  
  363.   local shapeIndex = getSelectedShapeIndex()
  364.   if shapeIndex then
  365.     textureInput.text = model.shapes[shapeIndex].texture or ""
  366.     tintSwitch:setState(model.shapes[shapeIndex].tint and true or false)
  367.     tintColorSelector.color = model.shapes[shapeIndex].tint or tintColorSelector.color
  368.   end
  369. end
  370.  
  371. local function updateModelFromWidgets()
  372.   model.label = #labelInput.text > 0 and labelInput.text or nil
  373.   model.tooltip = #tooltipInput.text > 0 and tooltipInput.text or nil
  374.   model.buttonMode = buttonModeSwitch.state
  375.   model.collidable = collisionSwitch.state and {true, true} or nil
  376.   model.emitRedstone = redstoneSwitch.state
  377.   model.lightLevel = lightLevelSlider.value > 0 and lightLevelSlider.value or nil
  378.  
  379.   local shapeIndex = getSelectedShapeIndex()
  380.   if shapeIndex then
  381.     model.shapes[shapeIndex].texture = #textureInput.text > 0 and textureInput.text or nil
  382.     model.shapes[shapeIndex].tint = tintSwitch.state and tintColorSelector.color or nil
  383.   end
  384. end
  385.  
  386. local function load(path)
  387.   model = filesystem.readTable(path)
  388.   updateSavePath(path)
  389.  
  390.   updateComboBoxFromModel()
  391.   updateWidgetsFromModel()
  392.   updateAddRemoveButtonsState()
  393. end
  394.  
  395. local function save(path)
  396.   filesystem.writeTable(path, model, true)
  397.   updateSavePath(path)
  398. end
  399.  
  400. saveItem.onTouch = function()
  401.   save(savePath)
  402. end
  403.  
  404. saveAsItem.onTouch = function()
  405.   local filesystemDialog = GUI.addFilesystemDialog(workspace, true, 50, math.floor(window.height * 0.8), "Save", "Cancel", "File name", "/")
  406.   filesystemDialog:setMode(GUI.IO_MODE_SAVE, GUI.IO_MODE_FILE)
  407.   filesystemDialog:addExtensionFilter(".3dm")
  408.   filesystemDialog:expandPath(paths.user.desktop)
  409.   filesystemDialog.filesystemTree.selectedItem = paths.user.desktop
  410.   filesystemDialog.onSubmit = function(path)
  411.     save(path)
  412.   end
  413.  
  414.   filesystemDialog:show()
  415. end
  416.  
  417. openItem.onTouch = function()
  418.   local filesystemDialog = GUI.addFilesystemDialog(workspace, true, 50, math.floor(window.height * 0.8), "Open", "Cancel", "File name", "/")
  419.   filesystemDialog:setMode(GUI.IO_MODE_OPEN, GUI.IO_MODE_FILE)
  420.   filesystemDialog:addExtensionFilter(".3dm")
  421.   filesystemDialog:expandPath(paths.user.desktop)
  422.   filesystemDialog.onSubmit = function(path)
  423.     load(path)
  424.  
  425.     workspace:draw()
  426.     updateHologram()
  427.   end
  428.   filesystemDialog:show()
  429. end
  430.  
  431. local viewLayout = window:addChild(GUI.layout(window.backgroundPanel.localX, 1, 1, 1, 1, 1))
  432. viewLayout:setSpacing(1, 1, 2)
  433.  
  434. local view = viewLayout:addChild(GUI.object(1, 1, 16 * viewPixelWidth, 16 * viewPixelHeight))
  435.  
  436. local function getShapeDrawingData(shape)
  437.   local width, height =
  438.     (shape[4] - shape[1]) * viewPixelWidth,
  439.     (shape[5] - shape[2]) * viewPixelHeight
  440.  
  441.   return
  442.     width > 0 and height > 0 and currentLayer >= shape[3] and currentLayer <= shape[6] - 1 and checkShapeState(shape),
  443.     view.x + shape[1] * viewPixelWidth,
  444.     view.y + view.height - shape[2] * viewPixelHeight - height,
  445.     width,
  446.     height
  447. end
  448.  
  449. view.draw = function()
  450.   local x, y, step = view.x, view.y, true
  451.   for j = 1, 16 do
  452.     for i = 1, 16 do
  453.       screen.drawRectangle(x, y, viewPixelWidth, viewPixelHeight, 0xF0F0F0, 0xE1E1E1, step and " " or "█")
  454.       x, step = x + viewPixelWidth, not step
  455.     end
  456.  
  457.     x, y, step = view.x, y + viewPixelHeight, not step
  458.   end
  459.  
  460.   GUI.drawShadow(view.x, view.y, view.width, view.height, nil, true)
  461.  
  462.   local selectedShape, shape = getSelectedShapeIndex()
  463.   if selectedShape then
  464.     for i = 1, #model.shapes do
  465.       shape = model.shapes[i]
  466.  
  467.       local focused, x, y, width, height = getShapeDrawingData(shape)
  468.       if focused then
  469.         screen.drawRectangle(x, y, width, height, i == selectedShape and colors[i] or color.blend(colors[i], 0xFFFFFF, 0.4), 0x0, " ")
  470.  
  471.         if currentLayer == shape[3] then
  472.           screen.drawRectangle(x, y, viewPixelWidth, viewPixelHeight, 0x0, 0x0, " ", i == selectedShape and 0.2 or 0.6)
  473.         end
  474.  
  475.         if currentLayer == shape[6] - 1 then
  476.           screen.drawRectangle(x + width - viewPixelWidth, y + height - viewPixelHeight, viewPixelWidth, viewPixelHeight, 0x0, 0x0, " ", i == shapeIndex and 0.4 or 0.8)
  477.         end
  478.       end
  479.     end
  480.   end
  481. end
  482.  
  483. toolLayout.eventHandler = function(workspace, toolLayout, e1, e2, e3, e4, e5)
  484.   if e1 == "scroll" then
  485.     local h, v = toolLayout:getMargin(1, 1)
  486.  
  487.     if e5 > 0 then
  488.       if v < 1 then
  489.         v = v + 1
  490.         toolLayout:setMargin(1, 1, h, v)
  491.         workspace:draw()
  492.       end
  493.     else
  494.       local child = toolLayout.children[#toolLayout.children]
  495.       if child.localY + child.height - 1 >= toolLayout.localY + toolLayout.height - 1 then
  496.         v = v - 1
  497.         toolLayout:setMargin(1, 1, h, v)
  498.         workspace:draw()
  499.       end
  500.     end
  501.   end
  502. end
  503.  
  504. local shapeX, shapeY, shapeZ
  505. view.eventHandler = function(workspace, view, e1, e2, e3, e4, e5)
  506.   if e1 == "touch" or e1 == "drag" then
  507.     if e5 == 0 then
  508.       local selectedShape = getSelectedShapeIndex()
  509.       if selectedShape then
  510.         local shape = model.shapes[selectedShape]
  511.         local x = math.floor((e3 - view.x) / view.width * 16)
  512.         local y = 15 - math.floor((e4 - view.y) / view.height * 16)
  513.  
  514.         if e1 == "touch" then
  515.           shapeX, shapeY, shapeZ = x, y, currentLayer
  516.           shape[1], shape[2], shape[3] = x, y, currentLayer
  517.           shape[4], shape[5], shape[6] = x + 1, y + 1, currentLayer + 1
  518.         elseif shapeX then
  519.           shape[1], shape[2], shape[3] = shapeX, shapeY, shapeZ
  520.           shape[4], shape[5], shape[6] = x, y, currentLayer
  521.           fixShape(shape)
  522.           shape[4], shape[5], shape[6] = shape[4] + 1, shape[5] + 1, shape[6] + 1
  523.         end
  524.  
  525.         workspace:draw()
  526.       end
  527.     else
  528.       -- Selecting shape
  529.       local shape
  530.       for i = #model.shapes, 1, -1 do
  531.         shape = model.shapes[i]
  532.  
  533.         local focused, x, y, width, height = getShapeDrawingData(shape)
  534.         if focused and e3 >= x and e3 <= x + width - 1 and e4 >= y and e4 <= y + height - 1 then
  535.           for j = 1, elementComboBox:count() do
  536.             if elementComboBox:getItem(j).shapeIndex == i then
  537.               elementComboBox.selectedItem = j
  538.               workspace:draw()
  539.  
  540.               break
  541.             end
  542.           end
  543.  
  544.           break
  545.         end
  546.       end
  547.     end
  548.   elseif e1 == "drop" then
  549.     shapeX, shapeY, shapeZ = nil, nil, nil
  550.     updateHologram()
  551.   elseif e1 == "scroll" then
  552.     local function fix()
  553.       local shapeIndex = getSelectedShapeIndex()
  554.       if shapeX and shapeIndex then
  555.         local shape = model.shapes[shapeIndex]
  556.         shape[3] = shapeZ
  557.         shape[6] = currentLayer
  558.         fixShape(shape)
  559.         shape[6] = shape[6] + 1
  560.       end
  561.     end
  562.  
  563.     if e5 > 0 then
  564.       if currentLayer < 15 then
  565.         currentLayer = currentLayer + 1
  566.         fix()
  567.  
  568.         workspace:draw()
  569.         updateHologram()
  570.       end
  571.     else
  572.       if currentLayer > 0 then
  573.         currentLayer = currentLayer - 1
  574.         fix()
  575.  
  576.         workspace:draw()
  577.         updateHologram()
  578.       end
  579.     end
  580.   elseif e1 == "component_added" or e1 == "component_removed" then
  581.     updateProxies()
  582.     workspace:draw()
  583.  
  584.     updateHologram()
  585.   end
  586. end
  587.  
  588. rotateButton.onTouch = function()
  589.   for i = 1, #model.shapes do
  590.     local shape = model.shapes[i]
  591.        
  592.     if axisComboBox.selectedItem == 1 then
  593.       shape[1], shape[2], shape[3], shape[4], shape[5], shape[6] = shape[1], -shape[3] + 16, shape[2], shape[4], -shape[6] + 16, shape[5]
  594.     elseif axisComboBox.selectedItem == 2 then
  595.       shape[1], shape[2], shape[3], shape[4], shape[5], shape[6] = -shape[3] + 16, shape[2], shape[1], -shape[6] + 16, shape[5], shape[4]
  596.     else
  597.       shape[1], shape[2], shape[3], shape[4], shape[5], shape[6] = shape[2], -shape[1] + 16, shape[3], shape[5], -shape[4] + 16, shape[6]
  598.     end
  599.  
  600.     fixShape(shape)
  601.   end
  602.  
  603.   workspace:draw()
  604.   updateHologram()
  605. end
  606.  
  607. flipButton.onTouch = function()
  608.   local function fix(shape, index)
  609.     shape[index] = 16 - shape[index]
  610.     shape[index + 3] = 16 - shape[index + 3]
  611.   end
  612.  
  613.   for i = 1, #model.shapes do
  614.     local shape = model.shapes[i]
  615.     if axisComboBox.selectedItem == 1 then
  616.       fix(shape, 1)
  617.     elseif axisComboBox.selectedItem == 2 then
  618.       fix(shape, 2)
  619.     else
  620.       fix(shape, 3)
  621.     end
  622.  
  623.     fixShape(shape)
  624.   end
  625.  
  626.   workspace:draw()
  627.   updateHologram()
  628. end
  629.  
  630. disabledListItem.onTouch = function()
  631.   updateComboBoxFromModel()
  632.   updateWidgetsFromModel()
  633.   updateAddRemoveButtonsState()
  634.  
  635.   workspace:draw()
  636.   updateHologram()
  637. end
  638.  
  639. enabledListItem.onTouch = disabledListItem.onTouch
  640.  
  641. local function addShape()
  642.   table.insert(model.shapes, {6, 6, 0, 10, 10, 1, state = modelList.selectedItem == 2 or nil, texture = #textureInput.text > 0 and textureInput.text or nil})
  643.  
  644.   updateComboBoxFromModel()
  645.   elementComboBox.selectedItem = elementComboBox:count()
  646.   updateWidgetsFromModel()
  647.   updateAddRemoveButtonsState()
  648. end
  649.  
  650. local function new()
  651.   model = {shapes = {}}
  652.   modelList.selectedItem = 1
  653.   addShape()
  654.   updateSavePath()
  655. end
  656.  
  657. newItem.onTouch = function()
  658.   new()
  659.  
  660.   workspace:draw()
  661.   updateHologram()
  662. end
  663.  
  664. addShapeButton.onTouch = function()
  665.   addShape()
  666.  
  667.   workspace:draw()
  668.   updateHologram()
  669. end
  670.  
  671. removeShapeButton.onTouch = function()
  672.   table.remove(model.shapes, getSelectedShapeIndex())
  673.  
  674.   updateComboBoxFromModel()
  675.   updateWidgetsFromModel()
  676.   updateAddRemoveButtonsState()
  677.  
  678.   workspace:draw()
  679.   updateHologram()
  680. end
  681.  
  682. printButton.onTouch = function()
  683.   print(model,numofmodels.value)
  684. end
  685.  
  686. cancelButton.onTouch = function()
  687.   proxies.printer3d.reset()
  688. end
  689.  
  690. elementComboBox.onItemSelected = function()
  691.   updateWidgetsFromModel()
  692.  
  693.   workspace:draw()
  694.   updateHologram()
  695. end
  696.  
  697. labelInput.onInputFinished = updateModelFromWidgets
  698. tooltipInput.onInputFinished = updateModelFromWidgets
  699. buttonModeSwitch.onStateChanged = updateModelFromWidgets
  700. collisionSwitch.onStateChanged = updateModelFromWidgets
  701. redstoneSwitch.onStateChanged = updateModelFromWidgets
  702. lightLevelSlider.onValueChanged = updateModelFromWidgets
  703. textureInput.onInputFinished = updateModelFromWidgets
  704. tintSwitch.onStateChanged = updateModelFromWidgets
  705. tintColorSelector.onColorSelected = updateModelFromWidgets
  706.  
  707. -- Overriding window removing for clearing hologram
  708. local overrideWindowRemove = window.remove
  709. window.remove = function(...)
  710.   overrideWindowRemove(...)
  711.  
  712.   if proxies.hologram then
  713.     proxies.hologram.clear()
  714.   end
  715. end
  716.  
  717. window.onResize = function(width, height)
  718.   window.backgroundPanel.width = width - toolPanel.width
  719.   window.backgroundPanel.height = height
  720.  
  721.   viewLayout.width = window.backgroundPanel.width
  722.   viewLayout.height = window.backgroundPanel.height
  723.  
  724.   toolPanel.height = height - 3
  725.  
  726.   toolLayout.height = toolPanel.height
  727.   printButton.localY = height - 2
  728.   cancelButton.localY = printButton.localY - 1
  729. end
  730.  
  731. --------------------------------------------------------------------------------
  732.  
  733. load(args[1] or (currentScriptDirectory .. "Sample.3dm"))
  734.  
  735. window:resize(window.width, window.height)
  736. workspace:draw()
Add Comment
Please, Sign In to add comment