marble-machinations/src/lib.rs

74 lines
1.6 KiB
Rust

pub mod blueprint;
pub mod board;
pub mod config;
pub mod editor;
pub mod input;
pub mod level;
pub mod marble_engine;
pub mod solution;
pub mod theme;
pub mod ui;
pub mod util;
use std::fs;
use arboard::Clipboard;
use config::Config;
use input::ActionId;
use raylib::{texture::Texture2D, RaylibHandle};
use util::{userdata_dir, MouseInput, Textures};
// use util::MouseInput;
pub const CONFIG_FILE_NAME: &str = "config.json";
pub struct Globals {
pub clipboard: Option<Clipboard>,
pub config: Config,
textures: Textures,
pub mouse: MouseInput,
}
impl Globals {
pub fn new(rl: &mut RaylibHandle, thread: &raylib::prelude::RaylibThread) -> Self {
let mut textures = Textures::default();
textures.load_dir("assets", rl, thread);
textures.load_dir("assets/tiles", rl, thread);
textures.load_dir("assets/digits", rl, thread);
let config_path = userdata_dir().join(CONFIG_FILE_NAME);
let config = fs::read_to_string(config_path)
.ok()
.and_then(|s| serde_json::from_str(&s).ok())
.unwrap_or_default();
Self {
clipboard: Clipboard::new()
.map_err(|e| eprintln!("System clipboard error: {e}"))
.ok(),
config,
textures,
mouse: MouseInput::default(),
}
}
pub fn update(&mut self, rl: &RaylibHandle) {
self.config.input.update(rl);
self.mouse.update(rl);
}
pub fn is_pressed(&self, action: ActionId) -> bool {
self.config.input.is_pressed(action)
}
pub fn is_held(&self, action: ActionId) -> bool {
self.config.input.is_held(action)
}
pub fn is_released(&self, action: ActionId) -> bool {
self.config.input.is_released(action)
}
pub fn get_tex(&self, name: &str) -> &Texture2D {
self.textures.get(name)
}
}