-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathButtonsAndRaycasting.cs
More file actions
183 lines (159 loc) · 6.81 KB
/
ButtonsAndRaycasting.cs
File metadata and controls
183 lines (159 loc) · 6.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
using Cameras;
using Cameras.Components;
using Data;
using DefaultPresentationAssets;
using Fonts;
using InputDevices;
using Materials;
using Meshes;
using Models;
using Physics;
using Physics.Components;
using Physics.Functions;
using Physics.Messages;
using Rendering;
using Shapes.Types;
using System;
using System.Diagnostics;
using System.Numerics;
using System.Runtime.InteropServices;
using Textures;
using Transforms;
using Transforms.Components;
using UI;
using UI.Components;
using Windows;
using Worlds;
namespace Abacus
{
public class ButtonsAndRaycasting : Program
{
private readonly Window window;
private readonly Camera worldCamera;
private Vector3 cameraPosition;
private Vector2 cameraPitchYaw;
public unsafe ButtonsAndRaycasting(Application application) : base(application)
{
//window to render everything
window = new(world, "Fly", default, new(900, 600), "vulkan", new(&WindowClosed));
window.IsResizable = true;
window.BecomeMaximized();
worldCamera = new(world, window, CameraSettings.CreatePerspectiveDegrees(90));
Transform cameraTransform = worldCamera.Become<Transform>();
cameraTransform.LocalPosition = new(0, 0, -10);
cameraPosition = cameraTransform.LocalPosition;
Camera uiCamera = new(world, window, CameraSettings.CreateOrthographic(1f));
//global references
Texture squareTexture = new(world, EmbeddedResource.GetAddress<SquareTexture>());
Model cubeModel = new(world, EmbeddedResource.GetAddress<CubeModel>());
Mesh cubeMesh = new(world, cubeModel);
Font robotoFont = new(world, EmbeddedResource.GetAddress<RobotoFont>());
Material unlitWorldMaterial = new(world, EmbeddedResource.GetAddress<UnlitTexturedMaterial>());
unlitWorldMaterial.AddPushConstant<Color>();
unlitWorldMaterial.AddPushConstant<LocalToWorld>();
unlitWorldMaterial.AddComponentBinding<CameraMatrices>(new(0, 0), worldCamera);
unlitWorldMaterial.AddTextureBinding(new(1, 0), squareTexture);
Settings settings = new(world);
Canvas canvas = new(settings, uiCamera);
Material textMaterial = new(world, EmbeddedResource.GetAddress<TextMaterial>());
textMaterial.AddComponentBinding<CameraMatrices>(new(1, 0), uiCamera);
textMaterial.AddPushConstant<Color>();
textMaterial.AddPushConstant<LocalToWorld>();
//crate test cube
MeshRenderer waveRenderer = new(world, cubeMesh, unlitWorldMaterial, worldCamera.RenderMask);
Transform waveTransform = waveRenderer.Become<Transform>();
waveRenderer.AddComponent(Color.Red);
waveRenderer.AddComponent(new IsBody(new CubeShape(0.5f), BodyType.Static));
//create ui boxes
Button testWindow = new(new(&TestBoxPressed), canvas);
testWindow.Size = new(300, 300);
testWindow.Position = new(20, 20);
Button anotherBox = new(new(&AnotherBoxPressed), canvas);
anotherBox.Size = new(190, 100);
anotherBox.Position = new(0, 0);
anotherBox.Anchor = Anchor.Centered;
anotherBox.Pivot = new(0.5f, 0.5f, 0f);
anotherBox.Color = new(0, 0.5f, 1, 1);
TextMesh textMesh = new(world, "abacus 123 hiii", robotoFont);
TextRenderer textRenderer = new(world, textMesh, textMaterial, uiCamera.RenderMask);
textRenderer.SetParent(anotherBox);
Transform textTransform = textRenderer.Become<Transform>();
textTransform.LocalPosition = new(4f, -4f, 0.1f);
textTransform.LocalScale = Vector3.One * 32f;
textRenderer.AddComponent(Color.Orange);
textRenderer.AddComponent(Anchor.TopLeft);
textRenderer.AddComponent(Pivot.TopLeft);
[UnmanagedCallersOnly]
static void TestBoxPressed(Entity entity)
{
Trace.WriteLine("Test box pressed");
}
[UnmanagedCallersOnly]
static void AnotherBoxPressed(Entity entity)
{
Trace.WriteLine("Another box pressed");
}
[UnmanagedCallersOnly]
static void WindowClosed(Window window)
{
window.Dispose();
}
}
public override void Dispose()
{
if (!window.IsDestroyed)
{
window.Dispose();
}
}
public unsafe override bool Update(double deltaTime)
{
if (window.IsDestroyed)
{
return false;
}
Transform cameraTransform = worldCamera.Become<Transform>();
SharedFunctions.MoveCameraAround(world, cameraTransform, deltaTime, ref cameraPosition, ref cameraPitchYaw, new(1f, 1f));
if (world.TryGetFirst(out Mouse mouse))
{
if (!mouse.ContainsComponent<IsPointer>())
{
mouse.AddComponent(new IsPointer(mouse.Position));
}
ref IsPointer pointer = ref mouse.GetComponent<IsPointer>();
pointer.position = mouse.Position;
pointer.action = default;
pointer.scroll = mouse.Scroll;
if (mouse.IsPressed(Mouse.Button.LeftButton))
{
pointer.HasPrimaryIntent = true;
}
if (mouse.IsPressed(Mouse.Button.RightButton))
{
pointer.HasSecondaryIntent = true;
}
if (mouse.WasPressed(Mouse.Button.LeftButton))
{
Vector2 screenPoint = worldCamera.Destination.GetScreenPointFromPosition(mouse.Position);
(Vector3 origin, Vector3 direction) ray = worldCamera.Matrices.GetRayFromScreenPoint(screenPoint);
simulator.Broadcast(new RaycastRequest(ray.origin, ray.direction, new(&OnRaycastHit)));
[UnmanagedCallersOnly]
static void OnRaycastHit(RaycastHitCallback.Input input)
{
ReadOnlySpan<RaycastHit> hits = input.Hits;
for (int i = 0; i < hits.Length; i++)
{
RaycastHit hit = hits[i];
ref Position position = ref input.world.TryGetComponent<Position>(hit.entity, out bool contains);
if (contains)
{
position.value.X += 0.1f;
}
}
}
}
}
return true;
}
}
}