Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Simple GUI API by DjSapsan (c) All right reserved.
- -- FEATURES:
- -- Buttons, labels, alignment, too many colors!
- -- TODO допилить удобный юзабельный интерфейс нажатий. Как-то неудобно приписывать всё в Events
- -- крч, в Events вечный цикл на получение события. Если получили клик, то отправляем запрос в API, а он возвращает GUIid (то-есть номер элемента (кнопки)) и через иф обрабатывать
- -- TODO запилить родительский объект (object?), с базовыми кордами и размерами (может стринговый ID?), а дочерние - кнопки, текст, иконки, картинки со своими полями.
- -- TODO может полноценный ГУИ с кругами и окнами???
- -- TODO еще много чего
- local version = 1
- -- переменные
- local nButtons=0 -- номер кнопки
- local nLabels=0 -- номер метки
- local button={} -- список кнопок
- local label={} -- список меток
- -- получить версию API
- function getVersion()
- return version
- end
- -- GUI API:
- -- обработка нажатия по кнопке, если есть нажатие по кнопке id, то возвращает true (готово 100500%)
- function click(id, mx, my)
- if button[id].active==1 then return (mx>=button[id].x)and(mx<=button[id].x+button[id].dx-1)and(my>=button[id].y)and(my<=button[id].y+button[id].dy-1) end
- return false
- end
- -- вывод текста (текст, х,у, цвет)
- function txt(ss,xx,yy,ccolor)
- term.setCursorPos(xx,yy)
- term.setTextColor(ccolor)
- term.write(ss)
- end
- -- создать кнопку (х,у,ширина,высота,цвет кнопки, текст, отрисовка <1> /<0>)
- function newButton(xx,yy,ddx,ddy,ccolor,ttext,aactive)
- nButtons=nButtons+1
- button[nButtons]={}
- button[nButtons].x=xx
- button[nButtons].dx=ddx
- button[nButtons].y=yy
- button[nButtons].dy=ddy
- button[nButtons].color=ccolor
- button[nButtons].text=ttext
- button[nButtons].active=aactive
- end
- -- создается новая метка (х,у,ширина,высота,цвет текста, текст, отрисовка <1> /<0>)
- function newLabel(xx,yy,ddx,ddy,ccolor,ttext,aactive)
- nLabels=nLabels+1
- label[nLabels]={}
- label[nLabels].x=xx
- label[nLabels].dx=ddx
- label[nLabels].y=yy
- label[nLabels].dy=ddy
- label[nLabels].color=ccolor
- label[nLabels].text=ttext
- label[nLabels].active=aactive
- end
- -- нарисовать кнопку по GUIid
- function drawButton(GUIid)
- xx=tonumber(button[GUIid].x)
- dxx=tonumber(button[GUIid].dx)
- yy=tonumber(button[GUIid].y)
- dyy=tonumber(button[GUIid].dy)
- c=button[GUIid].color
- text=button[GUIid].text
- l=#text
- term.setBackgroundColour(c)
- for a=xx,xx+dxx-1 do
- for b=yy,yy+dyy-1 do
- term.setCursorPos( a , b )
- term.write(" ")
- end
- end
- if l>dxx then term.setCursorPos( xx , yy ) print(string.sub(text,1,dxx)) else term.setCursorPos( xx+(dxx/2)-(l/2), yy+(dyy/2) ) term.write(text) end
- end
- -- нарисовать текст по GUIid (сложнаааа с матаном, не влезай - убьет)
- function drawLabel(GUIid)
- xx=tonumber(label[GUIid].x)
- dxx=tonumber(label[GUIid].dx)
- yy=tonumber(label[GUIid].y)
- dyy=tonumber(label[GUIid].dy)
- c=label[GUIid].color
- text=label[GUIid].text
- l=#text -- длина текста
- h=math.floor(l/dxx) --сколько строк нужно для записи текста
- area=dxx*dyy
- term.setTextColor(c)
- if (l>dxx)and(l>area) then
- for p=0,dyy-1 do
- term.setCursorPos(xx,yy+p) print( string.sub(text,p*dxx+1,dxx+p*dxx) ) --если текст больше площади метки
- end
- end
- if (l>dxx)and(l<area) then
- for p=0,h do
- term.setCursorPos(xx,yy+p) print( string.sub(text,p*dxx+1,dxx+p*dxx) ) --если текст больше ширины метки
- end
- end
- if l<dxx then term.setCursorPos( xx+(dxx/2)-(l/2), yy+(dyy/2) ) print(text) end --если текст меньше ширины метки
- term.setTextColor(1)
- end
- -- выключить все ГУИ
- function disableAllGUI()
- for GUIid=1,#button do
- button[GUIid].active=0
- end
- for GUIid=1,#label do
- label[GUIid].active=0
- end
- end
- -- включить кнопки (0 параметров - все, 1 параметр - только эту, 2 параметра - от и до крч)
- function enableButtons(...)
- local tArgs = {...}
- if #tArgs == 0 then
- for GUIid=1,#button do
- button[GUIid].active=1
- end
- end
- if #tArgs == 1 then
- button[tArgs[1]].active=1
- end
- if #tArgs == 2 then
- for GUIid=tArgs[1],tArgs[2] do
- button[GUIid].active=1
- end
- end
- end
- -- включить кнопки (0 параметров - все, 1 параметр - только эту, 2 параметра - от и до крч)
- function enableLabels(...)
- local tArgs = {...}
- if #tArgs == 0 then
- for GUIid=1,#button do
- label[GUIid].active=1
- end
- end
- if #tArgs == 1 then
- label[tArgs[1]].active=1
- end
- if #tArgs == 2 then
- for GUIid=tArgs[1],tArgs[2] do
- label[GUIid].active=1
- end
- end
- end
- function drawGUI() -- рисуется весь экран
- term.setBackgroundColour(32768)
- term.clear()
- for GUIid=1,#button do
- if button[GUIid].active==1 then drawButton(GUIid) end
- end
- term.setBackgroundColour(32768)
- for GUIid=1,#label do
- if label[GUIid].active==1 then drawLabel(GUIid) end
- end
- term.setBackgroundColour(32768)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement