Template repo for tiny cross-platform apps that can be modified on phone, tablet or computer.
cursor = nil

-- a single text box
function add_editor()
  -- some variables shared by all the widgets in the text box
  local x,y, w,h = 50, Menu_bottom+10, 100, 100  -- surface coords
  local font_height = typographic_scale[6]
  local font = g.newFont(scale(font_height))
  local e = edit.initialize_state(vy(y), vy(y+h), vx(x), vx(x+w), font, scale(font_height), floor(scale(font_height)*1.3))
  --e.filename = 'example.txt'
  --load_from_disk(e)
  Text.redraw_all(e)

  -- core editor widget
  local r = {}
  r.draw = function()
    g.setFont(font)
    edit.draw(e, {r=0.5, g=0.5, b=0.5}, cursor ~= r)
    color(0.5,0.5,0.5)
    rect('line', e.left-5, e.top-5, e.width+5*2, (e.bottom+5)-(e.top-5))
  end
  r.ispress = function(x2,y2)
    return x2 >= e.left-5 and x2 <= e.right+5 and y2 >= e.top-5 and y2 <= e.bottom+5
  end
  r.press = function(x2,y2, b)
    cursor = r
    edit.mouse_press(e, x2,y2, b)
  end
  r.update = function(dt, x2,y2)
    -- hack: ensure cursor shows within screen (or at least will show if the box is big enough)
    if e.cursor_x == nil then
      e.cursor1 = {line=1, pos=1}
      e.screen_top1 = {line=1, pos=1}
    end
    if cursor == r then
      --edit.update(e, dt)
    elseif f then
      if s then
        e.font_height = scale(font_height)
        e.line_height = floor(e.font_height*1.3)
        font = g.newFont(e.font_height)
      end
      g.setFont(font)
      e.left, e.right = vx(x), vx(x+w)
      e.width = e.right - e.left
      e.top, e.bottom = vy(y), vy(y+h)
      Text.redraw_all(e)
    end
  end
  r.release = function(x2,y2, b)
    if cursor == r then
      edit.mouse_release(e, x2,y2, b)
    end
  end
  r.presschord = function(chord, key) edit.keychord_press(e, chord, key) end
  r.textinput = function(t) edit.text_input(e, t) end
  r.releasekey = function(key) edit.key_release(e, key) end
  --r.quit = function() edit.quit(e) end  -- final save
  table.insert(widgets, r)

  -- move affordance
  local xoff, yoff
  table.insert(widgets, {
    draw = function()
      color(0.7,0.7,0.7)
      local x,y = e.left, e.top
      circle('fill', x+10,y-20, 15)
    end,
    ispress = function(x2,y2)
      return dist({x=e.left+10, y=e.top-20}, {x=x2, y=y2}) < 15
    end,
    press = function(x2,y2, b)
      xoff,yoff = x2-vx(x), y2-vy(y)
    end,
    update = function(dt, x2,y2)
      if not xoff then return end
      x,y = sx(x2-xoff), sy(y2-yoff)
      e.left, e.right = vx(x), vx(x+w)
      e.width = e.right - e.left
      e.top, e.bottom = vy(y), vy(y+h)
      Text.redraw_all(e)
    end,
    release = function(x2,y2, b)
      xoff,yoff = nil
    end,
  })

  -- resize affordances
  local adjw, adjh = {}, {}
  local width_adjustment, height_adjustment
  adjw.draw = function()
    color(0.5,0.5,0.5)
    for i=2,4 do -- i=1 is on the border
      line(e.right+5*i, e.top+5, e.right+5*i, e.bottom-5)
    end
  end
  adjh.draw = function()
    color(0.5,0.5,0.5)
    for i=2,4 do -- i=1 is on the border
      line(e.left+5, e.bottom+5*i, e.right-5, e.bottom+5*i)
    end
  end
  adjw.ispress = function(x2,y2)
    -- include a shadow to the right
    return x2 > e.right+5 and x2 <= e.right+5*5 and y2 >= e.top and y2 <= e.bottom
  end
  adjh.ispress = function(x2,y2)
    -- include a shadow below
    return x2 >= e.left and x2 <= e.right and y2 > e.bottom+5 and y2 <= e.bottom+5*5
  end
  adjw.press = function(x2,y2, b) width_adjustment = x2-e.right end
  adjh.press = function(x2,y2, b) height_adjustment = y2-e.bottom end
  adjw.release = function(x2,y2, b) width_adjustment = nil end
  adjh.release = function(x2,y2, b) height_adjustment = nil end
  adjw.update = function(dt, x2,y2)
    if not width_adjustment then return end
    e.right = x2-width_adjustment
    e.width = e.right - e.left
    w = iscale(e.width)
    g.setFont(font)
    Text.redraw_all(e)
  end
  adjh.update = function(dt, x2,y2)
    if not height_adjustment then return end
    e.bottom = y2-height_adjustment
    h = iscale(e.bottom-e.top)
  end
  table.insert(widgets, adjw)
  table.insert(widgets, adjh)

  -- font buttons
  r.refresh_font = function()
    e.font_height = scale(font_height)
    e.font = g.newFont(e.font_height)
    e.line_height = floor(e.font_height)*1.3
    Text.redraw_all(e)
  end
  local update_font = function(newf)
    if font_height == newf then return end
    font_height = newf
    r.refresh_font()
  end
  table.insert(widgets, {
    draw = function()
      color(0.7,0.7,0.7)
      local x,y = e.left-35, e.top-5
      poly('fill', x+15,y, x,y+30, x+30,y+30)
    end,
    ispress = function(x2,y2)
      local x,y = e.left-35, e.top-5
      return x2 >= x and x2 <= x+30 and y2 >= y and y2 <= y+30
    end,
    press = function()
      update_font(higher_element(typographic_scale, font_height))
    end,
  })
  table.insert(widgets, {
    draw = function()
      color(0.7,0.7,0.7)
      local x,y = e.left-35, e.top+35
      poly('fill', x+15,y+30, x,y, x+30,y)
    end,
    ispress = function(x2,y2)
      local x,y = e.left-35, e.top+35
      return x2 >= x and x2 <= x+30 and y2 >= y and y2 <= y+30
    end,
    press = function()
      update_font(lower_element(typographic_scale, font_height))
    end,
  })
end  -- phew! done with add_editor