Add tooltips to gui elements

This commit is contained in:
johni0702
2015-07-09 16:19:06 +02:00
parent d4e04feaeb
commit 9e886b6171
9 changed files with 311 additions and 2 deletions

View File

@@ -47,6 +47,7 @@ public abstract class AbstractComposedGuiElement<T extends AbstractComposedGuiEl
return (C) Proxy.newProxyInstance(getClass().getClassLoader(), new Class[]{ofType}, new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
boolean isGetter = method.getName().startsWith("get");
Object handled = null;
for (final GuiElement element : getChildren()) {
try {
@@ -55,8 +56,14 @@ public abstract class AbstractComposedGuiElement<T extends AbstractComposedGuiEl
} else if (ofType.isInstance(element)) {
handled = method.invoke(element, args);
}
if (Boolean.TRUE.equals(handled)) {
break;
if (handled != null) {
if (handled instanceof Boolean) {
if (Boolean.TRUE.equals(handled)) {
break;
}
} else if (isGetter) {
return handled;
}
}
} catch (Exception e) {
CrashReport crash = CrashReport.makeCrashReport(e, "Calling Gui method");

View File

@@ -22,10 +22,12 @@
package de.johni0702.minecraft.gui.element;
import de.johni0702.minecraft.gui.RenderInfo;
import de.johni0702.minecraft.gui.container.GuiContainer;
import lombok.Getter;
import net.minecraft.client.Minecraft;
import org.lwjgl.util.Dimension;
import org.lwjgl.util.Point;
import org.lwjgl.util.ReadableDimension;
public abstract class AbstractGuiElement<T extends AbstractGuiElement<T>> implements GuiElement<T> {
@@ -35,6 +37,8 @@ public abstract class AbstractGuiElement<T extends AbstractGuiElement<T>> implem
@Getter
private GuiContainer container;
private GuiElement tooltip;
@Getter
private boolean enabled = true;
@@ -65,6 +69,30 @@ public abstract class AbstractGuiElement<T extends AbstractGuiElement<T>> implem
return setEnabled(false);
}
@Override
public GuiElement getTooltip(RenderInfo renderInfo) {
if (tooltip != null) {
Point mouse = new Point(renderInfo.mouseX, renderInfo.mouseY);
if (container != null) {
container.convertFor(this, mouse);
}
ReadableDimension size = getMinSize();
if (mouse.getX() > 0
&& mouse.getY() > 0
&& mouse.getX() < size.getWidth()
&& mouse.getY() < size.getHeight()) {
return tooltip;
}
}
return null;
}
@Override
public T setTooltip(GuiElement tooltip) {
this.tooltip = tooltip;
return getThis();
}
@Override
public T setContainer(GuiContainer container) {
this.container = container;

View File

@@ -0,0 +1,104 @@
/*
* Copyright (c) 2015 johni0702
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package de.johni0702.minecraft.gui.element;
import de.johni0702.minecraft.gui.GuiRenderer;
import de.johni0702.minecraft.gui.RenderInfo;
import eu.crushedpixel.replaymod.utils.StringUtils;
import lombok.Getter;
import net.minecraft.client.gui.FontRenderer;
import net.minecraft.client.resources.I18n;
import org.lwjgl.util.Color;
import org.lwjgl.util.Dimension;
import org.lwjgl.util.ReadableColor;
import org.lwjgl.util.ReadableDimension;
public abstract class AbstractGuiTooltip<T extends AbstractGuiTooltip<T>> extends AbstractGuiElement<T> {
private static final int LINE_SPACING = 3;
private static final ReadableColor BACKGROUND_COLOR = new Color(16, 0, 16, 240);
private static final ReadableColor BORDER_LIGHT = new Color(80, 0, 255, 80);
private static final ReadableColor BORDER_DARK = new Color(40, 0, 127, 80);
@Getter
private String[] text = {};
@Getter
private ReadableColor color = ReadableColor.WHITE;
@Override
public void draw(GuiRenderer renderer, ReadableDimension size, RenderInfo renderInfo) {
int width = size.getWidth();
int height = size.getHeight();
// Draw background
renderer.drawRect(1, 0, width - 2, height, BACKGROUND_COLOR); // Top to bottom
renderer.drawRect(0, 1, 1, height - 2, BACKGROUND_COLOR); // Left pixel row
renderer.drawRect(width - 1, 1, 1, height - 2, BACKGROUND_COLOR); // Right pixel row
// Draw the border, it gets darker from top to bottom
renderer.drawRect(1, 1, width - 2, 1, BORDER_LIGHT); // Top border
renderer.drawRect(1, height - 2, width - 2, 1, BORDER_DARK); // Bottom border
renderer.drawRect(1, 2, 1, height - 4, BORDER_LIGHT, BORDER_LIGHT, BORDER_DARK, BORDER_DARK); // Left border
renderer.drawRect(width - 2, 2, 1, height - 4, BORDER_LIGHT, BORDER_LIGHT, BORDER_DARK, BORDER_DARK); // Right border
FontRenderer fontRenderer = getMinecraft().fontRendererObj;
int y = LINE_SPACING + 1;
for (String line : text) {
renderer.drawString(LINE_SPACING + 1, y, color, line);
y += fontRenderer.FONT_HEIGHT + LINE_SPACING;
}
}
@Override
public ReadableDimension getMinSize() {
FontRenderer fontRenderer = getMinecraft().fontRendererObj;
int height = 1 + LINE_SPACING + text.length * (fontRenderer.FONT_HEIGHT + LINE_SPACING);
int width = 0;
for (String line : text) {
int w = fontRenderer.getStringWidth(line);
if (w > width) {
width = w;
}
}
width+=4 * 2;
return new Dimension(width, height);
}
public T setText(String[]text) {
this.text = text;
return getThis();
}
public T setText(String text) {
return setText(StringUtils.splitStringInMultipleRows(text, 250));
}
public T setI18nText(String text, Object... args) {
return setText(I18n.format(text, args));
}
public T setColor(ReadableColor color) {
this.color = color;
return getThis();
}
}

View File

@@ -49,4 +49,7 @@ public interface GuiElement<T extends GuiElement<T>> {
T setEnabled();
T setDisabled();
GuiElement getTooltip(RenderInfo renderInfo);
T setTooltip(GuiElement tooltip);
}

View File

@@ -0,0 +1,30 @@
/*
* Copyright (c) 2015 johni0702
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package de.johni0702.minecraft.gui.element;
public class GuiTooltip extends AbstractGuiTooltip<GuiTooltip> {
@Override
protected GuiTooltip getThis() {
return this;
}
}