Skip to content

Commit

Permalink
add set cursor method and simplify coordinate rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
mikebarkmin committed Oct 15, 2023
1 parent 6b79038 commit dbe4c74
Show file tree
Hide file tree
Showing 12 changed files with 155 additions and 76 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ name: Changelog
index: 4
---

## 4.6.0

- 🚀 Feat: Add setCursor method to the stage class.

## 4.5.0

- 💥 BREAKING CHANGE: Only use double for methods instead of supporting both float and doubles. This was unnecessary and cluttered the BlueJ interface with "duplicated" methods. This could potentially break your project if you were using floats, just replace them with doubles, and you are good to go.
Expand Down
34 changes: 34 additions & 0 deletions docs/en/book/reference/stage/looks/setCursor.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"template": "method",
"related": [],
"scratchblock": "set cursor (file path)",
"name": "setCursor()",
"class": "Stage",
"description": "Sets the cursor. It is recommended that the size is 16x16 or 32x32 pixels. The active spot of the cursor defaults to the top left corner (x = 0, y = 0). You can set the active spot by using the x and y argument. The values for the parameters x and y must be less than the dimensions of the image.",
"syntax": [".setCursor(path), .setCursor(path,x,y)"],
"returns": "void",
"examples": [
{
"src": "StageSetCursor.java",
"preview": "StageSetCursor.gif",
"lines": "reg:[Rr]ecorder"
}
],
"parameters": [
{
"name": "path",
"description": "The path to the image",
"type": "String"
},
{
"name": "x",
"type": "int",
"description": "The x coordinate of the active spot of the cursor."
},
{
"name": "y",
"type": "int",
"description": "The y coordinate of the active spot of the cursor."
}
]
}
27 changes: 27 additions & 0 deletions examples/reference/StageSetCursor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import org.openpatch.scratch.*;
import org.openpatch.scratch.extensions.recorder.*;

