Change model size without data loss

This commit is contained in:
daa 2018-01-13 21:32:21 +03:00
parent cc082ab6f7
commit 2fb6b7a556
3 changed files with 49 additions and 19 deletions

View File

@ -30,7 +30,18 @@ impl Level {
pub fn append_line(&mut self,
content: &Vec<Vec<(HashMap<String, Value>, String)>>) {
// TODO: implement
let lines: Vec<Vec<(Option<Attrs>, Vec<char>)>> = content
.iter()
.map(|line_chars| {
line_chars
.iter()
.map(|c| {
(Some(Attrs::from_value_map(&c.0)), c.1.chars().collect())
})
.collect()
})
.collect();
//TODO: implement
}
pub fn from_multiline_content(
@ -64,7 +75,7 @@ impl Level {
..
} = render_state.font_ctx.cell_metrics();
let max_width_chars = (max_width as f64 / char_width) as u64;
let max_width_chars = (max_width as f64 / char_width) as usize;
let mut model_layout = ModelLayout::new();
let (columns, rows) = model_layout.layout(lines, max_width_chars);

View File

@ -121,6 +121,16 @@ impl UiModel {
self.right = right as usize;
}
/// Copy rows from 0 to to_row, col from 0 self.columns
///
/// Don't do any validation!
pub fn copy_rows(&self, target: &mut UiModel, to_row: usize) {
for (row_idx, line) in self.model[0..to_row + 1].iter().enumerate() {
let mut target_row = &mut target.model[row_idx];
line.copy_to(target_row, 0, self.columns);
}
}
#[inline]
fn copy_row(&mut self, target_row: i64, offset: i64, left_col: usize, right_col: usize) {
debug_assert_ne!(0, offset);

View File

@ -1,15 +1,31 @@
use ui_model::{UiModel, Attrs};
use ui_model::{Attrs, UiModel};
pub struct ModelLayout {
pub model: UiModel,
rows_filled: usize,
}
impl ModelLayout {
const COLUMNS_STEP: u64 = 50;
const ROWS_STEP: u64 = 10;
const COLUMNS_STEP: usize = 50;
const ROWS_STEP: usize = 10;
pub fn new() -> Self {
ModelLayout { model: UiModel::new(ModelLayout::ROWS_STEP, ModelLayout::COLUMNS_STEP) }
ModelLayout {
model: UiModel::new(ModelLayout::ROWS_STEP as u64, ModelLayout::COLUMNS_STEP as u64),
rows_filled: 0,
}
}
fn check_model_size(&mut self, rows: usize, columns: usize) {
if rows > self.model.rows || columns > self.model.columns {
let model_cols =
((columns / ModelLayout::COLUMNS_STEP) + 1) * ModelLayout::COLUMNS_STEP;
let model_rows = ((rows / ModelLayout::ROWS_STEP) + 1) * ModelLayout::ROWS_STEP;
let mut model = UiModel::new(model_rows as u64, model_cols as u64);
self.model.copy_rows(&mut model, self.rows_filled);
}
}
/// Wrap all lines into model
@ -18,19 +34,12 @@ impl ModelLayout {
pub fn layout(
&mut self,
lines: Vec<Vec<(Option<Attrs>, Vec<char>)>>,
max_columns: u64,
) -> (u64, u64) {
max_columns: usize,
) -> (usize, usize) {
let rows = ModelLayout::count_lines(&lines, max_columns);
if rows as usize > self.model.rows || max_columns as usize > self.model.columns {
let model_cols = ((max_columns / ModelLayout::COLUMNS_STEP) + 1) *
ModelLayout::COLUMNS_STEP;
let model_rows = ((rows as u64 / ModelLayout::ROWS_STEP) + 1) * ModelLayout::ROWS_STEP;
self.model = UiModel::new(model_rows, model_cols);
}
self.check_model_size(rows, max_columns);
self.rows_filled = rows;
let mut max_col_idx = 0;
let mut col_idx = 0;
@ -59,12 +68,12 @@ impl ModelLayout {
(max_col_idx + 1, rows)
}
fn count_lines(lines: &Vec<Vec<(Option<Attrs>, Vec<char>)>>, max_columns: u64) -> u64 {
fn count_lines(lines: &Vec<Vec<(Option<Attrs>, Vec<char>)>>, max_columns: usize) -> usize {
let mut row_count = 0;
for line in lines {
let len: usize = line.iter().map(|c| c.1.len()).sum();
row_count += len as u64 / (max_columns + 1) + 1;
row_count += len / (max_columns + 1) + 1;
}
row_count