Skip to content

Commit

Permalink
add: implement undoing of last char selected
Browse files Browse the repository at this point in the history
  • Loading branch information
zombrodo committed Mar 28, 2022
1 parent 89e0013 commit 97f0a60
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 10 deletions.
5 changes: 0 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,6 @@ it'll return `nil`.

To update the keyboard's letters, you can call `keyboard:setLetters(newLetters)`

## To-do

- Wordscapes allows for you to "undo" a swipe by just reversing your steps.
Simple enough addition, but not something I've gotten around to do just yet!

## Attribution

Example font is `mini-wakuwaku` by miniyama, gratefully retrieved from
Expand Down
31 changes: 26 additions & 5 deletions swipe.lua
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ function Swipe:setLetters(letters)
size = math.max(text:getWidth(), text:getHeight()),
letter = letter,
selected = false,
firstHover = false,
index = index
}
end
Expand Down Expand Up @@ -149,6 +150,7 @@ function Swipe:__track(letter)
or (letter.index ~= self.__selected[#self.__selected].index
and not letter.selected )then
letter.selected = true
letter.firstHover = true
table.insert(self.__selected, letter)
end
end
Expand Down Expand Up @@ -182,18 +184,37 @@ function Swipe:start(id, x, y)
end
end

-- Internal: removes the given letter from the list of selected letters. Not
-- intended to be used externally.
function Swipe:__untrack(letter)
letter.firstHover = true
letter.selected = false
table.remove(self.__selected)
end

-- Updates the swiping state. Use this if your input is a mouse. If you're using
-- touch behaviours, prefer `touchMoved`. Takes `(x, y)` as the current position
-- of the pointer.
-- Returns a table containing the letters currently selected in order.
function Swipe:moved(x, y)
if not self.isSwiping or not self:inBounds(x, y) then
return
end

for i, letter in ipairs(self.__letters) do
if self:__isHovering(letter, x, y) then
self:__track(letter)
-- If we've moved off of the letter we just set, then update its
-- `firstHover` flag
if letter.firstHover and not self:__isHovering(letter, x, y) then
letter.firstHover = false
end
-- If we're hovering over a letter that we've left, and returned to, then
if self:__isHovering(letter, x, y) and not letter.firstHover then
-- If it's the last letter in our list, we're undoing, so lets remove it
if self.__selected[#self.__selected] == letter then
self:__untrack(letter)
else
-- Otherwise, track it
self:__track(letter)
end
end
end
end
Expand All @@ -220,8 +241,8 @@ function Swipe:stop(id)
end

local result = self:get()

self.__selected = {}

return result
end
end
Expand Down Expand Up @@ -270,4 +291,4 @@ function Swipe:draw()
love.graphics.pop()
end

return Swipe
return Swipe

0 comments on commit 97f0a60

Please sign in to comment.