Exemplo n.º 1
0
 void dispatch() {
   synchronized (events) {
     if (mousemv != null) {
       mousepos = new Coord(mousemv.getX(), mousemv.getY());
       ui.mousemove(mousemv, mousepos);
       mousemv = null;
     }
     InputEvent e = null;
     while ((e = events.poll()) != null) {
       if (e instanceof MouseEvent) {
         MouseEvent me = (MouseEvent) e;
         if (me.getID() == MouseEvent.MOUSE_PRESSED) {
           ui.mousedown(me, new Coord(me.getX(), me.getY()), me.getButton());
         } else if (me.getID() == MouseEvent.MOUSE_RELEASED) {
           ui.mouseup(me, new Coord(me.getX(), me.getY()), me.getButton());
         } else if (me instanceof MouseWheelEvent) {
           ui.mousewheel(
               me, new Coord(me.getX(), me.getY()), ((MouseWheelEvent) me).getWheelRotation());
         }
       } else if (e instanceof KeyEvent) {
         KeyEvent ke = (KeyEvent) e;
         if (ke.getID() == KeyEvent.KEY_PRESSED) {
           ui.keydown(ke);
         } else if (ke.getID() == KeyEvent.KEY_RELEASED) {
           ui.keyup(ke);
         } else if (ke.getID() == KeyEvent.KEY_TYPED) {
           ui.type(ke);
         }
       }
       ui.lastevent = System.currentTimeMillis();
     }
   }
 }
 public void uimsg(int id, String msg, Object... args) {
   Widget w = widgets.get(id);
   synchronized (robots) {
     for (Robot r : robots) r.uimsg(id, w, msg, args);
   }
   super.uimsg(id, msg, args);
 }
 public void newwidget(int id, String type, Coord c, int parent, Object... args)
     throws InterruptedException {
   super.newwidget(id, type, c, parent, args);
   Widget w = widgets.get(id);
   synchronized (robots) {
     for (Robot r : robots) r.newwdg(id, w, args);
   }
 }
 public void destroy(Widget w) {
   int id;
   if (!rwidgets.containsKey(w)) id = -1;
   else id = rwidgets.get(w);
   synchronized (robots) {
     for (Robot r : robots) r.dstwdg(id, w);
   }
   super.destroy(w);
 }
Exemplo n.º 5
0
 UI newui(Session sess) {
   if (ui != null) ui.destroy();
   ui = new UI(new Coord(w, h), sess);
   ui.root.guprof = uprof;
   ui.root.grprof = rprof;
   ui.root.ggprof = gprof;
   if (getParent() instanceof Console.Directory) ui.cons.add((Console.Directory) getParent());
   ui.cons.add(this);
   if (glconf != null) ui.cons.add(glconf);
   return (ui);
 }
Exemplo n.º 6
0
  public void run() {
    try {
      Thread drawthread = new HackThread(drawfun, "Render thread");
      drawthread.start();
      synchronized (drawfun) {
        while (state == null) drawfun.wait();
      }
      try {
        long now, then;
        long frames[] = new long[128];
        int framep = 0, waited[] = new int[128];
        while (true) {
          int fwaited = 0;
          Debug.cycle();
          UI ui = this.ui;
          then = System.currentTimeMillis();
          CPUProfile.Frame curf = null;
          if (Config.profile) curf = uprof.new Frame();
          synchronized (ui) {
            if (ui.sess != null) ui.sess.glob.ctick();
            dispatch();
            ui.tick();
            if ((ui.root.sz.x != w) || (ui.root.sz.y != h)) ui.root.resize(new Coord(w, h));
          }
          if (curf != null) curf.tick("dsp");

          BGL buf = new BGL();
          GLState.Applier state = this.state;
          rootdraw(state, ui, buf);
          if (curf != null) curf.tick("draw");
          synchronized (drawfun) {
            now = System.currentTimeMillis();
            while (bufdraw != null) drawfun.wait();
            bufdraw = new Frame(buf, state.cgl);
            drawfun.notifyAll();
            fwaited += System.currentTimeMillis() - now;
          }

          ui.audio.cycle();
          if (curf != null) curf.tick("aux");

          now = System.currentTimeMillis();
          long fd = bgmode ? this.bgfd : this.fd;
          if (now - then < fd) {
            synchronized (events) {
              events.wait(fd - (now - then));
            }
            fwaited += System.currentTimeMillis() - now;
          }

          frames[framep] = now;
          waited[framep] = fwaited;
          for (int i = 0, ckf = framep, twait = 0; i < frames.length; i++) {
            ckf = (ckf - 1 + frames.length) % frames.length;
            twait += waited[ckf];
            if (now - frames[ckf] > 1000) {
              fps = i;
              uidle = ((double) twait) / ((double) (now - frames[ckf]));
              break;
            }
          }
          framep = (framep + 1) % frames.length;

          if (curf != null) curf.tick("wait");
          if (curf != null) curf.fin();
          if (Thread.interrupted()) throw (new InterruptedException());
        }
      } finally {
        drawthread.interrupt();
        drawthread.join();
      }
    } catch (InterruptedException e) {
    } finally {
      ui.destroy();
    }
  }
Exemplo n.º 7
0
  void rootdraw(GLState.Applier state, UI ui, BGL gl) {
    GLState.Buffer ibuf = new GLState.Buffer(state.cfg);
    gstate.prep(ibuf);
    ostate.prep(ibuf);
    GOut g = new GOut(gl, state.cgl, state.cfg, state, ibuf, new Coord(w, h));
    state.set(ibuf);

    g.state(ostate);
    g.apply();
    gl.glClearColor(0, 0, 0, 1);
    gl.glClear(GL.GL_COLOR_BUFFER_BIT);
    synchronized (ui) {
      ui.draw(g);
    }

    if (Config.dbtext) {
      int y = h - 150;
      FastText.aprintf(
          g,
          new Coord(10, y -= 15),
          0,
          1,
          "FPS: %d (%d%%, %d%% idle)",
          fps,
          (int) (uidle * 100.0),
          (int) (ridle * 100.0));
      Runtime rt = Runtime.getRuntime();
      long free = rt.freeMemory(), total = rt.totalMemory();
      FastText.aprintf(
          g,
          new Coord(10, y -= 15),
          0,
          1,
          "Mem: %,011d/%,011d/%,011d/%,011d",
          free,
          total - free,
          total,
          rt.maxMemory());
      FastText.aprintf(g, new Coord(10, y -= 15), 0, 1, "Tex-current: %d", TexGL.num());
      FastText.aprintf(g, new Coord(10, y -= 15), 0, 1, "GL progs: %d", g.st.numprogs());
      GameUI gi = ui.root.findchild(GameUI.class);
      if ((gi != null) && (gi.map != null)) {
        try {
          FastText.aprintf(
              g, new Coord(10, y -= 15), 0, 1, "MV pos: %s (%s)", gi.map.getcc(), gi.map.camera);
        } catch (Loading e) {
        }
        if (gi.map.rls != null)
          FastText.aprintf(
              g,
              new Coord(10, y -= 15),
              0,
              1,
              "Rendered: %,d+%,d(%,d)",
              gi.map.rls.drawn,
              gi.map.rls.instanced,
              gi.map.rls.instancified);
      }
      if (Resource.remote().qdepth() > 0)
        FastText.aprintf(
            g,
            new Coord(10, y -= 15),
            0,
            1,
            "RQ depth: %d (%d)",
            Resource.remote().qdepth(),
            Resource.remote().numloaded());
    }
    Object tooltip;
    try {
      synchronized (ui) {
        tooltip = ui.root.tooltip(mousepos, ui.root);
      }
    } catch (Loading e) {
      tooltip = "...";
    }
    Tex tt = null;
    if (tooltip != null) {
      if (tooltip instanceof Text) {
        tt = ((Text) tooltip).tex();
      } else if (tooltip instanceof Tex) {
        tt = (Tex) tooltip;
      } else if (tooltip instanceof Indir<?>) {
        Indir<?> t = (Indir<?>) tooltip;
        Object o = t.get();
        if (o instanceof Tex) tt = (Tex) o;
      } else if (tooltip instanceof String) {
        if (((String) tooltip).length() > 0) tt = (Text.render((String) tooltip)).tex();
      }
    }
    if (tt != null) {
      Coord sz = tt.sz();
      Coord pos = mousepos.add(sz.inv());
      if (pos.x < 0) pos.x = 0;
      if (pos.y < 0) pos.y = 0;
      g.chcolor(244, 247, 21, 192);
      g.rect(pos.add(-3, -3), sz.add(6, 6));
      g.chcolor(35, 35, 35, 192);
      g.frect(pos.add(-2, -2), sz.add(4, 4));
      g.chcolor();
      g.image(tt, pos);
    }
    ui.lasttip = tooltip;
    Resource curs = ui.root.getcurs(mousepos);
    if (curs != null) {
      if (cursmode == "awt") {
        if (curs != lastcursor) {
          try {
            setCursor(makeawtcurs(curs.layer(Resource.imgc).img, curs.layer(Resource.negc).cc));
            lastcursor = curs;
          } catch (Exception e) {
            cursmode = "tex";
          }
        }
      } else if (cursmode == "tex") {
        Coord dc = mousepos.add(curs.layer(Resource.negc).cc.inv());
        g.image(curs.layer(Resource.imgc), dc);
      }
    }
    state.clean();
    GLObject.disposeall(state.cgl, gl);
  }
Exemplo n.º 8
0
 public void wdgmsg(Widget sender, String msg, Object... args) {
   if (parent == null) ui.wdgmsg(sender, msg, args);
   else parent.wdgmsg(sender, msg, args);
 }
Exemplo n.º 9
0
  public Session _dc_run(String login, String password) throws InterruptedException {
    String authserver = (Config.authserv == null) ? address : Config.authserv;
    retry:
    do {
      byte[] cookie;
      AuthClient auth = null;
      try {
        try {
          auth = new AuthClient(authserver, login);
        } catch (UnknownHostException e) {
          _dc_log("Could not locate server");
          continue retry;
        }
        if (!auth.trypasswd(password)) {
          auth.close();
          password = "";
          _dc_log("Username or password incorrect");
          continue retry;
        }
        cookie = auth.cookie;

      } catch (java.io.IOException e) {
        ui.uimsg(1, "error", e.getMessage());
        continue retry;
      } finally {
        try {
          if (auth != null) auth.close();
        } catch (java.io.IOException e) {
        }
      }
      _dc_log("Connecting...");
      try {
        sess = new Session(InetAddress.getByName(address), login, cookie);
      } catch (UnknownHostException e) {
        _dc_log("Could not locate server");
        continue retry;
      }
      Thread.sleep(100);
      while (true) {
        if (sess.state == "") {
          break retry;
        } else if (sess.connfailed != 0) {
          String error;
          switch (sess.connfailed) {
            case 1:
              error = "Invalid authentication token";
              break;
            case 2:
              error = "Already logged in";
              break;
            case 3:
              error = "Could not connect to server";
              break;
            case 4:
              error = "This client is too old";
              break;
            case 5:
              error = "Authentication token expired";
              break;
            default:
              error = "Connection failed";
              break;
          }
          _dc_log(error);
          sess = null;
          continue retry;
        }
        synchronized (sess) {
          sess.wait();
        }
      }
    } while (true);
    return sess;
  }
Exemplo n.º 10
0
 public Session run(HavenPanel hp) throws InterruptedException {
   ui = hp.newui(null);
   ui.setreceiver(this);
   ui.bind(new LoginScreen(ui.root), 1);
   String username;
   boolean savepw = false;
   Utils.setpref("password", "");
   byte[] token = null;
   if (Utils.getpref("savedtoken", "").length() == 64)
     token = Utils.hex2byte(Utils.getpref("savedtoken", null));
   username = Utils.getpref("username", "");
   String authserver = (Config.authserv == null) ? address : Config.authserv;
   retry:
   do {
     byte[] cookie;
     if (initcookie != null) {
       username = inituser;
       cookie = initcookie;
       initcookie = null;
     } else if (token != null) {
       savepw = true;
       ui.uimsg(1, "token", username);
       while (true) {
         Message msg;
         synchronized (msgs) {
           while ((msg = msgs.poll()) == null) msgs.wait();
         }
         if (msg.id == 1) {
           if (msg.name == "login") {
             break;
           } else if (msg.name == "forget") {
             token = null;
             Utils.setpref("savedtoken", "");
             continue retry;
           }
         }
       }
       ui.uimsg(1, "prg", "Authenticating...");
       AuthClient auth = null;
       try {
         auth = new AuthClient(authserver, username);
         if (!auth.trytoken(token)) {
           auth.close();
           token = null;
           Utils.setpref("savedtoken", "");
           ui.uimsg(1, "error", "Invalid save");
           continue retry;
         }
         cookie = auth.cookie;
       } catch (java.io.IOException e) {
         ui.uimsg(1, "error", e.getMessage());
         continue retry;
       } finally {
         try {
           if (auth != null) auth.close();
         } catch (java.io.IOException e) {
         }
       }
     } else {
       String password;
       ui.uimsg(1, "passwd", username, savepw);
       while (true) {
         Message msg;
         synchronized (msgs) {
           while ((msg = msgs.poll()) == null) msgs.wait();
         }
         if (msg.id == 1) {
           if (msg.name == "login") {
             username = (String) msg.args[0];
             password = (String) msg.args[1];
             savepw = (Boolean) msg.args[2];
             break;
           }
         }
       }
       ui.uimsg(1, "prg", "Authenticating...");
       AuthClient auth = null;
       try {
         try {
           auth = new AuthClient(authserver, username);
         } catch (UnknownHostException e) {
           ui.uimsg(1, "error", "Could not locate server");
           continue retry;
         }
         if (!auth.trypasswd(password)) {
           auth.close();
           password = "";
           ui.uimsg(1, "error", "Username or password incorrect");
           continue retry;
         }
         cookie = auth.cookie;
         if (savepw) {
           if (auth.gettoken()) Utils.setpref("savedtoken", Utils.byte2hex(auth.token));
         }
       } catch (java.io.IOException e) {
         ui.uimsg(1, "error", e.getMessage());
         continue retry;
       } finally {
         try {
           if (auth != null) auth.close();
         } catch (java.io.IOException e) {
         }
       }
     }
     ui.uimsg(1, "prg", "Connecting...");
     try {
       sess = new Session(InetAddress.getByName(address), username, cookie);
     } catch (UnknownHostException e) {
       ui.uimsg(1, "error", "Could not locate server");
       continue retry;
     }
     Thread.sleep(100);
     while (true) {
       if (sess.state == "") {
         Utils.setpref("username", username);
         ui.destroy(1);
         break retry;
       } else if (sess.connfailed != 0) {
         String error;
         switch (sess.connfailed) {
           case 1:
             error = "Invalid authentication token";
             break;
           case 2:
             error = "Already logged in";
             break;
           case 3:
             error = "Could not connect to server";
             break;
           case 4:
             error = "This client is too old";
             break;
           case 5:
             error = "Authentication token expired";
             break;
           default:
             error = "Connection failed";
             break;
         }
         ui.uimsg(1, "error", error);
         sess = null;
         continue retry;
       }
       synchronized (sess) {
         sess.wait();
       }
     }
   } while (true);
   haven.error.ErrorHandler.setprop("usr", sess.username);
   return (sess);
   // (new RemoteUI(sess, ui)).start();
 }
  void redraw(GL gl) {
    GOut g = new GOut(gl, getContext(), MainFrame.getInnerSize());
    synchronized (ui) {
      ui.update();
    }
    if (Config.render_enable) {
      gl.glMatrixMode(GL.GL_PROJECTION);
      gl.glLoadIdentity();
      gl.glOrtho(0, getWidth(), 0, getHeight(), -1, 1);
      TexRT.renderall(g);
      if (curf != null) curf.tick("texrt");

      gl.glMatrixMode(GL.GL_PROJECTION);
      gl.glLoadIdentity();
      gl.glOrtho(0, getWidth(), getHeight(), 0, -1, 1);
      gl.glClearColor(0, 0, 0, 1);
      gl.glClear(GL.GL_COLOR_BUFFER_BIT);
      if (curf != null) curf.tick("cls");
      synchronized (ui) {
        ui.draw(g);
      }
      if (curf != null) curf.tick("draw");

      if (Config.dbtext) {
        g.atext("FPS: " + fps, new Coord(10, 545), 0, 1);
        g.atext("Texhit: " + dth, new Coord(10, 530), 0, 1);
        g.atext("Texmiss: " + dtm, new Coord(10, 515), 0, 1);
        Runtime rt = Runtime.getRuntime();
        long free = rt.freeMemory(), total = rt.totalMemory();
        g.atext(
            String.format(
                "Mem: %,011d/%,011d/%,011d/%,011d", free, total - free, total, rt.maxMemory()),
            new Coord(10, 500),
            0,
            1);
        g.atext(
            String.format("LCache: %d/%d", Layered.cache.size(), Layered.cache.cached()),
            new Coord(10, 485),
            0,
            1);
        g.atext(
            String.format("RT-current: %d", TexRT.current.get(gl).size()),
            new Coord(10, 470),
            0,
            1);
        if (Resource.qdepth() > 0)
          g.atext(
              String.format("RQ depth: %d (%d)", Resource.qdepth(), Resource.numloaded()),
              new Coord(10, 455),
              0,
              1);
      }
      Object tooltip = ui.root.tooltip(mousepos, true);
      Tex tt = null;
      if (tooltip != null) {
        if (tooltip instanceof Text) {
          tt = ((Text) tooltip).tex();
        } else if (tooltip instanceof Tex) {
          tt = (Tex) tooltip;
        } else if (tooltip instanceof String) {
          if (((String) tooltip).length() > 0) tt = (Text.render((String) tooltip)).tex();
        }
      }
      if (tt != null) {
        Coord sz = tt.sz();
        Coord pos = mousepos.add(sz.inv());
        if (pos.x < 0) pos.x = 0;
        if (pos.y < 0) pos.y = 0;
        g.chcolor(244, 247, 21, 192);
        g.rect(pos.add(-3, -3), sz.add(6, 6));
        g.chcolor(35, 35, 35, 192);
        g.frect(pos.add(-2, -2), sz.add(4, 4));
        g.chcolor();
        g.image(tt, pos);
      }
    }
    Resource curs = ui.root.getcurs(mousepos);
    if (!curs.loading) {
      if (cursmode == "awt") {
        if (curs != lastcursor) {
          try {
            setCursor(makeawtcurs(curs.layer(Resource.imgc).img, curs.layer(Resource.negc).cc));
            ark.bot.cursor_name = curs.name;
            ark.bot.cursor_name = ark.bot.cursor_name.replace("gfx/hud/curs/", "");
            lastcursor = curs;
          } catch (Exception e) {
            cursmode = "tex";
          }
        }
      } else if (cursmode == "tex") {
        Coord dc = mousepos.add(curs.layer(Resource.negc).cc.inv());
        g.image(curs.layer(Resource.imgc), dc);
      }
    }
  }
 UI newui(Session sess) {
   ui = new UI(new Coord(w, h), sess);
   ui.root.gprof = prof;
   ui.fsm = this.fsm;
   return (ui);
 }
 public void setfsm(FSMan fsm) {
   this.fsm = new SyncFSM(fsm);
   ui.fsm = this.fsm;
 }