hexyz is tower defense game, and a lua library for dealing with hexagonal grids
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

223 lines
7.4 KiB

  1. -- text popup in the middle of the screen that dissapates
  2. function gui_alert(message, color, decay_time)
  3. win.scene:append(
  4. am.scale(3) ^ am.text(message, color or COLORS.WHITE)
  5. :action(coroutine.create(function(self)
  6. am.wait(am.tween(self, decay_time or 1, { color = vec4(0) }, am.ease_in_out))
  7. win.scene:remove(self)
  8. end))
  9. )
  10. end
  11. local function gui_make_backing_rect(
  12. position,
  13. content_width,
  14. content_height,
  15. padding
  16. )
  17. local half_width = content_width/2
  18. local half_height = content_height/2
  19. local x1 = position[1] - half_width - padding
  20. local y1 = position[2] - half_height - padding
  21. local x2 = position[1] + half_width + padding
  22. local y2 = position[2] + half_height + padding
  23. return x1, y1, x2, y2
  24. end
  25. -- args {
  26. -- position vec2
  27. -- onclick function
  28. -- padding number
  29. --
  30. -- min_width number
  31. -- min_height number
  32. --
  33. -- label string
  34. -- font {
  35. -- color vec4
  36. -- halign "center" | "left" | "right"
  37. -- valign "center" | "top" | "bottom"
  38. -- }
  39. -- }
  40. function gui_make_button(args)
  41. local args = args or {}
  42. local position = args.position or vec2(0)
  43. local onclick = args.onclick
  44. local padding = args.padding or 6
  45. local min_width = args.min_width or 0
  46. local min_height = args.min_height or 0
  47. local label = args.label or ""
  48. local font = args.font or {
  49. color = vec4(1),
  50. halign = "center",
  51. valign = "center"
  52. }
  53. local scene = am.group()
  54. local text = am.text(args.label or "", font.color, font.halign, font.valign)
  55. scene:append(am.translate(args.position) ^ text)
  56. local content_width = math.max(min_width, text.width)
  57. local content_height = math.max(min_height, text.height)
  58. local x1, y1, x2, y2 = gui_make_backing_rect(position, content_width, content_height, padding)
  59. local back_rect = am.rect(x1 - padding/2, y1, x2, y2 + padding/2, vec4(0, 0, 0, 1))
  60. local front_rect = am.rect(x1, y1, x2, y2, vec4(0.4))
  61. scene:prepend(front_rect)
  62. scene:prepend(back_rect)
  63. scene:action(function(self)
  64. if point_in_rect(win:mouse_position(), back_rect) then
  65. if win:mouse_pressed"left" then
  66. front_rect.color = vec4(0.4)
  67. if onclick then
  68. onclick()
  69. end
  70. else
  71. front_rect.color = vec4(0, 0, 0, 1)
  72. end
  73. else
  74. front_rect.color = vec4(0.4)
  75. end
  76. end)
  77. return scene
  78. end
  79. function gui_textfield(
  80. position,
  81. dimensions,
  82. max,
  83. disallowed_chars
  84. )
  85. local width, height = dimensions.x, dimensions.y
  86. local disallowed_chars = disallowed_chars or {}
  87. local max = max or 10
  88. local padding = padding or 6
  89. local half_width = width/2
  90. local half_height = height/2
  91. local x1 = position[1] - half_width - padding
  92. local y1 = position[2] - half_height - padding
  93. local x2 = position[1] + half_width + padding
  94. local y2 = position[2] + half_height + padding
  95. local back_rect = am.rect(x1 - padding/2, y1, x2, y2 + padding/2, vec4(0, 0, 0, 1))
  96. local front_rect = am.rect(x1, y1, x2, y2, vec4(0.4))
  97. local group = am.group{
  98. back_rect,
  99. front_rect,
  100. am.translate(-width/2 + padding, 0) ^ am.scale(2) ^ am.text("", vec4(0, 0, 0, 1), "left"),
  101. am.translate(-width/2 + padding, -8) ^ am.line(vec2(0, 0), vec2(16, 0), 2, vec4(0, 0, 0, 1))
  102. }
  103. group:action(function(self)
  104. local keys = win:keys_pressed()
  105. if #keys == 0 then return end
  106. local chars = {}
  107. local shift = win:key_down("lshift") or win:key_down("rshift")
  108. for i,k in pairs(keys) do
  109. if k:len() == 1 then -- @HACK alphabetical or digit characters
  110. if string.match(k, "%a") then
  111. if shift then
  112. table.insert(chars, k:upper())
  113. else
  114. table.insert(chars, k)
  115. end
  116. elseif string.match(k, "%d") then
  117. if shift then
  118. if k == "1" then table.insert(chars, "!")
  119. elseif k == "2" then table.insert(chars, "@")
  120. elseif k == "3" then table.insert(chars, "#")
  121. elseif k == "4" then table.insert(chars, "$")
  122. elseif k == "5" then table.insert(chars, "%")
  123. elseif k == "6" then table.insert(chars, "^")
  124. elseif k == "7" then table.insert(chars, "&")
  125. elseif k == "8" then table.insert(chars, "*")
  126. elseif k == "9" then table.insert(chars, "(")
  127. elseif k == "0" then table.insert(chars, ")")
  128. end
  129. else
  130. table.insert(chars, k)
  131. end
  132. end
  133. -- begin non-alphabetical/digit
  134. elseif k == "minus" then
  135. if shift then table.insert(chars, "_")
  136. else table.insert(chars, "-") end
  137. elseif k == "equals" then
  138. if shift then table.insert(chars, "=")
  139. else table.insert(chars, "+") end
  140. elseif k == "leftbracket" then
  141. if shift then table.insert(chars, "{")
  142. else table.insert(chars, "[") end
  143. elseif k == "rightbracket" then
  144. if shift then table.insert(chars, "}")
  145. else table.insert(chars, "]") end
  146. elseif k == "backslash" then
  147. if shift then table.insert(chars, "|")
  148. else table.insert(chars, "\\") end
  149. elseif k == "semicolon" then
  150. if shift then table.insert(chars, ":")
  151. else table.insert(chars, ";") end
  152. elseif k == "quote" then
  153. if shift then table.insert(chars, "\"")
  154. else table.insert(chars, "'") end
  155. elseif k == "backquote" then
  156. if shift then table.insert(chars, "~")
  157. else table.insert(chars, "`") end
  158. elseif k == "comma" then
  159. if shift then table.insert(chars, "<")
  160. else table.insert(chars, ",") end
  161. elseif k == "period" then
  162. if shift then table.insert(chars, ">")
  163. else table.insert(chars, ".") end
  164. elseif k == "slash" then
  165. if shift then table.insert(chars, "?")
  166. else table.insert(chars, "/") end
  167. -- control characters
  168. elseif k == "backspace" then
  169. -- @NOTE this doesn't preserve the order of chars in the array so if
  170. -- someone presses a the key "a" then the backspace key in the same frame, in that order
  171. -- the backspace occurs first
  172. self"text".text = self"text".text:sub(1, self"text".text:len() - 1)
  173. elseif k == "tab" then
  174. -- @TODO
  175. elseif k == "space" then
  176. table.insert(chars, " ")
  177. elseif k == "capslock" then
  178. -- @TODO
  179. end
  180. end
  181. for _,c in pairs(chars) do
  182. if not disallowed_chars[c] then
  183. if self"text".text:len() <= max then
  184. self"text".text = self"text".text .. c
  185. end
  186. end
  187. end
  188. end)
  189. return group
  190. end
  191. function gui_open_modal()
  192. end