Beispiel #1
0
public class MenuGrid extends Widget {
  public static final Tex bg = Resource.loadtex("gfx/hud/invsq");
  public static final Coord bgsz = bg.sz().add(-1, -1);
  public static final Pagina next =
      new Glob.Pagina(Resource.local().loadwait("gfx/hud/sc-next").indir());
  public static final Pagina bk =
      new Glob.Pagina(Resource.local().loadwait("gfx/hud/sc-back").indir());
  public static final RichText.Foundry ttfnd =
      new RichText.Foundry(TextAttribute.FAMILY, "SansSerif", TextAttribute.SIZE, 10);
  private static Coord gsz = new Coord(4, 4);
  private Pagina cur, pressed, dragging, layout[][] = new Pagina[gsz.x][gsz.y];
  private UI.Grab grab;
  private int curoff = 0;
  private int pagseq = 0;
  private boolean loading = true;
  private Map<Character, Pagina> hotmap = new TreeMap<Character, Pagina>();

  @RName("scm")
  public static class $_ implements Factory {
    public Widget create(Widget parent, Object[] args) {
      return (new MenuGrid());
    }
  }

  public class PaginaException extends RuntimeException {
    public Pagina pag;

    public PaginaException(Pagina p) {
      super("Invalid pagina: " + p.res);
      pag = p;
    }
  }

  private boolean cons(Pagina p, Collection<Pagina> buf) {
    Pagina[] cp = new Pagina[0];
    Collection<Pagina> open, close = new HashSet<Pagina>();
    synchronized (ui.sess.glob.paginae) {
      open = new LinkedList<Pagina>();
      for (Pagina pag : ui.sess.glob.paginae) {
        if (pag.newp == 2) {
          pag.newp = 0;
          pag.fstart = 0;
        }
        open.add(pag);
      }
      for (Pagina pag : ui.sess.glob.pmap.values()) {
        if (pag.newp == 2) {
          pag.newp = 0;
          pag.fstart = 0;
        }
      }
    }
    boolean ret = true;
    while (!open.isEmpty()) {
      Iterator<Pagina> iter = open.iterator();
      Pagina pag = iter.next();
      iter.remove();
      try {
        AButton ad = pag.act();
        if (ad == null) throw (new PaginaException(pag));
        Pagina parent = paginafor(ad.parent);
        if ((pag.newp != 0) && (parent != null) && (parent.newp == 0)) {
          parent.newp = 2;
          parent.fstart = (parent.fstart == 0) ? pag.fstart : Math.min(parent.fstart, pag.fstart);
        }
        if (parent == p) buf.add(pag);
        else if ((parent != null) && !close.contains(parent) && !open.contains(parent))
          open.add(parent);
        close.add(pag);
      } catch (Loading e) {
        ret = false;
      }
    }
    return (ret);
  }

  public MenuGrid() {
    super(bgsz.mul(gsz).add(1, 1));
  }

  @Override
  protected void attach(UI ui) {
    super.attach(ui);
    Glob glob = ui.sess.glob;
    ObservableCollection<Pagina> p = glob.paginae;
    p.add(glob.paginafor(Resource.local().load("paginae/custom/plant-tree")));
    p.add(glob.paginafor(Resource.local().load("paginae/custom/fill-trough")));
    p.add(glob.paginafor(Resource.local().load("paginae/custom/fill-coop")));
    p.add(glob.paginafor(Resource.local().load("paginae/custom/fill-tarkiln")));
    p.add(glob.paginafor(Resource.local().load("paginae/custom/pick-mussels")));
    p.add(glob.paginafor(Resource.local().load("paginae/custom/fill-smelter")));
    p.add(glob.paginafor(Resource.local().load("paginae/custom/arrow-autoloader")));
  }

  private static Comparator<Pagina> sorter =
      new Comparator<Pagina>() {
        public int compare(Pagina a, Pagina b) {
          AButton aa = a.act(), ab = b.act();
          if ((aa.ad.length == 0) && (ab.ad.length > 0)) return (-1);
          if ((aa.ad.length > 0) && (ab.ad.length == 0)) return (1);
          return (aa.name.compareTo(ab.name));
        }
      };

  private void updlayout() {
    synchronized (ui.sess.glob.paginae) {
      List<Pagina> cur = new ArrayList<Pagina>();
      loading = !cons(this.cur, cur);
      Collections.sort(cur, sorter);
      int i = curoff;
      hotmap.clear();
      for (int y = 0; y < gsz.y; y++) {
        for (int x = 0; x < gsz.x; x++) {
          Pagina btn = null;
          if ((this.cur != null) && (x == gsz.x - 1) && (y == gsz.y - 1)) {
            btn = bk;
          } else if ((cur.size() > ((gsz.x * gsz.y) - 1)) && (x == gsz.x - 2) && (y == gsz.y - 1)) {
            btn = next;
          } else if (i < cur.size()) {
            Resource.AButton ad = cur.get(i).act();
            if (ad.hk != 0) hotmap.put(Character.toUpperCase(ad.hk), cur.get(i));
            btn = cur.get(i++);
          }
          layout[x][y] = btn;
        }
      }
      pagseq = ui.sess.glob.pagseq;
    }
  }

  private static Text rendertt(Resource res, boolean withpg) {
    Resource.AButton ad = res.layer(Resource.action);
    Resource.Pagina pg = res.layer(Resource.pagina);
    String tt = ad.name;
    int pos = tt.toUpperCase().indexOf(Character.toUpperCase(ad.hk));
    if (pos >= 0)
      tt =
          tt.substring(0, pos)
              + "$b{$col[255,128,0]{"
              + tt.charAt(pos)
              + "}}"
              + tt.substring(pos + 1);
    else if (ad.hk != 0) tt += " [" + ad.hk + "]";
    if (withpg && (pg != null)) {
      tt += "\n\n" + pg.text;
    }
    return (ttfnd.render(tt, 300));
  }

  private static Map<Pagina, Tex> glowmasks = new WeakHashMap<Pagina, Tex>();

  private Tex glowmask(Pagina pag) {
    Tex ret = glowmasks.get(pag);
    if (ret == null) {
      ret =
          new TexI(
              PUtils.glowmask(
                  PUtils.glowmask(pag.res().layer(Resource.imgc).img.getRaster()),
                  4,
                  new Color(32, 255, 32)));
      glowmasks.put(pag, ret);
    }
    return (ret);
  }

  public void draw(GOut g) {
    long now = System.currentTimeMillis();
    for (int y = 0; y < gsz.y; y++) {
      for (int x = 0; x < gsz.x; x++) {
        Coord p = bgsz.mul(new Coord(x, y));
        g.image(bg, p);
        Pagina btn = layout[x][y];
        if (btn != null) {
          Tex btex = btn.img.tex();
          g.image(btex, p.add(1, 1));
          if (btn.meter > 0) {
            double m = btn.meter / 1000.0;
            if (btn.dtime > 0) m += (1 - m) * (double) (now - btn.gettime) / (double) btn.dtime;
            m = Utils.clip(m, 0, 1);
            g.chcolor(255, 255, 255, 128);
            g.fellipse(p.add(bgsz.div(2)), bgsz.div(2), 90, (int) (90 + (360 * m)));
            g.chcolor();
          }
          if (btn.newp != 0) {
            if (btn.fstart == 0) {
              btn.fstart = now;
            } else {
              double ph = ((now - btn.fstart) / 1000.0) - (((x + (y * gsz.x)) * 0.15) % 1.0);
              if (ph < 1.25) {
                g.chcolor(255, 255, 255, (int) (255 * ((Math.cos(ph * Math.PI * 2) * -0.5) + 0.5)));
                g.image(glowmask(btn), p.sub(4, 4));
                g.chcolor();
              } else {
                g.chcolor(255, 255, 255, 128);
                g.image(glowmask(btn), p.sub(4, 4));
                g.chcolor();
              }
            }
          }
          if (btn == pressed) {
            g.chcolor(new Color(0, 0, 0, 128));
            g.frect(p.add(1, 1), btex.sz());
            g.chcolor();
          }
        }
      }
    }
    super.draw(g);
    if (dragging != null) {
      final Tex dt = dragging.img.tex();
      ui.drawafter(
          new UI.AfterDraw() {
            public void draw(GOut g) {
              g.image(dt, ui.mc.add(dt.sz().div(2).inv()));
            }
          });
    }
  }

  private Pagina curttp = null;
  private boolean curttl = false;
  private Text curtt = null;
  private long hoverstart;

  public Object tooltip(Coord c, Widget prev) {
    Pagina pag = bhit(c);
    long now = System.currentTimeMillis();
    if ((pag != null) && (pag.act() != null)) {
      if (prev != this) hoverstart = now;
      boolean ttl = (now - hoverstart) > 500;
      if ((pag != curttp) || (ttl != curttl)) {
        curtt = rendertt(pag.res(), ttl);
        curttp = pag;
        curttl = ttl;
      }
      return (curtt);
    } else {
      hoverstart = now;
      return ("");
    }
  }

  private Pagina bhit(Coord c) {
    Coord bc = c.div(bgsz);
    if ((bc.x >= 0) && (bc.y >= 0) && (bc.x < gsz.x) && (bc.y < gsz.y)) return (layout[bc.x][bc.y]);
    else return (null);
  }

  public boolean mousedown(Coord c, int button) {
    Pagina h = bhit(c);
    if ((button == 1) && (h != null)) {
      pressed = h;
      grab = ui.grabmouse(this);
    }
    return (true);
  }

  public void mousemove(Coord c) {
    if ((dragging == null) && (pressed != null)) {
      Pagina h = bhit(c);
      if (h != pressed) dragging = pressed;
    }
  }

  private Pagina paginafor(Resource.Named res) {
    return (ui.sess.glob.paginafor(res));
  }

  private void use(Pagina r, boolean reset) {
    Collection<Pagina> sub = new LinkedList<Pagina>(), cur = new LinkedList<Pagina>();
    cons(r, sub);
    cons(this.cur, cur);
    if (sub.size() > 0) {
      this.cur = r;
      curoff = 0;
    } else if (r == bk) {
      this.cur = paginafor(this.cur.act().parent);
      curoff = 0;
    } else if (r == next) {
      if ((curoff + 14) >= cur.size()) curoff = 0;
      else curoff += 14;
    } else {
      r.newp = 0;
      use(r);
      if (reset) this.cur = null;
      curoff = 0;
    }
    updlayout();
  }

  public boolean use(Pagina r) {
    String[] ad = r.act().ad;
    if ((ad == null) || (ad.length < 1)) {
      return false;
    }
    if (ad[0].equals("@")) {
      usecustom(ad);
    } else {
      wdgmsg("act", (Object[]) ad);
    }
    return true;
  }

  private void usecustom(String[] ad) {
    if (ad[1].equals("plant-tree")) {
      if (ui != null && ui.gui != null) {
        ui.gui.add(new TreePlantTool(ui.gui), 300, 200);
      }
    } else if (ad[1].equals("fill-trough")) {
      ui.gui.tasks.add(new FeedEdiblesTask("trough"));
    } else if (ad[1].equals("fill-coop")) {
      ui.gui.tasks.add(new FeedEdiblesTask("chickencoop"));
    } else if (ad[1].equals("fill-tarkiln")) {
      ui.gui.tasks.add(new FeedBlocksTask("tarkiln"));
    } else if (ad[1].equals("pick-mussels")) {
      ui.gui.tasks.add(new Forager(200, Integer.MAX_VALUE, "mussels"));
    } else if (ad[1].equals("fill-smelter")) {
      ui.gui.tasks.add(new FeedCoalTask("smelter", 11));
    } else if (ad[1].equals("arrow-autoloader")) {
      Config.enableAutoloader = !Config.enableAutoloader;
      ui.gui.msg(
          String.format("Autoloader is now turned %s", Config.enableAutoloader ? "on" : "off"));
    }
  }

  public void tick(double dt) {
    if (loading || (pagseq != ui.sess.glob.pagseq)) updlayout();
  }

  public boolean mouseup(Coord c, int button) {
    Pagina h = bhit(c);
    if ((button == 1) && (grab != null)) {
      if (dragging != null) {
        ui.dropthing(ui.root, ui.mc, dragging.res());
        dragging = pressed = null;
      } else if (pressed != null) {
        if (pressed == h) use(h, false);
        pressed = null;
      }
      grab.remove();
      grab = null;
    }
    return (true);
  }

  public void uimsg(String msg, Object... args) {
    if (msg == "goto") {
      String resnm = (String) args[0];
      if (resnm.equals("")) {
        cur = null;
      } else {
        Resource.Named res = Resource.remote().load(resnm, (Integer) args[1]);
        cur = paginafor(res);
      }
      curoff = 0;
      updlayout();
    }
  }

  public boolean globtype(char k, KeyEvent ev) {
    if ((k == 27) && (this.cur != null)) {
      this.cur = null;
      curoff = 0;
      updlayout();
      return (true);
    } else if ((k == 8) && (this.cur != null)) {
      this.cur = paginafor(this.cur.act().parent);
      curoff = 0;
      updlayout();
      return (true);
    } else if ((k == 'N') && (layout[gsz.x - 2][gsz.y - 1] == next)) {
      use(next, false);
      return (true);
    }
    Pagina r = hotmap.get(Character.toUpperCase(k));
    if (r != null) {
      use(r, true);
      return (true);
    }
    return (false);
  }
}
Beispiel #2
0
public class Fightsess extends Widget {
  public static final Tex lframe = Resource.loadtex("gfx/hud/combat/lframe");
  public static final int actpitch = 50;
  public final Indir<Resource>[] actions;
  public final boolean[] dyn;
  public int use = -1;
  public Coord pcc;
  public int pho;
  private final Fightview fv;

  @RName("fsess")
  public static class $_ implements Factory {
    public Widget create(Widget parent, Object[] args) {
      int nact = (Integer) args[0];
      return (new Fightsess(nact, parent.getparent(GameUI.class).fv));
    }
  }

  @SuppressWarnings("unchecked")
  public Fightsess(int nact, Fightview fv) {
    this.fv = fv;
    pho = -40;
    this.actions = (Indir<Resource>[]) new Indir[nact];
    this.dyn = new boolean[nact];
  }

  public void presize() {
    resize(parent.sz);
    pcc = sz.div(2);
  }

  protected void added() {
    presize();
  }

  private void updatepos() {
    MapView map;
    Gob pl;
    if (((map = getparent(GameUI.class).map) == null)
        || ((pl = map.player()) == null)
        || (pl.sc == null)) return;
    pcc = pl.sc;
    pho = (int) (pl.sczu.mul(20f).y) - 20;
  }

  private static final Text.Furnace ipf =
      new PUtils.BlurFurn(
          new Text.Foundry(Text.serif, 18, new Color(128, 128, 255)).aa(true),
          1,
          1,
          new Color(48, 48, 96));
  private final Text.UText<?> ip =
      new Text.UText<Integer>(ipf) {
        public String text(Integer v) {
          return ("IP: " + v);
        }

        public Integer value() {
          return (fv.current.ip);
        }
      };
  private final Text.UText<?> oip =
      new Text.UText<Integer>(ipf) {
        public String text(Integer v) {
          return ("IP: " + v);
        }

        public Integer value() {
          return (fv.current.oip);
        }
      };

  public void draw(GOut g) {
    updatepos();
    double now = System.currentTimeMillis() / 1000.0;

    for (Buff buff : fv.buffs.children(Buff.class))
      buff.draw(
          g.reclip(
              pcc.add(-buff.c.x - Buff.cframe.sz().x - 20, buff.c.y + pho - Buff.cframe.sz().y),
              buff.sz));
    if (fv.current != null) {
      for (Buff buff : fv.current.buffs.children(Buff.class))
        buff.draw(g.reclip(pcc.add(buff.c.x + 20, buff.c.y + pho - Buff.cframe.sz().y), buff.sz));

      g.aimage(ip.get().tex(), pcc.add(-75, 0), 1, 0.5);
      g.aimage(oip.get().tex(), pcc.add(75, 0), 0, 0.5);
    }

    if (now < fv.atkct) {
      int w = (int) ((fv.atkct - now) * 20);
      g.chcolor(255, 0, 128, 255);
      g.frect(pcc.add(-w, 20), new Coord(w * 2, 15));
      g.chcolor();
    }
    Coord ca = pcc.add(-(actions.length * actpitch) / 2, 45);
    for (int i = 0; i < actions.length; i++) {
      Indir<Resource> act = actions[i];
      try {
        if (act != null) {
          Tex img = act.get().layer(Resource.imgc).tex();
          g.image(img, ca);
          g.image(dyn[i] ? lframe : Buff.frame, ca.sub(Buff.imgoff));
          if (i == use) {
            g.chcolor(255, 0, 128, 255);
            Coord cc = ca.add(img.sz().x / 2, img.sz().y + 5);
            g.frect(cc.sub(2, 2), new Coord(5, 5));
            g.chcolor();
          }
        }
      } catch (Loading l) {
      }
      ca.x += actpitch;
    }
  }

  private Widget prevtt = null;

  public Object tooltip(Coord c, Widget prev) {
    for (Buff buff : fv.buffs.children(Buff.class)) {
      Coord dc = pcc.add(-buff.c.x - Buff.cframe.sz().x - 20, buff.c.y + pho - Buff.cframe.sz().y);
      if (c.isect(dc, buff.sz)) {
        Object ret = buff.tooltip(c.sub(dc), prevtt);
        if (ret != null) {
          prevtt = buff;
          return (ret);
        }
      }
    }
    if (fv.current != null) {
      for (Buff buff : fv.current.buffs.children(Buff.class)) {
        Coord dc = pcc.add(buff.c.x + 20, buff.c.y + pho - Buff.cframe.sz().y);
        if (c.isect(dc, buff.sz)) {
          Object ret = buff.tooltip(c.sub(dc), prevtt);
          if (ret != null) {
            prevtt = buff;
            return (ret);
          }
        }
      }
    }
    Coord ca = pcc.add(-(actions.length * actpitch) / 2, 45);
    for (int i = 0; i < actions.length; i++) {
      Indir<Resource> act = actions[i];
      try {
        if (act != null) {
          Tex img = act.get().layer(Resource.imgc).tex();
          if (c.isect(ca, img.sz())) {
            if (dyn[i]) return ("Combat discovery");
            return (act.get().layer(Resource.tooltip).t);
          }
        }
      } catch (Loading l) {
      }
      ca.x += actpitch;
    }
    return (null);
  }

  public void uimsg(String msg, Object... args) {
    if (msg == "act") {
      int n = (Integer) args[0];
      if (args.length > 1) {
        Indir<Resource> res = ui.sess.getres((Integer) args[1]);
        actions[n] = res;
        dyn[n] = ((Integer) args[2]) != 0;
      } else {
        actions[n] = null;
      }
    } else if (msg == "use") {
      this.use = (Integer) args[0];
    } else if (msg == "used") {
    } else if (msg == "dropped") {
    } else {
      super.uimsg(msg, args);
    }
  }

  public boolean globtype(char key, KeyEvent ev) {
    int c = ev.getKeyChar();
    if ((key == 0) && (c >= KeyEvent.VK_1) && (c < KeyEvent.VK_1 + actions.length)) {
      int n = c - KeyEvent.VK_1;
      if ((ev.getModifiersEx() & KeyEvent.CTRL_DOWN_MASK) != 0) wdgmsg("drop", n);
      else wdgmsg("use", n);
      return (true);
    }
    return (super.globtype(key, ev));
  }
}
Beispiel #3
0
public class Avaview extends Widget {
  static final String POSKEY = "pava_pos";
  public static final Coord dasz = new Coord(74, 74);
  private Coord asz;
  int avagob;
  boolean none = false;
  AvaRender myown = null;
  public Color color = Color.WHITE;
  public static final Coord unborder = new Coord(2, 2);
  public static final Tex missing = Resource.loadtex("gfx/hud/equip/missing");

  boolean dm = false;
  Coord doff;

  static {
    Widget.addtype(
        "av",
        new WidgetFactory() {
          public Widget create(Coord c, Widget parent, Object[] args) {
            if (UI.instance.mainview != null
                && UI.instance.mainview.playergob == (Integer) args[0]) {
              c = new Coord(Config.window_props.getProperty(POSKEY, c.toString()));
            }
            return (new Avaview(c, parent, (Integer) args[0]));
          }
        });
    Widget.addtype(
        "av2",
        new WidgetFactory() {
          public Widget create(Coord c, Widget parent, Object[] args) {
            List<Indir<Resource>> rl = new LinkedList<Indir<Resource>>();
            for (Object arg : args) rl.add(parent.ui.sess.getres((Integer) arg));
            return (new Avaview(c, parent, rl));
          }
        });
  }

  private Avaview(Coord c, Widget parent, Coord asz) {
    super(c, asz.add(Window.wbox.bisz()).add(unborder.mul(2).inv()), parent);
    this.asz = asz;
  }

  public Avaview(Coord c, Widget parent, int avagob, Coord asz) {
    this(c, parent, asz);
    this.avagob = avagob;
  }

  public Avaview(Coord c, Widget parent, int avagob) {
    this(c, parent, avagob, dasz);
  }

  public Avaview(Coord c, Widget parent, List<Indir<Resource>> rl) {
    this(c, parent, dasz);
    if (rl.size() == 0) none = true;
    else this.myown = new AvaRender(rl);
  }

  public void uimsg(String msg, Object... args) {
    if (msg == "upd") {
      this.avagob = (Integer) args[0];
      return;
    }
    if (msg == "ch") {
      List<Indir<Resource>> rl = new LinkedList<Indir<Resource>>();
      for (Object arg : args) rl.add(ui.sess.getres((Integer) arg));
      if (rl.size() == 0) {
        this.myown = null;
        none = true;
      } else {
        if (myown != null) myown.setlay(rl);
        else myown = new AvaRender(rl);
        none = false;
      }
      return;
    }
    super.uimsg(msg, args);
  }

  public void draw(GOut g) {
    Tex at = null;
    if (none) {
    } else if (myown != null) {
      at = myown;
    } else {
      Gob gob = ui.sess.glob.oc.getgob(avagob);
      Avatar ava = null;
      if (gob != null) ava = gob.getattr(Avatar.class);
      if (ava != null) at = ava.rend;
    }
    GOut g2 = g.reclip(Window.wbox.tloff().add(unborder.inv()), asz);
    int yo;
    if (at == null) {
      at = missing;
      yo = 0;
    } else {
      g2.image(Equipory.bg, new Coord(Equipory.bg.sz().x / 2 - asz.x / 2, 20).inv());
      yo = (20 * asz.y) / dasz.y;
    }
    Coord tsz = new Coord((at.sz().x * asz.x) / dasz.x, (at.sz().y * asz.y) / dasz.y);
    g2.image(at, new Coord(tsz.x / 2 - asz.x / 2, yo).inv(), tsz);
    g.chcolor(color);
    Window.wbox.draw(g, Coord.z, asz.add(Window.wbox.bisz()).add(unborder.mul(2).inv()));
  }

  public boolean mousedown(Coord c, int button) {
    switch (button) {
      case 1:
        ui.grabmouse(this);
        dm = true;
        doff = c;
        wdgmsg("click", button);
        break;
      case 3:
        if (parent instanceof Partyview || parent instanceof Fightview) {
          wdgmsg("click", button);
        } else if (avagob > 0
            && ui.sess.glob.oc.getgob(avagob) != null
            && ui.sess.glob.oc.getgob(avagob).getattr(Avatar.class) != null) {
          new XAvaGear(this.c.add(c), ui.root, avagob);
        } else if (myown != null) {
          new XAvaGear(this.c.add(c), ui.root, myown);
        }
    }
    return (true);
  }

  public boolean mouseup(Coord c, int button) {
    if (dm) {
      ui.grabmouse(null);
      dm = false;
      if (ui.mainview != null && avagob == ui.mainview.playergob) {
        Config.setWindowOpt(POSKEY, this.c.toString());
      } else if (parent instanceof Partyview) {
        ((Partyview) parent).saveparty();
      }
    } else {
      super.mouseup(c, button);
    }
    return (true);
  }

  public void mousemove(Coord c) {
    if (dm && !Config.global_ui_lock) {
      if (ui.mainview != null && avagob == ui.mainview.playergob) {
        this.c = this.c.add(c.add(doff.inv()));
      } else if (parent instanceof Partyview) {
        ((Partyview) parent).moveparty(c.add(doff.inv()));
      }
    } else {
      super.mousemove(c);
    }
  }
}
Beispiel #4
0
public class Window extends Widget implements DTarget {
  private static final Tex tleft = Resource.loadtex("gfx/hud/wnd/tleft");
  private static final Tex tmain = Resource.loadtex("gfx/hud/wnd/tmain");
  private static final Tex tright = Resource.loadtex("gfx/hud/wnd/tright");
  public static final BufferedImage[] cbtni =
      new BufferedImage[] {
        Resource.loadimg("gfx/hud/wnd/cbtn"),
        Resource.loadimg("gfx/hud/wnd/cbtnd"),
        Resource.loadimg("gfx/hud/wnd/cbtnh")
      };
  public static final BufferedImage[] lbtni =
      new BufferedImage[] {
        Resource.loadimg("gfx/hud/wnd/lbtn"),
        Resource.loadimg("gfx/hud/wnd/lbtnd"),
        Resource.loadimg("gfx/hud/wnd/lbtnh")
      };
  public static final BufferedImage[] rbtni =
      new BufferedImage[] {
        Resource.loadimg("gfx/hud/wnd/rbtn"),
        Resource.loadimg("gfx/hud/wnd/rbtnd"),
        Resource.loadimg("gfx/hud/wnd/rbtnh")
      };
  public static final Color cc = new Color(248, 230, 190);
  public static final Text.Furnace cf =
      new Text.Imager(new Text.Foundry(new Font("Serif", Font.BOLD, 15), cc).aa(true)) {
        protected BufferedImage proc(Text text) {
          return (rasterimg(blurmask2(text.img.getRaster(), 1, 1, Color.BLACK)));
        }
      };
  public static final IBox fbox =
      new IBox("gfx/hud", "ftl", "ftr", "fbl", "fbr", "fl", "fr", "ft", "fb");
  public static final IBox swbox =
      new IBox("gfx/hud", "stl", "str", "sbl", "sbr", "sl", "sr", "st", "sb");
  public static final IBox wbox =
      new IBox("gfx/hud/wnd", "tl", "tr", "bl", "br", "vl", "vr", "ht", "hb");
  private static final IBox topless =
      new IBox(Tex.empty, Tex.empty, wbox.cbl, wbox.cbr, wbox.bl, wbox.br, Tex.empty, wbox.bb);
  private static final int th = tleft.sz().y, tdh = th - tmain.sz().y, tc = tdh + 18;
  private static final Coord capc = new Coord(20, th - 3);
  public Coord mrgn = new Coord(5, 5);
  protected Text cap;
  private boolean dt = false;
  protected boolean dm = false;
  public Coord ctl, csz, atl, asz, ac;
  protected Coord doff;
  protected final IButton cbtn;
  private final Collection<Widget> twdgs = new LinkedList<Widget>();

  // ******************************
  private static final String OPT_POS = "_pos";
  //    static Tex bg = Resource.loadtex("gfx/hud/bgtex");
  //    static Tex cl = Resource.loadtex("gfx/hud/cleft");
  //    static Tex cm = Resource.loadtex("gfx/hud/cmain");
  //    static Tex cr = Resource.loadtex("gfx/hud/cright");
  public Coord tlo, rbo;
  public boolean justclose = false;
  protected final String name;

  @RName("wnd")
  public static class $_ implements Factory {
    public Widget create(Coord c, Widget parent, Object[] args) {
      if (args.length < 2) return (new Window(c, (Coord) args[0], parent, null));
      else return (new Window(c, (Coord) args[0], parent, (String) args[1]));
    }
  }

  public Window(Coord c, Coord sz, Widget parent, String cap) {
    super(c, new Coord(0, 0), parent);
    if (cap != null) {
      this.cap = cf.render(cap);
      name = cap;
    } else {
      this.cap = null;
      name = null;
    }
    resize(sz);
    setfocustab(true);
    parent.setfocus(this);
    cbtn = new IButton(Coord.z, this, cbtni[0], cbtni[1], cbtni[2]);
    cbtn.recthit = true;
    addtwdg(cbtn);
    loadOpts();
  }

  public Coord contentsz() {
    Coord max = new Coord(0, 0);
    for (Widget wdg = child; wdg != null; wdg = wdg.next) {
      if (twdgs.contains(wdg)) continue;
      if (!wdg.visible) continue;
      Coord br = wdg.c.add(wdg.sz);
      if (br.x > max.x) max.x = br.x;
      if (br.y > max.y) max.y = br.y;
    }
    return (max.sub(1, 1));
  }

  protected void placetwdgs() {
    int x = sz.x - 5;
    for (Widget ch : twdgs) ch.c = xlate(new Coord(x -= ch.sz.x + 5, tc - (ch.sz.y / 2)), false);
  }

  public void addtwdg(Widget wdg) {
    twdgs.add(wdg);
    placetwdgs();
  }

  public void resize(Coord sz) {
    IBox box;
    int th;
    if (cap == null) {
      box = wbox;
      th = 0;
    } else {
      box = topless;
      th = Window.th;
    }
    sz = sz.add(box.bisz()).add(0, th).add(mrgn.mul(2));
    this.sz = sz;
    ctl = box.btloff().add(0, th);
    csz = sz.sub(box.bisz()).sub(0, th);
    atl = ctl.add(mrgn);
    asz = csz.sub(mrgn.mul(2));
    ac = new Coord();
    // ac = tlo.add(wbox.btloff()).add(mrgn);
    placetwdgs();
    for (Widget ch = child; ch != null; ch = ch.next) ch.presize();
  }

  public Coord xlate(Coord c, boolean in) {
    if (in) return (c.add(atl));
    else return (c.sub(atl));
  }

  public void cdraw(GOut g) {}

