• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

CyclopsMC / IntegratedDynamics / 15141741537

20 May 2025 03:32PM UTC coverage: 45.27% (-0.04%) from 45.307%
15141741537

push

github

rubensworks
Bump mod version

2564 of 8413 branches covered (30.48%)

Branch coverage included in aggregate %.

11726 of 23153 relevant lines covered (50.65%)

2.4 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/main/java/org/cyclops/integrateddynamics/client/gui/container/ContainerScreenLogicProgrammerBase.java
1
package org.cyclops.integrateddynamics.client.gui.container;
2

3
import com.google.common.collect.Lists;
4
import com.mojang.blaze3d.platform.InputConstants;
5
import com.mojang.blaze3d.systems.RenderSystem;
6
import net.minecraft.client.gui.Font;
7
import net.minecraft.client.gui.GuiGraphics;
8
import net.minecraft.client.renderer.texture.TextureManager;
9
import net.minecraft.network.chat.Component;
10
import net.minecraft.resources.ResourceLocation;
11
import net.minecraft.world.entity.player.Inventory;
12
import net.minecraft.world.item.ItemStack;
13
import org.apache.commons.lang3.tuple.Triple;
14
import org.cyclops.cyclopscore.client.gui.component.button.ButtonText;
15
import org.cyclops.cyclopscore.client.gui.component.input.WidgetTextFieldExtended;
16
import org.cyclops.cyclopscore.client.gui.container.ContainerScreenScrolling;
17
import org.cyclops.cyclopscore.helper.Helpers;
18
import org.cyclops.cyclopscore.helper.MinecraftHelpers;
19
import org.cyclops.cyclopscore.helper.RenderHelpers;
20
import org.cyclops.integrateddynamics.IntegratedDynamics;
21
import org.cyclops.integrateddynamics.Reference;
22
import org.cyclops.integrateddynamics.RegistryEntries;
23
import org.cyclops.integrateddynamics.api.client.gui.subgui.IGuiInputElement;
24
import org.cyclops.integrateddynamics.api.logicprogrammer.ILogicProgrammerElement;
25
import org.cyclops.integrateddynamics.api.logicprogrammer.ILogicProgrammerElementType;
26
import org.cyclops.integrateddynamics.core.client.gui.subgui.SubGuiHolder;
27
import org.cyclops.integrateddynamics.core.evaluate.variable.gui.GuiElementValueTypeString;
28
import org.cyclops.integrateddynamics.core.helper.L10NValues;
29
import org.cyclops.integrateddynamics.core.logicprogrammer.LogicProgrammerElementTypes;
30
import org.cyclops.integrateddynamics.core.logicprogrammer.RenderPattern;
31
import org.cyclops.integrateddynamics.inventory.container.ContainerLogicProgrammerBase;
32
import org.cyclops.integrateddynamics.network.packet.LogicProgrammerActivateElementPacket;
33
import org.cyclops.integrateddynamics.network.packet.LogicProgrammerLabelPacket;
34
import org.cyclops.integrateddynamics.proxy.ClientProxy;
35
import org.lwjgl.glfw.GLFW;
36

37
import java.awt.*;
38
import java.util.List;
39

40
/**
41
 * Base gui for the logic programmer.
42
 * @author rubensworks
43
 */
44
public class ContainerScreenLogicProgrammerBase<C extends ContainerLogicProgrammerBase> extends ContainerScreenScrolling<C> {
45

46
    public static final int BOX_HEIGHT = 18;
47
    private static final Rectangle ITEM_POSITION = new Rectangle(19, 18, 56, BOX_HEIGHT - 1);
×
48

49
    protected final SubGuiHolder subGuiHolder = new SubGuiHolder();
×
50
    private final boolean hasLabeller;
51
    protected RenderPattern operatorConfigPattern = null;
×
52
    protected SubGuiOperatorInfo operatorInfoPattern = null;
×
53
    protected boolean firstInit = true;
×
54
    protected int relativeStep = -1;
×
55
    protected boolean swallowNextCharacter = false;
×
56

57
    public ContainerScreenLogicProgrammerBase(C container, Inventory playerInventory, Component title) {
58
        super(container, playerInventory, title);
×
59
        container.setGui(this);
×
60

61
        this.hasLabeller = playerInventory.contains(new ItemStack(RegistryEntries.ITEM_LABELLER));
×
62
    }
×
63

64
    @Override
65
    protected Rectangle getScrollRegion() {
66
        return new Rectangle(this.leftPos + 19, this.topPos + 18, 57, 178);
×
67
    }
68

69
    @Override
70
    public void init() {
71
        super.init();
×
72
        subGuiHolder.init(this.leftPos, this.topPos);
×
73
        if (firstInit) {
×
74
            setSearchFieldFocussed(true);
×
75
            firstInit = false;
×
76
        }
77
    }
×
78

79
    @Override
80
    public void containerTick() {
81
        super.containerTick();
×
82
        subGuiHolder.tick();
×
83
    }
×
84

85
    protected int getScrollX() {
86
        return 5;
×
87
    }
88

89
    protected int getScrollY() {
90
        return 18;
×
91
    }
92

93
    protected int getScrollHeight() {
94
        return 178;
×
95
    }
96

97
    @Override
98
    protected int getBaseXSize() {
99
        return 256;
×
100
    }
101

102
    @Override
103
    protected int getBaseYSize() {
104
        return 240;
×
105
    }
106

107
    @Override
108
    protected int getSearchX() {
109
        return 6;
×
110
    }
111

112
    protected int getSearchWidth() {
113
        return 70;
×
114
    }
115

116
    @Override
117
    protected ResourceLocation constructGuiTexture() {
118
        return ResourceLocation.fromNamespaceAndPath(Reference.MOD_ID, "textures/gui/logic_programmer.png");
×
119
    }
120

121
    protected float colorSmoothener(float color, boolean hover) {
122
        return 1F - ((1F - color) / (hover ? 2F : 4F));
×
123
    }
124

125
    @Override
126
    protected void renderBg(GuiGraphics guiGraphics, float partialTicks, int mouseX, int mouseY) {
127
        super.renderBg(guiGraphics, partialTicks, mouseX, mouseY);
×
128
        subGuiHolder.renderBg(guiGraphics, this.leftPos, this.topPos, getMinecraft().getTextureManager(), font, partialTicks, mouseX, mouseY);
×
129

130
        // Draw container name
131
        font.drawInBatch(Component.translatable(L10NValues.GUI_LOGICPROGRAMMER_FILTER),
×
132
                this.leftPos + offsetX + 5, this.topPos + offsetY + 208, Helpers.RGBToInt(80, 80, 80),
×
133
                false, guiGraphics.pose().last().pose(), guiGraphics.bufferSource(), Font.DisplayMode.NORMAL, 0, 15728880);
×
134

135
        // Draw operators
136
        ContainerLogicProgrammerBase container = (ContainerLogicProgrammerBase) getMenu();
×
137
        int boxHeight = BOX_HEIGHT;
×
138
        for(int i = 0; i < container.getPageSize(); i++) {
×
139
            if(container.isElementVisible(i)) {
×
140
                ILogicProgrammerElement element = container.getVisibleElement(i);
×
141

142
                Triple<Float, Float, Float> rgb = Helpers.intToRGB(element.getColor());
×
143
                boolean hover = LogicProgrammerElementTypes.areEqual(container.getActiveElement(), element)
×
144
                        || isPointInRegion(getElementPosition(container, i, false), new Point(mouseX, mouseY));
×
145
                RenderSystem.setShaderColor(colorSmoothener(rgb.getLeft(), hover), colorSmoothener(rgb.getMiddle(), hover),
×
146
                        colorSmoothener(rgb.getRight(), hover), 1);
×
147

148
                // Background
149
                guiGraphics.blit(texture, leftPos + offsetX + ITEM_POSITION.x,
×
150
                        topPos + offsetY + ITEM_POSITION.y + boxHeight * i, 19, 18, ITEM_POSITION.width, ITEM_POSITION.height);
151

152
                // Arrow
153
                if(hover) {
×
154
                    guiGraphics.blit(texture, leftPos + offsetX + ITEM_POSITION.x,
×
155
                            topPos + offsetY + ITEM_POSITION.y + boxHeight * i, 0, 240, 3, 16);
156
                }
157
                RenderSystem.setShaderColor(1, 1, 1, 1);
×
158

159
                // Operator info
160
                String aspectName = element.getSymbol();
×
161
                RenderHelpers.drawScaledCenteredString(guiGraphics.pose(), guiGraphics.bufferSource(), font, aspectName,
×
162
                        this.leftPos + offsetX + (hover ? 22 : 21),
×
163
                        this.topPos + offsetY + 26 + boxHeight * i,
164
                        53, Helpers.RGBToInt(40, 40, 40), false, Font.DisplayMode.NORMAL);
×
165
            }
166
        }
167
    }
×
168

169
    protected Rectangle getElementPosition(ContainerLogicProgrammerBase container, int i, boolean absolute) {
170
        return new Rectangle(ITEM_POSITION.x + offsetX + (absolute ? this.leftPos : 0),
×
171
                ITEM_POSITION.y + BOX_HEIGHT * i + offsetY + (absolute ? this.topPos : 0),
×
172
                ITEM_POSITION.width, ITEM_POSITION.height
173
        );
174
    }
175

176
    @Override
177
    protected void renderLabels(GuiGraphics guiGraphics, int mouseX, int mouseY) {
178
        // super.drawGuiContainerForegroundLayer(matrixStack, mouseX, mouseY);
179
        subGuiHolder.drawGuiContainerForegroundLayer(guiGraphics, this.leftPos, this.topPos, getMinecraft().getTextureManager(), font, mouseX, mouseY);
×
180
        // Draw operator tooltips
181
        ContainerLogicProgrammerBase container = getMenu();
×
182
        for(int i = 0; i < container.getPageSize(); i++) {
×
183
            if(container.isElementVisible(i)) {
×
184
                ILogicProgrammerElement element = container.getVisibleElement(i);
×
185
                if(isPointInRegion(getElementPosition(container, i, false), new Point(mouseX, mouseY))) {
×
186
                    List<Component> lines = Lists.newLinkedList();
×
187
                    element.loadTooltip(lines);
×
188
                    drawTooltip(lines, guiGraphics.pose(), mouseX - this.leftPos, mouseY - this.topPos);
×
189
                }
190
            }
191
        }
192
    }
×
193

194
    protected void onActivateElement(ILogicProgrammerElement<RenderPattern, ContainerScreenLogicProgrammerBase<?>, ContainerLogicProgrammerBase> element) {
195
        subGuiHolder.addSubGui(operatorInfoPattern = new SubGuiOperatorInfo(element));
×
196
        operatorInfoPattern.init(leftPos, topPos);
×
197
        subGuiHolder.addSubGui(operatorConfigPattern = element.createSubGui(88, 18, 160, 87, this, (ContainerLogicProgrammerBase) getMenu()));
×
198
        operatorConfigPattern.init(leftPos, topPos);
×
199
    }
×
200

201
    protected void onDeactivateElement(ILogicProgrammerElement element) {
202
        subGuiHolder.clear();
×
203
    }
×
204

205
    public boolean handleElementActivation(ILogicProgrammerElement element) {
206
        boolean activate = false;
×
207
        ContainerLogicProgrammerBase container = getMenu();
×
208
        ILogicProgrammerElement newActive = null;
×
209
        onDeactivateElement(element);
×
210
        if(container.getActiveElement() != element) {
×
211
            activate = true;
×
212
            newActive = element;
×
213
            if(element != null) {
×
214
                onActivateElement(element);
×
215
            }
216
        }
217
        container.setActiveElement(newActive,
×
218
                operatorConfigPattern == null ? 0 : operatorConfigPattern.getX(),
×
219
                operatorConfigPattern == null ? 0 : operatorConfigPattern.getY());
×
220
        if(newActive != null) {
×
221
            ILogicProgrammerElementType type = newActive.getType();
×
222
            IntegratedDynamics._instance.getPacketHandler().sendToServer(
×
223
                    new LogicProgrammerActivateElementPacket(type.getUniqueName(), type.getName(newActive)));
×
224
        } else {
×
225
            IntegratedDynamics._instance.getPacketHandler().sendToServer(
×
226
                    new LogicProgrammerActivateElementPacket(ResourceLocation.parse(""), ResourceLocation.parse("")));
×
227
        }
228
        return activate;
×
229
    }
230

231
    protected void setSearchFieldFocussed(boolean focused) {
232
        getSearchField().setFocused(focused);
×
233
    }
×
234

235
    protected boolean isSearchFieldFocussed() {
236
        return getSearchField().isFocused();
×
237
    }
238

239
    protected boolean selectPageElement(int elementId) {
240
        ContainerLogicProgrammerBase container = getMenu();
×
241

242
        // Deactivate current element
243
        if (elementId < 0) {
×
244
            handleElementActivation(container.getActiveElement());
×
245
            return false;
×
246
        }
247

248
        // Activate a new element
249
        for(int i = 0; i < container.getPageSize(); i++) {
×
250
            if (container.isElementVisible(i)) {
×
251
                if (elementId-- == 0) {
×
252
                    ILogicProgrammerElement element = container.getVisibleElement(i);
×
253
                    if (container.getActiveElement() != element) {
×
254
                        handleElementActivation(element);
×
255
                    }
256
                    return true;
×
257
                }
258
            }
259
        }
260
        return false;
×
261
    }
262

263
    protected boolean handleKeyCode(int keyCode, int scanCode) {
264
        InputConstants.Key inputCode = InputConstants.getKey(keyCode, scanCode);
×
265
        if(keyCode != GLFW.GLFW_KEY_LEFT_SHIFT && keyCode != GLFW.GLFW_KEY_RIGHT_SHIFT) {
×
266
            ContainerLogicProgrammerBase container = getMenu();
×
267
            int pageSize = container.getPageSize();
×
268
            int stepModifier = MinecraftHelpers.isShifted() ? pageSize - 1 : 1;
×
269
            boolean isElementFocused = container.getActiveElement() != null && container.getActiveElement().isFocused(operatorConfigPattern);
×
270

271
            if (ClientProxy.FOCUS_LP_SEARCH.isActiveAndMatches(inputCode)) {
×
272
                // Focus search field
273
                setSearchFieldFocussed(true);
×
274
                swallowNextCharacter = true;
×
275
                return true;
×
276
            } else if (isElementFocused && ClientProxy.FOCUS_LP_RENAME.isActiveAndMatches(inputCode) && hasLabeller()) {
×
277
                // Open labeller gui
278
                operatorInfoPattern.onButtonEditClick();
×
279
                swallowNextCharacter = true;
×
280
                return true;
×
281
            } else if (GLFW.GLFW_KEY_LEFT == keyCode && (!isElementFocused && isSearchFieldFocussed())) {
×
282
                // Unfocus search field
283
                setSearchFieldFocussed(isSearchFieldFocussed());
×
284
                return true;
×
285
            } else if (!isElementFocused && GLFW.GLFW_KEY_DOWN == keyCode) {
×
286
                // Scroll down
287
                if (!selectPageElement(relativeStep += stepModifier)) {
×
288
                    relativeStep -= stepModifier;
×
289
                    if (relativeStep > 0) {
×
290
                        getScrollbar().scrollRelative(-stepModifier);
×
291
                        selectPageElement(relativeStep);
×
292
                    }
293
                }
294
                return true;
×
295
            } else if (!isElementFocused && GLFW.GLFW_KEY_UP == keyCode) {
×
296
                // Scroll up
297
                if (!(relativeStep >= 0 && selectPageElement(relativeStep -= stepModifier))) {
×
298
                    getScrollbar().scrollRelative(stepModifier);
×
299
                    selectPageElement(relativeStep = 0);
×
300
                }
301
                return true;
×
302
            } else if (!isElementFocused
×
303
                    && (GLFW.GLFW_KEY_RIGHT == keyCode || GLFW.GLFW_KEY_TAB == keyCode
304
                    || GLFW.GLFW_KEY_ENTER == keyCode || GLFW.GLFW_KEY_KP_ENTER == keyCode)) {
305
                if (container.getActiveElement() != null) {
×
306
                    container.getActiveElement().setFocused(operatorConfigPattern, true);
×
307
                    setSearchFieldFocussed(false);
×
308
                }
309
                return true;
×
310
            }
311
        }
312
        return false;
×
313
    }
314

315
    @Override
316
    public boolean charTyped(char keyCode, int scanCode) {
317
        if (swallowNextCharacter) {
×
318
            swallowNextCharacter = false;
×
319
            return true;
×
320
        }
321

322
        return subGuiHolder.charTyped(keyCode, scanCode) || handleKeyCode(keyCode, scanCode) || super.charTyped(keyCode, scanCode);
×
323
    }
324

325
    @Override
326
    public boolean keyPressed(int keyCode, int scanCode, int modifiers) {
327
        if (keyCode != GLFW.GLFW_KEY_ESCAPE) {
×
328
            if (this.subGuiHolder.keyPressed(keyCode, scanCode, modifiers) || handleKeyCode(keyCode, scanCode)) {
×
329
                return true;
×
330
            }
331
        }
332
        return super.keyPressed(keyCode, scanCode, modifiers);
×
333
    }
334

335
    @Override
336
    public boolean mouseClicked(double mouseX, double mouseY, int mouseButton) {
337
        if (subGuiHolder.mouseClicked(mouseX, mouseY, mouseButton)) {
×
338
            if (isSearchFieldFocussed()) {
×
339
                setSearchFieldFocussed(false);
×
340
            }
341
            return true;
×
342
        }
343

344
        ContainerLogicProgrammerBase container = getMenu();
×
345
        for(int i = 0; i < container.getPageSize(); i++) {
×
346
            if (container.isElementVisible(i)) {
×
347
                ILogicProgrammerElement element = container.getVisibleElement(i);
×
348
                if (isPointInRegion(getElementPosition(container, i, false), new Point((int) mouseX, (int) mouseY))) {
×
349
                    boolean activated = handleElementActivation(element);
×
350
                    relativeStep = activated ? i : -1;
×
351
                    if (activated) {
×
352
                        container.getActiveElement().setFocused(operatorConfigPattern, true);
×
353
                        setSearchFieldFocussed(false);
×
354
                        return true;
×
355
                    }
356
                }
357
            }
358
        }
359
        boolean superRet = super.mouseClicked(mouseX, mouseY, mouseButton);
×
360

361
        // If the search box has been selected, de-active the current element.
362
        if(isSearchFieldFocussed() &&
×
363
                container.getActiveElement() != null && container.getActiveElement().isFocused(operatorConfigPattern)) {
×
364
            container.getActiveElement().setFocused(operatorConfigPattern, false);
×
365
            return true;
×
366
        }
367

368
        return superRet;
×
369
    }
370

371
    protected void label(String label) {
372
        IntegratedDynamics._instance.getPacketHandler().sendToServer(new LogicProgrammerLabelPacket(label));
×
373
    }
×
374

375
    protected boolean hasLabeller() {
376
        return this.hasLabeller;
×
377
    }
378

379
    public class SubGuiOperatorInfo extends GuiElementValueTypeString.SubGuiValueTypeInfo<RenderPattern, ContainerScreenLogicProgrammerBase<?>, ContainerLogicProgrammerBase> {
380

381
        private WidgetTextFieldExtended searchField;
382
        private ButtonText button = null;
×
383

384
        public SubGuiOperatorInfo(IGuiInputElement<RenderPattern, ContainerScreenLogicProgrammerBase<?>, ContainerLogicProgrammerBase> element) {
×
385
            super(ContainerScreenLogicProgrammerBase.this, getMenu(), element, 88, 106, 139, 20);
×
386

387
            if(hasLabeller()) {
×
388
                buttonList.add(button = new ButtonText(0, 0, 6, 10, Component.translatable("gui.integrateddynamics.button.edit"), Component.literal("E"),
×
389
                        (button) -> onButtonEditClick(), true));
×
390
            }
391

392
            int searchWidth = 113;
×
393
            this.searchField = new WidgetTextFieldExtended(ContainerScreenLogicProgrammerBase.this.font, 0, 0, searchWidth, 11,
×
394
                    Component.translatable("gui.cyclopscore.search"));
×
395
            this.searchField.setMaxLength(64);
×
396
            this.searchField.setBordered(true);
×
397
            this.searchField.setVisible(false);
×
398
            this.searchField.setTextColor(16777215);
×
399
            this.searchField.setCanLoseFocus(true);
×
400
            this.searchField.setValue("");
×
401
            this.searchField.setWidth(searchWidth);
×
402
        }
×
403

404
        @Override
405
        public void init(int guiLeft, int guiTop) {
406
            super.init(guiLeft, guiTop);
×
407
            int searchX = 90;
×
408
            int searchY = 110;
×
409
            this.searchField.setX(guiLeft + searchX);
×
410
            this.searchField.setY(guiTop + searchY);
×
411

412
            if (hasLabeller()) {
×
413
                button.setX(guiLeft + 220);
×
414
                button.setY(guiTop + 111);
×
415
            }
416
        }
×
417

418
        @Override
419
        protected boolean showError() {
420
            return container.canWriteActiveElementPre();
×
421
        }
422

423
        @Override
424
        protected Component getLastError() {
425
            return container.getLastError();
×
426
        }
427

428
        @Override
429
        protected ResourceLocation getTexture() {
430
            return texture;
×
431
        }
432

433
        @Override
434
        public boolean charTyped(char typedChar, int keyCode) {
435
            if (!this.searchField.isFocused() || !this.searchField.charTyped(typedChar, keyCode)) {
×
436
                return super.charTyped(typedChar, keyCode);
×
437
            } else {
438
                label(this.searchField.getValue());
×
439
                return true;
×
440
            }
441
        }
442

443
        @Override
444
        public boolean keyPressed(int typedChar, int keyCode, int modifiers) {
445
            if (this.searchField.isFocused() && typedChar != GLFW.GLFW_KEY_ESCAPE) {
×
446
                this.searchField.keyPressed(typedChar, keyCode, modifiers);
×
447
                label(this.searchField.getValue());
×
448
                return true;
×
449
            }
450
            return super.keyPressed(typedChar, keyCode, modifiers);
×
451
        }
452

453
        @Override
454
        public boolean mouseClicked(double mouseX, double mouseY, int mouseButton) {
455
            if(this.searchField.isVisible() && this.searchField.mouseClicked(mouseX, mouseY, mouseButton)) {
×
456
                return true;
×
457
            }
458
            return super.mouseClicked(mouseX, mouseY, mouseButton);
×
459
        }
460

461
        @Override
462
        public void renderBg(GuiGraphics guiGraphics, int guiLeft, int guiTop, TextureManager textureManager, Font font, float partialTicks, int mouseX, int mouseY) {
463
            super.renderBg(guiGraphics, guiLeft, guiTop, textureManager, font, partialTicks, mouseX, mouseY);
×
464
            this.searchField.render(guiGraphics, mouseX, mouseY, partialTicks);
×
465
        }
×
466

467
        @Override
468
        public boolean shouldRenderElementName() {
469
            return !this.searchField.isVisible();
×
470
        }
471

472
        public void onButtonEditClick() {
473
            this.searchField.setVisible(!this.searchField.isVisible());
×
474
            if(this.searchField.isVisible()) {
×
475
                this.searchField.setFocused(true);
×
476
                label(this.searchField.getValue());
×
477
            } else {
478
                label("");
×
479
            }
480
        }
×
481
    }
482

483
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc