A game framework written with osu! in mind.
at master 86 lines 2.6 kB view raw
1// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence. 2// See the LICENCE file in the repository root for full licence text. 3 4using Markdig.Extensions.Tables; 5using Markdig.Syntax; 6using osu.Framework.Allocation; 7using osu.Framework.Graphics.Shapes; 8using osuTK.Graphics; 9 10namespace osu.Framework.Graphics.Containers.Markdown 11{ 12 /// <summary> 13 /// Visualises a cell of a <see cref="MarkdownTable"/>. 14 /// </summary> 15 /// <code> 16 /// | cell 1 | cell 2 | 17 /// </code> 18 public class MarkdownTableCell : CompositeDrawable, IMarkdownTextFlowComponent 19 { 20 public float ContentWidth => textFlow.TotalTextWidth; 21 public float ContentHeight => textFlow.DrawHeight; 22 23 private MarkdownTextFlowContainer textFlow; 24 25 private readonly TableCell cell; 26 private readonly TableColumnDefinition definition; 27 28 [Resolved] 29 private IMarkdownTextFlowComponent parentFlowComponent { get; set; } 30 31 public MarkdownTableCell(TableCell cell, TableColumnDefinition definition) 32 { 33 this.cell = cell; 34 this.definition = definition; 35 36 RelativeSizeAxes = Axes.Both; 37 38 BorderThickness = 1.8f; 39 BorderColour = Color4.White; 40 Masking = true; 41 } 42 43 [BackgroundDependencyLoader] 44 private void load() 45 { 46 InternalChildren = new Drawable[] 47 { 48 new Box 49 { 50 RelativeSizeAxes = Axes.Both, 51 Alpha = 0, 52 AlwaysPresent = true 53 }, 54 textFlow = CreateTextFlow() 55 }; 56 57 textFlow.Anchor = Anchor.CentreLeft; 58 textFlow.Origin = Anchor.CentreLeft; 59 60 if (cell.LastChild is ParagraphBlock paragraphBlock) 61 textFlow.AddInlineText(paragraphBlock.Inline); 62 63 switch (definition.Alignment) 64 { 65 case TableColumnAlign.Center: 66 textFlow.TextAnchor = Anchor.Centre; 67 break; 68 69 case TableColumnAlign.Right: 70 textFlow.TextAnchor = Anchor.CentreRight; 71 break; 72 73 default: 74 textFlow.TextAnchor = Anchor.CentreLeft; 75 break; 76 } 77 } 78 79 public virtual MarkdownTextFlowContainer CreateTextFlow() 80 { 81 var flow = parentFlowComponent.CreateTextFlow(); 82 flow.Padding = new MarginPadding(10); 83 return flow; 84 } 85 } 86}