diff options
Diffstat (limited to 'src/Java/binnie/craftgui/controls/tab')
-rw-r--r-- | src/Java/binnie/craftgui/controls/tab/ControlTab.java | 128 | ||||
-rw-r--r-- | src/Java/binnie/craftgui/controls/tab/ControlTabBar.java | 86 |
2 files changed, 214 insertions, 0 deletions
diff --git a/src/Java/binnie/craftgui/controls/tab/ControlTab.java b/src/Java/binnie/craftgui/controls/tab/ControlTab.java new file mode 100644 index 0000000000..e05b7ed01a --- /dev/null +++ b/src/Java/binnie/craftgui/controls/tab/ControlTab.java @@ -0,0 +1,128 @@ +package binnie.craftgui.controls.tab; + +import binnie.craftgui.controls.core.Control; +import binnie.craftgui.controls.core.IControlValue; +import binnie.craftgui.core.Attribute; +import binnie.craftgui.core.CraftGUI; +import binnie.craftgui.core.ITooltip; +import binnie.craftgui.core.Tooltip; +import binnie.craftgui.core.geometry.IArea; +import binnie.craftgui.core.geometry.IPoint; +import binnie.craftgui.core.geometry.Position; +import binnie.craftgui.core.renderer.Renderer; +import binnie.craftgui.events.EventMouse.Down; +import binnie.craftgui.events.EventMouse.Down.Handler; +import binnie.craftgui.events.EventValueChanged; +import binnie.craftgui.minecraft.control.ControlItemDisplay; +import binnie.craftgui.minecraft.control.ControlTabIcon; +import binnie.craftgui.resource.Texture; +import binnie.craftgui.resource.minecraft.CraftGUITexture; +import java.util.List; + +public class ControlTab<T> + extends Control + implements ITooltip, IControlValue<T> +{ + private ControlTabBar<T> tabBar; + protected T value; + + public ControlTab(ControlTabBar<T> parent, float x, float y, float w, float h, T value) + { + super(parent, x, y, w, h); + setValue(value); + this.tabBar = parent; + addAttribute(Attribute.MouseOver); + + addSelfEventHandler(new EventMouse.Down.Handler() + { + public void onEvent(EventMouse.Down event) + { + ControlTab.this.callEvent(new EventValueChanged(ControlTab.this.getWidget(), ControlTab.this.getValue())); + } + }); + } + + public void getTooltip(Tooltip tooltip) + { + String name = getName(); + if ((name != null) && (!name.isEmpty())) { + tooltip.add(getName()); + } + if ((this.value instanceof ITooltip)) { + ((ITooltip)this.value).getTooltip(tooltip); + } + } + + public T getValue() + { + return this.value; + } + + public void setValue(T value) + { + this.value = value; + } + + public boolean isCurrentSelection() + { + return (getValue() != null) && (getValue().equals(this.tabBar.getValue())); + } + + public Position getTabPosition() + { + return this.tabBar.position; + } + + public String getName() + { + return this.value.toString(); + } + + public void onRenderBackground() + { + Object texture = CraftGUITexture.TabDisabled; + if (isMouseOver()) { + texture = CraftGUITexture.TabHighlighted; + } else if (isCurrentSelection()) { + texture = CraftGUITexture.Tab; + } + Texture lTexture = CraftGUI.Render.getTexture(texture); + Position position = getTabPosition(); + + Texture iTexture = lTexture.crop(position, 8.0F); + + + IArea area = getArea(); + if (texture == CraftGUITexture.TabDisabled) { + if ((position == Position.Top) || (position == Position.Left)) + { + area.setPosition(area.getPosition().sub(new IPoint(4 * position.x(), 4 * position.y()))); + area.setSize(area.getSize().add(new IPoint(4 * position.x(), 4 * position.y()))); + } + else + { + area.setSize(area.getSize().sub(new IPoint(4 * position.x(), 4 * position.y()))); + } + } + CraftGUI.Render.texture(iTexture, area); + if ((this instanceof ControlTabIcon)) + { + ControlTabIcon icon = (ControlTabIcon)this; + ControlItemDisplay item = (ControlItemDisplay)getWidgets().get(0); + if (texture == CraftGUITexture.TabDisabled) { + item.setColour(-1431655766); + } else { + item.setColour(-1); + } + if (icon.hasOutline()) + { + iTexture = CraftGUI.Render.getTexture(CraftGUITexture.TabOutline); + iTexture = iTexture.crop(position, 8.0F); + + CraftGUI.Render.colour(icon.getOutlineColour()); + + CraftGUI.Render.texture(iTexture, area.inset(2)); + } + } + } +} diff --git a/src/Java/binnie/craftgui/controls/tab/ControlTabBar.java b/src/Java/binnie/craftgui/controls/tab/ControlTabBar.java new file mode 100644 index 0000000000..d209887994 --- /dev/null +++ b/src/Java/binnie/craftgui/controls/tab/ControlTabBar.java @@ -0,0 +1,86 @@ +package binnie.craftgui.controls.tab; + +import binnie.craftgui.controls.core.Control; +import binnie.craftgui.controls.core.IControlValue; +import binnie.craftgui.core.IWidget; +import binnie.craftgui.core.geometry.IPoint; +import binnie.craftgui.core.geometry.Position; +import binnie.craftgui.events.EventHandler.Origin; +import binnie.craftgui.events.EventValueChanged; +import binnie.craftgui.events.EventValueChanged.Handler; +import java.util.Collection; +import java.util.Iterator; +import java.util.List; + +public class ControlTabBar<T> + extends Control + implements IControlValue<T> +{ + T value; + Position position; + + public ControlTabBar(IWidget parent, float x, float y, float width, float height, Position position) + { + super(parent, x, y, width, height); + this.position = position; + + addEventHandler(new EventValueChanged.Handler() + { + public void onEvent(EventValueChanged event) + { + ControlTabBar.this.setValue(event.getValue()); + } + }.setOrigin(EventHandler.Origin.DirectChild, this)); + } + + public void setValues(Collection<T> values) + { + for (int i = 0; i < getWidgets().size();) { + deleteChild((IWidget)getWidgets().get(0)); + } + float length = values.size(); + int tabDimension = (int)(getSize().y() / length); + if ((this.position == Position.Top) || (this.position == Position.Bottom)) { + tabDimension = (int)(getSize().x() / length); + } + int i = 0; + for (T value : values) + { + IWidget tab; + IWidget tab; + if ((this.position == Position.Top) || (this.position == Position.Bottom)) { + tab = createTab(i * tabDimension, 0.0F, tabDimension, getSize().y(), value); + } else { + tab = createTab(0.0F, i * tabDimension, getSize().x(), tabDimension, value); + } + i++; + } + if ((this.value == null) && (!values.isEmpty())) { + setValue(values.iterator().next()); + } + } + + public ControlTab<T> createTab(float x, float y, float w, float h, T value) + { + return new ControlTab(this, x, y, w, h, value); + } + + public T getValue() + { + return this.value; + } + + public void setValue(T value) + { + boolean change = this.value != value; + this.value = value; + if (change) { + callEvent(new EventValueChanged(this, value)); + } + } + + public Position getDirection() + { + return this.position; + } +} |