Cursor Fix

This commit is contained in:
Robert Jelic
2025-02-25 23:28:53 +01:00
parent 9d5e6d4aa3
commit 5fdf01bcfe
5 changed files with 42 additions and 41 deletions

View File

@@ -337,7 +337,7 @@ end
--- @param event string The event to call
--- @vararg any The event arguments
--- @return boolean handled Whether the event was handled
--- @return table child? The child that handled the event
--- @return table? child The child that handled the event
function Container:callChildrenEvent(visibleOnly, event, ...)
local children = visibleOnly and self.get("visibleChildrenEvents") or self.get("childrenEvents")
if children[event] then
@@ -349,6 +349,15 @@ function Container:callChildrenEvent(visibleOnly, event, ...)
end
end
end
if(children["*"])then
local events = children["*"]
for i = #events, 1, -1 do
local child = events[i]
if(child:dispatchEvent(event, ...))then
return true, child
end
end
end
return false
end

View File

@@ -55,7 +55,7 @@ function Frame:mouse_click(button, x, y)
local draggingMap = self.get("draggingMap")
for _, map in ipairs(draggingMap) do
local width = map.width
local width = map.width or 1
local height = map.height or 1
if type(width) == "string" and width == "width" then
@@ -109,11 +109,11 @@ end
--- @param y number The y position of the release
--- @return boolean handled Whether the event was handled
--- @protected
function Frame:mouse_release(button, x, y)
function Frame:mouse_up(button, x, y)
self.dragging = false
self.dragStartX = nil
self.dragStartY = nil
return Container.mouse_release(self, button, x, y)
return Container.mouse_up(self, button, x, y)
end
return Frame

View File

@@ -52,6 +52,11 @@ function Input:init(props, basalt)
return self
end
function Input:setCursor(x, y, blink, color)
x = math.min(self.get("width"), math.max(1, x))
return VisualElement.setCursor(self, x, y, blink, color)
end
--- @shortDescription Handles char events
--- @param char string The character that was typed
--- @return boolean handled Whether the event was handled
@@ -68,8 +73,11 @@ function Input:char(char)
self.set("text", text:sub(1, pos-1) .. char .. text:sub(pos))
self.set("cursorPos", pos + 1)
self:updateRender()
self:updateViewport()
local relPos = self.get("cursorPos") - self.get("viewOffset")
self:setCursor(relPos, 1, true, self.get("cursorColor") or self.get("foreground"))
return true
end
@@ -112,20 +120,6 @@ function Input:key(key)
return true
end
--- @shortDescription Handles focus events
--- @protected
function Input:focus()
VisualElement.focus(self)
self:updateRender()
end
--- @shortDescription Handles blur events
--- @protected
function Input:blur()
VisualElement.blur(self)
self:updateRender()
end
--- @shortDescription Handles mouse click events
--- @param button number The button that was clicked
--- @param x number The x position of the click
@@ -136,10 +130,18 @@ function Input:mouse_click(button, x, y)
if VisualElement.mouse_click(self, button, x, y) then
local relX, relY = self:getRelativePosition(x, y)
local text = self.get("text")
self:setCursor(math.min(relX, #text + 1), relY, true, self.get("cursorColor") or self.get("foreground"))
self:set("cursorPos", relX + self.get("viewOffset"))
local viewOffset = self.get("viewOffset")
local maxPos = #text + 1
local targetPos = math.min(maxPos, viewOffset + relX)
self.set("cursorPos", targetPos)
local visualX = targetPos - viewOffset
self:setCursor(visualX, 1, true, self.get("cursorColor") or self.get("foreground"))
return true
end
return false
end
--- Updates the input's viewport
@@ -151,16 +153,14 @@ function Input:updateViewport()
local viewOffset = self.get("viewOffset")
local textLength = #self.get("text")
if cursorPos - viewOffset > width then
self.set("viewOffset", cursorPos - width)
if cursorPos - viewOffset >= width then
self.set("viewOffset", cursorPos - width + 1)
elseif cursorPos <= viewOffset then
self.set("viewOffset", math.max(0, cursorPos - 1))
self.set("viewOffset", cursorPos - 1)
end
if viewOffset > textLength - width then
self.set("viewOffset", math.max(0, textLength - width))
end
self.set("viewOffset", math.max(0, math.min(self.get("viewOffset"), textLength - width + 1)))
return self
end

View File

@@ -16,14 +16,7 @@ Program.defineProperty(Program, "path", {default = "", type = "string"})
--- @property running boolean false Whether the program is running
Program.defineProperty(Program, "running", {default = false, type = "boolean"})
Program.defineEvent(Program, "key")
Program.defineEvent(Program, "char")
Program.defineEvent(Program, "key_up")
Program.defineEvent(Program, "paste")
Program.defineEvent(Program, "mouse_click")
Program.defineEvent(Program, "mouse_drag")
Program.defineEvent(Program, "mouse_scroll")
Program.defineEvent(Program, "mouse_up")
Program.defineEvent(Program, "*")
local BasaltProgram = {}
BasaltProgram.__index = BasaltProgram
@@ -162,7 +155,7 @@ function Program:dispatchEvent(event, ...)
if(self.get("focused"))then
local cursorBlink = program.window.getCursorBlink()
local cursorX, cursorY = program.window.getCursorPos()
self:setCursor(cursorX, cursorY, cursorBlink)
self:setCursor(cursorX, cursorY, cursorBlink, program.window.getTextColor())
end
self:updateRender()
end
@@ -177,7 +170,7 @@ function Program:focus()
if program then
local cursorBlink = program.window.getCursorBlink()
local cursorX, cursorY = program.window.getCursorPos()
self:setCursor(cursorX, cursorY, cursorBlink)
self:setCursor(cursorX, cursorY, cursorBlink, program.window.getTextColor())
end
end
end

View File

@@ -379,9 +379,8 @@ end
--- @protected
function VisualElement:setCursor(x, y, blink, color)
if self.parent then
local absX, absY = self:getAbsolutePosition(x, y)
absX = max(self.get("x"), min(absX, self.get("width") + self.get("x") - 1))
return self.parent:setCursor(absX, absY, blink, color)
local xPos, yPos = self:calculatePosition()
return self.parent:setCursor(x + xPos - 1, y + yPos - 1, blink, color)
end
return self
end