diff options
author | Draknyte1 <Draknyte1@hotmail.com> | 2016-01-20 14:24:34 +1000 |
---|---|---|
committer | Draknyte1 <Draknyte1@hotmail.com> | 2016-01-20 14:24:34 +1000 |
commit | 869c206c4fcc8001bd2e1d66f704290331813835 (patch) | |
tree | 96735ce8fe4665e2759c3374221d6f06f4527df2 /src/Java/binnie/craftgui/controls/scroll | |
parent | ec2c72827f01dd4bb2174137f1ab162f9ddaab62 (diff) | |
download | GT5-Unofficial-869c206c4fcc8001bd2e1d66f704290331813835.tar.gz GT5-Unofficial-869c206c4fcc8001bd2e1d66f704290331813835.tar.bz2 GT5-Unofficial-869c206c4fcc8001bd2e1d66f704290331813835.zip |
Initial Commit
Diffstat (limited to 'src/Java/binnie/craftgui/controls/scroll')
4 files changed, 267 insertions, 0 deletions
diff --git a/src/Java/binnie/craftgui/controls/scroll/ControlScroll.java b/src/Java/binnie/craftgui/controls/scroll/ControlScroll.java new file mode 100644 index 0000000000..1d03f5fbc7 --- /dev/null +++ b/src/Java/binnie/craftgui/controls/scroll/ControlScroll.java @@ -0,0 +1,22 @@ +package binnie.craftgui.controls.scroll; + +import binnie.craftgui.controls.core.Control; +import binnie.craftgui.core.IWidget; + +public class ControlScroll + extends Control +{ + private IControlScrollable scrollWidget; + + public ControlScroll(IWidget parent, float x, float y, float width, float height, IControlScrollable scrollWidget) + { + super(parent, x, y, width, height); + this.scrollWidget = scrollWidget; + new ControlScrollBar(this); + } + + public IControlScrollable getScrollableWidget() + { + return this.scrollWidget; + } +} diff --git a/src/Java/binnie/craftgui/controls/scroll/ControlScrollBar.java b/src/Java/binnie/craftgui/controls/scroll/ControlScrollBar.java new file mode 100644 index 0000000000..a94705767d --- /dev/null +++ b/src/Java/binnie/craftgui/controls/scroll/ControlScrollBar.java @@ -0,0 +1,94 @@ +package binnie.craftgui.controls.scroll; + +import binnie.craftgui.controls.core.Control; +import binnie.craftgui.core.Attribute; +import binnie.craftgui.core.CraftGUI; +import binnie.craftgui.core.IWidget; +import binnie.craftgui.core.geometry.IArea; +import binnie.craftgui.core.geometry.IPoint; +import binnie.craftgui.core.renderer.Renderer; +import binnie.craftgui.events.EventMouse.Down; +import binnie.craftgui.events.EventMouse.Down.Handler; +import binnie.craftgui.events.EventMouse.Drag; +import binnie.craftgui.events.EventMouse.Drag.Handler; +import binnie.craftgui.resource.minecraft.CraftGUITexture; + +public class ControlScrollBar + extends Control +{ + protected final IControlScrollable scrollable; + + public ControlScrollBar(ControlScroll parent) + { + this(parent, 0.0F, 0.0F, parent.getSize().x(), parent.getSize().y(), parent.getScrollableWidget()); + } + + public ControlScrollBar(IWidget parent, float x, float y, float w, float h, IControlScrollable scrollable2) + { + super(parent, x, y, w, h); + this.scrollable = scrollable2; + addAttribute(Attribute.MouseOver); + + addSelfEventHandler(new EventMouse.Drag.Handler() + { + public void onEvent(EventMouse.Drag event) + { + ControlScrollBar.this.scrollable.movePercentage(event.getDy() / (ControlScrollBar.this.h() - ControlScrollBar.this.getBarHeight())); + } + }); + addSelfEventHandler(new EventMouse.Down.Handler() + { + public void onEvent(EventMouse.Down event) + { + float shownPercentage = ControlScrollBar.this.scrollable.getPercentageShown(); + float percentageIndex = ControlScrollBar.this.scrollable.getPercentageIndex(); + float minPercent = (1.0F - shownPercentage) * percentageIndex; + float maxPercent = minPercent + shownPercentage; + float clickedPercentage = ControlScrollBar.this.getRelativeMousePosition().y() / (ControlScrollBar.this.h() - 2.0F); + clickedPercentage = Math.max(Math.min(clickedPercentage, 1.0F), 0.0F); + if (clickedPercentage > maxPercent) { + ControlScrollBar.this.scrollable.setPercentageIndex((clickedPercentage - shownPercentage) / (1.0F - shownPercentage)); + } + if (clickedPercentage < minPercent) { + ControlScrollBar.this.scrollable.setPercentageIndex(clickedPercentage / (1.0F - shownPercentage)); + } + } + }); + } + + public void onUpdateClient() {} + + public boolean isEnabled() + { + return this.scrollable.getPercentageShown() < 0.99F; + } + + public float getBarHeight() + { + return h() * this.scrollable.getPercentageShown(); + } + + protected IArea getRenderArea() + { + float height = getBarHeight(); + if (height < 6.0F) { + height = 6.0F; + } + float yOffset = ((int)h() - (int)getBarHeight()) * this.scrollable.getPercentageIndex(); + + return new IArea(0.0F, yOffset, getSize().x(), height); + } + + public void onRenderBackground() + { + IArea renderArea = getRenderArea(); + + Object texture = CraftGUITexture.ScrollDisabled; + if (isMouseOver()) { + texture = CraftGUITexture.ScrollHighlighted; + } else if (isEnabled()) { + texture = CraftGUITexture.Scroll; + } + CraftGUI.Render.texture(texture, renderArea); + } +} diff --git a/src/Java/binnie/craftgui/controls/scroll/ControlScrollableContent.java b/src/Java/binnie/craftgui/controls/scroll/ControlScrollableContent.java new file mode 100644 index 0000000000..442c9d3226 --- /dev/null +++ b/src/Java/binnie/craftgui/controls/scroll/ControlScrollableContent.java @@ -0,0 +1,134 @@ +package binnie.craftgui.controls.scroll; + +import binnie.craftgui.controls.core.Control; +import binnie.craftgui.core.IWidget; +import binnie.craftgui.core.geometry.IArea; +import binnie.craftgui.core.geometry.IPoint; +import binnie.craftgui.events.EventMouse.Wheel; +import binnie.craftgui.events.EventMouse.Wheel.Handler; +import binnie.craftgui.events.EventWidget.ChangeSize; +import binnie.craftgui.events.EventWidget.ChangeSize.Handler; + +public class ControlScrollableContent<T extends IWidget> + extends Control + implements IControlScrollable +{ + protected T controlChild; + protected float scrollBarSize; + + public ControlScrollableContent(IWidget parent, float x, float y, float w, float h, float scrollBarSize) + { + super(parent, x, y, w, h); + if (scrollBarSize != 0.0F) { + new ControlScroll(this, getSize().x() - scrollBarSize, 0.0F, scrollBarSize, getSize().y(), this); + } + addEventHandler(new EventMouse.Wheel.Handler() + { + public void onEvent(EventMouse.Wheel event) + { + if ((ControlScrollableContent.this.getRelativeMousePosition().x() > 0.0F) && (ControlScrollableContent.this.getRelativeMousePosition().y() > 0.0F) && (ControlScrollableContent.this.getRelativeMousePosition().x() < ControlScrollableContent.this.getSize().x()) && (ControlScrollableContent.this.getRelativeMousePosition().y() < ControlScrollableContent.this.getSize().y())) + { + if (ControlScrollableContent.this.getMovementRange() == 0.0F) { + return; + } + float percentageMove = 0.8F / ControlScrollableContent.this.getMovementRange(); + ControlScrollableContent.this.movePercentage(percentageMove * -event.getDWheel()); + } + } + }); + this.scrollBarSize = scrollBarSize; + } + + public void setScrollableContent(T child) + { + this.controlChild = child; + if (child == null) { + return; + } + child.setCroppedZone(this, new IArea(1.0F, 1.0F, getSize().x() - 2.0F - this.scrollBarSize, getSize().y() - 2.0F)); + child.addSelfEventHandler(new EventWidget.ChangeSize.Handler() + { + public void onEvent(EventWidget.ChangeSize event) + { + ControlScrollableContent.this.controlChild.setOffset(new IPoint(0.0F, -ControlScrollableContent.this.percentageIndex * ControlScrollableContent.this.getMovementRange())); + if (ControlScrollableContent.this.getMovementRange() == 0.0F) { + ControlScrollableContent.this.percentageIndex = 0.0F; + } + } + }); + } + + public T getContent() + { + return this.controlChild; + } + + public float getPercentageShown() + { + if ((this.controlChild == null) || (this.controlChild.getSize().y() == 0.0F)) { + return 1.0F; + } + float shown = getSize().y() / this.controlChild.getSize().y(); + return Math.min(shown, 1.0F); + } + + float percentageIndex = 0.0F; + + public float getPercentageIndex() + { + return this.percentageIndex; + } + + public void movePercentage(float percentage) + { + if (this.controlChild == null) { + return; + } + this.percentageIndex += percentage; + if (this.percentageIndex > 1.0F) { + this.percentageIndex = 1.0F; + } else if (this.percentageIndex < 0.0F) { + this.percentageIndex = 0.0F; + } + if (getMovementRange() == 0.0F) { + this.percentageIndex = 0.0F; + } + this.controlChild.setOffset(new IPoint(0.0F, -this.percentageIndex * getMovementRange())); + } + + public void setPercentageIndex(float index) + { + movePercentage(index - this.percentageIndex); + } + + public float getMovementRange() + { + if (this.controlChild == null) { + return 0.0F; + } + float range = this.controlChild.getSize().y() - getSize().y(); + return Math.max(range, 0.0F); + } + + public void onUpdateClient() + { + setPercentageIndex(getPercentageIndex()); + } + + public void ensureVisible(float minY, float maxY, float totalY) + { + minY /= totalY; + maxY /= totalY; + + float shownPercentage = getPercentageShown(); + float percentageIndex = getPercentageIndex(); + float minPercent = (1.0F - shownPercentage) * percentageIndex; + float maxPercent = minPercent + shownPercentage; + if (maxY > maxPercent) { + setPercentageIndex((maxY - shownPercentage) / (1.0F - shownPercentage)); + } + if (minY < minPercent) { + setPercentageIndex(minY / (1.0F - shownPercentage)); + } + } +} diff --git a/src/Java/binnie/craftgui/controls/scroll/IControlScrollable.java b/src/Java/binnie/craftgui/controls/scroll/IControlScrollable.java new file mode 100644 index 0000000000..39154815e5 --- /dev/null +++ b/src/Java/binnie/craftgui/controls/scroll/IControlScrollable.java @@ -0,0 +1,17 @@ +package binnie.craftgui.controls.scroll; + +import binnie.craftgui.core.IWidget; + +public abstract interface IControlScrollable + extends IWidget +{ + public abstract float getPercentageShown(); + + public abstract float getPercentageIndex(); + + public abstract void movePercentage(float paramFloat); + + public abstract void setPercentageIndex(float paramFloat); + + public abstract float getMovementRange(); +} |