Sketcher2 source code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

269 lines
6.8 KiB

package com.jotuntech.sketcher.common;
import java.nio.ByteBuffer;
/**
* Class for containting brush parameters for painting.
*
* @author Thor Harald Johansen
*/
public class Brush implements Streamable {
/** Display friendly name of brush. */
private String name;
/** Opacity of brush daub */
private int opacity = 0xFF;
/** Flow of brush daub. */
private int flow = 0x20;
/** Radius of brush daub. */
private float radius = 32f;
/** Hardness of brush daub. */
private int hardness = 1;
/** Spacing of brush daubs. */
private float spacing = 0.0f;
/** Route pressure to opacity of brush stroke. */
private boolean pressureToOpacity = true;
/** Route pressure to opacity of brush daub. */
private boolean pressureToFlow = false;
/** Route pressure to radius of brush daub. */
private boolean pressureToRadius = true;
/** Jitter of brush daub position. */
private float jitter = 0;
/** Amount of pixel noise applied to brush daub. */
private int noise = 0;
/** Degree of watercolor effect to apply to brush */
private int water = 0;
/** Sampling area for watercolor effect */
private float waterArea = 1f;
/** Lock transparency */
private boolean lockTransparency = false;
/**
* Constructs a new brush based on parameters.
*
* @param opacity A value from -0xFF to -0x01 (for erasers) and 0x01 to 0xFF
* (for brushes) specifying the opacity of the brush daub.
* @param flow A value from 0 to 0xFF specifying the flow of the brush daub.
* @param radius A value from 1.5 to 126 specifying the radius of the brush
* daub.
* @param hardness hardness A HARDNESS_<i>xxx...</i> constant specifying
* the hardness of the brush daub.
* @param spacing A value from 0.0 to 1.0 specifying the spacing of brush
* daubs as a fraction of the brush diameter.
* @param pressureToFlow Enables/disables the routing of tablet pressure
* to brush daub opacity.
* @param pressureToRadius Enables/disables the routing of tables pressure
* to brush daub radius.
* @param jitter A value from 0 to infinity specifying the brush position
* jitter as the fraction of the brush daub diameter.
* @param noise A value from 0x00 to 0xFF specifying the amount of pixel
* noise to add to the brush daub.
* @param water A value from 0x00 to 0xFF specifying the amount of environmental
* pickup done by the brush daub.
*/
public Brush(String name, int opacity, int flow, float radius, int hardness, float spacing, boolean pressureToOpacity, boolean pressureToFlow, boolean pressureToRadius, float jitter, int noise, int water, boolean lockTransparency, float waterArea) {
this.name = name;
this.opacity = opacity;
this.flow = flow;
this.radius = radius;
this.hardness = hardness;
this.spacing = spacing;
this.pressureToOpacity = pressureToOpacity;
this.pressureToFlow = pressureToFlow;
this.pressureToRadius = pressureToRadius;
this.jitter = jitter;
this.noise = noise;
this.water = water;
this.waterArea = waterArea;
this.lockTransparency = lockTransparency;
}
/** Constructs a new default brush. */
public Brush() {
}
/**
* Constructs a new brush.
*
* @param name Display name of the brush.
*/
public Brush(String name) {
this.name = name;
}
/**
* Returns display friendly name of brush
* @return Brush name
*/
public String getName() {
return name;
}
/**
* Set display friendly name of brush
* @param name Brush name
*/
public void setName(String name) {
this.name = name;
}
/**
* Returns copy of this brush.
*
* @return Copy of brush.
*/
public Brush copy() {
return new Brush(name, opacity, flow, radius, hardness, spacing, pressureToOpacity, pressureToFlow, pressureToRadius, jitter, noise, water, lockTransparency, waterArea);
}
public void decode(ByteBuffer in) {
opacity = in.getShort();
flow = in.getShort();
radius = in.getFloat();
hardness = in.get();
spacing = in.getFloat();
pressureToOpacity = in.get() != 0 ? true : false;
pressureToFlow = in.get() != 0 ? true : false;
pressureToRadius = in.get() != 0 ? true : false;
jitter = in.getFloat();
noise = in.getShort();
water = in.getShort();
waterArea = in.getFloat();
lockTransparency = in.get() != 0 ? true : false;
name = in.asCharBuffer().toString();
}
public void encode(ByteBuffer out) {
out.putShort((short) opacity);
out.putShort((short) flow);
out.putFloat(radius);
out.put((byte) hardness);
out.putFloat(spacing);
out.put((byte) (pressureToOpacity ? 0xFF : 0x00));
out.put((byte) (pressureToFlow ? 0xFF : 0x00));
out.put((byte) (pressureToRadius ? 0xFF : 0x00));
out.putFloat(jitter);
out.putShort((short) noise);
out.putShort((short) water);
out.putFloat(waterArea);
out.put((byte) (lockTransparency ? 0xFF : 0x00));
out.asCharBuffer().put(name);
}
public void setOpacity(int opacity) {
this.opacity = opacity;
}
public int getOpacity() {
return opacity;
}
public void setFlow(int flow) {
this.flow = flow;
}
public int getFlow() {
return flow;
}
public void setRadius(float radius) {
this.radius = radius;
}
public float getRadius() {
return radius;
}
public void setHardness(int hardness) {
this.hardness = hardness;
}
public int getHardness() {
return hardness;
}
public void setSpacing(float spacing) {
this.spacing = spacing;
}
public float getSpacing() {
return spacing;
}
public void setPressureToFlow(boolean pressureToFlow) {
this.pressureToFlow = pressureToFlow;
}
public boolean isPressureToFlow() {
return pressureToFlow;
}
public void setPressureToRadius(boolean pressureToRadius) {
this.pressureToRadius = pressureToRadius;
}
public boolean isPressureToRadius() {
return pressureToRadius;
}
public void setJitter(float jitter) {
this.jitter = jitter;
}
public float getJitter() {
return jitter;
}
public void setNoise(int noise) {
this.noise = noise;
}
public int getNoise() {
return noise;
}
public void setWater(int water) {
this.water = water;
}
public int getWater() {
return water;
}
public void setWaterArea(float waterArea) {
this.waterArea = waterArea;
}
public float getWaterArea() {
return waterArea;
}
public void setLockTransparency(boolean lockTransparency) {
this.lockTransparency = lockTransparency;
}
public boolean isLockTransparency() {
return lockTransparency;
}
public void setPressureToOpacity(boolean pressureToOpacity) {
this.pressureToOpacity = pressureToOpacity;
}
public boolean isPressureToOpacity() {
return pressureToOpacity;
}
}