checking logic

This commit is contained in:
Michael Freno
2025-10-31 12:18:39 -04:00
parent 9f215e252e
commit 747382614b

View File

@@ -840,7 +840,7 @@ function ImageCache.load(imagePath, loadImageData)
-- Cache the image
ImageCache._cache[normalizedPath] = {
image = image,
imageData = imgData
imageData = imgData,
}
return image, nil
@@ -927,7 +927,7 @@ function ImageCache.getStats()
return {
count = count,
memoryEstimate = memoryEstimate
memoryEstimate = memoryEstimate,
}
end
@@ -957,16 +957,16 @@ function ImageRenderer.calculateFit(imageWidth, imageHeight, boundsWidth, bounds
end
local result = {
sx = 0, -- Source X
sy = 0, -- Source Y
sw = imageWidth, -- Source width
sh = imageHeight, -- Source height
dx = 0, -- Destination X
dy = 0, -- Destination Y
dw = boundsWidth, -- Destination width
sx = 0, -- Source X
sy = 0, -- Source Y
sw = imageWidth, -- Source width
sh = imageHeight, -- Source height
dx = 0, -- Destination X
dy = 0, -- Destination Y
dw = boundsWidth, -- Destination width
dh = boundsHeight, -- Destination height
scaleX = 1, -- Scale factor X
scaleY = 1 -- Scale factor Y
scaleX = 1, -- Scale factor X
scaleY = 1, -- Scale factor Y
}
-- Calculate based on fit mode
@@ -976,7 +976,6 @@ function ImageRenderer.calculateFit(imageWidth, imageHeight, boundsWidth, bounds
result.scaleY = boundsHeight / imageHeight
result.dw = boundsWidth
result.dh = boundsHeight
elseif fitMode == "contain" then
-- Scale to fit within bounds (preserves aspect ratio)
local scale = math.min(boundsWidth / imageWidth, boundsHeight / imageHeight)
@@ -989,7 +988,6 @@ function ImageRenderer.calculateFit(imageWidth, imageHeight, boundsWidth, bounds
local posX, posY = ImageRenderer._parsePosition(objectPosition)
result.dx = (boundsWidth - result.dw) * posX
result.dy = (boundsHeight - result.dh) * posY
elseif fitMode == "cover" then
-- Scale to cover bounds (preserves aspect ratio, may crop)
local scale = math.max(boundsWidth / imageWidth, boundsHeight / imageHeight)
@@ -1016,7 +1014,6 @@ function ImageRenderer.calculateFit(imageWidth, imageHeight, boundsWidth, bounds
result.dy = 0
result.dw = boundsWidth
result.dh = boundsHeight
elseif fitMode == "none" then
-- Use natural size (no scaling)
result.scaleX = 1
@@ -1028,7 +1025,6 @@ function ImageRenderer.calculateFit(imageWidth, imageHeight, boundsWidth, bounds
local posX, posY = ImageRenderer._parsePosition(objectPosition)
result.dx = (boundsWidth - imageWidth) * posX
result.dy = (boundsHeight - imageHeight) * posY
elseif fitMode == "scale-down" then
-- Use none or contain, whichever is smaller
if imageWidth <= boundsWidth and imageHeight <= boundsHeight then
@@ -1038,7 +1034,6 @@ function ImageRenderer.calculateFit(imageWidth, imageHeight, boundsWidth, bounds
-- Image too large, use "contain"
return ImageRenderer.calculateFit(imageWidth, imageHeight, boundsWidth, boundsHeight, "contain", objectPosition)
end
else
error(formatError("ImageRenderer", string.format("Invalid fit mode: '%s'. Must be one of: fill, contain, cover, scale-down, none", tostring(fitMode))))
end
@@ -1065,11 +1060,11 @@ function ImageRenderer._parsePosition(position)
if #parts == 1 then
local val = parts[1]
if val == "left" or val == "right" then
parts = {val, "center"}
parts = { val, "center" }
elseif val == "top" or val == "bottom" then
parts = {"center", val}
parts = { "center", val }
else
parts = {val, val}
parts = { val, val }
end
elseif #parts == 0 then
return 0.5, 0.5 -- Default to center
@@ -1077,9 +1072,12 @@ function ImageRenderer._parsePosition(position)
local function parseValue(val)
-- Handle keywords
if val == "center" then return 0.5
elseif val == "left" or val == "top" then return 0
elseif val == "right" or val == "bottom" then return 1
if val == "center" then
return 0.5
elseif val == "left" or val == "top" then
return 0
elseif val == "right" or val == "bottom" then
return 1
end
-- Handle percentages
@@ -2340,6 +2338,29 @@ function Gui.init(config)
end
end
--- Check for Z-index coverage (occlusion)
---@param elem Element
---@param clickX number
---@param clickY number
---@return boolean
function Gui.isOccluded(elem, clickX, clickY)
for _, element in ipairs(Gui.topElements) do
if element.z > elem.z and element:contains(clickX, clickY) then
return true
end
--TODO: check if walking the children tree is necessary here - might only need to check for absolute positioned
--children
for _, child in ipairs(element.children) do
if child.positioning == "absolute" then
if child.z > elem.z and child:contains(clickX, clickY) then
return true
end
end
end
end
return false
end
--- Get current scale factors
---@return number, number -- scaleX, scaleY
function Gui.getScaleFactors()
@@ -2591,14 +2612,15 @@ function Gui.wheelmoved(x, y)
-- Check children first (depth-first)
if #element.children > 0 then
local childResult = findScrollableAtPosition(element.children, mx, my)
if childResult then return childResult end
if childResult then
return childResult
end
end
-- Check if this element is scrollable
local overflowX = element.overflowX or element.overflow
local overflowY = element.overflowY or element.overflow
if (overflowX == "scroll" or overflowX == "auto" or overflowY == "scroll" or overflowY == "auto") and
(element._overflowX or element._overflowY) then
if (overflowX == "scroll" or overflowX == "auto" or overflowY == "scroll" or overflowY == "auto") and (element._overflowX or element._overflowY) then
return element
end
end
@@ -4142,8 +4164,8 @@ function Element.new(props)
-- Scrollbar interaction state
self._scrollbarHovered = false
self._scrollbarDragging = false
self._hoveredScrollbar = nil -- "vertical" or "horizontal"
self._scrollbarDragOffset = 0 -- Offset from thumb top when drag started
self._hoveredScrollbar = nil -- "vertical" or "horizontal"
self._scrollbarDragOffset = 0 -- Offset from thumb top when drag started
return self
end
@@ -4154,6 +4176,15 @@ function Element:getBounds()
return { x = self.x, y = self.y, width = self:getBorderBoxWidth(), height = self:getBorderBoxHeight() }
end
--- Check if point is inside element bounds
--- @param x number
--- @param y number
--- @return boolean
function Element:contains(x, y)
local bounds = self:getBounds()
return bounds.x <= x and bounds.y <= y and bounds.x + bounds.width >= x and bounds.y + bounds.height >= y
end
--- Get border-box width (including padding)
---@return number
function Element:getBorderBoxWidth()
@@ -4246,7 +4277,7 @@ end
function Element:_calculateScrollbarDimensions()
local result = {
vertical = { visible = false, trackHeight = 0, thumbHeight = 0, thumbY = 0 },
horizontal = { visible = false, trackWidth = 0, thumbWidth = 0, thumbX = 0 }
horizontal = { visible = false, trackWidth = 0, thumbWidth = 0, thumbX = 0 },
}
local overflowX = self.overflowX or self.overflow
@@ -4307,20 +4338,10 @@ function Element:_drawScrollbars(dims)
local thumbColor = self.scrollbarColor
if self._scrollbarDragging then
-- Active state: brighter
thumbColor = Color.new(
math.min(1, thumbColor.r * 1.4),
math.min(1, thumbColor.g * 1.4),
math.min(1, thumbColor.b * 1.4),
thumbColor.a
)
thumbColor = Color.new(math.min(1, thumbColor.r * 1.4), math.min(1, thumbColor.g * 1.4), math.min(1, thumbColor.b * 1.4), thumbColor.a)
elseif self._scrollbarHovered then
-- Hover state: slightly brighter
thumbColor = Color.new(
math.min(1, thumbColor.r * 1.2),
math.min(1, thumbColor.g * 1.2),
math.min(1, thumbColor.b * 1.2),
thumbColor.a
)
thumbColor = Color.new(math.min(1, thumbColor.r * 1.2), math.min(1, thumbColor.g * 1.2), math.min(1, thumbColor.b * 1.2), thumbColor.a)
end
-- Vertical scrollbar
@@ -4330,13 +4351,11 @@ function Element:_drawScrollbars(dims)
-- Draw track
love.graphics.setColor(self.scrollbarTrackColor:toRGBA())
love.graphics.rectangle("fill", trackX, trackY,
self.scrollbarWidth, dims.vertical.trackHeight, self.scrollbarRadius)
love.graphics.rectangle("fill", trackX, trackY, self.scrollbarWidth, dims.vertical.trackHeight, self.scrollbarRadius)
-- Draw thumb with state-based color
love.graphics.setColor(thumbColor:toRGBA())
love.graphics.rectangle("fill", trackX, trackY + dims.vertical.thumbY,
self.scrollbarWidth, dims.vertical.thumbHeight, self.scrollbarRadius)
love.graphics.rectangle("fill", trackX, trackY + dims.vertical.thumbY, self.scrollbarWidth, dims.vertical.thumbHeight, self.scrollbarRadius)
end
-- Horizontal scrollbar
@@ -4346,13 +4365,11 @@ function Element:_drawScrollbars(dims)
-- Draw track
love.graphics.setColor(self.scrollbarTrackColor:toRGBA())
love.graphics.rectangle("fill", trackX, trackY,
dims.horizontal.trackWidth, self.scrollbarWidth, self.scrollbarRadius)
love.graphics.rectangle("fill", trackX, trackY, dims.horizontal.trackWidth, self.scrollbarWidth, self.scrollbarRadius)
-- Draw thumb with state-based color
love.graphics.setColor(thumbColor:toRGBA())
love.graphics.rectangle("fill", trackX + dims.horizontal.thumbX, trackY,
dims.horizontal.thumbWidth, self.scrollbarWidth, self.scrollbarRadius)
love.graphics.rectangle("fill", trackX + dims.horizontal.thumbX, trackY, dims.horizontal.thumbWidth, self.scrollbarWidth, self.scrollbarRadius)
end
-- Reset color
@@ -4382,8 +4399,7 @@ function Element:_getScrollbarAtPosition(mouseX, mouseY)
local trackW = self.scrollbarWidth
local trackH = dims.vertical.trackHeight
if mouseX >= trackX and mouseX <= trackX + trackW and
mouseY >= trackY and mouseY <= trackY + trackH then
if mouseX >= trackX and mouseX <= trackX + trackW and mouseY >= trackY and mouseY <= trackY + trackH then
-- Check if over thumb
local thumbY = trackY + dims.vertical.thumbY
local thumbH = dims.vertical.thumbHeight
@@ -4402,8 +4418,7 @@ function Element:_getScrollbarAtPosition(mouseX, mouseY)
local trackW = dims.horizontal.trackWidth
local trackH = self.scrollbarWidth
if mouseX >= trackX and mouseX <= trackX + trackW and
mouseY >= trackY and mouseY <= trackY + trackH then
if mouseX >= trackX and mouseX <= trackX + trackW and mouseY >= trackY and mouseY <= trackY + trackH then
-- Check if over thumb
local thumbX = trackX + dims.horizontal.thumbX
local thumbW = dims.horizontal.thumbWidth
@@ -4424,10 +4439,14 @@ end
---@param button number
---@return boolean -- True if event was consumed
function Element:_handleScrollbarPress(mouseX, mouseY, button)
if button ~= 1 then return false end -- Only left click
if button ~= 1 then
return false
end -- Only left click
local scrollbar = self:_getScrollbarAtPosition(mouseX, mouseY)
if not scrollbar then return false end
if not scrollbar then
return false
end
if scrollbar.region == "thumb" then
-- Start dragging thumb
@@ -4445,8 +4464,7 @@ function Element:_handleScrollbarPress(mouseX, mouseY, button)
self._scrollbarDragOffset = mouseX - thumbX
end
return true -- Event consumed
return true -- Event consumed
elseif scrollbar.region == "track" then
-- Click on track - jump to position
self:_scrollToTrackPosition(mouseX, mouseY, scrollbar.component)
@@ -4461,7 +4479,9 @@ end
---@param mouseY number
---@return boolean -- True if event was consumed
function Element:_handleScrollbarDrag(mouseX, mouseY)
if not self._scrollbarDragging then return false end
if not self._scrollbarDragging then
return false
end
local dims = self:_calculateScrollbarDimensions()
@@ -4480,7 +4500,6 @@ function Element:_handleScrollbarDrag(mouseX, mouseY)
self:setScrollPosition(nil, newScrollY)
return true
elseif self._hoveredScrollbar == "horizontal" then
local trackX = self.x + self.scrollbarPadding + self.padding.left
local trackW = dims.horizontal.trackWidth
@@ -4505,7 +4524,9 @@ end
---@param button number
---@return boolean -- True if event was consumed
function Element:_handleScrollbarRelease(button)
if button ~= 1 then return false end
if button ~= 1 then
return false
end
if self._scrollbarDragging then
self._scrollbarDragging = false
@@ -4536,7 +4557,6 @@ function Element:_scrollToTrackPosition(mouseX, mouseY, component)
local newScrollY = scrollRatio * self._maxScrollY
self:setScrollPosition(nil, newScrollY)
elseif component == "horizontal" then
local trackX = self.x + self.scrollbarPadding + self.padding.left
local trackW = dims.horizontal.trackWidth
@@ -4573,7 +4593,7 @@ function Element:_handleWheelScroll(x, y)
-- Vertical scrolling
if y ~= 0 and hasVerticalOverflow then
local delta = -y * self.scrollSpeed -- Negative because wheel up = scroll up
local delta = -y * self.scrollSpeed -- Negative because wheel up = scroll up
local newScrollY = self._scrollY + delta
self:setScrollPosition(nil, newScrollY)
scrolled = true
@@ -5344,8 +5364,10 @@ function Element:draw(backdropCanvas)
local finalOpacity = self.opacity * self.imageOpacity
-- Apply cornerRadius clipping if set
local hasCornerRadius = self.cornerRadius.topLeft > 0 or self.cornerRadius.topRight > 0
or self.cornerRadius.bottomLeft > 0 or self.cornerRadius.bottomRight > 0
local hasCornerRadius = self.cornerRadius.topLeft > 0
or self.cornerRadius.topRight > 0
or self.cornerRadius.bottomLeft > 0
or self.cornerRadius.bottomRight > 0
if hasCornerRadius then
-- Use stencil to clip image to rounded corners
@@ -5356,16 +5378,7 @@ function Element:draw(backdropCanvas)
end
-- Draw the image
ImageRenderer.draw(
self._loadedImage,
imageX,
imageY,
imageWidth,
imageHeight,
self.objectFit,
self.objectPosition,
finalOpacity
)
ImageRenderer.draw(self._loadedImage, imageX, imageY, imageWidth, imageHeight, self.objectFit, self.objectPosition, finalOpacity)
-- Clear stencil if it was used
if hasCornerRadius then