Пример #1
0
 /**
  * @param draggable attached to dragged actor.
  * @param actor is being dragged.
  * @param dragPane is under the actor. Stores a {@link FloatingGroup}.
  * @return true if actor was accepted by the group.
  */
 protected boolean addToFloatingGroup(
     final Draggable draggable, final Actor actor, final DragPane dragPane) {
   final FloatingGroup group = dragPane.getFloatingGroup();
   dragPane.stageToLocalCoordinates(DRAG_POSITION);
   float x = DRAG_POSITION.x + draggable.getOffsetX();
   if (x < 0f || x + actor.getWidth() > dragPane.getWidth()) {
     // Normalizing value if set to keep within parent's bounds:
     if (draggable.isKeptWithinParent()) {
       x = x < 0f ? 0f : dragPane.getWidth() - actor.getWidth() - 1f;
     } else {
       return CANCEL;
     }
   }
   float y = DRAG_POSITION.y + draggable.getOffsetY();
   if (y < 0f || y + actor.getHeight() > dragPane.getHeight()) {
     if (draggable.isKeptWithinParent()) {
       y = y < 0f ? 0f : dragPane.getHeight() - actor.getHeight() - 1f;
     } else {
       return CANCEL;
     }
   }
   actor.remove();
   actor.setPosition(x, y);
   group.addActor(actor);
   return APPROVE;
 }
Пример #2
0
  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));
  }
Пример #3
0
 /**
  * @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;
 }
Пример #4
0
  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();
  }
Пример #5
0
 /**
  * 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 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());
  }
Пример #7
0
 @Override
 public float getPrefHeight() {
   float first =
       firstWidget == null
           ? 0
           : (firstWidget instanceof Layout
               ? ((Layout) firstWidget).getPrefHeight()
               : firstWidget.getHeight());
   float second =
       secondWidget == null
           ? 0
           : (secondWidget instanceof Layout
               ? ((Layout) secondWidget).getPrefHeight()
               : secondWidget.getHeight());
   if (!vertical) return Math.max(first, second);
   return first + style.handle.getMinHeight() + second;
 }
Пример #8
0
 public SelectionGhost(ShapeRenderer shapeRenderer, Actor representedActor) {
   this.shapeRenderer = shapeRenderer;
   this.representedActor = representedActor;
   setOrigin(representedActor.getOriginX(), representedActor.getOriginY());
   setBounds(
       representedActor.getX(),
       representedActor.getY(),
       representedActor.getWidth(),
       representedActor.getHeight());
   setRotation(representedActor.getRotation());
   setScale(representedActor.getScaleX(), representedActor.getScaleY());
   startingRotation = representedActor.getRotation();
 }
Пример #9
0
  protected void checkBounds() {

    if (follow) {
      setX(followed.getX() - getWidth() / 2);
      setY(followed.getY() + followed.getHeight());
    }

    if (getX() + getWidth() > Gdx.app.getGraphics().getWidth())
      setX(getX() - (getWidth() - (Gdx.app.getGraphics().getWidth() - getX())));

    if (getY() + getHeight() > Gdx.app.getGraphics().getHeight())
      setY(getY() - (getHeight() - (Gdx.app.getGraphics().getHeight() - getY())));

    if (getX() < 0) setX(0);
    if (getY() < 0) setY(0);
  }
Пример #10
0
  private LinkedList<Unit> selectCharacters() {
    LinkedList<Unit> units = new LinkedList<>();

    for (Actor actor : getStage().getActors()) {
      if (!(actor instanceof Unit)) continue;

      Rectangle boundingBox =
          new Rectangle(actor.getX(), actor.getY(), actor.getWidth(), actor.getHeight());

      if (selectionBox.overlaps(boundingBox)) {
        units.add((Unit) actor);
      }
    }

    return units;
  }
Пример #11
0
 /**
  * @param actor is being dragged.
  * @param dragPane is under the actor. Stores a {@link VerticalGroup}.
  * @param directPaneChild actor under the cursor.
  * @return true if actor was accepted by the group.
  */
 protected boolean addToVerticalGroup(
     final Actor actor, final DragPane dragPane, final Actor directPaneChild) {
   final Array<Actor> children = dragPane.getChildren();
   final int indexOfDraggedActor = children.indexOf(actor, true);
   if (indexOfDraggedActor >= 0) {
     final int indexOfDirectChild = children.indexOf(directPaneChild, true);
     if (indexOfDirectChild > indexOfDraggedActor) {
       dragPane.addActorAfter(directPaneChild, actor);
     } else {
       dragPane.addActorBefore(directPaneChild, actor);
     }
   } else if (DRAG_POSITION.y < directPaneChild.getHeight() / 2f) { // Y inverted.
     dragPane.addActorAfter(directPaneChild, actor);
   } else {
     dragPane.addActorBefore(directPaneChild, actor);
   }
   return APPROVE;
 }
