Skip to content

Commit

Permalink
added rich textboxes
Browse files Browse the repository at this point in the history
  • Loading branch information
Retrisma committed Oct 12, 2023
1 parent 6d200eb commit 03b5022
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 19 deletions.
Binary file added Graphics/Sprites/dialoguebox.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion assets.lua
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ function loadfolder(path, type)
out[shortname].image:getWidth(), out[shortname].image:getHeight())
end
elseif type == "font" then
out[shortname] = love.graphics.newFont(lpath, 20)
out[shortname] = love.graphics.newFont(lpath, 32)
end
end

Expand Down
6 changes: 3 additions & 3 deletions camera.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ camera = {
xoff = 0, yoff = 0, -- offset
xlock = false, ylock = false, -- camera lock coordinates
init = false,
damping = 10
damping = 2
}

function caminit(map)
Expand Down Expand Up @@ -51,8 +51,8 @@ function updatecamera(dt)
if camera.xlock then camera.x = camera.xlock end
if camera.ylock then camera.y = camera.ylock end

camera.rx = qerp(camera.rx, camera.x + (camera.xoff * (window.width * (1 / window.scale))), dt)
camera.ry = qerp(camera.ry, camera.y, dt)
camera.rx = qerp(camera.rx, camera.x + (camera.xoff * (window.width * (1 / window.scale))), dt * camera.damping)
camera.ry = qerp(camera.ry, camera.y, dt * camera.damping)

camera.fx = math.floor(camera.rx)
camera.fy = math.floor(camera.ry)
Expand Down
21 changes: 10 additions & 11 deletions main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ require "drawing"
sti = require "lib/sti"
moonshine = require "lib/moonshine"

window = { width = 800, height = 460, scale = 1.5 }
window = { width = 800, height = 460, scale = 1.3 }

speed = { target = 1 / 60, multiplier = 1 }

Expand Down Expand Up @@ -43,24 +43,23 @@ function love.load()
end

Button:add(50, 100, "button", function() showdebug = not showdebug end)
--Button:add(250, 2200, "button", function() showdebug = not showdebug end)
Textbox:add(500, 290, "debug button", { scroll = false })
Textbox:add(200, 100, "Everyone knows Mario is cool as fuck. Everyone knows Mario is cool as fuck.", {
scroll = true,
shadow = { 1, 0, 0 },
image = images["dialoguebox"],
hpad = 15,
vpad = 10,
font = fonts["ElixiR"]
})
end

function love.update(dt)
--calculate fixed timestep
local rate = speed.target * dt * speed.multiplier * 30

updatecamera(dt)
updatecamera(rate)
updatemouse()

debug.mx = mouse.x
debug.my = mouse.y
debug.px = player.x
debug.py = player.y
debug.cx = camera.fx
debug.cy = camera.fy

--update all sprites
for _,v in pairs(p) do
v:update(rate)
Expand Down
64 changes: 60 additions & 4 deletions ui.lua
Original file line number Diff line number Diff line change
Expand Up @@ -44,19 +44,34 @@ function Button:animate(dt)
end

--textbox class
Textbox = { bank = "" }
Textbox = {
bank = "",
speed = 0.01, --delay in seconds per letter
cooldown = 0,
pixels = 0
}
local textbox_mt = class(Textbox)

function Textbox:init(x, y, text, opts)
opts = opts or {}
local o = {
x = x, y = y,
text = "",
scroll = opts.scroll or false
drawmode = "absolute"
}

for k,v in pairs(opts) do
o[k] = v
end

if o.scroll then o.bank = text else o.text = text end

local word = o.bank:match("(%w+)(.+)")
local wordsize = fonts["ElixiR"]:getWidth(" " .. (word or ""))
o.pixels = wordsize

o.width = o.width or (o.image and o.image:getWidth()) or 100

return o
end

Expand All @@ -70,11 +85,52 @@ end

function Textbox:update(dt)
if self.bank ~= "" then
self.text = self.text .. self.bank:sub(1, 1)
self.cooldown = self.cooldown + dt
if self.cooldown > self.speed then
self:nextletter()

self.cooldown = self.cooldown - self.speed
end
end
end

function Textbox:nextletter()
local char = self.bank:sub(1, 1)

if char == " " then
local word = self.bank:match("(%w+)(.+)")
local wordsize = fonts["ElixiR"]:getWidth(" " .. (word or ""))

if self.pixels + wordsize > self.width - (self.hpad * 2) then
self.text = self.text .. "\n"
self.pixels = 0
else
self.text = self.text .. char
end

self.bank = self.bank:sub(2)
self.pixels = self.pixels + wordsize
else
self.text = self.text .. char
self.bank = self.bank:sub(2)
end
end

function Textbox:draw()
love.graphics.print(self.text, self.x, self.y, 0)
local textpos = {
x = self.x + (self.hpad or 0),
y = self.y + (self.vpad or 0)
}

if self.font then love.graphics.setFont(self.font) end

if self.image then
love.graphics.draw(self.image, self.x, self.y)
end
if self.shadow then
love.graphics.setColor(self.shadow)
love.graphics.print(self.text, textpos.x + 1, textpos.y + 1, 0)
love.graphics.setColor(1, 1, 1, 1)
end
love.graphics.print(self.text, textpos.x, textpos.y, 0)
end

0 comments on commit 03b5022

Please sign in to comment.