/// <summary> /// Add a variable to the variableCache /// </summary> /// <param name="variableName"></param> private void AddVariable(string variableName) { VariableCache vc = new VariableCache(); bool cacheable; vc.evaluator = GetEvaluator(variableName, out cacheable); vc.value = new VariableOrNumber(variableName, cacheable, this); vc.cacheable = cacheable; if (vc.value.variableType == VariableOrNumber.VoNType.VariableValue) { RPMVesselComputer comp = RPMVesselComputer.Instance(vessel); object value = vc.evaluator(variableName, comp); if (value is string) { vc.value.stringValue = value as string; vc.value.isNumeric = false; vc.value.numericValue = 0.0; // If the evaluator returns the variableName, then we // have an unknown variable. Change the VoN type to // ConstantString so we don't waste cycles on update to // reevaluate it. if (vc.value.stringValue == variableName && !unrecognizedVariables.Contains(variableName)) { vc.value.variableType = VariableOrNumber.VoNType.ConstantString; unrecognizedVariables.Add(variableName); JUtil.LogInfo(this, "Unrecognized variable {0}", variableName); } } else { vc.value.numericValue = value.MassageToDouble(); vc.value.isNumeric = true; } } variableCache.Add(variableName, vc); if (vc.value.variableType == VariableOrNumber.VoNType.VariableValue) { // Only variables that are really variable need to be checked // during FixedUpdate. updatableVariables.Add(vc); } }
public void Click() { RPMVesselComputer comp = RPMVesselComputer.Instance(vessel); bool switchEnabled = true; if (!forcedShutdown) { if (perPodMasterSwitchValid) { switchEnabled = comp.GetPersistentVariable(perPodMasterSwitchName, false); } if (masterVariable != null) { switchEnabled = masterVariable.IsInRange(comp); } } if (!switchEnabled) { // If the master switch is 'off' and we're not here because // of a forced shutdown, don't allow this switch to work. // early return return; } if (isCustomAction) { if (switchGroupIdentifier >= 0) { if (!forcedShutdown && !customGroupState) { customGroupState = true; if (persistentVarValid) { comp.SetPersistentVariable(persistentVarName, switchGroupIdentifier); } } // else: can't turn off a radio group switch. } else if (customAction == CustomActions.Plugin && stateVariableValid) { int ivalue = comp.ProcessVariable(stateVariable).MassageToInt(); customGroupState = (ivalue < 1) && !forcedShutdown; } else { customGroupState = !customGroupState; if (persistentVarValid) { comp.SetPersistentVariable(persistentVarName, customGroupState); } } } else { vessel.ActionGroups.ToggleGroup(kspAction); } // Now we do extra things that with regular actions can't happen. switch (customAction) { case CustomActions.IntLight: SetInternalLights(customGroupState); break; case CustomActions.Plugin: actionHandler(customGroupState); break; case CustomActions.Stage: if (InputLockManager.IsUnlocked(ControlTypes.STAGING)) { StageManager.ActivateNextStage(); } break; case CustomActions.TransferToPersistent: if (stateVariableValid) { // stateVariable can disable the button functionality. int ivalue = comp.ProcessVariable(stateVariable).MassageToInt(); if (ivalue < 1) { return; // early - button disabled } } float getValue = comp.ProcessVariable(transferGetter).MassageToFloat(); comp.SetPersistentVariable(transferPersistentName, getValue); break; case CustomActions.TransferFromPersistent: if (stateVariableValid) { // stateVariable can disable the button functionality. int ivalue = comp.ProcessVariable(stateVariable).MassageToInt(); if (ivalue < 1) { return; // early - button disabled } } if (comp.HasPersistentVariable(transferPersistentName)) { transferSetter(comp.GetPersistentVariable(transferPersistentName, 0.0).MassageToDouble()); } break; case CustomActions.TransferFromVariable: if (stateVariableValid) { // stateVariable can disable the button functionality. int ivalue = comp.ProcessVariable(stateVariable).MassageToInt(); if (ivalue < 1) { return; // early - button disabled } } double xferValue = comp.ProcessVariable(transferGetter).MassageToDouble(); transferSetter(xferValue); break; case CustomActions.Transfer: JUtil.LogInfo(this, "The deprecated CustomActions.Transfer path was executed"); /*if (stateVariableValid) * { * // stateVariable can disable the button functionality. * int ivalue = comp.ProcessVariable(stateVariable).MassageToInt(); * if (ivalue < 1) * { * return; // early - button disabled * } * } * if (!string.IsNullOrEmpty(transferGetter)) * { * float value = comp.ProcessVariable(transferGetter).MassageToFloat(); * comp.SetPersistentVariable(transferPersistentName, value); * } * else if (comp.HasPersistentVariable(transferPersistentName)) * { * transferSetter(comp.GetPersistentVariable(transferPersistentName, 0.0).MassageToDouble()); * }*/ break; } }
public MonitorPage(int idNum, ConfigNode node, RasterPropMonitor thatMonitor) { ourMonitor = thatMonitor; screenWidth = ourMonitor.screenWidth; screenHeight = ourMonitor.screenHeight; cameraAspect = ourMonitor.cameraAspect; cameraObject = thatMonitor.cameraStructure; defaultColor = ourMonitor.defaultFontTintValue; screenXMin = 0; screenYMin = 0; pageNumber = idNum; isMutable = false; if (!node.HasData) { throw new ArgumentException("Empty page?"); } if (node.HasValue("name")) { string value = node.GetValue("name").Trim(); if (!IsValidPageName(value)) { JUtil.LogMessage(ourMonitor, "Warning, name given for page #{0} is invalid, ignoring.", pageNumber); } else { name = value; } } else { JUtil.LogMessage(ourMonitor, "Warning, page #{0} has no name. It's much better if it does.", pageNumber); } isDefault |= node.HasValue("default"); if (node.HasValue("button")) { SmarterButton.CreateButton(thatMonitor.internalProp, node.GetValue("button"), this, thatMonitor.PageButtonClick); } // Page locking system -- simple locking: simpleLockingPage |= node.HasValue("lockingPage"); // and name-based locking. if (node.HasValue("disableSwitchingTo")) { string[] tokens = node.GetValue("disableSwitchingTo").Split(','); foreach (string token in tokens) { disableSwitchingTo.Add(token.Trim()); } } unlocker |= node.HasValue("unlockerPage"); if (node.HasValue("localMargins")) { Vector4 margindata = ConfigNode.ParseVector4(node.GetValue("localMargins")); screenXMin = (int)margindata.x; screenYMin = (int)margindata.y; screenWidth = screenWidth - (int)margindata.z - screenXMin; screenHeight = screenHeight - (int)margindata.w - screenYMin; } pageFont = node.GetInt("defaultFontNumber") ?? 0; if (node.HasValue("defaultFontTint")) { defaultColor = ConfigNode.ParseColor32(node.GetValue("defaultFontTint")); } if (node.HasValue("techsRequired")) { techsRequired = node.GetValue("techsRequired").Split(new[] { ' ', ',', ';', '\t', '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries).ToList(); } if (node.HasValue("fallbackOnNoTech")) { fallbackPageName = node.GetValue("fallbackOnNoTech").Trim(); } if (node.HasNode("CONTEXTREDIRECT")) { ConfigNode[] redirectnodes = node.GetNodes("CONTEXTREDIRECT"); for (int i = 0; i < redirectnodes.Length; ++i) { string[] redirects = redirectnodes[i].GetValues("redirect"); for (int j = 0; j < redirects.Length; ++j) { string[] tokens = redirects[j].Split(','); if (tokens.Length > 2 || !IsValidPageName(tokens[0].Trim()) || !IsValidPageName(tokens[1].Trim())) { JUtil.LogMessage(ourMonitor, "Warning, invalid page redirect statement on page #{0}.", pageNumber); continue; } redirectPages[tokens[0].Trim()] = tokens[1].Trim(); } string[] renumbers = redirectnodes[i].GetValues("renumber"); for (int j = 0; j < renumbers.Length; ++j) { string[] tokens = renumbers[j].Split(','); if (tokens.Length > 2) { JUtil.LogMessage(ourMonitor, "Warning, invalid global button redirect statement on page #{0}: requires two arguments.", pageNumber); continue; } int from, to; if (!int.TryParse(tokens[0], out from) || !int.TryParse(tokens[1], out to)) { JUtil.LogMessage(ourMonitor, "Warning, invalid global button redirect statement on page #{0}: something isn't a number", pageNumber); continue; } redirectGlobals[from] = to; } } JUtil.LogMessage(this, "Page '{2}' (#{0}) registers {1} page redirects and {3} global button redirects.", idNum, redirectPages.Count, name, redirectGlobals.Count); } foreach (ConfigNode handlerNode in node.GetNodes("PAGEHANDLER")) { MonoBehaviour handlerModule; HandlerSupportMethods supportMethods; MethodInfo handlerMethod = InstantiateHandler(handlerNode, ourMonitor, out handlerModule, out supportMethods); if (handlerMethod != null && handlerModule != null) { try { pageHandlerMethod = (Func <int, int, string>)Delegate.CreateDelegate(typeof(Func <int, int, string>), handlerModule, handlerMethod); } catch { JUtil.LogErrorMessage(ourMonitor, "Incorrect signature for the page handler method {0}", handlerModule.name); break; } pageHandlerS = supportMethods; isMutable = true; pageHandlerModule = handlerModule; break; } } if (pageHandlerMethod == null) { if (node.HasValue("text")) { text = JUtil.LoadPageDefinition(node.GetValue("text")); isMutable |= text.IndexOf("$&$", StringComparison.Ordinal) != -1; if (!isMutable) { processedText = text; } } else { text = string.Empty; } } if (node.HasValue("textOverlay")) { textOverlay = JUtil.LoadPageDefinition(node.GetValue("textOverlay")); } foreach (ConfigNode handlerNode in node.GetNodes("BACKGROUNDHANDLER")) { MonoBehaviour handlerModule; HandlerSupportMethods supportMethods; MethodInfo handlerMethod = InstantiateHandler(handlerNode, ourMonitor, out handlerModule, out supportMethods); if (handlerMethod != null && handlerModule != null) { try { backgroundHandlerMethod = (Func <RenderTexture, float, bool>)Delegate.CreateDelegate(typeof(Func <RenderTexture, float, bool>), handlerModule, handlerMethod); } catch { JUtil.LogErrorMessage(ourMonitor, "Incorrect signature for the background handler method {0}", handlerModule.name); break; } backgroundHandlerS = supportMethods; isMutable = true; showNoSignal = node.HasValue("showNoSignal"); background = BackgroundType.Handler; backgroundHandlerModule = handlerModule; break; } } if (background == BackgroundType.None) { if (node.HasValue("cameraTransform")) { JUtil.LogInfo(this, "WARNING: 'cameraTransform' in a PAGE node is deprecated, and the functionality will be removed in a future RPM release. Please use a JSISteerableCamera background handler instead."); isMutable = true; background = BackgroundType.Camera; camera = node.GetValue("cameraTransform"); cameraFOV = defaultFOV; cameraFlickerChance = node.GetFloat("flickerChance") ?? 0; cameraFlickerRange = node.GetInt("flickerRange") ?? 0; if (node.HasValue("fov")) { float fov; cameraFOV = float.TryParse(node.GetValue("fov"), out fov) ? fov : defaultFOV; } else if (node.HasValue("zoomFov") && node.HasValue("zoomButtons")) { Vector3 zoomFov = ConfigNode.ParseVector3(node.GetValue("zoomFov")); Vector2 zoomButtons = ConfigNode.ParseVector2(node.GetValue("zoomButtons")); if ((int)zoomFov.z != 0 && ((int)zoomButtons.x != (int)zoomButtons.y)) { maxFOV = Math.Max(zoomFov.x, zoomFov.y); minFOV = Math.Min(zoomFov.x, zoomFov.y); zoomSteps = (int)zoomFov.z; zoomUpButton = (int)zoomButtons.x; zoomDownButton = (int)zoomButtons.y; zoomSkip = (maxFOV - minFOV) / zoomSteps; currentZoom = 0; cameraFOV = maxFOV; } else { JUtil.LogMessage(ourMonitor, "Ignored invalid camera zoom settings on page {0}.", pageNumber); } } } } if (background == BackgroundType.None) { if (node.HasValue("textureURL")) { string textureURL = node.GetValue("textureURL").EnforceSlashes(); if (GameDatabase.Instance.ExistsTexture(textureURL)) { backgroundTexture = GameDatabase.Instance.GetTexture(textureURL, false); background = BackgroundType.Texture; } } } if (node.HasValue("textureInterlayURL")) { string textureURL = node.GetValue("textureInterlayURL").EnforceSlashes(); if (GameDatabase.Instance.ExistsTexture(textureURL)) { interlayTexture = GameDatabase.Instance.GetTexture(textureURL, false); } else { JUtil.LogErrorMessage(ourMonitor, "Interlay texture {0} could not be loaded.", textureURL); } } if (node.HasValue("textureOverlayURL")) { string textureURL = node.GetValue("textureOverlayURL").EnforceSlashes(); if (GameDatabase.Instance.ExistsTexture(textureURL)) { overlayTexture = GameDatabase.Instance.GetTexture(textureURL, false); } else { JUtil.LogErrorMessage(ourMonitor, "Overlay texture {0} could not be loaded.", textureURL); } } }