Skip to content

Instantly share code, notes, and snippets.

@yoggy
Created May 25, 2015 09:42
Show Gist options
  • Save yoggy/c83918cfc206c1872be3 to your computer and use it in GitHub Desktop.
Save yoggy/c83918cfc206c1872be3 to your computer and use it in GitHub Desktop.
//
// test_dragg_op.pde
//
import java.util.*;
class Node {
float x, y, w, h;
color fill_color;
String label;
boolean selected = false;
public Node(String label, float x, float y, float w, float h, color fill_color) {
this.x = x;
this.y = y;
this.w = w;
this.h = h;
this.label = label;
this.fill_color = fill_color;
}
void selected(boolean val) {
this.selected = val;
}
boolean is_hit(float x, float y) {
if (x < this.x) return false;
if (y < this.y) return false;
if (this.x + this.w < x) return false;
if (this.y + this.h < y) return false;
return true;
}
boolean is_hit(SelectionRect rect) {
float center_x = x + w / 2;
float center_y = y + h / 2;
if (center_x < rect.x()) return false;
if (center_y < rect.y()) return false;
if (rect.x() + rect.w() < center_x) return false;
if (rect.y() + rect.h() < center_y) return false;
return true;
}
void move(float dx, float dy) {
this.x += dx;
this.y += dy;
}
void draw() {
strokeWeight(3);
stroke(0, 0, 0);
fill(fill_color);
rect(x, y, w, h, 10);
noStroke();
fill(0);
text(label, x + w / 2 - textWidth(label) / 2, y + h / 2);
if (selected == true) {
stroke(255, 0, 0);
noFill();
rect(x, y, w, h, 10);
}
}
}
class SelectionRect {
float sx, sy;
float x() {
if (sx < mouseX) {
return sx;
}
return mouseX;
}
float y() {
if (sy < mouseY) {
return sy;
}
return mouseY;
}
float w() {
return abs(sx - mouseX);
}
float h() {
return abs(sy - mouseY);
}
void draw() {
strokeWeight(2);
stroke(128, 128, 255);
fill(128, 128, 255, 100);
rect(x(), y(), w(), h());
}
}
int DRAGG_MODE_NONE = 0;
int DRAGG_MODE_MOVE_NODE = 1;
int DRAGG_MODE_RESIZE_NODE = 2;
int DRAGG_MODE_SELECTION_RECT = 3;
int mode = DRAGG_MODE_NONE;
int count = 1;
Vector<Node> nodes = new Vector<Node>();
Vector<Node> selected_nodes = new Vector<Node>();
SelectionRect selection_rect = new SelectionRect();
void setup() {
size(800, 600);
}
void draw() {
background(255);
for (int i = 0; i < nodes.size (); ++i) {
nodes.get(i).draw();
}
if (mode == DRAGG_MODE_SELECTION_RECT) {
selection_rect.draw();
}
}
void create_node(float x, float y) {
int w = 100;
int h = 60;
int hw = w / 2;
int hh = h / 2;
Node node = new Node("node" + nfs(count, 4), x - hw, y - hw, w, h, #aaffaa);
nodes.add(node);
count += 1;
}
void delete_nodes() {
for (int i = 0; i < selected_nodes.size (); ++i) {
Node node = selected_nodes.get(i);
if (node.selected ) {
nodes.remove(node);
}
}
}
void clear_selected_node() {
if (selected_nodes.size() > 0) {
for (int i = 0; i < selected_nodes.size (); ++i) {
Node node = selected_nodes.get(i);
node.selected(false);
}
selected_nodes.clear();
}
}
boolean check_selected_node(float x, float y) {
for (int i = 0; i < nodes.size (); ++i) {
Node node = nodes.get(i);
if (node.is_hit(x, y)) {
node.selected(true);
selected_nodes.add(node);
return true;
}
}
return false;
}
int get_hit_count(float x, float y) {
int hit_count = 0;
for (int i = 0; i < nodes.size (); ++i) {
Node node = nodes.get(i);
if (node.is_hit(x, y)) hit_count += 1;
}
return hit_count;
}
void process_dragged(float x, float y, float dx, float dy) {
if (mode == DRAGG_MODE_MOVE_NODE) {
if (get_hit_count(x, y) > 0) {
for (int i = 0; i < nodes.size (); ++i) {
Node node = nodes.get(i);
if (node.selected) {
node.move(dx, dy);
}
}
}
} else if (mode == DRAGG_MODE_SELECTION_RECT) {
selected_nodes.clear();
for (int i = 0; i < nodes.size (); ++i) {
Node node = nodes.get(i);
if (node.is_hit(selection_rect)) {
node.selected(true);
selected_nodes.add(node);
} else {
node.selected(false);
}
}
} else if (mode == DRAGG_MODE_RESIZE_NODE) {
}
}
float old_mouseX, old_mouseY;
float selection_rect_x, selection_rect_y;
void mousePressed(MouseEvent e) {
int click_count = e.getClickCount();
if (click_count == 1) {
if (!(keyPressed && keyCode == SHIFT) && !check_selected_node(mouseX, mouseY)) {
clear_selected_node();
}
if (check_selected_node(mouseX, mouseY)) {
mode = DRAGG_MODE_MOVE_NODE;
} else {
mode = DRAGG_MODE_SELECTION_RECT;
selection_rect.sx = mouseX;
selection_rect.sy = mouseY;
}
old_mouseX = mouseX;
old_mouseY = mouseY;
} else if (click_count == 2) {
println("double click");
if (get_hit_count(mouseX, mouseY) == 0) {
create_node(mouseX, mouseY);
}
}
}
void mouseReleased() {
mode = DRAGG_MODE_NONE;
}
void mouseDragged() {
float dx = mouseX - old_mouseX;
float dy = mouseY - old_mouseY;
process_dragged(mouseX, mouseY, dx, dy);
old_mouseX = mouseX;
old_mouseY = mouseY;
}
void keyPressed() {
println(keyCode);
if (keyCode == 8 || keyCode == 127) {
delete_nodes();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment