From 393e24a90b66ae5efc6a2c19749752a2baffbaf7 Mon Sep 17 00:00:00 2001 From: artdeell Date: Fri, 22 Apr 2022 17:11:59 +0300 Subject: [PATCH] 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 --- .../colorselector/AlphaSelectionListener.java | 2 +- .../pojavlaunch/colorselector/AlphaView.java | 17 ++-- .../colorselector/ColorSelector.java | 81 +++++++++++++++---- .../colorselector/ColorSideBySideView.java | 47 +++++++++++ .../colorselector/HueSelectionListener.java | 2 +- .../pojavlaunch/colorselector/HueView.java | 5 +- .../RectangleSelectionListener.java | 2 +- .../colorselector/SVRectangleView.java | 31 ++++--- .../main/res/layout/dialog_color_selector.xml | 32 +++++++- .../src/main/res/values/strings.xml | 1 + 10 files changed, 175 insertions(+), 45 deletions(-) create mode 100644 app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/colorselector/ColorSideBySideView.java diff --git a/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/colorselector/AlphaSelectionListener.java b/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/colorselector/AlphaSelectionListener.java index a61055980..b307fa98e 100644 --- a/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/colorselector/AlphaSelectionListener.java +++ b/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/colorselector/AlphaSelectionListener.java @@ -1,5 +1,5 @@ package net.kdt.pojavlaunch.colorselector; public interface AlphaSelectionListener { - void onAlphaSelected(int alpha, boolean tapping); + void onAlphaSelected(int alpha); } diff --git a/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/colorselector/AlphaView.java b/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/colorselector/AlphaView.java index 0a085f855..d454d4608 100644 --- a/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/colorselector/AlphaView.java +++ b/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/colorselector/AlphaView.java @@ -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); } diff --git a/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/colorselector/ColorSelector.java b/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/colorselector/ColorSelector.java index 09a5f9c5e..dd96b4635 100644 --- a/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/colorselector/ColorSelector.java +++ b/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/colorselector/ColorSelector.java @@ -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; + } } } diff --git a/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/colorselector/ColorSideBySideView.java b/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/colorselector/ColorSideBySideView.java new file mode 100644 index 000000000..d6904bdeb --- /dev/null +++ b/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/colorselector/ColorSideBySideView.java @@ -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; + } +} diff --git a/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/colorselector/HueSelectionListener.java b/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/colorselector/HueSelectionListener.java index cf8d76dfe..34becdd6f 100644 --- a/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/colorselector/HueSelectionListener.java +++ b/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/colorselector/HueSelectionListener.java @@ -1,5 +1,5 @@ package net.kdt.pojavlaunch.colorselector; public interface HueSelectionListener { - void onHueSelected(float hue, boolean tappng); + void onHueSelected(float hue); } diff --git a/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/colorselector/HueView.java b/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/colorselector/HueView.java index db5c92d26..4d05cf4a2 100644 --- a/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/colorselector/HueView.java +++ b/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/colorselector/HueView.java @@ -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; diff --git a/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/colorselector/RectangleSelectionListener.java b/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/colorselector/RectangleSelectionListener.java index 3cf2363db..72a47d499 100644 --- a/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/colorselector/RectangleSelectionListener.java +++ b/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/colorselector/RectangleSelectionListener.java @@ -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); } diff --git a/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/colorselector/SVRectangleView.java b/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/colorselector/SVRectangleView.java index 0256c0591..0d75ab0b7 100644 --- a/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/colorselector/SVRectangleView.java +++ b/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/colorselector/SVRectangleView.java @@ -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; diff --git a/app_pojavlauncher/src/main/res/layout/dialog_color_selector.xml b/app_pojavlauncher/src/main/res/layout/dialog_color_selector.xml index 0ca2433fc..a940bd374 100644 --- a/app_pojavlauncher/src/main/res/layout/dialog_color_selector.xml +++ b/app_pojavlauncher/src/main/res/layout/dialog_color_selector.xml @@ -1,6 +1,7 @@ @@ -18,11 +19,13 @@ @@ -36,4 +39,31 @@ app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/color_selector_hue_view" /> + + + + \ No newline at end of file diff --git a/app_pojavlauncher/src/main/res/values/strings.xml b/app_pojavlauncher/src/main/res/values/strings.xml index bbe976b5e..f13254f32 100644 --- a/app_pojavlauncher/src/main/res/values/strings.xml +++ b/app_pojavlauncher/src/main/res/values/strings.xml @@ -297,4 +297,5 @@ Arc Capes Enables capes from Arc. For more information please visit https://arccapes.com. Requires OptiFine. %s version configuration + FFFF0000