Skip to content

Commit

Permalink
new: Add cursor pulsing animation (#114)
Browse files Browse the repository at this point in the history
* added cursor pulsing animation

* change: Clean up cursor animation

---------

Co-authored-by: Yao Chung Hu <[email protected]>
  • Loading branch information
cattyngmd and FlashyReese authored Oct 28, 2024
1 parent fd82c0d commit 08f6fcf
Showing 1 changed file with 34 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ public class SearchTextFieldComponent extends AbstractWidget {
private int selectionEnd;
private int lastCursorPosition = this.getCursor();

// Cursor properties
private static final long CURSOR_ANIMATION_DURATION = 750;
private long nextCursorUpdate;
private boolean currentCursorState;
private float currentCursorAlpha;

public SearchTextFieldComponent(Dim2i dim, List<OptionPage> pages, AtomicReference<Component> tabFrameSelectedTab, AtomicReference<Integer> tabFrameScrollBarOffset, AtomicReference<Integer> optionPageScrollBarOffset, int tabDimHeight, SodiumVideoOptionsScreen sodiumVideoOptionsScreen, AtomicReference<String> lastSearch, AtomicReference<Integer> lastSearchIndex) {
this.dim = dim;
this.pages = pages;
Expand All @@ -73,6 +79,7 @@ public void render(GuiGraphics guiGraphics, int mouseX, int mouseY, float delta)
if (!this.isVisible()) {
return;
}
updateCursorAlpha();
if (!this.isFocused() && this.text.isBlank()) {
String key = "rso.search_bar_empty";
Component emptyText = Component.translatable(key);
Expand Down Expand Up @@ -109,7 +116,8 @@ public void render(GuiGraphics guiGraphics, int mouseX, int mouseY, float delta)
}
// Cursor
if (this.isFocused()) {
guiGraphics.fill(RenderType.guiOverlay(), cursorX, textStartY - 1, cursorX + 1, textStartY + 1 + this.font.lineHeight, -3092272);
int color = ((int) (this.currentCursorAlpha * 255) << 24) | 0x00D0D0D0;
guiGraphics.fill(RenderType.guiOverlay(), cursorX, textStartY - 1, cursorX + 1, textStartY + 1 + this.font.lineHeight, color);
}
// Highlighted text
if (selectionEndOffset != selectionStartOffset) {
Expand Down Expand Up @@ -184,6 +192,9 @@ public void write(String text) {

String beforeSelectionText = (new StringBuilder(this.text)).replace(selectionStartIndex, selectionEndIndex, filteredText).toString();
if (this.textPredicate.test(beforeSelectionText)) {
this.currentCursorState = true;
this.nextCursorUpdate = System.currentTimeMillis() + CURSOR_ANIMATION_DURATION;

this.text = beforeSelectionText;
this.setSelectionStart(selectionStartIndex + filteredTextLength);
this.setSelectionEnd(this.selectionStart);
Expand Down Expand Up @@ -490,6 +501,28 @@ public boolean keyPressed(int keyCode, int scanCode, int modifiers) {
}
}

private void updateCursorAlpha() {
long currentTimeMillis = System.currentTimeMillis();
if (currentTimeMillis >= this.nextCursorUpdate) {
this.currentCursorState = !this.currentCursorState;
this.nextCursorUpdate = currentTimeMillis + CURSOR_ANIMATION_DURATION;
}

float cursorAlpha = (float) (this.nextCursorUpdate - currentTimeMillis) / CURSOR_ANIMATION_DURATION;

if (cursorAlpha <= 0.25f) {
cursorAlpha *= 4f;
} else if (cursorAlpha >= 0.75f) {
cursorAlpha = (1 - cursorAlpha) * 4f;
} else {
cursorAlpha = 1f;
}

cursorAlpha = Math.clamp(cursorAlpha, 0f, 1f);

this.currentCursorAlpha = this.currentCursorState ? 1 : 1 - cursorAlpha;
}

public boolean isVisible() {
return visible;
}
Expand Down

0 comments on commit 08f6fcf

Please sign in to comment.