add an assert
[lines.love.git] / run.lua
blobd029bede44f6d05aaca28baaa0eb8096964b49fc
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 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 run.load_settings()
52 love.graphics.setFont(love.graphics.newFont(Settings.font_height))
53 -- determine default dimensions and flags
54 App.screen.width, App.screen.height, App.screen.flags = App.screen.size()
55 -- set up desired window dimensions
56 App.screen.flags.resizable = true
57 App.screen.flags.minwidth = math.min(App.screen.width, 200)
58 App.screen.flags.minheight = math.min(App.screen.height, 200)
59 App.screen.width, App.screen.height = Settings.width, Settings.height
60 App.screen.resize(App.screen.width, App.screen.height, App.screen.flags)
61 App.screen.move(Settings.x, Settings.y, Settings.displayindex)
62 Editor_state = edit.initialize_state(Margin_top, Margin_left, App.screen.width-Margin_right, Settings.font_height, math.floor(Settings.font_height*1.3))
63 Editor_state.filename = Settings.filename
64 Editor_state.screen_top1 = Settings.screen_top
65 Editor_state.cursor1 = Settings.cursor
66 end
68 function run.initialize_default_settings()
69 local font_height = 20
70 love.graphics.setFont(love.graphics.newFont(font_height))
71 run.initialize_window_geometry(App.width('m'))
72 Editor_state = edit.initialize_state(Margin_top, Margin_left, App.screen.width-Margin_right)
73 Editor_state.font_height = font_height
74 Editor_state.line_height = math.floor(font_height*1.3)
75 Settings = run.settings()
76 end
78 function run.initialize_window_geometry(em_width)
79 local os = love.system.getOS()
80 if os == 'Android' or os == 'iOS' then
81 -- maximizing on iOS breaks text rendering: https://github.com/deltadaedalus/vudu/issues/7
82 -- no point second-guessing window dimensions on mobile
83 App.screen.width, App.screen.height, App.screen.flags = App.screen.size()
84 return
85 end
86 -- maximize window
87 App.screen.resize(0, 0) -- maximize
88 App.screen.width, App.screen.height, App.screen.flags = App.screen.size()
89 -- shrink height slightly to account for window decoration
90 App.screen.height = App.screen.height-100
91 App.screen.width = 40*em_width
92 App.screen.flags.resizable = true
93 App.screen.flags.minwidth = math.min(App.screen.width, 200)
94 App.screen.flags.minheight = math.min(App.screen.height, 200)
95 App.screen.resize(App.screen.width, App.screen.height, App.screen.flags)
96 end
98 function run.resize(w, h)
99 --? print(("Window resized to width: %d and height: %d."):format(w, h))
100 App.screen.width, App.screen.height = w, h
101 Text.redraw_all(Editor_state)
102 Editor_state.selection1 = {} -- no support for shift drag while we're resizing
103 Editor_state.right = App.screen.width-Margin_right
104 Editor_state.width = Editor_state.right-Editor_state.left
105 Text.tweak_screen_top_and_cursor(Editor_state, Editor_state.left, Editor_state.right)
108 function run.file_drop(file)
109 -- first make sure to save edits on any existing file
110 if Editor_state.next_save then
111 save_to_disk(Editor_state)
113 -- clear the slate for the new file
114 App.initialize_globals()
115 Editor_state.filename = file:getFilename()
116 file:open('r')
117 Editor_state.lines = load_from_file(file)
118 file:close()
119 Text.redraw_all(Editor_state)
120 Editor_state.screen_top1 = {line=1, pos=1}
121 Editor_state.cursor1 = {line=1, pos=1}
125 -- keep a few blank lines around: https://merveilles.town/@akkartik/110084833821965708
126 love.window.setTitle('lines.love - '..Editor_state.filename)
132 function run.draw()
133 edit.draw(Editor_state)
136 function run.update(dt)
137 Cursor_time = Cursor_time + dt
138 edit.update(Editor_state, dt)
141 function run.quit()
142 edit.quit(Editor_state)
145 function run.settings()
146 if Settings == nil then
147 Settings = {}
149 if Current_app == 'run' then
150 Settings.x, Settings.y, Settings.displayindex = App.screen.position()
152 local filename = Editor_state.filename
153 if is_relative_path(filename) then
154 filename = love.filesystem.getWorkingDirectory()..'/'..filename -- '/' should work even on Windows
156 return {
157 x=Settings.x, y=Settings.y, displayindex=Settings.displayindex,
158 width=App.screen.width, height=App.screen.height,
159 font_height=Editor_state.font_height,
160 filename=filename,
161 screen_top=Editor_state.screen_top1, cursor=Editor_state.cursor1
165 function run.mouse_press(x,y, mouse_button)
166 Cursor_time = 0 -- ensure cursor is visible immediately after it moves
167 return edit.mouse_press(Editor_state, x,y, mouse_button)
170 function run.mouse_release(x,y, mouse_button)
171 Cursor_time = 0 -- ensure cursor is visible immediately after it moves
172 return edit.mouse_release(Editor_state, x,y, mouse_button)
175 function run.mouse_wheel_move(dx,dy)
176 Cursor_time = 0 -- ensure cursor is visible immediately after it moves
177 return edit.mouse_wheel_move(Editor_state, dx,dy)
180 function run.text_input(t)
181 Cursor_time = 0 -- ensure cursor is visible immediately after it moves
182 return edit.text_input(Editor_state, t)
185 function run.keychord_press(chord, key)
186 Cursor_time = 0 -- ensure cursor is visible immediately after it moves
187 return edit.keychord_press(Editor_state, chord, key)
190 function run.key_release(key, scancode)
191 Cursor_time = 0 -- ensure cursor is visible immediately after it moves
192 return edit.key_release(Editor_state, key, scancode)
195 function width(s)
196 return love.graphics.getFont():getWidth(s)