2023-01-02 19:08:46 +03:00
|
|
|
|
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;
|
2023-01-02 19:08:46 +03:00
|
|
|
|
|
|
|
|
|
namespace FractalTreeGtk
|
|
|
|
|
{
|
|
|
|
|
internal class CairoWindow : Window
|
|
|
|
|
{
|
2024-08-30 16:48:26 +03:00
|
|
|
|
private int FractalLevels { get; set; } = 2;
|
2025-01-19 19:35:29 +03:00
|
|
|
|
readonly DrawingArea _drawing = new();
|
2024-08-30 16:48:26 +03:00
|
|
|
|
Fractal _fractal = null!;
|
|
|
|
|
|
|
|
|
|
public CairoWindow(string title) : base(title)
|
|
|
|
|
{
|
2023-01-02 19:08:46 +03:00
|
|
|
|
Fullscreen();
|
2024-08-30 16:48:26 +03:00
|
|
|
|
_drawing.Drawn += Render;
|
|
|
|
|
Add(_drawing);
|
2023-01-02 19:08:46 +03:00
|
|
|
|
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
|
2025-01-19 19:35:29 +03:00
|
|
|
|
case Key.R:
|
2024-08-30 16:48:26 +03:00
|
|
|
|
GenerateFractal();
|
|
|
|
|
break;
|
2023-01-03 00:17:18 +03:00
|
|
|
|
}
|
2023-01-02 19:08:46 +03:00
|
|
|
|
}
|
|
|
|
|
|
2024-08-30 16:48:26 +03:00
|
|
|
|
private void Render(object o, DrawnArgs args)
|
2023-01-02 19:08:46 +03:00
|
|
|
|
{
|
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);
|
2023-01-02 19:08:46 +03:00
|
|
|
|
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-02 19:08:46 +03:00
|
|
|
|
{
|
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
|
|
|
|
}
|
2023-01-02 19:08:46 +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));
|
2023-01-02 19:08:46 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
2024-08-30 16:48:26 +03:00
|
|
|
|
}
|