1 /******************************************************************************* 2 * Copyright 2011 See AUTHORS file. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 ******************************************************************************/ 16 17 package com.badlogic.gdx.scenes.scene2d.utils; 18 19 import com.badlogic.gdx.scenes.scene2d.Actor; 20 import com.badlogic.gdx.scenes.scene2d.Event; 21 import com.badlogic.gdx.scenes.scene2d.EventListener; 22 23 /** Listener for {@link FocusEvent}. 24 * @author Nathan Sweet */ 25 abstract public class FocusListener implements EventListener { handle(Event event)26 public boolean handle (Event event) { 27 if (!(event instanceof FocusEvent)) return false; 28 FocusEvent focusEvent = (FocusEvent)event; 29 switch (focusEvent.getType()) { 30 case keyboard: 31 keyboardFocusChanged(focusEvent, event.getTarget(), focusEvent.isFocused()); 32 break; 33 case scroll: 34 scrollFocusChanged(focusEvent, event.getTarget(), focusEvent.isFocused()); 35 break; 36 } 37 return false; 38 } 39 40 /** @param actor The event target, which is the actor that emitted the focus event. */ keyboardFocusChanged(FocusEvent event, Actor actor, boolean focused)41 public void keyboardFocusChanged (FocusEvent event, Actor actor, boolean focused) { 42 } 43 44 /** @param actor The event target, which is the actor that emitted the focus event. */ scrollFocusChanged(FocusEvent event, Actor actor, boolean focused)45 public void scrollFocusChanged (FocusEvent event, Actor actor, boolean focused) { 46 } 47 48 /** Fired when an actor gains or loses keyboard or scroll focus. Can be cancelled to prevent losing or gaining focus. 49 * @author Nathan Sweet */ 50 static public class FocusEvent extends Event { 51 private boolean focused; 52 private Type type; 53 private Actor relatedActor; 54 reset()55 public void reset () { 56 super.reset(); 57 relatedActor = null; 58 } 59 isFocused()60 public boolean isFocused () { 61 return focused; 62 } 63 setFocused(boolean focused)64 public void setFocused (boolean focused) { 65 this.focused = focused; 66 } 67 getType()68 public Type getType () { 69 return type; 70 } 71 setType(Type focusType)72 public void setType (Type focusType) { 73 this.type = focusType; 74 } 75 76 /** The actor related to the event. When focus is lost, this is the new actor being focused, or null. When focus is gained, 77 * this is the previous actor that was focused, or null. */ getRelatedActor()78 public Actor getRelatedActor () { 79 return relatedActor; 80 } 81 82 /** @param relatedActor May be null. */ setRelatedActor(Actor relatedActor)83 public void setRelatedActor (Actor relatedActor) { 84 this.relatedActor = relatedActor; 85 } 86 87 /** @author Nathan Sweet */ 88 static public enum Type { 89 keyboard, scroll 90 } 91 } 92 } 93