Browse Source

starting more gui stuff

master
Nicholas Hayashi 4 years ago
parent
commit
eb65c89160
  1. 3
      main.lua
  2. BIN
      res/slider.png
  3. 7
      src/game.lua
  4. 2
      src/grid.lua
  5. 85
      src/gui.lua
  6. 9
      texture.lua

3
main.lua

@ -1,7 +1,7 @@
settings = am.load_state("settings", "json") or { settings = am.load_state("settings", "json") or {
fullscreen = true,
fullscreen = false,
window_width = 1920, window_width = 1920,
window_height = 1080, window_height = 1080,
music_volume = 0.1, music_volume = 0.1,
@ -36,6 +36,7 @@ require "src/extra"
require "src/geometry" require "src/geometry"
require "src/hexyz" require "src/hexyz"
require "src/game" require "src/game"
require "src/gui"
require "src/grid" require "src/grid"
require "src/mob" require "src/mob"
require "src/projectile" require "src/projectile"

BIN
res/slider.png

After

Width: 400  |  Height: 20  |  Size: 442 B

7
src/game.lua

@ -113,12 +113,9 @@ end
local function game_pause() local function game_pause()
win.scene("game").paused = true win.scene("game").paused = true
win.scene("game"):append(am.group{
win.scene:append(am.group{
am.rect(win.left, win.bottom, win.right, win.top, COLORS.TRANSPARENT), am.rect(win.left, win.bottom, win.right, win.top, COLORS.TRANSPARENT),
am.scale(2)
^ am.text(string.format(
"Paused.\nSeed: %d\nEscape to Resume\nf4 to start a new game", state.map.seed
), COLORS.BLACK),
gui_textfield(vec2(-300, 0), vec2(200, 100)),
} }
:tag"pause_menu") :tag"pause_menu")

2
src/grid.lua

@ -1,7 +1,7 @@
-- distance from hex centerpoint to any vertex -- distance from hex centerpoint to any vertex
HEX_SIZE = 24
HEX_SIZE = 26
HEX_PIXEL_WIDTH = hex_width(HEX_SIZE, ORIENTATION.FLAT) HEX_PIXEL_WIDTH = hex_width(HEX_SIZE, ORIENTATION.FLAT)
HEX_PIXEL_HEIGHT = hex_height(HEX_SIZE, ORIENTATION.FLAT) HEX_PIXEL_HEIGHT = hex_height(HEX_SIZE, ORIENTATION.FLAT)

85
src/gui.lua

@ -0,0 +1,85 @@
function gui_numberfield(dimensions, opts)
end
function gui_textfield(position, dimensions, max, disallowed_keys)
local width, height = dimensions.x, dimensions.y
local disallowed_keys = disallowed_keys or {}
local max = max or 99
local padding = 2
local outer_rect = am.rect(-width/2, -height/2, width/2, height/2, COLORS.VERY_DARK_GRAY)
local inner_rect = am.rect(-width/2 + padding
, -height/2 + padding
, width/2 - padding
, height/2 - padding
, COLORS.PALE_SILVER)
local group = am.group{
outer_rect,
inner_rect,
am.translate(-width/2 + 5, 0) ^ am.scale(2) ^ am.text("", COLORS.BLACK, "left"),
am.translate(-width/2 + 5, -8) ^ am.line(vec2(0, 0), vec2(16, 0), 2, COLORS.BLACK)
}
group:action(function(self)
local keys = win:keys_pressed()
if #keys == 0 then return end
-- @HACK all characters and digits are represented by a single string in amulet
-- so we don't have to iterate over everything
-- pattern matching doesn't work because control characters are also just normal strings
for i,k in pairs(keys) do
if not disallowed_keys[k] then
if k:len() == 1 then
if string.match(k, "%d") then
self"text".text = self"text".text .. k
elseif win:key_down("lshift") or win:key_down("rshift") then
self"text".text = self"text".text .. k:upper()
else
self"text".text = self"text".text .. k
end
elseif k == "space" then
self"text".text = self"text".text .. " "
elseif k == "backspace" then
self"text".text = self"text".text:sub(1, self"text".text:len() - 1)
elseif k == "enter" then
end
end
end
end)
return group
end
function gui_slider(position, dimensions, bar_color, circle_color, min, max, default_value, action)
local position = position or vec2(0)
local width = dimensions.x
local height = dimensions.y
local bar_color = bar_color or COLORS.WHITE
local circle_color = circle_color or COLORS.GREEN_YELLOW
local min = min or 0
local max = max or 100
local default_value = math.clamp(default_value or 50, min, max)
local slider = pack_texture_into_sprite(TEXTURES.GUI_SLIDER, width, height, bar_color)
local circle = am.circle(vec2(-width/2 + (default_value/max) * (width/2), 0), height, circle_color)
local node = am.translate(position) ^ am.group{
slider,
circle
}
if action then node:action(action) end
return node
end

9
texture.lua

@ -14,9 +14,11 @@ TEXTURES = {
LOGO = load_texture("res/logo.png"), LOGO = load_texture("res/logo.png"),
GEM1 = load_texture("res/gem1.png"), GEM1 = load_texture("res/gem1.png"),
-- gui stuff
BUTTON1 = load_texture("res/button1.png"), BUTTON1 = load_texture("res/button1.png"),
WIDER_BUTTON1 = load_texture("res/wider_button1.png"), WIDER_BUTTON1 = load_texture("res/wider_button1.png"),
TAB_ICON = load_texture("res/tab_icon.png"), TAB_ICON = load_texture("res/tab_icon.png"),
GUI_SLIDER = load_texture("res/slider.png"),
-- tower stuff -- tower stuff
TOWER_WALL = load_texture("res/tower_wall.png"), TOWER_WALL = load_texture("res/tower_wall.png"),
@ -40,12 +42,15 @@ TEXTURES = {
MOB_SPOODER = load_texture("res/mob_spooder.png"), MOB_SPOODER = load_texture("res/mob_spooder.png"),
} }
function pack_texture_into_sprite(texture, width, height)
return am.sprite{
function pack_texture_into_sprite(texture, width, height, color)
local sprite = am.sprite{
texture = texture, texture = texture,
s1 = 0, s2 = 1, t1 = 0, t2 = 1, s1 = 0, s2 = 1, t1 = 0, t2 = 1,
x1 = 0, x2 = width, width = width, x1 = 0, x2 = width, width = width,
y1 = 0, y2 = height, height = height y1 = 0, y2 = height, height = height
} }
sprite.color = color or vec4(1)
return sprite
end end
Loading…
Cancel
Save