-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathWindow.java
More file actions
260 lines (221 loc) · 8.69 KB
/
Window.java
File metadata and controls
260 lines (221 loc) · 8.69 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
package com.simon816.chatui;
import com.google.common.base.MoreObjects;
import com.google.common.collect.Lists;
import com.simon816.chatui.lib.ChatUILib;
import com.simon816.chatui.lib.PlayerChatView;
import com.simon816.chatui.lib.PlayerContext;
import com.simon816.chatui.lib.TopWindow;
import com.simon816.chatui.tabs.Tab;
import com.simon816.chatui.tabs.TextBufferTab;
import com.simon816.chatui.ui.AnchorPaneUI;
import com.simon816.chatui.ui.LineFactory;
import com.simon816.chatui.ui.UIComponent;
import com.simon816.chatui.util.TextBuffer;
import com.simon816.chatui.util.TextUtils;
import org.spongepowered.api.text.LiteralText;
import org.spongepowered.api.text.Text;
import org.spongepowered.api.text.action.TextActions;
import org.spongepowered.api.text.format.TextColors;
import org.spongepowered.api.text.format.TextStyles;
import java.util.List;
public class Window implements TopWindow {
private final List<Tab> tabs = Lists.newArrayList();
private final AnchorPaneUI pane;
private int activeIndex = -1;
public Window() {
this.pane = new AnchorPaneUI();
this.pane.addWithConstraint(new TabBar(), AnchorPaneUI.ANCHOR_TOP);
this.pane.getChildren().add(new TabHolder());
this.pane.addWithConstraint(new StatusBar(), AnchorPaneUI.ANCHOR_BOTTOM);
}
public void addTab(Tab tab, boolean switchTab) {
int idx = this.tabs.indexOf(tab);
if (idx == -1) {
this.tabs.add(tab);
idx = this.tabs.size() - 1;
}
if (switchTab) {
setTab(idx);
}
}
public void removeTab(Tab tab) {
removeTab(this.tabs.indexOf(tab));
}
public void removeTab(int index) {
if (index < this.activeIndex) {
this.activeIndex--;
} else if (index == this.activeIndex) {
setTab(index - 1);
}
this.tabs.remove(index).onClose();
}
public void setTab(int tabIndex) {
if (tabIndex >= this.tabs.size()) {
throw new IllegalArgumentException("Tab index doesn't exist");
}
if (tabIndex == this.activeIndex) {
return;
}
if (this.activeIndex > -1) {
this.tabs.get(this.activeIndex).onBlur();
}
this.activeIndex = tabIndex;
this.tabs.get(this.activeIndex).onFocus();
}
public void setTab(Tab tab) {
setTab(this.tabs.indexOf(tab));
}
public Tab getActiveTab() {
return this.tabs.get(this.activeIndex);
}
public int getActiveIndex() {
return this.activeIndex;
}
@Override
public void onClose() {
for (Tab tab : this.tabs) {
tab.onClose();
}
this.tabs.clear();
this.activeIndex = -1;
}
@Override
public boolean onCommand(PlayerChatView view, String[] args) {
String cmd = args[0];
if (cmd.equals("settab")) {
this.setTab(Integer.parseInt(args[1]));
} else if (cmd.equals("closetab")) {
this.removeTab(Integer.parseInt(args[1]));
} else if (this.activeIndex != -1) {
return this.tabs.get(this.activeIndex).onCommand(view, args);
} else {
return false;
}
return true;
}
@Override
public void onTextInput(PlayerChatView view, Text input) {
if (this.activeIndex != -1) {
this.tabs.get(this.activeIndex).onTextInput(view, input);
}
}
@Override
public Text draw(PlayerContext ctx) {
return this.pane.draw(ctx);
}
List<Tab> getTabs() {
return this.tabs;
}
static final LiteralText newTab = Text.builder("[+]")
.color(TextColors.GREEN)
.onClick(ChatUILib.command("newtab"))
.onHover(TextActions.showText(Text.of("New Tab")))
.build();
private class TabBar implements UIComponent {
private int tabListLines = 1;
TabBar() {
}
@Override
public void draw(PlayerContext ctx, LineFactory lineFactory) {
int maxWidth = ctx.width;
Text.Builder builder = Text.builder();
int currentLineWidth = 0;
TextBuffer buffer = new TextBuffer(ctx);
buffer.append(TextUtils.charCache('╔'));
this.tabListLines = 1;
for (int i = 0; i < getTabs().size(); i++) {
Tab tab = getTabs().get(i);
buffer.append(createTabButton(i));
if (tab.hasCloseButton()) {
buffer.append(createCloseButton(i));
}
buffer.append(TextUtils.charCache('═'));
currentLineWidth = addTabElement(lineFactory, buffer, builder, currentLineWidth, maxWidth, ctx);
buffer.clear();
}
buffer.append(newTab);
currentLineWidth = addTabElement(lineFactory, buffer, builder, currentLineWidth, maxWidth, ctx);
builder.append(ctx.utils().repeatAndTerminate('═', this.tabListLines == 1 ? '╗' : '╣', maxWidth - currentLineWidth));
lineFactory.appendNewLine(builder.build(), ctx);
}
private int addTabElement(LineFactory lineFactory, TextBuffer buffer, Text.Builder builder, int currentLineWidth, int maxWidth,
PlayerContext ctx) {
if (currentLineWidth + buffer.getWidth() > maxWidth) {
// Overspilled - finish this line and move to another one
builder.append(ctx.utils().repeatAndTerminate('═', this.tabListLines == 1 ? '╗' : '╣', maxWidth - currentLineWidth));
lineFactory.appendNewLine(builder.build(), ctx);
char startChar = '╠';
builder.removeAll();
currentLineWidth = ctx.utils().getWidth(startChar, false);
builder.append(TextUtils.charCache(startChar));
this.tabListLines++;
}
currentLineWidth += buffer.getWidth();
builder.append(buffer.getContents());
return currentLineWidth;
}
private Text createTabButton(int tabIndex) {
Text.Builder button = getTabs().get(tabIndex).getTitle().toBuilder();
button.color(TextColors.GREEN);
if (tabIndex == getActiveIndex()) {
button.style(TextStyles.BOLD);
} else {
button.onClick(ChatUILib.command("settab " + tabIndex));
}
return button.build();
}
private Text createCloseButton(int tabIndex) {
return Text.builder("[x]").color(TextColors.RED)
.onClick(ChatUILib.command("closetab " + tabIndex))
.onHover(TextActions.showText(Text.of("Close")))
.build();
}
@Override
public int getPrefHeight(PlayerContext ctx) {
return this.tabListLines;
}
}
private class TabHolder implements UIComponent {
TabHolder() {
}
@Override
public void draw(PlayerContext ctx, LineFactory lineFactory) {
if (getActiveIndex() != -1) {
getActiveTab().draw(ctx, lineFactory);
}
}
}
private class StatusBar implements UIComponent {
StatusBar() {
}
@Override
public void draw(PlayerContext ctx, LineFactory lineFactory) {
Text prefix = Text.of("╚Status: ");
Text content = getStatusBarContent();
int remWidth = ctx.width - ctx.utils().getWidth(prefix) - ctx.utils().getWidth(content);
Text line = Text.builder().append(prefix, content, ctx.utils().repeatAndTerminate('═', '╝', remWidth)).build();
lineFactory.appendNewLine(line, ctx);
}
private Text getStatusBarContent() {
int unread = 0;
for (Tab tab : getTabs()) {
if (tab instanceof TextBufferTab) {
unread += ((TextBufferTab) tab).getUnread();
}
}
return Text.builder(String.valueOf(unread)).color(TextColors.RED)
.append(Text.builder(" unread messages").color(TextColors.RESET).build()).build();
}
@Override
public int getPrefHeight(PlayerContext ctx) {
return 1;
}
}
@Override
public String toString() {
return MoreObjects.toStringHelper(this)
.add("active", getActiveTab())
.add("tabs", this.tabs)
.toString();
}
}