diff options
| author | Giuseppe Gadola <giusgadola@gmail.com> | 2023-02-15 12:38:21 +0100 |
|---|---|---|
| committer | Giuseppe Gadola <giusgadola@gmail.com> | 2023-02-15 12:38:21 +0100 |
| commit | acc753e440fb6ce54342c1f4e4fc980c5e008448 (patch) | |
| tree | 89d1737d19b1db8d4082fa5fdbff1792150cefa0 /lua | |
| parent | chore: renamed grey to gray (diff) | |
feat(commands): added hide and pause commands
Diffstat (limited to 'lua')
| -rw-r--r-- | lua/pets.lua | 15 | ||||
| -rw-r--r-- | lua/pets/animations.lua | 34 | ||||
| -rw-r--r-- | lua/pets/commands.lua | 8 | ||||
| -rw-r--r-- | lua/pets/pet.lua | 30 |
4 files changed, 77 insertions, 10 deletions
diff --git a/lua/pets.lua b/lua/pets.lua index ce9c61c..d82945f 100644 --- a/lua/pets.lua +++ b/lua/pets.lua @@ -54,9 +54,24 @@ function M.kill_all() end function M.list() + if #M.pets == 0 then + print("You have no pets :(") + end for pet in pairs(M.pets) do print(pet) end end +function M.toggle_pause() + for _, pet in pairs(M.pets) do + pet:toggle_pause() + end +end + +function M.toggle_hide() + for _, pet in pairs(M.pets) do + pet:toggle_hide() + end +end + return M diff --git a/lua/pets/animations.lua b/lua/pets/animations.lua index fad62e8..3965b7a 100644 --- a/lua/pets/animations.lua +++ b/lua/pets/animations.lua @@ -51,16 +51,10 @@ function M.Animation.new(sourcedir, type, style, popup, user_opts) return instance end --- @param bufnr buffer number of the popup --- @function start the animation -function M.Animation:start(bufnr) - if self.timer ~= nil then -- reset timer - self.timer = nil +function M.Animation:start_timer() + if self.timer == nil then + self.timer = vim.loop.new_timer() end - self.timer = vim.loop.new_timer() - self.bufnr = bufnr - self.current_action = "idle" - self.timer:start(0, 1000 / (self.speed_multiplier * 8), function() vim.schedule(function() M.Animation.next_frame(self) @@ -68,6 +62,26 @@ function M.Animation:start(bufnr) end) end +function M.Animation:stop_timer() + if self.timer == nil then + return + end + self.timer:stop() + self.timer:close() + self.timer = nil +end + +-- @param bufnr buffer number of the popup +-- @function start the animation +function M.Animation:start() + if self.timer ~= nil then -- reset timer + self.timer = nil + end + self.bufnr = self.popup.bufnr + self.current_action = self.current_action or "idle" + M.Animation.start_timer(self) +end + -- @function called on every tick from the timer, go to the next frame function M.Animation:next_frame() self.frame_counter = self.frame_counter + 1 @@ -146,6 +160,8 @@ function M.Animation:stop() end if self.timer then self.timer:stop() + self.timer:close() + self.timer = nil end end diff --git a/lua/pets/commands.lua b/lua/pets/commands.lua index bae97aa..b2b9888 100644 --- a/lua/pets/commands.lua +++ b/lua/pets/commands.lua @@ -44,3 +44,11 @@ end, { vim.api.nvim_create_user_command("PetsList", function() pets.list() end, {}) + +vim.api.nvim_create_user_command("PetsPauseToggle", function() + pets.toggle_pause() +end, {}) + +vim.api.nvim_create_user_command("PetsHideToggle", function() + pets.toggle_hide() +end, {}) diff --git a/lua/pets/pet.lua b/lua/pets/pet.lua index e13d9e0..2442eec 100644 --- a/lua/pets/pet.lua +++ b/lua/pets/pet.lua @@ -43,7 +43,7 @@ end -- start the animation of the pet function M.Pet:animate() self.popup:mount() - self.animation:start(self.popup.bufnr) + self.animation:start() end -- delete the pet :( @@ -56,4 +56,32 @@ function M.Pet:kill() end end +function M.Pet:toggle_pause() + if not self.paused then + self.animation:stop_timer() + self.paused = true + else + if self.animation.current_image then + self.animation.current_image:delete(0, { free = false }) + end + self.animation:start_timer() + self.paused = false + end +end + +function M.Pet:toggle_hide() + if not self.paused then + self.animation:stop_timer() + if self.animation.current_image then + self.animation.current_image:delete(0, { free = false }) + end + self.popup:unmount() + self.paused = true + else + self.popup:mount() + self.animation:start() + self.paused = false + end +end + return M |