Explorar o código

finish slider combine

John Dodis hai 1 ano
pai
achega
b1d8fe9db0
Modificáronse 2 ficheiros con 24 adicións e 68 borrados
  1. 5 3
      main.lua
  2. 19 65
      ui2d/ui2d.lua

+ 5 - 3
main.lua

@@ -5,6 +5,7 @@ local win1_pos_x = 300
 local win1_pos_y = 300
 local sl1 = 20
 local sl2 = 10
+local sl3 = 10.3
 local txt = "sample text"
 
 function lovr.load()
@@ -24,7 +25,7 @@ function lovr.draw( pass )
 	pass:setProjection( 1, mat4():orthographic( pass:getDimensions() ) )
 	UI2D.NewFrame( pass )
 
-	UI2D.Begin( "Χρόνος", 300, 300 )
+	UI2D.Begin( "first", 300, 300 )
 	if UI2D.Button( "first button" ) then
 		print( "from 1st button" )
 	end
@@ -40,15 +41,16 @@ function lovr.draw( pass )
 	end
 	UI2D.End( pass )
 
-	UI2D.Begin( "third##blah", 350, 240 )
+	UI2D.Begin( "third", 350, 240 )
 	UI2D.Button( "blah1" )
 	UI2D.Button( "blah2" )
 	UI2D.SameLine()
 	released, sl2 = UI2D.SliderInt( "hello", sl2, 0, 100 )
 	UI2D.End( pass )
 
-	UI2D.Begin( "abcdefghij123", 150, 100 )
+	UI2D.Begin( "fourth", 250, 250 )
 	UI2D.Button( txt )
+	released, sl3 = UI2D.SliderFloat( "hello", sl3, 0, 100, 300 )
 	UI2D.End( pass )
 
 	local ui_passes = UI2D.RenderFrame( pass )

+ 19 - 65
ui2d/ui2d.lua

@@ -2,23 +2,22 @@ local utf8 = require "utf8"
 local UI2D = {}
 
 local e_mouse_state = { clicked = 1, held = 2, released = 3, idle = 4 }
+local e_slider_type = { int = 1, float = 2 }
 local modal_window = nil
 local active_window = nil
 local active_widget = nil
 local dragged_window = nil
-local dragged_window_offset = { x = 0, y = 0 }
 local begin_idx = nil
 local margin = 8
-
 local separator_thickness = 4
-local font = { handle = nil, w = nil, h = nil }
 local windows = {}
 local color_themes = {}
+local font = { handle = nil, w = nil, h = nil }
+local dragged_window_offset = { x = 0, y = 0 }
 local mouse = { x = 0, y = 0, state = e_mouse_state.idle, prev_frame = 0, this_frame = 0 }
 local texture_flags = { mipmaps = true, usage = { 'sample', 'render', 'transfer' } }
 local layout = { x = 0, y = 0, w = 0, h = 0, row_h = 0, total_w = 0, total_h = 0, same_line = false, same_column = false }
 local clamp_sampler = lovr.graphics.newSampler( { wrap = 'clamp' } )
