Advertisement
Guest User

GuiManager.java

a guest
Aug 23rd, 2017
576
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.14 KB | None | 0 0
  1. /*
  2.  * Copyright (c) 2013, DarkStorm (darkstorm@evilminecraft.net)
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that the following conditions are met:
  7.  *
  8.  * 1. Redistributions of source code must retain the above copyright notice, this
  9.  *    list of conditions and the following disclaimer.
  10.  * 2. Redistributions in binary form must reproduce the above copyright notice,
  11.  *    this list of conditions and the following disclaimer in the documentation
  12.  *    and/or other materials provided with the distribution.
  13.  *
  14.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  15.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  16.  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  17.  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
  18.  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  19.  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  20.  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  21.  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  22.  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  23.  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24.  */
  25. package io.github.thewitherskull.witheraura.client.gui;
  26.  
  27. import java.awt.*;
  28. import java.util.concurrent.atomic.AtomicBoolean;
  29.  
  30. import net.minecraft.client.Minecraft;
  31.  
  32. import org.darkstorm.minecraft.gui.component.BoundedRangeComponent.ValueDisplay;
  33. import org.darkstorm.minecraft.gui.component.*;
  34. import org.darkstorm.minecraft.gui.component.Button;
  35. import org.darkstorm.minecraft.gui.component.Component;
  36. import org.darkstorm.minecraft.gui.component.Frame;
  37. import org.darkstorm.minecraft.gui.component.basic.*;
  38. import org.darkstorm.minecraft.gui.listener.*;
  39. import org.darkstorm.minecraft.gui.theme.Theme;
  40. import org.darkstorm.minecraft.gui.theme.simple.SimpleTheme;
  41.  
  42. import io.github.thewitherskull.witheraura.client.main.Client;
  43. import io.github.thewitherskull.witheraura.client.Module;
  44.  
  45. /**
  46.  * Minecraft GUI API
  47.  *
  48.  * This class is not actually intended for use; rather, you should use this as a
  49.  * template for your actual GuiManager, as the creation of frames is highly
  50.  * implementation-specific.
  51.  *
  52.  * @author DarkStorm (darkstorm@evilminecraft.net)
  53.  */
  54. public final class ExampleGuiManager extends AbstractGuiManager {
  55.     private class ModuleFrame extends BasicFrame {
  56.         private ModuleFrame() {
  57.         }
  58.  
  59.         private ModuleFrame(String title) {
  60.             super(title);
  61.         }
  62.     }
  63.  
  64.     private final AtomicBoolean setup;
  65.  
  66.     public ExampleGuiManager() {
  67.         setup = new AtomicBoolean();
  68.     }
  69.  
  70.     @Override
  71.     public void setup() {
  72.         if(!setup.compareAndSet(false, true))
  73.             return;
  74.  
  75.         createTestFrame();
  76.  
  77.         final Map<ModuleCategory, ModuleFrame> categoryFrames = new HashMap<ModuleCategory, ModuleFrame>();
  78.         for(Module module : Client.getModules()) {
  79.             ModuleFrame frame = categoryFrames.get(module.getCategory());
  80.             if(frame == null) {
  81.                 String name = module.getCategory().name().toLowerCase();
  82.                 name = Character.toUpperCase(name.charAt(0))
  83.                         + name.substring(1);
  84.                 frame = new ModuleFrame(name);
  85.                 frame.setTheme(theme);
  86.                 frame.setLayoutManager(new GridLayoutManager(2, 0));
  87.                 frame.setVisible(true);
  88.                 frame.setClosable(false);
  89.                 frame.setMinimized(true);
  90.                 addFrame(frame);
  91.                 categoryFrames.put(module.getCategory(), frame);
  92.             }
  93.             frame.add(new BasicLabel(module.getName()));
  94.             final Module updateModule = module;
  95.             Button button = new BasicButton(module.isEnabled() ? "Disable"
  96.                     : "Enable") {
  97.                 @Override
  98.                 public void update() {
  99.                     setText(updateModule.isEnabled() ? "Disable" : "Enable");
  100.                 }
  101.             };
  102.             button.addButtonListener(new ButtonListener() {
  103.                 @Override
  104.                 public void onButtonPress(Button button) {
  105.                     updateModule.toggle();
  106.                     button.setText(updateModule.isEnabled() ? "Disable"
  107.                             : "Enable");
  108.                 }
  109.             });
  110.             frame.add(button, HorizontalGridConstraint.RIGHT);
  111.         }
  112.  
  113.         // Optional equal sizing and auto-positioning
  114.         resizeComponents();
  115.         Minecraft minecraft = Minecraft.getMinecraft();
  116.         Dimension maxSize = recalculateSizes();
  117.         int offsetX = 5, offsetY = 5;
  118.         int scale = minecraft.gameSettings.guiScale;
  119.         if(scale == 0)
  120.             scale = 1000;
  121.         int scaleFactor = 0;
  122.         while(scaleFactor < scale && minecraft.displayWidth / (scaleFactor + 1) >= 320 && minecraft.displayHeight / (scaleFactor + 1) >= 240)
  123.             scaleFactor++;
  124.         for(Frame frame : getFrames()) {
  125.             frame.setX(offsetX);
  126.             frame.setY(offsetY);
  127.             offsetX += maxSize.width + 5;
  128.             if(offsetX + maxSize.width + 5 > minecraft.displayWidth / scaleFactor) {
  129.                 offsetX = 5;
  130.                 offsetY += maxSize.height + 5;
  131.             }
  132.         }
  133.     }
  134.  
  135.     private void createTestFrame() {
  136.         Theme theme = getTheme();
  137.         Frame testFrame = new BasicFrame("Frame");
  138.         testFrame.setTheme(theme);
  139.  
  140.         testFrame.add(new BasicLabel("TEST LOL"));
  141.         testFrame.add(new BasicLabel("TEST 23423"));
  142.         testFrame.add(new BasicLabel("TE123123123ST LOL"));
  143.         testFrame.add(new BasicLabel("31243 LO3242L432"));
  144.         BasicButton testButton = new BasicButton("Duplicate this frame!");
  145.         testButton.addButtonListener(new ButtonListener() {
  146.  
  147.             @Override
  148.             public void onButtonPress(Button button) {
  149.                 createTestFrame();
  150.             }
  151.         });
  152.         testFrame.add(new BasicCheckButton("This is a checkbox"));
  153.         testFrame.add(testButton);
  154.         ComboBox comboBox = new BasicComboBox("Simple theme", "Other theme", "Other theme 2");
  155.         comboBox.addComboBoxListener(new ComboBoxListener() {
  156.  
  157.             @Override
  158.             public void onComboBoxSelectionChanged(ComboBox comboBox) {
  159.                 Theme theme;
  160.                 switch(comboBox.getSelectedIndex()) {
  161.                 case 0:
  162.                     theme = new SimpleTheme();
  163.                     break;
  164.                 case 1:
  165.                     // Some other theme
  166.                     // break;
  167.                 case 2:
  168.                     // Another theme
  169.                     // break;
  170.                 default:
  171.                     return;
  172.                 }
  173.                 setTheme(theme);
  174.             }
  175.         });
  176.         testFrame.add(comboBox);
  177.         Slider slider = new BasicSlider("Test");
  178.         slider.setContentSuffix("things");
  179.         slider.setValueDisplay(ValueDisplay.INTEGER);
  180.         testFrame.add(slider);
  181.         testFrame.add(new BasicProgressBar(50, 0, 100, 1, ValueDisplay.PERCENTAGE));
  182.  
  183.         testFrame.setX(50);
  184.         testFrame.setY(50);
  185.         Dimension defaultDimension = theme.getUIForComponent(testFrame).getDefaultSize(testFrame);
  186.         testFrame.setWidth(defaultDimension.width);
  187.         testFrame.setHeight(defaultDimension.height);
  188.         testFrame.layoutChildren();
  189.         testFrame.setVisible(true);
  190.         testFrame.setMinimized(true);
  191.         addFrame(testFrame);
  192.     }
  193.  
  194.     @Override
  195.     protected void resizeComponents() {
  196.         Theme theme = getTheme();
  197.         Frame[] frames = getFrames();
  198.         Button enable = new BasicButton("Enable");
  199.         Button disable = new BasicButton("Disable");
  200.         Dimension enableSize = theme.getUIForComponent(enable).getDefaultSize(enable);
  201.         Dimension disableSize = theme.getUIForComponent(disable).getDefaultSize(disable);
  202.         int buttonWidth = Math.max(enableSize.width, disableSize.width);
  203.         int buttonHeight = Math.max(enableSize.height, disableSize.height);
  204.         for(Frame frame : frames) {
  205.             if(frame instanceof ModuleFrame) {
  206.                 for(Component component : frame.getChildren()) {
  207.                     if(component instanceof Button) {
  208.                         component.setWidth(buttonWidth);
  209.                         component.setHeight(buttonHeight);
  210.                     }
  211.                 }
  212.             }
  213.         }
  214.         recalculateSizes();
  215.     }
  216.  
  217.     private Dimension recalculateSizes() {
  218.         Frame[] frames = getFrames();
  219.         int maxWidth = 0, maxHeight = 0;
  220.         for(Frame frame : frames) {
  221.             Dimension defaultDimension = frame.getTheme().getUIForComponent(frame).getDefaultSize(frame);
  222.             maxWidth = Math.max(maxWidth, defaultDimension.width);
  223.             frame.setHeight(defaultDimension.height);
  224.             if(frame.isMinimized()) {
  225.                 for(Rectangle area : frame.getTheme().getUIForComponent(frame).getInteractableRegions(frame))
  226.                     maxHeight = Math.max(maxHeight, area.height);
  227.             } else
  228.                 maxHeight = Math.max(maxHeight, defaultDimension.height);
  229.         }
  230.         for(Frame frame : frames) {
  231.             frame.setWidth(maxWidth);
  232.             frame.layoutChildren();
  233.         }
  234.         return new Dimension(maxWidth, maxHeight);
  235.     }
  236. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement