bugfix: printing the first part of a line at the bottom made it seem non-wrapping
Still lots wrong here.
This commit is contained in:
parent
96dfe4f669
commit
84898ed43a
26
main.lua
26
main.lua
|
@ -64,20 +64,22 @@ Zoom = 1.5
|
||||||
|
|
||||||
Filename = love.filesystem.getUserDirectory()..'/lines.txt'
|
Filename = love.filesystem.getUserDirectory()..'/lines.txt'
|
||||||
|
|
||||||
|
New_foo = true
|
||||||
|
|
||||||
function love.load(arg)
|
function love.load(arg)
|
||||||
-- maximize window
|
-- maximize window
|
||||||
love.window.setMode(0, 0) -- maximize
|
--? love.window.setMode(0, 0) -- maximize
|
||||||
Screen_width, Screen_height, Screen_flags = love.window.getMode()
|
--? Screen_width, Screen_height, Screen_flags = love.window.getMode()
|
||||||
-- shrink slightly to account for window decoration
|
--? -- shrink slightly to account for window decoration
|
||||||
Screen_width = Screen_width-100
|
--? Screen_width = Screen_width-100
|
||||||
Screen_height = Screen_height-100
|
--? Screen_height = Screen_height-100
|
||||||
-- for testing line wrap
|
-- for testing line wrap
|
||||||
--? Screen_width = 120
|
Screen_width = 120
|
||||||
--? Screen_height = 200
|
Screen_height = 200
|
||||||
love.window.setMode(Screen_width, Screen_height)
|
love.window.setMode(Screen_width, Screen_height)
|
||||||
love.window.setTitle('Text with Lines')
|
love.window.setTitle('Text with Lines')
|
||||||
--? Line_width = 100
|
Line_width = 100
|
||||||
Line_width = math.floor(Screen_width/2/40)*40
|
--? Line_width = math.floor(Screen_width/2/40)*40
|
||||||
love.keyboard.setTextInput(true) -- bring up keyboard on touch screen
|
love.keyboard.setTextInput(true) -- bring up keyboard on touch screen
|
||||||
love.keyboard.setKeyRepeat(true)
|
love.keyboard.setKeyRepeat(true)
|
||||||
if #arg > 0 then
|
if #arg > 0 then
|
||||||
|
@ -116,7 +118,9 @@ function love.draw()
|
||||||
line.y = nil
|
line.y = nil
|
||||||
end
|
end
|
||||||
local y = 15
|
local y = 15
|
||||||
|
if New_foo then print('== draw') end
|
||||||
for line_index,line in ipairs(Lines) do
|
for line_index,line in ipairs(Lines) do
|
||||||
|
if New_foo then print('draw:', line_index, y) end
|
||||||
if y + math.floor(15*Zoom) > Screen_height then break end
|
if y + math.floor(15*Zoom) > Screen_height then break end
|
||||||
if line_index >= Screen_top1.line then
|
if line_index >= Screen_top1.line then
|
||||||
Screen_bottom1.line = line_index
|
Screen_bottom1.line = line_index
|
||||||
|
@ -140,12 +144,15 @@ function love.draw()
|
||||||
Drawing.draw(line)
|
Drawing.draw(line)
|
||||||
y = y + Drawing.pixels(line.h) + 10 -- padding
|
y = y + Drawing.pixels(line.h) + 10 -- padding
|
||||||
else
|
else
|
||||||
|
if New_foo then print('text') end
|
||||||
line.y = y
|
line.y = y
|
||||||
y, Screen_bottom1.pos = Text.draw(line, Line_width, line_index)
|
y, Screen_bottom1.pos = Text.draw(line, Line_width, line_index)
|
||||||
y = y + math.floor(15*Zoom) -- text height
|
y = y + math.floor(15*Zoom) -- text height
|
||||||
|
if New_foo then print('aa', y) end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
New_foo = false
|
||||||
--? print('screen bottom: '..tostring(Screen_bottom1.pos)..' in '..tostring(Lines[Screen_bottom1.line].data))
|
--? print('screen bottom: '..tostring(Screen_bottom1.pos)..' in '..tostring(Lines[Screen_bottom1.line].data))
|
||||||
--? os.exit(1)
|
--? os.exit(1)
|
||||||
end
|
end
|
||||||
|
@ -186,6 +193,7 @@ function love.textinput(t)
|
||||||
end
|
end
|
||||||
|
|
||||||
function keychord_pressed(chord)
|
function keychord_pressed(chord)
|
||||||
|
New_foo = true
|
||||||
if love.mouse.isDown('1') or chord:sub(1,2) == 'C-' then
|
if love.mouse.isDown('1') or chord:sub(1,2) == 'C-' then
|
||||||
Drawing.keychord_pressed(chord)
|
Drawing.keychord_pressed(chord)
|
||||||
elseif chord == 'escape' and love.mouse.isDown('1') then
|
elseif chord == 'escape' and love.mouse.isDown('1') then
|
||||||
|
|
47
text.lua
47
text.lua
|
@ -28,9 +28,7 @@ function Text.draw(line, line_width, line_index)
|
||||||
assert(x > 25) -- no overfull lines
|
assert(x > 25) -- no overfull lines
|
||||||
if line_index > Screen_top1.line or pos > Screen_top1.pos then
|
if line_index > Screen_top1.line or pos > Screen_top1.pos then
|
||||||
y = y + math.floor(15*Zoom)
|
y = y + math.floor(15*Zoom)
|
||||||
if y + math.floor(15*Zoom) > Screen_height then
|
if New_foo then print('text: new screen line', y, Screen_height, screen_line_starting_pos) end
|
||||||
return y, screen_line_starting_pos
|
|
||||||
end
|
|
||||||
screen_line_starting_pos = pos
|
screen_line_starting_pos = pos
|
||||||
if Debug_new_render then print('y', y) end
|
if Debug_new_render then print('y', y) end
|
||||||
end
|
end
|
||||||
|
@ -40,6 +38,11 @@ function Text.draw(line, line_width, line_index)
|
||||||
else
|
else
|
||||||
table.insert(line.screen_line_starting_pos, pos)
|
table.insert(line.screen_line_starting_pos, pos)
|
||||||
end
|
end
|
||||||
|
if line_index > Screen_top1.line or pos > Screen_top1.pos then
|
||||||
|
if y + math.floor(15*Zoom) >= Screen_height then
|
||||||
|
return y, screen_line_starting_pos
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
if Debug_new_render then print('checking to draw', pos, Screen_top1.pos) end
|
if Debug_new_render then print('checking to draw', pos, Screen_top1.pos) end
|
||||||
if line_index > Screen_top1.line or pos >= Screen_top1.pos then
|
if line_index > Screen_top1.line or pos >= Screen_top1.pos then
|
||||||
|
@ -279,42 +282,43 @@ function Text.keychord_pressed(chord)
|
||||||
end
|
end
|
||||||
elseif chord == 'down' then
|
elseif chord == 'down' then
|
||||||
assert(Lines[Cursor1.line].mode == 'text')
|
assert(Lines[Cursor1.line].mode == 'text')
|
||||||
|
print('down', Cursor1.line, Cursor1.pos, Screen_top1.line, Screen_top1.pos, Screen_bottom1.line, Screen_bottom1.pos)
|
||||||
if Text.cursor_at_final_screen_line() then
|
if Text.cursor_at_final_screen_line() then
|
||||||
-- line is done, skip to next text line
|
-- line is done, skip to next text line
|
||||||
--? print('down: cursor at final screen line of its line')
|
print('cursor at final screen line of its line')
|
||||||
|
--? os.exit(1)
|
||||||
local new_cursor_line = Cursor1.line
|
local new_cursor_line = Cursor1.line
|
||||||
while new_cursor_line < #Lines do
|
while new_cursor_line < #Lines do
|
||||||
new_cursor_line = new_cursor_line+1
|
new_cursor_line = new_cursor_line+1
|
||||||
if Lines[new_cursor_line].mode == 'text' then
|
if Lines[new_cursor_line].mode == 'text' then
|
||||||
Cursor1.line = new_cursor_line
|
Cursor1.line = new_cursor_line
|
||||||
Cursor1.pos = Text.nearest_cursor_pos(Lines[Cursor1.line].data, Cursor_x)
|
Cursor1.pos = Text.nearest_cursor_pos(Lines[Cursor1.line].data, Cursor_x)
|
||||||
--? print(Cursor1.pos)
|
print(Cursor1.pos)
|
||||||
break
|
break
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
--? print(Cursor1.line, Cursor1.pos, Screen_bottom1.line)
|
|
||||||
if Cursor1.line > Screen_bottom1.line then
|
if Cursor1.line > Screen_bottom1.line then
|
||||||
--? print('screen top before:', Screen_top1.line, Screen_top1.pos)
|
print('screen top before:', Screen_top1.line, Screen_top1.pos)
|
||||||
Screen_top1.line = Cursor1.line
|
Screen_top1.line = Cursor1.line
|
||||||
--? print('scroll up preserving cursor')
|
print('scroll up preserving cursor')
|
||||||
Text.scroll_up_while_cursor_on_screen()
|
Text.scroll_up_while_cursor_on_screen()
|
||||||
--? print('screen top after:', Screen_top1.line, Screen_top1.pos)
|
print('screen top after:', Screen_top1.line, Screen_top1.pos)
|
||||||
end
|
end
|
||||||
--? print('=>', Cursor1.line, Cursor1.pos, Screen_bottom1.line)
|
|
||||||
else
|
else
|
||||||
-- move down one screen line in current line
|
-- move down one screen line in current line
|
||||||
--? print('cursor is NOT at final screen line of its line')
|
print('cursor is NOT at final screen line of its line')
|
||||||
local screen_line_index, screen_line_starting_pos = Text.pos_at_start_of_cursor_screen_line()
|
local screen_line_index, screen_line_starting_pos = Text.pos_at_start_of_cursor_screen_line()
|
||||||
new_screen_line_starting_pos = Lines[Cursor1.line].screen_line_starting_pos[screen_line_index+1]
|
new_screen_line_starting_pos = Lines[Cursor1.line].screen_line_starting_pos[screen_line_index+1]
|
||||||
--? print('switching pos of screen line at cursor from '..tostring(screen_line_starting_pos)..' to '..tostring(new_screen_line_starting_pos))
|
print('switching pos of screen line at cursor from '..tostring(screen_line_starting_pos)..' to '..tostring(new_screen_line_starting_pos))
|
||||||
local s = string.sub(Lines[Cursor1.line].data, new_screen_line_starting_pos)
|
local s = string.sub(Lines[Cursor1.line].data, new_screen_line_starting_pos)
|
||||||
Cursor1.pos = new_screen_line_starting_pos + Text.nearest_cursor_pos(s, Cursor_x) - 1
|
Cursor1.pos = new_screen_line_starting_pos + Text.nearest_cursor_pos(s, Cursor_x) - 1
|
||||||
--? print('cursor pos is now '..tostring(Cursor1.pos))
|
print('cursor pos is now '..tostring(Cursor1.pos))
|
||||||
Screen_top1.line = Cursor1.line
|
Screen_top1.line = Cursor1.line
|
||||||
--? print('scroll up preserving cursor')
|
print('scroll up preserving cursor')
|
||||||
Text.scroll_up_while_cursor_on_screen()
|
Text.scroll_up_while_cursor_on_screen()
|
||||||
--? print('screen top after:', Screen_top1.line, Screen_top1.pos)
|
print('screen top after:', Screen_top1.line, Screen_top1.pos)
|
||||||
end
|
end
|
||||||
|
print('=>', Cursor1.line, Cursor1.pos, Screen_top1.line, Screen_top1.pos, Screen_bottom1.line, Screen_bottom1.pos)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -336,6 +340,7 @@ function Text.cursor_at_final_screen_line()
|
||||||
return true
|
return true
|
||||||
end
|
end
|
||||||
screen_lines = Lines[Cursor1.line].screen_line_starting_pos
|
screen_lines = Lines[Cursor1.line].screen_line_starting_pos
|
||||||
|
print(screen_lines[#screen_lines], Cursor1.pos)
|
||||||
return screen_lines[#screen_lines] <= Cursor1.pos
|
return screen_lines[#screen_lines] <= Cursor1.pos
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -358,13 +363,13 @@ function Text.move_cursor_down_to_next_text_line_while_scrolling_again_if_necess
|
||||||
end
|
end
|
||||||
|
|
||||||
function Text.scroll_up_while_cursor_on_screen()
|
function Text.scroll_up_while_cursor_on_screen()
|
||||||
local cursor_pos_screen_lines = Text.pos_at_start_of_cursor_screen_line()
|
local cursor2 = Text.to2(Cursor1)
|
||||||
--? print('cursor pos '..tostring(Cursor1.pos)..' is on the #'..tostring(cursor_pos_screen_lines)..' screen line down')
|
print('cursor pos '..tostring(Cursor1.pos)..' is on the #'..tostring(cursor2.screen_line)..' screen line down')
|
||||||
local y = Screen_height - cursor_pos_screen_lines*math.floor(15*Zoom)
|
local y = Screen_height - cursor2.screen_pos*math.floor(15*Zoom)
|
||||||
-- duplicate some logic from love.draw
|
-- duplicate some logic from love.draw
|
||||||
while true do
|
while true do
|
||||||
if Screen_top1.line == 1 then break end
|
if Screen_top1.line == 1 then break end
|
||||||
--? print('y', y)
|
print('y', y)
|
||||||
local h = 0
|
local h = 0
|
||||||
if Lines[Screen_top1.line-1].mode == 'drawing' then
|
if Lines[Screen_top1.line-1].mode == 'drawing' then
|
||||||
h = 20 + Drawing.pixels(Lines[Screen_top1.line-1].h)
|
h = 20 + Drawing.pixels(Lines[Screen_top1.line-1].h)
|
||||||
|
@ -374,7 +379,7 @@ function Text.scroll_up_while_cursor_on_screen()
|
||||||
local n = #Lines[Screen_top1.line-1].screen_line_starting_pos
|
local n = #Lines[Screen_top1.line-1].screen_line_starting_pos
|
||||||
h = h + n*math.floor(15*Zoom) -- text height
|
h = h + n*math.floor(15*Zoom) -- text height
|
||||||
end
|
end
|
||||||
--? print('height:', h)
|
print('height:', h)
|
||||||
if y - h < 0 then
|
if y - h < 0 then
|
||||||
break
|
break
|
||||||
end
|
end
|
||||||
|
@ -482,7 +487,7 @@ end
|
||||||
|
|
||||||
function Text.to2(pos1)
|
function Text.to2(pos1)
|
||||||
local result = {line=pos1.line, screen_line=1}
|
local result = {line=pos1.line, screen_line=1}
|
||||||
if Line[pos1.line].screen_line_starting_pos == nil then
|
if Lines[pos1.line].screen_line_starting_pos == nil then
|
||||||
result.screen_pos = pos1.pos
|
result.screen_pos = pos1.pos
|
||||||
else
|
else
|
||||||
for i=#Lines[pos1.line].screen_line_starting_pos,1,-1 do
|
for i=#Lines[pos1.line].screen_line_starting_pos,1,-1 do
|
||||||
|
|
Loading…
Reference in New Issue