Skip to main content

cuqueclicker_lib/ui/
mod.rs

1pub mod achievements;
2pub mod biscuit;
3pub mod border;
4pub mod debug_pane;
5pub mod effects;
6pub mod hands;
7pub mod prestige;
8pub mod sidebar;
9pub mod stats;
10pub mod toast;
11pub mod upgrades;
12
13use ratatui::{prelude::*, widgets::*};
14
15use crate::format;
16use crate::game::state::{Buff, GameState, HUD_FLASH_TICKS, TICK_HZ};
17use crate::i18n::t;
18
19// Hardcoded as "0.0.0" in source; release.yml patches Cargo.toml before
20// building so CARGO_PKG_VERSION reflects the real version in shipped
21// binaries. A 0.0.0 build advertises itself as "(dev)" in the HUD.
22const VERSION: &str = env!("CARGO_PKG_VERSION");
23
24fn hud_title() -> String {
25    if VERSION == "0.0.0" {
26        // Dev builds include the git branch (or short SHA on detached HEAD)
27        // so two instances built from different branches can be told apart
28        // at a glance — useful for side-by-side comparison.
29        match crate::build_info::GIT_BRANCH {
30            Some(branch) => format!(" CuqueClicker v0.0.0 (dev, {branch}) "),
31            None => " CuqueClicker v0.0.0 (dev) ".into(),
32        }
33    } else {
34        format!(" CuqueClicker v{VERSION} ")
35    }
36}
37
38#[derive(Copy, Clone, PartialEq, Eq, Debug)]
39pub enum Mode {
40    Game,
41    Stats,
42    Achievements,
43    Upgrades,
44    Prestige,
45}
46
47/// Click target for a help-bar hint or for the prestige-reset confirm
48/// line. Mirrors the keyboard shortcuts so the mouse-first player has
49/// equivalent reach to every action a key would fire.
50#[derive(Clone, Copy, PartialEq, Eq, Debug)]
51pub enum HelpAction {
52    /// Open the named mode (or close it back to Game if already there).
53    OpenMode(Mode),
54    /// Catch whatever golden is on screen.
55    GrabGolden,
56    /// Confirm-and-claim prestige reset (only when prestige_available > 0).
57    PrestigeReset,
58    /// Quit the program.
59    Quit,
60}
61
62/// Per-frame layout snapshot produced by [`draw`]. Single source of truth
63/// for every clickable region on screen + the play-area envelope.
64///
65/// The platform shells (`app.rs`, `wasm_app.rs`) **store this verbatim**
66/// and call [`crate::input::InputContext::from_layout`] to project it
67/// into the per-event input context. Adding a new clickable region only
68/// touches this struct + `InputContext` + the projection — never the
69/// platform code. (We were burned by the alternative once: a missing
70/// `green_coin_rect` field on the wasm-side `InputContext` constructor
71/// crashed the wasm build after the native code shipped just fine.)
72#[derive(Default)]
73pub struct DrawOutput {
74    pub biscuit_rect: Rect,
75    /// Screen position of the biscuit's focal cell ("the asshole"). Each
76    /// zoom level's art has the focal at a slightly different offset
77    /// inside its bounding box (TINY: col 7 of width 16, FULL: col 31 of
78    /// width 60, etc.), so the bbox center isn't the visual center. This
79    /// drives `hands::draw`'s orbit center.
80    pub biscuit_focal: (u16, u16),
81    /// One rect per Golden variant slot, indexed by `GoldenVariant as usize`
82    /// (Lucky=0, Frenzy=1, Buff=2). Zero-rect when that slot is empty.
83    /// Each is hit-tested independently — clicking one variant doesn't
84    /// vacuum up an active sibling.
85    pub golden_rects: [Rect; 3],
86    /// On-screen Green Coin marker rect, or zero-rect when no coin is
87    /// visible. Click-routes through `Action::CatchGreenCoin`.
88    pub green_coin_rect: Rect,
89    /// The whole left column where the biscuit + hands + particles live —
90    /// i.e. "the box that displays the ass." Used by the input router so
91    /// the scroll-wheel zoom fires anywhere in this region (including the
92    /// vast empty space around a small biscuit at low zoom), and only the
93    /// right-hand sidebar opts out of zoom.
94    pub play_area: Rect,
95    /// `(upgrade_idx, screen_row_rect)` pairs for the Upgrades panel —
96    /// populated only when the active mode renders that panel; empty
97    /// otherwise. The click router hit-tests these for `BuyUpgrade`.
98    /// First element of each tuple is also the digit-shortcut target,
99    /// kept aligned with `visible_upgrades`.
100    pub upgrade_rows: Vec<(usize, Rect)>,
101    /// `(fingerer_idx, screen_row_rect)` for the Game-mode sidebar.
102    pub fingerer_rows: Vec<(usize, Rect)>,
103    /// (action, rect) for every clickable help-bar hint at the bottom of
104    /// the play column. Mouse-first players use these to switch panels,
105    /// catch goldens, prestige-reset, and quit — all of which used to be
106    /// keyboard-only. Empty rects when the hint is non-actionable
107    /// (e.g. `[Space/Click] finger` is informational, not a click target).
108    pub help_hits: Vec<(HelpAction, Rect)>,
109    /// Click rect for the `Press [r] to reset and claim` confirm line in
110    /// the Prestige panel. Default rect when not in Prestige mode or no
111    /// prestige is available.
112    pub prestige_reset_rect: Rect,
113}
114
115fn wrapped_height(text: &str, width: u16) -> u16 {
116    if width == 0 {
117        return text.lines().count().max(1) as u16;
118    }
119    let mut total: u16 = 0;
120    for line in text.split('\n') {
121        let mut row_len: u16 = 0;
122        let mut rows: u16 = 1;
123        for word in line.split_whitespace() {
124            let wlen = word.chars().count() as u16;
125            if row_len == 0 {
126                row_len = wlen.min(width);
127            } else if row_len + 1 + wlen <= width {
128                row_len += 1 + wlen;
129            } else {
130                rows += 1;
131                row_len = wlen.min(width);
132            }
133        }
134        total = total.saturating_add(rows);
135    }
136    total.max(1)
137}
138
139fn draw_zoom_indicator(frame: &mut Frame, area: Rect, label: &str) {
140    let text = format!("zoom {}", label);
141    let w = text.chars().count() as u16;
142    if area.width < w || area.height == 0 {
143        return;
144    }
145    let col = area.x + area.width - w;
146    let row = area.y + area.height - 1;
147    let buf = frame.buffer_mut();
148    buf.set_string(
149        col,
150        row,
151        &text,
152        Style::default().fg(Color::Rgb(120, 120, 120)),
153    );
154}
155
156pub fn draw(
157    frame: &mut Frame,
158    state: &GameState,
159    mode: Mode,
160    zoom_idx: usize,
161    debug: bool,
162    mouse_pos: Option<(u16, u16)>,
163) -> DrawOutput {
164    let lang = t();
165    let area = frame.area();
166    let cols = Layout::horizontal([Constraint::Min(1), Constraint::Length(38)]).split(area);
167
168    let help_text = match mode {
169        Mode::Game => lang.help_game,
170        Mode::Stats => lang.help_stats,
171        Mode::Achievements => lang.help_ach,
172        Mode::Upgrades => lang.help_upgrades,
173        Mode::Prestige => lang.help_prestige,
174    };
175    let help_height = wrapped_height(help_text, cols[0].width).max(1);
176    let left = Layout::vertical([
177        Constraint::Length(3),
178        Constraint::Min(1),
179        Constraint::Length(help_height),
180    ])
181    .split(cols[0]);
182
183    // J5 count-up: render the smoothed `displayed_*` values rather than the
184    // raw current values. Big jumps (golden, max-buy, F4) ease in instead of
185    // snapping. Tween itself runs in `state.tick()`.
186    //
187    // Color sweep: TWO competing channels — green for cuques going UP
188    // (income, golden, F4), red for cuques going DOWN (purchase,
189    // prestige reset). Whichever channel is stronger this frame drives
190    // the lerp toward white. So a buy that lands during a still-decaying
191    // gain flash correctly flips the digits red as the spend channel
192    // overtakes the fading gain. Both lerp toward bright white at t=0,
193    // which matches the resting (no-flash) style — no hard cut.
194    let gain_t = (state.cuques_flash_ticks as f32 / HUD_FLASH_TICKS as f32).clamp(0.0, 1.0);
195    let spend_t = (state.cuques_spend_flash_ticks as f32 / HUD_FLASH_TICKS as f32).clamp(0.0, 1.0);
196    const FLASH_GAIN: (f32, f32, f32) = (80.0, 255.0, 80.0); // bright green
197    const FLASH_SPEND: (f32, f32, f32) = (255.0, 90.0, 90.0); // urgent red
198    const FLASH_REST: (f32, f32, f32) = (255.0, 255.0, 255.0);
199    let (peak, t) = if spend_t > gain_t {
200        (FLASH_SPEND, spend_t)
201    } else {
202        (FLASH_GAIN, gain_t)
203    };
204    let mix = 1.0 - t;
205    let r = peak.0 + (FLASH_REST.0 - peak.0) * mix;
206    let g = peak.1 + (FLASH_REST.1 - peak.1) * mix;
207    let b = peak.2 + (FLASH_REST.2 - peak.2) * mix;
208    let cuques_style = Style::default()
209        .fg(Color::Rgb(
210            r.clamp(0.0, 255.0) as u8,
211            g.clamp(0.0, 255.0) as u8,
212            b.clamp(0.0, 255.0) as u8,
213        ))
214        .add_modifier(Modifier::BOLD);
215    let mut hud_spans: Vec<Span> = vec![
216        Span::raw(format!("{}: ", lang.hud_cuques)),
217        Span::styled(format::big(state.displayed_cuques), cuques_style),
218        Span::raw(format!(
219            "   {}: {}",
220            lang.hud_fps,
221            format::rate(state.displayed_fps)
222        )),
223    ];
224    if state.prestige > 0 {
225        hud_spans.push(Span::styled(
226            format!(
227                "   {}: {} (+{:.0}%)",
228                lang.prestige_title.trim(),
229                state.prestige,
230                state.prestige as f64
231            ),
232            Style::default()
233                .fg(Color::Rgb(255, 215, 0))
234                .add_modifier(Modifier::BOLD),
235        ));
236    }
237    for b in &state.buffs {
238        let secs = b.ticks_remaining().div_ceil(TICK_HZ);
239        let (label, color) = match b {
240            Buff::ClickFrenzy { mult, .. } => (
241                format!("  [!! FRENZY x{} {}s]", *mult as u64, secs),
242                Color::Rgb(255, 80, 80),
243            ),
244        };
245        hud_spans.push(Span::styled(
246            label,
247            Style::default().fg(color).add_modifier(Modifier::BOLD),
248        ));
249    }
250    // Active timed per-fingerer modifiers — Purple Coin Buff golden today,
251    // anything else timed in the future. Phase 5 of #21 will replace this
252    // with a dedicated HUD strip; for now we mirror the legacy chip layout
253    // so UX continuity holds across phases.
254    for (id, st) in &state.fingerers_state {
255        for m in &st.modifiers {
256            let crate::game::modifier::ModifierDuration::Ticks(remaining) = m.duration else {
257                continue;
258            };
259            let secs = remaining.div_ceil(TICK_HZ);
260            let idx = crate::game::fingerer::FINGERERS
261                .iter()
262                .position(|f| f.id == id);
263            let name = idx
264                .and_then(|i| lang.fingerer_names.get(i).copied())
265                .unwrap_or("?");
266            // Pick a number to show: prefer the strongest single MulFactor
267            // effect (matches the old "x7" presentation); fall back to a
268            // count-of-effects marker for purely additive sources.
269            let mul = m.effects.iter().find_map(|e| match e {
270                crate::game::modifier::ModifierEffect::MulFactor(v) => Some(*v),
271                _ => None,
272            });
273            let label = match mul {
274                Some(v) => format!("  [++ {} x{} {}s]", name, v as u64, secs),
275                None => format!("  [++ {} {}s]", name, secs),
276            };
277            let color = match m.source {
278                crate::game::modifier::ModifierSource::PurpleCoin => Color::Rgb(220, 140, 255),
279                crate::game::modifier::ModifierSource::GreenCoin => Color::Rgb(120, 230, 140),
280            };
281            hud_spans.push(Span::styled(
282                label,
283                Style::default().fg(color).add_modifier(Modifier::BOLD),
284            ));
285        }
286    }
287    let title = hud_title();
288    border::draw_animated(frame, left[0], state, &title);
289    let hud_inner = Rect {
290        x: left[0].x + 1,
291        y: left[0].y + 1,
292        width: left[0].width.saturating_sub(2),
293        height: left[0].height.saturating_sub(2),
294    };
295    let hud = Paragraph::new(Line::from(hud_spans));
296    frame.render_widget(hud, hud_inner);
297
298    let biscuit_rect = biscuit::draw(frame, left[1], state, zoom_idx);
299    let biscuit_focal = biscuit::focal_point(zoom_idx, biscuit_rect);
300    hands::draw(frame, left[1], biscuit_rect, biscuit_focal, state);
301    effects::draw_particles(frame, biscuit_rect, &state.particles);
302    effects::draw_misclicks(frame, &state.misclick_particles);
303    draw_zoom_indicator(
304        frame,
305        left[1],
306        biscuit::level_label(zoom_idx).unwrap_or("100%"),
307    );
308
309    if debug {
310        debug_pane::draw(frame, left[1]);
311    }
312    // Each variant is rendered independently from its own slot. Order
313    // doesn't matter for the visual result — they're at different
314    // fractional positions on the biscuit (sometimes overlapping at
315    // random; that's fine).
316    let mut golden_rects: [Rect; 3] = [Rect::default(); 3];
317    for (i, slot) in state.goldens.iter().enumerate() {
318        if let Some(g) = slot {
319            golden_rects[i] = biscuit::draw_golden(frame, g, biscuit_rect);
320        }
321    }
322    let green_coin_rect = match &state.green_coin {
323        Some(c) => biscuit::draw_green_coin(frame, c, biscuit_rect),
324        None => Rect::default(),
325    };
326
327    // J1: achievement toast overlay. Lives in `left[1]` (biscuit/main area)
328    // so it covers nothing important on the right; auto-dismisses after
329    // TOAST_TICKS via the sim. We render *after* biscuit/golden so it
330    // always sits on top.
331    toast::draw(frame, left[1], state);
332
333    // Custom help-bar render: lay out `[X] label` tokens left-to-right,
334    // wrapping at the rect width, paint each with mode-aware styling
335    // (active mode bolded), and return per-token click rects so the
336    // mouse-first player can drive the game without ever touching a key.
337    let help_hits = draw_help(frame, left[2], help_text, mode, mouse_pos);
338
339    let mut upgrade_rows: Vec<(usize, Rect)> = Vec::new();
340    let mut fingerer_rows: Vec<(usize, Rect)> = Vec::new();
341    let mut prestige_reset_rect = Rect::default();
342    match mode {
343        Mode::Game => fingerer_rows = sidebar::draw(frame, cols[1], state, mouse_pos),
344        Mode::Stats => stats::draw(frame, cols[1], state),
345        Mode::Achievements => achievements::draw(frame, cols[1], state),
346        Mode::Upgrades => upgrade_rows = upgrades::draw(frame, cols[1], state, mouse_pos),
347        Mode::Prestige => prestige_reset_rect = prestige::draw(frame, cols[1], state, mouse_pos),
348    }
349
350    DrawOutput {
351        biscuit_rect,
352        biscuit_focal,
353        golden_rects,
354        green_coin_rect,
355        play_area: left[1],
356        upgrade_rows,
357        fingerer_rows,
358        help_hits,
359        prestige_reset_rect,
360    }
361}
362
363/// Custom help-bar renderer.
364///
365/// Splits the help string into "tokens" (each token is a contiguous
366/// non-whitespace run of `[X] label words`, separated from the next by
367/// a double space or a newline — the convention used in `i18n::Lang`'s
368/// help strings). Each token is laid out left-to-right with wrap at
369/// the rect's width, painted at the resolved screen position, and
370/// matched against a (mode, key) → action table. Clickable tokens get
371/// a slightly brighter color and BOLD; the token under the mouse
372/// cursor gets an additional brightness lift + bg fill so the player
373/// reads it as a button.
374fn draw_help(
375    frame: &mut Frame,
376    area: Rect,
377    text: &str,
378    mode: Mode,
379    mouse_pos: Option<(u16, u16)>,
380) -> Vec<(HelpAction, Rect)> {
381    let mut hits: Vec<(HelpAction, Rect)> = Vec::new();
382    if area.width == 0 || area.height == 0 {
383        return hits;
384    }
385    let buf = frame.buffer_mut();
386    let mut cursor_x: u16 = 0;
387    let mut cursor_y: u16 = 0;
388    for line in text.split('\n') {
389        // Tokens are separated by a literal `  ` (two spaces). Single
390        // spaces inside a token are content (e.g. "back to game").
391        for token in line.split("  ") {
392            let token = token.trim();
393            if token.is_empty() {
394                continue;
395            }
396            let w = token.chars().count() as u16;
397            // Wrap if the token wouldn't fit on the current line.
398            if cursor_x + w > area.width && cursor_x > 0 {
399                cursor_y += 1;
400                cursor_x = 0;
401            }
402            if cursor_y >= area.height {
403                break;
404            }
405            let action = map_help_token(token, mode);
406            // Hide `[q] quit` (or its localized equivalent) on platforms
407            // where the wasm/native runner has no authority to exit —
408            // see `platform::Capabilities::can_quit`. Skipping renders
409            // AND skips appending to `help_hits`, so the next token
410            // slides into the position cursor without leaving a gap.
411            if matches!(action, Some(HelpAction::Quit)) && !crate::platform::CAPABILITIES.can_quit {
412                continue;
413            }
414            let active = matches!(action, Some(HelpAction::OpenMode(m)) if m == mode);
415            let token_rect = Rect {
416                x: area.x + cursor_x,
417                y: area.y + cursor_y,
418                width: w.min(area.width.saturating_sub(cursor_x)),
419                height: 1,
420            };
421            // Style picker:
422            //  - active mode hint                : bright yellow, BOLD
423            //  - actionable (clickable)          : light gray, BOLD
424            //  - informational                   : dark gray
425            //  - hovered                         : color lifted, bg tint
426            // Hover lift fires ONLY on actionable hints — informational
427            // tokens like `[Space/Click] finger` and `[Shift] x10` are
428            // descriptive labels with no click handler, so brightening
429            // them on hover would advertise a button that doesn't exist.
430            let hovered = action.is_some()
431                && mouse_pos
432                    .map(|(mx, my)| {
433                        mx >= token_rect.x
434                            && mx < token_rect.x + token_rect.width
435                            && my == token_rect.y
436                    })
437                    .unwrap_or(false);
438            let mut style = if active {
439                Style::default()
440                    .fg(Color::Rgb(255, 220, 120))
441                    .add_modifier(Modifier::BOLD)
442            } else if action.is_some() {
443                Style::default()
444                    .fg(Color::Rgb(180, 180, 180))
445                    .add_modifier(Modifier::BOLD)
446            } else {
447                Style::default().fg(Color::DarkGray)
448            };
449            if hovered {
450                style = style
451                    .fg(Color::Rgb(255, 255, 255))
452                    .bg(Color::Rgb(40, 40, 50))
453                    .add_modifier(Modifier::BOLD);
454            }
455            buf.set_string(token_rect.x, token_rect.y, token, style);
456            if let Some(a) = action {
457                hits.push((a, token_rect));
458            }
459            cursor_x += w + 2; // double-space separator
460        }
461        cursor_y += 1;
462        cursor_x = 0;
463        if cursor_y >= area.height {
464            break;
465        }
466    }
467    hits
468}
469
470/// Match a help-bar token like `"[u] upgrades"` to a `HelpAction`,
471/// disambiguated by the current mode (so `[s] stats` opens Stats from
472/// Game but `[s/Esc] back to game` from Stats returns to Game).
473fn map_help_token(token: &str, mode: Mode) -> Option<HelpAction> {
474    // Extract the bracketed key. We accept the first `[...]` group;
475    // everything after the first `]` is descriptive label.
476    let open = token.find('[')?;
477    let close = token[open + 1..].find(']')? + open + 1;
478    let key = &token[open + 1..close];
479    // Universal hints first.
480    if key.eq_ignore_ascii_case("q") {
481        return Some(HelpAction::Quit);
482    }
483    // Back-to-game from any non-Game mode (the `[X/Esc] back ...` pattern
484    // covers stats / achievements / upgrades / prestige).
485    if mode != Mode::Game && (key.contains("Esc") || key.contains("esc")) {
486        return Some(HelpAction::OpenMode(Mode::Game));
487    }
488    // Single-letter mode openers, only meaningful from Game.
489    match (mode, key) {
490        (Mode::Game, "u") | (Mode::Game, "U") => Some(HelpAction::OpenMode(Mode::Upgrades)),
491        (Mode::Game, "p") | (Mode::Game, "P") => Some(HelpAction::OpenMode(Mode::Prestige)),
492        (Mode::Game, "s") | (Mode::Game, "S") => Some(HelpAction::OpenMode(Mode::Stats)),
493        (Mode::Game, "a") | (Mode::Game, "A") => Some(HelpAction::OpenMode(Mode::Achievements)),
494        (Mode::Game, "g") | (Mode::Game, "G") => Some(HelpAction::GrabGolden),
495        (Mode::Prestige, "r") | (Mode::Prestige, "R") => Some(HelpAction::PrestigeReset),
496        _ => None,
497    }
498}