FractalTree/FractalTreeGtk/CairoWindow.cs

71 lines
2.4 KiB
C#
Raw Normal View History

using Gtk;
using CairoObjective;
using FractalTreeGtk.Draw;
2023-01-30 00:24:30 +03:00
using System;
using System.Threading;
using System.Threading.Tasks;
using System.Diagnostics;
namespace FractalTreeGtk
{
internal class CairoWindow : Window
{
2023-01-03 00:17:18 +03:00
static int fractallevels = 2;
DrawingArea drawing = new DrawingArea();
2023-01-30 00:24:30 +03:00
Fractal fractal;
public CairoWindow(string title) : base(title) {
Fullscreen();
drawing.Drawn += Drawing_Drawn;
Add(drawing);
ShowAll();
2023-01-03 00:17:18 +03:00
KeyPressEvent += CairoWindow_KeyPressEvent;
2023-01-30 00:24:30 +03:00
CreateNewFractal();
}
public void CreateNewFractal()
{
fractal = new Fractal(fractallevels, this);
2023-01-03 00:17:18 +03:00
}
private void CairoWindow_KeyPressEvent(object o, KeyPressEventArgs args)
{
if(args.Event.Key == Gdk.Key.equal)
{
2023-01-30 00:24:30 +03:00
++fractallevels;
CreateNewFractal();
2023-01-03 00:17:18 +03:00
}
if (args.Event.Key == Gdk.Key.minus && fractallevels > 1)
{
2023-01-30 00:24:30 +03:00
--fractallevels;
CreateNewFractal();
2023-01-03 00:17:18 +03:00
}
}
private void Drawing_Drawn(object o, DrawnArgs args)
{
2023-01-03 18:32:30 +03:00
long branches = 0;
Text.CenterText = false;
args.Cr.Translate(AllocatedWidth / 2, AllocatedHeight);
Set.Context = args.Cr;
Set.Background(new Cairo.Color(0, 0, 0));
2023-01-03 18:32:30 +03:00
foreach (CairoObjective.DrawObjects.Line[] lines in (CairoObjective.DrawObjects.Line[][])fractal.Fractallines)
{
2023-01-30 00:24:30 +03:00
try
{
foreach (CairoObjective.DrawObjects.Line line in lines) {
branches++;
Line.Make(line);
}
}
catch (NullReferenceException)
{
Trace.WriteLine("Hello Null");
break;
2023-01-03 18:32:30 +03:00
}
}
2023-01-30 00:24:30 +03:00
Text.Make($"Total Branches: {branches}", -(AllocatedWidth / 2) + 1, -29, 21, new Cairo.Color(0, 0, 0));
Text.Make($"Levels: {fractallevels}", -(AllocatedWidth / 2) + 1, -4, 21, new Cairo.Color(0, 0, 0));
Text.Make($"Total Branches: {branches}", -(AllocatedWidth / 2), -30, new Cairo.Color(0.1, 1, 0.1));
Text.Make($"Levels: {fractallevels}", -(AllocatedWidth / 2), -5, new Cairo.Color(0.1,1,0.1));
}
}
}