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

CyclopsMC / IntegratedDynamics / 13341940185

15 Feb 2025 04:28AM UTC coverage: 39.0% (-0.002%) from 39.002%
13341940185

push

github

rubensworks
Fix color overlays not showing

1950 of 8351 branches covered (23.35%)

Branch coverage included in aggregate %.

10280 of 23008 relevant lines covered (44.68%)

2.1 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 net.minecraft.client.gui.Font;
6
import net.minecraft.client.gui.GuiGraphics;
7
import net.minecraft.client.renderer.RenderType;
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.util.ARGB;
12
import net.minecraft.world.entity.player.Inventory;
13
import net.minecraft.world.item.ItemStack;
14
import org.apache.commons.lang3.tuple.Triple;
15
import org.cyclops.cyclopscore.client.gui.component.button.ButtonText;
16
import org.cyclops.cyclopscore.client.gui.component.input.WidgetTextFieldExtended;
17
import org.cyclops.cyclopscore.client.gui.container.ContainerScreenScrolling;
18
import org.cyclops.cyclopscore.helper.IModHelpers;
19
import org.cyclops.integrateddynamics.IntegratedDynamics;
20
import org.cyclops.integrateddynamics.Reference;
21
import org.cyclops.integrateddynamics.RegistryEntries;
22
import org.cyclops.integrateddynamics.api.client.gui.subgui.IGuiInputElement;
23
import org.cyclops.integrateddynamics.api.logicprogrammer.ILogicProgrammerElement;
24
import org.cyclops.integrateddynamics.api.logicprogrammer.ILogicProgrammerElementType;
25
import org.cyclops.integrateddynamics.core.client.gui.subgui.SubGuiHolder;
26
import org.cyclops.integrateddynamics.core.evaluate.variable.gui.GuiElementValueTypeString;
27
import org.cyclops.integrateddynamics.core.helper.L10NValues;
28
import org.cyclops.integrateddynamics.core.logicprogrammer.LogicProgrammerElementTypes;
29
import org.cyclops.integrateddynamics.core.logicprogrammer.RenderPattern;
30
import org.cyclops.integrateddynamics.inventory.container.ContainerLogicProgrammerBase;
31
import org.cyclops.integrateddynamics.network.packet.LogicProgrammerActivateElementPacket;
32
import org.cyclops.integrateddynamics.network.packet.LogicProgrammerLabelPacket;
33
import org.cyclops.integrateddynamics.proxy.ClientProxy;
34
import org.lwjgl.glfw.GLFW;
35

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

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

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

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

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

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

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

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

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

83
    protected int getScrollX() {
84
        return 5;
×
85
    }
86

87
    protected int getScrollY() {
88
        return 18;
×
89
    }
90

91
    protected int getScrollHeight() {
92
        return 178;
×
93
    }
94

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

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

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

110
    protected int getSearchWidth() {
111
        return 70;
×
112
    }
113

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

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

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

128
        // Draw container name
129
        guiGraphics.drawString(font, Component.translatable(L10NValues.GUI_LOGICPROGRAMMER_FILTER),
×
130
                this.leftPos + offsetX + 5, this.topPos + offsetY + 208, IModHelpers.get().getBaseHelpers().RGBToInt(80, 80, 80),
×
131
                false);
132

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

140
                Triple<Float, Float, Float> rgb = IModHelpers.get().getBaseHelpers().intToRGB(element.getColor());
×
141
                boolean hover = LogicProgrammerElementTypes.areEqual(container.getActiveElement(), element)
×
142
                        || isPointInRegion(getElementPosition(container, i, false), new Point(mouseX, mouseY));
×
143
                int color = ARGB.colorFromFloat(
×
144
                        1F,
145
                        colorSmoothener(rgb.getLeft(), hover),
×
146
                        colorSmoothener(rgb.getMiddle(), hover),
×
147
                        colorSmoothener(rgb.getRight(), hover)
×
148
                );
149

150
                // Background
151
                guiGraphics.blit(RenderType::guiTextured, texture, leftPos + offsetX + ITEM_POSITION.x,
×
152
                        topPos + offsetY + ITEM_POSITION.y + boxHeight * i, 19, 18, ITEM_POSITION.width, ITEM_POSITION.height, 256, 256, color);
153

154
                // Arrow
155
                if(hover) {
×
156
                    guiGraphics.blit(RenderType::guiTextured, texture, leftPos + offsetX + ITEM_POSITION.x,
×
157
                            topPos + offsetY + ITEM_POSITION.y + boxHeight * i, 0, 240, 3, 16, 256, 256, color);
158
                }
159

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

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

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

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

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

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

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

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

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

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

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

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

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

314
    @Override
315
    public boolean charTyped(char keyCode, int scanCode) {
316
        return subGuiHolder.charTyped(keyCode, scanCode) || handleKeyCode(keyCode, scanCode) || super.charTyped(keyCode, scanCode);
×
317
    }
318

319
    @Override
320
    public boolean keyPressed(int keyCode, int scanCode, int modifiers) {
321
        if (keyCode != GLFW.GLFW_KEY_ESCAPE) {
×
322
            if (this.subGuiHolder.keyPressed(keyCode, scanCode, modifiers) || handleKeyCode(keyCode, scanCode)) {
×
323
                return true;
×
324
            }
325
        }
326
        return super.keyPressed(keyCode, scanCode, modifiers);
×
327
    }
328

329
    @Override
330
    public boolean mouseClicked(double mouseX, double mouseY, int mouseButton) {
331
        if (subGuiHolder.mouseClicked(mouseX, mouseY, mouseButton)) {
×
332
            if (isSearchFieldFocussed()) {
×
333
                setSearchFieldFocussed(false);
×
334
            }
335
            return true;
×
336
        }
337

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

355
        // If the search box has been selected, de-active the current element.
356
        if(isSearchFieldFocussed() &&
×
357
                container.getActiveElement() != null && container.getActiveElement().isFocused(operatorConfigPattern)) {
×
358
            container.getActiveElement().setFocused(operatorConfigPattern, false);
×
359
            return true;
×
360
        }
361

362
        return superRet;
×
363
    }
364

365
    protected void label(String label) {
366
        IntegratedDynamics._instance.getPacketHandler().sendToServer(new LogicProgrammerLabelPacket(label));
×
367
    }
×
368

369
    protected boolean hasLabeller() {
370
        return this.hasLabeller;
×
371
    }
372

373
    public class SubGuiOperatorInfo extends GuiElementValueTypeString.SubGuiValueTypeInfo<RenderPattern, ContainerScreenLogicProgrammerBase<?>, ContainerLogicProgrammerBase> {
374

375
        private WidgetTextFieldExtended searchField;
376
        private ButtonText button = null;
×
377

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

381
            if(hasLabeller()) {
×
382
                buttonList.add(button = new ButtonText(0, 0, 6, 10, Component.translatable("gui.integrateddynamics.button.edit"), Component.literal("E"),
×
383
                        (button) -> onButtonEditClick(), true));
×
384
            }
385

386
            int searchWidth = 113;
×
387
            this.searchField = new WidgetTextFieldExtended(ContainerScreenLogicProgrammerBase.this.font, 0, 0, searchWidth, 11,
×
388
                    Component.translatable("gui.cyclopscore.search"));
×
389
            this.searchField.setMaxLength(64);
×
390
            this.searchField.setBordered(true);
×
391
            this.searchField.setVisible(false);
×
392
            this.searchField.setTextColor(16777215);
×
393
            this.searchField.setCanLoseFocus(true);
×
394
            this.searchField.setValue("");
×
395
            this.searchField.setWidth(searchWidth);
×
396
        }
×
397

398
        @Override
399
        public void init(int guiLeft, int guiTop) {
400
            super.init(guiLeft, guiTop);
×
401
            int searchX = 90;
×
402
            int searchY = 110;
×
403
            this.searchField.setX(guiLeft + searchX);
×
404
            this.searchField.setY(guiTop + searchY);
×
405

406
            if (hasLabeller()) {
×
407
                button.setX(guiLeft + 220);
×
408
                button.setY(guiTop + 111);
×
409
            }
410
        }
×
411

412
        @Override
413
        protected boolean showError() {
414
            return container.canWriteActiveElementPre();
×
415
        }
416

417
        @Override
418
        protected Component getLastError() {
419
            return container.getLastError();
×
420
        }
421

422
        @Override
423
        protected ResourceLocation getTexture() {
424
            return texture;
×
425
        }
426

427
        @Override
428
        public boolean charTyped(char typedChar, int keyCode) {
429
            if (!this.searchField.isFocused() || !this.searchField.charTyped(typedChar, keyCode)) {
×
430
                return super.charTyped(typedChar, keyCode);
×
431
            } else {
432
                label(this.searchField.getValue());
×
433
                return true;
×
434
            }
435
        }
436

437
        @Override
438
        public boolean keyPressed(int typedChar, int keyCode, int modifiers) {
439
            if (this.searchField.isFocused() && typedChar != GLFW.GLFW_KEY_ESCAPE) {
×
440
                this.searchField.keyPressed(typedChar, keyCode, modifiers);
×
441
                label(this.searchField.getValue());
×
442
                return true;
×
443
            }
444
            return super.keyPressed(typedChar, keyCode, modifiers);
×
445
        }
446

447
        @Override
448
        public boolean mouseClicked(double mouseX, double mouseY, int mouseButton) {
449
            if(this.searchField.isVisible() && this.searchField.mouseClicked(mouseX, mouseY, mouseButton)) {
×
450
                return true;
×
451
            }
452
            return super.mouseClicked(mouseX, mouseY, mouseButton);
×
453
        }
454

455
        @Override
456
        public void renderBg(GuiGraphics guiGraphics, int guiLeft, int guiTop, TextureManager textureManager, Font font, float partialTicks, int mouseX, int mouseY) {
457
            super.renderBg(guiGraphics, guiLeft, guiTop, textureManager, font, partialTicks, mouseX, mouseY);
×
458
            this.searchField.render(guiGraphics, mouseX, mouseY, partialTicks);
×
459
        }
×
460

461
        @Override
462
        public boolean shouldRenderElementName() {
463
            return !this.searchField.isVisible();
×
464
        }
465

466
        public void onButtonEditClick() {
467
            this.searchField.setVisible(!this.searchField.isVisible());
×
468
            if(this.searchField.isVisible()) {
×
469
                this.searchField.setFocused(true);
×
470
                label(this.searchField.getValue());
×
471
            } else {
472
                label("");
×
473
            }
474
        }
×
475
    }
476

477
}
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