Skip to content

Commit

Permalink
Fix dots/health track widgets
Browse files Browse the repository at this point in the history
  • Loading branch information
m00nwtchr committed Jul 28, 2024
1 parent a651839 commit 099ef72
Show file tree
Hide file tree
Showing 8 changed files with 181 additions and 208 deletions.
14 changes: 14 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ codegen-units = 1
inherits = "test"

[dependencies]
iced = { version = "0.12", features = ["lazy", "advanced"] }
iced = { version = "0.12", features = ["lazy", "advanced"] }

cofd = { git = "https://github.com/m00nwtchr/cofd.git" }
codex-scraper = { git = "https://github.com/m00nwtchr/cofd-miner.git" }
Expand Down Expand Up @@ -60,8 +60,10 @@ directories = "5"

[target.'cfg(target_arch = "wasm32")'.dependencies]
i18n-embed = { version = "0.14", features = ["web-sys-requester"] }
rust-embed = { version = "8", features = [ "debug-embed" ] }
rust-embed = { version = "8", features = ["debug-embed"] }
console_log = "1"
console_error_panic_hook = "0.1"
getrandom = { version = "*", features = ["js"] }

[target.'cfg(target_arch = "wasm32")'.dependencies.web-sys]
version = "0.3.60"
Expand Down
2 changes: 1 addition & 1 deletion src/component/integrity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ where
widget::dots::Axis::Horizontal
})
.spacing(if flag {
4
4f32
} else {
SheetDots::<Event, Theme>::DEFAULT_SPACING
}),
Expand Down
35 changes: 16 additions & 19 deletions src/component/skills.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ pub fn skills_component<Message>(

#[derive(Clone)]
pub enum Event {
SkillChanged(u16, Skill),
RoteSkillChanged(Skill),
SpecialtyChanged(Skill, usize, String),
SpecialtySkillChanged(Skill),
Skill(u16, Skill),
RoteSkill(Skill),
Specialty(Skill, usize, String),
SpecialtySkill(Skill),
}

#[derive(Default, Clone)]
Expand Down Expand Up @@ -100,10 +100,7 @@ impl<Message> SkillsComponent<Message> {

col0 = col0.push(
checkbox("", flag)
.on_toggle({
let skill = skill;
move |_| Event::RoteSkillChanged(skill)
})
.on_toggle(move |_| Event::RoteSkill(skill))
.spacing(0),
);
}
Expand All @@ -119,15 +116,15 @@ impl<Message> SkillsComponent<Message> {

col1 = col1.push(
button(text(flt("skill", Some(skill.name())).unwrap()).style(
if !specialties.is_empty() {
theme::Text::Color(Color::from_rgb(0.0, 0.7, 0.0))
} else {
if specialties.is_empty() {
theme::Text::Default
} else {
theme::Text::Color(Color::from_rgb(0.0, 0.7, 0.0))
},
))
.padding(0)
.style(theme::Button::Text)
.on_press(Event::SpecialtySkillChanged(skill)),
.on_press(Event::SpecialtySkill(skill)),
);

let v = character.base_skills().get(skill);
Expand All @@ -140,7 +137,7 @@ impl<Message> SkillsComponent<Message> {
5,
Shape::Dots,
None,
move |val| Event::SkillChanged(val - mod_, skill),
move |val| Event::Skill(val - mod_, skill),
));

if let Some(specialty_skill) = state.specialty_skill {
Expand All @@ -152,7 +149,7 @@ impl<Message> SkillsComponent<Message> {
specialties.clone(),
closure!(clone skill, |i, val| {
text_input("", &val.unwrap_or_default())
.on_input(move |val| Event::SpecialtyChanged(skill, i, val))
.on_input(move |val| Event::Specialty(skill, i, val))
.padding(0)
.into()
}),
Expand All @@ -172,7 +169,7 @@ impl<Message> SkillsComponent<Message> {

column![
text(flt(cat.name(), None).unwrap()).size(H3_SIZE),
text(fl!("unskilled", num = cat.unskilled())).size(17),
text(fl!("unskilled", num = cat.unskilled())).size(13),
col
]
.spacing(TITLE_SPACING)
Expand All @@ -197,12 +194,12 @@ where

fn update(&mut self, state: &mut Self::State, event: Self::Event) -> Option<Message> {
match event {
Event::SkillChanged(val, skill) => Some((self.on_change)(val, skill)),
Event::RoteSkillChanged(skill) => Some((self.on_rote_change)(skill)),
Event::SpecialtyChanged(skill, i, val) => {
Event::Skill(val, skill) => Some((self.on_change)(val, skill)),
Event::RoteSkill(skill) => Some((self.on_rote_change)(skill)),
Event::Specialty(skill, i, val) => {
Some((self.on_specialty_change)(skill, i, val))
}
Event::SpecialtySkillChanged(skill) => {
Event::SpecialtySkill(skill) => {
if let Some(cur) = state.specialty_skill
&& cur == skill
{
Expand Down
25 changes: 14 additions & 11 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
use iced::{
executor,
widget::{button, column, row, Column},
Alignment, Application, Command, Element, Length, Theme,
Alignment, Application, Command, Element, Length, Settings, Size, Theme,

Check warning on line 17 in src/main.rs

View workflow job for this annotation

GitHub Actions / clippy

unused import: `Size`

warning: unused import: `Size` --> src/main.rs:17:62 | 17 | Alignment, Application, Command, Element, Length, Settings, Size, Theme, | ^^^^ | = note: `#[warn(unused_imports)]` on by default
};
#[cfg(target_arch = "wasm32")]
use log::Level;
Expand Down Expand Up @@ -60,7 +60,7 @@ struct PlayerCompanionApp {
const H2_SIZE: u16 = 25;
const H3_SIZE: u16 = 20;

const MAX_INPUT_WIDTH: u16 = 200;
const MAX_INPUT_WIDTH: f32 = 200f32;
pub const INPUT_PADDING: u16 = 1;

const TITLE_SPACING: u16 = 2;
Expand Down Expand Up @@ -205,16 +205,14 @@ impl Application for PlayerCompanionApp {

#[cfg(target_arch = "wasm32")]
{
use iced_native::{command, window};
use iced::window;
use iced::window::Id;
let window = web_sys::window().unwrap();
let (width, height) = (
(window.inner_width().unwrap().as_f64().unwrap()) as u32,
(window.inner_height().unwrap().as_f64().unwrap()) as u32,
window.inner_width().unwrap().as_f64().unwrap() as f32,
window.inner_height().unwrap().as_f64().unwrap() as f32,
);
Command::single(command::Action::Window(window::Action::Resize {
width,
height,
}))
window::resize(Id::MAIN, Size { width, height })
}
#[cfg(not(target_arch = "wasm32"))]
Command::none()
Expand Down Expand Up @@ -285,9 +283,14 @@ fn main() -> anyhow::Result<()> {
#[cfg(not(target_arch = "wasm32"))]
env_logger::init();
#[cfg(target_arch = "wasm32")]
console_log::init_with_level(Level::Warn).map_err(|err| anyhow::anyhow!(err))?;
{
console_log::init_with_level(Level::Warn).map_err(|err| anyhow::anyhow!(err))?;
std::panic::set_hook(Box::new(console_error_panic_hook::hook));
}

PlayerCompanionApp::run(Default::default())?;
PlayerCompanionApp::run(Settings {
..Settings::default()
})?;
Ok(())
}

Expand Down
60 changes: 25 additions & 35 deletions src/store.rs
Original file line number Diff line number Diff line change
@@ -1,52 +1,42 @@
#[cfg(target_arch = "wasm32")]
use anyhow::anyhow;
use cfg_if::cfg_if;
#[cfg(not(target_arch = "wasm32"))]
use directories::ProjectDirs;
use serde::{Deserialize, Serialize};

pub struct Store {
#[cfg(target_arch = "wasm32")]
local_storage: web_sys::Storage,
#[cfg(not(target_arch = "wasm32"))]
dirs: ProjectDirs,
dirs: directories::ProjectDirs,
}

impl Store {
pub fn new() -> Option<Store> {
cfg_if! {
if #[cfg(target_arch = "wasm32")] {
let window = web_sys::window()?;
let store = if let Ok(Some(local_storage)) = window.local_storage() {
Some(Self {
local_storage,
})
} else {
None
};
#[cfg(target_arch = "wasm32")]
{
let window = web_sys::window()?;
if let Ok(Some(local_storage)) = window.local_storage() {
Some(Self { local_storage })
} else {
let dirs = ProjectDirs::from("", "", "cofd-pc").unwrap();

let dir = dirs.data_dir().parent();
if dir.is_some() && !dir.unwrap().exists() {
std::fs::create_dir_all(dir.unwrap()).ok()?;
}


None
}
}
#[cfg(not(target_arch = "wasm32"))]
{
let dirs = directories::ProjectDirs::from("", "", "cofd-pc").unwrap();

Some(Self {
dirs
})
let dir = dirs.data_dir().parent();
if dir.is_some() && !dir.unwrap().exists() {
std::fs::create_dir_all(dir.unwrap()).ok()?;
}

Some(Self { dirs })
}
}

pub fn get<T: for<'a> Deserialize<'a>>(&self, name: &str) -> anyhow::Result<Option<T>> {
#[cfg(target_arch = "wasm32")]
let val = self
.local_storage
.get_item(name)
.map_err(|err| anyhow!("{:?}", err))?;
.map_err(|err| anyhow::anyhow!("{:?}", err))?;

#[cfg(not(target_arch = "wasm32"))]
let val = Some(std::fs::read_to_string(
Expand All @@ -63,13 +53,13 @@ impl Store {
pub fn set<T: Serialize>(&self, name: &str, value: &T) -> anyhow::Result<()> {
let val = ron::ser::to_string(value)?;

cfg_if! {
if #[cfg(target_arch = "wasm32")] {
self.local_storage.set_item(name, &val).map_err(|err| anyhow!("{:?}", err))?;
} else {
std::fs::write(self.dirs.data_dir().join(format!("{name}.ron")), val)?;
}
}
#[cfg(target_arch = "wasm32")]
self.local_storage
.set_item(name, &val)
.map_err(|err| anyhow::anyhow!("{:?}", err))?;

#[cfg(not(target_arch = "wasm32"))]
std::fs::write(self.dirs.data_dir().join(format!("{name}.ron")), val)?;

Ok(())
}
Expand Down
Loading

0 comments on commit 099ef72

Please sign in to comment.