Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- Crée des boutons.
- --]]
- function create(_side,_x,_y,_text,_callback)
- btn = {
- side=_side,
- x=_x,
- y=_y,
- text=_text,
- callback=_callback,
- fgColor=colors.white,
- bgColor=colors.black
- }
- if _side == 'term' then
- btn.device = term
- else
- btn.device = peripheral.wrap(_side)
- end
- function btn:draw()
- x = self.x
- y = self.y
- text = self.text
- self.device.setCursorPos(x,y)
- for chId = 1,text:len() do
- ch = text:sub(chId,chId)
- if ch == '\n' then
- y = y+1
- self.device.setCursorPos(x,y)
- else
- self.device.write(ch)
- end
- end
- end
- function btn:setBgColor(color)
- btn.bgColor = color
- btn:draw()
- end
- function btn:setFgColor(color)
- btn.fgColor = color
- btn:draw()
- end
- function btn:clicked(args)
- if self.side != 'term' and self.side != args[1] then
- -- Not the right monitor
- return
- end
- if args[2] >= self.x and args[3] >= self.y and
- args[2] <= self.x + self.w and args[3] <= self.x + self.h then
- if self.callback != nil then
- self.callback(args)
- end
- end
- end
- maxW = 0
- w = 0
- h = 1
- for chId = 1,_text:len() do
- ch = _text:sub(chId,chId)
- if ch == '\n' then
- maxW = math.max(maxW,w)
- h = h + 1
- else
- w = w + 1
- end
- end
- btn.h = h
- btn.w = maxW
- if _side == 'term' then
- slot_sig.connectSlot('monitor_touch', btn:clicked)
- else
- slot_sig.connectSlot('mouse_click', btn:clicked)
- end
- return btn
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement