Skip to content

Commit

Permalink
time defined tasks and pre paint tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
chompa111 committed Apr 24, 2023
1 parent a0793a2 commit f889508
Show file tree
Hide file tree
Showing 12 changed files with 103 additions and 101 deletions.
5 changes: 0 additions & 5 deletions src/main/java/graphical/basics/behavior/Behavior.java

This file was deleted.

32 changes: 0 additions & 32 deletions src/main/java/graphical/basics/behavior/FollowBehavior.java

This file was deleted.

5 changes: 0 additions & 5 deletions src/main/java/graphical/basics/gobject/AnimPart.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package graphical.basics.gobject;

import graphical.basics.BackGround;
import graphical.basics.behavior.Behavior;
import graphical.basics.gobject.struct.Gobject;
import graphical.basics.presentation.Presentation;
import graphical.basics.presentation.PresentationConfig;
Expand All @@ -22,10 +21,6 @@ public void addBefore(Gobject referential, Gobject gobject) {
Presentation.staticReference.addBefore(referential, gobject);
}

public void add(Behavior behavior) {
Presentation.staticReference.add(behavior);
}

public void remove(Gobject gobject) {
Presentation.staticReference.remove(gobject);
}
Expand Down
54 changes: 24 additions & 30 deletions src/main/java/graphical/basics/gobject/struct/Gobject.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
package graphical.basics.gobject.struct;


import graphical.basics.gobject.Group;
import graphical.basics.presentation.Positioning;
import graphical.basics.presentation.Presentation;
import graphical.basics.ColorHolder;
import graphical.basics.behavior.Behavior;
import graphical.basics.behavior.FollowBehavior;
import graphical.basics.gobject.Group;
import graphical.basics.location.Location;
import graphical.basics.location.LocationPair;
import graphical.basics.location.SupplierPoint;
import graphical.basics.presentation.Positioning;
import graphical.basics.presentation.Presentation;
import graphical.basics.task.Task;
import graphical.basics.task.TimeDefinedTask;
import graphical.basics.task.transformation.gobject.ColorTranform;
import graphical.basics.task.transformation.gobject.MorfTransform;
import graphical.basics.task.transformation.gobject.PositionTransform;
Expand All @@ -22,6 +21,7 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.Consumer;

import static graphical.basics.presentation.Positioning.Reference.*;

Expand All @@ -32,8 +32,6 @@ public abstract class Gobject {
public NumberHolder scale = new DoubleHolder(1);

public void paint(Graphics g, boolean b) {


var midpoint = getBorders().midPoint();
var g2d = (Graphics2D) g;
var oldT = (AffineTransform) g2d.getTransform().clone();
Expand Down Expand Up @@ -64,16 +62,16 @@ public void paint(Graphics g, boolean b) {

public abstract List<Location> getReferenceLocations();

public Task transform(Gobject gobject2, int steps) {
public TimeDefinedTask transform(Gobject gobject2, int steps) {
return new MorfTransform(this, gobject2, steps);
}

public Task transform(Gobject gobject2) {
public TimeDefinedTask transform(Gobject gobject2) {
return transform(gobject2, Presentation.staticReference.seconds(1));
}


public Task changeColor(Color color, int steps) {
public TimeDefinedTask changeColor(Color color, int steps) {
return new ColorTranform(this, color, steps);
}

Expand All @@ -83,28 +81,28 @@ public void setColor(Color color) {
}
}

public Task changeColor(Color color) {
public TimeDefinedTask changeColor(Color color) {
return changeColor(color, Presentation.staticReference.seconds(1));
}


public Task move(double x, double y, int steps) {
public TimeDefinedTask move(double x, double y, int steps) {
return new PositionTransform(this, x, y, steps);
}

public Task move(double x, double y) {
public TimeDefinedTask move(double x, double y) {
return move(x, y, Presentation.staticReference.seconds(1));
}

public Task moveTo(Location location, int steps) {
public TimeDefinedTask moveTo(Location location, int steps) {
var mylocation = getBorders().midPoint();
var diffx = location.getX() - mylocation.getX();
var diffy = location.getY() - mylocation.getY();

return this.move(diffx, diffy, steps);
}

public Task moveTo(Location location) {
public TimeDefinedTask moveTo(Location location) {
return this.moveTo(location, Presentation.staticReference.seconds(1));
}

Expand All @@ -123,21 +121,6 @@ public void setPositionTo(Location location) {
changeSetPosition(diffx, diffy);
}

public Behavior asSubtitle(Gobject gobject, Positioning.Reference reference) {
//por enquanto
switch (reference) {
case BOTTOM:
Positioning.align(gobject, this, BOTTOM);
return new FollowBehavior(gobject, new SupplierPoint(() -> Positioning.getPositionX(this, CENTER), () -> Positioning.getPositionY(this, BOTTOM)));
case RIGHT:
Positioning.align(gobject, this, LEFT);
return new FollowBehavior(gobject, new SupplierPoint(() -> Positioning.getPositionX(this, LEFT), () -> Positioning.getPositionY(this, CENTER)));
}

return new FollowBehavior(gobject, new SupplierPoint(() -> this.getBorders().midPoint().getX(), () -> this.getBorders().getL2().getY()));

}

public AffineTransform getTranformation() {

var midpoint = getBorders().midPoint();
Expand Down Expand Up @@ -226,4 +209,15 @@ public Group and(Gobject... gobjects) {
return new Group(result);
}

public Runnable addTask(Runnable task) {
return Presentation.staticReference.addTask(task);
}

public <T> Runnable addTask(T metadata, Consumer<T> task) {
return Presentation.staticReference.addTask(metadata, task);
}

public void removeTask(Runnable r) {
Presentation.staticReference.removeTask(r);
}
}
6 changes: 6 additions & 0 deletions src/main/java/graphical/basics/presentation/Animation.java
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,12 @@ public static Task wooble(Gobject gobject) {
.andThen(gobject.getAngle().change(0.5, presentation.seconds(0.5))));
}

public static Task wooble2(Gobject gobject) {
return gobject.getAngle().change(0.5).forSeconds(0.5)
.andThen(gobject.getAngle().change(-1.0).forSeconds(0.5)
.andThen(gobject.getAngle().change(0.5).forSeconds(0.5)));
}


public static Task t3b1b(Gobject a, Gobject b, int steps) {
return T3b1b.t3b1b(a, b, steps);
Expand Down
43 changes: 24 additions & 19 deletions src/main/java/graphical/basics/presentation/Presentation.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import codec.engine.JavaGraphicEngine;
import codec.engine.JavaNativeEngine;
import graphical.basics.BackGround;
import graphical.basics.behavior.Behavior;
import graphical.basics.gobject.Camera;
import graphical.basics.gobject.struct.Gobject;
import graphical.basics.location.Location;
Expand All @@ -24,6 +23,7 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.Consumer;

import static javax.swing.WindowConstants.EXIT_ON_CLOSE;

Expand All @@ -48,7 +48,7 @@ public abstract class Presentation {
JavaGraphicEngine graphicEngine;

List<Gobject> gobjects = new ArrayList<>();
List<Behavior> behaviors = new ArrayList<>();
private final List<Runnable> prePaintTasks = new ArrayList<>();

//FpsControler fpsControler = new FpsControler();

Expand Down Expand Up @@ -151,13 +151,13 @@ public void paint(Graphics g) {
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.setVisible(true);
frame.setTitle(" Saturn-preview ");

try {
var icn = ImageIO.read(new File("C:\\Users\\PICHAU\\Desktop\\saticon2.png"));
frame.setIconImage(icn);
} catch (IOException e) {
e.printStackTrace();
}
//
// try {
// var icn = ImageIO.read(new File("C:\\Users\\PICHAU\\Desktop\\saticon2.png"));
// frame.setIconImage(icn);
// } catch (IOException e) {
// e.printStackTrace();
// }
} else {
isDisablePreview = true;
}
Expand Down Expand Up @@ -203,7 +203,7 @@ public void joinBackGroundTasks() {


public void processFrame() {
runBehaviors();
prePaintTasks.forEach(Runnable::run);
frameCounter++;
paintComponent(bufferedGraphics);
if (!isDisablePreview)
Expand Down Expand Up @@ -236,11 +236,6 @@ public void paintComponent(Graphics g) {
g2d.setTransform(oldT);
}

public void runBehaviors() {
for (Behavior behavior : behaviors) {
behavior.update();
}
}

public void add(Gobject gobject) {
gobjects.add(gobject);
Expand All @@ -254,10 +249,6 @@ public void addBefore(Gobject referential, Gobject gobject) {
gobjects.add(gobjects.indexOf(referential), gobject);
}

public void add(Behavior behavior) {
behaviors.add(behavior);
}

public void remove(Gobject gobject) {
gobjects.remove(gobject);
}
Expand Down Expand Up @@ -354,4 +345,18 @@ public PresentationConfig getPresentationConfig() {
public List<Gobject> getGobjects() {
return gobjects;
}

public Runnable addTask(Runnable task) {
prePaintTasks.add(task);

return task;
}

public <T> Runnable addTask(T metadata, Consumer<T> task) {
return addTask(() -> task.accept(metadata));
}

public void removeTask(Runnable r) {
prePaintTasks.remove(r);
}
}
11 changes: 11 additions & 0 deletions src/main/java/graphical/basics/task/TimeDefinedTask.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package graphical.basics.task;

import graphical.basics.presentation.Presentation;

public interface TimeDefinedTask extends Task{
default Task forSeconds(double seconds){
return forFrames(Presentation.staticReference.seconds(seconds));
}

Task forFrames(int frames);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
import graphical.basics.ColorHolder;
import graphical.basics.gobject.struct.Gobject;
import graphical.basics.task.Task;
import graphical.basics.task.TimeDefinedTask;

import java.awt.*;
import java.util.List;

public class ColorTranform implements Task {
public class ColorTranform implements TimeDefinedTask {

int steps;
int stepCount;
Expand Down Expand Up @@ -85,4 +86,10 @@ public void step() {
public boolean isDone() {
return steps == stepCount;
}

@Override
public Task forFrames(int frames) {
steps = frames;
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@
import graphical.basics.gobject.struct.Gobject;
import graphical.basics.gobject.GobjectFrame;
import graphical.basics.task.Task;
import graphical.basics.task.TimeDefinedTask;

import java.awt.*;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

public class MorfTransform implements Task {
public class MorfTransform implements TimeDefinedTask {
Gobject g1;
Gobject g2;

Expand Down Expand Up @@ -50,7 +51,7 @@ public void setup() {
gf2 = new GobjectFrame(g2);
refIndex = Presentation.staticReference.getObjectIndex(g1);
Presentation.staticReference.remove(g1);
Presentation.staticReference.add(gf1,refIndex);
Presentation.staticReference.add(gf1, refIndex);
// Presentation.staticReference.add(g2);

colorHolders1 = new ArrayList<>();
Expand Down Expand Up @@ -188,7 +189,7 @@ public void step() {
// }

Presentation.staticReference.remove(gf1);
Presentation.staticReference.add(g2,refIndex);
Presentation.staticReference.add(g2, refIndex);
}


Expand Down Expand Up @@ -227,4 +228,9 @@ public boolean isDone() {
}


@Override
public Task forFrames(int frames) {
this.steps = frames;
return this;
}
}
Loading

0 comments on commit f889508

Please sign in to comment.