Page 1 of 2

how to attach text bubbles with text to a monster?

Posted: Tue Apr 17, 2012 10:50 am
by pch
hello,
I'm slowly moving forward with my dungeons, but there is still a lot to learn.
anyway I just decided to add a "text bubbles" so the monsters could speak.

I managed to make some kind of bubbles (just a rectangle so far) but don't know how to add some text to it to have it in one field (text)

here it goes:
"u" stands for unit
"fs" - fontSize - that's something lige grid size

Code: Select all

u.text = love.graphics.rectangle("fill", u.x * fs, u.y * fs, 100, 20)
should I make a table?

Code: Select all

u.text = {
love.graphics.rectangle("fill", u.x * fs, u.y * fs, 100, 20)
love.graphics.rectangle("line", u.x * fs, u.y * fs, 100, 20)
love.graphics.print("blablabla", u.x * fs + 5, u.y * fs + 5, 100, 20)
}
or there is a better way?

file just in case
(units.lua - there is a unit.txt, player.lua - theres a function that adds rectangle to unit)
to see a bubble, you must be "touching" a monster
and that is for love 8.0 - otherwise PO2 !!!!

Re: how to attach text bubbles with text to a monster?

Posted: Tue Apr 17, 2012 11:11 am
by Robin
Just so you know: functions like love.graphics.rectangle(), love.graphics.print(), etc. don't create objects and return nil. They just draw to the screen.

Try something like this:

Code: Select all

-- whenever the text changes:
u.text = "blablabla"

-- in love.draw:
love.graphics.rectangle("fill", u.x * fs, u.y * fs, 100, 20)
love.graphics.rectangle("line", u.x * fs, u.y * fs, 100, 20)
love.graphics.print(u.text, u.x * fs + 5, u.y * fs + 5, 100, 20)

Re: how to attach text bubbles with text to a monster?

Posted: Tue Apr 17, 2012 11:29 am
by pch
ok, so I put only text into .text - the rest will go to .draw

thanks, I had a different "vision" of it so that's why I couldnt figure it out:)

Re: how to attach text bubbles with text to a monster?

Posted: Tue Apr 17, 2012 12:25 pm
by coffee
Hello, nice to see you see still working in rogue games. Ok Robin already answered your question. I just want to quick point you for a probably unnoticed thing in your engine. If you in tile mode, call "display whole lvl" and then "switch to ascii" it only changes the player but not the remain tiles (as it does in "only discovered rooms"). And you really need to get a "quit". :D

Re: how to attach text bubbles with text to a monster?

Posted: Tue Apr 17, 2012 5:33 pm
by pch
hello there,
coffee wrote:...a probably unnoticed thing in your engine. If you in tile mode, call "display whole lvl" and then "switch to ascii" it only changes the player but not the remain tiles (as it does in "only discovered rooms")...
I'm aware of it, but I just left it like this because I was concetrated on other things - will change it later :)
coffee wrote: And you really need to get a "quit". :D
well... there is one but it is only visible in ASCII mode [>] and once you get there press SHIFT . (dot) :)

edit:
oops. there is no exit:) - looks like I disabled it for some reason.

Re: how to attach text bubbles with text to a monster?

Posted: Tue Apr 17, 2012 7:31 pm
by iemfi
Here's the text bubble function I just made for my project, all you need to feed it are the x and y position and the text. You can also feed it a font object, "ratio" of the width to height, the dialog box colour, and the text and outline colour.

Code: Select all

love.draw()
   drawBubble(love.mouse.getX(),love.mouse.getY(),'Hello, the quick brown fox jumped over the rubber pig')
end
function drawBubble(x,y,text,font,ratio,bgColour,textColour)
    bgColour=bgColour or {200,200,200,255}
    textColour=textColour or {0,0,0,255}
    ratio=ratio or 10
    local fontHeight=font:getHeight()
    local width=font:getWidth(text)
    local border=fontHeight
    local tail=fontHeight*1.5
    local w=0
    local lines=1
    if ratio<width/fontHeight then
        width=width/((width/fontHeight)/(ratio*2))
        w,lines=font:getWrap(text,width)
    end
    local boxWidth=width+border*2
    local boxHeight=fontHeight*lines+border*2
    local direction={1,1}
    if x+boxWidth+tail>=love.graphics.getWidth() then
        direction[1]=-1
    end
    if y+boxHeight+tail>=love.graphics.getHeight() then
        direction[2]=-1
    end
    local x1=x+tail*direction[1]
    local y1=y+tail/2*direction[2]
    local x2=x+(tail/2+boxWidth)*direction[1]
    local y2=y1
    local x3=x2
    local y3=y+(tail/2+boxHeight)*direction[2]
    local x4=x+(tail/2)*direction[1]
    local y4=y3
    local x5=x4
    local y5=y+tail*direction[2]
    local points={x,y,x1,y1,x2,y2,x3,y3,x4,y4,x5,y5}
    love.graphics.setColor(bgColour)
    local xFill=x+math.min(0,direction[1])*boxWidth+direction[1]*tail/2
    local yFill=y+math.min(0,direction[2])*boxHeight+direction[2]*tail/2

    love.graphics.rectangle('fill',xFill,yFill,boxWidth,boxHeight)
    love.graphics.triangle('fill',x,y,x1,y1,x5,y5)
    love.graphics.setColor(textColour)
    love.graphics.setLineWidth(1)
    love.graphics.polygon('line',points)
    love.graphics.setFont(font)
    love.graphics.printf(text,xFill+border,yFill+border,width,'center')
end

Re: how to attach text bubbles with text to a monster?

Posted: Tue Apr 17, 2012 7:42 pm
by pch
thanks a lot !
I would try it right away if I only could :)

...but I will have to wait till tommorow.

edit:
wow, it works like a charm - thank you.

Re: how to attach text bubbles with text to a monster?

Posted: Wed Apr 18, 2012 7:17 pm
by Petunien
iemfi wrote:

Code: Select all

love.draw()
    font=font or love.graphics.newFont(12)
Hmm, what is if no font is set? It will create a new font every frame, or?
That's not good for the performance. ^^

Re: how to attach text bubbles with text to a monster?

Posted: Wed Apr 18, 2012 7:30 pm
by iemfi
Yes, not good for performance, but unless your game is very performance intensive or you want to draw a lot of bubbles I don't think it would be a problem at all.

Re: how to attach text bubbles with text to a monster?

Posted: Wed Apr 18, 2012 7:46 pm
by Robin
iemfi wrote:Yes, not good for performance, but unless your game is very performance intensive or you want to draw a lot of bubbles I don't think it would be a problem at all.
Oh, yes it will be a problem. The garbage collection will make overtime, consuming memory and CPU time.