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

The following examples show how to use org.newdawn.slick.Image#getWidth() . 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: CurveRenderState.java    From opsu-dance with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Reads the first row of the slider gradient texture and upload it as
 * a 1D texture to OpenGL if it hasn't already been done.
 */
public void initGradient() {
	if (gradientTexture == 0) {
		Image slider = GameImage.SLIDER_GRADIENT.getImage().getScaledCopy(1.0f / GameImage.getUIscale());
		staticState.gradientTexture = GL11.glGenTextures();
		ByteBuffer buff = BufferUtils.createByteBuffer(slider.getWidth() * 4);
		for (int i = 0; i < slider.getWidth(); ++i) {
			Color col = slider.getColor(i, 0);
			buff.put((byte) (255 * col.b));
			buff.put((byte) (255 * col.b)); // I know this looks strange...
			buff.put((byte) (255 * col.b));
			buff.put((byte) (255 * col.a));
		}
		buff.flip();
		GL11.glBindTexture(GL11.GL_TEXTURE_1D, gradientTexture);
		GL11.glTexImage1D(GL11.GL_TEXTURE_1D, 0, GL11.GL_RGBA, slider.getWidth(), 0, GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, buff);
		EXTFramebufferObject.glGenerateMipmapEXT(GL11.GL_TEXTURE_1D);
	}
}
 
Example 2
Source File: MenuButton.java    From opsu with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Set x and y radius of the button based on current scale factor
 * and expansion direction.
 */
private void setHoverRadius() {
	Image image = this.img;
	if (image == null)
		image = anim.getCurrentFrame();

	int xOffset = 0, yOffset = 0;
	float currentScale = scale.getValue();
	if (dir != Expand.CENTER) {
		// offset by difference between normal/scaled image dimensions
		xOffset = (int) ((currentScale - 1f) * image.getWidth());
		yOffset = (int) ((currentScale - 1f) * image.getHeight());
		if (dir == Expand.UP || dir == Expand.DOWN)
			xOffset = 0;    // no horizontal offset
		if (dir == Expand.RIGHT || dir == Expand.LEFT)
			yOffset = 0;    // no vertical offset
		if (dir == Expand.RIGHT || dir == Expand.DOWN_RIGHT || dir == Expand.UP_RIGHT)
			xOffset *= -1;  // flip x for right
		if (dir == Expand.DOWN ||  dir == Expand.DOWN_LEFT || dir == Expand.DOWN_RIGHT)
			yOffset *= -1;  // flip y for down
	}
	this.xRadius = ((image.getWidth() * currentScale) + xOffset) / 2f;
	this.yRadius = ((image.getHeight() * currentScale) + yOffset) / 2f;
}
 
Example 3
Source File: GameRanking.java    From opsu with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void init(GameContainer container, StateBasedGame game)
		throws SlickException {
	this.game = game;
	this.input = container.getInput();

	int width = container.getWidth();
	int height = container.getHeight();

	// buttons
	Image retry = GameImage.PAUSE_RETRY.getImage();
	Image replay = GameImage.PAUSE_REPLAY.getImage();
	replayY = (height * 0.985f) - replay.getHeight() / 2f;
	retryY = replayY - (replay.getHeight() / 2f) - (retry.getHeight() / 1.975f);
	retryButton = new MenuButton(retry, width - (retry.getWidth() / 2f), retryY);
	replayButton = new MenuButton(replay, width - (replay.getWidth() / 2f), replayY);
	retryButton.setHoverFade();
	replayButton.setHoverFade();
}
 
Example 4
Source File: CurveRenderState.java    From opsu with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Reads the first row of the slider gradient texture and upload it as
 * a 1D texture to OpenGL if it hasn't already been done.
 */
public void initGradient() {
	if (gradientTexture == 0) {
		Image slider = GameImage.SLIDER_GRADIENT.getImage().getScaledCopy(1.0f / GameImage.getUIscale());
		staticState.gradientTexture = GL11.glGenTextures();
		ByteBuffer buff = BufferUtils.createByteBuffer(slider.getWidth() * 4);
		for (int i = 0; i < slider.getWidth(); ++i) {
			Color col = slider.getColor(i, 0);
			buff.put((byte) (255 * col.r));
			buff.put((byte) (255 * col.g));
			buff.put((byte) (255 * col.b));
			buff.put((byte) (255 * col.a));
		}
		buff.flip();
		GL11.glBindTexture(GL11.GL_TEXTURE_1D, gradientTexture);
		GL11.glTexImage1D(GL11.GL_TEXTURE_1D, 0, GL11.GL_RGBA, slider.getWidth(), 0, GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, buff);
		ContextCapabilities capabilities = GLContext.getCapabilities();
		if (capabilities.OpenGL30) {
			GL30.glGenerateMipmap(GL11.GL_TEXTURE_1D);
		} else if (capabilities.GL_EXT_framebuffer_object) {
			EXTFramebufferObject.glGenerateMipmapEXT(GL11.GL_TEXTURE_1D);
		} else {
			GL11.glTexParameteri(GL11.GL_TEXTURE_1D, GL14.GL_GENERATE_MIPMAP, GL11.GL_TRUE);
		}
	}
}
 
Example 5
Source File: LegacyCurveRenderState.java    From opsu with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Reads the first row of the slider gradient texture and upload it as
 * a 1D texture to OpenGL if it hasn't already been done.
 */
public void initGradient() {
	if (gradientTexture == 0) {
		Image slider = GameImage.SLIDER_GRADIENT_EXPERIMENTAL.getImage().getScaledCopy(1.0f / GameImage.getUIscale());
		staticState.gradientTexture = GL11.glGenTextures();
		ByteBuffer buff = BufferUtils.createByteBuffer(slider.getWidth() * 4);
		for (int i = 0; i < slider.getWidth(); ++i) {
			Color col = slider.getColor(i, 0);
			buff.put((byte) (255 * col.r));
			buff.put((byte) (255 * col.g));
			buff.put((byte) (255 * col.b));
			buff.put((byte) (255 * col.a));
		}
		buff.flip();
		GL11.glBindTexture(GL11.GL_TEXTURE_1D, gradientTexture);
		GL11.glTexImage1D(GL11.GL_TEXTURE_1D, 0, GL11.GL_RGBA, slider.getWidth(), 0, GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, buff);
		EXTFramebufferObject.glGenerateMipmapEXT(GL11.GL_TEXTURE_1D);
	}
}
 
Example 6
Source File: UI.java    From opsu with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Draws the volume bar on the middle right-hand side of the game container.
 * Only draws if the volume has recently been changed using with {@link #changeVolume(int)}.
 * @param g the graphics context
 */
public static void drawVolume(Graphics g) {
	if (volumeDisplay == -1)
		return;

	int width = container.getWidth(), height = container.getHeight();
	Image img = GameImage.VOLUME.getImage();

	// move image in/out
	float xOffset = 0;
	float ratio = (float) volumeDisplay / VOLUME_DISPLAY_TIME;
	if (ratio <= 0.1f)
		xOffset = img.getWidth() * (1 - (ratio * 10f));
	else if (ratio >= 0.9f)
		xOffset = img.getWidth() * (1 - ((1 - ratio) * 10f));

	img.drawCentered(width - img.getWidth() / 2f + xOffset, height / 2f);
	float barHeight = img.getHeight() * 0.9f;
	float volume = Options.getMasterVolume();
	g.setColor(Color.white);
	g.fillRoundRect(
			width - (img.getWidth() * 0.368f) + xOffset,
			(height / 2f) - (img.getHeight() * 0.47f) + (barHeight * (1 - volume)),
			img.getWidth() * 0.15f, barHeight * volume, 3
	);
}
 
Example 7
Source File: UI.java    From opsu-dance with GNU General Public License v3.0 5 votes vote down vote up
private static int calcTooltipLocationForImage(GameImage gi)
{
	Image img = gi.getImage();
	int w2 = img.getWidth() / 2;
	int h2 = img.getHeight() / 2;
	for (int i = 10; i > 0; i--) {
		float p = i / 10.0f;
		float a = img.getAlphaAt(w2 + (int) (w2 * p), h2 + (int) (h2 * p));
		System.out.printf("%f: %f%n", p, a);
		if (a > .7f) {
			return (int) (gi.getWidth() / 2.0f * p);
		}
	}
	return 0;
}
 
Example 8
Source File: LocatedImage.java    From slick2d-maven with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Create a new located image
 * 
 * @param image The image to be drawn
 * @param x The x location at which the image should be drawn
 * @param y The y location at which the image should be drawn
 */
public LocatedImage(Image image, int x, int y) {
	this.image = image;
	this.x = x;
	this.y = y;
	this.width = image.getWidth();
	this.height = image.getHeight();
}
 
Example 9
Source File: ImageCornerTest.java    From slick2d-maven with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * @see org.newdawn.slick.BasicGame#init(org.newdawn.slick.GameContainer)
 */
public void init(GameContainer container) throws SlickException {
	image = new Image("testdata/logo.png");
	
	width = image.getWidth() / 3;
	height = image.getHeight() / 3;
	
	images = new Image[] {
			image.getSubImage(0, 0, width, height), image.getSubImage(width,0,width,height), image.getSubImage(width*2,0,width,height),
			image.getSubImage(0, height, width, height), image.getSubImage(width,height,width,height), image.getSubImage(width*2,height,width,height),
			image.getSubImage(0, height*2, width, height), image.getSubImage(width,height*2,width,height), image.getSubImage(width*2,height*2,width,height),
	};
	
	images[0].setColor(Image.BOTTOM_RIGHT, 0,1,1,1);
	images[1].setColor(Image.BOTTOM_LEFT, 0,1,1,1);
	images[1].setColor(Image.BOTTOM_RIGHT, 0,1,1,1);
	images[2].setColor(Image.BOTTOM_LEFT, 0,1,1,1);
	images[3].setColor(Image.TOP_RIGHT, 0,1,1,1);
	images[3].setColor(Image.BOTTOM_RIGHT, 0,1,1,1);
	
	images[4].setColor(Image.TOP_RIGHT, 0,1,1,1);
	images[4].setColor(Image.TOP_LEFT, 0,1,1,1);
	images[4].setColor(Image.BOTTOM_LEFT, 0,1,1,1);
	images[4].setColor(Image.BOTTOM_RIGHT, 0,1,1,1);
	images[5].setColor(Image.TOP_LEFT, 0,1,1,1);
	images[5].setColor(Image.BOTTOM_LEFT, 0,1,1,1);
	
	images[6].setColor(Image.TOP_RIGHT, 0,1,1,1);
	images[7].setColor(Image.TOP_RIGHT, 0,1,1,1);
	images[7].setColor(Image.TOP_LEFT, 0,1,1,1);
	images[8].setColor(Image.TOP_LEFT, 0,1,1,1);
}
 
Example 10
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 11
Source File: MenuButton.java    From opsu with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Creates a new button from an Image.
 * @param img the image
 * @param x the center x coordinate
 * @param y the center y coordinate
 */
public MenuButton(Image img, float x, float y) {
	this.img = img;
	this.x = x;
	this.y = y;
	this.xRadius = img.getWidth() / 2f;
	this.yRadius = img.getHeight() / 2f;
}
 
Example 12
Source File: StarFountain.java    From opsu-dance with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Initializes the star fountain.
 * @param containerWidth the container width
 * @param containerHeight the container height
 */
public StarFountain(int containerWidth, int containerHeight) {
	Image img = GameImage.STAR2.getImage();
	float xOffset = containerWidth * 0.125f;
	this.xDirection = containerWidth / 2f - xOffset;
	this.yDirection = -containerHeight * 0.85f;
	this.left = new StarStream(xOffset - img.getWidth() / 2f, containerHeight, 0, yDirection, 0);
	this.right = new StarStream(containerWidth - xOffset - img.getWidth() / 2f, containerHeight, 0, yDirection, 0);
	left.setScaleSpread(1.1f, 0.2f);
	right.setScaleSpread(1.1f, 0.2f);
}
 
Example 13
Source File: GameData.java    From opsu with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Updates displayed ranking elements based on a delta value.
 * @param delta the delta interval since the last call
 * @param mouseX the mouse x coordinate
 * @param mouseY the mouse y coordinate
 */
public void updateRankingDisplays(int delta, int mouseX, int mouseY) {
	// graph tooltip
	Image graphImg = GameImage.RANKING_GRAPH.getImage();
	float graphX = 416 * GameImage.getUIscale();
	float graphY = 688 * GameImage.getUIscale();
	if (isGameplay &&
	    mouseX >= graphX - graphImg.getWidth() / 2f && mouseX <= graphX + graphImg.getWidth() / 2f &&
	    mouseY >= graphY - graphImg.getHeight() / 2f && mouseY <= graphY + graphImg.getHeight() / 2f) {
		if (performanceString == null)
			performanceString = getPerformanceString(hitErrors);
		UI.updateTooltip(delta, performanceString, true);
	}
}
 
Example 14
Source File: GameRanking.java    From opsu-dance with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void revalidate() {
	super.revalidate();

	// buttons
	Image retry = GameImage.PAUSE_RETRY.getImage();
	Image replay = GameImage.PAUSE_REPLAY.getImage();
	replayY = height * 0.985f - replay.getHeight() / 2f;
	retryY = replayY - (replay.getHeight() / 2f) - (retry.getHeight() / 1.975f);
	retryButton = new MenuButton(retry, width - (retry.getWidth() / 2f), retryY);
	replayButton = new MenuButton(replay, width - (replay.getWidth() / 2f), replayY);
	retryButton.setHoverFade();
	replayButton.setHoverFade();
}
 
Example 15
Source File: MainMenu.java    From opsu-dance with GNU General Public License v3.0 5 votes vote down vote up
private void drawMenuButton(
	Image img,
	int x,
	int y,
	int clipxtop,
	int clipxbot,
	Color col)
{
	col.bind();
	final Texture t = img.getTexture();
	t.bind(); 
	
	final int width = img.getWidth();
	final int height = img.getHeight();
	final float twidth = t.getWidth();
	final float theight = t.getHeight();
	y -= height / 2;
	
	final float texXtop = clipxtop > 0 ? (float) clipxtop / width * twidth : 0f;
	final float texXbot = clipxbot > 0 ? (float) clipxbot / width * twidth : 0f;

	GL11.glBegin(SGL.GL_QUADS); 
	GL11.glTexCoord2f(texXtop, 0);
	GL11.glVertex3i(x + clipxtop, y, 0);
	GL11.glTexCoord2f(twidth, 0);
	GL11.glVertex3i(x + width, y, 0);
	GL11.glTexCoord2f(twidth, theight);
	GL11.glVertex3i(x + width, y + height, 0);
	GL11.glTexCoord2f(texXbot, theight);
	GL11.glVertex3i(x + clipxbot, y + height, 0);
	GL11.glEnd(); 
}
 
Example 16
Source File: TGAWriter.java    From slick2d-maven with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/**
 * @see org.newdawn.slick.imageout.ImageWriter#saveImage(org.newdawn.slick.Image, java.lang.String, java.io.OutputStream, boolean)
 */
public void saveImage(Image image, String format, OutputStream output, boolean writeAlpha) throws IOException {
	DataOutputStream out = new DataOutputStream(new BufferedOutputStream(output));

	// ID Length
	out.writeByte((byte) 0);

	// Color Map
	out.writeByte((byte) 0);

	// Image Type
	out.writeByte((byte) 2);

	// Color Map - Ignored
	out.writeShort(flipEndian((short) 0));
	out.writeShort(flipEndian((short) 0));
	out.writeByte((byte) 0);

	// X, Y Offset
	out.writeShort(flipEndian((short) 0));
	out.writeShort(flipEndian((short) 0));

	// Width, Height, Depth
	out.writeShort(flipEndian((short) image.getWidth()));
	out.writeShort(flipEndian((short) image.getHeight()));
	if (writeAlpha) {
		out.writeByte((byte) 32);
		// Image Descriptor (can't be 0 since we're using 32-bit TGAs)
		// needs to not have 0x20 set to indicate it's not a flipped image
		out.writeByte((byte) 1);
	} else {
		out.writeByte((byte) 24);
		// Image Descriptor (must be 0 since we're using 24-bit TGAs)
		// needs to not have 0x20 set to indicate it's not a flipped image
		out.writeByte((byte) 0);
	}
	

	// Write out the image data
	Color c;

	for (int y = image.getHeight()-1; y <= 0; y--) {
		for (int x = 0; x < image.getWidth(); x++) {
			c = image.getColor(x, y);

			out.writeByte((byte) (c.b * 255.0f));
			out.writeByte((byte) (c.g * 255.0f));
			out.writeByte((byte) (c.r * 255.0f));
			if (writeAlpha) {
				out.writeByte((byte) (c.a * 255.0f));
			}
		}
	}

	out.close();
}
 
Example 17
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 18
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 19
Source File: DownloadNode.java    From opsu with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Draws the download result as a rectangular button.
 * @param g the graphics context
 * @param position the index (to offset the button from the topmost button)
 * @param hover true if the mouse is hovering over this button
 * @param focus true if the button is focused
 * @param previewing true if the beatmap is currently being previewed
 */
public void drawResult(Graphics g, float position, boolean hover, boolean focus, boolean previewing) {
	float textX = buttonBaseX + buttonWidth * 0.001f;
	float edgeX = buttonBaseX + buttonWidth * 0.985f;
	float y = buttonBaseY + position;
	float marginY = buttonHeight * 0.04f;
	Download dl = DownloadList.get().getDownload(beatmapSetID);

	// rectangle outline
	g.setColor((focus) ? Colors.BLACK_BG_FOCUS : (hover) ? Colors.BLACK_BG_HOVER : Colors.BLACK_BG_NORMAL);
	g.fillRect(buttonBaseX, y, buttonWidth, buttonHeight);

	// map is already loaded
	if (BeatmapSetList.get().containsBeatmapSetID(beatmapSetID)) {
		g.setColor(Colors.BLUE_BUTTON);
		g.fillRect(buttonBaseX, y, buttonWidth, buttonHeight);
	}

	// download progress
	if (dl != null) {
		float progress = dl.getProgress();
		if (progress > 0f) {
			g.setColor(Colors.GREEN);
			g.fillRect(buttonBaseX, y, buttonWidth * progress / 100f, buttonHeight);
		}
	}

	// preview button
	Image img = (previewing) ? GameImage.MUSIC_PAUSE.getImage() : GameImage.MUSIC_PLAY.getImage();
	img.drawCentered(textX + img.getWidth() / 2, y + buttonHeight / 2f);
	textX += img.getWidth() + buttonWidth * 0.001f;

	// text
	// TODO: if the title/artist line is too long, shorten it (e.g. add "...") instead of just clipping
	if (Options.useUnicodeMetadata()) {  // load glyphs
		Fonts.loadGlyphs(Fonts.BOLD, getTitle());
		Fonts.loadGlyphs(Fonts.BOLD, getArtist());
	}
	// TODO can't set clip again or else old clip will be cleared
	//g.setClip((int) textX, (int) (y + marginY), (int) (edgeX - textX - Fonts.DEFAULT.getWidth(creator)), Fonts.BOLD.getLineHeight());
	Fonts.BOLD.drawString(
			textX, y + marginY,
			String.format("%s - %s%s", getArtist(), getTitle(),
					(dl != null) ? String.format(" [%s]", dl.getStatus().getName()) : ""), Color.white);
	//g.clearClip();
	Fonts.DEFAULT.drawString(
			textX, y + marginY + Fonts.BOLD.getLineHeight(),
			String.format("Last updated: %s", date), Color.white);
	Fonts.DEFAULT.drawString(
			edgeX - Fonts.DEFAULT.getWidth(creator), y + marginY,
			creator, Color.white);
}
 
Example 20
Source File: MainMenu.java    From opsu-dance with GNU General Public License v3.0 4 votes vote down vote up
@Override
protected void revalidate() {

	this.barHeight = height * 0.1125f;

	this.textMarginX = (int) (width * 0.015f);
	this.textTopMarginY = (int) (height * 0.01f);
	this.textLineHeight = (int) (Fonts.MEDIUM.getLineHeight() * 0.925f);

	// initialize music buttons
	final int musicSize = (int) (this.textLineHeight * 0.8f);
	final float musicScale = (float) musicSize / MUSIC_STOP.getWidth();
	final int musicSpacing = (int) (musicSize * 0.8f) + musicSize; // (center to center)
	int x = width - this.textMarginX - musicSize / 2;
	int y = this.textLineHeight * 2 + this.textLineHeight / 2;
	this.musicNext = new MenuButton(MUSIC_NEXT.getScaledImage(musicScale), x, y);
	x -= musicSpacing;
	this.musicStop = new MenuButton(MUSIC_STOP.getScaledImage(musicScale), x, y);
	x -= musicSpacing;
	this.musicPause = new MenuButton(MUSIC_PAUSE.getScaledImage(musicScale), x, y);
	x -= musicSpacing;
	this.musicPlay = new MenuButton(MUSIC_PLAY.getScaledImage(musicScale), x, y);
	x -= musicSpacing;
	this.musicPrev = new MenuButton(MUSIC_PREVIOUS.getScaledImage(musicScale), x, y);
	this.musicButtons[0] = this.musicPrev;
	this.musicButtons[1] = this.musicPlay;
	this.musicButtons[2] = this.musicPause;
	this.musicButtons[3] = this.musicStop;
	this.musicButtons[4] = this.musicNext;
	for (MenuButton b : this.musicButtons) {
		b.setHoverExpand(1.15f);
	}

	// initialize music position bar location
	this.musicBarX = x - musicSize / 2;
	this.musicBarY = y + musicSize;
	this.musicBarWidth = musicSize + musicSpacing * 4;
	this.musicBarHeight = (int) (musicSize * 0.3f);

	// initialize repository button (only if a webpage can be opened)
	if (Desktop.isDesktopSupported() && Desktop.getDesktop().isSupported(BROWSE)) {
		final Image repoImg = GameImage.REPOSITORY.getImage();
		float repoX = this.textMarginX + repoImg.getWidth() / 2;
		final float repoY = height - this.barHeight / 2;
		repoButton = new MenuButton(repoImg, repoX, repoY);
		repoButton.setHoverAnimationDuration(100);
		repoButton.setHoverExpand(1.1f);
		repoX += repoImg.getWidth() * 1.5f;
		danceRepoButton = new MenuButton(repoImg, repoX, repoY);
		danceRepoButton.setHoverAnimationDuration(100);
		danceRepoButton.setHoverExpand(1.1f);
	}

	// initialize update buttons
	final float updateY = height * 17 / 18f;
	final Image downloadImg = GameImage.DOWNLOAD.getImage();
	updateButton = new MenuButton(downloadImg, width2, updateY);
	updateButton.setHoverAnimationDuration(400);
	updateButton.setHoverAnimationEquation(AnimationEquation.IN_OUT_QUAD);
	updateButton.setHoverExpand(1.1f);
	final Image updateImg = GameImage.UPDATE.getImage();
	restartButton = new MenuButton(updateImg, width2, updateY);
	restartButton.setHoverAnimationDuration(2000);
	restartButton.setHoverAnimationEquation(AnimationEquation.LINEAR);
	restartButton.setHoverRotate(360);

	// initialize star fountain
	starFountain = new StarFountain(width, height);

	// logo & buttons
	this.logo = new ImagePosition(MENU_LOGO.getImage());
	this.logoPositionOffsetX = 0.35f * MENU_LOGO.getHeight();
	this.logoPosition.setValues(0,  logoPositionOffsetX);
	this.buttonsX = width2 - MENU_OPTIONS.getWidth() / 2;
	this.buttonPositions[0] = new ImagePosition(MENU_PLAY.getImage());
	this.buttonPositions[1] = new ImagePosition(MENU_OPTIONS.getImage());
	this.buttonPositions[2] = new ImagePosition(MENU_EXIT.getImage());
	final int basey = height2 - MENU_OPTIONS.getHeight() / 2;
	final float yoffset = MENU_LOGO.getHeight() * 0.196378f;
	for (int i = 0; i < 3; i++) {
		this.buttonPositions[i].width = MENU_OPTIONS.getWidth();
		this.buttonPositions[i].y = (int) (basey + (i - 1f) * yoffset);
		this.buttonPositions[i].height = MENU_OPTIONS.getHeight();
	}
}