some finishing moves for the new event system

- focus order bug fixed
- added a new parameter for onChange() (its the value you would get with :getValue()
- small fix with drag events
- added paste events to textfields and input
- added a mouse event to input's (now you can change the text x position with mouse, like you can do in textfields
This commit is contained in:
Robert Jelic
2022-08-12 23:24:27 +02:00
parent f863b986c2
commit 373bbdc490
4 changed files with 105 additions and 9 deletions

View File

@@ -983,12 +983,12 @@ return function(name, parent, pTerm, basalt)
if (base.draw(self)) then
if(focusedObject~=focusedOBjectCache)then
if(focusedOBjectCache~=nil)then
focusedOBjectCache:getFocusHandler()
end
if(focusedObject~=nil)then
focusedObject:loseFocusHandler()
end
if(focusedOBjectCache~=nil)then
focusedOBjectCache:getFocusHandler()
end
focusedObject = focusedOBjectCache
end

View File

@@ -502,7 +502,7 @@ return function(name)
onChange = function(self, ...)
for _,v in pairs(table.pack(...))do
if(type(v)=="function")then
self:registerEvent("value_changed", v)
self:registerEvent("value_changed", v, value)
end
end
return self
@@ -730,8 +730,14 @@ return function(name)
end
local dX, dY = x + dragXOffset - (parentX - 1) + xO, y + dragYOffset - (parentY - 1) + yO
local val = eventSystem:sendEvent(event, self, event, button, dX, dY, dragStartX, dragStartY, x, y)
if(val==false)then return false end
return true
if(val~=nil)then return val end
if(self:isCoordsInObject(x, y))then
if(self.parent~=nil)then
self.parent:setFocusedObject(self)
end
return true
end
return false
end
if(self:isCoordsInObject(x, y))then

View File

@@ -55,6 +55,9 @@ return function(name)
base.setValue(self, tostring(val))
if not (internalValueChange) then
textX = tostring(val):len() + 1
wIndex = math.max(1, textX-self:getWidth()+1)
local obx, oby = self:getAnchorPosition()
self.parent:setCursor(true, obx + textX - wIndex, oby+math.floor(self.height/2), self.fgColor)
end
return self
end;
@@ -205,8 +208,9 @@ return function(name)
local val = tostring(base.getValue())
local cursorX = (textX <= val:len() and textX - 1 or val:len()) - (wIndex - 1)
if (cursorX > self.x + w - 1) then
cursorX = self.x + w - 1
local x = self:getX()
if (cursorX > x + w - 1) then
cursorX = x + w - 1
end
if (self.parent ~= nil) then
self.parent:setCursor(true, obx + cursorX, oby+math.floor(h/2), self.fgColor)
@@ -217,6 +221,70 @@ return function(name)
return false
end,
mouseHandler = function(self, button, x, y)
if(base.mouseHandler(self, button, x, y))then
local obx, oby = self:getAbsolutePosition(self:getAnchorPosition())
local w, h = self:getSize()
textX = x - obx + wIndex
local text = base.getValue()
if (textX > text:len()) then
textX = text:len() + 1
end
if (textX < wIndex) then
wIndex = textX - 1
if (wIndex < 1) then
wIndex = 1
end
end
local cursorX = (textX <= text:len() and textX - 1 or text:len()) - (wIndex - 1)
if (self.parent ~= nil) then
self.parent:setCursor(true, obx + cursorX, oby+math.floor(h/2), self.fgColor)
end
return true
end
end,
eventHandler = function(self, event, paste, p2, p3, p4)
if(base.eventHandler(self, event, paste, p2, p3, p4))then
if(event=="paste")then
if(self:isFocused())then
local text = base.getValue()
local w, h = self:getSize()
internalValueChange = true
if (inputType == "number") then
local cache = text
if (paste == ".") or (tonumber(paste) ~= nil) then
self:setValue(text:sub(1, textX - 1) .. paste .. text:sub(textX, text:len()))
textX = textX + paste:len()
end
if (tonumber(base.getValue()) == nil) then
self:setValue(cache)
end
else
self:setValue(text:sub(1, textX - 1) .. paste .. text:sub(textX, text:len()))
textX = textX + paste:len()
end
if (textX >= w + wIndex) then
wIndex = (textX+1)-w
end
local obx, oby = self:getAnchorPosition()
local val = tostring(base.getValue())
local cursorX = (textX <= val:len() and textX - 1 or val:len()) - (wIndex - 1)
local x = self:getX()
if (cursorX > x + w - 1) then
cursorX = x + w - 1
end
if (self.parent ~= nil) then
self.parent:setCursor(true, obx + cursorX, oby+math.floor(h/2), self.fgColor)
end
internalValueChange = false
end
end
end
end,
draw = function(self)
if (base.draw(self)) then
if (self.parent ~= nil) then
@@ -264,6 +332,7 @@ return function(name)
self.parent:addEvent("mouse_click", self)
self.parent:addEvent("key", self)
self.parent:addEvent("char", self)
self.parent:addEvent("other_event", self)
end
end,
}

View File

@@ -528,7 +528,27 @@ return function(name)
self:setVisualChanged()
return true
end
end;
end,
eventHandler = function(self, event, paste, p2, p3, p4)
if(base.eventHandler(self, event, paste, p2, p3, p4))then
if(event=="paste")then
if(self:isFocused())then
local w, h = self:getSize()
lines[textY] = lines[textY]:sub(1, textX - 1) .. paste .. lines[textY]:sub(textX, lines[textY]:len())
fgLines[textY] = fgLines[textY]:sub(1, textX - 1) .. tHex[self.fgColor]:rep(paste:len()) .. fgLines[textY]:sub(textX, fgLines[textY]:len())
bgLines[textY] = bgLines[textY]:sub(1, textX - 1) .. tHex[self.bgColor]:rep(paste:len()) .. bgLines[textY]:sub(textX, bgLines[textY]:len())
textX = textX + paste:len()
if (textX >= w + wIndex) then
wIndex = (textX+1)-w
end
local anchx, anchy = self:getAnchorPosition()
self.parent:setCursor(true, anchx + textX - wIndex, anchy + textY - hIndex, self.fgColor)
updateColors(self)
end
end
end
end,
draw = function(self)
if (base.draw(self)) then
@@ -577,6 +597,7 @@ return function(name)
self.parent:addEvent("mouse_drag", self)
self.parent:addEvent("key", self)
self.parent:addEvent("char", self)
self.parent:addEvent("other_event", self)
end,
}