Skip to content

Commit aa21811

Browse files
committed
Add transparent undecorated window example
1 parent 695cc2f commit aa21811

File tree

1 file changed

+199
-0
lines changed

1 file changed

+199
-0
lines changed
Lines changed: 199 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,199 @@
1+
"""
2+
Requirements:
3+
- raylib
4+
- pytweening
5+
- glm
6+
7+
Windows-Only Requirements:
8+
- win32gui
9+
- win32con
10+
- pywintypes
11+
"""
12+
13+
import sys, time
14+
import glm
15+
import pytweening as tween
16+
import screeninfo
17+
from raylib.dynamic import raylib as rl, ffi
18+
from raylib.colors import *
19+
20+
21+
Vec2 = lambda p: glm.vec2(p.x, p.y)
22+
CTM = lambda: round(time.perf_counter() * 1000)
23+
24+
monitors = screeninfo.get_monitors()
25+
drag = False
26+
offset = Vec2(rl.GetMousePosition())
27+
width = height = 160
28+
window_vel = glm.vec2()
29+
window_pos = glm.vec2(monitors[0].width - width, monitors[0].height - height)
30+
31+
rl.SetConfigFlags(
32+
rl.FLAG_WINDOW_TRANSPARENT
33+
| rl.FLAG_WINDOW_UNDECORATED
34+
| rl.FLAG_VSYNC_HINT
35+
| rl.FLAG_MSAA_4X_HINT
36+
)
37+
rl.InitWindow(width, height, b'')
38+
rl.SetWindowPosition(int(window_pos.x), int(window_pos.y))
39+
#rl.SetTargetFPS(500)
40+
target = rl.LoadRenderTexture(width, height)
41+
42+
# Top-Level Window Support Only On Windows
43+
if sys.platform == 'win32':
44+
import win32gui, win32con, pywintypes
45+
46+
# Set window to always top without moving it
47+
win32gui.SetWindowPos(
48+
pywintypes.HANDLE(ffi.cast('int', rl.GetWindowHandle())),
49+
win32con.HWND_TOPMOST,
50+
0, 0, 0, 0,
51+
win32con.SWP_NOSIZE | win32con.SWP_NOMOVE
52+
)
53+
54+
55+
#tint = glm.vec4(255, 255, 255, 255) When mouse over, brighten everything fade
56+
trans = 155
57+
elapsed = CTM()
58+
# Generate an 'L' pattern with pixels
59+
awd = rl.LoadTextureFromImage(rl.LoadImageEx(
60+
[
61+
WHITE, WHITE,
62+
WHITE, [0] * 4
63+
],
64+
2, 2
65+
))
66+
# start1 = [0, 0] ; end1 = [16, 16]
67+
# start2 = [0, 160] ; end2 = [16, 144]
68+
# start3 = [160, 160]; end3 = [144, 144]
69+
# start4 = [160, 0] ; end4 = [144, 16]
70+
71+
start1 = [0, 0] ; end1 = [8, 8]
72+
start2 = [0, 160] ; end2 = [8, 152]
73+
start3 = [160, 160]; end3 = [152, 152]
74+
start4 = [160, 0] ; end4 = [152, 8]
75+
76+
while not rl.WindowShouldClose():
77+
frame_start_time = CTM()
78+
79+
elapsed += 2 * rl.GetFrameTime()
80+
if elapsed > 1.0:
81+
elapsed = 0
82+
start1, end1 = end1, start1
83+
start2, end2 = end2, start2
84+
start3, end3 = end3, start3
85+
start4, end4 = end4, start4
86+
87+
mouse_pos = Vec2(rl.GetMousePosition())
88+
89+
if rl.IsKeyReleased(rl.KEY_ENTER):
90+
window_pos = glm.vec2()
91+
window_vel = glm.vec2()
92+
93+
if rl.CheckCollisionPointRec(list(mouse_pos), [0, 0, width, height]):
94+
if rl.IsMouseButtonPressed(rl.MOUSE_LEFT_BUTTON):
95+
drag = True
96+
offset = mouse_pos
97+
98+
trans = 255
99+
else:
100+
trans = 255
101+
102+
#mouse_pos += window_vel
103+
window_vel *= glm.vec2(0.9, 0.9)
104+
105+
if glm.length(window_vel):
106+
window_pos += window_vel
107+
#rl.SetWindowPosition(int(window_pos.x), int(window_pos.y))
108+
109+
110+
# Find which monitor the square is *currently within*
111+
wposx = window_pos.x + width // 2
112+
wposy = window_pos.y + height // 2
113+
found = False
114+
for monitor in reversed(monitors):
115+
if wposx > monitor.x and wposx < monitor.x + monitor.width:
116+
if wposy > monitor.y and wposy < monitor.y + monitor.height:
117+
found = True
118+
break
119+
120+
# If it is out of bounds, find the closest one
121+
if not found:
122+
sorted_monitors = list(
123+
sorted(
124+
monitors,
125+
key=lambda m: (m.x + m.width // 2) - window_pos.x + (m.y + m.height // 2) - window_pos.y
126+
)
127+
)
128+
monitor = sorted_monitors[0]
129+
130+
# Constrain window to bounds
131+
window_pos.x = max(monitor.x, window_pos.x)
132+
window_pos.x = min(monitor.x + monitor.width - width, window_pos.x)
133+
window_pos.y = max(monitor.y, window_pos.y)
134+
window_pos.y = min(monitor.y + monitor.height - height, window_pos.y)
135+
# Set here to "snap" and don't to enable "floating" back into position
136+
if not drag:
137+
rl.SetWindowPosition(int(window_pos.x), int(window_pos.y))
138+
139+
140+
if drag:
141+
window_pos += mouse_pos - offset
142+
drag = not rl.IsMouseButtonReleased(rl.MOUSE_LEFT_BUTTON)
143+
rl.SetWindowPosition(int(window_pos.x), int(window_pos.y))
144+
145+
if not drag:
146+
window_vel = (mouse_pos - offset) * glm.vec2(1, 0.6) * 0.8
147+
if any(glm.isnan(window_vel)):
148+
window_vel = glm.vec2()
149+
150+
rl.BeginDrawing()
151+
rl.ClearBackground([0] * 4)
152+
153+
rl.BeginTextureMode(target)
154+
rl.ClearBackground([0] * 4)
155+
rl.DrawRectangle(0, 0, width, height, [0] * 4)
156+
157+
rl.DrawTextureEx(
158+
awd, tween.getPointOnLine(*start1, *end1, tween.easeInOutQuad(elapsed)), 0, 32,
159+
[255, 200, 0, trans] # Yellow
160+
)
161+
162+
rl.DrawTextureEx(
163+
awd, tween.getPointOnLine(*start2, *end2, tween.easeInOutQuad(elapsed)), -90, 32,
164+
[255, 0, 55, trans] # Red
165+
)
166+
167+
rl.DrawTextureEx(
168+
awd, tween.getPointOnLine(*start3, *end3, tween.easeInOutQuad(elapsed)), -180, 32,
169+
[100, 200, 55, trans] # Green
170+
)
171+
172+
rl.DrawTextureEx(
173+
awd, tween.getPointOnLine(*start4, *end4, tween.easeInOutQuad(elapsed)), 90, 32,
174+
[55, 155, 255, trans] # Blue
175+
)
176+
177+
rl.EndTextureMode()
178+
179+
rl.DrawTexturePro(
180+
target.texture,
181+
[0, 0, width, -height],
182+
[0, 0, width, height],
183+
[0, 0],
184+
0,
185+
WHITE
186+
)
187+
188+
for i in range(1000):
189+
rl.DrawTextureEx(awd, [-32, -32], 0, 32, WHITE)
190+
191+
#rl.DrawFPS(0, 0)
192+
193+
rl.EndDrawing()
194+
195+
# Sleep any leftover millis
196+
time_taken = CTM() - frame_start_time
197+
if time_taken < 1000 / 60:
198+
time.sleep((1000 / 60 - time_taken) / 1000)
199+

0 commit comments

Comments
 (0)