private string GetConstructorFunc(IElement element, NamedObjectSave namedObject, ReferencedFileSave file) { string objectName = namedObject.FieldName; string width; string height; var widthVariable = namedObject.GetCustomVariable("Width"); var heightVariable = namedObject.GetCustomVariable("Height"); if (widthVariable?.Value == null) { width = "FlatRedBall.Camera.Main.DestinationRectangle.Width"; } else { width = widthVariable.Value.ToString(); } if (heightVariable?.Value == null) { height = "FlatRedBall.Camera.Main.DestinationRectangle.Height"; } else { height = heightVariable.Value.ToString(); } var toReturn = $"{objectName} = new Microsoft.Xna.Framework.Graphics.RenderTarget2D(FlatRedBall.FlatRedBallServices.GraphicsDevice, {width}, {height});"; return(toReturn); }
public static void SetVariableToDefault(NamedObjectSave currentNamedObject, string variableToSet) { // July 13, 2014 // This used to simply set the value to null, but why don't we remove it if it exists? // This way if an error is introduced by some plugin that sets the type to something invalid // the user can still remove it through this option and recover the type later. //currentNamedObject.SetPropertyValue(variableToSet, null); currentNamedObject.InstructionSaves.RemoveAll(item => item.Member == variableToSet); if (currentNamedObject.GetCustomVariable(variableToSet) != null) { // See if this variable is tunneled into in this element. // If so, set that value too. CustomVariableInNamedObject cvino = currentNamedObject.GetCustomVariable(variableToSet); object value = cvino.Value; foreach (CustomVariable customVariable in EditorLogic.CurrentElement.CustomVariables) { if (customVariable.SourceObject == currentNamedObject.InstanceName && customVariable.SourceObjectProperty == variableToSet) { customVariable.DefaultValue = value; break; } } GlueCommands.Self.RefreshCommands.RefreshPropertyGrid(); } }
void OnPropertyValueChanged(object s, PropertyValueChangedEventArgs e) { string propertyName = e.ChangedItem.Label; CustomVariable customVariable = null; if (mCurrentNos.SourceType == SourceType.Entity && !string.IsNullOrEmpty(mCurrentNos.SourceClassType)) { IElement entitySave = ObjectFinder.Self.GetIElement(mCurrentNos.SourceClassType); customVariable = entitySave.GetCustomVariable(propertyName).Clone(); } else { customVariable = new CustomVariable(); customVariable.Name = propertyName; } customVariable.DefaultValue = e.ChangedItem.Value; customVariable.Type = mCurrentNos.GetCustomVariable(propertyName).Type; mCurrentElementRuntime.SetCustomVariable(customVariable); if (mRuntimeOptions.ShouldSave) { GlueViewCommands.Self.GlueProjectSaveCommands.SaveGlux(); } }
private void ReactToFontSet(NamedObjectSave namedObjectSave, object oldValue) { string value = namedObjectSave.GetCustomVariable("Font").Value as string; if (!string.IsNullOrEmpty(value)) { IElement element = EditorLogic.CurrentElement; ReferencedFileSave referencedFileSave = element.GetReferencedFileSaveByInstanceNameRecursively(value); if (referencedFileSave != null) { string file = referencedFileSave.GetRelativePath(); file = ProjectManager.MakeAbsolute(file, true); string contents = FileManager.FromFileText(file); int size = StringFunctions.GetIntAfter( "size=", contents); float lineHeightInPixels = StringFunctions.GetIntAfter( "lineHeight=", contents); lineHeightInPixels /= 2.0f; namedObjectSave.SetPropertyValue("Scale", (float)lineHeightInPixels); namedObjectSave.SetPropertyValue("Spacing", (float)lineHeightInPixels); namedObjectSave.SetPropertyValue("NewLineDistance", (float)(lineHeightInPixels * 1.5f)); } } }
private static string ConstructorFunc(IElement element, NamedObjectSave namedObject, ReferencedFileSave referencedFile) { var emitterTypeSelected = namedObject.GetCustomVariable(LogicVarName)?.Value as string; var emitterGroupName = namedObject.GetCustomVariable(GroupVarName)?.Value as string; if (string.IsNullOrWhiteSpace(emitterTypeSelected)) { // No behavior selected, so we can't actually create the emitter return(string.Empty); } var parentString = element is EntitySave ? "this" : "null"; var groupNameString = $"\"{emitterGroupName}\""; var result = new StringBuilder(); result.AppendLine($"{namedObject.FieldName} = Parme.Frb.ParmeEmitterManager.Instance"); result.AppendLine($" .CreateEmitter(new {emitterTypeSelected}(), {parentString}, {groupNameString});"); return(result.ToString()); }
public void ReactToCustomVariableChangedValue(string changedMember, CustomVariable customVariable, object oldValue) { #region Name if (changedMember == "Name") { ReactToChangedCustomVariableName((string)oldValue, customVariable); } #endregion #region SetByDerived if (changedMember == "SetByDerived") { bool didErrorOccur = false; if (customVariable.SetByDerived && customVariable.IsShared) { MessageBox.Show("Variables that are IsShared cannot be SetByDerived"); didErrorOccur = true; } if (didErrorOccur) { customVariable.SetByDerived = (bool)oldValue; } else { ProjectManager.UpdateAllDerivedElementFromBaseValues(false, true); } } #endregion #region IsShared else if (changedMember == "IsShared") { HandleIsSharedVariableSet(customVariable, oldValue); } #endregion #region SouceObjectProperty else if (changedMember == "SourceObjectProperty") { // See if there is already a NOS that uses this SourceObject/SourceObjectProperty combo IElement currentElement = EditorLogic.CurrentElement; CustomVariable currentVariable = customVariable; if (!string.IsNullOrEmpty(currentVariable.SourceObject) && !string.IsNullOrEmpty(currentVariable.SourceObjectProperty)) { foreach (CustomVariable variableInLoop in currentElement.CustomVariables) { if (variableInLoop != currentVariable && !string.IsNullOrEmpty(variableInLoop.SourceObject) && currentVariable.SourceObject == variableInLoop.SourceObject && !string.IsNullOrEmpty(variableInLoop.SourceObjectProperty) && currentVariable.SourceObjectProperty == variableInLoop.SourceObjectProperty) { MessageBox.Show("There is already a variable that is modifying " + currentVariable.SourceObjectProperty + " on " + currentVariable.SourceObject); currentVariable.SourceObjectProperty = (string)oldValue; } } } } #endregion #region DefaultValue else if (changedMember == "DefaultValue") { customVariable.FixEnumerationTypes(); var currentElement = GlueState.Self.CurrentElement; if (!string.IsNullOrEmpty(customVariable.SourceObject)) { // See if the source NamedObjectSave has // this variable exposed, and if so, set that // variable too so the two mirror each other... // or make it null if this is a recasted variable. NamedObjectSave nos = currentElement.GetNamedObjectRecursively(customVariable.SourceObject); if (nos != null) { CustomVariableInNamedObject cvino = nos.GetCustomVariable(customVariable.SourceObjectProperty); // If the cvino is null, that means that the NOS doesn't have this exposed, so we don't // need to do anything. if (cvino != null) { if (string.IsNullOrEmpty(customVariable.OverridingPropertyType)) { cvino.Value = customVariable.DefaultValue; } else { cvino.Value = null; } } } } Plugins.PluginManager.ReactToElementVariableChange(currentElement, customVariable); } #endregion #region HasAccompanyingVelocityProperty else if (changedMember == "HasAccompanyingVelocityProperty") { ReactToChangedHasAccompanyingVelocityProperty(customVariable); } #endregion #region OverridingPropertyType else if (changedMember == "OverridingPropertyType") { if (customVariable.OverridingPropertyType != null) { customVariable.SetDefaultValueAccordingToType(customVariable.OverridingPropertyType); } GlueCommands.Self.RefreshCommands.RefreshPropertyGrid(); } #endregion #region Type else if (changedMember == "Type") { customVariable.SetDefaultValueAccordingToType(customVariable.Type); } #endregion }
private void ReactToTextureAddressMode(NamedObjectSave namedObjectSave, object oldValue) { bool isSprite = namedObjectSave.SourceType == SourceType.FlatRedBallType && namedObjectSave.SourceClassType == "Sprite"; if (isSprite) { var addressModeVariable = namedObjectSave.GetCustomVariable("TextureAddressMode"); if (addressModeVariable != null && addressModeVariable.Value != null && ((TextureAddressMode)(addressModeVariable.Value) == TextureAddressMode.Wrap || (TextureAddressMode)(addressModeVariable.Value) == TextureAddressMode.Mirror)) { // How big is the texture? var textureVariable = namedObjectSave.GetCustomVariable("Texture"); if (textureVariable != null && textureVariable.Value != null) { string value = textureVariable.Value as string; var rfs = namedObjectSave.GetContainer().GetReferencedFileSaveByInstanceName(value); if (rfs != null) { var width = ImageHeader.GetDimensions( ProjectManager.MakeAbsolute(rfs.Name)).Width; var height = ImageHeader.GetDimensions( ProjectManager.MakeAbsolute(rfs.Name)).Height; string whatIsWrong = null; if (FlatRedBall.Math.MathFunctions.IsPowerOfTwo(width) == false) { whatIsWrong = "This Sprite's texture (" + textureVariable.Value + ") has a width of " + width + " but it should be a power of two to use " + addressModeVariable.Value + " TextureAddressMode"; } if (FlatRedBall.Math.MathFunctions.IsPowerOfTwo(height) == false) { whatIsWrong = "This Sprite's texture (" + textureVariable.Value + ") has a height of " + height + " but it should be a power of two to use " + addressModeVariable.Value + " TextureAddressMode"; } if (!string.IsNullOrEmpty(whatIsWrong)) { whatIsWrong += "\nWhat would you like to do?"; MultiButtonMessageBox mbmb = new MultiButtonMessageBox(); mbmb.MessageText = whatIsWrong; mbmb.AddButton("Undo the change", DialogResult.Cancel); mbmb.AddButton("Keep the change (May cause runtime crashes)", DialogResult.Yes); var result = mbmb.ShowDialog(); if (result == DialogResult.Cancel) { addressModeVariable.Value = oldValue; } } } } } } }
public void ReactToNamedObjectChangedValue(string changedMember, string parent, object oldValue) { string combinedMember; if (string.IsNullOrEmpty(parent)) { combinedMember = changedMember; } else { combinedMember = parent + "." + changedMember; } NamedObjectSave namedObjectSave = EditorLogic.CurrentNamedObject; IElement element = EditorLogic.CurrentElement; if (PropertiesToMethods.ContainsKey(changedMember)) { PropertiesToMethods[changedMember](namedObjectSave, oldValue); } #region SourceType changed else if (changedMember == "SourceType") { bool didErrorOccur = false; if (didErrorOccur) { namedObjectSave.SourceType = (SourceType)oldValue; } else { if (namedObjectSave.SourceType == SourceType.Entity) { namedObjectSave.AddToManagers = true; } else if (namedObjectSave.SourceType == SourceType.File && namedObjectSave.GetContainerType() == ContainerType.Screen) { namedObjectSave.AddToManagers = false; } } } #endregion #region SourceClassType changed else if (changedMember == "SourceClassType") { ReactToChangedSourceClassType(namedObjectSave, oldValue); } #endregion #region SourceFile changed else if (changedMember == "SourceFile") { if (namedObjectSave.SourceFile != (string)oldValue) { // See if the current SourceName is valid or not List <string> availableSourceNames = AvailableNameablesStringConverter.GetAvailableNamedObjectSourceNames(namedObjectSave); bool isSourceNameValid = availableSourceNames.Contains(namedObjectSave.SourceName); if (!isSourceNameValid) { namedObjectSave.SourceName = "<NONE>"; } } } #endregion #region SourceName else if (changedMember == "SourceName") { // This needs to happen before we update custom properties ReactToChangedNosSourceName(namedObjectSave, oldValue as string); namedObjectSave.UpdateCustomProperties(); } #endregion #region InstanceName changed else if (changedMember == "InstanceName") { ReactToNamedObjectChangedInstanceName(namedObjectSave, oldValue); } #endregion #region SetByDerived Changed else if (changedMember == "SetByDerived") { if (namedObjectSave.SourceType == SourceType.Entity && !string.IsNullOrEmpty(namedObjectSave.SourceClassType)) { if (ProjectManager.VerifyReferenceGraph(ObjectFinder.Self.GetEntitySave(namedObjectSave.SourceClassType)) == ProjectManager.CheckResult.Failed) { namedObjectSave.SetByDerived = !namedObjectSave.SetByDerived; } } if (namedObjectSave.SetByDerived && namedObjectSave.ExposedInDerived) { // The user has just set SetByDerived to true, but ExposedInDerived means that // the derived expects that the base instantiates. We need to tell the user that // both values can't be true at the same time, and that ExposedInDerived will be set // to false. MessageBox.Show("You have set SetByDerived to true, but ExposedInDerived is also true. Both cannot be true at the same time " + "so Glue will set ExposedInDerived to false."); namedObjectSave.ExposedInDerived = false; } if (namedObjectSave.SourceType == SourceType.FlatRedBallType && namedObjectSave.IsList && namedObjectSave.SetByDerived == true && namedObjectSave.ContainedObjects.Count != 0) { MessageBox.Show("This list is not empty, so it can't be set to \"Set By Derived\". You must first empty the list", "Invalid Setting"); namedObjectSave.SetByDerived = false; } else { ProjectManager.UpdateAllDerivedElementFromBaseValues(false, true); } } #endregion #region ExposedInDerived Changed else if (changedMember == "ExposedInDerived") { if (namedObjectSave.SetByDerived && namedObjectSave.ExposedInDerived) { // See comment in ExposedByDerived block on why this occurs MessageBox.Show("You have set ExposedInDerived to true, but SetByDerived is also true. Both cannot be true at the same time " + "so Glue will set SetByDerived to false."); namedObjectSave.SetByDerived = false; } SetExposedByDerivedRecursively(namedObjectSave, oldValue); ProjectManager.UpdateAllDerivedElementFromBaseValues(false, true); } #endregion #region SourceClassGenericType else if (changedMember == "SourceClassGenericType") { ReactToSourceClassGenericType(namedObjectSave, oldValue); } #endregion #region IsDisabled else if (changedMember == "IsDisabled") { GlueState.Self.Find.ElementTreeNode(EditorLogic.CurrentElement).UpdateReferencedTreeNodes(); } #endregion #region SetByContainer Changed else if (changedMember == "SetByContainer") { if (namedObjectSave.SourceType == SourceType.Entity && !string.IsNullOrEmpty(namedObjectSave.SourceClassType)) { if (ProjectManager.VerifyReferenceGraph(ObjectFinder.Self.GetEntitySave(namedObjectSave.SourceClassType)) == ProjectManager.CheckResult.Failed) { namedObjectSave.SetByContainer = !namedObjectSave.SetByContainer; } } List <IElement> derivedElements = ObjectFinder.Self.GetAllElementsThatInheritFrom( EditorLogic.CurrentElement.Name); foreach (IElement derived in derivedElements) { foreach (NamedObjectSave nos in derived.NamedObjects) { if (nos.InstanceName == namedObjectSave.InstanceName) { nos.SetByContainer = namedObjectSave.SetByContainer; } } } if (EditorLogic.CurrentEntitySave != null) { List <NamedObjectSave> entityNamedObjects = ObjectFinder.Self.GetAllNamedObjectsThatUseEntity(EditorLogic.CurrentEntitySave.Name); foreach (NamedObjectSave nos in entityNamedObjects) { nos.UpdateCustomProperties(); } } } #endregion #region AddToManagers Changed else if (changedMember == "AddToManagers") { if (namedObjectSave.AddToManagers && namedObjectSave.GetContainerType() == ContainerType.Screen && namedObjectSave.SourceType == SourceType.File) { ScreenSave screenSave = namedObjectSave.GetContainer() as ScreenSave; ReferencedFileSave rfs = screenSave.GetReferencedFileSave(namedObjectSave.SourceFile); if (rfs != null && !rfs.IsSharedStatic) { System.Windows.Forms.MessageBox.Show("This object comes from a file. Files which are part of Screens " + "are automatically added to the engine managers. " + "Adding this object would result in double-membership in the engine which may cause unexpected results. " + "\n\nGlue will now set this value back to false."); namedObjectSave.AddToManagers = false; } } } #endregion #region LayerOn else if (changedMember == "LayerOn") { if (namedObjectSave.IsList) { DialogResult result = DialogResult.No; if (string.IsNullOrEmpty(namedObjectSave.LayerOn)) { result = MessageBox.Show("Do you want to remove every object in the List " + namedObjectSave.InstanceName + " from its Layer?", "Remove all from Layer?", MessageBoxButtons.YesNo); } else { result = MessageBox.Show("Do you want to add every object contained in the List " + namedObjectSave.InstanceName + " to the Layer " + namedObjectSave.LayerOn + "?", "Add all to Layer?", MessageBoxButtons.YesNo); } if (result == DialogResult.Yes) { namedObjectSave.SetLayerRecursively(namedObjectSave.LayerOn); } } } #endregion #region IsContainer else if (changedMember == "IsContainer") { HandleChangedIsContainer(namedObjectSave, element); } #endregion #region AttachToCamera else if (changedMember == "AttachToCamera") { if (namedObjectSave.IsList) { DialogResult result = DialogResult.No; if (namedObjectSave.AttachToCamera) { result = MessageBox.Show("Do you want to attach every object contained in the list " + namedObjectSave.InstanceName + " to the Camera?", "Attach all to Camera?", MessageBoxButtons.YesNo); } else { result = MessageBox.Show("Do you want to detach every object contained in the list " + namedObjectSave.InstanceName + " from the Camera?", "Detach all from the Camera?", MessageBoxButtons.YesNo); } if (result == DialogResult.Yes) { namedObjectSave.SetAttachToCameraRecursively(namedObjectSave.AttachToCamera); } } } #endregion #region DestinationRectangle.Y (for Layers) else if (parent == "DestinationRectangle" && changedMember == "Y") { // If the Y is odd, we should warn the user that it should be even // or else text will draw incorrectly if (namedObjectSave.DestinationRectangle.HasValue && namedObjectSave.DestinationRectangle.Value.Y % 2 == 1) { MessageBox.Show("Setting an odd value to the DestinationRectangle's Y may cause text to render improperly. An " + "even value is recommended"); } } #endregion #region RemoveFromManagersWhenInvisible else if (changedMember == "RemoveFromManagersWhenInvisible") { // is this an Entity instance? if (namedObjectSave.SourceType == SourceType.Entity && namedObjectSave.RemoveFromManagersWhenInvisible) { var entitySave = ObjectFinder.Self.GetEntitySave(namedObjectSave.SourceClassType); if (entitySave != null) { // Is this CreatedByOtherEntities? if (!entitySave.CreatedByOtherEntities) { MessageBox.Show("The Entity " + entitySave + " should have its CreatedByOtherEntities set to true to enable " + "visibility-based removal to work properly"); } } } } #endregion else if (namedObjectSave?.GetCustomVariable(changedMember) != null) { // See if this variable is tunneled into in this element. // If so, set that value too. CustomVariableInNamedObject cvino = namedObjectSave.GetCustomVariable(changedMember); object value = cvino.Value; foreach (CustomVariable customVariable in EditorLogic.CurrentElement.CustomVariables) { if (customVariable.SourceObject == namedObjectSave.InstanceName && customVariable.SourceObjectProperty == changedMember) { // The custom variable may have a different type: if (!string.IsNullOrEmpty(customVariable.OverridingPropertyType)) { // it does, so convert Type overridingType = TypeManager.GetTypeFromString(customVariable.OverridingPropertyType); customVariable.DefaultValue = System.Convert.ChangeType(value, overridingType); } else { customVariable.DefaultValue = value; } break; } } } // If we changed BitmapFont and if the NOS is marked as PixelPerfect // and if it's a Text object, then we should set the Scale, Spacing, and // NewLineDistance according to the set BitmapFont // We don't do an else because there could be a CustomVariable by the name // of BitmapFont as well, and we dont' want to eliminate that. if (changedMember == "Font" && namedObjectSave.SourceType == SourceType.FlatRedBallType && namedObjectSave.SourceClassType == "Text" && namedObjectSave.IsPixelPerfect) { ReactToFontSet(namedObjectSave, oldValue); } PropertyGridHelper.UpdateNamedObjectDisplay(); PluginManager.ReactToNamedObjectChangedValue(changedMember, oldValue); }
public static void ReactToRightClick() { #region Get the context menu PropertyGrid propertyGrid = MainGlueWindow.Self.PropertyGrid; mHighlightedCustomVariable = null; if (propertyGrid.ContextMenu == null) { propertyGrid.ContextMenu = new ContextMenu(); } ContextMenu contextMenu = propertyGrid.ContextMenu; contextMenu.MenuItems.Clear(); #endregion string label = propertyGrid.SelectedGridItem.Label; #region If there is a current StateSave if (EditorLogic.CurrentStateSave != null) { // Assume that it's a variable contextMenu.MenuItems.Add(mSetDefaultMenuItem); } #endregion #region If there is a current CustomVariable if (EditorLogic.CurrentCustomVariable != null) { // Assume that it's a variable contextMenu.MenuItems.Add(mSetDefaultMenuItem); } #endregion #region If there is a current NamedObject else if (EditorLogic.CurrentNamedObject != null) { NamedObjectSave namedObject = EditorLogic.CurrentNamedObject; // Is this a variable if (namedObject.GetCustomVariable(label) != null) { contextMenu.MenuItems.Add(mSetDefaultMenuItem); } else if (namedObject.IsLayer && label == "DestinationRectangle") { if (namedObject.DestinationRectangle == null || !namedObject.DestinationRectangle.HasValue) { contextMenu.MenuItems.Add(mUseCustomRectangle); } else { contextMenu.MenuItems.Add(mUseFullScreen); } } } #endregion #region If there is a current Entity Save (to be checked *after* the checks above) else if (EditorLogic.CurrentElement != null) { if (EditorLogic.CurrentTreeNode.IsRootCustomVariablesNode()) { CustomVariable customVariable = EditorLogic.CurrentElement.GetCustomVariable(label); if (customVariable != null) { mHighlightedCustomVariable = customVariable; contextMenu.MenuItems.Add(mSetDefaultMenuItem); } } else if (EditorLogic.CurrentEntitySave != null) { EntitySave sourceEntitySave = EditorLogic.CurrentEntitySave; if (label == "ImplementsIVisible" && sourceEntitySave != null && sourceEntitySave.ImplementsIVisible && sourceEntitySave.GetCustomVariable("Visible") == null ) { contextMenu.MenuItems.Add(mExposeVariable); } else if (label == "BaseEntity" && !string.IsNullOrEmpty(EditorLogic.CurrentEntitySave.BaseEntity)) { contextMenu.MenuItems.Add("Go to definition", GoToDefinitionClick); } } } #endregion PluginManager.ReactToPropertyGridRightClick(propertyGrid, contextMenu); }
private void GenerateCodeFor(NamedObjectSave namedObjectSave, ICodeBlock codeBlock) { if (namedObjectSave.DefinedByBase == false) { // don't generate if it's defined by base, the base class defines the properties // for now. Maybe eventually derived classes can override or change the behavior? // Not sure, that definitely does not seem like standard behavior, so maybe we leave // it to codegen. var sourceName = namedObjectSave.SourceNameWithoutParenthesis; T Get <T>(string name) { return(namedObjectSave.Properties.GetValue <T>(name)); } var creationOptions = Get <CollisionCreationOptions>( nameof(TileShapeCollectionPropertiesViewModel.CollisionCreationOptions)); var sourceType = namedObjectSave.SourceType; if (sourceType == SourceType.File) { codeBlock.Line($"//The NamedObject {namedObjectSave} has a SourceType of {sourceType}, so it may not instantiate " + $"properly. If you are experiencing a NullReferenceException, consider changing the" + $"SourceType to {SourceType.FlatRedBallType}"); } var isVisible = namedObjectSave.GetCustomVariable("Visible")?.ValueAsBool == true; if (!isVisible) { codeBlock.Line("// normally we wait to set variables until after the object is created, but in this case if the"); codeBlock.Line("// TileShapeCollection doesn't have its Visible set before creating the tiles, it can result in"); codeBlock.Line("// really bad performance issues, as shapes will be made visible, then invisible. Really bad perf!"); var ifBlock = codeBlock.If($"{namedObjectSave.InstanceName} != null"); { ifBlock.Line($"{namedObjectSave.InstanceName}.Visible = false;"); } } switch (creationOptions) { case CollisionCreationOptions.Empty: // do nothing break; case CollisionCreationOptions.FillCompletely: GenerateFillCompletely(namedObjectSave, codeBlock); break; case CollisionCreationOptions.BorderOutline: GenerateBorderOutline(namedObjectSave, codeBlock); break; case CollisionCreationOptions.FromLayer: // not handled: GenerateFromLayerCollision(namedObjectSave, codeBlock); break; case CollisionCreationOptions.FromProperties: GenerateFromProperties(namedObjectSave, codeBlock); break; case CollisionCreationOptions.FromType: GenerateFromTileType(namedObjectSave, codeBlock); break; } } }
private static object GetDefaultValueFor(NamedObjectSave nos, string property, string overridingType) { if (!string.IsNullOrEmpty(overridingType)) { Type overridingTypeAsType = TypeManager.GetTypeFromString(overridingType); string valueAsString = TypeManager.GetDefaultForType(overridingType); return(PropertyValuePair.ConvertStringToType(valueAsString, overridingTypeAsType)); } switch (nos.SourceType) { case SourceType.File: if (!string.IsNullOrEmpty(nos.SourceFile) && !string.IsNullOrEmpty(nos.SourceNameWithoutParenthesis)) { string absoluteFileName = ProjectManager.MakeAbsolute(nos.SourceFile, true); return(ContentParser.GetValueForProperty(absoluteFileName, nos.SourceNameWithoutParenthesis, property)); } break; case SourceType.Entity: if (!string.IsNullOrEmpty(nos.SourceClassType)) { IElement element = ObjectFinder.Self.GetIElement(nos.SourceClassType); if (element != null) { CustomVariable customVariable = element.GetCustomVariable(property); if (customVariable != null) { return(GetDefaultValueFor(customVariable, element)); } else if (property == "Visible" && element is EntitySave && ((EntitySave)element).ImplementsIVisible) { return(true); } else if (property == "Enabled" && element is EntitySave && ((EntitySave)element).ImplementsIWindow) { return(true); } } } break; case SourceType.FlatRedBallType: if (!string.IsNullOrEmpty(nos.SourceClassType)) { // See if there is a variable set for this already - there may be // now that we allow users to specify values right on FlatRedBall-type // NamedObjectSaves InstructionSave instructionSave = nos.GetCustomVariable(property); if (instructionSave != null && instructionSave.Value != null) { return(instructionSave.Value); } else { object value = GetExceptionForFlatRedBallTypeDefaultValue(nos, property); if (value != null) { return(value); } else { string classType = nos.SourceClassType; value = GetDefaultValueForPropertyInType(property, classType); return(value); } } } break; } return(null); }
public void ReactToCustomVariableChangedValue(string changedMember, CustomVariable customVariable, object oldValue) { #region Name if (changedMember == nameof(CustomVariable.Name)) { ReactToChangedCustomVariableName((string)oldValue, customVariable); } #endregion #region SetByDerived if (changedMember == nameof(CustomVariable.SetByDerived)) { bool didErrorOccur = false; if (customVariable.SetByDerived && customVariable.IsShared) { MessageBox.Show("Variables that are IsShared cannot be SetByDerived"); didErrorOccur = true; } if (didErrorOccur) { customVariable.SetByDerived = (bool)oldValue; } else { ProjectManager.UpdateAllDerivedElementFromBaseValues(true); } } #endregion #region IsShared else if (changedMember == nameof(CustomVariable.IsShared)) { HandleIsSharedVariableSet(customVariable, oldValue); } #endregion #region Scope else if (changedMember == nameof(CustomVariable.Scope)) { HandleScopeSet(customVariable, oldValue); } #endregion #region SouceObjectProperty else if (changedMember == nameof(CustomVariable.SourceObjectProperty)) { // See if there is already a NOS that uses this SourceObject/SourceObjectProperty combo IElement currentElement = EditorLogic.CurrentElement; CustomVariable currentVariable = customVariable; if (!string.IsNullOrEmpty(currentVariable.SourceObject) && !string.IsNullOrEmpty(currentVariable.SourceObjectProperty)) { foreach (CustomVariable variableInLoop in currentElement.CustomVariables) { if (variableInLoop != currentVariable && !string.IsNullOrEmpty(variableInLoop.SourceObject) && currentVariable.SourceObject == variableInLoop.SourceObject && !string.IsNullOrEmpty(variableInLoop.SourceObjectProperty) && currentVariable.SourceObjectProperty == variableInLoop.SourceObjectProperty) { MessageBox.Show("There is already a variable that is modifying " + currentVariable.SourceObjectProperty + " on " + currentVariable.SourceObject); currentVariable.SourceObjectProperty = (string)oldValue; } } } } #endregion #region DefaultValue else if (changedMember == nameof(CustomVariable.DefaultValue)) { customVariable.FixEnumerationTypes(); var currentElement = GlueState.Self.CurrentElement; if (!string.IsNullOrEmpty(customVariable.SourceObject)) { // See if the source NamedObjectSave has // this variable exposed, and if so, set that // variable too so the two mirror each other... // or make it null if this is a recasted variable. NamedObjectSave nos = currentElement.GetNamedObjectRecursively(customVariable.SourceObject); if (nos != null) { CustomVariableInNamedObject cvino = nos.GetCustomVariable(customVariable.SourceObjectProperty); // If the cvino is null, that means that the NOS doesn't have this exposed, so we don't // need to do anything. if (cvino != null) { if (string.IsNullOrEmpty(customVariable.OverridingPropertyType)) { cvino.Value = customVariable.DefaultValue; } else { cvino.Value = null; } } } } Plugins.PluginManager.ReactToElementVariableChange(currentElement, customVariable); } #endregion #region HasAccompanyingVelocityProperty else if (changedMember == nameof(CustomVariable.HasAccompanyingVelocityProperty)) { ReactToChangedHasAccompanyingVelocityProperty(customVariable); } #endregion #region OverridingPropertyType else if (changedMember == nameof(CustomVariable.OverridingPropertyType)) { if (customVariable.OverridingPropertyType != null) { customVariable.SetDefaultValueAccordingToType(customVariable.OverridingPropertyType); } GlueCommands.Self.RefreshCommands.RefreshPropertyGrid(); } #endregion #region Type else if (changedMember == nameof(CustomVariable.Type)) { var currentValue = customVariable.DefaultValue; var oldType = (string)oldValue; bool wasAbleToConvert = false; if (currentValue != null) { if (oldType == "int") { var valueAsInt = (int)currentValue; switch (customVariable.Type) { case "float": customVariable.DefaultValue = (float)valueAsInt; wasAbleToConvert = true; break; case "double": customVariable.DefaultValue = (double)valueAsInt; wasAbleToConvert = true; break; case "string": customVariable.DefaultValue = valueAsInt.ToString(); wasAbleToConvert = true; break; } } else if (oldType == "float") { var valueAsFloat = (float)currentValue; switch (customVariable.Type) { case "int": customVariable.DefaultValue = (int)valueAsFloat; wasAbleToConvert = true; break; case "double": customVariable.DefaultValue = (double)valueAsFloat; wasAbleToConvert = true; break; case "string": customVariable.DefaultValue = valueAsFloat.ToString(); wasAbleToConvert = true; break; } } else if (oldType == "double") { var valueAsDouble = (double)currentValue; switch (customVariable.Type) { case "int": customVariable.DefaultValue = (int)valueAsDouble; wasAbleToConvert = true; break; case "float": customVariable.DefaultValue = (float)valueAsDouble; wasAbleToConvert = true; break; case "string": customVariable.DefaultValue = valueAsDouble.ToString(); wasAbleToConvert = true; break; } } else if (oldType == "string") { var valueAsString = (string)currentValue; switch (customVariable.Type) { case "int": { if (int.TryParse(valueAsString, out int result)) { customVariable.DefaultValue = result; } } break; case "float": { if (float.TryParse(valueAsString, out float result)) { customVariable.DefaultValue = result; } } break; case "double": { if (double.TryParse(valueAsString, out double result)) { customVariable.DefaultValue = result; } } break; } } } if (wasAbleToConvert == false) { customVariable.SetDefaultValueAccordingToType(customVariable.Type); } // If the type changed, the Property Grid needs to be re-made so that the new // grid will have the right type for the DefaultValue cell: PropertyGridHelper.UpdateDisplayedPropertyGridProperties(); } #endregion }
private static object GetDefaultValueFor(NamedObjectSave nos, string property, string overridingType) { if (!string.IsNullOrEmpty(overridingType)) { Type overridingTypeAsType = TypeManager.GetTypeFromString(overridingType); string valueAsString = TypeManager.GetDefaultForType(overridingType); return PropertyValuePair.ConvertStringToType(valueAsString, overridingTypeAsType); } switch (nos.SourceType) { case SourceType.File: if (!string.IsNullOrEmpty(nos.SourceFile) && !string.IsNullOrEmpty(nos.SourceNameWithoutParenthesis)) { string absoluteFileName = ProjectManager.MakeAbsolute(nos.SourceFile, true); return ContentParser.GetValueForProperty(absoluteFileName, nos.SourceNameWithoutParenthesis, property); } break; case SourceType.Entity: if (!string.IsNullOrEmpty(nos.SourceClassType)) { IElement element = ObjectFinder.Self.GetIElement(nos.SourceClassType); if (element != null) { CustomVariable customVariable = element.GetCustomVariable(property); if (customVariable != null) { return GetDefaultValueFor(customVariable, element); } else if (property == "Visible" && element is EntitySave && ((EntitySave)element).ImplementsIVisible) { return true; } else if (property == "Enabled" && element is EntitySave && ((EntitySave)element).ImplementsIWindow) { return true; } } } break; case SourceType.FlatRedBallType: if (!string.IsNullOrEmpty(nos.SourceClassType)) { // See if there is a variable set for this already - there may be // now that we allow users to specify values right on FlatRedBall-type // NamedObjectSaves InstructionSave instructionSave = nos.GetCustomVariable(property); if (instructionSave != null && instructionSave.Value != null) { return instructionSave.Value; } else { object value = GetExceptionForFlatRedBallTypeDefaultValue(nos, property); if (value != null) { return value; } else { string classType = nos.SourceClassType; value = GetDefaultValueForPropertyInType(property, classType); return value; } } } break; } return null; }