public void drawDebug(SpriteBatch batch) { if (debugRenderer == null) { if (Gdx.graphics.isGL20Available()) debugRenderer = new ImmediateModeRenderer20(64, false, true, 0); else debugRenderer = new ImmediateModeRenderer10(64); } float x = getX(), y = getY(); debugRenderer.begin(batch.getProjectionMatrix(), GL10.GL_LINES); float x1 = x; float y1 = y; float x2 = x1 + getPrefWidth(); float y2 = y1 + getPrefHeight(); drawDebugItem(x1, y1, x2, y2, 1, 0, 0); for (Actor a : getChildren()) { if (debugRenderer.getNumVertices() == 64) { debugRenderer.end(); debugRenderer.begin(batch.getProjectionMatrix(), GL10.GL_LINES); } x1 = x + a.getX(); y1 = y + a.getY(); x2 = x1 + a.getWidth(); y2 = y1 + a.getHeight(); drawDebugItem(x1, y1, x2, y2, 0, 1, 0); } debugRenderer.end(); }
/** * Adds an actor as a child of this group. The actor is first removed from its parent group, if * any. */ public void addActor(Actor actor) { actor.remove(); children.add(actor); actor.setParent(this); actor.setStage(getStage()); childrenChanged(); }
public static void effect_shake( final Actor actor, final float timeCount, final float limitTimeCount, final float x, final float y) { // 振幅 float amp = AMPLITUDE * (float) Math.sqrt( actor.getWidth() * actor.getWidth() + actor.getHeight() * actor.getHeight()); float interval = limitTimeCount / FREQUENCE; // 每次间隔时间计数 int current = (int) (timeCount / interval); // 已经震过的次数 double rate = (timeCount - current * interval) / interval; // 现在正在进行的震动的时间进行比例 double phyRate = Math.cos(Math.PI * rate); // 现在进行震动的物理进行比例 Random r = new Random(SEED); for (int i = 0; i < current; i++) r.nextDouble(); double angle = r.nextDouble() * Math.PI * 2; // 本次震动的角度 float dx_max = (float) Math.cos(angle) * amp, dy_max = (float) Math.sin(angle) * amp; actor.setPosition((float) (dx_max * phyRate), (float) (dy_max * phyRate)); }
private void drawRecursive(Actor actor) { if (!invisibleActors && !actor.isVisible()) return; if (allActors) actor.debug(); if (actor.getDebug()) { actor.getDebugRects(debugRects); for (DebugRect debugRect : debugRects) drawRect(actor, debugRect); debugRects.clear(); debugRectPool.freeAll(usedRects); usedRects.clear(); } boolean draw = true; Rectangle scissorBounds = null; if (actor instanceof Group) scissorBounds = ((Group) actor).getScissorBounds(); if (scissorBounds != null) { shapes.flush(); draw = ScissorStack.pushScissors(scissorBounds); } if (draw) { // Children are still rendered, even if the group has no debugging enabled. if (actor instanceof Group) { Group group = (Group) actor; for (Actor child : group.getChildren()) drawRecursive(child); } if (scissorBounds != null) { shapes.flush(); ScissorStack.popScissors(); } } }
/** @return the nearest entity associated to the given actor */ public static EngineEntity getActorEntity(Actor actor) { if (actor == null) { return null; } Object o = actor.getUserObject(); return o instanceof EngineEntity ? (EngineEntity) o : getActorEntity(actor.getParent()); }
/** * Adds an actor as a child of this group, at a specific index. The actor is first removed from * its parent group, if any. */ public void addActorAt(int index, Actor actor) { actor.remove(); children.insert(index, actor); actor.setParent(this); actor.setStage(getStage()); childrenChanged(); }
/** * @param actor is being dragged. * @param dragPane is under the actor. Stores a {@link GridGroup} or unknown group. * @param directPaneChild actor under the cursor. * @return true if actor was accepted by the group. */ protected boolean addToOtherGroup( final Actor actor, final DragPane dragPane, final Actor directPaneChild) { final Array<Actor> children = dragPane.getChildren(); final int indexOfDirectChild = children.indexOf(directPaneChild, true); final int indexOfDraggedActor = children.indexOf(actor, true); if (indexOfDraggedActor >= 0) { // Dragging own actor. if (indexOfDraggedActor > indexOfDirectChild) { // Dropped after current position. dragPane.addActorBefore(directPaneChild, actor); } else { // Dropped before current position. dragPane.addActorAfter(directPaneChild, actor); } } else if (indexOfDirectChild == children.size - 1) { // Dragged into last element. if (DRAG_POSITION.y < directPaneChild.getHeight() / 2f || DRAG_POSITION.x > directPaneChild.getWidth() / 2f) { // Adding last: // last: dragPane.addActor(actor); } else { dragPane.addActorBefore(directPaneChild, actor); } } else if (indexOfDirectChild == 0) { // Dragged into first element. if (DRAG_POSITION.y < directPaneChild.getHeight() / 2f || DRAG_POSITION.x > directPaneChild.getWidth() / 2f) { dragPane.addActorAfter(directPaneChild, actor); } else { // Adding first: dragPane.addActorBefore(directPaneChild, actor); } } else { // Replacing hovered actor: dragPane.addActorBefore(directPaneChild, actor); } return APPROVE; }
/** * @return the positions of the actors in the list * @param actors */ private List<Vector2> getPositions(SnapshotArray<Actor> actors) { List<Vector2> oldPositions = new ArrayList<Vector2>(); for (Actor actor : actors) { oldPositions.add(new Vector2(actor.getX(), actor.getY())); } return oldPositions; }
/** * 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; }
/** * 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; }
public IntroScreen(final DirectedGame game) { super(game); rootTable = new Table(); rootTable.setFillParent(true); final Actor logo = new Image(Assets.assetsManager.get(Assets.HEADMADE_LOGO, Texture.class)); // logo.setOrigin(logo.getWidth() / 2, logo.getHeight() / 2); // logo.scaleBy(2f); logo.setColor(Color.BLACK); rootTable.add(logo).center().expand(); rootTable.row(); // rootTable.setDebug(true); this.stage.addActor(rootTable); stage.addListener( new InputListener() { @Override public boolean keyDown(InputEvent event, int keycode) { if (keycode == Keys.ESCAPE) { Gdx.app.exit(); return true; } return super.keyDown(event, keycode); } }); Assets.instance.loadAll(); }
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; }
@Override public void layout() { if (!vertical) calculateHorizBoundsAndPositions(); else calculateVertBoundsAndPositions(); Actor firstWidget = this.firstWidget; if (firstWidget != null) { Rectangle firstWidgetBounds = this.firstWidgetBounds; firstWidget.setBounds( firstWidgetBounds.x, firstWidgetBounds.y, firstWidgetBounds.width, firstWidgetBounds.height); if (firstWidget instanceof Layout) ((Layout) firstWidget).validate(); } Actor secondWidget = this.secondWidget; if (secondWidget != null) { Rectangle secondWidgetBounds = this.secondWidgetBounds; secondWidget.setBounds( secondWidgetBounds.x, secondWidgetBounds.y, secondWidgetBounds.width, secondWidgetBounds.height); if (secondWidget instanceof Layout) ((Layout) secondWidget).validate(); } }
@Override public void draw(Batch batch, float parentAlpha) { validate(); Color color = getColor(); Drawable handle = style.handle; applyTransform(batch, computeTransform()); Matrix4 transform = batch.getTransformMatrix(); if (firstWidget != null) { getStage().calculateScissors(firstWidgetBounds, firstScissors); if (ScissorStack.pushScissors(firstScissors)) { if (firstWidget.isVisible()) firstWidget.draw(batch, parentAlpha * color.a); batch.flush(); ScissorStack.popScissors(); } } if (secondWidget != null) { getStage().calculateScissors(secondWidgetBounds, secondScissors); if (ScissorStack.pushScissors(secondScissors)) { if (secondWidget.isVisible()) secondWidget.draw(batch, parentAlpha * color.a); batch.flush(); ScissorStack.popScissors(); } } batch.setColor(color.r, color.g, color.b, parentAlpha * color.a); handle.draw(batch, handleBounds.x, handleBounds.y, handleBounds.width, handleBounds.height); resetTransform(batch); }
/** * Calculate the bounds of the possible children of the given actor. If actor has no children, * then resultOrigin and resultSize are set to actor's bounds. */ public static void calculateBounds(Actor actor, Vector2 resultOrigin, Vector2 resultSize) { resultOrigin.set(0, 0); resultSize.set(actor.getWidth(), actor.getHeight()); if (actor instanceof Group && ((Group) actor).getChildren().size > 0) { calculateBounds(((Group) actor).getChildren(), resultOrigin, resultSize); } }
@Override public void Reset(boolean newGame) { for (Actor a : getChildren()) { a.remove(); } getChildren().clear(); }
/** cycles to the last element */ public void last() { if (cycle || hasLastElement()) { final int nextElement = (currentElement - 1 + getChildren().size) % getChildren().size; final Actor next = setupLastElement(); next.addAction( Actions.parallel( Actions.moveTo(0, 0, animationDuration, Interpolation.pow2In), Actions.fadeIn(animationDuration))); final Actor old = getChildren().get(currentElement); old.clearActions(); old.addAction( Actions.sequence( Actions.parallel( Actions.moveTo(animationXOffset, 0, animationDuration, Interpolation.pow2In), Actions.fadeOut(animationDuration)), Actions.visible(false))); currentElement = nextElement; fireElementChanged(); } else { checkBeforeFirst(); } }
/** * @param group an empty group to be the parent * @return the group with the current selection */ public Group createGroup(Group group) { // New group has the same transformation as this group.setBounds(getX(), getY(), getWidth(), getHeight()); group.setOrigin(getOriginX(), getOriginY()); group.setRotation(getRotation()); group.setScale(getScaleX(), getScaleY()); // Each children in the group must be contained by the new group Array<Actor> children = getChildren(); children.sort( new Comparator<Actor>() { @Override public int compare(Actor actor, Actor actor2) { return ((SelectionGhost) actor).getRepresentedActor().getZIndex() - ((SelectionGhost) actor2).getRepresentedActor().getZIndex(); } }); for (Actor actor : children) { SelectionGhost ghost = (SelectionGhost) actor; Actor representedActor = ghost.getRepresentedActor(); representedActor.setPosition(ghost.getX(), ghost.getY()); representedActor.setRotation(ghost.getRotation()); representedActor.setScale(ghost.getScaleX(), ghost.getScaleY()); group.addActor(representedActor); } return group; }
private TextField findNextTextField( Array<Actor> actors, TextField best, Vector2 bestCoords, Vector2 currentCoords, boolean up) { for (int i = 0, n = actors.size; i < n; i++) { Actor actor = actors.get(i); if (actor == this) continue; if (actor instanceof TextField) { TextField textField = (TextField) actor; if (textField.isDisabled() || !textField.focusTraversal) continue; Vector2 actorCoords = actor.getParent().localToStageCoordinates(tmp3.set(actor.getX(), actor.getY())); if ((actorCoords.y < currentCoords.y || (actorCoords.y == currentCoords.y && actorCoords.x > currentCoords.x)) ^ up) { if (best == null || (actorCoords.y > bestCoords.y || (actorCoords.y == bestCoords.y && actorCoords.x < bestCoords.x)) ^ up) { best = (TextField) actor; bestCoords.set(actorCoords); } } } else if (actor instanceof Group) best = findNextTextField(((Group) actor).getChildren(), best, bestCoords, currentCoords, up); } return best; }
@Override public boolean onEnd( final Draggable draggable, final Actor actor, final float stageX, final float stageY) { if (actor == null || actor.getStage() == null) { return CANCEL; } final Actor overActor = actor.getStage().hit(stageX, stageY, true); if (overActor == null || overActor == actor) { return CANCEL; } else if (overActor.isAscendantOf(actor)) { final DragPane dragPane = getDragPane(actor); if (dragPane != null && dragPane.isFloating()) { DRAG_POSITION.set(stageX, stageY); return addToFloatingGroup(draggable, actor, dragPane); } return CANCEL; } DRAG_POSITION.set(stageX, stageY); if (overActor instanceof DragPane) { return addDirectlyToPane(draggable, actor, (DragPane) overActor); } final DragPane dragPane = getDragPane(overActor); if (accept(actor, dragPane)) { return addActor(draggable, actor, overActor, dragPane); } return CANCEL; }
private boolean isInAction(final Group group) { for (final Actor actor : group.getChildren()) { if (actor.getActions().size > 0) return true; if (actor instanceof Group) return isInAction((Group) actor); } return false; }
Menu(TwoD game) { this.game = game; batch = new SpriteBatch(); font = new BitmapFont(); font.setColor(Color.WHITE); camera = new OrthographicCamera(); viewport = new FitViewport(1024, 786, camera); viewport.apply(); stage = new Stage(viewport, batch); stage.setDebugAll(true); gearScreen = new GearScreen(); gearScreen.setBounds(512, 0, 512, 786); inventoryScreen = new InventoryScreen(); inventoryScreen.setBounds(0, 0, 512, 786); stage.addActor(gearScreen); stage.addActor(inventoryScreen); dnd = new DragAndDrop(); inputMultiplexer = new InputMultiplexer(); inputMultiplexer.addProcessor(this); inputMultiplexer.addProcessor(stage); }
@Override public void handled(Object obj) { EntityDeployRequest edr = (EntityDeployRequest) obj; Entity entity; if (!Entity.ingame.containsKey(edr.id)) entity = edr.info.createClientEntity(); else entity = Entity.ingame.get(edr.id); Actor actor = null; if (entity instanceof CardEntity) { actor = new CardEntityView((CardEntity) entity); } else if (entity instanceof CardSlotEntity) { return; // TODO handle update CardSlotEntity } else if (entity instanceof PlayerEntity) { if (edr.isClient) actor = new ClientControlView(stage, (PlayerEntity) entity); else actor = new PlayerEntityView((PlayerEntity) entity); } else return; if (!edr.viewable) return; EntityViewManager.instance.addActor(stage, actor, entity); actor.setPosition( edr.x * stage.getWidth() - actor.getWidth() / 2, edr.y * stage.getHeight() - actor.getHeight() / 2); Log.fine(entity.id() + "@" + entity.getClass().getSimpleName()); }
private static void drawDebug(Array<Actor> actors, Batch batch) { for (int i = 0, n = actors.size; i < n; i++) { Actor actor = actors.get(i); if (!actor.isVisible()) continue; if (actor instanceof Table) ((Table) actor).layout.drawDebug(batch); if (actor instanceof Group) drawDebug(((Group) actor).getChildren(), batch); } }
/** * Adds an actor as a child of this group, immediately before another child actor. The actor is * first removed from its parent group, if any. */ public void addActorBefore(Actor actorBefore, Actor actor) { actor.remove(); int index = children.indexOf(actorBefore, true); children.insert(index, actor); actor.setParent(this); actor.setStage(getStage()); childrenChanged(); }
private void removeListener() { if (draggable == null) { return; } for (final Actor actor : getChildren()) { actor.removeListener(draggable); } }
/** Returns true if this actor is the same as or is the descendant of the specified actor. */ public boolean isDescendantOf(Actor actor) { if (actor == null) throw new IllegalArgumentException("actor cannot be null."); Actor parent = this; while (true) { if (parent == null) return false; if (parent == actor) return true; parent = parent.getParent(); } }
/** * Converts coordinates for this actor to those of a parent actor. The ascendant does not need to * be a direct parent. */ public Vector2 localToAscendantCoordinates(Actor ascendant, Vector2 localCoords) { Actor actor = this; while (actor.getParent() != null) { actor.localToParentCoordinates(localCoords); actor = actor.getParent(); if (actor == ascendant) break; } return localCoords; }
/** * Updates transformation of the represented actor to match the selection ghost transformation */ private void updateTransformation() { Actor parent = representedActor.getParent(); this.localToAscendantCoordinates(parent, tmp1.set(0, 0)); this.localToAscendantCoordinates(parent, tmp2.set(getWidth(), 0)); this.localToAscendantCoordinates(parent, tmp3.set(0, getHeight())); modifier.applyTransformation(representedActor, tmp1, tmp2, tmp3); representedActor.setRotation(startingRotation + getParent().getRotation()); }
/** * Converts coordinates for this group to those of a descendant actor. The descendant does not * need to be a direct child. * * @throws IllegalArgumentException if the specified actor is not a descendant of this group. */ public void localToDescendantCoordinates(Actor descendant, Vector2 localPoint) { Group parent = descendant.getParent(); if (parent == null) throw new IllegalArgumentException("Child is not a descendant: " + descendant); // First convert to the actor's parent coordinates. if (parent != this) localToDescendantCoordinates(parent, localPoint); // Then from each parent down to the descendant. descendant.parentToLocalCoordinates(localPoint); }