Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --# Milkshake v0.141109.05
- --# Requires Adv Computer or Adv Turtle
- --# This doesn't do anything yet but
- --# put graphics on the screen.
- --# Hover the mouse over the numbers
- --# use the scroll wheel to change.
- --# Click zoom to toggle AutoZoom
- --# or use the scroll wheel on it.
- --# You can drag the Top View around if
- --# you want, AutoZoom will recenter.
- --# Click QUIT to live your life.
- --# by lednerg
- --# VARIABLES
- --# The frameskip function relies on the game's clock
- --# which is not emulated properly in many emulators.
- local enableFrameskip = true
- --# Initial hole dimensions
- local inputX = 5
- local inputZ = 5
- local inputY = 5
- --# Accounts for ComputerCraft's tall pixel shape (sort of)
- local fixAspect = true
- --# Initial state of AutoZoom and Zoom
- local autoZoom = true
- local zoom = 100
- --#
- --# NOTE: Changing the variables below can result in crashes and stuff
- --#
- local termMaxX, termMaxZ = term.getSize()
- local termMidX = math.ceil(termMaxX /2) --# middle line of screen to separate top and front views
- term.clear()
- --# Windows for viewing hole shape and displaying text
- local drawMode = window.create(term.current(), 1, 1, termMaxX, termMaxZ, false)
- local topView = window.create(drawMode, 1, 1, termMidX -1, termMaxZ -1, true )
- local frontView = window.create(drawMode, termMidX +1, 2, termMidX -1, termMaxZ -2, true )
- local dimWin = window.create(drawMode, 1, termMaxZ, 17, 1, true )
- local zoomWin = window.create(drawMode, termMaxX -14, termMaxZ, 15, 1, true )
- --# Used for panning around the top view
- local offsetX = 0
- local offsetZ = 0
- local dragFromX = 9999
- local dragFromZ = 9999
- local oldPanX = 0
- local oldPanZ = 0
- --# Used to force redraws
- local zooming = false
- local panning = false
- local oldInputX = -1
- local oldInputZ = -1
- local oldInputY = -1
- local oldHoleX = 9999
- local oldHoleZ = 9999
- local oldHoleY = 9999
- local oldZoom = 9999
- local oldAZ = not autoZoom
- --# Hole shape dimensions
- local holeX = inputX
- local holeZ = inputZ
- local holeY = inputY
- --# Figures out the dimensions of the hole on the screen
- --# Top view uses X & Z, and Front view uses X & Y.
- function recalculateHoles()
- holeX = inputX
- holeZ = inputZ
- holeY = inputY
- if zoom > 100 then zoom = 100 end
- if zoom <= 0 then zoom = 1 end
- local oldZoom = zoom
- if autoZoom then --# AutoZoom routine (this could be better)
- zoom = 100
- if fixAspect then holeX = math.ceil(holeX *1.5) end
- while math.ceil(holeX) > termMidX -3 or math.ceil(holeZ) > termMaxZ -3 or math.ceil(holeY) > termMaxZ -3 do
- holeX = holeX *.99
- holeZ = holeZ *.99
- holeY = holeY *.99
- zoom = zoom *.99
- end
- offsetX, offsetZ = 0, 0
- if zoom ~= oldZoom then zooming = true end
- else
- if fixAspect then holeX = holeX *1.5 end
- holeX = holeX * (zoom /100)
- holeZ = holeZ * (zoom /100)
- holeY = holeY * (zoom /100)
- end
- holeX = math.ceil(holeX)
- holeZ = math.ceil(holeZ)
- holeY = math.ceil(holeY)
- end
- --# For finding the top left corner of a "centered" object
- function centeredX(winWidth, objWidth)
- return math.ceil((winWidth) /2) - math.floor(objWidth /2)
- end
- function centeredZ(winHeight, objHeight)
- return math.ceil((winHeight) /2) - math.floor(objHeight /2) + 1
- end
- --# The windows used to draw the hole shapes
- local tRect = window.create(topView, centeredX(termMidX -1, holeX), centeredZ(termMaxZ -1, holeZ), holeX, holeZ, true)
- local fRect = window.create(frontView, centeredX(termMidX -1, holeX), 1, holeX, holeY, true)
- --# Lets start drawing...
- drawMode.setVisible(true)
- drawMode.setBackgroundColor(colors.black)
- drawMode.clear()
- drawMode.setCursorPos(centeredX(termMaxX, 4) +1, termMaxZ)
- drawMode.setTextColor(colors.red)
- drawMode.write("QUIT")
- --# This is for the frameskip function
- local lastDrawAll = 0
- local day = os.day()
- --# For the text at the bottom of the screen
- function dimText(output, color, x, y)
- if x ~= nil then dimWin.setCursorPos(x, y) end
- if color ~= nil then dimWin.setTextColor(color) end
- dimWin.write(output)
- end
- --# This beast figures out what to redraw and then does it
- function drawAll()
- if (os.time() - lastDrawAll) <= .001 and enableFrameskip then
- --# we drew the previous frame not too long ago, we'll skip this one
- if day ~= os.day() then day = os.day() lastDrawAll = os.time() end
- --# since we use os.time() to compare frame draws, we have to account for new days
- return false
- else
- recalculateHoles()
- if holeX ~= oldHoleX or holeZ ~= oldHoleZ or panning then
- if holeX < oldHoleX or holeZ < oldHoleZ or panning then
- --# we'll only redraw the top view's background when the hole shrinks or moves
- topView.setBackgroundColor(colors.green)
- topView.clear()
- end
- --# drawing the top view hole
- tRect.reposition(centeredX(termMidX -1, holeX) + offsetX, centeredZ(termMaxZ -1, holeZ) + offsetZ, holeX, holeZ)
- tRect.setBackgroundColor(colors.black)
- end
- if holeX ~= oldHoleX or holeY ~= oldHoleY or panning or zooming then
- if holeX < oldHoleX or holeY < oldHoleY or panning or zooming then
- --# we'll only redraw the front view's background when the hole shrinks, moves, or the zoom changes
- frontView.setBackgroundColor(colours.lightGrey)
- frontView.clear()
- --# this part is for the dirt/grass layer which shrinks as you zoom out
- frontView.setBackgroundColor(colors.green)
- if zoom > 33.3 then frontView.setCursorPos(1, 1) frontView.clearLine() end
- frontView.setBackgroundColor(colors.brown)
- if zoom > 75 then frontView.setCursorPos(1, 4) frontView.clearLine() end
- if zoom > 50 then frontView.setCursorPos(1, 3) frontView.clearLine() end
- if zoom > 25 then frontView.setCursorPos(1, 2) frontView.clearLine() end
- if zoom > 16.5 and zoom <= 33.3 then frontView.setCursorPos(1, 1) frontView.clearLine() end
- zooming = false
- end
- --# drawing the front view hole
- fRect.reposition(centeredX(termMidX -1, holeX) + offsetX, 1, holeX, holeY)
- fRect.setBackgroundColor(colors.black)
- end
- if inputX ~= oldInputX or inputZ ~= oldInputZ or inputY ~= oldInputY then
- dimWin.setBackgroundColor(colors.black)
- dimWin.clear()
- dimText("X:", colors.cyan, 1, 1)
- dimText(tostring(inputX), colors.yellow)
- dimText("Z:", colors.cyan, 7, 1)
- dimText(tostring(inputZ), colors.yellow)
- dimText("Y:", colors.cyan, 13, 1)
- dimText(tostring(inputY), colors.yellow)
- end
- if oldZoom ~= zoom or oldAZ ~= autoZoom then
- if autoZoom then
- zoomWin.clear()
- zoomWin.setCursorPos(1, 1)
- zoomWin.setTextColor(colors.white)
- zoomWin.write("AutoZoom: ")
- if zoom ~=100 then zoomWin.setTextColor(colors.orange) end
- if zoom >= 2 then zoomWin.write((math.ceil(zoom *10) /10).. "%") else zoomWin.write((math.ceil(zoom *100) /100).. "%") end
- else
- zoomWin.clear()
- zoomWin.setCursorPos(5, 1)
- zoomWin.setTextColor(colors.red)
- if zoom >= 2 then zoomWin.write("Zoom: "..(math.ceil(zoom *10) /10).. "%") else zoomWin.write("Zoom: "..(math.ceil(zoom *100) /100).. "%") end
- end
- end
- oldHoleX, oldHoleZ, oldHoleY, oldZoom, oldAZ, oldInputX, oldInputZ, oldInputY = holeX, holeZ, holeY, zoom, autoZoom, inputX, inputZ, inputY
- if autoZoom or (offsetX == 0 and offsetZ == 0) then panning = false end
- lastDrawAll = os.time()
- end
- end
- --# This is an auto frameskip to cut down on lag
- --# Since it makes use of CC's clock, it doesn't play well with some emulators
- function frameskip()
- os.sleep(0.1)
- drawAll()
- end
- drawAll()
- --# Main loop and input stuff
- while true do
- local event = {os.pullEvent()}
- if event[1] == "mouse_scroll" then
- panning = false
- upDown, mouseX, mouseY = event[2], event[3], event[4]
- if mouseY == termMaxZ then --# scrolled in text area
- if mouseX < 6 and mouseX >= 1 then
- inputX = inputX - upDown
- if inputX < 1 then inputX = 1 end
- end
- if mouseX < 12 and mouseX >= 7 then
- inputZ = inputZ - upDown
- if inputZ < 1 then inputZ = 1 end
- end
- if mouseX < 18 and mouseX >= 13 then
- inputY = inputY - upDown
- if inputY < 1 then inputY = 1 end
- end
- if mouseX >= (termMaxX -14) then --# scrolled in zoom text
- if upDown == -1 then zoom = zoom *1.05 else zoom = zoom *0.952380 end
- if zoom >= 100 then zoom = 100 end
- if math.abs(zoom -100) < 4 then zoom = 100 end
- if math.abs(zoom -75) < 2 then zoom = 75 end
- if math.abs(zoom -50) < 1 then zoom = 50 end
- if math.abs(zoom -25) < .5 then zoom = 25 end
- if math.abs(zoom -10) < .25 then zoom = 10 end
- autoZoom = false
- zooming = true
- end
- if drawAll() == false then frameskip() end
- end
- end --# mouse_scroll
- if event[1] == "mouse_click" then
- mouseX, mouseY = event[3], event[4]
- if mouseY == termMaxZ then --# clicked in text area
- if mouseX >= (termMaxX -14) then --# clicked zoom to toggle autozoom
- autoZoom = not autoZoom
- if autoZoom then panning = true end
- if drawAll() == false then frameskip() end
- if autoZoom then panning = false end
- end
- end
- if mouseX <= termMidX -1 and mouseY <= termMaxZ -1 then --# clicked in topView area
- dragFromX, dragFromZ = mouseX, mouseY
- oldPanX, oldPanZ = 0, 0
- else
- dragFromX, dragFromZ = 9999, 9999
- end
- if (mouseX > centeredX(termMaxX, 4) and mouseX < centeredX(termMaxX, 4) +5) and mouseY == termMaxZ then --# "QUIT"
- break
- end
- end --# mouse_click
- if event[1] == "mouse_drag" then
- mouseX, mouseY = event[3], event[4]
- if dragFromX ~= 9999 and dragFromZ ~= 9999 then --# dragged from topView area
- local panX = mouseX - dragFromX
- offsetX = offsetX + panX - oldPanX
- oldPanX = panX
- local panZ = mouseY - dragFromZ
- offsetZ = offsetZ + panZ - oldPanZ
- oldPanZ = panZ
- panning = true
- autoZoom = false
- if drawAll() == false then frameskip() end
- end
- end --# mouse_drag
- end
- dimWin.clear()
- term.setCursorPos(1, termMaxZ)
Advertisement
Add Comment
Please, Sign In to add comment