Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

文本框无法重定位光标,删除中文字符报错 #9

Open
openbu-owner opened this issue Nov 18, 2018 · 1 comment
Open

文本框无法重定位光标,删除中文字符报错 #9

openbu-owner opened this issue Nov 18, 2018 · 1 comment

Comments

@openbu-owner
Copy link

No description provided.

@sharebophar
Copy link

sharebophar commented Dec 12, 2018

function UIEditText:getLineInfoAt(x, y)
    -- returns the wrap, line, and byteoffset
    -- of specified line
    local wraps = self.label:getWrap()
    local line = wraps[y] or ""
    local byteoffset = x
--  原代码
--  local line = wraps[self.cursorCoords.y] or ""
--  local byteoffset = utf8.offset(line, self.cursorCoords.x) or 0
    return wraps, line, byteoffset
end


function UIEditText:wedgeLineAt(x, y, xo, yo)
    -- return line info + both parts of line
    -- split by the current position of cursor
    local wraps, line, byteoffset = self:getLineInfo(x, y)
    left = utf8.sub(line,1, byteoffset + xo)
    right = utf8.sub(line,byteoffset + yo, -1)
--  原代码
--  left = line:sub(1, byteoffset + xo)
--  right = line:sub(byteoffset + yo, -1)
    return wraps, line, byteoffset, left, right
end


function UIEditText:insertCharAt(char, x, y)
    local wraps, line, byteoffset, left, right =
      self:wedgeLineAt(x, y, 0, 1)
    local newLine = left..char..right
    if x == 0 then
      newLine = char..left..right
    end
    -- some future wrapping :)
    -- if char ~= '\f' and #newLine % 20 == 0 then
    --     print('inserting f')
    --     self:insertCharAt('\f', x+1, y)
    -- end
    table.remove(wraps, y)
    table.insert(wraps, y, newLine)
    self.label:setText(table.concat(wraps, '\n'))
    self.events:dispatch(UI_TEXT_CHANGE, self.label:getText())
    self:moveCursor(utf8.len(char), 0)
--  原代码
--  self:moveCursor(#char, 0)
end

-------------------------------------
-- (callback)
-- on key down
-------------------------------------
function UIEditText:onKeyDown(key, scancode, isrepeat)
    local wraps, line, byteoffset = self:getLineInfo()
    if key == "backspace" and byteoffset then
        self:backspace()
    elseif key == "return" then
        self:newline()
    elseif key == "tab" then
        self:insertChar('  ')
    elseif key == "up" then
        if self.cursorCoords.y > 1 then
            self:moveCursor(0, -1)
        end
    elseif key == "down" then
        if self.cursorCoords.y < self.rows then
            self:moveCursor(0, 1)
        end
    elseif key == "left" then
        if self.cursorCoords.x > 0 then
            self:moveCursor(-1, 0)
        elseif self.cursorCoords.y > 1 then
            self:moveCursor(#(wraps[self.cursorCoords.y-1] or {}), -1)
        end
    elseif key == "right" then
        if self.cursorCoords.x < utf8.len(line) then
--  原代码
--      if self.cursorCoords.x < #line then
            self:moveCursor(1, 0)
        elseif self.cursorCoords.y < self.rows then
            self:moveCursor(-self.cursorCoords.x, 1)
        end
    end

    self.showCursorDt = 0
    self.showCursor = true
end


function UIEditText:moveCursor(xo, yo)
    self.cursorCoords.y = self.cursorCoords.y + yo
    if xo == 0 then
      local wraps = self.label:getWrap()
      local width = utf8.len(wraps[self.cursorCoords.y])
--  原代码
--    local width = #wraps[self.cursorCoords.y]
      if self.cursorCoords.x > width then
        self.cursorCoords.x = width
      end
    else
      self.cursorCoords.x = self.cursorCoords.x + xo
    end
end

-------------------------------------
-- draw cursor
-------------------------------------
function UIEditText:drawCursor()
    if not self.showCursor then
        return
    end

    local wraps = self.label:getWrap()
    local length = #wraps == 0 and 1 or #wraps
    local chars = utf8.sub((wraps[self.cursorCoords.y] or ""), 1, self.cursorCoords.x)
--  原代码
--  local chars = (wraps[self.cursorCoords.y] or ""):sub(
--    1, self.cursorCoords.x
--  )
    local width = self.label:measureWidth(chars)
    local maxX = self.label:getBoundingBox():getX()
    local maxY = self.label:getBoundingBox():getY()
    maxX = maxX + width + 1
    maxY = maxY + self.cursorCoords.y * self.label:getFont():getHeight()

    local r, g, b, a = love.graphics.getColor()
    local color = self.cursorColor
    love.graphics.setColor(color[1], color[2], color[3], color[4])
    love.graphics.setLineWidth(2)
    love.graphics.line(maxX, maxY, maxX, maxY - self.label:getFont():getHeight())
    love.graphics.setColor(r, g, b, a)
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants