Skip to content

Commit

Permalink
Remove all PhysBones action
Browse files Browse the repository at this point in the history
  • Loading branch information
Jared Williams committed May 31, 2022
1 parent dd80521 commit e933cce
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# 1.1.0

- remove all PhysBones action

# 1.0.0

- remove PhysBone action
Expand Down
4 changes: 4 additions & 0 deletions Editor/DataStructure.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,8 @@ class RemovePhysBoneAction : Action {
public string pathToGameObject;
public int physBoneIndex;
}

class RemoveAllPhysBonesAction : Action {
public string pathToGameObject;
}
}
6 changes: 6 additions & 0 deletions Editor/Exceptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,10 @@ public FailedToRemovePhysBoneException(string message) : base(message) {
public string pathToGameObject;
public int physBoneIndex;
}

public class FailedToRemoveAllPhysBonesException : System.Exception {
public FailedToRemoveAllPhysBonesException(string message) : base(message) {
}
public string pathToGameObject;
}
}
73 changes: 72 additions & 1 deletion Editor/VRC_Questifyer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ public class VRC_Questifyer : EditorWindow
enum Types {
SwitchToMaterial,
RemoveGameObject,
RemovePhysBone
RemovePhysBone,
RemoveAllPhysBones
}

VRCAvatarDescriptor sourceVrcAvatarDescriptor;
Expand Down Expand Up @@ -253,6 +254,8 @@ void RenderErrors() {
message = message + "Failed to remove game object: " + exception.Message + "\nGame object: " + (exception as FailedToRemoveGameObjectException).pathToGameObject;
} else if (exception is FailedToRemovePhysBoneException) {
message = message + "Failed to remove PhysBone: " + exception.Message + "\nPhysBone game object: " + (exception as FailedToRemovePhysBoneException).pathToGameObject + " (index " + (exception as FailedToRemovePhysBoneException).physBoneIndex + ")";
} else if (exception is FailedToRemoveAllPhysBonesException) {
message = message + "Failed to remove all PhysBones: " + exception.Message + "\nGame object: " + (exception as FailedToRemovePhysBoneException).pathToGameObject;
}

GUILayout.Label(message, guiStyle);
Expand Down Expand Up @@ -373,6 +376,20 @@ void RenderCreateActionForm() {
RenderPerformAtEndToggle();
RenderAddButton(selectedType, createFormFieldValue1 != "");
break;

case Types.RemoveAllPhysBones:
GUILayout.Label("Path to game object:");
createFormFieldValue1 = EditorGUILayout.TextField(createFormFieldValue1);

gameObjectToUse = (GameObject)EditorGUILayout.ObjectField("Search:", gameObjectToUse, typeof(GameObject));

if (gameObjectToUse != null) {
createFormFieldValue1 = Utils.GetRelativeGameObjectPath(gameObjectToUse, sourceVrcAvatarDescriptor.gameObject);
}

RenderPerformAtEndToggle();
RenderAddButton(selectedType, createFormFieldValue1 != "");
break;

default:
throw new System.Exception("Unknown type for dropdown!");
Expand Down Expand Up @@ -413,6 +430,12 @@ void AddAction(Types type, string fieldValue1, string fieldValue2, int fieldValu
};
break;

case Types.RemoveAllPhysBones:
action = new RemoveAllPhysBonesAction() {
pathToGameObject = fieldValue1
};
break;

default:
throw new System.Exception("Unknown type to add!");
break;
Expand Down Expand Up @@ -480,6 +503,8 @@ void RenderTypeForAction(Action action) {
label = "Remove Object";
} else if (action is RemovePhysBoneAction) {
label = "Remove PhysBone";
} else if (action is RemoveAllPhysBonesAction) {
label = "Remove All PhysBones";
} else {
throw new System.Exception("Unknown action!");
}
Expand Down Expand Up @@ -510,6 +535,9 @@ void RenderDataForAction(Action action) {
string pathToGameObject = (action as RemovePhysBoneAction).pathToGameObject;
string physBoneIndexStr = (action as RemovePhysBoneAction).physBoneIndex.ToString();
GUILayout.Label(pathToGameObject + " (" + physBoneIndexStr + ")", guiStyle);
} else if (action is RemoveAllPhysBonesAction) {
string pathToGameObject = (action as RemoveAllPhysBonesAction).pathToGameObject;
GUILayout.Label(pathToGameObject, guiStyle);
} else {
throw new System.Exception("Unknown action!");
}
Expand Down Expand Up @@ -608,6 +636,13 @@ void PerformAction(Action action, GameObject avatar) {
} catch (FailedToRemovePhysBoneException exception) {
errors.Add(exception);
}
} else if (action is RemoveAllPhysBonesAction) {
try {
string pathToGameObject = (action as RemoveAllPhysBonesAction).pathToGameObject;
RemoveAllPhysBonesOnGameObjectForAvatar(avatar, pathToGameObject);
} catch (FailedToRemovePhysBoneException exception) {
errors.Add(exception);
}
} else {
throw new System.Exception("Cannot perform action - unknown action type: " + nameof(action));
}
Expand Down Expand Up @@ -653,6 +688,30 @@ void RemovePhysBoneForAvatar(GameObject avatar, string pathToGameObject, int phy
DestroyImmediate(physBoneToRemove);
}

void RemoveAllPhysBonesOnGameObjectForAvatar(GameObject avatar, string pathToGameObject) {
Debug.Log("Removing all physbones at " + pathToGameObject + "...");

Transform gameObjectTransform = Utils.FindChild(avatar.transform, pathToGameObject);

if (gameObjectTransform == null) {
throw new FailedToRemoveAllPhysBonesException("Game object not found") {
pathToGameObject = pathToGameObject
};
}

VRCPhysBone[] physBones = gameObjectTransform.gameObject.GetComponents<VRCPhysBone>();

if (physBones.Length == 0) {
throw new FailedToRemoveAllPhysBonesException("No PhysBones found on the game object") {
pathToGameObject = pathToGameObject
};
}

foreach (VRCPhysBone physBone in physBones) {
DestroyImmediate(physBone);
}
}

void SwitchGameObjectMaterialForAvatar(GameObject avatar, string pathToRenderer, string pathToMaterial, int materialIndex = 0) {
Transform rendererTransform = Utils.FindChild(avatar.transform, pathToRenderer);

Expand Down Expand Up @@ -748,6 +807,13 @@ ActionJson ActionToJson(Action action) {
physBoneIndex = (action as RemovePhysBoneAction).physBoneIndex.ToString()
})
};
} else if (action is RemoveAllPhysBonesAction) {
actionJson = new ActionJson() {
type = Types.RemoveAllPhysBones.ToString(),
data = JObject.FromObject(new {
pathToGameObject = (action as RemoveAllPhysBonesAction).pathToGameObject
})
};
} else {
throw new System.Exception("Cannot convert action to JSON: unknown type " + nameof(action));
}
Expand Down Expand Up @@ -782,6 +848,11 @@ Action ActionJsonToAction(ActionJson actionJson) {
physBoneIndex = (int)jsonObject["physBoneIndex"]
};
break;
case Types.RemoveAllPhysBones:
action = new RemoveAllPhysBonesAction() {
pathToGameObject = (string)jsonObject["pathToGameObject"]
};
break;
default:
throw new System.Exception("Cannot convert action JSON to action: unknown type " + actionJson.type);
break;
Expand Down

0 comments on commit e933cce

Please sign in to comment.