Minimal text gui is now possible

This commit is contained in:
Sebastian Hugentobler 2014-05-08 12:10:59 +02:00
parent 54c2d18fd4
commit 40a71e3855
4 changed files with 132 additions and 15 deletions

View File

@ -22,3 +22,6 @@ run :
clean :
rm -rf $(BUILDDIR)/*
kill:
kill -s kill $(ps | grep "love src/" | awk '{print $1}')

BIN
src/assets/ui/font.ttf Normal file

Binary file not shown.

View File

@ -11,7 +11,9 @@ local ui = require "ui"
local characters = {}
function love.load()
function love.load(arg)
if arg[#arg] == "-debug" then require("mobdebug").start() end -- debugging in ZeroBraineStudio
windowWidth = love.graphics.getWidth()
windowHeight = love.graphics.getHeight()
@ -19,19 +21,28 @@ function love.load()
collision = map:getCollisionMap("collision")
initCharacters()
ui:showMessage("Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem. Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid ex ea commodi consequatur? Quis autem vel eum iure reprehenderit qui in ea voluptate velit esse quam nihil molestiae consequatur, vel illum qui dolorem eum fugiat quo voluptas nulla pariatur?")
end
function love.update(dt)
for _, char in ipairs(characters) do
char.controller.animation:update(dt)
if not ui.active then
for _, char in ipairs(characters) do
char.controller.animation:update(dt)
if char.relevantKeys then
for _, relevantKey in ipairs(char.relevantKeys) do
if love.keyboard.isDown(relevantKey) then
char.controller:sendKey(relevantKey)
if char.relevantKeys then
for _, relevantKey in ipairs(char.relevantKeys) do
if love.keyboard.isDown(relevantKey) then
char.controller:sendKey(relevantKey)
end
end
end
end
else
for _, relevantKey in ipairs(ui.relevantKeys) do
if love.keyboard.isDown(relevantKey) then
ui:sendKey(relevantKey)
end
end
end
collider:update(dt)
@ -48,10 +59,6 @@ function love.keyreleased(key)
end
end
end
if key == "w" then
print()
end
end
function love.draw()
@ -63,6 +70,7 @@ function love.draw()
map:setDrawRange(translateX, translateY, windowWidth, windowHeight)
map:draw()
ui:draw()
--debugDrawing()
end
@ -78,8 +86,6 @@ function debugDrawing()
end
function initCharacters()
local collisionBoxOffset = 5
map:addCustomLayer("character layer", 4)
local characterLayer = map.layers["character layer"]
@ -114,5 +120,3 @@ function initCharacters()
map.layers["characters"].visible = false
end

View File

@ -1,4 +1,114 @@
local ui = {}
ui.relevantKeys = { "return", "up", "down" }
ui.height = 240
ui.fullMessage = ""
ui.active = false
ui.border = 10
ui.textLines = {}
ui.startLine = 1
ui.endLine = 1
local font = love.graphics.newFont("assets/ui/font.ttf", 24)
love.graphics.setFont(font)
function ui:showMessage(message)
ui.active = true
local maxLines = math.floor((ui.height - ui.border) / font:getHeight())
local fullLines = font:getWrap(ui.fullMessage, windowWidth - ui.border)
ui.textLines = {}
local fullIndex = 1
local messageTail = message
while not endsWith(messageTail, ui.textLines[#ui.textLines]) do
fullIndex, line, messageTail = ui:getMaxString(string.sub(messageTail, fullIndex))
fullIndex = fullIndex + 1 -- account for the following space
table.insert(ui.textLines, line)
end
ui.endLine = maxLines
ui:setLineRange(ui.startLine, ui.endLine)
end
function ui:setLineRange(startLine, endLine)
ui.fullMessage = ""
if endLine > #ui.textLines then endLine = #ui.textLines end
for index = startLine, endLine, 1 do
ui.fullMessage = ui.fullMessage .. ui.textLines[index]
end
end
function endsWith(s, send)
if not send then return false end
return #s >= #send and s:find(send, #s-#send+1, true) and true or false
end
function ui:getMaxString(stringTail)
local index = string.len(stringTail)
local width = font:getWidth(string.sub(stringTail, 1, index))
local needsCutting = width > windowWidth - ui.border
while width > windowWidth - ui.border do
width = font:getWidth(string.sub(stringTail, 1, index))
index = index - 1
end
if needsCutting and
string.sub(stringTail, index + 1, index + 1) ~= " " then
local cursor = "!"
while cursor ~= " " and cursor ~= "" do
index = index - 1
cursor = string.sub(stringTail, index, index)
end
end
return index, string.sub(stringTail, 1, index), stringTail
end
function ui:draw()
if ui.active then
love.graphics.setColor(255, 255, 255, 150)
love.graphics.rectangle("fill", 0, windowHeight - ui.height, windowWidth, ui.height)
love.graphics.setColor(55, 60, 60, 255)
love.graphics.printf(ui.fullMessage, ui.border, windowHeight - ui.height + ui.border, windowWidth - ui.border)
end
end
function ui:sendKey(key)
if key == "up" then
if ui.startLine > 1 then
ui.startLine = ui.startLine - 1
ui.endLine = ui.endLine - 1
ui:setLineRange(ui.startLine, ui.endLine)
end
end
if key == "down" then
if ui.endLine < #ui.textLines then
ui.startLine = ui.startLine + 1
ui.endLine = ui.endLine + 1
ui:setLineRange(ui.startLine, ui.endLine)
end
end
if key == "return" then
ui.active = false
print(ui.active)
end
end
return ui