Esempio n. 1
0
        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>

        protected override void Update(GameTime gameTime)
        {
            if (isLoading)
            {
                if (!loadThread.IsAlive)
                {
                    UI.InitUI();
                    isLoading = false;
                    Logic.bye.Play();
                    Animation logoAnimation = new Animation(() =>
                    {
                        Logic.images.Remove("LOGO");
                        UI.EnableGroup(Logic.menuNamespace);
                        UI.DisableGroup(Logic.menuNamespace + "Z");
                        UI.GenerateFadeIn(1.0f);
                        Logic.menuMusicInstance          = Logic.menuMusic.CreateInstance();
                        Logic.menuMusicInstance.IsLooped = true;
                        Logic.menuMusicInstance.Volume   = Settings.volume;
                        Logic.menuMusicInstance.Play();
                    });
                    logoAnimation.AddFadeInOut(2.0f, 5.0f, 2.0f, Logic.images["LOGO"]);
                    Logic.animations.Add(logoAnimation);
                }
                return;
            }


            if (this.IsActive)
            {
                mouseState    = Mouse.GetState();
                keyboardState = Keyboard.GetState();
                pressedKeys   = keyboardState.GetPressedKeys();
                Vector2 mousePos = new Vector2(mouseState.X / scale.X, mouseState.Y / scale.Y);

                for (int i = Logic.animations.Count - 1; i >= 0; i--)
                {
                    if (Logic.animations[i].Update((float)gameTime.ElapsedGameTime.TotalSeconds))
                    {
                        Logic.animations.RemoveAt(i);
                    }
                }

                List <string> UIelements = new List <string>();

                foreach (KeyValuePair <string, Button> pair in Logic.buttons)
                {
                    if (pair.Value.enabled)
                    {
                        UIelements.Add(pair.Key);
                    }
                }

                foreach (KeyValuePair <string, TextBox> pair in Logic.textBoxes)
                {
                    if (pair.Value.enabled)
                    {
                        UIelements.Add(pair.Key);
                    }
                }

                foreach (KeyValuePair <string, InputBox> pair in Logic.inputBoxes)
                {
                    if (pair.Value.enabled)
                    {
                        UIelements.Add(pair.Key);
                    }
                }

                foreach (KeyValuePair <string, Grid> pair in Logic.grids)
                {
                    if (pair.Value.enabled)
                    {
                        UIelements.Add(pair.Key);
                    }
                }

                UIelements.Sort();

                //Left click
                if (mouseState.LeftButton == ButtonState.Pressed && mouseBeforeState.LeftButton == ButtonState.Released)
                {
                    int i;
                    for (i = UIelements.Count - 1; i >= 0; i--)
                    {
                        string name = UIelements[i];
                        //Klikanie buttonów
                        if (Logic.buttons.ContainsKey(name))
                        {
                            if (Geo.RectContains(Logic.buttons[name].location, mousePos) && Logic.buttons[name].enabled && Logic.buttons[name].registerClicks)
                            {
                                if (activeInputBox != null)
                                {
                                    activeInputBox.active = false;
                                }
                                activeInputBox = null;
                                if (Logic.buttons[name].clickEvent != null)
                                {
                                    Logic.buttons[name].clickEvent();
                                }
                                i = -1;
                            }
                        }

                        if (Logic.textBoxes.ContainsKey(name))
                        {
                            if (Geo.RectContains(Logic.textBoxes[name].location, mousePos) && Logic.textBoxes[name].enabled && Logic.textBoxes[name].registerClicks)
                            {
                                if (activeInputBox != null)
                                {
                                    activeInputBox.active = false;
                                }
                                activeInputBox = null;
                                i = -1;
                            }
                        }

                        if (Logic.inputBoxes.ContainsKey(name))
                        {
                            if (Geo.RectContains(Logic.inputBoxes[name].location, mousePos) && Logic.inputBoxes[name].enabled)
                            {
                                if (activeInputBox != null)
                                {
                                    activeInputBox.active = false;
                                }
                                activeInputBox        = Logic.inputBoxes[name];
                                activeInputBox.active = true;
                                i = -2;
                            }
                        }

                        if (Logic.grids.ContainsKey(name))
                        {
                            Grid grid = Logic.grids[name];
                            if (Geo.RectContains(Geo.Shrink(grid.location, grid.margin), mousePos) && grid.enabled)
                            {
                                if (activeInputBox != null)
                                {
                                    activeInputBox.active = false;
                                }
                                activeInputBox = null;
                                int pressX = (int)((mousePos.X - grid.location.X - grid.margin - grid.location.Width / 2 + grid.margin) / grid.zoom + grid.offset.X);
                                int pressY = (int)((mousePos.Y - grid.location.Y - grid.margin - grid.location.Height / 2 + grid.margin) / grid.zoom + grid.offset.Y);
                                int tileX  = pressX / (int)grid.fieldSize.X;
                                int tileY  = pressY / (int)grid.fieldSize.Y;
                                if (grid.clickEvent != null && tileX >= 0 && tileX < grid.sizeX && tileY >= 0 && tileY < grid.sizeY)
                                {
                                    grid.clickEvent(tileX, tileY);
                                }
                                i = -1;
                            }
                        }
                    }

                    if (i == -1)
                    {
                        if (activeInputBox != null)
                        {
                            activeInputBox.active = false;
                            activeInputBox        = null;
                        }
                    }
                }

                //Right click
                if (mouseState.RightButton == ButtonState.Pressed)
                {
                    if (mouseBeforeState.RightButton == ButtonState.Released)
                    {
                        for (int i = Logic.grids.Count - 1; i >= 0; i--)
                        {
                            Grid grid = Logic.grids.ElementAt(i).Value;
                            if (Geo.RectContains(Geo.Shrink(grid.location, grid.margin), mousePos) && grid.enabled && !grid.useScrollToScroll)
                            {
                                draggedGrid = grid;
                            }
                        }
                    }
                    else
                    {
                        if (draggedGrid != null)
                        {
                            draggedGrid.offset -= new Vector2(mouseState.X / scale.X - mouseBeforeState.X / scale.X, mouseState.Y / scale.Y - mouseBeforeState.Y / scale.Y) / draggedGrid.zoom;
                        }
                    }
                }
                else
                {
                    draggedGrid = null;
                }

                //Konwersja klawiszy do inputBoxa
                if (activeInputBox != null)
                {
                    if (keyboardState.IsKeyDown(Keys.LeftControl))
                    {
                        if (keyboardState.IsKeyDown(Keys.V) && keyboardBeforeState.IsKeyUp(Keys.V))
                        {
                            string sOut = Encoding.ASCII.GetString(Encoding.ASCII.GetBytes(GetClipboard()));
                            activeInputBox.Append(sOut);
                        }
                    }
                    else if (keyboardState.IsKeyDown(Keys.Delete))
                    {
                        activeInputBox.Clear();
                    }
                    else if (pressedKeys.Length > 0)
                    {
                        foreach (Keys key in pressedKeys)
                        {
                            char charkey;
                            if (keyboardBeforeState.IsKeyUp(key) &&
                                TryConvertKeys(key, out charkey, keyboardState.IsKeyDown(Keys.LeftShift) || keyboardState.IsKeyDown(Keys.RightShift)))
                            {
                                activeInputBox.Append(charkey);
                            }
                        }
                    }
                }


                //Backspace
                if (keyboardState.IsKeyDown(Keys.Back) && activeInputBox != null)
                {
                    if (backspaceHeld == false)
                    {
                        if (activeInputBox.text.Length > 0)
                        {
                            activeInputBox.text = activeInputBox.text.Remove(activeInputBox.text.Length - 1);
                        }
                        backspaceStart = gameTime.TotalGameTime.TotalMilliseconds;
                    }
                    if (backspaceHeld && gameTime.TotalGameTime.TotalMilliseconds - backspaceStart > 500 && gameTime.TotalGameTime.TotalMilliseconds - backspaceTimer > 25)
                    {
                        backspaceTimer = gameTime.TotalGameTime.TotalMilliseconds;
                        if (activeInputBox.text.Length > 0)
                        {
                            activeInputBox.text = activeInputBox.text.Remove(activeInputBox.text.Length - 1);
                        }
                    }
                    backspaceHeld = true;
                }
                else
                {
                    backspaceHeld = false;
                }

                //Scroll
                int scrollWheelDelta = mouseState.ScrollWheelValue - mouseBeforeState.ScrollWheelValue;
                if (scrollWheelDelta != 0)
                {
                    int i;
                    for (i = UIelements.Count - 1; i >= 0; i--)
                    {
                        string name = UIelements[i];
                        if (Logic.buttons.ContainsKey(name))
                        {
                            Button button = Logic.buttons[name];
                            if (Geo.RectContains(button.location, mousePos) && button.enabled)
                            {
                                i = -2;
                            }
                        }

                        if (Logic.inputBoxes.ContainsKey(name))
                        {
                            InputBox inputBox = Logic.inputBoxes[name];
                            if (Geo.RectContains(inputBox.location, mousePos) && inputBox.enabled)
                            {
                                i = -2;
                            }
                        }

                        if (Logic.textBoxes.ContainsKey(name))
                        {
                            TextBox textBox = Logic.textBoxes[name];
                            if (Geo.RectContains(textBox.location, mousePos) && textBox.enabled)
                            {
                                if (textBox.canScroll)
                                {
                                    if (scrollWheelDelta < 0)
                                    {
                                        textBox.scroll++;
                                    }
                                    else if (textBox.scroll > 0)
                                    {
                                        textBox.scroll--;
                                    }

                                    if (textBox.lines.Count - textBox.scroll < textBox.lineCount)
                                    {
                                        textBox.scroll = textBox.lines.Count - textBox.lineCount;
                                    }

                                    if (textBox.scroll < 0)
                                    {
                                        textBox.scroll = 0;
                                    }
                                }

                                i = -2;
                            }
                        }

                        if (Logic.grids.ContainsKey(name))
                        {
                            Grid grid = Logic.grids[name];
                            if (Geo.RectContains(grid.location, mousePos) && grid.enabled)
                            {
                                if (grid.useScrollToScroll)
                                {
                                    if (scrollWheelDelta < 0)
                                    {
                                        grid.offset.Y += 25.0f;
                                    }
                                    else if (scrollWheelDelta > 0)
                                    {
                                        grid.offset.Y -= 25.0f;
                                    }
                                }
                                else
                                {
                                    if (scrollWheelDelta < 0 && grid.zoom > 0.5f)
                                    {
                                        grid.zoom -= 0.1f;
                                    }
                                    else if (scrollWheelDelta > 0 && grid.zoom < 2.0f)
                                    {
                                        grid.zoom += 0.1f;
                                    }
                                }
                                i = -2;
                            }
                        }
                    }
                }
            }

            //Wy³¹czanie nieaktywnych obiektów
            if (activeInputBox != null && !activeInputBox.enabled)
            {
                activeInputBox = null;
            }
            if (draggedGrid != null && !draggedGrid.enabled)
            {
                draggedGrid = null;
            }

            //Cofanie gdy przejedziesz grida za bardzo
            foreach (KeyValuePair <string, Grid> gridpair in Logic.grids)
            {
                Grid grid    = gridpair.Value;
                int  marginX = (int)((grid.location.Width / 2 - grid.margin) / grid.zoom);
                int  marginY = (int)((grid.location.Height / 2 - grid.margin) / grid.zoom);
                if (grid.offset.X < marginX)
                {
                    grid.offset.X = Math.Min(grid.offset.X + 25, marginX);
                }
                if (grid.offset.Y < marginY)
                {
                    grid.offset.Y = Math.Min(grid.offset.Y + 25, marginY);
                }
                if (grid.offset.X > grid.sizeX * grid.fieldSize.X - marginX)
                {
                    grid.offset.X = Math.Max(grid.offset.X - 25, grid.sizeX * grid.fieldSize.X - marginX);
                }
                if (grid.offset.Y > grid.sizeY * grid.fieldSize.Y - marginY)
                {
                    grid.offset.Y = Math.Max(grid.offset.Y - 25, grid.sizeY * grid.fieldSize.Y - marginY);
                }
            }

            Logic.Update();

            mouseBeforeState    = mouseState;
            keyboardBeforeState = keyboardState;

            base.Update(gameTime);
        }
