From d49bb94e560a9fa353e5ca5ee960a28760e03725 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20N=C3=BCcke?= Date: Wed, 8 Apr 2015 13:01:07 +0200 Subject: [PATCH] Links work, basic navigation is in place. --- .../assets/opencomputers/doc/adapter.md | 4 ++ .../assets/opencomputers/doc/index.md | 2 +- .../scala/li/cil/oc/client/gui/Manual.scala | 72 ++++++++++++++++--- 3 files changed, 66 insertions(+), 12 deletions(-) create mode 100644 src/main/resources/assets/opencomputers/doc/adapter.md diff --git a/src/main/resources/assets/opencomputers/doc/adapter.md b/src/main/resources/assets/opencomputers/doc/adapter.md new file mode 100644 index 000000000..ad4059b63 --- /dev/null +++ b/src/main/resources/assets/opencomputers/doc/adapter.md @@ -0,0 +1,4 @@ +# Adapter +![Adapter](oredict:oc:adapter) + +The Adapter is at the core of most of OpenComputers' mod integration. \ No newline at end of file diff --git a/src/main/resources/assets/opencomputers/doc/index.md b/src/main/resources/assets/opencomputers/doc/index.md index f4daa3af4..a8dcdeb6d 100644 --- a/src/main/resources/assets/opencomputers/doc/index.md +++ b/src/main/resources/assets/opencomputers/doc/index.md @@ -4,7 +4,7 @@ This is some test text for the subset of Markdown supported by the planned ingam ![This is a tooltip...](../../textures/gui/printer_ink.png) *This* is *italic* text, ~~strikethrough~~ maybe abc-ter **some** text **in bold**. Is _this underlined_? Oh, no, _it's also italic!_ Well, this [a link](blah). ![This is rendered live.](oredict:oc:assembler) -## Smaller headline [also with *link* but this __one__ longer](huehue) +## Smaller headline [also with *link* but this __one__ longer](adapter.md) ![This is another tooltip.](item:OpenComputers:item@23) diff --git a/src/main/scala/li/cil/oc/client/gui/Manual.scala b/src/main/scala/li/cil/oc/client/gui/Manual.scala index 1ed9cbcee..bcc3430a0 100644 --- a/src/main/scala/li/cil/oc/client/gui/Manual.scala +++ b/src/main/scala/li/cil/oc/client/gui/Manual.scala @@ -1,5 +1,6 @@ package li.cil.oc.client.gui +import java.io.InputStream import java.util import com.google.common.base.Charsets @@ -14,6 +15,7 @@ import net.minecraft.util.ResourceLocation import org.lwjgl.input.Mouse import scala.collection.convert.WrapAsJava._ +import scala.collection.mutable import scala.io.Source class Manual extends GuiScreen { @@ -24,6 +26,8 @@ class Manual extends GuiScreen { var offset = 0 var document = Iterable.empty[PseudoMarkdown.Segment] var documentHeight = 0 + var history = mutable.Stack("index.md") + var hoveredLink = None: Option[String] final val documentMaxWidth = 230 final val documentMaxHeight = 176 final val scrollPosX = 244 @@ -34,10 +38,43 @@ class Manual extends GuiScreen { protected var scrollButton: ImageButton = _ - def loadPage(location: ResourceLocation): Iterator[String] = { - val resource = Minecraft.getMinecraft.getResourceManager.getResource(location) - val is = resource.getInputStream - Source.fromInputStream(is)(Charsets.UTF_8).getLines() + def loadPage(path: String): Iterator[String] = { + val location = new ResourceLocation(Settings.resourceDomain, if (path.startsWith("/")) path else "doc/" + path) + var is: InputStream = null + try { + val resource = Minecraft.getMinecraft.getResourceManager.getResource(location) + is = resource.getInputStream + // Force resolving immediately via toArray, otherwise we return a read + // iterator on a closed input stream (because of the finally). + Source.fromInputStream(is)(Charsets.UTF_8).getLines().toArray.iterator + } + catch { + case t: Throwable => + Iterator(s"Failed loading page '$path':") ++ t.toString.lines + } + finally { + Option(is).foreach(_.close()) + } + } + + def refreshPage(): Unit = { + document = PseudoMarkdown.parse(loadPage(history.top)) + documentHeight = PseudoMarkdown.height(document, documentMaxWidth, fontRendererObj) + scrollTo(offset) + } + + def pushPage(path: String): Unit = { + history.push(path) + scrollTo(0) + refreshPage() + } + + def popPage(): Unit = { + if (history.size > 1) { + history.pop() + scrollTo(0) + refreshPage() + } } override def doesGuiPauseGame = false @@ -53,9 +90,7 @@ class Manual extends GuiScreen { guiTop = midY - guiSize.getScaledHeight / 2 xSize = guiSize.getScaledWidth ySize = guiSize.getScaledHeight - offset = 0 - document = PseudoMarkdown.parse(loadPage(new ResourceLocation(Settings.resourceDomain, "doc/index.md"))) - documentHeight = PseudoMarkdown.height(document, documentMaxWidth, fontRendererObj) + refreshPage() scrollButton = new ImageButton(1, guiLeft + scrollPosX, guiTop + scrollPosY, 6, 13, Textures.guiButtonScroll) add(buttonList, scrollButton) @@ -68,10 +103,12 @@ class Manual extends GuiScreen { super.drawScreen(mouseX, mouseY, dt) PseudoMarkdown.render(document, guiLeft + 8, guiTop + 8, documentMaxWidth, documentMaxHeight, offset, fontRendererObj, mouseX, mouseY) match { - case Some(segment) => segment.tooltip match { - case Some(text) if text.nonEmpty => drawHoveringText(seqAsJavaList(text.lines.toSeq), mouseX, mouseY, fontRendererObj) - case _ => - } + case Some(segment) => + segment.tooltip match { + case Some(text) if text.nonEmpty => drawHoveringText(seqAsJavaList(text.lines.toSeq), mouseX, mouseY, fontRendererObj) + case _ => + } + hoveredLink = segment.link case _ => } } @@ -84,6 +121,19 @@ class Manual extends GuiScreen { } } + override def mouseClicked(mouseX: Int, mouseY: Int, button: Int): Unit = { + super.mouseClicked(mouseX, mouseY, button) + + if (button == 0) { + // Left click, did we hit a link? + hoveredLink.foreach(link => pushPage(link)) + } + else if (button == 1) { + // Right mouseclick = back. + popPage() + } + } + private def scrollUp() = scrollTo(offset - PseudoMarkdown.lineHeight(fontRendererObj) * 3) private def scrollDown() = scrollTo(offset + PseudoMarkdown.lineHeight(fontRendererObj) * 3)