Skip to content

Commit

Permalink
feat(junowen): more rich progress?
Browse files Browse the repository at this point in the history
  • Loading branch information
progre committed Nov 19, 2023
1 parent 0820b2c commit cdb19c3
Showing 1 changed file with 71 additions and 32 deletions.
103 changes: 71 additions & 32 deletions junowen/src/in_game_lobby/shared_room.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::ffi::c_void;
use std::{f64::consts::PI, ffi::c_void};

use junowen_lib::{InputValue, RenderingText, Th19};

Expand Down Expand Up @@ -48,34 +48,85 @@ fn make_leave_menu() -> (u8, CommonMenu) {
)
}

fn progress_text(progress: f64) -> (Vec<u8>, Vec<u8>, Vec<u8>, f64) {
fn progress_alphas(progress: f64) -> Vec<u8> {
const LENGTH: f64 = 20.0;
let progress = ((progress + 1.0) % 2.0 - 1.0) * LENGTH;
let mut base_text = vec![b'|'; 1];
let mut rear_text = vec![];
let mut front_text = vec![];
let progress = progress / 2.0 % 1.0;

// 4PI ごとに波と凪が交互に来る関数
let curve = |x: f64| ((x + PI).cos() + 1.0) / 2.0 * ((x + PI) / 2.0).cos().ceil();

(0..LENGTH as usize)
.map(|i| {
(curve((i as f64 / LENGTH / 2.0 - progress) * 4.0 * PI) * 0xff as f64).ceil() as u8
})
.collect()
}

/// アルファと cos カーブを使った表現
/// ボツ
#[allow(unused)]
fn render_progress_alpha(th19: &Th19, text_renderer: *const c_void, progress: f64) {
let text = b"| |";
let x = 640;
let y = 160 + 32 * 11;
let mut rt = RenderingText::default();
rt.set_text(text);
rt.x = (x * th19.screen_width().unwrap() / 1280) as f32;
rt.y = (y * th19.screen_height().unwrap() / 960) as f32;
rt.color = 0xff000000;
rt.font_type = 8;
rt.horizontal_align = 0;
th19.render_text(text_renderer, &rt);

rt.color = 0xffffffff;
rt.font_type = 6;
th19.render_text(text_renderer, &rt);

for (i, &alpha) in progress_alphas(progress).iter().enumerate() {
let x = (650 - 200 + i * 20) as u32;

rt.set_text(b"-");
rt.x = (x * th19.screen_width().unwrap() / 1280) as f32;
rt.color = (0xff - alpha) as u32 * 0x1000000;
rt.font_type = 8;
th19.render_text(text_renderer, &rt);
rt.color |= 0x00ffffff;
rt.font_type = 6;
th19.render_text(text_renderer, &rt);

rt.set_text(b"#");
rt.x = (x * th19.screen_width().unwrap() / 1280) as f32;
rt.color = alpha as u32 * 0x1000000;
rt.font_type = 8;
th19.render_text(text_renderer, &rt);
rt.color |= 0x00ffffff;
rt.font_type = 6;
th19.render_text(text_renderer, &rt);
}
}

fn progress_text(progress: f64) -> Vec<u8> {
const BUFFER_TIME: f64 = 0.25;
const LENGTH: f64 = 20.0 * (1.0 + BUFFER_TIME);
let progress = ((progress / (1.0 + BUFFER_TIME) + 1.0) % 2.0 - 1.0) * LENGTH;
let mut progress_text = vec![];
let (progress, left_char, right_char, left_len) = if progress >= 0.0 {
(progress, b'#', b'-', progress as usize)
} else {
let progress = -progress;
(progress, b'-', b'#', LENGTH as usize - progress as usize)
};
let right_len = LENGTH as usize - left_len;
base_text.append(&mut vec![left_char; left_len]);
rear_text.append(&mut vec![b' '; left_len]);
front_text.append(&mut vec![b' '; left_len]);
if progress < 20.0 {
base_text.push(b' ');
rear_text.push(b'-');
front_text.push(b'#');
progress_text.append(&mut vec![left_char; left_len]);
if progress < LENGTH {
progress_text.push(b'#');
}
base_text.append(&mut vec![right_char; right_len]);
rear_text.append(&mut vec![b' '; right_len]);
front_text.append(&mut vec![b' '; right_len]);
base_text.push(b'|');
progress_text.append(&mut vec![right_char; right_len]);

let fraction = progress - progress.floor();
(base_text, rear_text, front_text, fraction)
let mut text = vec![b'['];
progress_text[0..20].iter().for_each(|&x| text.push(x));
text.push(b']');
text
}

fn render_room_name(th19: &Th19, text_renderer: *const c_void, room_name: &str) {
Expand Down Expand Up @@ -116,20 +167,8 @@ fn render_progress_item(th19: &Th19, text_renderer: *const c_void, alpha: u8, te
}

fn render_progress(th19: &Th19, text_renderer: *const c_void, progress: f64) {
let (base_text, rear_text, front_text, fraction) = progress_text(progress);
let base_text = progress_text(progress);
render_progress_item(th19, text_renderer, 0xff, &base_text);
render_progress_item(
th19,
text_renderer,
0xff - (0xff as f64 * fraction) as u8,
&rear_text,
);
render_progress_item(
th19,
text_renderer,
(0xff as f64 * fraction) as u8,
&front_text,
);
}

pub struct SharedRoom {
Expand Down

0 comments on commit cdb19c3

Please sign in to comment.