|
| 1 | +/* |
| 2 | + * This file is part of Cubic World Generation, licensed under the MIT License (MIT). |
| 3 | + * |
| 4 | + * Copyright (c) 2015-2020 contributors |
| 5 | + * |
| 6 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | + * of this software and associated documentation files (the "Software"), to deal |
| 8 | + * in the Software without restriction, including without limitation the rights |
| 9 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | + * copies of the Software, and to permit persons to whom the Software is |
| 11 | + * furnished to do so, subject to the following conditions: |
| 12 | + * |
| 13 | + * The above copyright notice and this permission notice shall be included in |
| 14 | + * all copies or substantial portions of the Software. |
| 15 | + * |
| 16 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 22 | + * THE SOFTWARE. |
| 23 | + */ |
| 24 | +package io.github.opencubicchunks.cubicchunks.cubicgen.common.gui.component; |
| 25 | + |
| 26 | +import net.minecraft.client.Minecraft; |
| 27 | +import net.minecraft.client.gui.Gui; |
| 28 | +import net.minecraft.client.gui.GuiButton; |
| 29 | +import net.minecraft.client.renderer.GlStateManager; |
| 30 | +import net.minecraft.util.ResourceLocation; |
| 31 | + |
| 32 | +public class McGuiRender { |
| 33 | + protected static final ResourceLocation BUTTON_TEXTURES = new ResourceLocation("textures/gui/widgets.png"); |
| 34 | + |
| 35 | + private static final int BTN_TEX_MIN_X = 0; |
| 36 | + private static final int BTN_TEX_MIN_Y = 46; |
| 37 | + private static final int BTN_TEX_WIDTH = 200; |
| 38 | + private static final int BTN_TEX_HEIGHT = 20; |
| 39 | + private static final int WIDGETS_TEX_SIZE = 256; |
| 40 | + |
| 41 | + public static void prepareWidgetRender(Minecraft mc) { |
| 42 | + mc.getTextureManager().bindTexture(BUTTON_TEXTURES); |
| 43 | + GlStateManager.color(1.0F, 1.0F, 1.0F, 1.0F); |
| 44 | + GlStateManager.enableBlend(); |
| 45 | + GlStateManager.tryBlendFuncSeparate(GlStateManager.SourceFactor.SRC_ALPHA, GlStateManager.DestFactor.ONE_MINUS_SRC_ALPHA, GlStateManager.SourceFactor.ONE, GlStateManager.DestFactor.ZERO); |
| 46 | + GlStateManager.blendFunc(GlStateManager.SourceFactor.SRC_ALPHA, GlStateManager.DestFactor.ONE_MINUS_SRC_ALPHA); |
| 47 | + } |
| 48 | + |
| 49 | + public static void drawWidgetStringCentered(Minecraft mc, GuiButton btn, int colorOverride, boolean enabled, boolean hovered) { |
| 50 | + int color = 0xe0e0e0; |
| 51 | + if (colorOverride != 0) { |
| 52 | + color = colorOverride; |
| 53 | + } else if (!enabled) { |
| 54 | + color = 0xa0a0a0; |
| 55 | + } else if (hovered) { |
| 56 | + color = 0xffffa0; |
| 57 | + } |
| 58 | + btn.drawCenteredString(mc.fontRenderer, btn.displayString, btn.x + btn.width / 2, btn.y + (btn.height - 8) / 2, color); |
| 59 | + } |
| 60 | + |
| 61 | + public static void drawWidgetString(Minecraft mc, GuiButton btn, int colorOverride, boolean enabled, boolean hovered) { |
| 62 | + int color = 0xe0e0e0; |
| 63 | + if (colorOverride != 0) { |
| 64 | + color = colorOverride; |
| 65 | + } else if (!enabled) { |
| 66 | + color = 0xa0a0a0; |
| 67 | + } else if (hovered) { |
| 68 | + color = 0xffffa0; |
| 69 | + } |
| 70 | + btn.drawString(mc.fontRenderer, btn.displayString, btn.x, btn.y + (btn.height - 8) / 2, color); |
| 71 | + } |
| 72 | + |
| 73 | + public static void drawSliderBg(int x, int y, int width, int height) { |
| 74 | + drawButtonBg(ButtonMode.DISABLED, x, y, width, height); |
| 75 | + } |
| 76 | + |
| 77 | + public static void drawButtonBg(ButtonMode mode, int x, int y, int width, int height) { |
| 78 | + int yOffset = mode.ordinal() * BTN_TEX_HEIGHT; |
| 79 | + int texWidth = width / 2; |
| 80 | + if (texWidth > 180) { |
| 81 | + texWidth = 180; |
| 82 | + } |
| 83 | + Gui.drawScaledCustomSizeModalRect( |
| 84 | + x, y, |
| 85 | + BTN_TEX_MIN_X, BTN_TEX_MIN_Y + yOffset, |
| 86 | + texWidth, BTN_TEX_HEIGHT, |
| 87 | + width / 2, height, |
| 88 | + WIDGETS_TEX_SIZE, WIDGETS_TEX_SIZE |
| 89 | + ); |
| 90 | + Gui.drawScaledCustomSizeModalRect( |
| 91 | + x + width / 2, y, |
| 92 | + BTN_TEX_MIN_X + BTN_TEX_WIDTH - texWidth, BTN_TEX_MIN_Y + yOffset, |
| 93 | + texWidth, BTN_TEX_HEIGHT, |
| 94 | + width - width / 2, height, |
| 95 | + WIDGETS_TEX_SIZE, WIDGETS_TEX_SIZE |
| 96 | + ); |
| 97 | + } |
| 98 | + |
| 99 | + public enum ButtonMode { |
| 100 | + DISABLED, ENABLED, HOVERED |
| 101 | + } |
| 102 | +} |
0 commit comments