Skip to content

Commit

Permalink
broken UIBuilder stuff and random tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Vawlpe committed Jun 1, 2022
1 parent 5df20c3 commit 60579eb
Show file tree
Hide file tree
Showing 9 changed files with 400 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
src/bin/
src/obj
test/
ref/
build.sh
7 changes: 7 additions & 0 deletions src/ModTaker/Global.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace ModTaker
{
public static class Global
{
public static bool EnableSteam = false;
}
}
40 changes: 40 additions & 0 deletions src/ModTaker/MainMenu.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using UnityEngine;

namespace ModTaker
{
public class MainMenu
{
public enum MenuState
{
Main,
ChapterSelect,
Settings,
Credits,
Dialog,
SceneTransition,
Loading,
Quit
}

public MenuState CurrentMenuState = MenuState.Main;
public GameObject go = new GameObject();
public void toMainMenu()
{
CurrentMenuState = MenuState.Main;
go = MainMenuObject;
go.name = "MainMenu";
}

public GameObject MainMenuObject = UI.Build(new UIItem[]
{
new UIButton() {
Name = "TestButton",
Position = new Vector2(100, 200),
Size = new Vector2(150, 150),
Text = "Test",
FontSize = 30,
Sprite = Resources.Load<Sprite>("Button")
}
});
}
}
128 changes: 128 additions & 0 deletions src/ModTaker/UIBuilder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
using UnityEngine;
using UnityEngine.UI;

namespace ModTaker
{
public class UI
{
public static GameObject Build(UIItem[] uiItems)
{
var g = new GameObject();

var c = g.AddComponent<Canvas>();
c.renderMode = RenderMode.ScreenSpaceOverlay;
c.pixelPerfect = true;
c.sortingOrder = 1;

var cs = g.AddComponent<CanvasScaler>();
cs.uiScaleMode = CanvasScaler.ScaleMode.ScaleWithScreenSize;
cs.referenceResolution = new Vector2(1920, 1080);
cs.screenMatchMode = CanvasScaler.ScreenMatchMode.MatchWidthOrHeight;

var cr = g.AddComponent<GraphicRaycaster>();

foreach (UIItem uiItem in uiItems)
{
var i = uiItem.Build();
i.transform.SetParent(g.transform);
}

return g;
}
}

public class UIItem
{
public string Name = String.Empty;
public GameObject GO = new GameObject();

public Vector2 Position = Vector2.zero;
public Vector2 Size = Vector2.zero;
public Vector2 Anchor = Vector2.zero;
public Vector2 Pivot = Vector2.zero;
public Vector2 Offset = Vector2.zero;
public Vector2 Rotation = Vector2.zero;
public Vector2 Scale = Vector2.one;

public virtual GameObject Build()
{
GO.name = Name;
var RT = GO.AddComponent<RectTransform>();
RT.anchorMin = Anchor;
RT.anchorMax = Anchor;
RT.pivot = Pivot;
RT.anchoredPosition = Position + Offset;
RT.sizeDelta = Size;
RT.localScale = Scale;
RT.localPosition = Position;
RT.localRotation = Quaternion.Euler(Rotation);

return GO;
}
}

public class UIText : UIItem
{
public string Text = String.Empty;
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;

public override GameObject Build()
{
GO = base.Build();

Text text = GO.AddComponent<Text>();
text.text = Text;
text.font = Font;
text.color = Color;
text.alignment = TextAnchor;
text.alignment = (TextAnchor)TextAlignment;
text.fontSize = FontSize;
text.supportRichText = RichText;
text.raycastTarget = RaycastTarget;

return GO;
}
}

public class UIButton : UIItem
{
// Text
public string Text = String.Empty;
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;

// Image
public Sprite Sprite = null;

public override GameObject Build()
{
GO = base.Build();

var b = GO.AddComponent<Button>();
var i = GO.AddComponent<Image>();
i.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;

return GO;
}
}
}
16 changes: 16 additions & 0 deletions src/patch/GoalSprite.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#pragma warning disable CS0626 // orig_ method is marked external and has no attributes on it.
public class patch_GoalSprite : GoalSprite
{
private extern void orig_Start();
private void Start()
{
orig_Start();
// Stupid dialog manip, don't do this.
// I'll implement Diannex <-> DialogElement[] + String[] conversion later
// to simplify writing dialogue without scene access.
txt[19] = "You find yourself surrounded by the void.\n But something is different...";
txt[1] = "Greetings little one. It is just I, good old Beelzebub.\nChanging the fabric of reality? I'm afraid not.";
txt[2] = "Certainly interesting, but it won't get you out of here.";
}
}
#pragma warning restore CS0626 // orig_ method is marked external and has no attributes on it.
16 changes: 16 additions & 0 deletions src/patch/MainMenuStart.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using ModTaker;

