*looks the entire thread after logging in*
~Oh, what i´ve done, i've started a war lmao.
looked all replies (specially pgimeno's code) and it works with some params changed
Code: Select all
-- Function by pgimeno --
local function genRoundText(text, font, radius, align, topMargin)
-- Initializes an empty text object
local textObject = love.graphics.newText(font)
-- If topMargin is 0, use default font height
local fontHeight = font:getHeight()
if topMargin == 0 then
topMargin = fontHeight
end
-- Positions the initial y position of the text
local y = -radius + topMargin
-- Keep adding text until all given text is used
-- or the position y exceeds the radius of the circle
while text ~= "" and math.abs(y) < radius do
-- Calculates the maximum left and right distance the text can occupy
-- using the distance from the origin (0,0) to the nearest point on the circle
local ldist = math.floor(math.min(math.sqrt(radius^2 - y^2),
math.sqrt(radius^2 - (y + fontHeight)^2)))
-- Wraps the text to the calculated maximum distance
local _, wrapped = font:getWrap(text, ldist * 2)
local line = wrapped[1]
-- If the alignment is "justify", use our own justification feature
if align == 'justify' then
-- Checks if the line has been wrapped (if it is too long to fit
-- within the calculated maximum distance)
if wrapped[2] then
-- Break the line into words
-- and calculates the total distance occupied by the words
local words = {}
local nwords = 0
local total = 0
for word in string.gmatch(line, '[^ ]+') do
nwords = nwords + 1
words[nwords] = word
total = total + font:getWidth(word)
end
-- Calculates the distance to distribute between the words
local to_distribute = ldist * 2 - total
local x = 0
local current = 0
-- Adds each word to the text object
-- adjusting the spacing between them to justify the text
for i = 1, nwords do
local ww = font:getWidth(words[i])
textObject:add(words[i], -ldist + x, y)
if i ~= nwords then
local old = current
current = math.floor(to_distribute * i / (nwords - 1))
x = x + ww + (current - old)
end
end
else
align = 'left' -- If the line has not been wrapped, use the default alignment
end
end
-- If the alignment is not "justify", use the default alignment
if align ~= 'justify' then
textObject:addf(wrapped[1], ldist*2, align, -ldist, y)
end
-- Advance the y position for the next line
y = y + fontHeight
text = text:sub(#wrapped[1] + 1) -- Remove the used line from the remaining text string
end
return textObject -- Returns the created text object
end
function love.conf(t)
t.console = true
end
love.window.updateMode(320,320)
local font = love.graphics.setNewFont(16)
local txt = genRoundText("Lorem ipsum dolor sit amet"
.. " enim. Etiam ullamcorper. Suspendisse a pellentesque dui, non felis."
.. " Maecenas malesuada elit lectus felis, malesuada ultrices. Curabitur"
.. " et ligula. Ut molestie a, ultricies porta urna. Vestibulum commodo"
.. " volutpat a, convallis ac, laoreet enim. Phasellus fermentum in, dolor."
.. " Pellentesque facilisis. Nulla imperdiet sit amet magna. Vestibulum"
.. " dapibus, mauris nec malesuada fames ac turpis velit, rhoncus eu, luctus"
.. " et interdum adipiscing wisi. Aliquam erat ac ipsum. Integer"
, font, love.graphics.getWidth()/2, 'justify', love.graphics.getWidth()/1.6)
function love.draw()
love.graphics.draw(txt, love.graphics.getWidth()/2, love.graphics.getHeight()/2, 0, 1, 1, 0, 0)
love.graphics.circle("line", love.graphics.getWidth()/2, love.graphics.getHeight()/2, love.graphics.getWidth()/2)
end
The main idea is providing a way to show text in a round display of 320x320, the params i've used in this case are:
genRoundText(text, font, love.graphics.getWidth()/2, 'justify', love.graphics.getWidth()/(2*0.8)),
i've put getWidth()/2 due to the drawing function takes reference from the centre instead of (0,0), and put 0,8 on the topMargin to display the text like a visual novel on round display.
Idea: make a pull request on
https://github.com/tanema/talkies to provide an option to override printing function
Löve file attached.