|
| 1 | +package com.cleanroommc.modularui.test; |
| 2 | + |
| 3 | +import com.cleanroommc.modularui.api.drawable.IDrawable; |
| 4 | +import com.cleanroommc.modularui.api.drawable.IKey; |
| 5 | +import com.cleanroommc.modularui.drawable.GuiDraw; |
| 6 | +import com.cleanroommc.modularui.drawable.GuiTextures; |
| 7 | +import com.cleanroommc.modularui.drawable.Rectangle; |
| 8 | +import com.cleanroommc.modularui.screen.CustomModularScreen; |
| 9 | +import com.cleanroommc.modularui.screen.ModularPanel; |
| 10 | +import com.cleanroommc.modularui.screen.viewport.GuiContext; |
| 11 | +import com.cleanroommc.modularui.screen.viewport.ModularGuiContext; |
| 12 | +import com.cleanroommc.modularui.theme.WidgetTheme; |
| 13 | +import com.cleanroommc.modularui.utils.Alignment; |
| 14 | +import com.cleanroommc.modularui.utils.Color; |
| 15 | +import com.cleanroommc.modularui.utils.Platform; |
| 16 | +import com.cleanroommc.modularui.value.BoolValue; |
| 17 | +import com.cleanroommc.modularui.value.DoubleValue; |
| 18 | +import com.cleanroommc.modularui.value.EnumValue; |
| 19 | +import com.cleanroommc.modularui.widget.Widget; |
| 20 | +import com.cleanroommc.modularui.widgets.CycleButtonWidget; |
| 21 | +import com.cleanroommc.modularui.widgets.SliderWidget; |
| 22 | +import com.cleanroommc.modularui.widgets.ToggleButton; |
| 23 | +import com.cleanroommc.modularui.widgets.layout.Flow; |
| 24 | + |
| 25 | +import net.minecraft.client.Minecraft; |
| 26 | +import net.minecraft.client.renderer.GlStateManager; |
| 27 | +import net.minecraft.client.renderer.RenderHelper; |
| 28 | +import net.minecraft.client.renderer.RenderItem; |
| 29 | +import net.minecraft.init.Blocks; |
| 30 | +import net.minecraft.item.ItemStack; |
| 31 | + |
| 32 | +import org.jetbrains.annotations.NotNull; |
| 33 | + |
| 34 | +import java.util.Locale; |
| 35 | + |
| 36 | +public class GLTestGui extends CustomModularScreen { |
| 37 | + |
| 38 | + private static final ItemStack ITEM = new ItemStack(Blocks.CHEST); |
| 39 | + private static final int COLOR = Color.withAlpha(Color.RED.brighter(0), 0.75f); |
| 40 | + |
| 41 | + private RenderObject ro1; |
| 42 | + private RenderObject ro2; |
| 43 | + |
| 44 | + @Override |
| 45 | + public @NotNull ModularPanel buildUI(ModularGuiContext context) { |
| 46 | + this.ro1 = new RenderObject(); |
| 47 | + this.ro2 = new RenderObject(); |
| 48 | + return new ModularPanel("gl_test") |
| 49 | + .size(250) |
| 50 | + .padding(7) |
| 51 | + .child(Flow.column() |
| 52 | + .child(Flow.row() |
| 53 | + .fullWidth() |
| 54 | + .coverChildrenHeight() |
| 55 | + .child(buildRenderObjectConfig(this.ro1) |
| 56 | + .marginRight(2)) |
| 57 | + .child(new Rectangle().setColor(Color.TEXT_COLOR_DARK).asWidget() |
| 58 | + .width(1) |
| 59 | + .fullHeight()) |
| 60 | + .child(buildRenderObjectConfig(this.ro2) |
| 61 | + .marginLeft(2))) |
| 62 | + .child(createPreview().expanded())); |
| 63 | + |
| 64 | + } |
| 65 | + |
| 66 | + private Flow buildRenderObjectConfig(RenderObject ro) { |
| 67 | + return Flow.column() |
| 68 | + .widthRel(0.5f) |
| 69 | + .coverChildrenHeight() |
| 70 | + .child(new CycleButtonWidget() |
| 71 | + .value(new EnumValue.Dynamic<>(Type.class, () -> ro.type, val -> ro.type = val)) |
| 72 | + .widthRel(1f) |
| 73 | + .height(14) |
| 74 | + .overlay(IKey.dynamic(() -> "Type: " + ro.type.name().toLowerCase(Locale.ROOT)))) |
| 75 | + .child(new CycleButtonWidget() |
| 76 | + .value(new EnumValue.Dynamic<>(Lighting.class, () -> ro.lighting, val -> ro.lighting = val)) |
| 77 | + .widthRel(1f) |
| 78 | + .height(14) |
| 79 | + .overlay(IKey.dynamic(() -> "Lighting: " + ro.lighting.name().toLowerCase(Locale.ROOT)))) |
| 80 | + .child(new SliderWidget() |
| 81 | + .widthRel(1f) |
| 82 | + .height(14) |
| 83 | + .bounds(140, 180) |
| 84 | + .value(new DoubleValue.Dynamic(() -> ro.zLevel, val -> ro.zLevel = (float) val))) |
| 85 | + .child(Flow.row() |
| 86 | + .widthRel(1f) |
| 87 | + .coverChildrenHeight() |
| 88 | + .margin(0, 1) |
| 89 | + .mainAxisAlignment(Alignment.MainAxis.SPACE_BETWEEN) |
| 90 | + .child(IKey.str("enableDepth()").asWidget()) |
| 91 | + .child(new ToggleButton() |
| 92 | + .size(14) |
| 93 | + .stateBackground(GuiTextures.CHECK_BOX) |
| 94 | + .value(new BoolValue.Dynamic(() -> ro.depth, val -> ro.depth = val)))) |
| 95 | + .child(Flow.row() |
| 96 | + .widthRel(1f) |
| 97 | + .coverChildrenHeight() |
| 98 | + .margin(0, 1) |
| 99 | + .mainAxisAlignment(Alignment.MainAxis.SPACE_BETWEEN) |
| 100 | + .child(IKey.str("enableBlend()").asWidget()) |
| 101 | + .child(new ToggleButton() |
| 102 | + .size(14) |
| 103 | + .stateBackground(GuiTextures.CHECK_BOX) |
| 104 | + .value(new BoolValue.Dynamic(() -> ro.blend, val -> ro.blend = val)))) |
| 105 | + .child(Flow.row() |
| 106 | + .widthRel(1f) |
| 107 | + .coverChildrenHeight() |
| 108 | + .margin(0, 1) |
| 109 | + .mainAxisAlignment(Alignment.MainAxis.SPACE_BETWEEN) |
| 110 | + .child(IKey.str("enableTexture2D()").asWidget()) |
| 111 | + .child(new ToggleButton() |
| 112 | + .size(14) |
| 113 | + .stateBackground(GuiTextures.CHECK_BOX) |
| 114 | + .value(new BoolValue.Dynamic(() -> ro.texture, val -> ro.texture = val)))); |
| 115 | + } |
| 116 | + |
| 117 | + private Widget<?> createPreview() { |
| 118 | + return ((IDrawable) (context, x, y, width, height, widgetTheme) -> { |
| 119 | + ro1.draw(context, x, y, width, height, widgetTheme); |
| 120 | + ro2.draw(context, x, y, width, height, widgetTheme); |
| 121 | + }).asIcon().size(50).asWidget(); |
| 122 | + } |
| 123 | + |
| 124 | + private static void drawItem(GuiContext context, int x, int y, int width, int height, WidgetTheme widgetTheme) { |
| 125 | + GlStateManager.translate(x, y, 0); |
| 126 | + GlStateManager.scale(width / 16f, height / 16f, 1); |
| 127 | + RenderItem renderItem = Minecraft.getMinecraft().getRenderItem(); |
| 128 | + renderItem.zLevel = 0; |
| 129 | + renderItem.renderItemAndEffectIntoGUI(Platform.getClientPlayer(), ITEM, 0, 0); |
| 130 | + renderItem.zLevel = 0; |
| 131 | + } |
| 132 | + |
| 133 | + private static void drawColor(GuiContext context, int x0, int y0, int w, int h, WidgetTheme widgetTheme) { |
| 134 | + float x1 = x0 + w, y1 = y0 + h; |
| 135 | + float r = Color.getRedF(COLOR); |
| 136 | + float g = Color.getGreenF(COLOR); |
| 137 | + float b = Color.getBlueF(COLOR); |
| 138 | + float a = Color.getAlphaF(COLOR); |
| 139 | + Platform.startDrawing(Platform.DrawMode.QUADS, Platform.VertexFormat.POS_COLOR, bufferBuilder -> { |
| 140 | + bufferBuilder.pos(x0, y0, 0.0f).color(r, g, b, a).endVertex(); |
| 141 | + bufferBuilder.pos(x0, y1, 0.0f).color(r, g, b, a).endVertex(); |
| 142 | + bufferBuilder.pos(x1, y1, 0.0f).color(r, g, b, a).endVertex(); |
| 143 | + bufferBuilder.pos(x1, y0, 0.0f).color(r, g, b, a).endVertex(); |
| 144 | + }); |
| 145 | + } |
| 146 | + |
| 147 | + private static class RenderObject implements IDrawable { |
| 148 | + |
| 149 | + private Type type = Type.NONE; |
| 150 | + private Lighting lighting = Lighting.NONE; |
| 151 | + private float zLevel = 0; |
| 152 | + private boolean depth = false; |
| 153 | + private boolean blend = false; |
| 154 | + private boolean texture = false; |
| 155 | + |
| 156 | + |
| 157 | + @Override |
| 158 | + public void draw(GuiContext context, int x, int y, int width, int height, WidgetTheme widgetTheme) { |
| 159 | + GlStateManager.pushMatrix(); |
| 160 | + GlStateManager.translate(0, 0, zLevel); |
| 161 | + if (depth) GlStateManager.enableDepth(); |
| 162 | + if (blend) GlStateManager.enableBlend(); |
| 163 | + if (texture) GlStateManager.enableTexture2D(); |
| 164 | + lighting.enable.run(); |
| 165 | + type.render.draw(context, x, y, width, height, widgetTheme); |
| 166 | + lighting.disable.run(); |
| 167 | + if (texture) GlStateManager.disableTexture2D(); |
| 168 | + if (blend) GlStateManager.disableBlend(); |
| 169 | + if (depth) GlStateManager.disableDepth(); |
| 170 | + GlStateManager.popMatrix(); |
| 171 | + } |
| 172 | + } |
| 173 | + |
| 174 | + private enum Type { |
| 175 | + NONE((ctx, x, y, w, h, wt) -> {}), |
| 176 | + TEXTURE((ctx, x, y, w, h, wt) -> GuiDraw.drawTexture(x, y, w, h, 0f, 0f, 1f, 1f)), |
| 177 | + ITEM(GLTestGui::drawItem), |
| 178 | + COLOR(GLTestGui::drawColor); |
| 179 | + |
| 180 | + private final IDrawable render; |
| 181 | + |
| 182 | + Type(IDrawable render) {this.render = render;} |
| 183 | + } |
| 184 | + |
| 185 | + private enum Lighting { |
| 186 | + NONE(() -> {}, () -> {}), |
| 187 | + NORMAL(GlStateManager::enableLighting, GlStateManager::disableLighting), |
| 188 | + STANDARD(RenderHelper::enableStandardItemLighting, RenderHelper::disableStandardItemLighting), |
| 189 | + GUI_ITEM(RenderHelper::enableGUIStandardItemLighting, RenderHelper::disableStandardItemLighting); |
| 190 | + |
| 191 | + private final Runnable enable; |
| 192 | + private final Runnable disable; |
| 193 | + |
| 194 | + Lighting(Runnable enable, Runnable disable) { |
| 195 | + this.enable = enable; |
| 196 | + this.disable = disable; |
| 197 | + } |
| 198 | + } |
| 199 | +} |
0 commit comments