private static SmarterButton AttachBehaviour(InternalProp thatProp, InternalModel thatModel, string buttonName) { if (thatModel == null) { string[] tokens = buttonName.Split('|'); if (tokens.Length == 2) { // First token is the button name, second is the prop ID. int propID; if (int.TryParse(tokens[1], out propID)) { if (propID < thatProp.internalModel.props.Count) { if (propID < 0) thatModel = thatProp.internalModel; else thatProp = thatProp.internalModel.props[propID]; buttonName = tokens[0].Trim(); } else Debug.LogError(string.Format("Could not find a prop with ID {0}", propID)); } } else buttonName = buttonName.Trim(); } try { GameObject buttonObject; buttonObject = thatModel == null ? thatProp.FindModelTransform(buttonName).gameObject : thatModel.FindModelTransform(buttonName).gameObject; SmarterButton thatComponent = buttonObject.GetComponent<SmarterButton>() ?? buttonObject.AddComponent<SmarterButton>(); return thatComponent; } catch { Debug.LogError(string.Format( "Could not register a button on transform named '{0}' in {2} named '{1}'. Check your configuration.", buttonName, thatModel == null ? thatProp.propName : thatModel.name, thatModel == null ? "prop" : "internal model")); } return null; }
void UpdateDIVA(InternalProp prop) { foreach (InternalModule mod in prop.internalModules) { if (mod is DIVA_InternalModelSwitch) { (mod as DIVA_InternalModelSwitch).SetVisible (); } } }
public static void CreateButton(InternalProp thatProp, string buttonName, Action handlerFunction, Action releaseHandlerFunction = null, InternalModel thatModel = null) { SmarterButton buttonBehaviour; if ((buttonBehaviour = AttachBehaviour(thatProp, thatModel, buttonName)) == null) { return; } buttonBehaviour.clickHandlers.Add(handlerFunction); if (releaseHandlerFunction != null) { buttonBehaviour.releaseHandlers.Add(releaseHandlerFunction); } buttonBehaviour.part = (thatModel == null) ? thatProp.part : thatModel.part; }
public static void CreateButton(InternalProp thatProp, string buttonName, MonitorPage thatPage, Action<MonitorPage> handlerFunction, InternalModel thatModel = null) { SmarterButton buttonBehaviour; if ((buttonBehaviour = AttachBehaviour(thatProp, thatModel, buttonName)) == null) { return; } foreach (PageTriggerSet pageset in buttonBehaviour.pageTriggers) { if (pageset.Add(handlerFunction, thatPage)) { return; } } buttonBehaviour.pageTriggers.Add(new PageTriggerSet(handlerFunction, thatPage)); buttonBehaviour.part = (thatModel == null) ? thatProp.part : thatModel.part; }
public static void CreateButton(InternalProp thatProp, string buttonName, int numericID, Action<int> clickHandlerFunction, Action<int> releaseHandlerFunction, InternalModel thatModel = null) { SmarterButton buttonBehaviour; if ((buttonBehaviour = AttachBehaviour(thatProp, thatModel, buttonName)) == null) { return; } buttonBehaviour.clickHandlersID.Add(new HandlerID { function = clickHandlerFunction, idValue = numericID }); buttonBehaviour.releaseHandlersID.Add(new HandlerID { function = releaseHandlerFunction, idValue = numericID }); buttonBehaviour.part = (thatModel == null) ? thatProp.part : thatModel.part; }
public SwitchInfo(InternalProp prop) { this.prop = prop; }
private static Texture2D LoadFont(object caller, InternalProp thisProp, string location) { Texture2D font = null; if (!string.IsNullOrEmpty(location)) { if (GameDatabase.Instance.ExistsTexture(location.EnforceSlashes())) { font = GameDatabase.Instance.GetTexture(location.EnforceSlashes(), false); JUtil.LogMessage(caller, "Loading font texture from URL \"{0}\"", location); } else { font = (Texture2D)thisProp.FindModelTransform(location).GetComponent<Renderer>().material.mainTexture; JUtil.LogMessage(caller, "Loading font texture from a transform named \"{0}\"", location); } } return font; }
private static Texture2D LoadFont(object caller, InternalProp thisProp, string location, bool extra) { Texture2D font = null; if (!string.IsNullOrEmpty(location)) { JUtil.LogMessage(caller, "Trying to locate \"{0}\" in GameDatabase...", location); if (GameDatabase.Instance.ExistsTexture(location.EnforceSlashes())) { font = GameDatabase.Instance.GetTexture(location.EnforceSlashes(), false); JUtil.LogMessage(caller, "Loading{1} font texture from URL, \"{0}\"", location, extra ? " extra" : string.Empty); } else { font = (Texture2D)thisProp.FindModelTransform(location).renderer.material.mainTexture; JUtil.LogMessage(caller, "Loading{1} font texture from a transform named, \"{0}\"", location, extra ? " extra" : string.Empty); } } return font; }
/// <summary> /// Initialize and configure the callback handler. /// </summary> /// <param name="node"></param> /// <param name="variableName"></param> /// <param name="thisProp"></param> public CallbackAnimationSet(ConfigNode node, string variableName, InternalProp thisProp) { currentState = false; if (!node.HasData) { throw new ArgumentException("No data?!"); } string[] tokens = { }; if (node.HasValue("scale")) { tokens = node.GetValue("scale").Split(','); } if (tokens.Length != 2) { throw new ArgumentException("Could not parse 'scale' parameter."); } RasterPropMonitorComputer rpmComp = RasterPropMonitorComputer.Instantiate(thisProp, true); variable = new VariableOrNumberRange(rpmComp, variableName, tokens[0], tokens[1]); // That takes care of the scale, now what to do about that scale: if (node.HasValue("reverse")) { if (!bool.TryParse(node.GetValue("reverse"), out reverse)) { throw new ArgumentException("So is 'reverse' true or false?"); } } if (node.HasValue("flash")) { if(!bool.TryParse(node.GetValue("flash"), out flash)) { throw new ArgumentException("So is 'reverse' true or false?"); } } else { flash = false; } if (node.HasValue("alarmSound")) { alarmSoundVolume = 0.5f; if (node.HasValue("alarmSoundVolume")) { alarmSoundVolume = float.Parse(node.GetValue("alarmSoundVolume")); } audioOutput = JUtil.SetupIVASound(thisProp, node.GetValue("alarmSound"), alarmSoundVolume, false); if (node.HasValue("alarmMustPlayOnce")) { if (!bool.TryParse(node.GetValue("alarmMustPlayOnce"), out alarmMustPlayOnce)) { throw new ArgumentException("So is 'alarmMustPlayOnce' true or false?"); } } if (node.HasValue("alarmShutdownButton")) { SmarterButton.CreateButton(thisProp, node.GetValue("alarmShutdownButton"), AlarmShutdown); } if (node.HasValue("alarmSoundLooping")) { if (!bool.TryParse(node.GetValue("alarmSoundLooping"), out alarmSoundLooping)) { throw new ArgumentException("So is 'alarmSoundLooping' true or false?"); } audioOutput.audio.loop = alarmSoundLooping; } inIVA = (CameraManager.Instance.currentCameraMode == CameraManager.CameraMode.IVA); GameEvents.OnCameraChange.Add(CameraChangeCallback); } if (node.HasValue("animationName")) { animationName = node.GetValue("animationName"); if (node.HasValue("animationSpeed")) { animationSpeed = float.Parse(node.GetValue("animationSpeed")); if (reverse) { animationSpeed = -animationSpeed; } } else { animationSpeed = 0.0f; } Animation[] anims = node.HasValue("animateExterior") ? thisProp.part.FindModelAnimators(animationName) : thisProp.FindModelAnimators(animationName); if (anims.Length > 0) { onAnim = anims[0]; onAnim.enabled = true; onAnim[animationName].speed = 0; onAnim[animationName].normalizedTime = reverse ? 1f : 0f; looping = node.HasValue("loopingAnimation"); if (looping) { onAnim[animationName].wrapMode = WrapMode.Loop; onAnim.wrapMode = WrapMode.Loop; onAnim[animationName].speed = animationSpeed; mode = Mode.LoopingAnimation; } else { onAnim[animationName].wrapMode = WrapMode.Once; mode = Mode.Animation; } onAnim.Play(); //alwaysActive = node.HasValue("animateExterior"); } else { throw new ArgumentException("Animation "+ animationName +" could not be found."); } if (node.HasValue("stopAnimationName")) { stopAnimationName = node.GetValue("stopAnimationName"); anims = node.HasValue("animateExterior") ? thisProp.part.FindModelAnimators(stopAnimationName) : thisProp.FindModelAnimators(stopAnimationName); if (anims.Length > 0) { offAnim = anims[0]; offAnim.enabled = true; offAnim[stopAnimationName].speed = 0; offAnim[stopAnimationName].normalizedTime = reverse ? 1f : 0f; if (looping) { offAnim[stopAnimationName].wrapMode = WrapMode.Loop; offAnim.wrapMode = WrapMode.Loop; offAnim[stopAnimationName].speed = animationSpeed; mode = Mode.LoopingAnimation; } else { offAnim[stopAnimationName].wrapMode = WrapMode.Once; mode = Mode.Animation; } } } } else if (node.HasValue("activeColor") && node.HasValue("passiveColor") && node.HasValue("coloredObject")) { string colorNameString = "_EmissiveColor"; if (node.HasValue("colorName")) { colorNameString = node.GetValue("colorName"); } colorName = Shader.PropertyToID(colorNameString); if (reverse) { activeColor = JUtil.ParseColor32(node.GetValue("passiveColor"), thisProp.part, ref rpmComp); passiveColor = JUtil.ParseColor32(node.GetValue("activeColor"), thisProp.part, ref rpmComp); } else { passiveColor = JUtil.ParseColor32(node.GetValue("passiveColor"), thisProp.part, ref rpmComp); activeColor = JUtil.ParseColor32(node.GetValue("activeColor"), thisProp.part, ref rpmComp); } Renderer colorShiftRenderer = thisProp.FindModelComponent<Renderer>(node.GetValue("coloredObject")); affectedMaterial = colorShiftRenderer.material; affectedMaterial.SetColor(colorName, passiveColor); mode = Mode.Color; } else if (node.HasValue("controlledTransform") && node.HasValue("localRotationStart") && node.HasValue("localRotationEnd")) { controlledTransform = thisProp.FindModelTransform(node.GetValue("controlledTransform").Trim()); initialRotation = controlledTransform.localRotation; if (node.HasValue("longPath")) { longPath = true; if (reverse) { vectorEnd = ConfigNode.ParseVector3(node.GetValue("localRotationStart")); vectorStart = ConfigNode.ParseVector3(node.GetValue("localRotationEnd")); } else { vectorStart = ConfigNode.ParseVector3(node.GetValue("localRotationStart")); vectorEnd = ConfigNode.ParseVector3(node.GetValue("localRotationEnd")); } } else { if (reverse) { rotationEnd = Quaternion.Euler(ConfigNode.ParseVector3(node.GetValue("localRotationStart"))); rotationStart = Quaternion.Euler(ConfigNode.ParseVector3(node.GetValue("localRotationEnd"))); } else { rotationStart = Quaternion.Euler(ConfigNode.ParseVector3(node.GetValue("localRotationStart"))); rotationEnd = Quaternion.Euler(ConfigNode.ParseVector3(node.GetValue("localRotationEnd"))); } } mode = Mode.Rotation; } else if (node.HasValue("controlledTransform") && node.HasValue("localTranslationStart") && node.HasValue("localTranslationEnd")) { controlledTransform = thisProp.FindModelTransform(node.GetValue("controlledTransform").Trim()); initialPosition = controlledTransform.localPosition; if (reverse) { vectorEnd = ConfigNode.ParseVector3(node.GetValue("localTranslationStart")); vectorStart = ConfigNode.ParseVector3(node.GetValue("localTranslationEnd")); } else { vectorStart = ConfigNode.ParseVector3(node.GetValue("localTranslationStart")); vectorEnd = ConfigNode.ParseVector3(node.GetValue("localTranslationEnd")); } mode = Mode.Translation; } else if (node.HasValue("controlledTransform") && node.HasValue("localScaleStart") && node.HasValue("localScaleEnd")) { controlledTransform = thisProp.FindModelTransform(node.GetValue("controlledTransform").Trim()); initialScale = controlledTransform.localScale; if (reverse) { vectorEnd = ConfigNode.ParseVector3(node.GetValue("localScaleStart")); vectorStart = ConfigNode.ParseVector3(node.GetValue("localScaleEnd")); } else { vectorStart = ConfigNode.ParseVector3(node.GetValue("localScaleStart")); vectorEnd = ConfigNode.ParseVector3(node.GetValue("localScaleEnd")); } mode = Mode.Scale; } else if (node.HasValue("controlledTransform") && node.HasValue("textureLayers") && node.HasValue("textureShiftStart") && node.HasValue("textureShiftEnd")) { affectedMaterial = thisProp.FindModelTransform(node.GetValue("controlledTransform").Trim()).GetComponent<Renderer>().material; var textureLayers = node.GetValue("textureLayers").Split(','); for (int i = 0; i < textureLayers.Length; ++i) { textureLayer.Add(textureLayers[i].Trim()); } if (reverse) { textureShiftEnd = ConfigNode.ParseVector2(node.GetValue("textureShiftStart")); textureShiftStart = ConfigNode.ParseVector2(node.GetValue("textureShiftEnd")); } else { textureShiftStart = ConfigNode.ParseVector2(node.GetValue("textureShiftStart")); textureShiftEnd = ConfigNode.ParseVector2(node.GetValue("textureShiftEnd")); } mode = Mode.TextureShift; } else if (node.HasValue("controlledTransform") && node.HasValue("textureLayers") && node.HasValue("textureScaleStart") && node.HasValue("textureScaleEnd")) { affectedMaterial = thisProp.FindModelTransform(node.GetValue("controlledTransform").Trim()).GetComponent<Renderer>().material; var textureLayers = node.GetValue("textureLayers").Split(','); for (int i = 0; i < textureLayers.Length; ++i) { textureLayer.Add(textureLayers[i].Trim()); } if (reverse) { textureScaleEnd = ConfigNode.ParseVector2(node.GetValue("textureScaleStart")); textureScaleStart = ConfigNode.ParseVector2(node.GetValue("textureScaleEnd")); } else { textureScaleStart = ConfigNode.ParseVector2(node.GetValue("textureScaleStart")); textureScaleEnd = ConfigNode.ParseVector2(node.GetValue("textureScaleEnd")); } mode = Mode.TextureScale; } else { throw new ArgumentException("Cannot initiate any of the possible action modes."); } TurnOff(); }
internal MASPageLineString(ConfigNode config, InternalProp prop, MASFlightComputer comp, MASMonitor monitor, Transform pageRoot, float depth) : base(config, prop, comp) { string startColorString = string.Empty; if (!config.TryGetValue("startColor", ref startColorString)) { throw new ArgumentException("Unable to find 'startColor' in LINE_STRING " + name); } string endColorString = string.Empty; if (!config.TryGetValue("endColor", ref endColorString)) { endColorString = string.Empty; } string startWidthString = string.Empty; if (!config.TryGetValue("startWidth", ref startWidthString)) { throw new ArgumentException("Unable to find 'startWidth' in LINE_STRING " + name); } string endWidthString = string.Empty; if (!config.TryGetValue("endWidth", ref endWidthString)) { endWidthString = string.Empty; } string[] vertexStrings = config.GetValues("vertex"); if (vertexStrings.Length < 2) { throw new ArgumentException("Insufficient number of 'vertex' entries in LINE_STRING " + name + " (must have at least 2)"); } string variableName = string.Empty; if (config.TryGetValue("variable", ref variableName)) { variableName = variableName.Trim(); } string rotationVariableName = string.Empty; config.TryGetValue("rotation", ref rotationVariableName); bool loop = false; config.TryGetValue("loop", ref loop); lineOrigin = new GameObject(); lineOrigin.name = Utility.ComposeObjectName(pageRoot.gameObject.name, this.GetType().Name, name, (int)(-depth / MASMonitor.depthDelta)); lineOrigin.layer = pageRoot.gameObject.layer; lineOrigin.transform.parent = pageRoot; lineOrigin.transform.position = pageRoot.position; lineOrigin.transform.Translate(monitor.screenSize.x * -0.5f + position.x, monitor.screenSize.y * 0.5f - position.y, depth); layerOrigin = pageRoot.position + new Vector3(monitor.screenSize.x * -0.5f, monitor.screenSize.y * 0.5f, depth); string positionString = string.Empty; if (!config.TryGetValue("position", ref positionString)) { position = Vector2.zero; throw new ArgumentException("Unable to find 'position' in LINE_STRING " + name); } else { string[] pos = Utility.SplitVariableList(positionString); if (pos.Length != 2) { throw new ArgumentException("Invalid number of values for 'position' in LINE_STRING " + name); } variableRegistrar.RegisterVariableChangeCallback(pos[0], (double newValue) => { position.x = (float)newValue; lineOrigin.transform.position = layerOrigin + new Vector3(position.x, -position.y, 0.0f); }); variableRegistrar.RegisterVariableChangeCallback(pos[1], (double newValue) => { position.y = (float)newValue; lineOrigin.transform.position = layerOrigin + new Vector3(position.x, -position.y, 0.0f); }); } // add renderer stuff lineMaterial = new Material(MASLoader.shaders["MOARdV/Monitor"]); lineRenderer = lineOrigin.AddComponent <LineRenderer>(); lineRenderer.useWorldSpace = false; lineRenderer.material = lineMaterial; lineRenderer.startColor = startColor; lineRenderer.endColor = endColor; lineRenderer.startWidth = startWidth; lineRenderer.endWidth = endWidth; lineRenderer.loop = loop; int numVertices = vertexStrings.Length; lineRenderer.positionCount = numVertices; vertices = new Vector3[numVertices]; string textureName = string.Empty; if (config.TryGetValue("texture", ref textureName)) { Texture tex = GameDatabase.Instance.GetTexture(textureName, false); if (tex != null) { lineMaterial.mainTexture = tex; inverseTextureWidth = 1.0f / (float)tex.width; usesTexture = true; } } for (int i = 0; i < numVertices; ++i) { // Need to make a copy of the value for the lambda capture, // otherwise we'll try using i = numVertices in the callbacks. int index = i; vertices[i] = Vector3.zero; string[] vtx = Utility.SplitVariableList(vertexStrings[i]); if (vtx.Length != 2) { throw new ArgumentException("vertex " + (i + 1).ToString() + " does not contain two value in LINE_STRING " + name); } Action <double> vertexX = (double newValue) => { vertices[index].x = (float)newValue; lineRenderer.SetPosition(index, vertices[index]); if (usesTexture) { RecalculateTextureScale(); } }; variableRegistrar.RegisterVariableChangeCallback(vtx[0], vertexX); Action <double> vertexY = (double newValue) => { // Invert the value, since we stipulate +y is down on the monitor. vertices[index].y = -(float)newValue; lineRenderer.SetPosition(index, vertices[index]); if (usesTexture) { RecalculateTextureScale(); } }; variableRegistrar.RegisterVariableChangeCallback(vtx[1], vertexY); } lineRenderer.SetPositions(vertices); RenderPage(false); if (!string.IsNullOrEmpty(variableName)) { // Disable the lines if we're in variable mode lineOrigin.SetActive(false); variableRegistrar.RegisterVariableChangeCallback(variableName, VariableCallback); } else { lineOrigin.SetActive(true); } if (!string.IsNullOrEmpty(rotationVariableName)) { variableRegistrar.RegisterVariableChangeCallback(rotationVariableName, RotationCallback); } if (string.IsNullOrEmpty(endColorString)) { Color32 col; if (comp.TryGetNamedColor(startColorString, out col)) { startColor = col; endColor = col; } else { string[] startColors = Utility.SplitVariableList(startColorString); if (startColors.Length < 3 || startColors.Length > 4) { throw new ArgumentException("startColor does not contain 3 or 4 values in LINE_STRING " + name); } Action <double> startColorR = (double newValue) => { startColor.r = Mathf.Clamp01((float)newValue * (1.0f / 255.0f)); lineRenderer.startColor = startColor; lineRenderer.endColor = startColor; }; variableRegistrar.RegisterVariableChangeCallback(startColors[0], startColorR); Action <double> startColorG = (double newValue) => { startColor.g = Mathf.Clamp01((float)newValue * (1.0f / 255.0f)); lineRenderer.startColor = startColor; lineRenderer.endColor = startColor; }; variableRegistrar.RegisterVariableChangeCallback(startColors[1], startColorG); Action <double> startColorB = (double newValue) => { startColor.b = Mathf.Clamp01((float)newValue * (1.0f / 255.0f)); lineRenderer.startColor = startColor; lineRenderer.endColor = startColor; }; variableRegistrar.RegisterVariableChangeCallback(startColors[2], startColorB); if (startColors.Length == 4) { Action <double> startColorA = (double newValue) => { startColor.a = Mathf.Clamp01((float)newValue * (1.0f / 255.0f)); lineRenderer.startColor = startColor; lineRenderer.endColor = startColor; }; variableRegistrar.RegisterVariableChangeCallback(startColors[3], startColorA); } } lineRenderer.startColor = startColor; lineRenderer.endColor = startColor; } else { Color32 col; if (comp.TryGetNamedColor(startColorString, out col)) { startColor = col; } else { string[] startColors = Utility.SplitVariableList(startColorString); if (startColors.Length < 3 || startColors.Length > 4) { throw new ArgumentException("startColor does not contain 3 or 4 values in LINE_STRING " + name); } Action <double> startColorR = (double newValue) => { startColor.r = Mathf.Clamp01((float)newValue * (1.0f / 255.0f)); lineRenderer.startColor = startColor; }; variableRegistrar.RegisterVariableChangeCallback(startColors[0], startColorR); Action <double> startColorG = (double newValue) => { startColor.g = Mathf.Clamp01((float)newValue * (1.0f / 255.0f)); lineRenderer.startColor = startColor; }; variableRegistrar.RegisterVariableChangeCallback(startColors[1], startColorG); Action <double> startColorB = (double newValue) => { startColor.b = Mathf.Clamp01((float)newValue * (1.0f / 255.0f)); lineRenderer.startColor = startColor; }; variableRegistrar.RegisterVariableChangeCallback(startColors[2], startColorB); if (startColors.Length == 4) { Action <double> startColorA = (double newValue) => { startColor.a = Mathf.Clamp01((float)newValue * (1.0f / 255.0f)); lineRenderer.startColor = startColor; }; variableRegistrar.RegisterVariableChangeCallback(startColors[3], startColorA); } } if (comp.TryGetNamedColor(endColorString, out col)) { endColor = col; } else { string[] endColors = Utility.SplitVariableList(endColorString); if (endColors.Length < 3 || endColors.Length > 4) { throw new ArgumentException("endColor does not contain 3 or 4 values in LINE_STRING " + name); } Action <double> endColorR = (double newValue) => { endColor.r = Mathf.Clamp01((float)newValue * (1.0f / 255.0f)); lineRenderer.endColor = endColor; }; variableRegistrar.RegisterVariableChangeCallback(endColors[0], endColorR); Action <double> endColorG = (double newValue) => { endColor.g = Mathf.Clamp01((float)newValue * (1.0f / 255.0f)); lineRenderer.endColor = endColor; }; variableRegistrar.RegisterVariableChangeCallback(endColors[1], endColorG); Action <double> endColorB = (double newValue) => { endColor.b = Mathf.Clamp01((float)newValue * (1.0f / 255.0f)); lineRenderer.endColor = endColor; }; variableRegistrar.RegisterVariableChangeCallback(endColors[2], endColorB); if (endColors.Length == 4) { Action <double> endColorA = (double newValue) => { endColor.a = Mathf.Clamp01((float)newValue * (1.0f / 255.0f)); lineRenderer.endColor = endColor; }; variableRegistrar.RegisterVariableChangeCallback(endColors[3], endColorA); } } lineRenderer.startColor = startColor; lineRenderer.endColor = endColor; } if (string.IsNullOrEmpty(endWidthString)) { // Monowidth line Action <double> startWidthAction = (double newValue) => { startWidth = (float)newValue; lineRenderer.startWidth = startWidth; lineRenderer.endWidth = startWidth; }; variableRegistrar.RegisterVariableChangeCallback(startWidthString, startWidthAction); } else { Action <double> startWidthAction = (double newValue) => { startWidth = (float)newValue; lineRenderer.startWidth = startWidth; }; variableRegistrar.RegisterVariableChangeCallback(startWidthString, startWidthAction); Action <double> endWidthAction = (double newValue) => { endWidth = (float)newValue; lineRenderer.endWidth = endWidth; }; variableRegistrar.RegisterVariableChangeCallback(endWidthString, endWidthAction); } }
internal MASComponentTextureScale(ConfigNode config, InternalProp prop, MASFlightComputer comp) : base(config, prop, comp) { string transform = string.Empty; if (!config.TryGetValue("transform", ref transform)) { throw new ArgumentException("Missing 'transform' in TEXTURE_SCALE " + name); } string layers = "_MainTex"; config.TryGetValue("layers", ref layers); string variableName = string.Empty; if (config.TryGetValue("variable", ref variableName)) { variableName = variableName.Trim(); } config.TryGetValue("blend", ref blend); Transform t = prop.FindModelTransform(transform); Renderer r = t.GetComponent <Renderer>(); localMaterial = r.material; layer = layers.Split(); int layerLength = layer.Length; for (int i = 0; i < layerLength; ++i) { layer[i] = layer[i].Trim(); } string startUVstring = string.Empty; if (!config.TryGetValue("startUV", ref startUVstring)) { throw new ArgumentException("Missing or invalid 'startUV' in TEXTURE_SCALE " + name); } else { string[] uvs = Utility.SplitVariableList(startUVstring); if (uvs.Length != 2) { throw new ArgumentException("Incorrect number of values in 'startUV' in TEXTURE_SCALE " + name); } variableRegistrar.RegisterVariableChangeCallback(uvs[0], (double newValue) => { startUV.x = (float)newValue; UpdateUVs(); }); variableRegistrar.RegisterVariableChangeCallback(uvs[1], (double newValue) => { startUV.y = (float)newValue; UpdateUVs(); }); } // Final validations if (!string.IsNullOrEmpty(variableName)) { string endUVstring = string.Empty; if (string.IsNullOrEmpty(variableName)) { throw new ArgumentException("Invalid or missing 'variable' in TEXTURE_SCALE " + name); } else if (!config.TryGetValue("endUV", ref endUVstring)) { throw new ArgumentException("Invalid or missing 'endUV' in TEXTURE_SCALE " + name); } else { string[] uvs = Utility.SplitVariableList(endUVstring); if (uvs.Length != 2) { throw new ArgumentException("Incorrect number of values in 'endUV' in TEXTURE_SCALE " + name); } variableRegistrar.RegisterVariableChangeCallback(uvs[0], (double newValue) => { endUV.x = (float)newValue; UpdateUVs(); }); variableRegistrar.RegisterVariableChangeCallback(uvs[1], (double newValue) => { endUV.y = (float)newValue; UpdateUVs(); }); } } if (!string.IsNullOrEmpty(variableName)) { variableRegistrar.RegisterVariableChangeCallback(variableName, VariableCallback); } }
public VariableAnimationSet(ConfigNode node, InternalProp thisProp) { part = thisProp.part; if (!node.HasData) { throw new ArgumentException("No data?!"); } comp = RasterPropMonitorComputer.Instantiate(thisProp); string[] tokens = { }; if (node.HasValue("scale")) { tokens = node.GetValue("scale").Split(','); } if (tokens.Length != 2) { throw new ArgumentException("Could not parse 'scale' parameter."); } if (node.HasValue("variableName")) { string variableName; variableName = node.GetValue("variableName").Trim(); scaleEnds[2] = new VariableOrNumber(variableName, this); } else if (node.HasValue("stateMethod")) { Func<bool> stateFunction = (Func<bool>)comp.GetMethod(node.GetValue("stateMethod").Trim(), thisProp, typeof(Func<bool>)); if (stateFunction != null) { scaleEnds[2] = new VariableOrNumber(stateFunction, this); } else { throw new ArgumentException("Unrecognized stateMethod"); } } else { throw new ArgumentException("Missing variable name."); } scaleEnds[0] = new VariableOrNumber(tokens[0], this); scaleEnds[1] = new VariableOrNumber(tokens[1], this); // That takes care of the scale, now what to do about that scale: if (node.HasValue("reverse")) { if (!bool.TryParse(node.GetValue("reverse"), out reverse)) { throw new ArgumentException("So is 'reverse' true or false?"); } } if (node.HasValue("animationName")) { animationName = node.GetValue("animationName"); if (node.HasValue("animationSpeed")) { animationSpeed = float.Parse(node.GetValue("animationSpeed")); if (reverse) { animationSpeed = -animationSpeed; } } else { animationSpeed = 0.0f; } Animation[] anims = node.HasValue("animateExterior") ? thisProp.part.FindModelAnimators(animationName) : thisProp.FindModelAnimators(animationName); if (anims.Length > 0) { onAnim = anims[0]; onAnim.enabled = true; onAnim[animationName].speed = 0; onAnim[animationName].normalizedTime = reverse ? 1f : 0f; looping = node.HasValue("loopingAnimation"); if (looping) { onAnim[animationName].wrapMode = WrapMode.Loop; onAnim.wrapMode = WrapMode.Loop; onAnim[animationName].speed = animationSpeed; mode = Mode.LoopingAnimation; } else { onAnim[animationName].wrapMode = WrapMode.Once; mode = Mode.Animation; } onAnim.Play(); alwaysActive = node.HasValue("animateExterior"); } else { throw new ArgumentException("Animation could not be found."); } if (node.HasValue("stopAnimationName")) { stopAnimationName = node.GetValue("stopAnimationName"); anims = node.HasValue("animateExterior") ? thisProp.part.FindModelAnimators(stopAnimationName) : thisProp.FindModelAnimators(stopAnimationName); if (anims.Length > 0) { offAnim = anims[0]; offAnim.enabled = true; offAnim[stopAnimationName].speed = 0; offAnim[stopAnimationName].normalizedTime = reverse ? 1f : 0f; if (looping) { offAnim[stopAnimationName].wrapMode = WrapMode.Loop; offAnim.wrapMode = WrapMode.Loop; offAnim[stopAnimationName].speed = animationSpeed; mode = Mode.LoopingAnimation; } else { offAnim[stopAnimationName].wrapMode = WrapMode.Once; mode = Mode.Animation; } } } } else if (node.HasValue("activeColor") && node.HasValue("passiveColor") && node.HasValue("coloredObject")) { if (node.HasValue("colorName")) colorName = node.GetValue("colorName"); passiveColor = ConfigNode.ParseColor32(node.GetValue("passiveColor")); activeColor = ConfigNode.ParseColor32(node.GetValue("activeColor")); colorShiftRenderer = thisProp.FindModelComponent<Renderer>(node.GetValue("coloredObject")); colorShiftRenderer.material.SetColor(colorName, reverse ? activeColor : passiveColor); mode = Mode.Color; } else if (node.HasValue("controlledTransform") && node.HasValue("localRotationStart") && node.HasValue("localRotationEnd")) { controlledTransform = thisProp.FindModelTransform(node.GetValue("controlledTransform").Trim()); initialRotation = controlledTransform.localRotation; if (node.HasValue("longPath")) { longPath = true; vectorStart = ConfigNode.ParseVector3(node.GetValue("localRotationStart")); vectorEnd = ConfigNode.ParseVector3(node.GetValue("localRotationEnd")); } else { rotationStart = Quaternion.Euler(ConfigNode.ParseVector3(node.GetValue("localRotationStart"))); rotationEnd = Quaternion.Euler(ConfigNode.ParseVector3(node.GetValue("localRotationEnd"))); } mode = Mode.Rotation; } else if (node.HasValue("controlledTransform") && node.HasValue("localTranslationStart") && node.HasValue("localTranslationEnd")) { controlledTransform = thisProp.FindModelTransform(node.GetValue("controlledTransform").Trim()); initialPosition = controlledTransform.localPosition; vectorStart = ConfigNode.ParseVector3(node.GetValue("localTranslationStart")); vectorEnd = ConfigNode.ParseVector3(node.GetValue("localTranslationEnd")); mode = Mode.Translation; } else if (node.HasValue("controlledTransform") && node.HasValue("localScaleStart") && node.HasValue("localScaleEnd")) { controlledTransform = thisProp.FindModelTransform(node.GetValue("controlledTransform").Trim()); initialScale = controlledTransform.localScale; vectorStart = ConfigNode.ParseVector3(node.GetValue("localScaleStart")); vectorEnd = ConfigNode.ParseVector3(node.GetValue("localScaleEnd")); mode = Mode.Scale; } else if (node.HasValue("controlledTransform") && node.HasValue("textureLayers") && node.HasValue("textureShiftStart") && node.HasValue("textureShiftEnd")) { affectedMaterial = thisProp.FindModelTransform(node.GetValue("controlledTransform").Trim()).renderer.material; textureLayer = node.GetValue("textureLayers"); textureShiftStart = ConfigNode.ParseVector2(node.GetValue("textureShiftStart")); textureShiftEnd = ConfigNode.ParseVector2(node.GetValue("textureShiftEnd")); mode = Mode.TextureShift; } else if (node.HasValue("controlledTransform") && node.HasValue("textureLayers") && node.HasValue("textureScaleStart") && node.HasValue("textureScaleEnd")) { affectedMaterial = thisProp.FindModelTransform(node.GetValue("controlledTransform").Trim()).renderer.material; textureLayer = node.GetValue("textureLayers"); textureScaleStart = ConfigNode.ParseVector2(node.GetValue("textureScaleStart")); textureScaleEnd = ConfigNode.ParseVector2(node.GetValue("textureScaleEnd")); mode = Mode.TextureScale; } else { throw new ArgumentException("Cannot initiate any of the possible action modes."); } if (node.HasValue("threshold")) { threshold = ConfigNode.ParseVector2(node.GetValue("threshold")); } resourceAmount = 0.0f; if (threshold != Vector2.zero) { thresholdMode = true; float min = Mathf.Min(threshold.x, threshold.y); float max = Mathf.Max(threshold.x, threshold.y); threshold.x = min; threshold.y = max; if (node.HasValue("flashingDelay")) { flashingDelay = double.Parse(node.GetValue("flashingDelay")); } if (node.HasValue("alarmSound")) { alarmSoundVolume = 0.5f; if (node.HasValue("alarmSoundVolume")) alarmSoundVolume = float.Parse(node.GetValue("alarmSoundVolume")); audioOutput = JUtil.SetupIVASound(thisProp, node.GetValue("alarmSound"), alarmSoundVolume, false); if (node.HasValue("alarmMustPlayOnce")) { if (!bool.TryParse(node.GetValue("alarmMustPlayOnce"), out alarmMustPlayOnce)) throw new ArgumentException("So is 'alarmMustPlayOnce' true or false?"); } if (node.HasValue("alarmShutdownButton")) SmarterButton.CreateButton(thisProp, node.GetValue("alarmShutdownButton"), AlarmShutdown); if (node.HasValue("alarmSoundLooping")) { if (!bool.TryParse(node.GetValue("alarmSoundLooping"), out alarmSoundLooping)) throw new ArgumentException("So is 'alarmSoundLooping' true or false?"); audioOutput.audio.loop = alarmSoundLooping; } } if (node.HasValue("resourceAmount")) { resourceAmount = float.Parse(node.GetValue("resourceAmount")); } TurnOff(); } }
/// <summary> /// Get a plugin or internal method. /// </summary> /// <param name="packedMethod">The method to fetch in the format ModuleName:MethodName</param> /// <param name="internalProp">The internal prop that should be used to instantiate InternalModule plugin methods.</param> /// <param name="delegateType">The expected signature of the method.</param> /// <returns></returns> public Delegate GetMethod(string packedMethod, InternalProp internalProp, Type delegateType) { Delegate returnValue = GetInternalMethod(packedMethod, delegateType); if (returnValue == null && internalProp != null) { returnValue = JUtil.GetMethod(packedMethod, internalProp, delegateType); } return returnValue; }
/// <summary> /// Release resources /// </summary> public override void ReleaseResources(MASFlightComputer comp, InternalProp internalProp) { callbackVariable.UnregisterNumericCallback(VariableCallback); this.comp = null; }
// MOARdV TODO: If I understand the Unity docs correctly, we are leaking // some things here (material .get methods make copies, for instance). // I haven't seen conclusive signs of destructors working in child // objects like this, so do I need a manual method? Or make it a MonoBehavior // with only the OnDestroy implemented? public VariableAnimationSet(ConfigNode node, InternalProp thisProp) { part = thisProp.part; if (!node.HasData) { throw new ArgumentException("No data?!"); } string[] tokens = { }; if (node.HasValue("scale")) { tokens = node.GetValue("scale").Split(','); } if (tokens.Length != 2) { throw new ArgumentException("Could not parse 'scale' parameter."); } string variableName = string.Empty; if (node.HasValue("variableName")) { variableName = node.GetValue("variableName").Trim(); } else if (node.HasValue("stateMethod")) { RPMVesselComputer comp = RPMVesselComputer.Instance(part.vessel); string stateMethod = node.GetValue("stateMethod").Trim(); // Verify the state method actually exists Func <bool> stateFunction = (Func <bool>)comp.GetMethod(stateMethod, thisProp, typeof(Func <bool>)); if (stateFunction != null) { variableName = "PLUGIN_" + stateMethod; } else { throw new ArgumentException("Unrecognized stateMethod"); } } else { throw new ArgumentException("Missing variable name."); } variable = new VariableOrNumberRange(variableName, tokens[0], tokens[1]); // That takes care of the scale, now what to do about that scale: if (node.HasValue("reverse")) { if (!bool.TryParse(node.GetValue("reverse"), out reverse)) { throw new ArgumentException("So is 'reverse' true or false?"); } } if (node.HasValue("animationName")) { animationName = node.GetValue("animationName"); if (node.HasValue("animationSpeed")) { animationSpeed = float.Parse(node.GetValue("animationSpeed")); if (reverse) { animationSpeed = -animationSpeed; } } else { animationSpeed = 0.0f; } Animation[] anims = node.HasValue("animateExterior") ? thisProp.part.FindModelAnimators(animationName) : thisProp.FindModelAnimators(animationName); if (anims.Length > 0) { onAnim = anims[0]; onAnim.enabled = true; onAnim[animationName].speed = 0; onAnim[animationName].normalizedTime = reverse ? 1f : 0f; looping = node.HasValue("loopingAnimation"); if (looping) { onAnim[animationName].wrapMode = WrapMode.Loop; onAnim.wrapMode = WrapMode.Loop; onAnim[animationName].speed = animationSpeed; mode = Mode.LoopingAnimation; } else { onAnim[animationName].wrapMode = WrapMode.Once; mode = Mode.Animation; } onAnim.Play(); alwaysActive = node.HasValue("animateExterior"); } else { throw new ArgumentException("Animation could not be found."); } if (node.HasValue("stopAnimationName")) { stopAnimationName = node.GetValue("stopAnimationName"); anims = node.HasValue("animateExterior") ? thisProp.part.FindModelAnimators(stopAnimationName) : thisProp.FindModelAnimators(stopAnimationName); if (anims.Length > 0) { offAnim = anims[0]; offAnim.enabled = true; offAnim[stopAnimationName].speed = 0; offAnim[stopAnimationName].normalizedTime = reverse ? 1f : 0f; if (looping) { offAnim[stopAnimationName].wrapMode = WrapMode.Loop; offAnim.wrapMode = WrapMode.Loop; offAnim[stopAnimationName].speed = animationSpeed; mode = Mode.LoopingAnimation; } else { offAnim[stopAnimationName].wrapMode = WrapMode.Once; mode = Mode.Animation; } } } } else if (node.HasValue("activeColor") && node.HasValue("passiveColor") && node.HasValue("coloredObject")) { if (node.HasValue("colorName")) { colorName = node.GetValue("colorName"); } if (reverse) { activeColor = ConfigNode.ParseColor32(node.GetValue("passiveColor")); passiveColor = ConfigNode.ParseColor32(node.GetValue("activeColor")); } else { passiveColor = ConfigNode.ParseColor32(node.GetValue("passiveColor")); activeColor = ConfigNode.ParseColor32(node.GetValue("activeColor")); } Renderer colorShiftRenderer = thisProp.FindModelComponent <Renderer>(node.GetValue("coloredObject")); affectedMaterial = colorShiftRenderer.material; affectedMaterial.SetColor(colorName, passiveColor); mode = Mode.Color; } else if (node.HasValue("controlledTransform") && node.HasValue("localRotationStart") && node.HasValue("localRotationEnd")) { controlledTransform = thisProp.FindModelTransform(node.GetValue("controlledTransform").Trim()); initialRotation = controlledTransform.localRotation; if (node.HasValue("longPath")) { longPath = true; if (reverse) { vectorEnd = ConfigNode.ParseVector3(node.GetValue("localRotationStart")); vectorStart = ConfigNode.ParseVector3(node.GetValue("localRotationEnd")); } else { vectorStart = ConfigNode.ParseVector3(node.GetValue("localRotationStart")); vectorEnd = ConfigNode.ParseVector3(node.GetValue("localRotationEnd")); } } else { if (reverse) { rotationEnd = Quaternion.Euler(ConfigNode.ParseVector3(node.GetValue("localRotationStart"))); rotationStart = Quaternion.Euler(ConfigNode.ParseVector3(node.GetValue("localRotationEnd"))); } else { rotationStart = Quaternion.Euler(ConfigNode.ParseVector3(node.GetValue("localRotationStart"))); rotationEnd = Quaternion.Euler(ConfigNode.ParseVector3(node.GetValue("localRotationEnd"))); } } mode = Mode.Rotation; } else if (node.HasValue("controlledTransform") && node.HasValue("localTranslationStart") && node.HasValue("localTranslationEnd")) { controlledTransform = thisProp.FindModelTransform(node.GetValue("controlledTransform").Trim()); initialPosition = controlledTransform.localPosition; if (reverse) { vectorEnd = ConfigNode.ParseVector3(node.GetValue("localTranslationStart")); vectorStart = ConfigNode.ParseVector3(node.GetValue("localTranslationEnd")); } else { vectorStart = ConfigNode.ParseVector3(node.GetValue("localTranslationStart")); vectorEnd = ConfigNode.ParseVector3(node.GetValue("localTranslationEnd")); } mode = Mode.Translation; } else if (node.HasValue("controlledTransform") && node.HasValue("localScaleStart") && node.HasValue("localScaleEnd")) { controlledTransform = thisProp.FindModelTransform(node.GetValue("controlledTransform").Trim()); initialScale = controlledTransform.localScale; if (reverse) { vectorEnd = ConfigNode.ParseVector3(node.GetValue("localScaleStart")); vectorStart = ConfigNode.ParseVector3(node.GetValue("localScaleEnd")); } else { vectorStart = ConfigNode.ParseVector3(node.GetValue("localScaleStart")); vectorEnd = ConfigNode.ParseVector3(node.GetValue("localScaleEnd")); } mode = Mode.Scale; } else if (node.HasValue("controlledTransform") && node.HasValue("textureLayers") && node.HasValue("textureShiftStart") && node.HasValue("textureShiftEnd")) { affectedMaterial = thisProp.FindModelTransform(node.GetValue("controlledTransform").Trim()).renderer.material; var textureLayers = node.GetValue("textureLayers").Split(','); for (int i = 0; i < textureLayers.Length; ++i) { textureLayer.Add(textureLayers[i].Trim()); } if (reverse) { textureShiftEnd = ConfigNode.ParseVector2(node.GetValue("textureShiftStart")); textureShiftStart = ConfigNode.ParseVector2(node.GetValue("textureShiftEnd")); } else { textureShiftStart = ConfigNode.ParseVector2(node.GetValue("textureShiftStart")); textureShiftEnd = ConfigNode.ParseVector2(node.GetValue("textureShiftEnd")); } mode = Mode.TextureShift; } else if (node.HasValue("controlledTransform") && node.HasValue("textureLayers") && node.HasValue("textureScaleStart") && node.HasValue("textureScaleEnd")) { affectedMaterial = thisProp.FindModelTransform(node.GetValue("controlledTransform").Trim()).renderer.material; var textureLayers = node.GetValue("textureLayers").Split(','); for (int i = 0; i < textureLayers.Length; ++i) { textureLayer.Add(textureLayers[i].Trim()); } if (reverse) { textureScaleEnd = ConfigNode.ParseVector2(node.GetValue("textureScaleStart")); textureScaleStart = ConfigNode.ParseVector2(node.GetValue("textureScaleEnd")); } else { textureScaleStart = ConfigNode.ParseVector2(node.GetValue("textureScaleStart")); textureScaleEnd = ConfigNode.ParseVector2(node.GetValue("textureScaleEnd")); } mode = Mode.TextureScale; } else { throw new ArgumentException("Cannot initiate any of the possible action modes."); } if (!(node.HasValue("maxRateChange") && float.TryParse(node.GetValue("maxRateChange"), out maxRateChange))) { maxRateChange = 0.0f; } if (maxRateChange >= 60.0f) { // Animation rate is too fast to even notice @60Hz maxRateChange = 0.0f; } else { lastAnimUpdate = Planetarium.GetUniversalTime(); } if (node.HasValue("threshold")) { threshold = ConfigNode.ParseVector2(node.GetValue("threshold")); } resourceAmount = 0.0f; if (threshold != Vector2.zero) { thresholdMode = true; float min = Mathf.Min(threshold.x, threshold.y); float max = Mathf.Max(threshold.x, threshold.y); threshold.x = min; threshold.y = max; if (node.HasValue("flashingDelay")) { flashingDelay = double.Parse(node.GetValue("flashingDelay")); } if (node.HasValue("alarmSound")) { alarmSoundVolume = 0.5f; if (node.HasValue("alarmSoundVolume")) { alarmSoundVolume = float.Parse(node.GetValue("alarmSoundVolume")); } audioOutput = JUtil.SetupIVASound(thisProp, node.GetValue("alarmSound"), alarmSoundVolume, false); if (node.HasValue("alarmMustPlayOnce")) { if (!bool.TryParse(node.GetValue("alarmMustPlayOnce"), out alarmMustPlayOnce)) { throw new ArgumentException("So is 'alarmMustPlayOnce' true or false?"); } } if (node.HasValue("alarmShutdownButton")) { SmarterButton.CreateButton(thisProp, node.GetValue("alarmShutdownButton"), AlarmShutdown); } if (node.HasValue("alarmSoundLooping")) { if (!bool.TryParse(node.GetValue("alarmSoundLooping"), out alarmSoundLooping)) { throw new ArgumentException("So is 'alarmSoundLooping' true or false?"); } audioOutput.audio.loop = alarmSoundLooping; } } if (node.HasValue("resourceAmount")) { resourceAmount = float.Parse(node.GetValue("resourceAmount")); if (node.HasValue("resourceName")) { resourceName = node.GetValue("resourceName"); } else { resourceName = "ElectricCharge"; } } TurnOff(Planetarium.GetUniversalTime()); } }
internal MASPageEllipse(ConfigNode config, InternalProp prop, MASFlightComputer comp, MASMonitor monitor, Transform pageRoot, float depth) : base(config, prop, comp) { string startColorString = string.Empty; if (!config.TryGetValue("startColor", ref startColorString)) { throw new ArgumentException("Unable to find 'startColor' in ELLIPSE " + name); } string endColorString = string.Empty; if (!config.TryGetValue("endColor", ref endColorString)) { endColorString = string.Empty; } string startWidthString = string.Empty; if (!config.TryGetValue("startWidth", ref startWidthString)) { throw new ArgumentException("Unable to find 'startWidth' in ELLIPSE " + name); } string endWidthString = string.Empty; if (!config.TryGetValue("endWidth", ref endWidthString)) { endWidthString = string.Empty; } numVertices = 0; if (!config.TryGetValue("vertexCount", ref numVertices)) { throw new ArgumentException("Unable to find 'vertexCount' in ELLIPSE " + name); } else if (numVertices < 3) { throw new ArgumentException("'vertexCount' must be at least 3 in ELLIPSE " + name); } Vector2 position = Vector2.zero; if (!config.TryGetValue("position", ref position)) { throw new ArgumentException("Unable to find 'position' in ELLIPSE " + name); } string variableName = string.Empty; if (config.TryGetValue("variable", ref variableName)) { variableName = variableName.Trim(); } string rotationVariableName = string.Empty; config.TryGetValue("rotation", ref rotationVariableName); lineOrigin = new GameObject(); lineOrigin.name = Utility.ComposeObjectName(pageRoot.gameObject.name, this.GetType().Name, name, (int)(-depth / MASMonitor.depthDelta)); lineOrigin.layer = pageRoot.gameObject.layer; lineOrigin.transform.parent = pageRoot; lineOrigin.transform.position = pageRoot.position; lineOrigin.transform.Translate(monitor.screenSize.x * -0.5f + position.x, monitor.screenSize.y * 0.5f - position.y, depth); // add renderer stuff lineMaterial = new Material(MASLoader.shaders["MOARdV/Monitor"]); lineRenderer = lineOrigin.AddComponent <LineRenderer>(); lineRenderer.useWorldSpace = false; lineRenderer.material = lineMaterial; lineRenderer.startColor = startColor; lineRenderer.endColor = endColor; lineRenderer.startWidth = startWidth; lineRenderer.endWidth = endWidth; ++numVertices; lineRenderer.positionCount = numVertices; vertices = new Vector3[numVertices]; string textureName = string.Empty; if (config.TryGetValue("texture", ref textureName)) { Texture tex = GameDatabase.Instance.GetTexture(textureName, false); if (tex != null) { lineMaterial.mainTexture = tex; inverseTextureWidth = 1.0f / (float)tex.width; usesTexture = true; } } string startAngleName = string.Empty; if (config.TryGetValue("startAngle", ref startAngleName)) { variableRegistrar.RegisterVariableChangeCallback(startAngleName, (double newValue) => { startAngle = (float)newValue; RecalculateVertices(); }); } else { startAngle = 0.0f; } string endAngleName = string.Empty; if (config.TryGetValue("endAngle", ref endAngleName)) { if (string.IsNullOrEmpty(startAngleName)) { throw new ArgumentException("Missing 'startAngle', but found 'endAngle' in ELLIPSE " + name); } else { variableRegistrar.RegisterVariableChangeCallback(endAngleName, (double newValue) => { endAngle = (float)newValue; RecalculateVertices(); }); } } else if (!string.IsNullOrEmpty(startAngleName)) { throw new ArgumentException("Found 'startAngle', but missing 'endAngle' in ELLIPSE " + name); } else { endAngle = 360.0f; } string radiusXName = string.Empty; if (!config.TryGetValue("radiusX", ref radiusXName)) { throw new ArgumentException("Unable to find 'radiusX' in ELLIPSE " + name); } string radiusYName = string.Empty; if (!config.TryGetValue("radiusY", ref radiusYName)) { Action <double> newRadius = (double newValue) => { radiusX = (float)newValue; radiusY = radiusX; RecalculateVertices(); }; variableRegistrar.RegisterVariableChangeCallback(radiusXName, newRadius); } else { Action <double> newRadiusX = (double newValue) => { radiusX = (float)newValue; RecalculateVertices(); }; variableRegistrar.RegisterVariableChangeCallback(radiusXName, newRadiusX); Action <double> newRadiusY = (double newValue) => { radiusY = (float)newValue; RecalculateVertices(); }; variableRegistrar.RegisterVariableChangeCallback(radiusYName, newRadiusY); } RenderPage(false); if (!string.IsNullOrEmpty(variableName)) { // Disable the lines if we're in variable mode lineOrigin.SetActive(false); variableRegistrar.RegisterVariableChangeCallback(variableName, VariableCallback); } else { lineOrigin.SetActive(true); } if (!string.IsNullOrEmpty(rotationVariableName)) { variableRegistrar.RegisterVariableChangeCallback(rotationVariableName, RotationCallback); } if (string.IsNullOrEmpty(endColorString)) { Color32 namedColor; if (comp.TryGetNamedColor(startColorString, out namedColor)) { startColor = namedColor; lineRenderer.startColor = startColor; lineRenderer.endColor = startColor; } else { string[] startColors = Utility.SplitVariableList(startColorString); if (startColors.Length < 3 || startColors.Length > 4) { throw new ArgumentException("startColor does not contain 3 or 4 values in ELLIPSE " + name); } Action <double> startColorR = (double newValue) => { startColor.r = Mathf.Clamp01((float)newValue * (1.0f / 255.0f)); lineRenderer.startColor = startColor; lineRenderer.endColor = startColor; }; variableRegistrar.RegisterVariableChangeCallback(startColors[0], startColorR); Action <double> startColorG = (double newValue) => { startColor.g = Mathf.Clamp01((float)newValue * (1.0f / 255.0f)); lineRenderer.startColor = startColor; lineRenderer.endColor = startColor; }; variableRegistrar.RegisterVariableChangeCallback(startColors[1], startColorG); Action <double> startColorB = (double newValue) => { startColor.b = Mathf.Clamp01((float)newValue * (1.0f / 255.0f)); lineRenderer.startColor = startColor; lineRenderer.endColor = startColor; }; variableRegistrar.RegisterVariableChangeCallback(startColors[2], startColorB); if (startColors.Length == 4) { Action <double> startColorA = (double newValue) => { startColor.a = Mathf.Clamp01((float)newValue * (1.0f / 255.0f)); lineRenderer.startColor = startColor; lineRenderer.endColor = startColor; }; variableRegistrar.RegisterVariableChangeCallback(startColors[3], startColorA); } } } else { Color32 namedColor; if (comp.TryGetNamedColor(startColorString, out namedColor)) { startColor = namedColor; lineRenderer.startColor = startColor; lineRenderer.endColor = endColor; } else { string[] startColors = Utility.SplitVariableList(startColorString); if (startColors.Length < 3 || startColors.Length > 4) { throw new ArgumentException("startColor does not contain 3 or 4 values in ELLIPSE " + name); } Action <double> startColorR = (double newValue) => { startColor.r = Mathf.Clamp01((float)newValue * (1.0f / 255.0f)); lineRenderer.startColor = startColor; }; variableRegistrar.RegisterVariableChangeCallback(startColors[0], startColorR); Action <double> startColorG = (double newValue) => { startColor.g = Mathf.Clamp01((float)newValue * (1.0f / 255.0f)); lineRenderer.startColor = startColor; }; variableRegistrar.RegisterVariableChangeCallback(startColors[1], startColorG); Action <double> startColorB = (double newValue) => { startColor.b = Mathf.Clamp01((float)newValue * (1.0f / 255.0f)); lineRenderer.startColor = startColor; }; variableRegistrar.RegisterVariableChangeCallback(startColors[2], startColorB); if (startColors.Length == 4) { Action <double> startColorA = (double newValue) => { startColor.a = Mathf.Clamp01((float)newValue * (1.0f / 255.0f)); lineRenderer.startColor = startColor; }; variableRegistrar.RegisterVariableChangeCallback(startColors[3], startColorA); } } if (comp.TryGetNamedColor(endColorString, out namedColor)) { endColor = namedColor; lineRenderer.startColor = startColor; lineRenderer.endColor = endColor; } else { string[] endColors = Utility.SplitVariableList(endColorString); if (endColors.Length < 3 || endColors.Length > 4) { throw new ArgumentException("endColor does not contain 3 or 4 values in ELLIPSE " + name); } Action <double> endColorR = (double newValue) => { endColor.r = Mathf.Clamp01((float)newValue * (1.0f / 255.0f)); lineRenderer.endColor = endColor; }; variableRegistrar.RegisterVariableChangeCallback(endColors[0], endColorR); Action <double> endColorG = (double newValue) => { endColor.g = Mathf.Clamp01((float)newValue * (1.0f / 255.0f)); lineRenderer.endColor = endColor; }; variableRegistrar.RegisterVariableChangeCallback(endColors[1], endColorG); Action <double> endColorB = (double newValue) => { endColor.b = Mathf.Clamp01((float)newValue * (1.0f / 255.0f)); lineRenderer.endColor = endColor; }; variableRegistrar.RegisterVariableChangeCallback(endColors[2], endColorB); if (endColors.Length == 4) { Action <double> endColorA = (double newValue) => { endColor.a = Mathf.Clamp01((float)newValue * (1.0f / 255.0f)); lineRenderer.endColor = endColor; }; variableRegistrar.RegisterVariableChangeCallback(endColors[3], endColorA); } } } if (string.IsNullOrEmpty(endWidthString)) { // Monowidth line Action <double> startWidthAction = (double newValue) => { startWidth = (float)newValue; lineRenderer.startWidth = startWidth; lineRenderer.endWidth = startWidth; }; variableRegistrar.RegisterVariableChangeCallback(startWidthString, startWidthAction); } else { Action <double> startWidthAction = (double newValue) => { startWidth = (float)newValue; lineRenderer.startWidth = startWidth; }; variableRegistrar.RegisterVariableChangeCallback(startWidthString, startWidthAction); Action <double> endWidthAction = (double newValue) => { endWidth = (float)newValue; lineRenderer.endWidth = endWidth; }; variableRegistrar.RegisterVariableChangeCallback(endWidthString, endWidthAction); } }
/// <summary> /// Initialize and configure the callback handler. /// </summary> /// <param name="node"></param> /// <param name="variableName"></param> /// <param name="thisProp"></param> public CallbackAnimationSet(ConfigNode node, string variableName, InternalProp thisProp) { currentState = false; if (!node.HasData) { throw new ArgumentException("No data?!"); } string[] tokens = { }; if (node.HasValue("scale")) { tokens = node.GetValue("scale").Split(','); } if (tokens.Length != 2) { throw new ArgumentException("Could not parse 'scale' parameter."); } RasterPropMonitorComputer rpmComp = RasterPropMonitorComputer.Instantiate(thisProp, true); variable = new VariableOrNumberRange(rpmComp, variableName, tokens[0], tokens[1]); // That takes care of the scale, now what to do about that scale: if (node.HasValue("reverse")) { if (!bool.TryParse(node.GetValue("reverse"), out reverse)) { throw new ArgumentException("So is 'reverse' true or false?"); } } if (node.HasValue("flash")) { if (!bool.TryParse(node.GetValue("flash"), out flash)) { throw new ArgumentException("So is 'reverse' true or false?"); } } else { flash = false; } if (node.HasValue("alarmSound")) { alarmSoundVolume = 0.5f; if (node.HasValue("alarmSoundVolume")) { alarmSoundVolume = float.Parse(node.GetValue("alarmSoundVolume")); } audioOutput = JUtil.SetupIVASound(thisProp, node.GetValue("alarmSound"), alarmSoundVolume, false); if (node.HasValue("alarmMustPlayOnce")) { if (!bool.TryParse(node.GetValue("alarmMustPlayOnce"), out alarmMustPlayOnce)) { throw new ArgumentException("So is 'alarmMustPlayOnce' true or false?"); } } if (node.HasValue("alarmShutdownButton")) { SmarterButton.CreateButton(thisProp, node.GetValue("alarmShutdownButton"), AlarmShutdown); } if (node.HasValue("alarmSoundLooping")) { if (!bool.TryParse(node.GetValue("alarmSoundLooping"), out alarmSoundLooping)) { throw new ArgumentException("So is 'alarmSoundLooping' true or false?"); } audioOutput.audio.loop = alarmSoundLooping; } inIVA = (CameraManager.Instance.currentCameraMode == CameraManager.CameraMode.IVA); GameEvents.OnCameraChange.Add(CameraChangeCallback); } if (node.HasValue("animationName")) { animationName = node.GetValue("animationName"); if (node.HasValue("animationSpeed")) { animationSpeed = float.Parse(node.GetValue("animationSpeed")); if (reverse) { animationSpeed = -animationSpeed; } } else { animationSpeed = 0.0f; } Animation[] anims = node.HasValue("animateExterior") ? thisProp.part.FindModelAnimators(animationName) : thisProp.FindModelAnimators(animationName); if (anims.Length > 0) { onAnim = anims[0]; onAnim.enabled = true; onAnim[animationName].speed = 0; onAnim[animationName].normalizedTime = reverse ? 1f : 0f; looping = node.HasValue("loopingAnimation"); if (looping) { onAnim[animationName].wrapMode = WrapMode.Loop; onAnim.wrapMode = WrapMode.Loop; onAnim[animationName].speed = animationSpeed; mode = Mode.LoopingAnimation; } else { onAnim[animationName].wrapMode = WrapMode.Once; mode = Mode.Animation; } onAnim.Play(); //alwaysActive = node.HasValue("animateExterior"); } else { throw new ArgumentException("Animation " + animationName + " could not be found."); } if (node.HasValue("stopAnimationName")) { stopAnimationName = node.GetValue("stopAnimationName"); anims = node.HasValue("animateExterior") ? thisProp.part.FindModelAnimators(stopAnimationName) : thisProp.FindModelAnimators(stopAnimationName); if (anims.Length > 0) { offAnim = anims[0]; offAnim.enabled = true; offAnim[stopAnimationName].speed = 0; offAnim[stopAnimationName].normalizedTime = reverse ? 1f : 0f; if (looping) { offAnim[stopAnimationName].wrapMode = WrapMode.Loop; offAnim.wrapMode = WrapMode.Loop; offAnim[stopAnimationName].speed = animationSpeed; mode = Mode.LoopingAnimation; } else { offAnim[stopAnimationName].wrapMode = WrapMode.Once; mode = Mode.Animation; } } } } else if (node.HasValue("activeColor") && node.HasValue("passiveColor") && node.HasValue("coloredObject")) { string colorNameString = "_EmissiveColor"; if (node.HasValue("colorName")) { colorNameString = node.GetValue("colorName"); } colorName = Shader.PropertyToID(colorNameString); if (reverse) { activeColor = JUtil.ParseColor32(node.GetValue("passiveColor"), thisProp.part, ref rpmComp); passiveColor = JUtil.ParseColor32(node.GetValue("activeColor"), thisProp.part, ref rpmComp); } else { passiveColor = JUtil.ParseColor32(node.GetValue("passiveColor"), thisProp.part, ref rpmComp); activeColor = JUtil.ParseColor32(node.GetValue("activeColor"), thisProp.part, ref rpmComp); } Renderer colorShiftRenderer = thisProp.FindModelComponent <Renderer>(node.GetValue("coloredObject")); affectedMaterial = colorShiftRenderer.material; affectedMaterial.SetColor(colorName, passiveColor); mode = Mode.Color; } else if (node.HasValue("controlledTransform") && node.HasValue("localRotationStart") && node.HasValue("localRotationEnd")) { controlledTransform = thisProp.FindModelTransform(node.GetValue("controlledTransform").Trim()); initialRotation = controlledTransform.localRotation; if (node.HasValue("longPath")) { longPath = true; if (reverse) { vectorEnd = ConfigNode.ParseVector3(node.GetValue("localRotationStart")); vectorStart = ConfigNode.ParseVector3(node.GetValue("localRotationEnd")); } else { vectorStart = ConfigNode.ParseVector3(node.GetValue("localRotationStart")); vectorEnd = ConfigNode.ParseVector3(node.GetValue("localRotationEnd")); } } else { if (reverse) { rotationEnd = Quaternion.Euler(ConfigNode.ParseVector3(node.GetValue("localRotationStart"))); rotationStart = Quaternion.Euler(ConfigNode.ParseVector3(node.GetValue("localRotationEnd"))); } else { rotationStart = Quaternion.Euler(ConfigNode.ParseVector3(node.GetValue("localRotationStart"))); rotationEnd = Quaternion.Euler(ConfigNode.ParseVector3(node.GetValue("localRotationEnd"))); } } mode = Mode.Rotation; } else if (node.HasValue("controlledTransform") && node.HasValue("localTranslationStart") && node.HasValue("localTranslationEnd")) { controlledTransform = thisProp.FindModelTransform(node.GetValue("controlledTransform").Trim()); initialPosition = controlledTransform.localPosition; if (reverse) { vectorEnd = ConfigNode.ParseVector3(node.GetValue("localTranslationStart")); vectorStart = ConfigNode.ParseVector3(node.GetValue("localTranslationEnd")); } else { vectorStart = ConfigNode.ParseVector3(node.GetValue("localTranslationStart")); vectorEnd = ConfigNode.ParseVector3(node.GetValue("localTranslationEnd")); } mode = Mode.Translation; } else if (node.HasValue("controlledTransform") && node.HasValue("localScaleStart") && node.HasValue("localScaleEnd")) { controlledTransform = thisProp.FindModelTransform(node.GetValue("controlledTransform").Trim()); initialScale = controlledTransform.localScale; if (reverse) { vectorEnd = ConfigNode.ParseVector3(node.GetValue("localScaleStart")); vectorStart = ConfigNode.ParseVector3(node.GetValue("localScaleEnd")); } else { vectorStart = ConfigNode.ParseVector3(node.GetValue("localScaleStart")); vectorEnd = ConfigNode.ParseVector3(node.GetValue("localScaleEnd")); } mode = Mode.Scale; } else if (node.HasValue("controlledTransform") && node.HasValue("textureLayers") && node.HasValue("textureShiftStart") && node.HasValue("textureShiftEnd")) { affectedMaterial = thisProp.FindModelTransform(node.GetValue("controlledTransform").Trim()).GetComponent <Renderer>().material; var textureLayers = node.GetValue("textureLayers").Split(','); for (int i = 0; i < textureLayers.Length; ++i) { textureLayer.Add(textureLayers[i].Trim()); } if (reverse) { textureShiftEnd = ConfigNode.ParseVector2(node.GetValue("textureShiftStart")); textureShiftStart = ConfigNode.ParseVector2(node.GetValue("textureShiftEnd")); } else { textureShiftStart = ConfigNode.ParseVector2(node.GetValue("textureShiftStart")); textureShiftEnd = ConfigNode.ParseVector2(node.GetValue("textureShiftEnd")); } mode = Mode.TextureShift; } else if (node.HasValue("controlledTransform") && node.HasValue("textureLayers") && node.HasValue("textureScaleStart") && node.HasValue("textureScaleEnd")) { affectedMaterial = thisProp.FindModelTransform(node.GetValue("controlledTransform").Trim()).GetComponent <Renderer>().material; var textureLayers = node.GetValue("textureLayers").Split(','); for (int i = 0; i < textureLayers.Length; ++i) { textureLayer.Add(textureLayers[i].Trim()); } if (reverse) { textureScaleEnd = ConfigNode.ParseVector2(node.GetValue("textureScaleStart")); textureScaleStart = ConfigNode.ParseVector2(node.GetValue("textureScaleEnd")); } else { textureScaleStart = ConfigNode.ParseVector2(node.GetValue("textureScaleStart")); textureScaleEnd = ConfigNode.ParseVector2(node.GetValue("textureScaleEnd")); } mode = Mode.TextureScale; } else { throw new ArgumentException("Cannot initiate any of the possible action modes."); } TurnOff(); }
internal MASPageVerticalStrip(ConfigNode config, InternalProp prop, MASFlightComputer comp, MASMonitor monitor, Transform pageRoot, float depth) : base(config, prop, comp) { string textureName = string.Empty; if (!config.TryGetValue("texture", ref textureName)) { throw new ArgumentException("Unable to find 'texture' in VERTICAL_STRIP " + name); } Texture2D mainTexture = GameDatabase.Instance.GetTexture(textureName, false); if (mainTexture == null) { throw new ArgumentException("Unable to find 'texture' " + textureName + " for VERTICAL_STRIP " + name); } bool wrapMode = false; if (!config.TryGetValue("wrap", ref wrapMode)) { wrapMode = false; } mainTexture.wrapMode = (wrapMode) ? TextureWrapMode.Repeat : TextureWrapMode.Clamp; Vector2 size = Vector2.zero; if (!config.TryGetValue("size", ref size)) { throw new ArgumentException("Unable to find 'size' in VERTICAL_STRIP " + name); } string variableName = string.Empty; string inputName = string.Empty; if (!config.TryGetValue("input", ref inputName)) { throw new ArgumentException("Unable to find 'input' in VERTICAL_STRIP " + name); } string inputRange = string.Empty; if (!config.TryGetValue("inputRange", ref inputRange)) { throw new ArgumentException("Unable to find 'inputRange' in VERTICAL_STRIP " + name); } string[] ranges = Utility.SplitVariableList(inputRange); if (ranges.Length != 2) { throw new ArgumentException("Incorrect number of values in 'inputRange' in VERTICAL_STRIP " + name); } variableRegistrar.RegisterVariableChangeCallback(ranges[0], (double newValue) => inputRange1 = (float)newValue); variableRegistrar.RegisterVariableChangeCallback(ranges[1], (double newValue) => inputRange2 = (float)newValue); string displayRange = string.Empty; if (!config.TryGetValue("displayRange", ref displayRange)) { throw new ArgumentException("Unable to find 'displayRange' in VERTICAL_STRIP " + name); } ranges = Utility.SplitVariableList(displayRange); if (ranges.Length != 2) { throw new ArgumentException("Incorrect number of values in 'displayRange' in VERTICAL_STRIP " + name); } variableRegistrar.RegisterVariableChangeCallback(ranges[0], (double newValue) => displayRange1 = (float)newValue); variableRegistrar.RegisterVariableChangeCallback(ranges[1], (double newValue) => displayRange2 = (float)newValue); float displayHeight = 0.0f; if (!config.TryGetValue("displayHeight", ref displayHeight)) { throw new ArgumentException("Unable to find 'displayHeight' in VERTICAL_STRIP " + name); } texelWidth = mainTexture.texelSize.y; float textureSpan = displayHeight * texelWidth; textureOffset = textureSpan * 0.5f; if (config.TryGetValue("variable", ref variableName)) { variableName = variableName.Trim(); } componentOrigin = pageRoot.position + new Vector3(monitor.screenSize.x * -0.5f, monitor.screenSize.y * 0.5f, depth); // Set up our display surface. imageObject = new GameObject(); imageObject.name = Utility.ComposeObjectName(pageRoot.gameObject.name, this.GetType().Name, name, (int)(-depth / MASMonitor.depthDelta)); imageObject.layer = pageRoot.gameObject.layer; imageObject.transform.parent = pageRoot; imageObject.transform.position = pageRoot.position; imageObject.transform.Translate(monitor.screenSize.x * -0.5f + position.x, monitor.screenSize.y * 0.5f - position.y, depth); // add renderer stuff MeshFilter meshFilter = imageObject.AddComponent <MeshFilter>(); meshRenderer = imageObject.AddComponent <MeshRenderer>(); Mesh mesh = new Mesh(); mesh.vertices = new[] { new Vector3(0.0f, 0.0f, 0.0f), new Vector3(size.x, 0.0f, 0.0f), new Vector3(0.0f, -size.y, 0.0f), new Vector3(size.x, -size.y, 0.0f), }; mesh.uv = new[] { new Vector2(0.0f, 1.0f), Vector2.one, Vector2.zero, new Vector2(1.0f, 0.0f), }; mesh.triangles = new[] { 0, 1, 2, 1, 3, 2 }; mesh.RecalculateBounds(); mesh.UploadMeshData(true); meshFilter.mesh = mesh; imageMaterial = new Material(MASLoader.shaders["MOARdV/Monitor"]); imageMaterial.mainTexture = mainTexture; imageMaterial.mainTextureScale = new Vector2(1.00f, textureSpan); meshRenderer.material = imageMaterial; RenderPage(false); string positionString = string.Empty; if (!config.TryGetValue("position", ref positionString)) { throw new ArgumentException("Unable to find 'position' in VERTICAL_STRIP " + name); } else { string[] pos = Utility.SplitVariableList(positionString); if (pos.Length != 2) { throw new ArgumentException("Invalid number of values for 'position' in VERTICAL_STRIP " + name); } variableRegistrar.RegisterVariableChangeCallback(pos[0], (double newValue) => { position.x = (float)newValue; imageObject.transform.position = componentOrigin + new Vector3(position.x, -position.y, 0.0f); }); variableRegistrar.RegisterVariableChangeCallback(pos[1], (double newValue) => { position.y = (float)newValue; imageObject.transform.position = componentOrigin + new Vector3(position.x, -position.y, 0.0f); }); } variableRegistrar.RegisterVariableChangeCallback(inputName, InputCallback); if (!string.IsNullOrEmpty(variableName)) { // Disable the mesh if we're in variable mode imageObject.SetActive(false); variableRegistrar.RegisterVariableChangeCallback(variableName, VariableCallback); } else { imageObject.SetActive(true); } }
internal NumericInput(ConfigNode node, InternalProp internalProp) { if (!node.HasValue("switchTransform")) { throw new Exception("USERINPUTSET missing switchTransform"); } // XNOR! if (!(node.HasValue("increment") ^ node.HasNode("incrementCurve"))) { throw new Exception("USERINPUTSET missing increment or incrementCurve, or it has both"); } if (node.HasValue("increment") && !float.TryParse(node.GetValue("increment"), out increment)) { throw new Exception("USERINPUTSET bad increment"); } else if (node.HasNode("incrementCurve")) { ConfigNode incNode = node.GetNode("incrementCurve"); string[] keys = incNode.GetValues("key"); incrementCurve = new FloatCurve(); for (int i = 0; i < keys.Length; ++i) { string[] values = keys[i].Split(' '); if (values.Length == 2) { incrementCurve.Add(float.Parse(values[0]), float.Parse(values[1])); } else if (values.Length == 4) { incrementCurve.Add(float.Parse(values[0]), float.Parse(values[1]), float.Parse(values[2]), float.Parse(values[3])); } else { JUtil.LogErrorMessage(this, "Found a curve key with {0} entries?!?", values.Length); } } pressAndHold = true; } if (node.HasValue("reverse")) { bool.TryParse(node.GetValue("reverse"), out reverse); } if (node.HasValue("animationName")) { animationName = node.GetValue("animationName").Trim(); } float switchSoundVolume = 0.5f; if (node.HasValue("switchSoundVolume") && !float.TryParse("switchSoundVolume", out switchSoundVolume)) { switchSoundVolume = 0.5f; } if (node.HasValue("switchSound")) { audioOutput = JUtil.SetupIVASound(internalProp, node.GetValue("switchSound").Trim(), switchSoundVolume, false); } if (node.HasValue("customSpeed") && !float.TryParse("customSpeed", out customSpeed)) { customSpeed = 1.0f; } if (!string.IsNullOrEmpty(animationName)) { // Set up the animation Animation[] animators = internalProp.FindModelAnimators(animationName); if (animators.Length > 0) { anim = animators[0]; } else { JUtil.LogErrorMessage(this, "Could not find animation \"{0}\" on prop \"{1}\"", animationName, internalProp.name); return; } anim[animationName].wrapMode = WrapMode.Once; if (reverse) { anim[animationName].speed = float.MaxValue; anim[animationName].normalizedTime = 0.0f; } else { anim[animationName].speed = float.MinValue; anim[animationName].normalizedTime = 1.0f; } anim.Play(animationName); } string switchTransform = node.GetValue("switchTransform"); if (incrementCurve != null) { SmarterButton.CreateButton(internalProp, switchTransform, TimedClick, TimedRelease); } else { SmarterButton.CreateButton(internalProp, switchTransform, Click, TimedRelease); } }
/// <summary> /// Release resources /// </summary> public override void ReleaseResources(MASFlightComputer comp, InternalProp internalProp) { variableRegistrar.ReleaseResources(); UnityEngine.Object.Destroy(localMaterial); }
public VariableAnimationSet(ConfigNode node, InternalProp thisProp, RasterPropMonitorComputer rpmComp, JSIVariableAnimator parent) { varAnim = parent; onChangeDelegate = (Action<float>)Delegate.CreateDelegate(typeof(Action<float>), this, "OnChange"); part = thisProp.part; if (!node.HasData) { throw new ArgumentException("No data?!"); } string[] tokens = { }; if (node.HasValue("scale")) { tokens = node.GetValue("scale").Split(','); } if (tokens.Length != 2) { throw new ArgumentException("Could not parse 'scale' parameter."); } string variableName = string.Empty; if (node.HasValue("variableName")) { variableName = node.GetValue("variableName").Trim(); } else if (node.HasValue("stateMethod")) { string stateMethod = node.GetValue("stateMethod").Trim(); // Verify the state method actually exists Func<bool> stateFunction = (Func<bool>)rpmComp.GetMethod(stateMethod, thisProp, typeof(Func<bool>)); if (stateFunction != null) { variableName = "PLUGIN_" + stateMethod; } else { throw new ArgumentException("Unrecognized stateMethod"); } } else { throw new ArgumentException("Missing variable name."); } if (node.HasValue("modulo")) { variable = new VariableOrNumberRange(rpmComp, variableName, tokens[0], tokens[1], node.GetValue("modulo")); usesModulo = true; } else { variable = new VariableOrNumberRange(rpmComp, variableName, tokens[0], tokens[1]); usesModulo = false; } // That takes care of the scale, now what to do about that scale: if (node.HasValue("reverse")) { if (!bool.TryParse(node.GetValue("reverse"), out reverse)) { throw new ArgumentException("So is 'reverse' true or false?"); } } if (node.HasValue("animationName")) { animationName = node.GetValue("animationName"); if (node.HasValue("animationSpeed")) { animationSpeed = float.Parse(node.GetValue("animationSpeed")); if (reverse) { animationSpeed = -animationSpeed; } } else { animationSpeed = 0.0f; } Animation[] anims = node.HasValue("animateExterior") ? thisProp.part.FindModelAnimators(animationName) : thisProp.FindModelAnimators(animationName); if (anims.Length > 0) { onAnim = anims[0]; onAnim.enabled = true; onAnim[animationName].speed = 0; onAnim[animationName].normalizedTime = reverse ? 1f : 0f; looping = node.HasValue("loopingAnimation"); if (looping) { onAnim[animationName].wrapMode = WrapMode.Loop; onAnim.wrapMode = WrapMode.Loop; onAnim[animationName].speed = animationSpeed; mode = Mode.LoopingAnimation; } else { onAnim[animationName].wrapMode = WrapMode.Once; mode = Mode.Animation; } onAnim.Play(); alwaysActive = node.HasValue("animateExterior"); } else { throw new ArgumentException("Animation could not be found."); } if (node.HasValue("stopAnimationName")) { stopAnimationName = node.GetValue("stopAnimationName"); anims = node.HasValue("animateExterior") ? thisProp.part.FindModelAnimators(stopAnimationName) : thisProp.FindModelAnimators(stopAnimationName); if (anims.Length > 0) { offAnim = anims[0]; offAnim.enabled = true; offAnim[stopAnimationName].speed = 0; offAnim[stopAnimationName].normalizedTime = reverse ? 1f : 0f; if (looping) { offAnim[stopAnimationName].wrapMode = WrapMode.Loop; offAnim.wrapMode = WrapMode.Loop; offAnim[stopAnimationName].speed = animationSpeed; mode = Mode.LoopingAnimation; } else { offAnim[stopAnimationName].wrapMode = WrapMode.Once; mode = Mode.Animation; } } } } else if (node.HasValue("activeColor") && node.HasValue("passiveColor") && node.HasValue("coloredObject")) { string colorNameString = "_EmissiveColor"; if (node.HasValue("colorName")) { colorNameString = node.GetValue("colorName"); } colorName = Shader.PropertyToID(colorNameString); if (reverse) { activeColor = JUtil.ParseColor32(node.GetValue("passiveColor"), thisProp.part, ref rpmComp); passiveColor = JUtil.ParseColor32(node.GetValue("activeColor"), thisProp.part, ref rpmComp); } else { passiveColor = JUtil.ParseColor32(node.GetValue("passiveColor"), thisProp.part, ref rpmComp); activeColor = JUtil.ParseColor32(node.GetValue("activeColor"), thisProp.part, ref rpmComp); } Renderer colorShiftRenderer = thisProp.FindModelComponent<Renderer>(node.GetValue("coloredObject")); affectedMaterial = colorShiftRenderer.material; affectedMaterial.SetColor(colorName, passiveColor); mode = Mode.Color; } else if (node.HasValue("controlledTransform") && node.HasValue("localRotationStart") && node.HasValue("localRotationEnd")) { controlledTransform = thisProp.FindModelTransform(node.GetValue("controlledTransform").Trim()); initialRotation = controlledTransform.localRotation; if (node.HasValue("longPath")) { longPath = true; if (reverse) { vectorEnd = ConfigNode.ParseVector3(node.GetValue("localRotationStart")); vectorStart = ConfigNode.ParseVector3(node.GetValue("localRotationEnd")); } else { vectorStart = ConfigNode.ParseVector3(node.GetValue("localRotationStart")); vectorEnd = ConfigNode.ParseVector3(node.GetValue("localRotationEnd")); } } else { if (reverse) { rotationEnd = Quaternion.Euler(ConfigNode.ParseVector3(node.GetValue("localRotationStart"))); rotationStart = Quaternion.Euler(ConfigNode.ParseVector3(node.GetValue("localRotationEnd"))); } else { rotationStart = Quaternion.Euler(ConfigNode.ParseVector3(node.GetValue("localRotationStart"))); rotationEnd = Quaternion.Euler(ConfigNode.ParseVector3(node.GetValue("localRotationEnd"))); } } mode = Mode.Rotation; } else if (node.HasValue("controlledTransform") && node.HasValue("localTranslationStart") && node.HasValue("localTranslationEnd")) { controlledTransform = thisProp.FindModelTransform(node.GetValue("controlledTransform").Trim()); initialPosition = controlledTransform.localPosition; if (reverse) { vectorEnd = ConfigNode.ParseVector3(node.GetValue("localTranslationStart")); vectorStart = ConfigNode.ParseVector3(node.GetValue("localTranslationEnd")); } else { vectorStart = ConfigNode.ParseVector3(node.GetValue("localTranslationStart")); vectorEnd = ConfigNode.ParseVector3(node.GetValue("localTranslationEnd")); } mode = Mode.Translation; } else if (node.HasValue("controlledTransform") && node.HasValue("localScaleStart") && node.HasValue("localScaleEnd")) { controlledTransform = thisProp.FindModelTransform(node.GetValue("controlledTransform").Trim()); initialScale = controlledTransform.localScale; if (reverse) { vectorEnd = ConfigNode.ParseVector3(node.GetValue("localScaleStart")); vectorStart = ConfigNode.ParseVector3(node.GetValue("localScaleEnd")); } else { vectorStart = ConfigNode.ParseVector3(node.GetValue("localScaleStart")); vectorEnd = ConfigNode.ParseVector3(node.GetValue("localScaleEnd")); } mode = Mode.Scale; } else if (node.HasValue("controlledTransform") && node.HasValue("textureLayers") && node.HasValue("textureShiftStart") && node.HasValue("textureShiftEnd")) { affectedMaterial = thisProp.FindModelTransform(node.GetValue("controlledTransform").Trim()).GetComponent<Renderer>().material; var textureLayers = node.GetValue("textureLayers").Split(','); for (int i = 0; i < textureLayers.Length; ++i) { textureLayer.Add(textureLayers[i].Trim()); } if (reverse) { textureShiftEnd = ConfigNode.ParseVector2(node.GetValue("textureShiftStart")); textureShiftStart = ConfigNode.ParseVector2(node.GetValue("textureShiftEnd")); } else { textureShiftStart = ConfigNode.ParseVector2(node.GetValue("textureShiftStart")); textureShiftEnd = ConfigNode.ParseVector2(node.GetValue("textureShiftEnd")); } mode = Mode.TextureShift; } else if (node.HasValue("controlledTransform") && node.HasValue("textureLayers") && node.HasValue("textureScaleStart") && node.HasValue("textureScaleEnd")) { affectedMaterial = thisProp.FindModelTransform(node.GetValue("controlledTransform").Trim()).GetComponent<Renderer>().material; var textureLayers = node.GetValue("textureLayers").Split(','); for (int i = 0; i < textureLayers.Length; ++i) { textureLayer.Add(textureLayers[i].Trim()); } if (reverse) { textureScaleEnd = ConfigNode.ParseVector2(node.GetValue("textureScaleStart")); textureScaleStart = ConfigNode.ParseVector2(node.GetValue("textureScaleEnd")); } else { textureScaleStart = ConfigNode.ParseVector2(node.GetValue("textureScaleStart")); textureScaleEnd = ConfigNode.ParseVector2(node.GetValue("textureScaleEnd")); } mode = Mode.TextureScale; } else { throw new ArgumentException("Cannot initiate any of the possible action modes."); } if (!(node.HasValue("maxRateChange") && float.TryParse(node.GetValue("maxRateChange"), out maxRateChange))) { maxRateChange = 0.0f; } if (maxRateChange >= 60.0f) { // Animation rate is too fast to even notice @60Hz maxRateChange = 0.0f; } else { lastAnimUpdate = Planetarium.GetUniversalTime(); } if (node.HasValue("threshold")) { threshold = ConfigNode.ParseVector2(node.GetValue("threshold")); } resourceAmount = 0.0f; if (threshold != Vector2.zero) { thresholdMode = true; float min = Mathf.Min(threshold.x, threshold.y); float max = Mathf.Max(threshold.x, threshold.y); threshold.x = min; threshold.y = max; if (node.HasValue("flashingDelay")) { flashingDelay = double.Parse(node.GetValue("flashingDelay")); } if (node.HasValue("alarmSound")) { alarmSoundVolume = 0.5f; if (node.HasValue("alarmSoundVolume")) { alarmSoundVolume = float.Parse(node.GetValue("alarmSoundVolume")); } audioOutput = JUtil.SetupIVASound(thisProp, node.GetValue("alarmSound"), alarmSoundVolume, false); if (node.HasValue("alarmMustPlayOnce")) { if (!bool.TryParse(node.GetValue("alarmMustPlayOnce"), out alarmMustPlayOnce)) { throw new ArgumentException("So is 'alarmMustPlayOnce' true or false?"); } } if (node.HasValue("alarmShutdownButton")) { SmarterButton.CreateButton(thisProp, node.GetValue("alarmShutdownButton"), AlarmShutdown); } if (node.HasValue("alarmSoundLooping")) { if (!bool.TryParse(node.GetValue("alarmSoundLooping"), out alarmSoundLooping)) { throw new ArgumentException("So is 'alarmSoundLooping' true or false?"); } audioOutput.audio.loop = alarmSoundLooping; } inIVA = (CameraManager.Instance.currentCameraMode == CameraManager.CameraMode.IVA); GameEvents.OnCameraChange.Add(OnCameraChange); } if (node.HasValue("resourceAmount")) { resourceAmount = float.Parse(node.GetValue("resourceAmount")); if (node.HasValue("resourceName")) { resourceName = node.GetValue("resourceName"); } else { resourceName = "ElectricCharge"; } } TurnOff(Planetarium.GetUniversalTime()); } rpmComp.RegisterVariableCallback(variable.variableName, onChangeDelegate); }
internal MASComponentColliderAdvanced(ConfigNode config, InternalProp internalProp, MASFlightComputer comp) : base(config, internalProp, comp) { string collider = string.Empty; if (!config.TryGetValue("collider", ref collider)) { throw new ArgumentException("Missing 'collider' in COLLIDER_ADVANCED " + name); } string clickAction = string.Empty; string dragAction = string.Empty; string releaseAction = string.Empty; string monitorID = string.Empty; if (!config.TryGetValue("monitorID", ref monitorID)) { config.TryGetValue("onClick", ref clickAction); config.TryGetValue("onDrag", ref dragAction); config.TryGetValue("onRelease", ref releaseAction); if (string.IsNullOrEmpty(clickAction) && string.IsNullOrEmpty(dragAction) && string.IsNullOrEmpty(releaseAction)) { throw new ArgumentException("Missing 'monitorID', 'onClick', 'onDrag', or 'onRelease' in COLLIDER_ADVANCED " + name); } } string clickX = string.Empty; if (!config.TryGetValue("clickX", ref clickX)) { throw new ArgumentException("Missing 'clickX' in COLLIDER_ADVANCED " + name); } string clickY = string.Empty; if (!config.TryGetValue("clickY", ref clickY)) { throw new ArgumentException("Missing 'clickY' in COLLIDER_ADVANCED " + name); } Transform tr = internalProp.FindModelTransform(collider.Trim()); if (tr == null) { throw new ArgumentException("Unable to find transform '" + collider + "' in prop for COLLIDER_ADVANCED " + name); } float volume = -1.0f; if (config.TryGetValue("volume", ref volume)) { volume = Mathf.Clamp01(volume); } else { volume = -1.0f; } string sound = string.Empty; if (!config.TryGetValue("sound", ref sound) || string.IsNullOrEmpty(sound)) { sound = string.Empty; } AudioClip clip = null; if (string.IsNullOrEmpty(sound) == (volume >= 0.0f)) { throw new ArgumentException("Only one of 'sound' or 'volume' found in COLLIDER_ADVANCED " + name); } if (volume >= 0.0f) { //Try Load audio clip = GameDatabase.Instance.GetAudioClip(sound); if (clip == null) { throw new ArgumentException("Unable to load 'sound' " + sound + " in COLLIDER_ADVANCED " + name); } } buttonObject = tr.gameObject.AddComponent <AdvancedButtonObject>(); buttonObject.parent = this; if (string.IsNullOrEmpty(monitorID)) { if (!string.IsNullOrEmpty(clickAction)) { onClick = comp.GetColliderAction(clickAction, 0, "click", internalProp); } if (!string.IsNullOrEmpty(dragAction)) { onDrag = comp.GetColliderAction(dragAction, 0, "drag", internalProp); } if (!string.IsNullOrEmpty(releaseAction)) { onRelease = comp.GetColliderAction(releaseAction, 0, "release", internalProp); } buttonObject.onTouch = (Vector2 hitCoordinate, EventType eventType) => { if (eventType == EventType.MouseDown) { if (onClick != null) { onClick(hitCoordinate); } } else if (eventType == EventType.MouseDrag) { if (onDrag != null) { onDrag(hitCoordinate); } } else if (eventType == EventType.MouseUp) { if (onRelease != null) { onRelease(hitCoordinate); } } }; } else { buttonObject.onTouch = comp.GetHitAction(monitorID, internalProp, comp.HandleTouchEvent); } buttonObject.hitTransformation = comp.GetColliderTransformation(clickX, clickY, name, internalProp); Collider btnCollider = tr.gameObject.GetComponent <Collider>(); if (btnCollider == null) { throw new ArgumentException("Unable to retrieve Collider from GameObject in COLLIDER_ADVANCED " + name); } BoxCollider boxCollider = btnCollider as BoxCollider; if (boxCollider == null) { throw new ArgumentException("Collider for COLLIDER_ADVANCED " + name + " is not a BoxCollider"); } buttonObject.InitBoxCollider(boxCollider); if (!config.TryGetValue("logHits", ref buttonObject.debugEnabled)) { buttonObject.debugEnabled = false; } string variableName = string.Empty; if (config.TryGetValue("variable", ref variableName)) { variableName = variableName.Trim(); buttonObject.colliderEnabled = false; comp.RegisterVariableChangeCallback(variableName, internalProp, VariableCallback); } else { variableEnabled = true; } comp.RegisterVariableChangeCallback("fc.CrewConscious(-1)", internalProp, KerbalCallback); if (clip != null) { AudioSource audioSource = tr.gameObject.AddComponent <AudioSource>(); audioSource.clip = clip; audioSource.Stop(); audioSource.volume = GameSettings.SHIP_VOLUME * volume; audioSource.rolloffMode = AudioRolloffMode.Logarithmic; audioSource.maxDistance = 8.0f; audioSource.minDistance = 2.0f; audioSource.dopplerLevel = 0.0f; audioSource.panStereo = 0.0f; audioSource.playOnAwake = false; audioSource.loop = false; audioSource.pitch = 1.0f; buttonObject.audioSource = audioSource; } }