GuiScreenModInfo.java 25 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915
package com.mumfrey.liteloader.gui;

import static org.lwjgl.opengl.GL11.*;

import java.awt.image.BufferedImage;
import java.nio.DoubleBuffer;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import javax.imageio.ImageIO;

import org.lwjgl.BufferUtils;
import org.lwjgl.input.Keyboard;
import org.lwjgl.input.Mouse;

import com.mumfrey.liteloader.LiteMod;
import com.mumfrey.liteloader.core.EnabledModsList;
import com.mumfrey.liteloader.core.LiteLoader;
import com.mumfrey.liteloader.core.ModFile;
import com.mumfrey.liteloader.modconfig.ConfigPanel;
import com.mumfrey.liteloader.modconfig.ConfigManager;

import net.minecraft.src.DynamicTexture;
import net.minecraft.src.GuiButton;
import net.minecraft.src.GuiMainMenu;
import net.minecraft.src.GuiScreen;
import net.minecraft.src.Minecraft;
import net.minecraft.src.ResourceLocation;
import net.minecraft.src.Tessellator;

/**
 * GUI screen which displays info about loaded mods and also allows them to be enabled and
 * disabled. An instance of this class is created every time the main menu is displayed and is
 * drawn as an overlay until the tab is clicked, at which point it becomes the active GUI screen
 * and draws the parent main menu screen as its background to give the appearance of being
 * overlaid on the main menu. 
 *
 * @author Adam Mummery-Smith
 */
public class GuiScreenModInfo extends GuiScreen
{
	private static final int LEFT_EDGE       = 80;
	private static final int MARGIN          = 12;
	private static final int TAB_WIDTH       = 20;
	private static final int TAB_HEIGHT      = 40;
	private static final int TAB_TOP         = 20;
	private static final int PANEL_TOP       = 83;
	private static final int PANEL_BOTTOM    = 26;
	private static final int SCROLLBAR_WIDTH = 5;
	
	private static final double TWEEN_RATE = 0.08;

	/**
	 * Used for clipping
	 */
	private static DoubleBuffer doubleBuffer = BufferUtils.createByteBuffer(64).asDoubleBuffer();
	
	// Texture resources for the "about mods" screen, we load the texture directly anyway so it won't be from an RP
	public static ResourceLocation aboutTextureResource;
	private static DynamicTexture aboutTexture;

	/**
	 * Reference to the main menu which this screen is either overlaying or using as its background
	 */
	private GuiMainMenu mainMenu;
	
	/**
	 * Tick number (update counter) used for tweening
	 */
	private long tickNumber;
	
	/**
	 * Last tick number, for tweening
	 */
	private double lastTick;
	
	/**
	 * Current tween percentage (0.0 -> 1.0)
	 */
	private double tweenAmount = 0.0;
	
	/**
	 * Since we don't get real mouse events we have to simulate them by tracking the mouse state
	 */
	private boolean mouseDown, toggled;
	
	/**
	 * Timer used to handle double-clicking on a mod
	 */
	private int doubleClickTime = 0;
	
	/**
	 * Hover opacity for the tab
	 */
	private float tabOpacity = 0.0F;
	
	private boolean hideTab = true;
	
	/**
	 * List of enumerated mods
	 */
	private List<GuiModListEntry> mods = new ArrayList<GuiModListEntry>();
	
	/**
	 * Currently selected mod
	 */
	private GuiModListEntry selectedMod = null;
	
	/**
	 * Text to display under the header
	 */
	private String activeModText = "0 mod(s) loaded";
	
	/**
	 * Height of all the items in the list
	 */
	private int listHeight = 100;
	
	/**
	 * Scroll bar control for the mods list
	 */
	private GuiSimpleScrollBar scrollBar = new GuiSimpleScrollBar();
	
	/**
	 * Enable / disable button
	 */
	private GuiButton btnToggle;
	
	/**
	 * Config button 
	 */
	private GuiButton btnConfig;
	
	/**
	 * Enable the mod info tab checkbox
	 */
	private GuiCheckbox chkEnabled;
	
	private ConfigManager configManager;
	
	/**
	 * Configuration panel
	 */
	private GuiConfigPanelContainer configPanel;
	
	/**
	 * @param minecraft
	 * @param mainMenu
	 * @param loader
	 * @param enabledModsList
	 */
	public GuiScreenModInfo(Minecraft minecraft, GuiMainMenu mainMenu, LiteLoader loader, EnabledModsList enabledModsList, ConfigManager configManager, boolean hideTab)
	{
		this.mc = minecraft;
		this.fontRenderer = minecraft.fontRenderer;
		this.mainMenu = mainMenu;
		this.configManager = configManager;
		this.hideTab = hideTab;
		
		// Spawn the texture resource if we haven't already
		if (aboutTexture == null)
		{
			try
			{
				BufferedImage aboutImage = ImageIO.read(GuiScreenModInfo.class.getResourceAsStream("/assets/liteloader/textures/gui/about.png"));
				aboutTexture = new DynamicTexture(aboutImage);
				aboutTextureResource = minecraft.getTextureManager().getDynamicTextureLocation("about_assets", aboutTexture);
			}
			catch (Exception ex) {}
		}
		
		this.enumerateMods(loader, enabledModsList);
	}
	
	/**
	 * Populate the mods list
	 * 
	 * @param loader
	 * @param enabledModsList
	 */
	private void enumerateMods(LiteLoader loader, EnabledModsList enabledModsList)
	{
		this.activeModText = String.format("%d mod(s) loaded", loader.getLoadedMods().size());
		
		// Add mods to this treeset first, in order to sort them
		Map<String, GuiModListEntry> sortedMods = new TreeMap<String, GuiModListEntry>();
		
		// Active mods
		for (LiteMod mod : loader.getLoadedMods())
		{
			GuiModListEntry modListEntry = new GuiModListEntry(loader, enabledModsList, this.mc.fontRenderer, mod);
			sortedMods.put(modListEntry.getKey(), modListEntry);
		}
		
		// Disabled mods
		for (ModFile disabledMod : loader.getDisabledMods())
		{
			GuiModListEntry modListEntry = new GuiModListEntry(loader, enabledModsList, this.mc.fontRenderer, disabledMod);
			sortedMods.put(modListEntry.getKey(), modListEntry);
		}

		// Add the sorted mods to the mods list
		this.mods.addAll(sortedMods.values());
		
		// Select the first mod in the list
		if (this.mods.size() > 0)
			this.selectedMod = this.mods.get(0);
	}

	/**
	 * Release references prior to being disposed
	 */
	public void release()
	{
		this.mainMenu = null;
	}

	/**
	 * Get the parent menu
	 */
	public GuiMainMenu getMenu()
	{
		return this.mainMenu;
	}

	/**
	 * @return
	 */
	public boolean isTweeningOrOpen()
	{
		return this.tweenAmount > 0.0;
	}

	/* (non-Javadoc)
	 * @see net.minecraft.src.GuiScreen#initGui()
	 */
	@SuppressWarnings("unchecked")
	@Override
	public void initGui()
	{
		if (this.configPanel != null)
		{
			this.configPanel.setSize(this.width - LEFT_EDGE, this.height);
		}
		
		int rightPanelLeftEdge = LEFT_EDGE + MARGIN + 4 + (this.width - LEFT_EDGE - MARGIN - MARGIN - 4) / 2;
		
		this.buttonList.clear();
		this.buttonList.add(this.btnToggle = new GuiButton(0, rightPanelLeftEdge, this.height - PANEL_BOTTOM - 24, 90, 20, "Enable mod"));
		this.buttonList.add(this.btnConfig = new GuiButton(1, rightPanelLeftEdge + 92, this.height - PANEL_BOTTOM - 24, 69, 20, "Settings..."));
		if (!this.hideTab)
		{
			this.buttonList.add(this.chkEnabled = new GuiCheckbox(2, LEFT_EDGE + MARGIN, this.height - PANEL_BOTTOM + 9, "Show LiteLoader tab on main menu"));
		}
		
		this.selectMod(this.selectedMod);

		Keyboard.enableRepeatEvents(true);
	}
	
	@Override
	public void onGuiClosed()
	{
		Keyboard.enableRepeatEvents(false);
	}
	
	/* (non-Javadoc)
	 * @see net.minecraft.src.GuiScreen#setWorldAndResolution(net.minecraft.src.Minecraft, int, int)
	 */
	@Override
	public void setWorldAndResolution(Minecraft minecraft, int width, int height)
	{
		if (this.mc.currentScreen == this)
		{
			// Set res in parent screen if we are the active GUI
			this.mainMenu.setWorldAndResolution(minecraft, width, height);
		}
		
		super.setWorldAndResolution(minecraft, width, height);
	}
	
	/* (non-Javadoc)
	 * @see net.minecraft.src.GuiScreen#updateScreen()
	 */
	@Override
	public void updateScreen()
	{
		if (this.configPanel != null)
		{
			this.configPanel.onTick();
		}
		
		this.tickNumber++;
		
		if (this.mc.currentScreen == this)
		{
			this.mc.currentScreen = this.mainMenu;
			this.mainMenu.updateScreen();
			this.mc.currentScreen = this;
			if (this.chkEnabled != null) this.chkEnabled.checked = LiteLoader.getInstance().getDisplayModInfoScreenTab();
		}
		
		if (this.toggled)
		{
			this.onToggled();
		}
		
		if (this.doubleClickTime > 0)
			this.doubleClickTime--;
	}
	
	/* (non-Javadoc)
	 * @see net.minecraft.src.GuiScreen#drawScreen(int, int, float)
	 */
	@Override
	public void drawScreen(int mouseX, int mouseY, float partialTicks)
	{
		boolean active = this.mc.currentScreen == this;
		
		if (active)
		{
			// Draw the parent screen as our background if we are the active screen
			glClear(GL_DEPTH_BUFFER_BIT);
			this.mainMenu.drawScreen(-10, -10, partialTicks);
			glClear(GL_DEPTH_BUFFER_BIT);
		}
		else
		{
			// If this is not the active screen, copy the width and height from the parent GUI
			this.width = this.mainMenu.width;
			this.height = this.mainMenu.height;
		}

		// Calculate the current tween position
		float xOffset = (this.width - LEFT_EDGE) * this.calcTween(partialTicks, active) + 16.0F + (this.tabOpacity * -32.0F);
		int offsetMouseX = mouseX - (int)xOffset;
		
		// Handle mouse stuff here since we won't get mouse events when not the active GUI
		boolean mouseOverTab = !this.hideTab && (offsetMouseX > LEFT_EDGE - TAB_WIDTH && offsetMouseX < LEFT_EDGE && mouseY > TAB_TOP && mouseY < TAB_TOP + TAB_HEIGHT);
		this.handleMouseClick(offsetMouseX, mouseY, partialTicks, active, mouseOverTab);
		
		// Calculate the tab opacity, not framerate adjusted because we don't really care
		this.tabOpacity = mouseOverTab || this.isTweeningOrOpen() ? 0.5F : Math.max(0.0F, this.tabOpacity - partialTicks * 0.1F);
		
		// Draw the panel contents
		this.drawPanel(offsetMouseX, mouseY, partialTicks, active, xOffset);
		
		if (mouseOverTab && this.tweenAmount < 0.01)
		{
			this.drawTooltip("LiteLoader Mods", mouseX, mouseY, this.width - TAB_WIDTH - 2, this.height, 0xFFFFFF, 0xB0000000);
		}
	}

