Advertisement
YangSapiens

YangOs

Jan 17th, 2024 (edited)
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.51 KB | None | 0 0
  1. --define SYSTEM SETTINGS
  2. local osWork = true
  3. local osTargetFrames = 24
  4. local osScrW,osScrH = 51, 19
  5. local osBackground = colors.blue
  6. --end SYSTEM SETTINGS
  7.  
  8.  
  9.  
  10. --define OS_SHADOW_FUNCTIONS
  11. local function osShadow(sx, sy, sw, sh)
  12.    return {
  13.         x = sx,
  14.         y = sy,
  15.         width = sw,
  16.         height = sh
  17.    }
  18. end
  19. local function osShadowFromRect(r)
  20.    return osShadow(r.x,r.y,r.width,r.height)
  21. end
  22. local function osShadowFromWindow(w)
  23.     return osShadowFromRect(w.window_rect)
  24. end
  25. local function osShadowDraw(s)
  26.     for yoffset = 0, s.height-1 do
  27.         term.setCursorPos(s.x, s.y + yoffset)
  28.         term.write( (' '):rep(s.width))
  29.     end
  30. end
  31. --end OS_SHADOW_FUNCTIONS
  32.  
  33.  
  34.  
  35. --define OS_SHADOWS_FUNCTIONS
  36. local function osShadowsFromRects(rs)
  37.   local ss = {}
  38.   for i,r in ipairs(rs) do
  39.       ss[i]=osShadowFromRect(r)
  40.   end
  41.   return ss
  42. end
  43. local function osShadowsFromWindows(ws)
  44.     local ss = {}
  45.     for i,w in ipairs(ws) do
  46.       ss[i]=osShadowFromWindow(w)
  47.     end
  48.     return ss
  49. end
  50. local function osShadowsDraw(ss)
  51.     term.setBackgroundColor(osBackground)
  52.     for _,s in ipairs(ss) do
  53.         osShadowDraw(s)
  54.     end
  55. end    
  56. --end OS_SHADOWS_FUNCTIONS
  57.  
  58.  
  59.  
  60. --define OS_RECT_FUNCTIONS
  61. local function osRect(rx, ry, rw, rh, ri, rc, rt, rtc)
  62.     return {
  63.         x = rx,
  64.         y = ry,
  65.         width = rw,
  66.         height = rh,
  67.         index = ri,
  68.         color = rc,
  69.         text = rt,
  70.         text_color = rtc
  71.     }
  72. end
  73. local function osRectCopy(r)
  74.     return osRect(r.x,r.y,r.width,r.height,r.index,r.color,r.text,r.text_color)
  75. end
  76. local function osRectDraw(r)
  77.     if r.text_color then
  78.         term.setTextColor(r.text_color)
  79.     end        
  80.     term.setBackgroundColor(r.color)
  81.     local textoffset = 0
  82.     for yoffset = 0, r.height-1 do
  83.         term.setCursorPos(r.x, r.y + yoffset)
  84.         if r.text then
  85.             local soffset = yoffset*r.width
  86.             local start, ends = 1+soffset+textoffset,r.width+soffset+textoffset
  87.             local s = r.text:sub(start, ends)
  88.             local nline = s:find('\n')
  89.             local cnext = r.text:sub(ends+1,ends+1)
  90.             if nline then
  91.                 s = s:sub(1,nline-1)..(' '):rep(r.width-nline+1)
  92.                 textoffset = textoffset - r.width + nline
  93.             elseif cnext == '\n' then
  94.                 textoffset = textoffset + 1
  95.             else
  96.                 local i = s:reverse():find(' ')
  97.                 if i and (cnext~='' and cnext~=' ' and cnext~='\n') then
  98.                     textoffset = textoffset - i + 1
  99.                     s = s:sub(1,-i)
  100.                 end
  101.                 if #s<r.width then s = s..(' '):rep(r.width-#s) end
  102.             end
  103.             term.write(s)
  104.         else
  105.             term.write( (' '):rep(r.width))
  106.         end
  107.     end
  108. end
  109. --end OS_RECT_FUNCTIONS
  110.  
  111.  
  112.  
  113. --define OS_RECTS_FUNCTIONS
  114. local function osRectsCopy(rs)
  115.     local copy = {}
  116.     for i,r in ipairs(rs) do
  117.         copy[i]=osRectCopy(r)
  118.     end
  119.     return copy
  120. end
  121. local function osRectsDraw(rs)
  122.     local copy = osRectsCopy(rs)
  123.     table.sort(copy, function(r1, r2)return r1.index >= r2.index end)
  124.     for _,r in ipairs(copy) do
  125.         osRectDraw(r)
  126.     end
  127. end
  128. --end OS_RECTS_FUNCTIONS
  129.  
  130.  
  131.  
  132. --define OS_LOCAL_RECT_FUNCTIONS
  133. local function osLocalRectDraw(lr, x0, y0)
  134.     local copy = osRectCopy(lr)
  135.     copy.x = copy.x+x0
  136.     copy.y = copy.y+y0
  137.     osRectDraw(copy)
  138. end
  139. --end OS_LOCAL_RECT_FUNCTIONS
  140.  
  141.  
  142.  
  143. --define OS_LOCAL_RECTS_FUNCTIONS
  144. local function osLocalRectsDraw(lrs, x0, y0)
  145.     for _,lr in ipairs(lrs) do
  146.         osLocalRectDraw(lr, x0, y0)
  147.     end        
  148. end
  149. --end OS_LOCAL_RECTS_FUNCTIONS
  150.  
  151.  
  152.  
  153. --define OS_WINDOW_FUNCTIONS
  154. local function osWindow(wr, wlrs)
  155.     return {
  156.         window_rect = wr,
  157.         lrects = wlrs
  158.     }
  159. end
  160. local function osWindowCopy(w)
  161.     return osWindow(osRectCopy(w.window_rect), osRectsCopy(w.lrects))
  162. end    
  163. local function osWindowDraw(w)
  164.     local wr = w.window_rect
  165.     osRectDraw(wr)
  166.     osLocalRectsDraw(w.lrects, wr.x,wr.y)
  167. end
  168. --end OS_WINDOW_FUNCTIONS
  169.  
  170.  
  171.  
  172. --define OS_WINDOWS_FUNCTIONS
  173. local function osWindowsCopy(ws)
  174.     local copy = {}
  175.     for i,w in pairs(ws) do
  176.         copy[i] = osWindowCopy(w)
  177.     end
  178.     return copy
  179. end
  180. local function osWindowsDraw(ws)
  181.     local copy = osWindowsCopy(ws)
  182.     table.sort(copy, function(w1, w2)return w1.window_rect.index >= w2.window_rect.index end)
  183.     for _,w in pairs(copy) do
  184.         osWindowDraw(w)
  185.     end
  186. end
  187. --end OS_WINDOWS_FUNCTIONS
  188.  
  189.  
  190. --osRect rx, ry, rw, rh, ri, rc, rt, rtc
  191. --define OS_FUNCTIONS
  192. local osShadows = {}
  193. local osWindows = {
  194.     osWindow(
  195.         osRect(10,1,20,10,1,colors.red,"Hello",colors.white),
  196.         {osRect(1,5,5,5,1,colors.white,"Blue",colors.blue)}
  197.     )
  198. }
  199.  
  200. local function osMouseControl(x,y,event)
  201.     for _,w in ipairs(osWindows) do
  202.         local r = w.window_rect
  203.         if r.index==1 then
  204.             if event == "click" then
  205.                 r.x = x
  206.                 r.y = y  
  207.             elseif event=="drag" then
  208.                 r.width = math.abs(r.x-x)+1
  209.                 r.height = math.abs(r.y-y)+1  
  210.             end                
  211.         end
  212.     end        
  213. end
  214. local function osMouseClick()
  215.     while osWork do
  216.         local e,b,x,y=os.pullEvent('mouse_click')
  217.         osMouseControl(x,y,"click")
  218.     end
  219. end
  220. local function osMouseDrag()
  221.    while osWork do
  222.         local e,b,x,y=os.pullEvent('mouse_drag')
  223.         osMouseControl(x,y,"drag")
  224.    end
  225. end
  226. local function osMain()
  227.     osRectDraw(osRect(1,1,osScrW,osScrH,0,osBackground))
  228.     while osWork do
  229.         sleep(1/osTargetFrames)
  230.         osShadowsDraw(osShadows)
  231.         osWindowsDraw(osWindows)
  232.         osShadows = osShadowsFromWindows(osWindows)
  233.     end  
  234. end
  235. --end OS_FUNCTIONS
  236.  
  237.  
  238.  
  239. parallel.waitForAll(osMain,osMouseClick, osMouseDrag)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement