/
GameUI.java
1389 lines (1286 loc) · 37.7 KB
/
GameUI.java
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* This file is part of the Haven & Hearth game client.
* Copyright (C) 2009 Fredrik Tolf <fredrik@dolda2000.com>, and
* Björn Johannessen <johannessen.bjorn@gmail.com>
*
* Redistribution and/or modification of this file is subject to the
* terms of the GNU Lesser General Public License, version 3, as
* published by the Free Software Foundation.
*
* 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.
*
* Other parts of this source tree adhere to other copying
* rights. Please see the file `COPYING' in the root directory of the
* source tree for details.
*
* A copy the GNU Lesser General Public License is distributed along
* with the source tree of which this file is a part in the file
* `doc/LPGL-3'. If it is missing for any reason, please see the Free
* Software Foundation's website at <http://www.fsf.org/>, or write
* to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
* Boston, MA 02111-1307 USA
*/
package haven;
import java.util.*;
import java.awt.Color;
import java.awt.event.KeyEvent;
import java.awt.image.WritableRaster;
import static haven.Inventory.invsq;
public class GameUI extends ConsoleHost implements Console.Directory {
public static final Text.Foundry msgfoundry = new Text.Foundry(Text.dfont, 14);
private static final int blpw = 142, brpw = 142;
public final String chrid, genus;
public final long plid;
private final Hidepanel ulpanel, umpanel, urpanel, blpanel, brpanel, menupanel;
public Avaview portrait;
public MenuGrid menu;
public MapView map;
public LocalMiniMap mmap;
public Fightview fv;
private List<Widget> meters = new LinkedList<Widget>();
private Text lastmsg;
private double msgtime;
private Window invwnd, equwnd, makewnd;
private Coord makewndc = Utils.getprefc("makewndc", new Coord(400, 200));
public Inventory maininv;
public CharWnd chrwdg;
public MapWnd mapfile;
private Widget qqview;
public BuddyWnd buddies;
private final Zergwnd zerg;
public final Collection<Polity> polities = new ArrayList<Polity>();
public HelpWnd help;
public OptWnd opts;
public Collection<DraggedItem> hand = new LinkedList<DraggedItem>();
public WItem vhand;
public ChatUI chat;
public ChatUI.Channel syslog;
public double prog = -1;
private boolean afk = false;
@SuppressWarnings("unchecked")
public Indir<Resource>[] belt = new Indir[144];
public Belt beltwdg;
public final Map<Integer, String> polowners = new HashMap<Integer, String>();
public Bufflist buffs;
public abstract class Belt extends Widget {
public Belt(Coord sz) {
super(sz);
}
public void keyact(final int slot) {
if(map != null) {
Coord mvc = map.rootxlate(ui.mc);
if(mvc.isect(Coord.z, map.sz)) {
map.delay(map.new Hittest(mvc) {
protected void hit(Coord pc, Coord2d mc, MapView.ClickInfo inf) {
Object[] args = {slot, 1, ui.modflags(), mc.floor(OCache.posres)};
if(inf != null)
args = Utils.extend(args, MapView.gobclickargs(inf));
GameUI.this.wdgmsg("belt", args);
}
protected void nohit(Coord pc) {
GameUI.this.wdgmsg("belt", slot, 1, ui.modflags());
}
});
}
}
}
}
@RName("gameui")
public static class $_ implements Factory {
public Widget create(UI ui, Object[] args) {
String chrid = (String)args[0];
int plid = (Integer)args[1];
String genus = "";
if(args.length > 2)
genus = (String)args[2];
return(new GameUI(chrid, plid, genus));
}
}
private final Coord minimapc;
public GameUI(String chrid, long plid, String genus) {
this.chrid = chrid;
this.plid = plid;
this.genus = genus;
setcanfocus(true);
setfocusctl(true);
chat = add(new ChatUI(0, 0));
if(Utils.getprefb("chatvis", true)) {
chat.hresize(chat.savedh);
chat.show();
}
beltwdg.raise();
blpanel = add(new Hidepanel("gui-bl", null, new Coord(-1, 1)));
brpanel = add(new Hidepanel("gui-br", null, new Coord( 1, 1)) {
public void move(double a) {
super.move(a);
menupanel.move();
}
});
menupanel = add(new Hidepanel("menu", new Indir<Coord>() {
public Coord get() {
return(new Coord(GameUI.this.sz.x, Math.min(brpanel.c.y - 79, GameUI.this.sz.y - menupanel.sz.y)));
}
}, new Coord(1, 0)));
ulpanel = add(new Hidepanel("gui-ul", null, new Coord(-1, -1)));
umpanel = add(new Hidepanel("gui-um", null, new Coord( 0, -1)));
urpanel = add(new Hidepanel("gui-ur", null, new Coord( 1, -1)));
Tex lbtnbg = Resource.loadtex("gfx/hud/lbtn-bg");
blpanel.add(new Img(Resource.loadtex("gfx/hud/blframe")), 0, lbtnbg.sz().y - 33);
blpanel.add(new Img(lbtnbg), 0, 0);
minimapc = new Coord(4, 34 + (lbtnbg.sz().y - 33));
brpanel.add(new Img(Resource.loadtex("gfx/hud/brframe")), 0, 0);
menupanel.add(new MainMenu(), 0, 0);
mapbuttons();
foldbuttons();
portrait = ulpanel.add(new Avaview(Avaview.dasz, plid, "avacam") {
public boolean mousedown(Coord c, int button) {
return(true);
}
}, new Coord(10, 10));
buffs = ulpanel.add(new Bufflist(), new Coord(95, 65));
umpanel.add(new Cal(), new Coord(0, 10));
syslog = chat.add(new ChatUI.Log("System"));
opts = add(new OptWnd());
opts.hide();
zerg = add(new Zergwnd(), Utils.getprefc("wndc-zerg", new Coord(187, 50)));
zerg.hide();
}
public static final KeyBinding kb_map = KeyBinding.get("map", KeyMatch.forchar('A', KeyMatch.C));
public static final KeyBinding kb_claim = KeyBinding.get("ol-claim", KeyMatch.nil);
public static final KeyBinding kb_vil = KeyBinding.get("ol-vil", KeyMatch.nil);
public static final KeyBinding kb_rlm = KeyBinding.get("ol-rlm", KeyMatch.nil);
private void mapbuttons() {
blpanel.add(new MenuButton("lbtn-claim", kb_claim, "Display personal claims") {
public void click() {
if((map != null) && !map.visol(0))
map.enol(0, 1);
else
map.disol(0, 1);
}
}, 0, 0);
blpanel.add(new MenuButton("lbtn-vil", kb_vil, "Display village claims") {
public void click() {
if((map != null) && !map.visol(2))
map.enol(2, 3);
else
map.disol(2, 3);
}
}, 0, 0);
blpanel.add(new MenuButton("lbtn-rlm", kb_rlm, "Display realms") {
public void click() {
if((map != null) && !map.visol(4))
map.enol(4, 5);
else
map.disol(4, 5);
}
}, 0, 0);
blpanel.add(new MenuButton("lbtn-map", kb_map, "Map") {
public void click() {
if((mapfile != null) && mapfile.show(!mapfile.visible)) {
mapfile.raise();
fitwdg(mapfile);
setfocus(mapfile);
}
}
});
}
/* Ice cream */
private final IButton[] fold_br = new IButton[4];
private final IButton[] fold_bl = new IButton[2];
private void updfold(boolean reset) {
int br;
if(brpanel.tvis && menupanel.tvis)
br = 0;
else if(brpanel.tvis && !menupanel.tvis)
br = 1;
else if(!brpanel.tvis && !menupanel.tvis)
br = 2;
else
br = 3;
for(int i = 0; i < fold_br.length; i++)
fold_br[i].show(i == br);
fold_bl[1].show(!blpanel.tvis);
if(reset)
resetui();
}
private void foldbuttons() {
final Tex rdnbg = Resource.loadtex("gfx/hud/rbtn-maindwn");
final Tex rupbg = Resource.loadtex("gfx/hud/rbtn-upbg");
fold_br[0] = new IButton("gfx/hud/rbtn-dwn", "", "-d", "-h") {
public void draw(GOut g) {g.image(rdnbg, Coord.z); super.draw(g);}
public void click() {
menupanel.cshow(false);
updfold(true);
}
};
fold_br[1] = new IButton("gfx/hud/rbtn-dwn", "", "-d", "-h") {
public void draw(GOut g) {g.image(rdnbg, Coord.z); super.draw(g);}
public void click() {
brpanel.cshow(false);
updfold(true);
}
};
fold_br[2] = new IButton("gfx/hud/rbtn-up", "", "-d", "-h") {
public void draw(GOut g) {g.image(rupbg, Coord.z); super.draw(g);}
public void click() {
menupanel.cshow(true);
updfold(true);
}
public void presize() {
this.c = parent.sz.sub(this.sz);
}
};
fold_br[3] = new IButton("gfx/hud/rbtn-dwn", "", "-d", "-h") {
public void draw(GOut g) {g.image(rdnbg, Coord.z); super.draw(g);}
public void click() {
brpanel.cshow(true);
updfold(true);
}
};
menupanel.add(fold_br[0], 0, 0);
fold_br[0].lower();
brpanel.adda(fold_br[1], brpanel.sz.x, 32, 1, 1);
adda(fold_br[2], 1, 1);
fold_br[2].lower();
menupanel.add(fold_br[3], 0, 0);
fold_br[3].lower();
final Tex lupbg = Resource.loadtex("gfx/hud/lbtn-upbg");
fold_bl[0] = new IButton("gfx/hud/lbtn-dwn", "", "-d", "-h") {
public void click() {
blpanel.cshow(false);
updfold(true);
}
};
fold_bl[1] = new IButton("gfx/hud/lbtn-up", "", "-d", "-h") {
public void draw(GOut g) {g.image(lupbg, Coord.z); super.draw(g);}
public void click() {
blpanel.cshow(true);
updfold(true);
}
public void presize() {
this.c = new Coord(0, parent.sz.y - sz.y);
}
};
blpanel.add(fold_bl[0], 0, 0);
adda(fold_bl[1], 0, 1);
fold_bl[1].lower();
updfold(false);
}
protected void added() {
resize(parent.sz);
ui.cons.out = new java.io.PrintWriter(new java.io.Writer() {
StringBuilder buf = new StringBuilder();
public void write(char[] src, int off, int len) {
List<String> lines = new ArrayList<String>();
synchronized(this) {
buf.append(src, off, len);
int p;
while((p = buf.indexOf("\n")) >= 0) {
lines.add(buf.substring(0, p));
buf.delete(0, p + 1);
}
}
for(String ln : lines)
syslog.append(ln, Color.WHITE);
}
public void close() {}
public void flush() {}
});
Debug.log = ui.cons.out;
opts.c = sz.sub(opts.sz).div(2);
}
public class Hidepanel extends Widget {
public final String id;
public final Coord g;
public final Indir<Coord> base;
public boolean tvis;
private double cur;
public Hidepanel(String id, Indir<Coord> base, Coord g) {
this.id = id;
this.base = base;
this.g = g;
cur = show(tvis = Utils.getprefb(id + "-visible", true))?0:1;
}
public <T extends Widget> T add(T child) {
super.add(child);
pack();
if(parent != null)
move();
return(child);
}
public Coord base() {
if(base != null) return(base.get());
return(new Coord((g.x > 0)?parent.sz.x:(g.x < 0)?0:((parent.sz.x - this.sz.x) / 2),
(g.y > 0)?parent.sz.y:(g.y < 0)?0:((parent.sz.y - this.sz.y) / 2)));
}
public void move(double a) {
cur = a;
Coord c = new Coord(base());
if(g.x < 0)
c.x -= (int)(sz.x * a);
else if(g.x > 0)
c.x -= (int)(sz.x * (1 - a));
if(g.y < 0)
c.y -= (int)(sz.y * a);
else if(g.y > 0)
c.y -= (int)(sz.y * (1 - a));
this.c = c;
}
public void move() {
move(cur);
}
public void presize() {
move();
}
public boolean mshow(final boolean vis) {
clearanims(Anim.class);
if(vis)
show();
new NormAnim(0.25) {
final double st = cur, f = vis?0:1;
public void ntick(double a) {
if((a == 1.0) && !vis)
hide();
move(st + (Utils.smoothstep(a) * (f - st)));
}
};
tvis = vis;
updfold(false);
return(vis);
}
public boolean mshow() {
return(mshow(Utils.getprefb(id + "-visible", true)));
}
public boolean cshow(boolean vis) {
Utils.setprefb(id + "-visible", vis);
if(vis != tvis)
mshow(vis);
return(vis);
}
public void cdestroy(Widget w) {
parent.cdestroy(w);
}
}
static class Hidewnd extends Window {
Hidewnd(Coord sz, String cap, boolean lg) {
super(sz, cap, lg);
}
Hidewnd(Coord sz, String cap) {
super(sz, cap);
}
public void wdgmsg(Widget sender, String msg, Object... args) {
if((sender == this) && msg.equals("close")) {
this.hide();
return;
}
super.wdgmsg(sender, msg, args);
}
}
static class Zergwnd extends Hidewnd {
Tabs tabs = new Tabs(Coord.z, Coord.z, this);
final TButton kin, pol, pol2;
class TButton extends IButton {
Tabs.Tab tab = null;
final Tex inv;
TButton(String nm, boolean g) {
super(Resource.loadimg("gfx/hud/buttons/" + nm + "u"), Resource.loadimg("gfx/hud/buttons/" + nm + "d"));
if(g)
inv = Resource.loadtex("gfx/hud/buttons/" + nm + "g");
else
inv = null;
}
public void draw(GOut g) {
if((tab == null) && (inv != null))
g.image(inv, Coord.z);
else
super.draw(g);
}
public void click() {
if(tab != null) {
tabs.showtab(tab);
repack();
}
}
}
Zergwnd() {
super(Coord.z, "Kith & Kin", true);
kin = add(new TButton("kin", false));
kin.tooltip = Text.render("Kin");
pol = add(new TButton("pol", true));
pol2 = add(new TButton("rlm", true));
}
private void repack() {
tabs.indpack();
kin.c = new Coord(0, tabs.curtab.contentsz().y + 20);
pol.c = new Coord(kin.c.x + kin.sz.x + 10, kin.c.y);
pol2.c = new Coord(pol.c.x + pol.sz.x + 10, pol.c.y);
this.pack();
}
Tabs.Tab ntab(Widget ch, TButton btn) {
Tabs.Tab tab = add(tabs.new Tab() {
public void cresize(Widget ch) {
repack();
}
}, tabs.c);
tab.add(ch, Coord.z);
btn.tab = tab;
repack();
return(tab);
}
void dtab(TButton btn) {
btn.tab.destroy();
btn.tab = null;
repack();
}
void addpol(Polity p) {
/* This isn't very nice. :( */
TButton btn = p.cap.equals("Village")?pol:pol2;
ntab(p, btn);
btn.tooltip = Text.render(p.cap);
}
}
static class DraggedItem {
final GItem item;
final Coord dc;
DraggedItem(GItem item, Coord dc) {
this.item = item; this.dc = dc;
}
}
private void updhand() {
if((hand.isEmpty() && (vhand != null)) || ((vhand != null) && !hand.contains(vhand.item))) {
ui.destroy(vhand);
vhand = null;
}
if(!hand.isEmpty() && (vhand == null)) {
DraggedItem fi = hand.iterator().next();
vhand = add(new ItemDrag(fi.dc, fi.item));
}
}
private String mapfilename() {
StringBuilder buf = new StringBuilder();
buf.append(genus);
String chrid = Utils.getpref("mapfile/" + this.chrid, "");
if(!chrid.equals("")) {
if(buf.length() > 0) buf.append('/');
buf.append(chrid);
}
return(buf.toString());
}
public Coord optplacement(Widget child, Coord org) {
Set<Window> closed = new HashSet<>();
Set<Coord> open = new HashSet<>();
open.add(org);
Coord opt = null;
double optscore = Double.NEGATIVE_INFINITY;
Coord plc = null;
{
Gob pl = map.player();
if(pl != null)
plc = pl.sc;
}
Area parea = Area.sized(Coord.z, sz);
while(!open.isEmpty()) {
Coord cur = Utils.take(open);
double score = 0;
Area tarea = Area.sized(cur, child.sz);
if(parea.isects(tarea)) {
double outside = 1.0 - (((double)parea.overlap(tarea).area()) / ((double)tarea.area()));
if((outside > 0.75) && !cur.equals(org))
continue;
score -= Math.pow(outside, 2) * 100;
} else {
if(!cur.equals(org))
continue;
score -= 100;
}
{
boolean any = false;
for(Widget wdg = this.child; wdg != null; wdg = wdg.next) {
if(!(wdg instanceof Window))
continue;
Window wnd = (Window)wdg;
if(!wnd.visible)
continue;
Area warea = wnd.parentarea(this);
if(warea.isects(tarea)) {
any = true;
score -= ((double)warea.overlap(tarea).area()) / ((double)tarea.area());
if(!closed.contains(wnd)) {
open.add(new Coord(wnd.c.x - child.sz.x, cur.y));
open.add(new Coord(cur.x, wnd.c.y - child.sz.y));
open.add(new Coord(wnd.c.x + wnd.sz.x, cur.y));
open.add(new Coord(cur.x, wnd.c.y + wnd.sz.y));
closed.add(wnd);
}
}
}
if(!any)
score += 10;
}
if(plc != null) {
if(tarea.contains(plc))
score -= 100;
else
score -= (1 - Math.pow(tarea.closest(plc).dist(plc) / sz.dist(Coord.z), 2)) * 1.5;
}
score -= (cur.dist(org) / sz.dist(Coord.z)) * 0.75;
if(score > optscore) {
optscore = score;
opt = cur;
}
}
return(opt);
}
private void savewndpos() {
if(invwnd != null)
Utils.setprefc("wndc-inv", invwnd.c);
if(equwnd != null)
Utils.setprefc("wndc-equ", equwnd.c);
if(chrwdg != null)
Utils.setprefc("wndc-chr", chrwdg.sz);
if(zerg != null)
Utils.setprefc("wndc-zerg", zerg.c);
if(mapfile != null) {
Utils.setprefc("wndc-map", mapfile.c);
Utils.setprefc("wndsz-map", mapfile.asz);
}
}
public void addchild(Widget child, Object... args) {
String place = ((String)args[0]).intern();
if(place == "mapview") {
child.resize(sz);
map = add((MapView)child, Coord.z);
map.lower();
if(mmap != null)
ui.destroy(mmap);
if(mapfile != null) {
ui.destroy(mapfile);
mapfile = null;
}
mmap = blpanel.add(new LocalMiniMap(new Coord(133, 133), map), minimapc);
mmap.lower();
if(ResCache.global != null) {
MapFile file = MapFile.load(ResCache.global, mapfilename());
mmap.save(file);
mapfile = new MapWnd(mmap.save, map, Utils.getprefc("wndsz-map", new Coord(700, 500)), "Map");
mapfile.hide();
add(mapfile, Utils.getprefc("wndc-map", new Coord(50, 50)));
}
} else if(place == "menu") {
menu = (MenuGrid)brpanel.add(child, 20, 34);
} else if(place == "fight") {
fv = urpanel.add((Fightview)child, 0, 0);
} else if(place == "fsess") {
add(child, Coord.z);
} else if(place == "inv") {
invwnd = new Hidewnd(Coord.z, "Inventory") {
public void cresize(Widget ch) {
pack();
}
};
invwnd.add(maininv = (Inventory)child, Coord.z);
invwnd.pack();
invwnd.hide();
add(invwnd, Utils.getprefc("wndc-inv", new Coord(100, 100)));
} else if(place == "equ") {
equwnd = new Hidewnd(Coord.z, "Equipment");
equwnd.add(child, Coord.z);
equwnd.pack();
equwnd.hide();
add(equwnd, Utils.getprefc("wndc-equ", new Coord(400, 10)));
} else if(place == "hand") {
GItem g = add((GItem)child);
Coord lc = (Coord)args[1];
hand.add(new DraggedItem(g, lc));
updhand();
} else if(place == "chr") {
chrwdg = add((CharWnd)child, Utils.getprefc("wndc-chr", new Coord(300, 50)));
chrwdg.hide();
} else if(place == "craft") {
final Widget mkwdg = child;
makewnd = new Window(Coord.z, "Crafting", true) {
public void wdgmsg(Widget sender, String msg, Object... args) {
if((sender == this) && msg.equals("close")) {
mkwdg.wdgmsg("close");
return;
}
super.wdgmsg(sender, msg, args);
}
public void cdestroy(Widget w) {
if(w == mkwdg) {
ui.destroy(this);
makewnd = null;
}
}
public void destroy() {
Utils.setprefc("makewndc", makewndc = this.c);
super.destroy();
}
};
makewnd.add(mkwdg, Coord.z);
makewnd.pack();
fitwdg(add(makewnd, makewndc));
} else if(place == "buddy") {
zerg.ntab(buddies = (BuddyWnd)child, zerg.kin);
} else if(place == "pol") {
Polity p = (Polity)child;
polities.add(p);
zerg.addpol(p);
} else if(place == "chat") {
chat.addchild(child);
} else if(place == "party") {
add(child, 10, 95);
} else if(place == "meter") {
int x = (meters.size() % 3) * (IMeter.fsz.x + 5);
int y = (meters.size() / 3) * (IMeter.fsz.y + 2);
ulpanel.add(child, portrait.c.x + portrait.sz.x + 10 + x, portrait.c.y + y);
meters.add(child);
} else if(place == "buff") {
buffs.addchild(child);
} else if(place == "qq") {
if(qqview != null)
qqview.reqdestroy();
final Widget cref = qqview = child;
add(new AlignPanel() {
{add(cref);}
protected Coord getc() {
return(new Coord(10, GameUI.this.sz.y - blpanel.sz.y - this.sz.y - 10));
}
public void cdestroy(Widget ch) {
qqview = null;
destroy();
}
});
} else if(place == "misc") {
Coord c;
if(args[1] instanceof Coord) {
c = (Coord)args[1];
} else if(args[1] instanceof Coord2d) {
c = ((Coord2d)args[1]).mul(new Coord2d(this.sz.sub(child.sz))).round();
c = optplacement(child, c);
} else if(args[1] instanceof String) {
c = relpos((String)args[1], child, (args.length > 2) ? ((Object[])args[2]) : new Object[] {}, 0);
} else {
throw(new UI.UIException("Illegal gameui child", place, args));
}
add(child, c);
} else if(place == "abt") {
add(child, Coord.z);
} else {
throw(new UI.UIException("Illegal gameui child", place, args));
}
}
public void cdestroy(Widget w) {
if(w instanceof GItem) {
for(Iterator<DraggedItem> i = hand.iterator(); i.hasNext();) {
DraggedItem di = i.next();
if(di.item == w) {
i.remove();
updhand();
}
}
} else if(polities.contains(w)) {
polities.remove(w);
zerg.dtab(zerg.pol);
} else if(w == chrwdg) {
chrwdg = null;
}
meters.remove(w);
}
private static final Resource.Anim progt = Resource.local().loadwait("gfx/hud/prog").layer(Resource.animc);
private Tex curprog = null;
private int curprogf, curprogb;
private void drawprog(GOut g, double prog) {
int fr = Utils.clip((int)Math.floor(prog * progt.f.length), 0, progt.f.length - 2);
int bf = Utils.clip((int)(((prog * progt.f.length) - fr) * 255), 0, 255);
if((curprog == null) || (curprogf != fr) || (curprogb != bf)) {
if(curprog != null)
curprog.dispose();
WritableRaster buf = PUtils.imgraster(progt.f[fr][0].sz);
PUtils.blit(buf, progt.f[fr][0].img.getRaster(), Coord.z);
PUtils.blendblit(buf, progt.f[fr + 1][0].img.getRaster(), Coord.z, bf);
curprog = new TexI(PUtils.rasterimg(buf)); curprogf = fr; curprogb = bf;
}
g.aimage(curprog, new Coord(sz.x / 2, (sz.y * 4) / 10), 0.5, 0.5);
}
public void draw(GOut g) {
beltwdg.c = new Coord(chat.c.x, Math.min(chat.c.y - beltwdg.sz.y + 4, sz.y - beltwdg.sz.y));
super.draw(g);
if(prog >= 0)
drawprog(g, prog);
int by = sz.y;
if(chat.visible)
by = Math.min(by, chat.c.y);
if(beltwdg.visible)
by = Math.min(by, beltwdg.c.y);
if(cmdline != null) {
drawcmd(g, new Coord(blpw + 10, by -= 20));
} else if(lastmsg != null) {
if((Utils.rtime() - msgtime) > 3.0) {
lastmsg = null;
} else {
g.chcolor(0, 0, 0, 192);
g.frect(new Coord(blpw + 8, by - 22), lastmsg.sz().add(4, 4));
g.chcolor();
g.image(lastmsg.tex(), new Coord(blpw + 10, by -= 20));
}
}
if(!chat.visible) {
chat.drawsmall(g, new Coord(blpw + 10, by), 50);
}
}
private double lastwndsave = 0;
public void tick(double dt) {
super.tick(dt);
double now = Utils.rtime();
if(now - lastwndsave > 60) {
savewndpos();
lastwndsave = now;
}
double idle = now - ui.lastevent;
if(!afk && (idle > 300)) {
afk = true;
wdgmsg("afk");
} else if(afk && (idle <= 300)) {
afk = false;
}
}
public void uimsg(String msg, Object... args) {
if(msg == "err") {
String err = (String)args[0];
error(err);
} else if(msg == "msg") {
String text = (String)args[0];
msg(text);
} else if(msg == "prog") {
if(args.length > 0)
prog = ((Number)args[0]).doubleValue() / 100.0;
else
prog = -1;
} else if(msg == "setbelt") {
int slot = (Integer)args[0];
if(args.length < 2) {
belt[slot] = null;
} else {
belt[slot] = ui.sess.getres((Integer)args[1]);
}
} else if(msg == "polowner") {
int id = (Integer)args[0];
String o = (String)args[1];
boolean n = ((Integer)args[2]) != 0;
if(o != null)
o = o.intern();
String cur = polowners.get(id);
if(map != null) {
if((o != null) && (cur == null)) {
map.setpoltext(id, "Entering " + o);
} else if((o == null) && (cur != null)) {
map.setpoltext(id, "Leaving " + cur);
}
}
polowners.put(id, o);
} else if(msg == "showhelp") {
Indir<Resource> res = ui.sess.getres((Integer)args[0]);
if(help == null)
help = adda(new HelpWnd(res), 0.5, 0.5);
else
help.res = res;
} else if(msg == "map-mark") {
long gobid = ((Integer)args[0]) & 0xffffffff;
long oid = (Long)args[1];
Indir<Resource> res = ui.sess.getres((Integer)args[2]);
String nm = (String)args[3];
if(mapfile != null)
mapfile.markobj(gobid, oid, res, nm);
} else {
super.uimsg(msg, args);
}
}
public void wdgmsg(Widget sender, String msg, Object... args) {
if((sender == chrwdg) && (msg == "close")) {
chrwdg.hide();
return;
} else if((sender == mapfile) && (msg == "close")) {
mapfile.hide();
return;
} else if((sender == help) && (msg == "close")) {
ui.destroy(help);
help = null;
return;
}
super.wdgmsg(sender, msg, args);
}
private void fitwdg(Widget wdg) {
if(wdg.c.x < 0)
wdg.c.x = 0;
if(wdg.c.y < 0)
wdg.c.y = 0;
if(wdg.c.x + wdg.sz.x > sz.x)
wdg.c.x = sz.x - wdg.sz.x;
if(wdg.c.y + wdg.sz.y > sz.y)
wdg.c.y = sz.y - wdg.sz.y;
}
public static class MenuButton extends IButton {
private final KeyBinding gkey;
private final String tt;
MenuButton(String base, KeyBinding gkey, String tooltip) {
super("gfx/hud/" + base, "", "-d", "-h");
this.gkey = gkey;
this.tt = tooltip;
}
public void click() {}
public boolean globtype(char key, KeyEvent ev) {
if(gkey.key().match(ev)) {
click();
return(true);
}
return(super.globtype(key, ev));
}
private RichText rtt = null;
public Object tooltip(Coord c, Widget prev) {
if(!checkhit(c))
return(null);
if((prev != this) || (rtt == null)) {
String tt = this.tt;
if(gkey.key() != KeyMatch.nil)
tt += String.format(" ($col[255,255,0]{%s})", RichText.Parser.quote(gkey.key().name()));
if((rtt == null) || !rtt.text.equals(tt))
rtt = RichText.render(tt, 0);
}
return(rtt.tex());
}
}
public static final KeyBinding kb_inv = KeyBinding.get("inv", KeyMatch.forcode(KeyEvent.VK_TAB, 0));
public static final KeyBinding kb_equ = KeyBinding.get("equ", KeyMatch.forchar('E', KeyMatch.C));
public static final KeyBinding kb_chr = KeyBinding.get("chr", KeyMatch.forchar('T', KeyMatch.C));
public static final KeyBinding kb_bud = KeyBinding.get("bud", KeyMatch.forchar('B', KeyMatch.C));
public static final KeyBinding kb_opt = KeyBinding.get("opt", KeyMatch.forchar('O', KeyMatch.C));
private static final Tex menubg = Resource.loadtex("gfx/hud/rbtn-bg");
public class MainMenu extends Widget {
public MainMenu() {
super(menubg.sz());
add(new MenuButton("rbtn-inv", kb_inv, "Inventory") {
public void click() {
if((invwnd != null) && invwnd.show(!invwnd.visible)) {
invwnd.raise();
fitwdg(invwnd);
}
}
}, 0, 0);
add(new MenuButton("rbtn-equ", kb_equ, "Equipment") {
public void click() {
if((equwnd != null) && equwnd.show(!equwnd.visible)) {
equwnd.raise();
fitwdg(equwnd);
}
}
}, 0, 0);
add(new MenuButton("rbtn-chr", kb_chr, "Character Sheet") {
public void click() {
if((chrwdg != null) && chrwdg.show(!chrwdg.visible)) {
chrwdg.raise();
fitwdg(chrwdg);
}
}
}, 0, 0);
add(new MenuButton("rbtn-bud", kb_bud, "Kith & Kin") {
public void click() {
if(zerg.show(!zerg.visible)) {
zerg.raise();
fitwdg(zerg);
setfocus(zerg);
}
}
}, 0, 0);
add(new MenuButton("rbtn-opt", kb_opt, "Options") {
public void click() {
if(opts.show(!opts.visible)) {
opts.raise();
fitwdg(opts);
setfocus(opts);
}
}
}, 0, 0);
}
public void draw(GOut g) {
g.image(menubg, Coord.z);
super.draw(g);
}
}
public static final KeyBinding kb_shoot = KeyBinding.get("screenshot", KeyMatch.forchar('S', KeyMatch.M));
public static final KeyBinding kb_chat = KeyBinding.get("chat-toggle", KeyMatch.forchar('C', KeyMatch.C));
public static final KeyBinding kb_hide = KeyBinding.get("ui-toggle", KeyMatch.nil);
public boolean globtype(char key, KeyEvent ev) {
if(key == ':') {
entercmd();
return(true);
} else if((Config.screenurl != null) && kb_shoot.key().match(ev)) {
Screenshooter.take(this, Config.screenurl);
return(true);
} else if(kb_hide.key().match(ev)) {
toggleui();
return(true);
} else if(kb_chat.key().match(ev)) {
if(chat.visible && !chat.hasfocus) {
setfocus(chat);
} else {
if(chat.targeth == 0) {
chat.sresize(chat.savedh);
setfocus(chat);
} else {