Skip to content

Commit

Permalink
prepare zoom!
Browse files Browse the repository at this point in the history
  • Loading branch information
svencc committed Feb 1, 2024
1 parent 29ab571 commit 54e09e1
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 45 deletions.
4 changes: 1 addition & 3 deletions TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

# 1
* new InputComponent interacting with MapComponent
* (map) zooming
* (map) zooming (<<<<<<<--------------------------------)
* add sortable physics modifier components
* NULLImplementations -> Optionals ...
*
Expand All @@ -12,8 +12,6 @@
* add pre-click-event (preclick, click, doublecklic) events
*
* UI/MAP:
* panning
* zooming (scaling)
* add scale bar
* add measurement tool
* add menu
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public void update(final long elapsedNanoTime) {
physicsCoreComponent.setPositionX(newPositionX);
physicsCoreComponent.setPositionY(newPositionY);

final double restoringForce = physicsCoreComponent.getRestoringForce();
final double restoringForce = physicsCoreComponent.getFrictionForce();
if (restoringForce != 0) {
double newVelocityX = applyRestoringForce(physicsCoreComponent.getVelocityXComponent(), restoringForce);
double newVelocityY = applyRestoringForce(physicsCoreComponent.getVelocityYComponent(), restoringForce);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class PhysicCoreComponent extends ComponentTemplate {

private double dragInNewton = 1.0;

private double restoringForce = 0;
private double frictionForce = 0;


public PhysicCoreComponent() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ private void startMachines() {
mouseButtonEventFSMs.forEach(IsMouseButtonEventFSM::start);
}


@Override
@SuppressWarnings("unchecked")
public boolean mapEvents(@NonNull final Stream<NanoTimedEvent<? extends InputEvent>> timedMouseEventStream) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public RECOMMapEntity getRECOMMapEntity(@NonNull final RECOMMapComponent recomMa
final RECOMMapEntity recomMapEntity = new RECOMMapEntity();

final PhysicCoreComponent component = new PhysicCoreComponent();
component.setRestoringForce(100);
component.setFrictionForce(100);

recomMapEntity.addComponent(recomMapComponent);
recomMapEntity.addComponent(new RECOMMapInputComponent());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ public class RECOMMapComponent extends RenderableComponent implements AutoClosea
@Nullable
private ReactiveObserver<HeightMapDescriptorDto> mapTopographyDataReactiveObserver;

private int pixelFactor = 1;


public RECOMMapComponent(
@NonNull final MapsOverviewService mapsOverviewService,
Expand All @@ -52,7 +54,6 @@ public RECOMMapComponent(
this.setZIndex(0);
}


@PostConstruct
public void init() {
mapOverviewReactiveObserver = ReactiveObserver.reactWith(onReloadMapOverviewReaction());
Expand Down Expand Up @@ -115,4 +116,26 @@ public void close() {
}
}

public void zoomIn() {
if (pixelFactor == 1) {
return;
} else {
pixelFactor--;
swapBuffer();
}
}

public void zoomOut() {
pixelFactor++;
swapBuffer();
}

private void swapBuffer() {
// @TODO <<<<---------------------------------------------------------------------------------------------------
// swap buffer this.pixelBuffer = new PixelBuffer(dimension, pixelBufferArray);

// create LinkedList<PixelBuffer> pixelBufferList
// init first element with actual buffer!
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,60 +15,85 @@
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.Optional;

@Slf4j
@Component
public class RECOMMapInputComponent extends InputComponent {

private static final float velocityX = 140f;
private static final float MOVING_FORCE = 140f;
private static final int DRAG_SPEED_COEFFICIENT = 4;

@NonNull
private Optional<RECOMMapComponent> maybeMapComponent = Optional.empty();


@Override
public void handleInputCommand(@NonNull final IsCommand<?> inputCommand) {
switch (inputCommand) {
case KeyboardCommand keyboardCommand -> {
this.getEntity().<PhysicCoreComponent>locateComponent(ComponentType.PhysicsCoreComponent).ifPresent(physicsCoreComponent -> {
if (keyboardCommand.getNanoTimedEvent().getEvent().getCode().equals(KeyCode.LEFT)) {
physicsCoreComponent.addVelocityXComponent(velocityX);
} else if (keyboardCommand.getNanoTimedEvent().getEvent().getCode().equals(KeyCode.RIGHT)) {
physicsCoreComponent.addVelocityXComponent(-velocityX);
} else if (keyboardCommand.getNanoTimedEvent().getEvent().getCode().equals(KeyCode.UP)) {
physicsCoreComponent.addVelocityYComponent(velocityX);
} else if (keyboardCommand.getNanoTimedEvent().getEvent().getCode().equals(KeyCode.DOWN)) {
physicsCoreComponent.addVelocityYComponent(-velocityX);
}
});
case KeyboardCommand keyboardCommand -> handleKeyboardCommand(keyboardCommand);
case ScrollCommand scrollCommand -> handleScrollCommand(scrollCommand);
case MouseDragCommand mouseDragCommand -> handleMouseDragCommand(inputCommand, mouseDragCommand);
default -> logInputCommand(inputCommand);
}
}

private void handleKeyboardCommand(@NonNull final KeyboardCommand keyboardCommand) {
this.getEntity().<PhysicCoreComponent>locateComponent(ComponentType.PhysicsCoreComponent).ifPresent(physicsCoreComponent -> {
if (keyboardCommand.getNanoTimedEvent().getEvent().getCode().equals(KeyCode.LEFT)) {
physicsCoreComponent.addVelocityXComponent(MOVING_FORCE);
} else if (keyboardCommand.getNanoTimedEvent().getEvent().getCode().equals(KeyCode.RIGHT)) {
physicsCoreComponent.addVelocityXComponent(-MOVING_FORCE);
} else if (keyboardCommand.getNanoTimedEvent().getEvent().getCode().equals(KeyCode.UP)) {
physicsCoreComponent.addVelocityYComponent(MOVING_FORCE);
} else if (keyboardCommand.getNanoTimedEvent().getEvent().getCode().equals(KeyCode.DOWN)) {
physicsCoreComponent.addVelocityYComponent(-MOVING_FORCE);
}
case ScrollCommand scrollCommand -> {
// log.info("ScrollCommand received: {} ({})", inputCommand.getClass().getSimpleName(), mapToScrollDirection(scrollCommand.getNanoTimedEvent().getEvent()));
// code to zoom in/out
this.getEntity().<PhysicCoreComponent>locateComponent(ComponentType.PhysicsCoreComponent).ifPresent(physicsCoreComponent -> {
if (scrollCommand.getNanoTimedEvent().getEvent().getDeltaY() > 0) {
// todo
} else if (scrollCommand.getNanoTimedEvent().getEvent().getDeltaY() < 0) {
// todo
}
});
});
}

private void handleScrollCommand(@NonNull final ScrollCommand scrollCommand) {
this.getEntity().<PhysicCoreComponent>locateComponent(ComponentType.PhysicsCoreComponent).ifPresent(physicsCoreComponent -> {
if (scrollCommand.getNanoTimedEvent().getEvent().getDeltaY() > 0) {
locateRecomMapComponent().ifPresent(RECOMMapComponent::zoomIn);
} else if (scrollCommand.getNanoTimedEvent().getEvent().getDeltaY() < 0) {
locateRecomMapComponent().ifPresent(RECOMMapComponent::zoomOut);
}
case MouseDragCommand mouseDragCommand -> {
if (mouseDragCommand.getMouseButton().equals(MouseButton.SECONDARY)) {
this.getEntity().<PhysicCoreComponent>locateComponent(ComponentType.PhysicsCoreComponent).ifPresent(physicsCoreComponent -> {
if (mouseDragCommand.isInOriginPosition()) {
physicsCoreComponent.setVelocityXComponent(0);
physicsCoreComponent.setVelocityYComponent(0);
} else {
physicsCoreComponent.setVelocityXComponent(-1 * mouseDragCommand.getDistanceX() * DRAG_SPEED_COEFFICIENT);
physicsCoreComponent.setVelocityYComponent(-1 * mouseDragCommand.getDistanceY() * DRAG_SPEED_COEFFICIENT);
}
});
});
}

private void handleMouseDragCommand(
@NonNull final IsCommand<?> inputCommand,
@NonNull final MouseDragCommand mouseDragCommand
) {
if (mouseDragCommand.getMouseButton().equals(MouseButton.SECONDARY)) {
this.getEntity().<PhysicCoreComponent>locateComponent(ComponentType.PhysicsCoreComponent).ifPresent(physicsCoreComponent -> {
if (mouseDragCommand.isInOriginPosition()) {
physicsCoreComponent.setVelocityXComponent(0);
physicsCoreComponent.setVelocityYComponent(0);
} else {
log.info("MouseDragCommand received: {} ({})", inputCommand.getClass().getSimpleName(), mouseDragCommand.getMouseButton());
physicsCoreComponent.setVelocityXComponent(-1 * mouseDragCommand.getDistanceX() * DRAG_SPEED_COEFFICIENT);
physicsCoreComponent.setVelocityYComponent(-1 * mouseDragCommand.getDistanceY() * DRAG_SPEED_COEFFICIENT);
}
}
default -> logInputCommand(inputCommand);
});
} else {
log.info("MouseDragCommand received: {} ({})", inputCommand.getClass().getSimpleName(), mouseDragCommand.getMouseButton());
}
}

// @TODO; kann man das generalisieren?
@NonNull
private Optional<RECOMMapComponent> locateRecomMapComponent() {
if (maybeMapComponent.isEmpty()) {
maybeMapComponent = this.getEntity().locateComponents(ComponentType.RenderableComponent).stream()
.filter(component -> component instanceof RECOMMapComponent)
.map(component -> (RECOMMapComponent) component)
.findFirst();
}

return maybeMapComponent;
}

private void logInputCommand(@NonNull final IsCommand<?> inputCommand) {
switch (inputCommand) {
case MouseButtonCommand mouseButtonCommand ->
Expand Down

0 comments on commit 54e09e1

Please sign in to comment.