Skip to content

Instantly share code, notes, and snippets.

@TetsuOtter
Last active October 26, 2020 08:41
Show Gist options
  • Save TetsuOtter/ef42d29091f5b38d6d1ecba73ea7ed00 to your computer and use it in GitHub Desktop.
Save TetsuOtter/ef42d29091f5b38d6d1ecba73ea7ed00 to your computer and use it in GitHub Desktop.
BVE5/6用 入力デバイスプラグインのサンプルプログラム(Tetsu Otter作成版)
//Under the CC0 License
//I haven't done any checks in BVE5 or BVE6. If you have any questions or problems, please contact me : @Tetsu_Otter@twitter.com
//特に動作確認等行っていません. 何か質問や不具合等あれば, @Tetsu_Otter@twitter.comまで.
using System;
using System.Reflection;
using System.Windows.Forms;
using Mackoy.Bvets;
namespace TR.BVE5_6.InputDevicePISample
{
public class InputDeviceBVE5 : IInputDevice
{
public event InputEventHandler LeverMoved;
public event InputEventHandler KeyDown;
public event InputEventHandler KeyUp;
//各引数等に定数値
static class Axis
{
internal const int FuncKey = -1;
internal const int ATSKey = -2;
internal const int Reverser = 0;
internal const int Power = 1;
internal const int Brake = 2;
internal const int SHandle = 3;
internal const int Positive = 1;
internal const int Negative = 0;
}
static class KeyIndex
{
internal const int Horn1 = 0;
internal const int Horn2 = 1;
internal const int MusicHorn = 2;
internal const int ConstSPD = 3;
internal const int ATS_S = 4;
internal const int ATS_A1 = 5;
internal const int ATS_A2 = 6;
internal const int ATS_B1 = 7;
internal const int ATS_B2 = 8;
internal const int ATS_C1 = 9;
internal const int ATS_C2 = 10;
internal const int ATS_D = 11;
// ... 以下略
}
//BVE 設定ウィンドウ内「設定」キー押下で呼ばれるメソッド
public void Configure(IWin32Window owner)
=> MessageBox.Show(owner, "BVE5/6 入力デバイスPI サンプルプログラム(by Tetsu Otter)\nVersion : "//メッセージボックスのメッセージ部
+ Assembly.GetExecutingAssembly().GetName().Version.ToString(),//バージョンを取得している
Assembly.GetExecutingAssembly().GetName().Name);//メッセージボックスのウィンドウタイトル部
//プラグイン解放時(通常はBVE終了時)に呼ばれるメソッド
public void Dispose() {}
//プラグイン読み込み時(通常はBVE起動時 OR BVE設定内 入力デバイスPIのチェックボックスにチェックされたとき)に呼ばれるメソッド
public void Load(string settingsPath) {}
//ワンハンドルマスコンかどうかを記録する
bool IsOneHandle = false;
//各軸の可動範囲を教えてくれるメソッド
public void SetAxisRanges(int[][] ranges)
{
IsOneHandle =//ワンハンドルかどうかの判定
ranges[Axis.SHandle][Axis.Negative] < 0 //ワンハンドル軸の「Negative」(負)の方向が0未満 <=> 負の方向に可動
&& //かつ
0 < ranges[Axis.SHandle][Axis.Positive];//ワンハンドル軸の「Positive」(正)の方向が0より大きい <=> 正の方向に可動
//ranges[Axis.Brake][Axis.Positive]; //制動軸の可動範囲(0からこの値まで)
//ranges[Axis.Power][Axis.Positive]; //力行軸の可動範囲(0からこの値まで)
//ranges[Axis.Power][Axis.Negative]; //抑速軸の可動範囲(0からこの値まで) // 注意 : 負の値が入ります
//ranges[Axis.Reverser][Axis.Positive]; //レバーサー軸の正(Front)方向の可動範囲(通常は+1)
//ranges[Axis.Reverser][Axis.Negative]; //レバーサー軸の負(Back)方向の可動範囲(通常は-1)
}
public void Tick()
{
//毎フレーム実行する処理を書く
/*
// 例 : レバーサー「前」, 力行3段目, ブレーキ2段目, ATS_A2押す,Horn2離す を行う場合
LM(Axis.Reverser, Axis.Positive); //レバーサー「前」
LM(Axis.Power, 3); //力行3段目
LM(Axis.Brake, 2); //制動2段目
KE(KeyIndex.ATS_A2, true); //ATS_A2押す
KE(KeyIndex.Horn2, false); //Horn2離す
*/
}
//LeverMovedを楽に実行するためのメソッド
private void LM(int axis, int val) => LeverMoved?.Invoke(this, new InputEventArgs(axis, val));
//KeyDown/Upを楽に実行するためのメソッド
//index : KeyIndexクラスにある数値
//NewState:
// 押す(Down/Push) : true
// 離す(Up/Release): false
private void KE(int index, bool NewState)
{
bool IsFunc = index < 4;//基本機能キー群かどうか(ATSキー群じゃないか)の判定
var iea = new InputEventArgs(IsFunc ? Axis.FuncKey : Axis.ATSKey, index - (IsFunc ? 0 : 4));//キー情報を作成
if (NewState) KeyDown?.Invoke(this, iea);
else KeyUp?.Invoke(this, iea);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment