beginnings of a module for the text editor

[?]
May 18, 2022, 3:29 AM
BULPIBEGL7TMK6CVIE7IS7WGAHGOSUJBGJSFQK542MOWGHP2ADQQC

Dependencies

  • [2] TRK4TSN7 more precise cursor positioning
  • [3] BLWAYPKV extract a module
  • [4] 7IKRRESB longer names for indices in long loops
  • [5] H7OEU6WP experimental approach to combining keyboard and mouse while drawing
  • [6] 2INHXC3K position cursor by clicking on text
  • [7] PRPPZGDY speed up some obvious common cases
  • [8] ZD63LJ2T bugfix: keep the click to create a new drawing from creating a new shape in the drawing
  • [9] JS6JSYOT online contextual help
  • [10] TNTYISW6 rename
  • [11] JVRL5TWL store device-independent coordinates inside drawings
  • [12] 7RN3AETY bugfix: text sometimes getting colored like drawing borders
  • [13] BJ5X5O4A let's prevent the text cursor from ever getting on a drawing
  • [14] VVXVV2D2 change data model; text can now have metadata
  • [15] V5TP27FP ctrl-+ and ctrl-- to adjust font size
  • [16] XX7G2FFJ intermingle freehand line drawings with text
  • [17] WDWXNW7V slightly strange way to move points
  • [18] HJ3PM2VT .
  • [19] AVQ5MC5D finish uppercasing all globals
  • [20] IHG5RXP5 allow text to be typed while mouse hovers over drawing
  • [21] 3SYFA5JQ show cursor even on empty lines
  • [22] RJGZD4IN binary search to most natural up/down with proportional fonts
  • [23] IFGAJAF7 add a level of indirection to vertices of shapes
  • [24] FBDRL6LH delete points or shapes
  • [25] KHFU5NFD bugfix: up/down across drawings
  • [26] JCSLDGAH beginnings of support for multiple shapes
  • [27] M36DBSDE bit more polish to help screen
  • [28] HRWN5V6J Devine's suggestion to try to live with just freehand
  • [29] 3CS5KKCI up/down cursor movement
  • [30] 3D5RFWHV stop handling drawings in cursor_pos computations
  • [31] MGOQ5XAV start uppercasing globals
  • [*] R5QXEHUI somebody stop me
  • [*] OTIBCAUJ love2d scaffold

Change contents

  • file addition: text.lua (----------)
    [33.2]
    -- primitives for editing text
    Text = {}
    function Text.draw(line, line_index, cursor_line, y, cursor_pos)
    love.graphics.setColor(0,0,0)
    local love_text = love.graphics.newText(love.graphics.getFont(), line.data)
    love.graphics.draw(love_text, 25,y, 0, Zoom)
    if line_index == cursor_line then
    -- cursor
    love.graphics.print('_', Text.cursor_x(line.data, cursor_pos), y+6) -- drop the cursor down a bit to account for the increased font size
    end
    end
    function Text.nearest_cursor_pos(line, x, hint)
    if x == 0 then
    return 1
    end
    local max_x = Text.cursor_x(line, #line+1)
    if x > max_x then
    return #line+1
    end
    local currx = Text.cursor_x(line, hint)
    if currx > x-2 and currx < x+2 then
    return hint
    end
    local left, right = 1, #line+1
    if currx > x then
    right = hint
    else
    left = hint
    end
    while left < right-1 do
    local curr = math.floor((left+right)/2)
    local currxmin = Text.cursor_x(line, curr)
    local currxmax = Text.cursor_x(line, curr+1)
    if currxmin <= x and x < currxmax then
    return curr
    end
    if currxmin > x then
    right = curr
    else
    left = curr
    end
    end
    return right
    end
    function Text.cursor_x(line_data, cursor_pos)
    local line_before_cursor = line_data:sub(1, cursor_pos-1)
    local text_before_cursor = love.graphics.newText(love.graphics.getFont(), line_before_cursor)
    return 25+text_before_cursor:getWidth()*Zoom
    end
    return Text
  • edit in main.lua at line 4
    [5.19]
    [3.31]
    local Text = require 'text'
  • replacement in main.lua at line 124
    [5.1249][5.2:38](),[5.38][5.469:546](),[5.546][5.1410:1456](),[5.1456][4.263:303](),[4.303][5.32:50](),[5.1487][5.32:50](),[5.32][5.32:50](),[5.50][5.1488:1629](),[5.142][5.411:421](),[5.158][5.411:421](),[5.695][5.411:421](),[5.1629][5.411:421](),[5.411][5.411:421]()
    love.graphics.setColor(0,0,0)
    local text = love.graphics.newText(love.graphics.getFont(), line.data)
    love.graphics.draw(text, 25,y, 0, Zoom)
    if line_index == Cursor_line then
    -- cursor
    love.graphics.print('_', cursor_x(line.data, Cursor_pos), y+6) -- drop the cursor down a bit to account for the increased font size
    end
    [5.1249]
    [5.1294]
    Text.draw(line, line_index, Cursor_line, y, Cursor_pos)
  • replacement in main.lua at line 165
    [4.380][5.1958:2015](),[5.1958][5.1958:2015]()
    Cursor_pos = nearest_cursor_pos(line.data, x, 1)
    [4.380]
    [5.361]
    Cursor_pos = Text.nearest_cursor_pos(line.data, x, 1)
  • replacement in main.lua at line 536
    [5.4825][5.4779:4851]()
    local old_x = cursor_x(Lines[new_cursor_line].data, Cursor_pos)
    [5.4825]
    [5.4851]
    local old_x = Text.cursor_x(Lines[new_cursor_line].data, Cursor_pos)
  • replacement in main.lua at line 538
    [5.4889][5.4889:4973]()
    Cursor_pos = nearest_cursor_pos(Lines[Cursor_line].data, old_x, Cursor_pos)
    [5.4889]
    [5.1874]
    Cursor_pos = Text.nearest_cursor_pos(Lines[Cursor_line].data, old_x, Cursor_pos)
  • replacement in main.lua at line 548
    [5.5121][5.5061:5133]()
    local old_x = cursor_x(Lines[new_cursor_line].data, Cursor_pos)
    [5.5121]
    [5.5133]
    local old_x = Text.cursor_x(Lines[new_cursor_line].data, Cursor_pos)
  • replacement in main.lua at line 550
    [5.5171][5.5171:5255]()
    Cursor_pos = nearest_cursor_pos(Lines[Cursor_line].data, old_x, Cursor_pos)
    [5.5171]
    [5.2218]
    Cursor_pos = Text.nearest_cursor_pos(Lines[Cursor_line].data, old_x, Cursor_pos)
  • edit in main.lua at line 707
    [5.552][5.552:588](),[5.46][5.588:739](),[5.2966][5.588:739](),[5.588][5.588:739](),[5.739][5.6231:6278](),[5.283][5.778:783](),[5.662][5.778:783](),[5.6278][5.778:783](),[5.778][5.778:783](),[5.783][5.157:200](),[5.95][5.820:856](),[5.200][5.820:856](),[5.3016][5.820:856](),[5.820][5.820:856](),[5.856][5.201:383](),[5.383][5.856:889](),[5.856][5.856:889](),[5.889][5.384:450](),[5.450][5.889:959](),[5.889][5.889:959](),[5.959][2.1:130](),[2.130][5.1038:1056](),[5.1038][5.1038:1056](),[5.1056][5.576:584](),[5.576][5.576:584](),[5.584][2.131:156](),[2.156][5.1079:1133](),[5.1079][5.1079:1133](),[5.584][5.482:488](),[5.1072][5.482:488](),[5.1133][5.482:488](),[5.482][5.482:488](),[5.488][5.1134:1149](),[5.1149][5.488:493](),[5.488][5.488:493]()
    function cursor_x(line, cursor_pos)
    local line_before_cursor = line:sub(1, cursor_pos-1)
    local text_before_cursor = love.graphics.newText(love.graphics.getFont(), line_before_cursor)
    return 25+text_before_cursor:getWidth()*Zoom
    end
    function nearest_cursor_pos(line, x, hint)
    if x == 0 then
    return 1
    end
    local max_x = cursor_x(line, #line+1)
    if x > max_x then
    return #line+1
    end
    local currx = cursor_x(line, hint)
    if currx > x-2 and currx < x+2 then
    return hint
    end
    local left, right = 1, #line+1
    if currx > x then
    right = hint
    else
    left = hint
    end
    while left < right-1 do
    local curr = math.floor((left+right)/2)
    local currxmin = cursor_x(line, curr)
    local currxmax = cursor_x(line, curr+1)
    if currxmin <= x and x < currxmax then
    return curr
    end
    if currxmin > x then
    right = curr
    else
    left = curr
    end
    end
    return right
    end