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
16 -- called only for real run
17 function run
.initialize(arg
)
22 run
.initialize_default_settings()
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 edit
.fixup_cursor(Editor_state
)
33 load_from_disk(Editor_state
)
34 Text
.redraw_all(Editor_state
)
35 if Editor_state
.cursor1
.line
> #Editor_state
.lines
or Editor_state
.lines
[Editor_state
.cursor1
.line
].mode
~= 'text' then
36 edit
.fixup_cursor(Editor_state
)
39 love
.window
.setTitle('lines.love - '..Editor_state
.filename
)
42 print('ignoring commandline args after '..arg
[1])
45 if rawget(_G
, 'jit') then
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
= love
.window
.getMode()
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 love
.window
.setMode(App
.screen
.width
, App
.screen
.height
, App
.screen
.flags
)
61 love
.window
.setPosition(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
68 function run
.initialize_default_settings()
69 local font_height
= 20
70 love
.graphics
.setFont(love
.graphics
.newFont(font_height
))
71 local em
= App
.newText(love
.graphics
.getFont(), 'm')
72 run
.initialize_window_geometry(App
.width(em
))
73 Editor_state
= edit
.initialize_state(Margin_top
, Margin_left
, App
.screen
.width
-Margin_right
)
74 Editor_state
.font_height
= font_height
75 Editor_state
.line_height
= math
.floor(font_height
*1.3)
77 Settings
= run
.settings()
80 function run
.initialize_window_geometry(em_width
)
82 love
.window
.setMode(0, 0) -- maximize
83 App
.screen
.width
, App
.screen
.height
, App
.screen
.flags
= love
.window
.getMode()
84 -- shrink height slightly to account for window decoration
85 App
.screen
.height
= App
.screen
.height
-100
86 App
.screen
.width
= 40*em_width
87 App
.screen
.flags
.resizable
= true
88 App
.screen
.flags
.minwidth
= math
.min(App
.screen
.width
, 200)
89 App
.screen
.flags
.minheight
= math
.min(App
.screen
.height
, 200)
90 love
.window
.setMode(App
.screen
.width
, App
.screen
.height
, App
.screen
.flags
)
93 function run
.resize(w
, h
)
94 --? print(("Window resized to width: %d and height: %d."):format(w, h))
95 App
.screen
.width
, App
.screen
.height
= w
, h
96 Text
.redraw_all(Editor_state
)
97 Editor_state
.selection1
= {} -- no support for shift drag while we're resizing
98 Editor_state
.right
= App
.screen
.width
-Margin_right
99 Editor_state
.width
= Editor_state
.right
-Editor_state
.left
100 Text
.tweak_screen_top_and_cursor(Editor_state
, Editor_state
.left
, Editor_state
.right
)
103 function run
.file_drop(file
)
104 -- first make sure to save edits on any existing file
105 if Editor_state
.next_save
then
106 save_to_disk(Editor_state
)
108 -- clear the slate for the new file
109 App
.initialize_globals()
110 Editor_state
.filename
= file
:getFilename()
112 Editor_state
.lines
= load_from_file(file
)
114 Text
.redraw_all(Editor_state
)
115 edit
.fixup_cursor(Editor_state
)
116 love
.window
.setTitle('lines.love - '..Editor_state
.filename
)
120 edit
.draw(Editor_state
)
123 function run
.update(dt
)
124 Cursor_time
= Cursor_time
+ dt
125 edit
.update(Editor_state
, dt
)
129 edit
.quit(Editor_state
)
132 function run
.settings()
133 if Settings
== nil then
136 if Current_app
== 'run' then
137 Settings
.x
, Settings
.y
, Settings
.displayindex
= love
.window
.getPosition()
139 local filename
= Editor_state
.filename
140 if is_relative_path(filename
) then
141 filename
= love
.filesystem
.getWorkingDirectory()..'/'..filename
-- '/' should work even on Windows
144 x
=Settings
.x
, y
=Settings
.y
, displayindex
=Settings
.displayindex
,
145 width
=App
.screen
.width
, height
=App
.screen
.height
,
146 font_height
=Editor_state
.font_height
,
148 screen_top
=Editor_state
.screen_top1
, cursor
=Editor_state
.cursor1
152 function run
.mouse_press(x
,y
, mouse_button
)
153 Cursor_time
= 0 -- ensure cursor is visible immediately after it moves
154 return edit
.mouse_press(Editor_state
, x
,y
, mouse_button
)
157 function run
.mouse_release(x
,y
, mouse_button
)
158 Cursor_time
= 0 -- ensure cursor is visible immediately after it moves
159 return edit
.mouse_release(Editor_state
, x
,y
, mouse_button
)
162 function run
.text_input(t
)
163 Cursor_time
= 0 -- ensure cursor is visible immediately after it moves
164 return edit
.text_input(Editor_state
, t
)
167 function run
.keychord_press(chord
, key
)
168 Cursor_time
= 0 -- ensure cursor is visible immediately after it moves
169 return edit
.keychord_press(Editor_state
, chord
, key
)
172 function run
.key_release(key
, scancode
)
173 Cursor_time
= 0 -- ensure cursor is visible immediately after it moves
174 return edit
.key_release(Editor_state
, key
, scancode
)
177 -- use this sparingly
179 if Text_cache
[s
] == nil then
180 Text_cache
[s
] = App
.newText(love
.graphics
.getFont(), s
)