hadean-old/src/qt/GameView.ts

251 lines
5.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-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-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-23 00:12:02 -04:00
this.title.setText(this.game.name);
2021-07-26 23:15:05 -04:00
this.layout.addWidget(this.title, 0, 0);
this.layout.addWidget(this.timeLabel, 0, 1);
this.layout.addWidget(this.left, 1, 0);
this.layout.addWidget(this.right, 1, 1);
this.layout.setRowStretch(0, 0);
this.layout.setRowStretch(1, 1);
this.layout.setColumnStretch(0, 3);
this.layout.setColumnStretch(1, 2);
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-27 17:48:50 -04:00
class GridItem extends QWidget {
rootLayout: QGridLayout;
get layout(): QGridLayout {
return this.rootLayout;
}
constructor() {
super();
this.rootLayout = new QGridLayout()
this.setLayout(this.rootLayout);
this.setInlineStyle(`
width: \'100%\';
background: coral;
margin: 0px;
padding: 0px;
`);
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);
const activityLabel = new QLabel();
activityLabel.setText(right);
activityLabel.setAlignment(AlignmentFlag.AlignRight | AlignmentFlag.AlignTop);
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;
`)
this.centralWidget = new QWidget();
this.centralWidget.setInlineStyle(`
background: rgba(0, 0, 0, 0);
`)
// this.setVerticalScrollBarPolicy(ScrollBarPolicy.ScrollBarAlwaysOn);
this.setWidgetResizable(true);
this.setWidget(this.centralWidget);
this.vLayout = new QBoxLayout(Direction.TopToBottom);
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-26 14:23:41 -04:00
}