Advertisement
plytalent

OverlayUI-PythonServer

Feb 20th, 2022
794
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.41 KB | None | 0 0
  1. import asyncio
  2. from ctypes import wintypes
  3. import json
  4. import websockets
  5. import tkinter as tk
  6. import ctypes
  7.  
  8. loop = asyncio.new_event_loop()
  9. asyncio.set_event_loop(loop)
  10.  
  11. tkapp = tk.Tk()
  12. players={}
  13. player_overlay_state={}
  14. async def hello(websocket, path):
  15.     try:
  16.         while True:
  17.             data = json.loads(await websocket.recv())
  18.             match data["Operation"]:
  19.                 case "New":
  20.                     if data["Position"]:
  21.                         OpenNewTextLabel(data["Name"],data["Position"]["x"],data["Position"]["y"])
  22.                         await websocket.send("Update Overlay")
  23.                 case "Reposition":
  24.                     if data["Position"] and players[data["Name"]]:
  25.                         players[data["Name"]].geometry(f"100x100+{data['Position']['x']}+{data['Position']['y']}")
  26.                 case "Resize":
  27.                     if data["Position"] and players[data["Name"]]:
  28.                         players[data["Name"]].geometry(f"{data['Size']['x']}x{data['Size']['y']}+{data['Position']['x']}+{data['Position']['y']}")
  29.                 case "Destroy":
  30.                     players[data["name"]].destroy()
  31.                     players[data["name"]] = None
  32.                     player_overlay_state[data["name"]] = None
  33.     finally:
  34.         pass
  35.  
  36. async def tk_main(root):
  37.     while True:
  38.         root.update()
  39.         await asyncio.sleep(0.05)
  40.  
  41. start_server = websockets.serve(hello, 'localhost', 1820)
  42. def GetWindowRectFromName(name:str)-> tuple:
  43.     hwnd = ctypes.windll.user32.FindWindowW(0, name)
  44.     rect = wintypes.RECT()
  45.     ctypes.windll.user32.GetWindowRect(hwnd, ctypes.pointer(rect))
  46.     return (rect.left, rect.top, rect.right, rect.bottom)
  47. def on_closing(win):
  48.     print(win)
  49. def OpenNewTextLabel(name,x,y):
  50.     # Toplevel object which will
  51.     # be treated as a new window
  52.     if not name in players:
  53.         players[name] = tk.Toplevel(tkapp)
  54.     players[name].overrideredirect(True)
  55.     players[name].lift()
  56.     players[name].wm_attributes("-topmost", True)
  57.     players[name].geometry(f"100x100+{x}+{y}")
  58.     players[name].attributes("-alpha", 0.1)
  59.     players[name].protocol("WM_DELETE_WINDOW", on_closing)
  60.  
  61. _, _, width, height = GetWindowRectFromName("Roblox")
  62.  
  63. loop.run_until_complete(start_server)
  64. tkmain = asyncio.ensure_future(tk_main(tkapp),loop=loop)
  65.  
  66. try:
  67.     loop.run_forever()
  68. except KeyboardInterrupt:
  69.     tkmain.cancel()
  70.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement