hadean-old/src/qt/GameView.ts

345 lines
8.5 KiB
TypeScript
Raw Normal View History

2021-07-23 00:12:02 -04:00
import { Game } from '@game';
2021-07-27 17:48:50 -04:00
import { ItemState } from '@items';
2021-07-25 13:57:47 -04:00
import {
QLabel,
QTabWidget,
QWidget,
2021-07-26 14:23:41 -04:00
QIcon,
QGridLayout,
FocusPolicy,
2021-07-26 23:15:05 -04:00
AlignmentFlag,
2021-07-27 17:48:50 -04:00
QBoxLayout,
Direction,
QScrollArea,
2021-07-28 23:08:45 -04:00
WidgetEventTypes,
QPaintEvent,
QPainter,
QFrame,
QPushButton
2021-07-25 13:57:47 -04:00
} from '@nodegui/nodegui';
2021-07-27 17:48:50 -04:00
import network from '../multiplayer/mDNS.js';
import { Player } from '../multiplayer/Player.js';
2021-07-26 14:23:41 -04:00
import { Pawn } from '../Pawn.js';
2021-07-23 00:12:02 -04:00
import { View } from './View.js';
export class GameView extends View {
game: Game;
title: QLabel;
2021-07-26 23:15:05 -04:00
timeLabel: QLabel;
2021-07-23 00:12:02 -04:00
left: QWidget;
2021-07-25 13:57:47 -04:00
right: QTabWidget;
2021-07-28 23:08:45 -04:00
timeControl: TimeControl;
2021-07-23 00:12:02 -04:00
addComponents(): void {
this.addLayout();
this.title = new QLabel();
this.left = new QWidget();
2021-07-25 13:57:47 -04:00
this.right = new QTabWidget();
2021-07-26 23:15:05 -04:00
this.timeLabel = new TimeWidget();
2021-07-28 23:08:45 -04:00
this.timeControl = new TimeControl();
2021-07-23 00:12:02 -04:00
this.title.setText(this.game.name);
2021-07-28 23:08:45 -04:00
this.layout.addWidget(this.title, 0, 0, 1, 5);
this.layout.addWidget(this.timeControl, 0, 5, 1, 5)
this.layout.addWidget(this.timeLabel, 0, 10, 1, 5);
this.layout.addWidget(this.left, 1, 0, 1, 9);
this.layout.addWidget(this.right, 1, 9, 1, 6);
2021-07-26 23:15:05 -04:00
this.layout.setRowStretch(0, 0);
this.layout.setRowStretch(1, 1);
2021-07-28 23:08:45 -04:00
for(let i = 0; i < 15; i ++) {
this.layout.setColumnStretch(i, 1);
}
2021-07-25 13:57:47 -04:00
2021-07-26 14:23:41 -04:00
this.right.addTab(new PawnPageWidget(), new QIcon(), 'Pawns');
2021-07-27 17:48:50 -04:00
this.right.addTab(new InventoryPageWidget(), new QIcon(), 'Inventory');
this.right.addTab(new MultiplayerPageWidget(), new QIcon(), 'Multiplayer');
2021-07-23 00:12:02 -04:00
}
constructor(game: Game) {
super();
this.game = game;
}
2021-07-25 13:57:47 -04:00
}
2021-07-28 23:08:45 -04:00
class GridItem extends QFrame {
2021-07-27 17:48:50 -04:00
rootLayout: QGridLayout;
get layout(): QGridLayout {
return this.rootLayout;
}
constructor() {
super();
this.rootLayout = new QGridLayout()
this.setLayout(this.rootLayout);
this.setInlineStyle(`
2021-07-28 23:08:45 -04:00
width: '100%';
2021-07-27 17:48:50 -04:00
margin: 0px;
padding: 0px;
`);
2021-07-28 23:08:45 -04:00
this.rootLayout.setContentsMargins(0, 0, 0, 0);
this.rootLayout.setSpacing(0);
this.rootLayout.setVerticalSpacing(0);
this.rootLayout.setHorizontalSpacing(0);
// this.rootLayout.
2021-07-27 17:48:50 -04:00
this.setFocusPolicy(FocusPolicy.ClickFocus);
}
}
function addSplitText(layout: QGridLayout, left: string, right: string, row: number) {
layout.setSpacing(0);
const nameLabel = new QLabel();
nameLabel.setText(left);
nameLabel.setAlignment(AlignmentFlag.AlignLeft | AlignmentFlag.AlignTop);
2021-07-28 23:08:45 -04:00
// nameLabel.setInlineStyle('padding: 0px; margin: 0px;');
2021-07-27 17:48:50 -04:00
const activityLabel = new QLabel();
activityLabel.setText(right);
activityLabel.setAlignment(AlignmentFlag.AlignRight | AlignmentFlag.AlignTop);
2021-07-28 23:08:45 -04:00
// activityLabel.setInlineStyle('padding: 0px; margin: 0px;');
2021-07-27 17:48:50 -04:00
layout.addWidget(nameLabel, row, 0, 1, 1);
layout.addWidget(activityLabel, row, 1, 1, 1);
layout.setRowStretch(row, 1);
// in theory this is redundant, calling this
// function on the same layout multiple times...
layout.setColumnStretch(0, 1);
layout.setColumnStretch(1, 1);
}
class PawnWidget extends GridItem {
2021-07-26 14:23:41 -04:00
constructor(pawn: Pawn) {
super();
2021-07-27 17:48:50 -04:00
addSplitText(
this.layout,
pawn.name.first + ' ' + pawn.name.last,
pawn.status,
0
);
2021-07-26 14:23:41 -04:00
}
}
2021-07-27 17:48:50 -04:00
class ItemWidget extends GridItem {
constructor(itemState: ItemState<unknown>) {
super();
addSplitText(
this.layout,
itemState.name,
'' + (itemState.qty),
0
);
}
}
abstract class ScrollPanel extends QScrollArea {
centralWidget: QWidget;
vLayout: QBoxLayout;
2021-07-28 01:42:32 -04:00
widgets: QWidget[] = [];
2021-07-26 14:23:41 -04:00
constructor() {
super();
2021-07-27 17:48:50 -04:00
this.setInlineStyle(`
background: rgba(0, 0, 0, 0);
border: none;
`)
2021-07-28 23:08:45 -04:00
this.centralWidget = new QFrame();
2021-07-27 17:48:50 -04:00
this.centralWidget.setInlineStyle(`
background: rgba(0, 0, 0, 0);
2021-07-28 23:08:45 -04:00
`);
2021-07-27 17:48:50 -04:00
// this.setVerticalScrollBarPolicy(ScrollBarPolicy.ScrollBarAlwaysOn);
this.setWidgetResizable(true);
this.setWidget(this.centralWidget);
this.vLayout = new QBoxLayout(Direction.TopToBottom);
2021-07-28 23:08:45 -04:00
const a = 12;
this.vLayout.setContentsMargins(a, a, a, a);
this.vLayout.setSpacing(0);
2021-07-27 17:48:50 -04:00
this.centralWidget.setLayout(this.vLayout);
this.fill();
this.vLayout.addStretch(1);
}
2021-07-26 14:23:41 -04:00
2021-07-27 17:48:50 -04:00
refill() {
2021-07-28 01:42:32 -04:00
for(const component of this.widgets) {
// component.hide();
component.close();
// component.nodeParent = null;
// this.vLayout.removeWidget(component);
2021-07-27 17:48:50 -04:00
}
2021-07-28 01:42:32 -04:00
this.widgets = [];
2021-07-27 17:48:50 -04:00
this.fill();
}
addWidget(widget: QWidget) {
2021-07-28 01:42:32 -04:00
this.widgets.push(widget);
this.vLayout.insertWidget(0, widget);
2021-07-27 17:48:50 -04:00
}
abstract fill(): void;
}
class PawnPageWidget extends ScrollPanel {
fill() {
2021-07-26 14:23:41 -04:00
for(const pawn of Game.current.pawns) {
2021-07-27 17:48:50 -04:00
this.addWidget(new PawnWidget(pawn));
2021-07-26 14:23:41 -04:00
}
}
2021-07-26 23:15:05 -04:00
}
2021-07-27 17:48:50 -04:00
// class PawnPageWidget extends QListWidget {
// constructor() {
// super();
// for (const pawn of Game.current.pawns) {
// const pawnWidget = new PawnWidget(pawn);
// const item = new QListWidgetItem();
// this.addItem(item);
// this.setItemWidget(item, pawnWidget);
// }
// }
// }
// class PawnPageWidget extends QWidget {
// constructor() {
// super();
// this.setLayout(new QBoxLayout(Direction.TopToBottom));
// // this.setLayout(new FlexLayout());
// for(const pawn of Game.current.pawns) {
// this.layout.addWidget(new PawnWidget(pawn));
// }
// }
// }
2021-07-26 23:15:05 -04:00
class TimeWidget extends QLabel {
constructor() {
super();
this.setAlignment(AlignmentFlag.AlignRight | AlignmentFlag.AlignVCenter);
setInterval(() => {
this.setText(Game.current.clock.toString());
}, 100)
}
}
2021-07-27 17:48:50 -04:00
class InventoryPageWidget extends ScrollPanel {
fill() {
for(const itemState of Game.current.inv.items) {
this.addWidget(new ItemWidget(itemState))
}
}
}
class MultiplayerPlayerWidget extends GridItem {
constructor(player: Player) {
super();
addSplitText(
this.layout,
player.name,
player.host + ':' + player.port,
0
)
}
}
class MultiplayerPageWidget extends ScrollPanel {
constructor() {
super();
network.on('change', () => {
this.refill();
})
}
fill(): void {
for(const player of network.players) {
this.addWidget(new MultiplayerPlayerWidget(player))
}
}
2021-07-28 23:08:45 -04:00
}
class TimeControl extends GridItem {
pauseButton: QPushButton;
normalSpeedButton: QPushButton;
turboSpeedButton: QPushButton;
extremeSpeedButton: QPushButton;
NORMAL = 60;
TURBO = 180;
EXTREME = 360;
constructor() {
super();
this.pauseButton = new QPushButton();
this.pauseButton.setText('❚❚');
this.normalSpeedButton = new QPushButton();
this.normalSpeedButton.setText('');
this.turboSpeedButton = new QPushButton();
this.turboSpeedButton.setText(''.repeat(2));
this.extremeSpeedButton = new QPushButton();
this.extremeSpeedButton.setText(''.repeat(3));
this.layout.addWidget(this.pauseButton, 0, 0);
this.layout.addWidget(this.normalSpeedButton, 0, 1);
this.layout.addWidget(this.turboSpeedButton, 0, 2);
this.layout.addWidget(this.extremeSpeedButton, 0, 3);
this.updateButtons();
this.pauseButton.addEventListener('clicked', () => {
Game.current.clock.pause();
this.updateButtons();
});
this.normalSpeedButton.addEventListener('clicked', () => {
if(Game.current.clock.paused) Game.current.clock.resume();
Game.current.clock.targetTPS = this.NORMAL;
this.updateButtons();
});
this.turboSpeedButton.addEventListener('clicked', () => {
if(Game.current.clock.paused) Game.current.clock.resume();
Game.current.clock.targetTPS = this.TURBO;
this.updateButtons();
});
this.extremeSpeedButton.addEventListener('clicked', () => {
if(Game.current.clock.paused) Game.current.clock.resume();
Game.current.clock.targetTPS = this.EXTREME;
this.updateButtons();
});
}
updateButtons() {
const update = (a: boolean, b: boolean, c: boolean, d: boolean) => {
this.pauseButton.setEnabled(a);
this.normalSpeedButton.setEnabled(b);
this.turboSpeedButton.setEnabled(c);
this.extremeSpeedButton.setEnabled(d);
}
if(Game.current) {
if(Game.current.clock.paused) return update(false, true, true, true);
if(Game.current.clock.targetTPS === this.NORMAL) return update(true, false, true, true);
if(Game.current.clock.targetTPS === this.TURBO) return update(true, true, false, true);
if(Game.current.clock.targetTPS === this.EXTREME) return update(true, true, true, false);
} else {
update(false, false, false, false);
}
}
2021-07-26 14:23:41 -04:00
}