IgorBgz90 Posted April 1, 2015 Share Posted April 1, 2015 A simple example of a multiple text. function App:Start() self.maxTextWidth = 128 self.sourceText = "Text" self.tempText = "" self.text={} self.lineCount = 0 for w in string.gmatch(self.sourceText, "[%p%a%d]+") do if defaultfont:GetTextWidth(self.tempText) < self.maxTextWidth then self.tempText = self.tempText.." "..w else --We need to find the last word, learn length, and delete it. local temp1 = string.reverse(self.tempText) local temp2 = string.sub(temp1, 1, string.find(temp1," ")) temp2 = string.reverse(temp2) local temp3 = string.sub(self.tempText, 1, string.len(self.tempText)-string.len(temp2)) --Add new line to list. self.text[self.lineCount] = temp3 self.lineCount = self.lineCount + 1 self.tempText = "" --Now we can add the last word to new line. self.tempText = self.tempText..temp2.." "..w end end self.text[self.lineCount] = self.tempText self.lineCount = self.lineCount + 1 self.tempText = "" return true end function App:Loop() --Test text self.context:SetBlendMode(Blend.Alpha) self.context:SetColor(1,1,1,1) for i=0,self.lineCount,1 do self.context:DrawText(self.text[i],150,(150)+15*i); end self.context:SetBlendMode(Blend.Solid) self.context:Sync(true) return true end 3 Quote Link to comment Share on other sites More sharing options...
IgorBgz90 Posted April 1, 2015 Author Share Posted April 1, 2015 Let it be here. Suddenly come in handy! Quote Link to comment Share on other sites More sharing options...
Einlander Posted April 1, 2015 Share Posted April 1, 2015 I have a one too. My needlessly complicated version with markup language! No wordwrap though . Maybe one day... --This function will be called once when the program starts function App:Start() --Initialize Steamworks (optional) --Set the application title --Create a window local windowstyle = window.Titlebar if System:GetProperty("fullscreen")=="1" then windowstyle=windowstyle+window.FullScreen end self.window=Window:Create(self.title,0,0,System:GetProperty("screenwidth","1024"),System:GetProperty("screenheight","768"),windowstyle) self.window:HideMouse() --Create the graphics context self.context=Context:Create(self.window,0) if self.context==nil then return false end return true end --This is our main program loop and will be called continuously until the program ends function RGBAToVec4Scalar(rgb,a) return Vec4(rgb[1]/256,rgb[2]/256,rgb[3]/256,a/256) end function split(string1,sep) --http://lua-users.org/wiki/SplitJoin local sep, fields = sep or ":", {} local pattern = string.format("([^%s]+)", sep) string1 = string.gsub(string1, pattern, function© fields[#fields+1] = c end) return fields end function drawtext2(text,x,y,width,height,colorastable) local TempContext = Context:GetCurrent() local tempblendmode = TempContext:GetBlendMode() local oldcolor = TempContext:GetColor() TempContext:SetColor(RGBAToVec4Scalar(colorastable,colorastable[4])) TempContext:SetBlendMode(Blend.Alpha) --[NOTICE] blend mode needs to be set to include alpha --[NOTICE] all set colors need to be changed to the class color set local oldcolor = TempContext:GetColor() TempContext:SetColor(RGBAToVec4Scalar(colorastable,colorastable[4])) -- [NOTICE] impliment command that will ignore all commands that change size in font and line breaks -- preprocess string local textcommands = {} local workingstring=text local watch = 1 local last = 0 local wraplines=false while watch ~= nil do watch,last = string.find(workingstring, '##(.-)##',watch) if watch ~= nil then --print(string.sub(workingstring,watch,last)) table.insert(textcommands,{split(string.sub(workingstring,watch+2,last-2)),watch,last}) -- add it to list of all commands --split(string.sub(text,watch+2,last-2)) -- remove it from string workingstring = string.gsub(workingstring,string.sub(workingstring,watch,last),"",1) if watch ~= nil then watch =1 end end end local font = TempContext:GetFont() TempContext:SetBlendMode(Blend.Alpha) local i=0 local linewidth = 0 local lineheight = 0 local linecollection = {} local currentline = 1 local stringcount = 0 local ignorelinebreak = false --local currentlinehieghttotal = 0 while i < string.len(workingstring) do i=i+1 currentletter = string.sub(workingstring,i,i) -- seriously pushing optimizations, no redunant code -- process fonts for ii =1 , #textcommands do if i == textcommands[ii][2] then local currentcommand = string.upper(textcommands[ii][1][1]) if (currentcommand == "WRAPLINES") == true then --Debug:Error(textcommands[ii][1][2]) if string.upper(textcommands[ii][1][2]) == "TRUE" then wraplines = true end if string.upper(textcommands[ii][1][2]) == "FALSE" then wraplines = false end end if (currentcommand == "FONTSIZE") == true then -- so........ leadwerks dosent have a fontsize option........ end if (currentcommand == "LINEBREAK") == true then table.insert(linecollection,{linewidth,lineheight,stringcount}) linewidth = 0 lineheight = 0 stringcount = 0 end if (currentcommand == "COLORRGB") == true then end end end if lineheight < font:GetHeight() then lineheight = font:GetHeight() end if linewidth + font:GetTextWidth(currentletter) > width and wraplines == true then --stringcount = stringcount+1 table.insert(linecollection,{linewidth,lineheight,stringcount}) lineheight = 0 linewidth = 0 i=i-1 currentline = currentline +1 stringcount = 0 elseif linewidth + font:GetTextWidth(currentletter) > width and wraplines == false then -- keep counting strings. we will ignore them later stringcount = stringcount+1 else linewidth = linewidth + font:GetTextWidth(currentletter) stringcount = stringcount+1 end end table.insert(linecollection,{linewidth,lineheight,stringcount}) --"Einlanders Text Command:It supports forcedlinebreaks, Inline color changes and line wrapping. Font Size changesLeadwerks does'nt TEXT RENDERING LIKETHIS IS EXPENSIVE local stringcount = 1 local stringbuilder = "" local oldlinelocation = 0 local linelocation = 0 local currentheight = 0 local drawtext = false local currentcolor = TempContext:GetColor() for i = 1 , #linecollection do oldlinelocation = 0 linelocation = 0 stringbuilder = "" --build string till color change for stringlength = 1 , linecollection[3] do for ii =1 , #textcommands do if stringcount == textcommands[ii][2] then local currentcommand = string.upper(textcommands[ii][1][1]) if (currentcommand == "COLORRGB") == true then --context:SetColor(currentcolor) --context:DrawText(stringbuilder,x+oldlinelocation,y+currentheight) --oldlinelocation = linelocation --stringbuilder = "" drawtext = true end end if drawtext == true then TempContext:SetColor(currentcolor) TempContext:DrawText(stringbuilder,x+oldlinelocation,y+currentheight) oldlinelocation = linelocation stringbuilder = "" drawtext= false end if stringcount == textcommands[ii][2] then local currentcommand = string.upper(textcommands[ii][1][1]) if (currentcommand == "COLORRGB") == true then TempContext:SetColor( textcommands[ii][1][2]/255, textcommands[ii][1][3]/255, textcommands[ii][1][4]/255) currentcolor = TempContext:GetColor() end end end if drawtext == true then end currentletter = string.sub(workingstring,stringcount,stringcount) linelocation = linelocation + font:GetTextWidth(currentletter) if linelocation < width and (wraplines == false) then stringbuilder =stringbuilder .. currentletter elseif (wraplines == true) then stringbuilder =stringbuilder .. currentletter end stringcount = stringcount + 1 if currentheight > height then break end if stringlength == linecollection[3] then TempContext:DrawText(stringbuilder,x+oldlinelocation,y+currentheight) currentheight = currentheight + linecollection[2]+5 end end --print(string.sub(workingstring,stringcount,linecollection[3])) --stringcount =stringcount+ linecollection[3] end --os.exit() TempContext:SetColor(RGBAToVec4Scalar({oldcolor.r,oldcolor.g,oldcolor.b},256)) TempContext:SetBlendMode(tempblendmode) end function App:Loop() --If window has been closed, end the program if self.window:Closed() or self.window:KeyDown(Key.Escape) then return false end self.context:SetColor(Vec4(100/256,100/256,100/256,1)) self.context:DrawRect(50,200,300,180) -- need to set the box size manually drawtext2("##wraplines:true####colorrgb:256:0:0##Leadwerks Software##linebreak## ##colorrgb:256:256:256##Leadwerks Software was founded in 2006 to make game development fun and enjoyable.".. "The company launched Leadwerks 3, their first multiplatform product, in April at the GDC 2013 expo. Last summer, the company ".. "conducted a successful Kickstarter campaign to bring Leadwerks to the Linux operating system, reaching over 200% of their goal ".. "in just six weeks. A concurrent Greenlight campaign for Steam was also successful, making Leadwerks the first 3D game engine ".. "approved for distribution on Steam.",50,200,300,150,{255,255,255,255}) self.context:Sync(true) return true end 1 Quote Link to comment Share on other sites More sharing options...
IgorBgz90 Posted April 1, 2015 Author Share Posted April 1, 2015 Looks complicated . The idea is good. Quote Link to comment Share on other sites More sharing options...
beo6 Posted April 1, 2015 Share Posted April 1, 2015 nice scripts. i guess i now have to add automatic line-wrapping to my notes script. Quote Link to comment Share on other sites More sharing options...
beo6 Posted April 4, 2015 Share Posted April 4, 2015 Thank you again Igor, just added the line-wrapping to the notes script in the steam workshop. This is my implementation using 3 functions: -- trim spaces from string function Script:trim2(s) return s:match "^%s*(.-)%s*$" end -- wraps single line function Script:LineWrap( sourceText, maxTextWidth, font ) local tempText = "" local text = {} local lineCount = 0 for w in string.gmatch(sourceText, "[%p%a%d]+") do if font:GetTextWidth(tempText) < maxTextWidth then tempText = tempText.." "..w else --We need to find the last word, learn length, and delete it. local temp1 = string.reverse(tempText) local temp2 = string.sub(temp1, 1, string.find(temp1," ")) temp2 = string.reverse(temp2) local temp3 = string.sub(tempText, 1, string.len(tempText)-string.len(temp2)) --Add new line to list. text[lineCount] = temp3 lineCount = lineCount + 1 tempText = "" --Now we can add the last word to new line. tempText = tempText..temp2.." "..w end end text[lineCount] = tempText lineCount = lineCount + 1 tempText = "" return text end -- wraps Text function Script:WrapTextLines(text, width, font) local finalText = {} local tmpText = {} local count = 1 local countB = 1 for lKey,lValue in pairs(text) do if font:GetTextWidth(text[count]) > width then tmpText = self:LineWrap(text[count], width, font) for lKeyB,lValueB in pairs(tmpText) do table.insert(finalText, self:trim2(lValueB)) countB = countB + 1 end else table.insert(finalText, lValue) end count = count + 1 end return finalText end Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.