Skip to content

Commit

Permalink
prefer #[init(node = xxxx)] over getters
Browse files Browse the repository at this point in the history
  • Loading branch information
Lamby777 committed Aug 18, 2024
1 parent 2d0807b commit 976cd7f
Show file tree
Hide file tree
Showing 9 changed files with 69 additions and 86 deletions.
19 changes: 8 additions & 11 deletions pets-lib/src/battle/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,12 @@ pub struct BattleEngine {
/// won't die until both your HP and the bar show zero.
#[init(val = OnReady::manual())]
karma_timer: OnReady<Gd<Timer>>,

#[init(node = "Menu/DualMenu")]
dualmenu: OnReady<Gd<Control>>,

#[init(node = "Menu/DualMenu/AnimationPlayer")]
dualmenu_animator: OnReady<Gd<AnimationPlayer>>,
}

#[godot_api]
Expand Down Expand Up @@ -182,7 +188,7 @@ impl BattleEngine {
}

fn open_dualmenu(&mut self) {
self.dualmenu_animator()
self.dualmenu_animator
.play_animation_forwards("dualmenu_open", true);

self.menu_section = Some(MenuSection::Main);
Expand All @@ -194,7 +200,7 @@ impl BattleEngine {
}

fn close_dualmenu(&mut self) {
self.dualmenu_animator()
self.dualmenu_animator
.play_animation_forwards("dualmenu_open", false);

self.menu_section = None;
Expand Down Expand Up @@ -227,15 +233,6 @@ impl BattleEngine {
}
}

pub fn dualmenu(&self) -> Gd<Control> {
self.base().get_node_as::<Control>("Menu/DualMenu")
}

pub fn dualmenu_animator(&self) -> Gd<AnimationPlayer> {
self.dualmenu()
.get_node_as::<AnimationPlayer>("AnimationPlayer")
}

pub fn swap_party_member(&mut self, new_index: usize) {
self.current_party_member = new_index;
let pchar = self.battlers.good_guys[new_index].borrow().id();
Expand Down
20 changes: 10 additions & 10 deletions pets-lib/src/dialogue/dbox/dchoice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,25 @@ use crate::prelude::*;
#[class(init, base=MarginContainer)]
pub struct DChoice {
base: Base<MarginContainer>,

#[init(node = "RichTextLabel")]
txt_label: OnReady<Gd<RichTextLabel>>,
}

#[godot_api]
impl DChoice {
#[func]
pub fn set_text_tr(&mut self, text: GString) {
self.txt_label().set_text(tr(text));
}

pub fn txt_label(&self) -> Gd<RichTextLabel> {
self.base().get_node_as("RichTextLabel")
self.txt_label.set_text(tr(text));
}

/// tween the contained text label in/out of the window
pub fn tween_label(&self, up: bool) -> Gd<Tween> {
pub fn tween_label(&mut self, up: bool) -> Gd<Tween> {
let label = &mut self.txt_label;
let tw_end = if up { 0.0 } else { DBOX_CHOICE_HEIGHT };

tween(
self.txt_label(),
label,
"position:y",
None,
tw_end,
Expand All @@ -45,8 +45,8 @@ impl DChoice {
.unwrap()
}

pub fn put_label_under(&self) {
self.txt_label()
pub fn put_label_under(&mut self) {
self.txt_label
.set_position(Vector2::new(0.0, DBOX_CHOICE_HEIGHT));
}

Expand All @@ -69,7 +69,7 @@ impl IMarginContainer for DChoice {
return;
}

let label = self.txt_label();
let label = &self.txt_label;
let label_size = Vector2 {
x: label.get_size().x,
// y: label.get_size().y + 20.0,
Expand Down
62 changes: 26 additions & 36 deletions pets-lib/src/dialogue/dbox/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,15 @@ pub struct DialogBox {

#[init(val = OnReady::manual())]
text_visibility_timer: OnReady<Gd<Timer>>,

#[init(node = "AnimationPlayer")]
anim_player: OnReady<Gd<AnimationPlayer>>,
#[init(node = "VBox/SpeakerName")]
spk_txt: OnReady<Gd<RichTextLabel>>,
#[init(node = "VBox/Content")]
msg_txt: OnReady<Gd<RichTextLabel>>,
#[init(node = "%Choices")]
choice_container: OnReady<Gd<HBoxContainer>>,
}

#[godot_api]
Expand Down Expand Up @@ -85,17 +94,17 @@ impl DialogBox {
.then(|| self.translated_message())
.unwrap_or("".into());

self.spk_txt().set_text(spk);
self.msg_txt().set_text(msg);
self.spk_txt.set_text(spk);
self.msg_txt.set_text(msg);

self.msg_txt().set_visible_characters(0);
self.msg_txt.set_visible_characters(0);
self.text_visibility_timer.start();
}

/// See <https://github.com/Lamby777/PETS-G/issues/50>
#[func]
pub fn text_visibility_tick(&mut self) {
let mut label = self.msg_txt();
let label = &mut self.msg_txt;
let visible = label.get_visible_characters();
label.set_visible_characters(visible + 1);

Expand All @@ -114,12 +123,12 @@ impl DialogBox {
}

pub fn is_done_showing_text(&self) -> bool {
let label = self.msg_txt();
let label = &self.msg_txt;
label.get_visible_characters() >= label.get_text().len() as i32
}

pub fn skip_text_visibility(&mut self) {
let mut label = self.msg_txt();
let label = &mut self.msg_txt;
let len = label.get_text().len() as i32;
label.set_visible_characters(len);
}
Expand All @@ -138,14 +147,10 @@ impl DialogBox {
placeholders::process_placeholders(&content).into()
}

fn anim_player(&self) -> Gd<AnimationPlayer> {
self.base().get_node_as("AnimationPlayer")
}

#[func]
pub fn open_or_close(&mut self, open: bool) {
self.active = open;
self.anim_player().play_animation_forwards("open", open);
self.anim_player.play_animation_forwards("open", open);
self.do_draw();
}

Expand Down Expand Up @@ -184,21 +189,6 @@ impl DialogBox {
!self.choice_agent.bind().get_disabled()
}

/// Get the speaker name label
fn spk_txt(&self) -> Gd<RichTextLabel> {
self.base().get_node_as("VBox/SpeakerName")
}

/// Get the message text label
fn msg_txt(&self) -> Gd<RichTextLabel> {
self.base().get_node_as("VBox/Content")
}

/// Get the container for choice labels
fn choice_container(&self) -> Gd<HBoxContainer> {
self.base().get_node_as("%Choices")
}

/// If the dialog box is currently active
///
/// Active means either tweening on-screen,
Expand All @@ -208,8 +198,8 @@ impl DialogBox {
}

fn free_choice_labels(&mut self) {
let mut cont = self.choice_container();
let children = children_of_type::<DChoice, _>(cont.clone());
let cont = &mut self.choice_container;
let children = children_of_type::<DChoice, _>(&cont.clone());

for mut node in children {
node.set_name("deleted".into());
Expand All @@ -223,7 +213,7 @@ impl DialogBox {
pub fn recreate_choice_labels(&mut self) {
self.free_choice_labels();

let mut cont = self.choice_container();
let cont = &mut self.choice_container;

for (i, choice) in self.queued_choices.iter_shared().enumerate() {
godot_print!("Creating choice label: {}", choice);
Expand All @@ -236,7 +226,7 @@ impl DialogBox {
cont.add_child(dchoice.clone());

// put label below the window
dchoice.bind().put_label_under();
dchoice.bind_mut().put_label_under();
}

self.set_choice_label_focus_directions();
Expand Down Expand Up @@ -267,7 +257,7 @@ impl DialogBox {
}

pub fn choice_nodes(&self) -> Vec<Gd<DChoice>> {
self.choice_container()
self.choice_container
.get_children()
.iter_shared()
.filter_map(|n| n.try_cast::<DChoice>().ok())
Expand All @@ -276,12 +266,12 @@ impl DialogBox {

pub fn tween_choices_wave(&mut self, up: bool) {
self.choice_agent.bind_mut().set_disabled(!up);
let choices = self.choice_nodes();
let mut choices = self.choice_nodes();

for (i, cont) in choices.iter().enumerate() {
for (i, cont) in choices.iter_mut().enumerate() {
// if moving up, start below the window
if up {
cont.bind().put_label_under();
cont.bind_mut().put_label_under();
}

// we can't move the label into the closure because of
Expand All @@ -293,8 +283,8 @@ impl DialogBox {
// get the label again using the instance id
let label = Gd::<DChoice>::try_from_instance_id(label_id);

if let Ok(label) = label {
let mut tw = label.bind().tween_label(up);
if let Ok(mut label) = label {
let mut tw = label.bind_mut().tween_label(up);

// if tweening down, delete it after the tween
if !up {
Expand Down
18 changes: 8 additions & 10 deletions pets-lib/src/title_screen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,18 @@ struct TitleScreen {

#[init(node = "%SaveFilesContainer/ChoiceAgent")]
save_choices: OnReady<Gd<ChoiceAgent>>,

#[init(node = "%BlackFade")]
black: OnReady<Gd<ColorRect>>,

#[init(node = "%CreditsPanel")]
credits_panel: OnReady<Gd<PanelContainer>>,
}

#[godot_api]
impl TitleScreen {
fn credits_panel(&self) -> Gd<PanelContainer> {
self.base().get_node_as("%CreditsPanel")
}

fn black(&self) -> Gd<ColorRect> {
self.base().get_node_as("%BlackFade")
}

fn anim_out(&self) {
fade_black(&self.black(), true, BLACK_FADE_TIME);
fade_black(&self.black, true, BLACK_FADE_TIME);

let mut anim = self
.base()
Expand Down Expand Up @@ -96,7 +94,7 @@ impl TitleScreen {
}

"Credits" => {
let panel = self.credits_panel();
let panel = &mut self.credits_panel;
self.credits_up = !self.credits_up;

let y = match self.credits_up {
Expand Down
4 changes: 2 additions & 2 deletions pets-lib/src/util/choices.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ impl ChoiceAgent {
};

tween(
label.clone(),
&mut label.clone(),
"theme_override_colors/default_color",
None,
target_col,
Expand All @@ -97,7 +97,7 @@ impl ChoiceAgent {
if !self.tween_property.is_empty() {
// tween the custom param
tween(
target.clone(),
&mut target.clone(),
self.tween_property.clone(),
None,
target_val,
Expand Down
8 changes: 4 additions & 4 deletions pets-lib/src/util/node_stuff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ macro_rules! connect {
}

/// Recursively get all children of a node that are of a certain type.
pub fn children_of_type<T, Par>(parent: Gd<Par>) -> Vec<Gd<T>>
pub fn children_of_type<T, Par>(parent: &Gd<Par>) -> Vec<Gd<T>>
where
Par: Inherits<Node>,
T: Inherits<Node>,
Expand All @@ -101,7 +101,7 @@ where
}

// push children that match as well
res.extend(children_of_type::<T, _>(node));
res.extend(children_of_type::<T, _>(&node));
}

res
Expand Down Expand Up @@ -187,7 +187,7 @@ where
/// shorthand to do some tweeneroonies :3
/// `time` is in seconds
pub fn tween<NP, V, N>(
mut node: Gd<N>,
node: &mut Gd<N>,
property: NP,
start_value: Option<V>,
end_value: V,
Expand All @@ -204,7 +204,7 @@ where

let mut property = tween
.tween_property(
node.upcast::<Object>(),
node.clone().upcast::<Object>(),
property.into(),
end_value.to_variant(),
time,
Expand Down
9 changes: 4 additions & 5 deletions pets-lib/src/world/inv_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ pub struct InventoryNode {

#[init(node = "%ItemsRow")]
row: OnReady<Gd<HBoxContainer>>,

#[init(node = "%InventoryText")]
text_container: OnReady<Gd<BoxContainer>>,
}

#[godot_api]
Expand All @@ -34,10 +37,6 @@ impl InventoryNode {
self.is_open
}

fn text_container(&self) -> Gd<BoxContainer> {
self.base().get_node_as("%InventoryText")
}

fn item_icon(&self, index: usize) -> Gd<MarginContainer> {
if index > self.row.get_child_count() as usize {
panic!("Index out of bounds: {}", index);
Expand All @@ -47,7 +46,7 @@ impl InventoryNode {
}

fn update_text_labels(&mut self) {
let cont = self.text_container();
let cont = &self.text_container;
let mut name_txt =
cont.get_node_as::<RichTextLabel>("ItemName/RichTextLabel");
let mut desc_txt =
Expand Down
Loading

0 comments on commit 976cd7f

Please sign in to comment.