Skip to content

Commit

Permalink
Merge pull request #55 from ToniMacaroni/development
Browse files Browse the repository at this point in the history
2.3.2
  • Loading branch information
ToniMacaroni authored Jun 18, 2021
2 parents 5c13c07 + 8074b0e commit faf58c4
Show file tree
Hide file tree
Showing 16 changed files with 115 additions and 22 deletions.
33 changes: 28 additions & 5 deletions SaberFactory/Helpers/MaterialHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,17 @@ public static bool TryGetTexture(this Material material, string propName, out Te
return tex != null;
}

public static bool TryGetTexture(this Material material, int propId, out Texture tex)
{
tex = null;
if (!material.HasProperty(propId)) return false;
tex = material.GetTexture(propId);
return tex != null;
}

public static bool TryGetMainTexture(this Material material, out Texture tex)
{
return TryGetTexture(material, "_MainTex", out tex);
return TryGetTexture(material, MaterialProperties.MainTexture, out tex);
}

public static bool TryGetFloat(this Material material, string propName, out float val)
Expand All @@ -25,6 +33,14 @@ public static bool TryGetFloat(this Material material, string propName, out floa
return true;
}

public static bool TryGetFloat(this Material material, int propId, out float val)
{
val = 0;
if (!material.HasProperty(propId)) return false;
val = material.GetFloat(propId);
return true;
}

public static void SetMainColor(this Material material, Color color)
{
if (material.HasProperty(MaterialProperties.MainColor))
Expand All @@ -36,9 +52,16 @@ public static void SetMainColor(this Material material, Color color)

internal static class MaterialProperties
{
public static readonly string MainColor = "_Color";
public static readonly string CustomColors = "_CustomColors";
public static readonly string Glow = "_Glow";
public static readonly string Bloom = "_Bloom";
public static readonly int MainColor = Shader.PropertyToID("_Color");
public static readonly int MainTexture = Shader.PropertyToID("_MainTex");
public static readonly int CustomColors = Shader.PropertyToID("_CustomColors");
public static readonly int Glow = Shader.PropertyToID("_Glow");
public static readonly int Bloom = Shader.PropertyToID("_Bloom");
}

internal static class MaterialAttributes
{
public static readonly string SfNoPreview = "SFNoPreview";
public static readonly string HideInSf = "HideInSF";
}
}
5 changes: 5 additions & 0 deletions SaberFactory/Helpers/MaterialPropertyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,11 @@ protected BaseProperty(Shader shader, int idx)
Attributes = shader.GetPropertyAttributes(idx).ToList();
Type = shader.GetPropertyType(idx);
}

public bool HasAttribute(string name)
{
return Attributes.Contains(name);
}
}
}
}
9 changes: 9 additions & 0 deletions SaberFactory/Installers/PluginMenuInstaller.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,15 @@ private void BindRemoteSabers()
CoverPath = "SaberFactory.Resources.Icons.SFSaber_Icon.png"
});

BindSaber(new RemoteLocationPart.InitData
{
Name = "SF Default 2018",
Author = "Toni Macaroni",
RemoteLocation = "https://github.com/ToniMacaroni/SaberFactoryV2/blob/main/Sabers/SFDefault2018.saber?raw=true",
Filename = "SF Default 2018.saber",
CoverPath = "SaberFactory.Resources.Icons.SFSaber_Icon.png"
});

BindSaber(new RemoteLocationPart.InitData
{
Name = "SF Saber",
Expand Down
13 changes: 10 additions & 3 deletions SaberFactory/Models/CustomSaber/CustomSaberModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,13 +96,20 @@ public override void SyncFrom(BasePieceModel otherModel)
var mat = TrailModel.Material.Material;

TrailModel.CopyFrom(otherCs.TrailModel);
if (string.IsNullOrWhiteSpace(TrailModel.TrailOrigin) ||
mat.shader.name == TrailModel.Material.Material.shader.name)

// if trail isn't from other saber just copy props
// if trail IS from other saber but shares the same shader just copy props
// otherwise (trail is from other saber and shaders are different) copy the whole material
if (mat!=null && (string.IsNullOrWhiteSpace(TrailModel.TrailOrigin) ||
mat.shader.name == TrailModel.Material.Material.shader.name))
{
// just copy props from other material to current
mat.CopyPropertiesFromMaterial(TrailModel.Material.Material);
TrailModel.Material.Material = mat;
}
else
{
mat.TryDestoryImmediate();
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion SaberFactory/UI/CustomSaber/CustomComponents/PropList.bsml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<div id="root-object" bg="round-rect-panel" bg-color="#FFFFFF05" xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
xsi:schemaLocation='https://monkeymanboy.github.io/BSML-Docs/ https://raw.githubusercontent.com/monkeymanboy/BSML-Docs/gh-pages/BSMLSchema.xsd'>
<sui.scroll-view id='item-container' size-delta-x='-2' child-control-height='true' spacing='10' pad-bottom='10' pad-top='10'/>
<sui.scroll-view id='item-container' size-delta-x='-2' child-control-height='true' spacing='2' pad-bottom='10' pad-top='10'/>
</div>
21 changes: 20 additions & 1 deletion SaberFactory/UI/CustomSaber/Popups/MaterialEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ private void SetMaterial(Material material)
{
EPropertyType type;

if(prop.HasAttribute(MaterialAttributes.HideInSf)) continue;

if (prop.Attributes.Contains("MaterialToggle") || prop.Name == "_CustomColors")
{
var floatProp = (ShaderPropertyInfo.ShaderFloat)prop;
Expand All @@ -89,11 +91,24 @@ void Callback(object obj)
else
{
type = GetTypeFromShaderType(prop.Type);

if (type == EPropertyType.Unhandled) continue;

var propObject = GetPropObject(prop.Type, prop.PropId, material);
var callback = ConstructCallback(prop.Type, prop.PropId, material);

props.Add(new PropertyDescriptor(prop.Description, type, propObject, callback));
var propertyDescriptor = new PropertyDescriptor(prop.Description, type, propObject, callback);

if (prop is ShaderPropertyInfo.ShaderRange range)
{
propertyDescriptor.AddtionalData = new Vector2(range.Min, range.Max);
}
else if (prop is ShaderPropertyInfo.ShaderTexture texProp)
{
propertyDescriptor.AddtionalData = !prop.HasAttribute(MaterialAttributes.SfNoPreview);
}

props.Add(propertyDescriptor);
}
}

Expand All @@ -105,6 +120,7 @@ private EPropertyType GetTypeFromShaderType(ShaderPropertyType type)
return type switch
{
ShaderPropertyType.Float => EPropertyType.Float,
ShaderPropertyType.Range => EPropertyType.Float,
ShaderPropertyType.Color => EPropertyType.Color,
ShaderPropertyType.Texture => EPropertyType.Texture,
_ => EPropertyType.Unhandled
Expand All @@ -116,6 +132,7 @@ private object GetPropObject(ShaderPropertyType type, int propId, Material mater
return type switch
{
ShaderPropertyType.Float => material.GetFloat(propId),
ShaderPropertyType.Range => material.GetFloat(propId),
ShaderPropertyType.Color => material.GetColor(propId),
ShaderPropertyType.Texture => material.GetTexture(propId),
_ => null
Expand All @@ -128,6 +145,8 @@ private Action<object> ConstructCallback(ShaderPropertyType type, int propId, Ma
{
ShaderPropertyType.Float => (obj) => { material.SetFloat(propId, (float)obj); }
,
ShaderPropertyType.Range => (obj) => { material.SetFloat(propId, (float)obj); }
,
ShaderPropertyType.Color => (obj) => { material.SetColor(propId, (Color)obj); }
,
ShaderPropertyType.Texture => (obj) => { material.SetTexture(propId, (Texture2D)obj); }
Expand Down
7 changes: 4 additions & 3 deletions SaberFactory/UI/CustomSaber/Views/NavigationView.bsml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<vertical spacing='0.5' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
xsi:schemaLocation='https://monkeymanboy.github.io/BSML-Docs/ https://raw.githubusercontent.com/monkeymanboy/BSML-Docs/gh-pages/BSMLSchema.xsd'>

<vertical pref-width='16' background='round-rect-panel' bg-color='#666666bb' pad='2'>
<vertical pref-width='14.5' background='round-rect-panel' bg-color='#666666bb' pad='2'>
<macro.for-each items='nav-buttons'>
<sui.nav-button pref-width='12'
id='button'
Expand All @@ -22,9 +22,10 @@
</sui.nav-button>
</vertical>

<vertical pref-width='16' background='round-rect-panel' bg-color='#666666bb' pad-left='3' pad-right='3' pad-top='2'
pad-bottom='2' preferred-height='16'>
<vertical pref-width='14.5' background='round-rect-panel' bg-color='#666666bb' pad-left='2' pad-right='1' pad-top='1'
pad-bottom='1' preferred-height='16' vertical-fit='PreferredSize'>
<sui.icon-button
id='exit_btn'
pref-width='15'
pref-height='15'
on-click='clicked-exit'
Expand Down
6 changes: 6 additions & 0 deletions SaberFactory/UI/CustomSaber/Views/NavigationView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
using System.Collections.Generic;
using System.Threading;
using BeatSaberMarkupLanguage.Attributes;
using HMUI;
using SaberFactory.UI.CustomSaber.CustomComponents;
using SaberFactory.UI.Lib;
using TMPro;
using UnityEngine;
using Zenject;


Expand All @@ -21,12 +23,16 @@ internal class NavigationView : CustomViewController

[UIComponent("settings-notify-text")] private readonly TextMeshProUGUI _settingsNotifyText = null;
[UIValue("nav-buttons")] private List<object> _navButtons;
[UIObject("exit_btn")] private readonly GameObject _exitBtn = null;

private NavButton _currentSelectedNavButton;

[UIAction("#post-parse")]
private async void Setup()
{
// why is this icon so fucking big
_exitBtn.transform.Find("Content").GetComponent<StackLayoutGroup>().padding = new RectOffset(3, 3, 3, 3);

if (_navButtons is {} && _navButtons.Count > 0)
{
_currentSelectedNavButton = ((NavButtonWrapper) _navButtons[0]).NavButton;
Expand Down
9 changes: 6 additions & 3 deletions SaberFactory/UI/CustomSaber/Views/SaberSelectorView.bsml
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,20 @@
<horizontal spacing='5' child-expand-width='false' child-control-width='false' child-align='MiddleCenter'
pref-height='100'>
<vertical spacing='0.5'>
<sui.custom-list id='saber-list' title='Saber-Os' width='80' bg-color='#999' />
<vertical pref-width='80' bg='round-rect-panel' bg-color='#999' pad-left='2' pad-right='2' pad-top='1'
pad-bottom='1'>
<slider-setting text='Global Saber Width' value='saber-width' get-event='update-width' apply-on-change='true'
max='~global-saber-width-max' increment='0.05' />
<horizontal>
<sui.button text='Reload' pref-width='25' on-click='clicked-reload' />
<sui.button text='Reload All' pref-width='25' on-click='clicked-reloadall' />
<sui.button text='Delete' pref-width='25' on-click='clicked-delete' />
</horizontal>
</vertical>
<sui.custom-list id='saber-list' title='Saber-Os' height='78' width='80' bg-color='#999' />
<vertical pref-width='80' bg='round-rect-panel' bg-color='#999' pad-left='2' pad-right='2' pad-top='1'
pad-bottom='1'>
<slider-setting text='Global Saber Width' value='saber-width' get-event='update-width' apply-on-change='true'
max='~global-saber-width-max' increment='0.05' />
</vertical>
</vertical>
<vertical preferred-width='15' child-control-height='false' child-expand-height='false'>

Expand Down
6 changes: 3 additions & 3 deletions SaberFactory/UI/CustomSaber/Views/SettingsView.bsml
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@
on-click='profile-clicked'
icon='SaberFactory.Resources.Icons.profile.png'
hover-hint='Support me on Ko-Fi'
skew='0' show-line='false' normal-color='#00000000' hovered-color='#ffffff20' use-gradient='false'/>
skew='0' show-line='false' normal-color='#00000000' hovered-color='#00b6ff40' use-gradient='false'/>
<sui.icon-button
pad='1'
pref-width='15'
preferred-height='15'
on-click='discord-clicked'
icon='SaberFactory.Resources.Icons.discord.png'
hover-hint='Join our lovely server for help with the mod'
skew='0' show-line='false' normal-color='#00000000' hovered-color='#ffffff20' use-gradient='false'/>
skew='0' show-line='false' normal-color='#00000000' hovered-color='#00b6ff40' use-gradient='false'/>
<sui.icon-button
id='github-button'
active='false'
Expand All @@ -45,7 +45,7 @@
on-click='github-clicked'
icon='SaberFactory.Resources.Icons.update.png'
hover-hint='New version available on Github (click to see changelogs)'
skew='0' show-line='false' icon-color='#5dc968' normal-color='#00000000' hovered-color='#ffffff20' use-gradient='false'/>
skew='0' show-line='false' icon-color='#5dc968' normal-color='#00000000' hovered-color='#00b6ff40' use-gradient='false'/>
</horizontal>
</vertical>

Expand Down
10 changes: 9 additions & 1 deletion SaberFactory/UI/Lib/PropCells/FloatPropCell.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using BeatSaberMarkupLanguage.Attributes;
using System;
using BeatSaberMarkupLanguage.Attributes;
using BeatSaberMarkupLanguage.Components.Settings;
using TMPro;
using UnityEngine;
Expand All @@ -17,6 +18,13 @@ public override void SetData(PropertyDescriptor data)
if (!(data.PropObject is float val)) return;

OnChangeCallback = data.ChangedCallback;

if (data.AddtionalData is Vector2 minMax)
{
_sliderSetting.slider.minValue = minMax.x;
_sliderSetting.slider.maxValue = minMax.y;
}

_sliderSetting.slider.value = val;
_sliderSetting.ReceiveValue();
_sliderSettingText.text = data.Text;
Expand Down
6 changes: 5 additions & 1 deletion SaberFactory/UI/Lib/PropCells/TexturePropCell.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@ public override void SetData(PropertyDescriptor data)

OnChangeCallback = data.ChangedCallback;
_propName.text = ShortenText(data.Text, 14);
_propTexture.sprite = Utilities.LoadSpriteFromTexture(tex);

if (data.AddtionalData is bool showPreview && showPreview)
{
_propTexture.sprite = Utilities.LoadSpriteFromTexture(tex);
}

_backgroundImage.type = Image.Type.Sliced;
_backgroundImage.color = new Color(1, 0, 0, 0.5f);
Expand Down
2 changes: 2 additions & 0 deletions SaberFactory/UI/Lib/PropertyDescriptor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ public class PropertyDescriptor
public object PropObject;
public Action<object> ChangedCallback;

public object AddtionalData;

public PropertyDescriptor(string text, EPropertyType type, object propObject, Action<object> changedCallback)
{
Text = text;
Expand Down
2 changes: 1 addition & 1 deletion SaberFactory/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"id": "SaberFactory",
"name": "Saber Factory",
"author": "Toni Macaroni",
"version": "2.3.1",
"version": "2.3.2",
"description": "A highly customizable saber mod",
"gameVersion": "1.16.1",
"dependsOn": {
Expand Down
Binary file added Sabers/SFDefault2018.saber
Binary file not shown.
6 changes: 6 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
- fixed material editor not opening
- added `SFNoPreview` and `HideInSF` shader attributes
- material editor now takes the range of shader properties into consideration
- added option in config to hide the gameplay setup "sabers" button
- bunch of optimizations with material usage
- added 2018 default sabers

0 comments on commit faf58c4

Please sign in to comment.