Пример #1
0
        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();

            // If the Save File dialog has returned, save the image
            if (!String.IsNullOrEmpty(filename))
            {
                diskTexture.SaveToPhotoLibrary(filename);
                filename = null;
            }

            // Disk rotates every 5 seconds
            double seconds = gameTime.TotalGameTime.TotalSeconds;
            currentAngle = (float)(2 * Math.PI * seconds / 5);

            // Colors cycle every 10 seconds
            float fraction = (float)(6 * (seconds % 10) / 10);

            if (fraction < 1)
                currentColor = new Color(1, fraction, 0);
            else if (fraction < 2)
                currentColor = new Color(2 - fraction, 1, 0);
            else if (fraction < 3)
                currentColor = new Color(0, 1, fraction - 2);
            else if (fraction < 4)
                currentColor = new Color(0, 4 - fraction, 1);
            else if (fraction < 5)
                currentColor = new Color(fraction - 4, 0, 1);
            else
                currentColor = new Color(1, 0, 6 - fraction);

            // First assume no finger movement
            foreach (TouchInfo touchInfo in touchDictionary.Values)
                touchInfo.CurrentPosition = touchInfo.PreviousPosition;

            // Get all touches
            TouchCollection touches = TouchPanel.GetState();

            foreach (TouchLocation touch in touches)
            {
                // Let Button components have first dibs on touch
                bool touchHandled = false;

                foreach (GameComponent component in this.Components)
                {
                    if (component is IProcessTouch &&
                        (component as IProcessTouch).ProcessTouch(touch))
                    {
                        touchHandled = true;
                        break;
                    }
                }

                if (touchHandled)
                    continue;

                // Set TouchInfo items from touch information
                int id = touch.Id;

                switch (touch.State)
                {
                    case TouchLocationState.Pressed:
                        if (!touchDictionary.ContainsKey(id))
                            touchDictionary.Add(id, new TouchInfo());

                        touchDictionary[id].PreviousPosition = TranslateToTexture(touch.Position);
                        touchDictionary[id].CurrentPosition = TranslateToTexture(touch.Position);
                        break;

                    case TouchLocationState.Moved:
                        if (touchDictionary.ContainsKey(id))
                            touchDictionary[id].CurrentPosition =
                                        TranslateToTexture(touch.Position);
                        break;

                    case TouchLocationState.Released:
                        if (touchDictionary.ContainsKey(id))
                            touchDictionary.Remove(id);
                        break;
                }
            }

            // Calculate transforms for rotation
            Matrix translate1 = Matrix.CreateTranslation(-textureCenter.X, -textureCenter.Y, 0);
            Matrix translate2 = Matrix.CreateTranslation(textureCenter.X, textureCenter.Y, 0);

            Matrix previousRotation = translate1 *
                                            Matrix.CreateRotationZ(-previousAngle) *
                                                    translate2;
            Matrix currentRotation = translate1 *
                                            Matrix.CreateRotationZ(-currentAngle) *
                                                    translate2;

            bool textureNeedsUpdate = false;

            foreach (TouchInfo touchInfo in touchDictionary.Values)
            {
                // Now draw from previous to current points
                Vector2 point1 = Vector2.Transform(touchInfo.PreviousPosition, previousRotation);
                Vector2 point2 = Vector2.Transform(touchInfo.CurrentPosition, currentRotation);
                float radius = 6;

                RoundCappedLine line = new RoundCappedLine(point1, point2, radius);

                int yMin = (int)(Math.Min(point1.Y, point2.Y) - radius);
                int yMax = (int)(Math.Max(point1.Y, point2.Y) + radius);

                yMin = Math.Max(0, Math.Min(diskTexture.Height, yMin));
                yMax = Math.Max(0, Math.Min(diskTexture.Height, yMax));

                for (int y = yMin; y < yMax; y++)
                {
                    xCollection.Clear();
                    line.GetAllX(y, xCollection);

                    if (xCollection.Count == 2)
                    {
                        int xMin = (int)(Math.Min(xCollection[0], xCollection[1]) + 0.5f);
                        int xMax = (int)(Math.Max(xCollection[0], xCollection[1]) + 0.5f);

                        xMin = Math.Max(0, Math.Min(diskTexture.Width, xMin));
                        xMax = Math.Max(0, Math.Min(diskTexture.Width, xMax));

                        for (int x = xMin; x < xMax; x++)
                        {
                            if (IsWithinCircle(x, y))
                            {
                                // Draw pixel in four quadrants
                                int xFlip = diskTexture.Width - x;
                                int yFlip = diskTexture.Height - y;

                                pixels[y * diskTexture.Width + x] = currentColor.PackedValue;
                                pixels[y * diskTexture.Width + xFlip] = currentColor.PackedValue;
                                pixels[yFlip * diskTexture.Width + x] = currentColor.PackedValue;
                                pixels[yFlip * diskTexture.Width + xFlip] =
                                                                    currentColor.PackedValue;
                            }
                        }
                        textureNeedsUpdate = true;
                    }
                }
            }

            if (textureNeedsUpdate)
            {
                // Update the texture from the pixels array
                this.GraphicsDevice.Textures[0] = null;
                diskTexture.SetData<uint>(pixels);
            }

            // Prepare for next time through
            foreach (TouchInfo touchInfo in touchDictionary.Values)
                touchInfo.PreviousPosition = touchInfo.CurrentPosition;

            previousAngle = currentAngle;

            base.Update(gameTime);
        }
Пример #2
0
        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();

            bool canvasNeedsUpdate = false;
            int yMinUpdate = Int32.MaxValue, yMaxUpdate = 0;

            while (TouchPanel.IsGestureAvailable)
            {
                GestureSample gesture = TouchPanel.ReadGesture();

                if (gesture.GestureType == GestureType.FreeDrag &&
                    gesture.Delta != Vector2.Zero)
                {
                    Vector2 point1 = gesture.Position - gesture.Delta;
                    Vector2 point2 = gesture.Position;
                    float radius = 12;

                    RoundCappedLine line = new RoundCappedLine(point1, point2, radius);

                    int yMin = (int)(Math.Min(point1.Y, point2.Y) - radius - 1);
                    int yMax = (int)(Math.Max(point1.Y, point2.Y) + radius + 1);

                    yMin = Math.Max(0, Math.Min(canvas.Height, yMin));
                    yMax = Math.Max(0, Math.Min(canvas.Height, yMax));

                    for (int y = yMin; y < yMax; y++)
                    {
                        xCollection.Clear();
                        line.GetAllX(y, xCollection);

                        if (xCollection.Count == 2)
                        {
                            int xMin = (int)(Math.Min(xCollection[0],
                                                      xCollection[1]) + 0.5f);
                            int xMax = (int)(Math.Max(xCollection[0],
                                                      xCollection[1]) + 0.5f);

                            xMin = Math.Max(0, Math.Min(canvas.Width, xMin));
                            xMax = Math.Max(0, Math.Min(canvas.Width, xMax));

                            for (int x = xMin; x < xMax; x++)
                            {
                                pixels[y * canvas.Width + x] = Color.Red;
                            }
                            yMinUpdate = Math.Min(yMinUpdate, yMin);
                            yMaxUpdate = Math.Max(yMaxUpdate, yMax);
                            canvasNeedsUpdate = true;
                        }
                    }
                }
            }

            if (canvasNeedsUpdate)
            {
                this.GraphicsDevice.Textures[0] = null;

                int height = yMaxUpdate - yMinUpdate;
                Rectangle rect = new Rectangle(0, yMinUpdate, canvas.Width, height);
                canvas.SetData<Color>(0, rect, pixels,
                            yMinUpdate * canvas.Width, height * canvas.Width);
            }
            base.Update(gameTime);
        }
Пример #3
0
        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();

            bool canvasNeedsUpdate = false;
            int yMinUpdate = Int32.MaxValue, yMaxUpdate = 0;

            TouchCollection touches = TouchPanel.GetState();

            foreach (TouchLocation touch in touches)
            {
                if (touch.State == TouchLocationState.Moved)
                {
                    TouchLocation previousTouch;
                    touch.TryGetPreviousLocation(out previousTouch);

                    Vector2 point1 = previousTouch.Position;
                    Vector2 point2 = touch.Position;
                    float radius = 12;

                    RoundCappedLine line = new RoundCappedLine(point1, point2, radius);

                    int yMin = (int)(Math.Min(point1.Y, point2.Y) - radius - 1);
                    int yMax = (int)(Math.Max(point1.Y, point2.Y) + radius + 1);

                    yMin = Math.Max(0, Math.Min(canvas.Height, yMin));
                    yMax = Math.Max(0, Math.Min(canvas.Height, yMax));

                    for (int y = yMin; y < yMax; y++)
                    {
                        xCollection.Clear();
                        line.GetAllX(y, xCollection);

                        if (xCollection.Count == 2)
                        {
                            int xMin = (int)(Math.Min(xCollection[0],
                                                      xCollection[1]) + 0.5f);
                            int xMax = (int)(Math.Max(xCollection[0],
                                                      xCollection[1]) + 0.5f);

                            xMin = Math.Max(0, Math.Min(canvas.Width, xMin));
                            xMax = Math.Max(0, Math.Min(canvas.Width, xMax));

                            for (int x = xMin; x < xMax; x++)
                            {
                                pixels[y * canvas.Width + x] = Color.Red;
                            }
                            yMinUpdate = Math.Min(yMinUpdate, yMin);
                            yMaxUpdate = Math.Max(yMaxUpdate, yMax);
                            canvasNeedsUpdate = true;
                        }
                    }
                }
            }

            if (canvasNeedsUpdate)
            {
                this.GraphicsDevice.Textures[0] = null;

                int height = yMaxUpdate - yMinUpdate;
                Rectangle rect = new Rectangle(0, yMinUpdate, canvas.Width, height);
                canvas.SetData<Color>(0, rect, pixels,
                            yMinUpdate * canvas.Width, height * canvas.Width);
            }
            base.Update(gameTime);
        }
Пример #4
0
        /// <summary>
        /// 允许游戏运行逻辑,例如更新全部内容、
        /// 检查冲突、收集输入信息以及播放音频。
        /// </summary>
        /// <param name="gameTime">提供计时值的快照。</param>
        protected override void Update(GameTime gameTime)
        {
            // 允许游戏退出
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();

            // TODO: 在此处添加更新逻辑

            if (!string.IsNullOrWhiteSpace(filename)) {
                canvas.SaveToPhotoLibrary(filename);
                filename = string.Empty;
            }
            bool canvasNeedsUpdate = false;
            TouchCollection touches = TouchPanel.GetState();

            foreach( TouchLocation touch in touches){
                if (touchIdToIgnore.HasValue && touch.Id == touchIdToIgnore.Value) continue;

                bool touchHandled = false;

                foreach (GameComponent compnent in this.Components) {
                    if (compnent is IProcessTouch && (compnent as IProcessTouch).ProcessTouch(touch)) {
                        touchHandled = true;
                        break;
                    }
                }

                if (touchHandled) return;
                switch (touch.State) {
                    case TouchLocationState.Pressed: {
                        Vector2 postion = touch.Position;
                        ColorBlock newSelectColorBolck = null;
                        foreach (ColorBlock colorBolock in colorBlocks) {
                            Rectangle rect = colorBolock.destination;
                            if (postion.X >= rect.Left && postion.X < rect.Right && postion.Y >= rect.Top && postion.Y < rect.Bottom) {
                                drawingColor = colorBolock.color;
                                newSelectColorBolck = colorBolock;
                            }
                        }
                        if (newSelectColorBolck == null)
                        {
                            touchIdToIgnore = null;
                        }
                        else {
                            foreach (ColorBlock colorBlock in colorBlocks) {
                                colorBlock.isSelected = colorBlock == newSelectColorBolck;
                            }
                            touchIdToIgnore = touch.Id;
                        }
                        break; }
                    case TouchLocationState.Moved: {
                        TouchLocation prevTouchLocation;
                        touch.TryGetPreviousLocation(out prevTouchLocation);
                        Vector2 point1 = prevTouchLocation.Position - canvasPosition;
                        Vector2 point2 = touch.Position - canvasPosition;
                        float radius = 12;
                        RoundCappedLine line = new RoundCappedLine(point1, point2, radius);
                        int yMin = (int)(Math.Min(point1.Y, point2.Y) - radius);
                        int yMax = (int)(Math.Max(point1.Y, point2.Y) - radius);

                        yMin = Math.Max(0,Math.Min(canvas.Height,yMin));
                        yMax = Math.Max(0, Math.Min(canvas.Height, yMax));

                        for (int y = yMin; y < yMax; y++) {
                            xCollection.Clear();
                            line.GetAllX(y, xCollection);

                            if (xCollection.Count == 2) {
                                int xMin = (int)(Math.Min(xCollection[0], xCollection[1]) + 0.5f);
                                int xMax = (int)(Math.Max(xCollection[0], xCollection[1]) + 0.5f);
                                xMin = Math.Max(0, Math.Min(canvas.Width, xMin));
                                xMax = Math.Max(0,Math.Min(canvas.Width,xMax));

                                for (int x = xMin; x < xMax; x++) {
                                    pixels[y * canvas.Width + x] = drawingColor.PackedValue;
                                }

                                canvasNeedsUpdate = true;
                            }
                            if (canvasNeedsUpdate) {
                                canvas.SetData<uint>(pixels);
                                base.Update(gameTime);
                            }

                        }
                            break;
                    }
                }

            }

            base.Update(gameTime);
        }