Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --define SYSTEM SETTINGS
- local osWork = true
- local osTargetFrames = 24
- local osScrW,osScrH = 51, 19
- local osBackground = colors.blue
- --end SYSTEM SETTINGS
- --define OS_SHADOW_FUNCTIONS
- local function osShadow(sx, sy, sw, sh)
- return {
- x = sx,
- y = sy,
- width = sw,
- height = sh
- }
- end
- local function osShadowFromRect(r)
- return osShadow(r.x,r.y,r.width,r.height)
- end
- local function osShadowFromWindow(w)
- return osShadowFromRect(w.window_rect)
- end
- local function osShadowDraw(s)
- for yoffset = 0, s.height-1 do
- term.setCursorPos(s.x, s.y + yoffset)
- term.write( (' '):rep(s.width))
- end
- end
- --end OS_SHADOW_FUNCTIONS
- --define OS_SHADOWS_FUNCTIONS
- local function osShadowsFromRects(rs)
- local ss = {}
- for i,r in ipairs(rs) do
- ss[i]=osShadowFromRect(r)
- end
- return ss
- end
- local function osShadowsFromWindows(ws)
- local ss = {}
- for i,w in ipairs(ws) do
- ss[i]=osShadowFromWindow(w)
- end
- return ss
- end
- local function osShadowsDraw(ss)
- term.setBackgroundColor(osBackground)
- for _,s in ipairs(ss) do
- osShadowDraw(s)
- end
- end
- --end OS_SHADOWS_FUNCTIONS
- --define OS_RECT_FUNCTIONS
- local function osRect(rx, ry, rw, rh, ri, rc, rt, rtc)
- return {
- x = rx,
- y = ry,
- width = rw,
- height = rh,
- index = ri,
- color = rc,
- text = rt,
- text_color = rtc
- }
- end
- local function osRectCopy(r)
- return osRect(r.x,r.y,r.width,r.height,r.index,r.color,r.text,r.text_color)
- end
- local function osRectDraw(r)
- if r.text_color then
- term.setTextColor(r.text_color)
- end
- term.setBackgroundColor(r.color)
- local textoffset = 0
- for yoffset = 0, r.height-1 do
- term.setCursorPos(r.x, r.y + yoffset)
- if r.text then
- local soffset = yoffset*r.width
- local start, ends = 1+soffset+textoffset,r.width+soffset+textoffset
- local s = r.text:sub(start, ends)
- local nline = s:find('\n')
- local cnext = r.text:sub(ends+1,ends+1)
- if nline then
- s = s:sub(1,nline-1)..(' '):rep(r.width-nline+1)
- textoffset = textoffset - r.width + nline
- elseif cnext == '\n' then
- textoffset = textoffset + 1
- else
- local i = s:reverse():find(' ')
- if i and (cnext~='' and cnext~=' ' and cnext~='\n') then
- textoffset = textoffset - i + 1
- s = s:sub(1,-i)
- end
- if #s<r.width then s = s..(' '):rep(r.width-#s) end
- end
- term.write(s)
- else
- term.write( (' '):rep(r.width))
- end
- end
- end
- --end OS_RECT_FUNCTIONS
- --define OS_RECTS_FUNCTIONS
- local function osRectsCopy(rs)
- local copy = {}
- for i,r in ipairs(rs) do
- copy[i]=osRectCopy(r)
- end
- return copy
- end
- local function osRectsDraw(rs)
- local copy = osRectsCopy(rs)
- table.sort(copy, function(r1, r2)return r1.index >= r2.index end)
- for _,r in ipairs(copy) do
- osRectDraw(r)
- end
- end
- --end OS_RECTS_FUNCTIONS
- --define OS_LOCAL_RECT_FUNCTIONS
- local function osLocalRectDraw(lr, x0, y0)
- local copy = osRectCopy(lr)
- copy.x = copy.x+x0
- copy.y = copy.y+y0
- osRectDraw(copy)
- end
- --end OS_LOCAL_RECT_FUNCTIONS
- --define OS_LOCAL_RECTS_FUNCTIONS
- local function osLocalRectsDraw(lrs, x0, y0)
- for _,lr in ipairs(lrs) do
- osLocalRectDraw(lr, x0, y0)
- end
- end
- --end OS_LOCAL_RECTS_FUNCTIONS
- --define OS_WINDOW_FUNCTIONS
- local function osWindow(wr, wlrs)
- return {
- window_rect = wr,
- lrects = wlrs
- }
- end
- local function osWindowCopy(w)
- return osWindow(osRectCopy(w.window_rect), osRectsCopy(w.lrects))
- end
- local function osWindowDraw(w)
- local wr = w.window_rect
- osRectDraw(wr)
- osLocalRectsDraw(w.lrects, wr.x,wr.y)
- end
- --end OS_WINDOW_FUNCTIONS
- --define OS_WINDOWS_FUNCTIONS
- local function osWindowsCopy(ws)
- local copy = {}
- for i,w in pairs(ws) do
- copy[i] = osWindowCopy(w)
- end
- return copy
- end
- local function osWindowsDraw(ws)
- local copy = osWindowsCopy(ws)
- table.sort(copy, function(w1, w2)return w1.window_rect.index >= w2.window_rect.index end)
- for _,w in pairs(copy) do
- osWindowDraw(w)
- end
- end
- --end OS_WINDOWS_FUNCTIONS
- --osRect rx, ry, rw, rh, ri, rc, rt, rtc
- --define OS_FUNCTIONS
- local osShadows = {}
- local osWindows = {
- osWindow(
- osRect(10,1,20,10,1,colors.red,"Hello",colors.white),
- {osRect(1,5,5,5,1,colors.white,"Blue",colors.blue)}
- )
- }
- local function osMouseControl(x,y,event)
- for _,w in ipairs(osWindows) do
- local r = w.window_rect
- if r.index==1 then
- if event == "click" then
- r.x = x
- r.y = y
- elseif event=="drag" then
- r.width = math.abs(r.x-x)+1
- r.height = math.abs(r.y-y)+1
- end
- end
- end
- end
- local function osMouseClick()
- while osWork do
- local e,b,x,y=os.pullEvent('mouse_click')
- osMouseControl(x,y,"click")
- end
- end
- local function osMouseDrag()
- while osWork do
- local e,b,x,y=os.pullEvent('mouse_drag')
- osMouseControl(x,y,"drag")
- end
- end
- local function osMain()
- osRectDraw(osRect(1,1,osScrW,osScrH,0,osBackground))
- while osWork do
- sleep(1/osTargetFrames)
- osShadowsDraw(osShadows)
- osWindowsDraw(osWindows)
- osShadows = osShadowsFromWindows(osWindows)
- end
- end
- --end OS_FUNCTIONS
- parallel.waitForAll(osMain,osMouseClick, osMouseDrag)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement