-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.cs
More file actions
169 lines (135 loc) · 4.8 KB
/
App.cs
File metadata and controls
169 lines (135 loc) · 4.8 KB
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.InteropServices;
using SFML.Graphics;
using SFML.System;
using SFML.Window;
namespace KeyOverlay
{
class App
{
private Config config = new Config();
private bool reinit = false;
private RenderWindow window;
private RenderTexture texture;
private Shader shader;
private RenderStates states;
private Vector2i grabbedOffset;
private bool grabbed = false;
private List<Key> keys = new List<Key>();
public App() { }
private void load()
{
config.LoadConfig();
window = new RenderWindow(new VideoMode(config.Window.Width, config.Window.Height), "KeyOverlay", config.Window.Transparent ? Styles.None : (Styles.Titlebar | Styles.Close));
window.SetFramerateLimit(config.Window.FPS);
window.Closed += OnClose;
window.KeyPressed += OnKeyPress;
window.MouseButtonPressed += OnMousePress;
window.MouseButtonReleased += OnMouseRelease;
window.MouseMoved += OnMouseMove;
keys.Clear();
foreach(KeysConfig key in config.Keys)
{
var _key = new Key(key.Name, key.Key, config);
keys.Add(_key);
}
if(config.Window.Transparent)
{
// https://gist.github.com/Alia5/5d8c48941d1f73c1ef14967a5ffe33d5
SetWindowLong(window.SystemHandle, -16, 0x80000000 | 0x10000000);
MARGINS margins = new MARGINS { cyTopHeight = -1 };
DwmExtendFrameIntoClientArea(window.SystemHandle, margins);
}
texture = new RenderTexture(config.Window.Width, config.Window.Height);
string shaderText = File.ReadAllText("./shader.frag");
shader = Shader.FromString(null, null, shaderText);
shader.SetUniform("resolution", new Vector2f(config.Window.Width, config.Window.Height));
shader.SetUniform("size", config.Fade.Size);
shader.SetUniform("side", config.Fade.Side);
states = new RenderStates(shader);
}
private void createKeys()
{
}
private void OnClose(object sender, EventArgs e)
{
window.Close();
}
private void OnKeyPress(object sender, KeyEventArgs e)
{
switch(e.Code)
{
case Keyboard.Key.Escape:
window.Close();
break;
case Keyboard.Key.R:
window.Close();
reinit = true;
break;
default:
//
break;
}
}
private void OnMousePress(object sender, MouseButtonEventArgs e)
{
if(e.Button == Mouse.Button.Left)
{
grabbedOffset = window.Position - Mouse.GetPosition();
grabbed = true;
}
}
private void OnMouseRelease(object sender, MouseButtonEventArgs e)
{
if(e.Button == Mouse.Button.Left)
grabbed = false;
}
private void OnMouseMove(object sender, MouseMoveEventArgs e)
{
if(grabbed)
window.Position = Mouse.GetPosition() + grabbedOffset;
}
public void Run()
{
reinit = false;
load();
while(window.IsOpen && !reinit)
{
texture.Clear(Color.Transparent);
Vector2f offset = new Vector2f(
(config.Window.Width - config.Key.Size * keys.Count - config.Key.Margin * (keys.Count - 1)) / 2,
20 + config.Key.Size
);
foreach(Key key in keys)
{
offset = key.Draw(texture, offset);
}
texture.Display();
var t = texture.Texture;
shader.SetUniform("texture", t);
var sprite = new Sprite(t);
window.Clear(Color.Transparent);
window.DispatchEvents();
window.Draw(sprite, states);
window.Display();
}
if(reinit)
Run();
}
#region Imports
struct MARGINS
{
public int cxLeftWidth;
public int cxRightWidth;
public int cyTopHeight;
public int cyBottomHeight;
}
[DllImport("user32.dll")]
static extern int SetWindowLong(IntPtr hwnd, int index, uint newlong);
[DllImport("Dwmapi.dll")]
static extern int DwmExtendFrameIntoClientArea(IntPtr hwnd, MARGINS margins);
#endregion
}
}