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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
|
/* Banana-Chat - The first Open Source Knuddels Emulator
* Copyright (C) 2011 Flav <http://www.banana-coding.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package tools.popup;
import java.util.ArrayList;
import java.util.List;
import knuddels.SendOpcode;
import tools.PacketBuilder;
/**
*
* @author Flav
*/
public class Popup {
private String title, subtitle, message;
private int width, height;
private List<Panel> panels;
private String opcode, parameter;
public Popup(String title, String subtitle, String message, int width, int height) {
this.title = title;
this.subtitle = subtitle;
this.message = message;
this.width = width;
this.height = height;
panels = new ArrayList<Panel>();
}
public void addPanel(Panel panel) {
panels.add(panel);
}
public void setOpcode(String opcode, String parameter) {
this.opcode = opcode;
this.parameter = parameter;
}
@Override
public String toString() {
PacketBuilder buffer = new PacketBuilder(SendOpcode.POPUP.getValue());
buffer.writeByte(0x00);
addString(buffer, title);
if (opcode != null) {
buffer.writeByte('s');
addString(buffer, opcode);
addString(buffer, parameter);
}
addForeground(buffer, new int[] { 0x00, 0x00, 0x00 });
addBackground(buffer, new int[] { 0xBE, 0xBC, 0xFB });
buffer.writeByte(0xE3);
// border right
buffer.writeByte('E');
buffer.writeByte('l');
addString(buffer, " ");
addFontStyle(buffer, 'p', 5);
addBackground(buffer, new int[] { 0xBE, 0xBC, 0xFB });
buffer.writeByte(0xE3);
// border left
buffer.writeByte('W');
buffer.writeByte('l');
addString(buffer, " ");
addFontStyle(buffer, 'p', 5);
addBackground(buffer, new int[] { 0xBE, 0xBC, 0xFB });
buffer.writeByte(0xE3);
buffer.writeByte('C');
addLayout(buffer, 'B');
buffer.writeByte('N');
addLayout(buffer, 'B');
// border top
buffer.writeByte('N');
buffer.writeByte('l');
addString(buffer, " ");
addFontStyle(buffer, 'p', 5);
addBackground(buffer, new int[] { 0xBE, 0xBC, 0xFB });
buffer.writeByte(0xE3);
if (subtitle != null) {
buffer.writeByte('C');
buffer.writeByte('l');
addString(buffer, subtitle);
addFontStyle(buffer, 'b', 16);
addForeground(buffer, new int[] { 0x00, 0x00, 0x00 });
addBackground(buffer, new int[] { 0xE5, 0xE5, 0xFF });
buffer.writeByte(0xE3);
buffer.writeByte('S');
buffer.writeByte('l');
addString(buffer, " ");
addFontStyle(buffer, 'p', 5);
addBackground(buffer, new int[] { 0xBE, 0xBC, 0xFB });
buffer.writeByte(0xE3);
}
buffer.writeByte(0xE3);
if (message != null) {
buffer.writeByte('C');
buffer.writeByte('c');
addString(buffer, message);
addFrameSize(buffer, width, height);
addForeground(buffer, new int[] { 0x00, 0x00, 0x00 });
addBackground(buffer, new int[] { 0xBE, 0xBC, 0xFB });
addBackgroundImage(buffer, "pics/cloudsblue.gif", 17);
buffer.writeByte(0xE3);
}
boolean useBorderLayouts = panels.size() > 1;
int borderLayouts = 0;
for (Panel panel : panels) {
buffer.writeByte('S');
if (useBorderLayouts) {
borderLayouts++;
addLayout(buffer, 'B');
buffer.writeByte('N');
}
addLayout(buffer, 'F');
for (Component component : panel.getComponents()) {
ComponentType type = component.getType();
buffer.writeByte(type.getValue());
if(type != ComponentType.CHOICE) addString(buffer, component.getText());
if(type == ComponentType.CHOICE) {
Choice choice = (Choice) component;
if(choice.useIndex()) {
buffer.writeByte('c');
buffer.writeByte(choice.getSelectedIndex());
} else {
buffer.writeByte('C');
addString(buffer, choice.getSelected());
}
if(choice.getFontsize() > 0)
addFontStyle(buffer, 'p', choice.getFontsize());
if(choice.isDisabled()) {
buffer.writeByte('d');
}
addForeground(buffer, component.getForeground());
addBackground(buffer, component.getBackground());
buffer.writeByte(0xE3);
for(String item : choice.getItems()) {
addString(buffer, item);
}
} else if(type == ComponentType.BUTTON) {
Button button = (Button) component;
if (button.isStyled()) {
buffer.writeByte('c');
if (button.isColored()) {
buffer.writeByte('e');
}
}
if (button.isClose()) {
buffer.writeByte('d');
}
addFontStyle(buffer, 'p', 16);
if (button.isAction()) {
buffer.writeByte('s');
}
if (button.getCommand() != null) {
buffer.writeByte('u');
addString(buffer, button.getCommand());
}
} else if (type == ComponentType.TEXT_FIELD) {
addSize(buffer, ((TextField) component).getWidth());
} else if (type == ComponentType.LABEL) {
addFontStyle(buffer, 'p', 16);
} else if (type == ComponentType.TEXT_AREA) {
TextArea textarea = (TextArea) component;
addSize(buffer, textarea.getRows());
addSize(buffer, textarea.getColumns());
switch (textarea.getScrollbars()) {
case 0:
buffer.writeByte('b');
break;
case 1:
buffer.writeByte('s');
break;
case 2:
buffer.writeByte('w');
break;
}
if (textarea.isEditable()) {
buffer.writeByte('e');
}
}
if(type != ComponentType.CHOICE) {
addForeground(buffer, component.getForeground());
addBackground(buffer, component.getBackground());
}
buffer.writeByte(0xE3);
}
buffer.writeByte(0xE3);
}
for (int i = 0; i < borderLayouts; i++) {
buffer.writeByte(0xE3);
}
buffer.writeByte(0xE3);
buffer.writeByte(0xE3);
System.out.println(buffer.toString());
return buffer.toString();
}
private static void addSize(PacketBuilder buffer, int size) {
buffer.writeByte('A' + size);
}
private static void addString(PacketBuilder buffer, String str) {
buffer.writeString(str);
buffer.writeByte(0xF5);
}
private static void addFontStyle(PacketBuilder buffer, char weight, int size) {
if (weight != 'p') {
buffer.writeByte(weight);
}
buffer.writeByte('g');
addSize(buffer, size);
}
private static void addLayout(PacketBuilder buffer, char layout) {
buffer.writeByte('p');
buffer.writeByte(layout);
}
private static void addFrameSize(PacketBuilder buffer, int width, int height) {
buffer.writeByte('s');
buffer.writeShort(width);
buffer.writeShort(height);
}
private static void addForeground(PacketBuilder buffer, int[] color) {
buffer.writeByte('f');
buffer.write(color);
}
private static void addBackground(PacketBuilder buffer, int[] color) {
buffer.writeByte('h');
buffer.write(color);
}
private static void addBackgroundImage(PacketBuilder buffer, String image, int position) {
buffer.writeByte('i');
addString(buffer, image);
buffer.writeShort(position);
}
}
|