-
-
Notifications
You must be signed in to change notification settings - Fork 93
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This issue caused unexpected behavior when scrolling via mouse wheel and locked the camera to the boundaries of the map without the possibility to scroll to a different place via mousewheel. This was mainly caused due to a missing flag which caused the implementation to reset the focus multiple times during an update operation. With this change, the scrolling logic has also been moved to a dedicated class with an abstraction layer to the actual Swing components. This allows us to write tests for this part of the editor and makes the code better readable.
- Loading branch information
1 parent
97b736f
commit 66b4051
Showing
4 changed files
with
176 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
package de.gurkenlabs.utiliti.handlers; | ||
|
||
import de.gurkenlabs.litiengine.Game; | ||
import de.gurkenlabs.utiliti.components.Editor; | ||
|
||
import java.awt.geom.Point2D; | ||
import java.util.EventListener; | ||
|
||
public final class Scroll { | ||
private static final int SCROLL_MAX = 100; | ||
private static final int SCROLL_SPEED = 50; | ||
|
||
private static ScrollHandler verticalHandler; | ||
private static ScrollHandler horizontalHandler; | ||
|
||
private static float currentScrollSize; | ||
private static float currentScrollSpeed; | ||
|
||
private static boolean updating; | ||
|
||
private Scroll() { | ||
} | ||
|
||
public static void up() { | ||
final Point2D currentFocus = Game.world().camera().getFocus(); | ||
scroll(currentFocus.getX(), currentFocus.getY() - currentScrollSpeed); | ||
} | ||
|
||
public static void down() { | ||
final Point2D currentFocus = Game.world().camera().getFocus(); | ||
scroll(currentFocus.getX(), currentFocus.getY() + currentScrollSpeed); | ||
} | ||
|
||
public static void left() { | ||
final Point2D currentFocus = Game.world().camera().getFocus(); | ||
scroll(currentFocus.getX() - currentScrollSpeed, currentFocus.getY()); | ||
} | ||
|
||
public static void right() { | ||
final Point2D currentFocus = Game.world().camera().getFocus(); | ||
scroll(currentFocus.getX() + currentScrollSpeed, currentFocus.getY()); | ||
} | ||
|
||
public static void scroll(double x, double y) { | ||
if (Editor.instance().getMapComponent().isLoading()) { | ||
return; | ||
} | ||
|
||
Game.world().camera().setFocus(x, y); | ||
} | ||
|
||
public static ScrollHandler getVerticalHandler() { | ||
return verticalHandler; | ||
} | ||
|
||
public static ScrollHandler getHorizontalHandler() { | ||
return horizontalHandler; | ||
} | ||
|
||
public static void init(ScrollHandler vertical, ScrollHandler horizontal) { | ||
verticalHandler = vertical; | ||
horizontalHandler = horizontal; | ||
|
||
getVerticalHandler().onScrolled((handler) -> { | ||
final double y = getScrollValue(handler, Game.world().environment().getMap().getSizeInPixels().height); | ||
|
||
scroll(Game.world().camera().getFocus().getX(), y); | ||
}); | ||
|
||
getHorizontalHandler().onScrolled((handler) -> { | ||
final double x = getScrollValue(handler, Game.world().environment().getMap().getSizeInPixels().width); | ||
|
||
scroll(x, Game.world().camera().getFocus().getY()); | ||
}); | ||
|
||
Game.world().camera().onZoom(e -> updateScrollHandlers()); | ||
|
||
Game.world().camera().onFocus(e -> updateScrollHandlers()); | ||
|
||
Game.world().onLoaded(e -> updateScrollHandlers()); | ||
} | ||
|
||
private static void updateScrollHandlers() { | ||
if (Game.world().environment() == null || updating) { | ||
return; | ||
} | ||
|
||
updating = true; | ||
|
||
try { | ||
double relativeX = Game.world().camera().getFocus().getX() / Game.world().environment().getMap().getSizeInPixels().width; | ||
double relativeY = Game.world().camera().getFocus().getY() / Game.world().environment().getMap().getSizeInPixels().height; | ||
|
||
// decouple the scrollbar from the environment | ||
currentScrollSize = Math.round(SCROLL_MAX * Math.sqrt(Game.world().camera().getRenderScale())); | ||
currentScrollSpeed = SCROLL_SPEED / Game.world().camera().getZoom(); | ||
|
||
getHorizontalHandler().setMinimum(0); | ||
getHorizontalHandler().setMaximum((int) currentScrollSize); | ||
getVerticalHandler().setMinimum(0); | ||
getVerticalHandler().setMaximum((int) currentScrollSize); | ||
|
||
int valueX = (int) (relativeX * currentScrollSize); | ||
int valueY = (int) (relativeY * currentScrollSize); | ||
|
||
getHorizontalHandler().setValue(valueX); | ||
getVerticalHandler().setValue(valueY); | ||
} finally { | ||
updating = false; | ||
} | ||
} | ||
|
||
private static double getScrollValue(ScrollHandler scrollHandler, double actualSize) { | ||
double currentValue = scrollHandler.getValue() / currentScrollSize; | ||
return currentValue * actualSize; | ||
} | ||
|
||
public interface ScrollHandler { | ||
int getMinimum(); | ||
|
||
int getMaximum(); | ||
|
||
int getValue(); | ||
|
||
void setMinimum(int min); | ||
|
||
void setMaximum(int max); | ||
|
||
void setValue(int value); | ||
|
||
void onScrolled(ScrollHandlerEventListener listener); | ||
} | ||
|
||
public interface ScrollHandlerEventListener extends EventListener { | ||
void scrolled(ScrollHandler handler); | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
utiliti/src/de/gurkenlabs/utiliti/swing/ScrollHandlerBar.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package de.gurkenlabs.utiliti.swing; | ||
|
||
import de.gurkenlabs.utiliti.handlers.Scroll; | ||
|
||
import javax.swing.*; | ||
import java.util.List; | ||
import java.util.concurrent.CopyOnWriteArrayList; | ||
|
||
public class ScrollHandlerBar extends JScrollBar implements Scroll.ScrollHandler { | ||
private final List<Scroll.ScrollHandlerEventListener> listeners; | ||
|
||
public ScrollHandlerBar(int orientation) { | ||
super(orientation); | ||
|
||
this.listeners = new CopyOnWriteArrayList<>(); | ||
|
||
this.setDoubleBuffered(true); | ||
this.addAdjustmentListener(e -> { | ||
for (Scroll.ScrollHandlerEventListener listener : listeners) { | ||
listener.scrolled(this); | ||
} | ||
}); | ||
} | ||
|
||
@Override | ||
public void onScrolled(Scroll.ScrollHandlerEventListener listener) { | ||
this.listeners.add(listener); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters