-
Notifications
You must be signed in to change notification settings - Fork 1
/
EmojiSupport.cs
150 lines (136 loc) · 4.18 KB
/
EmojiSupport.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
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using System.Collections.Generic;
using Terraria;
using Terraria.DataStructures;
using Terraria.ID;
using Terraria.ModLoader;
using Terraria.UI;
using Terraria.UI.Chat;
namespace EmojiSupport
{
// TODO: Emoji from Terraria Community Forums?
// TODO: Custom Emoji from file. Syncing, etc.
public class EmojiSupport : Mod
{
internal static EmojiSupport instance;
internal static UserInterface emojiSupportUserInterface;
internal EmojiSupportUI emojiSupportUI;
public EmojiSupport()
{
Properties = new ModProperties()
{
Autoload = true,
};
}
public override void Load()
{
instance = this;
ChatManager.Register<EmojiTagHandler>(new string[]
{
"e",
"emoji"
});
if (!Main.dedServ)
{
emojiSupportUI = new EmojiSupportUI();
emojiSupportUI.Activate();
emojiSupportUserInterface = new UserInterface();
emojiSupportUserInterface.SetState(emojiSupportUI);
PremultiplyTexture(GetTexture("sheet_google_20"));
}
}
public override void Unload()
{
instance = null;
emojiSupportUserInterface = null;
}
int lastSeenScreenWidth;
int lastSeenScreenHeight;
public override void ModifyInterfaceLayers(List<GameInterfaceLayer> layers)
{
int inventoryLayerIndex = layers.FindIndex(layer => layer.Name.Equals("Vanilla: Mouse Text"));
if (inventoryLayerIndex != -1)
{
layers.Insert(inventoryLayerIndex, new LegacyGameInterfaceLayer(
"EmojiSupport: Keyboard",
delegate
{
if (Main.drawingPlayerChat)
{
if (lastSeenScreenWidth != Main.screenWidth || lastSeenScreenHeight != Main.screenHeight)
{
emojiSupportUserInterface.Recalculate();
lastSeenScreenWidth = Main.screenWidth;
lastSeenScreenHeight = Main.screenHeight;
}
DrawUpdateKeyboardToggle();
if (EmojiSupportUI.visible)
{
emojiSupportUserInterface.Update(Main._drawInterfaceGameTime);
emojiSupportUI.Draw(Main.spriteBatch);
}
}
else
{
if (emojiSupportUI.filterTextBox.focused)
{
emojiSupportUI.filterTextBox.Unfocus();
}
}
return true;
},
InterfaceScaleType.UI)
);
}
}
public override void PostUpdateInput()
{
if (Main.drawingPlayerChat && EmojiSupportUI.visible && emojiSupportUI.filterTextBox.focused)
{
// this is only capable of capturing non-special keys and copy pastes.
NewUITextBox box = emojiSupportUI.filterTextBox;
box.WriteAll(Main.GetInputText(box.Text));
// it is redundant, but ok I guess, since the NewUITextBox also calls GetInputText
}
}
internal void DrawUpdateKeyboardToggle()
{
Point mousePoint = new Point(Main.mouseX, Main.mouseY);
// calcualte?
int xPosition = Main.screenWidth - 220; //62; //78;
int yPosition = Main.screenHeight - 36 + 10;
Texture2D toggleEmojiKeyboardTexture = EmojiSupportUI.visible ? Main.inventoryTickOnTexture : Main.inventoryTickOffTexture;
Rectangle toggleEmojiKeyboardButtonRectangle = new Rectangle(xPosition, yPosition, toggleEmojiKeyboardTexture.Width, toggleEmojiKeyboardTexture.Height);
bool toggleEmojiKeyboardButtonHover = false;
if (toggleEmojiKeyboardButtonRectangle.Contains(mousePoint))
{
Main.LocalPlayer.mouseInterface = true;
toggleEmojiKeyboardButtonHover = true;
if (Main.mouseLeft && Main.mouseLeftRelease)
{
EmojiSupportUI.visible = !EmojiSupportUI.visible;
Main.PlaySound(EmojiSupportUI.visible ? SoundID.MenuOpen : SoundID.MenuClose);
}
}
Main.spriteBatch.Draw(toggleEmojiKeyboardTexture, toggleEmojiKeyboardButtonRectangle.TopLeft(), Color.White /** 0.7f*/);
if (toggleEmojiKeyboardButtonHover)
{
Main.HoverItem = new Item();
Main.hoverItemName = "Click to toggle Emoji keyboard";
}
}
public static void PremultiplyTexture(Texture2D texture)
{
Color[] buffer = new Color[texture.Width * texture.Height];
texture.GetData(buffer);
for (int i = 0; i < buffer.Length; i++)
{
buffer[i] = Color.FromNonPremultiplied(
buffer[i].R, buffer[i].G, buffer[i].B, buffer[i].A);
}
texture.SetData(buffer);
}
}
}