-
Notifications
You must be signed in to change notification settings - Fork 12
/
UIMenuListItem.ts
156 lines (131 loc) · 4.16 KB
/
UIMenuListItem.ts
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
import { Menu, Sprite, Text } from '../../';
import { Alignment, Font } from '../../../enums';
import { Color, LiteEvent, Point, Size, String } from '../../../utils';
import { ListItem } from '../modules/';
import { UIMenuItem } from './';
export class UIMenuListItem extends UIMenuItem {
public readonly listChanged = new LiteEvent();
public readonly listSelected = new LiteEvent();
protected supportsRightBadge = false;
protected supportsRightLabel = false;
private _itemText: Text;
private _leftArrow: Sprite;
private _rightArrow: Sprite;
private _index = 0;
private _arrowOnlyOnSelected = false;
private _items: ListItem[] = [];
private _textWidth = 0;
constructor(
text: string,
items: ListItem[],
startIndex = 0,
description?: string,
arrowOnlyOnSelected = true,
) {
super(text, description);
this._leftArrow = new Sprite('commonmenu', 'arrowleft', new Point(), new Size(30, 30));
this._rightArrow = new Sprite('commonmenu', 'arrowright', new Point(), new Size(30, 30));
this._itemText = new Text(
'',
new Point(),
0.35,
Color.white,
Font.ChaletLondon,
Alignment.Right,
);
this.ArrowOnlyOnSelected = arrowOnlyOnSelected;
this.Items = items;
this.Index = startIndex;
}
public get Items(): ListItem[] {
return this._items;
}
public set Items(value: ListItem[]) {
if (!value) {
throw new Error("Items can't be null");
}
this._items = value;
}
public get SelectedItem(): ListItem {
return this.Items[this.Index];
}
public set SelectedItem(value: ListItem) {
const index = this.Items.findIndex(i => i.id === value.id);
if (index >= 0) {
this.Index = index;
}
}
public get SelectedValue(): unknown {
const item = this.SelectedItem;
return item ? item.value : null;
}
public get Index(): number {
return this._index % this.Items.length;
}
public set Index(value: number) {
if (!this._items.length) {
return;
}
value = value < 0 ? this._items.length - 1 : value > this._items.length - 1 ? 0 : value;
this._index = value;
this._textWidth = 0;
}
public get ArrowOnlyOnSelected(): boolean {
return this._arrowOnlyOnSelected;
}
public set ArrowOnlyOnSelected(value: boolean) {
this._arrowOnlyOnSelected = value;
}
public get IsMouseInBoundsOfLeftArrow(): boolean {
return this.parent
? this.parent.isMouseInBounds(this._leftArrow.pos, this._leftArrow.size)
: false;
}
public get IsMouseInBoundsOfRightArrow(): boolean {
return this.parent
? this.parent.isMouseInBounds(this._rightArrow.pos, this._rightArrow.size)
: false;
}
public setVerticalPosition(y: number): void {
const yOffset = y + this.offset.Y + 147;
this._leftArrow.pos.Y = yOffset;
this._rightArrow.pos.Y = yOffset;
this._itemText.pos.Y = yOffset;
super.setVerticalPosition(y);
}
public draw(): void {
super.draw();
if (this._textWidth === undefined) {
const caption = this._getSelectedItemCaption();
this._itemText.caption = caption;
this._textWidth = String.measureString(
caption,
this._itemText.font,
this._itemText.scale,
Menu.screenWidth,
);
}
this._rightArrow.pos.X = this.offset.X + (this.parent ? this.parent.WidthOffset : 0) + 400;
this._itemText.pos.X = this._rightArrow.pos.X + 5;
this._itemText.color = this.enabled
? this.selected
? this.HighlightedForeColor
: this.ForeColor
: new Color(255, 163, 159, 148);
if (this._arrowOnlyOnSelected && !this.selected) {
this._itemText.pos.X += this._rightArrow.size.width / 2;
} else {
this._leftArrow.color = this._itemText.color;
this._rightArrow.color = this._itemText.color;
this._leftArrow.pos.X =
this._itemText.pos.X - this._textWidth - this._leftArrow.size.width + 5;
this._leftArrow.draw(Menu.screenResolution);
this._rightArrow.draw(Menu.screenResolution);
}
this._itemText.draw(undefined, Menu.screenResolution);
}
private _getSelectedItemCaption(): string {
const item = this.SelectedItem;
return item ? item.name : '';
}
}