use crate::classic::font;
use crate::classic::listfont::{self, ListInk};
use crate::classic::main::Painter;
use crate::classic::marks::{MarkPlace, RowMark, RowMarks, MARK_GAP};
use crate::classic::style::ClassicStyle;
use crate::classic::touch::Control;
use crate::draw::{Align, DrawCmd, Rect};
use crate::gui::ObjId;
use crate::player::Status;
use crate::runtime::SkinRuntime;
fn time_text(seconds: i32) -> String {
if seconds < 3600 {
format!("{}:{:02}", seconds / 60, seconds % 60)
} else {
format!(
"{}:{:02}:{:02}",
seconds / 3600,
(seconds / 60) % 60,
seconds % 60
)
}
}
fn minutes_text(seconds: i32) -> String {
format!("{}:{:02}", seconds / 60, seconds % 60)
}
fn numbered(n: usize, count: usize, title: &str) -> String {
let width = count.max(1).to_string().len();
format!("{n:>width$}. {title}")
}
pub(crate) const ROW_TOP: i32 = 22;
const LIST_LEFT: i32 = 2;
const LIST_RIGHT: i32 = 3;
const LIST_GAP: i32 = 4;
impl SkinRuntime {
fn classic_pl_font(face: &str) -> Option<crate::draw::FontDef> {
(!face.is_empty()).then(|| crate::draw::FontDef::new(&face.to_lowercase(), ""))
}
fn classic_pl_fitted(
&mut self,
text: &str,
font: Option<&crate::draw::FontDef>,
width: i32,
) -> String {
let size = self.classic_row_font();
let fits = |rt: &mut Self, s: &str| rt.host.text_width(s, font, size, false) <= width;
if fits(self, text) {
return text.to_string();
}
let chars: Vec<char> = text.chars().collect();
let cut = |n: usize| format!("{}...", chars[..n].iter().collect::<String>());
let (mut lo, mut hi) = (0, chars.len());
while lo < hi {
let mid = (lo + hi).div_ceil(2);
if fits(self, &cut(mid)) {
lo = mid;
} else {
hi = mid - 1;
}
}
cut(lo)
}
pub(crate) fn draw_classic_pl_shade(&mut self, o: ObjId, at: Rect, out: &mut Vec<DrawCmd>) {
let w = at.width;
let active = self
.layout_object(o)
.is_some_and(|l| self.has_focus(Some(l)));
let y = if active { 42 } else { 57 };
let mut p = Painter {
at,
out,
sheets: self.classic_sheets.clone(),
};
p.blit("pledit.bmp", 72, y, 25, 14, 0, 0);
let mut x = 25;
for _ in 1..(w - 50) / 25 {
p.blit("pledit.bmp", 72, 57, 25, 14, x, 0);
x += 25;
}
let rest = (w - 50) % 25;
if rest != 0 {
p.stretch("pledit.bmp", 72, 57, 24, 14, x, 0, rest, 14);
x += rest;
}
p.blit("pledit.bmp", 99, y, 50, 14, x, 0);
let index = self.player.track_index();
let (title, length) = usize::try_from(index)
.ok()
.and_then(|i| self.player.tracks().into_iter().nth(i))
.map_or((String::new(), 0), |t| (t.title, t.length_ms / 1000));
if self.classic_style == ClassicStyle::Winamp291 {
let count = self.player.tracks().len();
let line = numbered(index as usize + 1, count, &title);
Self::shade_line_291(&mut p, w, &line, length);
return;
}
let time = minutes_text(length);
let time_cells = time.chars().count() as i32;
let room = (w - 29 - 4) / 5 - time_cells - 1;
let mut x = 4;
let line: Vec<char> = numbered(index as usize + 1, self.player.tracks().len(), &title)
.chars()
.collect();
let room = room.max(0) as usize;
for (i, &c) in line.iter().take(room).enumerate() {
let c = if i + 1 == room && line.len() > room {
'\u{1}'
} else {
c
};
let (cx, cy) = font::cell(c);
p.blit("text.bmp", cx, cy, 5, 6, x, 4);
x += 5;
}
let time_x = w - 29 - time_cells * 5;
while x < time_x {
let (cx, cy) = font::cell(' ');
p.blit("text.bmp", cx, cy, 5, 6, x, 4);
x += 5;
}
for c in time.chars() {
let (cx, cy) = font::cell(c);
p.blit("text.bmp", cx, cy, 5, 6, x, 4);
x += 5;
}
if x < w - 30 {
let (cx, cy) = font::cell(' ');
p.blit("text.bmp", cx, cy, (w - 30 - x).min(5), 6, x, 4);
}
}
fn shade_line_291(p: &mut Painter, w: i32, line: &str, secs: i32) {
let time = format!("{}:{:02}", secs / 60, secs % 60);
let time_cells = time.chars().count() as i32;
let cells = ((w - 40) / 5 - time_cells - 1).max(0) as usize;
let chars: Vec<char> = line.chars().collect();
let cell = |p: &mut Painter, c: char, x: i32| {
let (cx, cy) = font::cell(c);
p.blit("text.bmp", cx, cy, 5, 6, x, 4);
};
let mut x = 5;
for (i, &c) in chars.iter().take(cells).enumerate() {
let cut = i + 1 == cells && i + 1 < chars.len();
cell(p, if cut { '\u{1}' } else { c }, x);
x += 5;
}
if chars.len() <= cells {
for _ in chars.len()..=cells {
cell(p, ' ', x);
x += 5;
}
}
cell(p, ' ', x);
x = w - 29 - time_cells * 5;
for c in time.chars() {
cell(p, c, x);
x += 5;
}
if x < w - 30 {
let (cx, cy) = font::cell(' ');
p.blit("text.bmp", cx, cy, w - 30 - x, 6, x, 4);
}
}
pub(crate) fn classic_pl_rows(&self, h: i32) -> i32 {
((h - 60) / self.classic_row()).max(0)
}
pub(crate) fn classic_row(&self) -> i32 {
self.classic_row_height
.unwrap_or((self.classic_list_font * 5 + 2) / 4)
+ self.list_row_extra
}
pub(crate) fn measure_classic_row(&mut self) {
let face = self.classic_pledit.font.clone();
let font = Self::classic_pl_font(&face);
self.classic_row_height = self.gdi_measured_height(font.as_ref(), self.classic_list_font);
}
pub(crate) fn classic_row_font(&self) -> i32 {
self.classic_list_font * 4 / 3
}
pub fn set_classic_list_font_size(&mut self, px: i32) {
let px = px.clamp(6, 40);
if px != self.classic_list_font {
self.classic_list_font = px;
if self.classic_skin {
self.measure_classic_row();
}
self.repaint();
}
}
pub(crate) fn classic_pl_scroll(&self, h: i32) -> i32 {
let past = self.player.tracks().len() as i32 - self.classic_pl_rows(h);
self.classic_pl_scroll.clamp(0, past.max(0))
}
pub(crate) fn classic_pl_thumb(&self, h: i32, count: i32, scroll: i32) -> i32 {
let past = count - self.classic_pl_rows(h);
if past <= 0 {
return 0;
}
((h - 76) * scroll.min(past) / past).max(0)
}
fn classic_list_ink(&mut self) -> Option<std::rc::Rc<ListInk>> {
if !self.classic_list_ink_read {
self.classic_list_ink_read = true;
let sheet = self.classic_sheet("text.bmp");
self.classic_list_ink = self
.image_size(&sheet)
.and_then(|(w, h)| {
let px = self.host.pixels(&sheet, 0, 0, w, h)?;
ListInk::of(&px, w, h)
})
.map(std::rc::Rc::new);
}
self.classic_list_ink.clone()
}
#[allow(clippy::too_many_arguments)]
fn letter_row(
ink: &ListInk,
block: &mut [u32],
width: i32,
top: i32,
number: usize,
title: &str,
time: &str,
rgb: u32,
mark: Option<&RowMark>,
cell: i32,
place: MarkPlace,
) {
let number = format!("{number}. ");
ink.write(&number, (LIST_LEFT, top), rgb, block, width, width);
let mut left = LIST_LEFT + number.chars().count() as i32 * listfont::CELL_W;
let time_x = width - LIST_RIGHT - time.chars().count() as i32 * listfont::CELL_W;
ink.write(time, (time_x, top), rgb, block, width, width);
let mut right = time_x - LIST_GAP;
if cell > 0 {
let x = if place == MarkPlace::Before {
left += cell + MARK_GAP;
left - cell - MARK_GAP
} else {
right -= cell + MARK_GAP;
right + MARK_GAP
};
if let Some(m) = mark {
Self::letter_mark(ink, block, width, (x, top), cell, m, rgb);
}
}
let room = (right - left) / listfont::CELL_W;
ink.write(
&listfont::fitted(title, room),
(left, top),
rgb,
block,
width,
right.max(left),
);
}
fn letter_mark(
ink: &ListInk,
block: &mut [u32],
width: i32,
(x, top): (i32, i32),
cell: i32,
mark: &RowMark,
rgb: u32,
) {
let w = mark.pixel_width();
if w == 0 {
let tw = mark.text.chars().count() as i32 * listfont::CELL_W;
ink.write(
&mark.text,
(x + (cell - tw) / 2, top),
rgb,
block,
width,
x + cell,
);
return;
}
let rows = block.len() as i32 / width.max(1);
let x0 = x + (cell - w) / 2;
let y0 = top + (listfont::CELL_H - 1 - mark.pixel_height()) / 2;
let picture = mark.picture(0xff00_0000 | (rgb & 0xff_ffff));
for py in 0..mark.pixel_height() {
for px in 0..w {
let (dx, dy) = (x0 + px, y0 + py);
let a = picture[(py * w + px) as usize];
if a != 0 && (0..width).contains(&dx) && (0..rows).contains(&dy) {
block[(dy * width + dx) as usize] = a;
}
}
}
}
fn outline_mark(
p: &mut Painter,
(x, y): (i32, i32),
(cell, row): (i32, i32),
mark: &RowMark,
color: crate::draw::Rgb,
face: Option<&String>,
size: i32,
) {
let (w, h) = (mark.pixel_width(), mark.pixel_height());
if w == 0 {
p.text(
&mark.text,
face,
size,
(x, y, cell, row),
color,
Align::Center,
);
return;
}
let rect = Rect::new(
p.at.left + x + (cell - w) / 2,
p.at.top + y + (row - h) / 2,
w,
h,
);
p.out.push(DrawCmd::Pixels {
rect,
w,
h,
argb: std::rc::Rc::from(
mark.picture(0xff00_0000 | (color.0 & 0xff_ffff))
.into_boxed_slice(),
),
alpha: 255,
stable: false,
});
}
fn mark_cell(marks: &RowMarks, mut text_w: impl FnMut(&str) -> i32) -> i32 {
marks
.marks
.iter()
.map(|m| match m.pixel_width() {
0 => text_w(&m.text),
w => w,
})
.max()
.unwrap_or(0)
}
pub(crate) fn draw_classic_pl(&mut self, o: ObjId, at: Rect, out: &mut Vec<DrawCmd>) {
let (w, h) = (at.width, at.height);
let active = self
.layout_object(o)
.is_some_and(|l| self.has_focus(Some(l)));
let style = self.classic_pledit.clone();
let tracks = self.player.tracks();
let current = self.player.track_index();
let mut p = Painter {
at,
out,
sheets: self.classic_sheets.clone(),
};
p.fill(12, 20, w - 32, h - 58, style.normal_bg);
let rows = self.classic_pl_rows(h);
let row = self.classic_row();
let font_size = self.classic_row_font();
let ink = if self.skin_list_font {
self.classic_list_ink()
} else {
None
};
let block_w = w - 32;
let mut block = ink
.as_ref()
.map(|_| vec![0u32; (block_w.max(0) * rows.max(0) * row) as usize]);
let mut lettered = false;
let marks = self.classic_row_marks.clone();
let face = Some(&style.font);
let def = Self::classic_pl_font(&style.font);
let letter_cell = Self::mark_cell(&marks, |t| t.chars().count() as i32 * listfont::CELL_W);
let outline_cell = Self::mark_cell(&marks, |t| {
self.host.text_width(t, def.as_ref(), font_size, false)
});
let selected = self.classic_selected_row;
let scroll = self.classic_pl_scroll(h).max(0) as usize;
for (i, t) in tracks
.iter()
.enumerate()
.skip(scroll)
.take(rows.max(0) as usize)
{
let y = ROW_TOP + (i - scroll) as i32 * row;
if selected == Some(i as i32) || self.classic_selected_more.contains(&(i as i32)) {
p.fill(12, y, w - 32, row, style.selected_bg);
}
let color = if i as i32 == current {
style.current
} else {
style.normal
};
let time = time_text(t.length_ms / 1000);
if let (Some(ink), Some(block)) = (ink.as_deref(), block.as_mut()) {
if listfont::covers(&t.title) {
let top = (i - scroll) as i32 * row + (row - listfont::CELL_H) / 2;
Self::letter_row(
ink,
block,
block_w,
top,
i + 1,
&t.title,
&time,
color.0,
marks.of(i),
letter_cell,
marks.place,
);
lettered = true;
continue;
}
}
let time = format!(" {} ", minutes_text(t.length_ms / 1000));
let time_w = self.host.text_width(&time, def.as_ref(), font_size, false);
let mut left = 0;
let mut right = block_w - time_w;
let line = if outline_cell > 0 {
let number = numbered(i + 1, tracks.len(), "");
let x = if marks.place == MarkPlace::Before {
p.text(
&number,
face,
font_size,
(12, y, w - 32, row),
color,
Align::Left,
);
let at = self
.host
.text_width(&number, def.as_ref(), font_size, false);
left = at + outline_cell + MARK_GAP;
at
} else {
right -= outline_cell + MARK_GAP;
right + MARK_GAP
};
if let Some(m) = marks.of(i) {
Self::outline_mark(
&mut p,
(12 + x, y),
(outline_cell, row),
m,
color,
face,
font_size,
);
}
if marks.place == MarkPlace::Before {
t.title.clone()
} else {
numbered(i + 1, tracks.len(), &t.title)
}
} else {
numbered(i + 1, tracks.len(), &t.title)
};
let title = self.classic_pl_fitted(&line, def.as_ref(), right - left);
p.text(
&title,
face,
font_size,
if outline_cell > 0 {
(12 + left, y, right - left, row)
} else {
(12, y, w - 32, row)
},
color,
Align::Left,
);
p.text(
&time,
face,
font_size,
(12, y, w - 32, row),
color,
Align::Right,
);
}
if let Some(block) = block.filter(|_| lettered) {
p.out.push(DrawCmd::Pixels {
rect: Rect::new(at.left + 12, at.top + ROW_TOP, block_w, rows * row),
w: block_w,
h: rows * row,
argb: std::rc::Rc::from(block.into_boxed_slice()),
alpha: 255,
stable: false,
});
}
let y1 = if active { 0 } else { 21 };
p.blit("pledit.bmp", 0, y1, 25, 20, 0, 0);
let tiles = (w - 150) / 25;
let mut x = 25;
if tiles != 0 {
if tiles % 2 != 0 {
p.blit("pledit.bmp", 127, y1, 12, 20, x, 0);
x += 12;
}
for _ in 0..tiles / 2 {
p.blit("pledit.bmp", 127, y1, 25, 20, x, 0);
x += 25;
}
}
p.blit("pledit.bmp", 26, y1, 100, 20, x, 0);
x += 100;
if tiles != 0 {
if tiles % 2 != 0 {
p.blit("pledit.bmp", 127, y1, 13, 20, x, 0);
x += 13;
}
for _ in 0..tiles / 2 {
p.blit("pledit.bmp", 127, y1, 25, 20, x, 0);
x += 25;
}
}
let rest = (w - 150) % 25;
if rest != 0 {
p.stretch("pledit.bmp", 127, y1, 25, 20, x, 0, rest, 20);
x += rest;
}
p.blit("pledit.bmp", 153, y1, 25, 20, x, 0);
let mut y = 20;
for _ in 0..(h - 58) / 29 {
p.blit("pledit.bmp", 0, 42, 12, 29, 0, y);
p.blit("pledit.bmp", 31, 42, 5, 29, w - 20, y);
p.blit("pledit.bmp", 44, 42, 7, 29, w - 7, y);
p.blit("pledit.bmp", 36, 42, 8, 29, w - 15, y);
y += 29;
}
let rest = (h - 58) % 29;
if rest != 0 {
p.stretch("pledit.bmp", 0, 42, 12, 29, 0, y, 12, rest);
p.stretch("pledit.bmp", 31, 42, 5, 29, w - 20, y, 5, rest);
p.stretch("pledit.bmp", 44, 42, 7, 29, w - 7, y, 7, rest);
p.blit("pledit.bmp", 36, 42, 8, rest, w - 15, y);
y += rest;
}
let thumb = self.classic_pl_thumb(h, tracks.len() as i32, scroll as i32);
let held = self
.classic_press_on(o)
.is_some_and(|p| p.control == Control::PlScroll);
p.blit(
"pledit.bmp",
if held { 61 } else { 52 },
53,
8,
18,
w - 15,
20 + thumb,
);
p.blit("pledit.bmp", 0, 72, 125, 38, 0, y);
let mut tiles = (w - 275) / 25;
let vis = tiles > 2;
if vis {
tiles -= 3;
}
let mut x = 125;
for _ in 0..tiles {
p.blit("pledit.bmp", 179, 0, 25, 38, x, y);
x += 25;
}
let rest = (w - 275) % 25;
if rest != 0 {
p.stretch("pledit.bmp", 179, 0, 25, 38, x, y, rest, 38);
x += rest;
}
if vis {
p.blit("pledit.bmp", 205, 0, 75, 38, x, y);
x += 75;
}
p.blit("pledit.bmp", 126, 72, 150, 38, x, y);
let held = self
.classic_press_on(o)
.and_then(|p| match p.control {
Control::PlButton(b) => Some((b, p.hover)),
_ => None,
})
.or(self
.classic_open_menu
.filter(|(menu_on, _)| *menu_on == o)
.map(|(_, b)| (b, None)));
if let Some((b, hover)) = held {
let left = b.menu_left(w);
let items = b.items();
let top = h - 12 - 18 * items;
p.blit("pledit.bmp", b.bar_source(), 111, 3, 18 * items, left, top);
for i in 0..items {
let sy = match i {
3 => 168,
2 => 111,
1 => 130,
_ => 149,
};
let sx = b.picture_source() + if hover == Some(i) { 23 } else { 0 };
p.blit("pledit.bmp", sx, sy, 22, 18, left + 3, h - 30 - 18 * i);
}
}
let total: i32 = tracks.iter().map(|t| t.length_ms / 1000).sum();
let picked: i32 = selected
.and_then(|i| tracks.get(i.max(0) as usize))
.map_or(0, |t| t.length_ms / 1000);
let line = format!("{}/{}", time_text(picked), time_text(total));
let mut x = w - 143;
for c in line.chars().chain(std::iter::repeat(' ')).take(18) {
let (cx, cy) = font::cell(c);
p.blit("text.bmp", cx, cy, 5, 6, x, h - 28);
x += 5;
}
let y = h - 15;
if self.player.status() == Status::Stopped {
p.blit("text.bmp", 142, 0, 4, 6, w - 86, y);
for x in [w - 82, w - 77, w - 72, w - 64, w - 59] {
p.blit("text.bmp", 142, 0, 5, 6, x, y);
}
} else {
let length = self.player.length_ms();
let remaining = self.classic_remaining && length > 0;
let ms = if remaining {
(length - self.player.position_ms()).max(0)
} else {
self.player.position_ms().max(0)
};
let seconds = ms / 1000;
let (minutes, seconds) = (seconds / 60, seconds % 60);
let digit =
|p: &mut Painter, n: i32, x: i32| p.blit("text.bmp", (n % 10) * 5, 6, 5, 6, x, y);
let (sx, sy) = if remaining { (75, 6) } else { (142, 0) };
if minutes / 100 == 0 {
p.blit("text.bmp", 142, 0, 4, 6, w - 86, y);
p.blit("text.bmp", sx, sy, 3, 6, w - 82, y);
} else {
p.blit("text.bmp", sx, sy, 3, 6, w - 86, y);
digit(&mut p, minutes / 100, w - 82);
}
digit(&mut p, minutes / 10, w - 77);
digit(&mut p, minutes, w - 72);
digit(&mut p, seconds / 10, w - 64);
digit(&mut p, seconds, w - 59);
}
}
}
#[cfg(test)]
mod tests {
use crate::classic::main::Painter;
use crate::draw::{DrawCmd, Rect};
use crate::runtime::SkinRuntime;
#[test]
fn the_2_91_shade_line_starts_at_5_and_leaves_a_column_before_the_length() {
let mut out = Vec::new();
let mut p = Painter {
at: Rect::new(0, 0, 275, 14),
out: &mut out,
sheets: std::rc::Rc::default(),
};
SkinRuntime::shade_line_291(&mut p, 275, "1. Andamp", 600);
let xs: Vec<i32> = out
.iter()
.filter_map(|c| match c {
DrawCmd::Image { dst, .. } => Some(dst.left),
_ => None,
})
.collect();
let mut want: Vec<i32> = (0..43).map(|i| 5 + 5 * i).collect();
want.extend([221, 226, 231, 236, 241]);
assert_eq!(xs, want);
}
#[test]
fn a_cut_2_91_shade_line_ends_on_the_ellipsis() {
let mut out = Vec::new();
let mut p = Painter {
at: Rect::new(0, 0, 275, 14),
out: &mut out,
sheets: std::rc::Rc::default(),
};
SkinRuntime::shade_line_291(&mut p, 275, &"x".repeat(60), 61);
let cells: Vec<(i32, i32)> = out
.iter()
.filter_map(|c| match c {
DrawCmd::Image { bitmap, dst, .. } => Some((dst.left, bitmap.x)),
_ => None,
})
.collect();
assert_eq!(cells[41], (210, crate::classic::font::cell('\u{1}').0));
assert_eq!(cells[42].0, 215, "the blank after it");
assert_eq!(cells[43].0, 275 - 29 - 20, "the length");
}
}