mirror of
https://github.com/processing/processing4.git
synced 2026-02-04 06:09:17 +01:00
tabs to spaces, some (but not all) formatting fixed
This commit is contained in:
@@ -1,21 +1,21 @@
|
||||
/* -*- mode: java; c-basic-offset: 2; indent-tabs-mode: nil -*- */
|
||||
|
||||
/*
|
||||
Part of the Processing project - http://processing.org
|
||||
Copyright (c) 2012-15 The Processing Foundation
|
||||
Part of the Processing project - http://processing.org
|
||||
Copyright (c) 2012-15 The Processing Foundation
|
||||
|
||||
This program is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License version 2
|
||||
as published by the Free Software Foundation.
|
||||
This program is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License version 2
|
||||
as published by the Free Software Foundation.
|
||||
|
||||
This program 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.
|
||||
This program 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 this program; if not, write to the Free Software Foundation, Inc.
|
||||
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program; if not, write to the Free Software Foundation, Inc.
|
||||
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
*/
|
||||
|
||||
package processing.mode.java.tweak;
|
||||
@@ -30,360 +30,357 @@ import processing.mode.java.pdex.JavaTextAreaPainter;
|
||||
|
||||
|
||||
public class ColorControlBox {
|
||||
public boolean visible;
|
||||
ArrayList<Handle> handles;
|
||||
ColorMode colorMode;
|
||||
Color color;
|
||||
boolean ilegalColor = false;
|
||||
boolean isBW;
|
||||
boolean isHex;
|
||||
|
||||
public boolean visible;
|
||||
String drawContext;
|
||||
|
||||
ArrayList<Handle> handles;
|
||||
ColorMode colorMode;
|
||||
Color color;
|
||||
boolean ilegalColor = false;
|
||||
boolean isBW;
|
||||
boolean isHex;
|
||||
// interface
|
||||
int x, y, width, height;
|
||||
JavaTextAreaPainter painter;
|
||||
|
||||
String drawContext;
|
||||
|
||||
// interface
|
||||
int x, y, width, height;
|
||||
JavaTextAreaPainter painter;
|
||||
public ColorControlBox(String context, ColorMode mode,
|
||||
ArrayList<Handle> handles) {
|
||||
this.drawContext = context;
|
||||
this.colorMode = mode;
|
||||
this.handles = handles;
|
||||
|
||||
|
||||
public ColorControlBox(String context, ColorMode mode, ArrayList<Handle> handles) {
|
||||
this.drawContext = context;
|
||||
this.colorMode = mode;
|
||||
this.handles = handles;
|
||||
// add this box to the handles so they can update this color on change
|
||||
for (Handle h : handles) {
|
||||
h.setColorBox(this);
|
||||
}
|
||||
|
||||
// add this box to the handles so they can update this color on change
|
||||
for (Handle h : handles) {
|
||||
h.setColorBox(this);
|
||||
}
|
||||
isBW = isGrayScale();
|
||||
isHex = isHexColor();
|
||||
color = getCurrentColor();
|
||||
|
||||
isBW = isGrayScale();
|
||||
isHex = isHexColor();
|
||||
color = getCurrentColor();
|
||||
visible = Settings.alwaysShowColorBoxes;
|
||||
}
|
||||
|
||||
visible = Settings.alwaysShowColorBoxes;
|
||||
}
|
||||
|
||||
|
||||
public void initInterface(JavaTextAreaPainter textAreaPainter, int x, int y, int w, int h)
|
||||
{
|
||||
this.painter = textAreaPainter;
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
this.width = w;
|
||||
this.height = h;
|
||||
}
|
||||
public void initInterface(JavaTextAreaPainter textAreaPainter,
|
||||
int x, int y, int w, int h) {
|
||||
this.painter = textAreaPainter;
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
this.width = w;
|
||||
this.height = h;
|
||||
}
|
||||
|
||||
public void setPos(int x, int y)
|
||||
{
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
}
|
||||
|
||||
public void draw(Graphics2D g2d)
|
||||
{
|
||||
if (!visible) {
|
||||
return;
|
||||
}
|
||||
public void setPos(int x, int y) {
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
}
|
||||
|
||||
AffineTransform trans = g2d.getTransform();
|
||||
g2d.translate(x, y);
|
||||
|
||||
// draw current color
|
||||
g2d.setColor(color);
|
||||
g2d.fillRoundRect(0, 0, width, height, 5, 5);
|
||||
public void draw(Graphics2D g2d) {
|
||||
if (!visible) {
|
||||
return;
|
||||
}
|
||||
|
||||
// draw black outline
|
||||
g2d.setStroke(new BasicStroke(1));
|
||||
g2d.setColor(Color.BLACK);
|
||||
g2d.drawRoundRect(0, 0, width, height, 5, 5);
|
||||
AffineTransform trans = g2d.getTransform();
|
||||
g2d.translate(x, y);
|
||||
|
||||
if (ilegalColor) {
|
||||
g2d.setColor(Color.RED);
|
||||
g2d.setStroke(new BasicStroke(2));
|
||||
g2d.drawLine(width-3, 3, 3, height-3);
|
||||
}
|
||||
// draw current color
|
||||
g2d.setColor(color);
|
||||
g2d.fillRoundRect(0, 0, width, height, 5, 5);
|
||||
|
||||
g2d.setTransform(trans);
|
||||
}
|
||||
// draw black outline
|
||||
g2d.setStroke(new BasicStroke(1));
|
||||
g2d.setColor(Color.BLACK);
|
||||
g2d.drawRoundRect(0, 0, width, height, 5, 5);
|
||||
|
||||
public boolean isGrayScale()
|
||||
{
|
||||
if (handles.size() <= 2) {
|
||||
int value = handles.get(0).newValue.intValue();
|
||||
if ((value&0xff000000) == 0) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
if (ilegalColor) {
|
||||
g2d.setColor(Color.RED);
|
||||
g2d.setStroke(new BasicStroke(2));
|
||||
g2d.drawLine(width-3, 3, 3, height-3);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
g2d.setTransform(trans);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if color is hex or webcolor
|
||||
* @return
|
||||
* true if number is hex or webcolor
|
||||
*/
|
||||
private boolean isHexColor()
|
||||
{
|
||||
if (handles.get(0).type == "hex" || handles.get(0).type == "webcolor") {
|
||||
int value = handles.get(0).value.intValue();
|
||||
if ((value&0xff000000) != 0) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
public boolean isGrayScale() {
|
||||
if (handles.size() <= 2) {
|
||||
int value = handles.get(0).newValue.intValue();
|
||||
if ((value&0xff000000) == 0) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
public Color getCurrentColor()
|
||||
{
|
||||
try {
|
||||
if (handles.size() == 1)
|
||||
{
|
||||
if (isBW) {
|
||||
// treat as color(gray)
|
||||
float gray = handles.get(0).newValue.floatValue();
|
||||
return verifiedGrayColor(gray);
|
||||
}
|
||||
else {
|
||||
// treat as color(argb)
|
||||
int argb = handles.get(0).newValue.intValue();
|
||||
return verifiedHexColor(argb);
|
||||
}
|
||||
}
|
||||
else if (handles.size() == 2)
|
||||
{
|
||||
if (isBW) {
|
||||
// color(gray, alpha)
|
||||
float gray = handles.get(0).newValue.floatValue();
|
||||
return verifiedGrayColor(gray);
|
||||
}
|
||||
else {
|
||||
// treat as color(argb, a)
|
||||
int argb = handles.get(0).newValue.intValue();
|
||||
float a = handles.get(1).newValue.floatValue();
|
||||
return verifiedHexColor(argb, a);
|
||||
}
|
||||
}
|
||||
else if (handles.size() == 3)
|
||||
{
|
||||
// color(v1, v2, v3)
|
||||
float v1 = handles.get(0).newValue.floatValue();
|
||||
float v2 = handles.get(1).newValue.floatValue();
|
||||
float v3 = handles.get(2).newValue.floatValue();
|
||||
return false;
|
||||
}
|
||||
|
||||
if (colorMode.modeType == ColorMode.RGB) {
|
||||
return verifiedRGBColor(v1, v2, v3, colorMode.aMax);
|
||||
}
|
||||
else {
|
||||
return verifiedHSBColor(v1, v2, v3, colorMode.aMax);
|
||||
}
|
||||
}
|
||||
else if (handles.size() == 4)
|
||||
{
|
||||
// color(v1, v2, v3, alpha)
|
||||
float v1 = handles.get(0).newValue.floatValue();
|
||||
float v2 = handles.get(1).newValue.floatValue();
|
||||
float v3 = handles.get(2).newValue.floatValue();
|
||||
float a = handles.get(3).newValue.floatValue();
|
||||
|
||||
if (colorMode.modeType == ColorMode.RGB) {
|
||||
return verifiedRGBColor(v1, v2, v3, a);
|
||||
}
|
||||
else {
|
||||
return verifiedHSBColor(v1, v2, v3, a);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception e) {
|
||||
System.out.println("error parsing color value: " + e.toString());
|
||||
ilegalColor = true;
|
||||
return Color.WHITE;
|
||||
}
|
||||
/**
|
||||
* @return true if number is hex or webcolor
|
||||
*/
|
||||
private boolean isHexColor() {
|
||||
if (handles.get(0).type == "hex" || handles.get(0).type == "webcolor") {
|
||||
int value = handles.get(0).value.intValue();
|
||||
if ((value & 0xff000000) != 0) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// couldn't figure out this color, return WHITE color
|
||||
ilegalColor = true;
|
||||
return Color.WHITE;
|
||||
}
|
||||
|
||||
private Color verifiedGrayColor(float gray)
|
||||
{
|
||||
if (gray < 0 || gray > colorMode.v1Max) {
|
||||
return colorError();
|
||||
}
|
||||
public Color getCurrentColor()
|
||||
{
|
||||
try {
|
||||
if (handles.size() == 1)
|
||||
{
|
||||
if (isBW) {
|
||||
// treat as color(gray)
|
||||
float gray = handles.get(0).newValue.floatValue();
|
||||
return verifiedGrayColor(gray);
|
||||
}
|
||||
else {
|
||||
// treat as color(argb)
|
||||
int argb = handles.get(0).newValue.intValue();
|
||||
return verifiedHexColor(argb);
|
||||
}
|
||||
}
|
||||
else if (handles.size() == 2)
|
||||
{
|
||||
if (isBW) {
|
||||
// color(gray, alpha)
|
||||
float gray = handles.get(0).newValue.floatValue();
|
||||
return verifiedGrayColor(gray);
|
||||
}
|
||||
else {
|
||||
// treat as color(argb, a)
|
||||
int argb = handles.get(0).newValue.intValue();
|
||||
float a = handles.get(1).newValue.floatValue();
|
||||
return verifiedHexColor(argb, a);
|
||||
}
|
||||
}
|
||||
else if (handles.size() == 3)
|
||||
{
|
||||
// color(v1, v2, v3)
|
||||
float v1 = handles.get(0).newValue.floatValue();
|
||||
float v2 = handles.get(1).newValue.floatValue();
|
||||
float v3 = handles.get(2).newValue.floatValue();
|
||||
|
||||
ilegalColor = false;
|
||||
gray = gray/colorMode.v1Max * 255;
|
||||
return new Color((int)gray, (int)gray, (int)gray, 255);
|
||||
}
|
||||
if (colorMode.modeType == ColorMode.RGB) {
|
||||
return verifiedRGBColor(v1, v2, v3, colorMode.aMax);
|
||||
}
|
||||
else {
|
||||
return verifiedHSBColor(v1, v2, v3, colorMode.aMax);
|
||||
}
|
||||
}
|
||||
else if (handles.size() == 4)
|
||||
{
|
||||
// color(v1, v2, v3, alpha)
|
||||
float v1 = handles.get(0).newValue.floatValue();
|
||||
float v2 = handles.get(1).newValue.floatValue();
|
||||
float v3 = handles.get(2).newValue.floatValue();
|
||||
float a = handles.get(3).newValue.floatValue();
|
||||
|
||||
private Color verifiedHexColor(int argb)
|
||||
{
|
||||
int r = (argb>>16)&0xff;
|
||||
int g = (argb>>8)&0xff;
|
||||
int b = (argb&0xff);
|
||||
if (colorMode.modeType == ColorMode.RGB) {
|
||||
return verifiedRGBColor(v1, v2, v3, a);
|
||||
}
|
||||
else {
|
||||
return verifiedHSBColor(v1, v2, v3, a);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception e) {
|
||||
System.out.println("error parsing color value: " + e.toString());
|
||||
ilegalColor = true;
|
||||
return Color.WHITE;
|
||||
}
|
||||
|
||||
ilegalColor = false;
|
||||
return new Color(r, g, b, 255);
|
||||
}
|
||||
// couldn't figure out this color, return WHITE color
|
||||
ilegalColor = true;
|
||||
return Color.WHITE;
|
||||
}
|
||||
|
||||
private Color verifiedHexColor(int argb, float alpha)
|
||||
{
|
||||
int r = (argb>>16)&0xff;
|
||||
int g = (argb>>8)&0xff;
|
||||
int b = (argb&0xff);
|
||||
private Color verifiedGrayColor(float gray)
|
||||
{
|
||||
if (gray < 0 || gray > colorMode.v1Max) {
|
||||
return colorError();
|
||||
}
|
||||
|
||||
ilegalColor = false;
|
||||
return new Color(r, g, b, 255);
|
||||
}
|
||||
ilegalColor = false;
|
||||
gray = gray/colorMode.v1Max * 255;
|
||||
return new Color((int)gray, (int)gray, (int)gray, 255);
|
||||
}
|
||||
|
||||
public Color verifiedRGBColor(float r, float g, float b, float a)
|
||||
{
|
||||
if (r < 0 || r > colorMode.v1Max ||
|
||||
g < 0 || g > colorMode.v2Max ||
|
||||
b < 0 || b > colorMode.v3Max) {
|
||||
return colorError();
|
||||
}
|
||||
private Color verifiedHexColor(int argb)
|
||||
{
|
||||
int r = (argb>>16)&0xff;
|
||||
int g = (argb>>8)&0xff;
|
||||
int b = (argb&0xff);
|
||||
|
||||
ilegalColor = false;
|
||||
r = r/colorMode.v1Max * 255;
|
||||
g = g/colorMode.v2Max * 255;
|
||||
b = b/colorMode.v3Max * 255;
|
||||
return new Color((int)r, (int)g, (int)b, 255);
|
||||
}
|
||||
ilegalColor = false;
|
||||
return new Color(r, g, b, 255);
|
||||
}
|
||||
|
||||
public Color verifiedHSBColor(float h, float s, float b, float a)
|
||||
{
|
||||
if (h < 0 || h > colorMode.v1Max ||
|
||||
s < 0 || s > colorMode.v2Max ||
|
||||
b < 0 || b > colorMode.v3Max) {
|
||||
return colorError();
|
||||
}
|
||||
private Color verifiedHexColor(int argb, float alpha)
|
||||
{
|
||||
int r = (argb>>16)&0xff;
|
||||
int g = (argb>>8)&0xff;
|
||||
int b = (argb&0xff);
|
||||
|
||||
ilegalColor = false;
|
||||
Color c = Color.getHSBColor(h/colorMode.v1Max, s/colorMode.v2Max, b/colorMode.v3Max);
|
||||
return new Color(c.getRed(), c.getGreen(), c.getBlue(), 255);
|
||||
}
|
||||
ilegalColor = false;
|
||||
return new Color(r, g, b, 255);
|
||||
}
|
||||
|
||||
private Color colorError()
|
||||
{
|
||||
ilegalColor = true;
|
||||
return Color.WHITE;
|
||||
}
|
||||
public Color verifiedRGBColor(float r, float g, float b, float a)
|
||||
{
|
||||
if (r < 0 || r > colorMode.v1Max ||
|
||||
g < 0 || g > colorMode.v2Max ||
|
||||
b < 0 || b > colorMode.v3Max) {
|
||||
return colorError();
|
||||
}
|
||||
|
||||
public void colorChanged()
|
||||
{
|
||||
color = getCurrentColor();
|
||||
}
|
||||
ilegalColor = false;
|
||||
r = r/colorMode.v1Max * 255;
|
||||
g = g/colorMode.v2Max * 255;
|
||||
b = b/colorMode.v3Max * 255;
|
||||
return new Color((int)r, (int)g, (int)b, 255);
|
||||
}
|
||||
|
||||
public int getTabIndex()
|
||||
{
|
||||
return handles.get(0).tabIndex;
|
||||
}
|
||||
public Color verifiedHSBColor(float h, float s, float b, float a)
|
||||
{
|
||||
if (h < 0 || h > colorMode.v1Max ||
|
||||
s < 0 || s > colorMode.v2Max ||
|
||||
b < 0 || b > colorMode.v3Max) {
|
||||
return colorError();
|
||||
}
|
||||
|
||||
public int getLine()
|
||||
{
|
||||
return handles.get(0).line;
|
||||
}
|
||||
ilegalColor = false;
|
||||
Color c = Color.getHSBColor(h/colorMode.v1Max, s/colorMode.v2Max, b/colorMode.v3Max);
|
||||
return new Color(c.getRed(), c.getGreen(), c.getBlue(), 255);
|
||||
}
|
||||
|
||||
public int getCharIndex()
|
||||
{
|
||||
int lastHandle = handles.size()-1;
|
||||
return handles.get(lastHandle).newEndChar + 2;
|
||||
}
|
||||
private Color colorError()
|
||||
{
|
||||
ilegalColor = true;
|
||||
return Color.WHITE;
|
||||
}
|
||||
|
||||
/* Check if the point is in the box
|
||||
*
|
||||
*/
|
||||
public boolean pick(int mx, int my)
|
||||
{
|
||||
if (!visible) {
|
||||
return false;
|
||||
}
|
||||
public void colorChanged()
|
||||
{
|
||||
color = getCurrentColor();
|
||||
}
|
||||
|
||||
if (mx>x && mx < x+width && my>y && my<y+height) {
|
||||
return true;
|
||||
}
|
||||
public int getTabIndex()
|
||||
{
|
||||
return handles.get(0).tabIndex;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
public int getLine()
|
||||
{
|
||||
return handles.get(0).line;
|
||||
}
|
||||
|
||||
/* Only show the color box if mouse is on the same line
|
||||
*
|
||||
* return true if there was change
|
||||
*/
|
||||
public boolean setMouseY(int my)
|
||||
{
|
||||
boolean change = false;
|
||||
public int getCharIndex()
|
||||
{
|
||||
int lastHandle = handles.size()-1;
|
||||
return handles.get(lastHandle).newEndChar + 2;
|
||||
}
|
||||
|
||||
if (my>y && my<y+height) {
|
||||
if (!visible) {
|
||||
change = true;
|
||||
}
|
||||
visible = true;
|
||||
}
|
||||
else {
|
||||
if (visible) {
|
||||
change = true;
|
||||
}
|
||||
visible = false;
|
||||
}
|
||||
/* Check if the point is in the box
|
||||
*
|
||||
*/
|
||||
public boolean pick(int mx, int my)
|
||||
{
|
||||
if (!visible) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return change;
|
||||
}
|
||||
if (mx>x && mx < x+width && my>y && my<y+height) {
|
||||
return true;
|
||||
}
|
||||
|
||||
/* Update the color numbers with the new values that were selected
|
||||
* in the color selector
|
||||
*
|
||||
* hue, saturation and brightness parameters are always 0-255
|
||||
*/
|
||||
public void selectorChanged(int hue, int saturation, int brightness)
|
||||
{
|
||||
if (isBW) {
|
||||
// color(gray) or color(gray, alpha)
|
||||
handles.get(0).setValue((float)hue/255*colorMode.v1Max);
|
||||
}
|
||||
else {
|
||||
if (handles.size() == 1 || handles.size() == 2) {
|
||||
// color(argb)
|
||||
int prevVal = handles.get(0).newValue.intValue();
|
||||
int prevAlpha = (prevVal>>24)&0xff;
|
||||
Color c = Color.getHSBColor((float)hue/255, (float)saturation/255, (float)brightness/255);
|
||||
int newVal = (prevAlpha<<24) | (c.getRed()<<16) | (c.getGreen()<<8) | (c.getBlue());
|
||||
handles.get(0).setValue(newVal);
|
||||
}
|
||||
else if (handles.size() == 3 || handles.size() == 4) {
|
||||
// color(v1, v2, v3) or color(v1, v2, v3, alpha)
|
||||
if (colorMode.modeType == ColorMode.HSB) {
|
||||
// HSB
|
||||
float v1 = (float)hue/255 * colorMode.v1Max;
|
||||
float v2 = (float)saturation/255 * colorMode.v2Max;
|
||||
float v3 = (float)brightness/255 * colorMode.v3Max;
|
||||
handles.get(0).setValue(v1);
|
||||
handles.get(1).setValue(v2);
|
||||
handles.get(2).setValue(v3);
|
||||
}
|
||||
else {
|
||||
// RGB
|
||||
Color c = Color.getHSBColor((float)hue/255, (float)saturation/255, (float)brightness/255);
|
||||
handles.get(0).setValue((float)c.getRed()/255*colorMode.v1Max);
|
||||
handles.get(1).setValue((float)c.getGreen()/255*colorMode.v2Max);
|
||||
handles.get(2).setValue((float)c.getBlue()/255*colorMode.v3Max);
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// update our own color
|
||||
color = getCurrentColor();
|
||||
/* Only show the color box if mouse is on the same line
|
||||
*
|
||||
* return true if there was change
|
||||
*/
|
||||
public boolean setMouseY(int my)
|
||||
{
|
||||
boolean change = false;
|
||||
|
||||
// update code text painter so the user will see the changes
|
||||
painter.updateCodeText();
|
||||
painter.repaint();
|
||||
}
|
||||
if (my>y && my<y+height) {
|
||||
if (!visible) {
|
||||
change = true;
|
||||
}
|
||||
visible = true;
|
||||
}
|
||||
else {
|
||||
if (visible) {
|
||||
change = true;
|
||||
}
|
||||
visible = false;
|
||||
}
|
||||
|
||||
public String toString()
|
||||
{
|
||||
return handles.size() + " handles, color mode: " + colorMode.toString();
|
||||
}
|
||||
return change;
|
||||
}
|
||||
|
||||
/* Update the color numbers with the new values that were selected
|
||||
* in the color selector
|
||||
*
|
||||
* hue, saturation and brightness parameters are always 0-255
|
||||
*/
|
||||
public void selectorChanged(int hue, int saturation, int brightness)
|
||||
{
|
||||
if (isBW) {
|
||||
// color(gray) or color(gray, alpha)
|
||||
handles.get(0).setValue((float)hue/255*colorMode.v1Max);
|
||||
}
|
||||
else {
|
||||
if (handles.size() == 1 || handles.size() == 2) {
|
||||
// color(argb)
|
||||
int prevVal = handles.get(0).newValue.intValue();
|
||||
int prevAlpha = (prevVal>>24)&0xff;
|
||||
Color c = Color.getHSBColor((float)hue/255, (float)saturation/255, (float)brightness/255);
|
||||
int newVal = (prevAlpha<<24) | (c.getRed()<<16) | (c.getGreen()<<8) | (c.getBlue());
|
||||
handles.get(0).setValue(newVal);
|
||||
}
|
||||
else if (handles.size() == 3 || handles.size() == 4) {
|
||||
// color(v1, v2, v3) or color(v1, v2, v3, alpha)
|
||||
if (colorMode.modeType == ColorMode.HSB) {
|
||||
// HSB
|
||||
float v1 = (float)hue/255 * colorMode.v1Max;
|
||||
float v2 = (float)saturation/255 * colorMode.v2Max;
|
||||
float v3 = (float)brightness/255 * colorMode.v3Max;
|
||||
handles.get(0).setValue(v1);
|
||||
handles.get(1).setValue(v2);
|
||||
handles.get(2).setValue(v3);
|
||||
}
|
||||
else {
|
||||
// RGB
|
||||
Color c = Color.getHSBColor((float)hue/255, (float)saturation/255, (float)brightness/255);
|
||||
handles.get(0).setValue((float)c.getRed()/255*colorMode.v1Max);
|
||||
handles.get(1).setValue((float)c.getGreen()/255*colorMode.v2Max);
|
||||
handles.get(2).setValue((float)c.getBlue()/255*colorMode.v3Max);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// update our own color
|
||||
color = getCurrentColor();
|
||||
|
||||
// update code text painter so the user will see the changes
|
||||
painter.updateCodeText();
|
||||
painter.repaint();
|
||||
}
|
||||
|
||||
|
||||
public String toString() {
|
||||
return handles.size() + " handles, color mode: " + colorMode.toString();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,118 +1,108 @@
|
||||
/* -*- mode: java; c-basic-offset: 2; indent-tabs-mode: nil -*- */
|
||||
|
||||
/*
|
||||
Part of the Processing project - http://processing.org
|
||||
Copyright (c) 2012-15 The Processing Foundation
|
||||
Part of the Processing project - http://processing.org
|
||||
Copyright (c) 2012-15 The Processing Foundation
|
||||
|
||||
This program is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License version 2
|
||||
as published by the Free Software Foundation.
|
||||
This program is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License version 2
|
||||
as published by the Free Software Foundation.
|
||||
|
||||
This program 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.
|
||||
This program 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 this program; if not, write to the Free Software Foundation, Inc.
|
||||
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program; if not, write to the Free Software Foundation, Inc.
|
||||
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
*/
|
||||
|
||||
package processing.mode.java.tweak;
|
||||
|
||||
|
||||
public class ColorMode {
|
||||
final static int RGB = 0;
|
||||
final static int HSB = 1;
|
||||
|
||||
float v1Max, v2Max, v3Max, aMax;
|
||||
int modeType;
|
||||
|
||||
boolean unrecognizedMode;
|
||||
String drawContext;
|
||||
|
||||
public ColorMode(String context)
|
||||
{
|
||||
this.drawContext = context;
|
||||
modeType = RGB;
|
||||
v1Max = 255;
|
||||
v2Max = 255;
|
||||
v3Max = 255;
|
||||
aMax = 255;
|
||||
|
||||
unrecognizedMode = false;
|
||||
}
|
||||
|
||||
public ColorMode(String context, int type, float v1, float v2, float v3, float a)
|
||||
{
|
||||
this.drawContext = context;
|
||||
modeType = type;
|
||||
v1Max = v1;
|
||||
v2Max = v2;
|
||||
v3Max = v3;
|
||||
aMax = a;
|
||||
|
||||
unrecognizedMode = false;
|
||||
}
|
||||
|
||||
public static ColorMode fromString(String context, String mode)
|
||||
{
|
||||
try
|
||||
{
|
||||
String[] elements = mode.split(",");
|
||||
final static int RGB = 0;
|
||||
final static int HSB = 1;
|
||||
|
||||
// determine the type of the color mode
|
||||
int type = RGB;
|
||||
if (elements[0].trim().equals("HSB")) {
|
||||
type = HSB;
|
||||
}
|
||||
float v1Max, v2Max, v3Max, aMax;
|
||||
int modeType;
|
||||
|
||||
if (elements.length == 1) {
|
||||
// colorMode in the form of colorMode(type)
|
||||
return new ColorMode(context, type, 255, 255, 255, 255);
|
||||
}
|
||||
else if (elements.length == 2) {
|
||||
// colorMode in the form of colorMode(type, max)
|
||||
float max = Float.parseFloat(elements[1].trim());
|
||||
return new ColorMode(context, type, max, max, max, max);
|
||||
}
|
||||
else if (elements.length == 4) {
|
||||
// colorMode in the form of colorMode(type, max1, max2, max3)
|
||||
float r = Float.parseFloat(elements[1].trim());
|
||||
float g = Float.parseFloat(elements[2].trim());
|
||||
float b = Float.parseFloat(elements[3].trim());
|
||||
return new ColorMode(context, type, r, g, b, 255);
|
||||
}
|
||||
else if (elements.length == 5) {
|
||||
// colorMode in the form of colorMode(type, max1, max2, max3, maxA)
|
||||
float r = Float.parseFloat(elements[1].trim());
|
||||
float g = Float.parseFloat(elements[2].trim());
|
||||
float b = Float.parseFloat(elements[3].trim());
|
||||
float a = Float.parseFloat(elements[4].trim());
|
||||
return new ColorMode(context, type, r, g, b, a);
|
||||
}
|
||||
}
|
||||
catch(Exception e) { }
|
||||
boolean unrecognizedMode;
|
||||
String drawContext;
|
||||
|
||||
/* if we failed to parse this mode (uses variables etc..)
|
||||
* we should still keep it so we'll know there is a mode declaration
|
||||
* and we should mark it as unrecognizable
|
||||
*/
|
||||
ColorMode newMode = new ColorMode(context);
|
||||
newMode.unrecognizedMode = true;
|
||||
return newMode;
|
||||
}
|
||||
|
||||
public String toString()
|
||||
{
|
||||
String type;
|
||||
if (modeType == RGB) {
|
||||
type = "RGB";
|
||||
}
|
||||
else {
|
||||
type = "HSB";
|
||||
}
|
||||
|
||||
return "ColorMode: " + type + ": (" + v1Max + ", " + v2Max + ", " + v3Max + ", " + aMax + ")";
|
||||
}
|
||||
|
||||
public ColorMode(String context) {
|
||||
this.drawContext = context;
|
||||
modeType = RGB;
|
||||
v1Max = 255;
|
||||
v2Max = 255;
|
||||
v3Max = 255;
|
||||
aMax = 255;
|
||||
unrecognizedMode = false;
|
||||
}
|
||||
|
||||
|
||||
public ColorMode(String context, int type,
|
||||
float v1, float v2, float v3, float a) {
|
||||
this.drawContext = context;
|
||||
modeType = type;
|
||||
v1Max = v1;
|
||||
v2Max = v2;
|
||||
v3Max = v3;
|
||||
aMax = a;
|
||||
unrecognizedMode = false;
|
||||
}
|
||||
|
||||
|
||||
static public ColorMode fromString(String context, String mode) {
|
||||
try {
|
||||
String[] elements = mode.split(",");
|
||||
|
||||
// determine the type of the color mode
|
||||
int type = RGB;
|
||||
if (elements[0].trim().equals("HSB")) {
|
||||
type = HSB;
|
||||
}
|
||||
|
||||
if (elements.length == 1) {
|
||||
// colorMode in the form of colorMode(type)
|
||||
return new ColorMode(context, type, 255, 255, 255, 255);
|
||||
|
||||
} else if (elements.length == 2) {
|
||||
// colorMode in the form of colorMode(type, max)
|
||||
float max = Float.parseFloat(elements[1].trim());
|
||||
return new ColorMode(context, type, max, max, max, max);
|
||||
|
||||
} else if (elements.length == 4) {
|
||||
// colorMode in the form of colorMode(type, max1, max2, max3)
|
||||
float r = Float.parseFloat(elements[1].trim());
|
||||
float g = Float.parseFloat(elements[2].trim());
|
||||
float b = Float.parseFloat(elements[3].trim());
|
||||
return new ColorMode(context, type, r, g, b, 255);
|
||||
|
||||
} else if (elements.length == 5) {
|
||||
// colorMode in the form of colorMode(type, max1, max2, max3, maxA)
|
||||
float r = Float.parseFloat(elements[1].trim());
|
||||
float g = Float.parseFloat(elements[2].trim());
|
||||
float b = Float.parseFloat(elements[3].trim());
|
||||
float a = Float.parseFloat(elements[4].trim());
|
||||
return new ColorMode(context, type, r, g, b, a);
|
||||
}
|
||||
} catch (Exception e) { }
|
||||
|
||||
// if we failed to parse this mode (uses variables etc..)
|
||||
// we should still keep it so we'll know there is a mode declaration
|
||||
// and we should mark it as unrecognizable
|
||||
ColorMode newMode = new ColorMode(context);
|
||||
newMode.unrecognizedMode = true;
|
||||
return newMode;
|
||||
}
|
||||
|
||||
|
||||
public String toString() {
|
||||
String type = (modeType == RGB) ? "RGB" : "HSB";
|
||||
return "ColorMode: " + type +
|
||||
": (" + v1Max + ", " + v2Max + ", " + v3Max + ", " + aMax + ")";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,21 +1,21 @@
|
||||
/* -*- mode: java; c-basic-offset: 2; indent-tabs-mode: nil -*- */
|
||||
|
||||
/*
|
||||
Part of the Processing project - http://processing.org
|
||||
Copyright (c) 2012-15 The Processing Foundation
|
||||
Part of the Processing project - http://processing.org
|
||||
Copyright (c) 2012-15 The Processing Foundation
|
||||
|
||||
This program is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License version 2
|
||||
as published by the Free Software Foundation.
|
||||
This program is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License version 2
|
||||
as published by the Free Software Foundation.
|
||||
|
||||
This program 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.
|
||||
This program 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 this program; if not, write to the Free Software Foundation, Inc.
|
||||
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program; if not, write to the Free Software Foundation, Inc.
|
||||
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
*/
|
||||
|
||||
package processing.mode.java.tweak;
|
||||
@@ -24,29 +24,27 @@ import java.awt.Color;
|
||||
|
||||
|
||||
public class ColorScheme {
|
||||
private static ColorScheme instance = null;
|
||||
public Color redStrokeColor;
|
||||
public Color progressFillColor;
|
||||
public Color progressEmptyColor;
|
||||
public Color markerColor;
|
||||
public Color whitePaneColor;
|
||||
|
||||
|
||||
private ColorScheme() {
|
||||
redStrokeColor = new Color(160, 20, 20); // dark red
|
||||
progressEmptyColor = new Color(180, 180, 180, 200);
|
||||
progressFillColor = new Color(0, 0, 0, 200);
|
||||
markerColor = new Color(228, 200, 91, 127);
|
||||
whitePaneColor = new Color(255, 255, 255, 120);
|
||||
}
|
||||
|
||||
|
||||
public static ColorScheme getInstance() {
|
||||
if (instance == null) {
|
||||
instance = new ColorScheme();
|
||||
}
|
||||
return instance;
|
||||
}
|
||||
private static ColorScheme instance = null;
|
||||
public Color redStrokeColor;
|
||||
public Color progressFillColor;
|
||||
public Color progressEmptyColor;
|
||||
public Color markerColor;
|
||||
public Color whitePaneColor;
|
||||
|
||||
|
||||
private ColorScheme() {
|
||||
redStrokeColor = new Color(160, 20, 20); // dark red
|
||||
progressEmptyColor = new Color(180, 180, 180, 200);
|
||||
progressFillColor = new Color(0, 0, 0, 200);
|
||||
markerColor = new Color(228, 200, 91, 127);
|
||||
whitePaneColor = new Color(255, 255, 255, 120);
|
||||
}
|
||||
|
||||
|
||||
public static ColorScheme getInstance() {
|
||||
if (instance == null) {
|
||||
instance = new ColorScheme();
|
||||
}
|
||||
return instance;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -1,21 +1,21 @@
|
||||
/* -*- mode: java; c-basic-offset: 2; indent-tabs-mode: nil -*- */
|
||||
|
||||
/*
|
||||
Part of the Processing project - http://processing.org
|
||||
Copyright (c) 2012-15 The Processing Foundation
|
||||
Part of the Processing project - http://processing.org
|
||||
Copyright (c) 2012-15 The Processing Foundation
|
||||
|
||||
This program is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License version 2
|
||||
as published by the Free Software Foundation.
|
||||
This program is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License version 2
|
||||
as published by the Free Software Foundation.
|
||||
|
||||
This program 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.
|
||||
This program 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 this program; if not, write to the Free Software Foundation, Inc.
|
||||
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program; if not, write to the Free Software Foundation, Inc.
|
||||
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
*/
|
||||
|
||||
package processing.mode.java.tweak;
|
||||
@@ -38,229 +38,110 @@ import javax.swing.JFrame;
|
||||
|
||||
|
||||
public class ColorSelector {
|
||||
int hue, saturation, brightness;
|
||||
int hue, saturation, brightness;
|
||||
|
||||
public JFrame frame;
|
||||
public ColorControlBox colorBox;
|
||||
ColorSelectorBox selectorBox;
|
||||
ColorSelectorSlider selectorSlider;
|
||||
SelectorTopBar topBar;
|
||||
public JFrame frame;
|
||||
public ColorControlBox colorBox;
|
||||
ColorSelectorBox selectorBox;
|
||||
ColorSelectorSlider selectorSlider;
|
||||
SelectorTopBar topBar;
|
||||
|
||||
|
||||
public ColorSelector(ColorControlBox colorBox) {
|
||||
this.colorBox = colorBox;
|
||||
createFrame();
|
||||
}
|
||||
public ColorSelector(ColorControlBox colorBox) {
|
||||
this.colorBox = colorBox;
|
||||
createFrame();
|
||||
}
|
||||
|
||||
|
||||
public void createFrame() {
|
||||
frame = new JFrame();
|
||||
frame.setBackground(Color.BLACK);
|
||||
public void createFrame() {
|
||||
frame = new JFrame();
|
||||
frame.setBackground(Color.BLACK);
|
||||
|
||||
Box box = Box.createHorizontalBox();
|
||||
box.setBackground(Color.BLACK);
|
||||
Box box = Box.createHorizontalBox();
|
||||
box.setBackground(Color.BLACK);
|
||||
|
||||
selectorSlider = new ColorSelectorSlider();
|
||||
selectorSlider = new ColorSelectorSlider();
|
||||
|
||||
if (!colorBox.isBW) {
|
||||
selectorBox = new ColorSelectorBox();
|
||||
box.add(selectorBox);
|
||||
}
|
||||
if (!colorBox.isBW) {
|
||||
selectorBox = new ColorSelectorBox();
|
||||
box.add(selectorBox);
|
||||
}
|
||||
|
||||
box.add(Box.createHorizontalGlue());
|
||||
box.add(selectorSlider, BorderLayout.CENTER);
|
||||
box.add(Box.createHorizontalGlue());
|
||||
box.add(Box.createHorizontalGlue());
|
||||
box.add(selectorSlider, BorderLayout.CENTER);
|
||||
box.add(Box.createHorizontalGlue());
|
||||
|
||||
frame.getContentPane().add(box, BorderLayout.CENTER);
|
||||
frame.pack();
|
||||
frame.setResizable(false);
|
||||
frame.setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
|
||||
}
|
||||
frame.getContentPane().add(box, BorderLayout.CENTER);
|
||||
frame.pack();
|
||||
frame.setResizable(false);
|
||||
frame.setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
|
||||
}
|
||||
|
||||
|
||||
public void show(int x, int y) {
|
||||
frame.setLocation(x, y);
|
||||
frame.setVisible(true);
|
||||
frame.repaint();
|
||||
}
|
||||
public void show(int x, int y) {
|
||||
frame.setLocation(x, y);
|
||||
frame.setVisible(true);
|
||||
frame.repaint();
|
||||
}
|
||||
|
||||
|
||||
public void hide() {
|
||||
this.colorBox = null;
|
||||
frame.setVisible(false);
|
||||
}
|
||||
public void hide() {
|
||||
this.colorBox = null;
|
||||
frame.setVisible(false);
|
||||
}
|
||||
|
||||
|
||||
public void refreshColor() {
|
||||
if (!colorBox.ilegalColor) {
|
||||
setColor(colorBox.color);
|
||||
}
|
||||
}
|
||||
public void refreshColor() {
|
||||
if (!colorBox.ilegalColor) {
|
||||
setColor(colorBox.color);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public void setColor(Color c) {
|
||||
if (selectorBox != null) {
|
||||
selectorBox.setToColor(c);
|
||||
}
|
||||
selectorSlider.setToColor(c);
|
||||
repaintSelector();
|
||||
}
|
||||
public void setColor(Color c) {
|
||||
if (selectorBox != null) {
|
||||
selectorBox.setToColor(c);
|
||||
}
|
||||
selectorSlider.setToColor(c);
|
||||
repaintSelector();
|
||||
}
|
||||
|
||||
|
||||
public void satBrightChanged() {
|
||||
repaintSelector();
|
||||
}
|
||||
public void satBrightChanged() {
|
||||
repaintSelector();
|
||||
}
|
||||
|
||||
|
||||
public void hueChanged() {
|
||||
if (selectorBox != null) {
|
||||
selectorBox.renderBack();
|
||||
}
|
||||
repaintSelector();
|
||||
}
|
||||
public void hueChanged() {
|
||||
if (selectorBox != null) {
|
||||
selectorBox.renderBack();
|
||||
}
|
||||
repaintSelector();
|
||||
}
|
||||
|
||||
|
||||
public void repaintSelector() {
|
||||
if (selectorBox != null) {
|
||||
selectorBox.repaint();
|
||||
}
|
||||
selectorSlider.repaint();
|
||||
}
|
||||
|
||||
|
||||
// . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
|
||||
|
||||
|
||||
class ColorSelectorBox extends JComponent {
|
||||
int lastX, lastY;
|
||||
BufferedImage backImg;
|
||||
|
||||
ColorSelectorBox() {
|
||||
if (!colorBox.ilegalColor) {
|
||||
setToColor(colorBox.color);
|
||||
}
|
||||
renderBack();
|
||||
|
||||
addMouseListener(new MouseAdapter() {
|
||||
public void mousePressed(MouseEvent e) {
|
||||
updateMouse(e);
|
||||
}
|
||||
});
|
||||
addMouseMotionListener(new MouseAdapter() {
|
||||
public void mouseDragged(MouseEvent e) {
|
||||
updateMouse(e);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
public void paintComponent(Graphics g) {
|
||||
g.drawImage(backImg, 0, 0, this);
|
||||
|
||||
Graphics2D g2 = (Graphics2D) g;
|
||||
// otherwise the oval is hideous
|
||||
// TODO make a proper hidpi version of all this
|
||||
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
|
||||
RenderingHints.VALUE_ANTIALIAS_ON);
|
||||
|
||||
g.setColor(lastY < 128 ? Color.BLACK : Color.WHITE);
|
||||
AffineTransform tx = g2.getTransform();
|
||||
g2.translate(lastX, lastY);
|
||||
//g2.drawOval(0, 0, 5, 5);
|
||||
g2.drawOval(-3, -3, 6, 6);
|
||||
g2.drawLine(-8, 0, -6, 0);
|
||||
g2.drawLine(6, 0, 8, 0);
|
||||
g2.drawLine(0, -8, 0, -6);
|
||||
g2.drawLine(0, 6, 0, 8);
|
||||
g2.setTransform(tx);
|
||||
}
|
||||
|
||||
|
||||
public void renderBack() {
|
||||
int[] pixels = new int[256 * 256];
|
||||
int index = 0;
|
||||
for (int j = 0; j < 256; j++) {
|
||||
for (int i = 0; i < 256; i++) {
|
||||
pixels[index++] = // color(hue, i, 255-j);
|
||||
Color.HSBtoRGB(hue / 255f, (i / 255f), (255-j)/255f);
|
||||
}
|
||||
}
|
||||
backImg = new BufferedImage(256, 256, BufferedImage.TYPE_INT_RGB);
|
||||
backImg.getRaster().setDataElements(0, 0, 256, 256, pixels);
|
||||
}
|
||||
|
||||
|
||||
public void setToColor(Color c) {
|
||||
// set selector color
|
||||
float hsb[] = Color.RGBtoHSB(c.getRed(), c.getGreen(), c.getBlue(), null);
|
||||
saturation = (int) (hsb[1] * 255);
|
||||
brightness = (int) (hsb[2] * 255);
|
||||
lastX = saturation;
|
||||
lastY = 255 - brightness;
|
||||
}
|
||||
|
||||
|
||||
void updateMouse(MouseEvent event) {
|
||||
int mouseX = event.getX();
|
||||
int mouseY = event.getY();
|
||||
|
||||
if (mouseX >= 0 && mouseX < 256 &&
|
||||
mouseY >= 0 && mouseY < 256) {
|
||||
lastX = mouseX;
|
||||
lastY = mouseY;
|
||||
updateColor();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void updateColor() {
|
||||
saturation = lastX;
|
||||
brightness = 255 - lastY;
|
||||
|
||||
satBrightChanged();
|
||||
colorBox.selectorChanged(hue, saturation, brightness);
|
||||
}
|
||||
|
||||
|
||||
public Dimension getPreferredSize() {
|
||||
return new Dimension(256, 256);
|
||||
}
|
||||
|
||||
|
||||
public Dimension getMinimumSize() {
|
||||
return getPreferredSize();
|
||||
}
|
||||
|
||||
|
||||
public Dimension getMaximumSize() {
|
||||
return getPreferredSize();
|
||||
}
|
||||
}
|
||||
public void repaintSelector() {
|
||||
if (selectorBox != null) {
|
||||
selectorBox.repaint();
|
||||
}
|
||||
selectorSlider.repaint();
|
||||
}
|
||||
|
||||
|
||||
// . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
|
||||
|
||||
|
||||
class ColorSelectorSlider extends JComponent {
|
||||
final int WIDE = 30;
|
||||
BufferedImage backImg;
|
||||
int lastY;
|
||||
class ColorSelectorBox extends JComponent {
|
||||
int lastX, lastY;
|
||||
BufferedImage backImg;
|
||||
|
||||
ColorSelectorSlider() {
|
||||
// size(30, 255);
|
||||
// noLoop();
|
||||
// colorMode(HSB, 255, 255, 255);
|
||||
// strokeWeight(1);
|
||||
// noFill();
|
||||
// loadPixels();
|
||||
if (!colorBox.ilegalColor) {
|
||||
setToColor(colorBox.color);
|
||||
}
|
||||
ColorSelectorBox() {
|
||||
if (!colorBox.ilegalColor) {
|
||||
setToColor(colorBox.color);
|
||||
}
|
||||
renderBack();
|
||||
|
||||
// draw the slider background
|
||||
renderBack();
|
||||
|
||||
addMouseListener(new MouseAdapter() {
|
||||
addMouseListener(new MouseAdapter() {
|
||||
public void mousePressed(MouseEvent e) {
|
||||
updateMouse(e);
|
||||
}
|
||||
@@ -270,99 +151,218 @@ public class ColorSelector {
|
||||
updateMouse(e);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
public void paintComponent(Graphics g) {
|
||||
g.drawImage(backImg, 0, 0, this);
|
||||
|
||||
Graphics2D g2 = (Graphics2D) g;
|
||||
// if (colorBox.isBW) {
|
||||
// stroke(lastY<128 ? 0 : 255);
|
||||
// }
|
||||
// else {
|
||||
// stroke(0);
|
||||
// }
|
||||
if (colorBox.isBW && lastY >= 128) {
|
||||
g2.setColor(Color.WHITE);
|
||||
} else {
|
||||
g2.setColor(Color.BLACK);
|
||||
}
|
||||
public void paintComponent(Graphics g) {
|
||||
g.drawImage(backImg, 0, 0, this);
|
||||
|
||||
AffineTransform tx = g2.getTransform();
|
||||
g2.translate(0, lastY);
|
||||
// draw left bracket
|
||||
// beginShape();
|
||||
// vertex(5, -2);
|
||||
// vertex(1, -2);
|
||||
// vertex(1, 2);
|
||||
// vertex(5, 2);
|
||||
// endShape();
|
||||
g.drawRect(1, -2, 6, 4);
|
||||
Graphics2D g2 = (Graphics2D) g;
|
||||
// otherwise the oval is hideous
|
||||
// TODO make a proper hidpi version of all this
|
||||
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
|
||||
RenderingHints.VALUE_ANTIALIAS_ON);
|
||||
|
||||
// draw middle lines
|
||||
g.drawLine(13, 0, 17, 0);
|
||||
g.drawLine(15, -2, 15, 2);
|
||||
g.setColor(lastY < 128 ? Color.BLACK : Color.WHITE);
|
||||
AffineTransform tx = g2.getTransform();
|
||||
g2.translate(lastX, lastY);
|
||||
//g2.drawOval(0, 0, 5, 5);
|
||||
g2.drawOval(-3, -3, 6, 6);
|
||||
g2.drawLine(-8, 0, -6, 0);
|
||||
g2.drawLine(6, 0, 8, 0);
|
||||
g2.drawLine(0, -8, 0, -6);
|
||||
g2.drawLine(0, 6, 0, 8);
|
||||
g2.setTransform(tx);
|
||||
}
|
||||
|
||||
// draw right bracket
|
||||
// beginShape();
|
||||
// vertex(24, -2);
|
||||
// vertex(28, -2);
|
||||
// vertex(28, 2);
|
||||
// vertex(24, 2);
|
||||
// endShape();
|
||||
g.drawRect(24, -2, 4, 4);
|
||||
g2.setTransform(tx);
|
||||
|
||||
/*
|
||||
if (colorBox.isBW) {
|
||||
// stroke(255);
|
||||
// rect(0, 0, 29, 254);
|
||||
g.setColor(Color.WHITE);
|
||||
g.drawRect(0, 0, WIDE, 255);
|
||||
} else {
|
||||
// stroke(0);
|
||||
// line(0, 0, 0, 255);
|
||||
// line(29, 0, 29, 255);
|
||||
g.setColor(Color.BLACK);
|
||||
public void renderBack() {
|
||||
int[] pixels = new int[256 * 256];
|
||||
int index = 0;
|
||||
for (int j = 0; j < 256; j++) {
|
||||
for (int i = 0; i < 256; i++) {
|
||||
pixels[index++] = // color(hue, i, 255-j);
|
||||
Color.HSBtoRGB(hue / 255f, (i / 255f), (255-j)/255f);
|
||||
}
|
||||
}
|
||||
backImg = new BufferedImage(256, 256, BufferedImage.TYPE_INT_RGB);
|
||||
backImg.getRaster().setDataElements(0, 0, 256, 256, pixels);
|
||||
}
|
||||
|
||||
|
||||
public void setToColor(Color c) {
|
||||
// set selector color
|
||||
float hsb[] = Color.RGBtoHSB(c.getRed(), c.getGreen(), c.getBlue(), null);
|
||||
saturation = (int) (hsb[1] * 255);
|
||||
brightness = (int) (hsb[2] * 255);
|
||||
lastX = saturation;
|
||||
lastY = 255 - brightness;
|
||||
}
|
||||
|
||||
|
||||
void updateMouse(MouseEvent event) {
|
||||
int mouseX = event.getX();
|
||||
int mouseY = event.getY();
|
||||
|
||||
if (mouseX >= 0 && mouseX < 256 &&
|
||||
mouseY >= 0 && mouseY < 256) {
|
||||
lastX = mouseX;
|
||||
lastY = mouseY;
|
||||
updateColor();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void updateColor() {
|
||||
saturation = lastX;
|
||||
brightness = 255 - lastY;
|
||||
|
||||
satBrightChanged();
|
||||
colorBox.selectorChanged(hue, saturation, brightness);
|
||||
}
|
||||
|
||||
|
||||
public Dimension getPreferredSize() {
|
||||
return new Dimension(256, 256);
|
||||
}
|
||||
|
||||
|
||||
public Dimension getMinimumSize() {
|
||||
return getPreferredSize();
|
||||
}
|
||||
|
||||
|
||||
public Dimension getMaximumSize() {
|
||||
return getPreferredSize();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
|
||||
|
||||
|
||||
class ColorSelectorSlider extends JComponent {
|
||||
final int WIDE = 30;
|
||||
BufferedImage backImg;
|
||||
int lastY;
|
||||
|
||||
ColorSelectorSlider() {
|
||||
// size(30, 255);
|
||||
// noLoop();
|
||||
// colorMode(HSB, 255, 255, 255);
|
||||
// strokeWeight(1);
|
||||
// noFill();
|
||||
// loadPixels();
|
||||
if (!colorBox.ilegalColor) {
|
||||
setToColor(colorBox.color);
|
||||
}
|
||||
|
||||
// draw the slider background
|
||||
renderBack();
|
||||
|
||||
addMouseListener(new MouseAdapter() {
|
||||
public void mousePressed(MouseEvent e) {
|
||||
updateMouse(e);
|
||||
}
|
||||
});
|
||||
addMouseMotionListener(new MouseAdapter() {
|
||||
public void mouseDragged(MouseEvent e) {
|
||||
updateMouse(e);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public void paintComponent(Graphics g) {
|
||||
g.drawImage(backImg, 0, 0, this);
|
||||
|
||||
Graphics2D g2 = (Graphics2D) g;
|
||||
// if (colorBox.isBW) {
|
||||
// stroke(lastY<128 ? 0 : 255);
|
||||
// }
|
||||
// else {
|
||||
// stroke(0);
|
||||
// }
|
||||
if (colorBox.isBW && lastY >= 128) {
|
||||
g2.setColor(Color.WHITE);
|
||||
} else {
|
||||
g2.setColor(Color.BLACK);
|
||||
}
|
||||
|
||||
AffineTransform tx = g2.getTransform();
|
||||
g2.translate(0, lastY);
|
||||
// draw left bracket
|
||||
// beginShape();
|
||||
// vertex(5, -2);
|
||||
// vertex(1, -2);
|
||||
// vertex(1, 2);
|
||||
// vertex(5, 2);
|
||||
// endShape();
|
||||
g.drawRect(1, -2, 6, 4);
|
||||
|
||||
// draw middle lines
|
||||
g.drawLine(13, 0, 17, 0);
|
||||
g.drawLine(15, -2, 15, 2);
|
||||
|
||||
// draw right bracket
|
||||
// beginShape();
|
||||
// vertex(24, -2);
|
||||
// vertex(28, -2);
|
||||
// vertex(28, 2);
|
||||
// vertex(24, 2);
|
||||
// endShape();
|
||||
g.drawRect(24, -2, 4, 4);
|
||||
g2.setTransform(tx);
|
||||
|
||||
/*
|
||||
if (colorBox.isBW) {
|
||||
// stroke(255);
|
||||
// rect(0, 0, 29, 254);
|
||||
g.setColor(Color.WHITE);
|
||||
g.drawRect(0, 0, WIDE, 255);
|
||||
} else {
|
||||
// stroke(0);
|
||||
// line(0, 0, 0, 255);
|
||||
// line(29, 0, 29, 255);
|
||||
g.setColor(Color.BLACK);
|
||||
g.drawLine(0, 0, 0, 255);
|
||||
g.drawLine(29, 0, 29, 255);
|
||||
}
|
||||
*/
|
||||
}
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
||||
|
||||
void renderBack() {
|
||||
int[] pixels = new int[WIDE * 256];
|
||||
int index = 0;
|
||||
void renderBack() {
|
||||
int[] pixels = new int[WIDE * 256];
|
||||
int index = 0;
|
||||
|
||||
int argb = 0;
|
||||
for (int j = 0; j < 256; j++) {
|
||||
if (colorBox.isBW) {
|
||||
int gray = 255 - j;
|
||||
argb = 0xff000000 | (gray << 16) | (gray << 8) | gray;
|
||||
} else {
|
||||
// color(255-j, 255, 255);
|
||||
argb = Color.HSBtoRGB((255 - j) / 255f, 1, 1);
|
||||
}
|
||||
for (int i = 0; i < WIDE; i++) {
|
||||
pixels[index++] = argb;
|
||||
}
|
||||
int argb = 0;
|
||||
for (int j = 0; j < 256; j++) {
|
||||
if (colorBox.isBW) {
|
||||
int gray = 255 - j;
|
||||
argb = 0xff000000 | (gray << 16) | (gray << 8) | gray;
|
||||
} else {
|
||||
// color(255-j, 255, 255);
|
||||
argb = Color.HSBtoRGB((255 - j) / 255f, 1, 1);
|
||||
}
|
||||
for (int i = 0; i < WIDE; i++) {
|
||||
pixels[index++] = argb;
|
||||
}
|
||||
}
|
||||
backImg = new BufferedImage(WIDE, 256, BufferedImage.TYPE_INT_RGB);
|
||||
backImg.getRaster().setDataElements(0, 0, WIDE, 256, pixels);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void setToColor(Color c) {
|
||||
// set slider position
|
||||
if (colorBox.isBW) {
|
||||
hue = c.getRed();
|
||||
} else {
|
||||
float hsb[] = Color.RGBtoHSB(c.getRed(), c.getGreen(), c.getBlue(), null);
|
||||
hue = (int)(hsb[0]*255);
|
||||
}
|
||||
lastY = 255 - hue;
|
||||
}
|
||||
void setToColor(Color c) {
|
||||
// set slider position
|
||||
if (colorBox.isBW) {
|
||||
hue = c.getRed();
|
||||
} else {
|
||||
float hsb[] = Color.RGBtoHSB(c.getRed(), c.getGreen(), c.getBlue(), null);
|
||||
hue = (int)(hsb[0]*255);
|
||||
}
|
||||
lastY = 255 - hue;
|
||||
}
|
||||
|
||||
|
||||
void updateMouse(MouseEvent event) {
|
||||
@@ -374,57 +374,57 @@ public class ColorSelector {
|
||||
}
|
||||
|
||||
|
||||
public void updateColor() {
|
||||
hue = 255 - lastY;
|
||||
hueChanged();
|
||||
colorBox.selectorChanged(hue, saturation, brightness);
|
||||
}
|
||||
public void updateColor() {
|
||||
hue = 255 - lastY;
|
||||
hueChanged();
|
||||
colorBox.selectorChanged(hue, saturation, brightness);
|
||||
}
|
||||
|
||||
|
||||
public Dimension getPreferredSize() {
|
||||
return new Dimension(30, 255);
|
||||
}
|
||||
public Dimension getPreferredSize() {
|
||||
return new Dimension(30, 255);
|
||||
}
|
||||
|
||||
|
||||
public Dimension getMinimumSize() {
|
||||
return getPreferredSize();
|
||||
}
|
||||
public Dimension getMinimumSize() {
|
||||
return getPreferredSize();
|
||||
}
|
||||
|
||||
|
||||
public Dimension getMaximumSize() {
|
||||
return getPreferredSize();
|
||||
}
|
||||
}
|
||||
public Dimension getMaximumSize() {
|
||||
return getPreferredSize();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
|
||||
|
||||
|
||||
public class SelectorTopBar extends JComponent {
|
||||
int barWidth;
|
||||
int barHeight = 16;
|
||||
public class SelectorTopBar extends JComponent {
|
||||
int barWidth;
|
||||
int barHeight = 16;
|
||||
|
||||
public SelectorTopBar(int w) {
|
||||
barWidth = w;
|
||||
}
|
||||
public SelectorTopBar(int w) {
|
||||
barWidth = w;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void paintComponent(Graphics g) {
|
||||
g.setColor(Color.GRAY);
|
||||
Dimension size = getSize();
|
||||
g.fillRect(0, 0, size.width, size.height);
|
||||
}
|
||||
@Override
|
||||
public void paintComponent(Graphics g) {
|
||||
g.setColor(Color.GRAY);
|
||||
Dimension size = getSize();
|
||||
g.fillRect(0, 0, size.width, size.height);
|
||||
}
|
||||
|
||||
public Dimension getPreferredSize() {
|
||||
return new Dimension(barWidth, barHeight);
|
||||
}
|
||||
public Dimension getPreferredSize() {
|
||||
return new Dimension(barWidth, barHeight);
|
||||
}
|
||||
|
||||
public Dimension getMinimumSize() {
|
||||
return getPreferredSize();
|
||||
}
|
||||
public Dimension getMinimumSize() {
|
||||
return getPreferredSize();
|
||||
}
|
||||
|
||||
public Dimension getMaximumSize() {
|
||||
return getPreferredSize();
|
||||
}
|
||||
}
|
||||
public Dimension getMaximumSize() {
|
||||
return getPreferredSize();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,21 +1,21 @@
|
||||
/* -*- mode: java; c-basic-offset: 2; indent-tabs-mode: nil -*- */
|
||||
|
||||
/*
|
||||
Part of the Processing project - http://processing.org
|
||||
Copyright (c) 2012-15 The Processing Foundation
|
||||
Part of the Processing project - http://processing.org
|
||||
Copyright (c) 2012-15 The Processing Foundation
|
||||
|
||||
This program is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License version 2
|
||||
as published by the Free Software Foundation.
|
||||
This program is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License version 2
|
||||
as published by the Free Software Foundation.
|
||||
|
||||
This program 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.
|
||||
This program 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 this program; if not, write to the Free Software Foundation, Inc.
|
||||
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program; if not, write to the Free Software Foundation, Inc.
|
||||
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
*/
|
||||
|
||||
package processing.mode.java.tweak;
|
||||
@@ -24,71 +24,69 @@ import java.awt.Graphics2D;
|
||||
import java.awt.Polygon;
|
||||
import java.awt.geom.AffineTransform;
|
||||
|
||||
public class HProgressBar {
|
||||
int x, y, size, width;
|
||||
int pos;
|
||||
int lPolyX, rPolyX;
|
||||
Polygon rightPoly, leftPoly;
|
||||
|
||||
public HProgressBar(int size, int width)
|
||||
{
|
||||
this.size = size;
|
||||
this.width = width;
|
||||
x = 0;
|
||||
y = 0;
|
||||
setPos(0);
|
||||
|
||||
|
||||
int xl[] = {0, 0, -(int)(size/1.5)};
|
||||
int yl[] = {-(int)((float)size/3), (int)((float)size/3), 0};
|
||||
leftPoly = new Polygon(xl, yl, 3);
|
||||
int xr[] = {0, (int)(size/1.5), 0};
|
||||
int yr[] = {-(int)((float)size/3), 0, (int)((float)size/3)};
|
||||
rightPoly = new Polygon(xr, yr, 3);
|
||||
}
|
||||
|
||||
public void setPos(int pos)
|
||||
{
|
||||
this.pos = pos;
|
||||
lPolyX = 0;
|
||||
rPolyX = 0;
|
||||
|
||||
if (pos > 0) {
|
||||
rPolyX = pos;
|
||||
}
|
||||
else if (pos < 0) {
|
||||
lPolyX = pos;
|
||||
}
|
||||
}
|
||||
|
||||
public void setWidth(int width)
|
||||
{
|
||||
this.width = width;
|
||||
}
|
||||
|
||||
public void draw(Graphics2D g2d)
|
||||
{
|
||||
AffineTransform trans = g2d.getTransform();
|
||||
g2d.translate(x, y);
|
||||
|
||||
// draw white cover on text line
|
||||
g2d.setColor(ColorScheme.getInstance().whitePaneColor);
|
||||
g2d.fillRect(-200+lPolyX, -size, 200-lPolyX-width/2, size+1);
|
||||
g2d.fillRect(width/2, -size, 200+rPolyX, size+1);
|
||||
|
||||
// draw left and right triangles and leading line
|
||||
g2d.setColor(ColorScheme.getInstance().progressFillColor);
|
||||
AffineTransform tmp = g2d.getTransform();
|
||||
g2d.translate(-width/2 - 5 + lPolyX, -size/2);
|
||||
g2d.fillRect(0, -1, -lPolyX, 2);
|
||||
g2d.fillPolygon(leftPoly);
|
||||
g2d.setTransform(tmp);
|
||||
g2d.translate(width/2 + 5 + rPolyX, -size/2);
|
||||
g2d.fillRect(-rPolyX, -1, rPolyX+1, 2);
|
||||
g2d.fillPolygon(rightPoly);
|
||||
g2d.setTransform(tmp);
|
||||
|
||||
g2d.setTransform(trans);
|
||||
}
|
||||
|
||||
public class HProgressBar {
|
||||
int x, y, size, width;
|
||||
int pos;
|
||||
int lPolyX, rPolyX;
|
||||
Polygon rightPoly, leftPoly;
|
||||
|
||||
|
||||
public HProgressBar(int size, int width) {
|
||||
this.size = size;
|
||||
this.width = width;
|
||||
x = 0;
|
||||
y = 0;
|
||||
setPos(0);
|
||||
|
||||
int xl[] = {0, 0, -(int)(size/1.5)};
|
||||
int yl[] = {-(int)((float)size/3), (int)((float)size/3), 0};
|
||||
leftPoly = new Polygon(xl, yl, 3);
|
||||
int xr[] = {0, (int)(size/1.5), 0};
|
||||
int yr[] = {-(int)((float)size/3), 0, (int)((float)size/3)};
|
||||
rightPoly = new Polygon(xr, yr, 3);
|
||||
}
|
||||
|
||||
|
||||
public void setPos(int pos) {
|
||||
this.pos = pos;
|
||||
lPolyX = 0;
|
||||
rPolyX = 0;
|
||||
|
||||
if (pos > 0) {
|
||||
rPolyX = pos;
|
||||
}
|
||||
else if (pos < 0) {
|
||||
lPolyX = pos;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public void setWidth(int width) {
|
||||
this.width = width;
|
||||
}
|
||||
|
||||
|
||||
public void draw(Graphics2D g2d) {
|
||||
AffineTransform trans = g2d.getTransform();
|
||||
g2d.translate(x, y);
|
||||
|
||||
// draw white cover on text line
|
||||
g2d.setColor(ColorScheme.getInstance().whitePaneColor);
|
||||
g2d.fillRect(-200+lPolyX, -size, 200-lPolyX-width/2, size+1);
|
||||
g2d.fillRect(width/2, -size, 200+rPolyX, size+1);
|
||||
|
||||
// draw left and right triangles and leading line
|
||||
g2d.setColor(ColorScheme.getInstance().progressFillColor);
|
||||
AffineTransform tmp = g2d.getTransform();
|
||||
g2d.translate(-width/2 - 5 + lPolyX, -size/2);
|
||||
g2d.fillRect(0, -1, -lPolyX, 2);
|
||||
g2d.fillPolygon(leftPoly);
|
||||
g2d.setTransform(tmp);
|
||||
g2d.translate(width/2 + 5 + rPolyX, -size/2);
|
||||
g2d.fillRect(-rPolyX, -1, rPolyX+1, 2);
|
||||
g2d.fillPolygon(rightPoly);
|
||||
g2d.setTransform(tmp);
|
||||
g2d.setTransform(trans);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,25 +1,26 @@
|
||||
/* -*- mode: java; c-basic-offset: 2; indent-tabs-mode: nil -*- */
|
||||
|
||||
/*
|
||||
Part of the Processing project - http://processing.org
|
||||
Copyright (c) 2012-15 The Processing Foundation
|
||||
Part of the Processing project - http://processing.org
|
||||
Copyright (c) 2012-15 The Processing Foundation
|
||||
|
||||
This program is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License version 2
|
||||
as published by the Free Software Foundation.
|
||||
This program is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License version 2
|
||||
as published by the Free Software Foundation.
|
||||
|
||||
This program 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.
|
||||
This program 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 this program; if not, write to the Free Software Foundation, Inc.
|
||||
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program; if not, write to the Free Software Foundation, Inc.
|
||||
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
*/
|
||||
|
||||
package processing.mode.java.tweak;
|
||||
|
||||
|
||||
public class Settings {
|
||||
public static boolean alwaysShowColorBoxes = true;
|
||||
public static boolean alwaysShowColorBoxes = true;
|
||||
}
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -25,164 +25,164 @@ import java.nio.ByteBuffer;
|
||||
|
||||
|
||||
public class TweakClient {
|
||||
private DatagramSocket socket;
|
||||
private InetAddress address;
|
||||
private boolean initialized;
|
||||
private int sketchPort;
|
||||
private DatagramSocket socket;
|
||||
private InetAddress address;
|
||||
private boolean initialized;
|
||||
private int sketchPort;
|
||||
|
||||
static final int VAR_INT = 0;
|
||||
static final int VAR_FLOAT = 1;
|
||||
static final int SHUTDOWN = 0xffffffff;
|
||||
static final int VAR_INT = 0;
|
||||
static final int VAR_FLOAT = 1;
|
||||
static final int SHUTDOWN = 0xffffffff;
|
||||
|
||||
|
||||
public TweakClient(int sketchPort) {
|
||||
this.sketchPort = sketchPort;
|
||||
public TweakClient(int sketchPort) {
|
||||
this.sketchPort = sketchPort;
|
||||
|
||||
try {
|
||||
socket = new DatagramSocket();
|
||||
// only local sketch is allowed
|
||||
address = InetAddress.getByName("127.0.0.1");
|
||||
initialized = true;
|
||||
try {
|
||||
socket = new DatagramSocket();
|
||||
// only local sketch is allowed
|
||||
address = InetAddress.getByName("127.0.0.1");
|
||||
initialized = true;
|
||||
|
||||
} catch (SocketException e) {
|
||||
initialized = false;
|
||||
} catch (SocketException e) {
|
||||
initialized = false;
|
||||
|
||||
} catch (UnknownHostException e) {
|
||||
socket.close();
|
||||
initialized = false;
|
||||
} catch (UnknownHostException e) {
|
||||
socket.close();
|
||||
initialized = false;
|
||||
|
||||
} catch (SecurityException e) {
|
||||
socket.close();
|
||||
initialized = false;
|
||||
}
|
||||
}
|
||||
} catch (SecurityException e) {
|
||||
socket.close();
|
||||
initialized = false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public void shutdown() {
|
||||
if (initialized) {
|
||||
// send shutdown to the sketch
|
||||
sendShutdown();
|
||||
initialized = false;
|
||||
}
|
||||
}
|
||||
public void shutdown() {
|
||||
if (initialized) {
|
||||
// send shutdown to the sketch
|
||||
sendShutdown();
|
||||
initialized = false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public boolean sendInt(int index, int val) {
|
||||
if (initialized) {
|
||||
try {
|
||||
byte[] buf = new byte[12];
|
||||
ByteBuffer bb = ByteBuffer.wrap(buf);
|
||||
bb.putInt(0, VAR_INT);
|
||||
bb.putInt(4, index);
|
||||
bb.putInt(8, val);
|
||||
DatagramPacket packet = new DatagramPacket(buf, buf.length, address, sketchPort);
|
||||
socket.send(packet);
|
||||
return true;
|
||||
public boolean sendInt(int index, int val) {
|
||||
if (initialized) {
|
||||
try {
|
||||
byte[] buf = new byte[12];
|
||||
ByteBuffer bb = ByteBuffer.wrap(buf);
|
||||
bb.putInt(0, VAR_INT);
|
||||
bb.putInt(4, index);
|
||||
bb.putInt(8, val);
|
||||
DatagramPacket packet = new DatagramPacket(buf, buf.length, address, sketchPort);
|
||||
socket.send(packet);
|
||||
return true;
|
||||
|
||||
} catch (Exception e) { }
|
||||
}
|
||||
return false;
|
||||
}
|
||||
} catch (Exception e) { }
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
public boolean sendFloat(int index, float val) {
|
||||
if (initialized) {
|
||||
try {
|
||||
byte[] buf = new byte[12];
|
||||
ByteBuffer bb = ByteBuffer.wrap(buf);
|
||||
bb.putInt(0, VAR_FLOAT);
|
||||
bb.putInt(4, index);
|
||||
bb.putFloat(8, val);
|
||||
socket.send(new DatagramPacket(buf, buf.length, address, sketchPort));
|
||||
return true;
|
||||
public boolean sendFloat(int index, float val) {
|
||||
if (initialized) {
|
||||
try {
|
||||
byte[] buf = new byte[12];
|
||||
ByteBuffer bb = ByteBuffer.wrap(buf);
|
||||
bb.putInt(0, VAR_FLOAT);
|
||||
bb.putInt(4, index);
|
||||
bb.putFloat(8, val);
|
||||
socket.send(new DatagramPacket(buf, buf.length, address, sketchPort));
|
||||
return true;
|
||||
|
||||
} catch (Exception e) { }
|
||||
}
|
||||
return false;
|
||||
}
|
||||
} catch (Exception e) { }
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
public boolean sendShutdown() {
|
||||
if (initialized) {
|
||||
try {
|
||||
byte[] buf = new byte[12];
|
||||
ByteBuffer bb = ByteBuffer.wrap(buf);
|
||||
bb.putInt(0, SHUTDOWN);
|
||||
socket.send(new DatagramPacket(buf, buf.length, address, sketchPort));
|
||||
return true;
|
||||
public boolean sendShutdown() {
|
||||
if (initialized) {
|
||||
try {
|
||||
byte[] buf = new byte[12];
|
||||
ByteBuffer bb = ByteBuffer.wrap(buf);
|
||||
bb.putInt(0, SHUTDOWN);
|
||||
socket.send(new DatagramPacket(buf, buf.length, address, sketchPort));
|
||||
return true;
|
||||
|
||||
} catch (Exception e) { }
|
||||
}
|
||||
return false;
|
||||
}
|
||||
} catch (Exception e) { }
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
static public String getServerCode(int listenPort,
|
||||
boolean hasInts, boolean hasFloats) {
|
||||
String serverCode = ""+
|
||||
"class TweakModeServer extends Thread\n"+
|
||||
"{\n"+
|
||||
" protected DatagramSocket socket = null;\n"+
|
||||
" protected boolean running = true;\n"+
|
||||
" final int INT_VAR = 0;\n"+
|
||||
" final int FLOAT_VAR = 1;\n"+
|
||||
" final int SHUTDOWN = 0xffffffff;\n"+
|
||||
" public TweakModeServer() {\n"+
|
||||
" this(\"TweakModeServer\");\n"+
|
||||
" }\n"+
|
||||
" public TweakModeServer(String name) {\n"+
|
||||
" super(name);\n"+
|
||||
" }\n"+
|
||||
" public void setup()\n"+
|
||||
" {\n"+
|
||||
" try {\n"+
|
||||
" socket = new DatagramSocket("+listenPort+");\n"+
|
||||
" socket.setSoTimeout(250);\n"+
|
||||
" } catch (IOException e) {\n"+
|
||||
" println(\"error: could not create TweakMode server socket\");\n"+
|
||||
" }\n"+
|
||||
" }\n"+
|
||||
" public void run()\n"+
|
||||
" {\n"+
|
||||
" byte[] buf = new byte[256];\n"+
|
||||
" while(running)\n"+
|
||||
" {\n"+
|
||||
" try {\n"+
|
||||
" DatagramPacket packet = new DatagramPacket(buf, buf.length);\n"+
|
||||
" socket.receive(packet);\n"+
|
||||
" ByteBuffer bb = ByteBuffer.wrap(buf);\n"+
|
||||
" int type = bb.getInt(0);\n"+
|
||||
" int index = bb.getInt(4);\n";
|
||||
static public String getServerCode(int listenPort,
|
||||
boolean hasInts, boolean hasFloats) {
|
||||
String serverCode = ""+
|
||||
"class TweakModeServer extends Thread\n"+
|
||||
"{\n"+
|
||||
" protected DatagramSocket socket = null;\n"+
|
||||
" protected boolean running = true;\n"+
|
||||
" final int INT_VAR = 0;\n"+
|
||||
" final int FLOAT_VAR = 1;\n"+
|
||||
" final int SHUTDOWN = 0xffffffff;\n"+
|
||||
" public TweakModeServer() {\n"+
|
||||
" this(\"TweakModeServer\");\n"+
|
||||
" }\n"+
|
||||
" public TweakModeServer(String name) {\n"+
|
||||
" super(name);\n"+
|
||||
" }\n"+
|
||||
" public void setup()\n"+
|
||||
" {\n"+
|
||||
" try {\n"+
|
||||
" socket = new DatagramSocket("+listenPort+");\n"+
|
||||
" socket.setSoTimeout(250);\n"+
|
||||
" } catch (IOException e) {\n"+
|
||||
" println(\"error: could not create TweakMode server socket\");\n"+
|
||||
" }\n"+
|
||||
" }\n"+
|
||||
" public void run()\n"+
|
||||
" {\n"+
|
||||
" byte[] buf = new byte[256];\n"+
|
||||
" while(running)\n"+
|
||||
" {\n"+
|
||||
" try {\n"+
|
||||
" DatagramPacket packet = new DatagramPacket(buf, buf.length);\n"+
|
||||
" socket.receive(packet);\n"+
|
||||
" ByteBuffer bb = ByteBuffer.wrap(buf);\n"+
|
||||
" int type = bb.getInt(0);\n"+
|
||||
" int index = bb.getInt(4);\n";
|
||||
|
||||
if (hasInts) {
|
||||
serverCode +=
|
||||
" if (type == INT_VAR) {\n"+
|
||||
" int val = bb.getInt(8);\n"+
|
||||
" tweakmode_int[index] = val;\n"+
|
||||
" }\n"+
|
||||
" else ";
|
||||
}
|
||||
if (hasFloats) {
|
||||
serverCode +=
|
||||
" if (type == FLOAT_VAR) {\n"+
|
||||
" float val = bb.getFloat(8);\n"+
|
||||
" tweakmode_float[index] = val;\n"+
|
||||
" }\n"+
|
||||
" else";
|
||||
}
|
||||
serverCode +=
|
||||
" if (type == SHUTDOWN) {\n"+
|
||||
" running = false;\n"+
|
||||
" }\n"+
|
||||
" } catch (SocketTimeoutException e) {\n"+
|
||||
" // nothing to do here just try receiving again\n"+
|
||||
" } catch (Exception e) {\n"+
|
||||
" }\n"+
|
||||
" }\n"+
|
||||
" socket.close();\n"+
|
||||
" }\n"+
|
||||
"}\n\n\n";
|
||||
if (hasInts) {
|
||||
serverCode +=
|
||||
" if (type == INT_VAR) {\n"+
|
||||
" int val = bb.getInt(8);\n"+
|
||||
" tweakmode_int[index] = val;\n"+
|
||||
" }\n"+
|
||||
" else ";
|
||||
}
|
||||
if (hasFloats) {
|
||||
serverCode +=
|
||||
" if (type == FLOAT_VAR) {\n"+
|
||||
" float val = bb.getFloat(8);\n"+
|
||||
" tweakmode_float[index] = val;\n"+
|
||||
" }\n"+
|
||||
" else";
|
||||
}
|
||||
serverCode +=
|
||||
" if (type == SHUTDOWN) {\n"+
|
||||
" running = false;\n"+
|
||||
" }\n"+
|
||||
" } catch (SocketTimeoutException e) {\n"+
|
||||
" // nothing to do here just try receiving again\n"+
|
||||
" } catch (Exception e) {\n"+
|
||||
" }\n"+
|
||||
" }\n"+
|
||||
" socket.close();\n"+
|
||||
" }\n"+
|
||||
"}\n\n\n";
|
||||
|
||||
return serverCode;
|
||||
}
|
||||
return serverCode;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user