Skip to content

Commit 8c91ebf

Browse files
Doom app: show list before starting
1 parent 6bc7c91 commit 8c91ebf

File tree

2 files changed

+78
-10
lines changed

2 files changed

+78
-10
lines changed

internal_filesystem/apps/com.micropythonos.doom/assets/doom.py

Lines changed: 77 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import lvgl as lv
2+
import os
13
from mpos.apps import Activity
24
from mpos import TaskManager, sdcard
35

@@ -14,32 +16,99 @@ class Doom(Activity):
1416
#partition_label = "retro-core"
1517
# Widgets:
1618
status_label = None
19+
wadlist = None
20+
bootfile_prefix = ""
21+
bootfile_to_write = ""
1722

1823
def onCreate(self):
1924
screen = lv.obj()
25+
screen.set_style_pad_all(15, 0)
26+
27+
# Create list widget for WAD files
28+
self.wadlist = lv.list(screen)
29+
self.wadlist.set_size(lv.pct(100), lv.pct(85))
30+
self.wadlist.align(lv.ALIGN.TOP_MID, 0, 0)
31+
32+
# Create status label for messages
2033
self.status_label = lv.label(screen)
2134
self.status_label.set_width(lv.pct(90))
22-
self.status_label.set_text(f'Looking for .wad or .zip files in {self.doomdir} on internal storage and SD card...')
2335
self.status_label.set_long_mode(lv.label.LONG_MODE.WRAP)
24-
self.status_label.center()
36+
self.status_label.align_to(self.wadlist, lv.ALIGN.OUT_BOTTOM_MID, 0, 0)
37+
self.status_label.add_flag(lv.obj.FLAG.HIDDEN)
38+
2539
self.setContentView(screen)
2640

2741
def onResume(self, screen):
2842
# Try to mount the SD card and if successful, use it, as retro-go can only use one or the other:
29-
bootfile_prefix = ""
43+
self.bootfile_prefix = ""
3044
mounted_sdcard = sdcard.mount_with_optional_format(self.mountpoint_sdcard)
3145
if mounted_sdcard:
3246
print("sdcard is mounted, configuring it...")
33-
bootfile_prefix = self.mountpoint_sdcard
34-
bootfile_to_write = bootfile_prefix + self.bootfile
35-
print(f"writing to {bootfile_to_write}")
47+
self.bootfile_prefix = self.mountpoint_sdcard
48+
self.bootfile_to_write = self.bootfile_prefix + self.bootfile
49+
print(f"writing to {self.bootfile_to_write}")
50+
51+
# Scan for WAD files and populate the list
52+
self.refresh_wad_list()
53+
54+
def scan_wad_files(self, directory):
55+
"""Scan a directory for .wad and .zip files"""
56+
wad_files = []
57+
try:
58+
for filename in os.listdir(directory):
59+
if filename.lower().endswith(('.wad', '.zip')):
60+
wad_files.append(filename)
61+
62+
# Sort the list for consistent ordering
63+
wad_files.sort()
64+
print(f"Found {len(wad_files)} WAD files in {directory}: {wad_files}")
65+
except OSError as e:
66+
print(f"Directory does not exist or cannot be read: {directory}")
67+
except Exception as e:
68+
print(f"Error scanning directory {directory}: {e}")
69+
70+
return wad_files
71+
72+
def refresh_wad_list(self):
73+
"""Scan for WAD files and populate the list"""
74+
print("refresh_wad_list: Clearing current list")
75+
self.wadlist.clean()
76+
77+
# Scan both internal storage and SD card
78+
internal_wads = self.scan_wad_files(self.doomdir)
79+
sdcard_wads = []
80+
if self.bootfile_prefix:
81+
sdcard_wads = self.scan_wad_files(self.bootfile_prefix + self.doomdir)
82+
83+
# Combine and deduplicate
84+
all_wads = list(set(internal_wads + sdcard_wads))
85+
all_wads.sort()
86+
87+
if len(all_wads) == 0:
88+
self.status_label.set_text(f"No .wad or .zip files found in {self.doomdir}")
89+
self.status_label.remove_flag(lv.obj.FLAG.HIDDEN)
90+
print("No WAD files found")
91+
return
92+
93+
# Hide status label if we have files
94+
self.status_label.add_flag(lv.obj.FLAG.HIDDEN)
95+
96+
# Populate list with WAD files
97+
print(f"refresh_wad_list: Populating list with {len(all_wads)} WAD files")
98+
for wad_file in all_wads:
99+
button = self.wadlist.add_button(None, wad_file)
100+
button.add_event_cb(lambda e, f=wad_file: self.wad_selected_cb(f), lv.EVENT.CLICKED, None)
101+
102+
def wad_selected_cb(self, wad_file):
103+
"""Handle WAD file selection from list"""
104+
print(f"wad_selected_cb: WAD file selected: {wad_file}")
105+
wadfile_path = self.doomdir + '/' + wad_file
36106
# Do it in a separate task so the UI doesn't hang (shows progress, status_label) and the serial console keeps showing prints
37-
TaskManager.create_task(self.start_wad(bootfile_prefix, bootfile_to_write, self.doomdir + '/Doom v1.9 Free Shareware.zip'))
107+
TaskManager.create_task(self.start_wad(self.bootfile_prefix, self.bootfile_to_write, wadfile_path))
38108

39109
def mkdir(self, dirname):
40110
# Would be better to only create it if it doesn't exist
41111
try:
42-
import os
43112
os.mkdir(dirname)
44113
except Exception as e:
45114
# Not really useful to show this in the UI, as it's usually just an "already exists" error:
@@ -57,7 +126,6 @@ async def start_wad(self, bootfile_prefix, bootfile_to_write, wadfile):
57126
self.mkdir(bootfile_prefix + self.retrogodir)
58127
self.mkdir(bootfile_prefix + self.configdir)
59128
try:
60-
import os
61129
import json
62130
# Would be better to only write this if it differs from what's already there:
63131
fd = open(bootfile_to_write, 'w')

scripts/run_desktop.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ fi
5555
binary=$(readlink -f "$binary")
5656
chmod +x "$binary"
5757

58-
pushd internal_filesystem/
58+
pushd "$scriptdir"/../internal_filesystem/
5959

6060
if [ -f "$script" ]; then
6161
echo "Running script $script"

0 commit comments

Comments
 (0)