	/**
	 * @param mouseX
	 * @param mouseY
	 * @param partialTicks
	 * @param active
	 * @param xOffset
	 */
	private void drawPanel(int mouseX, int mouseY, float partialTicks, boolean active, float xOffset)
	{
		glPushMatrix();
		glTranslatef(xOffset, 0.0F, 0.0F);
		
		// Draw the background and left edge
		drawRect(LEFT_EDGE, 0, this.width, this.height, 0xB0000000);
		
		if (!this.hideTab)
		{
			drawRect(LEFT_EDGE, 0, LEFT_EDGE + 1, TAB_TOP, 0xFFFFFFFF);
			drawRect(LEFT_EDGE, TAB_TOP + TAB_HEIGHT, LEFT_EDGE + 1, this.height, 0xFFFFFFFF);
			
			this.mc.getTextureManager().bindTexture(aboutTextureResource);
			glDrawTexturedRect(LEFT_EDGE - TAB_WIDTH, TAB_TOP, TAB_WIDTH + 1, TAB_HEIGHT, 80, 80, 122, 160, 0.5F + this.tabOpacity);
		}
		else
		{
			drawRect(LEFT_EDGE, 0, LEFT_EDGE + 1, this.height, 0xFFFFFFFF);
			this.mc.getTextureManager().bindTexture(aboutTextureResource);
		}
		
		// Only draw the panel contents if we are actually open
		if (this.isTweeningOrOpen())
		{
			if (this.configPanel != null)
			{
				this.drawConfigPanel(mouseX, mouseY, partialTicks);
			}
			else
			{
				this.drawInfoPanel(mouseX, mouseY, partialTicks);
				
				// Draw other controls inside the transform so that they slide properly
				super.drawScreen(mouseX, mouseY, partialTicks);
			}
		}
		else if (this.configPanel != null)
		{
			this.closeConfigPanel();
		}
		
		glPopMatrix();
	}

	/**
	 * @param mouseX
	 * @param mouseY
	 * @param partialTicks
	 */
	public void drawConfigPanel(int mouseX, int mouseY, float partialTicks)
	{
		if (this.configPanel.isCloseRequested())
		{
			this.closeConfigPanel();
			return;
		}
		
		glPushMatrix();
		glTranslatef(LEFT_EDGE, 0, 0);
		
		this.configPanel.draw(mouseX - LEFT_EDGE, mouseY, partialTicks);
		
		glPopMatrix();
	}

	/**
	 * @param mouseX
	 * @param mouseY
	 * @param partialTicks
	 */
	public void drawInfoPanel(int mouseX, int mouseY, float partialTicks)
	{
		// Draw the header pieces
		glDrawTexturedRect(LEFT_EDGE + MARGIN, 12, 128, 40, 0, 0, 256, 80, 1.0F); // liteloader logo
		glDrawTexturedRect(this.width - 32 - MARGIN, 12, 32, 45, 0, 80, 64, 170, 1.0F); // chicken
		
		// Draw header text
		this.fontRenderer.drawString("Version " + LiteLoader.getVersion(), LEFT_EDGE + MARGIN + 38, 50, 0xFFFFFFFF);
		this.fontRenderer.drawString(this.activeModText, LEFT_EDGE + MARGIN + 38, 60, 0xFFAAAAAA);
		
		// Draw top and bottom horizontal rules
		drawRect(LEFT_EDGE + MARGIN, 80, this.width - MARGIN, 81, 0xFF999999);
		drawRect(LEFT_EDGE + MARGIN, this.height - PANEL_BOTTOM + 2, this.width - MARGIN, this.height - PANEL_BOTTOM + 3, 0xFF999999);
		
		int innerWidth = this.width - LEFT_EDGE - MARGIN - MARGIN - 4;
		int panelWidth = innerWidth / 2;
		int panelHeight = this.height - PANEL_BOTTOM - PANEL_TOP;
		
		this.drawModsList(mouseX, mouseY, partialTicks, panelWidth, panelHeight);
		this.drawSelectedMod(mouseX, mouseY, partialTicks, panelWidth, panelHeight);
	}

