ReDestroyDeR

graph_api.lua

Oct 11th, 2021 (edited)
525
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.82 KB | None | 0 0
  1. -----------------
  2. --Graphical API--
  3. -----------------
  4.  
  5. ACTIVE_BUTTON_FADEOUT_DELAY_SECONDS = 0.5
  6.  
  7. function writeButtonText(button_window, text, width, height)
  8.     button_window.setCursorPos(1, height / 2 + 1)
  9.     button_window.write(text)
  10. end
  11.  
  12. function createButton(text, x_topleft, y_topleft, width, height, toggle, text_color, color, color_active, on_click, parent_window, custom_on_click_data)
  13.     -- Create button window and initialize default values
  14.     local button_window = window.create(parent_window, x_topleft, y_topleft, width, height)
  15.     assert(color ~= nil)
  16.     color_active = (color_active == nil and color or color_active)
  17.     button_window.setBackgroundColor(color)
  18.     button_window.setTextColor(text_color)
  19.     button_window.clear()
  20.  
  21.     -- Write text in the Center
  22.     writeButtonText(button_window, text, width, height)
  23.     button_window.setCursorBlink(false)
  24.  
  25.     return
  26.     {
  27.         window=button_window,
  28.         toggle=toggle,
  29.         color=color,
  30.         color_active=color_active,
  31.         on_click=on_click,
  32.         text=text,
  33.         custom=custom_on_click_data,
  34.         available=true
  35.     }
  36. end
  37.  
  38.  
  39. local buttons = {}
  40.  
  41. function registerButton(button)
  42.     table.insert(buttons, button)
  43. end
  44.  
  45. function createDirectionButtons(x_topleft, y_topleft, cell_size_x, cell_size_y, color, color_active, event_name, parent_window)
  46.     local data = {
  47.         selected="NONE",
  48.         status_window=window.create(parent_window, x_topleft + cell_size_x + 1, y_topleft + cell_size_y * 3 + 3, cell_size_x * 3, cell_size_y)
  49.     }
  50.  
  51.     data["status_window"].setBackgroundColor(parent_window.getBackgroundColor())
  52.     data["status_window"].clear()
  53.  
  54.     local function printStatus(window, selected)
  55.         window.setCursorPos(1, 1)
  56.         window.clearLine()
  57.         window.write(selected)
  58.     end
  59.  
  60.     printStatus(data["status_window"], data["selected"]:upper())
  61.  
  62.     local function action(window, payload)
  63.         local data = payload["data"]
  64.         local previous_tag = data["selected"]
  65.         local selected = data[previous_tag]
  66.  
  67.         if (selected ~= nil) then
  68.             selected["window"].setBackgroundColor(color)
  69.             selected["window"].clear()
  70.         end
  71.  
  72.         if previous_tag == payload["side"] then
  73.             data["selected"] = "NONE"
  74.             printStatus(data["status_window"], "NONE")
  75.         else
  76.             data["selected"] = payload["side"]
  77.             printStatus(data["status_window"], data["selected"]:upper())
  78.         end
  79.  
  80.         os.queueEvent(event_name, previous_tag, data["selected"]:upper(), selected)
  81.     end
  82.  
  83.     for y_cell = 1, 3, 1 do
  84.         for x_cell = 1, 3, 1 do
  85.             if (x_cell == 2 and y_cell == 1) then
  86.                 data["TOP"] = createButton("", x_topleft + cell_size_x + 1, y_topleft, cell_size_x, cell_size_y, true, color, color, color_active, action, parent_window, {data=data, side="TOP"})
  87.                 else if (x_cell == 1 and y_cell == 2) then
  88.                     data["LEFT"] = createButton("", x_topleft, y_topleft + cell_size_y + 1, cell_size_x, cell_size_y, true, color, color, color_active, action, parent_window, {data=data, side="LEFT"})
  89.                     else if (x_cell == 2 and y_cell == 2) then
  90.                         data["FRONT"] = createButton("", x_topleft + cell_size_x + 1, y_topleft + cell_size_y + 1, cell_size_x, cell_size_y, true, color, color, color_active, action, parent_window, {data=data, side="FRONT"})
  91.                         else if (x_cell == 3 and y_cell == 2) then
  92.                             data["RIGHT"] = createButton("", x_topleft + cell_size_x * 2 + 2, y_topleft + cell_size_y + 1, cell_size_x, cell_size_y, true, color, color, color_active, action, parent_window, {data=data, side="RIGHT"})
  93.                             else if (x_cell == 1 and y_cell == 3) then
  94.                                 data["BACK"] = createButton("", x_topleft, y_topleft + cell_size_y * 2 + 2, cell_size_x, cell_size_y, true, color, color, color_active, action, parent_window, {data=data, side="BACK"})
  95.                                 else if (x_cell == 2 and y_cell == 3) then
  96.                                     data["BOTTOM"] = createButton("", x_topleft + cell_size_x + 1, y_topleft + cell_size_y * 2 + 2, cell_size_x, cell_size_y, true, color, color, color_active, action, parent_window, {data=data, side="BOTTOM"})
  97.                                 end
  98.                             end
  99.                         end
  100.                     end
  101.                 end
  102.             end
  103.         end
  104.     end
  105.  
  106.     for key, value in pairs(data) do
  107.         if (key ~= "selected" and key ~= "status_window") then
  108.             registerButton(value)
  109.         end
  110.     end
  111.  
  112.     return data;
  113. end
  114.  
  115. -- Event handlers
  116.  
  117. local active_button_timer_ids = {}
  118.  
  119. function simulateButtonClick(button)
  120.     os.queueEvent("mouse_click", 1, button["window"].getPosition())
  121. end
  122.  
  123. function processButtonClick(button)
  124.     window = button["window"]
  125.     -- Change Background color of a button
  126.     if button["toggle"] then
  127.         if window.getBackgroundColor() == button["color"] then
  128.             window.setBackgroundColor(button["color_active"])
  129.         else
  130.             window.setBackgroundColor(button["color"])
  131.         end
  132.     else
  133.         window.setBackgroundColor(button["color_active"])
  134.         local timer_id = os.startTimer(ACTIVE_BUTTON_FADEOUT_DELAY_SECONDS)
  135.         local data =
  136.         {
  137.             window=window,
  138.             color=button["color"],
  139.             text=button["text"]
  140.         }
  141.         table.insert(active_button_timer_ids, timer_id, data)
  142.     end
  143.  
  144.     button["on_click"](button["window"], button["custom"])
  145.     window.clear()
  146.     writeButtonText(window, button["text"], window.getSize())
  147. end
  148.  
  149. function buttonClickEventHandler(x, y)
  150.     -- Executing button on click function if there was a click on it
  151.     for _, button in ipairs(buttons) do
  152.         if button["available"] then
  153.             local window = button["window"]
  154.             x_tl, y_tl = window.getPosition()
  155.             x_tl = x_tl
  156.             y_tl = y_tl
  157.  
  158.             x_br, y_br = window.getSize()
  159.             x_br = x_br + x_tl
  160.             y_br = y_br + y_tl
  161.  
  162.             if (x >= x_tl and x <= x_br and y >= y_tl and y <= y_br) then
  163.                 processButtonClick(button)
  164.                 break
  165.             end
  166.         end
  167.     end
  168. end
  169.  
  170. function mouseClickEventHandler(raw_event)
  171.     local event, mouse_button, x, y = raw_event[1], raw_event[2], raw_event[3], raw_event[4]
  172.  
  173.     if (mouse_button == 1) then
  174.         buttonClickEventHandler(x, y)
  175.     end
  176. end
  177.  
  178. function keyEventHandler(raw_event)
  179.     local event, key, is_help = raw_event[1], raw_event[2], raw_event[3]
  180.  
  181.     if (key == keys.slash) then
  182.         exit()
  183.     end
  184. end
  185.  
  186. function timerEventHandler(raw_event)
  187.     local event, id = raw_event[1], raw_event[2]
  188.  
  189.     if (active_button_timer_ids[id] ~= nil) then
  190.         local data = active_button_timer_ids[id]
  191.         data["window"].setBackgroundColor(data["color"])
  192.         data["window"].clear()
  193.         writeButtonText(data["window"], data["text"], data["window"].getSize())
  194.     end
  195. end
  196.  
  197. function eventHandlerPipeline(externalEventHandler)
  198.     while true do
  199.         local eventData = {os.pullEvent()}
  200.         local event = eventData[1]
  201.        
  202.         if event == "mouse_click" then
  203.             mouseClickEventHandler(eventData)
  204.             else if event == "key" then
  205.                 keyEventHandler(eventData)
  206.                 else if event == "timer" then
  207.                     timerEventHandler(eventData)
  208.                     externalEventHandler(event, eventData)
  209.                 else
  210.                     externalEventHandler(event, eventData)
  211.                 end
  212.             end
  213.         end
  214.     end
  215. end
  216.  
  217. -----------
  218. --CLASSES--
  219. -----------
  220.  
  221. MutableTextArea = {}
  222.  
  223. function MutableTextArea:new(parent_window, x_topleft, y_topleft, width, height, background_color, text_color, shadow, shadow_color, text)
  224.     local private = {}
  225.         if (shadow) then
  226.             private.shadow_window = window.create(parent_window, x_topleft - 1, y_topleft - 1, width, height)
  227.             private.shadow_window.setBackgroundColor(shadow_color)
  228.             private.shadow_window.clear()
  229.         end
  230.  
  231.         private.window = window.create(parent_window, x_topleft, y_topleft, width, height)
  232.         private.window.setBackgroundColor(background_color)
  233.         private.window.setTextColor(text_color)
  234.         private.window.clear()
  235.  
  236.     local public = {}
  237.         public.background_color = background_color or colors.black
  238.         public.text_color = text_color or colors.white
  239.         public.shadow = shadow or false
  240.         public.shadow_color = shadow_color
  241.         public.text = text or ""
  242.  
  243.         function public:setBackgroundColor(color)
  244.             public.background_color = color
  245.         end
  246.  
  247.         function public:setTextColor(color)
  248.             public.text_color = color
  249.         end
  250.  
  251.         function public:setShadow(bool)
  252.             public.shadow = bool
  253.             private:updateShadow()
  254.         end
  255.  
  256.         function public:setShadowColor(color)
  257.             public.shadow_color = color
  258.             private:updateShadow()
  259.         end
  260.  
  261.         function public:setText(text)
  262.             public.text = text
  263.         end
  264.  
  265.         function public:refresh()
  266.             private:updateWindow()
  267.         end
  268.  
  269.         function private:updateWindow()
  270.             private.window.setTextColor(public.text_color)
  271.             private.window.setBackgroundColor(public.background_color)
  272.             private.window.clear()
  273.             term.redirect(private.window)
  274.             term.setCursorPos(1, 1)
  275.             print(public.text)
  276.             term.redirect(parent_window)
  277.         end
  278.  
  279.         function private:updateShadow()
  280.             private.shadow_window.setBackgroundColor(public.shadow_color)
  281.             private.shadow_window.clear()
  282.         end
  283.  
  284.     setmetatable(public, self)
  285.     self.__index = self; return public
  286. end
  287.  
  288.  
  289. return {
  290.     writeButtonText = writeButtonText,
  291.     createButton = createButton,
  292.     registerButton = registerButton,
  293.     createDirectionButtons = createDirectionButtons,
  294.     eventHandlerPipeline = eventHandlerPipeline,
  295.     MutableTextArea = MutableTextArea,
  296.     buttons = buttons,
  297.     simulateButtonClick = simulateButtonClick
  298. }
  299.  
Advertisement
Add Comment
Please, Sign In to add comment