From 89578e13b7473c9c3f6c7a96363572ed499a23ed Mon Sep 17 00:00:00 2001 From: Sijmen Schoon Date: Sat, 13 Nov 2021 17:00:35 +0100 Subject: [PATCH] Add ability score fields --- index.html | 4 ++ src/ability_scores.rs | 139 ++++++++++++++++++++++++++++++++++++++++++ src/character_info.rs | 79 ++++++++++++++++++++++++ src/main.rs | 86 ++++++-------------------- 4 files changed, 239 insertions(+), 69 deletions(-) create mode 100644 src/ability_scores.rs create mode 100644 src/character_info.rs diff --git a/index.html b/index.html index e56164b..cb0059b 100644 --- a/index.html +++ b/index.html @@ -28,6 +28,10 @@ label { margin-bottom: 8px; } +.ability-scores .field input { + width: 65px; +} + #character-class { width: 130px; } diff --git a/src/ability_scores.rs b/src/ability_scores.rs new file mode 100644 index 0000000..560ef71 --- /dev/null +++ b/src/ability_scores.rs @@ -0,0 +1,139 @@ +use yew::services::console::ConsoleService; + +#[derive(Debug)] +pub enum Message { + Strength(yew::ChangeData), + Dexterity(yew::ChangeData), + Constitution(yew::ChangeData), + Intelligence(yew::ChangeData), + Wisdom(yew::ChangeData), + Charisma(yew::ChangeData), +} + +pub struct AbilityScores { + link: yew::ComponentLink, + + strength: i32, + dexterity: i32, + constitution: i32, + intelligence: i32, + wisdom: i32, + charisma: i32, +} + +impl yew::Component for AbilityScores { + type Message = Message; + type Properties = (); + + fn create(_props: Self::Properties, link: yew::ComponentLink) -> Self { + Self { + link, + strength: 10, + dexterity: 10, + constitution: 10, + intelligence: 10, + wisdom: 10, + charisma: 10, + } + } + + fn update(&mut self, message: Self::Message) -> yew::ShouldRender { + match message { + Message::Strength(yew::ChangeData::Value(strength)) => { + self.strength = strength.parse().unwrap_or(10); + } + Message::Dexterity(yew::ChangeData::Value(dexterity)) => { + self.dexterity = dexterity.parse().unwrap_or(10); + } + Message::Constitution(yew::ChangeData::Value(constitution)) => { + self.constitution = constitution.parse().unwrap_or(10); + } + Message::Intelligence(yew::ChangeData::Value(intelligence)) => { + self.intelligence = intelligence.parse().unwrap_or(10); + } + Message::Wisdom(yew::ChangeData::Value(wisdom)) => { + self.wisdom = wisdom.parse().unwrap_or(10); + } + Message::Charisma(yew::ChangeData::Value(charisma)) => { + self.charisma = charisma.parse().unwrap_or(10); + } + _ => { + ConsoleService::log(&format!("{:?}", message)); + return false; + } + } + + true + } + + fn change(&mut self, _props: Self::Properties) -> yew::ShouldRender { + false + } + + fn view(&self) -> yew::Html { + yew::html! { +
+
+ + {(self.strength - 10) / 2} + +
+
+ + {(self.dexterity - 10) / 2} + +
+
+ + {(self.constitution - 10) / 2} + +
+
+ + {(self.intelligence - 10) / 2} + +
+
+ + {(self.wisdom - 10) / 2} + +
+
+ + {(self.charisma - 10) / 2} + +
+
+ } + } +} diff --git a/src/character_info.rs b/src/character_info.rs new file mode 100644 index 0000000..453e580 --- /dev/null +++ b/src/character_info.rs @@ -0,0 +1,79 @@ +pub enum Message {} + +pub struct CharacterInfo { + _link: yew::ComponentLink, +} + +impl yew::Component for CharacterInfo { + type Message = Message; + type Properties = (); + + fn create(_props: Self::Properties, link: yew::ComponentLink) -> Self { + Self { _link: link } + } + + fn update(&mut self, _msg: Self::Message) -> yew::ShouldRender { + false + } + + fn change(&mut self, _props: Self::Properties) -> yew::ShouldRender { + false + } + + fn view(&self) -> yew::Html { + yew::html! { +
+
+ + +
+
+
+
+ + +
+
+ + +
+
+
+ + +
+
+
+
+ + +
+
+ + +
+
+
+
+ + +
+
+ + +
+
+
+ } + } +} diff --git a/src/main.rs b/src/main.rs index 53c5eec..e64f09c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,27 +1,23 @@ -enum Msg { - AddOne, +mod ability_scores; +mod character_info; + +use ability_scores::AbilityScores; +use character_info::CharacterInfo; + +struct Page { + _link: yew::ComponentLink, } -struct Model { - link: yew::ComponentLink, - value: i64, -} - -impl yew::Component for Model { - type Message = Msg; +impl yew::Component for Page { + type Message = (); type Properties = (); fn create(_props: Self::Properties, link: yew::ComponentLink) -> Self { - Self { link, value: 0 } + Self { _link: link } } - fn update(&mut self, msg: Self::Message) -> yew::ShouldRender { - match msg { - Msg::AddOne => { - self.value += 1; - true - } - } + fn update(&mut self, _msg: Self::Message) -> yew::ShouldRender { + false } fn change(&mut self, _props: Self::Properties) -> yew::ShouldRender { @@ -30,62 +26,14 @@ impl yew::Component for Model { fn view(&self) -> yew::Html { yew::html! { -
-
- - -
-
-
-
- - -
-
- - -
-
-
- - -
-
-
-
- - -
-
- - -
-
-
-
- - -
-
- - -
-
+
+ +
} } } fn main() { - yew::start_app::(); + yew::start_app::(); }