From a30e6a4031236d1ce1131925b095c0474c590aea Mon Sep 17 00:00:00 2001
From: jugeeya <jugeeya@live.com>
Date: Mon, 14 Aug 2023 12:39:04 -0700
Subject: [PATCH] Begin to use font glyphs, small refactor

---
 src/training/ui/input_log.rs | 28 ++++++++++++++++++++++------
 src/training/ui/menu.rs      | 13 ++++---------
 src/training/ui/mod.rs       | 14 ++++++++++++++
 3 files changed, 40 insertions(+), 15 deletions(-)

diff --git a/src/training/ui/input_log.rs b/src/training/ui/input_log.rs
index 2e80935..8b66827 100644
--- a/src/training/ui/input_log.rs
+++ b/src/training/ui/input_log.rs
@@ -11,6 +11,8 @@ use crate::{
     training::input_log::P1_INPUT_MAPPINGS,
 };
 
+use super::set_icon_text;
+
 macro_rules! log_parent_fmt {
     ($x:ident) => {
         format!("TrModInputLog{}", $x).as_str()
@@ -49,18 +51,32 @@ pub unsafe fn draw(root_pane: &Pane) {
 
     input_pane.set_text_string("NONE");
 
+    let mut glyphs = vec![];
     if first_log.buttons.contains(Buttons::ATTACK) {
         let potential_font_glyph = name_to_font_glyph(ButtonConfig::A, *p1_style_ptr);
         if let Some(font_glyph) = potential_font_glyph {
-            input_pane.set_text_string("");
-
-            let it = input_pane.text_buf as *mut u16;
-            input_pane.text_len = 1;
-            *it = font_glyph;
-            *(it.add(1)) = 0x0;
+            glyphs.push(font_glyph);
         }
     }
 
+    if first_log.buttons.contains(Buttons::SPECIAL) {
+        let potential_font_glyph = name_to_font_glyph(ButtonConfig::B, *p1_style_ptr);
+        if let Some(font_glyph) = potential_font_glyph {
+            glyphs.push(font_glyph);
+        }
+    }
+
+    if first_log.buttons.contains(Buttons::JUMP) {
+        let potential_font_glyph = name_to_font_glyph(ButtonConfig::X, *p1_style_ptr);
+        if let Some(font_glyph) = potential_font_glyph {
+            glyphs.push(font_glyph);
+        }
+    }
+
+    if !glyphs.is_empty() {
+        set_icon_text(input_pane, glyphs);
+    }
+
     log_pane
         .find_pane_by_name_recursive("FrameTxt")
         .unwrap()
diff --git a/src/training/ui/menu.rs b/src/training/ui/menu.rs
index 12b624c..3965cda 100644
--- a/src/training/ui/menu.rs
+++ b/src/training/ui/menu.rs
@@ -8,6 +8,8 @@ use training_mod_tui::{App, AppPage, NUM_LISTS};
 
 use crate::{common, common::menu::QUICK_MENU_ACTIVE, input::*};
 
+use super::set_icon_text;
+
 pub static NUM_MENU_TEXT_OPTIONS: usize = 32;
 pub static _NUM_MENU_TABS: usize = 3;
 
@@ -459,10 +461,7 @@ pub unsafe fn draw(root_pane: &Pane) {
 
         // Left/Right tabs have keys
         if let Some(key) = key {
-            let it = icon_pane.text_buf as *mut u16;
-            icon_pane.text_len = 1;
-            *it = **key;
-            *(it.add(1)) = 0x0;
+            set_icon_text(icon_pane, vec![**key]);
         }
 
         if *name == "CurrentTab" {
@@ -486,11 +485,7 @@ pub unsafe fn draw(root_pane: &Pane) {
             .find_pane_by_name_recursive("set_txt_icon")
             .unwrap()
             .as_textbox();
-        icon_pane.set_text_string("");
-        let it = icon_pane.text_buf as *mut u16;
-        icon_pane.text_len = 1;
-        *it = *key.unwrap();
-        *(it.add(1)) = 0x0;
+        set_icon_text(icon_pane, vec![*key.unwrap()]);
 
         key_help_pane
             .find_pane_by_name_recursive("set_txt_help")
diff --git a/src/training/ui/mod.rs b/src/training/ui/mod.rs
index 50136a4..159fceb 100644
--- a/src/training/ui/mod.rs
+++ b/src/training/ui/mod.rs
@@ -2,6 +2,7 @@
 use byte_unit::MEBIBYTE;
 use sarc::SarcFile;
 use skyline::nn::ui2d::*;
+use smash::ui2d::SmashTextBox;
 use training_mod_consts::{OnOff, MENU};
 
 use crate::common::{is_ready_go, is_training_mode, menu::QUICK_MENU_ACTIVE};
@@ -14,6 +15,19 @@ mod input_log;
 mod menu;
 pub mod notifications;
 
+pub unsafe fn set_icon_text(pane: &mut TextBox, icons: Vec<u16>) {
+    pane.set_text_string("");
+
+    let it = pane.text_buf as *mut u16;
+    pane.text_len = icons.len() as u16;
+    for (idx, icon) in icons.iter().enumerate() {
+        *(it.add(idx)) = *icon;
+    }
+
+    // Add nullptr at end to be sure
+    *(it.add(icons.len())) = 0x0;
+}
+
 #[skyline::hook(offset = 0x4b620)]
 pub unsafe fn handle_draw(layout: *mut Layout, draw_info: u64, cmd_buffer: u64) {
     let layout_name = skyline::from_c_str((*layout).layout_name);