lednerg

Milkshake v0.141109.05

Nov 9th, 2014
399
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 10.38 KB | None | 0 0
  1. --# Milkshake v0.141109.05
  2.  
  3. --# Requires Adv Computer or Adv Turtle
  4.  
  5. --# This doesn't do anything yet but
  6. --#   put graphics on the screen.
  7.  
  8. --# Hover the mouse over the numbers
  9. --#   use the scroll wheel to change.
  10. --# Click zoom to toggle AutoZoom
  11. --#   or use the scroll wheel on it.
  12. --# You can drag the Top View around if
  13. --#   you want, AutoZoom will recenter.
  14. --# Click QUIT to live your life.
  15.  
  16. --# by lednerg
  17.  
  18.  
  19. --#             VARIABLES
  20.  
  21. --# The frameskip function relies on the game's clock
  22. --# which is not emulated properly in many emulators.
  23. local enableFrameskip = true
  24.  
  25. --# Initial hole dimensions
  26. local inputX = 5
  27. local inputZ = 5
  28. local inputY = 5
  29.  
  30. --# Accounts for ComputerCraft's tall pixel shape (sort of)
  31. local fixAspect = true
  32.  
  33. --# Initial state of AutoZoom and Zoom
  34. local autoZoom = true
  35. local zoom = 100
  36.  
  37. --#
  38. --#  NOTE: Changing the variables below can result in crashes and stuff
  39. --#
  40.  
  41. local termMaxX, termMaxZ = term.getSize()
  42. local termMidX = math.ceil(termMaxX /2) --# middle line of screen to separate top and front views
  43.  
  44. term.clear()
  45.  
  46. --# Windows for viewing hole shape and displaying text
  47. local drawMode =  window.create(term.current(), 1,            1,        termMaxX,    termMaxZ,    false)
  48. local topView =   window.create(drawMode,       1,            1,        termMidX -1, termMaxZ -1, true )
  49. local frontView = window.create(drawMode,       termMidX +1,  2,        termMidX -1, termMaxZ -2, true )
  50. local dimWin =    window.create(drawMode,       1,            termMaxZ, 17,          1,           true )
  51. local zoomWin =   window.create(drawMode,       termMaxX -14, termMaxZ, 15,          1,           true )
  52.  
  53. --# Used for panning around the top view
  54. local offsetX = 0
  55. local offsetZ = 0
  56. local dragFromX = 9999
  57. local dragFromZ = 9999
  58. local oldPanX = 0
  59. local oldPanZ = 0
  60.  
  61. --# Used to force redraws
  62. local zooming = false
  63. local panning = false
  64. local oldInputX = -1
  65. local oldInputZ = -1
  66. local oldInputY = -1
  67. local oldHoleX = 9999
  68. local oldHoleZ = 9999
  69. local oldHoleY = 9999
  70. local oldZoom = 9999
  71. local oldAZ = not autoZoom
  72.  
  73. --# Hole shape dimensions
  74. local holeX = inputX
  75. local holeZ = inputZ
  76. local holeY = inputY
  77.  
  78. --# Figures out the dimensions of the hole on the screen
  79. --# Top view uses X & Z, and Front view uses X & Y.
  80. function recalculateHoles()
  81.   holeX = inputX
  82.   holeZ = inputZ
  83.   holeY = inputY
  84.   if zoom > 100 then zoom = 100 end
  85.   if zoom <= 0 then zoom = 1 end
  86.   local oldZoom = zoom
  87.   if autoZoom then --# AutoZoom routine (this could be better)
  88.     zoom = 100
  89.     if fixAspect then holeX = math.ceil(holeX *1.5) end
  90.     while math.ceil(holeX) > termMidX -3 or math.ceil(holeZ) > termMaxZ -3 or math.ceil(holeY) > termMaxZ -3 do
  91.       holeX = holeX *.99
  92.       holeZ = holeZ *.99
  93.       holeY = holeY *.99
  94.       zoom = zoom *.99
  95.     end
  96.     offsetX, offsetZ = 0, 0
  97.     if zoom ~= oldZoom then zooming = true end
  98.   else
  99.     if fixAspect then holeX = holeX *1.5 end
  100.     holeX = holeX * (zoom /100)
  101.     holeZ = holeZ * (zoom /100)
  102.     holeY = holeY * (zoom /100)
  103.   end
  104.   holeX = math.ceil(holeX)
  105.   holeZ = math.ceil(holeZ)
  106.   holeY = math.ceil(holeY)
  107. end
  108.  
  109. --# For finding the top left corner of a "centered" object
  110. function centeredX(winWidth, objWidth)
  111.   return math.ceil((winWidth) /2) - math.floor(objWidth /2)
  112. end
  113. function centeredZ(winHeight, objHeight)
  114.   return math.ceil((winHeight) /2) - math.floor(objHeight /2) + 1
  115. end
  116.  
  117. --# The windows used to draw the hole shapes
  118. local tRect = window.create(topView,   centeredX(termMidX -1, holeX), centeredZ(termMaxZ -1, holeZ), holeX, holeZ, true)
  119. local fRect = window.create(frontView, centeredX(termMidX -1, holeX), 1,                             holeX, holeY, true)
  120.  
  121. --# Lets start drawing...
  122. drawMode.setVisible(true)
  123. drawMode.setBackgroundColor(colors.black)
  124. drawMode.clear()
  125. drawMode.setCursorPos(centeredX(termMaxX, 4) +1, termMaxZ)
  126. drawMode.setTextColor(colors.red)
  127. drawMode.write("QUIT")
  128.  
  129. --# This is for the frameskip function
  130. local lastDrawAll = 0
  131. local day = os.day()
  132.  
  133. --# For the text at the bottom of the screen
  134. function dimText(output, color, x, y)
  135.   if x ~= nil then dimWin.setCursorPos(x, y) end
  136.   if color ~= nil then dimWin.setTextColor(color) end
  137.   dimWin.write(output)
  138. end
  139.  
  140. --# This beast figures out what to redraw and then does it
  141. function drawAll()
  142.   if (os.time() - lastDrawAll) <= .001 and enableFrameskip then
  143.    --# we drew the previous frame not too long ago, we'll skip this one
  144.     if day ~= os.day() then day = os.day() lastDrawAll = os.time() end
  145.      --# since we use os.time() to compare frame draws, we have to account for new days
  146.     return false
  147.   else
  148.     recalculateHoles()
  149.     if holeX ~= oldHoleX or holeZ ~= oldHoleZ or panning then
  150.       if holeX < oldHoleX or holeZ < oldHoleZ or panning then
  151.        --# we'll only redraw the top view's background when the hole shrinks or moves
  152.         topView.setBackgroundColor(colors.green)
  153.         topView.clear()
  154.       end
  155.       --# drawing the top view hole
  156.       tRect.reposition(centeredX(termMidX -1, holeX) + offsetX, centeredZ(termMaxZ -1, holeZ) + offsetZ, holeX, holeZ)
  157.       tRect.setBackgroundColor(colors.black)
  158.     end
  159.     if holeX ~= oldHoleX or holeY ~= oldHoleY or panning or zooming then
  160.       if holeX < oldHoleX or holeY < oldHoleY or panning or zooming then
  161.        --# we'll only redraw the front view's background when the hole shrinks, moves, or the zoom changes
  162.         frontView.setBackgroundColor(colours.lightGrey)
  163.         frontView.clear()
  164.         --# this part is for the dirt/grass layer which shrinks as you zoom out
  165.         frontView.setBackgroundColor(colors.green)
  166.         if zoom > 33.3 then frontView.setCursorPos(1, 1) frontView.clearLine() end
  167.         frontView.setBackgroundColor(colors.brown)
  168.         if zoom > 75 then frontView.setCursorPos(1, 4) frontView.clearLine() end
  169.         if zoom > 50 then frontView.setCursorPos(1, 3) frontView.clearLine() end
  170.         if zoom > 25 then frontView.setCursorPos(1, 2) frontView.clearLine() end
  171.         if zoom > 16.5 and zoom <= 33.3 then frontView.setCursorPos(1, 1) frontView.clearLine() end
  172.         zooming = false
  173.       end
  174.       --# drawing the front view hole
  175.       fRect.reposition(centeredX(termMidX -1, holeX) + offsetX, 1, holeX, holeY)
  176.       fRect.setBackgroundColor(colors.black)
  177.     end
  178.     if inputX ~= oldInputX or inputZ ~= oldInputZ or inputY ~= oldInputY then
  179.       dimWin.setBackgroundColor(colors.black)
  180.       dimWin.clear()
  181.       dimText("X:", colors.cyan, 1, 1)
  182.       dimText(tostring(inputX), colors.yellow)
  183.       dimText("Z:", colors.cyan, 7, 1)
  184.       dimText(tostring(inputZ), colors.yellow)
  185.       dimText("Y:", colors.cyan, 13, 1)
  186.       dimText(tostring(inputY), colors.yellow)
  187.     end
  188.     if oldZoom ~= zoom or oldAZ ~= autoZoom then
  189.       if autoZoom then
  190.         zoomWin.clear()
  191.         zoomWin.setCursorPos(1, 1)
  192.         zoomWin.setTextColor(colors.white)
  193.         zoomWin.write("AutoZoom: ")
  194.         if zoom ~=100 then zoomWin.setTextColor(colors.orange) end
  195.         if zoom >= 2 then zoomWin.write((math.ceil(zoom *10) /10).. "%") else zoomWin.write((math.ceil(zoom *100) /100).. "%") end
  196.       else
  197.         zoomWin.clear()
  198.         zoomWin.setCursorPos(5, 1)
  199.         zoomWin.setTextColor(colors.red)
  200.         if zoom >= 2 then zoomWin.write("Zoom: "..(math.ceil(zoom *10) /10).. "%") else zoomWin.write("Zoom: "..(math.ceil(zoom *100) /100).. "%") end
  201.       end
  202.     end
  203.     oldHoleX, oldHoleZ, oldHoleY, oldZoom, oldAZ, oldInputX, oldInputZ, oldInputY = holeX, holeZ, holeY, zoom, autoZoom, inputX, inputZ, inputY
  204.     if autoZoom or (offsetX == 0 and offsetZ == 0) then panning = false end
  205.     lastDrawAll = os.time()
  206.   end
  207. end
  208.  
  209. --# This is an auto frameskip to cut down on lag
  210. --# Since it makes use of CC's clock, it doesn't play well with some emulators
  211. function frameskip()
  212.   os.sleep(0.1)
  213.   drawAll()
  214. end
  215.  
  216. drawAll()
  217.  
  218. --# Main loop and input stuff
  219. while true do
  220.   local event = {os.pullEvent()}
  221.  
  222.   if event[1] == "mouse_scroll" then
  223.     panning = false
  224.     upDown, mouseX, mouseY = event[2], event[3], event[4]
  225.     if mouseY == termMaxZ then --# scrolled in text area
  226.       if mouseX < 6 and mouseX >= 1 then
  227.         inputX = inputX - upDown
  228.         if inputX < 1 then inputX = 1 end
  229.       end
  230.       if mouseX < 12 and mouseX >= 7 then
  231.         inputZ = inputZ - upDown
  232.         if inputZ < 1 then inputZ = 1 end
  233.       end
  234.       if mouseX < 18 and mouseX >= 13 then
  235.         inputY = inputY - upDown
  236.         if inputY < 1 then inputY = 1 end
  237.       end
  238.       if mouseX >= (termMaxX -14) then --# scrolled in zoom text
  239.         if upDown == -1 then zoom = zoom *1.05 else zoom = zoom *0.952380 end
  240.         if zoom >= 100 then zoom = 100 end
  241.         if math.abs(zoom -100) < 4 then zoom = 100 end
  242.         if math.abs(zoom -75) < 2 then zoom = 75 end
  243.         if math.abs(zoom -50) < 1 then zoom = 50 end
  244.         if math.abs(zoom -25) < .5 then zoom = 25 end
  245.         if math.abs(zoom -10) < .25 then zoom = 10 end
  246.         autoZoom = false
  247.         zooming = true
  248.       end
  249.     if drawAll() == false then frameskip() end
  250.     end
  251.   end --# mouse_scroll
  252.  
  253.   if event[1] == "mouse_click" then
  254.     mouseX, mouseY = event[3], event[4]
  255.     if mouseY == termMaxZ then --# clicked in text area
  256.       if mouseX >= (termMaxX -14) then --# clicked zoom to toggle autozoom
  257.         autoZoom = not autoZoom
  258.         if autoZoom then panning = true end
  259.         if drawAll() == false then frameskip() end
  260.         if autoZoom then panning = false end
  261.       end
  262.     end
  263.     if mouseX <= termMidX -1 and mouseY <= termMaxZ -1 then --# clicked in topView area
  264.       dragFromX, dragFromZ = mouseX, mouseY
  265.       oldPanX, oldPanZ = 0, 0
  266.     else
  267.       dragFromX, dragFromZ = 9999, 9999
  268.     end
  269.     if (mouseX > centeredX(termMaxX, 4) and mouseX < centeredX(termMaxX, 4) +5) and mouseY == termMaxZ then --# "QUIT"
  270.       break
  271.     end
  272.   end --# mouse_click
  273.  
  274.   if event[1] == "mouse_drag" then
  275.     mouseX, mouseY = event[3], event[4]
  276.     if dragFromX ~= 9999 and dragFromZ ~= 9999 then --# dragged from topView area
  277.       local panX = mouseX - dragFromX
  278.       offsetX = offsetX + panX - oldPanX
  279.       oldPanX = panX
  280.       local panZ = mouseY - dragFromZ
  281.       offsetZ = offsetZ + panZ - oldPanZ
  282.       oldPanZ = panZ
  283.       panning = true
  284.       autoZoom = false
  285.       if drawAll() == false then frameskip() end
  286.     end
  287.   end --# mouse_drag
  288. end
  289.  
  290. dimWin.clear()
  291. term.setCursorPos(1, termMaxZ)
Advertisement
Add Comment
Please, Sign In to add comment