/// <summary> /// Creates the action map /// </summary> /// <returns> /// An array of Action objects describing the game action /// to device object map for this application /// </returns> private void CreateActionMap(ActionCollection map) { #region Step 2: Define the action map. // **************************************************************************** // Step 2: Define the action map. // // The action map instructs DirectInput on how to map game actions to device // objects. By selecting a predefined game genre that closely matches our game, // you can largely avoid dealing directly with device details. For this sample // we've selected the DIVIRTUAL_FIGHTING_HAND2HAND, and this constant will need // to be selected into the DIACTIONFORMAT structure later to inform DirectInput // of our choice. Every device has a mapping from genre actions to device // objects, so mapping your game actions to genre actions almost guarantees // an appropriate device configuration for your game actions. // // If DirectInput has already been given an action map for this GUID, it // will have created a user map for this application // (C:\Program Files\Common Files\DirectX\DirectInput\User Maps\*.ini). If a // map exists, DirectInput will use the action map defined in the stored user // map instead of the map defined in your program. This allows the user to // customize controls without losing changes when the game restarts. If you // wish to make changes to the default action map without changing the // GUID, you will need to delete the stored user map from your hard drive // for the system to detect your changes and recreate a stored user map. // **************************************************************************** #endregion // Device input (joystick, etc.) that is pre-defined by dinput according // to genre type. The genre for this app is Action->Hand to Hand Fighting. map.Add(CreateAction(GameActions.Walk, FightingHandToHand.AxisLateral, 0, "Walk left/right")); map.Add(CreateAction(GameActions.Block, FightingHandToHand.ButtonBlock, 0, "Block")); map.Add(CreateAction(GameActions.Kick, FightingHandToHand.ButtonKick, 0, "Kick")); map.Add(CreateAction(GameActions.Punch, FightingHandToHand.ButtonPunch, 0, "Punch")); map.Add(CreateAction(GameActions.TheDeAppetizer, FightingHandToHand.ButtonSpecial1, 0, "\"The De-Appetizer\"")); // Map the apologize button to any button on the device. directInput // defines several "Any-Control Constants" for mapping game actions to // any device object of a particular type. map.Add(CreateAction(GameActions.Apologize, DInputHelper.ButtonAny(1), 0, "Apologize")); // Keyboard input mappings map.Add(CreateAction(GameActions.WalkLeft, Keyboard.Left, 0, "Walk left")); map.Add(CreateAction(GameActions.WalkRight, Keyboard.Right, 0, "Walk right")); map.Add(CreateAction(GameActions.Block, Keyboard.B, 0, "Block")); map.Add(CreateAction(GameActions.Kick, Keyboard.K, 0, "Kick")); map.Add(CreateAction(GameActions.Punch, Keyboard.P, 0, "Punch")); map.Add(CreateAction(GameActions.TheDeAppetizer, Keyboard.D, 0, "\"The De-Appetizer\"")); map.Add(CreateAction(GameActions.Apologize, Keyboard.A, 0, "Apologize")); // The AppFixed constant can be used to instruct directInput that the // current mapping can not be changed by the user. map.Add(CreateAction(GameActions.Quit, Keyboard.Q, ActionAttributeFlags.AppFixed, "Quit")); // Mouse input mappings map.Add(CreateAction(GameActions.Walk, Mouse.XAxis, 0, "Walk")); map.Add(CreateAction(GameActions.Punch, Mouse.Button0, 0, "Punch")); map.Add(CreateAction(GameActions.Kick, Mouse.Button1, 0, "Kick")); }
//* -----------------------------------------------------------------------* /// <summary> /// フォース フィードバックのプリセットデータを作成します。 /// </summary> /// /// <param name="anAxis">軸データ</param> /// <param name="nMagnitude">フォースの量</param> /// <param name="nDuration">フォースがかかる時間</param> /// <returns>フォース フィードバック管理クラス</returns> /// <exception cref="NotSupportedException"></exception> private EffectObject createEffect(int[] anAxis, int nMagnitude, int nDuration) { foreach (EffectInformation ei in device.GetEffects(EffectType.All)) { if (DInputHelper.GetTypeCode(ei.EffectType) == (int)EffectType.ConstantForce) { Effect effect = createEffect(anAxis); effect.EffectType = EffectType.ConstantForce; effect.Constant = new ConstantForce(); effect.Constant.Magnitude = nMagnitude; // HACK : ここで例外もなしにコケることがある。なんでだ? return(new EffectObject(ei.EffectGuid, effect, device)); } } throw new NotSupportedException(); }
//* -----------------------------------------------------------------------* /// <summary> /// フォース フィードバックのプリセットデータを作成します。 /// </summary> /// /// <param name="anAxis">軸データ</param> /// <param name="nStartMagnitude">フォースが始まる時の値</param> /// <param name="nLastMagnitude">フォースが終わる時の値</param> /// <param name="nDuration">フォースがかかる時間</param> /// <returns>フォース フィードバック管理クラス</returns> /// <exception cref="NotSupportedException"></exception> private EffectObject createEffect( int[] anAxis, int nStartMagnitude, int nLastMagnitude, int nDuration ) { foreach (EffectInformation ei in device.GetEffects(EffectType.All)) { if (DInputHelper.GetTypeCode(ei.EffectType) == (int)EffectType.RampForce) { Effect effect = createEffect(anAxis); effect.EffectType = EffectType.RampForce; effect.RampStruct = new RampForce(); effect.RampStruct.Start = nStartMagnitude; effect.RampStruct.End = nLastMagnitude; return(new EffectObject(ei.EffectGuid, effect, device)); } } throw new NotSupportedException(); }
public JOYSTICK_TYPE Connect(System.Windows.Forms.Control Parent) { ForceFeedbackSupported = false; //create force feedback capable joystick device. try { foreach (DeviceInstance di in Manager.GetDevices( DeviceClass.GameControl, EnumDevicesFlags.AttachedOnly | EnumDevicesFlags.ForceFeeback)) { ForceFeedbackSupported = true; device = new Device(di.InstanceGuid); break; } if (device == null) { //create force feedback capable joystick device. foreach (DeviceInstance di in Manager.GetDevices( DeviceClass.GameControl, EnumDevicesFlags.AttachedOnly)) { device = new Device(di.InstanceGuid); break; } //Throw exception if joystick not found. //throw new Exception("No joystick that supports forced feedback found."); if (device == null) { return(JOYSTICK_TYPE.NO_JOYSTICK); } } } catch (System.TypeInitializationException) { // Managed Direct X doesn't seem to be present or working return(JOYSTICK_TYPE.NO_JOYSTICK); } //set cooperative level. device.SetCooperativeLevel( Parent, CooperativeLevelFlags.Exclusive | CooperativeLevelFlags.Background); if (ForceFeedbackSupported) { // Since we will be playing force feedback effects, we should disable the // auto-centering spring. device.Properties.AutoCenter = false; } //Set axis mode absolute. device.Properties.AxisModeAbsolute = true; //Acquire joystick for capturing. device.Acquire(); //Configure axes int[] axis = null; foreach (DeviceObjectInstance doi in device.Objects) { //Set axes ranges. if ((doi.ObjectId & (int)DeviceObjectTypeFlags.Axis) != 0) { device.Properties.SetRange( ParameterHow.ById, doi.ObjectId, new InputRange(-5000, 5000)); } int[] temp; // Get info about first two FF axii on the device if ((doi.Flags & (int)ObjectInstanceFlags.Actuator) != 0) { if (axis != null) { temp = new int[axis.Length + 1]; axis.CopyTo(temp, 0); axis = temp; } else { axis = new int[1]; } // Store the offset of each axis. axis[axis.Length - 1] = doi.Offset; if (axis.Length == 2) { break; } } } //See if joystick supports ConstantForce and set it. foreach (EffectInformation ei in device.GetEffects(EffectType.All)) { //If the joystick supports ConstantForce, then apply it. if (DInputHelper.GetTypeCode(ei.EffectType) == (int)EffectType.ConstantForce) { // Fill in some generic values for the effect. e = new Effect(); e.SetDirection(new int[axis.Length]); e.SetAxes(new int[axis.Length]); e.ConditionStruct = new Condition[axis.Length]; e.EffectType = EffectType.ConstantForce; e.Constant = new ConstantForce(); e.Constant.Magnitude = 10000; e.Duration = (int)DI.Infinite; e.Gain = 10000; e.SamplePeriod = 0; e.TriggerButton = (int)Microsoft.DirectX.DirectInput.Button.NoTrigger; e.TriggerRepeatInterval = (int)DI.Infinite; e.Flags = EffectFlags.ObjectOffsets | EffectFlags.Cartesian; e.SetAxes(axis); var dd = e.GetDirection(); dd[0] = -100; dd[1] = 100; e.SetDirection(dd); // Create the effect, using the passed in guid. eo = new EffectObject(ei.EffectGuid, e, device); eo.Start(1, EffectStartFlags.NoDownload); force_ei = ei; break; } } if (eo == null) { return(JOYSTICK_TYPE.NO_FORCE_FEEDBACK); } return(JOYSTICK_TYPE.FORCE_FEEDBACK); }