implement font and text direction; call std and mock directly instead of importing

This commit is contained in:
Max Bradbury 2020-04-18 10:45:01 +01:00
parent c5604f2964
commit cc0780b9cd
4 changed files with 77 additions and 14 deletions

View File

@ -1,5 +1,17 @@
use crate::{Avatar, Dialogue, Ending, Item, Palette, Room, Sprite, Tile, Variable, mock, optional_data_line}; use crate::{
use std::fs; Avatar,
Dialogue,
Ending,
Font,
Item,
Palette,
Room,
Sprite,
TextDirection,
Tile,
Variable,
optional_data_line
};
#[derive(Debug, PartialEq)] #[derive(Debug, PartialEq)]
pub struct Game { pub struct Game {
@ -7,6 +19,9 @@ pub struct Game {
pub major_version: u8, pub major_version: u8,
pub minor_version: u8, pub minor_version: u8,
pub room_format: u8, // this is "0 = non-comma separated, 1 = comma separated" apparently pub room_format: u8, // this is "0 = non-comma separated, 1 = comma separated" apparently
pub font: Font,
pub custom_font: Option<String>, // used if font is Font::Custom
pub text_direction: TextDirection,
pub palettes: Vec<Palette>, pub palettes: Vec<Palette>,
pub rooms: Vec<Room>, pub rooms: Vec<Room>,
pub tiles: Vec<Tile>, pub tiles: Vec<Tile>,
@ -74,6 +89,9 @@ impl From<String> for Game {
let mut major_version: u8 = 1; let mut major_version: u8 = 1;
let mut minor_version: u8 = 0; let mut minor_version: u8 = 0;
let mut room_format: u8 = 1; let mut room_format: u8 = 1;
let mut font = Font::AsciiSmall;
let mut custom_font = None;
let mut text_direction = TextDirection::LeftToRight;
let mut palettes: Vec<Palette> = Vec::new(); let mut palettes: Vec<Palette> = Vec::new();
let mut rooms: Vec<Room> = Vec::new(); let mut rooms: Vec<Room> = Vec::new();
let mut tiles: Vec<Tile> = Vec::new(); let mut tiles: Vec<Tile> = Vec::new();
@ -91,6 +109,16 @@ impl From<String> for Game {
minor_version = parts.1; minor_version = parts.1;
} else if segment.starts_with("! ROOM_FORMAT") { } else if segment.starts_with("! ROOM_FORMAT") {
room_format = segment.replace("! ROOM_FORMAT ", "").parse().unwrap(); room_format = segment.replace("! ROOM_FORMAT ", "").parse().unwrap();
} else if segment.starts_with("DEFAULT_FONT") {
let segment = segment.replace("DEFAULT_FONT ", "");
font = Font::from(&segment);
if font == Font::Custom {
custom_font = Some(segment.to_string());
}
} else if segment.trim() == "TEXT_DIRECTION RTL".to_string() {
text_direction = TextDirection::RightToLeft;
} else if segment.starts_with("PAL") { } else if segment.starts_with("PAL") {
palettes.push(Palette::from(segment)); palettes.push(Palette::from(segment));
} else if segment.starts_with("ROOM") { } else if segment.starts_with("ROOM") {
@ -114,6 +142,9 @@ impl From<String> for Game {
major_version, major_version,
minor_version, minor_version,
room_format, room_format,
font,
custom_font,
text_direction,
palettes, palettes,
rooms, rooms,
tiles, tiles,
@ -231,34 +262,34 @@ fn test_game_from_string() {
include_str!["test-resources/default.bitsy"].to_string() include_str!["test-resources/default.bitsy"].to_string()
); );
let expected = mock::game_default(); let expected = crate::mock::game_default();
assert_eq!(output, expected); assert_eq!(output, expected);
} }
#[test] #[test]
fn test_game_to_string() { fn test_game_to_string() {
let output = mock::game_default().to_string(); let output = crate::mock::game_default().to_string();
let expected = include_str!["test-resources/default.bitsy"].to_string(); let expected = include_str!["test-resources/default.bitsy"].to_string();
assert_eq!(output, expected); assert_eq!(output, expected);
} }
#[test] #[test]
fn test_tile_ids() { fn test_tile_ids() {
assert_eq!(mock::game_default().tile_ids(), vec![10]); assert_eq!(crate::mock::game_default().tile_ids(), vec![10]);
} }
#[test] #[test]
fn test_new_tile_id() { fn test_new_tile_id() {
// default tile has an id of 10 ("a"), so 0 is available // default tile has an id of 10 ("a"), so 0 is available
assert_eq!(mock::game_default().new_tile_id(), 0); assert_eq!(crate::mock::game_default().new_tile_id(), 0);
let mut game = mock::game_default(); let mut game = crate::mock::game_default();
let mut tiles : Vec<Tile> = Vec::new(); let mut tiles : Vec<Tile> = Vec::new();
for n in 0..9 { for n in 0..9 {
if n != 4 { if n != 4 {
let mut new_tile = mock::tile_default(); let mut new_tile = crate::mock::tile_default();
new_tile.id = n; new_tile.id = n;
tiles.push(new_tile); tiles.push(new_tile);
} }
@ -270,7 +301,7 @@ fn test_new_tile_id() {
// fill in the space created above, and test that tile IDs get sorted // fill in the space created above, and test that tile IDs get sorted
let mut new_tile = mock::tile_default(); let mut new_tile = crate::mock::tile_default();
new_tile.id = 4; new_tile.id = 4;
game.tiles.push(new_tile); game.tiles.push(new_tile);
@ -279,11 +310,11 @@ fn test_new_tile_id() {
#[test] #[test]
fn test_add_tile() { fn test_add_tile() {
let mut game = mock::game_default(); let mut game = crate::mock::game_default();
let new_id = game.add_tile(mock::tile_default()); let new_id = game.add_tile(crate::mock::tile_default());
assert_eq!(new_id, 0); assert_eq!(new_id, 0);
assert_eq!(game.tiles.len(), 2); assert_eq!(game.tiles.len(), 2);
let new_id = game.add_tile(mock::tile_default()); let new_id = game.add_tile(crate::mock::tile_default());
assert_eq!(new_id, 1); assert_eq!(new_id, 1);
assert_eq!(game.tiles.len(), 3); assert_eq!(game.tiles.len(), 3);
} }
@ -315,7 +346,7 @@ fn test_bitsy_omnibus() {
let mut passes = 0; let mut passes = 0;
let mut skips = 0; let mut skips = 0;
for file in fs::read_dir("src/test-resources/omnibus").unwrap() { for file in std::fs::read_dir("src/test-resources/omnibus").unwrap() {
let path = file.unwrap().path(); let path = file.unwrap().path();
let nice_name = format!("{}", path.display()); let nice_name = format!("{}", path.display());
@ -327,7 +358,7 @@ fn test_bitsy_omnibus() {
} }
println!("\nTesting: {}...", path.display()); println!("\nTesting: {}...", path.display());
let game_data = fs::read_to_string(path).unwrap(); let game_data = std::fs::read_to_string(path).unwrap();
let game = Game::from(game_data.clone()); let game = Game::from(game_data.clone());
assert_eq!(game.to_string().trim(), game_data.trim()); assert_eq!(game.to_string().trim(), game_data.trim());
passes += 1; passes += 1;

View File

@ -5,6 +5,7 @@ pub mod colour;
pub mod dialogue; pub mod dialogue;
pub mod ending; pub mod ending;
pub mod exit; pub mod exit;
pub mod text;
pub mod game; pub mod game;
pub mod palette; pub mod palette;
pub mod image; pub mod image;
@ -21,6 +22,7 @@ use colour::Colour;
use dialogue::Dialogue; use dialogue::Dialogue;
use ending::Ending; use ending::Ending;
use exit::{Transition, Exit}; use exit::{Transition, Exit};
use text::{Font, TextDirection};
use game::Game; use game::Game;
use palette::Palette; use palette::Palette;
use image::Image; use image::Image;

View File

@ -191,6 +191,9 @@ pub fn game_default() -> Game {
major_version: 6, major_version: 6,
minor_version: 5, minor_version: 5,
room_format: 1, room_format: 1,
font: Font::AsciiSmall,
custom_font: None,
text_direction: TextDirection::LeftToRight,
palettes: vec![ palettes: vec![
Palette { Palette {
id: 0, id: 0,

27
src/text.rs Normal file
View File

@ -0,0 +1,27 @@
#[derive(Debug, Eq, PartialEq)]
pub enum Font {
AsciiSmall, // default - does not appear in game data
UnicodeEuropeanSmall,
UnicodeEuropeanLarge,
UnicodeAsian,
Arabic,
Custom,
}
impl Font {
pub(crate) fn from(str: &str) -> Font {
match str {
"unicode_european_small" => Font::UnicodeEuropeanSmall,
"unicode_european_large" => Font::UnicodeEuropeanLarge,
"unicode_asian" => Font::UnicodeAsian,
"arabic" => Font::Arabic,
_ => Font::Custom,
}
}
}
#[derive(Debug, Eq, PartialEq)]
pub enum TextDirection {
LeftToRight, // default
RightToLeft,
}