Browse Source

Allow released to fire when outside CustomWidget

Assert if Begin/End pairs don't match.
John Dodis 1 year ago
parent
commit
be959a7ef7
1 changed files with 16 additions and 8 deletions
  1. 16 8
      ui2d/ui2d.lua

+ 16 - 8
ui2d/ui2d.lua

@@ -15,6 +15,7 @@ local begin_idx = nil
 local margin = 8
 local margin = 8
 local next_z = 0
 local next_z = 0
 local separator_thickness = 2
 local separator_thickness = 2
+local begin_end_pairs = { b = 0, e = 0 }
 local windows = {}
 local windows = {}
 local color_themes = {}
 local color_themes = {}
 local overriden_colors = {}
 local overriden_colors = {}
@@ -508,6 +509,8 @@ function UI2D.Begin( name, x, y, is_modal )
 	if idx > 0 then
 	if idx > 0 then
 		windows[ idx ].was_called_this_frame = true
 		windows[ idx ].was_called_this_frame = true
 	end
 	end
+
+	begin_end_pairs.b = begin_end_pairs.b + 1
 end
 end
 
 
 function UI2D.End( main_pass )
 function UI2D.End( main_pass )
@@ -608,6 +611,7 @@ function UI2D.End( main_pass )
 	end
 	end
 
 
 	ResetLayout()
 	ResetLayout()
+	begin_end_pairs.e = begin_end_pairs.e + 1
 end
 end
 
 
 function UI2D.SetWindowPosition( id, x, y )
 function UI2D.SetWindowPosition( id, x, y )
@@ -1034,7 +1038,7 @@ function UI2D.TextBox( name, num_visible_chars, text )
 	if layout.same_line then
 	if layout.same_line then
 		bbox = { x = layout.x + layout.w + margin, y = layout.y, w = (4 * margin) + (num_visible_chars * font.w) + label_w, h = (2 * margin) + font.h }
 		bbox = { x = layout.x + layout.w + margin, y = layout.y, w = (4 * margin) + (num_visible_chars * font.w) + label_w, h = (2 * margin) + font.h }
 	elseif layout.same_column then
 	elseif layout.same_column then
-		bbox = { x = layout.x, y = layout.y + layout.h + margin, w = (4 * margin) + (num_visible_chars * font.w) + label_w, h = (2 * margin) + font.h  }
+		bbox = { x = layout.x, y = layout.y + layout.h + margin, w = (4 * margin) + (num_visible_chars * font.w) + label_w, h = (2 * margin) + font.h }
 	else
 	else
 		bbox = { x = margin, y = layout.y + layout.row_h + margin, w = (4 * margin) + (num_visible_chars * font.w) + label_w, h = (2 * margin) + font.h }
 		bbox = { x = margin, y = layout.y + layout.row_h + margin, w = (4 * margin) + (num_visible_chars * font.w) + label_w, h = (2 * margin) + font.h }
 	end
 	end
@@ -1449,15 +1453,16 @@ function UI2D.CustomWidget( name, width, height )
 
 
 			if mouse.state == e_mouse_state.clicked then
 			if mouse.state == e_mouse_state.clicked then
 				clicked = true
 				clicked = true
+				active_widget = cur_window.cw[ idx ]
 			end
 			end
+		end
 
 
-			if mouse.state == e_mouse_state.held then
-				held = true
-			end
-
-			if mouse.state == e_mouse_state.released then
-				released = true
-			end
+		if mouse.state == e_mouse_state.held and cur_window == active_window and active_widget == cur_window.cw[ idx ] then
+			held = true
+		end
+		if mouse.state == e_mouse_state.released and cur_window == active_window and active_widget == cur_window.cw[ idx ] then
+			released = true
+			active_widget = nil
 		end
 		end
 	end
 	end
 
 
@@ -1473,6 +1478,9 @@ function UI2D.NewFrame( main_pass )
 end
 end
 
 
 function UI2D.RenderFrame( main_pass )
 function UI2D.RenderFrame( main_pass )
+	assert( begin_end_pairs.b == begin_end_pairs.e, "Begin/End pairs don't match! Begin calls: " .. begin_end_pairs.b .. " - End calls: " .. begin_end_pairs.e )
+	begin_end_pairs.b = 0
+	begin_end_pairs.e = 0
 	table.sort( windows, function( a, b ) return a.z > b.z end )
 	table.sort( windows, function( a, b ) return a.z > b.z end )
 
 
 	local count = #windows
 	local count = #windows