Skip to content

Commit

Permalink
Feat[color_selector]: rewrite to be a SideDialogView
Browse files Browse the repository at this point in the history
  • Loading branch information
artdeell committed Nov 22, 2024
1 parent 6888de8 commit bb03cb8
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 84 deletions.
3 changes: 1 addition & 2 deletions app_pojavlauncher/src/main/java/com/kdt/SideDialogView.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.Interpolator;
import android.widget.Button;
import android.widget.TextView;

Expand Down Expand Up @@ -218,7 +217,7 @@ public void onAnimationEnd(Animator animation) {
}

/** @return Whether the dialog is currently displaying */
protected final boolean isDisplaying(){
public final boolean isDisplaying(){
return mDisplaying;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,76 +5,79 @@
import android.graphics.Color;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;

import androidx.annotation.Nullable;

import com.kdt.SideDialogView;

import net.kdt.pojavlaunch.R;

public class ColorSelector implements HueSelectionListener, RectangleSelectionListener, AlphaSelectionListener, TextWatcher{
public class ColorSelector extends SideDialogView implements HueSelectionListener, RectangleSelectionListener, AlphaSelectionListener, TextWatcher{
private static final int ALPHA_MASK = ~(0xFF << 24);
private final View mRootView;
private final HueView mHueView;
private final SVRectangleView mLuminosityIntensityView;
private final AlphaView mAlphaView;
private final ColorSideBySideView mColorView;
private final EditText mTextView;
private HueView mHueView;
private SVRectangleView mLuminosityIntensityView;
private AlphaView mAlphaView;
private ColorSideBySideView mColorView;
private EditText mTextView;

private ColorSelectionListener mColorSelectionListener;
private final float[] mHueTemplate = new float[] {0,1,1};
private final float[] mHsvSelected = new float[] {360,1,1};
private int mAlphaSelected = 0xff;
private final ColorStateList mTextColors;
private ColorStateList mTextColors;
private boolean mWatch = true;

private boolean mAlphaEnabled = true;


/**
* 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, ViewGroup parent, @Nullable ColorSelectionListener colorSelectionListener) {
super(context, parent, R.layout.dialog_color_selector);
mColorSelectionListener = colorSelectionListener;
}

mRootView = LayoutInflater.from(context).inflate(R.layout.dialog_color_selector,parent, false);
mHueView = mRootView.findViewById(R.id.color_selector_hue_view);
mLuminosityIntensityView = mRootView.findViewById(R.id.color_selector_rectangle_view);
mAlphaView = mRootView.findViewById(R.id.color_selector_alpha_view);
mColorView = mRootView.findViewById(R.id.color_selector_color_view);
mTextView = mRootView.findViewById(R.id.color_selector_hex_edit);
@Override
protected void onInflate() {
super.onInflate();
// Initialize the view contents
mHueView = mDialogContent.findViewById(R.id.color_selector_hue_view);
mLuminosityIntensityView = mDialogContent.findViewById(R.id.color_selector_rectangle_view);
mAlphaView = mDialogContent.findViewById(R.id.color_selector_alpha_view);
mColorView = mDialogContent.findViewById(R.id.color_selector_color_view);
mTextView = mDialogContent.findViewById(R.id.color_selector_hex_edit);
runColor(Color.RED);
mHueView.setHueSelectionListener(this);
mLuminosityIntensityView.setRectSelectionListener(this);
mAlphaView.setAlphaSelectionListener(this);
mTextView.addTextChangedListener(this);
mTextColors = mTextView.getTextColors();

mColorSelectionListener = colorSelectionListener;

parent.addView(mRootView);
}

/** @return The root view, mainly for position manipulation purposes */
public View getRootView(){
return mRootView;
mAlphaView.setVisibility(mAlphaEnabled ? View.VISIBLE : View.GONE);

// Set elevation to show above other side dialogs.
// Jank, should be done better
View contentParent = mDialogContent.findViewById(R.id.side_dialog_scrollview);
if(contentParent != null) {
ViewGroup dialogLayout = (ViewGroup) mDialogContent.getParent();
dialogLayout.setElevation(11);
dialogLayout.setTranslationZ(11);
}
}

/**
* Shows the color selector with the default (red) color selected.
*/
public void show() {
show(Color.RED);
public void show(boolean fromRight) {
show(fromRight, Color.RED);
}

/**
* Shows the color selector with the desired ARGB color selected
* @param previousColor the desired ARGB color
*/
public void show(int previousColor) {
public void show(boolean fromRight, int previousColor) {
appear(fromRight);
runColor(previousColor); // initialize
dispatchColorChange(); // set the hex text
}
Expand Down Expand Up @@ -157,8 +160,10 @@ public void setColorSelectionListener(ColorSelectionListener listener){

public void setAlphaEnabled(boolean alphaEnabled){
mAlphaEnabled = alphaEnabled;
mAlphaView.setVisibility(alphaEnabled ? View.VISIBLE : View.GONE);
mAlphaView.setAlpha(255);
if(mAlphaView != null) {
mAlphaView.setVisibility(alphaEnabled ? View.VISIBLE : View.GONE);
mAlphaView.setAlpha(255);
}
}

private void notifyColorSelector(int color){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,12 @@

import static net.kdt.pojavlaunch.Tools.currentDisplayMetrics;

import android.animation.ObjectAnimator;
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.EditText;
Expand All @@ -21,8 +19,6 @@
import android.widget.Switch;
import android.widget.TextView;

import androidx.constraintlayout.widget.ConstraintLayout;

import com.kdt.SideDialogView;

import net.kdt.pojavlaunch.EfficientAndroidLWJGLKeycode;
Expand Down Expand Up @@ -82,8 +78,6 @@ public void onLayoutChange(View v, int left, int top, int right, int bottom, int
// Color selector related stuff
private ColorSelector mColorSelector;
private final ViewGroup mParent;
private boolean mDisplayingColor = false;
private ObjectAnimator mColorEditorAnimator;

public EditControlSideDialog(Context context, ViewGroup parent) {
super(context, parent, R.layout.dialog_control_button_setting);
Expand All @@ -100,58 +94,25 @@ protected void onInflate() {

@Override
protected void onDestroy() {
mParent.removeView(mColorSelector.getRootView());
mColorSelector.disappear(true);
}

/* While the selector could be retrofitted to side dialog, it's not worth the effort */
private void buildColorSelector() {
mColorSelector = new ColorSelector(mParent.getContext(), mParent, null);
mColorSelector.getRootView().setElevation(11);
mColorSelector.getRootView().setTranslationZ(11);
mColorSelector.getRootView().setX(-mParent.getResources().getDimensionPixelOffset(R.dimen._280sdp));

mColorEditorAnimator = ObjectAnimator.ofFloat(mColorSelector.getRootView(), "x", 0).setDuration(600);
mColorEditorAnimator.setInterpolator(new AccelerateDecelerateInterpolator());
}

/**
* Slide the layout into the visible screen area
*/
public void appearColor(boolean fromRight, int color) {
if (fromRight) {
if (!mDisplayingColor || !isAtRightColor()) {
mColorEditorAnimator.setFloatValues(currentDisplayMetrics.widthPixels, currentDisplayMetrics.widthPixels - mDialogContent.getWidth() - mMargin);
mColorEditorAnimator.start();
}
} else {
if (!mDisplayingColor || isAtRightColor()) {
mColorEditorAnimator.setFloatValues(-mDialogContent.getWidth(), mMargin);
mColorEditorAnimator.start();
}
}

// Adjust the color selector to have the same size as the control settings
ViewGroup.LayoutParams params = mColorSelector.getRootView().getLayoutParams();
params.height = ((ViewGroup)mDialogContent.getParent()).getHeight();
mColorSelector.getRootView().setLayoutParams(params);

mDisplayingColor = true;
mColorSelector.show(color == -1 ? Color.WHITE : color);
mColorSelector.show(fromRight, color == -1 ? Color.WHITE : color);
}

/**
* Slide out the layout
*/
public void disappearColor() {
if (!mDisplayingColor) return;

mDisplayingColor = false;
if (isAtRight())
mColorEditorAnimator.setFloatValues(currentDisplayMetrics.widthPixels - mDialogContent.getWidth() - mMargin, currentDisplayMetrics.widthPixels);
else
mColorEditorAnimator.setFloatValues(mMargin, -mDialogContent.getWidth());

mColorEditorAnimator.start();
mColorSelector.disappear(false);
}

/**
Expand All @@ -160,7 +121,7 @@ public void disappearColor() {
* @return True if the last layer is disappearing
*/
public boolean disappearLayer() {
if (mDisplayingColor) {
if (mColorSelector.isDisplaying()) {
disappearColor();
return false;
} else {
Expand All @@ -176,7 +137,7 @@ public void adaptPanelPosition() {
if (mDisplaying) {
boolean isAtRight = mCurrentlyEditedButton.getControlView().getX() + mCurrentlyEditedButton.getControlView().getWidth() / 2f < currentDisplayMetrics.widthPixels / 2f;
appear(isAtRight);
if (mDisplayingColor) {
if (mColorSelector.isDisplaying()) {
Tools.runOnUiThread(() -> appearColor(isAtRight, mCurrentlyEditedButton.getProperties().bgColor));
}
}
Expand Down Expand Up @@ -515,10 +476,6 @@ private float safeParseFloat(String string) {
return out;
}

private boolean isAtRightColor() {
return mColorSelector.getRootView().getX() > currentDisplayMetrics.widthPixels / 2f;
}

public void setCurrentlyEditedButton(ControlInterface button) {
if (mCurrentlyEditedButton != null)
mCurrentlyEditedButton.getControlView().removeOnLayoutChangeListener(mLayoutChangedListener);
Expand Down

0 comments on commit bb03cb8

Please sign in to comment.