예제 #1
0
        /// <summary>
        /// Registers a PAction with the action manager. There is no corresponding Unregister
        /// call, so avoid spamming PActions.
        ///
        /// This call should occur after PUtil.InitLibrary() during the mod OnLoad(). If called
        /// earlier, it may fail with InvalidOperationException, and if called later, the
        /// user's custom key bind (if applicable) will be discarded.
        /// </summary>
        /// <param name="identifier">The identifier for this action.</param>
        /// <param name="title">The action's title.</param>
        /// <param name="binding">The default key binding for this action.</param>
        /// <returns>The action thus registered.</returns>
        /// <exception cref="InvalidOperationException">If PLib is not yet initialized.</exception>
        public static PAction Register(string identifier, LocString title,
                                       PKeyBinding binding = null)
        {
            // In case this call is used before the library was initialized
            if (!PUtil.PLibInit)
            {
                PUtil.InitLibrary(false);
                PUtil.LogWarning("PUtil.InitLibrary was not called before using " +
                                 "PAction.Register!");
            }
            int     actionID;
            PAction action;

            lock (PSharedData.GetLock(PRegistry.KEY_ACTION_LOCK)) {
                actionID = PSharedData.GetData <int>(PRegistry.KEY_ACTION_ID);
                if (actionID <= 0)
                {
                    throw new InvalidOperationException("PAction action ID is not set!");
                }
                PSharedData.PutData(PRegistry.KEY_ACTION_ID, actionID + 1);
            }
            action = new PAction(actionID, identifier, title);
            PActionManager.ConfigureTitle(action);
            action.AddKeyBinding(binding ?? new PKeyBinding());
            return(action);
        }
예제 #2
0
 /// <summary>
 /// Updates the localized title of this action as shown in the Key Bindings window.
 ///
 /// If you localize your key bindings, use this method in a postload patch to update
 /// the action title after all mods (including translation mods) have loaded.
 /// </summary>
 /// <param name="newTitle">The new localized title to apply.</param>
 public void UpdateLocalizedTitle(LocString newTitle)
 {
     if (newTitle != null && !string.IsNullOrEmpty(newTitle.text))
     {
         PActionManager.ConfigureTitle(new PAction(ID, Identifier, newTitle));
     }
 }
예제 #3
0
        /// <summary>
        /// Registers a PAction with the action manager. There is no corresponding Unregister
        /// call, so avoid spamming PActions.
        ///
        /// This call should occur after PUtil.LogModInit() during the mod OnLoad(). If called
        /// earlier, it may fail with InvalidOperationException, and if called later, the
        /// user's custom key bind (if applicable) will be discarded.
        /// </summary>
        /// <param name="identifier">The identifier for this action.</param>
        /// <param name="title">The action's title.</param>
        /// <param name="binding">The default key binding for this action.</param>
        /// <returns>The action thus registered.</returns>
        /// <exception cref="InvalidOperationException">If PLib is not yet initialized.</exception>
        public static PAction Register(string identifier, LocString title,
                                       PKeyBinding binding = null)
        {
            object locker = PSharedData.GetData <object>(PRegistry.KEY_ACTION_LOCK);
            int    actionID;

            if (locker == null)
            {
                throw new InvalidOperationException("PAction.Register called before PLib loaded!");
            }
            PAction action;

            lock (locker) {
                actionID = PSharedData.GetData <int>(PRegistry.KEY_ACTION_ID);
                if (actionID <= 0)
                {
                    throw new InvalidOperationException("PAction action ID is not set!");
                }
                PSharedData.PutData(PRegistry.KEY_ACTION_ID, actionID + 1);
            }
            action = new PAction(actionID, identifier, title);
            PActionManager.ConfigureTitle(action);
            action.AddKeyBinding(binding ?? new PKeyBinding());
            return(action);
        }
예제 #4
0
 /// <summary>
 /// Applied to KInputController to adjust array lengths if necessary.
 /// </summary>
 private static void QueueButtonEvent_Prefix(ref bool[] ___mActionState,
                                             KInputController.KeyDef key_def)
 {
     if (KInputManager.isFocused)
     {
         int max = PActionManager.Instance.GetMaxAction();
         key_def.mActionFlags = PActionManager.ExtendFlags(key_def.mActionFlags, max);
         ___mActionState      = PActionManager.ExtendFlags(___mActionState, max);
     }
 }
예제 #5
0
        /// <summary>
        /// Applied to GetKeycodeLocalized to quash warning spam on key codes that are valid
        /// but not handled by default.
        /// </summary>
        private static bool GetKeycodeLocalized_Prefix(KKeyCode key_code, ref string __result)
        {
            string newResult = PActionManager.GetExtraKeycodeLocalized(key_code);

            if (newResult != null)
            {
                __result = newResult;
            }
            return(newResult == null);
        }
예제 #6
0
        /// <summary>
        /// Adds a key binding to the game for this custom Action. It must be done after mods
        /// are loaded.
        /// </summary>
        /// <param name="binding">The default key binding for this action.</param>
        internal void AddKeyBinding(PKeyBinding binding)
        {
            var currentBindings = GameInputMapping.DefaultBindings;

            if (binding == null)
            {
                throw new ArgumentNullException("binding");
            }
            if (currentBindings != null)
            {
                // Only if GameInputMapping is initialized
                Action action     = GetKAction();
                bool   inBindings = false;
                int    n          = currentBindings.Length;
                for (int i = 0; i < n && !inBindings; i++)
                {
                    var cb = currentBindings[i];
                    if (cb.mAction == action)
                    {
                        // Already exists, but it really should not
                        PActionManager.LogKeyBindWarning(("Action {0} already exists; " +
                                                          "assigned to KeyCode {1}").F(action, cb.mKeyCode));
                        inBindings = true;
                        break;
                    }
                }
                if (!inBindings)
                {
                    var newBindings = new BindingEntry[n + 1];
                    Array.Copy(currentBindings, newBindings, n);
                    newBindings[n] = new BindingEntry(PActionManager.CATEGORY, binding.
                                                      GamePadButton, binding.Key, binding.Modifiers, action, true, false);
                    GameInputMapping.SetDefaultKeyBindings(newBindings);
                }
            }
            else
            {
                // Queue into PActionManager
                PActionManager.Instance.QueueKeyBind(this, binding);
            }
        }
예제 #7
0
 /// <summary>
 /// Applied to KeyDef (constructor) to adjust array lengths if necessary.
 /// </summary>
 private static void CKeyDef_Postfix(KInputController.KeyDef __instance)
 {
     __instance.mActionFlags = PActionManager.ExtendFlags(__instance.mActionFlags,
                                                          PActionManager.Instance.GetMaxAction());
 }
예제 #8
0
 /// <summary>
 /// Applied to KInputController to adjust array lengths if necessary.
 /// </summary>
 private static void IsActive_Prefix(ref bool[] ___mActionState)
 {
     ___mActionState = PActionManager.ExtendFlags(___mActionState, PActionManager.
                                                  Instance.GetMaxAction());
 }