Skip to content

Commit

Permalink
the beginnings of preview/wizard mode
Browse files Browse the repository at this point in the history
  • Loading branch information
knzai committed Aug 4, 2024
1 parent f639381 commit 8536ab9
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 22 deletions.
7 changes: 7 additions & 0 deletions src/file_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,11 @@ impl Raw {
pub fn parse(&self, parser: ParserType, width: usize) -> Image {
Image(parser.process_input(&self.0, width))
}

pub fn previews(&self) -> Vec<Image> {
self.widths(ImageType::CGA)
.iter()
.map(|w| Image(ParserType::CGA.process_input(&self.0, *w as usize)))
.collect()
}
}
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ impl ImageType {
pub fn widths(&self, byte_count: usize) -> Vec<i64> {
factor(self.pixel_count(byte_count).try_into().unwrap())
.into_iter()
.filter(|&x| x < 80)
.filter(|&x| x > 4 && x < 80)
.collect()
}
}
52 changes: 38 additions & 14 deletions src/wasm/image.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
use base64::{engine::general_purpose::STANDARD, Engine};

use web_sys::HtmlInputElement;
use web_sys::{HtmlElement, HtmlInputElement};
use yew::{html, Callback, Component, Context, Event, Html, Properties, SubmitEvent, TargetCast};

use crate::color::palette::palette_from_abbr;
use crate::file_data;
use crate::image::tile;
use crate::parser::ParserType;
use crate::png;
use crate::{file_data, png};

use crate::wasm::FileUpload;

Expand Down Expand Up @@ -42,6 +41,28 @@ impl ImageComponent {
};
format!("data:application/png;base64,{}", STANDARD.encode(data))
}

pub fn previews(&self, file: &FileUpload) -> Html {
if file.mime_type.contains("image") {
"".into()
} else {
let file_data = file_data::Raw::new(&file.data);
file_data
.previews()
.iter()
.map(|p| {
let palette = palette_from_abbr("cga0");
let mut bytes: Vec<u8> = Vec::new();

let _ = png::write_to(&mut bytes, tile(p.data(), self.height), palette.clone());
let src = format!("data:application/png;base64,{}", STANDARD.encode(bytes));
html! {
<img src={ src } />
}
})
.collect()
}
}
}

impl Component for ImageComponent {
Expand Down Expand Up @@ -76,18 +97,21 @@ impl Component for ImageComponent {
let file = &ctx.props().file;

html! {
<div class="preview-tile ">
<form onsubmit={noop}>
<label for="width">{"[Tile] Width"}</label>
<input name="width" type="number" value={self.width.to_string()} onchange={ctx.link().callback(Msg::Width)} />
<label for="height">{"[Tile] Height"}</label>
<input name="height" type="number" value={self.height.to_string()} onchange={ctx.link().callback(Msg::Height)} />
</form>
<p class="preview-name">{ file.name.to_string() }</p>
<div class=".preview-media">
<img src={ self.src(file) } />
<>
<div class="preview-tile ">
<div class=".preview-media">
<p class="preview-name">{ file.name.to_string() }</p>
<img src={ self.src(file) } />
</div>
<form onsubmit={noop}>
<label for="width">{"[Tile] Width"}</label>
<input name="width" type="number" value={self.width.to_string()} onchange={ctx.link().callback(Msg::Width)} />
<label for="height">{"[Tile] Height"}</label>
<input name="height" type="number" value={self.height.to_string()} onchange={ctx.link().callback(Msg::Height)} />
</form>
</div>
</div>
{self.previews(file)}
</>
}
}
}
4 changes: 1 addition & 3 deletions src/wasm/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,7 @@ impl Component for App {
<div id="wrapper">
<h1>{ "Process your CGA/EGAs" }</h1>
<FileInput accept="image/png,.bin,.cga,.ega" onload={ctx.link().callback( Msg::Loaded )} children={None}/>
<div id="preview-area">
{{ images }}
</div>
<div id="preview-area">{{ images }}</div>
</div>
}
}
Expand Down
4 changes: 0 additions & 4 deletions src/wasm/styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@ body {
color: #fcfcfc;
}

img {
display: block;
}

p {
text-align: center;
}
Expand Down

0 comments on commit 8536ab9

Please sign in to comment.