File size: 12,811 Bytes
7ffaa9e 7b453e7 7e312e3 7ffaa9e 7b453e7 7e312e3 7b453e7 7e312e3 7b453e7 0776d03 7b453e7 0776d03 7b453e7 0776d03 7b453e7 0776d03 7b453e7 0776d03 7b453e7 0776d03 7b453e7 7e312e3 7b453e7 7e312e3 7b453e7 7e312e3 7b453e7 7e312e3 7b453e7 7e312e3 7b453e7 7e312e3 7ffaa9e 7b453e7 7e312e3 7b453e7 7ffaa9e 7b453e7 7ffaa9e 7b453e7 7ffaa9e 0776d03 7ffaa9e 0776d03 7b453e7 0776d03 7b453e7 7ffaa9e 7b453e7 7ffaa9e 7b453e7 |
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 359 360 361 362 363 364 365 |
import { getConfig } from './config.js';
import { Time } from './time.js';
import { state } from './state.js';
import { drawPanel } from './panel.js';
import { GUI, TextArea, ScrollBar } from './gui.js';
import { LayerType } from './layer.js'; // Add this import
import { EventTypes } from './event.js'; // Add this import
export function createTerminal(canvas, ctx, inputHandler) {
const config = getConfig();
let targetHeight = 0;
let animationStartTime = 0;
let animating = false;
let animationStartHeight = 0;
// Create GUI components
const textArea = new TextArea({
font: config.terminal.font,
lineHeight: config.terminal.lineHeight
});
const scrollBar = new ScrollBar({
width: config.terminal.scrollBarWidth,
margin: config.terminal.scrollBarMargin,
minThumbHeight: config.terminal.scrollThumbMinHeight
});
function getMaxVisibleLines() {
// Account for input line height and bottom margin
const inputAreaHeight = config.terminal.lineHeight * 1.5; // Space for input line
const usableHeight = state.terminalHeight - inputAreaHeight - config.terminal.panelMarginY * 2;
return Math.floor(usableHeight / config.terminal.lineHeight);
}
function hasOverflow() {
const maxLines = getMaxVisibleLines();
const totalLines = state.commandHistory.length + 1; // +1 for input line
return totalLines > maxLines;
}
function scrollToBottom() {
const maxLines = getMaxVisibleLines();
const totalLines = state.commandHistory.length + 1;
state.maxScrollOffset = Math.max(0, totalLines - maxLines);
state.scrollOffset = 0; // Reset to bottom
}
function updateScrollOffset() {
const maxLines = getMaxVisibleLines();
const totalLines = state.commandHistory.length + 1; // +1 for input line
if (totalLines <= maxLines) {
// Reset scroll when no overflow
state.maxScrollOffset = 0;
state.scrollOffset = 0;
return;
}
state.maxScrollOffset = Math.max(0, totalLines - maxLines);
scrollToBottom();
}
function updateScroll(event) {
if (!state.isDraggingScrollbar) return;
const deltaY = event.clientY - state.lastMouseY;
state.lastMouseY = event.clientY;
state.scrollOffset = Math.max(0, Math.min(state.maxScrollOffset, state.scrollOffset - deltaY / 10));
event.preventDefault();
event.stopPropagation();
}
function isPointInTerminal(x, y) {
if (!state.terminalOpen) return false;
return y <= state.terminalHeight;
}
function startScroll(event) {
if (!state.terminalOpen) return;
const rect = canvas.getBoundingClientRect();
const x = event.clientX - rect.left;
const y = event.clientY - rect.top;
if (!isPointInTerminal(x, y)) return;
const scrollBarX = canvas.width - config.terminal.scrollBarWidth - config.terminal.scrollBarMargin * 2;
if (x >= scrollBarX && x <= scrollBarX + config.terminal.scrollBarWidth) {
state.isDraggingScrollbar = true;
state.lastMouseY = event.clientY;
event.preventDefault();
event.stopPropagation();
}
}
function updateAnimation() {
if (!animating) return;
const elapsed = (performance.now() - animationStartTime) / 1000;
const progress = Math.min(elapsed / config.terminal.animationSpeed, 1);
// Smooth easing
const eased = progress < 0.5
? 2 * progress * progress
: -1 + (4 - 2 * progress) * progress;
state.terminalHeight = animationStartHeight + (targetHeight - animationStartHeight) * eased;
if (progress >= 1) {
animating = false;
state.terminalHeight = targetHeight;
}
}
function drawTerminal() {
updateAnimation();
ctx.save();
if (state.terminalOpen || animating) {
// Position panel with margin
const panelX = config.terminal.panelMargin;
const panelY = config.terminal.panelMargin;
const panelWidth = canvas.width - (config.terminal.panelMargin * 2);
const panelHeight = state.terminalHeight - (config.terminal.panelMargin * 2);
drawPanel(ctx, panelX, panelY, panelWidth, panelHeight, config.terminal.margin);
// Draw terminal content background with padding
const contentX = panelX + config.terminal.margin + config.terminal.panelPadding;
const contentY = panelY + config.terminal.margin + config.terminal.panelPadding;
const contentWidth = panelWidth - (config.terminal.margin * 2) - (config.terminal.panelPadding * 2);
const contentHeight = panelHeight - (config.terminal.margin * 2) - (config.terminal.panelPadding * 2);
ctx.globalAlpha = config.terminalOpacity;
ctx.fillStyle = 'rgba(0, 0, 0, 0.95)';
ctx.fillRect(contentX, contentY, contentWidth, contentHeight);
}
ctx.restore();
}
function drawTerminalText() {
if (!state.terminalOpen) return;
ctx.save();
// Update cursor blink
state.cursorTimer += Time.deltaTime;
if (state.cursorTimer >= config.terminal.cursorBlinkRate) {
state.cursorTimer = 0;
state.cursorVisible = !state.cursorVisible;
}
// Calculate available space considering panel padding
const contentStart = config.terminal.panelMargin + config.terminal.margin + config.terminal.panelPadding;
const inputAreaHeight = config.terminal.lineHeight * 1.5;
const textAreaHeight = state.terminalHeight - inputAreaHeight - (contentStart * 2);
// Update text area
textArea.setLines(state.commandHistory);
textArea.setVisibleLines(Math.floor(textAreaHeight / config.terminal.lineHeight));
textArea.scrollOffset = state.scrollOffset;
// Draw command history with proper padding
textArea.draw(ctx,
contentStart + 5, // Extra 5px indent for text
contentStart,
canvas.width - config.terminal.scrollBarWidth - contentStart * 2,
textAreaHeight
);
// Draw input panel top border and input field - always visible
const inputPanelY = state.terminalHeight - config.terminal.panelMargin -
config.terminal.margin - config.terminal.panelPadding -
config.terminal.lineHeight * 1.8;
ctx.fillStyle = 'rgba(255, 255, 255, 0.2)';
ctx.fillRect(
contentStart,
inputPanelY,
canvas.width - (contentStart * 2),
1
);
// Always draw input line, with adjusted Y position (moved down 5px)
const inputText = '> ' + state.inputBuffer;
const lastLineY = inputPanelY + config.terminal.lineHeight + 5; // Added 5px here
GUI.drawText(ctx, inputText, contentStart + 5, lastLineY, {
font: config.terminal.font
});
if (state.cursorVisible) {
const metrics = GUI.measureText(ctx, inputText, config.terminal.font);
GUI.drawTextCursor(ctx,
contentStart + 5 + metrics.width + 1, // Add 1px margin from text
lastLineY + 4, // Move cursor down 4px
{
height: config.terminal.lineHeight * 0.8
}
);
}
// Draw scrollbar with proper alignment
if (hasOverflow()) {
const scrollRatio = state.maxScrollOffset > 0
? 1 - (state.scrollOffset / state.maxScrollOffset)
: 1;
const viewportRatio = textArea.visibleLines / textArea.lines.length;
scrollBar.draw(ctx,
canvas.width - contentStart - config.terminal.scrollBarWidth,
contentStart,
textAreaHeight,
scrollRatio,
viewportRatio
);
}
ctx.restore();
}
function handleTextInput(char) {
if (state.terminalOpen && char !== '`') {
state.inputBuffer += char;
}
}
function handleToggleTerminal(event) {
if (event.shiftKey) {
toggleTerminal(true);
} else {
toggleTerminal();
}
}
function toggleTerminal(full = false) {
state.terminalOpen = !state.terminalOpen;
if (state.scene) {
const gameLayer = state.scene.getLayer(LayerType.GAME);
const debugLayer = state.scene.getLayer(LayerType.DEBUG);
const guiLayer = state.scene.getLayer(LayerType.GUI);
if (state.terminalOpen) {
// Disable other layers' input when terminal is open
gameLayer?.disableInput();
debugLayer?.disableInput();
guiLayer?.disableInput();
// Also disable camera controls
state.inputHandler.disableCameraControls();
inputHandler.addTextInputListener(handleTextInput);
updateScrollOffset();
} else {
// Re-enable other layers' input when terminal is closed
gameLayer?.enableInput();
debugLayer?.enableInput();
guiLayer?.enableInput();
// Re-enable camera controls
state.inputHandler.enableCameraControls();
inputHandler.removeTextInputListener(handleTextInput);
state.scrollOffset = 0;
state.maxScrollOffset = 0;
state.isDraggingScrollbar = false;
}
}
// Animation setup
animating = true;
animationStartTime = performance.now();
animationStartHeight = state.terminalHeight;
targetHeight = state.terminalOpen ? canvas.height * config.terminal.heightRatio : 0;
}
function executeCommand(command) {
const trimmedCommand = command.trim();
state.commandHistory.push('> ' + command);
state.inputBuffer = '';
switch (trimmedCommand) {
case 'help':
state.commandHistory.push('Available commands:');
state.commandHistory.push(' help - Shows this help message');
state.commandHistory.push(' about - About this smolworld');
state.commandHistory.push(' clear - Clears the terminal');
break;
case 'about':
state.commandHistory.push('Smolworld: A tiny world simulation.');
break;
case 'clear':
state.commandHistory = [];
break;
default:
state.commandHistory.push('Unknown command: ' + command);
}
updateScrollOffset();
}
function handleEnter() {
executeCommand(state.inputBuffer);
}
function handleBackspace() {
state.inputBuffer = state.inputBuffer.slice(0, -1);
}
// Replace setupTerminalControls with direct event listeners
inputHandler.listenForKey('`', (event) => {
handleToggleTerminal(event);
});
inputHandler.listenForKey('Enter', () => {
if (state.terminalOpen) {
handleEnter();
}
});
inputHandler.listenForKey('Backspace', () => {
if (state.terminalOpen) {
handleBackspace();
}
});
// Setup mouse handlers through event system
inputHandler.on(EventTypes.MOUSE_DOWN, startScroll);
inputHandler.on(EventTypes.MOUSE_MOVE, updateScroll);
inputHandler.on(EventTypes.MOUSE_UP, (event) => {
if (state.isDraggingScrollbar) {
event.preventDefault();
event.stopPropagation();
}
state.isDraggingScrollbar = false;
});
inputHandler.on(EventTypes.MOUSE_WHEEL, (event) => {
const rect = canvas.getBoundingClientRect();
const x = event.clientX - rect.left;
const y = event.clientY - rect.top;
if (!state.terminalOpen || !isPointInTerminal(x, y) || !hasOverflow()) return;
event.preventDefault();
event.stopPropagation();
const scrollAmount = event.deltaY > 0 ? -1 : 1;
state.scrollOffset = Math.max(0, Math.min(state.maxScrollOffset, state.scrollOffset + scrollAmount));
});
return {
draw: drawTerminal,
drawText: drawTerminalText,
// Export methods needed by input handler
handleToggleTerminal,
handleEnter,
handleBackspace,
startScroll,
updateScroll,
isPointInTerminal,
hasOverflow
};
}
|