1
0
mirror of synced 2025-02-07 14:51:25 +01:00

Early returns and Switch Expressions (#715)

* refactor: shorten getGamePad- getJoystickId

* refactor: use switch expression

* refactor: CPad early return & shortened bool returns

* refactor: CTextConsole
This commit is contained in:
Julian Holfeld 2024-10-22 17:57:25 +02:00 committed by GitHub
parent a38003bdb2
commit 12eec18ab9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 201 additions and 215 deletions

View File

@ -3571,31 +3571,23 @@ namespace OpenTaiko {
private void GetJoystickID(string keyDescription) { private void GetJoystickID(string keyDescription) {
string[] strArray = keyDescription.Split(new char[] { ',' }); string[] strArray = keyDescription.Split(new char[] { ',' });
if (strArray.Length >= 2) { if (strArray.Length < 2 || !int.TryParse(strArray[0], out int result) || result < 0 || result > 9) {
int result = 0; return;
if ((int.TryParse(strArray[0], out result) && (result >= 0)) && (result <= 9)) {
if (this.dicJoystick.ContainsKey(result)) {
this.dicJoystick.Remove(result);
} }
this.dicJoystick.Remove(result);
this.dicJoystick.Add(result, strArray[1]); this.dicJoystick.Add(result, strArray[1]);
} }
}
}
private void GetGamepadID(string keyDescription) { private void GetGamepadID(string keyDescription) {
string[] strArray = keyDescription.Split(new char[] { ',' }); string[] strArray = keyDescription.Split(new char[] { ',' });
if (strArray.Length >= 2) { if (strArray.Length < 2 || !int.TryParse(strArray[0], out int result) || result < 0 || result > 9) {
int result = 0; return;
if ((int.TryParse(strArray[0], out result) && (result >= 0)) && (result <= 9)) {
if (this.dicGamepad.ContainsKey(result)) {
this.dicGamepad.Remove(result);
} }
this.dicGamepad.Remove(result);
this.dicGamepad.Add(result, strArray[1]); this.dicGamepad.Add(result, strArray[1]);
} }
}
}
private void ClearAllKeyAssignments() { private void ClearAllKeyAssignments() {
this.KeyAssign = new CKeyAssign(); this.KeyAssign = new CKeyAssign();

View File

@ -345,23 +345,14 @@ namespace OpenTaiko {
public T Unknown; public T Unknown;
public T this[int index] { public T this[int index] {
get { get {
switch (index) { return index switch {
case (int)EInstrumentPad.Drums: (int)EInstrumentPad.Drums => this.Drums,
return this.Drums; (int)EInstrumentPad.Guitar => this.Guitar,
(int)EInstrumentPad.Bass => this.Bass,
case (int)EInstrumentPad.Guitar: (int)EInstrumentPad.Taiko => this.Taiko,
return this.Guitar; (int)EInstrumentPad.Unknown => this.Unknown,
_ => throw new IndexOutOfRangeException()
case (int)EInstrumentPad.Bass: };
return this.Bass;
case (int)EInstrumentPad.Taiko:
return this.Taiko;
case (int)EInstrumentPad.Unknown:
return this.Unknown;
}
throw new IndexOutOfRangeException();
} }
set { set {
switch (index) { switch (index) {

View File

@ -11,12 +11,11 @@
if (id >= names.Length || id >= data.Length) if (id >= names.Length || id >= data.Length)
return false; return false;
string ext = ""; string ext = this.data[id].format switch {
"WAV" => ".wav",
if (data[id].format == "WAV") "OGG" => ".ogg",
ext = ".wav"; _ => ""
else if (data[id].format == "OGG") };
ext = ".ogg";
don[player] = data[id].path + "dong" + ext; don[player] = data[id].path + "dong" + ext;
ka[player] = data[id].path + "ka" + ext; ka[player] = data[id].path + "ka" + ext;
@ -45,15 +44,12 @@
private void tLoadFile(string path) { private void tLoadFile(string path) {
data = ConfigManager.GetConfig<List<HitSoundsData>>(path).ToArray(); data = ConfigManager.GetConfig<List<HitSoundsData>>(path).ToArray();
names = new string[data.Length]; names = new string[data.Length];
for (int i = 0; i < data.Length; i++) { for (int i = 0; i < data.Length; i++) {
names[i] = data[i].name; names[i] = data[i].name;
} }
} }
#endregion #endregion
} }
} }

View File

@ -35,7 +35,10 @@ namespace OpenTaiko {
// すべての入力デバイスについて… // すべての入力デバイスについて…
foreach (IInputDevice device in this.inputManager.InputDevices) { foreach (IInputDevice device in this.inputManager.InputDevices) {
if ((device.InputEvents != null) && (device.InputEvents.Count != 0)) { if (device.InputEvents == null || device.InputEvents.Count == 0) {
continue;
}
foreach (STInputEvent event2 in device.InputEvents) { foreach (STInputEvent event2 in device.InputEvents) {
for (int i = 0; i < stkeyassignArray.Length; i++) { for (int i = 0; i < stkeyassignArray.Length; i++) {
switch (stkeyassignArray[i].InputDevice) { switch (stkeyassignArray[i].InputDevice) {
@ -77,11 +80,13 @@ namespace OpenTaiko {
} }
} }
} }
}
return list; return list;
} }
public bool bPressed(EInstrumentPad part, EPad pad) { public bool bPressed(EInstrumentPad part, EPad pad) {
if (part != EInstrumentPad.Unknown) { if (part == EInstrumentPad.Unknown) {
return false;
}
CConfigIni.CKeyAssign.STKEYASSIGN[] stkeyassignArray = this.rConfigIni.KeyAssign[(int)part][(int)pad]; CConfigIni.CKeyAssign.STKEYASSIGN[] stkeyassignArray = this.rConfigIni.KeyAssign[(int)part][(int)pad];
for (int i = 0; i < stkeyassignArray.Length; i++) { for (int i = 0; i < stkeyassignArray.Length; i++) {
@ -95,7 +100,7 @@ namespace OpenTaiko {
case EInputDevice.MIDIInput: { case EInputDevice.MIDIInput: {
IInputDevice device2 = this.inputManager.MidiIn(stkeyassignArray[i].ID); IInputDevice device2 = this.inputManager.MidiIn(stkeyassignArray[i].ID);
if ((device2 == null) || !device2.KeyPressed(stkeyassignArray[i].Code)) if (device2 == null || !device2.KeyPressed(stkeyassignArray[i].Code))
break; break;
this.detectedDevice.MIDIIN = true; this.detectedDevice.MIDIIN = true;
@ -106,7 +111,7 @@ namespace OpenTaiko {
break; break;
IInputDevice device = this.inputManager.Joystick(stkeyassignArray[i].ID); IInputDevice device = this.inputManager.Joystick(stkeyassignArray[i].ID);
if ((device == null) || !device.KeyPressed(stkeyassignArray[i].Code)) if (device == null || !device.KeyPressed(stkeyassignArray[i].Code))
break; break;
this.detectedDevice.Joypad = true; this.detectedDevice.Joypad = true;
@ -117,7 +122,7 @@ namespace OpenTaiko {
break; break;
IInputDevice device = this.inputManager.Gamepad(stkeyassignArray[i].ID); IInputDevice device = this.inputManager.Gamepad(stkeyassignArray[i].ID);
if ((device == null) || !device.KeyPressed(stkeyassignArray[i].Code)) if (device == null || !device.KeyPressed(stkeyassignArray[i].Code))
break; break;
this.detectedDevice.Gamepad = true; this.detectedDevice.Gamepad = true;
@ -131,23 +136,25 @@ namespace OpenTaiko {
return true; return true;
} }
} }
}
return false; return false;
} }
public bool bPressedDGB(EPad pad) { public bool bPressedDGB(EPad pad) {
if (!this.bPressed(EInstrumentPad.Drums, pad) && !this.bPressed(EInstrumentPad.Guitar, pad)) { if (!this.bPressed(EInstrumentPad.Drums, pad) && !this.bPressed(EInstrumentPad.Guitar, pad)) {
return this.bPressed(EInstrumentPad.Bass, pad); return this.bPressed(EInstrumentPad.Bass, pad);
} }
return true; return true;
} }
public bool bPressedGB(EPad pad) { public bool bPressedGB(EPad pad) {
if (!this.bPressed(EInstrumentPad.Guitar, pad)) { return this.bPressed(EInstrumentPad.Guitar, pad) || this.bPressed(EInstrumentPad.Bass, pad);
return this.bPressed(EInstrumentPad.Bass, pad);
}
return true;
} }
public bool IsPressing(EInstrumentPad part, EPad pad) { public bool IsPressing(EInstrumentPad part, EPad pad) {
if (part != EInstrumentPad.Unknown) { if (part == EInstrumentPad.Unknown) {
return false;
}
CConfigIni.CKeyAssign.STKEYASSIGN[] stkeyassignArray = this.rConfigIni.KeyAssign[(int)part][(int)pad]; CConfigIni.CKeyAssign.STKEYASSIGN[] stkeyassignArray = this.rConfigIni.KeyAssign[(int)part][(int)pad];
for (int i = 0; i < stkeyassignArray.Length; i++) { for (int i = 0; i < stkeyassignArray.Length; i++) {
switch (stkeyassignArray[i].InputDevice) { switch (stkeyassignArray[i].InputDevice) {
@ -163,7 +170,7 @@ namespace OpenTaiko {
break; break;
} }
IInputDevice device = this.inputManager.Joystick(stkeyassignArray[i].ID); IInputDevice device = this.inputManager.Joystick(stkeyassignArray[i].ID);
if ((device == null) || !device.KeyPressing(stkeyassignArray[i].Code)) { if (device == null || !device.KeyPressing(stkeyassignArray[i].Code)) {
break; break;
} }
this.detectedDevice.Joypad = true; this.detectedDevice.Joypad = true;
@ -175,7 +182,7 @@ namespace OpenTaiko {
break; break;
} }
IInputDevice device = this.inputManager.Gamepad(stkeyassignArray[i].ID); IInputDevice device = this.inputManager.Gamepad(stkeyassignArray[i].ID);
if ((device == null) || !device.KeyPressing(stkeyassignArray[i].Code)) { if (device == null || !device.KeyPressing(stkeyassignArray[i].Code)) {
break; break;
} }
this.detectedDevice.Gamepad = true; this.detectedDevice.Gamepad = true;
@ -189,18 +196,12 @@ namespace OpenTaiko {
return true; return true;
} }
} }
}
return false; return false;
} }
public bool IsPressingGB(EPad pad) { public bool IsPressingGB(EPad pad) {
if (!this.IsPressing(EInstrumentPad.Guitar, pad)) { return this.IsPressing(EInstrumentPad.Guitar, pad) || this.IsPressing(EInstrumentPad.Bass, pad);
return this.IsPressing(EInstrumentPad.Bass, pad);
} }
return true;
}
// その他
#region [ private ] #region [ private ]
//----------------- //-----------------

View File

@ -13,27 +13,27 @@ namespace OpenTaiko {
} }
public void Print(int x, int y, EFontType font, string alphanumericString) { public void Print(int x, int y, EFontType font, string alphanumericString) {
if (!base.IsDeActivated && !string.IsNullOrEmpty(alphanumericString)) { if (base.IsDeActivated || string.IsNullOrEmpty(alphanumericString)) {
return;
}
int BOL = x; int BOL = x;
for (int i = 0; i < alphanumericString.Length; i++) { foreach (var ch in alphanumericString) {
char ch = alphanumericString[i];
if (ch == '\n') { if (ch == '\n') {
x = BOL; x = BOL;
y += this.fontHeight; y += this.fontHeight;
} else { } else {
int index = printableCharacters.IndexOf(ch); int index = printableCharacters.IndexOf(ch);
if (index < 0) { if (index >= 0) {
x += this.fontWidth;
} else {
if (this.fontTextures[(int)((int)font / (int)EFontType.WhiteSlim)] != null) { if (this.fontTextures[(int)((int)font / (int)EFontType.WhiteSlim)] != null) {
this.fontTextures[(int)((int)font / (int)EFontType.WhiteSlim)].t2D描画(x, y, this.characterRectangles[(int)((int)font % (int)EFontType.WhiteSlim), index]); this.fontTextures[(int)((int)font / (int)EFontType.WhiteSlim)].t2D描画(x, y, this.characterRectangles[(int)((int)font % (int)EFontType.WhiteSlim), index]);
} }
}
x += this.fontWidth; x += this.fontWidth;
} }
} }
} }
}
}
public override void DeActivate() { public override void DeActivate() {
if (this.characterRectangles != null) if (this.characterRectangles != null)
@ -43,7 +43,10 @@ namespace OpenTaiko {
} }
public override void CreateManagedResource() { public override void CreateManagedResource() {
if (!base.IsDeActivated) { if (base.IsDeActivated) {
return;
}
this.fontTextures[0] = OpenTaiko.Tx.TxC(@"Console_Font.png"); this.fontTextures[0] = OpenTaiko.Tx.TxC(@"Console_Font.png");
this.fontTextures[1] = OpenTaiko.Tx.TxC(@"Console_Font_Small.png"); this.fontTextures[1] = OpenTaiko.Tx.TxC(@"Console_Font_Small.png");
@ -63,19 +66,22 @@ namespace OpenTaiko {
base.CreateManagedResource(); base.CreateManagedResource();
} }
}
public override void ReleaseManagedResource() { public override void ReleaseManagedResource() {
if (!base.IsDeActivated) { if (base.IsDeActivated) {
return;
}
for (int i = 0; i < 2; i++) { for (int i = 0; i < 2; i++) {
if (this.fontTextures[i] != null) { if (this.fontTextures[i] == null) {
continue;
}
this.fontTextures[i].Dispose(); this.fontTextures[i].Dispose();
this.fontTextures[i] = null; this.fontTextures[i] = null;
} }
}
base.ReleaseManagedResource(); base.ReleaseManagedResource();
} }
}
#region [ private ] #region [ private ]
//----------------- //-----------------