NeoforceControls / Neoforce-Mono

MonoGame Version Currently 3.2
33 stars 6 forks source link

keyboard input #5

Open PedroAlvesq opened 10 years ago

PedroAlvesq commented 10 years ago

how i put my console when is use don´t move the my character your opens a new windows this is my code i using only the sharpdx

using System;
using SharpDX;
using SharpDX.Toolkit;
using System.Collections.Generic;
using SharpDX.Toolkit.Input;
using TomShane.Neoforce.Controls;
using SharpDX.Direct3D;
using SharpDX.Direct3D11;
using SharpDX.Toolkit.Content;
using SharpDX.Toolkit.Graphics;
using System.Text;

namespace TomShane.Neoforce.Central
{
    internal sealed class World : GameSystem
    {
        private Manager NeoManager;
        string texto;
        private SpriteBatch spriteBatch;
        private SpriteFont arial16BMFont;
        private PointerManager pointer;
        private Model model;
        private List<Model> models;
        private BoundingSphere modelBounds;
        private Matrix world;
        private Matrix view;
        private Matrix projection;
        private Window inventory;
        private Window Charactersstatus;

        private TomShane.Neoforce.Controls.TabControl tbc;
        public World(Game game, Manager ola, string text)
            : base(game)
        {

            NeoManager = ola;
            texto = text;
            System.Console.Write(texto);
            pointer = new PointerManager(game);

            // this game system has something to draw - enable drawing by default
            // this can be disabled to make objects drawn by this system disappear
            Visible = true;

            // this game system has logic that needs to be updated - enable update by default
            // this can be disabled to simulate a "pause" in logic update
            Enabled = true;

            // add the system itself to the systems list, so that it will get initialized and processed properly
            // this can be done after game initialization - the Game class supports adding and removing of game systems dynamically
            Game.GameSystems.Add(this);

        }

        /// <summary>
        /// Initialize here anything that depends on other services
        /// </summary>
        public override void Initialize()
        {

            base.Initialize();

            // get the camera service from service registry

        }
        private void HandleInput()
        {
            KeyboardState currentKeyboardState = NeoManager.KeyboardManager.GetState();

            // Check for exit.

            // Now move the sphere. First, we want to check to see if the sphere should
            // turn. turnAmount will be an accumulation of all the different possible
            // inputs.
            if (currentKeyboardState.IsKeyDown(Keys.A) ||
                currentKeyboardState.IsKeyDown(Keys.Left))
            {
               // turnAmount += 1;
            }
            if (currentKeyboardState.IsKeyDown(Keys.D) ||
                currentKeyboardState.IsKeyDown(Keys.Right))
            {
               // turnAmount -= 1;
            }

            // clamp the turn amount between -1 and 1, and then use the finished
            // value to turn the sphere.

            // Next, we want to move the sphere forward or back. to do this, 
            // we'll create a Vector3 and modify use the user's input to modify the Z
            // component, which corresponds to the forward direction.

            if (currentKeyboardState.IsKeyDown(Keys.W) ||
                currentKeyboardState.IsKeyDown(Keys.Up))

            {

            }

            if (currentKeyboardState.IsKeyDown(Keys.S) ||
                currentKeyboardState.IsKeyDown(Keys.Down))
            {

            }
            if (currentKeyboardState.IsKeyPressed(Keys.I))
            {
                inventory.Show();

            }
            if (currentKeyboardState.IsKeyDown(Keys.B))
            {
                Charactersstatus.Show();

            }
          /*  if (currentKeyboardState.IsKeyDown(Keys.Escape))
             {
                 if(tbc.Focused==true){
                 tbc.Visible = false;
                 }
                 else
                 {
                    tbc.Show();
                 }

               //  InitConsole(text).Show();
                 //windows();

             }
            */
            // next, we'll create a rotation matrix from the sphereFacingDirection, and
            // use it to transform the vector. If we didn't do this, pressing "up" would
            // always move the ball along +Z. By transforming it, we can move in the
            // direction the sphere is "facing."

            // Now we know how much the user wants to move. We'll construct a temporary
            // vector, newSpherePosition, which will represent where the user wants to
            // go. If that value is on the heightmap, we'll allow the move.

            // now we need to roll the ball "forward." to do this, we first calculate
            // how far it has moved.

            // once we've finished all computations, we can set spherePosition to the
            // new position that we calculated.

        }
        private void Inventory()
        {
            inventory = new Window(NeoManager);
            inventory.Init();
            inventory.Text = "Inventory";
            inventory.Width = 450;
            inventory.Height = 250;
            inventory.Center();
            inventory.Visible = true;
            inventory.CloseButtonVisible = true;
            inventory.Resizable = false;
            inventory.Movable = true;
            inventory.IconVisible = false;
            inventory.Close();
            NeoManager.Add(inventory);

        }
        private void charactersstatus()
        {
            Charactersstatus = new Window(NeoManager);
            Charactersstatus.Init();
            Charactersstatus.Text = "Characters Status";
            Charactersstatus.Width = 450;
            Charactersstatus.Height = 250;
            Charactersstatus.Center();
            Charactersstatus.Visible = true;
            Charactersstatus.CloseButtonVisible = true;
            Charactersstatus.Resizable = false;
            Charactersstatus.Movable = true;
            Charactersstatus.Close();
            Charactersstatus.IconVisible = false;
            NeoManager.Add(Charactersstatus);

        }
        /// <summary>
        /// Load all graphics content here.
        /// </summary>
        protected override void LoadContent()
        {

            arial16BMFont = Content.Load<SpriteFont>("Arial16");

            // Load the model (by default the model is loaded with a BasicEffect. Use ModelContentReaderOptions to change the behavior at loading time.
            models = new List<Model>();
            foreach (var modelName in new[] { "Dude", "Duck", "Car", "Happy", "Knot", "Skull", "Sphere", "Teapot", "helmet" })
            {
                model = Content.Load<Model>(modelName);

                // Enable default lighting  on model.
                BasicEffect.EnableDefaultLighting(model, true);

                models.Add(model);
            }
            model = models[0];

            // Instantiate a SpriteBatch
            spriteBatch = ToDisposeContent(new SpriteBatch(GraphicsDevice));

            Inventory();
            charactersstatus();
            InitConsole(texto);
            base.LoadContent();

            // InitRes();
            // initialize the basic effect (shader) to draw the geometry, the BasicEffect class is similar to one from XNA

        }

        /// <summary>
        /// Draw the scene content.
        /// </summary>
        /// <param name="gameTime">Structure containing information about elapsed game time.</param>

        /// <summary>
        /// Update the scene logic.
        /// </summary>
        /// <param name="gameTime">Structure containing information about elapsed game time.</param>
        public override void Update(GameTime gameTime)
        {
            var pointerState = pointer.GetState();
            if (pointerState.Points.Count > 0 && pointerState.Points[0].EventType == PointerEventType.Released)
            {
                // Go to next model when pressing key space
                model = models[(models.IndexOf(model) + 1) % models.Count];
            }

            // Calculate the bounds of this model
            modelBounds = model.CalculateBounds();

            // Calculates the world and the view based on the model size
            const float MaxModelSize = 10.0f;
            var scaling = MaxModelSize / modelBounds.Radius;
            view = Matrix.LookAtRH(new Vector3(0, 0, MaxModelSize * 2.5f), new Vector3(0, 0, 0), Vector3.UnitY);
            projection = Matrix.PerspectiveFovRH(0.9f, (float)GraphicsDevice.BackBuffer.Width / GraphicsDevice.BackBuffer.Height, 0.1f, MaxModelSize * 10.0f);
            world = Matrix.Translation(-modelBounds.Center.X, -modelBounds.Center.Y, -modelBounds.Center.Z) * Matrix.Scaling(scaling) * Matrix.RotationY((float)gameTime.TotalGameTime.TotalSeconds);

            //   NeoManager.Update(gameTime);
            HandleInput();
            base.Update(gameTime);
            //  NeoManager.Update(gameTime);

            // get the total elapsed seconds since the start of the game

        }

        public override void Draw(GameTime gameTime)
        {

            NeoManager.BeginDraw(gameTime);
            GraphicsDevice.Clear(SharpDX.Color.CornflowerBlue);
            model.Draw(GraphicsDevice, world, view, projection);

            /////GraphicsDevice.DepthStencilState = DepthStencilState.Default;
            ////  device.Clear();
            //// // Render the text
            spriteBatch.Begin();

            spriteBatch.DrawString(arial16BMFont, "Press the pointer to switch models...\r\nCurrent Model: " + model.Name, new Vector2(16, 16), Color.White);
            ////GraphicsDevice.Clear(Color.AntiqueWhite);

            //// GraphicsDevice.SamplerStates.Default;
            spriteBatch.End();

            //    GraphicsDevice.ClearState();

            NeoManager.EndDraw();

            base.Draw(gameTime);

            //    GraphicsDevice.SamplerStates[0] ola= SamplerState.LinearWrap;

            // set the parameters for cube drawing and draw it using the basic effect

        }

        private void InitConsole(string texto)
        {
            tbc = new TomShane.Neoforce.Controls.TabControl(NeoManager);
            //   tbc.BackColor = new Color(10, 12, 3);
            TomShane.Neoforce.Controls.Console con1 = new TomShane.Neoforce.Controls.Console(NeoManager);
            TomShane.Neoforce.Controls.Console con2 = new TomShane.Neoforce.Controls.Console(NeoManager);

            tbc.Visible = true;
            //  con2.BackColor
            // Setup of TabControl, which will be holding both consoles
            tbc.Init();
            tbc.AddPage("Global");
            tbc.AddPage("Guild");
            tbc.AddPage("PARTY");
            tbc.AddPage("TRADE");

            tbc.Alpha = 220;
            tbc.Left = 0;
            tbc.Height = 220;
            tbc.Width = 450;
            tbc.Top = NeoManager.TargetHeight - tbc.Height - 32;

            tbc.Movable = true;
            tbc.Resizable = true;
            tbc.MinimumHeight = 96;
            tbc.MinimumWidth = 160;

            tbc.TabPages[0].Add(con1);
            tbc.TabPages[1].Add(con2);
            NeoManager.Add(tbc);
            //tbc.StayOnTop = true;
            tbc.TabPages[0].TextColor = new Color(12, 12, 12);
            tbc.TabPages[1].TextColor = new Color(250, 250, 250);
            con1.Init();
            con1.Sender = texto;
            con2.Init();
            con2.Sender = texto;

            con2.Width = con1.Width = tbc.TabPages[0].ClientWidth;
            con2.Height = con1.Height = tbc.TabPages[0].ClientHeight;
            con2.Anchor = con1.Anchor = Anchors.All;

            con1.Channels.Add(new ConsoleChannel(0, "General", SharpDX.Color.Orange));
            con1.Channels.Add(new ConsoleChannel(1, "Private", SharpDX.Color.White));
            con1.Channels.Add(new ConsoleChannel(2, "System", SharpDX.Color.Yellow));
            con1.Channels.Add(new ConsoleChannel(3, "Guild", SharpDX.Color.Green));
            con1.Channels.Add(new ConsoleChannel(4, "Trade", SharpDX.Color.Red));

            // We want to share channels and message buffer in both consoles
            con2.Channels = con1.Channels;
            con2.MessageBuffer = con1.MessageBuffer;

            // In the second console we display only "Private" messages
            con2.ChannelFilter.Add(3);

            // Select default channels for each tab
            con1.SelectedChannel = 0;
            con2.SelectedChannel = 3;

            // Do we want to add timestamp or channel name at the start of every message?
            con1.MessageFormat = ConsoleMessageFormats.All;
            con2.MessageFormat = ConsoleMessageFormats.All;

            // Handler for altering incoming message
            con1.MessageSent += new ConsoleMessageEventHandler(con1_MessageSent);

            // We send initial welcome message to System channel
            con1.MessageBuffer.Add(new ConsoleMessage("System", "WELCOME TO THE SERVER! " + texto, 2));

            NeoManager.Add(tbc);
        }
        ////////////////////////////////////////////////////////////////////////////

        ////////////////////////////////////////////////////////////////////////////
        void con1_MessageSent(object sender, ConsoleMessageEventArgs e)
        {
            if (e.Message.Channel == 0)
            {
                //e.Message.Text = "(!) " + e.Message.Text;
            }
        }
    }
}
Grimston commented 10 years ago

You might be able to do that, by checking if the Console is focused or not.

Example logic.

if (Console.Focused == false) {
   //Allow Character movement
}

Or you can toggle the state of the TextBox on the Console with the property TextBoxVisible and do the same checks.

PedroAlvesq commented 10 years ago

i put like this and don´t work what is wrong

 public override void Update(GameTime gameTime)
        {
            var pointerState = pointer.GetState();
            if (pointerState.Points.Count > 0 && pointerState.Points[0].EventType == PointerEventType.Released)
            {
                // Go to next model when pressing key space
                model = models[(models.IndexOf(model) + 1) % models.Count];
            }

            // Calculate the bounds of this model
            modelBounds = model.CalculateBounds();

            // Calculates the world and the view based on the model size
            const float MaxModelSize = 10.0f;
            var scaling = MaxModelSize / modelBounds.Radius;
            view = Matrix.LookAtRH(new Vector3(0, 0, MaxModelSize * 2.5f), new Vector3(0, 0, 0), Vector3.UnitY);
            projection = Matrix.PerspectiveFovRH(0.9f, (float)GraphicsDevice.BackBuffer.Width / GraphicsDevice.BackBuffer.Height, 0.1f, MaxModelSize * 10.0f);
            world = Matrix.Translation(-modelBounds.Center.X, -modelBounds.Center.Y, -modelBounds.Center.Z) * Matrix.Scaling(scaling) * Matrix.RotationY((float)gameTime.TotalGameTime.TotalSeconds);

            if (con1.TextBoxVisible == false) { 
                HandleInput(); 

            }
            //   NeoManager.Update(gameTime);

            base.Update(gameTime);
            //  NeoManager.Update(gameTime);

            // get the total elapsed seconds since the start of the game

        }
Grimston commented 10 years ago
if (con1.TextBoxVisible == false) { HandleInput(); }

I'm guessing your now unable to press anything if the console has no focus.

You should still run HandleInput but move the player controls to another method and inside HandleInput check the console and execute player input when the console is not focused.

For example

private void HandleInput() {
    if(!console.TextBoxVisible) {
        HandlePlayerInput();
    }
    else {
        //Focused Input
    }

}

private void HandlePlayerInput();
PedroAlvesq commented 10 years ago

i put like this but don´t work don´t show the new windows when i using the console but when is not focus i can use it

 TomShane.Neoforce.Controls.Console con1;
  private void HandleInput()
        {
            if (!con1.TextBoxVisible)
            {
                HandlePlayerInput();
            }
            else
            {
                con1.Focused = true;

                //Focused Input
            }

        }
 public override void Update(GameTime gameTime)
        {
 HandleInput();    
   base.Update(gameTime);

        }