Skip to content

Commit

Permalink
Merge PR #2306 (and #2273) by @viveret & @msteiger - UI animation system
Browse files Browse the repository at this point in the history
  • Loading branch information
Cervator committed May 13, 2016
2 parents dbe0cf8 + ff340ab commit bc992dc
Show file tree
Hide file tree
Showing 49 changed files with 1,778 additions and 227 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
/*
* Copyright 2016 MovingBlocks
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.terasology.rendering.animation;

import org.junit.Assert;
import org.junit.Test;
import org.mockito.Mockito;

/**
* Tests the {@link Animation} class
*/
public class AnimationTest {

private static final float EPS = 0.001f;

private Container c = new Container();

@Test
public void startEndOnce() {
Animation ani = once();
AnimationListener listener = Mockito.mock(AnimationListener.class);
ani.addListener(listener);
ani.start();
ani.update(2.5f);
Mockito.verify(listener, Mockito.times(1)).onStart();
Mockito.verify(listener, Mockito.times(1)).onEnd();
}

@Test
public void startEndValuesOnce() {
Animation ani = once().start();
Assert.assertEquals(0.0f, c.value, 0.0f);
ani.update(2.5f);
Assert.assertEquals(1.0f, c.value, 0.0f);
}

@Test
public void startEndValuesInfinite() {
Animation ani = infinite().start();
Assert.assertEquals(0.0f, c.value, 0.0f);
ani.update(2.5f);
Assert.assertEquals(.25f, c.value, EPS); // (2.5 % 2) / 2
}

@Test
public void overflowInfinite() {
Animation ani = infinite().start();
Assert.assertEquals(0.0f, c.value, 0.0f);
ani.update(112.5f);
Assert.assertEquals(.25f, c.value, EPS); // (112.5 % 2) / 2
}

@Test
public void updates() {
Animation ani = once();
ani.update(2.5f); // ignored
Assert.assertEquals(0f, c.value, 0f);
ani.start();
ani.update(0.5f);
Assert.assertEquals(0.25f, c.value, EPS); // 0.5 / 2
ani.pause();
ani.update(0.5f); // ignored
Assert.assertEquals(0.25f, c.value, EPS); // same
ani.resume();
ani.update(1.0f);
Assert.assertEquals(0.75f, c.value, EPS); // 1.5 / 2
ani.update(1.0f);
Assert.assertEquals(1.00f, c.value, 0f); // 2.5 / 2 -> capped
ani.update(1.0f); // ignored
Assert.assertEquals(1.00f, c.value, 0f); // same
}

@Test
public void startEndOnceReverse() {
Animation ani = once().setReverseMode();
AnimationListener listener = Mockito.mock(AnimationListener.class);
ani.addListener(listener);
ani.start();
ani.update(2.5f);
Mockito.verify(listener, Mockito.times(1)).onStart();
Mockito.verify(listener, Mockito.times(1)).onEnd();
}

@Test
public void updatesReverse() {
Animation ani = once().setReverseMode();
ani.update(2.5f); // ignored
Assert.assertEquals(0f, c.value, 0f);
ani.start();
ani.update(0.5f);
Assert.assertEquals(0.75f, c.value, EPS); // 1 - 0.5 / 2
ani.pause();
ani.update(0.5f); // ignored
Assert.assertEquals(0.75f, c.value, EPS); // same
ani.resume();
ani.update(1.0f);
Assert.assertEquals(0.25f, c.value, EPS); // 1 - 1.5 / 2
ani.update(1.0f);
Assert.assertEquals(0.00f, c.value, 0f); // 1 - 2.5 / 2 -> capped
ani.update(1.0f); // ignored
Assert.assertEquals(0.00f, c.value, 0f); // same
}

private Animation once() {
return Animation.once(v -> c.value = v, 2f, TimeModifiers.linear());
}

private Animation infinite() {
return Animation.infinite(v -> c.value = v, 2f, TimeModifiers.linear());
}

private static class Container {
float value;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public class RenderingConfig {
private int windowWidth;
private int windowHeight;
private boolean fullscreen;
private boolean animatedMenu;
private ViewDistance viewDistance;
private boolean flickeringLight;
private boolean animateGrass;
Expand Down Expand Up @@ -138,6 +139,14 @@ public void setFullscreen(boolean fullscreen) {
this.fullscreen = fullscreen;
}

public boolean isAnimatedMenu() {
return animatedMenu;
}

public void setAnimatedMenu(boolean animatedMenu) {
this.animatedMenu = animatedMenu;
}

public ViewDistance getViewDistance() {
return viewDistance;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import org.terasology.logic.console.suggesters.ScreenSuggester;
import org.terasology.utilities.Assets;
import org.terasology.assets.ResourceUrn;
import org.terasology.assets.management.AssetManager;
import org.terasology.config.Config;
import org.terasology.engine.GameEngine;
Expand Down Expand Up @@ -74,6 +75,7 @@
import java.util.List;
import java.util.Locale;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.Callable;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;
Expand Down Expand Up @@ -213,19 +215,20 @@ public String showScreen(@CommandParam(value="uri", suggester = ScreenSuggester.

@Command(shortDescription = "Reloads a ui screen")
public String reloadScreen(@CommandParam("ui") String ui) {
Optional<UIElement> uiData = assetManager.getAsset(ui, UIElement.class);
if (uiData.isPresent()) {
boolean wasOpen = nuiManager.isOpen(uiData.get().getUrn());
Set<ResourceUrn> urns = assetManager.resolve(ui, UIElement.class);
if (urns.size() == 1) {
ResourceUrn urn = urns.iterator().next();
boolean wasOpen = nuiManager.isOpen(urn);
if (wasOpen) {
nuiManager.closeScreen(uiData.get().getUrn());
nuiManager.closeScreen(urn);
}

if (wasOpen) {
nuiManager.pushScreen(uiData.get());
nuiManager.pushScreen(urn);
}
return "Success";
} else {
return "Unable to resolve ui '" + ui + "'";
return "No unique resource found";
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
import org.terasology.rendering.nui.BaseInteractionListener;
import org.terasology.rendering.nui.CoreScreenLayer;
import org.terasology.rendering.nui.InteractionListener;
import org.terasology.rendering.nui.animation.MenuAnimationSystems;
import org.terasology.rendering.nui.animation.SwipeMenuAnimationSystem;
import org.terasology.rendering.nui.animation.SwipeMenuAnimationSystem.Direction;
import org.terasology.rendering.nui.databinding.ReadOnlyBinding;
import org.terasology.rendering.nui.events.NUIMouseClickEvent;
import org.terasology.rendering.nui.layouts.ScrollableArea;
Expand Down Expand Up @@ -57,6 +60,9 @@ public boolean onMouseClick(NUIMouseClickEvent event) {

@Override
public void initialise() {

setAnimationSystem(new SwipeMenuAnimationSystem(0.2f, Direction.TOP_TO_BOTTOM));

final ScrollableArea scrollArea = find("scrollArea", ScrollableArea.class);
scrollArea.moveToBottom();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ public boolean canBeFocus() {
}

@Override
public boolean isEscapeToCloseAllowed() {
protected boolean isEscapeToCloseAllowed() {
return false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public class DebugProperties extends CoreHudWidget {
private PropertyLayout propertyLayout;

@Override
protected void initialise() {
public void initialise() {
container = find("container", ColumnLayout.class);
propertyLayout = find("properties", PropertyLayout.class);
}
Expand Down
Loading

0 comments on commit bc992dc

Please sign in to comment.