Grip / mouseEntered()

This browser does not have a Java Plug-in.
Get the latest Java Plug-in here.


import compose.grips.*;

import traer.physics.Particle;
import traer.physics.ParticleSystem;
import traer.physics.Spring;
import compose.util.DampenedWave;

GripDispatcher grips;
PFont vagrb;

public void setup() {

	size(720, 380);
	smooth();

	grips = new GripDispatcher(this);

	vagrb = loadFont("vagrb.vlw");
	hint(ENABLE_NATIVE_FONTS);

	new EventSpewingGrip(1 * width / 3, height / 2, 100, false);
	new EventSpewingGrip(2 * width / 3, height / 2, 100, true);

}

public void draw() {

	background(230);
	
	// There are three particle systems responsible for "bubbles"
	// that need to be updated (traer physics)
	noGravity.tick();
	heavy.tick();
	light.tick();
	
	grips.update();
	grips.draw();
	
}

public class EventSpewingGrip extends Grip {

	public static final float MIN_RADIUS = 30, MAX_RADIUS = 200;

	public float radius;
	public float x, y;

	private StatusBubble isTheActiveBubble, isTheHoverBubble, isHoveringBubble, isSelectableBubble;

	public EventSpewingGrip(float x, float y, float radius, boolean selectable) {

		this.x = x;
		this.y = y;
		this.radius = radius;
		this.selectable = selectable;

		isTheActiveBubble = new StatusBubble(this, "isTheActive()", theActiveColor, 2 / 5f);
		isTheHoverBubble = new StatusBubble(this, "isTheHover()", theHoverColor, 2 / 5f);
		isHoveringBubble = new StatusBubble(this, "isHovering()", isHoveringColor, 1.5f / 5f);
		isSelectableBubble = new StatusBubble(this, "selectable", selectableColor, 1.5f / 5f);

	}

	public boolean isHovering() {
		return dist(x, y, mouseX, mouseY) < radius + strokeWeight / 2;
	}

	public void update() {

		if (isTheActive())
			isTheActiveBubble.show();
		else
			isTheActiveBubble.hide();

		if (isTheHover())
			isTheHoverBubble.show();
		else
			isTheHoverBubble.hide();

		if (isHovering())
			isHoveringBubble.show();
		else
			isHoveringBubble.hide();

		if (selectable)
			isSelectableBubble.show();
		else
			isSelectableBubble.hide();

	}

	/**
	 * This draw method only takes care of the EventSpewer's ellipse (the
	 * ring) and its name. The classes EventBubble and StatusBubble each
	 * have their own draw methods.
	 */
	public void draw() {

		pushStyle();
		colorMode(RGB, 255, 255, 255, 255);

		stroke(0);
		strokeWeight(strokeWeight);

		if (isTheActive()) {
			fill(red(theActiveColor), green(theActiveColor), blue(theActiveColor), opacity);
		} else if (isTheHover()) {
			fill(red(theHoverColor), green(theHoverColor), blue(theHoverColor), opacity);
		} else if (isHovering()) {
			fill(red(isHoveringColor), green(isHoveringColor), blue(isHoveringColor), opacity);
		} else {
			fill(255, opacity);
		}

		if (selectable) {
			stroke(selectableColor);
		}

		ellipse(x, y, radius * 2, radius * 2);

		if (selectable) {
			fill(selectableColor);
		} else {
			fill(0);
		}

		textFont(vagrb);

		pushMatrix();
		textAlign(CENTER);
		translate(x, y + textAscent() / 4);
		scale(1 / 2f);
		text("GRIP", 0, 0);
		popMatrix();
		popStyle();

	}

	public void focus() {
		new EventBubble(this, "focus()", true, focusColor, 2 / 5f);
	}

	public void blur() {
		new EventBubble(this, "blur()", true, blurColor, 2 / 5f);
	}

	public void mousePressed() {
		if (isTheActive()) {
			new EventBubble(this, "mousePressed()", true, mousePressedColor, 2.5f / 5f);
		}
	}

	public void mouseReleased() {
		if (isTheActive()) {
			new EventBubble(this, "mouseReleased()", true, mouseReleasedColor, 2.5f / 5f);
		}
	}

	public void mouseFirstDragged() {
		if (isTheActive()) {
			new EventBubble(this, "mouseFirstDragged()", true, mouseFirstDraggedColor, 2 / 5f);
		}
	}

	public void mouseDragged() {
		if (isTheActive()) {
				new EventBubble(this, "mouseDragged()", false, mouseDraggedColor, 1.2f / 5f);
				this.x += mouseX - pmouseX;
				this.y += mouseY - pmouseY;
		}
	}

	public void mouseMoved() {
		// That'd be a little much.
	}

	public void mouseClicked() {
		new EventBubble(this, "mouseClicked()", true, mouseClickedColor, 3f / 5f);
	}

	public void mouseDoubleClicked() {
		new EventBubble(this, "mouseDoubleClicked()", true, mouseDoubleClickedColor, 3f / 5f);
	}

	public void mouseEntered() {
		new EventBubble(this, "mouseEntered()", true, mouseEnteredColor, 2 / 5f);
	}

	public void mouseExited() {
		new EventBubble(this, "mouseExited()", true, mouseExitedColor, 2 / 5f);
	}

	public void mouseWheeled(int steps) {
		if (isTheHover()) {
			 radius += steps * 2;
			 radius = max(MIN_RADIUS, radius);
			 radius = min(MAX_RADIUS, radius);
		}
	}

	public void keyTyped(char c) {
		if (isTheActive()) {
			new EventBubble(this, "keyTyped(" + c + ")", true, keyTypedColor, 4 / 5f);
		}
	}

}

public class EventBubble extends Grip {

	String message;

	PVector acceleration = new PVector(), velocity = new PVector();
	traer.physics.Particle p;

	int color = 0xFF000000;
	float rotation;
	float scale = 1;
	int age = 0;

	ParticleSystem system;
	EventSpewingGrip spewedFrom;

	public EventBubble(EventSpewingGrip spewedFrom, String message, boolean useHeavyPhysics, int color, float scale) {
		this.spewedFrom = spewedFrom;
		pushStyle();
		textFont(vagrb);
		float tr = spewedFrom.radius + textWidth(message) / 2 * scale;
		popStyle();
		this.system = useHeavyPhysics ? heavy : light;
		this.message = message;
		this.color = color;
		this.scale = scale;
		this.rotation = random(minAngle, maxAngle);
		this.p = system.makeParticle(bubbleMass * scale, spewedFrom.x + tr * cos(rotation), spewedFrom.y + tr * sin(rotation), 0);
		for (int i = 0; i < system.numberOfParticles(); i++) {
			Particle cur = system.getParticle(i);
			if (cur != p) {
				system.makeAttraction(p, cur, -repulsion, 1f);
			}
		}
		float initialVelocity = random(minInitialVelocity, maxInitialVelocity);
		this.velocity = new PVector(initialVelocity * cos(rotation), initialVelocity * sin(rotation));
		p.velocity().set(velocity.x, velocity.y, 0f);
	}
	public void update() {
		if (age >= bubbleLife) {
			system.removeParticle(p);
			grips.remove(this);
		}
		age++;
	}
	public void draw() {

		if (p == null)
			return;

		pushMatrix();
		pushStyle();
		textFont(vagrb);
		textAlign(CENTER);
		translate(p.position().x(), p.position().y());
		rotate(rotation);

		float i = map(age, 0, bubbleLife, 0, 1);
		i = map(i, beginScaleOut, 1, 0, 1);
		i = constrain(i, 0, 1);

		// Scale out
		float s = map(i, 0, 1, scale, 0);
		s = min(s, scale);
		scale(s);

		// Draw bubble
		fill(color);
		noStroke();
		rectMode(CENTER);

		rect(0, textOffset, textWidth(message) + textPadding, textAscent() + textDescent() + textPadding);
		triangle(-(textWidth(message) + textPadding) / 2 - 30 + 2, 0 + textOffset, -(textWidth(message) + textPadding) / 2 + 2, 
				 10 + textOffset, -(textWidth(message) + textPadding) / 2 + 2, -10 + textOffset);
		
		// Draw message
		fill(255);
		text(message, 0, 0);
		popStyle();
		popMatrix();

	}
}

public class StatusBubble extends Grip {
	String message;
	int color = 0xFF000000;
	Particle anchor, p;
	float scale;
	DampenedWave size;
	float tr;
	Spring tether;

