A Razor UI panel for a minimap. It contains commented-out Razor markup and C# code for computing camera-relative ground positions and traces, and implements BuildHash to always change with Time.Now so the UI rebuilds frequently.
@using Sandbox;
@using Sandbox.UI;
@using System;
@inherits Panel
@attribute [StyleSheet("MinimapPanel.razor.scss")]
@*
@{
var cam = Manager.Instance.CameraController;
var degrees = cam.WorldRotation.Yaw() - 90;
var mapSize = 200f;
}
<root style="width:@(mapSize)px; height: @(mapSize)px;">
<MinimapTexture />
@{
var cameraPos = cam.CenterGroundPos;
var cameraRelativePos = cam.RelativeCenterGroundPos;
var viewportCenter = cameraRelativePos * mapSize;
var leftBot = cam.RelativeLowerLeftGroundPos * mapSize;
var leftTop = cam.RelativeUpperLeftGroundPos * mapSize;
var rightBot = cam.RelativeLowerRightGroundPos * mapSize;
var rightTop = cam.RelativeUpperRightGroundPos * mapSize;
var leftLength = (cam.RelativeUpperLeftGroundPos - cam.RelativeLowerLeftGroundPos).Length * mapSize;
var topLength = (cam.RelativeUpperRightGroundPos - cam.RelativeUpperLeftGroundPos).Length * mapSize;
var rightLength = (cam.RelativeLowerRightGroundPos - cam.RelativeUpperRightGroundPos).Length * mapSize;
var botLength = (cam.RelativeLowerLeftGroundPos - cam.RelativeLowerRightGroundPos).Length * mapSize;
// var pointColor = Color.Lerp(new Color(1f), new Color(0.5f), 0.5f + Utils.FastSin(Time.Now * 8f)).Rgba;
var pointColor = Color.White.WithAlpha(0.1f).Rgba;
var viewportSize = 80f;
}
<div class="line" style="left: @(leftBot.x * mapSize)px; bottom: @(leftBot.y * mapSize)px; height: @(5)px; transform: rotate(@(0)deg);"></div>
<div class="line" style="left: @(leftTop.x * mapSize)px; bottom: @(leftTop.y * mapSize)px; height: @(5)px; transform: rotate(@(0)deg);"></div>
<div class="line" style="left: @(rightBot.x * mapSize)px; bottom: @(rightBot.y * mapSize)px; height: @(5)px; transform: rotate(@(0)deg);"></div>
<div class="line" style="left: @(rightTop.x * mapSize)px; bottom: @(rightTop.y * mapSize)px; height: @(5)px; transform: rotate(@(0)deg);"></div>
</root>
*@
@code
{
// public Vector2 CenterGroundPos { get; set; }
// public Vector2 RelativeCenterGroundPos { get; set; }
// public Vector2 RelativeUpperLeftGroundPos { get; set; }
// public Vector2 RelativeLowerLeftGroundPos { get; set; }
// public Vector2 RelativeUpperRightGroundPos { get; set; }
// public Vector2 RelativeLowerRightGroundPos { get; set; }
// public override void Tick()
// {
// base.Tick();
// var camera = Scene.Camera;
// var groundFacingTrace = Scene.Trace.Ray(Scene.Camera.ScreenNormalToRay(new Vector3(0.5f, 0.5f, 0f)), 1500f).WithAllTags("bg_ground").Run();
// if (groundFacingTrace.Hit)
// CenterGroundPos = groundFacingTrace.HitPosition;
// Vector2 UpperLeftGroundPos, LowerLeftGroundPos, UpperRightGroundPos, LowerRightGroundPos;
// var upperLeftTrace = Scene.Trace.Ray(Scene.Camera.ScreenNormalToRay(new Vector3(0f, 0f, 0f)), 1500f).WithAllTags("bg_ground").Run();
// if (upperLeftTrace.Hit)
// UpperLeftGroundPos = upperLeftTrace.HitPosition;
// var lowerLeftTrace = Scene.Trace.Ray(Scene.Camera.ScreenNormalToRay(new Vector3(0f, 1f, 0f)), 1500f).WithAllTags("bg_ground").Run();
// if (lowerLeftTrace.Hit)
// LowerLeftGroundPos = lowerLeftTrace.HitPosition;
// var upperRightTrace = Scene.Trace.Ray(Scene.Camera.ScreenNormalToRay(new Vector3(1f, 0f, 0f)), 1500f).WithAllTags("bg_ground").Run();
// if (upperRightTrace.Hit)
// UpperRightGroundPos = upperRightTrace.HitPosition;
// var lowerRightTrace = Scene.Trace.Ray(Scene.Camera.ScreenNormalToRay(new Vector3(1f, 1f, 0f)), 1500f).WithAllTags("bg_ground").Run();
// if (lowerRightTrace.Hit)
// LowerRightGroundPos = lowerRightTrace.HitPosition;
// RelativeCenterGroundPos = new Vector2((CenterGroundPos.x + Math.Abs(grid.ArenaXMin)) / grid.ArenaWidth, (CenterGroundPos.y + Math.Abs(grid.ArenaYMin)) / grid.ArenaDepth);
// RelativeUpperLeftGroundPos = new Vector2((UpperLeftGroundPos.x + Math.Abs(grid.ArenaXMin)) / grid.ArenaWidth, (UpperLeftGroundPos.y + Math.Abs(grid.ArenaYMin)) / grid.ArenaDepth);
// RelativeLowerLeftGroundPos = new Vector2((LowerLeftGroundPos.x + Math.Abs(grid.ArenaXMin)) / grid.ArenaWidth, (LowerLeftGroundPos.y + Math.Abs(grid.ArenaYMin)) / grid.ArenaDepth);
// RelativeUpperRightGroundPos = new Vector2((UpperRightGroundPos.x + Math.Abs(grid.ArenaXMin)) / grid.ArenaWidth, (UpperRightGroundPos.y + Math.Abs(grid.ArenaYMin)) / grid.ArenaDepth);
// RelativeLowerRightGroundPos = new Vector2((LowerRightGroundPos.x + Math.Abs(grid.ArenaXMin)) / grid.ArenaWidth, (LowerRightGroundPos.y + Math.Abs(grid.ArenaYMin)) / grid.ArenaDepth);
// }
protected override int BuildHash()
{
return HashCode.Combine(
Time.Now
);
}
}