Thraka / SadConsole

A .NET ascii/ansi console engine written in C# for MonoGame and XNA. Create your own text roguelike (or other) games!
MIT License
1.23k stars 120 forks source link

Scrollbar isn't shown on table control #338

Closed Ven0maus closed 8 months ago

Ven0maus commented 11 months ago

Test setup where scroll bar should be visible:

using SadConsole;
using SadConsole.Configuration;
using SadConsole.UI;
using SadConsole.UI.Controls;
using SadRogue.Primitives;

namespace Demoproject
{
    internal class Program
    {
        static void Main(string[] args)
        {
            Settings.WindowTitle = "Demo";

            // Configure how SadConsole starts up
            Builder startup = new Builder()
                .SetScreenSize(90, 30)
                .SetStartingScreen<RootScreen>()
                .IsStartingScreenFocused(true)
                .ConfigureFonts((config, game) => config.UseBuiltinFontExtended());

            // Setup the engine and start the game
            Game.Create(startup);
            Game.Instance.Run();
            Game.Instance.Dispose();
        }
    }

    internal class RootScreen : ScreenObject
    {
        private readonly int _width = 90;
        private readonly int _height = 30;
        private readonly MapController _controller;

        public RootScreen()
        {
            Children.Add(_controller = new MapController(_width, _height));
        }
    }

    public class MapController : ControlsConsole
    {
        private Table DungeonTable;
        private int MaxSimultaneousRows = 8;
        public MapController(int width, int height) : base(width, height)
        {
            DungeonTable = new Table((Width / 2) - 1, MaxSimultaneousRows + 1)
            {
                Position = new Point(1, 5),
                AutoScrollOnCellSelection = true,
                DefaultSelectionMode = Table.TableCells.Layout.Mode.EntireRow,
                DefaultHoverMode = Table.TableCells.Layout.Mode.None,
                DefaultBackground = Color.Black,
                DefaultForeground = Color.White,
            };
            DungeonTable.Cells.HeaderRow = true;
            DungeonTable.SelectedCellChanged += DungeonTable_SelectedCellChanged;
            DungeonTable.SetupScrollBar(Orientation.Vertical, DungeonTable.Height, new Point(DungeonTable.Width - 1, 0));
            DungeonTable.VerticalScrollBar.SetThemeColors(Colors.CreateSadConsoleBlue());
            Controls.Add(DungeonTable);

            // Build the layout first
            // Anything defined on the row or column is taken over for all other rows/columns
            DungeonTable.Cells.Row(0).SetLayout(3, foreground: Color.White, background: Color.Blue, settings: new Table.Cell.Options(DungeonTable)
            {
                HorizontalAlignment = Table.Cell.Options.HorizontalAlign.Center,
                VerticalAlignment = Table.Cell.Options.VerticalAlign.Center,
            });
            DungeonTable.Cells.Column(0, 1).SetLayout(size: (int)(DungeonTable.Width * 0.4));
            DungeonTable.Cells.Column(2).SetLayout(size: (int)(DungeonTable.Width * 0.2));

            var dungeonNameHeaderText = "Dungeon";
            var authorHeaderText = "Author";
            var versionHeaderText = "Version";

            DungeonTable.Cells[0, 0].Value = dungeonNameHeaderText;
            DungeonTable.Cells[0, 1].Value = authorHeaderText;
            DungeonTable.Cells[0, 2].Value = versionHeaderText;

            var rowIndex = 1;
            for (int i=0; i < 20; i++)
            {
                DungeonTable.Cells[rowIndex, 0].SetLayout(Color.White, Color.Black, new Table.Cell.Options(DungeonTable));
                DungeonTable.Cells[rowIndex, 0].Value = $"Name{rowIndex}";
                DungeonTable.Cells[rowIndex, 1].SetLayout(Color.White, Color.Black, new Table.Cell.Options(DungeonTable)
                {
                    HorizontalAlignment = Table.Cell.Options.HorizontalAlign.Center,
                    Interactable = true,
                    Selectable = true
                });
                DungeonTable.Cells[rowIndex, 1].Value = $"Author{rowIndex}";
                DungeonTable.Cells[rowIndex, 2].SetLayout(Color.White, Color.Black, new Table.Cell.Options(DungeonTable)
                {
                    HorizontalAlignment = Table.Cell.Options.HorizontalAlign.Center,
                    Interactable = true,
                    Selectable = true
                });
                DungeonTable.Cells[rowIndex, 2].Value = $"Version{rowIndex}";
                rowIndex++;
            }
            DungeonTable.Cells[1, 0].Select();
        }

        private void DungeonTable_SelectedCellChanged(object? sender, Table.CellChangedEventArgs e)
        {
            var cell = e.Cell;
            if (cell == null) return;
            System.Console.WriteLine($"Selected: {cell.Column},{cell.Row}");
        }
    }
}
Thraka commented 8 months ago

Fixed for next version