bobmarley12345

enderchest

Jun 26th, 2024
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 24.36 KB | None | 0 0
  1. -- Animation timer data
  2. AppTimerId = 0
  3. AppLastEventTime = 0
  4.  
  5. -- The application animation timer tick rate
  6. AnimTimerTickRate = 10
  7.  
  8. -- The interval of an application tick, in seconds
  9. AppTimerInterval = 1 / AnimTimerTickRate
  10.  
  11. -- True or false if the current event is a timer/render tick
  12. IsAnimationTick = false
  13.  
  14. -- A helper to print a debug message to the screen
  15. LastDebugMessage = "Debug Message Here"
  16.  
  17. BLANK_EVENT = { type = nil, p1 = nil, p2 = nil, p3 = nil, p4 = nil, }
  18. FOCUSED_COMPONENT = nil
  19.  
  20. -- True when a component was focused during a mouse click bubble event
  21. HAS_SWITCHED_FOCUS_DURING_BUBBLE_CLICK = false
  22.  
  23. APP_ROOT_OBJECTS = {}
  24.  
  25. -- A base table template for a drawable component object
  26. DRAWABLE_TEMPLATE = {
  27. -- x,y are the x and y coords, relative to the parent
  28. -- w,h are the width and height of the object
  29. x = 0, y = 0, w = 0, h = 0,
  30. IsUpdating = false,
  31. OnAnimTick = function(this, time, delta)
  32. -- update object here. e contains the last event data,
  33. -- which may be the timer event
  34. end,
  35. RenderFunc = function(this, x, y)
  36. -- draw here. X and Y are absolute coordinates, accumulated
  37. -- during the recursive render process
  38. end,
  39. -- returns the handled state. True = don't go further
  40. -- These are the function definitions though, however, the handlers are stored in tables,
  41. -- OnPreviewMouseDown = function(this, time, delta, e) return false end, -- args = { btn, x, y }
  42. -- OnMouseDown = function(this, time, delta, e) return false end, -- args = { hit, btn, x, y }
  43. -- OnMouseDrag = function(this, time, delta, e) return false end, -- args = { btn, x, y }
  44. -- OnKeyPressed = function(this, time, delta, e) return false end, -- args = { key }
  45. -- OnCharPressed = function(this, time, delta, e) return false end, -- args = { ch }
  46. OnPreviewMouseDown = {},
  47. OnMouseDown = {},
  48. OnMouseDrag = {},
  49. OnKeyPressed = {},
  50. OnCharPressed = {},
  51.  
  52. -- focus info
  53. IsFocusable = false,
  54. IsFocused = false,
  55. CanProcessDragWhenFocused = false,
  56.  
  57. -- our parent object
  58. parent = nil,
  59. indexInParent = -1,
  60. IsInVisualTree = false,
  61. children = {}, -- a table of child drawable objects
  62. childrenToRemove = {}
  63. }
  64.  
  65. --region Helper String Functions
  66.  
  67. function string.jsub(str, startIndex, endIndex)
  68. if (endIndex == nil) then
  69. return string.sub(str, startIndex)
  70. end
  71. return string.sub(str, startIndex, endIndex - 1)
  72. end
  73.  
  74. -- Checks if the given `str` starts with the given `value` (optionally start searching with the given start offset index. 1 by default). `string.startsWith("hello", "he", 1)` == `true`
  75. function string.startsWith(str, value, startIndex)
  76. if (startIndex == nil) then
  77. startIndex = 1
  78. end
  79. return string.jsub(str, startIndex, string.len(value) + startIndex) == value
  80. end
  81.  
  82. -- Checks if the given `str` ends with the given value. string.endsWith("hello", "lo") == true
  83. function string.endsWith(str, value)
  84. local strLen = string.len(str)
  85. local valLen = string.len(value)
  86. return string.sub(str, strLen - valLen + 1, strLen) == value
  87. end
  88.  
  89. function string.contains(str, value, startIndex)
  90. if (startIndex == nil) then
  91. startIndex = 1
  92. end
  93.  
  94. return string.find(str, value) ~= nil
  95. end
  96.  
  97. function string.indexOf(str, value, startIndex)
  98. if (startIndex == nil) then
  99. startIndex = 1
  100. end
  101.  
  102. local s, e, c = string.find(str, value)
  103. if (s == nil) then
  104. return -1
  105. end
  106.  
  107. return s
  108. end
  109.  
  110. -- Clamps the given string to the given length. `string.len(str)` is smaller than or equal to `maxLen` then `str` is returned. Otherwise, a substring of `maxLen - 3` of characters + `"..."` is returned
  111. function string.clamp(str, maxLen)
  112. if (#str <= maxLen) then
  113. return str
  114. end
  115.  
  116. return (string.sub(str, 1, maxLen - 3) .. "...")
  117. end
  118.  
  119. --endregion
  120.  
  121. utils = {
  122. centerOf = function(start, ending)
  123. if (start == ending) then
  124. return start
  125. elseif (start < ending) then
  126. return math.floor((ending - start) / 2)
  127. else
  128. return math.floor((start - ending) / 2)
  129. end
  130. end,
  131. centerForText = function(size, text)
  132. local strLen = #text
  133. if (strLen < 1) then
  134. return math.floor(size / 2)
  135. else
  136. return math.floor((size / 2) - (strLen / 2))
  137. end
  138. end,
  139. thatOrNull = function(nullable, nonNull)
  140. if (nullable == nil) then return nonNull else return nullable end
  141. end,
  142. byteToMegaByte = function(bytes) return bytes / 1048576 end,
  143. byteToKiloByte = function(bytes) return bytes / 1024 end,
  144. byteToGigaByte = function(bytes) return bytes / 1073741824 end
  145. }
  146.  
  147. --region GUI Functions
  148.  
  149. GDI = {
  150. Handle = nil,
  151. HandleWidth = 0,
  152. HandleHeight = 0,
  153. -- returns a non null text colour
  154. nonNullTextColour = function(text_colour)
  155. if (text_colour == nil) then
  156. return colours.white
  157. end
  158.  
  159. return text_colour
  160. end,
  161. -- returns a non null background colour
  162. nonNullBackgroundColour = function(background_colour)
  163. if (background_colour == nil) then
  164. return colours.black
  165. end
  166.  
  167. return background_colour
  168. end,
  169. -- sets the monitor cursor pos
  170. cpos = function(x, y)
  171. GDI.Handle.setCursorPos(x, y)
  172. end,
  173. -- sets the monitor text colour
  174. textColour = function(colour)
  175. GDI.Handle.setTextColor(colour)
  176. end,
  177. -- sets the monitor background colour
  178. backgroundColour = function(colour)
  179. GDI.Handle.setBackgroundColor(colour)
  180. end,
  181. clearBackground = function(foreground, background)
  182. GDI.textColour(GDI.nonNullTextColour(foreground))
  183. GDI.backgroundColour(GDI.nonNullBackgroundColour(background))
  184. GDI.Handle.clear()
  185. end,
  186. clearMonitor = function()
  187. GDI.clearBackground(colours.white, colours.black)
  188. end,
  189. -- draws text on the monitor
  190. drawText = function(x, y, text, foreground, background)
  191. GDI.textColour(GDI.nonNullTextColour(foreground))
  192. GDI.backgroundColour(GDI.nonNullBackgroundColour(background))
  193. GDI.cpos(x, y)
  194. GDI.Handle.write(text)
  195. end,
  196. drawTextClamped = function(x, y, maxLength, text, foreground, background)
  197. GDI.drawText(x, y, string.clamp(text, maxLength), foreground, background)
  198. end,
  199. drawTextCenteredClamped = function(x, y, w, h, text, foreground, background)
  200. local theText = string.clamp(text, w)
  201. local finalX = x + math.floor((w / 2) - (#text / 2))
  202. local finalY = y + math.floor(h / 2)
  203. GDI.drawText(finalX, finalY, theText, foreground, background)
  204. end,
  205. drawLineH = function(x, y, w, background)
  206. GDI.drawText(x, y, string.rep(" ", w), background, background)
  207. end,
  208. drawLineV = function(x, y, h, background)
  209. GDI.textColour(GDI.nonNullTextColour(background))
  210. GDI.backgroundColour(GDI.nonNullBackgroundColour(background))
  211. for i = y, h do
  212. GDI.Handle.write(" ")
  213. GDI.cpos(x, i)
  214. end
  215. end,
  216. drawSquare = function(x, y, w, h, background)
  217. local line = y
  218. local endLine = y + h
  219. while (true) do
  220. GDI.drawLineH(x, line, w, background)
  221. line = line + 1
  222. if (line >= endLine) then
  223. return
  224. end
  225. end
  226. end,
  227. drawProgressBar = function(x, y, w, h, min, max, val, foreground, background)
  228. GDI.drawSquare(x, y, w, h, background)
  229. GDI.drawSquare(x, y, math.floor((val / (max - min)) * w), h, foreground)
  230. end,
  231. drawGroupBoxHeader = function(x, y, w, text, foreground, background)
  232. local strLen = string.len(text)
  233. if (strLen >= w) then
  234. GDI.drawText(x, y, string.sub(text, 1, w), foreground, background)
  235. elseif (strLen == (w - 1)) then
  236. GDI.drawText(x, y, text .. "-", foreground, background)
  237. elseif (strLen == (w - 2)) then
  238. GDI.drawText(x, y, "-" .. text .. "-", foreground, background)
  239. elseif (strLen == (w - 3)) then
  240. GDI.drawText(x, y, "-" .. text .. " -", foreground, background)
  241. else
  242. local leftOverSpace = strLen - w
  243. local halfLeftOver = leftOverSpace / 2
  244. if (leftOverSpace % 2 == 0) then
  245. -- even
  246. local glyphText = string.rep("-", halfLeftOver - 1)
  247. GDI.drawText(x, y, glyphText .. " " .. text .. " " .. glyphText)
  248. else
  249. -- odd
  250. local glyphTextA = string.rep("-", math.floor(halfLeftOver - 1))
  251. local glyphTextB = string.rep("-", math.ceil(halfLeftOver - 1))
  252. GDI.drawText(x, y, glyphTextA .. " " .. text .. " " .. glyphTextB)
  253. end
  254. end
  255. end,
  256. drawGroupBoxWall = function(x, y, h, foreground, background)
  257. GDI.textColour(GDI.nonNullTextColour(foreground))
  258. GDI.backgroundColour(GDI.nonNullBackgroundColour(background))
  259. for i = y, h do
  260. GDI.Handle.write("-")
  261. GDI.cpos(x, i)
  262. end
  263. end,
  264. drawGroupBoxFooter = function(x, y, w, foreground, background)
  265. GDI.drawText(x, y, string.rep("-", w), foreground, background)
  266. end,
  267. drawGroupBox = function(x, y, w, h, header, border_foreground, border_background, background)
  268. GDI.drawSquare(x, y, w, h, background)
  269. GDI.drawGroupBoxHeader(x, y, w, header, border_foreground, border_background)
  270. GDI.drawGroupBoxWall(x, y, h, border_foreground, border_background)
  271. GDI.drawGroupBoxWall(x + w, y, h, border_foreground, border_background)
  272. GDI.drawGroupBoxFooter(x, y + h, w, border_foreground, border_background)
  273. end,
  274. drawWindowTitle = function(x, y, w, title, foreground, background)
  275. if (background == nil) then
  276. background = colours.grey
  277. end
  278. local text = string.clamp(title, w - 5)
  279. GDI.drawText(x, y, text, foreground, background)
  280. local len = #text
  281. local endX = x + w
  282. GDI.drawLineH(x + len, y, w - len - 4, background)
  283. GDI.drawText(endX - 4, y, "-", colours.foreground, colours.lightGrey)
  284. GDI.drawText(endX - 3, y, "[]", colours.foreground, colours.lightGrey)
  285. GDI.drawText(endX - 1, y, "x", colours.foreground, colours.red)
  286. end,
  287. drawWindow = function(x, y, w, h, title, titleForeground, titleBackground, background)
  288. GDI.drawSquare(x, y, w, h, utils.thatOrNull(background, colours.white))
  289. GDI.drawWindowTitle(x, y, w, title, utils.thatOrNull(titleForeground, colours.lightGrey), utils.thatOrNull(titleBackground, colours.grey))
  290. end,
  291. SetRenderTarget = function (target)
  292. HANDLE = target
  293. GDI.clearMonitor()
  294.  
  295. local w,h = target.getSize()
  296. GDI.HandleWidth = w
  297. GDI.HandleHeight = h
  298. end
  299. }
  300.  
  301. app = {}
  302.  
  303.  
  304. -- the base component object
  305. CComponent = {}
  306. function CComponent.new()
  307.  
  308. end
  309.  
  310. -- a button object
  311. CButton = {}
  312. CButton.__index = CButton
  313. setmetatable(CButton, CComponent)
  314.  
  315. -- a text block object
  316. CTextBlock = {}
  317. setmetatable(CTextBlock, CComponent)
  318.  
  319. -- a component that contains a collection of page objects,
  320. -- with a next/prev button to switch between pages
  321. CPageViewer = {}
  322. setmetatable(CPageViewer, CComponent)
  323.  
  324. -- a page in a page viewer, which presents some content
  325. CPage = {}
  326. setmetatable(CPage, CComponent)
  327.  
  328. --endregion
  329.  
  330. function CComponent:GetAbsolutePosition()
  331. local nextComponent = self
  332. local x,y = 0,0
  333. while (nextComponent ~= nil) do
  334. x = x + nextComponent.x
  335. y = y + nextComponent.y
  336. nextComponent = nextComponent.parent
  337. end
  338. return x, y
  339. end
  340.  
  341. function CComponent:IsMouseOverComponent(absX, absY)
  342. local cX, cY = app.GetAbsolutePosition(self)
  343. return absX >= cX and absX < (cX + self.w) and absY >= cY and absY < (cY + self.h)
  344. end
  345.  
  346. function CComponent:GetRelativeMousePos(mPosX, mPosY)
  347. local absX, absY = app.GetAbsolutePosition(self)
  348. return (mPosX - absX), (mPosY - absY)
  349. end
  350.  
  351. function app.SetupTimer(nTime)
  352. AppTimerId = os.startTimer(nTime)
  353. end
  354.  
  355. function app.UpdateCachedIndices(children)
  356. for k, obj in pairs(children) do
  357. obj.indexInParent = k
  358. end
  359. end
  360.  
  361. function CComponent:InsertComponent(child)
  362. if (child.IsInVisualTree == true) then
  363. error("Child already added to visual tree. It must be removed first")
  364. return
  365. end
  366.  
  367. if (self.children == nil) then
  368. self.children = {}
  369. end
  370.  
  371. child.parent = self
  372. child.indexInParent = #table
  373. table.insert(self.children, child)
  374. child.IsInVisualTree = true
  375. app.UpdateCachedIndices(self.children)
  376. return child
  377. end
  378.  
  379. function CComponent:RemoveFromParent()
  380. if (self.IsInVisualTree ~= true or self.indexInParent == -1) then
  381. return
  382. end
  383.  
  384. if (self.parent == nil) then
  385. return
  386. end
  387.  
  388. local children = self.parent.children
  389. if (children == nil) then
  390. return
  391. end
  392.  
  393. table.remove(children, self.indexInParent)
  394. app.UpdateCachedIndices(children)
  395. end
  396.  
  397. function app.SwitchFocusTo(component)
  398. if (FOCUSED_COMPONENT ~= nil) then
  399. FOCUSED_COMPONENT.IsFocused = false
  400. end
  401.  
  402. FOCUSED_COMPONENT = component
  403. if (component ~= nil) then
  404. component.IsFocused = true
  405. end
  406. end
  407.  
  408. function app.RenderComponent(component, x, y)
  409. if (component.RenderFunc ~= nil) then
  410. component.RenderFunc(component, x, y)
  411. end
  412.  
  413. if (component.children ~= nil) then
  414. app.RenderComponents(component.children, x, y)
  415. end
  416. end
  417.  
  418. function app.RenderComponents(components, x, y)
  419. for k, v in pairs(components) do
  420. app.RenderComponent(v, x + v.x, y + v.y)
  421. end
  422. end
  423.  
  424. function app.DoAnimationTick(component, time, delta)
  425. component.IsUpdating = true
  426. if (component.OnAnimTick ~= nil) then
  427. component.OnAnimTick(component, time, delta)
  428. end
  429.  
  430. local children = component.children
  431. if (children ~= nil) then
  432. for k, comp in pairs(children) do
  433. if (comp.IsInVisualTree == true) then
  434. app.DoAnimationTick(comp, time, delta)
  435. end
  436. end
  437. end
  438. component.IsUpdating = false
  439. end
  440.  
  441. --#region Button Component
  442.  
  443. function CButton:DoAnimTick(self, time, delta)
  444. if (self.IsButtonPressed and self.IsToggleButton == false) then
  445. if (self.TimeToClearButtonPress ~= nil and time > self.TimeToClearButtonPress) then
  446. self.IsButtonPressed = false
  447. end
  448. end
  449. end
  450.  
  451. function CButton:CoreOnMouseDown(self, time, delta, e)
  452. if (self.IsToggleButton) then
  453. if (self.IsButtonPressed) then
  454. self.IsButtonPressed = false
  455. else
  456. self.IsButtonPressed = true
  457. end
  458. else
  459. self.TimeToClearButtonPress = time + 0.5
  460. self.IsButtonPressed = true
  461. end
  462.  
  463. LastDebugMessage = "Hit at: " .. e.x .. "," .. e.y
  464. end
  465.  
  466. function CButton:OnDraw(self, x, y)
  467. local tColour
  468. if (self.IsButtonPressed == true) then
  469. tColour = self.PressedColour
  470. else
  471. tColour = self.BackgroundColour
  472. end
  473. GDI.drawSquare(x, y, self.w, self.h, tColour)
  474. GDI.drawTextCenteredClamped(x, y, self.w, self.h, self.ButtonText, self.ForegroundColour, tColour)
  475. end
  476.  
  477. --#endregion
  478.  
  479. --#region Button Component
  480.  
  481. function CTextBlock:DoAnimTick(self, time, delta)
  482. end
  483.  
  484. function CTextBlock:CoreOnMouseDown(self, time, delta, e)
  485. end
  486.  
  487. function CTextBlock:OnDraw(self, x, y)
  488. GDI.drawTextCenteredClamped(x, y, self.w, self.h, self.Text, self.ForegroundColour, self.BackgroundColour)
  489. end
  490.  
  491. --#endregion
  492.  
  493. function app.Component_CreateCore(x, y, w, h)
  494. local obj = {}
  495. obj.x = x
  496. obj.y = y
  497. obj.w = w
  498. obj.h = h
  499. obj.OnPreviewMouseDown = {}
  500. obj.OnMouseDown = {}
  501. obj.OnMouseDrag = {}
  502. obj.OnKeyPressed = {}
  503. obj.OnCharPressed = {}
  504. return obj
  505. end
  506.  
  507. function app.CreateTextBlock(x, y, w, h, text, fgColour, bgColour)
  508. local tb = app.Component_CreateCore(x, y, w, h)
  509. tb.Text = text
  510. tb.OnAnimTick = core_component_textBlock.DoAnimTick
  511. tb.RenderFunc = core_component_textBlock.OnDraw
  512. table.insert(tb.OnMouseDown, core_component_textBlock.CoreOnMouseDown)
  513. tb.ForegroundColour = fgColour
  514. tb.BackgroundColour = bgColour
  515. return tb
  516. end
  517.  
  518. function app.CreateButton(x, y, w, h, text, isToggleButton, onClicked)
  519. local btn = app.Component_CreateCore(x, y, w, h)
  520. btn.ButtonText = text
  521. btn.OnAnimTick = CButton.DoAnimTick
  522. btn.RenderFunc = CButton.OnDraw
  523. table.insert(btn.OnMouseDown, CButton.CoreOnMouseDown)
  524. if onClicked ~= nil then
  525. table.insert(btn.OnMouseDown, onClicked)
  526. end
  527.  
  528. btn.IsButtonPressed = false
  529. if (isToggleButton == nil) then
  530. isToggleButton = false
  531. end
  532. btn.IsToggleButton = isToggleButton
  533. btn.ForegroundColour = colours.white
  534. btn.BackgroundColour = colours.lightGrey
  535. btn.PressedColour = colours.green
  536. return btn
  537. end
  538.  
  539. --region Application Entry Point and message handling
  540.  
  541. function app.DoTunnelMouseClick(component, time, delta, btn, absX, absY)
  542. if (app.IsMouseOverComponent(component, absX, absY) ~= true) then
  543. return nil
  544. end
  545.  
  546. if (component.OnPreviewMouseDown ~= nil and #component.OnPreviewMouseDown > 0) then
  547. local relX, relY = app.GetRelativeMousePos(component, absX, absY)
  548. local args = {btn=btn,x=relX,y=relY}
  549. for i, handler in pairs(component.OnPreviewMouseDown) do
  550. if (handler(component, time, delta, args)) then
  551. return component
  552. end
  553. end
  554. end
  555.  
  556. local items = component.children
  557. if (items ~= nil) then
  558. for i = #items, 1, -1 do
  559. local hitObj = app.DoTunnelMouseClick(items[i], time, delta, btn, absX, absY)
  560. if (hitObj ~= nil) then
  561. return hitObj
  562. end
  563. end
  564. end
  565.  
  566. return component
  567. end
  568.  
  569. function app.DoBubbleMouseClick(component, originalComponent, time, delta, btn, absX, absY)
  570. if (component.OnMouseDown ~= nil and #component.OnMouseDown > 0) then
  571. local relX, relY = app.GetRelativeMousePos(component, absX, absY)
  572. local args = {btn=btn,hit=originalComponent,x=relX,y=relY}
  573. for i, handler in pairs(component.OnMouseDown) do
  574. if (handler(component, time, delta, args)) then
  575. return
  576. end
  577. end
  578. end
  579.  
  580. if (component.parent ~= nil) then
  581. app.DoBubbleMouseClick(component.parent, originalComponent, time, delta, btn, absX, absY)
  582. end
  583. end
  584.  
  585. function app.OnMouseClick(time, delta, btn, absX, absY)
  586. local hitComponent = nil
  587. for i = #APP_ROOT_OBJECTS, 1, -1 do
  588. hitComponent = app.DoTunnelMouseClick(APP_ROOT_OBJECTS[i], time, delta, btn, absX, absY)
  589. if (hitComponent ~= nil) then
  590. break
  591. end
  592. end
  593.  
  594. -- just in case
  595. HAS_SWITCHED_FOCUS_DURING_BUBBLE_CLICK = true
  596. if (hitComponent ~= nil) then
  597. local finalComponent = app.DoBubbleMouseClick(hitComponent, hitComponent, time, delta, btn, absX, absY)
  598. if (finalComponent == nil) then
  599. if (hitComponent.IsFocusable == true) then
  600. app.SwitchFocusTo(hitComponent)
  601. else
  602. app.SwitchFocusTo(nil)
  603. end
  604. elseif (finalComponent.IsFocusable and finalComponent.IsFocused ~= true) then
  605. app.SwitchFocusTo(finalComponent)
  606. else
  607. app.SwitchFocusTo(nil)
  608. end
  609. else
  610. app.SwitchFocusTo(nil)
  611. end
  612. HAS_SWITCHED_FOCUS_DURING_BUBBLE_CLICK = false
  613. end
  614.  
  615. -- TODO: implement these
  616.  
  617. function app.OnMouseDrag(time, delta, btn, absX, absY)
  618. local component = FOCUSED_COMPONENT
  619. if (component == nil or component.CanProcessDragWhenFocused ~= true or #component.OnMouseDrag < 1) then
  620. return
  621. end
  622.  
  623. local relX, relY = app.GetRelativeMousePos(component, absX, absY)
  624. local args = {btn=btn,x=relX,y=relY}
  625. for i, handler in pairs(component.OnMouseDrag) do
  626. if (handler(component, time, delta, args)) then
  627. return
  628. end
  629. end
  630. end
  631.  
  632. function app.OnKeyPress(time, delta, keyCode)
  633. local component = FOCUSED_COMPONENT
  634. if (component == nil or #component.OnKeyPressed < 1) then
  635. return
  636. end
  637.  
  638. local relX, relY = app.GetRelativeMousePos(component, absX, absY)
  639. local args = {key=keyCode}
  640. for i, handler in pairs(component.OnKeyPressed) do
  641. if (handler(component, time, delta, args)) then
  642. return
  643. end
  644. end
  645. end
  646.  
  647. function app.OnCharPress(time, delta, ch)
  648.  
  649. end
  650.  
  651. -- Application Tick. This may be called extremely frequently during events
  652. -- such as mouse dragging, key events, etc. But this is also called during the
  653. -- application timer event, which is classed as a render tick
  654. --
  655. -- The time parameter is the os time (seconds since the computer started)
  656. --
  657. -- The delta parameter is the time since the last tick. Usually this is the
  658. -- tick rate interval, but it may be exactly 1 server tick if something like
  659. -- a mouse drag event came in
  660. local function OnApplicationMessage(time, delta, eventType, p1, p2, p3, p4, p5)
  661. if (eventType == "timer" and p1 == AnimTimerId) then
  662. app.SetupTimer(AnimIntervalSecs)
  663. -- tick animations
  664. for k, obj in pairs(APP_ROOT_OBJECTS) do
  665. app.DoAnimationTick(obj, time, delta)
  666. end
  667.  
  668. -- render application
  669. GDI.clearMonitor()
  670. app.RenderComponents(APP_ROOT_OBJECTS, 0, 0)
  671. GDI.drawText(1, 19, LastDebugMessage)
  672. else
  673. if (eventType == "mouse_click") then
  674. app.OnMouseClick(time, delta, p1, p2, p3)
  675. elseif (eventType == "mouse_drag") then
  676. app.OnMouseDrag(time, delta, p1, p2, p3)
  677. elseif (eventType == "key") then
  678. app.OnKeyPress(time, delta, p1)
  679. elseif (eventType == "char") then
  680. app.OnCharPress(time, delta, p1)
  681. end
  682. end
  683. end
  684.  
  685. local function AppMain()
  686. GDI.SetRenderTarget(term)
  687. -- load GUI components onto screen
  688. app.InsertComponent(nil, app.CreateButton(1, 1, 16, 3, "Copy Template", false))
  689. app.InsertComponent(nil, app.CreateButton(18, 4, 16, 3, "Paste Template", false))
  690. app.InsertComponent(nil, app.CreateTextBlock(1, 7, 16, 3, "eooeoe"))
  691.  
  692. -- Application event/message loop. no need to modify this!!!
  693. app.SetupTimer(AnimIntervalSecs)
  694. while true do
  695. local eType, p1, p2, p3, p4, p5 = os.pullEventRaw()
  696. local osTime = os.clock()
  697. if (eType == "terminate") then
  698. GDI.clearMonitor()
  699. GDI.drawText(1, 1, "Application Terminated", colours.white, colours.red)
  700. term.setCursorPos(1, 2)
  701. break
  702. end
  703.  
  704. OnApplicationMessage(osTime, osTime - AppLastTickTime, eType, p1, p2, p3, p4, p5)
  705. AppLastEventTime = osTime
  706. IsAnimationTick = false
  707. end
  708. end
  709.  
  710. --endregion
  711.  
  712. AppMain()
  713.  
  714. -- class MyButton {
  715. -- public:
  716. -- int id;
  717. -- char* text;
  718. --
  719. -- void setText(char* text) {
  720. -- this->text = text;
  721. -- if (text == nullptr) {
  722. -- this->id = -1;
  723. -- }
  724. -- }
  725. -- }
  726. --
  727. -- MyButton buttons[5];
  728. --
  729. -- void handleButton(MyButton* btn) {
  730. -- btn.id++;
  731. -- }
  732. --
  733. -- void main() {
  734. -- MyButton btn;
  735. -- btn.id = 24;
  736. -- handleButton(&btn);
  737. -- }
  738. --
  739. -- void onInputEvent(char* msg, void* p0, void* p1) {
  740. -- buttons[*(int*)p0].id = *(int*)p1
  741. -- buttons[*(int*)p0].setText((char*)"ok!")
  742. -- }
  743.  
  744. -- local MyButton = {}
  745. -- MyButton.__index = MyButton
  746. --
  747. -- function MyButton.new()
  748. -- local self = setmetatable({}, MyButton)
  749. -- self.id = 0
  750. -- self.text = nil
  751. -- return self
  752. -- end
  753. --
  754. -- function MyButton:setText(text)
  755. -- self.text = text
  756. -- if text == nil then
  757. -- self.id = -1
  758. -- end
  759. -- end
  760. --
  761. -- local buttons = {}
  762. -- for i = 1, 5 do
  763. -- buttons[i] = MyButton.new()
  764. -- end
  765. --
  766. -- function handleButton(btn)
  767. -- btn.id = btn.id + 1
  768. -- end
  769. --
  770. -- local function onInputEvent(msg, p0, p1)
  771. -- buttons[tonumber(p0)].id = tonumber(p1)
  772. -- buttons[tonumber(p0)]:setText("ok!")
  773. -- end
  774. --
  775. -- local btn = MyButton.new()
  776. -- btn.id = 24
  777. -- handleButton(btn)
  778. --
  779. -- onInputEvent(nil, 1, 2)
  780.  
Advertisement
Add Comment
Please, Sign In to add comment