Advertisement
YangSapiens

BUR OS

Mar 25th, 2023 (edited)
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.33 KB | None | 0 0
  1. local osScrW,osScrH=51,19
  2. local tick=0.05
  3. local osWork=true
  4. local osRunProgram
  5. local logo=
  6. [[
  7. # ##
  8. #### #
  9. #### #
  10. #### #
  11. #### BurOs
  12. ]]
  13. local defTC,defBC=
  14. term.getTextColor(),
  15. term.getBackgroundColor()
  16. local osCurKey
  17. local osAll={system_frames={},frames={}}
  18. local osWindows={}
  19.  
  20. local function table_find(t,a)
  21. for i,v in pairs(t) do
  22. if v==a then
  23. return i
  24. end
  25. end
  26. end
  27.  
  28. local function osInstT(s,rW,rH)
  29. if s:find('\n') then
  30. local cNs=0
  31. local sN=''
  32. for c in s:gmatch('.') do
  33. if c~='\n' then
  34. cNs=(cNs>=rW) and 1 or cNs+1
  35. sN=sN..c
  36. else
  37. for i=1,rW-cNs do
  38. sN=sN..' '
  39. end
  40. cNs=0
  41. end
  42. end
  43. s=sN
  44. end
  45. for i=1,rW*rH-#s do
  46. s=s..' '
  47. end
  48. local t={}
  49. for y=0,rH do
  50. local st=y*rW+(y>0 and 1 or 0)
  51. local en=st+rW
  52. local l=s:sub(st,en)
  53. local cs={}
  54. for c in l:gmatch('.') do
  55. table.insert(cs,c)
  56. end
  57. table.insert(t,cs)
  58. end
  59. return t
  60. end
  61.  
  62. local function osInitFrame(system)
  63. local sets={
  64. x=1,y=1,ox=0,oy=0,
  65. width=osScrW,height=osScrH,
  66. color=defBC,text='',visible=true,
  67. textColor=defTC,xvic=0,yvic=0
  68. }
  69. function sets:change(args)
  70. for i,v in pairs(args) do
  71. if sets[i] then
  72. sets[i]=v
  73. end
  74. end
  75. end
  76. local source=osAll[system and 'system_frames' or 'frames']
  77. table.insert(source,sets)
  78. function sets:destroy()
  79. local id=table_find(source,self)
  80. table.remove(source,id)
  81. end
  82. return sets
  83. end
  84.  
  85. local function osDrawFrame(rX,rY,rW,rH,rC,t,tC)
  86. if t then
  87. tC=tC or defTC
  88. t=osInstT(t,rW,rH)
  89. else
  90. tC=rC
  91. end
  92. for y=0,rH-1 do
  93. term.setCursorPos(rX,y+rY)
  94. for x=0,rW-1 do
  95. term.setTextColor(tC)
  96. term.setBackgroundColor(rC)
  97. term.write(t and t[y+1][x+1]or ' ')
  98. end
  99. end
  100. end
  101. local function osDrawAll()
  102. for _,f in pairs(osAll)do
  103. for _,r in pairs(f)do
  104. if r.visible then
  105. osDrawFrame(
  106. r.x+r.ox,
  107. r.y+r.oy,
  108. r.width,
  109. r.height,
  110. r.color,
  111. r.text,
  112. r.textColor
  113. )
  114. end
  115. end
  116. end
  117. end
  118.  
  119. local function osInitWindow()
  120. local other={}
  121. function other:change(args)
  122. for i,v in pairs(args) do
  123. self[i]=v
  124. end
  125. end
  126. local main=osInitFrame()
  127. local sets={
  128. grab=osInitFrame(),
  129. close=osInitFrame(),
  130. main=main,
  131. other=other
  132. }
  133. function sets:stabilize()
  134. local grab,close,main,other=self.grab,self.close,self.main,self.other
  135. grab.width=main.width-1
  136. grab.height=1
  137. close.x=grab.x+grab.width
  138. close.y=grab.y
  139. close.height=1
  140. close.width=1
  141. main.x=grab.x
  142. main.y=grab.y+1
  143. for _,i in pairs(other) do
  144. if type(i)=='table' then
  145. i.ox=main.x-1
  146. i.oy=main.y-1
  147. end
  148. end
  149. end
  150. sets:stabilize()
  151. function sets:change(args)
  152. for i,v in pairs(args) do
  153. if self[i] then
  154. self[i]:change(v)
  155. end
  156. end
  157. self:stabilize()
  158. end
  159. sets:change({main={color=colors.white,textColor=colors.black},grab={color=colors.blue,text='window',yvic=1},close={color=colors.red,text='X'}})
  160. function sets.grab:m1drag(x,y)
  161. local grab=sets.grab
  162. grab.x=x-math.floor(grab.width/2);grab.y=y-math.floor(grab.height/2)
  163. sets:stabilize()
  164. end
  165. function sets:addFrame(n,i)
  166. self.other[n]=i
  167. end
  168. function sets:getFrame(n)
  169. return self.other[n]
  170. end
  171. function sets:remFrame(n)
  172. self.other[n]=nil
  173. end
  174. function sets:desFrame(n)
  175. self.other[n]:destroy()
  176. self.other[n]=nil
  177. end
  178. function sets:destroy()
  179. local grab,close,main,other=self.grab,self.close,self.main,self.other
  180. grab:destroy()
  181. close:destroy()
  182. main:destroy()
  183. for _,i in pairs(other) do
  184. if type(i)=='table' then
  185. i:destroy()
  186. end
  187. end
  188. local id=table_find(osWindows,self)
  189. table.remove(osWindows,id)
  190. end
  191. function sets.close:m1click()
  192. sets:destroy()
  193. end
  194. table.insert(osWindows,sets)
  195. return sets
  196. end
  197.  
  198.  
  199. local function mouseFB(t,m,x,y)
  200. t='m'..tostring(m)..t
  201. local source=osAll[#osAll.system_frames>0 and 'system_frames' or 'frames']
  202. for i=#source,1,-1 do
  203. local b=source[i]
  204. local bx,by=b.x+b.ox,b.y+b.oy
  205. if x>=bx-b.xvic and y>=by-b.yvic and x<bx+b.width+b.xvic and y<by+b.height+b.yvic then
  206. if b[t] then
  207. b[t](b,x,y)
  208. end
  209. break
  210. end
  211. end
  212. end
  213. local function osMouseClick()
  214. while osWork do
  215. local e,m,x,y=os.pullEvent('mouse_click')
  216. mouseFB('click',m,x,y)
  217. end
  218. end
  219. local function osMouseDrag()
  220. while osWork do
  221. local e,m,x,y=os.pullEvent('mouse_drag')
  222. mouseFB('drag',m,x,y)
  223. end
  224. end
  225. local function osClear()
  226. term.clear()
  227. osDrawFrame(1,1,osScrW,osScrH,defBC)
  228. term.setCursorPos(1,1)
  229. end
  230. local function osInput()
  231. while true do
  232. local e,k=os.pullEvent('key')
  233. osCurKey=k
  234. end
  235. end
  236. local function osRender()
  237. while osWork do
  238. osDrawAll()
  239. os.sleep(tick)
  240. osClear()
  241. end
  242. end
  243. local function osIntro()
  244. local introScr=osInitFrame()
  245. local text='BUR OS v0.01 Starting...\n'..logo..'\nloading...\n'
  246. introScr:change({text,color=colors.blue})
  247. os.sleep(1)
  248. for i=1,10 do
  249. os.sleep(0.1)
  250. local loading=''
  251. for a=1,i do
  252. loading=loading..'#'
  253. end
  254. introScr.text=text..tostring(i*10)..'%:'..loading
  255. end
  256. introScr:destroy()
  257. end
  258. local function osOutro(message)
  259. local outroScr=osInitFrame()
  260. outroScr:change({text=message,color=colors.red})
  261. os.sleep(1)
  262. outroScr:destroy()
  263. end
  264. local function osAlert(message,w,h)
  265. local alertWin=osInitWindow()
  266. local ok=osInitFrame(true)
  267. ok:change({text='OK',width=2,height=1,x=math.floor(w/2),y=h-1})
  268. function ok:m1click()
  269. alertWin:destroy()
  270. end
  271. alertWin:addFrame('ok',ok)
  272. alertWin:change({main={width=w,height=h,text=message},grab={x=math.floor((osScrW-w)/2),y=math.floor((osScrH-h)/2),color=colors.red}})
  273. end
  274. local function osChoiceList(step,x,y,args,event)
  275. local size=-1
  276. for _,v in pairs(args) do
  277. size=math.max(size,#v)
  278. end
  279. local frames={}
  280. for i,t in pairs(args) do
  281. local b=osInitFrame(true)
  282. b:change({x=x,y=y+(i-1)*step,width=size,height=1,color=colors.white,textColor=colors.black,text=t})
  283. function b:m1click()
  284. for _,f in pairs(frames) do
  285. f:destroy()
  286. end
  287. event(args[i])
  288. end
  289. table.insert(frames,b)
  290. end
  291. end
  292.  
  293. local function startMenu(x,y)
  294. y=y-1
  295. local pathHis={}
  296. local path=''
  297. local function showFiles()
  298. osChoiceList(-1,x,y,{'[cancel]','[shutdown]','[reboot]','<--',table.unpack(fs.list(path))},
  299. function(res)
  300. if res:sub(-4,-1)=='.lua' then
  301. osWork=false
  302. osRunProgram=path:sub(2)..'\\'..res
  303. elseif res=='[shutdown]' or res=='[reboot]' then
  304. osOutro(res);os[res:sub(2,-2)]()
  305. elseif res=='[cancel]' then
  306. return
  307. elseif res=='<--' and #pathHis>0 then
  308. path=table.remove(pathHis)
  309. elseif res~='<--' then
  310. table.insert(pathHis,path)
  311. path=path..'\\'..res
  312. end
  313. showFiles()
  314. end)
  315. end
  316. showFiles()
  317. end
  318.  
  319. local function osMain()
  320. osIntro()
  321. local osBackground=osInitFrame()
  322. osBackground:change({color=colors.lightBlue })
  323. local osStart=osInitFrame()
  324. osStart:change({width=5,height=1,color=colors.red,text='start',y=osScrH})
  325.  
  326. function osStart:m1click(x,y)
  327. startMenu(1,y)
  328. end
  329.  
  330. local b=osInitFrame()
  331. b:change({text='PRESS ME :)',color=colors.red,width=11,height=1})
  332. function b:m1click()
  333. osAlert('Uberi ruki ot pc Vovi >:(',24,4)
  334. end
  335.  
  336. local w=osInitWindow()
  337. w:change({main={width=9,height=6,text='Vova krutoi'},grab={x=5,y=5}})
  338. end
  339. parallel.waitForAll(osMain,osRender,osMouseClick,osMouseDrag)
  340. term.setBackgroundColor(defBC);term.setTextColor(defTC)
  341. if osRunProgram then
  342. shell.run(osRunProgram)
  343. end
  344.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement