-
Notifications
You must be signed in to change notification settings - Fork 967
Expand file tree
/
Copy pathMainRadialMenu.ts
More file actions
260 lines (226 loc) · 7.2 KB
/
MainRadialMenu.ts
File metadata and controls
260 lines (226 loc) · 7.2 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
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
import { LitElement } from "lit";
import { customElement } from "lit/decorators.js";
import { assetUrl } from "../../../core/AssetUrls";
import { EventBus } from "../../../core/EventBus";
import { PlayerActions } from "../../../core/game/Game";
import { TileRef } from "../../../core/game/GameMap";
import { GameView, PlayerView } from "../../../core/game/GameView";
import {
CloseViewEvent,
ContextMenuEvent,
MouseUpEvent,
TouchEvent,
} from "../../InputHandler";
import { SendQuickChatEvent } from "../../Transport";
import { TransformHandler } from "../TransformHandler";
import { UIState } from "../UIState";
import { BuildMenu } from "./BuildMenu";
import { ChatIntegration } from "./ChatIntegration";
import { EmojiTable } from "./EmojiTable";
import { Layer } from "./Layer";
import { PlayerActionHandler } from "./PlayerActionHandler";
import { PlayerPanel } from "./PlayerPanel";
import { RadialMenu, RadialMenuConfig } from "./RadialMenu";
import {
centerButtonElement,
COLORS,
MenuElementParams,
rootMenuElement,
} from "./RadialMenuElements";
import { TargetSelectionMode } from "./TargetSelectionMode";
const donateTroopIcon = assetUrl("images/DonateTroopIconWhite.svg");
const swordIcon = assetUrl("images/SwordIconWhite.svg");
@customElement("main-radial-menu")
export class MainRadialMenu extends LitElement implements Layer {
private radialMenu: RadialMenu;
private playerActionHandler: PlayerActionHandler;
private chatIntegration: ChatIntegration;
private clickedTile: TileRef | null = null;
getTickIntervalMs() {
return 500;
}
constructor(
private eventBus: EventBus,
private game: GameView,
private transformHandler: TransformHandler,
private emojiTable: EmojiTable,
private buildMenu: BuildMenu,
private uiState: UIState,
private playerPanel: PlayerPanel,
) {
super();
const menuConfig: RadialMenuConfig = {
centerButtonIcon: swordIcon,
tooltipStyle: `
.radial-tooltip .cost {
margin-top: 4px;
color: ${COLORS.tooltip.cost};
}
.radial-tooltip .count {
color: ${COLORS.tooltip.count};
}
`,
};
this.radialMenu = new RadialMenu(
this.eventBus,
rootMenuElement,
centerButtonElement,
menuConfig,
);
this.playerActionHandler = new PlayerActionHandler(
this.eventBus,
this.uiState,
);
this.chatIntegration = new ChatIntegration(this.game, this.eventBus);
}
init() {
this.radialMenu.init();
// Handle left-click and touch: if target-selection mode is active, resolve
// the clicked tile as the chat target instead of performing a normal action.
const handleSelectClick = (x: number, y: number) => {
const mode = TargetSelectionMode.getInstance();
if (!mode.active) return false;
const worldCoords = this.transformHandler.screenToWorldCoordinates(x, y);
if (!this.game.isValidCoord(worldCoords.x, worldCoords.y)) return true;
const tile = this.game.ref(worldCoords.x, worldCoords.y);
const owner = this.game.owner(tile);
if (owner.isPlayer()) {
this.eventBus.emit(
new SendQuickChatEvent(
mode.pendingRecipient!,
mode.pendingKey!,
(owner as PlayerView).id(),
),
);
mode.exit();
}
// If tile has no owner, stay in mode and wait for another click.
return true;
};
this.eventBus.on(MouseUpEvent, (event) => {
if (handleSelectClick(event.x, event.y)) return;
});
this.eventBus.on(TouchEvent, (event) => {
if (handleSelectClick(event.x, event.y)) return;
});
// Escape cancels target-selection mode.
this.eventBus.on(CloseViewEvent, () => {
TargetSelectionMode.getInstance().exit();
});
this.eventBus.on(ContextMenuEvent, (event) => {
// While in target-selection mode:
// - left-click (isRightClick=false) → attempt to resolve the target
// - right-click (isRightClick=true) → cancel the mode
if (TargetSelectionMode.getInstance().active) {
if (event.isRightClick) {
TargetSelectionMode.getInstance().exit();
} else {
handleSelectClick(event.x, event.y);
}
return;
}
const worldCoords = this.transformHandler.screenToWorldCoordinates(
event.x,
event.y,
);
if (!this.game.isValidCoord(worldCoords.x, worldCoords.y)) {
return;
}
if (this.game.myPlayer() === null) {
return;
}
this.clickedTile = this.game.ref(worldCoords.x, worldCoords.y);
this.game
.myPlayer()!
.actions(this.clickedTile)
.then((actions) => {
this.updatePlayerActions(
this.game.myPlayer()!,
actions,
this.clickedTile!,
event.x,
event.y,
);
});
});
}
private async updatePlayerActions(
myPlayer: PlayerView,
actions: PlayerActions,
tile: TileRef,
screenX: number | null = null,
screenY: number | null = null,
) {
this.buildMenu.playerBuildables = actions.buildableUnits;
const tileOwner = this.game.owner(tile);
const recipient = tileOwner.isPlayer() ? (tileOwner as PlayerView) : null;
if (myPlayer && recipient) {
this.chatIntegration.setupChatModal(myPlayer, recipient);
}
const params: MenuElementParams = {
myPlayer,
selected: recipient,
tile,
playerActions: actions,
game: this.game,
buildMenu: this.buildMenu,
emojiTable: this.emojiTable,
playerActionHandler: this.playerActionHandler,
playerPanel: this.playerPanel,
chatIntegration: this.chatIntegration,
uiState: this.uiState,
closeMenu: () => this.closeMenu(),
eventBus: this.eventBus,
};
const isFriendlyTarget =
recipient !== null &&
recipient.isFriendly(myPlayer) &&
!recipient.isDisconnected();
this.radialMenu.setCenterButtonAppearance(
isFriendlyTarget ? donateTroopIcon : swordIcon,
isFriendlyTarget ? "#22d3ee" : "#0f2744",
isFriendlyTarget
? this.radialMenu.getDefaultCenterIconSize() * 0.75
: this.radialMenu.getDefaultCenterIconSize(),
);
this.radialMenu.setParams(params);
if (screenX !== null && screenY !== null) {
this.radialMenu.showRadialMenu(screenX, screenY);
} else {
this.radialMenu.refresh();
}
}
async tick() {
if (!this.radialMenu.isMenuVisible() || this.clickedTile === null) return;
this.game
.myPlayer()!
.actions(this.clickedTile)
.then((actions) => {
this.updatePlayerActions(
this.game.myPlayer()!,
actions,
this.clickedTile!,
);
});
}
renderLayer(context: CanvasRenderingContext2D) {
this.radialMenu.renderLayer(context);
}
shouldTransform(): boolean {
return this.radialMenu.shouldTransform();
}
closeMenu() {
if (this.radialMenu.isMenuVisible()) {
this.radialMenu.hideRadialMenu();
}
if (this.buildMenu.isVisible) {
this.buildMenu.hideMenu();
}
if (this.emojiTable.isVisible) {
this.emojiTable.hideTable();
}
if (this.playerPanel.isVisible) {
this.playerPanel.hide();
}
}
}