Skip to content

Commit

Permalink
Some old test stuff
Browse files Browse the repository at this point in the history
Signed-off-by: Vawlpe <[email protected]>
  • Loading branch information
Vawlpe committed Aug 1, 2022
1 parent a670fd0 commit 5495ae2
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 24 deletions.
31 changes: 31 additions & 0 deletions src/ModTaker/AssetScan.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using UnityEngine;
using UnityEngine.UI;

namespace ModTaker
{
public class AssetScanner
{
public static Dictionary<String, Sprite> ScanSpritesInScene(String sceneName)
{
var objs = from GameObject go in GameObject.FindObjectsOfType(typeof(GameObject)) where go.scene.name == sceneName select go;
var sprites = new Dictionary<String, Sprite>();
foreach (GameObject go in objs)
{
try {
foreach (var sr in go.GetComponentsInChildren<SpriteRenderer>())
if (sr.sprite != null) sprites.Add(sr.sprite.name, sr.sprite);
foreach (var im in go.GetComponentsInChildren<Image>())
if (im.sprite != null) sprites.Add(im.sprite.name, im.sprite);
foreach (var sr in go.GetComponents<SpriteRenderer>())
if (sr.sprite != null) sprites.Add(sr.sprite.name, sr.sprite);
foreach (var im in go.GetComponents<Image>())
if (im.sprite != null) sprites.Add(im.sprite.name, im.sprite);
} catch (System.Exception e) {
Debug.LogError(e);
continue;
}
}
return sprites;
}
}
}
3 changes: 3 additions & 0 deletions src/ModTaker/Global.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
using UnityEngine;

namespace ModTaker
{
public static class Global
{
public static bool EnableSteam = false;
public static Dictionary<String, Sprite> Sprites;
}
}
10 changes: 6 additions & 4 deletions src/ModTaker/MainMenu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,22 @@ public void toMainMenu()
CurrentMenuState = MenuState.Main;
CurrentMenuCanvas = menuObjects["Main"];

Array.ForEach(menuObjects.Values.ToArray(), (GameObject go) => go.SetActive(false));
CurrentMenuCanvas.SetActive(true);
}

public MainMenu()
{
menuObjects = new Dictionary<string, GameObject>()
{{
"Main", UI.Build("Main", new UIItem[] {
new UIButton()
"Main", UI.UICanvas.Build("Main", new UI.UIItem[] {
new UI.Helltaker.Button()
{
Name = "TestButton",
Text = "Test Button",
Position = new Vector2(0, 0),
Size = new Vector2(100, 100)
Position = new Vector2(-400, 40),
Size = new Vector2(1000, 100),
FontSize = 30
}
})
}};
Expand Down
13 changes: 13 additions & 0 deletions src/ModTaker/UI.Helltaker.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using UnityEngine;

namespace ModTaker.UI.Helltaker
{
public class Button : UIButton
{
public override GameObject Build()
{
this.Sprite = Global.Sprites["button0003"];
return base.Build();
}
}
}
35 changes: 22 additions & 13 deletions src/ModTaker/UIBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using UnityEngine;
using UnityEngine.UI;

namespace ModTaker
namespace ModTaker.UI
{
public class UI
public class UICanvas
{
public static GameObject Build(String Name, UIItem[] uiItems)
{
Expand Down Expand Up @@ -94,7 +94,6 @@ public class UIButton : UIItem
public Font Font = Resources.GetBuiltinResource<Font>("Arial.ttf");
public Color Color = Color.white;
public TextAnchor TextAnchor = TextAnchor.UpperLeft;
public TextAlignment TextAlignment = TextAlignment.Left;
public int FontSize = 12;
public bool RichText = false;
public bool RaycastTarget = false;
Expand All @@ -107,17 +106,27 @@ public override GameObject Build()
GO = base.Build();

var b = GO.AddComponent<Button>();
var i = GO.AddComponent<Image>();
i.sprite = Sprite;

if (Sprite is not null)
{
var io = new GameObject(Name + "_Image");
io.transform.SetParent(GO.transform);
io.AddComponent<Image>().sprite = Sprite;
}

var t = GO.AddComponent<Text>();
t.text = Text;
t.font = Font;
t.color = Color;
t.alignment = (TextAnchor)TextAlignment;
t.fontSize = FontSize;
t.supportRichText = RichText;
t.raycastTarget = RaycastTarget;
if (Text != String.Empty)
{
var to = new GameObject(Name + "_Text");
to.transform.SetParent(GO.transform);
var tc = to.AddComponent<Text>();
tc.text = Text;
tc.font = Font;
tc.color = Color;
tc.alignment = TextAnchor;
tc.fontSize = FontSize;
tc.supportRichText = RichText;
tc.raycastTarget = RaycastTarget;
}

return GO;
}
Expand Down
16 changes: 9 additions & 7 deletions src/patch/MainMenuStart.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
using ModTaker;
using UnityEngine;
using UnityEngine.SceneManagement;

public class patch_MainMenuStart : MainMenuStart
{
private extern void orig_Awake();
private void Awake()
{
orig_Awake();

// This is meant to give us the following structure:
// - mainMenuStarter (this)
// -- Main
// --- TestButton
// Instead all 3 of these objects are at the global level...
// wtf Unity? I explicitly set the parents of all of these objects so why does it not work?
Global.Sprites = AssetScanner.ScanSpritesInScene(SceneManager.GetActiveScene().name);
Debug.Log("Found " + Global.Sprites.Count + " sprites");
foreach (var s in Global.Sprites)
{
Debug.Log("Sprite: " + s.ToString());
}

var mm = new MainMenu();
mm.toMainMenu();
mm.CurrentMenuCanvas.transform.SetParent(this.transform);
Expand Down

0 comments on commit 5495ae2

Please sign in to comment.