omnipython-apps 0.1.4.dev8__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (218) hide show
  1. apps/__init__.py +5 -0
  2. apps/about/__init__.py +257 -0
  3. apps/about/app.json +1 -0
  4. apps/about/credits.py +12 -0
  5. apps/about/facts.py +398 -0
  6. apps/about/fonts/OFL.txt +93 -0
  7. apps/about/fonts/latin1_14.bin +0 -0
  8. apps/about/fonts/latin1_16.bin +0 -0
  9. apps/about/fonts/latin1_24.bin +0 -0
  10. apps/about/fonts/latin1_32.bin +0 -0
  11. apps/about/fonts/latin1_40.bin +0 -0
  12. apps/backline/__init__.py +58 -0
  13. apps/backline/app.json +1 -0
  14. apps/backline/app.py +475 -0
  15. apps/backline/art.py +717 -0
  16. apps/backline/ask.py +781 -0
  17. apps/backline/box.py +502 -0
  18. apps/backline/cast.py +137 -0
  19. apps/backline/desk/__init__.py +329 -0
  20. apps/backline/desk/editor.py +331 -0
  21. apps/backline/desk/grid.py +154 -0
  22. apps/backline/desk/look.py +169 -0
  23. apps/backline/desk/pedals.py +102 -0
  24. apps/backline/desk/perform.py +157 -0
  25. apps/backline/desk/prompt.py +210 -0
  26. apps/backline/desk/strip.py +159 -0
  27. apps/backline/desk_main.py +226 -0
  28. apps/backline/engine.py +622 -0
  29. apps/backline/floor.py +509 -0
  30. apps/backline/footswitch.py +119 -0
  31. apps/backline/glyphs.py +82 -0
  32. apps/backline/kits.py +428 -0
  33. apps/backline/midipedal.py +362 -0
  34. apps/backline/page_data.py +413 -0
  35. apps/backline/parts.py +145 -0
  36. apps/backline/pedal.py +107 -0
  37. apps/backline/remote.py +134 -0
  38. apps/backline/songs/ballad_68.json +1 -0
  39. apps/backline/songs/bossa.json +1 -0
  40. apps/backline/songs/country_train.json +1 -0
  41. apps/backline/songs/funk.json +1 -0
  42. apps/backline/songs/metal.json +1 -0
  43. apps/backline/songs/pop.json +1 -0
  44. apps/backline/songs/rock.json +1 -0
  45. apps/backline/songs/shuffle.json +1 -0
  46. apps/battery/__init__.py +101 -0
  47. apps/battery/app.json +1 -0
  48. apps/bit_world/act_deck.py +683 -0
  49. apps/bit_world/act_game.py +762 -0
  50. apps/bit_world/act_pet.py +1032 -0
  51. apps/bit_world/app.json +1 -0
  52. apps/bit_world/bit_output.py +45 -0
  53. apps/bit_world/bit_palette.py +503 -0
  54. apps/bit_world/bit_sound.py +431 -0
  55. apps/bit_world/bit_sprites.py +632 -0
  56. apps/bit_world/bit_world.py +534 -0
  57. apps/btreceiver/__init__.py +690 -0
  58. apps/btreceiver/app.json +1 -0
  59. apps/btreceiver/chime.py +155 -0
  60. apps/btreceiver/demo.py +529 -0
  61. apps/btreceiver/screen.py +633 -0
  62. apps/camera/__init__.py +316 -0
  63. apps/camera/app.json +1 -0
  64. apps/card/__init__.py +113 -0
  65. apps/card/app.json +1 -0
  66. apps/cast/__init__.py +631 -0
  67. apps/cast/app.json +1 -0
  68. apps/cast/receivers.py +193 -0
  69. apps/chess/__init__.py +18 -0
  70. apps/chess/app.json +1 -0
  71. apps/chess/app.py +922 -0
  72. apps/chess/art.py +323 -0
  73. apps/chess/ble.py +411 -0
  74. apps/chess/engine.py +925 -0
  75. apps/chess/game.py +327 -0
  76. apps/chess/net.py +261 -0
  77. apps/clock/__init__.py +38 -0
  78. apps/clock/app.json +1 -0
  79. apps/detect/__init__.py +214 -0
  80. apps/detect/app.json +1 -0
  81. apps/fmsynth/__init__.py +41 -0
  82. apps/fmsynth/algorithms.py +61 -0
  83. apps/fmsynth/app.json +1 -0
  84. apps/fmsynth/app.py +355 -0
  85. apps/fmsynth/editor.py +206 -0
  86. apps/fmsynth/engine.py +257 -0
  87. apps/fmsynth/meters.py +262 -0
  88. apps/fmsynth/midi.py +375 -0
  89. apps/fmsynth/panel_view.py +1071 -0
  90. apps/fmsynth/patches/fmsynth-01.syx +0 -0
  91. apps/fmsynth/settings.py +138 -0
  92. apps/fmsynth/voice.py +398 -0
  93. apps/gamepad/__init__.py +685 -0
  94. apps/gamepad/app.json +1 -0
  95. apps/gamepad/pad.py +94 -0
  96. apps/gamepad/watch.py +352 -0
  97. apps/hidden/__init__.py +72 -0
  98. apps/hidden/app.json +1 -0
  99. apps/home/__init__.py +483 -0
  100. apps/home/app.json +1 -0
  101. apps/home/main.py +36 -0
  102. apps/homeassistant/__init__.py +213 -0
  103. apps/homeassistant/api.py +104 -0
  104. apps/homeassistant/app.json +1 -0
  105. apps/huey/__init__.py +287 -0
  106. apps/huey/acting.py +184 -0
  107. apps/huey/actor.py +194 -0
  108. apps/huey/app.json +1 -0
  109. apps/huey/client.py +125 -0
  110. apps/huey/servers.py +121 -0
  111. apps/huey/settings.example.json +8 -0
  112. apps/huey/settings.py +97 -0
  113. apps/huey/turn.py +504 -0
  114. apps/huey/voice.py +217 -0
  115. apps/info/__init__.py +810 -0
  116. apps/info/app.json +1 -0
  117. apps/lora/__init__.py +62 -0
  118. apps/lora/app.json +1 -0
  119. apps/manifest.py +71 -0
  120. apps/medialink_baby_monitor/__init__.py +222 -0
  121. apps/medialink_baby_monitor/app.json +1 -0
  122. apps/medialink_doorbell/__init__.py +251 -0
  123. apps/medialink_doorbell/app.json +1 -0
  124. apps/medialink_hands_free/__init__.py +217 -0
  125. apps/medialink_hands_free/app.json +1 -0
  126. apps/medialink_surveillance_camera/__init__.py +258 -0
  127. apps/medialink_surveillance_camera/app.json +1 -0
  128. apps/medialink_video_doorbell/__init__.py +289 -0
  129. apps/medialink_video_doorbell/app.json +1 -0
  130. apps/medialink_videophone/__init__.py +273 -0
  131. apps/medialink_videophone/app.json +1 -0
  132. apps/medialink_walkie_talkie/__init__.py +228 -0
  133. apps/medialink_walkie_talkie/app.json +1 -0
  134. apps/midicontroller/__init__.py +774 -0
  135. apps/midicontroller/app.json +1 -0
  136. apps/midicontroller/links.py +421 -0
  137. apps/midicontroller/surface.py +343 -0
  138. apps/midiengine/__init__.py +977 -0
  139. apps/midiengine/app.json +1 -0
  140. apps/midiengine/banks.py +85 -0
  141. apps/midiengine/midiin.py +165 -0
  142. apps/midiengine/player.py +392 -0
  143. apps/midiengine/umidiparser.py +1413 -0
  144. apps/mp3/__init__.py +240 -0
  145. apps/mp3/app.json +1 -0
  146. apps/perch/__init__.py +119 -0
  147. apps/perch/app.json +1 -0
  148. apps/perch/huey.py +481 -0
  149. apps/perch/huey_props.py +386 -0
  150. apps/perch/huey_shift.py +42 -0
  151. apps/perch/huey_views.py +296 -0
  152. apps/perch/scene_bench.py +680 -0
  153. apps/perch/scene_coast.py +479 -0
  154. apps/perch/scene_desk.py +492 -0
  155. apps/perch/stage.py +1274 -0
  156. apps/perch/vec.py +366 -0
  157. apps/repl/__init__.py +325 -0
  158. apps/repl/app.json +1 -0
  159. apps/roku/__init__.py +156 -0
  160. apps/roku/app.json +1 -0
  161. apps/rokucam/__init__.py +362 -0
  162. apps/rokucam/app.json +1 -0
  163. apps/security/__init__.py +261 -0
  164. apps/security/app.json +1 -0
  165. apps/settings/__init__.py +221 -0
  166. apps/settings/app.json +1 -0
  167. apps/shell/__init__.py +33 -0
  168. apps/shell/app.json +1 -0
  169. apps/spotify/__init__.py +669 -0
  170. apps/spotify/app.json +1 -0
  171. apps/spotify/screen.py +82 -0
  172. apps/steps/__init__.py +58 -0
  173. apps/steps/app.json +1 -0
  174. apps/stompbox/__init__.py +31 -0
  175. apps/stompbox/app.json +1 -0
  176. apps/stompbox/app.py +668 -0
  177. apps/stompbox/boards.json +20 -0
  178. apps/stompbox/effects.py +241 -0
  179. apps/stompbox/engine.py +340 -0
  180. apps/stompbox/footswitch.py +331 -0
  181. apps/stompbox/inputs.py +449 -0
  182. apps/stompbox/meter.py +72 -0
  183. apps/stompbox/outputs.py +135 -0
  184. apps/stompbox/pedals.txt +45 -0
  185. apps/stompbox/presets.py +106 -0
  186. apps/stompbox/styles.py +135 -0
  187. apps/stompbox/ui.py +817 -0
  188. apps/stompbox/wavfile.py +73 -0
  189. apps/synthstation/__init__.py +20 -0
  190. apps/synthstation/app.json +1 -0
  191. apps/synthstation/audio.py +397 -0
  192. apps/synthstation/catalog.py +50 -0
  193. apps/synthstation/meters.py +136 -0
  194. apps/synthstation/midi.py +380 -0
  195. apps/synthstation/pads.py +177 -0
  196. apps/synthstation/parts.py +128 -0
  197. apps/synthstation/station.py +1069 -0
  198. apps/system/__init__.py +11 -0
  199. apps/system/app.json +1 -0
  200. apps/touchcal/__init__.py +324 -0
  201. apps/touchcal/app.json +1 -0
  202. apps/touchcal/fit.py +262 -0
  203. apps/wake/__init__.py +268 -0
  204. apps/wake/app.json +1 -0
  205. apps/wake/feed.py +155 -0
  206. apps/watch/__init__.py +355 -0
  207. apps/watch/app.json +1 -0
  208. apps/watch/faces.py +35 -0
  209. apps/watch/wheel.py +157 -0
  210. apps/weather/__init__.py +278 -0
  211. apps/weather/app.json +1 -0
  212. apps/weather/icons.py +172 -0
  213. apps/weather/sizing.py +71 -0
  214. apps/weather/zippad.py +172 -0
  215. omnipython_apps-0.1.4.dev8.dist-info/METADATA +33 -0
  216. omnipython_apps-0.1.4.dev8.dist-info/RECORD +218 -0
  217. omnipython_apps-0.1.4.dev8.dist-info/WHEEL +5 -0
  218. omnipython_apps-0.1.4.dev8.dist-info/top_level.txt +1 -0
@@ -0,0 +1,1032 @@
1
+ # SPDX-FileCopyrightText: 2026 Brad Barnett
2
+ #
3
+ # SPDX-License-Identifier: MIT
4
+
5
+ """Act 1: Bit's Solarpunk Workshop (Desktop Companion and Smart Assistant).
6
+
7
+ Showcases how pygraphics powers expressive desktop pets, smart home companions,
8
+ and ambient desk displays with multi-shade pixel art, eye tracking, typewriter
9
+ speech bubbles, live voice waveforms, and interactive reaction buttons.
10
+
11
+ Needs: pygraphics, bit_palette, bit_sprites.
12
+ """
13
+
14
+ from array import array
15
+
16
+ from bit_palette import (
17
+ ABYSS,
18
+ ABYSS_RGB,
19
+ AMBER,
20
+ AQUA_GLOW,
21
+ BRONZE,
22
+ COPPER,
23
+ CORAL,
24
+ CREAM,
25
+ CYAN,
26
+ DARK_MOSS,
27
+ DARK_WOOD,
28
+ DEEP_SLATE,
29
+ DEEP_SLATE_RGB,
30
+ DEEP_TEAL,
31
+ EMERALD,
32
+ FOREST,
33
+ GOLD,
34
+ ICE_CYAN,
35
+ LIME_GLOW,
36
+ MIST,
37
+ MOSS,
38
+ NEON_PINK,
39
+ NIGHT,
40
+ NIGHT_RGB,
41
+ OAK,
42
+ SILVER,
43
+ SLATE,
44
+ STEEL,
45
+ SUN_YELLOW,
46
+ TEAL,
47
+ VOID,
48
+ VOID_RGB,
49
+ WHITE,
50
+ copy_frame,
51
+ draw_pixel_panel,
52
+ icos,
53
+ isin,
54
+ make_fb,
55
+ paint_gradient,
56
+ rect_sources,
57
+ restore_area,
58
+ restore_rects,
59
+ restore_sources,
60
+ rng_int,
61
+ )
62
+
63
+ # Most moving rectangles a frame can have outside the fixed boxes.
64
+ MAX_MOVED = 40
65
+
66
+ # What the speech card's text needs: nothing, the newly typed letters, or a full redraw.
67
+ SPEECH_SAME = 0
68
+ SPEECH_ADD = 1
69
+ SPEECH_ALL = 2
70
+
71
+ # The clock's hundredths, made once so the readout needs no new string each frame.
72
+ TWO_DIGITS = tuple("%02d" % i for i in range(60))
73
+
74
+ # Action button definitions: (label, mood, emote_key, accent_color, speech_line).
75
+ ACTIONS = (
76
+ (
77
+ "1:GREET",
78
+ "love",
79
+ "heart",
80
+ NEON_PINK,
81
+ "Hi! I'm Bit -- your pixel-art desktop companion built on pygraphics!",
82
+ ),
83
+ (
84
+ "2:SING",
85
+ "sing",
86
+ "note",
87
+ AQUA_GLOW,
88
+ "Synthesizing polyphonic chiptune harmonies via real-time synthio!",
89
+ ),
90
+ (
91
+ "3:SCAN",
92
+ "scan",
93
+ "sparkle",
94
+ LIME_GLOW,
95
+ "Scanning Maker Lab... Temp 22.4C, Solar Array 99%, Inspiration HIGH!",
96
+ ),
97
+ (
98
+ "4:COFFEE",
99
+ "coffee",
100
+ "bolt",
101
+ GOLD,
102
+ "Espresso capacitor charged! Ready to build games, pets & HMIs!",
103
+ ),
104
+ (
105
+ "5:NAP",
106
+ "sleep",
107
+ "zzz",
108
+ CYAN,
109
+ "Entering cozy low-power standby... tap anywhere to wake me up!",
110
+ ),
111
+ )
112
+
113
+
114
+ class WorkshopAct:
115
+ """Render and animate Act 1: Bit's Solarpunk Maker Lab and Companion HUD."""
116
+
117
+ # *background* is a (bytearray, FrameBuffer) pair from make_fb at the panel's size to
118
+ # draw the static scene into; left as None, the act makes its own.
119
+ def __init__(self, layout, sprites, sound, background=None):
120
+ self.lay = layout
121
+ self.sprites = sprites
122
+ self.sound = sound
123
+ self.phase = 0
124
+ self.time_ms = 0
125
+ self.mood = "happy"
126
+ self._mood_label = (None, "") # (mood, its label), so the label is made once
127
+ self.mood_ms = 3500
128
+ self.active_btn = 0
129
+ self.bounce_vy = 0.0
130
+ self.bounce_y = 0.0
131
+ self.scan_sweep = 0.0
132
+
133
+ # Eye tracking target relative to Bit's center.
134
+ self.look_dx = 0
135
+ self.look_dy = 0
136
+
137
+ # Typewriter dialogue state (start fully revealed so first frame is complete).
138
+ self.speech_full = ACTIONS[0][4]
139
+ self.speech_chars = len(self.speech_full)
140
+
141
+ # Attract-mode timer for automatic companion demonstrations.
142
+ self.auto_ms = 1600
143
+ self.auto_index = 0
144
+
145
+ # Precompute key layout rectangles for 480x320 through 800x480.
146
+ w = layout.w
147
+ sy = layout.scene_y
148
+ sh = layout.scene_h
149
+ pad = layout.pad
150
+
151
+ self.win_rect = (pad, sy + pad, int(w * 0.27), int(sh * 0.56))
152
+ self.desk_y = sy + int(sh * 0.67)
153
+ self.btn_bar_y = sy + sh - (30 if layout.compact else 38)
154
+ self.btn_bar_h = 26 if layout.compact else 32
155
+
156
+ # Center pedestal where Bit hovers.
157
+ self.bit_home_x = int(w * 0.45)
158
+ self.bit_home_y = sy + int(sh * 0.40)
159
+
160
+ # Right-side holographic assistant cards.
161
+ rx = int(w * 0.62)
162
+ rw = w - rx - pad
163
+ ch1 = int(sh * 0.24)
164
+ ch2 = int(sh * 0.26)
165
+ ch3 = (self.btn_bar_y - sy) - ch1 - ch2 - pad * 4
166
+ self.card_chrono = (rx, sy + pad, rw, ch1)
167
+ self.card_voice = (rx, sy + pad * 2 + ch1, rw, ch2)
168
+ self.card_speech = (rx, sy + pad * 3 + ch1 + ch2, rw, max(44, ch3))
169
+
170
+ # Five interactive bottom buttons.
171
+ self.btn_rects = []
172
+ btn_count = len(ACTIONS)
173
+ avail_w = w - pad * 2
174
+ gap = 4 if layout.compact else 8
175
+ bw = (avail_w - gap * (btn_count - 1)) // btn_count
176
+ for i in range(btn_count):
177
+ bx = pad + i * (bw + gap)
178
+ self.btn_rects.append((bx, self.btn_bar_y, bw, self.btn_bar_h))
179
+
180
+ # Window starfield coordinates: [x, y, phase_offset].
181
+ wx, wy, ww, wh = self.win_rect
182
+ self.stars = []
183
+ for i in range(22):
184
+ sx = wx + 8 + ((i * 37 + 13) % max(8, ww - 16))
185
+ sy_star = wy + 8 + ((i * 53 + 7) % max(8, int(wh * 0.62)))
186
+ self.stars.append((sx, sy_star, i * 19))
187
+
188
+ # Floating emote particles: [x, y, vx, vy, emote_name, life], life counted in
189
+ # hundred-thousandths so that it boxes no floats.
190
+ self.particles = []
191
+
192
+ # Pre-render the static workshop interior into a background FrameBuffer.
193
+ self.bg_buf, self.bg_fb = background or make_fb(w, layout.h, VOID)
194
+ self.bg_view = memoryview(self.bg_buf)
195
+ self._build_static_background()
196
+
197
+ # Rising spore motes above the terrarium: [x, y, phase, color, path, step].
198
+ self.spores = []
199
+ self._init_spores()
200
+
201
+ # The clock's text up to its hundredths, made again once a second.
202
+ self._clock_s = -1
203
+ self._clock_text = ""
204
+ # The speech card's line breaks, as start and end letter indexes in speech_full.
205
+ cx, cy, cw, ch = self.card_speech
206
+ self._speech_cols = max(14, (cw - 16) // 8)
207
+ self._speech_line_h = 11 if layout.compact else 13
208
+ self._speech_lines = max(2, (ch - 22) // self._speech_line_h)
209
+ self._spans = array("h", bytes(4 * self._speech_lines))
210
+ self._spans_was = array("h", bytes(4 * self._speech_lines))
211
+ self._lines = 0
212
+ self._lines_was = 0
213
+ self._simple_text = None
214
+ self._simple = False
215
+
216
+ # The stars, the clock's hundredths and the telemetry bars change in place, and
217
+ # the spores, steam, pad ring and Bit move inside boxes that hold nothing else,
218
+ # so the background under all of them is prepared once.
219
+ cx, cy, cw, ch = self.card_chrono
220
+ bx0 = cx + cw - (64 if layout.compact else 84)
221
+ bar_max = 6 + ((2048 * (ch - 26)) >> 12)
222
+ step = 8 if layout.compact else 10
223
+ fixed = [(sx - 1, sy_star - 1, 3, 3) for sx, sy_star, _ in self.stars]
224
+ fixed.append((cx + 80, cy + 17, 16, 21))
225
+ fixed.append((bx0, cy + ch - 7 - bar_max, 6 * step + 6, bar_max))
226
+ self._cards = (
227
+ self.card_chrono,
228
+ self.card_voice,
229
+ self.card_speech,
230
+ (0, self.btn_bar_y, w, self.btn_bar_h),
231
+ )
232
+ self._spore_box = self._safe(self._spore_bounds())
233
+ self._steam_box = self._safe(self._bounds(self._put_steam))
234
+ self._pad_box = self._safe(self._bounds(self._put_pad))
235
+ self._bit_box = self._safe(self._bounds(self._put_bit))
236
+ for box in (self._spore_box, self._steam_box, self._pad_box, self._bit_box):
237
+ if box is not None:
238
+ fixed.append(box)
239
+ self.fixed_sources = rect_sources(self.bg_buf, fixed, w, layout.h)
240
+ # Where this frame's other moving parts are; it trades places with the array
241
+ # a PanelState keeps, so each display buffer remembers its own.
242
+ self._moved = array("h", bytes(8 * MAX_MOVED))
243
+
244
+ # Populate rising bioluminescent spore particles above the desk terrarium.
245
+ def _init_spores(self):
246
+ wx, _, _, _ = self.win_rect
247
+ tx = wx + 18
248
+ ty = self.desk_y - 16
249
+ self._spore_reset = self._spore_path(self.ter_y + 10)
250
+ for i in range(8):
251
+ path = self._spore_path(ty - (i * 11) % 64)
252
+ self.spores.append(
253
+ [
254
+ tx + (i * 7) % 36,
255
+ path[0],
256
+ i * 31,
257
+ LIME_GLOW if (i & 1) == 0 else AQUA_GLOW,
258
+ path,
259
+ 0,
260
+ ]
261
+ )
262
+
263
+ # The whole pixel rows a spore starting at y passes through, rising 0.45 a frame,
264
+ # until it drops below the terrarium again. The float steps are taken here once,
265
+ # so a frame only reads the next row.
266
+ def _spore_path(self, y):
267
+ limit = self.ter_y - 34
268
+ rows = [int(y)]
269
+ while True:
270
+ y -= 0.45
271
+ if y < limit:
272
+ return array("h", rows)
273
+ rows.append(int(y))
274
+
275
+ # The box every spore stays in, glow included.
276
+ def _spore_bounds(self):
277
+ sways = [(isin(p) * 3) >> 10 for p in range(256)]
278
+ xs = [sp[0] for sp in self.spores]
279
+ ys = list(self._spore_reset)
280
+ for sp in self.spores:
281
+ ys.extend(sp[4])
282
+ x0 = min(xs) + min(sways) - 7
283
+ y0 = min(ys) - 7
284
+ return (x0, y0, max(xs) + max(sways) + 9 - x0, max(ys) + 9 - y0)
285
+
286
+ # The box around every rectangle put(out, n, phase) makes, over all phases.
287
+ def _bounds(self, put):
288
+ out = array("h", bytes(8 * 4))
289
+ x0 = y0 = 32767
290
+ x1 = y1 = -32768
291
+ for phase in range(256):
292
+ n = put(out, 0, phase)
293
+ for i in range(0, n, 4):
294
+ x0 = min(x0, out[i])
295
+ y0 = min(y0, out[i + 1])
296
+ x1 = max(x1, out[i] + out[i + 2])
297
+ y1 = max(y1, out[i + 1] + out[i + 3])
298
+ return (x0, y0, x1 - x0, y1 - y0)
299
+
300
+ # box, if restoring it every frame can't erase anything drawn only now and then:
301
+ # the cards, the buttons or the stage bar. Otherwise None, and its parts are
302
+ # restored one rectangle at a time.
303
+ def _safe(self, box):
304
+ x, y, w, h = box
305
+ if y < self.lay.scene_y:
306
+ return None
307
+ for ox, oy, ow, oh in self._cards:
308
+ if x < ox + ow and ox < x + w and y < oy + oh and oy < y + h:
309
+ return None
310
+ return box
311
+
312
+ # Render the static workshop wall, observatory window, workbench, and holo frames.
313
+ def _build_static_background(self):
314
+ fb = self.bg_fb
315
+ lay = self.lay
316
+ w = lay.w
317
+ sy = lay.scene_y
318
+ sh = lay.scene_h
319
+
320
+ # Paint the workshop back wall gradient and subtle steel paneling seams.
321
+ paint_gradient(fb, 0, sy, w, self.desk_y - sy, NIGHT_RGB, DEEP_SLATE_RGB, step=3)
322
+ seam_step = 48 if lay.compact else 64
323
+ for x in range(seam_step, w, seam_step):
324
+ fb.vline(x, sy, self.desk_y - sy, NIGHT)
325
+ fb.pixel(x + 1, sy + 8, STEEL)
326
+ fb.pixel(x + 1, self.desk_y - 10, STEEL)
327
+
328
+ # Draw the arched night-sky observatory window on the left wall.
329
+ wx, wy, ww, wh = self.win_rect
330
+ draw_pixel_panel(fb, wx, wy, ww, wh, VOID, STEEL, SILVER, ABYSS)
331
+ paint_gradient(fb, wx + 3, wy + 3, ww - 6, wh - 6, VOID_RGB, ABYSS_RGB, step=2)
332
+
333
+ # Draw twin solarpunk moons inside the window.
334
+ moon_x = wx + ww - (26 if lay.compact else 38)
335
+ moon_y = wy + (22 if lay.compact else 30)
336
+ moon_r = 10 if lay.compact else 14
337
+ fb.ellipse(moon_x, moon_y, moon_r, moon_r, SUN_YELLOW, True)
338
+ fb.ellipse(moon_x - 4, moon_y - 3, moon_r - 2, moon_r - 2, VOID, True)
339
+ fb.ellipse(moon_x - moon_r - 14, moon_y + 12, 4, 4, AQUA_GLOW, True)
340
+
341
+ # Draw silhouetted solarpunk botanical domes and spires along the horizon.
342
+ hor_y = wy + wh - (26 if lay.compact else 36)
343
+ fb.fill_rect(wx + 3, hor_y, ww - 6, wy + wh - 3 - hor_y, NIGHT)
344
+ dome_x1 = wx + int(ww * 0.28)
345
+ dome_x2 = wx + int(ww * 0.65)
346
+ fb.ellipse(dome_x1, hor_y, int(ww * 0.18), 14, DEEP_TEAL, True)
347
+ fb.ellipse(dome_x2, hor_y + 2, int(ww * 0.14), 10, DARK_MOSS, True)
348
+ fb.vline(dome_x1, hor_y - 22, 22, CYAN)
349
+ fb.pixel(dome_x1, hor_y - 23, ICE_CYAN)
350
+ # Window muntin bars and brass sill.
351
+ fb.hline(wx + 3, wy + int(wh * 0.45), ww - 6, SLATE)
352
+ fb.vline(wx + ww // 2, wy + 3, wh - 6, SLATE)
353
+ fb.fill_rect(wx - 2, wy + wh - 2, ww + 4, 5, BRONZE)
354
+ fb.hline(wx - 2, wy + wh - 2, ww + 4, GOLD)
355
+
356
+ # Draw a hanging solarpunk bookshelf and overhead pendant lamp above Bit.
357
+ shelf_x = wx + ww + (16 if lay.compact else 26)
358
+ shelf_w = self.card_chrono[0] - shelf_x - (16 if lay.compact else 26)
359
+ shelf_y = wy + (26 if lay.compact else 38)
360
+ if shelf_w > 60:
361
+ # Brass ceiling suspension rods and oak shelf plank.
362
+ fb.vline(shelf_x + 10, sy, shelf_y - sy, BRONZE)
363
+ fb.vline(shelf_x + shelf_w - 11, sy, shelf_y - sy, BRONZE)
364
+ fb.fill_rect(shelf_x, shelf_y, shelf_w, 6, OAK)
365
+ fb.hline(shelf_x, shelf_y, shelf_w, GOLD)
366
+ fb.hline(shelf_x, shelf_y + 5, shelf_w, DARK_WOOD)
367
+ # Pixel-art technical manuals on the shelf.
368
+ bx = shelf_x + 14
369
+ for b_col, b_h in ((CORAL, 18), (CYAN, 22), (GOLD, 16), (EMERALD, 20), (NEON_PINK, 17)):
370
+ if bx + 10 < shelf_x + shelf_w - 36:
371
+ fb.fill_rect(bx, shelf_y - b_h, 7, b_h, b_col)
372
+ fb.hline(bx + 1, shelf_y - b_h + 3, 5, WHITE)
373
+ bx += 9
374
+ # Glowing plasma specimen jar on the right end of the shelf.
375
+ jx = shelf_x + shelf_w - 28
376
+ fb.fill_rect(jx, shelf_y - 18, 12, 18, DEEP_TEAL)
377
+ fb.rect(jx, shelf_y - 18, 12, 18, SILVER)
378
+ fb.fill_rect(jx + 3, shelf_y - 12, 6, 9, AQUA_GLOW)
379
+ fb.fill_rect(jx + 2, shelf_y - 20, 8, 3, GOLD)
380
+ # Trailing bioluminescent ivy vine hanging from the left shelf edge.
381
+ for vy_step in range(0, 28, 5):
382
+ fb.fill_rect(shelf_x + 3 + (vy_step & 1) * 2, shelf_y + 6 + vy_step, 4, 4, EMERALD)
383
+ fb.pixel(shelf_x + 4, shelf_y + 7 + vy_step, LIME_GLOW)
384
+
385
+ # Draw the warm oak and brushed-copper maker workbench surface.
386
+ desk_h = (sy + sh) - self.desk_y
387
+ fb.fill_rect(0, self.desk_y, w, desk_h, DARK_WOOD)
388
+ fb.fill_rect(0, self.desk_y, w, 10, OAK)
389
+ fb.hline(0, self.desk_y, w, GOLD)
390
+ fb.hline(0, self.desk_y + 1, w, AMBER)
391
+ fb.hline(0, self.desk_y + 9, w, BRONZE)
392
+
393
+ # Draw maker tool drawers and an illuminated power bus on the workbench apron.
394
+ apron_y = self.desk_y + (16 if lay.compact else 22)
395
+ apron_h = self.btn_bar_y - apron_y - (6 if lay.compact else 10)
396
+ if apron_h >= 24:
397
+ dw = int(ww * 0.92)
398
+ draw_pixel_panel(fb, wx, apron_y, dw, apron_h, OAK, BRONZE, GOLD)
399
+ mid_ay = apron_y + (apron_h >> 1)
400
+ fb.hline(wx + 4, mid_ay, dw - 8, DARK_WOOD)
401
+ fb.fill_rect(wx + (dw >> 1) - 12, apron_y + 6, 24, 4, GOLD)
402
+ fb.fill_rect(wx + (dw >> 1) - 12, mid_ay + 6, 24, 4, GOLD)
403
+
404
+ bus_x = self.bit_home_x - (56 if lay.compact else 78)
405
+ bus_w = 112 if lay.compact else 156
406
+ draw_pixel_panel(fb, bus_x, apron_y, bus_w, apron_h, ABYSS, TEAL, CYAN)
407
+ bus_lbl = "LAB BUS" if lay.compact else "LAB POWER BUS"
408
+ fb.text(bus_lbl, bus_x + ((bus_w - len(bus_lbl) * 8) >> 1), apron_y + 6, AQUA_GLOW)
409
+ for i in range(6):
410
+ lx = bus_x + 10 + i * (16 if lay.compact else 22)
411
+ ly = apron_y + apron_h - (12 if lay.compact else 16)
412
+ lcol = LIME_GLOW if i < 4 else GOLD
413
+ fb.fill_rect(lx, ly, 10 if lay.compact else 14, 6, lcol)
414
+
415
+ # Draw the holographic emitter pad on the workbench beneath Bit.
416
+ pad_rx = 44 if lay.compact else 64
417
+ pad_ry = 8 if lay.compact else 11
418
+ pad_cy = self.desk_y + (4 if lay.compact else 5)
419
+ fb.ellipse(self.bit_home_x, pad_cy, pad_rx + 6, pad_ry + 2, BRONZE, True)
420
+ fb.ellipse(self.bit_home_x, pad_cy, pad_rx, pad_ry, DEEP_TEAL, True)
421
+ fb.ellipse(self.bit_home_x, pad_cy, pad_rx, pad_ry, CYAN, False)
422
+ fb.ellipse(self.bit_home_x, pad_cy, pad_rx >> 1, max(2, pad_ry >> 1), AQUA_GLOW, False)
423
+
424
+ # Blit the static terrarium capsule and copper coffee mug onto the left desk.
425
+ ter = self.sprites.terrarium
426
+ mug = self.sprites.mug
427
+ self.ter_x = wx + 12
428
+ self.ter_y = self.desk_y - ter.h + 4
429
+ ter.draw(fb, self.ter_x, self.ter_y)
430
+
431
+ self.mug_x = self.ter_x + ter.w + (12 if lay.compact else 20)
432
+ self.mug_y = self.desk_y - mug.h + 4
433
+ mug.draw(fb, self.mug_x, self.mug_y)
434
+
435
+ # Draw the three right-hand holographic HUD card frames.
436
+ for rect, border in (
437
+ (self.card_chrono, CYAN),
438
+ (self.card_voice, EMERALD),
439
+ (self.card_speech, AMBER),
440
+ ):
441
+ cx, cy, cw, ch = rect
442
+ draw_pixel_panel(fb, cx, cy, cw, ch, ABYSS, border, ICE_CYAN, VOID)
443
+
444
+ # Trigger one of the 5 companion actions by index (0..4).
445
+ def trigger_action(self, idx):
446
+ idx = idx % len(ACTIONS)
447
+ self.active_btn = idx
448
+ _, mood, emote_key, _, speech = ACTIONS[idx]
449
+ self.mood = mood
450
+ self.mood_ms = 4000
451
+ self.speech_full = speech
452
+ self.speech_chars = 0
453
+ self.bounce_vy = -5.5
454
+ if mood == "scan":
455
+ self.scan_sweep = 1.0
456
+ self.sound.chirp(mood)
457
+ self._spawn_emotes(emote_key, 4)
458
+
459
+ # Spawn floating pixel-art reaction emotes around Bit.
460
+ def _spawn_emotes(self, emote_key, count=3):
461
+ for i in range(count):
462
+ ox = rng_int(-42, 42)
463
+ oy = rng_int(-28, 10)
464
+ vx = (ox / 28.0) + (i - 1) * 0.35
465
+ vy = -1.2 - rng_int(0, 10) * 0.1
466
+ self.particles.append(
467
+ [
468
+ float(self.bit_home_x + ox),
469
+ float(self.bit_home_y - 18 + oy),
470
+ vx,
471
+ vy,
472
+ emote_key,
473
+ 100000,
474
+ ]
475
+ )
476
+ if len(self.particles) > 14:
477
+ self.particles = self.particles[-14:]
478
+
479
+ # Handle pointer down inside the Workshop scene; return True if consumed.
480
+ def on_pointer_down(self, x, y):
481
+ self.update_pointer_look(x, y)
482
+ # Check the 5 bottom action buttons first.
483
+ for i, (bx, by, bw, bh) in enumerate(self.btn_rects):
484
+ if bx <= x < bx + bw and by <= y < by + bh:
485
+ self.trigger_action(i)
486
+ return True
487
+ # Check if the user tapped directly on Bit to pet/boop the robot.
488
+ body = self.sprites.bit_body[0]
489
+ if (
490
+ abs(x - self.bit_home_x) <= (body.w >> 1) + 12
491
+ and abs(y - self.bit_home_y) <= (body.h >> 1) + 12
492
+ ):
493
+ next_act = (self.active_btn + 1) % len(ACTIONS)
494
+ self.trigger_action(next_act)
495
+ return True
496
+ # Check if the user tapped the coffee mug or terrarium.
497
+ if self.mug_x <= x <= self.mug_x + self.sprites.mug.w and y >= self.mug_y - 10:
498
+ self.trigger_action(3)
499
+ return True
500
+ if self.ter_x <= x <= self.ter_x + self.sprites.terrarium.w and y >= self.ter_y - 10:
501
+ self.trigger_action(2)
502
+ return True
503
+ return False
504
+
505
+ # Aim Bit's CRT eyes toward the latest pointer coordinate.
506
+ def update_pointer_look(self, x, y):
507
+ dx = (x - self.bit_home_x) // 28
508
+ dy = (y - self.bit_home_y) // 32
509
+ self.look_dx = max(-6, min(6, dx))
510
+ self.look_dy = max(-4, min(4, dy))
511
+
512
+ # Advance physics, particles, typewriter text, and attract-mode choreography.
513
+ def update(self, dt, auto_mode=True):
514
+ self.phase = (self.phase + 4) & 0xFF
515
+ # The timers count whole ms, so a frame boxes no floats for them.
516
+ dt_ms = round(dt * 1000.0)
517
+ self.time_ms += dt_ms
518
+
519
+ # Advance typewriter speech reveal.
520
+ if self.speech_chars < len(self.speech_full):
521
+ self.speech_chars = min(
522
+ len(self.speech_full), self.speech_chars + max(1, dt_ms * 42 // 1000)
523
+ )
524
+
525
+ # Spring physics for Bit's excited vertical bounce.
526
+ if self.bounce_y != 0.0 or self.bounce_vy != 0.0:
527
+ self.bounce_y += self.bounce_vy
528
+ self.bounce_vy += 0.48
529
+ if self.bounce_y >= 0.0 and self.bounce_vy > 0.0:
530
+ self.bounce_y = 0.0
531
+ self.bounce_vy = 0.0
532
+
533
+ if self.scan_sweep > 0.0:
534
+ self.scan_sweep = max(0.0, self.scan_sweep - dt * 0.85)
535
+
536
+ # Decay temporary reaction moods back to happy/neutral.
537
+ if self.mood_ms > 0:
538
+ self.mood_ms -= dt_ms
539
+ if self.mood_ms <= 0 and self.mood != "sleep":
540
+ self.mood = "happy"
541
+
542
+ # Attract-mode choreography: cycle through Bit's reactions automatically.
543
+ if auto_mode:
544
+ self.look_dx = (isin(self.phase >> 1) * 5) >> 10
545
+ self.look_dy = (icos(self.phase >> 1) * 3) >> 10
546
+ self.auto_ms -= dt_ms
547
+ if self.auto_ms <= 0:
548
+ self.auto_ms = 3400
549
+ self.auto_index = (self.auto_index + 1) % len(ACTIONS)
550
+ self.trigger_action(self.auto_index)
551
+
552
+ # Update floating emote particles.
553
+ i = len(self.particles) - 1
554
+ while i >= 0:
555
+ p = self.particles[i]
556
+ p[0] += p[2]
557
+ p[1] += p[3]
558
+ p[5] -= dt_ms * 65
559
+ if p[5] <= 0:
560
+ self.particles.pop(i)
561
+ i -= 1
562
+
563
+ # Update rising terrarium spores.
564
+ for sp in self.spores:
565
+ step = sp[5] + 1
566
+ if step >= len(sp[4]):
567
+ sp[4] = self._spore_reset
568
+ step = 0
569
+ sp[5] = step
570
+ sp[1] = sp[4][step]
571
+ sp[2] = (sp[2] + 5) & 0xFF
572
+
573
+ # Render the complete Workshop scene into the target FrameBuffer.
574
+ def draw(self, fb, draw_obj=None, st=None):
575
+ # Copy the pre-rendered workshop interior in one fast C-level blit.
576
+ copy_frame(fb, self.bg_fb)
577
+ if st is not None:
578
+ st.sig = {}
579
+ self._draw_scene(fb, st, True, SPEECH_ALL, True, True)
580
+ if st is not None:
581
+ if st.moved is None:
582
+ st.moved = array("h", bytes(8 * MAX_MOVED))
583
+ st.moved_n = self._moving(st.moved)
584
+ self._remember(st.sig)
585
+
586
+ # Record in sig what the speech card, buttons and mood card show now.
587
+ def _remember(self, sig):
588
+ sig["speech_text"] = self.speech_full
589
+ sig["speech_chars"] = self.speech_chars
590
+ sig["buttons"] = self.active_btn
591
+ sig["mood"] = self.mood
592
+
593
+ # Repaint only what moved since this display buffer was last drawn.
594
+ def draw_changes(self, fb, draw_obj, st):
595
+ cur = self._moved
596
+ n = self._moving(cur)
597
+ w = self.lay.w
598
+ h = self.lay.h
599
+ view = self.bg_view
600
+ sig = st.sig
601
+ buttons = sig.get("buttons") != self.active_btn
602
+ mood = sig.get("mood") != self.mood
603
+ # An emote can drift over a card, and restoring where it was erases what the card
604
+ # draws only now and then, so a card it touched is drawn again.
605
+ touched = self._touched(cur, n) | self._touched(st.moved, st.moved_n)
606
+ buttons = buttons or touched & 8
607
+ if sig.get("speech_text") != self.speech_full or touched & 4:
608
+ speech = SPEECH_ALL
609
+ elif sig.get("speech_chars") != self.speech_chars:
610
+ speech = SPEECH_ADD if self._speech_grew(sig.get("speech_chars")) else SPEECH_ALL
611
+ else:
612
+ speech = SPEECH_SAME
613
+ restore_sources(fb, self.fixed_sources)
614
+ for i in range(0, n, 4):
615
+ restore_area(fb, view, cur[i], cur[i + 1], cur[i + 2], cur[i + 3], w, h)
616
+ # What this buffer showed last time, unless it was just restored above.
617
+ was = st.moved
618
+ for j in range(0, st.moved_n, 4):
619
+ x = was[j]
620
+ y = was[j + 1]
621
+ rw = was[j + 2]
622
+ rh = was[j + 3]
623
+ for i in range(0, n, 4):
624
+ if cur[i] == x and cur[i + 1] == y and cur[i + 2] == rw and cur[i + 3] == rh:
625
+ break
626
+ else:
627
+ restore_area(fb, view, x, y, rw, rh, w, h)
628
+ if speech == SPEECH_ALL:
629
+ cx, cy, cw, ch = self.card_speech
630
+ restore_area(fb, view, cx + 6, cy + 18, cw - 12, ch - 18, w, h)
631
+ if buttons:
632
+ restore_rects(fb, view, self.btn_rects, w, h)
633
+ if mood:
634
+ cx, cy, cw, ch = self.card_voice
635
+ restore_area(fb, view, cx + 4, cy + 4, cw - 8, 12, w, h)
636
+ # The clock's text up to its hundredths, when the second changed (16).
637
+ if sig.get("clock") != self.time_ms // 1000:
638
+ cx, cy, cw, ch = self.card_chrono
639
+ restore_area(fb, view, cx + 4, cy + 17, cw - 8, 21, w, h)
640
+ touched |= 16
641
+ self._draw_scene(fb, st, False, speech, buttons, mood, touched)
642
+ st.moved = cur
643
+ st.moved_n = n
644
+ self._moved = was
645
+ self._remember(sig)
646
+
647
+ # Which of the chrono card (1), the voice card (2), the speech card (4) and the
648
+ # button bar (8) the n numbers of rectangles in rects overlap, as bits.
649
+ def _touched(self, rects, n):
650
+ bits = 0
651
+ for i in range(0, n, 4):
652
+ x = rects[i]
653
+ y = rects[i + 1]
654
+ x1 = x + rects[i + 2]
655
+ y1 = y + rects[i + 3]
656
+ bit = 1
657
+ for ox, oy, ow, oh in self._cards:
658
+ if x < ox + ow and ox < x1 and y < oy + oh and oy < y1:
659
+ bits |= bit
660
+ bit <<= 1
661
+ return bits
662
+
663
+ # Put the rectangle x, y, w, h in out at n and return where the next one goes.
664
+ @staticmethod
665
+ def _put(out, n, x, y, w, h):
666
+ out[n] = x
667
+ out[n + 1] = y
668
+ out[n + 2] = w
669
+ out[n + 3] = h
670
+ return n + 4
671
+
672
+ # Put in out the rectangles of what the scene paints over the background now, apart
673
+ # from the fixed sources and the boxes, and return how many numbers that took.
674
+ def _moving(self, out):
675
+ put = self._put
676
+ n = 0
677
+ wx, wy, ww, wh = self.win_rect
678
+ meteor_tick = (self.phase >> 1) & 0x7F
679
+ if meteor_tick < 24:
680
+ mx = wx + 14 + meteor_tick * ((ww - 36) // 24)
681
+ my = wy + 10 + meteor_tick * 2
682
+ n = put(out, n, mx - 8, my - 4, 9, 5)
683
+ if self._spore_box is None:
684
+ for sp in self.spores:
685
+ sx = sp[0] + ((isin(sp[2]) * 3) >> 10)
686
+ n = put(out, n, sx - 7, sp[1] - 7, 16, 16)
687
+ if self._steam_box is None:
688
+ n = self._put_steam(out, n, self.phase)
689
+ if self._pad_box is None:
690
+ n = self._put_pad(out, n, self.phase)
691
+ # Bit stays in his box unless a bounce lifts him out of it.
692
+ m = self._put_bit(out, n, self.phase, int(self.bounce_y))
693
+ box = self._bit_box
694
+ if (
695
+ box is None
696
+ or out[n] < box[0]
697
+ or out[n + 1] < box[1]
698
+ or out[n] + out[n + 2] > box[0] + box[2]
699
+ or out[n + 1] + out[n + 3] > box[1] + box[3]
700
+ ):
701
+ n = m
702
+ if self.scan_sweep > 0.0:
703
+ scan_y = int(
704
+ self.lay.scene_y
705
+ + 24
706
+ + (1.0 - self.scan_sweep) * (self.desk_y - self.lay.scene_y - 16)
707
+ )
708
+ beam_x0 = wx + 8
709
+ n = put(out, n, beam_x0, scan_y, self.card_chrono[0] - beam_x0 - 8, 2)
710
+ for p in self.particles:
711
+ em = self.sprites.emotes.get(p[4])
712
+ if em is not None and n < len(out):
713
+ n = put(out, n, int(p[0]) - (em.w >> 1), int(p[1]) - (em.h >> 1), em.w, em.h)
714
+ return n
715
+
716
+ # Put the three steam puffs' rectangles at phase in out at n.
717
+ def _put_steam(self, out, n, phase):
718
+ for i in range(3):
719
+ p = (phase + i * 85) & 0xFF
720
+ rise = (p * 26) >> 8
721
+ sway = (isin((p * 2) & 0xFF) * 4) >> 10
722
+ st_x = self.mug_x + (self.sprites.mug.w >> 1) - 2 + sway
723
+ n = self._put(out, n, st_x, self.mug_y - 4 - rise, 3, 2)
724
+ return n
725
+
726
+ # Put the pad ring's rectangle at phase in out at n.
727
+ def _put_pad(self, out, n, phase):
728
+ pad_cy = self.desk_y + (4 if self.lay.compact else 5)
729
+ pulse_r = (34 if self.lay.compact else 50) + ((isin(phase * 2) * 6) >> 10)
730
+ return self._put(out, n, self.bit_home_x - pulse_r - 1, pad_cy - 7, pulse_r * 2 + 3, 15)
731
+
732
+ # Put the rectangle of Bit, his thruster flame and the glows around them at phase,
733
+ # bounced by bounce pixels, in out at n.
734
+ def _put_bit(self, out, n, phase, bounce=0):
735
+ bob = ((isin(phase) * (5 if self.lay.compact else 8)) >> 10) + bounce
736
+ bit_cy = self.bit_home_y + bob
737
+ frame = (phase >> 4) & 1
738
+ body = self.sprites.bit_body[frame & 1]
739
+ flame = self.sprites.flames[frame % len(self.sprites.flames)]
740
+ px = self.sprites.pet_px
741
+ top = bit_cy - (body.h >> 1)
742
+ flame_cy = top + body.h - px + (flame.h >> 1)
743
+ half = max(body.w >> 1, 12) + px * 2 + 2
744
+ bottom = max(top + body.h - px + flame.h, flame_cy + 12, top + body.h) + 2
745
+ return self._put(out, n, self.bit_home_x - half, top - 10, half * 2, bottom - top + 10)
746
+
747
+ # Paint everything that moves. The speech card's text as speech asks, the buttons
748
+ # and the mood label only when asked, and the cards' fixed labels only when full.
749
+ def _draw_scene(self, fb, st, full, speech, buttons, mood, touched=0):
750
+ # Animate twinkling stars and a periodic shooting star in the window.
751
+ wx, wy, ww, wh = self.win_rect
752
+ for sx, sy_star, offs in self.stars:
753
+ b = isin((self.phase + offs) & 0xFF)
754
+ col = WHITE if b > 600 else (AQUA_GLOW if b > 0 else STEEL)
755
+ fb.pixel(sx, sy_star, col)
756
+ if b > 780:
757
+ fb.hline(sx - 1, sy_star, 3, ICE_CYAN)
758
+ fb.vline(sx, sy_star - 1, 3, ICE_CYAN)
759
+
760
+ meteor_tick = (self.phase >> 1) & 0x7F
761
+ if meteor_tick < 24:
762
+ mx = wx + 14 + meteor_tick * ((ww - 36) // 24)
763
+ my = wy + 10 + meteor_tick * 2
764
+ fb.line(mx - 8, my - 4, mx, my, ICE_CYAN)
765
+ fb.pixel(mx, my, WHITE)
766
+
767
+ # Draw rising bioluminescent spores above the terrarium with soft alpha glow.
768
+ for sp in self.spores:
769
+ sx = int(sp[0]) + ((isin(sp[2]) * 3) >> 10)
770
+ sy_sp = int(sp[1])
771
+ self.sprites.draw_glow(fb, sx + 1, sy_sp + 1, sp[3], large=False)
772
+ fb.fill_rect(sx, sy_sp, 2, 2, WHITE)
773
+
774
+ # Draw curling warm steam puffs above the copper coffee mug.
775
+ for i in range(3):
776
+ p = (self.phase + i * 85) & 0xFF
777
+ rise = (p * 26) >> 8
778
+ sway = (isin((p * 2) & 0xFF) * 4) >> 10
779
+ st_x = self.mug_x + (self.sprites.mug.w >> 1) - 2 + sway
780
+ st_y = self.mug_y - 4 - rise
781
+ col = WHITE if rise < 12 else MIST
782
+ fb.fill_rect(st_x, st_y, 3, 2, col)
783
+
784
+ # Draw pulsing holographic rings and vertical projection beams under Bit.
785
+ pad_cy = self.desk_y + (4 if self.lay.compact else 5)
786
+ pulse_r = (34 if self.lay.compact else 50) + ((isin(self.phase * 2) * 6) >> 10)
787
+ fb.ellipse(self.bit_home_x, pad_cy, pulse_r, 6, AQUA_GLOW, False)
788
+
789
+ # Compute Bit's vertical hover bob + bounce offset.
790
+ bob = ((isin(self.phase) * (5 if self.lay.compact else 8)) >> 10) + int(self.bounce_y)
791
+ bit_cy = self.bit_home_y + bob
792
+
793
+ # Choose current expression (with natural periodic blink in happy/neutral mood).
794
+ draw_mood = self.mood
795
+ if draw_mood in ("happy", "neutral") and ((self.phase & 0x7F) > 120):
796
+ draw_mood = "blink"
797
+
798
+ self.sprites.draw_workshop_bit(
799
+ fb,
800
+ self.bit_home_x,
801
+ bit_cy,
802
+ (self.phase >> 4) & 1,
803
+ draw_mood,
804
+ self.look_dx,
805
+ self.look_dy,
806
+ glow_col=AQUA_GLOW,
807
+ )
808
+
809
+ # If SCAN is active, sweep a glowing laser visor beam across the scene.
810
+ if self.scan_sweep > 0.0:
811
+ scan_y = int(
812
+ self.lay.scene_y
813
+ + 24
814
+ + (1.0 - self.scan_sweep) * (self.desk_y - self.lay.scene_y - 16)
815
+ )
816
+ beam_x0 = wx + 8
817
+ beam_w = self.card_chrono[0] - beam_x0 - 8
818
+ fb.hline(beam_x0, scan_y, beam_w, LIME_GLOW)
819
+ fb.hline(beam_x0, scan_y + 1, beam_w, EMERALD)
820
+
821
+ # Draw floating pixel-art reaction emotes.
822
+ for p in self.particles:
823
+ em = self.sprites.emotes.get(p[4])
824
+ if em is not None:
825
+ em.draw_center(fb, int(p[0]), int(p[1]))
826
+
827
+ # Draw the three live right-side holographic companion cards.
828
+ self._draw_chrono_card(fb, st, full, touched & 17)
829
+ self._draw_voice_card(fb, full, mood or touched & 2)
830
+ if full or touched & 4:
831
+ fb.text("BIT // ASSISTANT LOG", self.card_speech[0] + 8, self.card_speech[1] + 6, GOLD)
832
+ if speech == SPEECH_ADD:
833
+ self._type_speech(fb)
834
+ elif speech == SPEECH_ALL:
835
+ self._draw_speech_card(fb)
836
+ if buttons:
837
+ self._draw_action_buttons(fb)
838
+
839
+ # Render Card 1: Lab Chrono, Solar Charge, and 7-bar Eco Telemetry.
840
+ # The clock's text up to its hundredths changes once a second, so it's drawn then,
841
+ # and the hundredths, whose background is a fixed source, every frame. touched
842
+ # has bit 1 when something moved over the card and bit 16 when the second changed.
843
+ def _draw_chrono_card(self, fb, st, full, touched=0):
844
+ cx, cy, cw, ch = self.card_chrono
845
+ compact = self.lay.compact
846
+ if full or touched & 1:
847
+ hdr = "LAB // CHRONO" if compact else "SOLARPUNK LAB // CHRONO"
848
+ fb.text(hdr, cx + 8, cy + 6, AQUA_GLOW)
849
+ secs = self.time_ms // 1000
850
+ if full or touched:
851
+ if secs != self._clock_s:
852
+ self._clock_s = secs
853
+ self._clock_text = "08:%02d:%02d." % (24 + ((secs // 60) % 35), secs % 60)
854
+ if compact:
855
+ fb.text(self._clock_text, cx + 8, cy + 18, WHITE)
856
+ else:
857
+ fb.text16(self._clock_text, cx + 8, cy + 20, WHITE)
858
+ if st is not None:
859
+ st.sig["clock"] = secs
860
+ frames = TWO_DIGITS[(self.phase >> 2) % 60]
861
+ if compact:
862
+ fb.text(frames, cx + 80, cy + 18, WHITE)
863
+ else:
864
+ fb.text16(frames, cx + 80, cy + 20, WHITE)
865
+
866
+ # Solar array charge indicator + animated mini bar chart on the right.
867
+ bar_y = cy + ch - (14 if compact else 18)
868
+ if full or touched & 1:
869
+ fb.text("SOLAR 99%", cx + 8, bar_y, LIME_GLOW)
870
+ bx0 = cx + cw - (64 if self.lay.compact else 84)
871
+ for i in range(7):
872
+ bh = 6 + (((isin(self.phase + i * 29) + 1024) * (ch - 26)) >> 12)
873
+ bx = bx0 + i * (8 if self.lay.compact else 10)
874
+ col = LIME_GLOW if (i & 1) == 0 else CYAN
875
+ fb.fill_rect(bx, cy + ch - 7 - bh, 5 if self.lay.compact else 6, bh, col)
876
+
877
+ # Render Card 2: Live Voice Synthesizer Waveform and Biometric Status.
878
+ # The header and the mood label only when full or the mood changed, as the mood's
879
+ # repaint is the only thing that paints over them.
880
+ def _draw_voice_card(self, fb, full, mood):
881
+ cx, cy, cw, ch = self.card_voice
882
+ if full or mood:
883
+ hdr = "VOICE SYNTH" if self.lay.compact else "VOICE SYNTH & MOOD"
884
+ fb.text(hdr, cx + 8, cy + 6, LIME_GLOW)
885
+ if full or mood:
886
+ if self._mood_label[0] != self.mood:
887
+ label = self.mood.upper() if self.lay.compact else f"MOOD:{self.mood.upper()}"
888
+ self._mood_label = (self.mood, label)
889
+ mood_label = self._mood_label[1]
890
+ fb.text(mood_label, cx + cw - len(mood_label) * 8 - 8, cy + 6, GOLD)
891
+
892
+ # Draw real-time voice waveform inside a dark scope well.
893
+ wx = cx + 8
894
+ wy = cy + 18
895
+ ww = cw - 16
896
+ wh = ch - 26
897
+ fb.fill_rect(wx, wy, ww, wh, VOID)
898
+ fb.rect(wx, wy, ww, wh, DEEP_TEAL)
899
+ mid_y = wy + (wh >> 1)
900
+ fb.hline(wx + 1, mid_y, ww - 2, DEEP_SLATE)
901
+
902
+ samples = self.sound.scope_samples
903
+ n = len(samples)
904
+ prev_x = wx + 2
905
+ prev_y = mid_y
906
+ span_w = max(1, ww - 4)
907
+ half_h = max(4, (wh >> 1) - 3)
908
+ for i in range(n):
909
+ sx = wx + 2 + (i * span_w) // (n - 1)
910
+ sy_pt = mid_y - ((samples[i] * half_h) // 100)
911
+ if i > 0:
912
+ fb.line(prev_x, prev_y, sx, sy_pt, AQUA_GLOW)
913
+ prev_x = sx
914
+ prev_y = sy_pt
915
+
916
+ # Render Card 3: Companion Typewriter Speech Bubble with Word Wrapping.
917
+ def _draw_speech_card(self, fb):
918
+ cx, cy, cw, ch = self.card_speech
919
+ line_h = self._speech_line_h
920
+ if self._wraps_simply():
921
+ full = self.speech_full
922
+ spans = self._spans
923
+ for i in range(self._wrap(self.speech_chars, spans)):
924
+ line = full[spans[2 * i] : spans[2 * i + 1]]
925
+ fb.text(line, cx + 8, cy + 20 + i * line_h, CREAM)
926
+ return
927
+ visible = self.speech_full[: self.speech_chars]
928
+ max_cols = self._speech_cols
929
+ words = visible.split(" ")
930
+ lines = []
931
+ cur = ""
932
+ for w in words:
933
+ cand = w if not cur else cur + " " + w
934
+ if len(cand) <= max_cols:
935
+ cur = cand
936
+ else:
937
+ if cur:
938
+ lines.append(cur)
939
+ cur = w[:max_cols]
940
+ if cur:
941
+ lines.append(cur)
942
+
943
+ for i, line in enumerate(lines[: self._speech_lines]):
944
+ fb.text(line, cx + 8, cy + 20 + i * line_h, CREAM)
945
+
946
+ # True when no word of the speech is wider than the card. Then each line is one
947
+ # stretch of speech_full, which _wrap finds without making strings.
948
+ def _wraps_simply(self):
949
+ text = self.speech_full
950
+ if self._simple_text is not text:
951
+ self._simple_text = text
952
+ self._simple = all(len(w) <= self._speech_cols for w in text.split(" "))
953
+ return self._simple
954
+
955
+ # Wrap the first n letters of the speech as the card shows them, a line breaking
956
+ # before the word that won't fit. Line i is speech_full[spans[2i]:spans[2i + 1]].
957
+ # Returns how many lines the card shows.
958
+ def _wrap(self, n, spans):
959
+ full = self.speech_full
960
+ cols = self._speech_cols
961
+ most = self._speech_lines
962
+ count = 0
963
+ start = end = 0
964
+ word = 0
965
+ while True:
966
+ space = full.find(" ", word, n)
967
+ word_end = n if space < 0 else space
968
+ if end == start:
969
+ start = word
970
+ end = word_end
971
+ elif word_end - start <= cols:
972
+ end = word_end
973
+ else:
974
+ if count < most:
975
+ spans[2 * count] = start
976
+ spans[2 * count + 1] = end
977
+ count += 1
978
+ start = word
979
+ end = word_end
980
+ if space < 0:
981
+ break
982
+ word = space + 1
983
+ if end > start:
984
+ if count < most:
985
+ spans[2 * count] = start
986
+ spans[2 * count + 1] = end
987
+ count += 1
988
+ return min(count, most)
989
+
990
+ # True when the speech, typed on from was_n letters, only adds letters to the ends
991
+ # of the lines this buffer shows, so _type_speech can draw just those.
992
+ def _speech_grew(self, was_n):
993
+ if was_n is None or was_n > self.speech_chars or not self._wraps_simply():
994
+ return False
995
+ was = self._spans_was
996
+ now = self._spans
997
+ self._lines_was = self._wrap(was_n, was)
998
+ self._lines = self._wrap(self.speech_chars, now)
999
+ if self._lines_was > self._lines:
1000
+ return False
1001
+ for i in range(self._lines_was):
1002
+ if was[2 * i] != now[2 * i] or was[2 * i + 1] > now[2 * i + 1]:
1003
+ return False
1004
+ return True
1005
+
1006
+ # Draw the letters typed since this buffer last showed the speech, from the spans
1007
+ # _speech_grew just worked out.
1008
+ def _type_speech(self, fb):
1009
+ cx, cy, _, _ = self.card_speech
1010
+ full = self.speech_full
1011
+ was = self._spans_was
1012
+ now = self._spans
1013
+ for i in range(self._lines):
1014
+ start = now[2 * i]
1015
+ end = now[2 * i + 1]
1016
+ a = was[2 * i + 1] if i < self._lines_was else start
1017
+ if end > a:
1018
+ fb.text(full[a:end], cx + 8 + (a - start) * 8, cy + 20 + i * self._speech_line_h, CREAM)
1019
+
1020
+ # Render the 5 interactive companion reaction buttons along the bottom bar.
1021
+ def _draw_action_buttons(self, fb):
1022
+ for i, (bx, by, bw, bh) in enumerate(self.btn_rects):
1023
+ label, _, _, accent, _ = ACTIONS[i]
1024
+ active = i == self.active_btn
1025
+ bg = DEEP_TEAL if active else NIGHT
1026
+ border = accent if active else STEEL
1027
+ draw_pixel_panel(fb, bx, by, bw, bh, bg, border, WHITE if active else None)
1028
+ # Trim button label if needed on compact 480x320 screens.
1029
+ text = label if len(label) * 8 <= bw - 8 else label[2:]
1030
+ tx = bx + ((bw - len(text) * 8) >> 1)
1031
+ ty = by + ((bh - 8) >> 1)
1032
+ fb.text(text, tx, ty, WHITE if active else SILVER)