-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTela.cs
More file actions
146 lines (126 loc) · 4.79 KB
/
Tela.cs
File metadata and controls
146 lines (126 loc) · 4.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
using System;
using System.Drawing;
using System.Windows.Forms;
using ScreenSaverMatrixStyle.Classes;
namespace ScreenSaverMatrixStyle
{
public class Tela : Form
{
private System.Windows.Forms.Timer timer = null!;
private MatrixColumn[] columns = null!;
private int fontSize = 14;
private int cols;
private int rows;
private Random random = new Random();
private char[,] charGrid = null!;
private long frameCount = 0;
private Point? lastMousePosition = null;
public Tela()
{
// Basic Form setup without Designer
this.FormBorderStyle = FormBorderStyle.None;
this.WindowState = FormWindowState.Maximized;
this.BackColor = Color.Black;
this.DoubleBuffered = true;
Cursor.Hide(); // Hide cursor
this.MouseMove += Tela_MouseMove;
this.KeyDown += (s, e) => Application.Exit();
this.MouseDown += (s, e) => Application.Exit();
}
public Tela(Rectangle bounds) : this()
{
this.Bounds = bounds;
this.StartPosition = FormStartPosition.Manual;
}
private void Tela_MouseMove(object? sender, MouseEventArgs e)
{
if (lastMousePosition != null)
{
if (Math.Abs(lastMousePosition.Value.X - e.X) > 5 ||
Math.Abs(lastMousePosition.Value.Y - e.Y) > 5)
{
Application.Exit();
}
}
lastMousePosition = e.Location;
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
// Calculate grid dimensions
cols = this.Width / fontSize;
rows = this.Height / fontSize + 1; // +1 to cover bottom edge
columns = new MatrixColumn[cols];
charGrid = new char[cols, rows];
for (int i = 0; i < cols; i++)
{
columns[i] = new MatrixColumn(i, rows);
for (int j = 0; j < rows; j++)
{
charGrid[i, j] = GetRandomChar();
}
}
timer = new System.Windows.Forms.Timer();
timer.Interval = 30;
timer.Tick += Timer_Tick;
timer.Start();
}
private char GetRandomChar()
{
string chars = "$%#@!*abcdefghijklmnopqrstuvwxyz1234567890?;:ABCDEFGHIJKLMNOPQRSTUVWXYZ^&";
return chars[random.Next(chars.Length)];
}
private void Timer_Tick(object? sender, EventArgs e)
{
frameCount++;
for (int i = 0; i < columns.Length; i++)
{
var column = columns[i];
if (frameCount % column.Speed == 0)
{
column.MoveDown();
if (column.ShouldReset())
{
column.Reset();
}
}
}
this.Invalidate();
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
Graphics g = e.Graphics;
using (Font font = new Font("Consolas", fontSize))
using (Brush whiteBrush = new SolidBrush(Color.White))
using (Brush greenBrush = new SolidBrush(Color.FromArgb(0, 255, 0)))
using (Brush darkGreenBrush = new SolidBrush(Color.FromArgb(0, 128, 0)))
{
for (int i = 0; i < cols; i++)
{
var column = columns[i];
int x = i * fontSize;
int headY = column.Y;
int tailY = column.GetTailY();
// Optimization: only loop through visible part of the column
int startY = Math.Max(0, tailY);
int endY = Math.Min(rows - 1, headY);
for (int j = startY; j <= endY; j++)
{
char c = charGrid[i, j];
// Randomly change character for "glitch" effect
if (random.Next(40) == 0)
{
c = GetRandomChar();
charGrid[i, j] = c;
}
Brush brush = darkGreenBrush;
if (j == headY) brush = whiteBrush;
else if (j > headY - 2) brush = greenBrush;
g.DrawString(c.ToString(), font, brush, x, j * fontSize);
}
}
}
}
}
}