organicomplex 0.7.0

Interactive complex-valued cellular automaton on 2D and 3D grids in search of that stuff - emergence, open-endedness, organicity etc.
use crate::{
	base::RGB,
	sys::Key,
	fontset::CFont
};

use super::{
	ERR_ALREADY_SET,
	ERR_NOT_SET_CANNOT_RUN,
	Context,
	Master
};

const MID: &str = "Telemetry";
const COLOR: RGB = RGB{r: 0xFF, g: 0xFF, b: 0xFF};
const SHADE: u8 = 0xE0;
const MARGIN: i32 = 0;

pub enum MTelemetry {
	Unset,
	Set {
		show: bool
	}
}

impl MTelemetry {
	pub fn new() -> Box<Self> {
		Box::new(Self::Unset)
	}
}

impl Master for MTelemetry {
	fn id(&self) -> String {
		String::from(MID)
	}

	fn start(&mut self, _ctx: &mut Context) -> Result<(), String> {
		match self {
			Self::Unset => {
				let show = false;
				*self = Self::Set{show};
				Ok(())
			},
			Self::Set{..} => Err(format!("{} {}", MID, ERR_ALREADY_SET))
		}
	}

    fn run(&mut self, Context{sys, fontset, state, ..}: &mut Context) -> Result<(), String> {
		match self {
		 	&mut Self::Set{ref mut show, ..} => {
				if !sys.draw_locked() { // only when new frame is required, to decrease load
					if *show {
						let font = fontset.get(CFont::System);
						let fh = font.height();
						let mut y: i32 = -(sys.height() >> 1);
						let x = -(sys.width() >> 1);
						sys.draw_text(font, format!("FPS = {:.3}", sys.fps()), 0, COLOR, SHADE, MARGIN, x, y).unwrap_or(()); y += fh;
						sys.draw_text(font, format!("Playtime: {:.3}", (state.playtime as f64) * 1e-6), 0, COLOR, SHADE, MARGIN, x, y).unwrap_or(());
					}
				}

				// Process input regardless of draw lock

		        if sys.poll_key(Key::T) {
		            *show = !(*show);
		        }

				Ok(())
			},

			Self::Unset => Err(format!("{} {}", MID, ERR_NOT_SET_CANNOT_RUN))
		}
    }

}