GUI/texture refactoring

This commit is contained in:
bdew committed 2014-02-26 17:48:53 +02:00
1 parent 4e2df02b8a
commit 5d7c0cd5e8
19 files changed
+295 -160

No files matched your search

+25
View File
@@ -0,0 +1,25 @@
/*
* Copyright (c) bdew, 2013 - 2014
* https://github.com/bdew/bdlib
*
* This mod is distributed under the terms of the Minecraft Mod Public
* License 1.0, or MMPL. Please check the contents of the license located in
* https://raw.github.com/bdew/bdlib/master/MMPL-1.0.txt
*/
package net.bdew.lib.gui
case class BasePoint[T: Numeric](x: T, y: T) {
val n = implicitly[Numeric[T]]
import n.mkNumericOps
def this(p: BasePoint[T]) = this(p.x, p.y)
def +(that: BasePoint[T]) = BasePoint(this.x + that.x, this.y + that.y)
def +(xOff: T, yOff: T) = BasePoint(this.x + xOff, this.y + yOff)
def -(that: BasePoint[T]) = BasePoint(this.x - that.x, this.y - that.y)
def -(xOff: T, yOff: T) = BasePoint(this.x - xOff, this.y - yOff)
def map[R: Numeric](f: T => R) = new BasePoint(f(x), f(y))
}
+40
View File
@@ -0,0 +1,40 @@
/*
* Copyright (c) bdew, 2013 - 2014
* https://github.com/bdew/bdlib
*
* This mod is distributed under the terms of the Minecraft Mod Public
* License 1.0, or MMPL. Please check the contents of the license located in
* https://raw.github.com/bdew/bdlib/master/MMPL-1.0.txt
*/
package net.bdew.lib.gui
case class BaseRect[T: Numeric](x: T, y: T, w: T, h: T) {
val n = implicitly[Numeric[T]]
import n.mkNumericOps
def this(p: BasePoint[T], w: T, h: T) = this(p.x, p.y, w, h)
def contains(p: BasePoint[T]) = n.gteq(p.x, x) && n.gteq(p.y, y) && n.lteq(p.x, x2) && n.lteq(p.y, y2)
def origin = BasePoint(x, y)
def map[R: Numeric](f: T => R) = new BaseRect(f(x), f(y), f(w), f(h))
def interpolate(xf1: Float, yf1: Float, xf2: Float, yf2: Float) =
new BaseRect[Float](
xf1 * w.toFloat() + x.toFloat(),
yf1 * h.toFloat() + y.toFloat(),
(xf2 - xf1) * w.toFloat(),
(yf2 - yf1) * h.toFloat()
)
def +(p: BasePoint[T]) = new BaseRect(origin + p, w, h)
def -(p: BasePoint[T]) = new BaseRect(origin - p, w, h)
def x1 = x
def x2 = x + w
def y1 = y
def y2 = y + h
}
+16 -3
View File
@@ -10,16 +10,25 @@
package net.bdew.lib.gui
import org.lwjgl.opengl.GL11
import net.bdew.lib.Misc
case class Color(r: Float, g: Float, b: Float, a: Float = 1) {
def this(c: (Float, Float, Float)) = this(c._1, c._2, c._3)
def this(c: (Float, Float, Float, Float)) = this(c._1, c._2, c._3, c._4)
def activate() {
GL11.glColor4f(r, g, b, a)
}
def activate() = GL11.glColor4f(r, g, b, a)
def asARGB =
Misc.clamp((a * 255).round, 0, 255) << 24 |
Misc.clamp((r * 255).round, 0, 255) << 16 |
Misc.clamp((g * 255).round, 0, 255) << 8 |
Misc.clamp((b * 255).round, 0, 255)
def asRGB =
Misc.clamp((r * 255).round, 0, 255) << 16 |
Misc.clamp((g * 255).round, 0, 255) << 8 |
Misc.clamp((b * 255).round, 0, 255)
}
object Color {
def fromRGB(r: Int, g: Int, b: Int, a: Int = 255) = Color(r / 255F, g / 255F, b / 255F, a / 255F)
final val white = Color(1, 1, 1)
final val black = Color(0, 0, 0)
final val red = Color(1, 0, 0)
@@ -28,4 +37,8 @@ object Color {
final val yellow = Color(1, 1, 0)
final val cyan = Color(0, 1, 1)
final val magenta = Color(1, 0, 1)
final val gray = Color(0.5F, 0.5F, 0.5F)
final val lightgray = Color(0.75F, 0.75F, 0.75F)
final val darkgray = Color(0.25F, 0.25F, 0.25F)
}
+46
View File
@@ -0,0 +1,46 @@
/*
* Copyright (c) bdew, 2013 - 2014
* https://github.com/bdew/bdlib
*
* This mod is distributed under the terms of the Minecraft Mod Public
* License 1.0, or MMPL. Please check the contents of the license located in
* https://raw.github.com/bdew/bdlib/master/MMPL-1.0.txt
*/
package net.bdew.lib.gui
import net.minecraft.client.renderer.Tessellator
import net.minecraft.client.gui.FontRenderer
trait DrawTarget {
def getZLevel: Double
def getFontRenderer: FontRenderer
def drawTexture(r: Rect, t: Texture, color: Color = Color.white)
def drawText(text: String, p: Point, color: Color, shadow: Boolean)
def drawTextMultiline(text: String, r: Rect, color: Color)
def drawTextureInterpolate(r: Rect, t: Texture, x1: Float, y1: Float, x2: Float, y2: Float, color: Color = Color.white) =
drawTexture(r.interpolate(x1, y1, x2, y2), Texture.interpolate(t, x1, y1, x2, y2))
}
trait SimpleDrawTarget extends DrawTarget {
private final val T = Tessellator.instance
def drawTexture(r: Rect, t: Texture, color: Color = Color.white) {
val z = getZLevel
t.bind()
color.activate()
T.startDrawingQuads()
T.addVertexWithUV(r.x1, r.y2, z, t.u1, t.v2)
T.addVertexWithUV(r.x2, r.y2, z, t.u2, t.v2)
T.addVertexWithUV(r.x2, r.y1, z, t.u2, t.v1)
T.addVertexWithUV(r.x1, r.y1, z, t.u1, t.v1)
T.draw()
}
def drawText(text: String, p: Point, color: Color, shadow: Boolean) =
getFontRenderer.drawString(text, p.x.round, p.y.round, color.asARGB, shadow)
def drawTextMultiline(text: String, r: Rect, color: Color) =
getFontRenderer.drawSplitString(text, r.x.round, r.y.round, r.w.round, color.asARGB)
}
-25
View File
@@ -1,25 +0,0 @@
/*
* Copyright (c) bdew, 2013 - 2014
* https://github.com/bdew/bdlib
*
* This mod is distributed under the terms of the Minecraft Mod Public
* License 1.0, or MMPL. Please check the contents of the license located in
* https://raw.github.com/bdew/bdlib/master/MMPL-1.0.txt
*/
package net.bdew.lib.gui
case class Point(x: Int, y: Int) {
def this(p: Point) = this(p.x, p.y)
def +(that: Point) = new Point(this.x + that.x, this.y + that.y)
def -(that: Point) = new Point(this.x - that.x, this.y - that.y)
}
object Point {
import language.implicitConversions
implicit def awt2point(p: java.awt.Point): Point = new Point(p.x, p.y)
implicit def point2awt(p: Point): java.awt.Point = new java.awt.Point(p.x, p.y)
implicit def tuple2point(p: (Int, Int)): Point = new Point(p._1, p._2)
}
-22
View File
@@ -1,22 +0,0 @@
/*
* Copyright (c) bdew, 2013 - 2014
* https://github.com/bdew/bdlib
*
* This mod is distributed under the terms of the Minecraft Mod Public
* License 1.0, or MMPL. Please check the contents of the license located in
* https://raw.github.com/bdew/bdlib/master/MMPL-1.0.txt
*/
package net.bdew.lib.gui
case class Rect(x: Int, y: Int, w: Int, h: Int) {
def this(p: Point, w: Int, h: Int) = this(p.x, p.y, w, h)
def contains(p: Point) = p.x >= x && p.y >= y && p.x <= x + w && p.y <= y + h
def origin = new Point(x, y)
def +(p: Point) = new Rect(origin + p, w, h)
def -(p: Point) = new Rect(origin - p, w, h)
def x1 = x
def x2 = x + w
def y1 = y
def y2 = y + w
}
+85
View File
@@ -0,0 +1,85 @@
/*
* Copyright (c) bdew, 2013 - 2014
* https://github.com/bdew/bdlib
*
* This mod is distributed under the terms of the Minecraft Mod Public
* License 1.0, or MMPL. Please check the contents of the license located in
* https://raw.github.com/bdew/bdlib/master/MMPL-1.0.txt
*/
package net.bdew.lib.gui
import net.minecraft.util.{Icon, ResourceLocation}
import net.minecraft.client.Minecraft
import net.minecraft.client.renderer.texture.TextureMap
trait Texture {
def u1: Float
def u2: Float
def v1: Float
def v2: Float
def bind(): Unit
}
class InterpolatedTexture(val texture: Texture, rect: Rect) extends Texture {
def bind() = texture.bind()
def u1 = rect.x1 * (texture.u2 - texture.u1) + texture.u1
def u2 = rect.x2 * (texture.u2 - texture.u1) + texture.u1
def v1 = rect.y1 * (texture.v2 - texture.v1) + texture.v1
def v2 = rect.y2 * (texture.v2 - texture.v1) + texture.v1
}
class IconWrapper(val resource: ResourceLocation, val icon: Icon) extends Texture {
def u1 = icon.getMinU
def u2 = icon.getMaxU
def v1 = icon.getMinV
def v2 = icon.getMaxV
def bind() = Minecraft.getMinecraft.renderEngine.bindTexture(resource)
}
class Sprite(val resource: ResourceLocation, r: Rect, scale: Int = 256) extends Texture {
val rect = Rect(r.x / scale, r.y / scale, r.w / scale, r.h / scale)
def u1 = rect.x1
def u2 = rect.x2
def v1 = rect.y1
def v2 = rect.y2
def bind() = Minecraft.getMinecraft.renderEngine.bindTexture(resource)
}
class ScaledResourceLocation(path: String, val scale: Int = 256) extends ResourceLocation(path) {
def this(domain: String, path: String, scale: Int = 256) = this(domain + ":" + path, scale)
}
object Texture {
val ITEMS = TextureMap.locationItemsTexture
val BLOCKS = TextureMap.locationBlocksTexture
// From minecraft Icon
def apply(res: ResourceLocation, i: Icon) = new IconWrapper(res, i)
// From a ResourceLocation and position, scale optional
def apply(res: ResourceLocation, r: Rect) = new Sprite(res, r)
def apply(res: ResourceLocation, r: Rect, scale: Int) = new Sprite(res, r, scale)
def apply(res: ResourceLocation, u1: Float, v1: Float, w: Float, h: Float) = new Sprite(res, Rect(u1, v1, w, h))
def apply(res: ResourceLocation, u1: Float, v1: Float, w: Float, h: Float, scale: Int) = new Sprite(res, Rect(u1, v1, w, h), scale)
// From a path (with optional domain) and position, no scale
def apply(path: String, r: Rect) = new Sprite(new ResourceLocation(path), r)
def apply(path: String, u1: Float, v1: Float, w: Float, h: Float) = new Sprite(new ResourceLocation(path), Rect(u1, v1, w, h))
def apply(domain: String, path: String, r: Rect) = new Sprite(new ResourceLocation(domain, path), r)
def apply(domain: String, path: String, u1: Float, v1: Float, w: Float, h: Float) = new Sprite(new ResourceLocation(domain, path), Rect(u1, v1, w, h))
// From a path (with optional domain), position and scale
def apply(path: String, r: Rect, scale: Int) = new Sprite(new ResourceLocation(path), r, scale)
def apply(path: String, u1: Float, v1: Float, w: Float, h: Float, scale: Int) = new Sprite(new ResourceLocation(path), Rect(u1, v1, w, h), scale)
def apply(domain: String, path: String, r: Rect, scale: Int) = new Sprite(new ResourceLocation(domain, path), r, scale)
def apply(domain: String, path: String, u1: Float, v1: Float, w: Float, h: Float, scale: Int) = new Sprite(new ResourceLocation(domain, path), Rect(u1, v1, w, h), scale)
// From a ScaledResourceLocation and position
def apply(res: ScaledResourceLocation, r: Rect) = new Sprite(res, r, res.scale)
def apply(res: ScaledResourceLocation, u1: Float, v1: Float, w: Float, h: Float) = new Sprite(res, Rect(u1, v1, w, h), res.scale)
// A fraction of another Texture, parameters should be between 0 and 1
def interpolate(texture: Texture, rect: Rect) = new InterpolatedTexture(texture, rect)
def interpolate(texture: Texture, u1: Float, v1: Float, u2: Float, v2: Float) = new InterpolatedTexture(texture, Rect(u1, v1, u2 - u1, v2 - v1))
}
@@ -1,20 +0,0 @@
/*
* Copyright (c) bdew, 2013 - 2014
* https://github.com/bdew/bdlib
*
* This mod is distributed under the terms of the Minecraft Mod Public
* License 1.0, or MMPL. Please check the contents of the license located in
* https://raw.github.com/bdew/bdlib/master/MMPL-1.0.txt
*/
package net.bdew.lib.gui
import net.minecraft.util.ResourceLocation
class TextureLocation(val resource: ResourceLocation, val p: Point) extends Point(p.x, p.y) {
def this(resource: ResourceLocation, x: Int, y: Int) = this(resource, Point(x, y))
}
class TextureLocationScaled(val resource: ResourceLocation, val r: Rect) extends Rect(r.x, r.y, r.w, r.h) {
def this(resource: ResourceLocation, x: Int, y: Int, w: Int, h: Int) = this(resource, Rect(x, y, w, h))
}
+4 -46
View File
@@ -11,13 +11,10 @@ package net.bdew.lib.gui
import scala.collection.mutable
import net.bdew.lib.gui.widgets.BaseWidget
import net.minecraft.util.Icon
import net.minecraft.client.gui.FontRenderer
import net.minecraft.client.Minecraft
import org.lwjgl.opengl.GL11
import net.minecraft.client.renderer.Tessellator
trait WidgetContainer {
trait WidgetContainer extends DrawTarget {
val rect: Rect
val widgets = mutable.MutableList.empty[BaseWidget]
@@ -58,62 +55,23 @@ trait WidgetContainer {
GL11.glPopMatrix()
}
def handleTooltip(op: Point, tip: mutable.MutableList[String]) {
val p = op
def handleTooltip(p: Point, tip: mutable.MutableList[String]) {
for (w <- activeWidgets if w.rect.contains(p))
w.handleTooltip(p - w.rect.origin, tip)
}
def drawTexture(r: Rect, t: TextureLocation) {
Minecraft.getMinecraft.renderEngine.bindTexture(t.resource)
drawTexture(r, t.p)
}
def looseFocus() =
for (w <- activeWidgets)
w.looseFocus()
def getOffsetFromWindow: Point
def drawTexture(r: Rect, uv: Point, color: Color = Color.white): Unit
def drawIcon(r: Rect, i: Icon, color: Color = Color.white)
def drawTextureScaled(r: Rect, t: TextureLocationScaled, color: Color = Color.white)
}
class WidgetContainerWindow(val parent: BaseScreen, xSz: Int, ySz: Int) extends WidgetContainer {
class WidgetContainerWindow(val parent: BaseScreen, xSz: Int, ySz: Int) extends WidgetContainer with SimpleDrawTarget {
def getZLevel = parent.getZLevel
def getFontRenderer = parent.getFontRenderer
def getOffsetFromWindow = Point(0, 0)
val rect = Rect(0, 0, xSz, ySz)
final val F = 1 / 256F
def addVertexUVScaled(x: Float, y: Float, z: Float, u: Float, v: Float) {
Tessellator.instance.addVertexWithUV(x, y, z, u * F, v * F)
}
def drawTextureScaled(r: Rect, l: TextureLocationScaled, color: Color = Color.white) {
val t = l.r
val z = parent.getZLevel
Minecraft.getMinecraft.renderEngine.bindTexture(l.resource)
color.activate()
Tessellator.instance.startDrawingQuads()
addVertexUVScaled(r.x1, r.y2, z, t.x1, t.y2)
addVertexUVScaled(r.x2, r.y2, z, t.x2, t.y2)
addVertexUVScaled(r.x2, r.y1, z, t.x2, t.y1)
addVertexUVScaled(r.x1, r.y1, z, t.x1, t.y1)
Tessellator.instance.draw()
}
def drawTexture(r: Rect, uv: Point, color: Color = Color.white) {
color.activate()
parent.drawTexturedModalRect(r.x, r.y, uv.x, uv.y, r.w, r.h)
}
def drawIcon(r: Rect, i: Icon, color: Color = Color.white) {
color.activate()
parent.drawTexturedModelRectFromIcon(r.x, r.y, i, r.w, r.h)
}
}
+29
View File
@@ -0,0 +1,29 @@
/*
* Copyright (c) bdew, 2013 - 2014
* https://github.com/bdew/bdlib
*
* This mod is distributed under the terms of the Minecraft Mod Public
* License 1.0, or MMPL. Please check the contents of the license located in
* https://raw.github.com/bdew/bdlib/master/MMPL-1.0.txt
*/
package net.bdew.lib
package object gui {
type Rect = BaseRect[Float]
type Point = BasePoint[Float]
import language.implicitConversions
implicit def awt2point(p: java.awt.Point) = BasePoint(p.x, p.y)
implicit def point2awt(p: BasePoint[Int]): java.awt.Point = new java.awt.Point(p.x, p.y)
implicit def tuple2point[T: Numeric](p: (T, T)) = BasePoint(p._1, p._2)
// Converts different kinds of Point/Rect as long as there's an implicit for the underlying type
implicit def pointconvert[T, R](a: BasePoint[R])(implicit f: R => T, n: Numeric[T]) = BasePoint[T](f(a.x), f(a.y))
implicit def rectconvert[T, R](a: BaseRect[R])(implicit f: R => T, n: Numeric[T]) = BaseRect[T](f(a.x), f(a.y), f(a.w), f(a.h))
def Rect(x: Float, y: Float, w: Float, h: Float) = BaseRect(x, y, w, h)
def Point(x: Float, y: Float) = BasePoint(x, y)
}
+6
View File
@@ -0,0 +1,6 @@
import net.bdew.lib.gui._
val a = Rect(1, 1, 2, 2)
a.contains(Point(1.5F, 1.5F))
@@ -14,12 +14,12 @@ import net.bdew.lib.gui.{Point, Rect}
import net.minecraft.client.Minecraft
class WidgetButton(val rect: Rect, text: String, clicked: WidgetButton => Unit)
extends GuiButton(0, rect.x, rect.y, rect.w, rect.h, text) with Widget {
extends GuiButton(0, rect.x.round, rect.y.round, rect.w.round, rect.h.round, text) with Widget {
override def mouseClicked(p: Point, button: Int) {
Minecraft.getMinecraft.sndManager.playSoundFX("random.click", 1.0F, 1.0F)
clicked(this)
}
override def draw(mouse: Point) = drawButton(Minecraft.getMinecraft, mouse.x, mouse.y)
override def draw(mouse: Point) = drawButton(Minecraft.getMinecraft, mouse.x.round, mouse.y.round)
}
@@ -9,28 +9,24 @@
package net.bdew.lib.gui.widgets
import net.bdew.lib.gui.{Point, TextureLocation, Rect, Direction}
import net.bdew.lib.gui._
import net.bdew.lib.data.base.DataSlotNumeric
class WidgetFillDataslot[T](val rect: Rect, val texture: TextureLocation, dir: Direction.Direction, dslot: DataSlotNumeric[T], maxval: T)(implicit num: Numeric[T]) extends Widget {
class WidgetFillDataslot[T](val rect: Rect, val texture: Texture, dir: Direction.Direction, dslot: DataSlotNumeric[T], maxval: T)(implicit num: Numeric[T]) extends Widget {
def getFillArea(max: Int): Int = (num.toFloat(dslot) / num.toFloat(maxval) * max).round
import num.mkNumericOps
override def draw(mouse: Point) {
bindTexture(texture.resource)
val fill = dslot.cval.toFloat() / maxval.toFloat()
dir match {
case Direction.UP =>
val fill = getFillArea(rect.h)
parent.drawTexture(Rect(rect.x, rect.y + rect.h - fill, rect.w, fill), Point(texture.x, texture.y + rect.h - fill))
parent.drawTextureInterpolate(rect, texture, 0, 1 - fill, 1, 1)
case Direction.DOWN =>
val fill = getFillArea(rect.h)
parent.drawTexture(Rect(rect.x, rect.y, rect.w, fill), Point(texture.x, texture.y))
parent.drawTextureInterpolate(rect, texture, 0, 0, 1, fill)
case Direction.LEFT =>
val fill = getFillArea(rect.w)
parent.drawTexture(Rect(rect.x + rect.w - fill, rect.y, fill, rect.h), Point(texture.x + rect.w - fill, texture.y))
parent.drawTextureInterpolate(rect, texture, 1 - fill, 0, 1, 1)
case Direction.RIGHT =>
val fill = getFillArea(rect.w)
parent.drawTexture(Rect(rect.x, rect.y, fill, rect.h), Point(texture.x, texture.y))
parent.drawTextureInterpolate(rect, texture, 0, 0, fill, 1)
}
}
}
@@ -9,15 +9,14 @@
package net.bdew.lib.gui.widgets
import net.bdew.lib.gui.{Point, TextureLocation, Rect}
import net.bdew.lib.gui.{Texture, Point, Rect}
import net.bdew.lib.data.DataSlotTankBase
import scala.collection.mutable
import net.minecraft.client.renderer.texture.TextureMap
import java.text.DecimalFormat
import net.bdew.lib.Misc
import org.lwjgl.opengl.GL11
class WidgetFluidGauge(val rect: Rect, overlay: TextureLocation, dslot: DataSlotTankBase) extends Widget {
class WidgetFluidGauge(val rect: Rect, overlay: Texture, dslot: DataSlotTankBase) extends Widget {
val formater = new DecimalFormat("#,###")
override def handleTooltip(p: Point, tip: mutable.MutableList[String]) {
@@ -35,17 +34,16 @@ class WidgetFluidGauge(val rect: Rect, overlay: TextureLocation, dslot: DataSlot
val fstack = dslot.getFluid
if (fstack != null && fstack.getFluid.getStillIcon != null) {
val icon = fstack.getFluid.getStillIcon
bindTexture(TextureMap.locationBlocksTexture)
var fillHeight: Int = if (dslot.getCapacity > 0) (rect.h * fstack.amount / dslot.getCapacity).round else 0
val icon = Texture(Texture.BLOCKS, fstack.getFluid.getStillIcon)
var fillHeight = if (dslot.getCapacity > 0) rect.h * fstack.amount / dslot.getCapacity else 0
var yStart: Int = 0
while (fillHeight > 0) {
if (fillHeight > 16) {
parent.drawIcon(Rect(rect.x, rect.y + rect.h - 16 - yStart, rect.w, 16), icon)
parent.drawTexture(new Rect(rect.x, rect.y2 - 16 - yStart, rect.w, 16), icon)
fillHeight -= 16
} else {
parent.drawIcon(Rect(rect.x, rect.y + rect.h - fillHeight - yStart, rect.w, fillHeight), icon)
parent.drawTextureInterpolate(new Rect(rect.x, rect.y2 - 16 - yStart, rect.w, 16), icon, 0, 1 - fillHeight / 16, 1, 1)
fillHeight = 0
}
yStart = yStart + 16
@@ -9,10 +9,10 @@
package net.bdew.lib.gui.widgets
import net.bdew.lib.gui.{Point, Direction, TextureLocation, Rect}
import net.bdew.lib.data.DataSlotFloat
import net.bdew.lib.gui._
import scala.collection.mutable
import net.bdew.lib.data.DataSlotFloat
class WidgetProgressBar(rect: Rect, texture: TextureLocation, dslot: DataSlotFloat) extends WidgetFillDataslot[Float](rect, texture, Direction.RIGHT, dslot, 1) {
class WidgetProgressBar(rect: Rect, texture: Texture, dslot: DataSlotFloat) extends WidgetFillDataslot[Float](rect, texture, Direction.RIGHT, dslot, 1) {
override def handleTooltip(p: Point, tip: mutable.MutableList[String]) = tip += "%.0f".format(dslot.cval * 100) + "%"
}
@@ -10,13 +10,10 @@
package net.bdew.lib.gui.widgets
import net.bdew.lib.gui._
import net.minecraft.util.Icon
import net.bdew.lib.gui.Rect
class WidgetSubcontainer(val rect: Rect) extends BaseWidget with WidgetContainer {
class WidgetSubcontainer(val rect: Rect) extends BaseWidget with WidgetContainer with SimpleDrawTarget {
def getZLevel = parent.getZLevel
def getFontRenderer = parent.getFontRenderer
def getOffsetFromWindow = parent.getOffsetFromWindow + rect.origin
def drawTexture(r: Rect, uv: Point, color: Color = Color.white) = parent.drawTexture(r, uv, color)
def drawIcon(r: Rect, i: Icon, color: Color = Color.white) = parent.drawIcon(r, i, color)
def drawTextureScaled(r: Rect, t: TextureLocationScaled, color: Color = Color.white) = parent.drawTextureScaled(r, t, color)
}
@@ -12,11 +12,11 @@ package net.bdew.lib.gui.widgets
import net.minecraft.client.gui.{FontRenderer, GuiTextField}
import net.bdew.lib.gui.{Point, Rect}
class WidgetTextEdit(val rect: Rect, fr: FontRenderer) extends GuiTextField(fr, rect.x, rect.y, rect.w, rect.h) with Widget {
class WidgetTextEdit(val rect: Rect, fr: FontRenderer) extends GuiTextField(fr, rect.x.round, rect.y.round, rect.w.round, rect.h.round) with Widget {
override def mouseClicked(p: Point, button: Int) = {
val pp = p + rect.origin
super.mouseClicked(pp.x, pp.y, button)
super.mouseClicked(pp.x.round, pp.y.round, button)
}
override def looseFocus() = setFocused(false)
@@ -9,19 +9,19 @@
package net.bdew.lib.power
import net.bdew.lib.gui.{Point, TextureLocation, Rect}
import net.bdew.lib.gui.{Texture, Point, Rect}
import scala.collection.mutable
import java.text.DecimalFormat
import net.minecraft.client.Minecraft
import net.bdew.lib.gui.widgets.Widget
class WidgetPowerGauge(val rect: Rect, texture: TextureLocation, dslot: DataSlotPower) extends Widget {
class WidgetPowerGauge(val rect: Rect, texture: Texture, dslot: DataSlotPower) extends Widget {
val formater = new DecimalFormat("#,###")
val a = rect.map(_.round)
override def draw(mouse: Point) {
Minecraft.getMinecraft.renderEngine.bindTexture(texture.resource)
val fill = (dslot.stored / dslot.capacity * rect.h).round
parent.drawTexture(Rect(rect.x, rect.y + rect.h - fill, rect.w, fill), Point(texture.x, texture.y + rect.h - fill))
val fill = dslot.stored / dslot.capacity
parent.drawTextureInterpolate(rect, texture, 0, 1 - fill, 1, 1)
}
override def handleTooltip(p: Point, tip: mutable.MutableList[String]) = tip += formater.format(dslot.stored) + "/" + formater.format(dslot.capacity) + " MJ"
+15 -6
View File
@@ -13,21 +13,30 @@ import net.minecraft.client.renderer.texture.IconRegister
import net.minecraftforge.common.MinecraftForge
import net.minecraftforge.event.ForgeSubscribe
import net.minecraftforge.client.event.TextureStitchEvent
import net.minecraft.util.Icon
import net.bdew.lib.gui.{Texture, IconWrapper}
/**
* Allows loading of texture that don't belong to specific block or item into vanilla block/item atlas
* @param kind 0=Blocks / 1=Items
*/
class IconPreloader(kind: Int) {
var icons = Set.empty[Entry]
var icons = Set.empty[TextureLoc]
case class Entry(loc: String) {
var icon: Icon = null
val sheet = kind match {
case 0 => Texture.BLOCKS
case 1 => Texture.ITEMS
case x => sys.error("Invalid spritesheet number: %d".format(x))
}
case class TextureLoc(loc: String) {
var texture: IconWrapper = null
icons += this
}
implicit def entry2icon(v: Entry) = v.icon
import language.implicitConversions
implicit def entry2icon(v: TextureLoc) = v.texture.icon
implicit def entry2texture(v: TextureLoc) = v.texture
def registerIcons(reg: IconRegister) {}
@@ -38,7 +47,7 @@ class IconPreloader(kind: Int) {
@ForgeSubscribe
def preTextureStitch(ev: TextureStitchEvent.Pre) {
if (ev.map.getTextureType == kind) {
for (x <- icons) x.icon = ev.map.registerIcon(x.loc)
for (x <- icons) x.texture = Texture(sheet, ev.map.registerIcon(x.loc))
registerIcons(ev.map)
}
}