-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.lua
162 lines (133 loc) · 3.68 KB
/
main.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
--[[pod_format="raw",created="2024-03-15 21:08:04",modified="2024-04-03 21:55:58",revision=1412]]
printh"---"
dev = fstat"/ram/devmode.txt" and env().corun_program
-- dev_import_filename = dev and "/desktop/p8/secretpal_test.p8"
-- dev_export_filename = dev and "/desktop/temp.p64"
local function _include_lib(name,cond)
if (cond) cp("/appdata/system/"..name,"/ram/cart/"..name)
include(name)
end
local function include_lib(name) _include_lib(name,dev and not fstat(name)) end
local function include_libf(name) _include_lib(name,dev) end --force
include_lib "lib/pq.lua"
include_lib "lib/sort.lua"
include "src/tool.lua"
include "src/gui.lua"
include "src/import.lua"
include "src/warn.lua"
include "src/export.lua"
function _init()
reset_state() --set up the window
menuitem{
id = "clear",
label = "\^:0f19392121213f00 Clear",
shortcut = "CTRL-N",
action = reset_state,
}
menuitem{
id = "open_file",
label = "\^:00387f7f7f7f7f00 Import .p8",
shortcut = "CTRL-O",
action = function()
real_intention="import_p8"
create_process("/system/apps/filenav.p64", {
path="/desktop",
-- open_with = env().prog_name, --this just launches a new p8x8 window; no good
intention="save_file_as", -- TODO: I'd rather use "open_file" but the filesystem doesn't let me process the file -- it tries to literally open it (and fails b/c it doesn't know how to open a .p8)
window_attribs={workspace="current", autoclose=true},
-- use_ext="p8", --?
})
end,
}
menuitem{
id = "export_p64",
label = "\^:7f4141417f616500 Export .p64",
shortcut = "CTRL-E",
action = action_export_p64,
}
-- edit palette (bg checkboard)
pal(0x20,0x1a1520,2)
pal(0x21,0x0f0415,2)
local import_filename = env().argv[1] or dev_import_filename
if import_filename then
import_p8(import_filename)
end
end
function _update()
if has_focus then
gui:update_all()
end
end
function _draw()
gui:draw_all()
end
has_focus=true
on_event("lost_focus",function() has_focus=false end)
on_event("gained_focus",function() has_focus=true end)
on_event("resize", function(msg)
generate_gui(msg.width,msg.height)
end)
-- invoked by filenav intention
on_event("save_file_as", function(msg)
-- HACK: open_file doesn't work nicely, so I run multiple actions pretending to be "save as", because they let me give the user a file chooser without taking any automatic action
if real_intention=="import_p8" then
import_p8(msg.filename)
elseif real_intention=="export_p64" then
export_p64(msg.filename)
end
end)
----------
-- DATA --
----------
function reset_state()
active_cart = nil
gui_set_preview_image(nil)
export_path = nil
window{
width = 140,
height = 104,
title = "p8x8 converter",
autoclose = true, -- esc=quit
}
generate_gui()
end
-- drag-and-drop .p8 files from the desktop
on_event("drop_items",function(msg)
local err
for item in all(msg.items) do
if item.pod_type == "file_reference" then
if item.attrib == "file" then
local ext = item.fullpath:ext()
if ext == "p8" then
import_p8(item.fullpath)
return
else
err = string.format("*error: want a '.p8' file, got '%s'",ext)
end
else
err = "*error: want a '.p8' file, got a folder"
end
end
end
if err then
notify(err)
end
end)
---------------
function action_export_p64()
if dev_export_filename then
export_p64(dev_export_filename)
else
export_p64(export_path)
--[[
-- TODO: open filepicker with default file of export_path
-- I'm not sure how to set the default name (is it possible?)
real_intention="export_p64"
create_process("/system/apps/filenav.p64", {
path="/desktop",
intention="save_file_as",
window_attribs={workspace="current", autoclose=true},
})
--]]
end
end