86 lines
2.8 KiB
C#
Raw Permalink Normal View History

using Gtk;
using CairoObjective;
using FractalTreeGtk.Draw;
2023-01-30 00:24:30 +03:00
using System;
using System.Diagnostics;
2024-08-30 16:48:26 +03:00
using Key = Gdk.Key;
namespace FractalTreeGtk
{
internal class CairoWindow : Window
{
2024-08-30 16:48:26 +03:00
private int FractalLevels { get; set; } = 2;
readonly DrawingArea _drawing = new();
2024-08-30 16:48:26 +03:00
Fractal _fractal = null!;
public CairoWindow(string title) : base(title)
{
Fullscreen();
2024-08-30 16:48:26 +03:00
_drawing.Drawn += Render;
Add(_drawing);
ShowAll();
2023-01-03 00:17:18 +03:00
KeyPressEvent += CairoWindow_KeyPressEvent;
2024-08-30 16:48:26 +03:00
GenerateFractal();
2023-01-30 00:24:30 +03:00
}
2024-08-30 16:48:26 +03:00
private void GenerateFractal()
2023-01-30 00:24:30 +03:00
{
2024-08-30 16:48:26 +03:00
Trace.WriteLine("Generating fractal");
_fractal = new Fractal(FractalLevels, this);
2023-01-03 00:17:18 +03:00
}
2024-08-30 16:48:26 +03:00
2023-01-03 00:17:18 +03:00
private void CairoWindow_KeyPressEvent(object o, KeyPressEventArgs args)
{
2024-08-30 16:48:26 +03:00
switch (args.Event.Key)
2023-01-03 00:17:18 +03:00
{
2024-08-30 16:48:26 +03:00
case Key.equal: //Increase fractal levels
case Key.plus:
FractalLevels++;
GenerateFractal();
break;
case Key.minus: //Decrease fractal levels
if (FractalLevels > 1)
{
FractalLevels--;
GenerateFractal();
}
break;
case Key.r: //Regenerate fractal
case Key.R:
2024-08-30 16:48:26 +03:00
GenerateFractal();
break;
2023-01-03 00:17:18 +03:00
}
}
2024-08-30 16:48:26 +03:00
private void Render(object o, DrawnArgs args)
{
2023-01-03 18:32:30 +03:00
long branches = 0;
Text.CenterText = false;
2024-08-30 16:48:26 +03:00
args.Cr.Translate(AllocatedWidth / 2f, AllocatedHeight);
Set.Context = args.Cr;
Set.Background(new Cairo.Color(0, 0, 0));
2024-08-30 16:48:26 +03:00
foreach (CairoObjective.DrawObjects.Line[] lines in
_fractal.FractalLines as CairoObjective.DrawObjects.Line[][])
{
2023-01-30 00:24:30 +03:00
try
{
2024-08-30 16:48:26 +03:00
foreach (CairoObjective.DrawObjects.Line line in lines)
{
2023-01-30 00:24:30 +03:00
branches++;
Line.Make(line);
}
}
2024-08-30 16:48:26 +03:00
catch (NullReferenceException e)
2023-01-30 00:24:30 +03:00
{
2024-08-30 16:48:26 +03:00
Trace.WriteLine(e);
2023-01-30 00:24:30 +03:00
break;
2023-01-03 18:32:30 +03:00
}
}
2024-08-30 16:48:26 +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));
2024-08-30 16:48:26 +03:00
Text.Make($"Levels: {FractalLevels}", -(AllocatedWidth / 2) + 1, -4, 21, new Cairo.Color(0, 0, 0));
Text.Make($"Total Branches: {branches}", -(AllocatedWidth / 2), -30, 21, new Cairo.Color(0.1, 1, 0.1));
Text.Make($"Levels: {FractalLevels}", -(AllocatedWidth / 2), -5, 21, new Cairo.Color(0.1, 1, 0.1));
}
}
2024-08-30 16:48:26 +03:00
}