Advertisement
LeshaInc

21:03

Apr 5th, 2015
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.74 KB | None | 0 0
  1. -- * * *
  2. -- GUI API (OpenComputers) (1.0)
  3. -- 05/04/2015 (c) computercraft.ru
  4. -- Created by Totoro, LeshaInc, Xom and with some magic
  5. -- * * *
  6.  
  7. -- Создаем таблицу
  8. local API = {}
  9. local param= {}
  10.  
  11. -- Подключаем системные API
  12. local event = require('event')
  13. local term = require("term")
  14. local component = require("component")
  15. local gpu = component.gpu
  16.  
  17. -- Константы
  18. -- Получаем разрешение монитора
  19. local WIDTH, HEIGHT = gpu.getResolution() -- понятные названия - наше все =)
  20. -- Цвета
  21. local color = {}
  22. color.green = 0x00AA00
  23. color.red = 0xAA0000
  24. color.black = 0x000000
  25. color.white = 0xFFFFFF
  26. -- Символы из Юникода
  27. local LOWER_HALF_BLOCK = "▄"
  28. local UPPER_HALF_BLOCK = "▀"
  29. local LEFT_HALF_BLOCK = "▌"
  30. local RIGHT_HALF_BLOCK = "▐"
  31. local FLOWERS = "✽ ✾ ✿ ❁ ❃ ❋ ❀"
  32.  
  33.  
  34. -- Функция очистки (можно заменить на вызов term.clear()) На всякий случай
  35. function API.xclear()
  36. gpu.setBackground(Black)
  37. gpu.fill(1, 1, WIDTH, HEIGHT, " ")
  38. end
  39.  
  40. function API.clear()
  41. for i = 1, HEIGHT, 1 do
  42. for j = 1, WIDTH, 1 do
  43. if gpu.get(i, j) then
  44. gpu.set(i, j
  45. end
  46.  
  47. -- Псевдографика --
  48. -- Функция рисования пикселя
  49. function API.pixel(x,y,color)
  50. gpu.setBackground(color)
  51. gpu.set(x,y," ")
  52. end
  53.  
  54. -- Линия от А до Б
  55. -- (Запасной вариант) LeshaInc опять ничего не понял.
  56. function API.xline(x1, y1, x2, y2, color, symbol)
  57. local deltax,deltay,stat,errors= 0,0,1
  58. deltax= x1-x2
  59. deltay= y1-y2
  60. errors= deltax/deltay
  61. if (x1 < x2)then
  62. stat= -1
  63. end
  64. for i=x1,x2,stat do
  65.  
  66. end
  67. end
  68.  
  69. -- Функция создания заполненной коробки
  70. function API.box(x,y,WIDTH,HEIGHT,color)
  71. gpu.setBackground(color)
  72. gpu.fill(x,y,WIDTH,HEIGHT," ")
  73. end
  74.  
  75. -- Функция выдачи позиции курсора
  76. function API.getClick()
  77. local name, x, y, button, playerName = event.pull()
  78. return x, y, button, playerName
  79. end
  80.  
  81. -- Функция написания текста центрированного относительно X
  82. function API.centerTextX(y,text,color)
  83. gpu.setForeground(color)
  84. gpu.set(w/2 - #text/2, y, text)
  85. end
  86.  
  87. -- Функция написания текста центрированного относительно Y
  88. function API.centerTextY(x,text,color)
  89. gpu.setForeground(color)
  90. gpu.set(x,h/2-#text/2,text)
  91. end
  92.  
  93. -- Функция написания текста центрированно относительно XY
  94. function API.centerTextXY(text,color)
  95. gpu.setForeground(color)
  96. gpu.set(x-#text/2,y-#text/2,text)
  97.  
  98. -- Функция отображения 'пустой' коробки
  99. function API.emptyBox(x,y,WIDTH,HEIGHT,color_inside,color_side,strip_thickness)
  100. gpu.setBackground(color_side)
  101. gpu.fill(x,y,WIDTH,HEIGHT, " ")
  102. gpu.setBackground(color_inside)
  103. gpu.fill(x+strip_thickness,y+strip_thickness,WIDTH-strip_thickness,HEIGHT-strip_thickness, " ")
  104. end
  105.  
  106. -- Ported from CC paintutils lib: LeshaInc говорит:"Это за грани моего понимания."
  107. function API.line(startX, startY, endX, endY, nColor)
  108. if type(startX) ~= "number" or type(startX) ~= "number" or
  109. type(endX) ~= "number" or type(endY) ~= "number" or
  110. (nColor ~= nil and type(nColor) ~= "number") then
  111. error("Expected startX, startY, endX, endY, color", 2)
  112. end
  113.  
  114. startX = math.floor(startX)
  115. startY = math.floor(startY)
  116. endX = math.floor(endX)
  117. endY = math.floor(endY)
  118.  
  119. if startX == endX and startY == endY then
  120. API.pixel(startX, startY, nColor)
  121. return
  122. end
  123.  
  124. local minX = math.min(startX, endX)
  125. if minX == startX then
  126. minY = startY
  127. maxX = endX
  128. maxY = endY
  129. else
  130. minY = endY
  131. maxX = startX
  132. maxY = startY
  133. end
  134.  
  135. -- TODO: clip to screen rectangle?
  136.  
  137. local xDiff = maxX - minX
  138. local yDiff = maxY - minY
  139.  
  140. if xDiff > math.abs(yDiff) then
  141. local y = minY
  142. local dy = yDiff / xDiff
  143. for x=minX,maxX do
  144. API.pixel(x, math.floor(y + 0.5), nColor)
  145. y = y + dy
  146. end
  147. else
  148. local x = minX
  149. local dx = xDiff / yDiff
  150. if maxY >= minY then
  151. for y=minY,maxY do
  152. API.pixel(math.floor(x + 0.5), y, nColor)
  153. x = x + dx
  154. end
  155. else
  156. for y=minY,maxY,-1 do
  157. API.pixel(math.floor(x + 0.5 ), y, nColor)
  158. x = x - dx
  159. end
  160. end
  161. end
  162. end
  163.  
  164. return API
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement