Text wrapping algorithm

master
E. Almqvist 5 years ago
parent 903982e57a
commit b08cdecbc6
  1. 68
      gamemode/engine/derma/lib/cl_menu_dialogueBox.lua
  2. 2
      gamemode/engine/lib/client/cl_cinematic.lua
  3. 4
      gamemode/settings/sh_dialogues.lua

@ -5,8 +5,6 @@
-- \ \ / ____ \| | | | | | | | __/ (__| | | | / / -- \ \ / ____ \| | | | | | | | __/ (__| | | | / /
-- \_\ /_/ \_\_|_| |_| |_|_|\___|\___|_| |_| /_/ -- \_\ /_/ \_\_|_| |_| |_|_|\___|\___|_| |_| /_/
-- This is mainly used for cinematic, do not confuse it with NPC dialogue
local log = {} local log = {}
local scale = Quantum.Client.ResolutionScale local scale = Quantum.Client.ResolutionScale
local padding = 10 * scale local padding = 10 * scale
@ -14,18 +12,53 @@ local padding_s = 4 * scale
local theme = Quantum.Client.Menu.GetAPI( "theme" ) local theme = Quantum.Client.Menu.GetAPI( "theme" )
function log.genTextLinebreak( txt, font, maxW )
local wordlist = string.Split( txt, " " ) -- all words are seperated by spaces
local lines = {}
surface.SetFont(font)
for i=1, #wordlist do -- loop through all of the words
local str = wordlist[i] -- "workspace string"
local wordsUsed = {i}
for n=i-1, 1, -1 do -- add all words behind i
if(wordlist[n] != nil) then
str = wordlist[n] .. " " .. str
table.insert(wordsUsed, n) -- remove them all later from wordlist
end
end
local add = ""
if( wordlist[i+1] != nil ) then add = " " .. wordlist[i+1] end
if(surface.GetTextSize(str .. add) >= maxW) then
table.insert(lines, str .. "\n")
for _, wordI in pairs(wordsUsed) do
wordlist[wordI] = nil
end
end
end
return lines
end
function log.appendLinesToStr(tbl)
local out = ""
for _, l in SortedPairs(tbl) do
out = out .. l
end
return out
end
function log.createDialogueBox( logdata, parent ) function log.createDialogueBox( logdata, parent )
cinematic = cinematic || true cinematic = cinematic || true
local fw, fh = parent:GetSize() local fw, fh = parent:GetSize()
local logtext = logdata["init"].question local logtext = logdata["init"].question
local box = vgui.Create( "DPanel", parent ) local box = vgui.Create( "DPanel", parent )
box:SetSize( 775 * scale, 80 * scale ) local maxW, maxH = 775 * scale, 160 * scale
box.Paint = theme.sharpblurpanel( self ) box:SetSize( maxW, maxH )
box.w, box.h = box:GetSize() box.Paint = function( self, w, h )
box:SetPos( fw/2 - box.w/2, fh*0.7 - box.h/2 ) surface.SetDrawColor( 20, 20, 20, 220 )
box.x, box.y = box:GetPos() surface.DrawRect( 0, 0, w, h )
end
local scroll = vgui.Create( "DScrollPanel", box ) local scroll = vgui.Create( "DScrollPanel", box )
scroll:SetSize( box:GetSize() ) scroll:SetSize( box:GetSize() )
scroll.Paint = function( self ) end scroll.Paint = function( self ) end
@ -36,18 +69,27 @@ function log.createDialogueBox( logdata, parent )
end end
sb.btnUp:SetSize(0,0) sb.btnUp:SetSize(0,0)
sb.btnDown:SetSize(0,0) sb.btnDown:SetSize(0,0)
scroll:SetSize( box:GetSize() )
scroll.w, scroll.h = scroll:GetSize() scroll.w, scroll.h = scroll:GetSize()
local text = vgui.Create( "DLabel", scroll ) local text = vgui.Create( "DLabel", scroll )
text:SetText( logtext )
text:SetFont( "q_dialogue_question" ) text:SetFont( "q_dialogue_question" )
text.lines = log.genTextLinebreak( logtext, text:GetFont(), maxW - (padding) )
text:SetText(log.appendLinesToStr(text.lines))
text:SetTextColor( Color( 240, 240, 240, 255 ) ) text:SetTextColor( Color( 240, 240, 240, 255 ) )
text:SetSize( scroll.w * 0.95, scroll.h * 0.95 ) text:SetWidth( maxW )
text:SetWrap( true ) text:SizeToContentsY()
text.w, text.h = text:GetSize() text.w, text.h = text:GetSize()
text:SetPos(padding, padding)
text:SetPos( scroll.w/2 - text.w/2, 0 )
box:SizeToChildren( false, true )
box.w, box.h = box:GetSize()
if( box.h > maxH ) then
box:SetHeight(maxH)
end
box:SetPos( fw/2 - box.w/2, fh*0.7 )
return box return box
end end

@ -32,7 +32,7 @@ function Quantum.Client.Cam.Start( scene, loop, drawviewer )
hook.Remove( "CalcView", "Quantum_Cinematic" ) -- if a cinematic is already running; cancel it hook.Remove( "CalcView", "Quantum_Cinematic" ) -- if a cinematic is already running; cancel it
hook.Add( "CalcView", "Quantum_Cinematic", function( ply, pos, ang, fov ) hook.Add( "CalcView", "Quantum_Cinematic", function( ply, pos, ang, fov )
time = scene[Quantum.Client.Cam.Temp.scene_index].velocity || 5 time = scene[Quantum.Client.Cam.Temp.scene_index].velocity || 5
fov = scene[Quantum.Client.Cam.Temp.scene_index].fov || 20 fov = scene[Quantum.Client.Cam.Temp.scene_index].fov || 20
frac = math.Clamp( frac + FrameTime()/time, 0, 1 ) frac = math.Clamp( frac + FrameTime()/time, 0, 1 )

@ -9,7 +9,7 @@
---- CREATE DIALOGUE INSTANCES UNDER THIS LINE ---- ---- CREATE DIALOGUE INSTANCES UNDER THIS LINE ----
Quantum.Dialogue.Create( "npc_generalvendor" ) Quantum.Dialogue.Create( "npc_generalvendor" )
Quantum.Dialogue.AddQuestion( "npc_generalvendor", "init", "What do you want?" ) Quantum.Dialogue.AddQuestion( "npc_generalvendor", "init", "What do you want? I don't have time for any shenanigans right now! Go away! REEEEEEEEEEEEEE! Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur mi leo, blandit in ullamcorper sit amet, tristique in purus. Morbi vehicula suscipit faucibus. Quisque ultricies nisl magna, quis facilisis sem aliquam id. Integer non est sagittis, laoreet ipsum et, euismod odio. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. In sollicitudin turpis eget libero maximus commodo." )
Quantum.Dialogue.AddResponse( "npc_generalvendor", "init", { Quantum.Dialogue.AddResponse( "npc_generalvendor", "init", {
text = "To do business.", text = "To do business.",
@ -33,4 +33,4 @@ Quantum.Dialogue.Create( "npc_generalvendor" )
func = function() func = function()
print("You pressed this response! SELL") print("You pressed this response! SELL")
end end
}, 2 ) }, 2 )

Loading…
Cancel
Save