smoothretro82

HUD template

Nov 23rd, 2025
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.59 KB | None | 0 0
  1. /********************************************************************
  2. * ADVANCED HUD TEMPLATE
  3. * - Dragging
  4. * - Resize handles (SE, E, S)
  5. * - Tabs
  6. * - Snap-to-grid
  7. * - Snap-to-screen-edge
  8. * - Close + Minimize
  9. ********************************************************************/
  10.  
  11. class HUD {
  12. constructor({
  13. title = "HUD",
  14. width = 260,
  15. height = 180,
  16. startX = 100,
  17. startY = 100,
  18. snapGrid = 20,
  19. snapEdge = 15,
  20. tabs = {}
  21. } = {}) {
  22.  
  23. this.snapGrid = snapGrid;
  24. this.snapEdge = snapEdge;
  25. this.tabs = tabs;
  26.  
  27. // HUD Container
  28. this.hud = document.createElement("div");
  29. Object.assign(this.hud.style, {
  30. position: "absolute",
  31. left: startX + "px",
  32. top: startY + "px",
  33. width: width + "px",
  34. height: height + "px",
  35. background: "lightgray",
  36. border: "1px solid black",
  37. borderRadius: "8px",
  38. zIndex: 9999,
  39. display: "flex",
  40. flexDirection: "column",
  41. overflow: "hidden",
  42. fontFamily: "Roboto Mono, monospace"
  43. });
  44.  
  45. /**************** HEADER ****************/
  46. this.header = document.createElement("div");
  47. Object.assign(this.header.style, {
  48. height: "28px",
  49. background: "gray",
  50. color: "white",
  51. display: "flex",
  52. alignItems: "center",
  53. justifyContent: "space-between",
  54. padding: "0 6px",
  55. cursor: "grab",
  56. userSelect: "none"
  57. });
  58. this.header.textContent = title;
  59.  
  60. // Header buttons
  61. this.btnArea = document.createElement("div");
  62. this.minBtn = this.#makeHeaderButton("-");
  63. this.closeBtn = this.#makeHeaderButton("X");
  64. this.btnArea.appendChild(this.minBtn);
  65. this.btnArea.appendChild(this.closeBtn);
  66. this.header.appendChild(this.btnArea);
  67. this.hud.appendChild(this.header);
  68.  
  69. /**************** TABS ****************/
  70. this.tabBar = document.createElement("div");
  71. Object.assign(this.tabBar.style, {
  72. display: "flex",
  73. background: "#d0d0d0",
  74. height: "24px",
  75. borderBottom: "1px solid #aaa"
  76. });
  77. this.hud.appendChild(this.tabBar);
  78.  
  79. this.tabContents = {};
  80.  
  81. Object.keys(tabs).forEach((name, index) => {
  82. const tabBtn = document.createElement("div");
  83. Object.assign(tabBtn.style, {
  84. flex: "1",
  85. textAlign: "center",
  86. paddingTop: "4px",
  87. fontSize: "12px",
  88. cursor: "pointer",
  89. background: index === 0 ? "#fff" : "#c7c7c7"
  90. });
  91. tabBtn.textContent = name;
  92. tabBtn.onclick = () => this.#switchTab(name);
  93. this.tabBar.appendChild(tabBtn);
  94.  
  95. const tabContent = document.createElement("div");
  96. tabContent.innerHTML = tabs[name];
  97. Object.assign(tabContent.style, {
  98. flex: "1",
  99. overflow: "auto",
  100. padding: "10px",
  101. display: index === 0 ? "block" : "none"
  102. });
  103.  
  104. this.tabContents[name] = tabContent;
  105. this.hud.appendChild(tabContent);
  106. });
  107.  
  108. /**************** RESIZE HANDLES ****************/
  109. this.#addResizeHandle("se", "8px", "8px", "se-resize");
  110. this.#addResizeHandle("e", "0", "8px", "e-resize", true);
  111. this.#addResizeHandle("s", "8px", "0", "s-resize", false, true);
  112.  
  113. // Append HUD to page
  114. document.body.appendChild(this.hud);
  115.  
  116. // Setup interactions
  117. this.#enableDragging();
  118. this.#setupClose();
  119. this.#setupMinimize();
  120. }
  121.  
  122.  
  123. /**************** UTILITIES ****************/
  124. #makeHeaderButton(label) {
  125. const b = document.createElement("span");
  126. Object.assign(b.style, {
  127. cursor: "pointer",
  128. fontWeight: "bold",
  129. fontSize: "16px",
  130. padding: "0 6px"
  131. });
  132. b.textContent = label;
  133. return b;
  134. }
  135.  
  136.  
  137. /**************** TABS ****************/
  138. #switchTab(name) {
  139. const allNames = Object.keys(this.tabContents);
  140.  
  141. allNames.forEach((tab, i) => {
  142. // show/hide content
  143. this.tabContents[tab].style.display = (tab === name) ? "block" : "none";
  144.  
  145. // highlight tab
  146. this.tabBar.children[i].style.background =
  147. (tab === name) ? "#fff" : "#c7c7c7";
  148. });
  149. }
  150.  
  151.  
  152. /**************** DRAGGING ****************/
  153. #enableDragging() {
  154. let isDragging = false;
  155. let offsetX = 0, offsetY = 0;
  156.  
  157. this.header.addEventListener("mousedown", (e) => {
  158. if (e.target === this.minBtn || e.target === this.closeBtn) return;
  159. isDragging = true;
  160. offsetX = e.clientX - this.hud.offsetLeft;
  161. offsetY = e.clientY - this.hud.offsetTop;
  162. this.header.style.cursor = "grabbing";
  163. });
  164.  
  165. document.addEventListener("mousemove", (e) => {
  166. if (!isDragging) return;
  167.  
  168. let x = e.clientX - offsetX;
  169. let y = e.clientY - offsetY;
  170.  
  171. // Snap to grid
  172. x = Math.round(x / this.snapGrid) * this.snapGrid;
  173. y = Math.round(y / this.snapGrid) * this.snapGrid;
  174.  
  175. // Snap to edges
  176. if (Math.abs(x) < this.snapEdge) x = 0;
  177. if (Math.abs(y) < this.snapEdge) y = 0;
  178.  
  179. const maxX = window.innerWidth - this.hud.offsetWidth;
  180. const maxY = window.innerHeight - this.hud.offsetHeight;
  181.  
  182. if (Math.abs(x - maxX) < this.snapEdge) x = maxX;
  183. if (Math.abs(y - maxY) < this.snapEdge) y = maxY;
  184.  
  185. this.hud.style.left = x + "px";
  186. this.hud.style.top = y + "px";
  187. });
  188.  
  189. document.addEventListener("mouseup", () => {
  190. isDragging = false;
  191. this.header.style.cursor = "grab";
  192. });
  193. }
  194.  
  195.  
  196. /**************** CLOSE BUTTON ****************/
  197. #setupClose() {
  198. this.closeBtn.onclick = () => {
  199. this.hud.remove();
  200. };
  201. }
  202.  
  203.  
  204. /**************** MINIMIZE BUTTON ****************/
  205. #setupMinimize() {
  206. this.minBtn.onclick = () => {
  207. const collapsed = this.tabBar.style.display === "none";
  208.  
  209. this.tabBar.style.display = collapsed ? "flex" : "none";
  210.  
  211. Object.values(this.tabContents).forEach(
  212. c => c.style.display = collapsed ? "block" : "none"
  213. );
  214.  
  215. this.minBtn.textContent = collapsed ? "-" : "+";
  216. this.hud.style.height = collapsed ? "180px" : "28px";
  217. };
  218. }
  219.  
  220.  
  221. /**************** RESIZING ****************/
  222. #addResizeHandle(pos, right, bottom, cursor, verticalOnly = false, horizontalOnly = false) {
  223. const handle = document.createElement("div");
  224. Object.assign(handle.style, {
  225. position: "absolute",
  226. width: verticalOnly ? "6px" : "12px",
  227. height: horizontalOnly ? "6px" : "12px",
  228. right,
  229. bottom,
  230. cursor,
  231. zIndex: 10000,
  232. background: "transparent"
  233. });
  234. this.hud.appendChild(handle);
  235.  
  236. let resizing = false;
  237.  
  238. handle.addEventListener("mousedown", () => resizing = true);
  239.  
  240. document.addEventListener("mousemove", (e) => {
  241. if (!resizing) return;
  242.  
  243. if (!horizontalOnly) {
  244. const newW = e.clientX - this.hud.offsetLeft;
  245. if (newW > 120) this.hud.style.width = newW + "px";
  246. }
  247. if (!verticalOnly) {
  248. const newH = e.clientY - this.hud.offsetTop;
  249. if (newH > 60) this.hud.style.height = newH + "px";
  250. }
  251. });
  252.  
  253. document.addEventListener("mouseup", () => resizing = false);
  254. }
  255. }
  256.  
  257.  
  258. /********************************************************************
  259. * CREATE YOUR HUD (no more spawn button)
  260. ********************************************************************/
  261. const myHUD = new HUD({
  262. title: "Super HUD",
  263. width: 260,
  264. height: 200,
  265. startX: 100,
  266. startY: 100,
  267. snapGrid: 20,
  268. snapEdge: 15,
  269. tabs: {
  270. "Main": `
  271. <p>Welcome to the new advanced HUD!</p>
  272. `,
  273. "Settings": `<p>Future settings go here.</p>`,
  274. "About": `<p>Hello</p>`
  275. }
  276. });
  277.  
Advertisement
Add Comment
Please, Sign In to add comment