public MainWindow() : base(Gtk.WindowType.Toplevel) { Build(); //this.DrawingTrack.ExposeEvent += OnDrawingTrackExposeEvent; Context g = Gdk.CairoHelper.Create(this.GdkWindow); g.SelectFontFace("DejaVu Sans Mono", FontSlant.Normal, FontWeight.Normal); g.SetFontSize(14); fe = g.FontExtents; g.Dispose(); this.DoubleBuffered = true; this.DrawingTrack.HeightRequest = (int)(MainClass.tracks.MaxRow * fe.Height); this.vscrollbarTrack.Adjustment.Lower = 0; this.vscrollbarTrack.Adjustment.Value = 0; this.vscrollbarTrack.Adjustment.Upper = (MainClass.tracks.MaxRow + 1.0); DisplayLines = (int)((double)this.drawingarea1.HeightRequest / fe.Height + 1.0); if ((DisplayLines & 1) == 0) { DisplayLines++; // muss ungerade sein } this.drawingarea1.HeightRequest = DisplayLines * (int)fe.Height; Console.WriteLine(DisplayLines); y_mid = this.drawingarea1.HeightRequest / 2; }
public void ShouldSetHorizontalFontExtentsDelegate() { using (var font = new Font(Font)) using (var fontFuncs = new FontFunctions()) { var expected = new FontExtents { Ascender = 1337 }; fontFuncs.SetHorizontalFontExtentsDelegate((Font f, object fd, out FontExtents e) => { e = expected; return(true); }); fontFuncs.MakeImmutable(); font.SetFontFunctions(fontFuncs, "FontData"); var result = font.TryGetHorizontalFontExtents(out var extents); Assert.True(result); Assert.Equal(expected, extents); } }
// Get the line spacing for the font selected into this graphics object. public override int GetLineSpacing() { Xsharp.Font xfont = (font as DrawingFont).xfont; FontExtents extents = xfont.GetFontExtents(graphics); return(extents.Ascent + extents.Descent); }
public override void OnLayoutChanges(LayoutingType layoutType) { base.OnLayoutChanges(layoutType); if (layoutType == LayoutingType.Height) { using (ImageSurface img = new ImageSurface(Format.Argb32, 10, 10)) { using (Context gr = new Context(img)) { //Cairo.FontFace cf = gr.GetContextFontFace (); gr.SelectFontFace(Font.Name, Font.Slant, Font.Wheight); gr.SetFontSize(Font.Size); fe = gr.FontExtents; } } lineHeight = fe.Height; visibleLines = (int)Math.Floor((double)ClientRectangle.Height / fe.Height); System.Diagnostics.Debug.WriteLine("Visible lines: {0})", visibleLines); if (Scroll + visibleLines > Datas?.Count) { Scroll = Datas.Count - visibleLines; } } }
protected override bool OnExposeEvent(Gdk.EventExpose args) { using (Context g = Gdk.CairoHelper.Create(args.Window)) { DrawCurvedRectangle(g); g.SetSourceColor(new Color(0.8, 0.8, 0.8, 1)); g.FillPreserve(); if (Visited) { g.SetSourceColor(new Color(0, 0.8, 0, 1)); } else { g.SetSourceColor(new Color(0, 0.65, 1, 1)); } g.LineWidth = 5; g.Stroke(); SetupFont(g); FontExtents fe = g.FontExtents; TextExtents te = g.TextExtents(caption); double x = Width / 2 + te.XBearing - te.Width / 2; double y = Height / 2 + fe.Descent + fe.Height / 2; g.MoveTo(x, y); g.SetSourceColor(new Color(0, 0, 0)); g.ShowText(caption); } return(true); }
void drawCenteredTextLine(Context gr, PointD center, string txt) { if (string.IsNullOrEmpty(txt)) { return; } FontExtents fe = gr.FontExtents; TextExtents te = gr.TextExtents(txt); Rectangle rText = new Rectangle( (int)(center.X - te.Width / 2), (int)(center.Y - (fe.Ascent + fe.Descent) / 2), (int)te.Width, (int)(fe.Ascent + fe.Descent)); gr.Operator = Operator.Clear; Rectangle r = rText; r.Inflate(2); gr.Rectangle(r); gr.Fill(); gr.Operator = Operator.Over; gr.MoveTo(rText.X, rText.Y + fe.Ascent); gr.ShowText(txt); gr.Fill(); }
/// <summary> /// Generates an unscaled text texture. /// </summary> /// <param name="text">The text to texture.</param> /// <param name="font">The font of the text.</param> /// <param name="background">The background of the text (Default: none/null)</param> /// <returns>The loaded unscaled texture.</returns> public LoadedTexture GenUnscaledTextTexture(string text, CairoFont font, TextBackground background = null) { if (background == null) { background = defaultBackground; } double maxwidth = 0; string[] lines = text.Split('\n'); for (int i = 0; i < lines.Length; i++) { lines[i] = lines[i].TrimEnd(); TextExtents textents = ((CairoFont)font).GetTextExtents(lines[i]); maxwidth = Math.Max(textents.Width, maxwidth); } FontExtents fextents = ((CairoFont)font).GetFontExtents(); int width = (int)maxwidth + 1 + 2 * background.Padding; int height = (int)fextents.Height * lines.Length + 1 + 2 * background.Padding; return(GenTextTexture(text, font, width, height, background)); }
static List <CharacterData> CreateCharacterData( string fontName, FontSlant fontSlant, FontWeight fontWeight, double fontSize, List <char> fontChars, out double bitmapWidth, out double bitmapHeight) { List <CharacterData> cds = new List <CharacterData>(); using (ImageSurface surface = new ImageSurface(Format.Argb32, 256, 256)) { using (Context g = new Context(surface)) { SetupContext(g, fontName, fontSlant, fontWeight, fontSize); FontExtents fe = g.FontExtents; double x = 0; double y = 0; for (int i = 0; i < fontChars.Count; i++) { CharacterData cd = new CharacterData(); cds.Add(cd); cd.Character = new String(fontChars[i], 1); TextExtents te = g.TextExtents(cd.Character); double aliasSpace; if (cd.Character == " ") { aliasSpace = 0.0; } else { aliasSpace = 1.0; } cd.Bearing = new PointD(-te.XBearing + aliasSpace, -te.YBearing + aliasSpace); cd.Location = new Cairo.Rectangle(x, 0, te.Width + aliasSpace * 2, te.Height + aliasSpace * 2); cd.Cropping = new Cairo.Rectangle(0, fe.Ascent + aliasSpace * 2 - cd.Bearing.Y, te.XAdvance + aliasSpace, cd.Location.Height); cd.Kerning = new Vector3(0, (float)cd.Location.Width, (float)(cd.Bearing.X + cd.Cropping.Width - cd.Location.Width)); x += cd.Location.Width; if (cd.Location.Height > y) { y = cd.Location.Height; } } bitmapWidth = x; bitmapHeight = y; } } return(cds); }
public bool TryGetVerticalFontExtents(out FontExtents extents) { fixed(FontExtents *e = &extents) { return(HarfBuzzApi.hb_font_get_v_extents(Handle, e)); } }
// Draw a string using the current font and brush. public override void DrawString (String s, int x, int y, StringFormat format) { Xsharp.Font xfont = (font as DrawingFont).xfont; FontExtents extents = xfont.GetFontExtents(graphics); xfont.DrawString(graphics, RestrictXY(x), RestrictXY(y) + extents.Ascent, s, 0, s.Length); }
private void DrawText(Cairo.Context g, string text, int top, int left) { g.SetSourceRGB(0, 0, 0); g.SetFontSize(cellHeight / 1.5); FontExtents fe = g.FontExtents; TextExtents te = g.TextExtents(text); g.MoveTo(left * cellWidth + (cellWidth - te.Width) / 2, top * cellHeight + (cellHeight + te.YBearing + fe.Height) / 2); g.ShowText(text); }
void updateGraphic(string file) { int width = 100; int height = 15; int x = 10; int y = 6; Bitmap bmp = new Bitmap(file); BitmapData data = bmp.LockBits(new System.Drawing.Rectangle(x, y, width, height), ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb); int stride = 4 * width; using (ImageSurface draw = new ImageSurface(data.Scan0, Format.Argb32, width, height, stride)) { using (Context gr = new Context(draw)) { //Rectangle r = new Rectangle(0, 0, renderBounds.Width, renderBounds.Height); gr.SelectFontFace("MagicMedieval", FontSlant.Normal, FontWeight.Bold); gr.SetFontSize(12); gr.SetSourceColor(Crow.Color.Black); string test = "Test string"; FontExtents fe = gr.FontExtents; TextExtents te = gr.TextExtents(test); double xt = 20;// 0.5 - te.XBearing - te.Width / 2, double yt = fe.Height; gr.MoveTo(xt, yt); gr.ShowText(test); using (ImageSurface imgSurf = new ImageSurface(@"images/manaw.png")) { gr.SetSourceSurface(imgSurf, 0, 0); gr.Paint(); } draw.Flush(); } //draw.WriteToPng(directories.rootDir + @"test.png"); } imgHelpers.imgHelpers.flipY(data.Scan0, stride, height); GL.TexSubImage2D(TextureTarget.Texture2D, 0, x, bmp.Height - y - height, width, height, OpenTK.Graphics.OpenGL.PixelFormat.Bgra, PixelType.UnsignedByte, data.Scan0); bmp.UnlockBits(data); }
/// <summary> /// Adjust the bounds so that it fits given text in one line /// </summary> /// <param name="text">The text to adjust</param> /// <param name="bounds">The bounds to adjust the text to.</param> /// <param name="onlyGrow">If true, the box will not be made smaller</param> public void AutoBoxSize(string text, ElementBounds bounds, bool onlyGrow = false) { double textWidth = 0; double textHeight = 0; FontExtents fontExtents = GetFontExtents(); if (text.Contains('\n')) { string[] lines = text.Split('\n'); for (int i = 0; i < lines.Length; i++) { if (lines[i].Length == 0 && i == lines.Length - 1) { break; } TextExtents extents = GetTextExtents(lines[i]); textWidth = Math.Max(textWidth, extents.Width); textHeight += fontExtents.Height; } } else { TextExtents extents = GetTextExtents(text); textWidth = extents.Width; textHeight = fontExtents.Height; } if (text.Length == 0) { textWidth = 0; textHeight = 0; } if (onlyGrow) { // Divide by guiscale because calcWorldBounds will multiply it again with guiscale bounds.fixedWidth = Math.Max(bounds.fixedWidth, textWidth / RuntimeEnv.GUIScale + 1); bounds.fixedHeight = Math.Max(bounds.fixedHeight, textHeight / RuntimeEnv.GUIScale); } else { // Divide by guiscale because calcWorldBounds will multiply it again with guiscale bounds.fixedWidth = Math.Max(1, textWidth / RuntimeEnv.GUIScale + 1); bounds.fixedHeight = Math.Max(1, textHeight / RuntimeEnv.GUIScale); } }
private void DrawText(string text, int top, int left) { using (Cairo.Context g = Gdk.CairoHelper.Create(this.GdkWindow)) { g.SetSourceRGB(0, 0, 0); g.SetFontSize(cellHeight / 1.5); FontExtents fe = g.FontExtents; TextExtents te = g.TextExtents(text); g.MoveTo(left * cellWidth + (cellWidth - te.Width) / 2, top * cellHeight + (cellHeight + te.YBearing + fe.Height) / 2); g.ShowText(text); g.GetTarget().Dispose(); } }
protected override int measureRawSize(LayoutingType lt) { if (lines == null) { lines = getLines; } using (ImageSurface img = new ImageSurface(Format.Argb32, 10, 10)) { using (Context gr = new Context(img)) { //Cairo.FontFace cf = gr.GetContextFontFace (); gr.SelectFontFace(Font.Name, Font.Slant, Font.Wheight); gr.SetFontSize(Font.Size); fe = gr.FontExtents; te = new TextExtents(); if (lt == LayoutingType.Height) { int lc = lines.Count; //ensure minimal height = text line height if (lc == 0) { lc = 1; } return((int)Math.Ceiling(fe.Height * lc) + Margin * 2); } try { foreach (string s in lines) { string l = s.Replace("\t", new String(' ', Interface.TabSize)); TextExtents tmp = gr.TextExtents(l); if (tmp.XAdvance > te.XAdvance) { te = tmp; } } } catch (Exception ex) { return(-1); } return((int)Math.Ceiling(te.XAdvance) + Margin * 2); } } }
protected override int measureRawSize(LayoutingType lt) { if (lines == null) { lines = getLines; } if (!textMeasureIsUpToDate) { using (ImageSurface img = new ImageSurface(Format.Argb32, 10, 10)) { using (Context gr = new Context(img)) { //Cairo.FontFace cf = gr.GetContextFontFace (); gr.SelectFontFace(Font.Name, Font.Slant, Font.Wheight); gr.SetFontSize(Font.Size); gr.FontOptions = Interface.FontRenderingOptions; gr.Antialias = Interface.Antialias; fe = gr.FontExtents; te = new TextExtents(); cachedTextSize.Height = (int)Math.Ceiling((fe.Ascent + fe.Descent) * Math.Max(1, lines.Count)) + Margin * 2; try { foreach (string s in lines) { string l = s.Replace("\t", new String(' ', Interface.TabSize)); TextExtents tmp = gr.TextExtents(l); if (tmp.XAdvance > te.XAdvance) { te = tmp; } } cachedTextSize.Width = (int)Math.Ceiling(te.XAdvance) + Margin * 2; textMeasureIsUpToDate = true; } catch { return(-1); } } } } return(lt == LayoutingType.Height ? cachedTextSize.Height : cachedTextSize.Width); }
public override void OnLayoutChanges(LayoutingType layoutType) { base.OnLayoutChanges(layoutType); if (layoutType == LayoutingType.Height) { using (ImageSurface img = new ImageSurface(Format.Argb32, 10, 10)) { using (Context gr = new Context(img)) { //Cairo.FontFace cf = gr.GetContextFontFace (); gr.SelectFontFace(Font.Name, Font.Slant, Font.Wheight); gr.SetFontSize(Font.Size); fe = gr.FontExtents; } } visibleLines = (int)Math.Floor((double)ClientRectangle.Height / fe.Height); MaxScrollY = lines.Count - visibleLines; } }
public SyntaxNodePrinter(RoslynEditor editor, Context ctx, int totalLines, int firstLine = 0, int visibleLines = 1) : base(SyntaxWalkerDepth.StructuredTrivia) { this.editor = editor; this.ctx = ctx; this.totalLines = totalLines; this.firstLine = firstLine; this.visibleLines = visibleLines; printLineNumbers = (editor.IFace as CrowIDE).PrintLineNumbers; printedLinesNumbers = new int [visibleLines]; bounds = editor.ClientRectangle; fe = ctx.FontExtents; y = bounds.Top; currentCol = -1; // < 0 => margin no printed currentLine = 0; printedLines = (firstLine == 0) ? 0 : -1; //<0 until firstLine is reached /*if (printedLines == 0) * checkPrintMargin ();*/ }
public bool TryGetHorizontalFontExtents(out FontExtents extents) => HarfBuzzApi.hb_font_get_h_extents(Handle, out extents);
public extern static bool hb_font_get_v_extents(hb_font_t font, out FontExtents extents);
void ComposeButton(Context ctx, ImageSurface surface, bool pressed) { double embossHeight = scaled(2.5); if (buttonStyle == EnumButtonStyle.Normal) { embossHeight = scaled(1.5); } Bounds.CalcWorldBounds(); normalText.AutoBoxSize(true); pressedText.Bounds = normalText.Bounds.CopyOnlySize(); if (buttonStyle != EnumButtonStyle.None) { // Brown background Rectangle(ctx, Bounds.bgDrawX, Bounds.bgDrawY, Bounds.OuterWidth, Bounds.OuterHeight); ctx.SetSourceRGBA(69 / 255.0, 52 / 255.0, 36 / 255.0, 1); ctx.Fill(); } if (buttonStyle == EnumButtonStyle.MainMenu) { // Top shine Rectangle(ctx, Bounds.bgDrawX, Bounds.bgDrawY, Bounds.OuterWidth, embossHeight); ctx.SetSourceRGBA(1, 1, 1, 0.15); ctx.Fill(); } if (buttonStyle == EnumButtonStyle.Normal) { // Top shine Rectangle(ctx, Bounds.bgDrawX, Bounds.bgDrawY, Bounds.OuterWidth - embossHeight, embossHeight); ctx.SetSourceRGBA(1, 1, 1, 0.15); ctx.Fill(); // Left shine Rectangle(ctx, Bounds.bgDrawX, Bounds.bgDrawY + embossHeight, embossHeight, Bounds.OuterHeight - embossHeight); ctx.SetSourceRGBA(1, 1, 1, 0.15); ctx.Fill(); } // Pretty elaborate way of vertically centering the text. Le sigh. FontExtents fontex = normalText.Font.GetFontExtents(); TextExtents textex = normalText.Font.GetTextExtents(normalText.GetText()); double resetY = -fontex.Ascent - textex.YBearing; textOffsetY = resetY + (normalText.Bounds.InnerHeight + textex.YBearing) / 2; normalText.Bounds.fixedY += textOffsetY; normalText.ComposeElements(ctx, surface); normalText.Bounds.fixedY -= textOffsetY; // ShowMultilineText changes height Bounds.CalcWorldBounds(); if (buttonStyle == EnumButtonStyle.MainMenu) { // Bottom shade Rectangle(ctx, Bounds.bgDrawX, Bounds.bgDrawY + Bounds.OuterHeight - embossHeight, Bounds.OuterWidth, embossHeight); ctx.SetSourceRGBA(0, 0, 0, 0.2); ctx.Fill(); } if (buttonStyle == EnumButtonStyle.Normal) { // Bottom shade Rectangle(ctx, Bounds.bgDrawX + embossHeight, Bounds.bgDrawY + Bounds.OuterHeight - embossHeight, Bounds.OuterWidth - 2 * embossHeight, embossHeight); ctx.SetSourceRGBA(0, 0, 0, 0.2); ctx.Fill(); // Right shade Rectangle(ctx, Bounds.bgDrawX + Bounds.OuterWidth - embossHeight, Bounds.bgDrawY, embossHeight, Bounds.OuterHeight); ctx.SetSourceRGBA(0, 0, 0, 0.2); ctx.Fill(); } if (buttonStyle == EnumButtonStyle.Normal) { //EmbossRoundRectangleElement(ctx, bounds.bgDrawX, bounds.bgDrawY, bounds.OuterWidth, bounds.OuterHeight, false, 2); } }
void ComposeButton(Context ctx, ImageSurface surface) { double embossHeight = scaled(2.5); if (buttonStyle == EnumButtonStyle.Normal || buttonStyle == EnumButtonStyle.Small) { embossHeight = scaled(1.5); } if (buttonStyle != EnumButtonStyle.None) { // Brown background Rectangle(ctx, 0, 0, Bounds.OuterWidth, Bounds.OuterHeight); ctx.SetSourceRGBA(69 / 255.0, 52 / 255.0, 36 / 255.0, 1); ctx.Fill(); } if (buttonStyle == EnumButtonStyle.MainMenu) { // Top shine Rectangle(ctx, 0, 0, Bounds.OuterWidth, embossHeight); ctx.SetSourceRGBA(1, 1, 1, 0.15); ctx.Fill(); } if (buttonStyle == EnumButtonStyle.Normal || buttonStyle == EnumButtonStyle.Small) { // Top shine Rectangle(ctx, 0, 0, Bounds.OuterWidth - embossHeight, embossHeight); ctx.SetSourceRGBA(1, 1, 1, 0.15); ctx.Fill(); // Left shine Rectangle(ctx, 0, 0 + embossHeight, embossHeight, Bounds.OuterHeight - embossHeight); ctx.SetSourceRGBA(1, 1, 1, 0.15); ctx.Fill(); } // Pretty elaborate way of vertically centering the text. Le sigh. FontExtents fontex = normalText.Font.GetFontExtents(); TextExtents textex = normalText.Font.GetTextExtents(normalText.GetText()); double resetY = -fontex.Ascent - textex.YBearing; textOffsetY = (resetY + (normalText.Bounds.InnerHeight + textex.YBearing) / 2) / RuntimeEnv.GUIScale; normalText.Bounds.fixedY += textOffsetY; normalText.ComposeElements(ctx, surface); normalText.Bounds.fixedY -= textOffsetY; // ShowMultilineText changes height Bounds.CalcWorldBounds(); if (buttonStyle == EnumButtonStyle.MainMenu) { // Bottom shade Rectangle(ctx, 0, 0 + Bounds.OuterHeight - embossHeight, Bounds.OuterWidth, embossHeight); ctx.SetSourceRGBA(0, 0, 0, 0.2); ctx.Fill(); } if (buttonStyle == EnumButtonStyle.Normal || buttonStyle == EnumButtonStyle.Small) { // Bottom shade Rectangle(ctx, 0 + embossHeight, 0 + Bounds.OuterHeight - embossHeight, Bounds.OuterWidth - 2 * embossHeight, embossHeight); ctx.SetSourceRGBA(0, 0, 0, 0.2); ctx.Fill(); // Right shade Rectangle(ctx, 0 + Bounds.OuterWidth - embossHeight, 0, embossHeight, Bounds.OuterHeight); ctx.SetSourceRGBA(0, 0, 0, 0.2); ctx.Fill(); } }
public bool TryGetVerticalFontExtents(out FontExtents extents) => HarfBuzzApi.hb_font_get_v_extents(Handle, out extents);
protected override void onDraw(Context gr) { base.onDraw(gr); FontExtents fe = gr.FontExtents; }
private static bool FontExtentsProxyImplementation(IntPtr font, IntPtr fontData, out FontExtents extents, IntPtr context) { var del = GetMulti <FontExtentsDelegate> (context, out _); var userData = GetUserData <FontUserData> (fontData); return(del.Invoke(userData.Font, userData.FontData, out extents)); }
private void ComposeElement(GuiComposer composer, JsonDialogSettings settings, DialogElement elem, int elemKey, double x, double y) { double factor = settings.SizeMultiplier; double labelWidth = 0; if (elem.Label != null) { CairoFont font = CairoFont.WhiteSmallText(); font.UnscaledFontsize *= factor; TextExtents extents = font.GetTextExtents(elem.Label); labelWidth = extents.Width / factor / RuntimeEnv.GUIScale + 1; FontExtents fext = font.GetFontExtents(); ElementBounds labelBounds = ElementBounds.Fixed(x, y + Math.Max(0, (elem.Height * factor - fext.Height / RuntimeEnv.GUIScale) / 2), labelWidth, elem.Height).WithScale(factor); composer.AddStaticText(elem.Label, font, labelBounds); labelWidth += 8; if (elem.Tooltip != null) { CairoFont tfont = CairoFont.WhiteSmallText(); tfont.UnscaledFontsize *= factor; composer.AddHoverText(elem.Tooltip, tfont, 350, labelBounds.FlatCopy(), "tooltip-" + elem.Code); composer.GetHoverText("tooltip-" + elem.Code).SetAutoWidth(true); } } ElementBounds bounds = ElementBounds.Fixed(x + labelWidth, y, elem.Width - labelWidth, elem.Height).WithScale(factor); string currentValue = settings.OnGet?.Invoke(elem.Code); switch (elem.Type) { case EnumDialogElementType.Slider: { string key = "slider-" + elemKey; composer.AddSlider((newval) => { settings.OnSet?.Invoke(elem.Code, newval + ""); return(true); }, bounds, key); int curVal = 0; int.TryParse(currentValue, out curVal); composer.GetSlider(key).SetValues(curVal, elem.MinValue, elem.MaxValue, elem.Step); composer.GetSlider(key).Scale = factor; //composer.GetSlider(key).TriggerOnlyOnMouseUp(true); break; } case EnumDialogElementType.Switch: { string key = "switch-" + elemKey; composer.AddSwitch((newval) => { settings.OnSet?.Invoke(elem.Code, newval ? "1" : "0"); }, bounds, key, 30 * factor, 5 * factor); composer.GetSwitch(key).SetValue(currentValue == "1"); } break; case EnumDialogElementType.Input: { string key = "input-" + elemKey; CairoFont font = CairoFont.WhiteSmallText(); font.UnscaledFontsize *= factor; composer.AddTextInput(bounds, (newval) => { settings.OnSet?.Invoke(elem.Code, newval); }, font, key); composer.GetTextInput(key).SetValue(currentValue); break; } case EnumDialogElementType.NumberInput: { string key = "numberinput-" + elemKey; CairoFont font = CairoFont.WhiteSmallText(); font.UnscaledFontsize *= factor; composer.AddNumberInput(bounds, (newval) => { settings.OnSet?.Invoke(elem.Code, newval); }, font, key); composer.GetNumberInput(key).SetValue(currentValue); break; } case EnumDialogElementType.Button: if (elem.Icon != null) { composer.AddIconButton(elem.Icon, (val) => { settings.OnSet?.Invoke(elem.Code, null); }, bounds); } else { CairoFont font = CairoFont.ButtonText(); font.WithFontSize(elem.FontSize); composer.AddButton(elem.Text, () => { settings.OnSet?.Invoke(elem.Code, null); return(true); }, bounds.WithFixedPadding(8, 0), font); } if (elem.Tooltip != null && elem.Label == null) { CairoFont tfont = CairoFont.WhiteSmallText(); tfont.UnscaledFontsize *= factor; composer.AddHoverText(elem.Tooltip, tfont, 350, bounds.FlatCopy(), "tooltip-" + elem.Code); composer.GetHoverText("tooltip-" + elem.Code).SetAutoWidth(true); } break; case EnumDialogElementType.Text: composer.AddStaticText(elem.Text, CairoFont.WhiteMediumText().WithFontSize(elem.FontSize), bounds); break; case EnumDialogElementType.Select: case EnumDialogElementType.DynamicSelect: { string[] values = elem.Values; string[] names = elem.Names; if (elem.Type == EnumDialogElementType.DynamicSelect) { string[] compos = currentValue.Split(new string[] { "\n" }, StringSplitOptions.None); values = compos[0].Split(new string[] { "||" }, StringSplitOptions.None); names = compos[1].Split(new string[] { "||" }, StringSplitOptions.None); currentValue = compos[2]; } int selectedIndex = Array.FindIndex(values, w => w.Equals(currentValue)); if (elem.Mode == EnumDialogElementMode.DropDown) { string key = "dropdown-" + elemKey; composer.AddDropDown(values, names, selectedIndex, (newval, on) => { settings.OnSet?.Invoke(elem.Code, newval); }, bounds, key); composer.GetDropDown(key).Scale = factor; composer.GetDropDown(key).Font.UnscaledFontsize *= factor; } else { if (elem.Icons != null && elem.Icons.Length > 0) { ElementBounds[] manybounds = new ElementBounds[elem.Icons.Length]; double elemHeight = (elem.Height - 4 * elem.Icons.Length) / elem.Icons.Length; for (int i = 0; i < manybounds.Length; i++) { manybounds[i] = bounds.FlatCopy().WithFixedHeight(elemHeight - 4).WithFixedOffset(0, i * (4 + elemHeight)).WithScale(factor); } string key = "togglebuttons-" + elemKey; CairoFont font = CairoFont.WhiteSmallText(); font.UnscaledFontsize *= factor; composer.AddIconToggleButtons(elem.Icons, font, (newval) => { settings.OnSet?.Invoke(elem.Code, elem.Values[newval]); }, manybounds, key); if (currentValue != null && currentValue.Length > 0) { composer.ToggleButtonsSetValue(key, selectedIndex); } if (elem.Tooltips != null) { for (int i = 0; i < elem.Tooltips.Length; i++) { CairoFont tfont = CairoFont.WhiteSmallText(); tfont.UnscaledFontsize *= factor; composer.AddHoverText(elem.Tooltips[i], tfont, 350, manybounds[i].FlatCopy()); } } } } } break; } elementNumber++; }
protected override void onDraw(Context gr) { base.onDraw(gr); gr.SelectFontFace(Font.Name, Font.Slant, Font.Wheight); gr.SetFontSize(Font.Size); gr.FontOptions = Interface.FontRenderingOptions; gr.Antialias = Interface.Antialias; rText = new Rectangle(new Size( measureRawSize(LayoutingType.Width), measureRawSize(LayoutingType.Height))); rText.Width -= 2 * Margin; rText.Height -= 2 * Margin; widthRatio = 1f; heightRatio = 1f; Rectangle cb = ClientRectangle; rText.X = cb.X; rText.Y = cb.Y; if (horizontalStretch) { widthRatio = (float)cb.Width / (float)rText.Width; if (!verticalStretch) { heightRatio = widthRatio; } } if (verticalStretch) { heightRatio = (float)cb.Height / (float)rText.Height; if (!horizontalStretch) { widthRatio = heightRatio; } } rText.Width = (int)(widthRatio * (float)rText.Width); rText.Height = (int)(heightRatio * (float)rText.Height); switch (TextAlignment) { case Alignment.TopLeft: //ok rText.X = cb.X; rText.Y = cb.Y; break; case Alignment.Top: //ok rText.Y = cb.Y; rText.X = cb.X + cb.Width / 2 - rText.Width / 2; break; case Alignment.TopRight: //ok rText.Y = cb.Y; rText.X = cb.Right - rText.Width; break; case Alignment.Left: //ok rText.X = cb.X; rText.Y = cb.Y + cb.Height / 2 - rText.Height / 2; break; case Alignment.Right: //ok rText.X = cb.X + cb.Width - rText.Width; rText.Y = cb.Y + cb.Height / 2 - rText.Height / 2; break; case Alignment.Bottom: //ok rText.X = cb.Width / 2 - rText.Width / 2; rText.Y = cb.Height - rText.Height; break; case Alignment.BottomLeft: //ok rText.X = cb.X; rText.Y = cb.Bottom - rText.Height; break; case Alignment.BottomRight: //ok rText.Y = cb.Bottom - rText.Height; rText.X = cb.Right - rText.Width; break; case Alignment.Center: //ok rText.X = cb.X + cb.Width / 2 - rText.Width / 2; rText.Y = cb.Y + cb.Height / 2 - rText.Height / 2; break; } gr.FontMatrix = new Matrix(widthRatio * (float)Font.Size, 0, 0, heightRatio * (float)Font.Size, 0, 0); fe = gr.FontExtents; #region draw text cursor if (HasFocus && Selectable) { if (mouseLocalPos >= 0) { computeTextCursor(gr); if (SelectionInProgress) { if (SelBegin < 0) { SelBegin = new Point(CurrentColumn, CurrentLine); SelStartCursorPos = textCursorPos; SelRelease = -1; } else { SelRelease = new Point(CurrentColumn, CurrentLine); if (SelRelease == SelBegin) { SelRelease = -1; } else { SelEndCursorPos = textCursorPos; } } } else { computeTextCursorPosition(gr); } } else { computeTextCursorPosition(gr); } Foreground.SetAsSource(gr); gr.LineWidth = 1.0; gr.MoveTo(0.5 + textCursorPos + rText.X, rText.Y + CurrentLine * fe.Height); gr.LineTo(0.5 + textCursorPos + rText.X, rText.Y + (CurrentLine + 1) * fe.Height); gr.Stroke(); } #endregion //****** debug selection ************* // if (SelRelease >= 0) { // new SolidColor(Color.DarkGreen).SetAsSource(gr); // Rectangle R = new Rectangle ( // rText.X + (int)SelEndCursorPos - 3, // rText.Y + (int)(SelRelease.Y * fe.Height), // 6, // (int)fe.Height); // gr.Rectangle (R); // gr.Fill (); // } // if (SelBegin >= 0) { // new SolidColor(Color.DarkRed).SetAsSource(gr); // Rectangle R = new Rectangle ( // rText.X + (int)SelStartCursorPos - 3, // rText.Y + (int)(SelBegin.Y * fe.Height), // 6, // (int)fe.Height); // gr.Rectangle (R); // gr.Fill (); // } //******************* for (int i = 0; i < lines.Count; i++) { string l = lines [i].Replace("\t", new String(' ', Interface.TabSize)); int lineLength = (int)gr.TextExtents(l).XAdvance; Rectangle lineRect = new Rectangle( rText.X, rText.Y + (int)Math.Ceiling(i * fe.Height), lineLength, (int)Math.Ceiling(fe.Height)); // if (TextAlignment == Alignment.Center || // TextAlignment == Alignment.Top || // TextAlignment == Alignment.Bottom) // lineRect.X += (rText.Width - lineLength) / 2; // else if (TextAlignment == Alignment.Right || // TextAlignment == Alignment.TopRight || // TextAlignment == Alignment.BottomRight) // lineRect.X += (rText.Width - lineLength); if (string.IsNullOrWhiteSpace(l)) { continue; } Foreground.SetAsSource(gr); gr.MoveTo(lineRect.X, rText.Y + fe.Ascent + fe.Height * i); gr.ShowText(l); gr.Fill(); if (Selectable) { if (SelRelease >= 0 && i >= selectionStart.Y && i <= selectionEnd.Y) { gr.SetSourceColor(selBackground); Rectangle selRect = lineRect; int cpStart = (int)SelStartCursorPos, cpEnd = (int)SelEndCursorPos; if (SelBegin.Y > SelRelease.Y) { cpStart = cpEnd; cpEnd = (int)SelStartCursorPos; } if (i == selectionStart.Y) { selRect.Width -= cpStart; selRect.Left += cpStart; } if (i == selectionEnd.Y) { selRect.Width -= (lineLength - cpEnd); } gr.Rectangle(selRect); gr.FillPreserve(); gr.Save(); gr.Clip(); gr.SetSourceColor(SelectionForeground); gr.MoveTo(lineRect.X, rText.Y + fe.Ascent + fe.Height * i); gr.ShowText(l); gr.Fill(); gr.Restore(); } } } }
protected override void onDraw(Context gr) { base.onDraw(gr); /*gr.FontFace = Font.Name; * gr.FontSize = (uint)Font.Size;*/ FontExtents fe = gr.FontExtents; Rectangle r = ClientRectangle; r.Inflate(-(int)fe.Height); double gridSize; double gridX = r.X; double gridY = r.Y; if (r.Width > r.Height) { gridX += (r.Width - r.Height) / 2; gridSize = r.Height; } else { gridY += (r.Height - r.Width) / 2; gridSize = r.Width; } Fill white = new SolidColor(Colors.Ivory); white.SetAsSource(IFace, gr); gr.Rectangle(gridX, gridY, gridSize, gridSize); gr.Fill(); double cellSize = gridSize / 8; Fill black = new SolidColor(Colors.Grey); black.SetAsSource(IFace, gr); for (int x = 0; x < 8; x++) { for (int y = 0; y < 8; y++) { if ((x + y) % 2 != 0) { gr.Rectangle(gridX + x * cellSize, gridY + y * cellSize, cellSize, cellSize); } } } gr.Fill(); Foreground.SetAsSource(IFace, gr); for (int x = 0; x < 8; x++) { string L = new string(new char[] { (char)(x + 97) }); gr.MoveTo(gridX + x * cellSize - gr.TextExtents(L).XAdvance / 2 + cellSize / 2, gridY + gridSize + fe.Ascent + 1); gr.ShowText(L); } for (int y = 0; y < 8; y++) { string L = (y + 1).ToString(); gr.MoveTo(gridX - gr.TextExtents(L).XAdvance - 1, gridY + fe.Ascent + cellSize * (7 - y) + cellSize / 2 - fe.Height / 2); gr.ShowText(L); } gr.Fill(); if (board == null) { return; } for (int x = 0; x < 8; x++) { for (int y = 0; y < 8; y++) { if (board [x, y] == null) { continue; } string spriteName; if (board [x, y].Player == ChessColor.White) { spriteName = "w"; } else { spriteName = "b"; } switch (board[x, y].Type) { case PieceType.Pawn: spriteName += "p"; break; case PieceType.Rook: spriteName += "r"; break; case PieceType.Knight: spriteName += "k"; break; case PieceType.Bishop: spriteName += "b"; break; case PieceType.King: spriteName += "K"; break; case PieceType.Queen: spriteName += "q"; break; } sprites.Paint(IFace, gr, new Rectangle( (int)(gridX + x * cellSize), (int)(gridY + (7 - y) * cellSize), (int)cellSize, (int)cellSize), spriteName); } } }