private Actor fireEnterAndExit(Actor overLast, int screenX, int screenY, int pointer) { // Find the actor under the point. screenToStageCoordinates(tempCoords.set(screenX, screenY)); Actor over = hit(tempCoords.x, tempCoords.y, true); if (over == overLast) return overLast; // Exit overLast. if (overLast != null) { InputEvent event = Pools.obtain(InputEvent.class); event.setStage(this); event.setStageX(tempCoords.x); event.setStageY(tempCoords.y); event.setPointer(pointer); event.setType(InputEvent.Type.exit); event.setRelatedActor(over); overLast.fire(event); Pools.free(event); } // Enter over. if (over != null) { InputEvent event = Pools.obtain(InputEvent.class); event.setStage(this); event.setStageX(tempCoords.x); event.setStageY(tempCoords.y); event.setPointer(pointer); event.setType(InputEvent.Type.enter); event.setRelatedActor(overLast); over.fire(event); Pools.free(event); } return over; }
/** * Applies a touch up event to the stage and returns true if an actor in the scene {@link * Event#handle() handled} the event. Only {@link InputListener listeners} that returned true for * touchDown will receive this event. */ public boolean touchUp(int screenX, int screenY, int pointer, int button) { pointerTouched[pointer] = false; pointerScreenX[pointer] = screenX; pointerScreenY[pointer] = screenY; if (touchFocuses.size == 0) return false; screenToStageCoordinates(tempCoords.set(screenX, screenY)); InputEvent event = Pools.obtain(InputEvent.class); event.setType(Type.touchUp); event.setStage(this); event.setStageX(tempCoords.x); event.setStageY(tempCoords.y); event.setPointer(pointer); event.setButton(button); SnapshotArray<TouchFocus> touchFocuses = this.touchFocuses; TouchFocus[] focuses = touchFocuses.begin(); for (int i = 0, n = touchFocuses.size; i < n; i++) { TouchFocus focus = focuses[i]; if (focus.pointer != pointer || focus.button != button) continue; if (!touchFocuses.removeValue(focus, true)) continue; // Touch focus already gone. event.setTarget(focus.target); event.setListenerActor(focus.listenerActor); if (focus.listener.handle(event)) event.handle(); Pools.free(focus); } touchFocuses.end(); boolean handled = event.isHandled(); Pools.free(event); return handled; }
/** * Applies a mouse moved event to the stage and returns true if an actor in the scene {@link * Event#handle() handled} the event. This event only occurs on the desktop. */ public boolean mouseMoved(int screenX, int screenY) { if (screenX < viewport.getScreenX() || screenX >= viewport.getScreenX() + viewport.getScreenWidth()) return false; if (Gdx.graphics.getHeight() - screenY < viewport.getScreenY() || Gdx.graphics.getHeight() - screenY >= viewport.getScreenY() + viewport.getScreenHeight()) return false; mouseScreenX = screenX; mouseScreenY = screenY; screenToStageCoordinates(tempCoords.set(screenX, screenY)); InputEvent event = Pools.obtain(InputEvent.class); event.setStage(this); event.setType(Type.mouseMoved); event.setStageX(tempCoords.x); event.setStageY(tempCoords.y); Actor target = hit(tempCoords.x, tempCoords.y, true); if (target == null) target = root; target.fire(event); boolean handled = event.isHandled(); Pools.free(event); return handled; }
/** * Cancels touch focus for all listeners except the specified listener. * * @see #cancelTouchFocus() */ public void cancelTouchFocusExcept(EventListener exceptListener, Actor exceptActor) { InputEvent event = Pools.obtain(InputEvent.class); event.setStage(this); event.setType(InputEvent.Type.touchUp); event.setStageX(Integer.MIN_VALUE); event.setStageY(Integer.MIN_VALUE); // Cancel all current touch focuses except for the specified listener, allowing for concurrent // modification, and never // cancel the same focus twice. SnapshotArray<TouchFocus> touchFocuses = this.touchFocuses; TouchFocus[] items = touchFocuses.begin(); for (int i = 0, n = touchFocuses.size; i < n; i++) { TouchFocus focus = items[i]; if (focus.listener == exceptListener && focus.listenerActor == exceptActor) continue; if (!touchFocuses.removeValue(focus, true)) continue; // Touch focus already gone. event.setTarget(focus.target); event.setListenerActor(focus.listenerActor); event.setPointer(focus.pointer); event.setButton(focus.button); focus.listener.handle(event); // Cannot return TouchFocus to pool, as it may still be in use (eg if cancelTouchFocus is // called from touchDragged). } touchFocuses.end(); Pools.free(event); }
@Override public void use(LivingEntity caster) { CastingBuff buff = Pools.obtain(CastingBuff.class); buff.setSkill(childSkill); buff.setInterval(interval); SkillManager.applyBuff(buff, caster, buffDuration); }
/** * Applies a touch down event to the stage and returns true if an actor in the scene {@link * Event#handle() handled} the event. */ public boolean touchDown(int screenX, int screenY, int pointer, int button) { if (screenX < viewport.getScreenX() || screenX >= viewport.getScreenX() + viewport.getScreenWidth()) return false; if (Gdx.graphics.getHeight() - screenY < viewport.getScreenY() || Gdx.graphics.getHeight() - screenY >= viewport.getScreenY() + viewport.getScreenHeight()) return false; pointerTouched[pointer] = true; pointerScreenX[pointer] = screenX; pointerScreenY[pointer] = screenY; screenToStageCoordinates(tempCoords.set(screenX, screenY)); InputEvent event = Pools.obtain(InputEvent.class); event.setType(Type.touchDown); event.setStage(this); event.setStageX(tempCoords.x); event.setStageY(tempCoords.y); event.setPointer(pointer); event.setButton(button); Actor target = hit(tempCoords.x, tempCoords.y, true); if (target == null) { if (root.getTouchable() == Touchable.enabled) root.fire(event); } else { target.fire(event); } boolean handled = event.isHandled(); Pools.free(event); return handled; }
/** * @param oldText May be null. * @return True if the text was changed. */ boolean changeText(String oldText, String newText) { if (newText.equals(oldText)) return false; text = newText; ChangeEvent changeEvent = Pools.obtain(ChangeEvent.class); boolean cancelled = fire(changeEvent); text = cancelled ? oldText : newText; Pools.free(changeEvent); return !cancelled; }
@Override public Weapon cloneObject() { TripleDirectionWeapon weapon = Pools.obtain(TripleDirectionWeapon.class); cloneCommon(weapon); weapon.angle0 = this.angle0; weapon.angle1 = this.angle1; weapon.angle2 = this.angle2; return weapon; }
public void layoutCards() { Array<Card> cards = Pools.obtain(Array.class); for (int i = 0; i < 4; i++) { cards.clear(); for (int c : players[i].getCards()) cards.add(getCardController().getCard(c)); screen.playDealingAnimation(i, cards); } cards.clear(); Pools.free(cards); }
/** * Adds the listener to be notified for all touchDragged and touchUp events for the specified * pointer and button. The actor will be used as the {@link Event#getListenerActor() listener * actor} and {@link Event#getTarget() target}. */ public void addTouchFocus( EventListener listener, Actor listenerActor, Actor target, int pointer, int button) { TouchFocus focus = Pools.obtain(TouchFocus.class); focus.listenerActor = listenerActor; focus.target = target; focus.listener = listener; focus.pointer = pointer; focus.button = button; touchFocuses.add(focus); }
private void updateValueFromTouch(float touchX, float touchY) { int newV = (int) (touchX / BasicColorPicker.PALETTE_SIZE * maxValue / sizes.scaleFactor); int newS = (int) (touchY / BasicColorPicker.PALETTE_SIZE * maxValue / sizes.scaleFactor); setValue(newS, newV); ChangeEvent changeEvent = Pools.obtain(ChangeEvent.class); fire(changeEvent); Pools.free(changeEvent); }
/** * Calculate the bounds of the given actors as a group * * @param actors the actors * @param resultOrigin result origin of the bounds * @param resultSize result size of the bounds */ public static void calculateBounds( Array<Actor> actors, Vector2 resultOrigin, Vector2 resultSize) { resultOrigin.set(0, 0); resultSize.set(0, 0); if (actors.size == 0) { return; } Vector2 origin = Pools.obtain(Vector2.class); Vector2 size = Pools.obtain(Vector2.class); Vector2 leftTop = Pools.obtain(Vector2.class); Vector2 rightBottom = Pools.obtain(Vector2.class); float minX = Float.POSITIVE_INFINITY; float minY = Float.POSITIVE_INFINITY; float maxX = Float.NEGATIVE_INFINITY; float maxY = Float.NEGATIVE_INFINITY; for (Actor actor : actors) { calculateBounds(actor, origin, size); size.add(origin); leftTop.set(origin.x, size.y); rightBottom.set(size.x, origin.y); actor.localToParentCoordinates(origin); actor.localToParentCoordinates(size); actor.localToParentCoordinates(leftTop); actor.localToParentCoordinates(rightBottom); minX = Math.min(minX, Math.min(origin.x, Math.min(size.x, Math.min(leftTop.x, rightBottom.x)))); minY = Math.min(minY, Math.min(origin.y, Math.min(size.y, Math.min(leftTop.y, rightBottom.y)))); maxX = Math.max(maxX, Math.max(origin.x, Math.max(size.x, Math.max(leftTop.x, rightBottom.x)))); maxY = Math.max(maxY, Math.max(origin.y, Math.max(size.y, Math.max(leftTop.y, rightBottom.y)))); } Pools.free(origin); Pools.free(size); Pools.free(leftTop); Pools.free(rightBottom); resultOrigin.set(minX, minY); resultSize.set(maxX - minX, maxY - minY); }
/** * Applies a key up event to the actor that has {@link Stage#setKeyboardFocus(Actor) keyboard * focus}, if any, and returns true if the event was {@link Event#handle() handled}. */ public boolean keyUp(int keyCode) { Actor target = keyboardFocus == null ? root : keyboardFocus; InputEvent event = Pools.obtain(InputEvent.class); event.setStage(this); event.setType(InputEvent.Type.keyUp); event.setKeyCode(keyCode); target.fire(event); boolean handled = event.isHandled(); Pools.free(event); return handled; }
@Override public void act(float delta) { totalTime += delta; accum += delta; if (accum > end) { accum = 0; if (end > .3f) end -= .01f; addActor(Pools.obtain(Mob.class).init(faceRegion, end * 10f)); } super.act(delta); }
/** Set the current animation. Any queued animations are cleared. */ public TrackEntry setAnimation(int trackIndex, Animation animation, boolean loop) { TrackEntry current = expandToIndex(trackIndex); if (current != null) freeAll(current.next); TrackEntry entry = Pools.obtain(TrackEntry.class); entry.animation = animation; entry.loop = loop; entry.endTime = animation.getDuration(); setCurrent(trackIndex, entry); return entry; }
/** * Applies a key typed event to the actor that has {@link Stage#setKeyboardFocus(Actor) keyboard * focus}, if any, and returns true if the event was {@link Event#handle() handled}. */ public boolean keyTyped(char character) { Actor target = keyboardFocus == null ? root : keyboardFocus; InputEvent event = Pools.obtain(InputEvent.class); event.setStage(this); event.setType(InputEvent.Type.keyTyped); event.setCharacter(character); target.fire(event); boolean handled = event.isHandled(); Pools.free(event); return handled; }
/** Adjusts the position and size of the given group to its children */ public static void adjustGroup(Actor root) { if (!(root instanceof Group)) { return; } Group group = (Group) root; if (group.getChildren().size == 0) { return; } Vector2 origin = Pools.obtain(Vector2.class); Vector2 size = Pools.obtain(Vector2.class); Vector2 tmp3 = Pools.obtain(Vector2.class); Vector2 tmp4 = Pools.obtain(Vector2.class); calculateBounds(group.getChildren(), origin, size); /* * minX and minY are the new origin (new 0, 0), so everything inside the * group must be translated that much. */ for (Actor actor : group.getChildren()) { actor.setPosition(actor.getX() - origin.x, actor.getY() - origin.y); } /* * Now, we calculate the current origin (0, 0) and the new origin (minX, * minY), and group is translated by that difference. */ group.localToParentCoordinates(tmp3.set(0, 0)); group.localToParentCoordinates(tmp4.set(origin.x, origin.y)); tmp4.sub(tmp3); group.setBounds(group.getX() + tmp4.x, group.getY() + tmp4.y, size.x, size.y); group.setOrigin(size.x / 2.0f, size.y / 2.0f); Pools.free(origin); Pools.free(size); Pools.free(tmp3); Pools.free(tmp4); }
private void dealCards(int round) { Array<Integer> cards = Pools.obtain(Array.class); for (int i = 0; i < 4; i++) { cards.clear(); getDealer().deal(Math.min(round, 13), cards); players[i].receiveCards(cards); } cards.clear(); Pools.free(cards); layoutCards(); getSounds().playDealSound(); }
/** * For a given actor computes and applies the transformation to keep the same screen * transformation in a new group * * @param actor * @param parent */ public static void computeTransformFor(Actor actor, Group parent) { Vector2 tmp1 = Pools.obtain(Vector2.class); Vector2 tmp2 = Pools.obtain(Vector2.class); Vector2 tmp3 = Pools.obtain(Vector2.class); Vector2 tmp4 = Pools.obtain(Vector2.class); Vector2 tmp5 = Pools.obtain(Vector2.class); calculateBounds(actor, tmp4, tmp5); Vector2 o = tmp1.set(tmp4.x, tmp4.y); Vector2 t = tmp2.set(tmp4.x + tmp5.x, tmp4.y); Vector2 n = tmp3.set(tmp4.x, tmp4.y + tmp5.y); actor.localToAscendantCoordinates(parent, o); actor.localToAscendantCoordinates(parent, t); actor.localToAscendantCoordinates(parent, n); actor.setRotation(actor.getRotation() + actor.getParent().getRotation()); applyTransformation(actor, o, t, n); Pools.free(tmp1); Pools.free(tmp2); Pools.free(tmp3); Pools.free(tmp4); Pools.free(tmp5); }
/** * Applies a mouse scroll event to the stage and returns true if an actor in the scene {@link * Event#handle() handled} the event. This event only occurs on the desktop. */ public boolean scrolled(int amount) { Actor target = scrollFocus == null ? root : scrollFocus; screenToStageCoordinates(tempCoords.set(mouseScreenX, mouseScreenY)); InputEvent event = Pools.obtain(InputEvent.class); event.setStage(this); event.setType(InputEvent.Type.scrolled); event.setScrollAmount(amount); event.setStageX(tempCoords.x); event.setStageY(tempCoords.y); target.fire(event); boolean handled = event.isHandled(); Pools.free(event); return handled; }
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) { if (pointer == 0 && button != 0) return false; stageToLocalCoordinates(Vector2.tmp); x = Vector2.tmp.x; y = Vector2.tmp.y; if (x > 0 && x < getWidth() && y > 0 && y < getHeight()) { listSelectedIndex = (int) ((getHeight() - y) / itemHeight); listSelectedIndex = Math.max(0, listSelectedIndex); listSelectedIndex = Math.min(items.length - 1, listSelectedIndex); selectedIndex = listSelectedIndex; if (items.length > 0) { ChangeEvent changeEvent = Pools.obtain(ChangeEvent.class); SelectBox.this.fire(changeEvent); Pools.free(changeEvent); } } return true; }
/** * Sets this actor as the event {@link Event#setTarget(Actor) target} and propagates the event to * this actor and ancestor actors as necessary. If this actor is not in the stage, the stage must * be set before calling this method. * * <p>Events are fired in 2 phases. The first phase notifies listeners on each actor starting at * the root and propagating downward to (and including) this actor. The second phase notifes * listeners on each actor starting at this actor and, if {@link Event#getBubbles()} is true, * propagating upward to the root. If the event is {@link Event#stop() stopped} at any time, it * will not propagate to the next actor. * * @return true of the event was {@link Event#cancel() cancelled}. */ public boolean fire(Event event) { if (event.getStage() == null) event.setStage(getStage()); event.setTarget(this); // Collect ancestors so event propagation is unaffected by hierarchy changes. Array<Group> ancestors = Pools.obtain(Array.class); Group parent = getParent(); while (parent != null) { ancestors.add(parent); parent = parent.getParent(); } try { // Notify all parent capture listeners, starting at the root. Ancestors may stop an event // before children receive it. for (int i = ancestors.size - 1; i >= 0; i--) { Group currentTarget = ancestors.get(i); currentTarget.notify(event, true); if (event.isStopped()) return event.isCancelled(); } // Notify the target capture listeners. notify(event, true); if (event.isStopped()) return event.isCancelled(); // Notify the target listeners. notify(event, false); if (!event.getBubbles()) return event.isCancelled(); if (event.isStopped()) return event.isCancelled(); // Notify all parent listeners, starting at the target. Children may stop an event before // ancestors receive it. for (int i = 0, n = ancestors.size; i < n; i++) { ancestors.get(i).notify(event, false); if (event.isStopped()) return event.isCancelled(); } return event.isCancelled(); } finally { ancestors.clear(); Pools.free(ancestors); } }
/** * Adds an animation to be played delay seconds after the current or last queued animation. * * @param delay May be <= 0 to use duration of previous animation minus any mix duration plus the * negative delay. */ public TrackEntry addAnimation(int trackIndex, Animation animation, boolean loop, float delay) { TrackEntry entry = Pools.obtain(TrackEntry.class); entry.animation = animation; entry.loop = loop; entry.endTime = animation.getDuration(); TrackEntry last = expandToIndex(trackIndex); if (last != null) { while (last.next != null) last = last.next; last.next = entry; } else tracks.set(trackIndex, entry); if (delay <= 0) { if (last != null) delay += last.endTime - data.getMix(last.animation, animation); else delay = 0; } entry.delay = delay; return entry; }
/** * Sets the actor that will receive scroll events. * * @param actor May be null. */ public void setScrollFocus(Actor actor) { if (scrollFocus == actor) return; FocusEvent event = Pools.obtain(FocusEvent.class); event.setStage(this); event.setType(FocusEvent.Type.scroll); Actor oldScrollFocus = scrollFocus; if (oldScrollFocus != null) { event.setFocused(false); event.setRelatedActor(actor); oldScrollFocus.fire(event); } if (!event.isCancelled()) { scrollFocus = actor; if (actor != null) { event.setFocused(true); event.setRelatedActor(oldScrollFocus); actor.fire(event); if (event.isCancelled()) setScrollFocus(oldScrollFocus); } } Pools.free(event); }
/** * Calls the {@link Actor#act(float)} method on each actor in the stage. Typically called each * frame. This method also fires enter and exit events. * * @param delta Time in seconds since the last frame. */ public void act(float delta) { // Update over actors. Done in act() because actors may change position, which can fire // enter/exit without an input event. for (int pointer = 0, n = pointerOverActors.length; pointer < n; pointer++) { Actor overLast = pointerOverActors[pointer]; // Check if pointer is gone. if (!pointerTouched[pointer]) { if (overLast != null) { pointerOverActors[pointer] = null; screenToStageCoordinates( tempCoords.set(pointerScreenX[pointer], pointerScreenY[pointer])); // Exit over last. InputEvent event = Pools.obtain(InputEvent.class); event.setType(InputEvent.Type.exit); event.setStage(this); event.setStageX(tempCoords.x); event.setStageY(tempCoords.y); event.setRelatedActor(overLast); event.setPointer(pointer); overLast.fire(event); Pools.free(event); } continue; } // Update over actor for the pointer. pointerOverActors[pointer] = fireEnterAndExit(overLast, pointerScreenX[pointer], pointerScreenY[pointer], pointer); } // Update over actor for the mouse on the desktop. ApplicationType type = Gdx.app.getType(); if (type == ApplicationType.Desktop || type == ApplicationType.Applet || type == ApplicationType.WebGL) mouseOverActor = fireEnterAndExit(mouseOverActor, mouseScreenX, mouseScreenY, -1); // Run actions and determine whether to request rendering (for when setContinuousRendering is // off) root.act(delta); }
static LayerRenderNode obtain(RenderGraph graph, CanvasLayer layer) { LayerRenderNode command = Pools.obtain(LayerRenderNode.class); command.graph = graph; command.layer = layer; return command; }
public <E extends Event> E getEvent(Class<E> _type) { E event = Pools.obtain(_type); event.setManager(this); return event; }
/** Fires that this {@link #focusActor} has gained focus */ private void fireFocus() { FocusEvent dropEvent = Pools.obtain(FocusEvent.class); dropEvent.setActor(focusActor); focusActor.fire(dropEvent); Pools.free(dropEvent); }
/** * Calculates the {@code float} distance to another bounding area. * * @param anotherArea Another bounding area * @param fromBorder If true, distance is calculated between the borders, otherwise they are * calculated between their centers. * @return A positive or zero float representing the distance between this bounding area and the * given one, or a negative float if {@code fromBorder} is true and one area contains the * other. */ public float distanceTo(BoundingAreaComponent anotherArea, boolean fromBorder) { Vector2 intersection = Pools.obtain(Vector2.class); float d = distanceTo(anotherArea, intersection, fromBorder); Pools.free(intersection); return d; }
static SimpleRenderNode obtain(GlCall call) { SimpleRenderNode command = Pools.obtain(SimpleRenderNode.class); command.calls.add(call); return command; }