-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWindow.cs
More file actions
225 lines (190 loc) · 6.5 KB
/
Window.cs
File metadata and controls
225 lines (190 loc) · 6.5 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
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
using BlackBox.Machine;
using FontStashSharp;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
namespace BlackBox;
//todo: IMPORTANT: create api for bitmap and file subwindows
//also make file subwindow actually useful!
/// <summary>
/// MonoGame window with terminal rendering (hostspace)
/// </summary>
public class Window : Game
{
private const int TERMINAL_WIDTH = 80;
private const int TERMINAL_HEIGHT = 25;
private const string TITLE = "Black Box";
private const string FONT_PATH = "./JetBrainsMono-Regular.ttf";
private const int FONT_SIZE = 24;
private const float CURSOR_BLINK_RATE = 0.5f;
public static int Gap = 6;
private readonly GraphicsDeviceManager graphics;
private SpriteBatch? spriteBatch;
private Texture2D? pixelTexture;
private DynamicSpriteFont? font;
private FontSystem? fontSystem;
public static Terminal Terminal = new Terminal(TERMINAL_WIDTH, TERMINAL_HEIGHT);
private int charWidth = 8;
private int charHeight = FONT_SIZE;
private int windowWidth;
private int windowHeight;
private bool showCursor = true;
private float cursorBlinkTime;
public static Rectangle MainPanelRectangle;
public static Rectangle BitmapPanelRectangle;
public static Rectangle FilePanelRectangle;
public static Rectangle BackgroundRectangle;
public static RenderTarget2D MainPanel;
public static RenderTarget2D BitmapPanel;
public static RenderTarget2D FilePanel;
public static RenderTarget2D Background;
public Window()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
IsMouseVisible = true;
IsFixedTimeStep = true;
TargetElapsedTime = TimeSpan.FromSeconds(1.0 / 60.0);
Window.Title = TITLE;
}
protected override void Initialize()
{
Terminal.InitializeInput(Window);
base.Initialize();
}
protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
// Load font using FontStashSharp
if (File.Exists(FONT_PATH))
{
fontSystem = new FontSystem();
fontSystem.AddFont(File.ReadAllBytes(FONT_PATH));
font = fontSystem.GetFont(charHeight);
}
else
{
Console.WriteLine("Could not find font file: " + FONT_PATH);
// Fallback: we'd need a default font - for now just exit
Exit();
return;
}
// Measure character dimensions
var testSize = font.MeasureString("M");
charWidth = (int)testSize.X;
// Calculate window size
windowWidth = TERMINAL_WIDTH * charWidth;
windowHeight = (TERMINAL_HEIGHT + 1) * charHeight;
graphics.PreferredBackBufferWidth = windowWidth + Gap + 300;
graphics.PreferredBackBufferHeight = windowHeight;
graphics.ApplyChanges();
MainPanel = new RenderTarget2D(GraphicsDevice, windowWidth, windowHeight);
BitmapPanel = new RenderTarget2D(GraphicsDevice, 256, 256);
FilePanel = new RenderTarget2D(GraphicsDevice, 256, 256);
Background = new RenderTarget2D(GraphicsDevice, graphics.PreferredBackBufferWidth, graphics.PreferredBackBufferHeight);
// Create 1x1 white pixel for rectangle drawing
pixelTexture = new Texture2D(GraphicsDevice, 1, 1);
pixelTexture.SetData(new[] { Color.White });
}
protected override void Update(GameTime gameTime)
{
Terminal.UpdateInput(gameTime);
ProcessScrolling();
Host.Loop();
base.Update(gameTime);
}
private void ProcessScrolling()
{
if (Terminal.IsKeyPressed(Keys.PageUp))
Terminal.PageUp();
else if (Terminal.IsKeyPressed(Keys.PageDown))
Terminal.PageDown();
if (Terminal.IsKeyDown(Keys.LeftControl) || Terminal.IsKeyDown(Keys.RightControl))
{
if (Terminal.IsKeyPressed(Keys.Up))
Terminal.PageUp();
else if (Terminal.IsKeyPressed(Keys.Down))
Terminal.PageDown();
}
}
protected override void Draw(GameTime gameTime)
{
if (spriteBatch == null || pixelTexture == null || font == null)
return;
Recalculate(Window.ClientBounds.Width, Window.ClientBounds.Height);
// --- Background ---
GraphicsDevice.SetRenderTarget(Background);
GraphicsDevice.Clear(Color.White);
// --- Main Panel (terminal) ---
GraphicsDevice.SetRenderTarget(MainPanel);
GraphicsDevice.Clear(Color.Black);
spriteBatch.Begin(samplerState: SamplerState.PointClamp);
for (int y = 0; y < Terminal.Height; y++)
{
for (int x = 0; x < Terminal.Width; x++)
{
var bgColor = Terminal.GetBackgroundColor(x, y);
var fgColor = Terminal.GetForegroundColor(x, y);
var ch = Terminal.GetChar(x, y);
int posX = x * charWidth;
int posY = y * charHeight;
spriteBatch.Draw(pixelTexture, new Rectangle(posX, posY, charWidth, charHeight), new Color(bgColor.r, bgColor.g, bgColor.b));
if (ch != ' ')
spriteBatch.DrawString(font, ch.ToString(), new Vector2(posX, posY), new Color(fgColor.r, fgColor.g, fgColor.b));
}
}
cursorBlinkTime += (float)gameTime.ElapsedGameTime.TotalSeconds;
if (cursorBlinkTime >= CURSOR_BLINK_RATE)
{
showCursor = !showCursor;
cursorBlinkTime = 0;
}
if (showCursor)
{
int cursorScreenY = Terminal.CursorY - Terminal.ViewportOffset;
if (cursorScreenY >= 0 && cursorScreenY < Terminal.Height)
{
spriteBatch.Draw(pixelTexture,
new Rectangle(Terminal.CursorX * charWidth, (cursorScreenY + 1) * charHeight - 5, charWidth, 2),
Color.White);
}
}
spriteBatch.End();
// --- Bitmap Panel ---
GraphicsDevice.SetRenderTarget(BitmapPanel);
GraphicsDevice.Clear(Color.Gray);
// --- File Panel ---
GraphicsDevice.SetRenderTarget(FilePanel);
GraphicsDevice.Clear(Color.Gray);
// --- Composite to screen ---
GraphicsDevice.SetRenderTarget(null);
spriteBatch.Begin(samplerState: SamplerState.AnisotropicClamp);
spriteBatch.Draw(Background, BackgroundRectangle, Color.White);
spriteBatch.Draw(MainPanel, MainPanelRectangle, Color.White);
spriteBatch.Draw(BitmapPanel, BitmapPanelRectangle, Color.White);
spriteBatch.Draw(FilePanel, FilePanelRectangle, Color.White);
spriteBatch.End();
base.Draw(gameTime);
}
public static void Recalculate(int screenW, int screenH)
{
int sideW = (int)(screenW * 0.3f);
int miniSize = Math.Min(sideW, (screenH - Gap) / 2);
sideW = Math.Min(sideW, miniSize);
int mainW = screenW - sideW - Gap;
BackgroundRectangle = new Rectangle(0, 0, screenW, screenH);
MainPanelRectangle = new Rectangle(0, 0, mainW, screenH);
BitmapPanelRectangle = new Rectangle(mainW + Gap, 0, sideW, miniSize);
FilePanelRectangle = new Rectangle(mainW + Gap, screenH - miniSize, sideW, miniSize);
}
protected override void UnloadContent()
{
pixelTexture?.Dispose();
base.UnloadContent();
}
public static void Main()
{
Window game = new();
game.Run();
}
}