finalised gamelog
This commit is contained in:
parent
8de3648bae
commit
e258767405
10 changed files with 73 additions and 224 deletions
|
|
@ -1,91 +1,60 @@
|
|||
use super::{ events, LogFragment, Logger };
|
||||
use bracket_lib::prelude::*;
|
||||
use std::sync::Mutex;
|
||||
use std::collections::BTreeMap;
|
||||
use notan::prelude::*;
|
||||
use notan::text::CreateText;
|
||||
use crate::consts::{ TILESIZE, FONTSIZE };
|
||||
|
||||
lazy_static! {
|
||||
pub static ref LOG: Mutex<Vec<Vec<LogFragment>>> = Mutex::new(Vec::new());
|
||||
pub static ref LOG: Mutex<BTreeMap<i32, Vec<LogFragment>>> = Mutex::new(BTreeMap::new());
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub fn append_fragment(fragment: LogFragment) {
|
||||
LOG.lock().unwrap().push(vec![fragment]);
|
||||
pub fn render_log(gfx: &mut Graphics, font: ¬an::draw::Font, pos: &(f32, f32), width: f32) {
|
||||
let mut text = gfx.create_text();
|
||||
let log = LOG.lock().unwrap();
|
||||
let latest: Vec<_> = log.iter().rev().take(5).collect();
|
||||
let mut init = false;
|
||||
let mut y = pos.1;
|
||||
for (_, entries) in latest {
|
||||
let mut written_on_line = false;
|
||||
for frag in entries.iter() {
|
||||
if !written_on_line {
|
||||
text.add(&frag.text)
|
||||
.font(font)
|
||||
.position(pos.0, y)
|
||||
.size(FONTSIZE)
|
||||
.max_width(width)
|
||||
.color(Color::from_rgb(frag.colour.r, frag.colour.g, frag.colour.b))
|
||||
.v_align_bottom();
|
||||
written_on_line = true;
|
||||
init = true;
|
||||
} else {
|
||||
text.chain(&frag.text)
|
||||
.color(Color::from_rgb(frag.colour.r, frag.colour.g, frag.colour.b))
|
||||
.size(FONTSIZE);
|
||||
}
|
||||
}
|
||||
if init {
|
||||
y = text.last_bounds().min_y();
|
||||
}
|
||||
}
|
||||
gfx.render(&text);
|
||||
}
|
||||
|
||||
pub fn append_entry(fragments: Vec<LogFragment>) {
|
||||
LOG.lock().unwrap().push(fragments);
|
||||
pub fn append_entry(turn: i32, fragments: Vec<LogFragment>) {
|
||||
let mut log = LOG.lock().unwrap();
|
||||
if let Some(existing) = log.get_mut(&turn) {
|
||||
existing.extend(fragments);
|
||||
} else {
|
||||
log.insert(turn, fragments);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn clear_log() {
|
||||
LOG.lock().unwrap().clear();
|
||||
}
|
||||
|
||||
pub fn print_log(
|
||||
console: &mut Box<dyn Console>,
|
||||
pos: Point,
|
||||
_descending: bool,
|
||||
len: usize,
|
||||
maximum_len: i32
|
||||
) {
|
||||
let mut y = pos.y;
|
||||
let mut x = pos.x;
|
||||
// Reverse the log, take the number we want to show, and iterate through them
|
||||
LOG.lock()
|
||||
.unwrap()
|
||||
.iter()
|
||||
.rev()
|
||||
.take(len)
|
||||
.for_each(|log| {
|
||||
let mut entry_len = -2;
|
||||
// Iterate through each message fragment, and get the total length
|
||||
// in lines, by adding the length of every fragment and dividing it
|
||||
// by the maximum length we desire. Then shuffle our start-y by that much.
|
||||
log.iter().for_each(|frag| {
|
||||
entry_len += frag.text.len() as i32;
|
||||
});
|
||||
let lines = entry_len / maximum_len;
|
||||
y -= lines;
|
||||
let mut i = 0;
|
||||
log.iter().for_each(|frag| {
|
||||
// Split every fragment up into single characters.
|
||||
let parts = frag.text.split("");
|
||||
for part in parts {
|
||||
// This is an extremely hacky solution to a problem I don't understand yet.
|
||||
// -- without this, the lines *here* and the line count *above* wont match.
|
||||
if part == "" || part == "\\" {
|
||||
continue;
|
||||
}
|
||||
if i > entry_len {
|
||||
break;
|
||||
}
|
||||
i += 1;
|
||||
if x + (part.len() as i32) > pos.x + maximum_len {
|
||||
if y > pos.y - (len as i32) {
|
||||
console.print(x, y, "-");
|
||||
}
|
||||
y += 1;
|
||||
x = pos.x;
|
||||
}
|
||||
// Stay within bounds
|
||||
if y > pos.y - (len as i32) {
|
||||
console.print_color(
|
||||
x,
|
||||
y,
|
||||
frag.colour.into(),
|
||||
RGB::named(BLACK).into(),
|
||||
part
|
||||
);
|
||||
}
|
||||
x += part.len() as i32;
|
||||
}
|
||||
});
|
||||
// Take away one from the y-axis, because we want to start each entry
|
||||
// on a new line, and go up an additional amount depending on how many
|
||||
// lines our *previous* entry took.
|
||||
y -= 1 + lines;
|
||||
x = pos.x;
|
||||
});
|
||||
}
|
||||
|
||||
pub fn setup_log() {
|
||||
clear_log();
|
||||
events::clear_events();
|
||||
|
|
@ -98,11 +67,11 @@ pub fn setup_log() {
|
|||
.log();
|
||||
}
|
||||
|
||||
pub fn clone_log() -> Vec<Vec<crate::gamelog::LogFragment>> {
|
||||
pub fn clone_log() -> BTreeMap<i32, Vec<LogFragment>> {
|
||||
return LOG.lock().unwrap().clone();
|
||||
}
|
||||
|
||||
pub fn restore_log(log: &mut Vec<Vec<crate::gamelog::LogFragment>>) {
|
||||
pub fn restore_log(log: &mut BTreeMap<i32, Vec<LogFragment>>) {
|
||||
LOG.lock().unwrap().clear();
|
||||
LOG.lock().unwrap().append(log);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue