private IntMap<Savable> intSavableMapFromKV(int[] keys, Savable[] values) {
    if (keys == null || values == null) {
      return null;
    }

    IntMap<Savable> map = new IntMap<Savable>(keys.length);
    for (int x = 0; x < keys.length; x++) map.put(keys[x], values[x]);

    return map;
  }
Beispiel #2
0
  private void invokeAnalogsAndActions(int hash, float value, boolean applyTpf) {
    if (value < axisDeadZone) {
      invokeAnalogs(hash, value, !applyTpf);
      return;
    }

    ArrayList<Mapping> maps = bindings.get(hash);
    if (maps == null) {
      return;
    }

    boolean valueChanged = !axisValues.containsKey(hash);
    if (applyTpf) {
      value *= frameTPF;
    }

    int size = maps.size();
    for (int i = size - 1; i >= 0; i--) {
      Mapping mapping = maps.get(i);
      ArrayList<InputListener> listeners = mapping.listeners;
      int listenerSize = listeners.size();
      for (int j = listenerSize - 1; j >= 0; j--) {
        InputListener listener = listeners.get(j);

        if (listener instanceof ActionListener && valueChanged) {
          ((ActionListener) listener).onAction(mapping.name, true, frameTPF);
        }

        if (listener instanceof AnalogListener) {
          ((AnalogListener) listener).onAnalog(mapping.name, value, frameTPF);
        }
      }
    }
  }
  public void exposeObject(String name, Object obj) throws IOException {
    // Create a local object
    LocalObject localObj = new LocalObject();
    localObj.objectName = name;
    localObj.objectId = objectIdCounter++;
    localObj.theObject = obj;
    // localObj.methods   = obj.getClass().getMethods();

    ArrayList<Method> methodList = new ArrayList<Method>();
    for (Method method : obj.getClass().getMethods()) {
      if (method.getDeclaringClass() == obj.getClass()) {
        methodList.add(method);
      }
    }
    localObj.methods = methodList.toArray(new Method[methodList.size()]);

    // Put it in the store
    localObjects.put(localObj.objectId, localObj);

    // Inform the others of its existence
    RemoteObjectDefMessage defMsg = new RemoteObjectDefMessage();
    defMsg.objects = new ObjectDef[] {makeObjectDef(localObj)};

    if (client != null) {
      client.send(defMsg);
      logger.log(Level.FINE, "Client: Sending {0}", defMsg);
    } else {
      server.broadcast(defMsg);
      logger.log(Level.FINE, "Server: Sending {0}", defMsg);
    }
  }
  Object invokeRemoteMethod(RemoteObject remoteObj, Method method, Object[] args) {
    Integer methodIdInt = remoteObj.methodMap.get(method);
    if (methodIdInt == null)
      throw new RuntimeException("Method not implemented by remote object owner: " + method);

    boolean needReturn = method.getReturnType() != void.class;
    short objectId = remoteObj.objectId;
    short methodId = methodIdInt.shortValue();
    RemoteMethodCallMessage call = new RemoteMethodCallMessage();
    call.methodId = methodId;
    call.objectId = objectId;
    call.args = args;

    Invocation invoke = null;
    if (needReturn) {
      call.invocationId = invocationIdCounter++;
      invoke = new Invocation();
      // Note: could cause threading issues if used from multiple threads
      pendingInvocations.put(call.invocationId, invoke);
    }

    if (server != null) {
      remoteObj.client.send(call);
      logger.log(Level.FINE, "Server: Sending {0}", call);
    } else {
      client.send(call);
      logger.log(Level.FINE, "Client: Sending {0}", call);
    }

    if (invoke != null) {
      synchronized (invoke) {
        while (!invoke.available) {
          try {
            invoke.wait();
          } catch (InterruptedException ex) {
            ex.printStackTrace();
          }
        }
      }
      // Note: could cause threading issues if used from multiple threads
      pendingInvocations.remove(call.invocationId);
      return invoke.retVal;
    } else {
      return null;
    }
  }
Beispiel #5
0
  private void onJoyAxisEventQueued(JoyAxisEvent evt) {
    //        for (int i = 0; i < rawListeners.size(); i++){
    //            rawListeners.get(i).onJoyAxisEvent(evt);
    //        }

    int joyId = evt.getJoyIndex();
    int axis = evt.getAxisIndex();
    float value = evt.getValue();
    if (value < axisDeadZone && value > -axisDeadZone) {
      int hash1 = JoyAxisTrigger.joyAxisHash(joyId, axis, true);
      int hash2 = JoyAxisTrigger.joyAxisHash(joyId, axis, false);

      Float val1 = axisValues.get(hash1);
      Float val2 = axisValues.get(hash2);

      if (val1 != null && val1.floatValue() > axisDeadZone) {
        invokeActions(hash1, false);
      }
      if (val2 != null && val2.floatValue() > axisDeadZone) {
        invokeActions(hash2, false);
      }

      axisValues.remove(hash1);
      axisValues.remove(hash2);

    } else if (value < 0) {
      int hash = JoyAxisTrigger.joyAxisHash(joyId, axis, true);
      int otherHash = JoyAxisTrigger.joyAxisHash(joyId, axis, false);

      // Clear the reverse direction's actions in case we
      // crossed center too quickly
      Float otherVal = axisValues.get(otherHash);
      if (otherVal != null && otherVal.floatValue() > axisDeadZone) {
        invokeActions(otherHash, false);
      }

      invokeAnalogsAndActions(hash, -value, true);
      axisValues.put(hash, -value);
      axisValues.remove(otherHash);
    } else {
      int hash = JoyAxisTrigger.joyAxisHash(joyId, axis, false);
      int otherHash = JoyAxisTrigger.joyAxisHash(joyId, axis, true);

      // Clear the reverse direction's actions in case we
      // crossed center too quickly
      Float otherVal = axisValues.get(otherHash);
      if (otherVal != null && otherVal.floatValue() > axisDeadZone) {
        invokeActions(otherHash, false);
      }

      invokeAnalogsAndActions(hash, value, true);
      axisValues.put(hash, value);
      axisValues.remove(otherHash);
    }
  }
  private void onConnection(HostedConnection conn) {
    if (localObjects.size() > 0) {
      // send a object definition message
      ObjectDef[] defs = new ObjectDef[localObjects.size()];
      int i = 0;
      for (Entry<LocalObject> entry : localObjects) {
        defs[i] = makeObjectDef(entry.getValue());
        i++;
      }

      RemoteObjectDefMessage defMsg = new RemoteObjectDefMessage();
      defMsg.objects = defs;
      if (this.client != null) {
        this.client.send(defMsg);
        logger.log(Level.FINE, "Client: Sending {0}", defMsg);
      } else {
        conn.send(defMsg);
        logger.log(Level.FINE, "Server: Sending {0}", defMsg);
      }
    }
  }
Beispiel #7
0
  private void invokeTimedActions(int hash, long time, boolean pressed) {
    if (!bindings.containsKey(hash)) {
      return;
    }

    if (pressed) {
      pressedButtons.put(hash, time);
    } else {
      Long pressTimeObj = pressedButtons.remove(hash);
      if (pressTimeObj == null) {
        return; // under certain circumstances it can be null, ignore
      } // the event then.

      long pressTime = pressTimeObj;
      long lastUpdate = lastLastUpdateTime;
      long releaseTime = time;
      long timeDelta = releaseTime - Math.max(pressTime, lastUpdate);

      if (timeDelta > 0) {
        invokeAnalogs(hash, computeAnalogValue(timeDelta), false);
      }
    }
  }
Beispiel #8
0
 /** Do not use. Called to reset pressed keys or buttons when focus is restored. */
 public void reset() {
   pressedButtons.clear();
   axisValues.clear();
 }
  private void onMessage(HostedConnection source, Message message) {
    // Might want to do more strict validation of the data
    // in the message to prevent crashes

    if (message instanceof RemoteObjectDefMessage) {
      RemoteObjectDefMessage defMsg = (RemoteObjectDefMessage) message;

      ObjectDef[] defs = defMsg.objects;
      for (ObjectDef def : defs) {
        RemoteObject remoteObject = new RemoteObject(this, source);
        remoteObject.objectId = (short) def.objectId;
        remoteObject.methodDefs = def.methodDefs;
        remoteObjects.put(def.objectName, remoteObject);
        remoteObjectsById.put(def.objectId, remoteObject);
      }

      synchronized (receiveObjectLock) {
        receiveObjectLock.notifyAll();
      }
    } else if (message instanceof RemoteMethodCallMessage) {
      RemoteMethodCallMessage call = (RemoteMethodCallMessage) message;
      LocalObject localObj = localObjects.get(call.objectId);
      if (localObj == null) return;

      if (call.methodId < 0 || call.methodId >= localObj.methods.length) return;

      Object obj = localObj.theObject;
      Method method = localObj.methods[call.methodId];
      Object[] args = call.args;
      Object ret = null;
      try {
        ret = method.invoke(obj, args);
      } catch (IllegalAccessException ex) {
        logger.log(Level.WARNING, "RMI: Error accessing method", ex);
      } catch (IllegalArgumentException ex) {
        logger.log(Level.WARNING, "RMI: Invalid arguments", ex);
      } catch (InvocationTargetException ex) {
        logger.log(Level.WARNING, "RMI: Invocation exception", ex);
      }

      if (method.getReturnType() != void.class) {
        // send return value back
        RemoteMethodReturnMessage retMsg = new RemoteMethodReturnMessage();
        retMsg.invocationID = call.invocationId;
        retMsg.retVal = ret;
        if (server != null) {
          source.send(retMsg);
          logger.log(Level.FINE, "Server: Sending {0}", retMsg);
        } else {
          client.send(retMsg);
          logger.log(Level.FINE, "Client: Sending {0}", retMsg);
        }
      }
    } else if (message instanceof RemoteMethodReturnMessage) {
      RemoteMethodReturnMessage retMsg = (RemoteMethodReturnMessage) message;
      Invocation invoke = pendingInvocations.get(retMsg.invocationID);
      if (invoke == null) {
        logger.log(Level.WARNING, "Cannot find invocation ID: {0}", retMsg.invocationID);
        return;
      }

      synchronized (invoke) {
        invoke.retVal = retMsg.retVal;
        invoke.available = true;
        invoke.notifyAll();
      }
    }
  }