2
0
John Dodis 1 жил өмнө
parent
commit
f42e48716b
2 өөрчлөгдсөн 40 нэмэгдсэн , 3 устгасан
  1. 2 1
      main.lua
  2. 38 2
      ui2d/ui2d.lua

+ 2 - 1
main.lua

@@ -33,12 +33,13 @@ function lovr.draw( pass )
 	UI2D.End( pass )
 
 	UI2D.Begin( "second", 400, 200 )
+	UI2D.ProgressBar( 20 )
 	UI2D.Button( "first button2" )
-	UI2D.Button( "second button2" )
 	released, sl1 = UI2D.SliderInt( "a slider", sl1, 0, 100 )
 	if released then
 		print( released, sl1 )
 	end
+	UI2D.Button( "second button2" )
 	UI2D.End( pass )
 
 	UI2D.Begin( "third", 350, 240 )

+ 38 - 2
ui2d/ui2d.lua

@@ -203,6 +203,8 @@ local function Slider( type, name, v, v_min, v_max, width )
 	local bbox = {}
 	if layout.same_line then
 		bbox = { x = layout.x + layout.w + margin, y = layout.y, w = slider_w + margin + text_w, h = (2 * margin) + font.h }
+	elseif layout.same_column then
+		bbox = { x = layout.x, y = layout.y + layout.h + margin, w = slider_w + margin + text_w, h = (2 * margin) + font.h }
 	else
 		bbox = { x = margin, y = layout.y + layout.row_h + margin, w = slider_w + margin + text_w, h = (2 * margin) + font.h }
 	end
@@ -484,11 +486,15 @@ function UI2D.SameLine()
 	layout.same_line = true
 end
 
+function UI2D.SameColumn()
+	layout.same_column = true
+end
+
 function UI2D.Button( name, width, height )
 	local text = GetLabelPart( name )
 	local cur_window = windows[ begin_idx ]
-	local text_w = font.handle:getWidth( text )
-	local num_lines = GetLineCount( text )
+	local text_w = utf8.len( name ) * font.w
+	local num_lines = GetLineCount( name )
 
 	local bbox = {}
 	if layout.same_line then
@@ -539,6 +545,36 @@ function UI2D.SliderFloat( name, v, v_min, v_max, width, num_decimals )
 	return Slider( e_slider_type.float, name, v, v_min, v_max, width, num_decimals )
 end
 
+function UI2D.ProgressBar( progress, width )
+	if width and width >= (2 * margin) + (4 * font.w) then
+		width = width
+	else
+		width = 300
+	end
+
+	local bbox = {}
+	if layout.same_line then
+		bbox = { x = layout.x + layout.w + margin, y = layout.y, w = width, h = (2 * margin) + font.h }
+	elseif layout.same_column then
+		bbox = { x = layout.x, y = layout.y + layout.h, w = width, h = (2 * margin) + font.h }
+	else
+		bbox = { x = margin, y = layout.y + layout.row_h + margin, w = width, h = (2 * margin) + font.h }
+	end
+
+	UpdateLayout( bbox )
+
+	progress = Clamp( progress, 0, 100 )
+	local fill_w = math.floor( (width * progress) / 100 )
+	local str = progress .. "%"
+
+	table.insert( windows[ begin_idx ].command_list,
+		{ type = "rect_fill", bbox = { x = bbox.x, y = bbox.y, w = fill_w, h = bbox.h }, color = colors.progress_bar_fill } )
+	table.insert( windows[ begin_idx ].command_list,
+		{ type = "rect_fill", bbox = { x = bbox.x + fill_w, y = bbox.y, w = bbox.w - fill_w, h = bbox.h }, color = colors.progress_bar_bg } )
+	table.insert( windows[ begin_idx ].command_list, { type = "rect_wire", bbox = bbox, color = colors.progress_bar_border } )
+	table.insert( windows[ begin_idx ].command_list, { type = "text", text = str, bbox = bbox, color = colors.text } )
+end
+
 function UI2D.NewFrame( main_pass )
 	font.handle:setPixelDensity( 1.0 )
 end