/**
*
*/
package com.AmishRobot.LedController;
import yuku.ambilwarna.AmbilWarnaKotak;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
/**
* @author Mike Kane
* This file is part of LED-Shirt-w-Android-Control.
*
* Copyright (C) 2011 Michael Kane
* https://github.com/thisIsMikeKane/LED-Shirt-w-Android-Control#led-shirt-w-android-control
*
* LED-Shirt-w-Android-Control is free software: you can redistribute it
* and/or modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* LED-Shirt-w-Android-Control is distributed in the hope that it will
* be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with LED-Shirt-w-Android-Control.
* If not, see <http://www.gnu.org/licenses/>.
*/
public class SET_ALL_RGB_Activity extends Activity {
// Debugging
private static final String TAG = "SET_ALL_RGB_Activity";
private static final boolean D = true;
/* Identity of parent activity */
private LEDControllerActivity PA;
/* View elements */
private Button mSetButton;
/* Color picker variables */
View viewHue;
AmbilWarnaKotak viewSatVal;
ImageView viewCursor;
View viewOldColor;
View viewNewColor;
ImageView viewTarget;
ViewGroup viewContainer;
float[] currentColorHsv = new float[3];
int color = 0xFFFFFFFF;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (D) Log.e(TAG, "+ ON CREATE +");
/* Identify parent activity to use its BT Stack */
PA = (LEDControllerActivity) this.getParent();
/* Setup view */
Color.colorToHSV(color, currentColorHsv);
final View view = LayoutInflater.from(getBaseContext()).inflate(R.layout.tab_rgb, null);
viewHue = view.findViewById(R.id.ambilwarna_viewHue);
viewSatVal = (AmbilWarnaKotak) view.findViewById(R.id.ambilwarna_viewSatBri);
viewCursor = (ImageView) view.findViewById(R.id.ambilwarna_cursor);
viewOldColor = view.findViewById(R.id.ambilwarna_warnaLama);
viewNewColor = view.findViewById(R.id.ambilwarna_warnaBaru);
viewTarget = (ImageView) view.findViewById(R.id.ambilwarna_target);
viewContainer = (ViewGroup) view.findViewById(R.id.ambilwarna_viewContainer);
viewSatVal.setHue(getHue());
viewOldColor.setBackgroundColor(color);
viewNewColor.setBackgroundColor(color);
// Initialize the Set button with a listener for click events
mSetButton = (Button) view.findViewById(R.id.tab_sar_but_set);
mSetButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
byte msg[] = new byte[4];
getColor();
// Specify RGB values between 0-31
msg[0] = LEDControllerActivity.SET_ALL_RGB;
msg[1] = (byte) (( ( (color & 0x00FF0000) >>16) / 8) & 0xFF);
msg[2] = (byte) (( ( (color & 0x0000FF00) >> 8) / 8) & 0xFF);
msg[3] = (byte) (( ( (color & 0x000000FF) ) / 8) & 0xFF);
PA.sendMessage(msg, 4);
viewOldColor.setBackgroundColor(color);
}
});
/* Listener for Hue change */
viewHue.setOnTouchListener(new View.OnTouchListener() {
@Override public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_MOVE
|| event.getAction() == MotionEvent.ACTION_DOWN
|| event.getAction() == MotionEvent.ACTION_UP) {
float y = event.getY();
if (y < 0.f) y = 0.f;
if (y > viewHue.getMeasuredHeight()) y = viewHue.getMeasuredHeight() - 0.001f; // to avoid looping from end to start.
float hue = 360.f - 360.f / viewHue.getMeasuredHeight() * y;
if (hue == 360.f) hue = 0.f;
setHue(hue);
// update view
viewSatVal.setHue(getHue());
moveCursor();
viewNewColor.setBackgroundColor(getColor());
return true;
}
return false;
}
});
/* Listener for saturation value change */
viewSatVal.setOnTouchListener(new View.OnTouchListener() {
@Override public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_MOVE
|| event.getAction() == MotionEvent.ACTION_DOWN
|| event.getAction() == MotionEvent.ACTION_UP) {
float x = event.getX(); // touch event are in dp units.
float y = event.getY();
if (x < 0.f) x = 0.f;
if (x > viewSatVal.getMeasuredWidth()) x = viewSatVal.getMeasuredWidth();
if (y < 0.f) y = 0.f;
if (y > viewSatVal.getMeasuredHeight()) y = viewSatVal.getMeasuredHeight();
setSat(1.f / viewSatVal.getMeasuredWidth() * x);
setVal(1.f - (1.f / viewSatVal.getMeasuredHeight() * y));
// update view
moveTarget();
viewNewColor.setBackgroundColor(getColor());
return true;
}
return false;
}
});
// Set up the window layout
setContentView(view);
// move cursor & target on first draw
ViewTreeObserver vto = view.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override public void onGlobalLayout() {
moveCursor();
moveTarget();
view.getViewTreeObserver().removeGlobalOnLayoutListener(this);
}
});
}
protected void moveCursor() {
float y = viewHue.getMeasuredHeight() - (getHue() * viewHue.getMeasuredHeight() / 360.f);
if (y == viewHue.getMeasuredHeight()) y = 0.f;
RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) viewCursor.getLayoutParams();
layoutParams.leftMargin = (int) (viewHue.getLeft() - Math.floor(viewCursor.getMeasuredWidth() / 2) - viewContainer.getPaddingLeft());
;
layoutParams.topMargin = (int) (viewHue.getTop() + y - Math.floor(viewCursor.getMeasuredHeight() / 2) - viewContainer.getPaddingTop());
;
viewCursor.setLayoutParams(layoutParams);
}
protected void moveTarget() {
float x = getSat() * viewSatVal.getMeasuredWidth();
float y = (1.f - getVal()) * viewSatVal.getMeasuredHeight();
RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) viewTarget.getLayoutParams();
layoutParams.leftMargin = (int) (viewSatVal.getLeft() + x - Math.floor(viewTarget.getMeasuredWidth() / 2) - viewContainer.getPaddingLeft());
layoutParams.topMargin = (int) (viewSatVal.getTop() + y - Math.floor(viewTarget.getMeasuredHeight() / 2) - viewContainer.getPaddingTop());
viewTarget.setLayoutParams(layoutParams);
}
private int getColor() {
color = Color.HSVToColor(currentColorHsv);
return color;
}
private float getHue() {
return currentColorHsv[0];
}
private float getSat() {
return currentColorHsv[1];
}
private float getVal() {
return currentColorHsv[2];
}
private void setHue(float hue) {
currentColorHsv[0] = hue;
}
private void setSat(float sat) {
currentColorHsv[1] = sat;
}
private void setVal(float val) {
currentColorHsv[2] = val;
}
}