Finally. The color selector is at home.

Other changes:
The ColorSelectionListener is back!
Now ColorSelector.runColor(int) is used for unified initialization
Added invalidate flag into SVRectangleView.setColor() to avoid a useless invalidation in runColor()
Moved repeating Color.HSVtoColor into ColorSelector.dispatchColorChange()
Fixed a bug: mAlphaSelected wasn't set to the appropriate value by runColor, leading to the Color Preview and HEX edit breaking in some cases
This commit is contained in:
artdeell 2022-04-22 22:08:40 +03:00
parent 393e24a90b
commit cfae15c680
5 changed files with 72 additions and 20 deletions

View File

@ -10,7 +10,6 @@ import android.graphics.RectF;
import android.graphics.Shader; import android.graphics.Shader;
import android.graphics.drawable.Drawable; import android.graphics.drawable.Drawable;
import android.util.AttributeSet; import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent; import android.view.MotionEvent;
import android.view.View; import android.view.View;

View File

@ -0,0 +1,9 @@
package net.kdt.pojavlaunch.colorselector;
public interface ColorSelectionListener {
/**
* This method gets called by the ColorSelector when the color is selected
* @param color the selected color
*/
void onColorSelected(int color);
}

View File

@ -18,77 +18,108 @@ public class ColorSelector implements HueSelectionListener, RectangleSelectionLi
AlphaView mAlphaView; AlphaView mAlphaView;
ColorSideBySideView mColorView; ColorSideBySideView mColorView;
EditText mTextView; EditText mTextView;
ColorSelectionListener mColorSelectionListener;
float[] mHueTemplate = new float[] {0,1,1}; float[] mHueTemplate = new float[] {0,1,1};
float[] mHsvSelected = new float[] {360,1,1}; float[] mHsvSelected = new float[] {360,1,1};
int mAlphaSelected = 0xff; int mAlphaSelected = 0xff;
boolean mWatch = true; boolean mWatch = true;
AlertDialog mDialog; AlertDialog mDialog;
public ColorSelector(Context context) {
/**
* Creates a color selector dialog for this Context.
* @param context Context used for this ColorSelector dialog
* @param colorSelectionListener Color selection listener to which the events will be sent to. Can be null.
*/
public ColorSelector(Context context, ColorSelectionListener colorSelectionListener) {
AlertDialog.Builder builder = new AlertDialog.Builder(context); AlertDialog.Builder builder = new AlertDialog.Builder(context);
View view = LayoutInflater.from(context).inflate(R.layout.dialog_color_selector,null); View view = LayoutInflater.from(context).inflate(R.layout.dialog_color_selector,null);
mHueView = view.findViewById(R.id.color_selector_hue_view); mHueView = view.findViewById(R.id.color_selector_hue_view);
mHueView.setHue(0f);
mLuminosityIntensityView = view.findViewById(R.id.color_selector_rectangle_view); mLuminosityIntensityView = view.findViewById(R.id.color_selector_rectangle_view);
mLuminosityIntensityView.setColor(Color.RED);
mLuminosityIntensityView.setLuminosityIntensity(1,1);
mAlphaView = view.findViewById(R.id.color_selector_alpha_view); mAlphaView = view.findViewById(R.id.color_selector_alpha_view);
mAlphaView.setAlpha(0xff);
mColorView = view.findViewById(R.id.color_selector_color_view); mColorView = view.findViewById(R.id.color_selector_color_view);
mColorView.setColor(Color.RED);
mTextView = view.findViewById(R.id.color_selector_hex_edit); mTextView = view.findViewById(R.id.color_selector_hex_edit);
runColor(Color.RED);
mHueView.setHueSelectionListener(this); mHueView.setHueSelectionListener(this);
mLuminosityIntensityView.setRectSelectionListener(this); mLuminosityIntensityView.setRectSelectionListener(this);
mAlphaView.setAlphaSelectionListener(this); mAlphaView.setAlphaSelectionListener(this);
mTextView.addTextChangedListener(this); mTextView.addTextChangedListener(this);
builder.setView(view); builder.setView(view);
builder.setPositiveButton(android.R.string.ok,(dialog,which)->{ builder.setPositiveButton(android.R.string.ok,(dialog,which)->{
if (mColorSelectionListener != null) {
mColorSelectionListener.onColorSelected(Color.HSVToColor(mAlphaSelected, mHsvSelected));
}
}); });
builder.setNegativeButton(android.R.string.cancel, ((dialog, which) -> {}));
mColorSelectionListener = colorSelectionListener;
mDialog = builder.create(); mDialog = builder.create();
} }
/**
* Shows the color selector with the default (red) color selected.
*/
public void show() { public void show() {
runColor(Color.RED);
dispatchColorChange();
mDialog.show();
}
/**
* Shows the color selector with the desired ARGB color selected
* @param previousColor the desired ARGB color
*/
public void show(int previousColor) {
runColor(previousColor); // initialize
dispatchColorChange(); // set the hex text
mDialog.show(); mDialog.show();
} }
@Override @Override
public void onHueSelected(float hue) { public void onHueSelected(float hue) {
mHsvSelected[0] = mHueTemplate[0] = hue; mHsvSelected[0] = mHueTemplate[0] = hue;
mLuminosityIntensityView.setColor(Color.HSVToColor(mHueTemplate)); mLuminosityIntensityView.setColor(Color.HSVToColor(mHueTemplate), true);
dispatchColorChange(Color.HSVToColor(mAlphaSelected, mHsvSelected)); dispatchColorChange();
} }
@Override @Override
public void onLuminosityIntensityChanged(float luminosity, float intensity) { public void onLuminosityIntensityChanged(float luminosity, float intensity) {
mHsvSelected[1] = intensity; mHsvSelected[1] = intensity;
mHsvSelected[2] = luminosity; mHsvSelected[2] = luminosity;
dispatchColorChange(Color.HSVToColor(mAlphaSelected, mHsvSelected)); dispatchColorChange();
} }
@Override @Override
public void onAlphaSelected(int alpha) { public void onAlphaSelected(int alpha) {
mAlphaSelected = alpha; mAlphaSelected = alpha;
dispatchColorChange(Color.HSVToColor(mAlphaSelected, mHsvSelected)); dispatchColorChange();
} }
/**
* Replaces the alpha value of the color passed in, and returns the result.
* @param color the color to replace the alpha of
* @param alpha the alpha to use
* @return the new color
*/
public static int setAlpha(int color, int alpha) { public static int setAlpha(int color, int alpha) {
return color & ALPHA_MASK | ((alpha & 0xFF) << 24); return color & ALPHA_MASK | ((alpha & 0xFF) << 24);
} }
protected void dispatchColorChange(int color) { //IUO: called on all color changes
protected void dispatchColorChange() {
int color = Color.HSVToColor(mAlphaSelected, mHsvSelected);
mColorView.setColor(color); mColorView.setColor(color);
mWatch = false; mWatch = false;
mTextView.setText(String.format("%08X",color)); mTextView.setText(String.format("%08X",color));
} }
//IUO: sets all Views to render the desired color. Used for initilaization and HEX color input
protected void runColor(int color) { protected void runColor(int color) {
Color.RGBToHSV(Color.red(color), Color.green(color), Color.blue(color), mHsvSelected); Color.RGBToHSV(Color.red(color), Color.green(color), Color.blue(color), mHsvSelected);
mHueTemplate[0] = mHsvSelected[0]; mHueTemplate[0] = mHsvSelected[0];
mHueView.setHue(mHsvSelected[0]); mHueView.setHue(mHsvSelected[0]);
mLuminosityIntensityView.setColor(Color.HSVToColor(mHueTemplate)); mLuminosityIntensityView.setColor(Color.HSVToColor(mHueTemplate), false);
mLuminosityIntensityView.setLuminosityIntensity(mHsvSelected[2], mHsvSelected[1]); mLuminosityIntensityView.setLuminosityIntensity(mHsvSelected[2], mHsvSelected[1]);
mAlphaView.setAlpha(Color.alpha(color)); mAlphaSelected = Color.alpha(color);
mAlphaView.setAlpha(mAlphaSelected);
mColorView.setColor(color); mColorView.setColor(color);
} }

View File

@ -62,9 +62,9 @@ public class SVRectangleView extends View {
invalidate(); invalidate();
} }
public void setColor(int color) { public void setColor(int color, boolean invalidate) {
mColorPaint.setColor(color); mColorPaint.setColor(color);
invalidate(); if(invalidate) invalidate();
} }
public void setRectSelectionListener(RectangleSelectionListener listener) { public void setRectSelectionListener(RectangleSelectionListener listener) {

View File

@ -9,6 +9,7 @@ import android.widget.ArrayAdapter;
import android.widget.CheckBox; import android.widget.CheckBox;
import android.widget.EditText; import android.widget.EditText;
import android.widget.ImageButton; import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.SeekBar; import android.widget.SeekBar;
import android.widget.Spinner; import android.widget.Spinner;
import android.widget.TextView; import android.widget.TextView;
@ -18,6 +19,7 @@ import androidx.appcompat.app.AlertDialog;
import net.kdt.pojavlaunch.EfficientAndroidLWJGLKeycode; import net.kdt.pojavlaunch.EfficientAndroidLWJGLKeycode;
import net.kdt.pojavlaunch.R; import net.kdt.pojavlaunch.R;
import net.kdt.pojavlaunch.Tools; import net.kdt.pojavlaunch.Tools;
import net.kdt.pojavlaunch.colorselector.ColorSelector;
import net.kdt.pojavlaunch.customcontrols.buttons.ControlButton; import net.kdt.pojavlaunch.customcontrols.buttons.ControlButton;
import net.kdt.pojavlaunch.customcontrols.ControlData; import net.kdt.pojavlaunch.customcontrols.ControlData;
@ -56,6 +58,9 @@ public class EditControlButtonPopup {
protected TextView mStrokeWidthTextView; protected TextView mStrokeWidthTextView;
protected TextView mStrokeColorTextView; protected TextView mStrokeColorTextView;
protected ColorSelector mColorSelector;
protected ImageView mEditingView;
protected final ControlButton mControlButton; protected final ControlButton mControlButton;
protected final ControlData mProperties; protected final ControlData mProperties;
@ -168,9 +173,11 @@ public class EditControlButtonPopup {
spinner.setAdapter(mAdapter); spinner.setAdapter(mAdapter);
} }
mColorSelector = new ColorSelector(ctx,color -> mEditingView.setImageDrawable(new ColorDrawable(color)));
//Set color imageButton behavior //Set color imageButton behavior
mBackgroundColorButton.setOnClickListener(view -> ActionPopupWindow.showColorPicker(ctx, "Edit background color", true, mBackgroundColorButton)); mBackgroundColorButton.setOnClickListener(this::colorButtonHandler);
mStrokeColorButton.setOnClickListener(view -> ActionPopupWindow.showColorPicker(ctx, "Edit stroke color", false, mStrokeColorButton)); mStrokeColorButton.setOnClickListener(this::colorButtonHandler);
//Set dialog buttons behavior //Set dialog buttons behavior
@ -183,6 +190,12 @@ public class EditControlButtonPopup {
setupCheckerboards(); setupCheckerboards();
} }
protected void colorButtonHandler(View view) {
ImageView imgView = (ImageView) view;
mEditingView = imgView;
mColorSelector.show(((ColorDrawable)(imgView.getDrawable())).getColor());
}
protected void setupDialogButtons(){ protected void setupDialogButtons(){
//Set dialog buttons behavior //Set dialog buttons behavior
mBuilder.setPositiveButton(android.R.string.ok, (dialogInterface1, i) -> { mBuilder.setPositiveButton(android.R.string.ok, (dialogInterface1, i) -> {