TextContainer/EditableTextContainer selection fixed when using hAlignment

This commit is contained in:
hbomb79 2017-03-19 22:44:49 +13:00
parent 50497e5972
commit 4bed043f2e
3 changed files with 21 additions and 16 deletions

View file

@ -77,21 +77,22 @@ function EditableTextContainer:onKeyDown( event, handled )
local old_tX
if key == "up" or key == "down" then
if not self.cache.tX then self.cache.tX = ( isShift and selection or position ) - lines[ isShift and self.cache.selY or self.cache.y ][ 2 ] end
local line = lines[ isShift and self.cache.selY or self.cache.y ]
if not self.cache.tX then self.cache.tX = ( isShift and selection or position ) - line[ 2 ] + line[ 5 ] - 1 end
old_tX = self.cache.tX
end
if key == "up" then
local previousLine = lines[ ( isShift and self.cache.selY or self.cache.y ) - 1]
local previousLine = lines[ ( isShift and self.cache.selY or self.cache.y ) - 1 ]
if not previousLine then return end
self[ isShift and "selection" or "position" ] = math.min( previousLine[ 2 ] + self.cache.tX, previousLine[ 3 ] - 1 )
self[ isShift and "selection" or "position" ] = math.min( previousLine[ 2 ] + self.cache.tX - previousLine[ 5 ] + 1, previousLine[ 3 ] )
elseif key == "down" then
local nextLine = lines[ ( isShift and self.cache.selY or self.cache.y ) + 1]
local nextLine = lines[ ( isShift and self.cache.selY or self.cache.y ) + 1 ]
if not nextLine then return end
self[ isShift and "selection" or "position" ] = math.min( nextLine[ 2 ] + self.cache.tX, nextLine[ 3 ] - 1 )
self[ isShift and "selection" or "position" ] = math.min( nextLine[ 2 ] + self.cache.tX - nextLine[ 5 ] + 1, nextLine[ 3 ] - 1 )
elseif key == "left" then
if isShift then
self.selection = selection - 1

View file

@ -4,7 +4,7 @@ local function resolvePosition( self, lines, X, Y )
if posY == 0 then return 0 end
local selectedLine = lines[ posY ]
return math.min( selectedLine[ 3 ] - ( posY == #lines and 0 or 1 ), selectedLine[ 2 ] + X - 1 )
return math.min( selectedLine[ 3 ] - ( posY == #lines and 0 or 1 ), selectedLine[ 2 ] + X - selectedLine[ 5 ] - 1 )
end
--[[
@ -114,15 +114,11 @@ function TextContainer:drawLines( lines, selectionStart, selectionStop )
local isSelection = selectionStart and selectionStop
canvas:clear( bg )
local cacheX, cacheY, cacheSelX, cacheSelY = 0, 1, false, false
local cacheX, cacheY, cacheSelX, cacheSelY = ( hAlign == "centre" and width / 2 or ( hAlign == "right" and width ) or 0 ), 1, false, false
for i = self.yScroll + 1, #lines do
local Y, line, xOffset = yOffset + i - self.yScroll, lines[ i ], 1
local Y, line = yOffset + i - self.yScroll, lines[ i ]
local lineContent, lineStart, lineEnd = line[ 1 ], line[ 2 ], line[ 3 ]
if hAlign == "centre" then
xOffset = math.floor( width / 2 - ( #line / 2 ) + .5 )
elseif hAlign == "right" then
xOffset = width - #line + 1
end
local xOffset = line[ 5 ]
if isSelection then
local pre, current, post
@ -169,7 +165,7 @@ function TextContainer:drawLines( lines, selectionStart, selectionStop )
if pos == lineEnd and self.lineConfig.lines[ i + 1 ] then
cacheY = i + 1
else
cacheX, cacheY = pos - lineStart + 1, i
cacheX, cacheY = pos - lineStart + xOffset, i
end
end
if sel and sel >= lineStart and sel <= lineEnd then

View file

@ -38,6 +38,7 @@ end
]]
function MTextDisplay:wrapText( width )
local text, width, lines = self.text, width or self.width, {}
local align, halfWidth = self.horizontalAlign, width / 2
local current = 1
while text and string_len( text ) > 0 do
@ -64,9 +65,16 @@ function MTextDisplay:wrapText( width )
current = current + string_len( pre ) + ( match and #match or 1 )
end
lines[ #lines + 1 ], text = { pre, starting, current - 1, #lines + 1 }, post
local offset = 0
if align == "centre" then
offset = math.floor( halfWidth - ( #pre / 2 ) + .5 )
elseif align == "right" then
offset = width - #pre
end
if createTrail then lines[ #lines + 1 ] = { "", current, current, #lines + 1 } end
lines[ #lines + 1 ], text = { pre, starting, current - 1, #lines + 1, offset < 1 and 1 or offset }, post
if createTrail then lines[ #lines + 1 ] = { "", current, current, #lines + 1, align == "centre" and halfWidth or ( align == "right" and width ) or 0 } end
end
self.lineConfig.lines = lines