-
Notifications
You must be signed in to change notification settings - Fork 0
/
ScreenSaverForm.cs
358 lines (327 loc) · 13.7 KB
/
ScreenSaverForm.cs
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace ScreenSaver
{
public class ScreenSaverForm : Form
{
private Point MouseXY;
private readonly int ScreenNumber;
private PictureBox SSPictureBox;
public Bitmap SSBitmap;
public int SpriteColumns = 0;
public int SpriteRows = 0;
public int[,] UsedKatakanaIndexes = null;
private ColBitMap[] ColBitMaps = null;
int CurrentCreditLine = 0;
int CurrentLinePos = 0;
int CreditLineCyclesShown = 0;
int BeforeChangeCyclesShown = 0;
public bool DoShowCredits = true; //show credits on start
public bool DoShowConsole = false;
private long Cycles = 0;
public void ReorderColumnIndexesDownMove(int colIndex, bool clearfirst = false)
{
for (int i = SpriteRows - 1; i > 0; i--)
{
UsedKatakanaIndexes[colIndex, i] = UsedKatakanaIndexes[colIndex, i - 1];
}
if (clearfirst)
{
UsedKatakanaIndexes[colIndex, 0] = -1;
}
}
public ScreenSaverForm(int scrn)
{
ScreenNumber = scrn;
InitializeComponent();
}
protected override void Dispose(bool disposing)
{
ScreenInstances.ScrForms.Remove(this);
base.Dispose(disposing);
}
private void ScreenSaverForm_Load(object sender, EventArgs e)
{
Bounds = Screen.AllScreens[ScreenNumber].Bounds;
if (!System.Diagnostics.Debugger.IsAttached)
{
Cursor.Hide();
TopMost = true;
}
SSBitmap = new Bitmap(Bounds.Width, Bounds.Height);
SpriteColumns = (int)Math.Ceiling((double)SSBitmap.Width / 16);
SpriteRows = (int)Math.Ceiling((double)SSBitmap.Height / 24);
UsedKatakanaIndexes = new int[SpriteColumns, SpriteRows];
for (int i = 0; i < SpriteColumns; i++)
{
for (int u = 0; u < SpriteRows; u++)
{
UsedKatakanaIndexes[i,u] = -1;
}
}
ColBitMaps = new ColBitMap[SpriteColumns];
Array.Clear(ColBitMaps, 0, SpriteColumns);
}
public void DoShowScreenSaverCycle()
{
try
{
Invoke((MethodInvoker)delegate
{
if (DoShowCredits)
{
ShowCredits();
}
else
if (DoShowConsole)
{
ShowConsoleStrings();
}
else
{
using (Graphics graphics = Graphics.FromImage(SSBitmap))
{
int modcount = SpriteColumns / 10; //we will modify only 10% of columns per step
for (int i = 0; i < modcount; i++)
{
var selectedcol = ScreenInstances.EntropySrc.Next(SpriteColumns);
TransformColumn(selectedcol);
if (ColBitMaps[selectedcol].IsModified)
{
Point sp = new Point(selectedcol * 16, 0);
graphics.DrawImageUnscaled(ColBitMaps[selectedcol].ColumnBitMap, sp);
}
}
}
}
if (!DoShowCredits)
{
if (Cycles < 3000)
{
Cycles++;
}
else
{
Cycles = 0;
DoShowCredits = true;
using (Graphics graphics = Graphics.FromImage(SSBitmap))
{
graphics.Clear(Color.Black);
}
}
}
SSPictureBox.Image = SSBitmap;
SSPictureBox.Refresh();
});
}
catch (TaskCanceledException)
{
return;
}
catch (Exception ex)
{
var s = ex.ToString();
}
}
private void ShowCredits()
{
var startcol = (SpriteColumns - ScreenInstances.CreditsStrings[CurrentCreditLine].Length) / 2;
var row = SpriteRows / 2 + 1;
using (Graphics graphics = Graphics.FromImage(SSBitmap))
{
if (CreditLineCyclesShown < 10)
{
for (int i = 0; i <= CurrentLinePos; i++)
{
if (i < CurrentLinePos)
{
Point sp = new Point((startcol + CurrentLinePos) * 16, row * 24);
graphics.DrawImageUnscaled(ScreenInstances.GetKatakanaSprite(ScreenInstances.GetLetterSprite(ScreenInstances.CreditsStrings[CurrentCreditLine][i]), false), sp);
}
Point sp2 = new Point((startcol + CurrentLinePos + 1) * 16, row * 24);
graphics.DrawImageUnscaled(ScreenInstances.GetKatakanaSprite(114 + 120 * (CreditLineCyclesShown % 4), false), sp2);
CreditLineCyclesShown++;
}
}
else
{
CreditLineCyclesShown = 0;
if (CurrentLinePos < ScreenInstances.CreditsStrings[CurrentCreditLine].Length)
{
CurrentLinePos++;
}
else
{
if (BeforeChangeCyclesShown < 10)
{
Point sp2 = new Point((startcol + CurrentLinePos + 1) * 16, row * 24);
graphics.DrawImageUnscaled(ScreenInstances.GetKatakanaSprite(114 + 120 * (BeforeChangeCyclesShown % 4), false), sp2);
BeforeChangeCyclesShown++;
return;
}
else
{
BeforeChangeCyclesShown = 0;
}
CurrentLinePos = 0;
if (CurrentCreditLine < ScreenInstances.CreditsStrings.Count)
{
SolidBrush shadowBrush = new SolidBrush(Color.Black);
Rectangle clearrect = new Rectangle(startcol * 16, row * 24, (ScreenInstances.CreditsStrings[CurrentCreditLine].Length + 2) * 16, 24);
graphics.FillRectangle(shadowBrush, clearrect);
CurrentCreditLine++;
if (CurrentCreditLine == ScreenInstances.CreditsStrings.Count)
{
CurrentCreditLine = 0;
DoShowCredits = false;
DoShowConsole = true;
}
}
}
}
}
}
private void ShowConsoleStrings()
{
using (Graphics graphics = Graphics.FromImage(SSBitmap))
{
if (CreditLineCyclesShown < 10)
{
for (int i = 0; i <= CurrentLinePos; i++)
{
if (i < CurrentLinePos)
{
Point sp = new Point(CurrentLinePos * 16, 0);
graphics.DrawImageUnscaled(ScreenInstances.GetKatakanaSprite(ScreenInstances.GetLetterSprite(ScreenInstances.ConsoleStrings[CurrentCreditLine][i]), false), sp);
}
Point sp2 = new Point((CurrentLinePos + 1) * 16, 0);
graphics.DrawImageUnscaled(ScreenInstances.GetKatakanaSprite(114 + 120 * (CreditLineCyclesShown % 4), false), sp2);
CreditLineCyclesShown++;
}
}
else
{
CreditLineCyclesShown = 0;
if (CurrentLinePos < ScreenInstances.ConsoleStrings[CurrentCreditLine].Length)
{
CurrentLinePos++;
}
else
{
if (BeforeChangeCyclesShown < 10)
{
Point sp2 = new Point((CurrentLinePos + 1) * 16, 0);
graphics.DrawImageUnscaled(ScreenInstances.GetKatakanaSprite(114 + 120 * (BeforeChangeCyclesShown % 4), false), sp2);
BeforeChangeCyclesShown++;
return;
}
else
{
BeforeChangeCyclesShown = 0;
}
CurrentLinePos = 0;
if (CurrentCreditLine < ScreenInstances.CreditsStrings.Count)
{
SolidBrush shadowBrush = new SolidBrush(Color.Black);
Rectangle clearrect = new Rectangle(0, 0, (ScreenInstances.ConsoleStrings[CurrentCreditLine].Length + 2) * 16, 24);
graphics.FillRectangle(shadowBrush, clearrect);
CurrentCreditLine++;
if (CurrentCreditLine == ScreenInstances.ConsoleStrings.Count)
{
CurrentCreditLine = 0;
DoShowConsole = false;
}
}
}
}
}
}
private void TransformColumn(int colIndex)
{
if (ColBitMaps[colIndex] == null)
{
ColBitMaps[colIndex] = new ColBitMap(colIndex, ScreenNumber);
}
ColBitMaps[colIndex].IsModified = false;
var first_index = ColBitMaps[colIndex].FirstSpriteIndex;
var sprites = ColBitMaps[colIndex].SpriteCount;
if ((first_index == 0 //if we have symbol in top row
|| ScreenInstances.EntropySrc.Next(2) == 1) //or we add more symbols randomly with 50% chance
&& (sprites <= (int)(SpriteRows * 0.5))) //main condition: column filled less or equal than 50% with non empty symbols
{
ColBitMaps[colIndex].AddTopSprite(); //move all column down by 1 row, insert symbol to top row
}
else
{
ColBitMaps[colIndex].InPlaceMutation(); //change symbols in-place
}
}
private void OnMouseEvent(object sender, MouseEventArgs e)
{
if (!System.Diagnostics.Debugger.IsAttached)
{
if (!MouseXY.IsEmpty)
{
if (MouseXY != new Point(e.X, e.Y))
{
ScreenInstances.TerminateSS();
}
if (e.Clicks > 0)
{
ScreenInstances.TerminateSS();
}
}
MouseXY = new Point(e.X, e.Y);
}
}
private void ScreenSaverForm_KeyDown(object sender, KeyEventArgs e)
{
if (!System.Diagnostics.Debugger.IsAttached)
{
ScreenInstances.TerminateSS();
}
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.SSPictureBox = new System.Windows.Forms.PictureBox();
((System.ComponentModel.ISupportInitialize)(this.SSPictureBox)).BeginInit();
this.SuspendLayout();
//
// SSPictureBox
//
this.SSPictureBox.Dock = System.Windows.Forms.DockStyle.Fill;
this.SSPictureBox.Location = new System.Drawing.Point(0, 0);
this.SSPictureBox.Name = "SSPictureBox";
this.SSPictureBox.Size = new System.Drawing.Size(1024, 768);
this.SSPictureBox.TabIndex = 0;
this.SSPictureBox.TabStop = false;
this.SSPictureBox.WaitOnLoad = true;
this.SSPictureBox.MouseDown += new System.Windows.Forms.MouseEventHandler(this.OnMouseEvent);
this.SSPictureBox.MouseMove += new System.Windows.Forms.MouseEventHandler(this.OnMouseEvent);
//
// ScreenSaverForm
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.BackColor = System.Drawing.Color.Black;
this.ClientSize = new System.Drawing.Size(1024, 768);
this.Controls.Add(this.SSPictureBox);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
this.Name = "ScreenSaverForm";
this.Text = "ScreenSaver";
this.Load += new System.EventHandler(this.ScreenSaverForm_Load);
this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.ScreenSaverForm_KeyDown);
((System.ComponentModel.ISupportInitialize)(this.SSPictureBox)).EndInit();
this.ResumeLayout(false);
}
#endregion
}
}