Skip to content

Commit

Permalink
Remove Rc<RefCell<>> from characters, simplify many components
Browse files Browse the repository at this point in the history
  • Loading branch information
m00nwtchr committed Jul 31, 2024
1 parent 7436c44 commit b5dcdcf
Show file tree
Hide file tree
Showing 18 changed files with 1,012 additions and 1,426 deletions.
47 changes: 24 additions & 23 deletions Cargo.lock

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

122 changes: 39 additions & 83 deletions src/component/attributes.rs
Original file line number Diff line number Diff line change
@@ -1,54 +1,58 @@
use std::{cell::RefCell, rc::Rc};

use cofd::{character::modifier::ModifierTarget, prelude::TraitCategory, prelude::*};
use iced::widget::{component, Component};
use iced::{
widget::{column, row, text, Column},
Alignment, Length,
};

use cofd::{character::modifier::ModifierTarget, prelude::TraitCategory, prelude::*};

use crate::i18n::Translate;
use crate::widget::dots;
use crate::{
fl,
widget::dots::{Shape, SheetDots},
Element, H2_SIZE, TITLE_SPACING,
};

pub struct AttributeBar<Message> {
// attributes: Attributes,
character: Rc<RefCell<Character>>,
on_change: Box<dyn Fn(u16, Attribute) -> Message>,
}

pub fn attribute_bar<Message>(
// attributes: Attributes,
character: Rc<RefCell<Character>>,
on_change: impl Fn(u16, Attribute) -> Message + 'static,
) -> AttributeBar<Message> {
AttributeBar::new(character, on_change)
}
#[derive(Debug, Clone)]
pub struct AttributeBar;

#[derive(Clone)]
pub struct Event(u16, Attribute);
pub struct Message(u16, Attribute);

impl<Message> AttributeBar<Message> {
fn new(
// attributes: Attributes,
character: Rc<RefCell<Character>>,
on_change: impl Fn(u16, Attribute) -> Message + 'static,
) -> Self {
Self {
// attributes,
character,
on_change: Box::new(on_change),
}
impl AttributeBar {
pub fn new() -> Self {
Self
}

pub fn update(&mut self, message: Message, character: &mut Character) {

Check failure on line 26 in src/component/attributes.rs

View workflow job for this annotation

GitHub Actions / clippy

this argument is passed by value, but not consumed in the function body

error: this argument is passed by value, but not consumed in the function body --> src/component/attributes.rs:26:36 | 26 | pub fn update(&mut self, message: Message, character: &mut Character) { | ^^^^^^^ help: consider taking a reference instead: `&Message` | help: consider marking this type as `Copy` --> src/component/attributes.rs:19:1 | 19 | pub struct Message(u16, Attribute); | ^^^^^^^^^^^^^^^^^^ = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_pass_by_value note: the lint level is defined here --> src/main.rs:2:9 | 2 | #![deny(clippy::pedantic)] | ^^^^^^^^^^^^^^^^ = note: `#[deny(clippy::needless_pass_by_value)]` implied by `#[deny(clippy::pedantic)]`
let Message(val, attr) = message;
*character.base_attributes_mut().get_mut(&attr) = val;
}

pub fn view(&self, character: &Character) -> Element<Message> {
column![
text(fl!("attributes")).size(H2_SIZE),
row![
column![
text(fl!("power")),
text(fl!("finesse")),
text(fl!("resistance"))
]
.spacing(3)
.width(Length::Fill)
.align_items(Alignment::End),
self.mk_attr_col(&character, TraitCategory::Mental),

Check warning on line 43 in src/component/attributes.rs

View workflow job for this annotation

GitHub Actions / clippy

this expression creates a reference which is immediately dereferenced by the compiler

warning: this expression creates a reference which is immediately dereferenced by the compiler --> src/component/attributes.rs:43:22 | 43 | self.mk_attr_col(&character, TraitCategory::Mental), | ^^^^^^^^^^ help: change this to: `character` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_borrow = note: `#[warn(clippy::needless_borrow)]` on by default
self.mk_attr_col(&character, TraitCategory::Physical),

Check warning on line 44 in src/component/attributes.rs

View workflow job for this annotation

GitHub Actions / clippy

this expression creates a reference which is immediately dereferenced by the compiler

warning: this expression creates a reference which is immediately dereferenced by the compiler --> src/component/attributes.rs:44:22 | 44 | self.mk_attr_col(&character, TraitCategory::Physical), | ^^^^^^^^^^ help: change this to: `character` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_borrow
self.mk_attr_col(&character, TraitCategory::Social),

Check warning on line 45 in src/component/attributes.rs

View workflow job for this annotation

GitHub Actions / clippy

this expression creates a reference which is immediately dereferenced by the compiler

warning: this expression creates a reference which is immediately dereferenced by the compiler --> src/component/attributes.rs:45:22 | 45 | self.mk_attr_col(&character, TraitCategory::Social), | ^^^^^^^^^^ help: change this to: `character` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_borrow
column![].width(Length::Fill)
]
.spacing(10)
]
.spacing(TITLE_SPACING)
.align_items(Alignment::Center)
.into()
}

fn mk_attr_col<Theme>(&self, character: &Character, category: TraitCategory) -> Element<Event, Theme>
where
Theme: text::StyleSheet + dots::StyleSheet + 'static,
{
fn mk_attr_col(&self, character: &Character, category: TraitCategory) -> Element<Message> {
let mut col1 = Column::new().spacing(3);
let mut col2 = Column::new()
.spacing(5)
Expand All @@ -68,7 +72,7 @@ impl<Message> AttributeBar<Message> {
5,
Shape::Dots,
None,
move |val| Event(val - mod_, attr),
move |val| Message(val - mod_, attr),
));
}

Expand All @@ -78,51 +82,3 @@ impl<Message> AttributeBar<Message> {
.into()
}
}

impl<Message, Theme> Component<Message, Theme> for AttributeBar<Message>
where
Theme: text::StyleSheet + dots::StyleSheet + 'static,
{
type State = ();
type Event = Event;

fn update(&mut self, _state: &mut Self::State, event: Self::Event) -> Option<Message> {
Some((self.on_change)(event.0, event.1))
}

fn view(&self, _state: &Self::State) -> Element<Event, Theme> {
let character = self.character.borrow();

column![
text(fl!("attributes")).size(H2_SIZE),
row![
column![
text(fl!("power")),
text(fl!("finesse")),
text(fl!("resistance"))
]
.spacing(3)
.width(Length::Fill)
.align_items(Alignment::End),
self.mk_attr_col(&character, TraitCategory::Mental),
self.mk_attr_col(&character, TraitCategory::Physical),
self.mk_attr_col(&character, TraitCategory::Social),
column![].width(Length::Fill)
]
.spacing(10)
]
.spacing(TITLE_SPACING)
.align_items(Alignment::Center)
.into()
}
}

impl<'a, Message, Theme> From<AttributeBar<Message>> for Element<'a, Message, Theme>
where
Message: 'a,
Theme: text::StyleSheet + dots::StyleSheet + 'static,
{
fn from(info_bar: AttributeBar<Message>) -> Self {
component(info_bar)
}
}
Loading

0 comments on commit b5dcdcf

Please sign in to comment.