aboutsummaryrefslogtreecommitdiff
path: root/plugins/motiontrail.lua
blob: 16e73079954ed92bdb7f78eaeec0b4277add3f7b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
-- mod-version:3
local core = require "core"
local config = require "core.config"
local common = require "core.common"
local style = require "core.style"
local DocView = require "core.docview"

config.plugins.motiontrail = common.merge({
  enabled = true,
  steps = 50,
  -- The config specification used by the settings gui
  config_spec = {
    name = "Motion Trail",
    {
      label = "Enabled",
      description = "Disable or enable the caret motion trail effect.",
      path = "enabled",
      type = "toggle",
      default = true
    },
    {
      label = "Steps",
      description = "Amount of trail steps to generate on caret movement.",
      path = "steps",
      type = "number",
      default = 50,
      min = 10,
      max = 100
    },
  }
}, config.plugins.motiontrail)


local function lerp(a, b, t)
  return a + (b - a) * t
end


local function get_caret_rect(dv)
  local line, col = dv.doc:get_selection()
  local x, y = dv:get_line_screen_position(line, col)
  return x, y, style.caret_width, dv:get_line_height()
end


local last_x, last_y, last_view

local draw = DocView.draw

function DocView:draw(...)
  draw(self, ...)
  if not config.plugins.motiontrail.enabled or self ~= core.active_view then
    return
  end

  local x, y, w, h = get_caret_rect(self)

  if last_view == self and (x ~= last_x or y ~= last_y) then
    local lx = x
    for i = 0, 1, 1 / config.plugins.motiontrail.steps do
      local ix = lerp(x, last_x, i)
      local iy = lerp(y, last_y, i)
      local iw = math.max(w, math.ceil(math.abs(ix - lx)))
      renderer.draw_rect(ix, iy, iw, h, style.caret)
      lx = ix
    end
    core.redraw = true
  end

  last_view, last_x, last_y = self, x, y
end