public void Update(MouseComponent mouse, KeyboardComponent keyboard)
        {
            if (!mouse.IsMouseOwner(this))
            {
                return;
            }

            var deltaMouseX     = mouse.DeltaPosition().X;
            var deltaMouseY     = mouse.DeltaPosition().Y;
            var deltaMouseWheel = mouse.DeletaScrollWheel();

            if (keyboard.IsKeyReleased(Keys.F4))
            {
                Zoom    = 10;
                _lookAt = Vector3.Zero;
            }

            var ownsMouse = mouse.MouseOwner;

            if (keyboard.IsKeyDown(Keys.LeftAlt))
            {
                mouse.MouseOwner = this;
            }
            else
            {
                if (ownsMouse == this)
                {
                    mouse.MouseOwner = null;
                    mouse.ClearStates();
                    return;
                }
            }


            if (keyboard.IsKeyDown(Keys.LeftAlt))
            {
                mouse.MouseOwner = this;
                if (mouse.IsMouseButtonDown(MouseButton.Left))
                {
                    Yaw   += deltaMouseX * 0.01f;
                    Pitch += deltaMouseY * 0.01f;
                }
                if (mouse.IsMouseButtonDown(MouseButton.Right))
                {
                    MoveCameraRight(deltaMouseX * 0.01f * Zoom * .1f);
                    MoveCameraUp(-deltaMouseY * 0.01f * Zoom * .1f);
                }
                else if (deltaMouseWheel != 0)
                {
                    if (Math.Abs(deltaMouseWheel) > 250)   // Weird bug, sometimes this value is very large, probably related to state clearing. Temp fix
                    {
                        deltaMouseWheel = 250 * Math.Sign(deltaMouseWheel);
                    }

                    var oldZoom = (Zoom / 10);
                    Zoom += (deltaMouseWheel * 0.005f) * oldZoom;
                    //_logger.Here().Information($"Setting zoom {Zoom} - {deltaMouseWheel} - {oldZoom}");
                }
            }
        }
 public override void Initialize()
 {
     _graphicsDevice = Game.GraphicsDevice;
     _mouse          = GetComponent <MouseComponent>();
     _keyboard       = GetComponent <KeyboardComponent>();
     base.Initialize();
 }
        public void UpdateTriggersOnPressedCallback()
        {
            var system = new KeyboardSystem();

            // set up callback
            var timesCalled = 0;
            var component   = new KeyboardComponent();

            component.OnPress(Keys.F, () => timesCalled++);

            var e = new Cobalt.Ecs.CobaltEntity();

            e.Set(component);
            system.Add(e);

            // update: should be called
            this.pressedKeys = new[] { Keys.F };
            system.Update(1);
            Assert.That(timesCalled == 1);

            // update: shouldn't be called again
            system.Update(1);
            Assert.That(timesCalled == 1);

            // release: shouldn't be called
            this.pressedKeys = new Keys[0];
            system.Update(1);
            Assert.That(timesCalled == 1);

            // presss G instead; shouldn't be called
            this.pressedKeys = new[] { Keys.G };
            system.Update(1);
            Assert.That(timesCalled == 1);
        }
        /// <summary>
        /// A Player without network component
        /// </summary>
        /// <param name="model"></param>
        /// <param name="gamePadIndex"></param>
        /// <param name="transformPos"></param>
        /// <param name="cameraPos"></param>
        /// <param name="cameraAspectRatio"></param>
        /// <param name="followPlayer"></param>
        /// <param name="texture"></param>
        /// <returns></returns>
        public static Entity NewLocalPlayer(String model, int gamePadIndex, Vector3 transformPos, Vector3 cameraPos, float cameraAspectRatio, bool followPlayer, Texture2D texture)
        {
            Entity            player                    = NewBasePlayer(model, gamePadIndex, transformPos, texture, LOCAL_PLAYER);
            CameraComponent   cameraComponent           = new CameraComponent(player, cameraPos, cameraAspectRatio, followPlayer);
            KeyboardComponent keyboardComponent         = new KeyboardComponent(player);
            GamePadComponent  gamePadComponent          = new GamePadComponent(player, gamePadIndex);
            FPSComponent      fpsComponent              = new FPSComponent(player);
            UIComponent       spriteFPSCounterComponent = new UIComponent(player)
            {
                Position   = new Vector2(10, 10),
                Text       = fpsComponent.CurrentFramesPerSecond.ToString(),
                Color      = Color.White,
                SpriteFont = AssetManager.Instance.GetContent <SpriteFont>("menu")
            };
            NetworkDiagnosticComponent networkDiagnosticComponent = new NetworkDiagnosticComponent(player);

            ComponentManager.Instance.AddComponentToEntity(player, cameraComponent);
            ComponentManager.Instance.AddComponentToEntity(player, keyboardComponent);
            ComponentManager.Instance.AddComponentToEntity(player, gamePadComponent);
            ComponentManager.Instance.AddComponentToEntity(player, fpsComponent);
            ComponentManager.Instance.AddComponentToEntity(player, networkDiagnosticComponent);
            ComponentManager.Instance.AddComponentToEntity(player, spriteFPSCounterComponent);


            return(player);
        }
Exemple #5
0
        public TrueCraftGame(MultiplayerClient client, IPEndPoint endPoint)
        {
            Window.Title          = "TrueCraft";
            Content.RootDirectory = "Content";
            Graphics = new GraphicsDeviceManager(this);
            Graphics.SynchronizeWithVerticalRetrace = false;
            Graphics.IsFullScreen              = UserSettings.Local.IsFullscreen;
            Graphics.PreferredBackBufferWidth  = UserSettings.Local.WindowResolution.Width;
            Graphics.PreferredBackBufferHeight = UserSettings.Local.WindowResolution.Height;
            Client                   = client;
            EndPoint                 = endPoint;
            NextPhysicsUpdate        = DateTime.MinValue;
            ChunkMeshes              = new List <Mesh>();
            IncomingChunks           = new ConcurrentBag <Mesh>();
            PendingMainThreadActions = new ConcurrentBag <Action>();
            MouseCaptured            = true;

            var keyboardComponent = new KeyboardComponent(this);

            KeyboardComponent = keyboardComponent;
            Components.Add(keyboardComponent);

            var mouseComponent = new MouseComponent(this);

            MouseComponent = mouseComponent;
            Components.Add(mouseComponent);
        }
        public void Update(GameTime gameTime)
        {
            if (!Enabled)
            {
                return;
            }

            if (KeyboardComponent.KeyPressed(Keys.Down))
            {
                if (!MoveToNextHistoryCommand())
                {
                    AutoCompleteSelectedIndex = Math.Min(++AutoCompleteSelectedIndex, AutoCompleteVariation.Length - 1);
                }
            }

            if (KeyboardComponent.KeyPressed(Keys.Up))
            {
                if (AutoCompleteSelectedIndex > 0)
                {
                    AutoCompleteSelectedIndex = Math.Max(--AutoCompleteSelectedIndex, 0);
                }
                else
                {
                    MoveToPreviousHistoryCommand();
                }
            }

            TextEditor.Update(gameTime);
        }
        protected override void Initialize()
        {
            //Initialize keyboard input.
            keyboard = new KeyboardComponent(this);
            Components.Add(keyboard);

            //Call base class's initialization.
            base.Initialize();
        }
Exemple #8
0
        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                KeyboardComponent.Dispose();
                MouseComponent.Dispose();
            }

            base.Dispose(disposing);
        }
Exemple #9
0
        public ChatInterface(MultiplayerClient client, KeyboardComponent keyboard, FontRenderer font)
        {
            Client   = client;
            Keyboard = keyboard;
            Font     = font;

            Input        = string.Empty;
            Messages     = new List <ChatMessage>();
            DummyTexture = new Texture2D(keyboard.Game.GraphicsDevice, 1, 1);
            DummyTexture.SetData(new[] { Color.White });

            Client.ChatMessage += OnChatMessage;
            Keyboard.KeyDown   += OnKeyDown;
        }
Exemple #10
0
 public static bool IsPressed(ref KeyboardComponent component, PixelGlueButtons key)
 {
     if (UserKeybinds.GenericToKeybinds.TryGetValue(key, out var realKey))
     {
         for (int i = 0; i < component.OldButtons?.Count; i++)
         {
             if (component.OldButtons[i] == key)
             {
                 return(false);
             }
         }
         return(Keyboard.GetState().IsKeyDown(realKey.userBind) || Keyboard.GetState().IsKeyDown(realKey.defaultBind));
     }
     return(false);
 }
Exemple #11
0
        public override void Update(GameTime gameTime)
        {
            if (KeyboardComponent.KeyPressed(OpenKey))
            {
                Toggle();
            }

            if (!IsOpened)
            {
                return;
            }

            InputManager.Update(gameTime);
            RenderManager.Update(gameTime);
        }
Exemple #12
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="parent">The Screen that this console will be parented to</param>
        public ActorConsole(SadConsole.Console parent) : base(_actorConsoleWidth, _actorConsoleHeight)
        {
            _actorList   = new List <Actor>();
            _player      = new Player(1, 1);
            _mapScreen   = new MapScreen();
            _kbComponent = new KeyboardComponent(_player, ref _mapScreen);

            this.Components.Add(_kbComponent);
            _actorList.Add(_player);

            foreach (Actor act in _actorList)
            {
                this.Children.Add(act);
            }

            this.Parent = parent;
        }
        public override void Initialize()
        {
            UpdateOrder = (int)ComponentUpdateOrderEnum.SelectionComponent;
            DrawOrder   = (int)ComponentDrawOrderEnum.SelectionComponent;

            _mouseComponent    = GetComponent <MouseComponent>();
            _keyboardComponent = GetComponent <KeyboardComponent>();
            _camera            = GetComponent <ArcBallCamera>();
            _sceneManger       = GetComponent <SceneManager>();
            _selectionManager  = GetComponent <SelectionManager>();
            _commandManager    = GetComponent <CommandExecutor>();

            _spriteBatch = new SpriteBatch(GraphicsDevice);
            _textTexture = new Texture2D(GraphicsDevice, 1, 1);
            _textTexture.SetData(new Color[1 * 1] {
                Color.White
            });

            base.Initialize();
        }
        public override void Initialize()
        {
            _commandManager   = GetComponent <CommandExecutor>();
            _selectionManager = GetComponent <SelectionManager>();
            _keyboard         = GetComponent <KeyboardComponent>();
            _mouse            = GetComponent <MouseComponent>();
            var camera         = GetComponent <ArcBallCamera>();
            var resourceLibary = GetComponent <ResourceLibary>();

            _selectionManager.SelectionChanged += OnSelectionChanged;

            var font = resourceLibary.Content.Load <SpriteFont>("Fonts\\DefaultFont");

            _gizmo                 = new Gizmo(camera, _mouse, GraphicsDevice, new SpriteBatch(GraphicsDevice), font);
            _gizmo.ActivePivot     = PivotType.ObjectCenter;
            _gizmo.TranslateEvent += GizmoTranslateEvent;
            _gizmo.RotateEvent    += GizmoRotateEvent;
            _gizmo.ScaleEvent     += GizmoScaleEvent;
            _gizmo.StartEvent     += GizmoTransformStart;
            _gizmo.StopEvent      += GizmoTransformEnd;
        }
Exemple #15
0
        /*
         * Gets player keyboard input and takes appropriate action.
         */
        public void ParsePlayerInput(GameTime gameTime, Entity playerEntity)
        {
            VelocityComponent velocityComponent = componentManager.ConcurrentGetComponentOfEntity <VelocityComponent>(playerEntity);
            KeyboardComponent keyboardComponent = componentManager.ConcurrentGetComponentOfEntity <KeyboardComponent>(playerEntity);
            GamePadComponent  gamePadComponent  = componentManager.ConcurrentGetComponentOfEntity <GamePadComponent>(playerEntity);
            GravityComponent  gravityComponent  = componentManager.ConcurrentGetComponentOfEntity <GravityComponent>(playerEntity);

            /* Keyboard actions */
            if (keyboardComponent != null && velocityComponent != null)
            {
                KeyboardState state = Keyboard.GetState();

                if (state.IsKeyDown(Keys.Up) && !state.IsKeyDown(Keys.Down))
                {
                    PlayerActions.AcceleratePlayerForwards(gameTime, velocityComponent);
                }
                if (state.IsKeyDown(Keys.Down) && !state.IsKeyDown(Keys.Up))
                {
                    PlayerActions.AcceleratePlayerBackwards(gameTime, velocityComponent);
                }

                if (state.IsKeyDown(Keys.Left) && !state.IsKeyDown(Keys.Right))
                {
                    PlayerActions.AcceleratePlayerLeftwards(gameTime, velocityComponent);
                }
                if (state.IsKeyDown(Keys.Right) && !state.IsKeyDown(Keys.Left))
                {
                    PlayerActions.AcceleratePlayerRightwards(gameTime, velocityComponent);
                }
                if (state.IsKeyDown(Keys.Space))
                {
                    if (!gravityComponent.HasJumped)
                    {
                        PlayerActions.PlayerJump(gameTime, velocityComponent, playerEntity);
                        gravityComponent.HasJumped = true;
                    }
                }
            }
            #region
            /* Gamepad actions */
            if (gamePadComponent != null && velocityComponent != null)
            {
                GamePadState state = GamePad.GetState(gamePadComponent.Index);

                if (state.IsButtonDown(Buttons.A) && !state.IsButtonDown(Buttons.B))
                {
                    PlayerActions.AcceleratePlayerForwards(gameTime, velocityComponent);
                }
                if (state.IsButtonDown(Buttons.B) && !state.IsButtonDown(Buttons.A))
                {
                    PlayerActions.AcceleratePlayerBackwards(gameTime, velocityComponent);
                }
                if (state.IsButtonDown(Buttons.LeftThumbstickLeft) && !state.IsButtonDown(Buttons.LeftThumbstickRight))
                {
                    PlayerActions.AcceleratePlayerLeftwards(gameTime, velocityComponent);
                }
                if (state.IsButtonDown(Buttons.LeftThumbstickRight) && !state.IsButtonDown(Buttons.LeftThumbstickLeft))
                {
                    PlayerActions.AcceleratePlayerRightwards(gameTime, velocityComponent);
                }
                if (state.IsButtonDown(Buttons.Y) && !state.IsButtonDown(Buttons.A))
                {
                    if (!gravityComponent.HasJumped)
                    {
                        PlayerActions.PlayerJump(gameTime, velocityComponent, playerEntity);
                        gravityComponent.HasJumped = true;
                    }
                }
            }
            #endregion
        }