Advertisement
DjSapsan

DJGUI

Apr 25th, 2016
353
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.79 KB | None | 0 0
  1. -- Simple GUI API by DjSapsan (c) All right reserved.
  2. -- FEATURES:
  3. -- Buttons, labels, alignment, too many colors!
  4.  
  5. -- TODO допилить удобный юзабельный интерфейс нажатий. Как-то неудобно приписывать всё в Events
  6. -- крч, в Events вечный цикл на получение события. Если получили клик, то отправляем запрос в API, а он возвращает  GUIid (то-есть номер элемента (кнопки)) и через иф обрабатывать
  7. -- TODO запилить родительский объект (object?), с базовыми кордами и размерами (может стринговый ID?), а дочерние - кнопки, текст, иконки, картинки со своими полями.
  8. -- TODO может полноценный ГУИ с кругами и окнами???
  9. -- TODO еще много чего
  10.  
  11. local version = 1
  12.  
  13. -- переменные
  14.  
  15. local nButtons=0                    -- номер кнопки
  16. local nLabels=0                     -- номер метки
  17. local button={}                     -- список кнопок
  18. local label={}                          -- список меток
  19.  
  20. -- получить версию API
  21. function getVersion()
  22.     return version
  23. end
  24.  
  25. -- GUI API:
  26.  
  27. -- обработка нажатия по кнопке, если есть нажатие по кнопке id, то возвращает true (готово 100500%)
  28. function click(id, mx, my)                         
  29.     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
  30.     return false
  31. end
  32.  
  33. -- вывод текста (текст, х,у, цвет)
  34. function txt(ss,xx,yy,ccolor)
  35.     term.setCursorPos(xx,yy)
  36.     term.setTextColor(ccolor)
  37.     term.write(ss)
  38. end
  39.  
  40. -- создать кнопку (х,у,ширина,высота,цвет кнопки, текст, отрисовка <1> /<0>)
  41. function newButton(xx,yy,ddx,ddy,ccolor,ttext,aactive)
  42.     nButtons=nButtons+1
  43.     button[nButtons]={}
  44.     button[nButtons].x=xx
  45.     button[nButtons].dx=ddx
  46.     button[nButtons].y=yy
  47.     button[nButtons].dy=ddy
  48.     button[nButtons].color=ccolor
  49.     button[nButtons].text=ttext
  50.     button[nButtons].active=aactive
  51. end
  52.  
  53. -- создается новая метка (х,у,ширина,высота,цвет текста, текст, отрисовка <1> /<0>)
  54. function newLabel(xx,yy,ddx,ddy,ccolor,ttext,aactive)      
  55.     nLabels=nLabels+1
  56.     label[nLabels]={}
  57.     label[nLabels].x=xx
  58.     label[nLabels].dx=ddx
  59.     label[nLabels].y=yy
  60.     label[nLabels].dy=ddy
  61.     label[nLabels].color=ccolor
  62.     label[nLabels].text=ttext
  63.     label[nLabels].active=aactive
  64. end
  65.  
  66. -- нарисовать кнопку по GUIid
  67. function drawButton(GUIid)                     
  68.      xx=tonumber(button[GUIid].x)
  69.      dxx=tonumber(button[GUIid].dx)
  70.      yy=tonumber(button[GUIid].y)
  71.      dyy=tonumber(button[GUIid].dy)
  72.      c=button[GUIid].color
  73.      text=button[GUIid].text
  74.      l=#text
  75.     term.setBackgroundColour(c)
  76.     for a=xx,xx+dxx-1 do
  77.         for b=yy,yy+dyy-1 do
  78.             term.setCursorPos( a , b )
  79.             term.write(" ")
  80.         end
  81.     end
  82.    
  83.     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
  84.      
  85. end
  86.  
  87. -- нарисовать текст по GUIid (сложнаааа с матаном, не влезай - убьет)
  88. function drawLabel(GUIid)                      
  89.      xx=tonumber(label[GUIid].x)
  90.      dxx=tonumber(label[GUIid].dx)
  91.      yy=tonumber(label[GUIid].y)
  92.      dyy=tonumber(label[GUIid].dy)
  93.      c=label[GUIid].color
  94.      text=label[GUIid].text
  95.      l=#text                -- длина текста
  96.      h=math.floor(l/dxx)    --сколько строк нужно для записи текста
  97.      area=dxx*dyy
  98.     term.setTextColor(c)
  99.  
  100.     if (l>dxx)and(l>area) then
  101.         for p=0,dyy-1 do
  102.             term.setCursorPos(xx,yy+p) print( string.sub(text,p*dxx+1,dxx+p*dxx) )      --если текст больше площади метки
  103.         end
  104.     end
  105.    
  106.     if (l>dxx)and(l<area) then
  107.         for p=0,h do
  108.             term.setCursorPos(xx,yy+p) print( string.sub(text,p*dxx+1,dxx+p*dxx) )      --если текст больше ширины метки
  109.         end
  110.     end
  111.    
  112.     if l<dxx then term.setCursorPos( xx+(dxx/2)-(l/2), yy+(dyy/2) ) print(text)  end    --если текст меньше ширины метки
  113.    
  114.     term.setTextColor(1)
  115. end
  116.  
  117. -- выключить все ГУИ
  118. function disableAllGUI()
  119.  
  120.         for GUIid=1,#button do
  121.             button[GUIid].active=0
  122.         end
  123.        
  124.         for GUIid=1,#label do
  125.             label[GUIid].active=0
  126.         end
  127.        
  128. end
  129.  
  130. -- включить кнопки (0 параметров - все, 1 параметр - только эту, 2 параметра - от и до крч)
  131. function enableButtons(...)
  132.     local tArgs = {...}
  133.    
  134.     if #tArgs == 0 then
  135.         for GUIid=1,#button do
  136.             button[GUIid].active=1
  137.         end
  138.     end
  139.    
  140.     if #tArgs == 1 then
  141.         button[tArgs[1]].active=1
  142.     end
  143.    
  144.     if #tArgs == 2 then
  145.         for GUIid=tArgs[1],tArgs[2] do
  146.             button[GUIid].active=1
  147.         end
  148.     end
  149.    
  150. end
  151.  
  152. -- включить кнопки (0 параметров - все, 1 параметр - только эту, 2 параметра - от и до крч)
  153. function enableLabels(...)
  154.     local tArgs = {...}
  155.    
  156.     if #tArgs == 0 then
  157.         for GUIid=1,#button do
  158.             label[GUIid].active=1
  159.         end
  160.     end
  161.    
  162.     if #tArgs == 1 then
  163.         label[tArgs[1]].active=1
  164.     end
  165.    
  166.     if #tArgs == 2 then
  167.         for GUIid=tArgs[1],tArgs[2] do
  168.             label[GUIid].active=1
  169.         end
  170.     end
  171.    
  172. end
  173.  
  174. function drawGUI()                      -- рисуется весь экран
  175.     term.setBackgroundColour(32768)
  176.     term.clear()
  177.    
  178.         for GUIid=1,#button do
  179.             if button[GUIid].active==1 then drawButton(GUIid) end
  180.         end
  181.        
  182.     term.setBackgroundColour(32768)
  183.    
  184.         for GUIid=1,#label do
  185.             if label[GUIid].active==1 then drawLabel(GUIid) end
  186.         end
  187.        
  188.     term.setBackgroundColour(32768)
  189. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement