Quellcode durchsuchen

Left align title. Truncate if it doesn't fit

John Dodis vor 1 Jahr
Ursprung
Commit
559f565767
2 geänderte Dateien mit 14 neuen und 5 gelöschten Zeilen
  1. 4 4
      main.lua
  2. 10 1
      ui2d/ui2d.lua

+ 4 - 4
main.lua

@@ -8,7 +8,7 @@ local sl2 = 10
 local txt = "sample text"
 
 function lovr.load()
-	UI2D.Init()
+	UI2D.Init(16)
 end
 
 function lovr.update( dt )
@@ -24,7 +24,7 @@ function lovr.draw( pass )
 	pass:setProjection( 1, mat4():orthographic( pass:getDimensions() ) )
 	UI2D.NewFrame( pass )
 
-	UI2D.Begin( "first", 300, 300 )
+	UI2D.Begin( "Χρόνος", 300, 300 )
 	if UI2D.Button( "first button" ) then
 		print( "from 1st button" )
 	end
@@ -47,7 +47,7 @@ function lovr.draw( pass )
 	released, sl2 = UI2D.SliderInt( "hello", sl2, 0, 100 )
 	UI2D.End( pass )
 
-	UI2D.Begin( "time", 150, 100 )
+	UI2D.Begin( "abcdefghij123", 150, 100 )
 	UI2D.Button( txt )
 	UI2D.End( pass )
 
@@ -56,6 +56,6 @@ function lovr.draw( pass )
 	pass:setColor( 1, 0, 0 )
 	pass:plane( 100, 100, 0, 100, 100 )
 	table.insert( ui_passes, pass )
-	print( #ui_passes )
+	-- print( #ui_passes )
 	return lovr.graphics.submit( ui_passes )
 end

+ 10 - 1
ui2d/ui2d.lua

@@ -1,3 +1,4 @@
+local utf8 = require "utf8"
 local UI2D = {}
 
 local e_mouse_state = { clicked = 1, held = 2, released = 3, idle = 4 }
@@ -337,8 +338,16 @@ function UI2D.End( main_pass )
 	table.insert( windows[ begin_idx ].command_list,
 		{ type = "rect_fill", bbox = { x = 0, y = 0, w = cur_window.w, h = (2 * margin) + font.h }, color = title_col } )
 
+	local txt = cur_window.title
+	local title_w = utf8.len( txt ) * font.w
+	if title_w > cur_window.w - (2 * margin) then -- Truncate title
+		local num_chars = ((cur_window.w - (2 * margin)) / font.w) - 3
+		txt = string.sub( txt, 1, num_chars ) .. "..."
+		title_w = utf8.len( txt ) * font.w
+	end
+
 	table.insert( windows[ begin_idx ].command_list,
-		{ type = "text", text = cur_window.title, bbox = { x = 0, y = 0, w = cur_window.w, h = (2 * margin) + font.h }, color = colors.text } )
+		{ type = "text", text = txt, bbox = { x = margin, y = 0, w = title_w, h = (2 * margin) + font.h }, color = colors.text } )
 
 	table.insert( windows[ begin_idx ].command_list,
 		{ type = "rect_wire", bbox = { x = 0, y = 0, w = cur_window.w, h = cur_window.h }, color = colors.window_border } )