	EventSpewingGrip spewedFrom;

	public StatusBubble(EventSpewingGrip spewedFrom, String message, int color, float scale) {
		this.spewedFrom = spewedFrom;
		this.message = message;
		this.anchor = noGravity.makeParticle(1, spewedFrom.x, spewedFrom.y, 0);
		anchor.makeFixed();
		this.p = noGravity.makeParticle(1, spewedFrom.x, spewedFrom.y, 0);
		this.color = color;
		this.scale = scale;
		tr = spewedFrom.radius + 20;
		tether = noGravity.makeSpring(anchor, p, 1, 0.4f, tr);
		for (int i = 0; i < noGravity.numberOfParticles(); i++) {
			Particle cur = noGravity.getParticle(i);
			if (cur != p && cur.isFree()) {
				noGravity.makeAttraction(p, cur, -300, 0.1f);
			}
		}
		size = new DampenedWave();
		p.position().add(random(-2), random(-1), 0);
	}
	
	public void show() {
		this.size.setRestPosition(1);
	}
	public void hide() {
		this.size.setRestPosition(0);
	}
	public void update() {
		size.update();
		anchor.position().set(spewedFrom.x, spewedFrom.y, 0);
		tether.setRestLength(spewedFrom.radius + 20);
	}
	public void draw() {
		pushStyle();
		noFill();
		float a = atan2(p.position().y() - anchor.position().y(), p.position().x() - anchor.position().x());
		if (size.getRestPosition() != 0) {
			stroke(color);
			strokeWeight(4);
			line(spewedFrom.x + (spewedFrom.radius + strokeWeight / 2) * cos(a), spewedFrom.y + (spewedFrom.radius + strokeWeight / 2) * sin(a), p.position().x(), p.position().y());
		}
		pushMatrix();
		textFont(vagrb, 48);
		textAlign(CENTER);
		translate(p.position().x(), p.position().y());
		rotate(a);
		rotate(PI / 2);
		noStroke();
		scale(max(scale * size.getPosition(), 0));
		scale(0.85f);
		fill(color);
		rectMode(CENTER);
		rect(0, textOffset, textWidth(message) + textPadding, textAscent() + textDescent() + textPadding);

		fill(255);
		if (a > 0) {
			rotate(PI);
			text(message, 0, 22);
		} else {

			text(message, 0, 0);
		}
		popMatrix();
		popStyle();

	}

}

/* Physics variables */

public float minAngle = -PI / 6;
public float maxAngle = PI / 6;
public float bubbleMass = 2;
public float minInitialVelocity = 10, maxInitialVelocity = 20;
public float repulsion = 20;
public int bubbleLife = 40;
public float textOffset = -11, textPadding = 30;
public float beginScaleOut = 0.9f;

ParticleSystem noGravity = new ParticleSystem(0f, 0.5f);
ParticleSystem heavy = new ParticleSystem(0, 0.2f);
ParticleSystem light = new ParticleSystem(0.85f, 0.4f);

/* Colors */

float strokeWeight = 12;
int opacity = 210;
int theActiveColor = 0xFF00AAFF, 
	theHoverColor = 0xFF00E8BB, 
	isHoveringColor = 0xFF00B290, 
	selectableColor = 0xFF004E89;

public int focusColor = theActiveColor, 
		   blurColor = theActiveColor, 
		   mousePressedColor = 0xFF000000, 
		   mouseReleasedColor = 0xFF000000, 
		   mouseFirstDraggedColor = 0xFF666666, 
		   mouseDraggedColor = mouseFirstDraggedColor, 
		   mouseClickedColor = 0xFFFF003C, 
		   mouseDoubleClickedColor = mouseClickedColor, 
		   mouseExitedColor = 0xFF00E8BB, 
		   mouseEnteredColor = mouseExitedColor, 
		   keyTypedColor = 0, 
		   mouseWheeledColor = 0;
Description

Called when this grip becomes the hover.

Syntax
class FancyGrip extends Grip {
   void mouseEntered() {
      statements
   }
}
Parameters
None
Returns
None
Usage
Web & Applications
Related
Last Modified: February 2, 2010

No Comments

You must be logged in to post a comment.