Refactor game log to allow coloring of messages
This commit is contained in:
parent
7e3ceab2a2
commit
c3e51d7b06
@ -1,24 +1,18 @@
|
|||||||
/// A struct to hold the player's game log.
|
//! Game Log
|
||||||
#[derive(Default)]
|
//!
|
||||||
pub struct GameLog {
|
//! Where the exploits of the current game are recorded
|
||||||
pub entries: Vec<String>,
|
mod builder;
|
||||||
}
|
mod logstore;
|
||||||
|
|
||||||
impl GameLog {
|
use ::rltk::RGB;
|
||||||
/// Convenience constructor that adds the first
|
pub use builder::*;
|
||||||
/// entry to the game log.
|
use logstore::*;
|
||||||
pub fn new<S: ToString>(first_entry: S) -> Self {
|
pub use logstore::{clear_log, log_display};
|
||||||
let mut log = GameLog::default();
|
|
||||||
log.append(first_entry);
|
/// A section of colored text for the game log
|
||||||
|
pub struct LogFragment {
|
||||||
log
|
/// The color of the text
|
||||||
}
|
pub color: RGB,
|
||||||
|
/// The text fragment
|
||||||
/// Convenience method for adding an entry to the game log.
|
pub text: String,
|
||||||
/// Generic on the [`ToString`] trait so that [`&str`], [`String`],
|
|
||||||
/// and `&String` types, all work without more type juggling
|
|
||||||
/// at the call site.
|
|
||||||
pub fn append<S: ToString>(&mut self, s: S) {
|
|
||||||
self.entries.push(s.to_string());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
63
src/game_log/builder.rs
Normal file
63
src/game_log/builder.rs
Normal file
@ -0,0 +1,63 @@
|
|||||||
|
use ::rltk::prelude::*;
|
||||||
|
|
||||||
|
use super::{append_entry, LogFragment};
|
||||||
|
use crate::colors;
|
||||||
|
|
||||||
|
/// The game log builder - builds one line at a time
|
||||||
|
pub struct Logger {
|
||||||
|
current_color: RGB,
|
||||||
|
fragments: Vec<LogFragment>,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Convenience method to create a new [`Logger`] and
|
||||||
|
/// immediately append text
|
||||||
|
pub fn line<T: ToString>(text: T) -> Logger {
|
||||||
|
Logger::new().append(text)
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Logger {
|
||||||
|
pub fn new() -> Self {
|
||||||
|
Logger {
|
||||||
|
current_color: colors::WHITE,
|
||||||
|
fragments: Vec::new(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Sets the color of text to be added to the current line
|
||||||
|
pub fn color(mut self, color: RGB) -> Self {
|
||||||
|
self.current_color = color;
|
||||||
|
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The original [`color`](Logger::color) method, using [`rltk`] color constants
|
||||||
|
#[allow(dead_code)]
|
||||||
|
pub fn rltk_color(mut self, color: (u8, u8, u8)) -> Self {
|
||||||
|
self.current_color = RGB::named(color);
|
||||||
|
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Appends text to the current line of the game log.
|
||||||
|
/// Generic on the [`ToString`] trait so that [`&str`], [`String`],
|
||||||
|
/// and `&String` types, all work without more type juggling
|
||||||
|
/// at the call site.
|
||||||
|
pub fn append<T: ToString>(mut self, text: T) -> Self {
|
||||||
|
self.fragments.push(LogFragment {
|
||||||
|
color: self.current_color,
|
||||||
|
text: text.to_string(),
|
||||||
|
});
|
||||||
|
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Convenience method to append text with a new color
|
||||||
|
pub fn append_color<T: ToString>(mut self, color: RGB, text: T) -> Self {
|
||||||
|
self.color(color).append(text)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Append the current line to the log output
|
||||||
|
pub fn log(self) {
|
||||||
|
append_entry(self.fragments);
|
||||||
|
}
|
||||||
|
}
|
35
src/game_log/logstore.rs
Normal file
35
src/game_log/logstore.rs
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
use std::sync::Mutex;
|
||||||
|
|
||||||
|
use ::rltk::prelude::*;
|
||||||
|
|
||||||
|
use super::LogFragment;
|
||||||
|
|
||||||
|
lazy_static! {
|
||||||
|
static ref LOG: Mutex<Vec<Vec<LogFragment>>> = Mutex::new(Vec::new());
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn append_fragment(fragment: LogFragment) {
|
||||||
|
LOG.lock().unwrap().push(vec![fragment]);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn append_entry(fragments: Vec<LogFragment>) {
|
||||||
|
LOG.lock().unwrap().push(fragments);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn clear_log() {
|
||||||
|
LOG.lock().unwrap().clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn log_display() -> TextBuilder {
|
||||||
|
let mut buf = TextBuilder::empty();
|
||||||
|
|
||||||
|
LOG.lock().unwrap().iter().rev().take(12).for_each(|log| {
|
||||||
|
log.iter().for_each(|frag| {
|
||||||
|
buf.fg(frag.color);
|
||||||
|
buf.line_wrap(&frag.text);
|
||||||
|
});
|
||||||
|
buf.ln();
|
||||||
|
});
|
||||||
|
|
||||||
|
buf
|
||||||
|
}
|
19
src/gui.rs
19
src/gui.rs
@ -4,6 +4,7 @@ mod tooltip;
|
|||||||
|
|
||||||
use std::cmp::Ordering;
|
use std::cmp::Ordering;
|
||||||
|
|
||||||
|
use ::rltk::prelude::*;
|
||||||
use ::rltk::{Point, Rltk, RGB};
|
use ::rltk::{Point, Rltk, RGB};
|
||||||
use ::specs::prelude::*;
|
use ::specs::prelude::*;
|
||||||
pub use enums::*;
|
pub use enums::*;
|
||||||
@ -15,8 +16,7 @@ use crate::components::{
|
|||||||
InBackpack, KnownSpells, MagicItem, MagicItemClass, Name, ObfuscatedName, Pools, StatusEffect,
|
InBackpack, KnownSpells, MagicItem, MagicItemClass, Name, ObfuscatedName, Pools, StatusEffect,
|
||||||
Viewshed, Weapon,
|
Viewshed, Weapon,
|
||||||
};
|
};
|
||||||
use crate::game_log::GameLog;
|
use crate::{camera, colors, game_log, Map, MasterDungeonMap, State};
|
||||||
use crate::{camera, colors, Map, MasterDungeonMap, State};
|
|
||||||
|
|
||||||
pub fn get_item_color(ecs: &World, item: Entity) -> RGB {
|
pub fn get_item_color(ecs: &World, item: Entity) -> RGB {
|
||||||
let dm = ecs.fetch::<crate::map::MasterDungeonMap>();
|
let dm = ecs.fetch::<crate::map::MasterDungeonMap>();
|
||||||
@ -77,8 +77,6 @@ pub fn draw_hollow_box(
|
|||||||
fg: RGB,
|
fg: RGB,
|
||||||
bg: RGB,
|
bg: RGB,
|
||||||
) {
|
) {
|
||||||
use rltk::to_cp437;
|
|
||||||
|
|
||||||
console.set(sx, sy, fg, bg, to_cp437('┌'));
|
console.set(sx, sy, fg, bg, to_cp437('┌'));
|
||||||
console.set(sx + width, sy, fg, bg, to_cp437('┐'));
|
console.set(sx + width, sy, fg, bg, to_cp437('┐'));
|
||||||
console.set(sx, sy + height, fg, bg, to_cp437('└'));
|
console.set(sx, sy + height, fg, bg, to_cp437('└'));
|
||||||
@ -95,8 +93,6 @@ pub fn draw_hollow_box(
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn draw_ui(ecs: &World, ctx: &mut Rltk) {
|
pub fn draw_ui(ecs: &World, ctx: &mut Rltk) {
|
||||||
use rltk::to_cp437;
|
|
||||||
|
|
||||||
draw_hollow_box(ctx, 0, 0, 79, 59, colors::BOX_GRAY, colors::BLACK); // Overall box
|
draw_hollow_box(ctx, 0, 0, 79, 59, colors::BOX_GRAY, colors::BLACK); // Overall box
|
||||||
draw_hollow_box(ctx, 0, 0, 49, 45, colors::BOX_GRAY, colors::BLACK); // Map box
|
draw_hollow_box(ctx, 0, 0, 49, 45, colors::BOX_GRAY, colors::BLACK); // Map box
|
||||||
draw_hollow_box(ctx, 0, 45, 79, 14, colors::BOX_GRAY, colors::BLACK); // Log box
|
draw_hollow_box(ctx, 0, 45, 79, 14, colors::BOX_GRAY, colors::BLACK); // Log box
|
||||||
@ -329,14 +325,9 @@ pub fn draw_ui(ecs: &World, ctx: &mut Rltk) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Draw the log
|
// Draw the log
|
||||||
let log = ecs.fetch::<GameLog>();
|
let mut block = TextBlock::new(1, 46, 79, 58);
|
||||||
let mut y = 46;
|
block.print(&game_log::log_display());
|
||||||
for s in log.entries.iter().rev() {
|
block.render(&mut ::rltk::BACKEND_INTERNAL.lock().consoles[0].console);
|
||||||
if y < 59 {
|
|
||||||
ctx.print(2, y, s);
|
|
||||||
}
|
|
||||||
y += 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
draw_tooltips(ecs, ctx);
|
draw_tooltips(ecs, ctx);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user