예제 #1
0
        public Window(WindowOptions options)
        {
            if (Application.MainWindow != null && Application.RenderingBackend == RenderingBackend.ES20)
            {
                // ES20 doesn't allow multiple contexts for now, because of a bug in OpenTK
                throw new Lime.Exception("Attempt to create a second window for ES20 rendering backend. Use OpenGL backend instead.");
            }
            if (options.UseTimer && options.AsyncRendering)
            {
                throw new Lime.Exception("Can't use both timer and async rendering");
            }
            if (options.ToolWindow)
            {
                form = new ToolForm();
            }
            else
            {
                form = new Form();
            }
            Input = new WindowInput(this);
            using (var graphics = form.CreateGraphics()) {
                PixelScale = CalcPixelScale(graphics.DpiX);
            }
            if (options.Style == WindowStyle.Borderless)
            {
                borderStyle = FormBorderStyle.None;
            }
            else
            {
                borderStyle = options.FixedSize ? FormBorderStyle.FixedSingle : FormBorderStyle.Sizable;
            }
            form.FormBorderStyle = borderStyle;
            form.MaximizeBox     = !options.FixedSize;
            if (options.MinimumDecoratedSize != Vector2.Zero)
            {
                MinimumDecoratedSize = options.MinimumDecoratedSize;
            }
            if (options.MaximumDecoratedSize != Vector2.Zero)
            {
                MaximumDecoratedSize = options.MaximumDecoratedSize;
            }
            renderControl = CreateRenderControl(Application.RenderingBackend);
            renderControl.CreateControl();
            renderControl.UnbindContext();
            renderControl.Dock        = DockStyle.Fill;
            renderControl.Paint      += OnPaint;
            renderControl.KeyDown    += OnKeyDown;
            renderControl.KeyUp      += OnKeyUp;
            renderControl.KeyPress   += OnKeyPress;
            renderControl.MouseDown  += OnMouseDown;
            renderControl.MouseUp    += OnMouseUp;
            renderControl.Resize     += OnResize;
            renderControl.MouseWheel += OnMouseWheel;
            renderControl.MouseEnter += (sender, args) => {
                Application.WindowUnderMouse = this;
            };
            renderControl.MouseLeave += (sender, args) => {
                if (Application.WindowUnderMouse == this)
                {
                    Application.WindowUnderMouse = null;
                }
            };
            renderControl.BeforeBoundsChanged += WaitForRendering;
            form.Move        += OnMove;
            form.Activated   += OnActivated;
            form.Deactivate  += OnDeactivate;
            form.FormClosing += OnClosing;
            form.FormClosed  += OnClosed;
            form.Shown       += OnShown;
            active            = Form.ActiveForm == form;

            if (options.UseTimer)
            {
                timer = new System.Windows.Forms.Timer {
                    Interval = (int)(1000.0 / 65),
                    Enabled  = true,
                };
                timer.Tick += OnTick;
            }
            else
            {
                vSync = options.VSync;
                renderControl.VSync = vSync;
                System.Windows.Forms.Application.Idle += OnTick;
            }

            form.Controls.Add(renderControl);
            stopwatch = new Stopwatch();
            stopwatch.Start();

            if (options.Icon != null)
            {
                form.Icon = (System.Drawing.Icon)options.Icon;
            }
            Cursor     = MouseCursor.Default;
            Title      = options.Title;
            ClientSize = options.ClientSize;
            if (options.Visible)
            {
                Visible = true;
            }
            if (options.Screen != null && options.Screen >= 0 && Screen.AllScreens.Length > options.Screen)
            {
                form.Location = GetCenter(Screen.AllScreens[options.Screen.Value].WorkingArea);
            }
            if (options.Centered)
            {
                Center();
            }
            if (Application.MainWindow == null)
            {
                Application.MainWindow = this;
                Closing += reason => Application.DoExiting();
                Closed  += Application.DoExited;
            }
            else
            {
                Form.Owner         = Application.MainWindow.Form;
                Form.StartPosition = FormStartPosition.CenterParent;
            }
            AsyncRendering = options.AsyncRendering;
            if (AsyncRendering)
            {
                renderThreadTokenSource   = new CancellationTokenSource();
                renderThreadToken         = renderThreadTokenSource.Token;
                renderThread              = new Thread(RenderLoop);
                renderThread.IsBackground = true;
                renderThread.Start();
            }
            Application.Windows.Add(this);
        }