Skip to content

Commit a18902a

Browse files
authored
Fix mousePressed and add examples (#208)
* RLangPApplet: Add mousePressedVar Signed-off-by: Ce Gao <[email protected]> * examples: Add examples Signed-off-by: Ce Gao <[email protected]> * reference: Split the examples Signed-off-by: Ce Gao <[email protected]>
1 parent e61933d commit a18902a

File tree

11 files changed

+101
-5
lines changed

11 files changed

+101
-5
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
settings <- function() {
2+
size(640, 360)
3+
}
4+
5+
setup <- function() {
6+
background(102)
7+
}
8+
9+
draw <- function() {
10+
stroke(255)
11+
if (mousePressedVar == TRUE) {
12+
line(mouseX, mouseY, pmouseX, pmouseY)
13+
14+
}
15+
}
+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
settings <- function() {
2+
size(640, 360)
3+
}
4+
5+
setup <- function() {
6+
background(102)
7+
}
8+
9+
draw <- function() {
10+
# Call the variableEllipse() method and send it the parameters for the current
11+
# mouse position and the previous mouse position
12+
variableEllipse(mouseX, mouseY, pmouseX, pmouseY)
13+
}
14+
15+
16+
# The simple method variableEllipse() was created specifically for this program.
17+
# It calculates the speed of the mouse and draws a small ellipse if the mouse is
18+
# moving slowly and draws a large ellipse if the mouse is moving quickly
19+
20+
variableEllipse <- function(x, y, px, py) {
21+
speed = abs(x - px) + abs(y - py)
22+
stroke(speed)
23+
ellipse(x, y, speed, speed)
24+
}

examples/Examples/Pulses/Pulses.rpde

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
angle <- 0
2+
3+
settings <- function() {
4+
size(640, 360)
5+
}
6+
7+
setup <- function() {
8+
background(102)
9+
noStroke()
10+
fill(0, 102)
11+
}
12+
13+
draw <- function() {
14+
if (mousePressedVar == TRUE) {
15+
angle <- angle + 5
16+
val = cos(radians(angle)) * 12
17+
for (a in c(0, 75, 150, 225, 300)) {
18+
xoff = cos(radians(a)) * val
19+
yoff = sin(radians(a)) * val
20+
fill(0)
21+
ellipse(mouseX + xoff, mouseY + yoff, val, val)
22+
}
23+
fill(255)
24+
ellipse(mouseX, mouseY, 2, 2)
25+
}
26+
}

examples/reference/mouseButton/mouseButton0/mouseButton0.rpde renamed to examples/reference/mouseButtonVar/mouseButtonVar0/mouseButtonVar0.rpde

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ draw <- function() {
55
}
66

77
mousePressed <- function() {
8-
if (mouseButton == LEFT) {
8+
if (mouseButtonVar == LEFT) {
99
fill(0)
10-
} else if (mouseButton == RIGHT) {
10+
} else if (mouseButtonVar == RIGHT) {
1111
fill(255)
1212
} else {
1313
fill(126)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Click within the image and press the left and right mouse buttons to change the
2+
# value of the rectangle
3+
draw <- function() {
4+
if (mousePressedVar && (mouseButtonVar == LEFT)) {
5+
fill(0)
6+
} else if (mousePressedVar && (mouseButtonVar == RIGHT)) {
7+
fill(255)
8+
} else {
9+
fill(126)
10+
}
11+
rect(25, 25, 50, 50)
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
category: Input
2+
subcategory: Mouse
3+
description: "
4+
The <b>mousePressedVar</b> variable stores whether or not a mouse button is currently being pressed. The value is true when any mouse button is pressed, and false if no button is pressed. The <b>mouseButton</b> variable (see the related reference entry) can be used to determine which button has been pressed.
5+
"
6+
related:
7+
- mouseX
8+
- mouseY
9+
- pmouseX
10+
- pmouseY
11+
- mousePressed
12+
- mouseReleased
13+
- mouseClicked
14+
- mouseMoved
15+
- mouseDragged
16+
- mouseButtonVar
17+
- mouseWheel

examples/reference/mousePressed/mousePressed0/mousePressed0.rpde renamed to examples/reference/mousePressedVar/mousePressedVar1/mousePressedVar1.rpde

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Click within the image to change the value of the rectangle
22

33
draw <- function() {
4-
if (mousePressed == TRUE) {
4+
if (mousePressedVar == TRUE) {
55
fill(0)
66
} else {
77
fill(255)

src/rprocessing/RLangPApplet.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -443,7 +443,8 @@ private void wrapMouseVariables() {
443443
this.renjinEngine.put("mouseY", mouseY);
444444
this.renjinEngine.put("pmouseX", pmouseX);
445445
this.renjinEngine.put("pmouseY", pmouseY);
446-
this.renjinEngine.put("mouseButton", mouseButton);
446+
this.renjinEngine.put("mouseButtonVar", mouseButton);
447+
this.renjinEngine.put("mousePressedVar", mousePressed);
447448
}
448449

449450
private void applyFunction(String name) {
@@ -485,6 +486,7 @@ protected void wrapKeyVariables() {
485486
this.renjinEngine.put("key", pyKey);
486487
}
487488
this.renjinEngine.put("keyCode", keyCode);
489+
this.renjinEngine.put("keyPressedVar", keyPressed);
488490
}
489491

490492
/**

src/rprocessing/mode/RLangMode.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ public String[] getExtensions() {
102102
public File[] getExampleCategoryFolders() {
103103
return new File[] {new File(examplesFolder, "Basics"), new File(examplesFolder, "Libraries"),
104104
new File(examplesFolder, "reference"), new File(examplesFolder, "R Packages"),
105-
new File(examplesFolder, "Demo")};
105+
new File(examplesFolder, "Examples")};
106106
}
107107

108108
/**

0 commit comments

Comments
 (0)