	/**
	 * @param mouseX
	 * @param mouseY
	 * @param partialTicks
	 * @param width
	 * @param height
	 */
	public void drawModsList(int mouseX, int mouseY, float partialTicks, int width, int height)
	{
		this.scrollBar.drawScrollBar(mouseX, mouseY, partialTicks, LEFT_EDGE + MARGIN + width - SCROLLBAR_WIDTH, PANEL_TOP, SCROLLBAR_WIDTH, height, this.listHeight);

		// clip outside of scroll area
		glEnableClipping(LEFT_EDGE + MARGIN, LEFT_EDGE + MARGIN + width - SCROLLBAR_WIDTH - 1, PANEL_TOP, this.height - PANEL_BOTTOM);
		
		// handle scrolling
		glPushMatrix();
		glTranslatef(0.0F, PANEL_TOP - this.scrollBar.getValue(), 0.0F);
		
		mouseY -= (PANEL_TOP - this.scrollBar.getValue());
		
		int yPos = 0;
		for (GuiModListEntry mod : this.mods)
		{
			// drawListEntry returns a value indicating the height of the item drawn
			yPos += mod.drawListEntry(mouseX, mouseY, partialTicks, LEFT_EDGE + MARGIN, yPos, width - 6, mod == this.selectedMod);
		}
		
		glPopMatrix();
		glDisableClipping();
		
		this.listHeight = yPos;
		this.scrollBar.setMaxValue(this.listHeight - height);
	}

	/**
	 * @param mouseX
	 * @param mouseY
	 * @param partialTicks
	 * @param width
	 * @param height
	 */
	public void drawSelectedMod(int mouseX, int mouseY, float partialTicks, int width, int height)
	{
		if (this.selectedMod != null)
		{
			int left = LEFT_EDGE + MARGIN + width;
			int right = this.width - MARGIN;

			glEnableClipping(left, right, PANEL_TOP, this.height - PANEL_BOTTOM - 28);
			this.selectedMod.drawInfo(mouseX, mouseY, partialTicks, left, PANEL_TOP, right - left);
			glDisableClipping();
		}
	}

	/**
	 * @param mod
	 * @return
	 */
	public void selectMod(GuiModListEntry mod)
	{
		this.selectedMod = mod;
		this.btnToggle.drawButton = false;
		this.btnConfig.drawButton = false;
		
		if (this.selectedMod != null && this.selectedMod.canBeToggled())
		{
			this.btnToggle.drawButton = true;
			this.btnToggle.displayString = this.selectedMod.willBeEnabled() ? "Disable mod" : "Enable mod";
			
			this.btnConfig.drawButton = this.configManager.hasPanel(this.selectedMod.getModClass());
		}
	}

	/**
	 * Toggle the selected mod's enabled status
	 */
	public void toggleSelectedMod()
	{
		if (this.selectedMod != null)
		{
			this.selectedMod.toggleEnabled();
			this.selectMod(this.selectedMod);
		}
	}
	
	/* (non-Javadoc)
	 * @see net.minecraft.src.GuiScreen#actionPerformed(net.minecraft.src.GuiButton)
	 */
	@Override
	protected void actionPerformed(GuiButton button)
	{
		if (button.id == 0)
		{
			this.toggleSelectedMod();
		}
		
		if (button.id == 1)
		{
			this.openConfigPanel();
		}
		
		if (button.id == 2 && this.chkEnabled != null)
		{
			this.chkEnabled.checked = !this.chkEnabled.checked;
			LiteLoader.getInstance().setDisplayModInfoScreenTab(this.chkEnabled.checked);
			
			if (!this.chkEnabled.checked)
			{
				this.chkEnabled.displayString = "Show LiteLoader tab on main menu \247e(use \2479CTRL\247e+\2479SHIFT\247e+\2479TAB\247e)";
			}
		}
	}
	
	/* (non-Javadoc)
	 * @see net.minecraft.src.GuiScreen#keyTyped(char, int)
	 */
	@Override
	protected void keyTyped(char keyChar, int keyCode)
	{
		if (this.configPanel != null)
		{
			this.configPanel.keyPressed(keyChar, keyCode);
			return;
		}
		
		if (keyCode == Keyboard.KEY_ESCAPE)
		{
			this.onToggled();
			return;
		}
		else if (keyCode == Keyboard.KEY_UP)
		{
			int selectedIndex = this.mods.indexOf(this.selectedMod) - 1;
			if (selectedIndex > -1) this.selectMod(this.mods.get(selectedIndex));
			this.scrollSelectedModIntoView();
		}
		else if (keyCode == Keyboard.KEY_DOWN)
		{
			int selectedIndex = this.mods.indexOf(this.selectedMod);
			if (selectedIndex > -1 && selectedIndex < this.mods.size() - 1) this.selectMod(this.mods.get(selectedIndex + 1));
			this.scrollSelectedModIntoView();
		}
		else if (keyCode == Keyboard.KEY_SPACE || keyCode == Keyboard.KEY_RETURN || keyCode == Keyboard.KEY_NUMPADENTER || keyCode == Keyboard.KEY_RIGHT)
		{
			this.toggleSelectedMod();
		}
	}
	
	private void scrollSelectedModIntoView()
	{
		if (this.selectedMod == null) return;
		
		int yPos = 0;
		for (GuiModListEntry mod : this.mods)
		{
			if (mod == this.selectedMod) break;
			yPos += mod.getHeight();
		}
		
		// Mod is above the top of the visible window
		if (yPos < this.scrollBar.getValue())
		{
			this.scrollBar.setValue(yPos);
			return;
		}
		
		int panelHeight = this.height - PANEL_BOTTOM - PANEL_TOP;
		int modHeight = this.selectedMod.getHeight();
		
		// Mod is below the bottom of the visible window
		if (yPos - this.scrollBar.getValue() + modHeight > panelHeight)
		{
			this.scrollBar.setValue(yPos - panelHeight + modHeight);
		}
	}

	/* (non-Javadoc)
	 * @see net.minecraft.src.GuiScreen#mouseClicked(int, int, int)
	 */
	@Override
	protected void mouseClicked(int mouseX, int mouseY, int button)
	{
		if (this.configPanel != null)
		{
			this.configPanel.mousePressed(mouseX - LEFT_EDGE, mouseY, button);
			return;
		}
		
		if (button == 0)
		{
			if (this.scrollBar.wasMouseOver())
			{
				this.scrollBar.setDragging(true);
			}
			
			if (mouseY > PANEL_TOP && mouseY < this.height - PANEL_BOTTOM)
			{
				GuiModListEntry lastSelectedMod = this.selectedMod;
				
				for (GuiModListEntry mod : this.mods)
				{
					if (mod.mouseWasOver())
					{
						this.selectMod(mod);
						
						// handle double-click
						if (mod == lastSelectedMod && this.doubleClickTime > 0 && this.btnConfig.drawButton)
						{
							this.actionPerformed(this.btnConfig);
						}
						
						this.doubleClickTime = 5;
					}
				}
			}
		}
		
		super.mouseClicked(mouseX, mouseY, button);
	}
	
	/* (non-Javadoc)
	 * @see net.minecraft.src.GuiScreen#mouseMovedOrUp(int, int, int)
	 */
	@Override
	protected void mouseMovedOrUp(int mouseX, int mouseY, int button)
	{
		if (this.configPanel != null)
		{
			if (button == -1)
				this.configPanel.mouseMoved(mouseX - LEFT_EDGE, mouseY);
			else
				this.configPanel.mouseReleased(mouseX - LEFT_EDGE, mouseY, button);
			
			return;
		}
		
		if (button == 0)
		{
			this.scrollBar.setDragging(false);
		}
		
		super.mouseMovedOrUp(mouseX, mouseY, button);
	}
	
	/* (non-Javadoc)
	 * @see net.minecraft.src.GuiScreen#handleMouseInput()
	 */
	@Override
	public void handleMouseInput()
	{
		int mouseWheelDelta = Mouse.getEventDWheel();
		if (mouseWheelDelta != 0)
		{
			if (this.configPanel != null)
				this.configPanel.mouseWheelScrolled(mouseWheelDelta);
			else
				this.scrollBar.offsetValue(-mouseWheelDelta / 8);
		}
		
		super.handleMouseInput();
	}

	/**
	 * @param mouseX
	 * @param active
	 * @param mouseOverTab
	 */
	public void handleMouseClick(int mouseX, int mouseY, float partialTicks, boolean active, boolean mouseOverTab)
	{
		boolean mouseDown = Mouse.isButtonDown(0);
		if (((active && mouseX < LEFT_EDGE && this.tweenAmount > 0.75) || mouseOverTab) && !this.mouseDown && mouseDown)
		{
			this.mouseDown = true;
			this.toggled = true;
		}
		else if (this.mouseDown && !mouseDown)
		{
			this.mouseDown = false;
		}
	}

	/**
	 * @param partialTicks
	 * @param active
	 * @return
	 */
	private float calcTween(float partialTicks, boolean active)
	{
		double tickValue = this.tickNumber + partialTicks;
		
		if (active && this.tweenAmount < 1.0)
		{
			this.tweenAmount = Math.min(1.0, this.tweenAmount + ((tickValue - this.lastTick) * TWEEN_RATE));
		}
		else if (!active && this.isTweeningOrOpen())
		{
			this.tweenAmount = Math.max(0.0, this.tweenAmount - ((tickValue - this.lastTick) * TWEEN_RATE));
		}
		
		this.lastTick = tickValue;
		return 1.0F - (float)Math.sin(this.tweenAmount * 0.5 * Math.PI);
	}

	/**
	 * Called when the tab is clicked
	 */
	private void onToggled()
	{
		this.toggled = false;
		this.mc.displayGuiScreen(this.mc.currentScreen == this ? this.mainMenu : this);
	}
	
	/**
	 * Callback for the "config" button, display the config panel for the currently selected mod
	 */
	private void openConfigPanel()
	{
		if (this.selectedMod != null && this.selectedMod.getModClass() != null)
		{
			ConfigPanel panel = this.configManager.getPanel(this.selectedMod.getModClass());
			if (panel != null)
			{
				if (this.configPanel != null)
				{
					this.configPanel.onHidden();
				}
				
				this.configPanel = new GuiConfigPanelContainer(panel, this.selectedMod.getModInstance());
				this.configPanel.setSize(this.width - LEFT_EDGE, this.height);
				this.configPanel.onShown();
			}
		}
	}

	/* (non-Javadoc)
	 * @see com.mumfrey.liteloader.modconfig.ConfigPanelHost#close()
	 */
	private void closeConfigPanel()
	{
		if (this.configPanel != null)
		{
			this.configPanel.onHidden();
			this.configPanel = null;
		}
	}
	
	/**
	 * Draw a tooltip at the specified location and clip to screenWidth and screenHeight
	 * 
	 * @param fontRenderer
	 * @param tooltipText
	 * @param mouseX
	 * @param mouseY
	 * @param screenWidth
	 * @param screenHeight
	 * @param colour
	 * @param backgroundColour
	 */
	protected void drawTooltip(String tooltipText, int mouseX, int mouseY, int screenWidth, int screenHeight, int colour, int backgroundColour)
	{
		int textSize = this.fontRenderer.getStringWidth(tooltipText);
		mouseX = Math.max(0, Math.min(screenWidth - textSize - 6, mouseX - 6));
		mouseY = Math.max(0, Math.min(screenHeight - 16, mouseY - 18));
		
		drawRect(mouseX, mouseY, mouseX + textSize + 6, mouseY + 16, backgroundColour);
		this.fontRenderer.drawStringWithShadow(tooltipText, mouseX + 3, mouseY + 4, colour);
	}

	
	/**
	 * @param x
	 * @param y
	 * @param width
	 * @param height
	 * @param u
	 * @param v
	 * @param u2
	 * @param v2
	 * @param alpha
	 */
	private static void glDrawTexturedRect(int x, int y, int width, int height, int u, int v, int u2, int v2, float alpha)
	{
		glDisable(GL_LIGHTING);
		glEnable(GL_BLEND);
		glAlphaFunc(GL_GREATER, 0.0F);
		glEnable(GL_TEXTURE_2D);
		glColor4f(1.0F, 1.0F, 1.0F, alpha);
		
		float texMapScale = 0.00390625F; // 256px
		
		Tessellator tessellator = Tessellator.instance;
		tessellator.startDrawingQuads();
		tessellator.addVertexWithUV(x + 0,     y + height, 0, u  * texMapScale, v2 * texMapScale);
		tessellator.addVertexWithUV(x + width, y + height, 0, u2 * texMapScale, v2 * texMapScale);
		tessellator.addVertexWithUV(x + width, y + 0,      0, u2 * texMapScale, v  * texMapScale);
		tessellator.addVertexWithUV(x + 0,     y + 0,      0, u  * texMapScale, v  * texMapScale);
		tessellator.draw();
		
		glDisable(GL_BLEND);
		glAlphaFunc(GL_GREATER, 0.01F);
	}
	
	/**
	 * Enable OpenGL clipping planes (uses planes 2, 3, 4 and 5)
	 * 
	 * @param xLeft Left edge clip or -1 to not use this plane
	 * @param xRight Right edge clip or -1 to not use this plane
	 * @param yTop Top edge clip or -1 to not use this plane
	 * @param yBottom Bottom edge clip or -1 to not use this plane
	 */
	final static void glEnableClipping(int xLeft, int xRight, int yTop, int yBottom)
	{
		// Apply left edge clipping if specified
		if (xLeft != -1)
		{
			doubleBuffer.clear();
			doubleBuffer.put(1).put(0).put(0).put(-xLeft).flip();
			glClipPlane(GL_CLIP_PLANE2, doubleBuffer);
			glEnable(GL_CLIP_PLANE2);
		}
		
		// Apply right edge clipping if specified
		if (xRight != -1)
		{
			doubleBuffer.clear();
			doubleBuffer.put(-1).put(0).put(0).put(xRight).flip();
			glClipPlane(GL_CLIP_PLANE3, doubleBuffer);
			glEnable(GL_CLIP_PLANE3);
		}
		
		// Apply top edge clipping if specified
		if (yTop != -1)
		{
			doubleBuffer.clear();
			doubleBuffer.put(0).put(1).put(0).put(-yTop).flip();
			glClipPlane(GL_CLIP_PLANE4, doubleBuffer);
			glEnable(GL_CLIP_PLANE4);
		}
		
		// Apply bottom edge clipping if specified
		if (yBottom != -1)
		{
			doubleBuffer.clear();
			doubleBuffer.put(0).put(-1).put(0).put(yBottom).flip();
			glClipPlane(GL_CLIP_PLANE5, doubleBuffer);
			glEnable(GL_CLIP_PLANE5);
		}
	}
	
	/**
	 * Disable OpenGL clipping planes (uses planes 2, 3, 4 and 5)
	 */
	final static void glDisableClipping()
	{
		glDisable(GL_CLIP_PLANE5);
		glDisable(GL_CLIP_PLANE4);
		glDisable(GL_CLIP_PLANE3);
		glDisable(GL_CLIP_PLANE2);
	}
}