DrawingExamples extended and splitted into seperate Widgets
This commit is contained in:
Родитель
f22f26fd6a
Коммит
e011cce6bd
|
@ -62,7 +62,10 @@ namespace Samples
|
|||
AddSample (n, "Canvas with Widget", typeof(CanvasWithWidget));
|
||||
AddSample (n, "Chart", typeof(ChartSample));
|
||||
AddSample (n, "Colors", typeof(ColorsSample));
|
||||
AddSample (n, "Figures", typeof(DrawingFigures));
|
||||
AddSample (n, "Transformations", typeof(DrawingTransforms));
|
||||
AddSample (n, "Images and Patterns", typeof(DrawingPatternsAndImages));
|
||||
AddSample (n, "Text", typeof(DrawingTexts));
|
||||
|
||||
AddSample (null, "Frames", typeof(Frames));
|
||||
AddSample (null, "Images", typeof(Images));
|
||||
|
|
|
@ -29,42 +29,86 @@ using Xwt.Drawing;
|
|||
|
||||
namespace Samples
|
||||
{
|
||||
public class DrawingTransforms: Canvas
|
||||
public class Drawings: Canvas
|
||||
{
|
||||
public DrawingTransforms ()
|
||||
|
||||
public enum Test
|
||||
{
|
||||
Figures,
|
||||
PatternsAndImages,
|
||||
Texts,
|
||||
Transforms
|
||||
}
|
||||
|
||||
protected override void OnDraw (Xwt.Drawing.Context ctx)
|
||||
Test test = Test.Figures;
|
||||
|
||||
public Drawings (Test test)
|
||||
{
|
||||
this.test = test;
|
||||
}
|
||||
|
||||
protected override void OnDraw (Context ctx)
|
||||
{
|
||||
base.OnDraw (ctx);
|
||||
ctx.SetColor(Colors.White);
|
||||
ctx.Rectangle(this.Bounds);
|
||||
ctx.Fill();
|
||||
switch (test) {
|
||||
case Test.Figures:
|
||||
Figures (ctx, 5, 5);
|
||||
break;
|
||||
case Test.PatternsAndImages:
|
||||
PatternsAndImages (ctx, 5, 5);
|
||||
break;
|
||||
case Test.Texts:
|
||||
Texts (ctx, 5, 5);
|
||||
break;
|
||||
case Test.Transforms:
|
||||
Transforms (ctx, 5, 5);
|
||||
break;
|
||||
}
|
||||
ctx.SetColor(this.BackgroundColor);
|
||||
}
|
||||
|
||||
public virtual void Figures (Context ctx, double x, double y)
|
||||
{
|
||||
Rectangles (ctx, x, y);
|
||||
Curves1 (ctx, x, y + 60);
|
||||
}
|
||||
|
||||
public virtual void Rectangles (Context ctx, double x, double y)
|
||||
{
|
||||
ctx.Save ();
|
||||
ctx.Translate (x, y);
|
||||
|
||||
// Simple rectangles
|
||||
|
||||
ctx.SetLineWidth (1);
|
||||
ctx.Rectangle (100, 5, 10, 10);
|
||||
ctx.Rectangle (0, 0, 10, 10);
|
||||
ctx.SetColor (Colors.Black);
|
||||
ctx.Fill ();
|
||||
|
||||
ctx.Rectangle (115, 5, 10, 10);
|
||||
ctx.Rectangle (15, 0, 10, 10);
|
||||
ctx.SetColor (Colors.Black);
|
||||
ctx.Stroke ();
|
||||
|
||||
//
|
||||
|
||||
ctx.SetLineWidth (3);
|
||||
ctx.Rectangle (100, 20, 10, 10);
|
||||
ctx.Rectangle (0, 15, 10, 10);
|
||||
ctx.SetColor (Colors.Black);
|
||||
ctx.Fill ();
|
||||
|
||||
ctx.Rectangle (115, 20, 10, 10);
|
||||
ctx.Rectangle (15, 15, 10, 10);
|
||||
ctx.SetColor (Colors.Black);
|
||||
ctx.Stroke ();
|
||||
|
||||
// Rectangle with hole
|
||||
ctx.Restore ();
|
||||
|
||||
ctx.Rectangle (10, 100, 40, 40);
|
||||
ctx.MoveTo (45, 135);
|
||||
// Rectangle with hole
|
||||
ctx.Save ();
|
||||
ctx.Translate (x + 50, y);
|
||||
|
||||
ctx.Rectangle (0, 0, 40, 40);
|
||||
ctx.MoveTo (35, 35);
|
||||
ctx.RelLineTo (0, -20);
|
||||
ctx.RelLineTo (-20, 0);
|
||||
ctx.RelLineTo (0, 20);
|
||||
|
@ -72,34 +116,166 @@ namespace Samples
|
|||
ctx.SetColor (Colors.Black);
|
||||
ctx.Fill ();
|
||||
|
||||
ctx.Restore ();
|
||||
|
||||
// Rounded Rectangle with Arcs
|
||||
ctx.Save ();
|
||||
ctx.Translate (x + 120, y);
|
||||
|
||||
var r = 5;
|
||||
var l = 0;
|
||||
var t = 0;
|
||||
var w = 50;
|
||||
var h = 30;
|
||||
|
||||
ctx.SetColor (Colors.Black);
|
||||
// top left
|
||||
ctx.Arc (l + r, t + r, r, 180, 270);
|
||||
// top right
|
||||
ctx.Arc (l + w - r, t + r, r, 270, 0);
|
||||
// bottom right
|
||||
ctx.Arc (l + w - r, t + h - r, r, 0, 90);
|
||||
// bottom left
|
||||
ctx.Arc (l + r, t + h - r, r, 90, 180);
|
||||
|
||||
ctx.ClosePath ();
|
||||
ctx.StrokePreserve ();
|
||||
ctx.SetColor (Colors.AntiqueWhite);
|
||||
ctx.Fill ();
|
||||
ctx.Restore ();
|
||||
}
|
||||
|
||||
public virtual void Curves1 (Context ctx, double x, double y)
|
||||
{
|
||||
ctx.Save ();
|
||||
ctx.Translate (x, y);
|
||||
|
||||
ctx.SetLineWidth (1);
|
||||
Action curve1 = () => {
|
||||
ctx.MoveTo (0, 30);
|
||||
ctx.CurveTo (20, 0, 50, 0, 60, 25);
|
||||
};
|
||||
Action curve2 = () => {
|
||||
ctx.LineTo (0, 0);
|
||||
ctx.CurveTo (20, 30, 50, 30, 60, 5);
|
||||
};
|
||||
Action paint = () => {
|
||||
curve1 ();
|
||||
curve2 ();
|
||||
ctx.ClosePath ();
|
||||
ctx.SetColor (new Color (0, 0, 0, .5));
|
||||
ctx.StrokePreserve ();
|
||||
ctx.SetColor (new Color (1, 0, 1, .5));
|
||||
ctx.Fill ();
|
||||
};
|
||||
paint ();
|
||||
|
||||
ctx.Translate (0, 40);
|
||||
curve2 = () => {
|
||||
ctx.MoveTo (0, 0);
|
||||
ctx.CurveTo (20, 30, 50, 30, 60, 5);
|
||||
};
|
||||
paint ();
|
||||
ctx.Restore ();
|
||||
}
|
||||
|
||||
public virtual void PatternsAndImages (Context ctx, double x, double y)
|
||||
{
|
||||
ctx.Save ();
|
||||
ctx.Translate (x, y);
|
||||
|
||||
ctx.SetColor(Colors.Black);
|
||||
// Dashed lines
|
||||
|
||||
ctx.SetLineDash (15, 10, 10, 5, 5);
|
||||
ctx.Rectangle (100, 100, 100, 100);
|
||||
ctx.Rectangle (10, 10, 100, 100);
|
||||
ctx.Stroke ();
|
||||
ctx.SetLineDash (0);
|
||||
|
||||
// Image
|
||||
var arcColor = new Color (1, 0, 1);
|
||||
ImageBuilder ib = new ImageBuilder (30, 30, ImageFormat.ARGB32);
|
||||
ib.Context.Arc (15, 15, 15, 0, 360);
|
||||
ib.Context.SetColor (new Color (1, 0, 1));
|
||||
ib.Context.SetColor (arcColor);
|
||||
ib.Context.Fill ();
|
||||
ib.Context.SetColor (Colors.DarkKhaki);
|
||||
ib.Context.Rectangle (0, 0, 5, 5);
|
||||
ib.Context.Fill ();
|
||||
var img = ib.ToImage ();
|
||||
ctx.DrawImage (img, 90, 90);
|
||||
ctx.DrawImage (img, 90, 140, 50, 10);
|
||||
ctx.DrawImage (img, 0, 0);
|
||||
ctx.DrawImage (img, 0, 50, 50, 10);
|
||||
|
||||
ctx.Arc (190, 190, 15, 0, 360);
|
||||
ctx.SetColor (new Color (1, 0, 1, 0.4));
|
||||
ctx.Arc (100, 100, 15, 0, 360);
|
||||
arcColor.Alpha=0.4;
|
||||
ctx.SetColor (arcColor);
|
||||
ctx.Fill ();
|
||||
|
||||
// ImagePattern
|
||||
|
||||
ctx.Save ();
|
||||
ctx.Translate (90, 220);
|
||||
|
||||
ctx.Translate (x + 130, y);
|
||||
ctx.Pattern = new ImagePattern (img);
|
||||
ctx.Rectangle (0, 0, 100, 70);
|
||||
ctx.Rectangle (0, 0, 100, 100);
|
||||
ctx.Fill ();
|
||||
ctx.Restore ();
|
||||
|
||||
ctx.Translate (30, 30);
|
||||
ctx.Restore ();
|
||||
}
|
||||
|
||||
public virtual void Texts (Xwt.Drawing.Context ctx, double x, double y)
|
||||
{
|
||||
ctx.Save ();
|
||||
|
||||
ctx.Translate (x, y);
|
||||
|
||||
ctx.SetColor (Colors.Black);
|
||||
|
||||
var text = new TextLayout (ctx);
|
||||
text.Font = this.Font.WithSize (10);
|
||||
|
||||
text.Text = "Lorem ipsum dolor sit amet,";
|
||||
ctx.DrawTextLayout (text, 0, 0);
|
||||
var size1 = text.GetSize ();
|
||||
|
||||
// proofing width; test should align with text above
|
||||
ctx.SetColor (Colors.DarkMagenta);
|
||||
text.Text = "consetetur sadipscing elitr, sed diam nonumy";
|
||||
text.Width = size1.Width;
|
||||
ctx.DrawTextLayout (text, 0, size1.Height+10);
|
||||
|
||||
var size2 = text.GetSize ();
|
||||
text.Text = string.Format ("Size 1 {0}\r\nSize 2 {1}", size1, size2);
|
||||
|
||||
ctx.Save ();
|
||||
ctx.SetColor (Colors.Black);
|
||||
|
||||
ctx.Rotate (10);
|
||||
// maybe someone knows a formula with angle and textsize to calculyte ty
|
||||
var ty = 30;
|
||||
ctx.DrawTextLayout (text, ty, size1.Height+size2.Height+20);
|
||||
|
||||
ctx.Restore ();
|
||||
|
||||
// scale example here:
|
||||
|
||||
ctx.Restore ();
|
||||
}
|
||||
|
||||
public virtual void Transforms (Xwt.Drawing.Context ctx, double x, double y)
|
||||
{
|
||||
Rotate (ctx, x, y);
|
||||
Scale (ctx, x + 100, y);
|
||||
|
||||
}
|
||||
|
||||
public virtual void Rotate (Xwt.Drawing.Context ctx, double x, double y)
|
||||
{
|
||||
ctx.Save ();
|
||||
ctx.Translate (x + 30, y + 30);
|
||||
|
||||
// Rotation
|
||||
|
||||
double end = 270;
|
||||
|
||||
for (double n = 0; n<=end; n += 5) {
|
||||
|
@ -114,6 +290,58 @@ namespace Samples
|
|||
}
|
||||
|
||||
ctx.ResetTransform ();
|
||||
ctx.Restore ();
|
||||
}
|
||||
|
||||
public virtual void Scale (Context ctx, double ax, double ay)
|
||||
{
|
||||
ctx.Save ();
|
||||
ctx.Translate (ax, ay);
|
||||
|
||||
ctx.SetLineWidth (1);
|
||||
|
||||
var x = 0d;
|
||||
var y = 0d;
|
||||
var w = 10d;
|
||||
|
||||
for (var i = 1d; i < 3; i += .5) {
|
||||
ctx.Save ();
|
||||
ctx.Rectangle (x, y, w, w);
|
||||
// ctx.Scale (i, i);
|
||||
ctx.Stroke ();
|
||||
ctx.Restore ();
|
||||
ctx.MoveTo (x += w / 2, y += w / 2);
|
||||
}
|
||||
|
||||
ctx.Restore ();
|
||||
}
|
||||
}
|
||||
|
||||
public class DrawingFigures:Drawings
|
||||
{
|
||||
public DrawingFigures ():base(Drawings.Test.Figures)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
public class DrawingPatternsAndImages:Drawings
|
||||
{
|
||||
public DrawingPatternsAndImages ():base(Drawings.Test.PatternsAndImages)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
public class DrawingTexts:Drawings
|
||||
{
|
||||
public DrawingTexts ():base(Drawings.Test.Texts)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
public class DrawingTransforms:Drawings
|
||||
{
|
||||
public DrawingTransforms ():base(Drawings.Test.Transforms)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -63,11 +63,13 @@ namespace Xwt.CairoBackend
|
|||
CairoContextBackend gc = (CairoContextBackend) backend;
|
||||
gc.GlobalAlpha = alpha;
|
||||
}
|
||||
|
||||
const double degrees = System.Math.PI / 180d;
|
||||
|
||||
public void Arc (object backend, double xc, double yc, double radius, double angle1, double angle2)
|
||||
{
|
||||
CairoContextBackend gc = (CairoContextBackend) backend;
|
||||
gc.Context.Arc (xc, yc, radius, (angle1 * System.Math.PI) / 180, (angle2 * System.Math.PI) / 180);
|
||||
CairoContextBackend gc = (CairoContextBackend)backend;
|
||||
gc.Context.Arc (xc, yc, radius, angle1 * degrees, angle2 * degrees);
|
||||
}
|
||||
|
||||
public void Clip (object backend)
|
||||
|
|
|
@ -95,11 +95,11 @@
|
|||
<Compile Include="Xwt.GtkBackend\FileDialogBackend.cs" />
|
||||
<Compile Include="Xwt.GtkBackend\PanedBackend.cs" />
|
||||
<Compile Include="Xwt.GtkBackend\SelectColorDialogBackend.cs" />
|
||||
<Compile Include="Xwt.CairoBackend\GradientBackendHandler.cs" />
|
||||
<Compile Include="Xwt.CairoBackend\CairoConversion.cs" />
|
||||
<Compile Include="Xwt.CairoBackend\CairoTextLayoutBackendHandler.cs" />
|
||||
<Compile Include="Xwt.CairoBackend\CairoContextBackendHandler.cs" />
|
||||
<Compile Include="Xwt.GtkBackend\ContextBackendHandler.cs" />
|
||||
<Compile Include="Xwt.CairoBackend\CairoGradientBackendHandler.cs" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
|
||||
<ItemGroup>
|
||||
|
|
|
@ -83,13 +83,14 @@ namespace Xwt.Drawing
|
|||
/// <summary>
|
||||
/// Adds a circular arc of the given radius to the current path.
|
||||
/// The arc is centered at (xc, yc),
|
||||
/// begins at angle1 and proceeds in the direction of increasing angles to end at angle2.
|
||||
/// If angle2 is less than angle1
|
||||
/// it will be progressively increased by 2*M_PI until it is greater than angle1.
|
||||
/// begins at angle1 and proceeds in the direction
|
||||
/// of increasing angles to end at angle2.
|
||||
/// If angle2 is less than angle1,
|
||||
/// it will be progressively increased by 2*Math.PI until it is greater than angle1.
|
||||
/// If there is a current point, an initial line segment will be added to the path
|
||||
/// to connect the current point to the beginning of the arc.
|
||||
/// If this initial line is undesired,
|
||||
/// it can be avoided by calling begin_new_sub_path() before calling arc().
|
||||
/// it can be avoided by calling NewPath() before calling Arc().
|
||||
/// </summary>
|
||||
/// <param name='xc'>
|
||||
/// Xc.
|
||||
|
@ -101,10 +102,10 @@ namespace Xwt.Drawing
|
|||
/// Radius.
|
||||
/// </param>
|
||||
/// <param name='angle1'>
|
||||
/// Angle1.
|
||||
/// Angle1 in degrees
|
||||
/// </param>
|
||||
/// <param name='angle2'>
|
||||
/// Angle2.
|
||||
/// Angle2 in degrees
|
||||
/// </param>
|
||||
public void Arc (double xc, double yc, double radius, double angle1, double angle2)
|
||||
{
|
||||
|
|
Загрузка…
Ссылка в новой задаче