public class StageSetCursor {

public StageSetCursor() {
Stage myStage = new Stage(600, 240);
GifRecorder recorder =
new GifRecorder("examples/reference/" + this.getClass().getName() + ".gif");
recorder.start();
while(myStage.getTimer().forMillis(5000)) {
if (myStage.getMouseX() < 0) {
myStage.setCursor("assets/cursor_hand.png");
} else {
myStage.setCursor("assets/cursor_sword.png", 30, 30);
}
myStage.display("Mouse: " + myStage.getMouseX() + ", " + myStage.getMouseY());
myStage.wait(16);
}
recorder.stop();
myStage.exit();
}

public static void main(String[] args) {
new StageSetCursor();
}
}
Binary file added examples/reference/assets/cursor_hand.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/reference/assets/cursor_sword.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
115 changes: 67 additions & 48 deletions src/org/openpatch/scratch/Stage.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ public class Stage {

private PGraphics debugBuffer;

private String cursor;
private int cursorActiveSpotX;
private int cursorActiveSpotY;
private final Text display;
private final AbstractMap<String, Timer> timer;
List<Text> texts;
Expand All @@ -72,6 +75,7 @@ public Stage(int width, final int height) {
}

public Stage(int width, final int height, String assets) {
this.cursor = null;
this.texts = new CopyOnWriteArrayList<>();
this.pens = new CopyOnWriteArrayList<>();
this.sprites = new CopyOnWriteArrayList<>();
Expand Down Expand Up @@ -875,8 +879,38 @@ public void broadcast(String message) {

public void whenIReceive(String message) {}

/** Draws the current backdrop or if none a solid color */
public void pre() {
public void setCursor(String path) {
this.cursor = path;
this.cursorActiveSpotX = 0;
this.cursorActiveSpotY = 0;
}

public void setCursor(String path, int x, int y) {
this.cursor = path;
this.cursorActiveSpotX = x;
this.cursorActiveSpotY = y;
}

/**
* Stop the execution of the whole applications for the given milliseconds.
*
* @param millis Milliseconds
*/
public void wait(int millis) {
try {
Thread.sleep(millis);
} catch (InterruptedException e) {
}
}

public void run() {}

/** Close the window and therefore the whole application. */
public void exit() {
Window.getInstance().exit();
}

public void draw() {
Applet applet = Applet.getInstance();
if (applet == null) return;
// redraw background to clear screen
Expand All @@ -888,64 +922,34 @@ public void pre() {
this.backdrops.get(this.currentBackdrop).drawAsBackground();
}
this.backgroundBuffer.beginDraw();
this.backgroundBuffer.translate(this.getWidth() / 2, this.getHeight() / 2);
if (this.eraseBackgroundBuffer) {
this.backgroundBuffer.clear();
this.eraseBackgroundBuffer = false;
}
this.pens.stream().filter(p -> p.isInBackground()).forEach(p -> p.draw());
this.sprites.stream().forEach(s -> s.getPen().draw());
this.sprites.stream().filter(s -> s.getPen().isInBackground()).forEach(s -> s.getPen().draw());
while (!this.backgroundStamps.isEmpty()) {
this.backgroundStamps.poll().draw(this.backgroundBuffer);
}
this.backgroundBuffer.endDraw();
if (this.backgroundBuffer.pixels != null) {
applet.image(this.backgroundBuffer, applet.width / 2, applet.height / 2);
applet.image(this.backgroundBuffer, 0, 0);
} else {
try {
this.backgroundBuffer.loadPixels();
} catch (Exception e) {
}
}

this.foregroundBuffer.beginDraw();
if (this.eraseForegroundBuffer) {
this.foregroundBuffer.clear();
this.eraseForegroundBuffer = false;
}
this.pens.stream().filter(p -> !p.isInBackground()).forEach(p -> p.draw());
while (!this.foregroundStamps.isEmpty()) {
this.foregroundStamps.poll().draw(this.foregroundBuffer);
}
this.foregroundBuffer.endDraw();
}

/**
* Stop the execution of the whole applications for the given milliseconds.
*
* @param millis Milliseconds
*/
public void wait(int millis) {
try {
Thread.sleep(millis);
} catch (InterruptedException e) {
}
}

public void run() {}

/** Close the window and therefore the whole application. */
public void exit() {
Window.getInstance().exit();
}

public void draw() {
Applet applet = Applet.getInstance();
if (applet == null) return;

if (this.sorter != null) {
this.sprites.sort(this.sorter);
}

if (this.cursor != null) {
applet.cursor(Image.loadImage(this.cursor), this.cursorActiveSpotX, this.cursorActiveSpotY);
}

this.run();
this.sprites.stream().forEach(s -> s.run());

Expand All @@ -956,8 +960,22 @@ public void draw() {
this.display.draw();
}

// draw foreground
this.foregroundBuffer.beginDraw();
this.foregroundBuffer.translate(this.getWidth() / 2, this.getHeight() / 2);
if (this.eraseForegroundBuffer) {
this.foregroundBuffer.clear();
this.eraseForegroundBuffer = false;
}
this.pens.stream().filter(p -> !p.isInBackground()).forEach(p -> p.draw());
this.sprites.stream().filter(s -> !s.getPen().isInBackground()).forEach(s -> s.getPen().draw());
while (!this.foregroundStamps.isEmpty()) {
this.foregroundStamps.poll().draw(this.foregroundBuffer);
}
this.foregroundBuffer.endDraw();

if (this.foregroundBuffer.pixels != null) {
applet.image(this.foregroundBuffer, applet.width / 2, applet.height / 2);
applet.image(this.foregroundBuffer, 0, 0);
} else {
try {
this.foregroundBuffer.loadPixels();
Expand All @@ -967,25 +985,26 @@ public void draw() {

if (applet.isDebug()) {
this.debugBuffer.beginDraw();
this.debugBuffer.translate(this.getWidth() / 2, this.getHeight() / 2);
this.debugBuffer.clear();
this.sprites.stream().forEach(s -> s.drawDebug());
this.debugBuffer.strokeWeight(1);
this.debugBuffer.stroke(Window.DEBUG_COLOR[0], Window.DEBUG_COLOR[1], Window.DEBUG_COLOR[2]);
this.debugBuffer.fill(Window.DEBUG_COLOR[0], Window.DEBUG_COLOR[1], Window.DEBUG_COLOR[2]);
var w = this.getWidth() / 2;
var h = this.getHeight() / 2;
this.debugBuffer.line((float) (this.mouseX + w), 0, (float) (this.mouseX + w), applet.height);
this.debugBuffer.line(
0, (float) (-this.mouseY + h), applet.width, (float) (-this.mouseY + h));
(float) this.mouseX, -applet.height / 2, (float) this.mouseX, applet.height);
this.debugBuffer.line(
-applet.width / 2, (float) -this.mouseY, applet.width, (float) -this.mouseY);
this.debugBuffer.textFont(Font.getDefaultFont());
this.debugBuffer.text(
"(" + this.mouseX + ", " + this.mouseY + ")",
(float) (this.mouseX + w),
(float) (-this.mouseY + h));
"(" + this.mouseX + ", " + this.mouseY + ")", (float) this.mouseX, (float) -this.mouseY);
this.debugBuffer.pushMatrix();
this.debugBuffer.translate(-this.getWidth() / 2, -this.getHeight() / 2);
this.debugBuffer.text("FPS: " + Math.round(applet.frameRate * 100) / 100, 20, 10);
this.debugBuffer.popMatrix();
this.debugBuffer.endDraw();

applet.image(this.debugBuffer, applet.width / 2, applet.height / 2);
applet.image(this.debugBuffer, 0, 0);
}
}
}
1 change: 0 additions & 1 deletion src/org/openpatch/scratch/extensions/hitbox/Hitbox.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ private void drawDebug(PGraphics buffer, double r, double g, double b) {
buffer.stroke((float) r, (float) g, (float) b);
buffer.strokeWeight(2);
buffer.noFill();
buffer.translate(buffer.width / 2, buffer.height / 2);
drawShape(buffer);
buffer.pop();
}
Expand Down
1 change: 0 additions & 1 deletion src/org/openpatch/scratch/extensions/pen/Pen.java
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,6 @@ public void draw() {
Iterator<CopyOnWriteArrayList<Point>> pointsBufferIter = this.pointsBuffer.iterator();

buffer.push();
buffer.translate(this.stage.getWidth() / 2, this.stage.getHeight() / 2);

while (pointsBufferIter.hasNext()) {
CopyOnWriteArrayList<Point> points = pointsBufferIter.next();
Expand Down
10 changes: 5 additions & 5 deletions src/org/openpatch/scratch/extensions/text/Text.java
Original file line number Diff line number Diff line change
Expand Up @@ -267,8 +267,8 @@ private void drawBubble() {
return;
}

this.y = -this.sprite.getY() + applet.height / 2 - this.sprite.getHeight() * 1.1 / 2;
this.x = this.sprite.getX() + applet.width / 2 + this.sprite.getWidth() * 0.9 / 2;
this.y = -this.sprite.getY() - this.sprite.getHeight() * 1.1 / 2;
this.x = this.sprite.getX() + this.sprite.getWidth() * 0.9 / 2;

this.width = maxLineWidth + 16;
this.text = String.join("\n", lines);
Expand All @@ -293,7 +293,7 @@ private void drawBubble() {
}

if (mirror) {
x = (this.sprite.getX() + applet.width / 2 - this.sprite.getWidth() * 0.9 / 2 - this.width);
x = (this.sprite.getX() - this.sprite.getWidth() * 0.9 / 2 - this.width);
if (x < 0) {
x = 0;
}
Expand Down Expand Up @@ -369,7 +369,7 @@ private void drawBox() {

applet.rectMode(PApplet.CORNER);
applet.translate(
(float) (this.x + applet.width / 2), (float) (-this.y + applet.height / 2 - this.height));
(float) this.x, (float) (-this.y - this.height));
applet.stroke(
(float) this.strokeColor.getRed(),
(float) this.strokeColor.getGreen(),
Expand Down Expand Up @@ -403,7 +403,7 @@ private void drawPlain() {
}
this.text = String.join("\n", lines);

applet.translate((float) (this.x + applet.width / 2), (float) (-this.y + applet.height / 2));
applet.translate((float) this.x, (float) -this.y);
applet.fill(
(float) this.textColor.getRed(),
(float) this.textColor.getGreen(),
Expand Down
19 changes: 8 additions & 11 deletions src/org/openpatch/scratch/internal/Applet.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ public Applet(int width, final int height, final String assets) {

this.stages = new ConcurrentHashMap<>();

this.registerMethod("pre", this);
this.registerMethod("mouseEvent", this);
this.registerMethod("keyEvent", this);
if (Applet.instance == null) {
Expand Down Expand Up @@ -153,8 +152,11 @@ public void runSketch() {

public void setup() {
this.windowTitle("Scratch for Java");
this.windowResizable(false);

this.imageMode(PConstants.CENTER);
this.rectMode(PConstants.CENTER);

this.loading = this.loadImage("loading.png");
var loadingScaleX = this.INITIAL_WIDTH / 480.0;
var loadingScaleY =
Expand Down Expand Up @@ -230,12 +232,6 @@ private float loadingStatus() {
return this.numberAssets > 0 ? this.loadedAssets / (float) this.numberAssets : 1;
}

public void pre() {
if (this.hasLoaded && this.stage != null) {
this.stage.pre();
}
}

public void mouseEvent(MouseEvent e) {
if (this.hasLoaded && this.stage != null) {
this.stage.mouseEvent(e);
Expand All @@ -258,18 +254,19 @@ public void draw() {
}
deltaTime = (currentMillis - lastMillis) / 1000.0;
lastMillis = currentMillis;
this.translate(this.width / 2, this.height / 2);
if (!this.hasLoaded || this.loadingStatus() < 1) {
this.background(0x222222);
this.image(this.loading, this.width / 2, this.height / 2);
this.image(this.loading, 0, 0);
this.textAlign(CENTER);
this.stroke(0xf58219);
this.textFont(Font.getDefaultFont());
this.textSize(14);
this.text(this.loadingText, this.width / 2, this.height / 2 + this.loading.height / 2 + 20);
this.text(this.loadingText, 0, this.loading.height / 2 + 20);
this.text(
round(this.loadingStatus() * 100) + "%",
this.width / 2,
this.height / 2 + this.loading.height / 2 + 40);
0,
this.loading.height / 2 + 40);
this.textSize(14);
} else if (this.stage != null) {
this.stage.draw();
Expand Down
Loading

0 comments on commit dbe4c74

Please sign in to comment.