Parcourir la source

checkbox/radiobutton

John Dodis il y a 1 an
Parent
commit
78a0d45665
2 fichiers modifiés avec 93 ajouts et 0 suppressions
  1. 18 0
      main.lua
  2. 75 0
      ui2d/ui2d.lua

+ 18 - 0
main.lua

@@ -9,6 +9,9 @@ local sl3 = 10.3
 local txt = "sample text"
 local icon = lovr.graphics.newTexture( "ui2d/lovrlogo.png" )
 local tab_bar_idx = 1
+local check1 = true
+local check2 = false
+local rb_idx = 1
 
 function lovr.load()
 	UI2D.Init( 16 )
@@ -34,6 +37,15 @@ function lovr.draw( pass )
 	if UI2D.ImageButton( icon, 40, 40 ) then
 		print( "img" )
 	end
+	if UI2D.RadioButton( "Radio1", rb_idx == 1 ) then
+		rb_idx = 1
+	end
+	if UI2D.RadioButton( "Radio2", rb_idx == 2 ) then
+		rb_idx = 2
+	end
+	if UI2D.RadioButton( "Radio3", rb_idx == 3 ) then
+		rb_idx = 3
+	end
 	UI2D.Button( "second button" )
 	UI2D.End( pass )
 
@@ -58,6 +70,12 @@ function lovr.draw( pass )
 
 	UI2D.Begin( "fourth", 250, 250 )
 	UI2D.Button( txt )
+	if UI2D.CheckBox( "Really?", check1 ) then
+		check1 = not check1
+	end
+	if UI2D.CheckBox( "Another check", check2 ) then
+		check2 = not check2
+	end
 	released, sl3 = UI2D.SliderFloat( "hello", sl3, 0, 100, 300 )
 	UI2D.End( pass )
 

+ 75 - 0
ui2d/ui2d.lua

@@ -728,6 +728,81 @@ function UI2D.Label( text, compact )
 	table.insert( windows[ begin_idx ].command_list, { type = "text", text = text, bbox = bbox, color = colors.text } )
 end
 
+function UI2D.CheckBox( text, checked )
+	local cur_window = windows[ begin_idx ]
+	local text_w = font.handle:getWidth( text )
+
+	local bbox = {}
+	if layout.same_line then
+		bbox = { x = layout.x + layout.w + margin, y = layout.y, w = font.h + margin + text_w, h = (2 * margin) + font.h }
+	else
+		bbox = { x = margin, y = layout.y + layout.row_h + margin, w = font.h + margin + text_w, h = (2 * margin) + font.h }
+	end
+
+	UpdateLayout( bbox )
+
+	local result = false
+	local col = colors.check_border
+
+	if not modal_window or (modal_window and modal_window == cur_window.id) then
+		if PointInRect( mouse.x, mouse.y, bbox.x + cur_window.x, bbox.y + cur_window.y, bbox.w, bbox.h ) and cur_window == active_window then
+			col = colors.check_border_hover
+			if mouse.state == e_mouse_state.clicked then
+				result = true
+			end
+		end
+	end
+
+	local check_rect = { x = bbox.x, y = bbox.y + margin, w = font.h, h = font.h }
+	local text_rect = { x = bbox.x + font.h + margin, y = bbox.y, w = text_w + margin, h = bbox.h }
+	table.insert( windows[ begin_idx ].command_list, { type = "rect_wire", bbox = check_rect, color = col } )
+	table.insert( windows[ begin_idx ].command_list, { type = "text", text = text, bbox = text_rect, color = colors.text } )
+
+	if checked and type( checked ) == "boolean" then
+		table.insert( windows[ begin_idx ].command_list, { type = "text", text = "✔", bbox = check_rect, color = colors.check_mark } )
+	end
+
+	return result
+end
+
+function UI2D.RadioButton( text, checked )
+	local cur_window = windows[ begin_idx ]
+	local text_w = font.handle:getWidth( text )
+
+	local bbox = {}
+	if layout.same_line then
+		bbox = { x = layout.x + layout.w + margin, y = layout.y, w = font.h + margin + text_w, h = (2 * margin) + font.h }
+	else
+		bbox = { x = margin, y = layout.y + layout.row_h + margin, w = font.h + margin + text_w, h = (2 * margin) + font.h }
+	end
+
+	UpdateLayout( bbox )
+
+	local result = false
+	local col = colors.radio_border
+
+	if not modal_window or (modal_window and modal_window == cur_window.id) then
+		if PointInRect( mouse.x, mouse.y, bbox.x + cur_window.x, bbox.y + cur_window.y, bbox.w, bbox.h ) and cur_window == active_window then
+			col = colors.radio_border_hover
+
+			if mouse.state == e_mouse_state.clicked then
+				result = true
+			end
+		end
+	end
+
+	local check_rect = { x = bbox.x, y = bbox.y + margin, w = font.h, h = font.h }
+	local text_rect = { x = bbox.x + font.h + margin, y = bbox.y, w = text_w + margin, h = bbox.h }
+	table.insert( windows[ begin_idx ].command_list, { type = "circle_wire", bbox = check_rect, color = col } )
+	table.insert( windows[ begin_idx ].command_list, { type = "text", text = text, bbox = text_rect, color = colors.text } )
+
+	if checked and type( checked ) == "boolean" then
+		table.insert( windows[ begin_idx ].command_list, { type = "circle_fill", bbox = check_rect, color = colors.radio_mark } )
+	end
+
+	return result
+end
+
 function UI2D.NewFrame( main_pass )
 	font.handle:setPixelDensity( 1.0 )
 end