start supporting LÖVE v12
[lines.love.git] / run.lua
blob2d8662742f371caf174a86ce539e99559eb703c8
1 run = {}
3 Editor_state = {}
5 -- called both in tests and real run
6 function run.initialize_globals()
7 -- tests currently mostly clear their own state
9 -- blinking cursor
10 Cursor_time = 0
11 end
13 -- called only for real run
14 function run.initialize(arg)
15 log_new('run')
16 if Settings then
17 run.load_settings()
18 else
19 run.initialize_default_settings()
20 end
22 if #arg > 0 and Editor_state.filename ~= absolutize(arg[1]) then
23 Editor_state.filename = arg[1]
24 load_from_disk(Editor_state)
25 Text.redraw_all(Editor_state)
26 Editor_state.screen_top1 = {line=1, pos=1}
27 Editor_state.cursor1 = {line=1, pos=1}
28 else
29 load_from_disk(Editor_state)
30 Text.redraw_all(Editor_state)
31 end
32 edit.check_locs(Editor_state)
36 -- keep a few blank lines around: https://merveilles.town/@akkartik/110084833821965708
37 love.window.setTitle('lines.love - '..Editor_state.filename)
41 if #arg > 1 then
42 print('ignoring commandline args after '..arg[1])
43 end
45 if rawget(_G, 'jit') then
46 jit.off()
47 jit.flush()
48 end
49 end
51 function print_and_log(s)
52 print(s)
53 log(3, s)
54 end
56 function run.load_settings()
57 love.graphics.setFont(love.graphics.newFont(Settings.font_height))
58 -- set up desired window dimensions and make window resizable
59 _, _, App.screen.flags = App.screen.size()
60 App.screen.flags.resizable = true
61 App.screen.width, App.screen.height = Settings.width, Settings.height
62 App.screen.resize(App.screen.width, App.screen.height, App.screen.flags)
63 run.set_window_position_from_settings(Settings)
64 Editor_state = edit.initialize_state(Margin_top, Margin_left, App.screen.width-Margin_right, Settings.font_height, math.floor(Settings.font_height*1.3))
65 Editor_state.filename = Settings.filename
66 Editor_state.screen_top1 = Settings.screen_top
67 Editor_state.cursor1 = Settings.cursor
68 end
70 function run.set_window_position_from_settings(settings)
71 local os = love.system.getOS()
72 if os == 'Linux' then
73 -- love.window.setPosition doesn't quite seem to do what is asked of it on Linux.
74 App.screen.move(settings.x, settings.y-37, settings.displayindex)
75 else
76 App.screen.move(settings.x, settings.y, settings.displayindex)
77 end
78 end
80 function run.initialize_default_settings()
81 local font_height = 20
82 love.graphics.setFont(love.graphics.newFont(font_height))
83 run.initialize_window_geometry()
84 Editor_state = edit.initialize_state(Margin_top, Margin_left, App.screen.width-Margin_right)
85 Editor_state.font_height = font_height
86 Editor_state.line_height = math.floor(font_height*1.3)
87 Settings = run.settings()
88 end
90 function run.initialize_window_geometry()
91 -- Initialize window width/height and make window resizable.
93 -- I get tempted to have opinions about window dimensions here, but they're
94 -- non-portable:
95 -- - maximizing doesn't work on mobile and messes things up
96 -- - maximizing keeps the title bar on screen in Linux, but off screen on
97 -- Windows. And there's no way to get the height of the title bar.
98 -- It seems more robust to just follow LÖVE's default window size until
99 -- someone overrides it.
100 App.screen.width, App.screen.height, App.screen.flags = App.screen.size()
101 App.screen.flags.resizable = true
102 App.screen.resize(App.screen.width, App.screen.height, App.screen.flags)
105 function run.resize(w, h)
106 --? print(("Window resized to width: %d and height: %d."):format(w, h))
107 App.screen.width, App.screen.height = w, h
108 Text.redraw_all(Editor_state)
109 Editor_state.selection1 = {} -- no support for shift drag while we're resizing
110 Editor_state.right = App.screen.width-Margin_right
111 Editor_state.width = Editor_state.right-Editor_state.left
112 Text.tweak_screen_top_and_cursor(Editor_state, Editor_state.left, Editor_state.right)
115 function run.file_drop(file)
116 -- first make sure to save edits on any existing file
117 if Editor_state.next_save then
118 save_to_disk(Editor_state)
120 -- clear the slate for the new file
121 App.initialize_globals()
122 Editor_state.filename = file:getFilename()
123 file:open('r')
124 Editor_state.lines = load_from_file(file)
125 file:close()
126 Text.redraw_all(Editor_state)
127 Editor_state.screen_top1 = {line=1, pos=1}
128 Editor_state.cursor1 = {line=1, pos=1}
132 -- keep a few blank lines around: https://merveilles.town/@akkartik/110084833821965708
133 love.window.setTitle('lines.love - '..Editor_state.filename)
139 function run.draw()
140 edit.draw(Editor_state)
143 function run.update(dt)
144 Cursor_time = Cursor_time + dt
145 edit.update(Editor_state, dt)
148 function run.quit()
149 edit.quit(Editor_state)
152 function run.settings()
153 if Settings == nil then Settings = {} end
154 Settings.x, Settings.y, Settings.displayindex = App.screen.position()
155 return {
156 x=Settings.x, y=Settings.y, displayindex=Settings.displayindex,
157 width=App.screen.width, height=App.screen.height,
158 font_height=Editor_state.font_height,
159 filename=absolutize(Editor_state.filename),
160 screen_top=Editor_state.screen_top1, cursor=Editor_state.cursor1
164 function absolutize(path)
165 if is_relative_path(path) then
166 return love.filesystem.getWorkingDirectory()..'/'..path -- '/' should work even on Windows
168 return path
171 function run.mouse_press(x,y, mouse_button)
172 Cursor_time = 0 -- ensure cursor is visible immediately after it moves
173 return edit.mouse_press(Editor_state, x,y, mouse_button)
176 function run.mouse_release(x,y, mouse_button)
177 Cursor_time = 0 -- ensure cursor is visible immediately after it moves
178 return edit.mouse_release(Editor_state, x,y, mouse_button)
181 function run.mouse_wheel_move(dx,dy)
182 Cursor_time = 0 -- ensure cursor is visible immediately after it moves
183 return edit.mouse_wheel_move(Editor_state, dx,dy)
186 function run.text_input(t)
187 Cursor_time = 0 -- ensure cursor is visible immediately after it moves
188 return edit.text_input(Editor_state, t)
191 function run.keychord_press(chord, key)
192 Cursor_time = 0 -- ensure cursor is visible immediately after it moves
193 return edit.keychord_press(Editor_state, chord, key)
196 function run.key_release(key, scancode)
197 Cursor_time = 0 -- ensure cursor is visible immediately after it moves
198 return edit.key_release(Editor_state, key, scancode)
201 function width(s)
202 return love.graphics.getFont():getWidth(s)