crosshair ui

This commit is contained in:
ntr committed 2023-08-08 15:16:59 +10:00
1 parent 8fdc3dba82
commit 90dfa02659
8 files changed
+241 -7

No files matched your search

+2 -3
View File
@@ -1,6 +1,5 @@
<ui:UXML xmlns:ui="UnityEngine.UIElements" xmlns:uie="UnityEditor.UIElements" xsi="http://www.w3.org/2001/XMLSchema-instance" engine="UnityEngine.UIElements" editor="UnityEditor.UIElements" noNamespaceSchemaLocation="../../UIElementsSchema/UIElements.xsd" editor-extension-mode="False">
<Style src="project://database/Assets/Src/UI/TimingUi.uss?fileID=7433441132597879392&amp;guid=cf92f075876ddf7ffb4cd5c59633ad7b&amp;type=3#ShipUi" />
<ui:VisualElement style="flex-grow: 1; background-color: rgba(0, 0, 0, 0); align-items: center; justify-content: center;">
<ui:Label tabindex="-1" text="✛" display-tooltip-when-elided="true" style="opacity: 0.5; position: relative; -unity-text-align: middle-center; -unity-font: url(&apos;project://database/Assets/UI%20Toolkit/Fonts/Jura-Light.otf?fileID=12800000&amp;guid=54b9163f81dfb0d7baa7fda0d795ceeb&amp;type=3#Jura-Light&apos;); -unity-font-definition: initial; font-size: 16px;" />
<ui:VisualElement style="flex-grow: 1; background-color: rgba(0, 0, 0, 0);">
<CrosshairComponents.FourArmCrosshair style="width: 100%; height: 100%;" />
</ui:VisualElement>
</ui:UXML>
+212
View File
@@ -0,0 +1,212 @@
using System;
using Unity.Collections;
using UnityEngine;
using UnityEngine.UIElements;
namespace CrosshairComponents {
public class ThreeArmCrosshair : VisualElement {
// Need this uxml traits and factory for it to show up in editor
public new class UxmlTraits : VisualElement.UxmlTraits {
public override void Init(VisualElement ve, IUxmlAttributes bag, CreationContext cc) {
base.Init(ve, bag, cc);
}
}
public new class UxmlFactory : UxmlFactory<ThreeArmCrosshair, UxmlTraits> { }
static CustomStyleProperty<Color> s_TrackColor = new CustomStyleProperty<Color>("--track-color");
static CustomStyleProperty<Color> s_ProgressColor = new CustomStyleProperty<Color>("--progress-color");
Color m_TrackColor = Color.white;
Color m_SpeedColor = Color.white;
// This is the label that displays the percentage.
Label m_Label;
// This is the number that the Label displays as a percentage.
float m_Speed;
public float speed {
get => m_Speed;
set {
// Whenever the progress property changes, MarkDirtyRepaint() is named. This causes a call to the
// generateVisualContents callback.
m_Speed = value;
m_Label.text = Mathf.Clamp(Mathf.Round(value), 0, 100) + "%";
MarkDirtyRepaint();
}
}
public ThreeArmCrosshair() {
// Create a Label, add a USS class name, and add it to this visual tree.
m_Label = new Label();
Add(m_Label);
// Register a callback after custom style resolution.
RegisterCallback<CustomStyleResolvedEvent>(evt => CustomStylesResolved(evt));
// Register a callback to generate the visual content of the control.
generateVisualContent += GenerateVisualContent;
}
static void CustomStylesResolved(CustomStyleResolvedEvent evt) {
ThreeArmCrosshair element = (ThreeArmCrosshair)evt.currentTarget;
element.UpdateCustomStyles();
}
// After the custom colors are resolved, this method uses them to color the meshes and (if necessary) repaint
// the control.
void UpdateCustomStyles() {
bool repaint = false;
if (customStyle.TryGetValue(s_ProgressColor, out m_SpeedColor))
repaint = true;
if (customStyle.TryGetValue(s_TrackColor, out m_TrackColor))
repaint = true;
if (repaint)
MarkDirtyRepaint();
}
void GenerateVisualContent(MeshGenerationContext context) {
float width = contentRect.width;
float height = contentRect.height;
float length = height / 100;
Vector2 centre = new Vector2(width / 2, height / 2);
Vector2 centreArm = new Vector2(centre.x, centre.y - length);
float angle = (float)(Math.PI / 3);
float angleSin = (float)Math.Sin(angle);
Vector2 rightArm = new Vector2(centre.x + angleSin * length, centre.y + angleSin * length);
Vector2 leftArm = new Vector2(centre.x - angleSin * length, centre.y + angleSin * length);
var painter = context.painter2D;
painter.lineWidth = 2.0f;
painter.lineCap = LineCap.Butt;
painter.strokeColor = m_TrackColor;
painter.BeginPath();
painter.MoveTo(centre);
painter.LineTo(centreArm);
painter.Stroke();
painter.BeginPath();
painter.MoveTo(centre);
painter.LineTo(rightArm);
painter.Stroke();
painter.BeginPath();
painter.MoveTo(centre);
painter.LineTo(leftArm);
painter.Stroke();
}
}
public class FourArmCrosshair : VisualElement {
// Need this uxml traits and factory for it to show up in editor
public new class UxmlTraits : VisualElement.UxmlTraits {
public override void Init(VisualElement ve, IUxmlAttributes bag, CreationContext cc) {
base.Init(ve, bag, cc);
}
}
public new class UxmlFactory : UxmlFactory<FourArmCrosshair, UxmlTraits> { }
// These are USS class names for the control overall and the label.
public static readonly string ussClassName = "crosshair";
public static readonly string ussLabelClassName = "crosshair__label";
static CustomStyleProperty<Color> s_TrackColor = new CustomStyleProperty<Color>("--track-color");
static CustomStyleProperty<Color> s_ProgressColor = new CustomStyleProperty<Color>("--progress-color");
Color m_TrackColor = Color.white;
Color m_SpeedColor = Color.white;
// This is the label that displays the percentage.
Label m_Label;
// This is the number that the Label displays as a percentage.
float m_Speed;
public float speed {
get => m_Speed;
set {
// Whenever the progress property changes, MarkDirtyRepaint() is named. This causes a call to the
// generateVisualContents callback.
m_Speed = value;
m_Label.text = Mathf.Clamp(Mathf.Round(value), 0, 100) + "%";
MarkDirtyRepaint();
}
}
public FourArmCrosshair() {
// Create a Label, add a USS class name, and add it to this visual tree.
m_Label = new Label();
Add(m_Label);
// Register a callback after custom style resolution.
RegisterCallback<CustomStyleResolvedEvent>(evt => CustomStylesResolved(evt));
// Register a callback to generate the visual content of the control.
generateVisualContent += GenerateVisualContent;
}
static void CustomStylesResolved(CustomStyleResolvedEvent evt) {
FourArmCrosshair element = (FourArmCrosshair)evt.currentTarget;
element.UpdateCustomStyles();
}
// After the custom colors are resolved, this method uses them to color the meshes and (if necessary) repaint
// the control.
void UpdateCustomStyles() {
bool repaint = false;
if (customStyle.TryGetValue(s_ProgressColor, out m_SpeedColor))
repaint = true;
if (customStyle.TryGetValue(s_TrackColor, out m_TrackColor))
repaint = true;
if (repaint)
MarkDirtyRepaint();
}
void GenerateVisualContent(MeshGenerationContext context) {
float width = contentRect.width;
float height = contentRect.height;
float length = height / 100;
Vector2 centre = new Vector2(width / 2, height / 2);
Vector2 topArm = new Vector2(centre.x, centre.y - length);
Vector2 rightArm = new Vector2(centre.x + length, centre.y);
Vector2 leftArm = new Vector2(centre.x - length, centre.y);
Vector2 bottomArm = new Vector2(centre.x, centre.y + length);
var painter = context.painter2D;
painter.lineWidth = 2.0f;
painter.lineCap = LineCap.Butt;
painter.strokeColor = m_TrackColor;
painter.BeginPath();
painter.MoveTo(centre);
painter.LineTo(topArm);
painter.Stroke();
painter.BeginPath();
painter.MoveTo(centre);
painter.LineTo(bottomArm);
painter.Stroke();
painter.BeginPath();
painter.MoveTo(centre);
painter.LineTo(rightArm);
painter.Stroke();
painter.BeginPath();
painter.MoveTo(centre);
painter.LineTo(leftArm);
painter.Stroke();
}
}
}
+11
View File
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 34c48ca28628408a493e22a9a93f7231
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
+4
View File
@@ -0,0 +1,4 @@
.crosshair {
width: 100%;
height: 100%;
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 48feeaa21d44b719b9ecf7857f06e39c
ScriptedImporter:
internalIDToNameTable: []
externalObjects: {}
serializedVersion: 2
userData:
assetBundleName:
assetBundleVariant:
script: {fileID: 12385, guid: 0000000000000000e000000000000000, type: 0}
disableValidation: 0
@@ -4,7 +4,6 @@ using UnityEngine.UIElements;
namespace SpaceRaceUi
{
// An element that displays progress inside a partially filled circle
public class RadialProgress : VisualElement
{
File renamed without changes.
+1 -3
View File
@@ -1,8 +1,6 @@
# SpaceRace!
- show result on finish lap
- hide current time and pb
- TOTAL_TIME DELTA
- ballin triforce crosshair
- shields