Skip to content

Commit 4ba0cad

Browse files
committed
almost got height previews working
1 parent e36bbad commit 4ba0cad

File tree

5 files changed

+48
-12
lines changed

5 files changed

+48
-12
lines changed

src/file_data.rs

+11
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,17 @@ impl Raw {
3232
Image(parser.process_input(&self.0, width))
3333
}
3434

35+
pub fn height_previews(&self, parser: ParserType, width: usize) -> Vec<Image> {
36+
let image = self.parse(parser, width);
37+
let mut previews = self
38+
.heights(parser.image_type(), width)
39+
.iter()
40+
.map(|h| image.as_row(*h as usize))
41+
.collect::<Vec<Image>>();
42+
previews.push(image.as_row(200));
43+
previews
44+
}
45+
3546
pub fn width_previews(&self, parser: ParserType) -> Vec<Image> {
3647
let mut previews = self
3748
.widths(parser.image_type())

src/image.rs

+2-5
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,8 @@ impl Image {
5858
}
5959
}
6060

61-
pub fn sprites(&self, height: usize) -> Vec<Image> {
62-
self.data()
63-
.chunks(height)
64-
.map(|chunk| Image(chunk.to_vec()))
65-
.collect()
61+
pub fn as_row(&self, tile_height: usize) -> Image {
62+
Image(concat_tiles(self.data(), tile_height))
6663
}
6764
}
6865

src/wasm/index.html

+12-3
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,15 @@
3939
});
4040

4141
class PreviewItem extends HTMLElement {
42-
constructor(src) {
42+
constructor(src, height=false) {
4343
super();
4444
const shadow = this.attachShadow({mode: "open"});
4545
const fragment = D.createRange().createContextualFragment('<h3></h3><img/>');
4646
const h3 = $('h3', fragment);
4747
this.img = $('img', fragment);
4848

4949
this.img.onload = function () {
50-
h3.innerText = this.width;
50+
h3.innerText = height ? this.height : this.width;
5151
}
5252
this.img.src = src;
5353
this.addEventListener('click', this.onClick);
@@ -58,7 +58,16 @@
5858
let parser = $('h2', this.parentElement).innerText;
5959
$('#width').value = width;
6060
$('#parser').value = parser
61-
//alert(window.wasmBindings.tile_previews(parser, width));
61+
62+
let wrapper = $('preview-wrapper');
63+
wrapper.textContent = "";
64+
const container = D.createElement("preview-area");
65+
container.className = "rows";
66+
container.innerHTML = `<h2>heights</h2>`
67+
68+
let images = window.wasmBindings.tile_previews($('#file-input').data, parser, parseInt(width));
69+
images.forEach(src => container.appendChild(new PreviewItem(src, true)));
70+
wrapper.appendChild(container);
6271
}
6372
}
6473
customElements.define("preview-item", PreviewItem);

src/wasm/main.rs

+16-4
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,22 @@ pub fn previews(data: &[u8]) -> JsValue {
3434
JsValue::from_serde(&hm).unwrap()
3535
}
3636

37-
// #[wasm_bindgen]
38-
// pub fn tile_previews(data: &[u8], parser: String, width: usize) -> String {
39-
// format!("{} {}", parser, width)
40-
// }
37+
#[wasm_bindgen]
38+
pub fn tile_previews(data: &[u8], parser: String, width: usize) -> Vec<String> {
39+
let parser = ParserType::type_str(&parser);
40+
let palette = parser.image_type().default_color_palette();
41+
let file_data = Raw::new(data);
42+
file_data
43+
.height_previews(parser, width)
44+
.iter()
45+
.map(|p| {
46+
format!(
47+
"data:application/png;base64,{}",
48+
STANDARD.encode(png::write2(p.data(), palette.clone()))
49+
)
50+
})
51+
.collect()
52+
}
4153

4254
pub fn preview(data: &Raw, parser: ParserType) -> Vec<String> {
4355
let palette = parser.image_type().default_color_palette();

src/wasm/styles.css

+7
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,13 @@ preview-area {
5050
height: 300px;
5151
overflow-y: scroll;
5252
}
53+
preview-area.rows {
54+
flex-flow: column nowrap;
55+
height: auto;
56+
max-width: 300px;
57+
overflow-x: scroll;
58+
overflow-y: auto;
59+
}
5360
preview-item {
5461
margin: 1rem;
5562
padding: .5rem .5rem .8rem .5rem;

0 commit comments

Comments
 (0)