Java Code Examples for org.newdawn.slick.Image#setAlpha()

The following examples show how to use org.newdawn.slick.Image#setAlpha() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: GameData.java    From opsu-dance with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Draws a number with defaultSymbols.
 * @param n the number to draw
 * @param x the center x coordinate
 * @param y the center y coordinate
 * @param scale the scale to apply
 * @param alpha the alpha level
 */
public void drawSymbolNumber(int n, float x, float y, float scale, float alpha) {
	int length = (int) (Math.log10(n) + 1);
	float digitWidth = getDefaultSymbolImage(0).getWidth();
	if (digitWidth <= 1f) {
		return;
	}
	digitWidth = (digitWidth - SkinService.skin.getHitCircleFontOverlap()) * scale;
	float cx = x + ((length - 1) * (digitWidth / 2));

	for (int i = 0; i < length; i++) {
		Image digit = getDefaultSymbolImage(n % 10).getScaledCopy(scale);
		digit.setAlpha(alpha);
		digit.drawCentered(cx, y);
		cx -= digitWidth;
		n /= 10;
	}
}
 
Example 2
Source File: Beatmap.java    From opsu-dance with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Draws the beatmap background image.
 * @param width the container width
 * @param height the container height
 * @param alpha the alpha value
 * @param stretch if true, stretch to screen dimensions; otherwise, maintain aspect ratio
 * @return true if successful, false if any errors were produced
 */
public boolean drawBackground(int width, int height, float alpha, boolean stretch) {
	if (bg == null) {
		return false;
	}

	ImageLoader imageLoader = bgImageCache.get(bg);
	if (imageLoader == null)
		return false;

	Image bgImage = imageLoader.getImage();
	if (bgImage == null)
		return true;

	int swidth = width;
	int sheight = height;
	if (!stretch) {
		// fit image to screen
		if (bgImage.getWidth() / (float) bgImage.getHeight() > width / (float) height)  // x > y
			sheight = (int) (width * bgImage.getHeight() / (float) bgImage.getWidth());
		else
			swidth = (int) (height * bgImage.getWidth() / (float) bgImage.getHeight());
	} else {
		// fill screen while maintaining aspect ratio
		if (bgImage.getWidth() / (float) bgImage.getHeight() > width / (float) height)  // x > y
			swidth = (int) (height * bgImage.getWidth() / (float) bgImage.getHeight());
		else
			sheight = (int) (width * bgImage.getHeight() / (float) bgImage.getWidth());
	}
	bgImage = bgImage.getScaledCopy(swidth, sheight);
	bgImage.setAlpha(alpha);
	bgImage.drawCentered(width / 2, height / 2);
	return true;
}
 
Example 3
Source File: GameData.java    From opsu with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Draws a number with defaultSymbols.
 * @param n the number to draw
 * @param x the center x coordinate
 * @param y the center y coordinate
 * @param scale the scale to apply
 * @param alpha the alpha level
 */
public void drawSymbolNumber(int n, float x, float y, float scale, float alpha) {
	int length = (int) (Math.log10(n) + 1);
	float digitWidth = getDefaultSymbolImage(0).getWidth() * scale;
	float cx = x + ((length - 1) * (digitWidth / 2));

	for (int i = 0; i < length; i++) {
		Image digit = getDefaultSymbolImage(n % 10).getScaledCopy(scale);
		digit.setAlpha(alpha);
		digit.drawCentered(cx, y);
		cx -= digitWidth;
		n /= 10;
	}
}
 
Example 4
Source File: GameRanking.java    From opsu with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void render(GameContainer container, StateBasedGame game, Graphics g)
		throws SlickException {
	int width = container.getWidth();
	int height = container.getHeight();
	int mouseX = input.getMouseX(), mouseY = input.getMouseY();

	Beatmap beatmap = MusicController.getBeatmap();

	// background
	float parallaxX = 0, parallaxY = 0;
	if (Options.isParallaxEnabled()) {
		int offset = (int) (height * (GameImage.PARALLAX_SCALE - 1f));
		parallaxX = -offset / 2f * (mouseX - width / 2) / (width / 2);
		parallaxY = -offset / 2f * (mouseY - height / 2) / (height / 2);
	}
	if (!beatmap.drawBackground(width, height, parallaxX, parallaxY, 0.5f, true)) {
		Image bg = GameImage.MENU_BG.getImage();
		if (Options.isParallaxEnabled()) {
			bg = bg.getScaledCopy(GameImage.PARALLAX_SCALE);
			bg.setAlpha(0.5f);
			bg.drawCentered(width / 2 + parallaxX, height / 2 + parallaxY);
		} else {
			bg.setAlpha(0.5f);
			bg.drawCentered(width / 2, height / 2);
			bg.setAlpha(1f);
		}
	}

	// ranking screen elements
	data.drawRankingElements(g, beatmap, animationProgress.getTime());

	// buttons
	replayButton.draw();
	if (data.isGameplay() && !GameMod.AUTO.isActive())
		retryButton.draw();
	UI.getBackButton().draw(g);

	UI.draw(g);
}
 
Example 5
Source File: Beatmap.java    From opsu with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Draws the beatmap background image.
 * @param width the container width
 * @param height the container height
 * @param offsetX the x offset (from the screen center)
 * @param offsetY the y offset (from the screen center)
 * @param alpha the alpha value
 * @param stretch if true, stretch to screen dimensions; otherwise, maintain aspect ratio
 * @return true if successful, false if any errors were produced
 */
public boolean drawBackground(int width, int height, float offsetX, float offsetY, float alpha, boolean stretch) {
	if (bg == null)
		return false;

	ImageLoader imageLoader = bgImageCache.get(bg);
	if (imageLoader == null)
		return false;

	Image bgImage = imageLoader.getImage();
	if (bgImage == null)
		return true;

	int swidth = width;
	int sheight = height;
	if (!stretch) {
		// fit image to screen
		if (bgImage.getWidth() / (float) bgImage.getHeight() > width / (float) height)  // x > y
			sheight = (int) (width * bgImage.getHeight() / (float) bgImage.getWidth());
		else
			swidth = (int) (height * bgImage.getWidth() / (float) bgImage.getHeight());
	} else {
		// fill screen while maintaining aspect ratio
		if (bgImage.getWidth() / (float) bgImage.getHeight() > width / (float) height)  // x > y
			swidth = (int) (height * bgImage.getWidth() / (float) bgImage.getHeight());
		else
			sheight = (int) (width * bgImage.getHeight() / (float) bgImage.getWidth());
	}
	if (Options.isParallaxEnabled()) {
		swidth = (int) (swidth * GameImage.PARALLAX_SCALE);
		sheight = (int) (sheight * GameImage.PARALLAX_SCALE);
	}
	bgImage = bgImage.getScaledCopy(swidth, sheight);
	bgImage.setAlpha(alpha);
	if (!Options.isParallaxEnabled() && offsetX == 0f && offsetY == 0f)
		bgImage.drawCentered(width / 2, height / 2);
	else
		bgImage.drawCentered(width / 2 + offsetX, height / 2 + offsetY);
	return true;
}
 
Example 6
Source File: ScoreData.java    From opsu-dance with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Draws the score data as a rectangular button.
 * @param g the graphics context
 * @param position the index (to offset the button from the topmost button)
 * @param rank the score rank
 * @param prevScore the previous (lower) score, or -1 if none
 * @param focus whether the button is focused
 * @param t the animation progress [0,1]
 */
public void draw(Graphics g, float position, int rank, long prevScore, boolean focus, float t) {
	float x = baseX - buttonWidth * (1 - AnimationEquation.OUT_BACK.calc(t)) / 2.5f;
	float textX = x + buttonWidth * 0.24f;
	float edgeX = x + buttonWidth * 0.98f;
	float y = baseY + position;
	float midY = y + buttonHeight / 2f;
	float marginY = Fonts.DEFAULT.getLineHeight() * 0.01f;
	Color c = Colors.WHITE_FADE;
	float alpha = t;
	float oldAlpha = c.a;
	c.a = alpha;

	// rectangle outline
	Color rectColor = (focus) ? Colors.BLACK_BG_FOCUS : Colors.BLACK_BG_NORMAL;
	float oldRectAlpha = rectColor.a;
	rectColor.a *= AnimationEquation.IN_QUAD.calc(alpha);
	g.setColor(rectColor);
	g.fillRect(x, y, buttonWidth, buttonHeight);
	rectColor.a = oldRectAlpha;

	// rank
	if (focus) {
		Fonts.LARGE.drawString(
				x + buttonWidth * 0.04f,
				y + (buttonHeight - Fonts.LARGE.getLineHeight()) / 2f,
				Integer.toString(rank + 1), c
		);
	}

	// grade image
	Image img = getGrade().getMenuImage();
	img.setAlpha(alpha);
	img.drawCentered(x + buttonWidth * 0.15f, midY);
	img.setAlpha(1f);

	// score
	float textOffset = (buttonHeight - Fonts.MEDIUM.getLineHeight() - Fonts.SMALL.getLineHeight()) / 2f;
	Fonts.MEDIUM.drawString(textX, y + textOffset,
			String.format("Score: %s (%dx)", NumberFormat.getNumberInstance().format(score), combo), c);

	// hit counts (custom: osu! shows user instead, above score)
	String player = (playerName == null) ? "" : String.format("  (%s)", playerName);
	Fonts.SMALL.drawString(textX, y + textOffset + Fonts.MEDIUM.getLineHeight(),
			String.format("300:%d  100:%d  50:%d  Miss:%d%s", hit300, hit100, hit50, miss, player), c);

	// mods
	StringBuilder sb = new StringBuilder();
	for (GameMod mod : GameMod.values()) {
		if ((mod.getBit() & mods) > 0) {
			sb.append(mod.getAbbreviation());
			sb.append(',');
		}
	}
	if (sb.length() > 0) {
		sb.setLength(sb.length() - 1);
		String modString = sb.toString();
		Fonts.DEFAULT.drawString(edgeX - Fonts.DEFAULT.getWidth(modString), y + marginY, modString, c);
	}

	// accuracy
	String accuracy = String.format("%.2f%%", getScorePercent());
	Fonts.DEFAULT.drawString(edgeX - Fonts.DEFAULT.getWidth(accuracy), y + marginY + Fonts.DEFAULT.getLineHeight(), accuracy, c);

	// score difference
	String diff = (prevScore < 0 || score < prevScore) ?
			"-" : String.format("+%s", NumberFormat.getNumberInstance().format(score - prevScore));
	Fonts.DEFAULT.drawString(edgeX - Fonts.DEFAULT.getWidth(diff), y + marginY + Fonts.DEFAULT.getLineHeight() * 2, diff, c);

	// time since
	if (getTimeSince() != null) {
		Image clock = GameImage.HISTORY.getImage();
		clock.drawCentered(x + buttonWidth * 1.02f + clock.getWidth() / 2f, midY);
		Fonts.DEFAULT.drawString(
				x + buttonWidth * 1.03f + clock.getWidth(),
				midY - Fonts.DEFAULT.getLineHeight() / 2f,
				getTimeSince(), c
		);
	}

	c.a = oldAlpha;
}
 
Example 7
Source File: Spinner.java    From opsu-dance with GNU General Public License v3.0 4 votes vote down vote up
@Override
public void draw(Graphics g, int trackPosition, float mirrorAngle) {
	if (mirrorAngle != 0f) {
		return;
	}
	// only draw spinners shortly before start time
	int timeDiff = hitObject.getTime() - trackPosition;
	final int fadeInTime = gameState.getFadeInTime();
	if (timeDiff - fadeInTime > 0)
		return;

	boolean spinnerComplete = (rotations >= rotationsNeeded);
	float alpha = Utils.clamp(1 - (float) timeDiff / fadeInTime, 0f, 1f);

	// darken screen
	if (SkinService.skin.isSpinnerFadePlayfield()) {
		float oldAlpha = Colors.BLACK_ALPHA.a;
		if (timeDiff > 0)
			Colors.BLACK_ALPHA.a *= alpha;
		g.setColor(Colors.BLACK_ALPHA);
		g.fillRect(0, 0, width, height);
		Colors.BLACK_ALPHA.a = oldAlpha;
	}

	// rpm
	Image rpmImg = GameImage.SPINNER_RPM.getImage();
	rpmImg.setAlpha(alpha);
	rpmImg.drawCentered(width2, height - rpmImg.getHeight() / 2f);
	if (timeDiff < 0)
		data.drawSymbolString(Integer.toString(drawnRPM), (width + rpmImg.getWidth() * 0.95f) / 2f,
				height - data.getScoreSymbolImage('0').getHeight() * 1.025f, 1f, 1f, true);

	// spinner meter (subimage)
	Image spinnerMetre = GameImage.SPINNER_METRE.getImage();
	int spinnerMetreY = (spinnerComplete) ? 0 : (int) (spinnerMetre.getHeight() * (1 - (rotations / rotationsNeeded)));
	Image spinnerMetreSub = spinnerMetre.getSubImage(
			0, spinnerMetreY,
			spinnerMetre.getWidth(), spinnerMetre.getHeight() - spinnerMetreY
	);
	spinnerMetreSub.setAlpha(alpha);
	spinnerMetreSub.draw(0, height - spinnerMetreSub.getHeight());

	// main spinner elements
	GameImage.SPINNER_CIRCLE.getImage().setAlpha(alpha);
	GameImage.SPINNER_CIRCLE.getImage().setRotation(drawRotation * 360f);
	GameImage.SPINNER_CIRCLE.getImage().drawCentered(width2, height2);
	if (!GameMod.HIDDEN.isActive()) {
		float approachScale = 1 - Utils.clamp(((float) timeDiff / (hitObject.getTime() - hitObject.getEndTime())), 0f, 1f);
		Image approachCircleScaled = GameImage.SPINNER_APPROACHCIRCLE.getImage().getScaledCopy(approachScale);
		approachCircleScaled.setAlpha(alpha);
		approachCircleScaled.drawCentered(width2, height2);
	}
	GameImage.SPINNER_SPIN.getImage().setAlpha(alpha);
	GameImage.SPINNER_SPIN.getImage().drawCentered(width2, height * 3 / 4);

	if (spinnerComplete) {
		GameImage.SPINNER_CLEAR.getImage().drawCentered(width2, height / 4);
		int extraRotations = (int) (rotations - rotationsNeeded);
		if (extraRotations > 0)
			data.drawSymbolNumber(extraRotations * 1000, width2, height * 2 / 3, 1f, 1f);
	}
}
 
Example 8
Source File: PauseOverlay.java    From opsu-dance with GNU General Public License v3.0 4 votes vote down vote up
@Override
public void render(Graphics g)
{
	if (this.fadeInTime < 0) {
		return;
	}

	if (this.readyToResume) {
		// draw glowing hit select circle and pulse effect
		Image cursorCircle = HITCIRCLE_SELECT.absScale(HITCIRCLE.getWidth());
		cursorCircle.setAlpha(1.0f);
		cursorCircle.drawCentered(this.mousePauseX, this.mousePauseY);
		final float pulseScale = 1f + this.pausePulseTiming;
		Image cursorCirclePulse = cursorCircle.getScaledCopy(pulseScale);
		cursorCirclePulse.setAlpha(1f - this.pausePulseTiming);
		cursorCirclePulse.drawCentered(this.mousePauseX, this.mousePauseY);
		return;
	}

	final float fadein = clamp((float) this.fadeInTime / LOSE_FADEIN_TIME, 0f, 1f);
	glDisable(GL_TEXTURE_2D);
	glColor4f(0f, 0f, 0f, .6f * fadein);
	glBegin(GL_QUADS);
	glVertex2f(0, 0);
	glVertex2f(width, 0);
	glVertex2f(width, height);
	glVertex2f(0, height);
	glEnd();
	glEnable(GL_TEXTURE_2D);

	glColor4f(1f, 1f, 1f, fadein);
	this.background.draw(0, 0, null);

	if (!this.isLose) {
		glColor4f(1f, 1f, 1f, fadein);
		this.continueButton.draw(null);
	}
	glColor4f(1f, 1f, 1f, fadein);
	this.retryButton.draw(null);
	glColor4f(1f, 1f, 1f, fadein);
	this.backButton.draw(null);
}
 
Example 9
Source File: ScoreData.java    From opsu with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Draws the score data as a rectangular button.
 * @param g the graphics context
 * @param position the index (to offset the button from the topmost button)
 * @param rank the score rank
 * @param prevScore the previous (lower) score, or -1 if none
 * @param focus whether the button is focused
 * @param t the animation progress [0,1]
 */
public void draw(Graphics g, float position, int rank, long prevScore, boolean focus, float t) {
	float x = baseX - buttonWidth * (1 - AnimationEquation.OUT_BACK.calc(t)) / 2.5f;
	float rankX = x + buttonWidth * 0.04f;
	float edgeX = x + buttonWidth * 0.98f;
	float y = baseY + position;
	float midY = y + buttonHeight / 2f;
	float marginY = Fonts.DEFAULT.getLineHeight() * 0.01f;
	Color c = Colors.WHITE_FADE;
	float alpha = t;
	float oldAlpha = c.a;
	c.a = alpha;

	// rectangle outline
	g.setLineWidth(1f);
	Color rectColor = (focus) ? Colors.BLACK_BG_HOVER : Colors.BLACK_BG_NORMAL;
	float oldRectAlpha = rectColor.a;
	rectColor.a *= AnimationEquation.IN_QUAD.calc(alpha);
	g.setColor(rectColor);
	g.fillRect(x + 1, y + 1, buttonWidth - 1, buttonHeight - 1);
	rectColor.a *= 1.25f;
	g.setColor(rectColor);
	g.drawRect(x, y, buttonWidth, buttonHeight);
	rectColor.a = oldRectAlpha;

	// rank
	if (focus) {
		Fonts.LARGE.drawString(
			rankX, y + (buttonHeight - Fonts.LARGE.getLineHeight()) / 2f,
			Integer.toString(rank + 1), c
		);
	}

	// grade image
	float gradeX = rankX + Fonts.LARGE.getWidth("###");
	Image img = getGrade().getMenuImage();
	img.setAlpha(alpha);
	img.draw(gradeX, midY - img.getHeight() / 2f);
	img.setAlpha(1f);

	// player
	float textX = gradeX + img.getWidth() * 1.2f;
	float textOffset = (buttonHeight - Fonts.LARGE.getLineHeight() - Fonts.MEDIUM.getLineHeight()) / 2f;
	if (playerName != null)
		Fonts.LARGE.drawString(textX, y + textOffset, playerName);
	textOffset += Fonts.LARGE.getLineHeight() - 4;

	// score
	Fonts.MEDIUM.drawString(
		textX, y + textOffset,
		String.format("Score: %s (%dx)", NumberFormat.getNumberInstance().format(score), combo), c
	);

	// mods
	StringBuilder sb = new StringBuilder();
	for (GameMod mod : GameMod.values()) {
		if ((mod.getBit() & mods) > 0) {
			sb.append(mod.getAbbreviation());
			sb.append(',');
		}
	}
	if (sb.length() > 0) {
		sb.setLength(sb.length() - 1);
		String modString = sb.toString();
		Fonts.DEFAULT.drawString(edgeX - Fonts.DEFAULT.getWidth(modString), y + marginY, modString, c);
	}

	// accuracy
	String accuracy = String.format("%.2f%%", getScorePercent());
	Fonts.DEFAULT.drawString(edgeX - Fonts.DEFAULT.getWidth(accuracy), y + marginY + Fonts.DEFAULT.getLineHeight(), accuracy, c);

	// score difference
	String diff = (prevScore < 0 || score < prevScore) ?
		"-" : String.format("+%s", NumberFormat.getNumberInstance().format(score - prevScore));
	Fonts.DEFAULT.drawString(edgeX - Fonts.DEFAULT.getWidth(diff), y + marginY + Fonts.DEFAULT.getLineHeight() * 2, diff, c);

	// time since
	if (getTimeSince() != null) {
		Image clock = GameImage.HISTORY.getImage();
		clock.drawCentered(x + buttonWidth * 1.02f + clock.getWidth() / 2f, midY);
		Fonts.DEFAULT.drawString(
			x + buttonWidth * 1.03f + clock.getWidth(),
			midY - Fonts.DEFAULT.getLineHeight() / 2f,
			getTimeSince(), c
		);
	}

	c.a = oldAlpha;
}
 
Example 10
Source File: Spinner.java    From opsu with GNU General Public License v3.0 4 votes vote down vote up
@Override
public void draw(Graphics g, int trackPosition) {
	// only draw spinners shortly before start time
	int timeDiff = hitObject.getTime() - trackPosition;
	final int fadeInTime = game.getFadeInTime();
	if (timeDiff - fadeInTime > 0)
		return;

	boolean spinnerComplete = (rotations >= rotationsNeeded);
	float alpha = Utils.clamp(1 - (float) timeDiff / fadeInTime, 0f, 1f);

	// darken screen
	if (Options.getSkin().isSpinnerFadePlayfield()) {
		float oldAlpha = Colors.BLACK_ALPHA.a;
		if (timeDiff > 0)
			Colors.BLACK_ALPHA.a *= alpha;
		g.setColor(Colors.BLACK_ALPHA);
		g.fillRect(0, 0, width, height);
		Colors.BLACK_ALPHA.a = oldAlpha;
	}

	// rpm
	Image rpmImg = GameImage.SPINNER_RPM.getImage();
	rpmImg.setAlpha(alpha);
	rpmImg.drawCentered(width / 2f, height - rpmImg.getHeight() / 2f);
	if (timeDiff < 0)
		data.drawSymbolString(Integer.toString(drawnRPM), (width + rpmImg.getWidth() * 0.95f) / 2f,
				height - data.getScoreSymbolImage('0').getHeight() * 1.025f, 1f, 1f, true);

	// spinner meter (subimage)
	Image spinnerMetre = GameImage.SPINNER_METRE.getImage();
	int spinnerMetreY = (spinnerComplete) ? 0 : (int) (spinnerMetre.getHeight() * (1 - (rotations / rotationsNeeded)));
	Image spinnerMetreSub = spinnerMetre.getSubImage(
			0, spinnerMetreY,
			spinnerMetre.getWidth(), spinnerMetre.getHeight() - spinnerMetreY
	);
	spinnerMetreSub.setAlpha(alpha);
	spinnerMetreSub.draw(0, height - spinnerMetreSub.getHeight());

	// main spinner elements
	GameImage.SPINNER_CIRCLE.getImage().setAlpha(alpha);
	GameImage.SPINNER_CIRCLE.getImage().setRotation(drawRotation * 360f);
	GameImage.SPINNER_CIRCLE.getImage().drawCentered(width / 2, height / 2);
	if (!GameMod.HIDDEN.isActive()) {
		float approachScale = 1 - Utils.clamp(((float) timeDiff / (hitObject.getTime() - hitObject.getEndTime())), 0f, 1f);
		Image approachCircleScaled = GameImage.SPINNER_APPROACHCIRCLE.getImage().getScaledCopy(approachScale);
		approachCircleScaled.setAlpha(alpha);
		approachCircleScaled.drawCentered(width / 2, height / 2);
	}
	GameImage.SPINNER_SPIN.getImage().setAlpha(alpha);
	GameImage.SPINNER_SPIN.getImage().drawCentered(width / 2, height * 3 / 4);

	if (spinnerComplete) {
		GameImage.SPINNER_CLEAR.getImage().drawCentered(width / 2, height / 4);
		int extraRotations = (int) (rotations - rotationsNeeded);
		if (extraRotations > 0)
			data.drawSymbolNumber(extraRotations * 1000, width / 2, height * 2 / 3, 1f, 1f);
	}
}
 
Example 11
Source File: UserButton.java    From opsu with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Draws a user button.
 * @param g the graphics context
 * @param alpha the alpha multiplier
 */
public void draw(Graphics g, float alpha) {
	int padding = 4;
	int cx = x + padding, cy = y + padding;
	float t = bgAlpha.getValue();
	float oldWhiteAlpha = Colors.WHITE_FADE.a;
	Colors.WHITE_FADE.a = alpha;

	// rectangle
	Color bg;
	if (flashing) {
		bg = flashColor;
		bg.a = t * alpha;
	} else {
		bg = bgColor;
		bg.a = (0.5f * t) * alpha;
	}
	g.setColor(bg);
	g.fillRoundRect(cx, cy, buttonWidth - padding * 2, buttonHeight - padding * 2, 4);

	// no user?
	if (user == null && placeholderText != null) {
		Fonts.LARGE.drawString(
			x + (buttonWidth - Fonts.LARGE.getWidth(placeholderText)) / 2,
			y + (buttonHeight - Fonts.LARGE.getLineHeight()) / 2,
			placeholderText, Colors.WHITE_FADE
		);
		Colors.WHITE_FADE.a = oldWhiteAlpha;
		return;
	}

	// icon
	int iconSize = buttonHeight - padding * 4;
	Image img = getIconImage(user.getIconId());
	img.setAlpha(alpha);
	img.draw(cx + padding, cy + padding);

	// text
	int textX = cx + iconSize + padding * 3;
	int textY = cy + padding / 2;
	Fonts.MEDIUM.drawString(textX, textY, user.getName(), Colors.WHITE_FADE);
	textY += Fonts.MEDIUM.getLineHeight() - 3;
	Fonts.SMALL.drawString(textX, textY, String.format("Score: %,d", user.getScore()), Colors.WHITE_FADE);
	textY += Fonts.SMALL.getLineHeight() - 2;
	Fonts.SMALL.drawString(textX, textY, String.format("Accuracy: %.2f%%", user.getAccuracy()), Colors.WHITE_FADE);
	textY += Fonts.SMALL.getLineHeight() - 2;
	Fonts.SMALL.drawString(textX, textY, String.format("Lv%d", user.getLevel()), Colors.WHITE_FADE);

	// progress bar
	int barX = textX + Fonts.SMALL.getWidth("Lv#####");
	int barWidth = x + buttonWidth - padding - barX - 1;
	int barHeight = buttonHeight / 7;
	int barY = y + buttonHeight - padding - barHeight - 1;
	int barRadius = 8;
	float barAlpha = (0.75f + 0.25f * t) * alpha;
	barBgColor.a = barBorderColor.a = barFillColor.a = barAlpha;
	g.setColor(barBgColor);
	g.fillRoundRect(barX, barY, barWidth, barHeight, barRadius);
	g.setClip(barX, barY, (int) (barWidth * user.getNextLevelProgress()), barHeight);
	g.setColor(barFillColor);
	g.fillRoundRect(barX, barY, barWidth, barHeight, barRadius);
	g.clearClip();
	g.setAntiAlias(true);
	g.setColor(barBorderColor);
	g.setLineWidth(2f);
	g.drawRoundRect(barX, barY, barWidth, barHeight, barRadius);
	g.resetLineWidth();
	g.setAntiAlias(false);

	Colors.WHITE_FADE.a = oldWhiteAlpha;
}
 
Example 12
Source File: MenuButton.java    From opsu with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Draw the button with a color filter at the given scale.
 * @param filter the color to filter with when drawing
 * @param scaleOverride the scale to use when drawing (only works for normal images)
 */
@SuppressWarnings("deprecation")
public void draw(Color filter, float scaleOverride) {
	// animations: get current frame
	Image image = this.img;
	if (image == null) {
		anim.updateNoDraw();
		image = anim.getCurrentFrame();
	}

	// normal images
	if (imgL == null) {
		float xScaleOffset = 0f, yScaleOffset = 0f;
		if (scaleOverride != 1f) {
			image = image.getScaledCopy(scaleOverride);
			xScaleOffset = image.getWidth() / 2f - xRadius;
			yScaleOffset = image.getHeight() / 2f - yRadius;
		}
		lastScale = scaleOverride;
		if (hoverEffect == 0)
			image.draw(x - xRadius, y - yRadius, filter);
		else {
			float oldAlpha = image.getAlpha();
			float oldAngle = image.getRotation();
			if ((hoverEffect & EFFECT_EXPAND) > 0) {
				if (scale.getValue() != 1f) {
					image = image.getScaledCopy(scale.getValue());
					image.setAlpha(oldAlpha);
					if (scaleOverride != 1f) {
						xScaleOffset = image.getWidth() / 2f - xRadius;
						yScaleOffset = image.getHeight() / 2f - yRadius;
					}
					lastScale *= scale.getValue();
				}
			}
			if ((hoverEffect & EFFECT_FADE) > 0)
				image.setAlpha(alpha.getValue());
			if ((hoverEffect & EFFECT_ROTATE) > 0)
				image.setRotation(angle.getValue());
			image.draw(x - xRadius - xScaleOffset, y - yRadius - yScaleOffset, filter);
			if (image == this.img) {
				image.setAlpha(oldAlpha);
				image.setRotation(oldAngle);
			}
		}
	}

	// 3-part images
	else {
		if (hoverEffect == 0) {
			image.draw(x - xRadius + imgL.getWidth(), y - yRadius, filter);
			imgL.draw(x - xRadius, y - yRadius, filter);
			imgR.draw(x + xRadius - imgR.getWidth(), y - yRadius, filter);
		} else if ((hoverEffect & EFFECT_FADE) > 0) {
			float a = image.getAlpha(), aL = imgL.getAlpha(), aR = imgR.getAlpha();
			float currentAlpha = alpha.getValue();
			image.setAlpha(currentAlpha);
			imgL.setAlpha(currentAlpha);
			imgR.setAlpha(currentAlpha);
			image.draw(x - xRadius + imgL.getWidth(), y - yRadius, filter);
			imgL.draw(x - xRadius, y - yRadius, filter);
			imgR.draw(x + xRadius - imgR.getWidth(), y - yRadius, filter);
			image.setAlpha(a);
			imgL.setAlpha(aL);
			imgR.setAlpha(aR);
		}
	}

	// text
	if (text != null)
		font.drawString(x - font.getWidth(text) / 2f, y - font.getLineHeight() / 2f, text, color);
}
 
Example 13
Source File: BeatmapSetNode.java    From opsu with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Draws the button.
 * @param x the x coordinate
 * @param y the y coordinate
 * @param grade the highest grade, if any
 * @param focus true if this is the focused node
 */
public void draw(float x, float y, Grade grade, boolean focus) {
	Image bg = GameImage.MENU_BUTTON_BG.getImage();
	boolean expanded = (beatmapIndex > -1);
	Beatmap beatmap = beatmapSet.get(expanded ? beatmapIndex : 0);
	bg.setAlpha(0.9f);
	Color bgColor;
	Color textColor = Options.getSkin().getSongSelectInactiveTextColor();

	// get drawing parameters
	if (expanded) {
		x -= bg.getWidth() / 10f;
		if (focus) {
			bgColor = Color.white;
			textColor = Options.getSkin().getSongSelectActiveTextColor();
		} else
			bgColor = Colors.BLUE_BUTTON;
	} else if (beatmapSet.isPlayed())
		bgColor = Colors.ORANGE_BUTTON;
	else
		bgColor = Colors.PINK_BUTTON;
	bg.draw(x, y, bgColor);

	float cx = x + (bg.getWidth() * 0.043f);
	float cy = y + (bg.getHeight() * 0.18f) - 3;

	// draw grade
	if (grade != Grade.NULL) {
		Image gradeImg = grade.getMenuImage();
		gradeImg.drawCentered(cx - bg.getWidth() * 0.01f + gradeImg.getWidth() / 2f, y + bg.getHeight() / 2.2f);
		cx += gradeImg.getWidth();
	}

	// draw text
	if (Options.useUnicodeMetadata()) {  // load glyphs
		Fonts.loadGlyphs(Fonts.MEDIUM, beatmap.titleUnicode);
		Fonts.loadGlyphs(Fonts.DEFAULT, beatmap.artistUnicode);
	}
	Fonts.MEDIUM.drawString(cx, cy, beatmap.getTitle(), textColor);
	Fonts.DEFAULT.drawString(cx, cy + Fonts.MEDIUM.getLineHeight() - 3,
			String.format("%s // %s", beatmap.getArtist(), beatmap.creator), textColor);
	if (expanded || beatmapSet.size() == 1)
		Fonts.BOLD.drawString(cx, cy + Fonts.MEDIUM.getLineHeight() + Fonts.DEFAULT.getLineHeight() - 6,
				beatmap.version, textColor);

	// draw stars
	// (note: in osu!, stars are also drawn for beatmap sets of size 1)
	if (expanded) {
		if (beatmap.starRating >= 0) {
			Image star = GameImage.STAR.getImage();
			float starOffset = star.getWidth() * 1.25f;
			float starX = cx + starOffset * 0.02f;
			float starY = cy + Fonts.MEDIUM.getLineHeight() + Fonts.DEFAULT.getLineHeight() * 2 - 6f * GameImage.getUIscale();
			float starCenterY = starY + star.getHeight() / 2f;
			final float baseAlpha = focus ? 1f : 0.8f;
			final float smallStarScale = 0.4f;
			final int maxStars = 10;
			star.setAlpha(baseAlpha);
			int i = 1;
			for (; i < beatmap.starRating && i <= maxStars; i++) {
				if (focus)
					star.drawFlash(starX + (i - 1) * starOffset, starY, star.getWidth(), star.getHeight(), textColor);
				else
					star.draw(starX + (i - 1) * starOffset, starY);
			}
			if (i <= maxStars) {
				float partialStarScale = smallStarScale + (float) (beatmap.starRating - i + 1) * (1f - smallStarScale);
				Image partialStar = star.getScaledCopy(partialStarScale);
				partialStar.setAlpha(baseAlpha);
				float partialStarY = starCenterY - partialStar.getHeight() / 2f;
				if (focus)
					partialStar.drawFlash(starX + (i - 1) * starOffset, partialStarY, partialStar.getWidth(), partialStar.getHeight(), textColor);
				else
					partialStar.draw(starX + (i - 1) * starOffset, partialStarY);
			}
			if (++i <= maxStars) {
				Image smallStar = star.getScaledCopy(smallStarScale);
				smallStar.setAlpha(0.5f);
				float smallStarY = starCenterY - smallStar.getHeight() / 2f;
				for (; i <= maxStars; i++)
					smallStar.draw(starX + (i - 1) * starOffset, smallStarY);
			}
		}
	}
}