Skip to content

Commit

Permalink
constrain tooltip to screen
Browse files Browse the repository at this point in the history
  • Loading branch information
douira committed Oct 27, 2024
1 parent 6788ca7 commit 159935c
Showing 1 changed file with 15 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@
import java.util.List;
import java.util.stream.Stream;

// TODO: constrain the tooltip to its safe area if it's too big, then show a scroll bar if it's still too big
// TODO: scrolling the tooltip?
// TODO: show a scroll bar on the tooltip if it's too big to fit on the screen
// TODO: make the search bar work
// TODO: wrap options within groups in two columns
// TODO: make the mod config headers interactive: only show one mod's pages at a time, click on a mod header to open that mod's first settings page and close the previous mod's page list
Expand Down Expand Up @@ -278,26 +277,30 @@ private Stream<ControlElement<?>> getActiveControls() {
}

private void renderOptionTooltip(GuiGraphics graphics, ControlElement<?> element) {
int textPadding = 5;
int boxPadding = 5;

int boxWidth = this.width - 340;
int textPadding = Layout.INNER_MARGIN;
int boxMargin = Layout.INNER_MARGIN;
int lineHeight = this.font.lineHeight + 3;

int boxY = element.getY();
int boxX = element.getLimitX() + boxPadding;
int boxX = element.getLimitX() + boxMargin;

int boxWidth = Math.min(200, this.width - boxX - boxMargin);

Option<?> option = element.getOption();
List<FormattedCharSequence> tooltip = new ArrayList<>(this.font.split(option.getTooltip(), boxWidth - (textPadding * 2)));
var splitWidth = boxWidth - (textPadding * 2);
List<FormattedCharSequence> tooltip = new ArrayList<>(this.font.split(option.getTooltip(),splitWidth));

OptionImpact impact = option.getImpact();

if (impact != null) {
tooltip.add(Language.getInstance().getVisualOrder(Component.translatable("sodium.options.performance_impact_string", impact.getName()).withStyle(ChatFormatting.GRAY)));
var impactText = Component.translatable("sodium.options.performance_impact_string",
impact.getName());
tooltip.addAll(this.font.split(impactText.withStyle(ChatFormatting.GRAY), splitWidth));
}

int boxHeight = (tooltip.size() * 12) + boxPadding;
int boxHeight = (tooltip.size() * lineHeight) + boxMargin;
int boxYLimit = boxY + boxHeight;
int boxYCutoff = this.height - 40;
int boxYCutoff = this.height - Layout.INNER_MARGIN;

// If the box is going to be cut off on the Y-axis, move it back up the difference
if (boxYLimit > boxYCutoff) {
Expand All @@ -307,7 +310,7 @@ private void renderOptionTooltip(GuiGraphics graphics, ControlElement<?> element
graphics.fill(boxX, boxY, boxX + boxWidth, boxY + boxHeight, 0x40000000);

for (int i = 0; i < tooltip.size(); i++) {
graphics.drawString(this.font, tooltip.get(i), boxX + textPadding, boxY + textPadding + (i * 12), Colors.FOREGROUND);
graphics.drawString(this.font, tooltip.get(i), boxX + textPadding, boxY + textPadding + (i * lineHeight), Colors.FOREGROUND);
}
}

Expand Down

0 comments on commit 159935c

Please sign in to comment.