Пример #1
0
        static void Main(string[] args)
        {
            using (var renderForm = new RenderForm("DirectReact sample"))
            {
                renderForm.ClientSize = new System.Drawing.Size(1280, 720);
                var bounds      = new Bounds(x: 0, y: 0, width: renderForm.ClientSize.Width, height: renderForm.ClientSize.Height);
                var first       = true;
                var eventSource = new RawInputEventSource(() => {
                    if (first)
                    {
                        first = false;
                        return(renderForm.PointToClient(Cursor.Position));
                    }
                    if (renderForm.Focused)
                    {
                        return(renderForm.PointToClient(Cursor.Position));
                    }
                    return(null);
                });

                using (var renderer = new Renderer(renderForm.Handle, bounds))
                {
                    var store = new Store <StoreState, StoreState>(
                        new StoreState {
                        Clicked = false, Selection = new TextSelection[] { new TextSelection(2, 2) }
                    },
                        (state, action) => action);

                    var rootElement       = new ReduxProviderElement <StoreState, StoreState>(store, RootComponent.CreateElement(null));
                    var backgroundUpdater = new BackgroundUpdateContext(rootElement, renderer, null, bounds);
                    store.StateChanged   += (existing, newState) => backgroundUpdater.OnNextUpdate(new ChangeEvent <StoreState>(existing, newState));
                    eventSource.Mouse    += (ChangeEvent <MouseState> mouseEvent) => backgroundUpdater.OnNextUpdate(mouseEvent);
                    eventSource.Keyboard += (ChangeEvent <KeyboardState> keyboardEvent) => backgroundUpdater.OnNextUpdate(keyboardEvent);
                    renderForm.Resize    += (sender, _args) =>
                    {
                        var newbounds   = new Bounds(x: 0, y: 0, width: renderForm.ClientSize.Width, height: renderForm.ClientSize.Height);
                        var resizeEvent = new ChangeEvent <ResizeState>(new ResizeState(bounds.Width, bounds.Height), new ResizeState(newbounds.Width, newbounds.Height));
                        bounds = newbounds;
                        backgroundUpdater.OnNextUpdate(resizeEvent);
                    };

                    // Get the ball rolling
                    backgroundUpdater.OnNextUpdate(() => { });

                    RenderLoop.Run(renderForm, () =>
                    {
                        backgroundUpdater.EnsureRenderedOneFrame();
                    });
                }
            }
        }
Пример #2
0
        internal static void Initialize(RenderForm form)
        {
            _form       = form;
            _keyPressed = new HashSet <Key>();
            _keyUp      = new HashSet <Key>();
            _keyDown    = new HashSet <Key>();

            _mouseHeld = new HashSet <int>();
            _mouseDown = new HashSet <int>();
            _mouseUp   = new HashSet <int>();

            DirectInput directInput = new DirectInput();

            _keyboard = new Keyboard(directInput);
            //_keyboard.SetCooperativeLevel(form.Handle, CooperativeLevel.Exclusive | CooperativeLevel.Foreground );
            _keyboard.Properties.BufferSize = 128;

            _mouse = new Mouse(directInput);
            _mouse.Properties.AxisMode   = DeviceAxisMode.Relative;
            _mouse.Properties.BufferSize = 128;
            //_mouse.SetCooperativeLevel(form.Handle, CooperativeLevel.Foreground | CooperativeLevel.NonExclusive);

            form.MouseMove    += new MouseEventHandler(form_MouseMove);
            MousePositionPoint = form.PointToClient(Cursor.Position);
        }
Пример #3
0
        public void Update(double delta)
        {
            // Truncate mouse position to screen dimensions
            Point position = _form.PointToClient(Cursor.Position);

            _mousePosition = new Vector2I(position.X, position.Y);
            MouseMoved.Raise(_mousePosition);

            MouseButtons buttons = Control.MouseButtons;
            bool         leftMouseButtonPressed = (buttons == MouseButtons.Left);

            if (leftMouseButtonPressed && !_previousMouseButtons[0])
            {
                MouseClicked.Raise(_mousePosition, 0);
            }
            else if (_previousMouseButtons[0] && !leftMouseButtonPressed)
            {
                MouseReleased.Raise(_mousePosition, 0);
            }
            _previousMouseButtons[0] = leftMouseButtonPressed;

            bool rightMouseButtonPressed = (buttons == MouseButtons.Right);

            if (rightMouseButtonPressed && !_previousMouseButtons[1])
            {
                MouseClicked.Raise(_mousePosition, 1);
            }
            else if (_previousMouseButtons[1] && !rightMouseButtonPressed)
            {
                MouseReleased.Raise(_mousePosition, 1);
            }
            _previousMouseButtons[1] = rightMouseButtonPressed;


            foreach (Key key in _keys.Cast <Key>().Where(key => key != Key.None))
            {
                if (Keyboard.IsKeyDown(key))
                {
                    if (_pressedKeys.ContainsKey(key))
                    {
                        _pressedKeys[key] += delta;
                        if (_pressedKeys[key] > 0.1)
                        {
                            _pressedKeys[key] = 0.0;
                            KeyPressed.Raise(key, InputInterop.GetCharFromKey(key), true);
                        }
                    }
                    else
                    {
                        _pressedKeys.Add(key, -0.9);
                        KeyPressed.Raise(key, InputInterop.GetCharFromKey(key), false);
                    }
                }
                else
                {
                    if (_pressedKeys.ContainsKey(key))
                    {
                        _pressedKeys.Remove(key);
                        KeyReleased.Raise(key);
                    }
                }
            }
        }
Пример #4
0
        // Rotate based on FPSCamera.
        public void Update(ref DXInput di, ref FPSCamera cam, ref RenderForm form)
        {
            float t = sw.ElapsedMilliseconds / 1000.0f;

            viewProj = Matrix.Multiply(cam.GetViewMatrix(), proj);

            // Left mouse button
            if ((di.currMouseState.Buttons[0] == true) && (di.prevMouseState.Buttons[0] == true))
            {
                xRot += 0.0025f;
            }
            // Middle mouse button
            else if ((di.currMouseState.Buttons[2] == true) && (di.prevMouseState.Buttons[2] == true))
            {
                zRot += 0.0025f;
            }
            // Right mouse button
            else if ((di.currMouseState.Buttons[1] == true) && (di.prevMouseState.Buttons[1] == true))
            {
                yRot += 0.0025f;
            }

            float yawDelta   = 0.0f;
            float pitchDelta = 0.0f;

            float moveFactorV    = 0.001f;
            float moveFactorH    = 0.001f;
            float pitchYawFactor = 0.00005f;

            // Move up or down using Q and Z.
            if ((di.currKeyboardState.IsPressed(SDI.Key.Q)) && (di.prevKeyboardState.IsPressed(SDI.Key.Q)))
            {
                //cam.ApplyZoom(0.005f);
                cam.MoveVertically(moveFactorV);
            }
            else if ((di.currKeyboardState.IsPressed(SDI.Key.Z)) && (di.prevKeyboardState.IsPressed(SDI.Key.Z)))
            {
                //cam.ApplyZoom(-0.005f);
                cam.MoveVertically(-moveFactorV);
            }
            // Strafe left or right using A and D.
            if ((di.currKeyboardState.IsPressed(SDI.Key.A)) && (di.prevKeyboardState.IsPressed(SDI.Key.A)))
            {
                yawDelta = -0.001f;
                cam.Strafe(-moveFactorH);
            }
            else if ((di.currKeyboardState.IsPressed(SDI.Key.D)) && (di.prevKeyboardState.IsPressed(SDI.Key.D)))
            {
                yawDelta = 0.001f;
                cam.Strafe(moveFactorH);
            }
            // Move forward or backward using W and S.
            if ((di.currKeyboardState.IsPressed(SDI.Key.W)) && (di.prevKeyboardState.IsPressed(SDI.Key.W)))
            {
                pitchDelta = -0.001f;
                cam.MoveForward(moveFactorH);
            }
            else if ((di.currKeyboardState.IsPressed(SDI.Key.S)) && (di.prevKeyboardState.IsPressed(SDI.Key.S)))
            {
                pitchDelta = 0.001f;
                cam.MoveForward(-moveFactorH);
            }

            // Pitch the camera using I and K.
            if ((di.currKeyboardState.IsPressed(SDI.Key.I)) && (di.prevKeyboardState.IsPressed(SDI.Key.I)))
            {
                cam.Pitch(-pitchYawFactor);
            }
            else if ((di.currKeyboardState.IsPressed(SDI.Key.K)) && (di.prevKeyboardState.IsPressed(SDI.Key.K)))
            {
                cam.Pitch(pitchYawFactor);
            }
            // Yaw the camera using J and L.
            if ((di.currKeyboardState.IsPressed(SDI.Key.J)) && (di.prevKeyboardState.IsPressed(SDI.Key.J)))
            {
                cam.Yaw(-pitchYawFactor);
            }
            else if ((di.currKeyboardState.IsPressed(SDI.Key.L)) && (di.prevKeyboardState.IsPressed(SDI.Key.L)))
            {
                cam.Yaw(pitchYawFactor);
            }

            //cam.ApplyRotation(yawDelta, pitchDelta);


            // Limit camera position.
            float camX    = cam.getCamX();
            float camY    = cam.getCamY();
            float camZ    = cam.getCamZ();
            int   updated = 0;

            int te_size = te.getTerrainSize();

            for (int i = 0; i < te_size; i++)
            {
                for (int j = 0; j < te_size; j++)
                {
                    if (((int)vt[te_size * i + j].Position.X == (int)camX) &&
                        ((int)vt[te_size * i + j].Position.Z == (int)camZ))
                    {
                        if (camY < vt[te_size * i + j].Position.Y)
                        {
                            cam.setCamY(vt[te_size * i + j].Position.Y + 0.5f);
                        }

                        updated = 1;
                        break;
                    }
                }

                if (updated == 1)
                {
                    break;
                }
            }

            viewProj = Matrix.Multiply(cam.GetViewMatrix(), proj);

            var rot = Matrix.RotationX(xRot) * Matrix.RotationY(yRot) * Matrix.RotationZ(zRot);

            cBufferData.world = rot;
            var wvp = viewProj;// * rot;

            wvp.Transpose();
            cBufferData.wvp         = wvp;
            cBufferData.view        = cam.GetViewMatrix();
            cBufferData.modelViewIT = Matrix.Transpose(Matrix.Invert(cBufferData.world));
            devCon.UpdateSubresource(ref cBufferData, cBuffer);

            // Get mouse coordinates
            float xc = di.mouseX;
            float yc = di.mouseY;
            float zc = di.mouseZ;

            // Get the system cursor position.
            System.Drawing.Point p = new System.Drawing.Point(Cursor.Position.X, Cursor.Position.Y);
            //xc = p.X;
            //yc = p.Y;

            xc = form.PointToClient(p).X;
            yc = form.PointToClient(p).Y;
            //xc = (2.0f * xc / formWidth) - 1.0f;
            //yc = ((2.0f * yc / formHeight) - 1.0f) * -1.0f;


            //int minCursorX = form.DesktopLocation.X;
            //int minCursorY = form.DesktopLocation.Y;
            //int maxCursorX = form.DesktopBounds.Right;
            //int maxCursorY = form.DesktopBounds.Bottom;

            /*if(xc < minCursorX) { xc = minCursorX; }
            *  if(yc < minCursorY) { yc = minCursorY; }
            *  if(xc > maxCursorX) { xc = maxCursorX; }
            *  if(yc > maxCursorY) { yc = maxCursorY; }*/

            //System.Console.WriteLine("{0} {1} {2}", xc, yc, zc);
            //System.Console.WriteLine("{0} {1} {2} {3}", minCursorX, minCursorY, maxCursorX, maxCursorY);

            //System.Console.WriteLine("{0} {1}", xc, yc);

            bool rp = rayPick(new Vector2(xc, yc), bb);

            if (rp == true)
            {
                cBufferData.lightCol2 = new Vector4(1.3f, 1.4f, 1.45f, 1.0f);
            }
            else
            {
                cBufferData.lightCol2 = new Vector4(0.13f, 0.14f, 0.145f, 1.0f);
            }

            devCon.UpdateSubresource(ref cBufferData, cBuffer);
        }