Added method for drawing tooltips.

Currently used for game mods and the music buttons in the main menu.

Signed-off-by: Jeffrey Han <itdelatrisu@gmail.com>
This commit is contained in:
Jeffrey Han 2015-02-26 02:06:34 -05:00
parent 148400b75d
commit ad5277882e
4 changed files with 86 additions and 10 deletions

View File

@ -50,9 +50,9 @@ public enum GameMod {
FLASHLIGHT (Category.HARD, 4, GameImage.MOD_FLASHLIGHT, "FL", 1024, Input.KEY_G, 1.12f, false, FLASHLIGHT (Category.HARD, 4, GameImage.MOD_FLASHLIGHT, "FL", 1024, Input.KEY_G, 1.12f, false,
"Restricted view area."), "Restricted view area."),
RELAX (Category.SPECIAL, 0, GameImage.MOD_RELAX, "RL", 128, Input.KEY_Z, 0f, false, RELAX (Category.SPECIAL, 0, GameImage.MOD_RELAX, "RL", 128, Input.KEY_Z, 0f, false,
"You don't need to click. Give your clicking/tapping finger a break from the heat of things. **UNRANKED**"), "You don't need to click.\nGive your clicking/tapping finger a break from the heat of things.\n**UNRANKED**"),
AUTOPILOT (Category.SPECIAL, 1, GameImage.MOD_AUTOPILOT, "AP", 8192, Input.KEY_X, 0f, false, AUTOPILOT (Category.SPECIAL, 1, GameImage.MOD_AUTOPILOT, "AP", 8192, Input.KEY_X, 0f, false,
"Automatic cursor movement - just follow the rhythm. **UNRANKED**"), "Automatic cursor movement - just follow the rhythm.\n**UNRANKED**"),
SPUN_OUT (Category.SPECIAL, 2, GameImage.MOD_SPUN_OUT, "SO", 4096, Input.KEY_V, 0.9f, SPUN_OUT (Category.SPECIAL, 2, GameImage.MOD_SPUN_OUT, "SO", 4096, Input.KEY_V, 0.9f,
"Spinners will be automatically completed."), "Spinners will be automatically completed."),
AUTO (Category.SPECIAL, 3, GameImage.MOD_AUTO, "", 2048, Input.KEY_B, 1f, AUTO (Category.SPECIAL, 3, GameImage.MOD_AUTO, "", 2048, Input.KEY_B, 1f,
@ -285,6 +285,12 @@ public enum GameMod {
*/ */
public String getDescription() { return description; } public String getDescription() { return description; }
/**
* Returns whether or not the mod is implemented.
* @return true if implemented
*/
public boolean isImplemented() { return implemented; }
/** /**
* Toggles the active status of the mod. * Toggles the active status of the mod.
* @param checkInverse if true, perform checks for mutual exclusivity * @param checkInverse if true, perform checks for mutual exclusivity

View File

@ -646,6 +646,52 @@ public class Utils {
g.fillRect(scrollbarX, unitBaseY + offsetY, scrollbarWidth, scrollbarHeight); g.fillRect(scrollbarX, unitBaseY + offsetY, scrollbarWidth, scrollbarHeight);
} }
/**
* Draws a tooltip near the current mouse coordinates, bounded by the
* container dimensions.
* @param g the graphics context
* @param text the tooltip text
* @param newlines whether to check for line breaks ('\n')
*/
public static void drawTooltip(Graphics g, String text, boolean newlines) {
int containerWidth = container.getWidth(), containerHeight = container.getHeight();
int margin = containerWidth / 100, textMarginX = 2;
int offset = GameImage.CURSOR_MIDDLE.getImage().getWidth() / 2;
int lineHeight = FONT_SMALL.getLineHeight();
int textWidth = textMarginX * 2, textHeight = lineHeight;
if (newlines) {
String[] lines = text.split("\\n");
int maxWidth = FONT_SMALL.getWidth(lines[0]);
for (int i = 1; i < lines.length; i++) {
int w = FONT_SMALL.getWidth(lines[i]);
if (w > maxWidth)
maxWidth = w;
}
textWidth += maxWidth;
textHeight += lineHeight * (lines.length - 1);
} else
textWidth += FONT_SMALL.getWidth(text);
// get drawing coordinates
int x = input.getMouseX() + offset, y = input.getMouseY() + offset;
if (x + textWidth > containerWidth - margin)
x = containerWidth - margin - textWidth;
else if (x < margin)
x = margin;
if (y + textHeight > containerHeight - margin)
y = containerHeight - margin - textHeight;
else if (y < margin)
y = margin;
// draw tooltip text inside a filled rectangle
g.setColor(Color.black);
g.fillRect(x, y, textWidth, textHeight);
g.setColor(Color.darkGray);
g.setLineWidth(1);
g.drawRect(x, y, textWidth, textHeight);
FONT_SMALL.drawString(x + textMarginX, y, text, Color.white);
}
/** /**
* Takes a screenshot. * Takes a screenshot.
* @author http://wiki.lwjgl.org/index.php?title=Taking_Screen_Shots * @author http://wiki.lwjgl.org/index.php?title=Taking_Screen_Shots

View File

@ -150,8 +150,6 @@ public class ButtonMenu extends BasicGameState {
@Override @Override
public void draw(GameContainer container, StateBasedGame game, Graphics g) { public void draw(GameContainer container, StateBasedGame game, Graphics g) {
super.draw(container, game, g);
int width = container.getWidth(); int width = container.getWidth();
int height = container.getHeight(); int height = container.getHeight();
@ -171,9 +169,21 @@ public class ButtonMenu extends BasicGameState {
category.getName(), category.getColor()); category.getName(), category.getColor());
} }
// buttons (TODO: draw descriptions when hovering) // buttons
for (GameMod mod : GameMod.values()) Input input = container.getInput();
int mouseX = input.getMouseX(), mouseY = input.getMouseY();
GameMod hoverMod = null;
for (GameMod mod : GameMod.values()) {
mod.draw(); mod.draw();
if (hoverMod == null && mod.contains(mouseX, mouseY))
hoverMod = mod;
}
super.draw(container, game, g);
// tooltips
if (hoverMod != null && hoverMod.isImplemented())
Utils.drawTooltip(g, hoverMod.getDescription(), true);
} }
@Override @Override
@ -280,6 +290,10 @@ public class ButtonMenu extends BasicGameState {
// draw buttons // draw buttons
for (int i = 0; i < buttons.length; i++) for (int i = 0; i < buttons.length; i++)
menuButtons[i].draw(buttons[i].getColor()); menuButtons[i].draw(buttons[i].getColor());
Utils.drawVolume(g);
Utils.drawFPS();
Utils.drawCursor();
} }
/** /**
@ -564,9 +578,6 @@ public class ButtonMenu extends BasicGameState {
g.setBackground(Color.black); g.setBackground(Color.black);
if (menuState != null) if (menuState != null)
menuState.draw(container, game, g); menuState.draw(container, game, g);
Utils.drawVolume(g);
Utils.drawFPS();
Utils.drawCursor();
} }
@Override @Override

View File

@ -259,6 +259,16 @@ public class MainMenu extends BasicGameState {
Utils.drawVolume(g); Utils.drawVolume(g);
Utils.drawFPS(); Utils.drawFPS();
Utils.drawCursor(); Utils.drawCursor();
// tooltips
if (musicPositionBarContains(mouseX, mouseY))
Utils.drawTooltip(g, "Click to seek to a specific point in the song.", false);
else if (musicPlay.contains(mouseX, mouseY))
Utils.drawTooltip(g, (MusicController.isPlaying()) ? "Pause" : "Play", false);
else if (musicNext.contains(mouseX, mouseY))
Utils.drawTooltip(g, "Next track", false);
else if (musicPrevious.contains(mouseX, mouseY))
Utils.drawTooltip(g, "Previous track", false);
} }
@Override @Override
@ -273,10 +283,13 @@ public class MainMenu extends BasicGameState {
if (repoButton != null) if (repoButton != null)
repoButton.hoverUpdate(delta, mouseX, mouseY); repoButton.hoverUpdate(delta, mouseX, mouseY);
downloadsButton.hoverUpdate(delta, mouseX, mouseY); downloadsButton.hoverUpdate(delta, mouseX, mouseY);
// ensure only one button is in hover state at once
if (musicPositionBarContains(mouseX, mouseY))
mouseX = mouseY = -1;
musicPlay.hoverUpdate(delta, mouseX, mouseY); musicPlay.hoverUpdate(delta, mouseX, mouseY);
musicPause.hoverUpdate(delta, mouseX, mouseY); musicPause.hoverUpdate(delta, mouseX, mouseY);
if (musicPlay.contains(mouseX, mouseY)) if (musicPlay.contains(mouseX, mouseY))
mouseX = mouseY = -1; // ensure only one button is in hover state at once mouseX = mouseY = -1;
musicNext.hoverUpdate(delta, mouseX, mouseY); musicNext.hoverUpdate(delta, mouseX, mouseY);
musicPrevious.hoverUpdate(delta, mouseX, mouseY); musicPrevious.hoverUpdate(delta, mouseX, mouseY);