Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generified revisions for the sake of extensibility #38

Merged
merged 2 commits into from
Aug 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package dev.enjarai.trickster.revision;

import dev.enjarai.trickster.spell.Pattern;
import dev.enjarai.trickster.spell.SpellPart;

public class CreateInnerCircleRevision implements Revision {
@Override
public Pattern pattern() {
return Pattern.of(0, 4, 8, 5);
}

@Override
public SpellPart apply(RevisionContext ctx, SpellPart root, SpellPart drawingPart) {
drawingPart.glyph = new SpellPart();
return root;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package dev.enjarai.trickster.revision;

import dev.enjarai.trickster.spell.Pattern;
import dev.enjarai.trickster.spell.SpellPart;

public class CreateOuterSubcircleRevision implements Revision {
@Override
public Pattern pattern() {
return Pattern.of(6, 3, 0, 4, 8);
}

@Override
public SpellPart apply(RevisionContext ctx, SpellPart root, SpellPart drawingPart) {
if (drawingPart != root) {
if (root.glyph == drawingPart) {
root.subParts.add(new SpellPart());
} else {
drawingPart.setSubPartInTree(current -> {
current.subParts.add(new SpellPart());
return current;
}, root, true);
}
}

return root;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package dev.enjarai.trickster.revision;

import dev.enjarai.trickster.spell.Pattern;
import dev.enjarai.trickster.spell.SpellPart;

public class CreateSubcircleRevision implements Revision {
@Override
public Pattern pattern() {
return Pattern.of(0, 4, 8, 7);
}

@Override
public SpellPart apply(RevisionContext ctx, SpellPart root, SpellPart drawingPart) {
drawingPart.subParts.add(new SpellPart());
return root;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package dev.enjarai.trickster.revision;

import dev.enjarai.trickster.spell.Pattern;
import dev.enjarai.trickster.spell.SpellPart;

public class DeleteBranchRevision implements Revision {
@Override
public Pattern pattern() {
return Pattern.of(0, 4, 8, 5, 2, 1, 0, 3, 6, 7, 8);
}

@Override
public SpellPart apply(RevisionContext ctx, SpellPart root, SpellPart drawingPart) {
if (drawingPart == root)
return new SpellPart();

drawingPart.setSubPartInTree(current -> null, root, false);
return root;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package dev.enjarai.trickster.revision;

import dev.enjarai.trickster.spell.Pattern;
import dev.enjarai.trickster.spell.SpellPart;

public class DeleteCircleRevision implements Revision {
@Override
public Pattern pattern() {
return Pattern.of(0, 4, 8);
}

@Override
public SpellPart apply(RevisionContext ctx, SpellPart root, SpellPart drawingPart) {
var firstSubpart = drawingPart.getSubParts().stream().findFirst();

if (drawingPart == root)
return firstSubpart.orElse(new SpellPart());

drawingPart.setSubPartInTree(current -> firstSubpart.orElse(null), root, false);
return root;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package dev.enjarai.trickster.revision;

import dev.enjarai.trickster.spell.Pattern;
import dev.enjarai.trickster.spell.SpellPart;

public class ExecuteOffHandRevision implements Revision {
@Override
public Pattern pattern() {
return Pattern.of(4, 3, 0, 4, 5, 2, 4, 1);
}

@Override
public SpellPart apply(RevisionContext ctx, SpellPart root, SpellPart drawingPart) {
//TODO: make this independent of SpellPartWidget
ctx.executeOffhand();
return root;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package dev.enjarai.trickster.revision;

import dev.enjarai.trickster.spell.Pattern;
import dev.enjarai.trickster.spell.SpellPart;

public class InsertParentCircleRevision implements Revision {
@Override
public Pattern pattern() {
return Pattern.of(3, 0, 4, 8);
}

@Override
public SpellPart apply(RevisionContext ctx, SpellPart root, SpellPart drawingPart) {
var newPart = new SpellPart();
newPart.subParts.add(drawingPart);

if (drawingPart == root)
return newPart;

drawingPart.setSubPartInTree(current -> newPart, root, false);
return root;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package dev.enjarai.trickster.revision;

import dev.enjarai.trickster.spell.Pattern;
import dev.enjarai.trickster.spell.SpellPart;

public class IntoInnerCircleRevision implements Revision {
@Override
public Pattern pattern() {
return Pattern.of(1, 0, 4, 8);
}

@Override
public SpellPart apply(RevisionContext ctx, SpellPart root, SpellPart drawingPart) {
var newPart = new SpellPart();
newPart.glyph = drawingPart;

if (drawingPart == root)
return newPart;

drawingPart.setSubPartInTree(current -> newPart, root, false);
return root;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package dev.enjarai.trickster.revision;

import dev.enjarai.trickster.spell.Pattern;
import dev.enjarai.trickster.spell.SpellPart;

public class ReplaceCircleWithCrowMindRevision implements Revision {
@Override
public Pattern pattern() {
return Pattern.of(4, 1, 2, 4, 3, 6, 8, 5);
}

@Override
public SpellPart apply(RevisionContext ctx, SpellPart root, SpellPart drawingPart) {
return root; //TODO
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package dev.enjarai.trickster.revision;

import dev.enjarai.trickster.spell.Pattern;
import dev.enjarai.trickster.spell.SpellPart;

public class ReplaceCircleWithOffHandRevision implements Revision {
@Override
public Pattern pattern() {
return Pattern.of(4, 0, 1, 4, 2, 1);
}

@Override
public SpellPart apply(RevisionContext ctx, SpellPart root, SpellPart drawingPart) {
var offhand = ctx.getOtherHandSpell().deepClone();

if (drawingPart == root)
return offhand;

drawingPart.setSubPartInTree(current -> offhand, root, false);
return root;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package dev.enjarai.trickster.revision;

import dev.enjarai.trickster.spell.Pattern;
import dev.enjarai.trickster.spell.SpellPart;

public class ReplaceGlyphWithOffHandRevision implements Revision {
@Override
public Pattern pattern() {
return Pattern.of(1, 2, 4, 1, 0, 4, 7);
}

@Override
public SpellPart apply(RevisionContext ctx, SpellPart root, SpellPart drawingPart) {
drawingPart.glyph = ctx.getOtherHandSpell().deepClone();
return root;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package dev.enjarai.trickster.revision;

import dev.enjarai.trickster.spell.Pattern;
import dev.enjarai.trickster.spell.SpellPart;

public class ReplaceOuterCircleRevision implements Revision {
@Override
public Pattern pattern() {
return Pattern.of(1, 2, 4, 6);
}

@Override
public SpellPart apply(RevisionContext ctx, SpellPart root, SpellPart drawingPart) {
if (drawingPart != root) {
if (root.glyph == drawingPart)
return drawingPart;

drawingPart.setSubPartInTree(current -> drawingPart, root, true);
}

return root;
}
}
20 changes: 20 additions & 0 deletions src/client/java/dev/enjarai/trickster/revision/Revision.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package dev.enjarai.trickster.revision;

import dev.enjarai.trickster.spell.Pattern;
import dev.enjarai.trickster.spell.SpellPart;

public interface Revision {
/**
* @return The pattern that triggers this revision.
*/
Pattern pattern();

/**
* Applies this revision over the root spell part, replacing the root with this function's return.
* @param ctx The context of the editor.
* @param root The current root.
* @param drawingPart The spell part that is being drawn in.
* @return The new root spell part.
*/
SpellPart apply(RevisionContext ctx, SpellPart root, SpellPart drawingPart);
}
38 changes: 38 additions & 0 deletions src/client/java/dev/enjarai/trickster/revision/Revisions.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package dev.enjarai.trickster.revision;

import dev.enjarai.trickster.spell.Pattern;

import java.util.*;

public class Revisions {
private static final Map<Pattern, Revision> REGISTRY = new HashMap<>();

public static final Revision CREATE_SUBCIRCLE = register(new CreateSubcircleRevision());
public static final Revision CREATE_INNER_CIRCLE = register(new CreateInnerCircleRevision());
public static final Revision INSERT_PARENT_CIRCLE = register(new InsertParentCircleRevision());
public static final Revision INTO_INNER_CIRCLE = register(new IntoInnerCircleRevision());
public static final Revision REPLACE_OUTER_CIRCLE = register(new ReplaceOuterCircleRevision());
public static final Revision CREATE_OUTER_SUBCIRCLE = register(new CreateOuterSubcircleRevision());
public static final Revision DELETE_CIRCLE = register(new DeleteCircleRevision());
public static final Revision DELETE_BRANCH = register(new DeleteBranchRevision());
public static final Revision SWAP_SUBCIRCLE = register(new SwapSubcircleRevision());
public static final Revision SHIFT_SUBCIRCLE_BACKWARDS = register(new ShiftSubcircleBackwardsRevision());
public static final Revision SHIFT_SUBCIRCLE_FORWARDS = register(new ShiftSubcircleForwardsRevision());
public static final Revision REPLACE_CIRCLE_WITH_OFF_HAND = register(new ReplaceCircleWithOffHandRevision());
public static final Revision REPLACE_GLYPH_WITH_OFF_HAND = register(new ReplaceGlyphWithOffHandRevision());
public static final Revision REPLACE_CIRCLE_WITH_CROW_MIND = register(new ReplaceCircleWithCrowMindRevision());
public static final Revision EXECUTE_OFF_HAND = register(new ExecuteOffHandRevision());
public static final Revision WRITE_ADDRESS_TO_OFF_HAND = register(new WriteAddressToOffHandRevision());

public static Optional<Revision> lookup(Pattern pattern) {
return Optional.ofNullable(REGISTRY.get(pattern));
}

public static Revision register(Revision revision) {
if (REGISTRY.put(revision.pattern(), revision) != null) {
//TODO: add override warning
}

return revision;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package dev.enjarai.trickster.revision;

import dev.enjarai.trickster.spell.Pattern;
import dev.enjarai.trickster.spell.SpellPart;

public class ShiftSubcircleBackwardsRevision implements Revision {
@Override
public Pattern pattern() {
return Pattern.of(1, 0, 3);
}

@Override
public SpellPart apply(RevisionContext ctx, SpellPart root, SpellPart drawingPart) {
if (!drawingPart.subParts.isEmpty())
drawingPart.subParts.add(drawingPart.subParts.removeFirst());

return root;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package dev.enjarai.trickster.revision;

import dev.enjarai.trickster.spell.Pattern;
import dev.enjarai.trickster.spell.SpellPart;

public class ShiftSubcircleForwardsRevision implements Revision {
@Override
public Pattern pattern() {
return Pattern.of(1, 2, 5);
}

@Override
public SpellPart apply(RevisionContext ctx, SpellPart root, SpellPart drawingPart) {
if (!drawingPart.subParts.isEmpty())
drawingPart.subParts.addFirst(drawingPart.subParts.removeLast());

return root;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package dev.enjarai.trickster.revision;

import dev.enjarai.trickster.spell.Pattern;
import dev.enjarai.trickster.spell.SpellPart;

public class SwapSubcircleRevision implements Revision {
@Override
public Pattern pattern() {
return Pattern.of(2, 4, 3);
}

@Override
public SpellPart apply(RevisionContext ctx, SpellPart root, SpellPart drawingPart) {
if (drawingPart.subParts.size() > 1)
drawingPart.subParts.addFirst(drawingPart.subParts.remove(1));

return root;
}
}
Loading