Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- callUpdate = true
- bgColor = colors.black
- space = term
- objects = {}
- buttons = {}
- function centerText(w, ln)
- return math.ceil((w - ln) / 2)
- end
- --класс текста
- Text={}
- function Text:new(t, x, y, col)
- local obj = {text = t or "", x = x or 0, y = y or 0, color = col or colors.white}
- function obj:blit()
- table.insert(objects, self)
- end
- function obj:draw()
- space.setCursorPos(obj.x, obj.y)
- space.setTextColor(obj.color)
- space.write(obj.text)
- end
- function obj:getPos()
- return self.x, self.y
- end
- function obj:setPos(x, y)
- obj.x = x
- obj.y = y
- end
- function obj:setColor(col)
- obj.color = col
- end
- setmetatable(obj, self)
- self.__index = self
- return obj
- end
- --класс кнопки
- Button={}
- function Button:new(x, y, endx, endy, col, prcol, text, func)
- local obj = {}
- obj.x = x or 0
- obj.y = y or 0
- obj.endx = endx or 0
- obj.endy = endy or 0
- obj.color = col or colors.white
- obj.prColor = prcol or obj.color
- obj.buffCol = obj.color
- obj.pressed = false
- obj.func = func or function() end
- obj.text = text or ""
- function obj:blit()
- table.insert(objects, self)
- table.insert(buttons, self)
- end
- function obj:draw()
- paintutils.drawFilledBox(obj.x,obj.y,obj.endx,obj.endy,obj.color)
- if string.len(obj.text) <= (obj.endx - obj.x) and string.len(obj.text) ~= 0 then
- space.setBackgroundColor(obj.color)
- space.setCursorPos(obj.x + centerText(obj.endx - obj.x, string.len(obj.text)), obj.y + centerText(obj.endy - obj.y, 0))
- space.setTextColor(colors.white)
- space.write(obj.text)
- space.setBackgroundColor(bgColor)
- end
- end
- function obj:getPos()
- return self.x, self.y
- end
- function obj:setPos(x, y)
- obj.endx = x + obj.endx - obj.x
- obj.endy = y + obj.y
- obj.x = x
- obj.y = y
- end
- function obj:setColor(col)
- --paintutils.drawFilledBox(obj.x,obj.y,obj.endx,obj.endy,col)
- callUpdate = true
- end
- setmetatable(obj, self)
- self.__index = self
- return obj
- end
- --Луп функция
- function mainloop()
- while true do
- if callUpdate == true then
- space.clear()
- for i = 1, table.getn(objects) do
- objects[i].draw()
- end
- callUpdate = false
- space.setBackgroundColor(bgColor)
- end
- parallel.waitForAny(
- function ()
- local mdevent, mdbutton, mdx, mdy = os.pullEvent("mouse_click")
- if mdbutton == 1 then
- for i = 1, table.getn(buttons) do
- o = buttons[i]
- if mdx >= o.x and mdy >= o.y and mdx <= o.endx and mdy <= o.endy then
- buttons[i].color = buttons[i].prColor
- callUpdate = true
- if buttons[i].pressed == false then
- buttons[i].func()
- end
- buttons[i].pressed = true
- end
- end
- end
- end,
- function()
- local muevent, mubutton, mux, muy = os.pullEvent("mouse_up")
- if mubutton == 1 then
- for i = 1, table.getn(buttons) do
- o = buttons[i]
- buttons[i].color = buttons[i].buffCol
- buttons[i].pressed = false
- callUpdate = true
- end
- end
- end
- )
- os.sleep(0);
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement