Cursor Fix
This commit is contained in:
@@ -337,7 +337,7 @@ end
|
|||||||
--- @param event string The event to call
|
--- @param event string The event to call
|
||||||
--- @vararg any The event arguments
|
--- @vararg any The event arguments
|
||||||
--- @return boolean handled Whether the event was handled
|
--- @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, ...)
|
function Container:callChildrenEvent(visibleOnly, event, ...)
|
||||||
local children = visibleOnly and self.get("visibleChildrenEvents") or self.get("childrenEvents")
|
local children = visibleOnly and self.get("visibleChildrenEvents") or self.get("childrenEvents")
|
||||||
if children[event] then
|
if children[event] then
|
||||||
@@ -349,6 +349,15 @@ function Container:callChildrenEvent(visibleOnly, event, ...)
|
|||||||
end
|
end
|
||||||
end
|
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
|
return false
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -55,7 +55,7 @@ function Frame:mouse_click(button, x, y)
|
|||||||
local draggingMap = self.get("draggingMap")
|
local draggingMap = self.get("draggingMap")
|
||||||
|
|
||||||
for _, map in ipairs(draggingMap) do
|
for _, map in ipairs(draggingMap) do
|
||||||
local width = map.width
|
local width = map.width or 1
|
||||||
local height = map.height or 1
|
local height = map.height or 1
|
||||||
|
|
||||||
if type(width) == "string" and width == "width" then
|
if type(width) == "string" and width == "width" then
|
||||||
@@ -109,11 +109,11 @@ end
|
|||||||
--- @param y number The y position of the release
|
--- @param y number The y position of the release
|
||||||
--- @return boolean handled Whether the event was handled
|
--- @return boolean handled Whether the event was handled
|
||||||
--- @protected
|
--- @protected
|
||||||
function Frame:mouse_release(button, x, y)
|
function Frame:mouse_up(button, x, y)
|
||||||
self.dragging = false
|
self.dragging = false
|
||||||
self.dragStartX = nil
|
self.dragStartX = nil
|
||||||
self.dragStartY = nil
|
self.dragStartY = nil
|
||||||
return Container.mouse_release(self, button, x, y)
|
return Container.mouse_up(self, button, x, y)
|
||||||
end
|
end
|
||||||
|
|
||||||
return Frame
|
return Frame
|
||||||
@@ -52,6 +52,11 @@ function Input:init(props, basalt)
|
|||||||
return self
|
return self
|
||||||
end
|
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
|
--- @shortDescription Handles char events
|
||||||
--- @param char string The character that was typed
|
--- @param char string The character that was typed
|
||||||
--- @return boolean handled Whether the event was handled
|
--- @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("text", text:sub(1, pos-1) .. char .. text:sub(pos))
|
||||||
self.set("cursorPos", pos + 1)
|
self.set("cursorPos", pos + 1)
|
||||||
self:updateRender()
|
|
||||||
self:updateViewport()
|
self:updateViewport()
|
||||||
|
|
||||||
|
local relPos = self.get("cursorPos") - self.get("viewOffset")
|
||||||
|
self:setCursor(relPos, 1, true, self.get("cursorColor") or self.get("foreground"))
|
||||||
|
|
||||||
return true
|
return true
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -112,20 +120,6 @@ function Input:key(key)
|
|||||||
return true
|
return true
|
||||||
end
|
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
|
--- @shortDescription Handles mouse click events
|
||||||
--- @param button number The button that was clicked
|
--- @param button number The button that was clicked
|
||||||
--- @param x number The x position of the click
|
--- @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
|
if VisualElement.mouse_click(self, button, x, y) then
|
||||||
local relX, relY = self:getRelativePosition(x, y)
|
local relX, relY = self:getRelativePosition(x, y)
|
||||||
local text = self.get("text")
|
local text = self.get("text")
|
||||||
self:setCursor(math.min(relX, #text + 1), relY, true, self.get("cursorColor") or self.get("foreground"))
|
local viewOffset = self.get("viewOffset")
|
||||||
self:set("cursorPos", relX + 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
|
return true
|
||||||
end
|
end
|
||||||
|
return false
|
||||||
end
|
end
|
||||||
|
|
||||||
--- Updates the input's viewport
|
--- Updates the input's viewport
|
||||||
@@ -151,16 +153,14 @@ function Input:updateViewport()
|
|||||||
local viewOffset = self.get("viewOffset")
|
local viewOffset = self.get("viewOffset")
|
||||||
local textLength = #self.get("text")
|
local textLength = #self.get("text")
|
||||||
|
|
||||||
if cursorPos - viewOffset > width then
|
if cursorPos - viewOffset >= width then
|
||||||
self.set("viewOffset", cursorPos - width)
|
self.set("viewOffset", cursorPos - width + 1)
|
||||||
elseif cursorPos <= viewOffset then
|
elseif cursorPos <= viewOffset then
|
||||||
|
self.set("viewOffset", cursorPos - 1)
|
||||||
self.set("viewOffset", math.max(0, cursorPos - 1))
|
|
||||||
end
|
end
|
||||||
|
|
||||||
if viewOffset > textLength - width then
|
self.set("viewOffset", math.max(0, math.min(self.get("viewOffset"), textLength - width + 1)))
|
||||||
self.set("viewOffset", math.max(0, textLength - width))
|
|
||||||
end
|
|
||||||
return self
|
return self
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -16,14 +16,7 @@ Program.defineProperty(Program, "path", {default = "", type = "string"})
|
|||||||
--- @property running boolean false Whether the program is running
|
--- @property running boolean false Whether the program is running
|
||||||
Program.defineProperty(Program, "running", {default = false, type = "boolean"})
|
Program.defineProperty(Program, "running", {default = false, type = "boolean"})
|
||||||
|
|
||||||
Program.defineEvent(Program, "key")
|
Program.defineEvent(Program, "*")
|
||||||
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")
|
|
||||||
|
|
||||||
local BasaltProgram = {}
|
local BasaltProgram = {}
|
||||||
BasaltProgram.__index = BasaltProgram
|
BasaltProgram.__index = BasaltProgram
|
||||||
@@ -162,7 +155,7 @@ function Program:dispatchEvent(event, ...)
|
|||||||
if(self.get("focused"))then
|
if(self.get("focused"))then
|
||||||
local cursorBlink = program.window.getCursorBlink()
|
local cursorBlink = program.window.getCursorBlink()
|
||||||
local cursorX, cursorY = program.window.getCursorPos()
|
local cursorX, cursorY = program.window.getCursorPos()
|
||||||
self:setCursor(cursorX, cursorY, cursorBlink)
|
self:setCursor(cursorX, cursorY, cursorBlink, program.window.getTextColor())
|
||||||
end
|
end
|
||||||
self:updateRender()
|
self:updateRender()
|
||||||
end
|
end
|
||||||
@@ -177,7 +170,7 @@ function Program:focus()
|
|||||||
if program then
|
if program then
|
||||||
local cursorBlink = program.window.getCursorBlink()
|
local cursorBlink = program.window.getCursorBlink()
|
||||||
local cursorX, cursorY = program.window.getCursorPos()
|
local cursorX, cursorY = program.window.getCursorPos()
|
||||||
self:setCursor(cursorX, cursorY, cursorBlink)
|
self:setCursor(cursorX, cursorY, cursorBlink, program.window.getTextColor())
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -379,9 +379,8 @@ end
|
|||||||
--- @protected
|
--- @protected
|
||||||
function VisualElement:setCursor(x, y, blink, color)
|
function VisualElement:setCursor(x, y, blink, color)
|
||||||
if self.parent then
|
if self.parent then
|
||||||
local absX, absY = self:getAbsolutePosition(x, y)
|
local xPos, yPos = self:calculatePosition()
|
||||||
absX = max(self.get("x"), min(absX, self.get("width") + self.get("x") - 1))
|
return self.parent:setCursor(x + xPos - 1, y + yPos - 1, blink, color)
|
||||||
return self.parent:setCursor(absX, absY, blink, color)
|
|
||||||
end
|
end
|
||||||
return self
|
return self
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user