public class patch_MainMenuStart : MainMenuStart
{
private extern void orig_Awake();
private void Awake()
{
// Okay wait what? i end up with 2 objects in the scene
// Both are called "New Game Object" and neither is attached to the mainMenuStart object
// The first has the canvas components... and the second only has a transform
// WTF Unity?
var mm = new MainMenu();
mm.toMainMenu();
mm.go.transform.SetParent(this.transform);
}
}
32 changes: 32 additions & 0 deletions src/patch/SteamManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#pragma warning disable CS8618, CS0626 // Disable warnings.
using MonoMod;
using G = ModTaker.Global;

// Disable SteamManager without removing it.
public class patch_SteamManager : SteamManager
{
[MonoModReplace]
protected static bool s_EverInialized = false;

[MonoModPublic]
public static new SteamManager Instance;

protected extern void orig_Awake();
protected new void Awake()
{
if (G.EnableSteam) orig_Awake();
}

protected extern void orig_OnEnable();
protected new void OnEnable()
{
if (G.EnableSteam) orig_OnEnable();
}

protected extern void orig_Update();
protected new void Update()
{
if (G.EnableSteam) orig_Update();
}
}
#pragma warning restore CS8618 // Re-enable warnings.
126 changes: 126 additions & 0 deletions src/patch/widgetScript.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
#pragma warning disable CS0626 // orig_ method is marked external and has no attributes on it
using UnityEngine;
using UnityEngine.UI;
using MonoMod;

// Patch widgetScript to add a mod chapter button to the UI
public class patch_widgetScript : widgetScript
{
[MonoModReplace]
private int currentIndex = 666;
[MonoModReplace]
private int tempIndex;

private extern void orig_OnEnable();
private void OnEnable()
{
// Clone chapterDLC object into chapterMOD, changing it's name and localPosition
var chapterMOD = UnityEngine.Object.Instantiate(transform.Find("chapterDLC").gameObject, transform).transform;
chapterMOD.gameObject.name = "chapterMOD";
chapterMOD.transform.localPosition = new Vector3(-675f, -325f, 0f);;

// Change text properties of chapterMOD
var chapterMOD_text = chapterMOD.Find("chapter_text").GetComponent<Text>();
chapterMOD_text.text = "MOD";
chapterMOD_text.fontSize = 28;
chapterMOD_text.gameObject.transform.localPosition = new Vector3(-1f, -3f, 0f);

// Add chapterMOD animator to chapterAnimator array
chapterAnimator = chapterAnimator.Concat(new Animator[] {chapterMOD.GetComponent<Animator>()}).ToArray();

// Add chapterMOD as a child of the widget Object
chapterMOD.transform.SetParent(transform);

// Call original OnEnable method
orig_OnEnable();
}

// Replace Update with out own that takes into account the mod chapter button
[MonoModReplace]
private void Update()
{
if (Input.GetButton("Cancel"))
{
goalScript.BackFromWidget(1);
base.gameObject.SetActive(value: false);
}
if (Input.GetButton("Submit"))
{
if (!submitBlock)
{
submitBlock = true;
Manager.instance.RandomizeSfx(false, confirmSound);
switch(currentIndex)
{
case 0:
goalScript.BackFromWidget(2);
base.gameObject.SetActive(false);
break;
case <9:
goalScript.playerScript.DialogueRestart(currentIndex + 1);
break;
case 9:
goalScript.playerScript.DialogueRestart(currentIndex + 6);
break;
case 10:
goalScript.playerScript.DialogueRestart(currentIndex + 7);
break;
case 11:
goalScript.BackFromWidget(1);
base.gameObject.SetActive(false);
break;
}
}
}
else submitBlock = false;
switch (Input.GetAxis("Horizontal"))
{
case 0f:
keyDown = false;
break;
case <0f:
if (keyDown)
break;

keyDown = true;
Manager.instance.RandomizeSfx(false, pickSound);

if (currentIndex > 0)
currentIndex--;
else currentIndex = 11;

ChapterChange();
break;
case >0f:
if (keyDown)
break;

keyDown = true;
Manager.instance.RandomizeSfx(false, pickSound);

if (currentIndex < 11)
currentIndex++;
else currentIndex = 0;

ChapterChange();
break;
}
}

// Replace ChapterChange with out own that takes into account the mod chapter button
[MonoModReplace]
private void ChapterChange()
{
chapterAnimator[tempIndex].SetBool("activeAnim", false);
chapterAnimator[currentIndex].SetBool("activeAnim", true);
tempIndex = currentIndex;
chapterTitle.text = currentIndex switch
{
10 => Manager.instance.dlcTxt[0],
11 => "ModTaker v0.1 | 1 Mod(s) Loaded",
_ => chapterTxt[currentIndex]
};
chapterTitle.color = currentIndex >= 10 ? new Color(0.9f, 0.3f, 0.3f) : Color.white;
}
}
#pragma warning restore CS0626 // orig_ method is marked external and has no attributes on it
Loading

0 comments on commit 60579eb

Please sign in to comment.