Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -----------------
- --Graphical API--
- -----------------
- ACTIVE_BUTTON_FADEOUT_DELAY_SECONDS = 0.5
- function writeButtonText(button_window, text, width, height)
- button_window.setCursorPos(1, height / 2 + 1)
- button_window.write(text)
- end
- function createButton(text, x_topleft, y_topleft, width, height, toggle, text_color, color, color_active, on_click, parent_window, custom_on_click_data)
- -- Create button window and initialize default values
- local button_window = window.create(parent_window, x_topleft, y_topleft, width, height)
- assert(color ~= nil)
- color_active = (color_active == nil and color or color_active)
- button_window.setBackgroundColor(color)
- button_window.setTextColor(text_color)
- button_window.clear()
- -- Write text in the Center
- writeButtonText(button_window, text, width, height)
- button_window.setCursorBlink(false)
- return
- {
- window=button_window,
- toggle=toggle,
- color=color,
- color_active=color_active,
- on_click=on_click,
- text=text,
- custom=custom_on_click_data,
- available=true
- }
- end
- local buttons = {}
- function registerButton(button)
- table.insert(buttons, button)
- end
- function createDirectionButtons(x_topleft, y_topleft, cell_size_x, cell_size_y, color, color_active, event_name, parent_window)
- local data = {
- selected="NONE",
- 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)
- }
- data["status_window"].setBackgroundColor(parent_window.getBackgroundColor())
- data["status_window"].clear()
- local function printStatus(window, selected)
- window.setCursorPos(1, 1)
- window.clearLine()
- window.write(selected)
- end
- printStatus(data["status_window"], data["selected"]:upper())
- local function action(window, payload)
- local data = payload["data"]
- local previous_tag = data["selected"]
- local selected = data[previous_tag]
- if (selected ~= nil) then
- selected["window"].setBackgroundColor(color)
- selected["window"].clear()
- end
- if previous_tag == payload["side"] then
- data["selected"] = "NONE"
- printStatus(data["status_window"], "NONE")
- else
- data["selected"] = payload["side"]
- printStatus(data["status_window"], data["selected"]:upper())
- end
- os.queueEvent(event_name, previous_tag, data["selected"]:upper(), selected)
- end
- for y_cell = 1, 3, 1 do
- for x_cell = 1, 3, 1 do
- if (x_cell == 2 and y_cell == 1) then
- 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"})
- else if (x_cell == 1 and y_cell == 2) then
- 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"})
- else if (x_cell == 2 and y_cell == 2) then
- 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"})
- else if (x_cell == 3 and y_cell == 2) then
- 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"})
- else if (x_cell == 1 and y_cell == 3) then
- 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"})
- else if (x_cell == 2 and y_cell == 3) then
- 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"})
- end
- end
- end
- end
- end
- end
- end
- end
- for key, value in pairs(data) do
- if (key ~= "selected" and key ~= "status_window") then
- registerButton(value)
- end
- end
- return data;
- end
- -- Event handlers
- local active_button_timer_ids = {}
- function simulateButtonClick(button)
- os.queueEvent("mouse_click", 1, button["window"].getPosition())
- end
- function processButtonClick(button)
- window = button["window"]
- -- Change Background color of a button
- if button["toggle"] then
- if window.getBackgroundColor() == button["color"] then
- window.setBackgroundColor(button["color_active"])
- else
- window.setBackgroundColor(button["color"])
- end
- else
- window.setBackgroundColor(button["color_active"])
- local timer_id = os.startTimer(ACTIVE_BUTTON_FADEOUT_DELAY_SECONDS)
- local data =
- {
- window=window,
- color=button["color"],
- text=button["text"]
- }
- table.insert(active_button_timer_ids, timer_id, data)
- end
- button["on_click"](button["window"], button["custom"])
- window.clear()
- writeButtonText(window, button["text"], window.getSize())
- end
- function buttonClickEventHandler(x, y)
- -- Executing button on click function if there was a click on it
- for _, button in ipairs(buttons) do
- if button["available"] then
- local window = button["window"]
- x_tl, y_tl = window.getPosition()
- x_tl = x_tl
- y_tl = y_tl
- x_br, y_br = window.getSize()
- x_br = x_br + x_tl
- y_br = y_br + y_tl
- if (x >= x_tl and x <= x_br and y >= y_tl and y <= y_br) then
- processButtonClick(button)
- break
- end
- end
- end
- end
- function mouseClickEventHandler(raw_event)
- local event, mouse_button, x, y = raw_event[1], raw_event[2], raw_event[3], raw_event[4]
- if (mouse_button == 1) then
- buttonClickEventHandler(x, y)
- end
- end
- function keyEventHandler(raw_event)
- local event, key, is_help = raw_event[1], raw_event[2], raw_event[3]
- if (key == keys.slash) then
- exit()
- end
- end
- function timerEventHandler(raw_event)
- local event, id = raw_event[1], raw_event[2]
- if (active_button_timer_ids[id] ~= nil) then
- local data = active_button_timer_ids[id]
- data["window"].setBackgroundColor(data["color"])
- data["window"].clear()
- writeButtonText(data["window"], data["text"], data["window"].getSize())
- end
- end
- function eventHandlerPipeline(externalEventHandler)
- while true do
- local eventData = {os.pullEvent()}
- local event = eventData[1]
- if event == "mouse_click" then
- mouseClickEventHandler(eventData)
- else if event == "key" then
- keyEventHandler(eventData)
- else if event == "timer" then
- timerEventHandler(eventData)
- externalEventHandler(event, eventData)
- else
- externalEventHandler(event, eventData)
- end
- end
- end
- end
- end
- -----------
- --CLASSES--
- -----------
- MutableTextArea = {}
- function MutableTextArea:new(parent_window, x_topleft, y_topleft, width, height, background_color, text_color, shadow, shadow_color, text)
- local private = {}
- if (shadow) then
- private.shadow_window = window.create(parent_window, x_topleft - 1, y_topleft - 1, width, height)
- private.shadow_window.setBackgroundColor(shadow_color)
- private.shadow_window.clear()
- end
- private.window = window.create(parent_window, x_topleft, y_topleft, width, height)
- private.window.setBackgroundColor(background_color)
- private.window.setTextColor(text_color)
- private.window.clear()
- local public = {}
- public.background_color = background_color or colors.black
- public.text_color = text_color or colors.white
- public.shadow = shadow or false
- public.shadow_color = shadow_color
- public.text = text or ""
- function public:setBackgroundColor(color)
- public.background_color = color
- end
- function public:setTextColor(color)
- public.text_color = color
- end
- function public:setShadow(bool)
- public.shadow = bool
- private:updateShadow()
- end
- function public:setShadowColor(color)
- public.shadow_color = color
- private:updateShadow()
- end
- function public:setText(text)
- public.text = text
- end
- function public:refresh()
- private:updateWindow()
- end
- function private:updateWindow()
- private.window.setTextColor(public.text_color)
- private.window.setBackgroundColor(public.background_color)
- private.window.clear()
- term.redirect(private.window)
- term.setCursorPos(1, 1)
- print(public.text)
- term.redirect(parent_window)
- end
- function private:updateShadow()
- private.shadow_window.setBackgroundColor(public.shadow_color)
- private.shadow_window.clear()
- end
- setmetatable(public, self)
- self.__index = self; return public
- end
- return {
- writeButtonText = writeButtonText,
- createButton = createButton,
- registerButton = registerButton,
- createDirectionButtons = createDirectionButtons,
- eventHandlerPipeline = eventHandlerPipeline,
- MutableTextArea = MutableTextArea,
- buttons = buttons,
- simulateButtonClick = simulateButtonClick
- }
Advertisement
Add Comment
Please, Sign In to add comment