Пример #12
0
  public void positionHorizontally() {
    float spacing = 50f;
    float offset = 0f;
    float menuWidth = 0f;
    float maxHeight = 0f;
    for (Entry<String, MenuElement> element : this.elements.entrySet()) {
      Actor actor = element.getValue().getElement();
      float width = actor.getWidth();
      float height = actor.getHeight();
      float move = width / 2;
      if (maxHeight < height) maxHeight = height;
      menuWidth += offset + move;
      actor.setPosition(offset + move, 0.0f, Align.center);
      offset += width + spacing;
    }

    this.group.setHeight(maxHeight);
    this.group.setWidth(menuWidth);
    // this.group.setPosition(menuWidth / this.elements.size() * -1f, 0.0f);
    this.moveToActive(0f);
  }
Пример #13
0
 private void computeSize(Array<Node> nodes, float indent) {
   float ySpacing = this.ySpacing;
   for (int i = 0, n = nodes.size; i < n; i++) {
     Node node = nodes.get(i);
     float rowWidth = indent + iconSpacing;
     Actor actor = node.actor;
     if (actor instanceof Layout) {
       Layout layout = (Layout) actor;
       rowWidth += layout.getPrefWidth();
       node.height = layout.getPrefHeight();
       layout.pack();
     } else {
       rowWidth += actor.getWidth();
       node.height = actor.getHeight();
     }
     if (node.icon != null) {
       rowWidth += iconSpacing * 2 + node.icon.getMinWidth();
       node.height = Math.max(node.height, node.icon.getMinHeight());
     }
     prefWidth = Math.max(prefWidth, rowWidth);
     prefHeight -= node.height + ySpacing;
     if (node.expanded) computeSize(node.children, indent + indentSpacing);
   }
 }
Пример #14
0
 public static void moveActorCenterCenterX(Actor act, float x, float y) {
   act.setX(x - act.getWidth() / 2 + screenWidth / 2);
   act.setY(-y - act.getHeight() / 2 + screenHeight / 2);
 }
Пример #15
0
 public float getHeight(Actor widget) {
   return widget.getHeight();
 }
Пример #16
0
 public float getPrefHeight(Actor actor) {
   if (actor instanceof Layout) return ((Layout) actor).getPrefHeight();
   return actor.getHeight();
 }
Пример #17
0
 @Override
 public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
   ball.setPosition(ball.getX(), this.y + ball.getHeight());
   return true;
 }
Пример #18
0
  public void layout() {
    final Drawable bg = style.background;
    final Drawable hScrollKnob = style.hScrollKnob;
    final Drawable vScrollKnob = style.vScrollKnob;

    float bgLeftWidth = 0, bgRightWidth = 0, bgTopHeight = 0, bgBottomHeight = 0;
    if (bg != null) {
      bgLeftWidth = bg.getLeftWidth();
      bgRightWidth = bg.getRightWidth();
      bgTopHeight = bg.getTopHeight();
      bgBottomHeight = bg.getBottomHeight();
    }

    float width = getWidth();
    float height = getHeight();

    float scrollbarHeight = 0;
    if (hScrollKnob != null) scrollbarHeight = hScrollKnob.getMinHeight();
    if (style.hScroll != null)
      scrollbarHeight = Math.max(scrollbarHeight, style.hScroll.getMinHeight());
    float scrollbarWidth = 0;
    if (vScrollKnob != null) scrollbarWidth = vScrollKnob.getMinWidth();
    if (style.vScroll != null)
      scrollbarWidth = Math.max(scrollbarWidth, style.vScroll.getMinWidth());

    // Get available space size by subtracting background's padded area.
    areaWidth = width - bgLeftWidth - bgRightWidth;
    areaHeight = height - bgTopHeight - bgBottomHeight;

    if (widget == null) return;

    // Get widget's desired width.
    float widgetWidth, widgetHeight;
    if (widget instanceof Layout) {
      Layout layout = (Layout) widget;
      widgetWidth = layout.getPrefWidth();
      widgetHeight = layout.getPrefHeight();
    } else {
      widgetWidth = widget.getWidth();
      widgetHeight = widget.getHeight();
    }

    // Determine if horizontal/vertical scrollbars are needed.
    scrollX = forceScrollX || (widgetWidth > areaWidth && !disableX);
    scrollY = forceScrollY || (widgetHeight > areaHeight && !disableY);

    boolean fade = fadeScrollBars;
    if (!fade) {
      // Check again, now taking into account the area that's taken up by any enabled scrollbars.
      if (scrollY) {
        areaWidth -= scrollbarWidth;
        if (!scrollX && widgetWidth > areaWidth && !disableX) {
          scrollX = true;
        }
      }
      if (scrollX) {
        areaHeight -= scrollbarHeight;
        if (!scrollY && widgetHeight > areaHeight && !disableY) {
          scrollY = true;
          areaWidth -= scrollbarWidth;
        }
      }
    }

    // Set the widget area bounds.
    widgetAreaBounds.set(bgLeftWidth, bgBottomHeight, areaWidth, areaHeight);

    if (fade) {
      // Make sure widget is drawn under fading scrollbars.
      if (scrollX) areaHeight -= scrollbarHeight;
      if (scrollY) areaWidth -= scrollbarWidth;
    } else {
      if (scrollbarsOnTop) {
        // Make sure widget is drawn under non-fading scrollbars.
        if (scrollX) widgetAreaBounds.height += scrollbarHeight;
        if (scrollY) widgetAreaBounds.width += scrollbarWidth;
      } else {
        // Offset widget area y for horizontal scrollbar.
        if (scrollX) {
          if (hScrollOnBottom) {
            widgetAreaBounds.y += scrollbarHeight;
          } else {
            widgetAreaBounds.y = 0;
          }
        }
        // Offset widget area x for vertical scrollbar.
        if (scrollY) {
          if (vScrollOnRight) {
            widgetAreaBounds.x = 0;
          } else {
            widgetAreaBounds.x += scrollbarWidth;
          }
        }
      }
    }

    // If the widget is smaller than the available space, make it take up the available space.
    widgetWidth = disableX ? width : Math.max(areaWidth, widgetWidth);
    widgetHeight = disableY ? height : Math.max(areaHeight, widgetHeight);

    maxX = widgetWidth - areaWidth;
    maxY = widgetHeight - areaHeight;
    if (fade) {
      // Make sure widget is drawn under fading scrollbars.
      if (scrollX) maxY -= scrollbarHeight;
      if (scrollY) maxX -= scrollbarWidth;
    }
    scrollX(MathUtils.clamp(amountX, 0, maxX));
    scrollY(MathUtils.clamp(amountY, 0, maxY));

    // Set the bounds and scroll knob sizes if scrollbars are needed.
    if (scrollX) {
      if (hScrollKnob != null) {
        float hScrollHeight =
            style.hScroll != null ? style.hScroll.getMinHeight() : hScrollKnob.getMinHeight();
        // the small gap where the two scroll bars intersect might have to flip from right to left
        float boundsX, boundsY;
        if (vScrollOnRight) {
          boundsX = bgLeftWidth;
        } else {
          boundsX = bgLeftWidth + scrollbarWidth;
        }
        // bar on the top or bottom
        if (hScrollOnBottom) {
          boundsY = bgBottomHeight;
        } else {
          boundsY = height - bgTopHeight - hScrollHeight;
        }
        hScrollBounds.set(boundsX, boundsY, areaWidth, hScrollHeight);
        hKnobBounds.width =
            Math.max(
                hScrollKnob.getMinWidth(), (int) (hScrollBounds.width * areaWidth / widgetWidth));
        hKnobBounds.height = hScrollKnob.getMinHeight();
        hKnobBounds.x =
            hScrollBounds.x
                + (int) ((hScrollBounds.width - hKnobBounds.width) * getScrollPercentX());
        hKnobBounds.y = hScrollBounds.y;
      } else {
        hScrollBounds.set(0, 0, 0, 0);
        hKnobBounds.set(0, 0, 0, 0);
      }
    }
    if (scrollY) {
      if (vScrollKnob != null) {
        float vScrollWidth =
            style.vScroll != null ? style.vScroll.getMinWidth() : vScrollKnob.getMinWidth();
        // the small gap where the two scroll bars intersect might have to flip from bottom to top
        float boundsX, boundsY;
        if (hScrollOnBottom) {
          boundsY = height - bgTopHeight - areaHeight;
        } else {
          boundsY = bgBottomHeight;
        }
        // bar on the left or right
        if (vScrollOnRight) {
          boundsX = width - bgRightWidth - vScrollWidth;
        } else {
          boundsX = bgLeftWidth;
        }
        vScrollBounds.set(boundsX, boundsY, vScrollWidth, areaHeight);
        vKnobBounds.width = vScrollKnob.getMinWidth();
        vKnobBounds.height =
            Math.max(
                vScrollKnob.getMinHeight(),
                (int) (vScrollBounds.height * areaHeight / widgetHeight));
        if (vScrollOnRight) {
          vKnobBounds.x = width - bgRightWidth - vScrollKnob.getMinWidth();
        } else {
          vKnobBounds.x = bgLeftWidth;
        }
        vKnobBounds.y =
            vScrollBounds.y
                + (int) ((vScrollBounds.height - vKnobBounds.height) * (1 - getScrollPercentY()));
      } else {
        vScrollBounds.set(0, 0, 0, 0);
        vKnobBounds.set(0, 0, 0, 0);
      }
    }

    widget.setSize(widgetWidth, widgetHeight);
    if (widget instanceof Layout) ((Layout) widget).validate();
  }
Пример #19
0
  public void layout() {
    Table table = getTable();
    float width = table.getWidth();
    float height = table.getHeight();

    super.layout(0, 0, width, height);

    java.util.List<Cell> cells = getCells();
    if (round) {
      for (int i = 0, n = cells.size(); i < n; i++) {
        Cell c = cells.get(i);
        if (c.getIgnore()) continue;
        float widgetWidth = Math.round(c.getWidgetWidth());
        float widgetHeight = Math.round(c.getWidgetHeight());
        float widgetX = Math.round(c.getWidgetX());
        float widgetY = height - Math.round(c.getWidgetY()) - widgetHeight;
        c.setWidgetX(widgetX);
        c.setWidgetY(widgetY);
        c.setWidgetWidth(widgetWidth);
        c.setWidgetHeight(widgetHeight);
        Actor actor = (Actor) c.getWidget();
        if (actor != null) {
          actor.setX(widgetX);
          actor.setY(widgetY);
          if (actor.getWidth() != widgetWidth || actor.getHeight() != widgetHeight) {
            actor.setWidth(widgetWidth);
            actor.setHeight(widgetHeight);
            if (actor instanceof Layout) ((Layout) actor).invalidate();
          }
        }
      }
    } else {
      for (int i = 0, n = cells.size(); i < n; i++) {
        Cell c = cells.get(i);
        if (c.getIgnore()) continue;
        float widgetWidth = c.getWidgetWidth();
        float widgetHeight = c.getWidgetHeight();
        float widgetX = c.getWidgetX();
        float widgetY = height - c.getWidgetY() - widgetHeight;
        c.setWidgetX(widgetX);
        c.setWidgetY(widgetY);
        c.setWidgetWidth(widgetWidth);
        c.setWidgetHeight(widgetHeight);
        Actor actor = (Actor) c.getWidget();
        if (actor != null) {
          actor.setX(widgetX);
          actor.setY(widgetY);
          if (actor.getWidth() != widgetWidth || actor.getHeight() != widgetHeight) {
            actor.setWidth(widgetWidth);
            actor.setHeight(widgetHeight);
            if (actor instanceof Layout) ((Layout) actor).invalidate();
          }
        }
      }
    }
    // Validate children separately from sizing actors to ensure actors without a cell are
    // validated.
    Array<Actor> children = table.getChildren();
    for (int i = 0, n = children.size; i < n; i++) {
      Actor child = children.get(i);
      if (child instanceof Layout) ((Layout) child).validate();
    }
  }
Пример #20
0
 /**
  * Draws all children. {@link #applyTransform(SpriteBatch)} should be called before and {@link
  * #resetTransform(SpriteBatch)} after this method if {@link #setTransform(boolean) transform} is
  * true. If {@link #setTransform(boolean) transform} is false these methods don't need to be
  * called, children positions are temporarily offset by the group position when drawn. This method
  * avoids drawing children completely outside the {@link #setCullingArea(Rectangle) culling area},
  * if set.
  */
 protected void drawChildren(SpriteBatch batch, float parentAlpha) {
   parentAlpha *= getColor().a;
   DelayedRemovalArray<Actor> children = this.children;
   children.begin();
   if (cullingArea != null) {
     // Draw children only if inside culling area.
     if (transform) {
       for (int i = 0, n = children.size; i < n; i++) {
         Actor child = children.get(i);
         if (!child.isVisible()) continue;
         float x = child.getX();
         float y = child.getY();
         if (x <= cullingArea.x + cullingArea.width
             && y <= cullingArea.y + cullingArea.height
             && x + child.getWidth() >= cullingArea.x
             && y + child.getHeight() >= cullingArea.y) {
           child.draw(batch, parentAlpha);
         }
       }
       batch.flush();
     } else {
       // No transform for this group, offset each child.
       float offsetX = getX();
       float offsetY = getY();
       setPosition(0, 0);
       for (int i = 0, n = children.size; i < n; i++) {
         Actor child = children.get(i);
         if (!child.isVisible()) continue;
         float x = child.getX();
         float y = child.getY();
         if (x <= cullingArea.x + cullingArea.width
             && y <= cullingArea.y + cullingArea.height
             && x + child.getWidth() >= cullingArea.x
             && y + child.getHeight() >= cullingArea.y) {
           child.translate(offsetX, offsetY);
           child.draw(batch, parentAlpha);
           child.setPosition(x, y);
         }
       }
       setPosition(offsetX, offsetY);
     }
   } else {
     if (transform) {
       for (int i = 0, n = children.size; i < n; i++) {
         Actor child = children.get(i);
         if (!child.isVisible()) continue;
         child.draw(batch, parentAlpha);
       }
       batch.flush();
     } else {
       // No transform for this group, offset each child.
       float offsetX = getX();
       float offsetY = getY();
       setPosition(0, 0);
       for (int i = 0, n = children.size; i < n; i++) {
         Actor child = children.get(i);
         if (!child.isVisible()) continue;
         float x = child.getX();
         float y = child.getY();
         child.translate(offsetX, offsetY);
         child.draw(batch, parentAlpha);
         child.setPosition(x, y);
       }
       setPosition(offsetX, offsetY);
     }
   }
   children.end();
 }