.
[view.love.git] / run.lua
blob049362e1541fa2ce478d26378052d65b4d2c0083
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 -- a few text objects we can avoid recomputing unless the font changes
10 Text_cache = {}
12 -- blinking cursor
13 Cursor_time = 0
14 end
16 -- called only for real run
17 function run.initialize(arg)
18 log_new('run')
19 if Settings then
20 run.load_settings()
21 else
22 run.initialize_default_settings()
23 end
25 if #arg > 0 then
26 Editor_state.filename = arg[1]
27 load_from_disk(Editor_state)
28 Text.redraw_all(Editor_state)
29 Editor_state.screen_top1 = {line=1, pos=1}
30 Editor_state.cursor1 = {line=1, pos=1}
31 else
32 load_from_disk(Editor_state)
33 Text.redraw_all(Editor_state)
34 end
35 edit.check_locs(Editor_state)
39 -- keep a few blank lines around: https://merveilles.town/@akkartik/110084833821965708
40 love.window.setTitle('lines.love - '..Editor_state.filename)
44 if #arg > 1 then
45 print('ignoring commandline args after '..arg[1])
46 end
48 if rawget(_G, 'jit') then
49 jit.off()
50 jit.flush()
51 end
52 end
54 function run.load_settings()
55 love.graphics.setFont(love.graphics.newFont(Settings.font_height))
56 -- determine default dimensions and flags
57 App.screen.width, App.screen.height, App.screen.flags = App.screen.size()
58 -- set up desired window dimensions
59 App.screen.flags.resizable = true
60 App.screen.flags.minwidth = math.min(App.screen.width, 200)
61 App.screen.flags.minheight = math.min(App.screen.height, 200)
62 App.screen.width, App.screen.height = Settings.width, Settings.height
63 App.screen.resize(App.screen.width, App.screen.height, App.screen.flags)
64 App.screen.move(Settings.x, Settings.y, Settings.displayindex)
65 Editor_state = edit.initialize_state(Margin_top, Margin_left, App.screen.width-Margin_right, Settings.font_height, math.floor(Settings.font_height*1.3))
66 Editor_state.filename = Settings.filename
67 Editor_state.screen_top1 = Settings.screen_top
68 Editor_state.cursor1 = Settings.cursor
69 end
71 function run.initialize_default_settings()
72 local font_height = 20
73 love.graphics.setFont(love.graphics.newFont(font_height))
74 local em = App.newText(love.graphics.getFont(), 'm')
75 run.initialize_window_geometry(App.width(em))
76 Editor_state = edit.initialize_state(Margin_top, Margin_left, App.screen.width-Margin_right)
77 Editor_state.font_height = font_height
78 Editor_state.line_height = math.floor(font_height*1.3)
79 Editor_state.em = em
80 Settings = run.settings()
81 end
83 function run.initialize_window_geometry(em_width)
84 local os = love.system.getOS()
85 if os == 'Android' or os == 'iOS' then
86 -- maximizing on iOS breaks text rendering: https://github.com/deltadaedalus/vudu/issues/7
87 -- no point second-guessing window dimensions on mobile
88 App.screen.width, App.screen.height, App.screen.flags = App.screen.size()
89 return
90 end
91 -- maximize window
92 App.screen.resize(0, 0) -- maximize
93 App.screen.width, App.screen.height, App.screen.flags = App.screen.size()
94 -- shrink height slightly to account for window decoration
95 App.screen.height = App.screen.height-100
96 App.screen.width = 40*em_width
97 App.screen.flags.resizable = true
98 App.screen.flags.minwidth = math.min(App.screen.width, 200)
99 App.screen.flags.minheight = math.min(App.screen.height, 200)
100 App.screen.resize(App.screen.width, App.screen.height, App.screen.flags)
103 function run.resize(w, h)
104 --? print(("Window resized to width: %d and height: %d."):format(w, h))
105 App.screen.width, App.screen.height = w, h
106 Text.redraw_all(Editor_state)
107 Editor_state.selection1 = {} -- no support for shift drag while we're resizing
108 Editor_state.right = App.screen.width-Margin_right
109 Editor_state.width = Editor_state.right-Editor_state.left
110 Text.tweak_screen_top_and_cursor(Editor_state, Editor_state.left, Editor_state.right)
113 function run.file_drop(file)
114 -- first make sure to save edits on any existing file
115 if Editor_state.next_save then
116 save_to_disk(Editor_state)
118 -- clear the slate for the new file
119 App.initialize_globals()
120 Editor_state.filename = file:getFilename()
121 file:open('r')
122 Editor_state.lines = load_from_file(file)
123 file:close()
124 Text.redraw_all(Editor_state)
125 Editor_state.screen_top1 = {line=1, pos=1}
126 Editor_state.cursor1 = {line=1, pos=1}
130 -- keep a few blank lines around: https://merveilles.town/@akkartik/110084833821965708
131 love.window.setTitle('lines.love - '..Editor_state.filename)
137 function run.draw()
138 edit.draw(Editor_state)
141 function run.update(dt)
142 Cursor_time = Cursor_time + dt
143 edit.update(Editor_state, dt)
146 function run.quit()
147 edit.quit(Editor_state)
150 function run.settings()
151 if Settings == nil then
152 Settings = {}
154 if Current_app == 'run' then
155 Settings.x, Settings.y, Settings.displayindex = App.screen.position()
157 local filename = Editor_state.filename
158 if is_relative_path(filename) then
159 filename = love.filesystem.getWorkingDirectory()..'/'..filename -- '/' should work even on Windows
161 return {
162 x=Settings.x, y=Settings.y, displayindex=Settings.displayindex,
163 width=App.screen.width, height=App.screen.height,
164 font_height=Editor_state.font_height,
165 filename=filename,
166 screen_top=Editor_state.screen_top1, cursor=Editor_state.cursor1
170 function run.mouse_press(x,y, mouse_button)
171 Cursor_time = 0 -- ensure cursor is visible immediately after it moves
172 return edit.mouse_press(Editor_state, x,y, mouse_button)
175 function run.mouse_release(x,y, mouse_button)
176 Cursor_time = 0 -- ensure cursor is visible immediately after it moves
177 return edit.mouse_release(Editor_state, x,y, mouse_button)
180 function run.mouse_wheel_move(dx,dy)
181 Cursor_time = 0 -- ensure cursor is visible immediately after it moves
182 return edit.mouse_wheel_move(Editor_state, dx,dy)
185 function run.text_input(t)
186 Cursor_time = 0 -- ensure cursor is visible immediately after it moves
187 return edit.text_input(Editor_state, t)
190 function run.keychord_press(chord, key)
191 Cursor_time = 0 -- ensure cursor is visible immediately after it moves
192 return edit.keychord_press(Editor_state, chord, key)
195 function run.key_release(key, scancode)
196 Cursor_time = 0 -- ensure cursor is visible immediately after it moves
197 return edit.key_release(Editor_state, key, scancode)
200 -- use this sparingly
201 function to_text(s)
202 if Text_cache[s] == nil then
203 Text_cache[s] = App.newText(love.graphics.getFont(), s)
205 return Text_cache[s]