Advertisement
Corona

[Computercraft] Window Test

Jul 21st, 2015
342
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.56 KB | None | 0 0
  1. os.loadAPI("./lib/draw")
  2. os.loadAPI("./lib/window")
  3.  
  4.  
  5.  
  6.  
  7. --local m = peripheral.wrap("right")
  8. --m.clear()
  9.  
  10. term.clear()
  11.  
  12. --window.setOutput(m)
  13.  
  14. local winT = window.windowTable
  15.  
  16. --register a new window by setting it up as below and reister it with window:register(order)
  17. local w1 = window.Window:new()
  18. w1:setPos(4,5)
  19. w1:setSize(30,10)
  20. --w1:setVisible(true)
  21. w1:register(2)
  22. w1:setText("Ultralong Text just to show how well this actually works!!! Lorem ipsum dolor blablah... Maybe I'll implement a linefeed option somehow. And a \"cut at spaces\" option")
  23. w1:setTitle("Text")
  24.  
  25. local w2 = window.Window:new()
  26. w2:setPos(16,7)
  27. w2:setSize(25,12)
  28. w2:setColor(colors.lime)
  29. --w2:setVisible(true)
  30. w2:register(1)
  31. w2:setText("Another Test Text, just to see if overlapping works as intended!")
  32.  
  33. local w3 = window.Window:new()
  34. w3:setPos(20,3)
  35. w3:setSize(15,8)
  36. w3:setColor(colors.orange)
  37. w3:register(3)
  38.  
  39.  
  40. local w4 = window.Window:new()
  41. w4:setPos(23,10)
  42. w4:setSize(10,9)
  43. w4:setColor(colors.red)
  44. w4:register(1)
  45. w4:setText("Test Textwrap in Window (cutting y already implemented!)")
  46.  
  47. -- for k,v in pairs(winT) do
  48. --   print(v.uuid)
  49. --   os.pullEvent("key")
  50. -- end
  51. -- sleep(3)
  52.  
  53. while true do
  54.  
  55.  
  56.   sleep(0.02) --short sleep time to reduce screen flickering
  57.   term.clear()
  58.  
  59.   --debug stuff
  60.   term.setCursorPos(1,1)
  61.   print("TSize: "..#winT)
  62.   --
  63.  
  64.   window.drawWindows() --draws all windows in WindowTable
  65.   local ev, btn, cx,cy = os.pullEvent()
  66.  
  67.   if ev == "mouse_click" then
  68.     for i=1, #winT do --Runs through WIndows from front to back to prevent covered windows from triggering
  69.       if(winT[i]:isClicked(cx,cy))then --If window is clicked, register it as front window.
  70.         winT[i]:register(1)
  71.         break
  72.       end
  73.     end
  74.   elseif ev == "mouse_drag" then --Beware! messy code!
  75.     tVars = {winT[1]:getClick(lx,ly)} --gets the click coordinates within the window, assumes top window because it's lazier this way
  76.     if tVars[1] then
  77.       if tVars[3] == 1 then --some math or rather if statements to determine if the window is dragged at the top
  78.         wx, wy = winT[1]:getPos()
  79.         winT[1]:setPos( wx + (cx-lx), wy + (cy-ly) )
  80.       elseif tVars[2] == winT[1].xs and tVars[3] == winT[1].ys then --same idea, just the right lower corner this time (resizing)
  81.         if not (winT[1].xs < 15 and cx-lx < 0) then
  82.           winT[1].xs = winT[1].xs + (cx-lx)
  83.         end
  84.         if not (winT[1].ys < 5 and cy-ly < 0) then
  85.           winT[1].ys = winT[1].ys + (cy-ly)
  86.           --TODO: Add functions to alter size and stuff.
  87.         end
  88.       end
  89.     end
  90.   end
  91.   lx, ly = cx, cy --backs up the click to scan for differences for drag
  92. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement