Merge pull request #180 from tylerthardy/infoboxTooltips

Add tooltips for InfoBoxes
This commit is contained in:
Adam
2017-10-16 21:12:55 -04:00
committed by GitHub
5 changed files with 72 additions and 2 deletions

View File

@@ -27,11 +27,13 @@ package net.runelite.client.ui.overlay;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.Rectangle;
public abstract class Overlay
{
private OverlayPosition position; // where to draw it
private OverlayPriority priority; // if multiple overlays exist in the same position, who wins
private Rectangle bounds; //screen bounds of overlay after OverlayRenderer decides location
public Overlay(OverlayPosition position)
{
@@ -65,4 +67,14 @@ public abstract class Overlay
}
public abstract Dimension render(Graphics2D graphics);
public Rectangle getBounds()
{
return bounds;
}
public void storeBounds(Rectangle bounds)
{
this.bounds = bounds;
}
}

View File

@@ -22,11 +22,11 @@
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package net.runelite.client.ui.overlay;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.List;
@@ -61,6 +61,9 @@ public class TopDownRendererLeft implements Renderer
if (dimension == null)
continue;
Rectangle bounds = new Rectangle(BORDER_LEFT, y, (int) dimension.getWidth(), (int) dimension.getHeight());
overlay.storeBounds(bounds);
y += dimension.getHeight() + PADDING;
}
}

View File

@@ -26,6 +26,7 @@ package net.runelite.client.ui.overlay;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.List;
@@ -74,6 +75,9 @@ public class TopDownRendererRight implements Renderer
graphics.drawImage(image, clientWidth - BORDER_RIGHT - (int) dimension.getWidth(), y, null);
graphics.dispose();
Rectangle bounds = new Rectangle(clientWidth - BORDER_RIGHT - (int) dimension.getWidth(), y, (int) dimension.getWidth(), (int) dimension.getHeight());
overlay.storeBounds(bounds);
y += dimension.getHeight() + PADDING;
}
}

View File

@@ -31,6 +31,8 @@ public abstract class InfoBox
{
private final BufferedImage image;
private String tooltip;
public InfoBox(BufferedImage image)
{
this.image = image;
@@ -54,4 +56,14 @@ public abstract class InfoBox
{
return false;
}
public String getTooltip()
{
return tooltip;
}
public void setTooltip(String tooltip)
{
this.tooltip = tooltip;
}
}

View File

@@ -29,10 +29,12 @@ import java.awt.Color;
import java.awt.Dimension;
import java.awt.FontMetrics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.util.List;
import net.runelite.api.Client;
import net.runelite.api.GameState;
import net.runelite.api.Point;
import net.runelite.client.RuneLite;
import net.runelite.client.ui.overlay.Overlay;
import net.runelite.client.ui.overlay.OverlayPosition;
@@ -69,9 +71,14 @@ public class InfoBoxOverlay extends Overlay
FontMetrics metrics = graphics.getFontMetrics();
int width = infoBoxes.size() * BOXSIZE;
int width = infoBoxes.size() * (BOXSIZE + SEPARATOR);
Point mouse = client.getMouseCanvasPosition();
int mouseX = mouse.getX();
int mouseY = mouse.getY();
int x = 0;
Rectangle overlayBounds = this.getBounds();
for (InfoBox box : infoBoxes)
{
if (!box.render())
@@ -101,6 +108,38 @@ public class InfoBoxOverlay extends Overlay
graphics.setColor(color);
graphics.drawString(str, x + ((BOXSIZE - metrics.stringWidth(str)) / 2), BOXSIZE - SEPARATOR);
if (overlayBounds == null)
{
continue;
}
String tooltip = box.getTooltip();
if (tooltip == null || tooltip.isEmpty())
{
continue;
}
Rectangle bounds = new Rectangle((int) overlayBounds.getX() + x, (int) overlayBounds.getY(), BOXSIZE, BOXSIZE);
if (bounds.contains(mouse.getX(), mouse.getY()))
{
int tooltipWidth = metrics.stringWidth(tooltip);
int height = metrics.getHeight();
Color gray = new Color(Color.darkGray.getRed(), Color.darkGray.getGreen(), Color.darkGray.getBlue(), 190);
graphics.setColor(gray);
// Draws the background rect
graphics.fillRect(mouseX, mouseY - (height / 2), tooltipWidth + 6, height);
// Draws the outline of the rect
graphics.setColor(Color.BLACK);
graphics.drawRect(mouseX, mouseY - (height / 2), tooltipWidth + 6, height);
// Tooltip text
graphics.setColor(Color.WHITE);
graphics.drawString(tooltip, mouseX + 3, mouseY + 5);
}
x += BOXSIZE + SEPARATOR;
}