  public void draw(GOut g) {
    g.chcolor(0, 0, 0, 160);
    if (ctl == null || csz == null) {
      return;
    }
    g.frect(ctl, csz);
    g.chcolor();
    cdraw(g.reclip(xlate(Coord.z, true), asz));
    if (cap != null) {
      topless.draw(g, new Coord(0, th), sz.sub(0, th));
      g.image(tleft, Coord.z);
      Coord tmul = new Coord(tleft.sz().x, tdh);
      Coord tmbr = new Coord(sz.x - tright.sz().x, th);
      for (int x = tmul.x; x < tmbr.x; x += tmain.sz().x) {
        g.image(tmain, new Coord(x, tdh), tmul, tmbr);
      }
      g.image(tright, new Coord(sz.x - tright.sz().x, tdh));
      g.image(cap.tex(), capc.sub(0, cap.sz().y));
    } else {
      wbox.draw(g, Coord.z, sz);
    }
    /*
    if(cap != null) {
        GOut cg = og.reclip(new Coord(0, -7), sz.add(0, 7));
        int w = cap.tex().sz().x;
        cg.image(cl, new Coord((sz.x / 2) - (w / 2) - cl.sz().x, 0));
        cg.image(cm, new Coord((sz.x / 2) - (w / 2), 0), new Coord(w, cm.sz().y));
        cg.image(cr, new Coord((sz.x / 2) + (w / 2), 0));
        cg.image(cap.tex(), new Coord((sz.x / 2) - (w / 2), 0));
    }
    */
    super.draw(g);
  }

  public void uimsg(String msg, Object... args) {
    if (msg == "pack") {
      pack();
    } else if (msg == "dt") {
      dt = (Integer) args[0] != 0;
    } else {
      super.uimsg(msg, args);
    }
  }

  public boolean mousedown(Coord c, int button) {
    parent.setfocus(this);
    raise();
    if (super.mousedown(c, button)) return (true);
    if (c.y < tdh && cap != null) return (false);
    if (button == 1) {
      ui.grabmouse(this);
      dm = true;
      doff = c;
    }
    return (true);
  }

  public boolean mouseup(Coord c, int button) {
    if (dm) {
      canceldm();
      storeOpt(OPT_POS, this.c);
    } else {
      super.mouseup(c, button);
    }
    return (true);
  }

  public void canceldm() {
    if (dm) ui.grabmouse(null);
    dm = false;
  }

  public void mousemove(Coord c) {
    if (dm) {
      this.c = this.c.add(c.add(doff.inv()));
    } else {
      super.mousemove(c);
    }
  }

  public void wdgmsg(Widget sender, String msg, Object... args) {
    if (sender == cbtn) {
      if (justclose) ui.destroy(this);
      else wdgmsg("close");
    } else {
      super.wdgmsg(sender, msg, args);
    }
  }

  public boolean type(char key, java.awt.event.KeyEvent ev) {
    if (super.type(key, ev)) return (true);
    if (key == 27) {
      if (justclose) ui.destroy(this);
      else wdgmsg("close");
      return (true);
    }
    return (false);
  }

  public boolean drop(Coord cc, Coord ul) {
    if (dt) {
      wdgmsg("drop", cc);
      return (true);
    }
    return (false);
  }

  public boolean iteminteract(Coord cc, Coord ul) {
    return (false);
  }

  public Object tooltip(Coord c, Widget prev) {
    Object ret = super.tooltip(c, prev);
    if (ret != null) return (ret);
    else return ("");
  }

  protected void storeOpt(String opt, String value) {
    if (name == null) {
      return;
    }
    Config.setWindowOpt(name + opt, value);
  }

  protected void storeOpt(String opt, Coord value) {
    storeOpt(opt, value.toString());
  }

  protected void storeOpt(String opt, boolean value) {
    if (name == null) {
      return;
    }
    Config.setWindowOpt(name + opt, value);
  }

  protected Coord getOptCoord(String opt, Coord def) {
    synchronized (Config.window_props) {
      try {
        return new Coord(Config.window_props.getProperty(name + opt, def.toString()));
      } catch (Exception e) {
        return def;
      }
    }
  }

  protected boolean getOptBool(String opt, boolean def) {
    synchronized (Config.window_props) {
      try {
        return Config.window_props.getProperty(name + opt, null).equals("true");
      } catch (Exception e) {
        return def;
      }
    }
  }

  protected void loadOpts() {
    if (name == null) {
      return;
    }
    c = getOptCoord(OPT_POS, c);
  }
}