Basic multiple windows working

This commit is contained in:
Simon Lübeß
2024-09-10 23:02:34 +02:00
parent 352073869f
commit 2e3c0d0aa4
6 changed files with 128 additions and 22 deletions
+6
View File
@@ -43,6 +43,12 @@
margin-top: 6px;
background: linear-gradient(0deg, #7270ed, #6a11ea);
}
button:hover {
box-shadow: none;
margin-left: 6px;
margin-top: 6px;
background: linear-gradient(0deg, #7d2eea, #908ee8);
}
#msg {
visibility: hidden;
opacity: 0;
+66
View File
@@ -12,10 +12,67 @@ namespace GlitchyEditor;
class UltralightLayer : Layer
{
Window windoww ~ delete _;
int2 cursorPosition;
public this()
{
NoApp();
//RenderApp();
WindowDescription windowDesc = .Default;
windowDesc.Icon = "Resources/Textures/GlitchyEngineIcon.ico";
windowDesc.Title = "Test";
windoww = new Window(windowDesc);
windoww.EventCallback = new => EventHandler;
}
private void EventHandler(Event e)
{
EventDispatcher dispatcher = EventDispatcher(e);
dispatcher.Dispatch<MouseMovedEvent>(scope => MouseMoved);
dispatcher.Dispatch<MouseButtonPressedEvent>(scope (e) => MousePressed(e, true));
dispatcher.Dispatch<MouseButtonReleasedEvent>(scope (e) => MousePressed(e, false));
}
private bool MouseMoved(MouseMovedEvent e)
{
if (e.Window == windoww)
{
Log.EngineLogger.Warning($"{e.PositionX}");
ULMouseEvent evt = ulCreateMouseEvent(.kMouseEventType_MouseMoved, e.PositionX, e.PositionY, .kMouseButton_None);
ulViewFireMouseEvent(view, evt);
ulDestroyMouseEvent(evt);
cursorPosition = .(e.PositionX, e.PositionY);
}
return false;
}
private bool MousePressed(MouseButtonEvent e, bool press)
{
if (e.Window == windoww)
{
ULMouseButton button = .kMouseButton_None;
switch (e.MouseButton)
{
case .LeftButton: button = .kMouseButton_Left;
case .RightButton: button = .kMouseButton_Right;
case .MiddleButton: button = .kMouseButton_Middle;
default: button = .kMouseButton_None;
}
ULMouseEvent evt = ulCreateMouseEvent(press ? .kMouseEventType_MouseDown : .kMouseEventType_MouseUp, cursorPosition.X, cursorPosition.Y, button);
ulViewFireMouseEvent(view, evt);
ulDestroyMouseEvent(evt);
}
return false;
}
private static void OnAppUpdate(void* user_data)
@@ -162,6 +219,13 @@ class UltralightLayer : Layer
CopybitmapToTexture(ulBitmapSurfaceGetBitmap(surface));
ulSurfaceClearDirtyBounds(surface);
}
Render();
}
private void Render()
{
RenderCommand.Clear(windoww.SwapChain.BackBuffer, .Color | .Depth, .(0.7f, 0.2f, 0.2f), 1.0f, 0);
}
private void CreateView()
@@ -171,6 +235,8 @@ class UltralightLayer : Layer
view = ulCreateView(renderer, 500, 500, viewConfig, null);
ulViewSetDOMReadyCallback(view, => OnDOMReady, null);
ulDestroyViewConfig(viewConfig);
ULString url = ulCreateString("file:///app.html");
+15 -3
View File
@@ -15,6 +15,9 @@ namespace GlitchyEngine
static Application s_Instance = null;
private Window _mainWindow;
private append List<Window> _windows = .();
private RendererAPI _rendererApi;
private bool _running = true;
@@ -53,6 +56,8 @@ namespace GlitchyEngine
public Settings Settings {get; private set;} = new .() ~ delete _;
public List<Window> Windows => _windows;
public this()
{
Profiler.ProfileFunction!();
@@ -122,7 +127,11 @@ namespace GlitchyEngine
delete _layerStack;
delete _rendererApi;
delete _mainWindow;
while (!_windows.IsEmpty)
{
delete _windows.Back;
}
delete _gameTime;
@@ -197,8 +206,11 @@ namespace GlitchyEngine
#if IMGUI
_imGuiLayer.ImGuiRender();
#endif
_mainWindow.SwapChain.Present();
for (Window window in _windows)
{
window.SwapChain.Present();
}
}
}
}
+12 -4
View File
@@ -4,6 +4,7 @@ namespace GlitchyEngine.Events
{
public class MouseMovedEvent : Event, IEvent
{
private Window _window;
private int32 _mouseX, _mouseY;
public override EventType EventType => .MouseMoved;
@@ -14,11 +15,14 @@ namespace GlitchyEngine.Events
public static EventType StaticType => .MouseMoved;
public Window Window => _window;
public int32 PositionX => _mouseX;
public int32 PositionY => _mouseY;
public this(int32 x, int32 y)
public this(Window window, int32 x, int32 y)
{
_window = window;
_mouseX = x;
_mouseY = y;
}
@@ -95,14 +99,18 @@ namespace GlitchyEngine.Events
public abstract class MouseButtonEvent : Event
{
protected Window _window;
protected MouseButton _mouseButton;
public override EventCategory Category => .Input | .Mouse;
public Window Window => _window;
public MouseButton MouseButton => _mouseButton;
protected this(MouseButton mouseButton)
protected this(Window window, MouseButton mouseButton)
{
_window = window;
_mouseButton = mouseButton;
}
}
@@ -115,7 +123,7 @@ namespace GlitchyEngine.Events
public static EventType StaticType => .MouseButtonPressed;
public this(MouseButton mouseButton) : base(mouseButton) { }
public this(Window window, MouseButton mouseButton) : base(window, mouseButton) { }
public override void ToString(String strBuffer)
{
@@ -132,7 +140,7 @@ namespace GlitchyEngine.Events
public static EventType StaticType => .MouseButtonReleased;
public this(MouseButton mouseButton) : base(mouseButton) { }
public this(Window window, MouseButton mouseButton) : base(window, mouseButton) { }
public override void ToString(String strBuffer)
{
@@ -162,6 +162,8 @@ namespace GlitchyEngine
_minMaxInfo.MaximumTrackingSize.x = int32.MaxValue;
_minMaxInfo.MaximumTrackingSize.y = int32.MaxValue;
Application.Instance.Windows.Add(this);
Init(desc);
}
@@ -181,7 +183,10 @@ namespace GlitchyEngine
Log.EngineLogger.Error($"Failed to destroy window: Message ({(int)res}): {res}");
}
// TODO manage window classes somehow
UnregisterClass(WindowClassName.ToScopedNativeWChar!(), _instanceHandle);
Application.Instance.Windows.Remove(this);
}
[LinkName(.C)]
@@ -217,7 +222,7 @@ namespace GlitchyEngine
{
uint32 lastError = GetLastError();
Log.EngineLogger.Error($"Failed to register window class. Message({(int)lastError}): {(HResult)lastError}");
Runtime.FatalError("Failed to register window class");
//Runtime.FatalError("Failed to register window class");
}
{
@@ -381,34 +386,34 @@ namespace GlitchyEngine
// Left mouse button
case WM_LBUTTONDOWN:
{
var event = scope MouseButtonPressedEvent(.LeftButton);
var event = scope MouseButtonPressedEvent(window, .LeftButton);
window._eventCallback(event);
}
case WM_LBUTTONUP:
{
var event = scope MouseButtonReleasedEvent(.LeftButton);
var event = scope MouseButtonReleasedEvent(window, .LeftButton);
window._eventCallback(event);
}
// Right mouse button
case WM_RBUTTONDOWN:
{
var event = scope MouseButtonPressedEvent(.RightButton);
var event = scope MouseButtonPressedEvent(window, .RightButton);
window._eventCallback(event);
}
case WM_RBUTTONUP:
{
var event = scope MouseButtonReleasedEvent(.RightButton);
var event = scope MouseButtonReleasedEvent(window, .RightButton);
window._eventCallback(event);
}
// Middle mouse button
case WM_MBUTTONDOWN:
{
var event = scope MouseButtonPressedEvent(.MiddleButton);
var event = scope MouseButtonPressedEvent(window, .MiddleButton);
window._eventCallback(event);
}
case WM_MBUTTONUP:
{
var event = scope MouseButtonReleasedEvent(.MiddleButton);
var event = scope MouseButtonReleasedEvent(window, .MiddleButton);
window._eventCallback(event);
}
// X button
@@ -420,7 +425,7 @@ namespace GlitchyEngine
else
button = .XButton2;
var event = scope MouseButtonPressedEvent(button);
var event = scope MouseButtonPressedEvent(window, button);
window._eventCallback(event);
}
case WM_XBUTTONUP:
@@ -431,7 +436,7 @@ namespace GlitchyEngine
else
button = .XButton2;
var event = scope MouseButtonReleasedEvent(button);
var event = scope MouseButtonReleasedEvent(window, button);
window._eventCallback(event);
}
// Vertical scrolling
@@ -453,8 +458,8 @@ namespace GlitchyEngine
case WM_MOUSEMOVE:
{
SplitHighAndLowOrder!(lParam, let x, let y);
var event = scope MouseMovedEvent(x, y);
var event = scope MouseMovedEvent(window, x, y);
window._eventCallback(event);
}
case WM_INPUT:
@@ -470,8 +475,8 @@ namespace GlitchyEngine
RAWINPUT* raw = (.)rawData.CArray();
if (raw.Header.Type == RIM_TYPEMOUSE)
{
var event = scope MouseMovedEvent(raw.Data.Mouse.lLastX, raw.Data.Mouse.lLastY);
window._eventCallback(event);
var event = scope MouseMovedEvent(window, raw.Data.Mouse.lLastX, raw.Data.Mouse.lLastY);
//window._eventCallback(event);
// We accumulate the raw movements an collect them once each frame in WindowsInput.Impl_NewFrame()
window._rawMouseMovementAccumulator += int2(raw.Data.Mouse.lLastX, raw.Data.Mouse.lLastY);
+11 -2
View File
@@ -31,7 +31,7 @@ namespace GlitchyEngine
public delegate void EventCallback(Event e);
protected EventCallback _eventCallback ~ delete _;
protected EventCallback _eventCallback = new => DefaultEventHandler ~ delete _;
public SwapChain SwapChain => _swapChain;
@@ -105,7 +105,11 @@ namespace GlitchyEngine
public EventCallback EventCallback
{
get => _eventCallback;
set => _eventCallback = value;
set
{
delete _eventCallback;
_eventCallback = value;
}
}
public extern void Update();
@@ -114,5 +118,10 @@ namespace GlitchyEngine
* Sets the Icon of the window to the given file.
*/
public extern Result<void> SetIcon(StringView filePath);
private void DefaultEventHandler(Event e)
{
Log.EngineLogger.Error($"No event handler registered for window.");
}
}
}