Add command input button for Virtual Keyboard Mod

This commit is contained in:
yangzhi 2020-02-05 14:15:22 +08:00
parent dff5cbcae0
commit 3805301a44
6 changed files with 110 additions and 66 deletions

View File

@ -5,7 +5,7 @@
<RootNamespace>StardewModdingAPI.Mods.SaveBackup</RootNamespace> <RootNamespace>StardewModdingAPI.Mods.SaveBackup</RootNamespace>
<TargetFramework>net45</TargetFramework> <TargetFramework>net45</TargetFramework>
<LangVersion>latest</LangVersion> <LangVersion>latest</LangVersion>
<OutputPath>C:\Users\Chris\source\repos\SMAPI\bin\Debug\Mods\SaveBackup\</OutputPath> <OutputPath></OutputPath>
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath> <AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
<PlatformTarget>x86</PlatformTarget> <PlatformTarget>x86</PlatformTarget>
</PropertyGroup> </PropertyGroup>

View File

@ -1,10 +1,12 @@
using System; using System;
using System.Collections.Concurrent;
using Microsoft.Xna.Framework; using Microsoft.Xna.Framework;
using StardewModdingAPI.Events; using StardewModdingAPI.Events;
using StardewValley; using StardewValley;
using StardewValley.Menus; using StardewValley.Menus;
using static StardewModdingAPI.Mods.VirtualKeyboard.ModConfig;
using System.Reflection; using System.Reflection;
using Microsoft.Xna.Framework.Input;
using static StardewModdingAPI.Mods.VirtualKeyboard.ModConfig;
namespace StardewModdingAPI.Mods.VirtualKeyboard namespace StardewModdingAPI.Mods.VirtualKeyboard
{ {
@ -50,8 +52,7 @@ namespace StardewModdingAPI.Mods.VirtualKeyboard
helper.Events.Input.ButtonReleased += this.EventInputButtonReleased; helper.Events.Input.ButtonReleased += this.EventInputButtonReleased;
helper.Events.Input.ButtonPressed += this.EventInputButtonPressed; helper.Events.Input.ButtonPressed += this.EventInputButtonPressed;
MainActivity activity = this.helper.Reflection.GetField<MainActivity>(typeof(MainActivity), "instance").GetValue(); object score = this.GetSCore(this.helper);
object score = activity.GetType().GetField("core", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(activity);
object eventManager = score.GetType().GetField("EventManager", BindingFlags.Public | BindingFlags.Instance).GetValue(score); object eventManager = score.GetType().GetField("EventManager", BindingFlags.Public | BindingFlags.Instance).GetValue(score);
this.buttonPressed = eventManager.GetType().GetField("ButtonPressed", BindingFlags.Public | BindingFlags.Instance).GetValue(eventManager); this.buttonPressed = eventManager.GetType().GetField("ButtonPressed", BindingFlags.Public | BindingFlags.Instance).GetValue(eventManager);
@ -61,9 +62,16 @@ namespace StardewModdingAPI.Mods.VirtualKeyboard
this.RaiseButtonReleased = this.buttonReleased.GetType().GetMethod("Raise", BindingFlags.Public | BindingFlags.Instance); this.RaiseButtonReleased = this.buttonReleased.GetType().GetMethod("Raise", BindingFlags.Public | BindingFlags.Instance);
} }
private object GetSCore(IModHelper helper)
{
MainActivity activity = this.helper.Reflection.GetField<MainActivity>(typeof(MainActivity), "instance").GetValue();
object score = activity.GetType().GetField("core", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(activity);
return score;
}
private bool shouldTrigger(Vector2 screenPixels) private bool shouldTrigger(Vector2 screenPixels)
{ {
if (this.buttonRectangle.Contains(screenPixels.X * Game1.options.zoomLevel, screenPixels.Y * Game1.options.zoomLevel)) if (this.buttonRectangle.Contains(screenPixels.X * Game1.options.zoomLevel, screenPixels.Y * Game1.options.zoomLevel) && !this.hidden)
{ {
if (!this.hidden) if (!this.hidden)
Toolbar.toolbarPressed = true; Toolbar.toolbarPressed = true;
@ -80,7 +88,7 @@ namespace StardewModdingAPI.Mods.VirtualKeyboard
} }
Vector2 screenPixels = e.Cursor.ScreenPixels; Vector2 screenPixels = e.Cursor.ScreenPixels;
if (this.shouldTrigger(screenPixels) && !this.hidden) if (this.shouldTrigger(screenPixels))
{ {
object inputState = e.GetType().GetField("InputState", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(e); object inputState = e.GetType().GetField("InputState", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(e);
@ -108,6 +116,31 @@ namespace StardewModdingAPI.Mods.VirtualKeyboard
Vector2 screenPixels = e.Cursor.ScreenPixels; Vector2 screenPixels = e.Cursor.ScreenPixels;
if (this.shouldTrigger(screenPixels)) if (this.shouldTrigger(screenPixels))
{ {
if (this.buttonKey == SButton.RightWindows)
{
Game1.activeClickableMenu = new NamingMenu(command =>
{
if (command.Length > 0)
{
object score = this.GetSCore(this.helper);
object sgame = score.GetType().GetField("GameInstance", BindingFlags.Public | BindingFlags.Instance)?.GetValue(score);
ConcurrentQueue<string> commandQueue = sgame.GetType().GetProperty("CommandQueue", BindingFlags.Public | BindingFlags.Instance)?.GetValue(sgame) as ConcurrentQueue<string>;
commandQueue?.Enqueue(command);
Game1.activeClickableMenu.exitThisMenu();
}
}, "Command", "")
{
randomButton = new ClickableTextureComponent(new Rectangle(-100, -100, 0, 0),
Game1.mobileSpriteSheet, new Rectangle(87, 22, 20, 20), 4f, false)
};
return;
}
if (this.buttonKey == SButton.RightControl)
{
SGameConsole.Instance.Show();
return;
}
object inputState = e.GetType().GetField("InputState", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(e); object inputState = e.GetType().GetField("InputState", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(e);
object buttonReleasedEventArgs = Activator.CreateInstance(typeof(ButtonReleasedEventArgs), BindingFlags.NonPublic | BindingFlags.Instance, null, new object[] { this.buttonKey, e.Cursor, inputState }, null); object buttonReleasedEventArgs = Activator.CreateInstance(typeof(ButtonReleasedEventArgs), BindingFlags.NonPublic | BindingFlags.Instance, null, new object[] { this.buttonKey, e.Cursor, inputState }, null);
try try
@ -127,9 +160,14 @@ namespace StardewModdingAPI.Mods.VirtualKeyboard
/// <param name="e">The event arguments.</param> /// <param name="e">The event arguments.</param>
private void OnRenderingHud(object sender, EventArgs e) private void OnRenderingHud(object sender, EventArgs e)
{ {
if (!Game1.eventUp && !this.hidden && Game1.activeClickableMenu is GameMenu == false && Game1.activeClickableMenu is ShopMenu == false && Game1.activeClickableMenu is IClickableMenu == false) if (!this.hidden)
{ {
IClickableMenu.drawButtonWithText(Game1.spriteBatch, Game1.smallFont, this.alias, this.buttonRectangle.X, this.buttonRectangle.Y, this.buttonRectangle.Width, this.buttonRectangle.Height, Color.BurlyWood * this.transparency); float scale = this.transparency;
if (!Game1.eventUp && Game1.activeClickableMenu is GameMenu == false && Game1.activeClickableMenu is ShopMenu == false && Game1.activeClickableMenu is IClickableMenu == false)
{
scale *= 0.5f;
}
IClickableMenu.drawButtonWithText(Game1.spriteBatch, Game1.smallFont, this.alias, this.buttonRectangle.X, this.buttonRectangle.Y, this.buttonRectangle.Width, this.buttonRectangle.Height, Color.BurlyWood * scale);
} }
} }
} }

View File

@ -1,27 +1,20 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Xna.Framework;
using StardewValley;
using StardewValley.Menus;
namespace StardewModdingAPI.Mods.VirtualKeyboard namespace StardewModdingAPI.Mods.VirtualKeyboard
{ {
class ModConfig class ModConfig
{ {
public Toggle vToggle = new Toggle(new Rectangle(36, 12, 64, 64)); public Toggle vToggle = new Toggle(new Rect(36, 12, 64, 64));
public VirtualButton[] buttons { get; set;} = new VirtualButton[] {
public VirtualButton[] buttons { get; set; } = new VirtualButton[] { new VirtualButton(SButton.Q, new Rect(192, 80, 90, 90), 0.5f),
new VirtualButton(SButton.Q, new Rect(192, 150, 90, 90, 6), 0.5f), new VirtualButton(SButton.I, new Rect(288, 80, 90, 90), 0.5f),
new VirtualButton(SButton.I, new Rect(288, 150, 90, 90, 6), 0.5f), new VirtualButton(SButton.O, new Rect(384, 80, 90, 90), 0.5f),
new VirtualButton(SButton.O, new Rect(384, 150, 90, 90, 6), 0.5f), new VirtualButton(SButton.P, new Rect(480, 80, 90, 90), 0.5f)
new VirtualButton(SButton.P, new Rect(480, 150, 90, 90, 6), 0.5f),
new VirtualButton(SButton.MouseRight, new Rect(580, 150, 150, 90, 6), 0.5f, "RightMouse")
}; };
internal class VirtualButton public VirtualButton[] buttonsExtend { get; set; } = new VirtualButton[] {
{ new VirtualButton(SButton.MouseRight, new Rect(192, 170, 162, 90), 0.5f, "RightMouse"),
new VirtualButton(SButton.RightWindows, new Rect(362, 170, 162, 90), 0.5f, "Command"),
new VirtualButton(SButton.RightControl, new Rect(532, 170, 162, 90), 0.5f, "Console")
};
internal class VirtualButton {
public SButton key; public SButton key;
public Rect rectangle; public Rect rectangle;
public float transparency; public float transparency;
@ -34,13 +27,12 @@ namespace StardewModdingAPI.Mods.VirtualKeyboard
this.alias = alias; this.alias = alias;
} }
} }
internal class Toggle internal class Toggle
{ {
public Rectangle rectangle; public Rect rectangle;
//public float scale; //public float scale;
public Toggle(Rectangle rectangle) public Toggle(Rect rectangle)
{ {
this.rectangle = rectangle; this.rectangle = rectangle;
//this.scale = scale; //this.scale = scale;
@ -52,15 +44,13 @@ namespace StardewModdingAPI.Mods.VirtualKeyboard
public int Y; public int Y;
public int Width; public int Width;
public int Height; public int Height;
public int Padding;
public Rect(int x, int y, int width, int height, int padding) public Rect(int x, int y, int width, int height)
{ {
this.X = x; this.X = x;
this.Y = y; this.Y = y;
this.Width = width; this.Width = width;
this.Height = height; this.Height = height;
this.Padding = padding;
} }
} }
} }

View File

@ -1,12 +1,13 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using StardewModdingAPI; using StardewModdingAPI;
using StardewValley;
using StardewValley.Tools;
namespace StardewModdingAPI.Mods.VirtualKeyboard namespace StardewModdingAPI.Mods.VirtualKeyboard
{ {
public class ModEntry : Mod class ModEntry : Mod
{ {
public override void Entry(IModHelper helper) public override void Entry(IModHelper helper)
{ {

View File

@ -13,11 +13,12 @@ namespace StardewModdingAPI.Mods.VirtualKeyboard
private readonly IModHelper helper; private readonly IModHelper helper;
private readonly IMonitor Monitor; private readonly IMonitor Monitor;
private bool enabled = false; private int enabledStage = 0;
private bool isDefault = true; private bool isDefault = true;
private ClickableTextureComponent virtualToggleButton; private ClickableTextureComponent virtualToggleButton;
private List<KeyButton> keyboard = new List<KeyButton>(); private List<KeyButton> keyboard = new List<KeyButton>();
private List<KeyButton> keyboardExtend = new List<KeyButton>();
private ModConfig modConfig; private ModConfig modConfig;
private Texture2D texture; private Texture2D texture;
@ -30,6 +31,8 @@ namespace StardewModdingAPI.Mods.VirtualKeyboard
this.modConfig = helper.ReadConfig<ModConfig>(); this.modConfig = helper.ReadConfig<ModConfig>();
for (int i = 0; i < this.modConfig.buttons.Length; i++) for (int i = 0; i < this.modConfig.buttons.Length; i++)
this.keyboard.Add(new KeyButton(helper, this.modConfig.buttons[i], this.Monitor)); this.keyboard.Add(new KeyButton(helper, this.modConfig.buttons[i], this.Monitor));
for (int i = 0; i < this.modConfig.buttonsExtend.Length; i++)
this.keyboardExtend.Add(new KeyButton(helper, this.modConfig.buttonsExtend[i], this.Monitor));
if (this.modConfig.vToggle.rectangle.X != 36 || this.modConfig.vToggle.rectangle.Y != 12) if (this.modConfig.vToggle.rectangle.X != 36 || this.modConfig.vToggle.rectangle.Y != 12)
this.isDefault = false; this.isDefault = false;
@ -44,30 +47,48 @@ namespace StardewModdingAPI.Mods.VirtualKeyboard
private void VirtualToggleButtonPressed(object sender, ButtonPressedEventArgs e) private void VirtualToggleButtonPressed(object sender, ButtonPressedEventArgs e)
{ {
Vector2 screenPixels = e.Cursor.ScreenPixels; Vector2 screenPixels = e.Cursor.ScreenPixels;
if (!this.enabled && this.shouldTrigger(screenPixels)) if (this.shouldTrigger(screenPixels))
{ {
this.hiddenKeys(true, false); switch (this.enabledStage)
}
else if (this.enabled && this.shouldTrigger(screenPixels))
{
this.hiddenKeys(false, true);
if (Game1.activeClickableMenu is IClickableMenu menu)
{ {
menu.exitThisMenu(); case 0:
Toolbar.toolbarPressed = true; foreach (var keys in this.keyboard)
{
keys.hidden = false;
}
foreach (var keys in this.keyboardExtend)
{
keys.hidden = true;
}
this.enabledStage = 1;
break;
case 1 when this.keyboardExtend.Count > 0:
foreach (var keys in this.keyboardExtend)
{
keys.hidden = false;
}
this.enabledStage = 2;
break;
default:
foreach (var keys in this.keyboard)
{
keys.hidden = true;
}
foreach (var keys in this.keyboardExtend)
{
keys.hidden = true;
}
this.enabledStage = 0;
if (Game1.activeClickableMenu is IClickableMenu menu)
{
menu.exitThisMenu();
Toolbar.toolbarPressed = true;
}
break;
} }
} }
} }
private void hiddenKeys(bool enabled, bool hidden)
{
this.enabled = enabled;
foreach (var keys in this.keyboard)
{
keys.hidden = hidden;
}
}
private bool shouldTrigger(Vector2 screenPixels) private bool shouldTrigger(Vector2 screenPixels)
{ {
if (this.virtualToggleButton.containsPoint((int)(screenPixels.X * Game1.options.zoomLevel), (int)(screenPixels.Y * Game1.options.zoomLevel))) if (this.virtualToggleButton.containsPoint((int)(screenPixels.X * Game1.options.zoomLevel), (int)(screenPixels.Y * Game1.options.zoomLevel)))
@ -104,12 +125,13 @@ namespace StardewModdingAPI.Mods.VirtualKeyboard
} }
float scale = 1f; float scale = 1f;
if (!this.enabled) if (this.enabledStage == 0)
{ {
scale = 0.5f; scale = 0.5f;
} }
if(!Game1.eventUp && Game1.activeClickableMenu is GameMenu == false && Game1.activeClickableMenu is ShopMenu == false) if (!Game1.eventUp && Game1.activeClickableMenu is GameMenu == false && Game1.activeClickableMenu is ShopMenu == false)
this.virtualToggleButton.draw(Game1.spriteBatch, Color.White * scale, 0.000001f); scale = 0.25f;
this.virtualToggleButton.draw(Game1.spriteBatch, Color.White * scale, 0.000001f);
} }
} }
} }

View File

@ -16,7 +16,7 @@
<AndroidResgenFile>Resources\Resource.designer.cs</AndroidResgenFile> <AndroidResgenFile>Resources\Resource.designer.cs</AndroidResgenFile>
<GenerateSerializationAssemblies>Off</GenerateSerializationAssemblies> <GenerateSerializationAssemblies>Off</GenerateSerializationAssemblies>
<AndroidUseLatestPlatformSdk>false</AndroidUseLatestPlatformSdk> <AndroidUseLatestPlatformSdk>false</AndroidUseLatestPlatformSdk>
<TargetFrameworkVersion>v10.0</TargetFrameworkVersion> <TargetFrameworkVersion>v9.0</TargetFrameworkVersion>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols> <DebugSymbols>true</DebugSymbols>
@ -83,9 +83,6 @@
<Reference Include="xTile"> <Reference Include="xTile">
<HintPath>..\..\..\..\..\AndroidStudioProjects\SMAPI Android Installer\app\src\main\assets\Stardew\xTile.dll</HintPath> <HintPath>..\..\..\..\..\AndroidStudioProjects\SMAPI Android Installer\app\src\main\assets\Stardew\xTile.dll</HintPath>
</Reference> </Reference>
<Reference Include="TMXTile">
<HintPath>..\..\..\..\..\AndroidStudioProjects\SMAPI Android Installer\app\src\main\assets\Stardew\TMXTile.dll</HintPath>
</Reference>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="Constants.cs" /> <Compile Include="Constants.cs" />
@ -239,10 +236,6 @@
<Compile Include="Framework\Networking\MultiplayerPeerMod.cs" /> <Compile Include="Framework\Networking\MultiplayerPeerMod.cs" />
<Compile Include="Framework\Networking\RemoteContextModel.cs" /> <Compile Include="Framework\Networking\RemoteContextModel.cs" />
<Compile Include="Framework\Networking\RemoteContextModModel.cs" /> <Compile Include="Framework\Networking\RemoteContextModModel.cs" />
<Compile Include="Framework\Networking\SGalaxyNetClient.cs" />
<Compile Include="Framework\Networking\SGalaxyNetServer.cs" />
<Compile Include="Framework\Networking\SLidgrenClient.cs" />
<Compile Include="Framework\Networking\SLidgrenServer.cs" />
<Compile Include="Framework\Patching\GamePatcher.cs" /> <Compile Include="Framework\Patching\GamePatcher.cs" />
<Compile Include="Framework\Patching\IHarmonyPatch.cs" /> <Compile Include="Framework\Patching\IHarmonyPatch.cs" />
<Compile Include="Framework\PerformanceMonitoring\AlertContext.cs" /> <Compile Include="Framework\PerformanceMonitoring\AlertContext.cs" />