Esempio n. 2
0
        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>

        protected override void Draw(GameTime gameTime)
        {
            framesTimeSinceCheck += (float)gameTime.ElapsedGameTime.TotalSeconds;
            frames++;
            if (framesTimeSinceCheck >= 1.0f)
            {
                fps    = frames;
                frames = 0;
                framesTimeSinceCheck = 0.0f;
            }

            //Loading
            if (isLoading)
            {
                GraphicsDevice.Clear(Color.Black);
                spriteBatch.Begin();
                spriteBatch.DrawString(Logic.guifont, loadingString, (new Vector2(Settings.resolution.X, Settings.resolution.Y) - Logic.guifont.MeasureString(loadingString)) / 2, Color.White);
                spriteBatch.End();
                return;
            }

            //Rysowanie gridów
            foreach (KeyValuePair <string, Grid> gridpair in Logic.grids)
            {
                Grid grid = gridpair.Value;
                if (grid.enabled)
                {
                    spriteBatch.Begin();
                    GraphicsDevice.SetRenderTarget(grid.renderTarget);
                    GraphicsDevice.Clear(Color.Transparent);
                    for (int x = 0; x < grid.sizeX; x++)
                    {
                        for (int y = 0; y < grid.sizeY; y++)
                        {
                            Rectangle targetRect = new Rectangle(x * (int)grid.fieldSize.X - (int)grid.offset.X, y * (int)grid.fieldSize.Y - (int)grid.offset.Y, (int)grid.fieldSize.X, (int)grid.fieldSize.Y);
                            targetRect.X      = (int)(grid.zoom * targetRect.X);
                            targetRect.Y      = (int)(grid.zoom * targetRect.Y);
                            targetRect.Width  = (int)(grid.zoom * targetRect.Width) + 1;
                            targetRect.Height = (int)(grid.zoom * targetRect.Height) + 1;
                            targetRect.X     += grid.location.Width / 2 - grid.margin;
                            targetRect.Y     += grid.location.Height / 2 - grid.margin;
                            if (targetRect.Intersects(new Rectangle(0, 0, grid.location.Width, grid.location.Height)))
                            {
                                grid.drawEvent(spriteBatch, targetRect, x, y);
                            }
                        }
                    }
                    spriteBatch.End();
                }
            }

            GraphicsDevice.SetRenderTarget(null);
            GraphicsDevice.Clear(Color.Black);

            spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend);

            List <string> UIelements = new List <string>();

            foreach (KeyValuePair <string, Button> pair in Logic.buttons)
            {
                if (pair.Value.enabled)
                {
                    UIelements.Add(pair.Key);
                }
            }

            foreach (KeyValuePair <string, TextBox> pair in Logic.textBoxes)
            {
                if (pair.Value.enabled)
                {
                    UIelements.Add(pair.Key);
                }
            }

            foreach (KeyValuePair <string, InputBox> pair in Logic.inputBoxes)
            {
                if (pair.Value.enabled)
                {
                    UIelements.Add(pair.Key);
                }
            }

            foreach (KeyValuePair <string, Grid> pair in Logic.grids)
            {
                if (pair.Value.enabled)
                {
                    UIelements.Add(pair.Key);
                }
            }

            foreach (KeyValuePair <string, RawImage> pair in Logic.images)
            {
                if (pair.Value.enabled)
                {
                    UIelements.Add(pair.Key);
                }
            }

            UIelements.Sort();

            for (int id = 0; id < UIelements.Count; id++)
            {
                string name = UIelements[id];

                //Rysowanie guzików
                if (Logic.buttons.ContainsKey(name))
                {
                    Button button = Logic.buttons[name];
                    if (button.texture != null)
                    {
                        spriteBatch.Draw(button.texture, Geo.Scale(button.location), Color.White);
                    }
                }

                //Rysowanie textBoxów
                if (Logic.textBoxes.ContainsKey(name))
                {
                    TextBox textBox = Logic.textBoxes[name];
                    if (textBox.texture != null)
                    {
                        spriteBatch.Draw(textBox.texture, Geo.Scale(textBox.location), Color.White);
                    }
                    for (int i = textBox.scroll; i < textBox.lines.Count && i < textBox.lineCount + textBox.scroll; i++)
                    {
                        float _x = 0;

                        if (textBox.align == Alignment.Left)
                        {
                            _x = textBox.location.X + textBox.margin;
                        }
                        else if (textBox.align == Alignment.Centered)
                        {
                            _x = textBox.location.X + (textBox.location.Width - textBox.font.MeasureString(textBox.lines[i]).X) / 2;
                        }
                        _x = (int)_x;

                        Vector2 position = new Vector2(_x, textBox.location.Y + textBox.font.LineSpacing * (i - textBox.scroll) + textBox.margin);

                        spriteBatch.DrawString(textBox.font, textBox.lines[i], position * scale, textBox.colors[i], 0.0f, Vector2.Zero, scale, SpriteEffects.None, 0);
                    }
                }

                //Rysowanie inputBoxów
                if (Logic.inputBoxes.ContainsKey(name))
                {
                    InputBox inputBox = Logic.inputBoxes[name];
                    if (inputBox.texture != null)
                    {
                        spriteBatch.Draw(inputBox.texture, Geo.Scale(inputBox.location), inputBox.active ? Color.Gray : Color.White);
                    }
                    string text = inputBox.GetText();
                    if (inputBox.active)
                    {
                        text += "|";
                    }
                    Vector2 position = new Vector2(inputBox.location.X + inputBox.margin, inputBox.location.Y + inputBox.location.Height / 2 - inputBox.font.LineSpacing / 2);

                    if (inputBox.text != "")
                    {
                        spriteBatch.DrawString(inputBox.font, text, position * scale, inputBox.color, 0.0f, Vector2.Zero, scale, SpriteEffects.None, 0);
                    }
                    else
                    {
                        spriteBatch.DrawString(inputBox.font, inputBox.emptyText, position * scale, inputBox.emptyColor, 0.0f, Vector2.Zero, scale, SpriteEffects.None, 0);
                    }
                }

                //Rysowanie gridów
                if (Logic.grids.ContainsKey(name))
                {
                    Grid grid = Logic.grids[name];
                    if (grid.boxTexture != null)
                    {
                        spriteBatch.Draw(grid.boxTexture, Geo.Scale(grid.location), Color.White);
                    }
                    spriteBatch.Draw(grid.renderTarget, Geo.Scale(Geo.Shrink(grid.location, grid.margin)), Color.White);
                }

                //Rysowanie RawImage
                if (Logic.images.ContainsKey(name))
                {
                    RawImage image = Logic.images[name];
                    spriteBatch.Draw(image.texture, Geo.Scale(new Rectangle(image.location.X + image.location.Width / 2, image.location.Y + image.location.Height / 2, image.location.Width, image.location.Height)), null, Color.White * image.opacity, image.rotation, new Vector2(image.texture.Width, image.texture.Height) / 2, SpriteEffects.None, 0);
                }
            }

            //Rysowanie kursora
            spriteBatch.Draw(cursorTexture, new Rectangle(Mouse.GetState().X - 16, Mouse.GetState().Y, 32, 32), Color.White);

            spriteBatch.DrawString(Logic.font, fps.ToString(), new Vector2(0, 0), Color.White);

            spriteBatch.End();

            base.Draw(gameTime);
        }