public static void Wrap(Vessel parent, Action <BaseEvent, bool> pass) { UIPartActionController controller = UIPartActionController.Instance; if (!controller) { return; } // Get the open context menus FieldInfo listFieldInfo = controller.GetType().GetFields(BindingFlags.Instance | BindingFlags.NonPublic) .First(fi => fi.FieldType == typeof(List <UIPartActionWindow>)); List <UIPartActionWindow> openWindows = (List <UIPartActionWindow>)listFieldInfo.GetValue(controller); foreach (UIPartActionWindow window in openWindows.Where(l => l.part.vessel == parent)) { // Get the list of all UIPartActionItem's FieldInfo itemsFieldInfo = typeof(UIPartActionWindow).GetFields(BindingFlags.Instance | BindingFlags.NonPublic) .First(fi => fi.FieldType == typeof(List <UIPartActionItem>)); List <UIPartActionItem> item = (List <UIPartActionItem>)itemsFieldInfo.GetValue(window); // We only need the UIPartActionEventItem's IEnumerable <UIPartActionItem> actionEventButtons = item.Where(l => (l as UIPartActionEventItem) != null); foreach (UIPartActionEventItem button in actionEventButtons) { BaseEvent originalEvent = button.Evt; // Get the BaseEventDelegate object from the button FieldInfo partEventFieldInfo = typeof(BaseEvent).GetFields(BindingFlags.Instance | BindingFlags.NonPublic) .First(fi => fi.FieldType == typeof(BaseEventDelegate)); BaseEventDelegate partEvent = (BaseEventDelegate)partEventFieldInfo.GetValue(originalEvent); object[] customAttributes = partEvent.Method.GetCustomAttributes(typeof(KSPEvent), true); // Look for the custom attribute skip_control bool skip_control = customAttributes.Any(a => ((KSPEvent)a).category.Contains("skip_control")); if (!skip_control) { // Look for the custom attribute skip_delay bool ignore_delay = customAttributes.Any(a => ((KSPEvent)a).category.Contains("skip_delay")); // Override the old BaseEvent with our BaseEvent to the button FieldInfo eventField = typeof(UIPartActionEventItem).GetFields(BindingFlags.Instance | BindingFlags.NonPublic) .First(fi => fi.FieldType == typeof(BaseEvent)); BaseEventList eventList = originalEvent.listParent; int listIndex = eventList.IndexOf(originalEvent); // create the new BaseEvent BaseEvent hookedEvent = Wrapper.CreateWrapper(originalEvent, pass, ignore_delay); eventList.RemoveAt(listIndex); eventList.Add(hookedEvent); eventField.SetValue(button, hookedEvent); } } } }
public static void AddClick(Component component, BaseEventDelegate onClick, bool passClickEvent = false, bool posForce = false) { ClickListener cl = Get <ClickListener>(component); cl.onClick += onClick; cl.canPassClickEvent = passClickEvent; }
private void OnPartActionUICreate(Part part) { // Get the PAW window UIPartActionWindow paw = UIPartActionController.Instance.GetItem(part); // Prevent the button to be added once created if (paw.ListItems.Exists(p => p is UIPartActionButton && ((UIPartActionButton)p).Evt.name == "ConfigurePart")) { return; } BaseEventDelegate del = new BaseEventDelegate(delegate { OnPawConfigure(part); }); string buttonTitle; if (part.Modules.Contains <ModuleB9PartSwitch>() || part.Modules.Contains <ModulePartVariants>()) { buttonTitle = "Part info and configuration"; } else { buttonTitle = "Part info"; } BaseEvent baseEvent = new BaseEvent(null, "ConfigurePart", del, new KSPEvent()); baseEvent.guiName = buttonTitle; baseEvent.active = true; baseEvent.guiActive = true; baseEvent.guiActiveEditor = true; paw.AddEventControl(baseEvent, part, null); }
public static void ReplaceEvent(this PartModule module, string name, BaseEventDelegate replaceAction) { if (!module.Events.Contains(name)) return; var oldEvent = module.Events[name]; var callback = oldEvent.TryGetPropertyValue("onEvent"); if (callback == null) return; var baseEvent = new BaseEvent(module.Events, name, replaceAction); oldEvent.CopyEventData(baseEvent); module.Events.Remove(oldEvent); module.Events.Add(baseEvent); }
// Adding Selfie button private void addSelfie(KerbalEVA evaCtl) { Log.dbg("Adding Selfie to {0}", evaCtl.GUIName); BaseEventList pEvents = evaCtl.Events; BaseEventDelegate slf = new BaseEventDelegate(TakeSelfie); KSPEvent evt = new KSPEvent { active = true, externalToEVAOnly = true, guiActive = true, guiActiveEditor = false, guiActiveUnfocused = false, guiActiveUncommand = false, guiName = "Take Selfie", name = "TakeSelfie" }; BaseEvent selfie = new BaseEvent(pEvents, "Take Selfie", slf, evt); pEvents.Add(selfie); selfie.guiActive = true; selfie.active = true; }
public void CreateItemGroup(string[] btnGroupDisplayName, string[] btnGroupParameter) { nonStandFloor = new List <ScrollItem>(); allScrollItem = new List <ScrollItem>(); if (btnGroupParameter == null) { btnGroupParameter = btnGroupDisplayName; } ScrollItem[] lastScrollItem = scrollRect.content.GetComponentsInChildren <ScrollItem>(); // Debug.Log(lastScrollItem.Length); for (int i = 0; i < lastScrollItem.Length; i++) { if (lastScrollItem[i] != null) { DestroyObject(lastScrollItem[i].gameObject); } } Vector2 startPos = itemSpace; for (int i = 0; i < btnGroupDisplayName.Length; i++) { ScrollItem sItem = Instantiate(itemPrefab, new Vector3(0, 0, 0), new Quaternion(), scrollRect.content); Vector2 itemSize = sItem.SetupItem(btnGroupDisplayName[i], scroolItemHeight, startPos, scrollItemMinWidth, fontSize, paddingFactor); Vector2 iSpace = Vector2.zero; // sItem.imageButton.btnNameForRemote = "B" + i.ToString() + "_" + this.gameObject.name + btnGroupParameter[i]; sItem.imageButton.btnNameForRemote = Regex.Replace(sItem.imageButton.btnNameForRemote, @"[\u4e00-\u9fa5]", ""); RemoteGather.AddImageToGroup(sItem.imageButton, true); sItem.imageButton.RecordOrginState(); if (itemDirection == ItemDirection.水平) { itemSize = new Vector2(itemSize.x, 0); iSpace = new Vector2(itemSpace.x, 0); } else { itemSize = new Vector2(0, -itemSize.y); iSpace = new Vector2(0, itemSpace.y); } // if(i!= btnGroupDisplayName.Length-1) startPos += itemSize + iSpace; //设置item的按钮事件 BaseEventDelegate itemTrue = new BaseEventDelegate(); BaseEventDelegate itemfalse = new BaseEventDelegate(); itemTrue.parameterTargetSlot = new int[] { 0, 3 }; itemfalse.parameterTargetSlot = new int[] { 0, 3 }; itemTrue.parameterList = new EventParametar[2]; itemfalse.parameterList = new EventParametar[2]; itemTrue.parameterList[0].pObject = sItem; itemTrue.parameterList[1].pString = btnGroupParameter[i]; itemfalse.parameterList[0].pObject = sItem; itemfalse.parameterList[1].pString = btnGroupParameter[i]; itemTrue.excuteMethodName = "ItemBtnExeTrue"; itemfalse.excuteMethodName = "ItemBtnExeFalse"; itemTrue.targetMono = this; itemfalse.targetMono = this; itemTrue.currentEditorChooseFunName = "ScrollMenu/ItemBtnExeTrue"; itemTrue.lastEditorChooseFunName = "ScrollMenu/ItemBtnExeTrue"; itemfalse.currentEditorChooseFunName = "ScrollMenu/ItemBtnExeTrue"; itemfalse.lastEditorChooseFunName = "ScrollMenu/ItemBtnExeTrue"; sItem.imageButton.trueEventList.Add(itemTrue); sItem.imageButton.falseEventList.Add(itemfalse); allScrollItem.Add(sItem); //判断参数是否可以转成有效的数字,如果可以就是楼层 int temp; if (!int.TryParse(btnGroupParameter[i].Replace("F", ""), out temp)) { nonStandFloor.Add(sItem); } } if (itemDirection == ItemDirection.水平) { scrollRect.content.sizeDelta = new Vector2(startPos.x, scrollRect.GetComponent <RectTransform>().sizeDelta.y); //设置居中 if (startPos.x < (scrollRect.GetComponent <RectTransform>().sizeDelta.x - 200)) { Vector2 offset = new Vector2((scrollRect.GetComponent <RectTransform>().sizeDelta.x - 200 - startPos.x) * 0.5f, 0f); // Debug.Log(offset); foreach (ScrollItem s in scrollRect.content.GetComponentsInChildren <ScrollItem>()) { s.GetComponent <RectTransform>().anchoredPosition += offset; s.imageButton.RecordOrginState(); } } } else { scrollRect.content.sizeDelta = new Vector2(scrollRect.GetComponent <RectTransform>().sizeDelta.x, -startPos.y); } }
private void reinitEvents(Vessel v) { printDebug("entered"); if (v.evaController == null) { return; } KerbalEVA evaCtl = v.evaController; ProtoCrewMember crew = v.GetVesselCrew() [0]; String kerbalName = crew.name; printDebug("evCtl found; checking name: " + kerbalName); Tourist t; if (!tourists.TryGetValue(kerbalName, out t)) { return; } printDebug("among tourists: " + kerbalName); t.smile = false; t.taken = false; if (!Tourist.isTourist(v.GetVesselCrew()[0])) { printDebug("...but is a crew"); return; // not a real tourist } // Change crew type right away to avoid them being crew after recovery crew.type = ProtoCrewMember.KerbalType.Tourist; BaseEventList pEvents = evaCtl.Events; foreach (BaseEvent e in pEvents) { printDebug("disabling event " + e.guiName); e.guiActive = false; e.guiActiveUnfocused = false; e.guiActiveUncommand = false; } // Adding Selfie button BaseEventDelegate slf = new BaseEventDelegate(TakeSelfie); KSPEvent evt = new KSPEvent(); evt.active = true; evt.externalToEVAOnly = true; evt.guiActive = true; evt.guiActiveEditor = false; evt.guiActiveUnfocused = false; evt.guiActiveUncommand = false; evt.guiName = "Take Selfie"; evt.name = "TakeSelfie"; BaseEvent selfie = new BaseEvent(pEvents, "Take Selfie", slf, evt); pEvents.Add(selfie); selfie.guiActive = true; selfie.active = true; foreach (PartModule m in evaCtl.part.Modules) { if (!m.ClassName.Equals("ModuleScienceExperiment")) { continue; } printDebug("science module id: " + ((ModuleScienceExperiment)m).experimentID); // Disable all science foreach (BaseEvent e in m.Events) { e.guiActive = false; e.guiActiveUnfocused = false; e.guiActiveUncommand = false; } foreach (BaseAction a in m.Actions) { a.active = false; } } printDebug("Initializing sound"); // Should we always invalidate cache??? fx = null; getOrCreateAudio(evaCtl.part.gameObject); }
public void GenPoint360() { Debug.Log("GenPoint360"); for (int i = 0; i < point360Floors.Length; i++) { ColliderTriggerButton[] childTran = point360Floors[i].colliderTriggerRoot.GetComponentsInChildren <ColliderTriggerButton>(); for (int k = 0; k < childTran.Length; k++) { DestroyImmediate(childTran[k].gameObject); } for (int j = 0; j < point360Floors[i].cubemapGroup.Length; j++) { string[] splitNameString = point360Floors[i].cubemapGroup[j].name.Split('_'); // Debug.Log(splitNameString.Length); if (splitNameString.Length != 4) { Debug.LogError(point360Floors[i].cubemapGroup[j].name + "名字不标准"); return; } string cubemapName = splitNameString[0]; float fx, fy, fz; Vector3 pos = new Vector3(); if (float.TryParse(splitNameString[1], out fx) && float.TryParse(splitNameString[2], out fy) && float.TryParse(splitNameString[3], out fz)) { if (posUnit == Unit.mm) { fx = 0.001f * fx; fy = 0.001f * fy; fz = 0.001f * fz; } else if (posUnit == Unit.cm) { fx = 0.01f * fx; fy = 0.01f * fy; fz = 0.01f * fz; } if (posUpAxe == UpAxe.Y) { pos = new Vector3(fx, fy, fz); } else { pos = new Vector3(-fx, fz, fy); } GameObject point = GameObject.Instantiate(point360Perfab, Vector3.zero, new Quaternion(), point360Floors[i].colliderTriggerRoot); point.name = cubemapName; point.transform.localPosition = new Vector3(pos.x, pos.y - baseHeight, pos.z); ColliderTriggerButton co = point.GetComponent <ColliderTriggerButton>(); co.btnNameForRemote = cubemapName; //设置colliderTriggerTrue的按钮事件 BaseEventDelegate colliderTriggerTrue = new BaseEventDelegate(); colliderTriggerTrue.parameterTargetSlot = new int[] { 0, 6, 2 }; colliderTriggerTrue.parameterList = new EventParametar[3]; colliderTriggerTrue.parameterList[0].pObject = point360Floors[i].cubemapGroup[j]; colliderTriggerTrue.parameterList[1].pVec3 = new Vector3(point.transform.position.x, point.transform.position.y + baseHeight, point.transform.position.z); colliderTriggerTrue.parameterList[2].pFloat = 0; colliderTriggerTrue.excuteMethodName = "ChangeCubemap"; colliderTriggerTrue.targetMono = this; colliderTriggerTrue.currentEditorChooseFunName = "Point360Manager/ChangeCubemap"; colliderTriggerTrue.lastEditorChooseFunName = "Point360Manager/ChangeCubemap"; co.trueEventList.Add(colliderTriggerTrue); } else { Debug.LogError(point360Floors[i].cubemapGroup[j].name + "位置信息错误,请检查!"); } } } }
public WrappedEvent(BaseEvent originalEvent, BaseEventList baseParentList, string name, BaseEventDelegate baseActionDelegate, KSPEvent kspEvent) : base(baseParentList, name, baseActionDelegate, kspEvent) { _originalEvent = originalEvent; }
public static void WrapPartActionEventItem(Part part, Action <BaseEvent, bool> passthrough) { var controller = UIPartActionController.Instance; if (!controller) { return; } // get the part action window corresponding to the part var window = controller.GetItem(part); if (window == null) { return; } // get all the items that makes this window (toggle buttons, sliders, etc.) var partActionItems = window.ListItems; // loop through all of those UI components for (var i = 0; i < partActionItems.Count(); i++) { // check that the part action item is actually a UIPartActionFieldItem (it could be a UIPartActionEventItem) var uiPartActionEventItem = (partActionItems[i] as UIPartActionEventItem); if (uiPartActionEventItem == null) { continue; } // get event from button BaseEvent originalEvent = uiPartActionEventItem.Evt; // Search for the BaseEventDelegate (BaseEvent.onEvent) field defined for the current BaseEvent type. // Note that 'onEvent' is protected, so we have to go through reflection. FieldInfo partEventFieldInfo = typeof(BaseEvent).GetFields(BindingFlags.Instance | BindingFlags.NonPublic) .First(fi => fi.FieldType == typeof(BaseEventDelegate)); // Get the actual value of the 'onEvent' field BaseEventDelegate partEvent = (BaseEventDelegate)partEventFieldInfo.GetValue(originalEvent); // Gets the method represented by the delegate and from this method returns an array of custom attributes applied to this member. // Simply put, we want all [KSPEvent] attributes applied to the BaseEventDelegate.Method field. object[] customAttributes = partEvent.Method.GetCustomAttributes(typeof(KSPEvent), true); // Look for the custom attribute skip_control bool skipControl = customAttributes.Any(a => ((KSPEvent)a).category.Contains("skip_control")); if (skipControl) { continue; } /* * Override the old BaseEvent with our BaseEvent to the button */ // fix problems with other mods (behavior not seen with KSP) when the customAttributes list is empty. KSPEvent kspEvent = !customAttributes.Any() ? WrappedEvent.KspEventFromBaseEvent(originalEvent) : (KSPEvent)customAttributes[0]; // Look for the custom attribute skip_delay bool ignoreDelay = customAttributes.Any(a => ((KSPEvent)a).category.Contains("skip_delay")); // create the new BaseEvent BaseEvent hookedEvent = EventWrapper.CreateWrapper(originalEvent, passthrough, ignoreDelay, kspEvent); // get the original event index in the event list BaseEventList eventList = originalEvent.listParent; int listIndex = eventList.IndexOf(originalEvent); // remove the original event in the event list and add our hooked event eventList.RemoveAt(listIndex); eventList.Add(hookedEvent); // get the baseEvent field from UIPartActionEventItem (note: this is uiPartActionEventItem.Evt, but we can't set its value...) FieldInfo baseEventField = typeof(UIPartActionEventItem).GetFields(BindingFlags.Instance | BindingFlags.NonPublic) .First(fi => fi.FieldType == typeof(BaseEvent)); // replace the button baseEvent value with our hooked event baseEventField.SetValue(uiPartActionEventItem, hookedEvent); } }
public WrappedEvent(BaseEvent originalEvent, BaseEventList baseParentList, string name, BaseEventDelegate baseActionDelegate) : base(baseParentList, name, baseActionDelegate) { this.originalEvent = originalEvent; }