Skip to content

Commit

Permalink
Allow defining center of rotation for geometry.
Browse files Browse the repository at this point in the history
  • Loading branch information
danielperano committed Apr 1, 2024
1 parent e41652d commit c258895
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
14 changes: 11 additions & 3 deletions Obsidian/src/main/java/myworld/obsidian/display/Renderer.java
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ public void render(Canvas canvas, Bounds2D componentBounds, String styleDebugNam

Move position = style.rule(StyleRules.POSITION, renderVars);
Rotate rotation = style.rule(StyleRules.ROTATION, renderVars);
Move rotationCenter = style.rule(StyleRules.ROTATION_CENTER, renderVars);
Skew skew = style.rule(StyleRules.SKEW, renderVars);

Matrix33 transform = Matrix33.IDENTITY;
Expand All @@ -151,9 +152,16 @@ public void render(Canvas canvas, Bounds2D componentBounds, String styleDebugNam
}

if (rotation != null) {
transform = Matrix33.makeRotate(-rotation.angle(),
new Point(componentBounds.left() + componentBounds.width() / 2f,
componentBounds.top() + componentBounds.height() / 2f))
Point center;
if(rotationCenter != null){
center = new Point(rotationCenter.x().toPixels(componentBounds.width()) - 0.5f,
rotationCenter.y().toPixels(componentBounds.height()) - 0.5f);
}else{
// If rotation center is not defined, use the drawing's center.
center = new Point(componentBounds.left() + componentBounds.width() / 2f,
componentBounds.top() + componentBounds.height() / 2f);
}
transform = Matrix33.makeRotate(-rotation.angle(), center)
.makeConcat(transform);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ public class StyleRules {
public static final String COLOR = "color";
public static final String POSITION = "position";
public static final String ROTATION = "rotation";
public static final String ROTATION_CENTER = "rotation-center";
public static final String SKEW = "skew";

public static final String OVERFLOW_MODE = "overflow-mode";
Expand Down

0 comments on commit c258895

Please sign in to comment.