Skip to content

Commit

Permalink
Feat: Click the text to edit Seekbar
Browse files Browse the repository at this point in the history
  • Loading branch information
ShirosakiMio committed Feb 23, 2025
1 parent 8c1b3e6 commit 7a8fefd
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -243,9 +243,7 @@ private SimpleMultimap<String, RemoteMod.Version, List<RemoteMod.Version>> sortV
}
}
return false;
}).forEach(v -> {
classifiedVersions.put(recommendedVersion, v);
});
}).forEach(v -> classifiedVersions.put(recommendedVersion, v));
}
return classifiedVersions;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.OvalShape;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.MotionEvent;
Expand All @@ -14,7 +17,6 @@

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.content.res.AppCompatResources;
import androidx.appcompat.widget.AppCompatSeekBar;

import com.tungsten.fclcore.fakefx.beans.property.BooleanProperty;
Expand All @@ -40,6 +42,8 @@ public class FCLNumberSeekBar extends AppCompatSeekBar {
private Paint textPaint;
private String suffix;
private GestureDetector gestureDetector;
private ShapeDrawable thumbDrawable;
private Rect textBounds;

private final IntegerProperty theme = new IntegerPropertyBase() {

Expand Down Expand Up @@ -115,12 +119,10 @@ private void init(AttributeSet attrs) {
suffix = Optional.ofNullable(typedArray.getString(R.styleable.FCLNumberSeekBar_suffix)).orElse("");
typedArray.recycle();
}
setThumb(AppCompatResources.getDrawable(getContext(), R.drawable.transparent));
textPaint = new Paint();
textPaint.setColor(Color.WHITE);
textPaint.setTextSize(40);
textPaint.setTextAlign(Paint.Align.CENTER);

gestureDetector = new GestureDetector(getContext(), new GestureDetector.SimpleOnGestureListener() {
@Override
public boolean onDoubleTap(@NonNull MotionEvent e) {
Expand All @@ -137,6 +139,25 @@ public boolean onDoubleTap(@NonNull MotionEvent e) {
dialog.show();
return true;
}

@Override
public boolean onSingleTapUp(@NonNull MotionEvent e) {
if (e.getX() >= computeThumbX() - textBounds.width() / 2f && e.getX() <= computeThumbX() + textBounds.width() / 2f) {
EditDialog dialog = new EditDialog(getContext(), s -> {
try {
int i = Integer.parseInt(s);
if (i >= getMin() && i <= getMax()) {
setProgress(i);
}
} catch (Throwable ignore) {
}
});
dialog.getEditText().setInputType(EditorInfo.TYPE_NUMBER_FLAG_DECIMAL);
dialog.show();
return true;
}
return false;
}
});
}

Expand Down Expand Up @@ -275,12 +296,19 @@ public String getName() {
@Override
protected synchronized void onDraw(Canvas canvas) {
super.onDraw(canvas);

float width = getWidth() - getPaddingStart() - getPaddingEnd();
float progressRatio = (float) (getProgress() - getMin()) / (getMax() - getMin());
float thumbX = getPaddingStart() + (width * progressRatio);
if (thumbDrawable == null) {
textBounds = new Rect();
textPaint.getTextBounds(getMax() + suffix, 0, (getMax() + suffix).length(), textBounds);
thumbDrawable = new ShapeDrawable();
thumbDrawable.setShape(new OvalShape());
thumbDrawable.getPaint().setColor(Color.TRANSPARENT);
thumbDrawable.setIntrinsicHeight(getHeight());
thumbDrawable.setIntrinsicWidth(textBounds.width());
setThumb(thumbDrawable);
}
textPaint.setTextSize(getHeight() / 1.5f);
float textY = getHeight() / 2f - (textPaint.descent() + textPaint.ascent()) / 2f;
canvas.drawText(getProgress() + suffix, thumbX, textY, textPaint);
canvas.drawText(getProgress() + suffix, computeThumbX(), textY, textPaint);
}

@Override
Expand All @@ -290,4 +318,10 @@ public boolean onTouchEvent(MotionEvent event) {
}
return super.onTouchEvent(event);
}

private float computeThumbX() {
float width = getWidth() - getPaddingStart() - getPaddingEnd();
float progressRatio = (float) (getProgress() - getMin()) / (getMax() - getMin());
return getPaddingStart() + (width * progressRatio);
}
}

0 comments on commit 7a8fefd

Please sign in to comment.