Language dropdown with pixel flags in settings, title and login
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
b025036277
commit
d8e9184a9c
3 files changed
+334
-5
No files matched your search
@@ -0,0 +1,207 @@
|
||||
// Pixel flags for the language dropdown: 12 x 9 pixels each, one letter per pixel
|
||||
// (see PALETTE). Simplified renditions; the coats of arms are a few pixels only.
|
||||
|
||||
use macroquad::prelude::*;
|
||||
|
||||
pub const FLAG_W: usize = 12;
|
||||
pub const FLAG_H: usize = 9;
|
||||
|
||||
fn palette(c: char) -> Option<Color> {
|
||||
let rgb = |r: u8, g: u8, b: u8| Some(Color::from_rgba(r, g, b, 255));
|
||||
match c {
|
||||
'R' => rgb(206, 32, 41), // red
|
||||
'W' => rgb(240, 240, 240), // white
|
||||
'B' => rgb(0, 57, 166), // blue
|
||||
'N' => rgb(1, 33, 105), // navy (Union Jack)
|
||||
'Y' => rgb(255, 204, 0), // yellow
|
||||
'G' => rgb(0, 146, 70), // green
|
||||
'D' => rgb(0, 102, 0), // dark green (Portugal)
|
||||
'K' => rgb(16, 16, 16), // black
|
||||
'S' => rgb(0, 106, 167), // Swedish blue
|
||||
'F' => rgb(0, 47, 108), // Finnish blue
|
||||
'L' => rgb(13, 94, 175), // Greek blue
|
||||
'O' => rgb(170, 21, 27), // dark red (coats of arms)
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
// three horizontal stripes of three rows each
|
||||
macro_rules! tricolour {
|
||||
($a:literal, $b:literal, $c:literal) => {
|
||||
[$a, $a, $a, $b, $b, $b, $c, $c, $c]
|
||||
};
|
||||
}
|
||||
|
||||
// the flag of a language code
|
||||
pub fn flag(code: &str) -> Option<[&'static str; FLAG_H]> {
|
||||
Some(match code {
|
||||
"de" => tricolour!("KKKKKKKKKKKK", "RRRRRRRRRRRR", "YYYYYYYYYYYY"),
|
||||
"nl" => tricolour!("RRRRRRRRRRRR", "WWWWWWWWWWWW", "BBBBBBBBBBBB"),
|
||||
"ru" => tricolour!("WWWWWWWWWWWW", "BBBBBBBBBBBB", "RRRRRRRRRRRR"),
|
||||
"hu" => tricolour!("RRRRRRRRRRRR", "WWWWWWWWWWWW", "GGGGGGGGGGGG"),
|
||||
"it" => ["GGGGWWWWRRRR"; FLAG_H],
|
||||
"ro" => ["BBBBYYYYRRRR"; FLAG_H],
|
||||
"en" => [
|
||||
"RWNNWRRWNNWR",
|
||||
"WRRWWRRWWRRW",
|
||||
"NNWRWRRWRWNN",
|
||||
"WWWWWRRWWWWW",
|
||||
"RRRRRRRRRRRR",
|
||||
"WWWWWRRWWWWW",
|
||||
"NNWRWRRWRWNN",
|
||||
"WRRWWRRWWRRW",
|
||||
"RWNNWRRWNNWR",
|
||||
],
|
||||
"es" => [
|
||||
"RRRRRRRRRRRR",
|
||||
"RRRRRRRRRRRR",
|
||||
"YYYYYYYYYYYY",
|
||||
"YYOOYYYYYYYY",
|
||||
"YYOROYYYYYYY",
|
||||
"YYOOYYYYYYYY",
|
||||
"YYYYYYYYYYYY",
|
||||
"RRRRRRRRRRRR",
|
||||
"RRRRRRRRRRRR",
|
||||
],
|
||||
"pt" => [
|
||||
"DDDDDRRRRRRR",
|
||||
"DDDDDRRRRRRR",
|
||||
"DDDDDRRRRRRR",
|
||||
"DDDYYYRRRRRR",
|
||||
"DDDYRYRRRRRR",
|
||||
"DDDYYYRRRRRR",
|
||||
"DDDDDRRRRRRR",
|
||||
"DDDDDRRRRRRR",
|
||||
"DDDDDRRRRRRR",
|
||||
],
|
||||
"da" => [
|
||||
"RRRWWRRRRRRR",
|
||||
"RRRWWRRRRRRR",
|
||||
"RRRWWRRRRRRR",
|
||||
"RRRWWRRRRRRR",
|
||||
"WWWWWWWWWWWW",
|
||||
"RRRWWRRRRRRR",
|
||||
"RRRWWRRRRRRR",
|
||||
"RRRWWRRRRRRR",
|
||||
"RRRWWRRRRRRR",
|
||||
],
|
||||
"sv" => [
|
||||
"SSSYYSSSSSSS",
|
||||
"SSSYYSSSSSSS",
|
||||
"SSSYYSSSSSSS",
|
||||
"SSSYYSSSSSSS",
|
||||
"YYYYYYYYYYYY",
|
||||
"SSSYYSSSSSSS",
|
||||
"SSSYYSSSSSSS",
|
||||
"SSSYYSSSSSSS",
|
||||
"SSSYYSSSSSSS",
|
||||
],
|
||||
"fi" => [
|
||||
"WWWFFWWWWWWW",
|
||||
"WWWFFWWWWWWW",
|
||||
"WWWFFWWWWWWW",
|
||||
"FFFFFFFFFFFF",
|
||||
"FFFFFFFFFFFF",
|
||||
"WWWFFWWWWWWW",
|
||||
"WWWFFWWWWWWW",
|
||||
"WWWFFWWWWWWW",
|
||||
"WWWFFWWWWWWW",
|
||||
],
|
||||
"no" => [
|
||||
"RRRWNWRRRRRR",
|
||||
"RRRWNWRRRRRR",
|
||||
"RRRWNWRRRRRR",
|
||||
"WWWWNWWWWWWW",
|
||||
"NNNNNNNNNNNN",
|
||||
"WWWWNWWWWWWW",
|
||||
"RRRWNWRRRRRR",
|
||||
"RRRWNWRRRRRR",
|
||||
"RRRWNWRRRRRR",
|
||||
],
|
||||
"hr" => [
|
||||
"RRRRRRRRRRRR",
|
||||
"RRRRRRRRRRRR",
|
||||
"RRRRRRRRRRRR",
|
||||
"WWWWRWRWWWWW",
|
||||
"WWWWWRWRWWWW",
|
||||
"WWWWRWRWWWWW",
|
||||
"BBBBWRWRBBBB",
|
||||
"BBBBBWRBBBBB",
|
||||
"BBBBBBBBBBBB",
|
||||
],
|
||||
"sr" => [
|
||||
"RRRRRRRRRRRR",
|
||||
"RRRYYRRRRRRR",
|
||||
"RROWWORRRRRR",
|
||||
"BBOWWOBBBBBB",
|
||||
"BBOWWOBBBBBB",
|
||||
"BBBOOBBBBBBB",
|
||||
"WWWWWWWWWWWW",
|
||||
"WWWWWWWWWWWW",
|
||||
"WWWWWWWWWWWW",
|
||||
],
|
||||
"sk" => [
|
||||
"WWWWWWWWWWWW",
|
||||
"WWWWWWWWWWWW",
|
||||
"WWRRRRRWWWWW",
|
||||
"BBRRWRRBBBBB",
|
||||
"BBRWWWRBBBBB",
|
||||
"BBRRWRRBBBBB",
|
||||
"RRRBBBRRRRRR",
|
||||
"RRRRBRRRRRRR",
|
||||
"RRRRRRRRRRRR",
|
||||
],
|
||||
"cs" => [
|
||||
"BWWWWWWWWWWW",
|
||||
"BBWWWWWWWWWW",
|
||||
"BBBWWWWWWWWW",
|
||||
"BBBBWWWWWWWW",
|
||||
"BBBBBRRRRRRR",
|
||||
"BBBBRRRRRRRR",
|
||||
"BBBRRRRRRRRR",
|
||||
"BBRRRRRRRRRR",
|
||||
"BRRRRRRRRRRR",
|
||||
],
|
||||
"el" => [
|
||||
"LLWLLWWWWWWW",
|
||||
"LLWLLLLLLLLL",
|
||||
"WWWWWWWWWWWW",
|
||||
"LLWLLLLLLLLL",
|
||||
"LLWLLWWWWWWW",
|
||||
"LLLLLLLLLLLL",
|
||||
"WWWWWWWWWWWW",
|
||||
"LLLLLLLLLLLL",
|
||||
"WWWWWWWWWWWW",
|
||||
],
|
||||
_ => return None,
|
||||
})
|
||||
}
|
||||
|
||||
// draws a flag with its top-left corner at (x, y), px screen pixels per flag pixel
|
||||
pub fn draw_flag(code: &str, x: f32, y: f32, px: f32) {
|
||||
let rows = flag(code).unwrap_or(["WWWWWWWWWWWW"; FLAG_H]);
|
||||
for (r, row) in rows.iter().enumerate() {
|
||||
for (k, ch) in row.chars().enumerate() {
|
||||
if let Some(c) = palette(ch) {
|
||||
draw_rectangle(x + k as f32 * px, y + r as f32 * px, px, px, c);
|
||||
}
|
||||
}
|
||||
}
|
||||
draw_rectangle_lines(x - 1.0, y - 1.0, FLAG_W as f32 * px + 2.0, FLAG_H as f32 * px + 2.0, 1.0, Color::new(0.0, 0.0, 0.0, 0.6));
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn every_language_has_a_complete_flag() {
|
||||
for code in ["de", "en", "es", "pt", "nl", "da", "no", "sv", "fi", "ru", "hr", "hu", "sr", "ro", "sk", "cs", "it", "el"] {
|
||||
let f = flag(code).unwrap_or_else(|| panic!("{} has a flag", code));
|
||||
for row in f.iter() {
|
||||
assert_eq!(row.chars().count(), FLAG_W, "{}: {}", code, row);
|
||||
assert!(row.chars().all(|c| palette(c).is_some()), "{}: {}", code, row);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user