We're almost there!

Added side by side (alpha, non-alpha color view)
Removed "tapping" from all listeners (since it became pointless)
Now all listeners don't fire events when the values are set programmatically
Remove unneeded logging
Clean up typos
This commit is contained in:
artdeell 2022-04-22 17:11:59 +03:00
parent 9937413d24
commit 393e24a90b
10 changed files with 175 additions and 45 deletions

View File

@ -1,5 +1,5 @@
package net.kdt.pojavlaunch.colorselector;
public interface AlphaSelectionListener {
void onAlphaSelected(int alpha, boolean tapping);
void onAlphaSelected(int alpha);
}

View File

@ -35,25 +35,32 @@ public class AlphaView extends View {
mBlackPaint.setColor(Color.BLACK);
}
public void setAlphaSelectionListener(AlphaSelectionListener alphaSelectionListener) {
mAlphaSelectionListener = alphaSelectionListener;
}
public void setAlpha(int alpha) {
if(mAlphaSelectionListener != null) mAlphaSelectionListener.onAlphaSelected(mSelectedAlpha,false);
mSelectedAlpha = alpha;
invalidate();
}
@SuppressLint("ClickableViewAccessibility")
@Override
public boolean onTouchEvent(MotionEvent event) {
mSelectedAlpha = (int) (mAlphaDiv * event.getX());
if(mAlphaSelectionListener != null) mAlphaSelectionListener.onAlphaSelected(mSelectedAlpha,true);
if(mSelectedAlpha < 0) mSelectedAlpha = 0;
if(mSelectedAlpha > 0xff) mSelectedAlpha = 0xff;
if(mAlphaSelectionListener != null) mAlphaSelectionListener.onAlphaSelected(mSelectedAlpha);
invalidate();
return true;
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
protected void onSizeChanged(int w, int h, int old_w, int old_h) {
mViewSize.right = w;
mViewSize.bottom = h;
float h2 = mViewSize.bottom / 2f;
mShaderPaint.setShader(new LinearGradient(0,h2,w,h2, Color.BLACK, 0, Shader.TileMode.REPEAT));
mShaderPaint.setShader(new LinearGradient(0,h2,w,h2, 0, Color.BLACK, Shader.TileMode.REPEAT));
mAlphaDiv = 255f / mViewSize.right;
mScreenDiv = mViewSize.right / 255f;
mHeightThird = mViewSize.bottom / 3f;
@ -61,11 +68,9 @@ public class AlphaView extends View {
@Override
protected void onDraw(Canvas canvas) {
Log.i("onDraw","onDraw");
mCheckerboardDrawable.draw(canvas);
canvas.drawRect(mViewSize, mShaderPaint);
float linePos = mSelectedAlpha * mScreenDiv;
Log.i("onDraw",linePos+"");
canvas.drawLine(linePos, 0 ,linePos, mHeightThird, mBlackPaint);
canvas.drawLine(linePos, mHeightThird * 2 ,linePos, mViewSize.bottom, mBlackPaint);
}

View File

@ -8,31 +8,43 @@ import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import net.kdt.pojavlaunch.R;
public class ColorSelector implements HueSelectionListener, RectangleSelectionListener, AlphaSelectionListener{
public class ColorSelector implements HueSelectionListener, RectangleSelectionListener, AlphaSelectionListener, TextWatcher{
static final int ALPHA_MASK = ~(0xFF << 24);
HueView mHueView;
SVRectangleView mLuminosityIntensityView;
AlphaView mAlphaView;
ColorSideBySideView mColorView;
EditText mTextView;
float[] mHueTemplate = new float[] {0,1,1};
float[] mHsvSelected = new float[] {360,1,1};
int mAlphaSelected = 0xff;
boolean mWatch = true;
AlertDialog mDialog;
public ColorSelector(Context context) {
AlertDialog.Builder bldr = new AlertDialog.Builder(context);
AlertDialog.Builder builder = new AlertDialog.Builder(context);
View view = LayoutInflater.from(context).inflate(R.layout.dialog_color_selector,null);
mHueView = view.findViewById(R.id.color_selector_hue_view);
mHueView.setHueSelectionListener(this);
mHueView.setHue(0f);
mLuminosityIntensityView = view.findViewById(R.id.color_selector_rectangle_view);
mLuminosityIntensityView.setRectSelectionListener(this);
mLuminosityIntensityView.setColor(Color.RED);
mLuminosityIntensityView.setLuminosityIntensity(1,1);
//mTextView = view.findViewById(R.id.color_selector_hex_edit);
bldr.setView(view);
bldr.setPositiveButton(android.R.string.ok,(dialog,which)->{
mAlphaView = view.findViewById(R.id.color_selector_alpha_view);
mAlphaView.setAlpha(0xff);
mColorView = view.findViewById(R.id.color_selector_color_view);
mColorView.setColor(Color.RED);
mTextView = view.findViewById(R.id.color_selector_hex_edit);
mHueView.setHueSelectionListener(this);
mLuminosityIntensityView.setRectSelectionListener(this);
mAlphaView.setAlphaSelectionListener(this);
mTextView.addTextChangedListener(this);
builder.setView(view);
builder.setPositiveButton(android.R.string.ok,(dialog,which)->{
});
mDialog = bldr.create();
mDialog = builder.create();
}
public void show() {
@ -40,25 +52,62 @@ public class ColorSelector implements HueSelectionListener, RectangleSelectionLi
}
@Override
public void onHueSelected(float hue, boolean tapping) {
public void onHueSelected(float hue) {
mHsvSelected[0] = mHueTemplate[0] = hue;
mLuminosityIntensityView.setColor(Color.HSVToColor(mHueTemplate));
dispatchColorChange(Color.HSVToColor(mHsvSelected),tapping);
dispatchColorChange(Color.HSVToColor(mAlphaSelected, mHsvSelected));
}
@Override
public void onLuminosityIntensityChanged(float luminosity, float intensity, boolean tapping) {
public void onLuminosityIntensityChanged(float luminosity, float intensity) {
mHsvSelected[1] = intensity;
mHsvSelected[2] = luminosity;
dispatchColorChange(Color.HSVToColor(mHsvSelected),tapping);
dispatchColorChange(Color.HSVToColor(mAlphaSelected, mHsvSelected));
}
@Override
public void onAlphaSelected(int alpha, boolean tapping) {
public void onAlphaSelected(int alpha) {
mAlphaSelected = alpha;
dispatchColorChange(Color.HSVToColor(mAlphaSelected, mHsvSelected));
}
protected void dispatchColorChange(int color, boolean tapping) {
public static int setAlpha(int color, int alpha) {
return color & ALPHA_MASK | ((alpha & 0xFF) << 24);
}
protected void dispatchColorChange(int color) {
mColorView.setColor(color);
mWatch = false;
mTextView.setText(String.format("%08X",color));
}
protected void runColor(int color) {
Color.RGBToHSV(Color.red(color), Color.green(color), Color.blue(color), mHsvSelected);
mHueTemplate[0] = mHsvSelected[0];
mHueView.setHue(mHsvSelected[0]);
mLuminosityIntensityView.setColor(Color.HSVToColor(mHueTemplate));
mLuminosityIntensityView.setLuminosityIntensity(mHsvSelected[2], mHsvSelected[1]);
mAlphaView.setAlpha(Color.alpha(color));
mColorView.setColor(color);
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {}
@Override
public void afterTextChanged(Editable s) {
if(mWatch) {
try {
int color = Integer.parseInt(s.toString(), 16);
runColor(color);
}catch (NumberFormatException ignored) {
}
}else{
mWatch = true;
}
}
}

View File

@ -0,0 +1,47 @@
package net.kdt.pojavlaunch.colorselector;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;
import androidx.annotation.Nullable;
import top.defaults.checkerboarddrawable.CheckerboardDrawable;
public class ColorSideBySideView extends View {
Paint mPaint;
CheckerboardDrawable mCheckerboardDrawable = CheckerboardDrawable.create();
int mColor;
int mAlphaColor;
float mWidth;
float mHeight;
float mHalfHeight;
public ColorSideBySideView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
mPaint = new Paint();
}
public void setColor(int color) {
mColor = ColorSelector.setAlpha(color, 0xff);
mAlphaColor = color;
invalidate();
}
@Override
protected void onDraw(Canvas canvas) {
mCheckerboardDrawable.draw(canvas);
mPaint.setColor(mColor);
canvas.drawRect(0,0,mWidth, mHalfHeight, mPaint);
mPaint.setColor(mAlphaColor);
canvas.drawRect(0,mHalfHeight,mWidth,mHeight, mPaint);
}
@Override
protected void onSizeChanged(int w, int h, int old_w, int old_h) {
mHalfHeight = h/2f;
mWidth = w;
mHeight = h;
}
}

View File

@ -1,5 +1,5 @@
package net.kdt.pojavlaunch.colorselector;
public interface HueSelectionListener {
void onHueSelected(float hue, boolean tappng);
void onHueSelected(float hue);
}

View File

@ -37,7 +37,6 @@ public class HueView extends View {
}
public void setHue(float hue) {
mSelectionHue = hue;
if(mHueSelectionListener != null) mHueSelectionListener.onHueSelected(mSelectionHue, false);
invalidate();
}
@ -46,7 +45,7 @@ public class HueView extends View {
public boolean onTouchEvent(MotionEvent event) {
mSelectionHue = event.getX() * mWidthHueRatio;
invalidate();
if(mHueSelectionListener != null) mHueSelectionListener.onHueSelected(mSelectionHue, true);
if(mHueSelectionListener != null) mHueSelectionListener.onHueSelected(mSelectionHue);
return true;
}
@ -59,7 +58,7 @@ public class HueView extends View {
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
protected void onSizeChanged(int w, int h, int old_w, int old_h) {
mWidth = w;
mHeight = h;
mHeightThird = mHeight / 3;

View File

@ -1,5 +1,5 @@
package net.kdt.pojavlaunch.colorselector;
public interface RectangleSelectionListener {
void onLuminosityIntensityChanged(float luminosity, float intensity, boolean tapping);
void onLuminosityIntensityChanged(float luminosity, float intensity);
}

View File

@ -20,21 +20,21 @@ import net.kdt.pojavlaunch.Tools;
public class SVRectangleView extends View {
Bitmap mSvRectangle;
Paint mColorPaint = new Paint();
Paint mCrosshairPaint = new Paint();
Paint mPointerPaint = new Paint();
RectF mViewSize;
float mHeightInverted;
float mWidthInverted;
float mCrosshairSize;
float mPointerSize;
float mFingerPosX;
float mFingerPosY;
RectangleSelectionListener mRectSeelctionListener;
RectangleSelectionListener mRectSelectionListener;
public SVRectangleView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
mColorPaint.setColor(Color.BLACK);
mColorPaint.setStyle(Paint.Style.FILL);
mCrosshairSize = Tools.dpToPx(6);
mCrosshairPaint.setColor(Color.BLACK);
mCrosshairPaint.setStrokeWidth(Tools.dpToPx(3));
mPointerSize = Tools.dpToPx(6);
mPointerPaint.setColor(Color.BLACK);
mPointerPaint.setStrokeWidth(Tools.dpToPx(3));
}
@SuppressLint("ClickableViewAccessibility")
@ -51,7 +51,7 @@ public class SVRectangleView extends View {
if(mFingerPosY < 0) mFingerPosY = 0;
else if(mFingerPosY > 1) mFingerPosY = 1;
if(mRectSeelctionListener != null) mRectSeelctionListener.onLuminosityIntensityChanged(mFingerPosY,mFingerPosX, true);
if(mRectSelectionListener != null) mRectSelectionListener.onLuminosityIntensityChanged(mFingerPosY,mFingerPosX);
invalidate();
return true;
}
@ -59,7 +59,6 @@ public class SVRectangleView extends View {
public void setLuminosityIntensity(float luminosity, float intensity) {
mFingerPosX = intensity;
mFingerPosY = luminosity;
if(mRectSeelctionListener != null) mRectSeelctionListener.onLuminosityIntensityChanged(mFingerPosY,mFingerPosX, false);
invalidate();
}
@ -69,13 +68,13 @@ public class SVRectangleView extends View {
}
public void setRectSelectionListener(RectangleSelectionListener listener) {
mRectSeelctionListener = listener;
mRectSelectionListener = listener;
}
protected void drawCrosshair(Canvas canvas, float x, float y) {
canvas.drawLine(mCrosshairSize * 2 + x, y, mCrosshairSize + x, y, mCrosshairPaint);
canvas.drawLine(x - mCrosshairSize * 2, y, x - mCrosshairSize, y, mCrosshairPaint);
canvas.drawLine(x, mCrosshairSize * 2 + y, x, mCrosshairSize + y, mCrosshairPaint);
canvas.drawLine(x, y - mCrosshairSize * 2, x, y - mCrosshairSize, mCrosshairPaint);
protected void drawPointer(Canvas canvas, float x, float y) {
canvas.drawLine(mPointerSize * 2 + x, y, mPointerSize + x, y, mPointerPaint);
canvas.drawLine(x - mPointerSize * 2, y, x - mPointerSize, y, mPointerPaint);
canvas.drawLine(x, mPointerSize * 2 + y, x, mPointerSize + y, mPointerPaint);
canvas.drawLine(x, y - mPointerSize * 2, x, y - mPointerSize, mPointerPaint);
}
@Override
@ -83,11 +82,11 @@ public class SVRectangleView extends View {
super.onDraw(canvas);
canvas.drawRect(mViewSize, mColorPaint);
canvas.drawBitmap(mSvRectangle, 0,0, null);
drawCrosshair(canvas, mViewSize.right * mFingerPosX, mViewSize.bottom * mFingerPosY);
drawPointer(canvas, mViewSize.right * mFingerPosX, mViewSize.bottom * mFingerPosY);
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
protected void onSizeChanged(int w, int h, int old_w, int old_h) {
mViewSize = new RectF(0,0, w,h);
mWidthInverted = 1/mViewSize.right;
mHeightInverted = 1/mViewSize.bottom;

View File

@ -1,6 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
@ -18,11 +19,13 @@
<net.kdt.pojavlaunch.colorselector.SVRectangleView
android:id="@+id/color_selector_rectangle_view"
android:layout_width="200dp"
android:layout_width="0dp"
android:layout_height="150dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:focusable="true"
app:layout_constraintEnd_toStartOf="@+id/color_selector_hex_edit"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
@ -36,4 +39,31 @@
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/color_selector_hue_view" />
<EditText
android:id="@+id/color_selector_hex_edit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:hint="@string/color_default_hex"
android:inputType="number"
android:minHeight="48dp"
android:typeface="monospace"
app:layout_constraintBottom_toTopOf="@+id/color_selector_hue_view"
app:layout_constraintEnd_toEndOf="parent"
android:importantForAutofill="no"/>
<net.kdt.pojavlaunch.colorselector.ColorSideBySideView
android:id="@+id/color_selector_color_view"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toTopOf="@+id/color_selector_hex_edit"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/color_selector_rectangle_view"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

View File

@ -297,4 +297,5 @@
<string name="arc_capes_title">Arc Capes</string>
<string name="arc_capes_desc">Enables capes from Arc. For more information please visit https://arccapes.com. Requires OptiFine.</string>
<string name="migrated_profile_str">%s version configuration</string>
<string name="color_default_hex" translatable="false">FFFF0000</string>
</resources>