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

feat(gestaltv7-eventsystem): Migration Event and @ReceiveEvent to gestalt's #36

Merged
merged 5 commits into from
Dec 7, 2021
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
17 changes: 2 additions & 15 deletions src/main/java/org/terasology/books/DefaultDocumentData.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,5 @@
/*
* Copyright 2017 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.
*/
// Copyright 2021 The Terasology Foundation
// SPDX-License-Identifier: Apache-2.0
package org.terasology.books;

import org.terasology.engine.rendering.nui.widgets.browser.data.DocumentData;
Expand Down
6 changes: 4 additions & 2 deletions src/main/java/org/terasology/books/RecipeParagraph.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ public class RecipeParagraph implements ParagraphData, ParagraphRenderable {
* @param itemResult The result of the recipe, if it is an item. If it is a block, this should be left empty.
* @param resultCount The amount of blocks/items produced by this recipe.
*/
public RecipeParagraph(int blockIngredients, List<Block> blockIngredientsList, List<Prefab> itemIngredients, Block blockResult, Prefab itemResult, int resultCount) {
public RecipeParagraph(int blockIngredients, List<Block> blockIngredientsList, List<Prefab> itemIngredients, Block blockResult,
Prefab itemResult, int resultCount) {
ingredientIcons = new ItemIcon[blockIngredients];
for (int i = 0; i < ingredientIcons.length; i++) {
ItemIcon itemIcon = new ItemIcon();
Expand Down Expand Up @@ -124,7 +125,8 @@ private void initializeForBlock(ItemIcon itemIcon, Block blockIngredient) {
* The ingredients are drawn in a strip from left to right, followed by the result.
*/
@Override
public void renderContents(Canvas canvas, Vector2i startPos, ContainerRenderSpace containerRenderSpace, int leftIndent, int rightIndent, ParagraphRenderStyle defaultStyle, HorizontalAlign horizontalAlign, HyperlinkRegister hyperlinkRegister) {
public void renderContents(Canvas canvas, Vector2i startPos, ContainerRenderSpace containerRenderSpace, int leftIndent, int rightIndent,
ParagraphRenderStyle defaultStyle, HorizontalAlign horizontalAlign, HyperlinkRegister hyperlinkRegister) {
int ingredientsCount = ingredientIcons.length;
int drawingWidth =
ingredientsCount * iconSize + (ingredientsCount - 1) * ingredientSpacing + resultSpacing + iconSize;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
import java.util.List;

/**
* Used in a prefab to represent a recipe. The prefab can then be inserted in a book using a <recipe> tag inside a page as such:
* <br>
* <code><recipe module:prefab></code>
* Used in a prefab to represent a recipe. The prefab can then be inserted in a book using a <pre>{@code <recipe>}</pre>
* tag inside a page as such: <br>
* <pre>{@code <recipe module:prefab>}</pre>
*/
public class BookRecipeComponent implements Component<BookRecipeComponent> {
/**
Expand Down
27 changes: 7 additions & 20 deletions src/main/java/org/terasology/books/logic/BookcaseSystem.java
Original file line number Diff line number Diff line change
@@ -1,31 +1,18 @@
/*
* Copyright 2017 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.
*/
// Copyright 2020 The Terasology Foundation
// SPDX-License-Identifier: Apache-2.0
package org.terasology.books.logic;

import org.joml.Vector3f;
import org.terasology.engine.entitySystem.entity.EntityRef;
import org.terasology.engine.entitySystem.event.ReceiveEvent;
import org.terasology.engine.entitySystem.systems.BaseComponentSystem;
import org.terasology.engine.entitySystem.systems.RegisterMode;
import org.terasology.engine.entitySystem.systems.RegisterSystem;
import org.terasology.module.inventory.components.InventoryComponent;
import org.terasology.module.inventory.events.BeforeItemPutInInventory;
import org.terasology.engine.logic.inventory.events.DropItemEvent;
import org.terasology.engine.logic.location.LocationComponent;
import org.terasology.engine.world.block.entity.CreateBlockDropsEvent;
import org.terasology.gestalt.entitysystem.event.ReceiveEvent;
import org.terasology.module.inventory.components.InventoryComponent;
import org.terasology.module.inventory.events.BeforeItemPutInInventory;

import java.util.List;

Expand All @@ -44,7 +31,7 @@ public class BookcaseSystem extends BaseComponentSystem {
* @param event the triggering event (somebody tried to put something into the bookcase)
* @param entity the object that is being submitted as an potential entry to the bookcase's inventory
*/
@ReceiveEvent(components = {BookcaseComponent.class})
@ReceiveEvent(components = BookcaseComponent.class)
public void filterBook(BeforeItemPutInInventory event, EntityRef entity) {
if (!event.getItem().hasComponent(BookComponent.class)) {
event.consume();
Expand All @@ -57,7 +44,7 @@ public void filterBook(BeforeItemPutInInventory event, EntityRef entity) {
* @param event the triggering event (something caused block drops - the bookcase was destroyed)
* @param entity the bookcase's entity, has the target inventory to go through
*/
@ReceiveEvent(components = {BookcaseComponent.class})
@ReceiveEvent(components = BookcaseComponent.class)
public void onDestroyBookCase(CreateBlockDropsEvent event, EntityRef entity, LocationComponent location, InventoryComponent inventory) {
Vector3f pos = location.getWorldPosition(new Vector3f());
List<EntityRef> items = inventory.itemSlots;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// Copyright 2020 The Terasology Foundation
// SPDX-License-Identifier: Apache-2.0

package org.terasology.books.rendering.nui.layers;

import com.google.common.base.Joiner;
Expand All @@ -11,13 +10,10 @@
import org.terasology.books.logic.BookComponent;
import org.terasology.books.logic.BookRecipeComponent;
import org.terasology.books.logic.EditBooksComponent;
import org.terasology.engine.entitySystem.entity.EntityManager;
import org.terasology.engine.entitySystem.entity.EntityRef;
import org.terasology.engine.entitySystem.prefab.Prefab;
import org.terasology.engine.entitySystem.prefab.PrefabManager;
import org.terasology.engine.logic.clipboard.ClipboardManager;
import org.terasology.engine.logic.common.DisplayNameComponent;
import org.terasology.module.inventory.systems.InventoryUtils;
import org.terasology.engine.logic.players.LocalPlayer;
import org.terasology.engine.registry.In;
import org.terasology.engine.rendering.nui.BaseInteractionScreen;
Expand All @@ -26,11 +22,9 @@
import org.terasology.engine.rendering.nui.widgets.browser.data.ParagraphData;
import org.terasology.engine.rendering.nui.widgets.browser.data.basic.HTMLLikeParser;
import org.terasology.engine.rendering.nui.widgets.browser.ui.BrowserWidget;
import org.terasology.engine.rendering.nui.widgets.browser.ui.style.ParagraphRenderStyle;
import org.terasology.engine.utilities.Assets;
import org.terasology.engine.world.block.BlockManager;
import org.terasology.module.inventory.systems.InventoryUtils;
import org.terasology.nui.Color;
import org.terasology.nui.HorizontalAlign;
import org.terasology.nui.UITextureRegion;
import org.terasology.nui.WidgetUtil;
import org.terasology.nui.databinding.Binding;
Expand All @@ -43,46 +37,18 @@
import java.util.Collection;
import java.util.List;

/**
* A simple enumeration containing different states the book can be in.
*/
enum State {
CLOSED_LEFT,
OPEN_LEFT,
PAGES,
OPEN_RIGHT,
CLOSED_RIGHT
}

/**
* A Screen class that displays a book. The book is optionally editable, has pages which can be switched and there is a title.
*/
public class BookScreen extends BaseInteractionScreen {
private static final String STATUS_EDITING = "Editing";
private static final String STATUS_READING = "Reading";
private static final String STATUS_READ_ONLY = "Read-only";

private static final Logger logger = LoggerFactory.getLogger(BookScreen.class);

/* Local List of pages that the bookComponent contains */
static List<String> pages;
/* Boolean to see which edit button was clicked (left or right) */
static boolean leftPageEditing = true;
/* Index for finding which page number is opened */
static Binding<Integer> index;

@In
private static PrefabManager prefabManager;
@In
private static BlockManager blockManager;
@In
private NUIManager nuiManager;
@In
private LocalPlayer localPlayer;
@In
private ClipboardManager clipboardManager;
@In
private EntityManager entityManager;
private static final Logger logger = LoggerFactory.getLogger(BookScreen.class);

private static BookComponent book;
private static EntityRef bookEntity;
Expand All @@ -95,13 +61,32 @@ public class BookScreen extends BaseInteractionScreen {
private static UIButton arrowForward;
private static UIButton arrowBackward;
private static UILabel title;
private static Binding<UITextureRegion> coverBackL = new DefaultBinding<>(Assets.getTextureRegion("Books:book#interiorLeft").get());
private static Binding<UITextureRegion> coverBackR = new DefaultBinding<>(Assets.getTextureRegion("Books:book#interiorRight").get());
private static Binding<UITextureRegion> coverFrontL = new DefaultBinding<>(Assets.getTextureRegion("Books:book#exteriorLeft").get());
private static Binding<UITextureRegion> coverFrontR = new DefaultBinding<>(Assets.getTextureRegion("Books:book#exteriorRight").get());
private static Binding<UITextureRegion> pageL = new DefaultBinding<>(Assets.getTextureRegion("Books:book#pageLeft").get());
private static Binding<UITextureRegion> pageR = new DefaultBinding<>(Assets.getTextureRegion("Books:book#pageRight").get());
private static Binding<UITextureRegion> blank = new DefaultBinding<>(Assets.getTextureRegion("Books:blank").get());
private static final Binding<UITextureRegion> COVER_BACK_L = new DefaultBinding<>(Assets.getTextureRegion(
"Books:book#interiorLeft").get());
private static final Binding<UITextureRegion> COVER_BACK_R = new DefaultBinding<>(Assets.getTextureRegion(
"Books:book#interiorRight").get());
private static final Binding<UITextureRegion> COVER_FRONT_L = new DefaultBinding<>(Assets.getTextureRegion(
"Books:book#exteriorLeft").get());
private static final Binding<UITextureRegion> COVER_FRONT_R = new DefaultBinding<>(Assets.getTextureRegion(
"Books:book#exteriorRight").get());
private static final Binding<UITextureRegion> PAGE_L = new DefaultBinding<>(Assets.getTextureRegion(
"Books:book#pageLeft").get());
private static final Binding<UITextureRegion> PAGE_R = new DefaultBinding<>(Assets.getTextureRegion(
"Books:book#pageRight").get());
private static final Binding<UITextureRegion> BLANK = new DefaultBinding<>(Assets.getTextureRegion(
"Books:blank").get());

private static final String STATUS_EDITING = "Editing";
private static final String STATUS_READING = "Reading";
private static final String STATUS_READ_ONLY = "Read-only";


@In
private static PrefabManager prefabManager;
@In
private NUIManager nuiManager;
@In
private LocalPlayer localPlayer;

private String status;
private UIButton save;
Expand All @@ -112,12 +97,6 @@ public class BookScreen extends BaseInteractionScreen {
private UIButton deleteRight;
private UIButton addPage;
private UILabel statusText;
private ParagraphRenderStyle centerRenderStyle = new ParagraphRenderStyle() {
@Override
public HorizontalAlign getHorizontalAlignment() {
return HorizontalAlign.CENTER;
}
};

public BookScreen() {
}
Expand All @@ -136,33 +115,36 @@ private static DocumentData createDocument(String text) {
*/
private static Collection<ParagraphData> createParagraphs(String text) {
Collection<ParagraphData> paragraphs = new ArrayList<ParagraphData>();
while (text.length() > 0) {
if (text.contains("<recipe")) {
int i = text.indexOf("<recipe");
paragraphs.add(createTextParagraph(text.substring(0, i)));
text = text.substring(i);
i = text.indexOf(">");
String inputText = text;
while (inputText.length() > 0) {
if (inputText.contains("<recipe")) {
int i = inputText.indexOf("<recipe");
paragraphs.add(createTextParagraph(inputText.substring(0, i)));
inputText = inputText.substring(i);
i = inputText.indexOf(">");
// Capture the text following the "<recipe" tag and remove spaces
String recipePrefabName = text.substring("<recipe".length(), i).replaceAll("\\s","");
String recipePrefabName = inputText.substring("<recipe".length(), i).replaceAll("\\s", "");
paragraphs.add(createRecipeParagraph(recipePrefabName));
text = text.substring(i + 1);
inputText = inputText.substring(i + 1);
} else {
paragraphs.add(createTextParagraph(text));
text = "";
paragraphs.add(createTextParagraph(inputText));
inputText = "";
}
}
return paragraphs;
}

private static ParagraphData createTextParagraph(String text) {
return HTMLLikeParser.parseHTMLLikeParagraph(null, "<c " + "198"/*Color.BLACK.getRepresentation()*/ + ">" + text.replace("\n", "<l>") + "</c>");
return HTMLLikeParser.parseHTMLLikeParagraph(null,
"<c 198>" + text.replace("\n", "<l>") + "</c>");
}

private static RecipeParagraph createRecipeParagraph(String prefabName) {
Prefab recipePrefab = prefabManager.getPrefab(prefabName);
BookRecipeComponent bookRecipeComponent = recipePrefab.getComponent(BookRecipeComponent.class);
return new RecipeParagraph(bookRecipeComponent.blockIngredients, bookRecipeComponent.blockIngredientsList, bookRecipeComponent.itemIngredients,
bookRecipeComponent.blockResult, bookRecipeComponent.itemResult, bookRecipeComponent.resultCount);
return new RecipeParagraph(bookRecipeComponent.blockIngredients, bookRecipeComponent.blockIngredientsList,
bookRecipeComponent.itemIngredients, bookRecipeComponent.blockResult,
bookRecipeComponent.itemResult, bookRecipeComponent.resultCount);
}

static State getState() {
Expand Down Expand Up @@ -206,8 +188,8 @@ static String getTextRight() {
}

static void updatePage() {
pageLeft.bindTexture(blank);
pageRight.bindTexture(blank);
pageLeft.bindTexture(BLANK);
pageRight.bindTexture(BLANK);
arrowForward.setVisible(true);
arrowBackward.setVisible(true);
title.setText("");
Expand All @@ -216,8 +198,8 @@ static void updatePage() {
textRight.navigateTo(createDocument(getTextRight()));

if (getState().equals(State.CLOSED_RIGHT)) {
coverRight.bindTexture(coverFrontR);
coverLeft.bindTexture(blank);
coverRight.bindTexture(COVER_FRONT_R);
coverLeft.bindTexture(BLANK);
arrowBackward.setVisible(false);
if (book.title != null) {
title.setText(book.title);
Expand All @@ -234,20 +216,20 @@ static void updatePage() {
}
}
} else if (getState().equals(State.CLOSED_LEFT)) {
coverRight.bindTexture(blank);
coverLeft.bindTexture(coverFrontL);
coverRight.bindTexture(BLANK);
coverLeft.bindTexture(COVER_FRONT_L);
arrowForward.setVisible(false);
} else {
coverLeft.bindTexture(coverBackL);
coverRight.bindTexture(coverBackR);
coverLeft.bindTexture(COVER_BACK_L);
coverRight.bindTexture(COVER_BACK_R);

if (getState().equals(State.OPEN_RIGHT)) {
pageRight.bindTexture(pageR);
pageRight.bindTexture(PAGE_R);
} else if (getState().equals(State.OPEN_LEFT)) {
pageLeft.bindTexture(pageL);
pageLeft.bindTexture(PAGE_L);
} else {
pageLeft.bindTexture(pageL);
pageRight.bindTexture(pageR);
pageLeft.bindTexture(PAGE_L);
pageRight.bindTexture(PAGE_R);
}
}
}
Expand Down Expand Up @@ -511,4 +493,15 @@ private void backward() {
index.set(index.get() - 1);
}
}

/**
* A simple enumeration containing different states the book can be in.
*/
enum State {
CLOSED_LEFT,
OPEN_LEFT,
PAGES,
OPEN_RIGHT,
CLOSED_RIGHT
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ public void initialise() {
BookScreen.updatePage();
nuiManager.closeScreen(this);
} else {
if (BookScreen.getState().equals(State.OPEN_RIGHT)) {
if (BookScreen.getState().equals(BookScreen.State.OPEN_RIGHT)) {
BookScreen.pages.set(BookScreen.index.get(), pageText.getText());
} else if (BookScreen.getState().equals(State.PAGES)) {
} else if (BookScreen.getState().equals(BookScreen.State.PAGES)) {
BookScreen.pages.set(BookScreen.index.get() + 1, pageText.getText());
}
BookScreen.updatePage();
Expand Down