Advertisement
astral17

AGUI.lua

Jul 17th, 2018
351
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 23.17 KB | None | 0 0
  1. local gpu = require("component").gpu
  2. local event = require("event")
  3. local unicode = require("unicode")
  4.  
  5. local IsInited = false
  6. local gui = {}
  7. local elements -- = {}
  8. local timers -- = {}
  9.  
  10. gui.Version = "0.6"
  11.  
  12. -- function gui.getVersion(requestVersion)
  13.   -- if type(goodVersion) ~= "table" then
  14.     -- return false
  15.   -- end
  16.   -- if V
  17. -- end
  18.  
  19. --gui.Elements = elements
  20.  
  21. function gui.NewIsClicked(x1, y1, x2, y2)
  22.   return function(x, y) return x1 <= x and x <= x2 and y1 <= y and y <= y2;end
  23. end
  24.  
  25. function gui:IsClicked(x, y)
  26.   return self.X <= x and x <= self.X + self.Width - 1 and self.Y <= y and y <= self.Y + self.Height - 1
  27. end
  28.  
  29. function gui.GetCenterSegment(l, r, width)
  30.   return math.floor((l + r - (width or 0) + 1) / 2)
  31. end
  32.  
  33. function gui.GetCenter(sX, sWidth, width)
  34.   return sX + math.floor((sWidth - (width or 0)) / 2)
  35.   -- return gui.GetCenterSegment(sX, sX + sWidth - 1, width)
  36. end
  37.  
  38. function gui.gc()
  39.   for index, element in pairs(elements) do
  40.     if type(element) == "table" and element.Garbage then
  41.       elements[index] = nil
  42.     end
  43.   end
  44. end
  45.  
  46. function gui.OnClick(_, _, x, y, button, name)
  47.   gui.gc()
  48.   for index, element in pairs(elements) do
  49.     if type(element) == "table" and element.Enabled and type(element.OnClick) == "function" then
  50.       -- element:OnClick(x, y)
  51.       pcall(element.OnClick, element, x, y, button, name)
  52.     end
  53.   end
  54.  
  55.   for index, element in pairs(elements) do
  56.     if type(element) == "table" and element.Enabled and type(element.IsClicked) == "function" and type(element.OnElementClick) == "function" and element:IsClicked(x, y) then
  57.       -- element:OnElementClick(x - element.X + 1, y - element.Y + 1)
  58.       pcall(element.OnElementClick, element, x - element.X + 1, y - element.Y + 1, button, name)
  59.       break
  60.     end
  61.   end
  62. end
  63.  
  64. function gui.OnKeyDown(_, _, key1, key2, name)
  65.   gui.gc()
  66.   for index, element in pairs(elements) do
  67.     if type(element) == "table" and element.Enabled and element.Focused and type(element.OnKeyDown) == "function" then
  68.       -- element:OnKeyDown(key1, key2, name)
  69.       pcall(element.OnKeyDown, element, key1, key2, name)
  70.     end
  71.   end
  72. end
  73.  
  74. function gui.OnKeyUp(_, _, key1, key2, name)
  75.   gui.gc()
  76.   for index, element in pairs(elements) do
  77.     if type(element) == "table" and element.Enabled and element.Focused and type(element.OnKeyUp) == "function" then
  78.       -- element:OnKeyDown(key1, key2, name)
  79.       pcall(element.OnKeyUp, element, key1, key2, name)
  80.     end
  81.   end
  82. end
  83.  
  84. -- OnScroll
  85. -- OnDrag
  86. -- OnDrop
  87. -- OnClipboard
  88. -- OnScreenResized
  89.  
  90. function gui.Init()
  91.   if IsInited then
  92.     return false
  93.   end
  94.   IsInited = true
  95.   elements = {}
  96.   timers = {}
  97.   event.listen("touch", gui.OnClick)
  98.   event.listen("key_down", gui.OnKeyDown)
  99.   event.listen("key_up", gui.OnKeyUp)
  100. end
  101.  
  102. function gui.Destroy()
  103.   event.ignore("touch", gui.OnClick)
  104.   event.ignore("key_down", gui.OnKeyDown)
  105.   event.ignore("key_up", gui.OnKeyUp)
  106.   IsInited = false
  107.   for index, timer in pairs(timers) do
  108.     event.cancel(timer)
  109.   end
  110.   timers = nil
  111.   for index, element in pairs(elements) do
  112.     if type(element) == "table" and type(element.Destroy) == "function" then
  113.       -- element:Destroy()
  114.       pcall(element.Destroy, element)
  115.     end
  116.   end
  117.   elements = nil
  118.   -- WorkingSpaceStack = nil
  119.   -- _G.guiDestroy = nil
  120. end
  121.  
  122. -- function gui.CreateWorkingSpace()
  123.   -- local LocalGUI = {elements = nil}
  124.   -- return
  125. -- end
  126.  
  127. -- function _G.guiDestroy()
  128.   -- gui.Destroy()
  129. -- end
  130.  
  131. function gui.Create(element)
  132.   if type(element) ~= "table" then
  133.     return {}
  134.   end
  135.   if type(element.Destroy) ~= "function" then
  136.     element.Destroy = function(self)
  137.       if type(self.Disable) == "function" then
  138.         self:Disable()
  139.       end
  140.       self.Garbage = true
  141.     end
  142.   end
  143.   if type(element.Enabled) ~= "boolean" then
  144.     element.Enabled = false
  145.   end
  146.  
  147.   if type(element.Enable) ~= "function" then
  148.     element.Enable = function(self) self.Enabled = true; return self end
  149.   end
  150.  
  151.   if type(element.Disable) ~= "function" then
  152.     element.Disable = function(self) self.Enabled = false; return self end
  153.   end
  154.  
  155.   if type(element.Init) ~= "function" then
  156.     element.Init = function(self) return self end
  157.   end
  158.  
  159.   if type(element.Paint) ~= "function" then
  160.     element.Paint = function(self)
  161.       if type(self.OnPaint) == "function" then
  162.         self:OnPaint()
  163.       end
  164.       return self
  165.     end
  166.   end
  167.  
  168.   if type(element.Modify) ~= "function" then
  169.     element.Modify = function(self, tbl)
  170.       for index, element in pairs(tbl) do
  171.         self[index] = element
  172.       end
  173.       return self
  174.     end
  175.   end
  176.  
  177.   if type(element.X) ~= "number" then
  178.     element.X = 1
  179.   end
  180.  
  181.   if type(element.Y) ~= "number" then
  182.     element.Y = 1
  183.   end
  184.  
  185.   if type(element.Width) ~= "number" then
  186.     element.Width = 1
  187.   end
  188.  
  189.   if type(element.Height) ~= "number" then
  190.     element.Height = 1
  191.   end
  192.  
  193.   if type(element.Focused) ~= "boolean" then
  194.     element.Focused = false
  195.   end
  196.  
  197.   table.insert(elements, element)
  198.   return element
  199. end
  200.  
  201. -- Group --
  202.  
  203. function gui.CreateGroup(group, elements, x, y, width, height)
  204.   if type(group) ~= "table" then
  205.     group = {}
  206.   end
  207.  
  208.   group.X = x or 1
  209.   group.Y = y or 1
  210.   group.Width = width or 1
  211.   group.Height = height or 1
  212.  
  213.   if type(group.Init) ~= "function" then
  214.     group.Init = function(self)
  215.       for index, element in pairs(self.Elements) do
  216.         if type(element) == "table" and type(element.Init) == "function" then
  217.           element.X = element.X + self.X - 1
  218.           element.Y = element.Y + self.Y - 1
  219.           element.Parent = self
  220.           element:Init()
  221.         end
  222.       end
  223.       return self
  224.     end
  225.   end
  226.  
  227.   if type(group.Enabled) ~= "boolean" then
  228.     group.Enabled = false
  229.   end
  230.  
  231.   if type(group.Paint) ~= "function" then
  232.     group.Paint = function(self)
  233.       if type(self.OnPaint) == "function" then
  234.         self:OnPaint()
  235.       end
  236.       for index, element in pairs(self.Elements) do
  237.         if type(element) == "table" and type(element.Paint) == "function" then
  238.           element:Paint()
  239.         end
  240.       end
  241.       return self
  242.     end
  243.   end
  244.  
  245.   if type(group.Enable) ~= "function" then
  246.     group.Enable = function(self)
  247.       for index, element in pairs(self.Elements) do
  248.         if type(element) == "table" and type(element.Enable) == "function" then
  249.           element:Enable()
  250.         end
  251.       end
  252.       self.Enabled = true
  253.       return self
  254.     end
  255.   end
  256.  
  257.   if type(group.Disable) ~= "function" then
  258.     group.Disable = function(self)
  259.       for index, element in pairs(self.Elements) do
  260.         if type(element) == "table" and type(element.Disable) == "function" then
  261.           element:Disable()
  262.         end
  263.       end
  264.       self.Enabled = false
  265.       return self
  266.     end
  267.   end
  268.  
  269.   if type(group.Destroy) ~= "function" then
  270.     group.Destroy = function(self)
  271.       for index, element in pairs(self.Elements) do
  272.         if type(element) == "table" and type(element.Destroy) == "function" then
  273.           element:Destroy()
  274.         end
  275.       end
  276.       self = nil
  277.     end
  278.   end
  279.  
  280.   group.Elements = elements
  281.   if type(group.Elements) ~= "table" then
  282.     group.Elements = {}
  283.   end
  284.   return group
  285. end
  286.  
  287. --~Group~--
  288.  
  289. gui.backend = {}
  290. -- Text --
  291. gui.backend.Text = {}
  292. local text = gui.backend.Text
  293.  
  294. function text:Paint()
  295.   gpu.setBackground(self.BackColor)
  296.   gpu.setForeground(self.TextColor)
  297.   if self.Width > 0 then
  298.     -- gpu.set(self.X + math.floor(self.Width / 2) - math.ceil(unicode.len(self.Text) / 2), self.Y, self.Text)
  299.     gpu.set(self.X + math.floor((self.Width - self.PaintedTextLen) / 2), self.Y, string.rep(" ", self.PaintedTextLen))
  300.     gpu.set(self.X + math.floor((self.Width - unicode.len(self.Text)) / 2), self.Y, self.Text)
  301.   else
  302.     gpu.set(self.X, self.Y, string.rep(" ", self.PaintedTextLen))
  303.     gpu.set(self.X, self.Y, self.Text)
  304.   end
  305.   self.PaintedTextLen = unicode.len(self.Text)
  306.   gpu.setBackground(0x000000)
  307.   gpu.setForeground(0xffffff)
  308.   return self
  309. end
  310.  
  311. function text:Create(x, y, width, text)
  312.   return gui.Create{
  313.     X = x,
  314.     Y = y,
  315.     Width = width or 0,
  316.     Paint = self.Paint,
  317.     Text = text,
  318.     PaintedTextLen = 0,
  319.     BackColor = 0x000000,
  320.     TextColor = 0xffffff,
  321.   }
  322. end
  323.  
  324. -- Button --
  325. gui.backend.Button = {}
  326. local button = gui.backend.Button
  327.  
  328. function button:Paint(pressed)
  329.   gpu.setBackground(self.BackColor)
  330.   gpu.setForeground(self.TextColor)
  331.   gpu.fill(self.X, self.Y, self.Width, self.Height, " ")
  332.   gpu.set(self.X + math.floor(self.Width / 2) - math.ceil(unicode.len(self.Text) / 2), self.Y + math.floor(self.Height / 2), self.Text)
  333.   gpu.setBackground(0x000000)
  334.   gpu.setForeground(0xffffff)
  335.   return self
  336. end
  337.  
  338. function button:Create(x, y, width, height, text, onclick)
  339.   return gui.Create{
  340.     OnElementClick = onclick,
  341.     Paint = self.Paint,
  342.     IsClicked = gui.IsClicked,
  343.     Text = text,
  344.     X = x,
  345.     Y = y,
  346.     Width = width,
  347.     Height = height,
  348.     BackColor = 0x666666,
  349.     TextColor = 0xffffff,
  350.   }
  351. end
  352.  
  353. -- TextBox --
  354. gui.backend.TextBox = {}
  355. local textbox = gui.backend.TextBox
  356.  
  357. function textbox:Init()
  358.   self.Timer = event.timer(0.5, function()
  359.     if self.Enable and self.Focused then
  360.       self.Blink = not self.Blink
  361.       self:Paint()
  362.     end
  363.   end, math.huge)
  364.   table.insert(timers, self.Timer)
  365. end
  366.  
  367. function textbox:Destroy()
  368.   event.cancel(self.Timer)
  369. end
  370.  
  371. function textbox:OnClick(x, y)
  372.   self.Focused = false
  373.   self.Blink = false
  374.   self:Paint()
  375. end
  376.  
  377. function textbox:OnElementClick(x, y)
  378.   self.Focused = true
  379.   if self.Focused then
  380.     self.CursorPosition = math.min(math.max(1, x), unicode.len(self.Text) + 1)
  381.     self:Paint()
  382.   end
  383.   return self
  384. end
  385.  
  386. function textbox:OnKeyDown(key1, key2)
  387.   if key1 == 8 then -- BackSpace
  388.     if self.CursorPosition > 1 then
  389.       self.Text = unicode.sub(self.Text, 1, self.CursorPosition - 2) .. unicode.sub(self.Text, self.CursorPosition, unicode.len(self.Text))
  390.       self.CursorPosition = self.CursorPosition - 1
  391.     end
  392.   elseif key2 == 211 then -- Delete
  393.     if self.CursorPosition <= unicode.len(self.Text) then
  394.       self.Text = unicode.sub(self.Text, 1, self.CursorPosition - 1) .. unicode.sub(self.Text, self.CursorPosition + 1, unicode.len(self.Text))
  395.     end
  396.   elseif key2 == 199 then -- Home
  397.     self.CursorPosition = 1
  398.   elseif key2 == 207 then -- End
  399.     self.CursorPosition = unicode.len(self.Text) + 1
  400.   elseif key2 == 203 then -- Left
  401.     self.CursorPosition = math.max(self.CursorPosition - 1, 1)
  402.   elseif key2 == 205 then -- Right
  403.     self.CursorPosition = math.min(self.CursorPosition + 1, unicode.len(self.Text) + 1)
  404.   elseif key1 ~= 0 and (not self.AvailableChars or self.AvailableChars:find(unicode.char(key1))) then
  405.     self.Text = unicode.sub(self.Text, 1, self.CursorPosition - 1) .. unicode.char(key1) .. unicode.sub(self.Text, self.CursorPosition, unicode.len(self.Text))
  406.     self.CursorPosition = self.CursorPosition + 1
  407.   end
  408.   self:Paint()
  409. end
  410.  
  411. function textbox:Paint()
  412.   gpu.setBackground(self.BackColor)
  413.   gpu.setForeground(self.TextColor)
  414.   gpu.set(self.X, self.Y, string.rep(" ", self.Width))
  415.   gpu.set(self.X, self.Y, unicode.sub(self.Text, 1, self.Width)) -- TODO normal cut
  416.   if self.Blink and self.CursorPosition <= self.Width then
  417.     gpu.setBackground(self.TextColor)
  418.     gpu.setForeground(self.BackColor)
  419.     gpu.set(self.X + self.CursorPosition - 1, self.Y, self.CursorPosition <= #self.Text and unicode.sub(self.Text, self.CursorPosition, self.CursorPosition) or " ")
  420.   end
  421.  
  422.   gpu.setBackground(0x000000)
  423.   gpu.setForeground(0xffffff)
  424.   return self
  425. end
  426.  
  427. function textbox:Create(x, y, width, text, availableChars)
  428.  
  429.   return gui.Create{
  430.     Init = self.Init,
  431.     Destroy = self.Destroy,
  432.     OnClick = self.OnClick,
  433.     OnElementClick = self.OnElementClick,
  434.     OnKeyDown = self.OnKeyDown,
  435.     Paint = self.Paint,
  436.     IsClicked = gui.IsClicked,
  437.     Text = text,
  438.     AvailableChars = availableChars,
  439.     CursorPosition = 0,
  440.     BackColor = 0x333333,
  441.     TextColor  = 0xffffff,
  442.     X = x,
  443.     Y = y,
  444.     Width = width,
  445.     Height = 1,
  446.   }
  447. end
  448.  
  449. -- CheckBox --
  450.  
  451. gui.backend.CheckBox = {}
  452. local checkbox = gui.backend.CheckBox
  453.  
  454. function checkbox:Init()
  455.   -- print("O")os.sleep(1)
  456.   if self.Width == 0 then
  457.     -- print("!")os.sleep(1)
  458.     self.Width = 4 + unicode.len(self.Text)
  459.   end
  460.   -- print(self.Width)os.sleep(1)
  461. end
  462.  
  463. function checkbox:Paint()
  464.   gpu.setBackground(self.BackColor)
  465.   gpu.setForeground(self.TextColor)
  466.  
  467.   local box = "[ ] "
  468.   if self.Checked then
  469.     box = "[X] "
  470.   end
  471.   gpu.set(self.X, self.Y, unicode.sub(box .. self.Text, 1, self.Width) .. string.rep(" ", self.Width - box:len() - self.Text:len()))
  472.  
  473.   gpu.setBackground(0x000000)
  474.   gpu.setForeground(0xffffff)
  475.   return self
  476. end
  477.  
  478. function checkbox:OnElementClick(x, y)
  479.   self.Checked = not self.Checked
  480.   if type(self.OnCheckedChanged) == "function" then
  481.     pcall(self.OnCheckedChanged, self)
  482.   end
  483.   self:Paint()
  484. end
  485.  
  486. function checkbox:Create(x, y, width, text, checked)
  487.   return gui.Create{
  488.     Init = self.Init,
  489.     IsClicked = gui.IsClicked,
  490.     OnElementClick = self.OnElementClick,
  491.     -- OnCheckedChanged
  492.     Paint = self.Paint,
  493.     X = x,
  494.     Y = y,
  495.     Width = width,
  496.     Text = text,
  497.     BackColor = 0x000000, -- 0x333333,
  498.     TextColor  = 0xffffff,
  499.     Checked = checked or false,
  500.   }
  501. end
  502.  
  503. -- RadioButton --
  504.  
  505. gui.backend.RadioButton = {}
  506. local radiobutton = gui.backend.RadioButton
  507.  
  508. function radiobutton:Paint()
  509.   gpu.setBackground(self.BackColor)
  510.   gpu.setForeground(self.TextColor)
  511.  
  512.   local box = "( ) "
  513.   if self.Checked then
  514.     box = "(X) "
  515.   end
  516.   gpu.set(self.X, self.Y, unicode.sub(box .. self.Text, 1, self.Width) .. string.rep(" ", self.Width - box:len() - self.Text:len()))
  517.  
  518.   gpu.setBackground(0x000000)
  519.   gpu.setForeground(0xffffff)
  520.   return self
  521. end
  522.  
  523. function radiobutton:OnAnotherChecked()
  524.   self.Checked = false
  525. end
  526.  
  527. function radiobutton:OnElementClick(x, y)
  528.   self.Checked = true
  529.   if type(self.Parent) == "table" then
  530.     self.Parent:OnChecked(self.Name)
  531.   else
  532.     self:Paint()
  533.   end
  534. end
  535.  
  536. function radiobutton:Create(x, y, width, text)
  537.   return gui.Create{
  538.     IsClicked = gui.IsClicked,
  539.     OnElementClick = self.OnElementClick,
  540.     OnAnotherChecked = self.OnAnotherChecked,
  541.     Paint = self.Paint,
  542.     X = x,
  543.     Y = y,
  544.     Width = width,
  545.     Text = text,
  546.     BackColor = 0x000000, -- 0x333333,
  547.     TextColor  = 0xffffff,
  548.     Checked = true,
  549.   }
  550. end
  551.  
  552. -- RadioGroup --
  553.  
  554. gui.backend.RadioGroup = {}
  555. local radiogroup = gui.backend.RadioGroup
  556.  
  557. function radiogroup:Init()
  558.   if self.Checked == nil then
  559.     self.Checked = next(self.Elements)
  560.   end
  561.   for index, element in pairs(self.Elements) do
  562.     element.Parent = self
  563.     element.Name = index
  564.     if self.Checked ~= index then
  565.       element.Checked = false
  566.     end
  567.     element:Init()
  568.   end
  569.   return self
  570. end
  571.  
  572. function radiogroup:OnChecked(name)
  573.   self.Checked = name
  574.   for index, element in pairs(self.Elements) do
  575.     if type(element) == "table" then
  576.       if index ~= name then
  577.         element:OnAnotherChecked()
  578.       end
  579.       element:Paint()
  580.     end
  581.   end
  582. end
  583.  
  584. function radiogroup:Create(elements, checked)
  585.   return gui.CreateGroup({
  586.     Init = self.Init,
  587.     OnChecked = self.OnChecked,
  588.     Checked = checked,
  589.   }, elements)
  590. end
  591.  
  592. -- MessageBox --
  593. gui.backend.MessageBox = {}
  594. local msgbox = gui.backend.MessageBox
  595.  
  596. function msgbox:Init()
  597.   local widthScreen, heightScreen = gpu.getResolution()
  598.   widthScreen  = self.ScreenWidth  or widthScreen
  599.   heightScreen = self.ScreenHeight or heightScreen
  600.   self.Width = math.max(self.Width, unicode.len(self.Title) + 2, unicode.len(self.Text) + 2)
  601.   self.X = gui.GetCenter(1, widthScreen , self.Width)
  602.   self.Y = gui.GetCenter(1, heightScreen, self.Height)
  603.   -- self.X = math.floor((widthScreen  - self.Width ) / 2) + 1
  604.   -- self.Y = math.floor((heightScreen - self.Height) / 2) + 1
  605.   self.Elements = gui.CreateGroup(nil,
  606.   {
  607.     Title = gui.backend.Text:Create(self.X, self.Y + 1, self.Width, self.Title):Modify{TextColor = self.TitleColor, BackColor = self.BackColor},
  608.     Text = gui.backend.Text:Create(self.X, self.Y + 3, self.Width, self.Text):Modify{TextColor = self.TextColor, BackColor = self.BackColor},
  609.     Button = gui.backend.Button:Create(self.X, self.Y + 5, self.Width, 3, "OK", function()
  610.       if type(self.ButtonHandle) == "function" then
  611.         self:ButtonHandle()
  612.       end
  613.       self:Destroy()
  614.     end):Modify{BackColor = self.ButtonColor},
  615.   }):Init():Enable()
  616.   return self
  617. end
  618.  
  619. function msgbox:Destroy()
  620.   self.Elements:Destroy()
  621.   self.Garbage = true
  622. end
  623.  
  624. function msgbox:Paint()
  625.   -- local x,y=(cfg.MapWidth-6)/((cfg.SuperQuality and 2)or 1),(math.floor(cfg.MapHeight/2)-3)/((cfg.SuperQuality and 2)or 1)
  626.   gpu.setBackground(self.BackColor)
  627.   gpu.fill(self.X, self.Y, self.Width, 5, " ")
  628.   self.Elements:Paint()
  629.   return self
  630.  
  631. end
  632.  
  633. function msgbox:Create(title, text, buttonText)
  634.   return gui.Create{
  635.     Init = self.Init,
  636.     Destroy = self.Destroy,
  637.     Paint = self.Paint,
  638.     X = 0,
  639.     Y = 0,
  640.     Width = 10,
  641.     Height = 8,
  642.     Title = title or "Error",
  643.     Text = text or "oops...",
  644.     ButtonText = buttonText or "OK",
  645.     TitleColor = 0xff0000,
  646.     TextColor = 0x000000,
  647.     BackColor = 0xffffff,
  648.     ButtonColor = 0xff0000,
  649.     -- ButtonHandle
  650.   }
  651. end
  652.  
  653. function gui.Error(text)
  654.   return gui.backend.MessageBox:Create(nil, text):Init():Paint()
  655. end
  656.  
  657. -- Canvas --
  658. gui.backend.Canvas = {}
  659. local canvas = gui.backend.Canvas
  660.  
  661. function canvas.GetOffsetFunction(func, ...)
  662.   local offset = {...}
  663.   return function(...)
  664.     local args = {...}
  665.     for index, value in pairs(offset) do
  666.       args[index] = args[index] + value
  667.     end
  668.     return func(table.unpack(args))
  669.   end
  670. end
  671.  
  672. function canvas:Init()
  673.   self.get = canvas.GetOffsetFunction(gpu.get, self.X - 1, self.Y - 1)
  674.   self.set = canvas.GetOffsetFunction(gpu.set, self.X - 1, self.Y - 1)
  675.   self.fill = canvas.GetOffsetFunction(gpu.fill, self.X - 1, self.Y - 1)
  676.   self.copy = canvas.GetOffsetFunction(gpu.copy, self.X - 1, self.Y - 1)
  677. end
  678.  
  679. function canvas:Paint()
  680.   gpu.setBackground(self.BackColor)
  681.   self.fill(1, 1, self.Width, self.Height, " ")
  682.   if type(self.OnPaint) == "function" then
  683.     self:OnPaint()
  684.   end
  685. end
  686.  
  687. function canvas:Create(x, y, width, height)
  688.   return gui.Create{
  689.     Paint = self.Paint,
  690.     Init = self.Init,
  691.     IsClicked = gui.IsClicked,
  692.     X = x,
  693.     Y = y,
  694.     Width = width,
  695.     Height = height,
  696.     BackColor = 0x000000,
  697.   }
  698. end
  699.  
  700. -- Form --
  701. gui.backend.Form = {}
  702. local form = gui.backend.Form
  703.  
  704. function form:Init()
  705.   for _, element in pairs(self.Elements) do
  706.     -- element.X = element.X + self.X - 1
  707.     -- element.Y = element.Y + self.Y - 1
  708.     element.Parent = self
  709.     element:Init()
  710.   end
  711.   return self
  712. end
  713.  
  714. function form:Paint(NoClear)
  715.   if type(self.OnPaint) == "function" then
  716.     self:OnPaint()
  717.   end
  718.   if not NoClear then
  719.     gpu.setBackground(0x000000)
  720.     gpu.setForeground(0xffffff)
  721.     width, height = gpu.getResolution()
  722.     gpu.fill(1, 1, width, height, " ")
  723.     pcall(gpu.setResolution, self.Width, self.Height)
  724.   end
  725.   for index, element in pairs(self.Elements) do
  726.     element:Paint()
  727.   end
  728.   return self
  729. end
  730.  
  731. function form:Show()
  732.   if type(self.OnEnable) == "function" then
  733.     pcall(self.OnEnable, self)
  734.   end
  735.   for index, element in pairs(self.Elements) do
  736.     element:Enable()
  737.   end
  738.   self.Enabled = true
  739.   return self
  740. end
  741.  
  742. function form:Hide(NoClear)
  743.   if type(self.OnDisable) == "function" then
  744.     pcall(self.OnDisable, self)
  745.   end
  746.   for _, element in pairs(self.Elements) do
  747.     element:Disable()
  748.   end
  749.   if not NoClear then
  750.     gpu.setResolution(gpu.maxResolution())
  751.     gpu.setBackground(0x000000)
  752.     gpu.setForeground(0xffffff)
  753.     width, height = gpu.getResolution()
  754.     gpu.fill(1, 1, width, height, " ")
  755.   end
  756.   self.Enabled = false
  757.   return self
  758. end
  759.  
  760. function form:Destroy()
  761.   self:Disable()
  762.   for _, element in pairs(self.Elements) do
  763.     if type(element) == "table" and type(element.Destroy) == "function" then
  764.       element:Destroy()
  765.     end
  766.   end
  767.   self.Garbage = true
  768. end
  769.  
  770. function form:Create(width, height, elements)
  771.   return gui.Create{
  772.     Enable = self.Show,
  773.     Disable = self.Hide,
  774.     Destroy = self.Destroy,
  775.     Paint = self.Paint,
  776.     Init = self.Init,
  777.     Width = width,
  778.     Height = height,
  779.     Elements = elements,
  780.   }
  781. end
  782.  
  783. return gui --[[
  784. ---------------------------------------------------------
  785. gui.Init()
  786.  
  787. ScoresForm = gui.backend.Form:Create(20, 20,
  788.   {
  789.     gui.backend.TextBox:Create(1, 1, 10, "YourфText"),
  790.    
  791.     gui.backend.CheckBox:Create(1, 3, 20, "всё ок?"),
  792.    
  793.     TMP = gui.backend.RadioGroup:Create
  794.     {
  795.       qm = gui.backend.RadioButton:Create(1, 5, 20, "QWERTYмэн"),
  796.       q2 = gui.backend.RadioButton:Create(2, 6, 10, "Qмэн"),
  797.     },
  798.    
  799.     gui.backend.Button:Create(1, 18, 20, 3, "Back", function()
  800.       print(ScoresForm.Elements["TMP"].Checked) os.sleep(1)
  801.       ScoresForm:Disable(true)
  802.       MainForm:Enable():Paint()
  803.     end),
  804.   }
  805. ):Init()
  806.  
  807. SettingsForm = gui.backend.Form:Create(20, 17,
  808.   {
  809.     gui.backend.Text:Create(1, 1, 20, "Settings"),
  810.    
  811.     gui.backend.Text:Create(1, 3, nil, "Width"),
  812.     Width  = gui.backend.TextBox:Create(1, 4, 20, "0", "0123456789"),
  813.    
  814.     gui.backend.Text:Create(1, 6, nil, "Height"),
  815.     Height = gui.backend.TextBox:Create(1, 7, 20, "0", "0123456789"),
  816.    
  817.     SQ     = gui.backend.CheckBox:Create(1, 9, 20, "Super Quality"),
  818.    
  819.     gui.backend.Text:Create(1, 11, nil, "Map Generation Mode"),
  820.     Mode = gui.backend.RadioGroup:Create
  821.     {
  822.       gui.backend.RadioButton:Create(1, 12, 20, "Recursive"),
  823.       gui.backend.RadioButton:Create(1, 13, 20, "Hunt&Kill"),
  824.     },
  825.    
  826.     gui.backend.Button:Create(1, 15, 20, 3, "Back", function()
  827.       -- print(SettingsForm.Elements["Mode"].Checked) os.sleep(1)
  828.       -- print(SettingsForm.Elements["SQ"].Checked) os.sleep(1)
  829.       -- print(SettingsForm.Elements["Width"].Text) os.sleep(1)
  830.       SettingsForm:Disable(true)
  831.       MainForm:Enable():Paint()
  832.     end),
  833.   }
  834. ):Init()
  835.  
  836. MainForm = gui.backend.Form:Create(20, 15,
  837.   {
  838.     gui.backend.Button:Create(1, 1, 20, 3, "New Game", function() MainForm:Disable(true) quit = true end),
  839.     gui.backend.Button:Create(1, 5, 20, 3, "High Scores", function()
  840.       MainForm:Disable(true) --error("!")
  841.       ScoresForm:Enable():Paint()
  842.     end),
  843.     gui.backend.Button:Create(1, 9, 20, 3, "Settings", function()
  844.       MainForm:Disable(true)
  845.       SettingsForm:Enable():Paint()
  846.     end),
  847.     gui.backend.Button:Create(1, 13, 20, 3, "Exit", function() quit = true end),
  848.   }
  849. )
  850.  
  851. MainForm:Init():Enable():Paint()
  852. Err = gui.backend.MessageBox:Create():Init():Paint()
  853. gui.Error("GG!")
  854. -- gui.backend.MessageBox:Create("congratulation","You Win!"):Modify{ButtonHandle = function() print("HANDLED")  end}:Init():Paint()
  855.  
  856. eoe = event.onError
  857. event.onError = function(...)
  858.   quit = true
  859.   print("event error") os.sleep(1)
  860.   return eoe(...)
  861. end
  862.  
  863. quit = false
  864. pcall(function()
  865.   while not quit do
  866.     event.pull()
  867.   end
  868. end)
  869.  
  870. event.onError = eoe
  871. -- print(SettingsForm.Elements["Width"].Text, SettingsForm.Elements["Height"].Text) os.sleep(1)
  872.  
  873. gui.Destroy()
  874. --]]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement