// Copyright (c) 2012-2024 Wojciech Figat. All rights reserved. using System; using FlaxEngine.Assertions; namespace FlaxEngine.GUI { /// /// Root control implementation used by the . /// /// [HideInEditor] public sealed class WindowRootControl : RootControl { private Window _window; private Control _focusedControl; private Control _trackingControl; /// /// Gets the native window object. /// public Window Window => _window; /// /// Sets the window title. /// public string Title { get => _window.Title; set => _window.Title = value; } /// /// Gets a value indicating whether this window is in fullscreen mode. /// public bool IsFullscreen => _window.IsFullscreen; /// /// Gets a value indicating whether this window is in windowed mode. /// public bool IsWindowed => _window.IsWindowed; /// /// Gets a value indicating whether this instance is visible. /// public bool IsShown => _window.IsVisible; /// /// Gets a value indicating whether this window is minimized. /// public bool IsMinimized => _window.IsMinimized; /// /// Gets a value indicating whether this window is maximized. /// public bool IsMaximized => _window.IsMaximized; internal WindowRootControl(Window window) { _window = window; ClipChildren = false; if (Style.Current != null) BackgroundColor = Style.Current.Background; } /// /// Shows the window. /// public void Show() { _window.Show(); } /// /// Hides the window. /// public void Hide() { _window.Hide(); } /// /// Minimizes the window. /// public void Minimize() { _window.Minimize(); } /// /// Maximizes the window. /// public void Maximize() { _window.Maximize(); } /// /// Restores the window state before minimizing or maximizing. /// public void Restore() { _window.Restore(); } /// /// Closes the window. /// /// The closing reason. public void Close(ClosingReason reason = ClosingReason.CloseEvent) { _window.Close(reason); } /// /// Brings window to the front of the Z order. /// /// True if move to the front by force, otherwise false. public void BringToFront(bool force = false) { _window.BringToFront(force); } /// /// Flashes the window to bring use attention. /// public void FlashWindow() { _window.FlashWindow(); } /// /// Gets or sets the current focused control /// public override Control FocusedControl { get => _focusedControl; set { Assert.IsTrue(_focusedControl == null || _focusedControl.Root == this, "Invalid control to focus"); Focus(value); } } /// public override CursorType Cursor { get => _window.Cursor; set => _window.Cursor = value; } /// public override Float2 TrackingMouseOffset => _window.TrackingMouseOffset / _window.DpiScale; /// public override WindowRootControl RootWindow => this; /// public override Float2 MousePosition { get => _window.MousePosition / _window.DpiScale; set => _window.MousePosition = value * _window.DpiScale; } /// public override void StartTrackingMouse(Control control, bool useMouseScreenOffset) { if (control == null) throw new ArgumentNullException(); if (_trackingControl == control) return; if (_trackingControl != null) EndTrackingMouse(); if (control.AutoFocus) Focus(control); _trackingControl = control; _window.StartTrackingMouse(useMouseScreenOffset); } /// public override void EndTrackingMouse() { if (_trackingControl != null) { var control = _trackingControl; _trackingControl = null; control.OnEndMouseCapture(); _window.EndTrackingMouse(); } } /// public override bool GetKey(KeyboardKeys key) { return _window.GetKey(key); } /// public override bool GetKeyDown(KeyboardKeys key) { return _window.GetKeyDown(key); } /// public override bool GetKeyUp(KeyboardKeys key) { return _window.GetKeyUp(key); } /// public override bool GetMouseButton(MouseButton button) { return _window.GetMouseButton(button); } /// public override bool GetMouseButtonDown(MouseButton button) { return _window.GetMouseButtonDown(button); } /// public override bool GetMouseButtonUp(MouseButton button) { return _window.GetMouseButtonUp(button); } /// public override Float2 PointFromScreen(Float2 location) { return _window.ScreenToClient(location) / _window.DpiScale; } /// public override Float2 PointToScreen(Float2 location) { return _window.ClientToScreen(location * _window.DpiScale); } /// public override void Focus() { _window.Focus(); } /// public override void DoDragDrop(DragData data) { _window.DoDragDrop(data); } /// protected override bool Focus(Control c) { if (IsDisposing || _focusedControl == c) return false; // Change focused control Control previous = _focusedControl; _focusedControl = c; // Fire events if (previous != null) { previous.OnLostFocus(); Assert.IsFalse(previous.IsFocused); } if (_focusedControl != null) { _focusedControl.OnGotFocus(); Assert.IsTrue(_focusedControl.IsFocused); } // Update flags UpdateContainsFocus(); return true; } /// public override bool OnMouseDown(Float2 location, MouseButton button) { if (_trackingControl != null) { return _trackingControl.OnMouseDown(_trackingControl.PointFromWindow(location), button); } return base.OnMouseDown(location, button); } /// public override bool OnMouseUp(Float2 location, MouseButton button) { if (_trackingControl != null) { return _trackingControl.OnMouseUp(_trackingControl.PointFromWindow(location), button); } return base.OnMouseUp(location, button); } /// public override bool OnMouseDoubleClick(Float2 location, MouseButton button) { if (_trackingControl != null) { return _trackingControl.OnMouseDoubleClick(_trackingControl.PointFromWindow(location), button); } return base.OnMouseDoubleClick(location, button); } /// public override bool OnMouseWheel(Float2 location, float delta) { if (_trackingControl != null) { return _trackingControl.OnMouseWheel(_trackingControl.PointFromWindow(location), delta); } return base.OnMouseWheel(location, delta); } /// public override void OnMouseMove(Float2 location) { if (_trackingControl != null) { _trackingControl.OnMouseMove(_trackingControl.PointFromWindow(location)); return; } base.OnMouseMove(location); } /// public override void OnMouseMoveRelative(Float2 mouseMotion) { if (_trackingControl != null) { _trackingControl.OnMouseMoveRelative(mouseMotion); return; } base.OnMouseMoveRelative(mouseMotion); } } }