Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --------------------------------------------------------------------------------
- -- DON'T TOUCH ANYTHING BELOW UNLESS YOU KNOW WHAT YOU'RE DOING
- --------------------------------------------------------------------------------
- local compass_offset_inches = compass_offset_feet * 12
- local compass_radius_inches = compass_radius_feet * 12
- local WINDOW_NAME_TEMPLATE = "T_Compass3D_Point"
- --------------------------------------------------------------------------------
- -- Core
- --------------------------------------------------------------------------------
- Compass3D = Compass3D or {}
- function Compass3D.OnInitialize()
- Compass3D.Camera.OnInitialize()
- for i = 1, #compass_windows do
- local w = compass_windows[i]
- -- Create UI element
- CreateWindowFromTemplate(w.name, WINDOW_NAME_TEMPLATE, "Root")
- WindowSetShowing(w.name, true)
- WindowSetScale(w.name, w.scale * InterfaceCore.GetScale())
- WindowSetTintColor(w.name, w.red, w.green, w.blue)
- -- Set icon
- local texture, tx, ty = GetIconData(w.icon)
- DynamicImageSetTexture(w.name .. "_Icon", texture, tx, ty)
- w.offset_x = w.direction_x * compass_radius_inches
- w.offset_y = w.direction_y * compass_radius_inches
- end
- end
- function Compass3D.OnShutdown()
- Compass3D.Camera.OnShutdown()
- for i = 1, #compass_windows do
- DestroyWindow(compass_windows[i].name)
- end
- end
- function Compass3D.OnUpdate(time_delta_s)
- local camera_pivot_x, camera_pivot_y, camera_pivot_z, camera_pivot_zone_id = Compass3D.Camera.GetPivotZonePosition()
- -- Hide all points if camera is unavailable
- if not camera_pivot_x then
- for i = 1, #compass_windows do
- WindowSetAlpha(compass_windows[i].name, 0)
- end
- return
- end
- -- Convert screen dimensions to UI-logical pixels
- local screen_width, screen_height = GetScreenResolution()
- screen_width = screen_width / InterfaceCore.GetScale()
- screen_height = screen_height / InterfaceCore.GetScale()
- local center_x, center_y = screen_width * 0.5, screen_height * 0.5
- -- Update every compass point
- for i = 1, #compass_windows do
- local window = compass_windows[i]
- -- Calculate world-space position
- local world_x = camera_pivot_x + window.offset_x
- local world_y = camera_pivot_y + window.offset_y
- local world_z = camera_pivot_z + compass_offset_inches
- -- Project to screen
- local visible, screen_x, screen_y = WorldToScreen(world_x, world_y, world_z)
- screen_x = screen_x / InterfaceCore.GetScale()
- screen_y = screen_y / InterfaceCore.GetScale()
- -- Set alpha (0 if not visible or off-screen)
- local new_alpha = (visible and
- screen_x >= 0 and screen_x <= screen_width and
- screen_y >= 0 and screen_y <= screen_height)
- and window.alpha or 0
- WindowSetAlpha(window.name, new_alpha)
- -- Update position if visible
- if new_alpha > 0 then
- WindowClearAnchors(window.name)
- WindowAddAnchor(window.name, "center", "Root", "center",
- screen_x - center_x, screen_y - center_y)
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment