From 40a71e3855b9a881bbdb93310fa6d487e32bddd7 Mon Sep 17 00:00:00 2001 From: Sebastian Hugentobler Date: Thu, 8 May 2014 12:10:59 +0200 Subject: [PATCH] Minimal text gui is now possible --- Makefile | 3 ++ src/assets/ui/font.ttf | Bin 0 -> 13544 bytes src/main.lua | 34 +++++++------ src/ui.lua | 110 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 132 insertions(+), 15 deletions(-) create mode 100644 src/assets/ui/font.ttf diff --git a/Makefile b/Makefile index 57fee6c..d2caae4 100644 --- a/Makefile +++ b/Makefile @@ -22,3 +22,6 @@ run : clean : rm -rf $(BUILDDIR)/* + +kill: + kill -s kill $(ps | grep "love src/" | awk '{print $1}') diff --git a/src/assets/ui/font.ttf b/src/assets/ui/font.ttf new file mode 100644 index 0000000000000000000000000000000000000000..c7ed1d91950f994bd6c71efba69332a0459d26e4 GIT binary patch literal 13544 zcmb_jTZ|l6wcSg;{?W9@xTl_e075lJPI=lAY?V&AjJpE@sc=S8%xyZ_$%_R6TNNBu3- z*FW<3*6m;X;n|W%a2)jmhfXe@+5aCqUKNQBqU}?MUOqQ18^iw+DP`y{9b0_y%+eA@ zmOI}>ec;#&pE)`|w&N9%@@pcuzjpk{;^E6b-Ti0u{RPL4<7lX?kG_MkHle=h_{nqU zfBjJS2I?7(*Izh&XmS3HFAL|2qxRU|cb*&F^MA6cga(mc{q3_)`Rh`)ltg41{7T3Ret0I5 zr9=*vWGQ1=N762LI;JhhdRVXF5_nAlkUisur2Hq~~Hr_Yj>zeler>i$TqtmBJo$5o%na?ho% zFL77)p^bCMj}@e#i9U`O@<^95w52+C*2T5+>dUE{AM38H^igizlUP~h!gd~sqo29^ z#Ep|k+M9^(t{vt9hZ}{5M zA7PO{`P~oSKk7z6xBY?&xbS5q?R$J9xGt3|)q%mGRl}=CM#sh{CfCH*O$%1E&VQKk)a#9fPL^Umg73;6H}e4V@W!cj(fp?W@vNzZ|}P z_~7tYhu>d){p!Q3zp?tSBNHR{j2s?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= {} -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 - - diff --git a/src/ui.lua b/src/ui.lua index 95ffbfb..95c89bd 100644 --- a/src/ui.lua +++ b/src/ui.lua @@ -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