Example usage for com.badlogic.gdx.graphics.g2d Sprite setOrigin

List of usage examples for com.badlogic.gdx.graphics.g2d Sprite setOrigin

Introduction

In this page you can find the example usage for com.badlogic.gdx.graphics.g2d Sprite setOrigin.

Prototype

public void setOrigin(float originX, float originY) 

Source Link

Document

Sets the origin in relation to the sprite's position for scaling and rotation.

Usage

From source file:aurelienribon.texturepackergui.Canvas.java

License:Apache License

@Override
public void render() {
    if (previousPageRequested) {
        previousPageRequested = false;//from   w  w  w  . j  a v  a  2 s .  c om
        index = index - 1 < 0 ? sprites.size() - 1 : index - 1;
    }

    if (nextPageRequested) {
        nextPageRequested = false;
        index = index + 1 >= sprites.size() ? 0 : index + 1;
    }

    if (packReloadRequested) {
        packReloadRequested = false;
        index = 0;
        camera.position.set(0, 0, 0);
        camera.update();

        sprites.clear();
        if (atlas != null)
            atlas.dispose();

        if (packFile != null && packFile.exists()) {
            try {
                atlas = new TextureAtlas(packFile);
                List<Texture> textures = new ArrayList<Texture>();

                for (AtlasRegion region : atlas.getRegions()) {
                    if (!textures.contains(region.getTexture()))
                        textures.add(region.getTexture());
                }

                for (Texture tex : textures) {
                    Sprite sp = new Sprite(tex);
                    sp.setOrigin(sp.getWidth() / 2, sp.getHeight() / 2);
                    sp.setPosition(-sp.getOriginX(), -sp.getOriginY());
                    sprites.add(sp);
                }
            } catch (GdxRuntimeException ex) {
                atlas = null;
                sprites.clear();
                callback.atlasError();
            }
        }
    }

    // Render

    Gdx.gl.glClearColor(1, 1, 1, 1);
    Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);

    float w = Gdx.graphics.getWidth();
    float h = Gdx.graphics.getHeight();
    float tw = bgTex.getWidth();
    float th = bgTex.getHeight();

    batch.getProjectionMatrix().setToOrtho2D(0, 0, w, h);
    batch.begin();
    batch.disableBlending();
    batch.draw(bgTex, 0f, 0f, w, h, 0f, 0f, w / tw, h / th);
    batch.enableBlending();
    batch.end();

    batch.setProjectionMatrix(camera.combined);
    batch.begin();
    if (!sprites.isEmpty())
        sprites.get(index).draw(batch);
    batch.end();

    batch.getProjectionMatrix().setToOrtho2D(0, 0, w, h);
    batch.begin();
    font.setColor(Color.WHITE);
    lblNextPage.draw(batch);
    lblPreviousPage.draw(batch);
    infoLabel.draw(batch);
    if (sprites.isEmpty())
        font.draw(batch, "No page to show", 10, 65);
    else
        font.draw(batch, "Page " + (index + 1) + " / " + sprites.size(), 10, 65);
    font.draw(batch, String.format(Locale.US, "Zoom: %.0f %%", 100f / camera.zoom), 10, 45);
    font.draw(batch, "Fps: " + Gdx.graphics.getFramesPerSecond(), 10, 25);
    batch.end();
}

From source file:CB_Locator.Map.MapViewBase.java

License:Open Source License

