Previous commit

This commit is contained in:
Saveliy Savenak
2024-08-30 16:48:26 +03:00
parent 0e330bc47d
commit 4e18d216e6
7 changed files with 117 additions and 296 deletions

View File

@@ -2,69 +2,85 @@
using CairoObjective;
using FractalTreeGtk.Draw;
using System;
using System.Threading;
using System.Threading.Tasks;
using System.Diagnostics;
using Key = Gdk.Key;
namespace FractalTreeGtk
{
internal class CairoWindow : Window
{
static int fractallevels = 2;
DrawingArea drawing = new DrawingArea();
Fractal fractal;
public CairoWindow(string title) : base(title) {
private int FractalLevels { get; set; } = 2;
readonly DrawingArea _drawing = new DrawingArea();
Fractal _fractal = null!;
public CairoWindow(string title) : base(title)
{
Fullscreen();
drawing.Drawn += Drawing_Drawn;
Add(drawing);
_drawing.Drawn += Render;
Add(_drawing);
ShowAll();
KeyPressEvent += CairoWindow_KeyPressEvent;
CreateNewFractal();
GenerateFractal();
}
public void CreateNewFractal()
private void GenerateFractal()
{
fractal = new Fractal(fractallevels, this);
Trace.WriteLine("Generating fractal");
_fractal = new Fractal(FractalLevels, this);
}
private void CairoWindow_KeyPressEvent(object o, KeyPressEventArgs args)
{
if(args.Event.Key == Gdk.Key.equal)
switch (args.Event.Key)
{
++fractallevels;
CreateNewFractal();
}
if (args.Event.Key == Gdk.Key.minus && fractallevels > 1)
{
--fractallevels;
CreateNewFractal();
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
GenerateFractal();
break;
}
}
private void Drawing_Drawn(object o, DrawnArgs args)
private void Render(object o, DrawnArgs args)
{
long branches = 0;
Text.CenterText = false;
args.Cr.Translate(AllocatedWidth / 2, AllocatedHeight);
args.Cr.Translate(AllocatedWidth / 2f, AllocatedHeight);
Set.Context = args.Cr;
Set.Background(new Cairo.Color(0, 0, 0));
foreach (CairoObjective.DrawObjects.Line[] lines in (CairoObjective.DrawObjects.Line[][])fractal.Fractallines)
foreach (CairoObjective.DrawObjects.Line[] lines in
_fractal.FractalLines as CairoObjective.DrawObjects.Line[][])
{
try
{
foreach (CairoObjective.DrawObjects.Line line in lines) {
foreach (CairoObjective.DrawObjects.Line line in lines)
{
branches++;
Line.Make(line);
}
}
catch (NullReferenceException)
catch (NullReferenceException e)
{
Trace.WriteLine("Hello Null");
Trace.WriteLine(e);
break;
}
}
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));
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));
}
}
}
}