-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKeyButton.cs
151 lines (139 loc) · 4.72 KB
/
KeyButton.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using StardewValley;
using StardewValley.Menus;
using System.Reflection;
namespace VirtualKeyboard
{
public class EaseInOutFloat
{
float target;
float start;
float duration;
float currentTime;
public float Value { get; private set; }
public EaseInOutFloat(float start, float target, float duration)
{
this.start = start;
this.Value = start;
this.target = target;
this.duration = duration;
this.currentTime = 0f;
}
public void SetTarget(float target)
{
start = Value;
this.target = target;
currentTime = 0;
}
public void Update(float deltaTime)
{
float normalizedTime = currentTime / duration;
float easedTime = EaseInOutQuad(normalizedTime);
Value = start + (target - start) * easedTime;
currentTime += deltaTime;
if (currentTime > duration)
currentTime = duration;
}
float EaseInOutQuad(float t)
{
return t < 0.5 ? 2 * t * t : -1 + (4 - 2 * t) * t;
}
internal void SetTarget(object value)
{
throw new NotImplementedException();
}
}
internal class KeyButton : OptionsButton
{
public FieldInfo isHeldDownField;
public bool isHeldDown => (bool)isHeldDownField.GetValue(this);
public Color color = Color.White;
public float opacity
{
get => opacityInOut != null ? opacityInOut.Value : _opacity;
set
{
if (opacityInOut != null)
opacityInOut.SetTarget(value);
else
this._opacity = value;
}
}
float _opacity = 1;
public EaseInOutFloat opacityInOut;
public string key;
public Action<KeyButton> onKeyDown;
public Action<KeyButton> onKeyUp;
public int buttonHorizonLayout = 0;//vertical layout
private Texture2D icon;
string iconPath;
public KeyButton(string key, string label, Action<KeyButton> onKeyDown) : base(label, null, -1, -1)
{
this.key = key;
this.onKeyDown = onKeyDown;
isHeldDownField = typeof(OptionsButton).GetField("isHeldDown", BindingFlags.Instance | BindingFlags.NonPublic);
}
public void SetIcon(string iconPath)
{
this.iconPath = iconPath;
this.icon = ModEntry.Instance.Helper.ModContent.Load<Texture2D>(iconPath);
}
public override void receiveLeftClick(int x, int y)
{
base.receiveLeftClick(x, y);
if (enabled && bounds.Contains(x, y))
{
onKeyDown?.Invoke(this);
}
}
public override void releaseLeftClick(int x, int y)
{
base.releaseLeftClick(x, y);
if (enabled & bounds.Contains(x, y))
{
onKeyUp?.Invoke(this);
}
}
public void Draw(SpriteBatch batch, TimeSpan deltaTime)
{
if (opacityInOut != null)
{
var delta = (float)deltaTime.TotalMilliseconds / 1000f;
opacityInOut.Update(delta);
}
if (string.IsNullOrEmpty(iconPath) == false)
{
var iconRect = bounds;
iconRect.Y += isHeldDown ? 16 : 0;
var color = Color.White * opacity;
batch.Draw(icon, iconRect, color);
}
else
{
//get texture with tile pos x,z
var drawShadow = !isHeldDown;
var scale = 4; //adjust for default
var srcTexture = Game1.mouseCursors;
var srcRect = new Rectangle(256, 256, 10, 10);
drawTextureBoxWithIconAndText(batch, Game1.dialogueFont,
srcTexture, srcRect,
null, srcRect,
label,
bounds.X - (isHeldDown ? 4 : 0),
bounds.Y + (isHeldDown ? 4 : 0),
button.bounds.Width,
button.bounds.Height,
Color.White * opacity,
scale, drawShadow,
iconLeft: true, isClickable: true,
heldDown: false, drawIcon: false,
reverseColors: false, bold: true);
}
}
public void SetSize(int size)
{
bounds.Width = bounds.Height = size;
}
}
}