Skip to content

Commit

Permalink
Fix link mouse hover/click positioning.
Browse files Browse the repository at this point in the history
  • Loading branch information
gigaherz committed Jun 2, 2018
1 parent e0ce711 commit 59a17f4
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 84 deletions.
6 changes: 3 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ repositories {
apply plugin: 'net.minecraftforge.gradle.forge'
apply plugin: 'maven-publish'

version = "2.0.1"
version = "2.5.0"
group = "gigaherz.guidebook" // http://maven.apache.org/guides/mini/guide-naming-conventions.html
archivesBaseName = "Guidebook-1.12.2"

minecraft {
version = "1.12.2-14.23.2.2619"
version = "1.12.2-14.23.4.2707"
runDir = "run"

mappings = "snapshot_20180223"
mappings = "snapshot_20180601"

replace "@VERSION@", project.version
replaceIn "GuidebookMod.java"
Expand Down
23 changes: 8 additions & 15 deletions src/main/java/gigaherz/guidebook/guidebook/BookDocument.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,7 @@
import gigaherz.guidebook.GuidebookMod;
import gigaherz.guidebook.guidebook.conditions.ConditionManager;
import gigaherz.guidebook.guidebook.conditions.IDisplayCondition;
import gigaherz.guidebook.guidebook.drawing.Point;
import gigaherz.guidebook.guidebook.drawing.Rect;
import gigaherz.guidebook.guidebook.drawing.VisualElement;
import gigaherz.guidebook.guidebook.drawing.VisualPage;
import gigaherz.guidebook.guidebook.drawing.*;
import gigaherz.guidebook.guidebook.elements.*;
import gigaherz.guidebook.guidebook.templates.TemplateDefinition;
import gigaherz.guidebook.guidebook.templates.TemplateElement;
Expand Down Expand Up @@ -670,15 +667,15 @@ public class PageData

public final List<Element> elements = Lists.newArrayList();

public List<VisualPage> reflow(Rect leftBounds, Rect rightBounds, int pageNumber)
public List<VisualPage> reflow(Size pageSize, int pageNumber)
{
VisualPage page = new VisualPage();
Rect pageBounds = new Rect(new Point(), pageSize);

Rect pageBounds = (pageNumber & 1) == 0 ? leftBounds : rightBounds;
int top = pageBounds.position.y;
int top = 0;
for(Element element : elements)
{
top = element.reflow(page.children, getRendering(), new Rect(new Point(pageBounds.position.x, top), pageBounds.size), pageBounds);
top = element.reflow(page.children, getRendering(), new Rect(new Point(0, top), pageSize), pageBounds);
}

return Collections.singletonList(page);
Expand Down Expand Up @@ -707,12 +704,12 @@ public List<VisualPage> reflow(Rect leftBounds, Rect rightBounds, int pageNumber
public class PageGroup extends PageData
{
@Override
public List<VisualPage> reflow(Rect leftBounds, Rect rightBounds, int pageNumber)
public List<VisualPage> reflow(Size pageSize, int pageNumber)
{
List<VisualPage> pages = Lists.newArrayList();

VisualPage page = new VisualPage();
Rect pageBounds = (pageNumber & 1) == 0 ? leftBounds : rightBounds;
Rect pageBounds = new Rect(new Point(0,0), pageSize);

int top = pageBounds.position.y;
for (Element element : elements)
Expand All @@ -733,9 +730,7 @@ public List<VisualPage> reflow(Rect leftBounds, Rect rightBounds, int pageNumber
{
VisualPage page2 = new VisualPage();

Rect pageBounds2;
int offsetY = 0;
int offsetX = 0;
for (VisualElement child : page.children)
{
int cpy = child.position.y + offsetY;
Expand All @@ -746,13 +741,11 @@ public List<VisualPage> reflow(Rect leftBounds, Rect rightBounds, int pageNumber
page2 = new VisualPage();

pageNumber++;
pageBounds2 = (pageNumber & 1) == 0 ? leftBounds : rightBounds;
offsetY = pageBounds.position.y - child.position.y;
offsetX = pageBounds2.position.x - pageBounds.position.x;
}

child.position = new Point(
child.position.x + offsetX,
child.position.x,
child.position.y + offsetY);
page2.children.add(child);

Expand Down
121 changes: 56 additions & 65 deletions src/main/java/gigaherz/guidebook/guidebook/client/BookRendering.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.FontRenderer;
import net.minecraft.client.gui.Gui;
import net.minecraft.client.gui.ScaledResolution;
import net.minecraft.client.renderer.GlStateManager;
import net.minecraft.client.renderer.RenderHelper;
import net.minecraft.item.ItemStack;
Expand All @@ -29,10 +28,13 @@ public class BookRendering implements IBookGraphics
public static final int DEFAULT_BOOK_HEIGHT = 198;
public static final int DEFAULT_INNER_MARGIN = 22;
public static final int DEFAULT_OUTER_MARGIN = 10;
public static final int DEFAULT_BOTTOM_MARGIN = 8;
public static final int DEFAULT_VERTICAL_MARGIN = 18;

private BookDocument book;

double scaledWidthD;
double scaledHeightD;
int scaledWidth;
int scaledHeight;

Expand All @@ -44,6 +46,7 @@ public class BookRendering implements IBookGraphics
private int innerMargin;
private int outerMargin;
private int verticalMargin;
private int bottomMargin;
private int pageWidth = bookWidth / 2 - innerMargin - outerMargin;
private int pageHeight = bookHeight - verticalMargin;

Expand Down Expand Up @@ -87,6 +90,8 @@ public void computeScaledResolution2(Minecraft minecraftClient, float scaleFacto
this.scaledWidth = minecraftClient.displayWidth;
this.scaledHeight = minecraftClient.displayHeight;
int scaleFactor = 1;
double w = (DEFAULT_BOOK_WIDTH * 1.1) / scaleFactorCoef;
double h = (DEFAULT_BOOK_HEIGHT * 1.1) / scaleFactorCoef;
boolean flag = minecraftClient.isUnicode();
int i = GuidebookMod.bookGUIScale < 0 ? minecraftClient.gameSettings.guiScale : GuidebookMod.bookGUIScale;

Expand All @@ -95,20 +100,18 @@ public void computeScaledResolution2(Minecraft minecraftClient, float scaleFacto
i = 1000;
}

while (scaleFactor < i && this.scaledWidth / (scaleFactor + 1) >= 320 && this.scaledHeight / (scaleFactor + 1) >= 240)
while (scaleFactor < i && this.scaledWidth / (scaleFactor + 1) >= w && this.scaledHeight / (scaleFactor + 1) >= h)
{
++scaleFactor;
}

scaleFactor = MathHelper.floor(Math.max(1, scaleFactor * scaleFactorCoef));

if (flag && scaleFactor % 2 != 0 && scaleFactor != 1)
if (flag && scaleFactor % 2 != 0 && scaleFactor > 1)
{
--scaleFactor;
}

double scaledWidthD = (double) this.scaledWidth / (double) scaleFactor;
double scaledHeightD = (double) this.scaledHeight / (double) scaleFactor;
this.scaledWidthD = (double) minecraftClient.displayWidth / (double) scaleFactor;
this.scaledHeightD = (double) minecraftClient.displayHeight / (double) scaleFactor;
this.scaledWidth = MathHelper.ceil(scaledWidthD);
this.scaledHeight = MathHelper.ceil(scaledHeightD);
}
Expand All @@ -120,35 +123,35 @@ public void setScalingFactor()

if (MathHelper.epsilonEquals(fontSize, 1.0f))
{
this.scaledWidth = gui.width;
this.scaledHeight = gui.height;

this.hasScale = false;
this.scalingFactor = 1.0f;
this.scaledWidth = gui.width;
this.scaledHeight = gui.height;

this.bookWidth = DEFAULT_BOOK_WIDTH;
this.bookHeight = DEFAULT_BOOK_HEIGHT;
this.innerMargin = DEFAULT_INNER_MARGIN;
this.outerMargin = DEFAULT_OUTER_MARGIN;
this.verticalMargin = DEFAULT_VERTICAL_MARGIN;
this.bottomMargin = DEFAULT_BOTTOM_MARGIN;
}
else
{
ScaledResolution sr = new ScaledResolution(mc);
computeScaledResolution2(mc, fontSize);

this.hasScale = true;
this.scalingFactor = Math.min(sr.getScaledWidth() / (float) scaledWidth, sr.getScaledHeight() / (float) scaledHeight);
this.scalingFactor = Math.min(gui.width / (float) scaledWidth, gui.height / (float) scaledHeight);

this.bookWidth = (int) (DEFAULT_BOOK_WIDTH / fontSize);
this.bookHeight = (int) (DEFAULT_BOOK_HEIGHT / fontSize);
this.innerMargin = (int) (DEFAULT_INNER_MARGIN / fontSize);
this.outerMargin = (int) (DEFAULT_OUTER_MARGIN / fontSize);
this.verticalMargin = (int) (DEFAULT_VERTICAL_MARGIN / fontSize);
this.bottomMargin = (int) (DEFAULT_BOTTOM_MARGIN / fontSize);
}

this.pageWidth = this.bookWidth / 2 - this.innerMargin - this.outerMargin;
this.pageHeight = this.bookHeight - this.verticalMargin;
this.pageHeight = this.bookHeight - this.verticalMargin - this.bottomMargin;
}

@Override
Expand Down Expand Up @@ -311,14 +314,14 @@ public boolean mouseClicked(int mouseButton)

final VisualPage pgLeft = ch.pages.get(currentPair * 2);

if (mouseClickPage(mouseX, mouseY, pgLeft))
if (mouseClickPage(mouseX, mouseY, pgLeft, true))
return true;

if (currentPair * 2 + 1 < ch.pages.size())
{
final VisualPage pgRight = ch.pages.get(currentPair * 2 + 1);

if (mouseClickPage(mouseX, mouseY, pgRight))
if (mouseClickPage(mouseX, mouseY, pgRight, false))
return true;
}
}
Expand All @@ -339,14 +342,13 @@ private VisualChapter getVisualChapter(int chapter)

BookDocument.SectionData bc = book.getChapter(chapters.size());

Rect rl = getPageBounds(true);
Rect rr = getPageBounds(false);
Size pageSize = new Size(pageWidth, pageHeight);
for(BookDocument.PageData section : bc.sections)
{
if(!Strings.isNullOrEmpty(section.id))
ch.pagesByName.put(section.id, ch.pages.size());

ch.pages.addAll(section.reflow(rl,rr,ch.pages.size()));
ch.pages.addAll(section.reflow(pageSize,ch.pages.size()));
}

ch.totalPairs = (ch.pages.size()+1)/2;
Expand All @@ -356,8 +358,11 @@ private VisualChapter getVisualChapter(int chapter)
return chapters.get(chapter);
}

private boolean mouseClickPage(int mX, int mY, VisualPage pg)
private boolean mouseClickPage(int mX, int mY, VisualPage pg, boolean isLeftPage)
{
Point offset = getPageOffset(isLeftPage);
mX -= offset.x;
mY -= offset.y;
for (VisualElement e : pg.children)
{
if (mX >= e.position.x && mX <= (e.position.x + e.size.width) &&
Expand All @@ -380,15 +385,15 @@ public boolean mouseHover(int mouseX, int mouseY)

final VisualPage pgLeft = ch.pages.get(currentPair * 2);

VisualElement hovering = mouseHoverPage(pgLeft);
VisualElement hovering = mouseHoverPage(pgLeft, true);

if (hovering == null)
{
if (currentPair * 2 + 1 < ch.pages.size())
{
final VisualPage pgRight = ch.pages.get(currentPair * 2 + 1);

hovering = mouseHoverPage(pgRight);
hovering = mouseHoverPage(pgRight, false);
}
}

Expand All @@ -408,21 +413,19 @@ public boolean mouseHover(int mouseX, int mouseY)
}

@Nullable
private VisualElement mouseHoverPage(VisualPage pg)
private VisualElement mouseHoverPage(VisualPage pg, boolean isLeftPage)
{
Minecraft mc = Minecraft.getMinecraft();
int dw = hasScale ? scaledWidth : gui.width;
int dh = hasScale ? scaledHeight : gui.height;
int mX = Mouse.getX() * dw / mc.displayWidth;
int mY = dh - Mouse.getY() * dh / mc.displayHeight;
Point offset = getPageOffset(isLeftPage);

return mouseHoverContainer(mX, mY, pg.children);
}
mX -= offset.x;
mY -= offset.y;

@Nullable
private VisualElement mouseHoverContainer(int mX, int mY, List<VisualElement> elements)
{
for (VisualElement e : elements)
for (VisualElement e : pg.children)
{
if (e.wantsHover() &&
mX >= e.position.x && mX <= (e.position.x + e.size.width) &&
Expand All @@ -431,36 +434,30 @@ private VisualElement mouseHoverContainer(int mX, int mY, List<VisualElement> el
return e;
}
}

return null;
}

Rect getPageBounds(boolean leftPage)
@Override
public void drawCurrentPages()
{
int guiWidth = gui.width;
int guiHeight = gui.height;

if (hasScale)
{
guiWidth = scaledWidth;
guiHeight = scaledHeight;
GlStateManager.pushMatrix();
GlStateManager.scale(scalingFactor, scalingFactor, scalingFactor);
}

int left = guiWidth / 2 - pageWidth - innerMargin;
int right = guiWidth / 2 + innerMargin;
int top = (guiHeight - pageHeight) / 2 - 9;

return new Rect(leftPage ? left : right,top,pageWidth,pageHeight);
}
drawPage(currentPair * 2);
drawPage(currentPair * 2 + 1);

@Override
public void drawCurrentPages()
{
if (hasScale)
{
GlStateManager.pushMatrix();
GlStateManager.scale(scalingFactor, scalingFactor, scalingFactor);
GlStateManager.popMatrix();
}
}

Point getPageOffset(boolean leftPage)
{
int guiWidth = gui.width;
int guiHeight = gui.height;

Expand All @@ -471,27 +468,10 @@ public void drawCurrentPages()
}

int left = guiWidth / 2 - pageWidth - innerMargin;
int top = (guiHeight - pageHeight) / 2 - 9;
int bottom = top + pageHeight - 3;

drawPage(currentPair * 2);
drawPage(currentPair * 2 + 1);

String cnt = "" + ((getVisualChapter(currentChapter).startPair + currentPair) * 2 + 1) + "/" + (getTotalPairCount() * 2);
Size sz = measure(cnt);

addString(left + (pageWidth-sz.width)/2, bottom, cnt, 0xFF000000, 1.0f);

if (hasScale)
{
GlStateManager.popMatrix();
}
}
int right = guiWidth / 2 + innerMargin;
int top = (guiHeight - pageHeight) / 2 - bottomMargin;

private int getTotalPairCount()
{
VisualChapter last = chapters.get(chapters.size() - 1);
return last.startPair + last.totalPairs;
return new Point(leftPage ? left : right,top);
}

private void drawPage(int page)
Expand All @@ -502,10 +482,21 @@ private void drawPage(int page)

VisualPage pg = ch.pages.get(page);

Point offset = getPageOffset((page & 1) == 0);
GlStateManager.pushMatrix();
GlStateManager.translate(offset.x, offset.y, 0);

for (VisualElement e : pg.children)
{
e.draw(this);
}

String cnt = String.valueOf(ch.startPair * 2 + page+1);
Size sz = measure(cnt);

addString((pageWidth-sz.width)/2, pageHeight + 2, cnt, 0xFF000000, 1.0f);

GlStateManager.popMatrix();
}

@Override
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/assets/gbook/xml/guidebook.xml
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@
<stack item="minecraft:gold_ingot"/>
</recipe.result>
</recipe>
</page>
</page><
</chapter>
<chapter id="templates">
<page>
Expand Down

0 comments on commit 59a17f4

Please sign in to comment.