Example #1
0
  private void init(String text, Image image, MenuItemStyle style) {
    this.style = style;
    this.image = image;
    setSkin(VisUI.getSkin());
    Sizes sizes = getSkin().get(Sizes.class);

    defaults().space(3);

    if (image != null) image.setScaling(Scaling.fit);
    add(image).size(sizes.menuItemIconSize);

    label = new Label(text, new LabelStyle(style.font, style.fontColor));
    label.setAlignment(Align.left);
    add(label).expand().fill();

    add(shortcutLabel = new VisLabel("", "menuitem-shortcut")).padLeft(10).right();
    shortcutLabelColor = shortcutLabel.getStyle().fontColor;

    subMenuIconCell =
        add(subMenuImage = new Image(style.subMenu))
            .padLeft(3)
            .padRight(3)
            .size(style.subMenu.getMinWidth(), style.subMenu.getMinHeight());
    subMenuIconCell.setActor(null);

    addListener(
        new ChangeListener() {
          @Override
          public void changed(ChangeEvent event, Actor actor) {
            // makes submenu item not clickable
            if (subMenu != null) event.stop();
          }
        });

    addListener(
        new InputListener() {
          @Override
          public void enter(InputEvent event, float x, float y, int pointer, Actor fromActor) {
            if (subMenu == null || isDisabled()) {
              // hides last visible submenu (if any)
              PopupMenu parent = (PopupMenu) getParent();
              parent.setSubMenu(null);
            } else {
              Stage stage = getStage();
              Vector2 pos = localToStageCoordinates(new Vector2(0, 0));

              subMenu.setPosition(
                  pos.x + getWidth() - 1, pos.y - subMenu.getHeight() + getHeight());
              if (subMenu.getY() < 0) {
                subMenu.setY(subMenu.getY() + subMenu.getHeight() - getHeight());
              }

              stage.addActor(subMenu);

              PopupMenu parent = (PopupMenu) getParent();
              parent.setSubMenu(subMenu);
            }
          }
        });
  }
Example #2
0
  @Override
  public void draw(Batch batch, float parentAlpha) {
    Color fontColor;
    if (isDisabled() && style.disabledFontColor != null) fontColor = style.disabledFontColor;
    else if (isPressed() && style.downFontColor != null) fontColor = style.downFontColor;
    else if (isChecked() && style.checkedFontColor != null)
      fontColor =
          (isOver() && style.checkedOverFontColor != null)
              ? style.checkedOverFontColor
              : style.checkedFontColor;
    else if (isOver() && style.overFontColor != null) fontColor = style.overFontColor;
    else fontColor = style.fontColor;
    if (fontColor != null) label.getStyle().fontColor = fontColor;

    if (isDisabled()) shortcutLabel.getStyle().fontColor = style.disabledFontColor;
    else shortcutLabel.getStyle().fontColor = shortcutLabelColor;

    if (image != null && generateDisabledImage) {
      if (isDisabled()) image.setColor(Color.GRAY);
      else image.setColor(Color.WHITE);
    }

    super.draw(batch, parentAlpha);
  }