Created
July 12, 2014 19:31
-
-
Save MultiMote/37437e2a837ca625ba05 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.multi.microZ.gui.elements; | |
import net.minecraft.client.Minecraft; | |
import net.minecraft.client.gui.FontRenderer; | |
import net.minecraft.client.gui.Gui; | |
import org.lwjgl.input.Mouse; | |
import java.util.List; | |
/** | |
* Created by multimote on 12.07.14. | |
*/ | |
public class ScrollingListElement extends Gui { | |
private FontRenderer fontRenderer; | |
public List<String> strings; | |
public int indexSelected; | |
public int xPos; | |
public int yPos; | |
public int width; | |
public int height; | |
public int sliderPos; | |
private int elementsDisplayed; | |
private int firstIndex; | |
public ScrollingListElement(FontRenderer fr, int x, int y, int width, int height, List<String> list) { | |
this.fontRenderer = fr; | |
this.xPos = x; | |
this.yPos = y; | |
this.width = width; | |
this.height = height; | |
this.strings = list; | |
} | |
public boolean isMouseIn(int x, int y){ | |
return x >= this.xPos && x < this.xPos + this.width && y >= this.yPos && y < this.yPos + this.height; | |
} | |
public boolean isMouseInSliderZone(int x, int y){ | |
return isMouseIn(x, y) && x>xPos+width-8 && x<xPos+width-1; | |
} | |
public void drawSlider(int x, int y){ | |
drawRect(x, y+1, x+5, y+getSliderHeight()-3, 0xFFFFFFFF); | |
} | |
public int getSliderHeight(){ | |
return strings.size() == 0 ? 5 : this.height / Math.max((strings.size())/8, 8); | |
} | |
public float getRelativeSliderPos(){ | |
return height==0 ? 0 : (float)sliderPos/(float)(height-getSliderHeight()); | |
} | |
public void mouseClicked(int x, int y){ | |
if(isMouseIn(x,y) && x<xPos+width-10){ | |
int yIn = Math.round((float)((y - yPos)+2)/8)-1; | |
if(yIn<elementsDisplayed) indexSelected=yIn+firstIndex < 0 ? 0 : yIn+firstIndex; | |
} | |
} | |
public void drawList(int x, int y, float f) { | |
drawRect(xPos, yPos, xPos+width, yPos+height, 0xFFFFFFFF); | |
drawRect(xPos+1, yPos+1, xPos+width-1, yPos+height-1, 0xFF000000); | |
drawRect(xPos+width-9, yPos, xPos+width-8, yPos+height, 0xFFFFFFFF); | |
drawSlider(xPos+width-7, yPos+sliderPos+1); | |
firstIndex = (int)((float)strings.size() * getRelativeSliderPos()); | |
int p = 0; | |
for(int i=firstIndex; i<strings.size(); i++){ | |
int yString = yPos + 2 + p * 8; | |
if(yString+8 < yPos+height){ | |
String toDisplay = fontRenderer.getStringWidth(strings.get(i)) > this.width-10 ? fontRenderer.trimStringToWidth(strings.get(i), this.width-15)+"..." : strings.get(i); | |
if(i==indexSelected)drawRect(xPos+1, yString, xPos+width-10, yString+8, 0xFF555555); | |
fontRenderer.drawString(toDisplay, xPos+2, yString, 0xFFFFFFFF); | |
p++; | |
} | |
} | |
elementsDisplayed = p; | |
if(indexSelected>strings.size()-1)indexSelected=strings.size()-1; | |
if(indexSelected<0)indexSelected=0; | |
if(Mouse.isButtonDown(0) && isMouseInSliderZone(x, y)) | |
{ | |
sliderPos=y-getSliderHeight()/2-yPos; | |
if(yPos+sliderPos < yPos)sliderPos = 0; | |
if(yPos+sliderPos+getSliderHeight() > yPos+height)sliderPos = height-getSliderHeight(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment