Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dev #129

Merged
merged 8 commits into from
Oct 16, 2024
Merged

Dev #129

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,22 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [2101.1.4]

### Added
* Added a dropdown menu widget
* Exposed some more methods in `EditStringConfigOverlay`

### Changed
* Replaced `ContextMenu.CButton` with `ContextButton`

### Fixed
* Fixed `allowEditMode` in `TextBox` not applying in all situations
* Fixed items/fluids/images in respective resource search screens being jammed together too closely

## [2101.1.3]

### Fixed
* Fixed an issue in config loading/saving of certain data types (affects FTB Chunks minimap info settings)
* Added tr_tr translations (thanks @RuyaSavascisi)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,6 @@ public boolean onClosedByKey(Key key) {
public boolean keyPressed(Key key) {
if (super.keyPressed(key)) {
return true;
} else if ((key.is(InputConstants.KEY_RETURN) || key.is(InputConstants.KEY_NUMPADENTER)) && key.modifiers.shift()) {
doAccept();
return true;
} else if (key.is(InputConstants.KEY_INSERT)) {
addButton.onClicked(MouseButton.LEFT);
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,9 +184,6 @@ public boolean onClosedByKey(Key key) {
public boolean keyPressed(Key key) {
if (super.keyPressed(key)) {
return true;
} else if ((key.is(InputConstants.KEY_RETURN) || key.is(InputConstants.KEY_NUMPADENTER)) && key.modifiers.shift()) {
doAccept();
return true;
} else if (key.is(InputConstants.KEY_ADD) || key.is(InputConstants.KEY_EQUALS)) {
buttonExpandAll.onClicked(MouseButton.LEFT);
} else if (key.is(InputConstants.KEY_MINUS) || key.is(GLFW.GLFW_KEY_KP_SUBTRACT)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@
import java.util.Objects;

public class EditStringConfigOverlay<T> extends ModalPanel {
private final EditField textBox;
protected final EditField textBox;
private final Button accept, cancel;
private final ConfigFromString<T> config;
private final ConfigCallback callback;
private final TextField titleField;
private final Component title;
private boolean addAcceptCancelButtons = true;

private T currentValue;

Expand Down Expand Up @@ -60,8 +61,10 @@ public void addWidgets() {
add(titleField);
}
add(textBox);
add(accept);
add(cancel);
if(addAcceptCancelButtons) {
add(accept);
add(cancel);
}
}

@Override
Expand All @@ -70,8 +73,11 @@ public void alignWidgets() {
titleField.setPosAndSize(2, 2, width, getGui().getTheme().getFontHeight() + 4);
}
textBox.setPosAndSize(2, titleField.getHeight() + 1, width - 36, 14);
accept.setPos(textBox.width + 2, textBox.getPosY());
cancel.setPos(accept.getPosX() + accept.width + 2, textBox.getPosY());
if(addAcceptCancelButtons) {
accept.setPos(textBox.width + 2, textBox.getPosY());
cancel.setPos(accept.getPosX() + accept.width + 2, textBox.getPosY());
}


height = title == null ? 16 : 30;
}
Expand All @@ -90,19 +96,26 @@ public void drawBackground(GuiGraphics graphics, Theme theme, int x, int y, int
theme.drawContextMenuBackground(graphics, x - 1, y - 1, w + 2, h + 2);
}

private void onAccepted(Button btn, MouseButton mb) {
protected void onAccepted(Button btn, MouseButton mb) {
if (textBox.isTextValid()) {
config.setCurrentValue(currentValue);
callback.save(true);
getGui().popModalPanel();
}
}

private void onCancelled(Button btn, MouseButton mb) {
protected void onCancelled(Button btn, MouseButton mb) {
callback.save(false);
getGui().popModalPanel();
}

/**
* @param addAcceptCancelButtons if true, accept and cancel buttons will be added to the overlay
*/
public void setAddAcceptCancelButtons(boolean addAcceptCancelButtons) {
this.addAcceptCancelButtons = addAcceptCancelButtons;
}

/**
* Provides positioning information for when an edit panel needs to be overlaid on the widget
*/
Expand All @@ -114,7 +127,7 @@ record Offset(int x, int y) {
}
}

private class EditField extends TextBox {
protected class EditField extends TextBox {
public EditField() {
super(EditStringConfigOverlay.this);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,18 +160,6 @@ public void drawForeground(GuiGraphics graphics, Theme theme, int x, int y, int
}
}

@Override
public boolean keyPressed(Key key) {
if (super.keyPressed(key)) {
return true;
} else if ((key.is(InputConstants.KEY_RETURN) || key.is(InputConstants.KEY_NUMPADENTER)) && key.modifiers.shift()) {
doAccept();
return true;
}

return false;
}

protected void setSelected(SelectableResource<T> stack) {
long count = selectedStack == null || selectedStack.isEmpty() ? Math.max(stack.getCount(), countBox.getCount()) : selectedStack.getCount();
selectedStack = stack.copyWithCount(count);
Expand Down Expand Up @@ -433,7 +421,15 @@ public void addMouseOverText(TooltipList list) {

@Override
public void drawBackground(GuiGraphics graphics, Theme theme, int x, int y, int w, int h) {
theme.drawSlot(graphics, x, y, w, h, getWidgetType());
theme.drawSlot(graphics, x, y, w, h, WidgetType.NORMAL);
if (isMouseOver) {
Color4I.WHITE.withAlpha(30).draw(graphics, x + 1, y + 1, w - 2, h - 2);
}
}

@Override
public void drawIcon(GuiGraphics graphics, Theme theme, int x, int y, int w, int h) {
super.drawIcon(graphics, theme, x + 1, y + 1, w - 2, h - 2);
}

@Override
Expand Down
2 changes: 2 additions & 0 deletions common/src/main/java/dev/ftb/mods/ftblibrary/icon/Icons.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ public interface Icons {
Icon LOCK = get("lock");
Icon LOCK_OPEN = get("lock_open");
Icon SUPPORT = getImage("support");
Icon DROPDOWN_OUT = get("dropdown_out");
Icon DROPDOWN_IN = get("dropdown_in");

static Icon get(String id) {
return Icon.getIcon(FTBLibrary.MOD_ID + ":icons/" + id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,9 +171,6 @@ public boolean doesGuiPauseGame() {
public boolean keyPressed(Key key) {
if (super.keyPressed(key)) {
return true;
} else if ((key.is(InputConstants.KEY_RETURN) || key.is(InputConstants.KEY_NUMPADENTER)) && key.modifiers.shift()) {
doAccept();
return true;
} else if (key.is(InputConstants.KEY_ADD) || key.is(InputConstants.KEY_EQUALS)) {
collapseAll(false);
} else if (key.is(InputConstants.KEY_MINUS) || key.is(GLFW.GLFW_KEY_KP_SUBTRACT)) {
Expand Down
30 changes: 22 additions & 8 deletions common/src/main/java/dev/ftb/mods/ftblibrary/ui/BaseScreen.java
Original file line number Diff line number Diff line change
Expand Up @@ -311,23 +311,27 @@ public final void draw(GuiGraphics graphics, Theme theme, int x, int y, int w, i
}

public Optional<ModalPanel> getContextMenu() {
return modalPanels.stream().filter(p -> p instanceof ContextMenu).findFirst();
return modalPanels.stream().filter(p -> p instanceof PopupMenu).findFirst();
}

public void openContextMenu(@Nullable ContextMenu newContextMenu) {
if (newContextMenu == null) {
modalPanels.removeIf(p -> p instanceof ContextMenu);
public void openPopupMenu(@Nullable PopupMenu popupMenu) {
if (popupMenu == null) {
modalPanels.removeIf(p -> p instanceof PopupMenu);
return;
}

pushModalPanel(newContextMenu);
pushModalPanel(popupMenu.getModalPanel());

// default positioning where the mouse was clicked. caller is free to reposition if needed
var x = getX();
var y = getY();
int px = Math.min((getMouseX() - x), screen.getGuiScaledWidth() - newContextMenu.width - x) - 3;
int py = Math.min((getMouseY() - y), screen.getGuiScaledHeight() - newContextMenu.height - y) - 3;
newContextMenu.setPos(px, py);
int px = Math.min((getMouseX() - x), screen.getGuiScaledWidth() - popupMenu.getModalPanel().width - x) - 3;
int py = Math.min((getMouseY() - y), screen.getGuiScaledHeight() - popupMenu.getModalPanel().height - y) - 3;
popupMenu.getModalPanel().setPos(px, py);
}

public void openContextMenu(@Nullable ContextMenu newContextMenu) {
openPopupMenu(newContextMenu);
}

public ContextMenu openContextMenu(@NotNull List<ContextMenuItem> menuItems) {
Expand All @@ -336,6 +340,16 @@ public ContextMenu openContextMenu(@NotNull List<ContextMenuItem> menuItems) {
return contextMenu;
}

public void openDropdownMenu(@Nullable DropDownMenu dropDownMenu) {
openPopupMenu(dropDownMenu);
}

public DropDownMenu openDropdownMenu(@NotNull List<ContextMenuItem> menuItems) {
var contextMenu = new DropDownMenu(this, menuItems);
openDropdownMenu(contextMenu);
return contextMenu;
}

@Override
public void closeContextMenu() {
openContextMenu((ContextMenu) null);
Expand Down
63 changes: 63 additions & 0 deletions common/src/main/java/dev/ftb/mods/ftblibrary/ui/ContextButton.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package dev.ftb.mods.ftblibrary.ui;

import dev.ftb.mods.ftblibrary.ui.input.MouseButton;
import dev.ftb.mods.ftblibrary.util.TooltipList;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.network.chat.Component;

public class ContextButton extends Button {
public final ContextMenuItem item;
private final boolean hasIcons;

public ContextButton(Panel panel, ContextMenuItem item, boolean hasIcons) {
super(panel, item.getTitle(), item.getIcon());
this.hasIcons = hasIcons;
this.item = item;
setSize(panel.getGui().getTheme().getStringWidth(item.getTitle()) + (hasIcons ? 14 : 4), 12);
}

@Override
public void addMouseOverText(TooltipList list) {
item.addMouseOverText(list);
}

@Override
public WidgetType getWidgetType() {
if (!item.isClickable()) {
return WidgetType.NORMAL; // no hovered highlighting
}

return item.isEnabled() ? super.getWidgetType() : WidgetType.DISABLED;
}

@Override
public void drawIcon(GuiGraphics graphics, Theme theme, int x, int y, int w, int h) {
item.drawIcon(graphics, theme, x, y, w, h);
}

@Override
public void draw(GuiGraphics graphics, Theme theme, int x, int y, int w, int h) {
GuiHelper.setupDrawing();

if (hasIcons) {
drawIcon(graphics, theme, x + 1, y + 2, 8, 8);
theme.drawString(graphics, getTitle(), x + 11, y + 2, theme.getContentColor(getWidgetType()), Theme.SHADOW);
} else {
theme.drawString(graphics, getTitle(), x + 2, y + 2, theme.getContentColor(getWidgetType()), Theme.SHADOW);
}
}

@Override
public void onClicked(MouseButton button) {
if (item.isClickable()) {
playClickSound();
}

if (item.getYesNoText().getString().isEmpty()) {
item.onClicked(ContextButton.this, parent, button);
} else {
getGui().openYesNo(item.getYesNoText(), Component.literal(""), () -> item.onClicked(ContextButton.this, parent, button));
}
}

}
65 changes: 8 additions & 57 deletions common/src/main/java/dev/ftb/mods/ftblibrary/ui/ContextMenu.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@

import dev.ftb.mods.ftblibrary.icon.Color4I;
import dev.ftb.mods.ftblibrary.ui.input.MouseButton;
import dev.ftb.mods.ftblibrary.util.TooltipList;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.network.chat.Component;

import java.util.List;

public class ContextMenu extends ModalPanel {
public class ContextMenu extends ModalPanel implements PopupMenu {
private static final int MARGIN = 3;

private final List<ContextMenuItem> items;
Expand Down Expand Up @@ -113,60 +111,9 @@ public void draw(GuiGraphics graphics, Theme theme, int x, int y, int w, int h)
graphics.pose().popPose();
}

public static class CButton extends Button {
public final ContextMenu contextMenu;
public final ContextMenuItem item;

public CButton(ContextMenu panel, ContextMenuItem item) {
super(panel, item.getTitle(), item.getIcon());
contextMenu = panel;
this.item = item;
setSize(panel.getGui().getTheme().getStringWidth(item.getTitle()) + (contextMenu.hasIcons ? 14 : 4), 12);
}

@Override
public void addMouseOverText(TooltipList list) {
item.addMouseOverText(list);
}

@Override
public WidgetType getWidgetType() {
if (!item.isClickable()) {
return WidgetType.NORMAL; // no hovered highlighting
}

return item.isEnabled() ? super.getWidgetType() : WidgetType.DISABLED;
}

@Override
public void drawIcon(GuiGraphics graphics, Theme theme, int x, int y, int w, int h) {
item.drawIcon(graphics, theme, x, y, w, h);
}

@Override
public void draw(GuiGraphics graphics, Theme theme, int x, int y, int w, int h) {
GuiHelper.setupDrawing();

if (contextMenu.hasIcons) {
drawIcon(graphics, theme, x + 1, y + 2, 8, 8);
theme.drawString(graphics, getTitle(), x + 11, y + 2, theme.getContentColor(getWidgetType()), Theme.SHADOW);
} else {
theme.drawString(graphics, getTitle(), x + 2, y + 2, theme.getContentColor(getWidgetType()), Theme.SHADOW);
}
}

@Override
public void onClicked(MouseButton button) {
if (item.isClickable()) {
playClickSound();
}

if (item.getYesNoText().getString().isEmpty()) {
item.onClicked(CButton.this, contextMenu, button);
} else {
getGui().openYesNo(item.getYesNoText(), Component.literal(""), () -> item.onClicked(CButton.this, contextMenu, button));
}
}
@Override
public ModalPanel getModalPanel() {
return this;
}

public static class CSeparator extends Button {
Expand All @@ -185,4 +132,8 @@ public void draw(GuiGraphics graphics, Theme theme, int x, int y, int w, int h)
public void onClicked(MouseButton button) {
}
}

public boolean hasIcons() {
return hasIcons;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public boolean isClickable() {
}

public Widget createWidget(ContextMenu panel) {
return new ContextMenu.CButton(panel, this);
return new ContextButton(panel, this, panel.hasIcons());
}

@Override
Expand Down
Loading
Loading