⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 inputhandler.java

📁 java 3d game jme 工程开发源代码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
                device.createTriggers( action, axis, button, allowRepeats, this );
            }
        }
        else {
            InputHandlerDevice device = devicesMap.get( deviceName );
            if ( device != null ) {
                device.createTriggers( action, axis, button, allowRepeats, this );
            }
            else {
                throw new UnsupportedOperationException( "Device '" + deviceName + "' is unknown!" );
            }
        }
    }

    /**
     * Removes a keyboard input action from the list of keyActions that are
     * polled during update.
     *
     * @param inputAction The action to remove.
     */
    public void removeAction( InputActionInterface inputAction ) {
        synchronized ( this ) {
            for ( int i = allTriggers.size() - 1; i >= 0; i-- ) {
                ActionTrigger trigger = allTriggers.get( i );
                if ( trigger.action == inputAction ) {
                    trigger.remove();
                    //go on, action could be in more triggers
                }
            }
        }
    }

    /**
     * Removes a keyboard input action from the list of keyActions that are
     * polled during update.
     *
     * @param triggerCommand the command that should be removed
     */
    public void removeAction( String triggerCommand ) {
        synchronized ( this ) {
            KeyBindingManager.getKeyBindingManager().remove(triggerCommand);
            for ( int i = allTriggers.size() - 1; i >= 0; i-- ) {
                ActionTrigger trigger = allTriggers.get( i );
                if ( trigger.name.equals(triggerCommand) ) {
                    trigger.remove();
                    //go on, action could be in more triggers
                }
            }
        }
    }

    /**
     * Removes all actions and triggers from this handler. Commonly used to get the handler and the actions
     * garbage collected.
     */
    public void removeAllActions() {
        synchronized ( this ) {
            for ( int i = allTriggers.size() - 1; i >= 0; i-- ) {
                ActionTrigger trigger = allTriggers.get( i );
                trigger.remove();
            }
        }
    }

    /**
     * Clears all actions currently registered.
     */
    public void clearActions() {
        synchronized ( this ) {
            for ( int i = allTriggers.size() - 1; i >= 0; i-- ) {
                ActionTrigger trigger = allTriggers.get( i );
                trigger.remove();
            }
        }
    }

    /**
     * Checks all actions to see if they should be invoked. If
     * so, {@link InputActionInterface#performAction(InputActionEvent)} is called on the action with the given time.
     * <br>
     * This method can be invoked while the handler is disabled. Thus the method should
     * check {@link #isEnabled()} and return immediately if it evaluates to false.
     * <br>
     * This method should normally not be overwritten by subclasses. If an InputHandler needs to
     * execute something in each update register an action with triggerCommand = null. Exception to this
     * is an InputHandler that checks additional input types, that cannot be handled via {@link InputHandlerDevice}s.
     *
     * @param time The time to pass to every action that is active.
     * @see #addAction(com.jme.input.action.InputActionInterface,String,boolean)
     */
    public void update( float time ) {
        if ( !isEnabled() ) {
            return;
        }
        processTriggers( time );
        updateAttachedHandlers( time );
        //TODO: provide a list of events that occur this frame?
    }

    /**
     * Process triggers and call {@link ActionTrigger#performAction} if appropriate.
     * @param time The time to pass to every trigger that is called.
     */
    protected void processTriggers( float time ) {
        event.setTime( time );
        synchronized ( this ) {
            for ( ActionTrigger trigger = activeTriggers; trigger != null; ) {
                ActionTrigger nextTrigger = trigger.getNext(); //perform action might deactivate the action
                // -> getNext() would return null then
                trigger.performAction( event );
                trigger = nextTrigger;
            }
        }
    }

    /**
     * Update attached handlers.
     * @param time The time to pass to every action that is active.
     */
    protected void updateAttachedHandlers( float time ) {
        for ( int i = this.sizeOfAttachedHandlers() - 1; i >= 0; i-- ) {
            InputHandler handler = this.getFromAttachedHandlers( i );
            if ( handler.isEnabled() ) {
                handler.update( time );
            }
        }
    }


    public static float getFloatProp( HashMap<String, Object> props, String key, float defaultVal ) {
        if ( props == null || props.get( key ) == null ) {
            return defaultVal;
        }
        
        return Float.parseFloat( props.get( key ).toString() );        
    }

    public static int getIntProp( HashMap<String, Object> props, String key, int defaultVal ) {
        if ( props == null || props.get( key ) == null ) {
            return defaultVal;
        }
        
        return Integer.parseInt( props.get( key ).toString() );        
    }

    public static boolean getBooleanProp( HashMap<String, Object> props, String key, boolean defaultVal ) {
        if ( props == null || props.get( key ) == null ) {
            return defaultVal;
        }
        
        return "true".equalsIgnoreCase( props.get( key ).toString() );        
    }

    public static Object getObjectProp( HashMap<String, Object> props, String key, Object defaultVal ) {
        if ( props == null || props.get( key ) == null ) {
            return defaultVal;
        }
        
        return props.get( key );
    }


    /**
     * @return true if this handler is currently enabled
     */
    public boolean isEnabled() {
        return this.enabled;
    }

    /**
     * store the value for field enabled
     */
    private boolean enabled = true;

    /**
     * Enable/disable the handler: disabled handler do not invoke actions and do not update attached handlers.
     *
     * @param value true to enable the handler, false to disable the handler
     */
    public synchronized void setEnabled( final boolean value ) {
        final boolean oldValue = this.enabled;
        if ( oldValue != value ) {
            this.enabled = value;
            // todo: we could decide to have one device per handler to reduce amount of triggers that are checked on
            // each event, devices would then be notified here about disabling the handler
        }
    }

    /**
     * enabled/disables all attached handlers but this handler keeps its status.
     *
     * @param enabled true to enable all attached handlers, false to disable them
     */
    public void setEnabledOfAttachedHandlers( boolean enabled ) {
        for ( int i = this.sizeOfAttachedHandlers() - 1; i >= 0; i-- ) {
            InputHandler handler = this.getFromAttachedHandlers( i );
            handler.setEnabled( enabled );
        }
    }

    /**
     * List of InputHandlers
     */
    private ArrayList <InputHandler>attachedHandlers;

    /**
     * Attach a handler which should be updated in this handlers update method.
     *
     * @param value handler to attach
     * @return true if handler was not attached before
     */
    public boolean addToAttachedHandlers( InputHandler value ) {
        return value != null && value.setParent( this );
    }

    /**
     * Attach a handler which should be updated in this handlers update method.
     *
     * @param value handler to attach
     * @return true if handler was not attached before
     */
    private boolean addToAttachedHandlers_internal( InputHandler value ) {
        boolean changed = false;
        if ( value != null ) {
            if ( this.attachedHandlers == null ) {
                this.attachedHandlers = new ArrayList<InputHandler>();
            }
            else if ( this.attachedHandlers.contains( value ) ) {
                return false;
            }
            changed = this.attachedHandlers.add( value );
            if ( changed ) {
                value.setParent( this );
            }
        }
        return changed;
    }

    /**
     * Get an element from the attachedHandlers association.
     *
     * @param index index of element to be retrieved
     * @return the element, null if index out of range
     */
    public InputHandler getFromAttachedHandlers( int index ) {
        if ( attachedHandlers != null && index >= 0 && index < attachedHandlers.size() ) {
            return attachedHandlers.get( index );
        }
        
        return null;       
    }

    public void removeAllFromAttachedHandlers() {
        for ( int i = this.sizeOfAttachedHandlers() - 1; i >= 0; i-- ) {
            InputHandler handler = this.getFromAttachedHandlers( i );
            this.removeFromAttachedHandlers( handler );
        }
    }

    public boolean removeFromAttachedHandlers( InputHandler value ) {
        boolean changed = false;
        if ( ( this.attachedHandlers != null ) && ( value != null ) ) {
            changed = this.attachedHandlers.remove( value );
            if ( changed ) {
                value.setParent( null );
            }
        }
        return changed;
    }

    /**
     * @return number of attached handlers
     */
    public int sizeOfAttachedHandlers() {
        return ( ( this.attachedHandlers == null )
                ? 0
                : this.attachedHandlers.size() );
    }

    /**
     * store value for field parent
     */
    private InputHandler parent;

    /**
     * Query parent handler.
     *
     * @return current parent
     */
    public InputHandler getParent() {
        return this.parent;
    }

    /**
     * @param value new value for field parent
     * @return true if parent was changed
     */
    protected boolean setParent( InputHandler value ) {
        boolean changed = false;
        final InputHandler oldValue = this.parent;
        if ( oldValue != value ) {
            if ( oldValue != null ) {
                this.parent = null;
                oldValue.removeFromAttachedHandlers( this );
            }
            this.parent = value;
            if ( value != null ) {
                value.addToAttachedHandlers_internal( this );
            }
            changed = true;
        }
        return changed;
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -