示例#1
0
        public virtual void RenderControls(ISurface surface, bool force = false)
        {
            if ((surface.IsDirty || force) && surface.Tint.A != 255)
            {
                int                  cellCount;
                Rectangle            rect;
                Point                point;
                Controls.ControlBase control;
                Cell                 cell;
                Font                 font;
                // For each control
                for (int i = 0; i < Controls.Count; i++)
                {
                    if (Controls[i].IsVisible)
                    {
                        control   = Controls[i];
                        cellCount = control.TextSurface.Cells.Length;

                        font = control.AlternateFont == null ? surface.Font : control.AlternateFont;

                        // Draw background of each cell for the control
                        for (int cellIndex = 0; cellIndex < cellCount; cellIndex++)
                        {
                            cell = control[cellIndex];

                            if (cell.IsVisible)
                            {
                                point = BasicSurface.GetPointFromIndex(cellIndex, control.TextSurface.Width);
                                point = new Point(point.X + control.Position.X, point.Y + control.Position.Y);

                                if (surface.RenderArea.Contains(point.X, point.Y))
                                {
                                    point = new Point(point.X - surface.RenderArea.Left, point.Y - surface.RenderArea.Top);
                                    rect  = surface.RenderRects[surface.GetIndexFromPoint(point)];

                                    if (cell.Background != Color.Transparent)
                                    {
                                        Global.SpriteBatch.Draw(font.FontImage, rect, font.GlyphRects[font.SolidGlyphIndex], cell.Background, 0f, Vector2.Zero, SpriteEffects.None, 0.23f);
                                    }

                                    if (cell.Foreground != Color.Transparent)
                                    {
                                        Global.SpriteBatch.Draw(font.FontImage, rect, font.GlyphRects[cell.Glyph], cell.Foreground, 0f, Vector2.Zero, cell.Mirror, 0.26f);
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        /// <summary>
        /// Returns a surface with the specified image rendered to it as characters.
        /// </summary>
        /// <param name="image">The image to render.</param>
        /// <returns>The surface.</returns>
        public BasicSurface GetSurface(Texture2D image)
        {
            editor.Clear();

            image.GetData <Color>(pixels);

            System.Threading.Tasks.Parallel.For(0, surface.Width * surface.Height, (i) =>
                                                //for (int i = 0; i < surface.Width * surface.Height; i++)
            {
                int allR = 0;
                int allG = 0;
                int allB = 0;

                int min = i * fontPixels;
                int max = min + fontPixels;

                for (int pixel = min; pixel < max; pixel++)
                {
                    Color color = pixels[indexes[pixel]];

                    allR += color.R;
                    allG += color.G;
                    allB += color.B;
                }

                // print our character
                byte sr = (byte)(allR / fontPixels);
                byte sg = (byte)(allG / fontPixels);
                byte sb = (byte)(allB / fontPixels);

                var newColor = new Color(sr, sg, sb);
                float sbri   = newColor.GetBrightness() * 255;


                Point surfacePoint = surface.GetPointFromIndex(i);
                if (UseBlockMode)
                {
                    if (sbri > 204)
                    {
                        editor.SetGlyph(surfacePoint.X, surfacePoint.Y, 219, newColor); //█
                    }
                    else if (sbri > 152)
                    {
                        editor.SetGlyph(surfacePoint.X, surfacePoint.Y, 178, newColor); //▓
                    }
                    else if (sbri > 100)
                    {
                        editor.SetGlyph(surfacePoint.X, surfacePoint.Y, 177, newColor); //▒
                    }
                    else if (sbri > 48)
                    {
                        editor.SetGlyph(surfacePoint.X, surfacePoint.Y, 176, newColor); //░
                    }
                    else
                    {
                        editor.SetGlyph(surfacePoint.X, surfacePoint.Y, 0, Color.Black);
                    }
                }
                else
                {
                    if (sbri > 230)
                    {
                        editor.SetGlyph(surfacePoint.X, surfacePoint.Y, (int)'#', newColor);
                    }
                    else if (sbri > 207)
                    {
                        editor.SetGlyph(surfacePoint.X, surfacePoint.Y, (int)'&', newColor);
                    }
                    else if (sbri > 184)
                    {
                        editor.SetGlyph(surfacePoint.X, surfacePoint.Y, (int)'$', newColor);
                    }
                    else if (sbri > 161)
                    {
                        editor.SetGlyph(surfacePoint.X, surfacePoint.Y, (int)'X', newColor);
                    }
                    else if (sbri > 138)
                    {
                        editor.SetGlyph(surfacePoint.X, surfacePoint.Y, (int)'x', newColor);
                    }
                    else if (sbri > 115)
                    {
                        editor.SetGlyph(surfacePoint.X, surfacePoint.Y, (int)'=', newColor);
                    }
                    else if (sbri > 92)
                    {
                        editor.SetGlyph(surfacePoint.X, surfacePoint.Y, (int)'+', newColor);
                    }
                    else if (sbri > 69)
                    {
                        editor.SetGlyph(surfacePoint.X, surfacePoint.Y, (int)';', newColor);
                    }
                    else if (sbri > 46)
                    {
                        editor.SetGlyph(surfacePoint.X, surfacePoint.Y, (int)':', newColor);
                    }
                    else if (sbri > 23)
                    {
                        editor.SetGlyph(surfacePoint.X, surfacePoint.Y, (int)'.', newColor);
                    }
                    else
                    {
                        editor.SetGlyph(surfacePoint.X, surfacePoint.Y, 0, Color.Black);
                    }
                }
            }
                                                );

            return(surface);
        }
示例#3
0
        public void ProcessMouse(MouseConsoleState info, ISurface surface, bool isInBounds)
        {
            if (info.Mouse.LeftClicked && info.IsOnConsole)
            {
                Cell cellToMatch     = new Cell();
                Cell currentFillCell = new Cell();

                info.Cell.CopyAppearanceTo(cellToMatch);

                currentFillCell.Glyph      = CharacterPickPanel.SharedInstance.SettingCharacter;
                currentFillCell.Foreground = CharacterPickPanel.SharedInstance.SettingForeground;
                currentFillCell.Background = CharacterPickPanel.SharedInstance.SettingBackground;
                currentFillCell.Mirror     = CharacterPickPanel.SharedInstance.SettingMirrorEffect;

                Func <Cell, bool> isTargetCell = (c) =>
                {
                    if (c.Glyph == 0 && cellToMatch.Glyph == 0)
                    {
                        return(c.Background == cellToMatch.Background);
                    }

                    return(c.Foreground == cellToMatch.Foreground &&
                           c.Background == cellToMatch.Background &&
                           c.Glyph == cellToMatch.Glyph &&
                           c.Mirror == cellToMatch.Mirror);
                };

                Action <Cell> fillCell = (c) =>
                {
                    currentFillCell.CopyAppearanceTo(c);
                    //console.TextSurface.SetEffect(c, _currentFillCell.Effect);
                };

                System.Collections.Generic.List <Cell> cells = new System.Collections.Generic.List <Cell>(surface.Cells);

                Func <Cell, SadConsole.Algorithms.NodeConnections <Cell> > getConnectedCells = (c) =>
                {
                    Algorithms.NodeConnections <Cell> connections = new Algorithms.NodeConnections <Cell>();

                    Point position = BasicSurface.GetPointFromIndex(cells.IndexOf(c), surface.Width);

                    connections.West  = surface.IsValidCell(position.X - 1, position.Y) ? surface.GetCell(position.X - 1, position.Y) : null;
                    connections.East  = surface.IsValidCell(position.X + 1, position.Y) ? surface.GetCell(position.X + 1, position.Y) : null;
                    connections.North = surface.IsValidCell(position.X, position.Y - 1) ? surface.GetCell(position.X, position.Y - 1) : null;
                    connections.South = surface.IsValidCell(position.X, position.Y + 1) ? surface.GetCell(position.X, position.Y + 1) : null;

                    return(connections);
                };

                if (!isTargetCell(currentFillCell))
                {
                    SadConsole.Algorithms.FloodFill <Cell>(info.Cell, isTargetCell, fillCell, getConnectedCells);
                }

                info.Console.TextSurface.IsDirty = true;
            }

            if (info.Mouse.RightButtonDown && info.IsOnConsole)
            {
                var cell = info.Cell;

                CharacterPickPanel.SharedInstance.SettingCharacter    = cell.Glyph;
                CharacterPickPanel.SharedInstance.SettingForeground   = cell.Foreground;
                CharacterPickPanel.SharedInstance.SettingBackground   = cell.Background;
                CharacterPickPanel.SharedInstance.SettingMirrorEffect = cell.Mirror;
            }
        }