protected void renderPositionMarker(Batch batch) {
    PointD point = Descriptor.ToWorld(
            Descriptor.LongitudeToTileX(MapTileLoader.MAX_MAP_ZOOM, Locator.getLongitude()),
            Descriptor.LatitudeToTileY(MapTileLoader.MAX_MAP_ZOOM, Locator.getLatitude()),
            MapTileLoader.MAX_MAP_ZOOM, MapTileLoader.MAX_MAP_ZOOM);

    Vector2 vPoint = new Vector2((float) point.X, -(float) point.Y);

    myPointOnScreen = worldToScreen(vPoint);

    myPointOnScreen.y -= ySpeedVersatz;/* ww  w  .j  a v  a2 s  .  co m*/

    if (showAccuracyCircle) {

        if (accuracyDrawable == null) {
            accuracyDrawable = new AccuracyDrawable(this.mapIntWidth, this.mapIntWidth);
        }

        float radius = (pixelsPerMeter * Locator.getCoordinate().getAccuracy());

        if (radius > GL_UISizes.PosMarkerSize / 2) {
            accuracyDrawable.draw(batch, myPointOnScreen.x, myPointOnScreen.y, radius);
        }
    }

    boolean lastUsedCompass = Locator.UseMagneticCompass();
    boolean Transparency = LocatorSettings.PositionMarkerTransparent.getValue();

    int arrowId = 0;
    if (lastUsedCompass) {
        arrowId = Transparency ? 1 : 0;
    } else {
        arrowId = Transparency ? 3 : 2;
    }

    if (CarMode)
        arrowId = 15;

    Sprite arrow = Sprites.Arrows.get(arrowId);
    arrow.setRotation(-arrowHeading);
    arrow.setBounds(myPointOnScreen.x - GL_UISizes.halfPosMarkerSize,
            myPointOnScreen.y - GL_UISizes.halfPosMarkerSize, GL_UISizes.PosMarkerSize,
            GL_UISizes.PosMarkerSize);
    arrow.setOrigin(GL_UISizes.halfPosMarkerSize, GL_UISizes.halfPosMarkerSize);
    arrow.draw(batch);

}

From source file:CB_UI.GL_UI.Controls.ArrowView.java

License:Open Source License

@Override
public void render(Batch batch) {

    Sprite arrow = Sprites.Arrows.get(0);
    arrow.setRotation(0);//from   ww w  . j  a  v  a  2  s  .co  m
    arrow.setBounds(-(getWidth() / 2), 0, getWidth(), getHeight());
    arrow.setOrigin(this.getCenterPosX(), this.getCenterPosY());
    arrow.draw(batch);

}

From source file:CB_UI.GL_UI.Controls.MapInfoPanel.java

License:Open Source License

private void setArrowDrawable(boolean forceSet) {
    boolean tmp = Locator.UseMagneticCompass();
    if (!forceSet && tmp == lastUsedCompass)
        return;// no change required
    lastUsedCompass = tmp;//from  w  w w. j  av  a2s.co  m
    int arrowId = 0;
    if (lastUsedCompass) {
        arrowId = 0;
    } else {
        arrowId = 2;
    }
    Sprite arrowSprite = new Sprite(Sprites.Arrows.get(arrowId));
    arrowSprite.setRotation(0);// reset rotation
    arrowSprite.setOrigin(0, 0);
    arrow.setDrawable(new SpriteDrawable(arrowSprite));
}

From source file:CB_UI.GL_UI.Views.CompassView.java

License:Open Source License

private void setArrowDrawable(boolean forceSet) {
    boolean tmp = Locator.UseMagneticCompass();
    if (!forceSet && tmp == lastUsedCompass)
        return;// no change required
    lastUsedCompass = tmp;// w  w w  .ja v  a 2 s  .c  om

    int arrowId = 0;
    if (lastUsedCompass) {
        arrowId = 0;
    } else {
        arrowId = 2;
    }
    Sprite arrowSprite = new Sprite(Sprites.Arrows.get(arrowId));
    arrowSprite.setRotation(0);// reset rotation
    arrowSprite.setOrigin(0, 0);
    arrow.setDrawable(new SpriteDrawable(arrowSprite));
}

From source file:CB_UI.GL_UI.Views.MapView.java

License:Open Source License

private void RenderTargetArrow(Batch batch) {

    if (GlobalCore.getSelectedCache() == null)
        return;//from   ww  w. ja  va  2 s  .  com

    Coordinate coord = (GlobalCore.getSelectedWaypoint() != null) ? GlobalCore.getSelectedWaypoint().Pos
            : GlobalCore.getSelectedCache().Pos;

    if (coord == null) {
        return;
    }
    float x = (float) (256.0 * Descriptor.LongitudeToTileX(MapTileLoader.MAX_MAP_ZOOM, coord.getLongitude()));
    float y = (float) (-256.0 * Descriptor.LatitudeToTileY(MapTileLoader.MAX_MAP_ZOOM, coord.getLatitude()));

    float halfHeight = (mapIntHeight / 2) - ySpeedVersatz;
    float halfWidth = mapIntWidth / 2;

    // create ScreenRec
    try {
        if (TargetArrowScreenRec == null) {
            TargetArrowScreenRec = new CB_RectF(0, 0, mapIntWidth, mapIntHeight);
            if (Mode != MapMode.Compass) {
                TargetArrowScreenRec.ScaleCenter(0.9f);

                if (Mode == MapMode.Normal) {
                    TargetArrowScreenRec.setHeight(TargetArrowScreenRec.getHeight()
                            - (TargetArrowScreenRec.getHeight() - info.getY()) - zoomBtn.getHeight());
                    TargetArrowScreenRec.setY(zoomBtn.getMaxY());
                }
            }
        }

        Vector2 ScreenCenter = new Vector2(halfWidth, halfHeight);

        Vector2 screen = worldToScreen(new Vector2(x, y));
        Vector2 target = new Vector2(screen.x, screen.y);

        Vector2 newTarget = TargetArrowScreenRec.getIntersection(ScreenCenter, target);

        // Rotation berechnen
        if (newTarget != null) {

            float direction = get_angle(ScreenCenter.x, ScreenCenter.y, newTarget.x, newTarget.y);
            direction = 180 - direction;

            // draw sprite
            Sprite arrow = Sprites.Arrows.get(4);
            arrow.setRotation(direction);

            float boundsX = newTarget.x - GL_UISizes.TargetArrow.halfWidth;
            float boundsY = newTarget.y - GL_UISizes.TargetArrow.height;

            arrow.setBounds(boundsX, boundsY, GL_UISizes.TargetArrow.width, GL_UISizes.TargetArrow.height);

            arrow.setOrigin(GL_UISizes.TargetArrow.halfWidth, GL_UISizes.TargetArrow.height);
            arrow.draw(batch);

            // get real bounding box of TargetArrow
            float t[] = arrow.getVertices();
            float maxX = Math.max(Math.max(t[0], t[5]), Math.max(t[10], t[15]));
            float minX = Math.min(Math.min(t[0], t[5]), Math.min(t[10], t[15]));
            float maxY = Math.max(Math.max(t[1], t[6]), Math.max(t[11], t[16]));
            float minY = Math.min(Math.min(t[1], t[6]), Math.min(t[11], t[16]));
            TargetArrow.set(minX, minY, maxX - minX, maxY - minY);
        } else {
            TargetArrow.set(0, 0, 0, 0);
        }
    } catch (Exception e) {
        TargetArrow.set(0, 0, 0, 0);
    }
}

From source file:CB_UI.GL_UI.Views.MapView.java

License:Open Source License

public void renderWPI(Batch batch, SizeF WpUnderlay, SizeF WpSize, WaypointRenderInfo wpi) {
    Vector2 screen = worldToScreen(new Vector2(wpi.MapX, wpi.MapY));

    screen.y -= ySpeedVersatz;/*from   w  ww  .ja  v  a 2  s  .c om*/
    // FIXME create a LineDrawable class for create one times and set the Coordinates with calculated Triangles
    if (myPointOnScreen != null && showDirectLine && (wpi.Selected)
            && (wpi.Waypoint == GlobalCore.getSelectedWaypoint())) {
        // FIXME render only if visible on screen (intersect the screen rec)
        Quadrangle line = new Quadrangle(myPointOnScreen.x, myPointOnScreen.y, screen.x, screen.y,
                3 * UI_Size_Base.that.getScale());
        if (paint == null) {
            paint = new GL_Paint();
            paint.setGLColor(Color.RED);
        }
        PolygonDrawable po = new PolygonDrawable(line.getVertices(), line.getTriangles(), paint,
                this.mapIntWidth, this.mapIntHeight);
        po.draw(batch, 0, 0, this.mapIntWidth, this.mapIntHeight, 0);
        po.dispose();
    }
    // Don't render if outside of screen !!
    if ((screen.x < 0 - WpSize.width || screen.x > this.getWidth() + WpSize.height)
            || (screen.y < 0 - WpSize.height || screen.y > this.getHeight() + WpSize.height)) {
        if (wpi.Cache != null && (wpi.Cache.Id == infoBubble.getCacheId()) && infoBubble.isVisible()) {
            // check if wp selected
            if (wpi.Waypoint != null && wpi.Waypoint.equals(infoBubble.getWaypoint())
                    || wpi.Waypoint == null && infoBubble.getWaypoint() == null)
                infoBubble.setInvisible();
        }
        return;
    }

    float NameYMovement = 0;

    if ((aktZoom >= zoomCross) && (wpi.Selected) && (wpi.Waypoint == GlobalCore.getSelectedWaypoint())) {
        // Draw Cross and move screen vector
        Sprite cross = Sprites.MapOverlay.get(3);
        cross.setBounds(screen.x - WpUnderlay.halfWidth, screen.y - WpUnderlay.halfHeight, WpUnderlay.width,
                WpUnderlay.height);
        cross.draw(batch);

        screen.add(-WpUnderlay.width, WpUnderlay.height);
        NameYMovement = WpUnderlay.height;
    }

    if (wpi.UnderlayIcon != null) {
        wpi.UnderlayIcon.setBounds(screen.x - WpUnderlay.halfWidth, screen.y - WpUnderlay.halfHeight,
                WpUnderlay.width, WpUnderlay.height);
        wpi.UnderlayIcon.draw(batch);
    }
    if (wpi.Icon != null) {
        wpi.Icon.setBounds(screen.x - WpSize.halfWidth, screen.y - WpSize.halfHeight, WpSize.width,
                WpSize.height);
        wpi.Icon.draw(batch);
    }

    // draw Favorite symbol
    if (wpi.Cache != null && wpi.Cache.isFavorite()) {
        batch.draw(Sprites.getSprite(IconName.favorit.name()), screen.x + (WpSize.halfWidth / 2),
                screen.y + (WpSize.halfHeight / 2), WpSize.width, WpSize.height);
    }

    if (wpi.OverlayIcon != null) {
        wpi.OverlayIcon.setBounds(screen.x - WpUnderlay.halfWidth, screen.y - WpUnderlay.halfHeight,
                WpUnderlay.width, WpUnderlay.height);
        wpi.OverlayIcon.draw(batch);
    }

    boolean drawAsWaypoint = wpi.Waypoint != null;

    // Rating des Caches darstellen
    if (wpi.Cache != null && showRating && (!drawAsWaypoint) && (wpi.Cache.Rating > 0) && (aktZoom >= 15)) {
        Sprite rating = Sprites.MapStars.get((int) Math.min(wpi.Cache.Rating * 2, 5 * 2));
        rating.setBounds(screen.x - WpUnderlay.halfWidth,
                screen.y - WpUnderlay.halfHeight - WpUnderlay.Height4_8, WpUnderlay.width,
                WpUnderlay.Height4_8);
        rating.setOrigin(WpUnderlay.width / 2, WpUnderlay.Height4_8 / 2);
        rating.setRotation(0);
        rating.draw(batch);
        NameYMovement += WpUnderlay.Height4_8;
    }

    // Beschriftung
    if (wpi.Cache != null && showTitles && (aktZoom >= 15)) {
        try {
            String Name = drawAsWaypoint ? wpi.Waypoint.getTitle() : wpi.Cache.getName();

            if (layout == null)
                layout = new GlyphLayout(Fonts.getNormal(), Name);
            else
                layout.setText(Fonts.getNormal(), Name);

            float halfWidth = layout.width / 2;
            Fonts.getNormal().draw(batch, layout, screen.x - halfWidth,
                    screen.y - WpUnderlay.halfHeight - NameYMovement);
        } catch (Exception e) {
        }
    }

    // Show D/T-Rating
    if (wpi.Cache != null && showDT && (!drawAsWaypoint) && (aktZoom >= 15)) {
        Sprite difficulty = Sprites.MapStars.get((int) Math.min(wpi.Cache.getDifficulty() * 2, 5 * 2));
        difficulty.setBounds(screen.x - WpUnderlay.width - GL_UISizes.infoShadowHeight,
                screen.y - (WpUnderlay.Height4_8 / 2), WpUnderlay.width, WpUnderlay.Height4_8);
        difficulty.setOrigin(WpUnderlay.width / 2, WpUnderlay.Height4_8 / 2);
        difficulty.setRotation(90);
        difficulty.draw(batch);

        Sprite terrain = Sprites.MapStars.get((int) Math.min(wpi.Cache.getTerrain() * 2, 5 * 2));
        terrain.setBounds(screen.x + GL_UISizes.infoShadowHeight, screen.y - (WpUnderlay.Height4_8 / 2),
                WpUnderlay.width, WpUnderlay.Height4_8);
        terrain.setOrigin(WpUnderlay.width / 2, WpUnderlay.Height4_8 / 2);
        terrain.setRotation(90);
        terrain.draw(batch);

    }

    if (wpi.Cache != null && (wpi.Cache.Id == infoBubble.getCacheId()) && infoBubble.isVisible()) {
        if (infoBubble.getWaypoint() == wpi.Waypoint) {
            Vector2 pos = new Vector2(screen.x - infoBubble.getHalfWidth(), screen.y);
            infoBubble.setPos(pos);
        }
    }
}

From source file:CB_UI_Base.GL_UI.Controls.ZoomButtons.java

License:Open Source License

@Override
public void render(Batch batch) {
    if (withoutDrawing)
        return;//from   w ww  .j ava2  s.com
    super.render(batch);

    if (firstDraw) {
        resetFadeOut();
        firstDraw = false;
    }

    if (!this.isVisible())
        return;
    // Log.d("CACHEBOX", "in=" + fadeIn + " out=" + fadeOut + " Fade=" + FadeValue);
    checkFade();

    // draw down button
    Sprite btnDown;
    if (zoom == minzoom) {
        btnDown = Sprites.ZoomBtn.get(2);// disabled
    } else {
        btnDown = Sprites.ZoomBtn.get(onTouchDown ? 1 : 0);
    }

    float hw = HitRecDown.getWidth();
    float hh = HitRecDown.getHeight();
    float hx = HitRecDown.getX();
    float hy = HitRecDown.getY();
    float offX = 0;
    float offY = 0;

    if (portrait) {
        float e = btnDown.getWidth() / 2;
        float f = btnDown.getHeight() / 2;

        btnDown.setOrigin(e, f);
        btnDown.setRotation(90);

        hw = hh;
        hh = HitRecDown.getWidth();

        // caclc offset
        offX = -(e - f);
        offY = -(f - e);
    } else {
        btnDown.setRotation(0f);
    }

    btnDown.setBounds(hx + offX, hy + offY, hw, hh);
    btnDown.draw(batch, FadeValue);

    // draw up button
    Sprite btnUp;
    if (zoom == maxzoom) {
        btnUp = Sprites.ZoomBtn.get(5);// disabled
    } else {
        btnUp = Sprites.ZoomBtn.get(onTouchUp ? 4 : 3);
    }

    hw = HitRecUp.getWidth();
    hh = HitRecUp.getHeight();
    hx = HitRecUp.getX();
    hy = HitRecUp.getY();

    if (portrait) {
        float e = btnUp.getWidth() / 2;
        float f = btnUp.getHeight() / 2;
        btnUp.setOrigin(e, f);
        btnUp.setRotation(90);

        hw = hh;
        hh = HitRecUp.getWidth();
    } else {
        btnUp.setRotation(0f);
    }

    btnUp.setBounds(hx + offX, hy + offY, hw, hh);
    btnUp.draw(batch, FadeValue);
}

From source file:com.alma42.mapgen.game.WorldController.java

License:Apache License

private void createUser() {
    final int width = 32;
    final int height = 32;
    final Pixmap pixmap = createUser(width, height);
    // Create a new texture from pixmap data
    final Texture texture = new Texture(pixmap);
    // Create new sprites using the just created texture
    final Sprite spr = new Sprite(texture);
    // Define sprite size to be 1m x 1m in game world
    spr.setSize(1, 1);/*from   w ww .  jav a  2s  .  co  m*/
    // Set origin to sprites center
    spr.setOrigin(spr.getWidth() / 2.0f, spr.getHeight() / 2.0f);
    spr.setPosition(0, 0);
    // Put new sprite into array
    this.testSprites.add(spr);
}

From source file:com.alma42.mapgen.game.WorldController.java

License:Apache License

private void initTestObjects() {
    this.map = new Map(6000, 10000, BiomeManagerFactory.ISLAND);
    this.map.createMap();
    this.testSprites = new ArrayList<Sprite>();
    createUser();/*from   ww  w  .j av  a 2s .  c  o  m*/

    // Create new sprites using a random texture region
    for (final AGridComponent component : this.map.getGrid().getChilds().values()) {
        final Pixmap pixmap = createProceduralPixmap(component);
        // Create a new texture from pixmap data
        final Texture texture = new Texture(pixmap);
        final Sprite spr = new Sprite(texture);
        // Define sprite size to be 1m x 1m in game world
        spr.setSize((float) component.getSize(), (float) component.getSize());
        final Point center = component.getCenter();
        // Set origin to sprites center
        spr.setOrigin((float) center.getX(), (float) center.getY());
        // Calculate random position for sprite
        spr.setPosition((float) center.getX(), (float) center.getY());
        // spr.setColor(biomeManager.getBiome(component).getValue());
        // Put new sprite into array
        this.testSprites.add(spr);
    }
    // Set first sprite as selected one
    this.selectedSprite = 0;
}