-local e_slider_type = { int = 1, float = 2 }
 
 color_themes.dark =
 {
@@ -208,7 +207,7 @@ local function Slider( type, name, v, v_min, v_max, width )
 		bbox = { x = margin, y = layout.y + layout.row_h + margin, w = slider_w + margin + text_w, h = (2 * margin) + font.h }
 	end
 
-	if width  and width > bbox.w then
+	if width and width > bbox.w then
 		bbox.w = width
 		slider_w = width - margin - text_w
 	end
@@ -230,8 +229,12 @@ local function Slider( type, name, v, v_min, v_max, width )
 
 	if mouse.state == e_mouse_state.held and active_widget == cur_window.id .. name and cur_window == active_window then
 		v = MapRange( bbox.x + 2, bbox.x + slider_w - 2, v_min, v_max, mouse.x - cur_window.x )
-		v = Clamp( math.ceil( v ), v_min, v_max )
-		if v == 0 then v = 0 end
+		if type == e_slider_type.float then
+			v = Clamp( v, v_min, v_max )
+		else
+			v = Clamp( math.ceil( v ), v_min, v_max )
+			if v == 0 then v = 0 end
+		end
 	end
 	if mouse.state == e_mouse_state.released and active_widget == cur_window.id .. name then
 		active_widget = nil
@@ -245,10 +248,18 @@ local function Slider( type, name, v, v_min, v_max, width )
 	local thumb_pos = MapRange( v_min, v_max, bbox.x, bbox.x + slider_w - font.h, v )
 	local thumb_rect = { x = thumb_pos, y = bbox.y + (bbox.h / 2) - (font.h / 2), w = font.h, h = font.h }
 
+	local value
+	if type == e_slider_type.float then
+		num_decimals = num_decimals or 2
+		local str_fmt = "%." .. num_decimals .. "f"
+		value = string.format( str_fmt, v )
+	else
+		value = v
+	end
 	table.insert( windows[ begin_idx ].command_list, { type = "rect_fill", bbox = slider_rect, color = col } )
 	table.insert( windows[ begin_idx ].command_list, { type = "rect_fill", bbox = thumb_rect, color = colors.slider_thumb } )
 	table.insert( windows[ begin_idx ].command_list, { type = "text", text = text, bbox = text_label_rect, color = colors.text } )
-	table.insert( windows[ begin_idx ].command_list, { type = "text", text = v, bbox = text_value_rect, color = colors.text } )
+	table.insert( windows[ begin_idx ].command_list, { type = "text", text = value, bbox = text_value_rect, color = colors.text } )
 	return result, v
 end
 
@@ -520,63 +531,6 @@ function UI2D.Button( name, width, height )
 	return result
 end
 
--- function UI2D.SliderInt( name, v, v_min, v_max, width )
--- 	local text = GetLabelPart( name )
--- 	local cur_window = windows[ begin_idx ]
--- 	local text_w = font.handle:getWidth( text )
-
--- 	local slider_w = 10 * font.w
--- 	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 }
--- 	else
--- 		bbox = { x = margin, y = layout.y + layout.row_h + margin, w = slider_w + margin + text_w, h = (2 * margin) + font.h }
--- 	end
-
--- 	if width and type( width ) == "number" and width > bbox.w then
--- 		bbox.w = width
--- 		slider_w = width - margin - text_w
--- 	end
-
--- 	UpdateLayout( bbox )
-
--- 	local col = colors.slider_bg
--- 	local result = false
-
--- 	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, slider_w, bbox.h ) and cur_window == active_window then
--- 			col = colors.slider_bg_hover
-
--- 			if mouse.state == e_mouse_state.clicked then
--- 				active_widget = cur_window.id .. name
--- 			end
--- 		end
--- 	end
-
--- 	if mouse.state == e_mouse_state.held and active_widget == cur_window.id .. name and cur_window == active_window then
--- 		v = MapRange( bbox.x + 2, bbox.x + slider_w - 2, v_min, v_max, mouse.x - cur_window.x )
--- 		v = Clamp( math.ceil( v ), v_min, v_max )
--- 		if v == 0 then v = 0 end
--- 	end
--- 	if mouse.state == e_mouse_state.released and active_widget == cur_window.id .. name then
--- 		active_widget = nil
--- 		result = true
--- 	end
-
--- 	local value_text_w = font.handle:getWidth( v )
--- 	local text_label_rect = { x = bbox.x + slider_w + margin, y = bbox.y, w = text_w, h = bbox.h }
--- 	local text_value_rect = { x = bbox.x, y = bbox.y, w = slider_w, h = bbox.h }
--- 	local slider_rect = { x = bbox.x, y = bbox.y + (bbox.h / 2) - (font.h / 2), w = slider_w, h = font.h }
--- 	local thumb_pos = MapRange( v_min, v_max, bbox.x, bbox.x + slider_w - font.h, v )
--- 	local thumb_rect = { x = thumb_pos, y = bbox.y + (bbox.h / 2) - (font.h / 2), w = font.h, h = font.h }
-
--- 	table.insert( windows[ begin_idx ].command_list, { type = "rect_fill", bbox = slider_rect, color = col } )
--- 	table.insert( windows[ begin_idx ].command_list, { type = "rect_fill", bbox = thumb_rect, color = colors.slider_thumb } )
--- 	table.insert( windows[ begin_idx ].command_list, { type = "text", text = text, bbox = text_label_rect, color = colors.text } )
--- 	table.insert( windows[ begin_idx ].command_list, { type = "text", text = v, bbox = text_value_rect, color = colors.text } )
--- 	return result, v
--- end
-
 function UI2D.SliderInt( name, v, v_min, v_max, width )
 	return Slider( e_slider_type.int, name, v, v_min, v_max, width )
 end