diff --git a/Events-API-Board/.dclignore b/Events-API-Board/.dclignore new file mode 100644 index 000000000..7499ff37a --- /dev/null +++ b/Events-API-Board/.dclignore @@ -0,0 +1,20 @@ +.* +bin/*.map +package-lock.json +yarn-lock.json +build.json +export +tsconfig.json +tslint.json +node_modules +*.ts +*.tsx +.vscode +Dockerfile +dist +README.md +*.blend +*.fbx +*.zip +*.rar +src diff --git a/Events-API-Board/.github/workflows/ci.yml b/Events-API-Board/.github/workflows/ci.yml new file mode 100644 index 000000000..f3ccb9e3f --- /dev/null +++ b/Events-API-Board/.github/workflows/ci.yml @@ -0,0 +1,19 @@ +name: test-build + +on: + push: + pull_request: + +jobs: + lint-and-build: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - name: Use Node.js 18.x + uses: actions/setup-node@v1 + with: + node-version: 18.x + - name: install dependencies + run: npm install + - name: npm run build + run: npm run build diff --git a/Events-API-Board/.gitignore b/Events-API-Board/.gitignore new file mode 100644 index 000000000..24fe526b3 --- /dev/null +++ b/Events-API-Board/.gitignore @@ -0,0 +1,9 @@ +package-lock.json +*.js +node_modules +bin/ +.DS_Store +**/.DS_Store +npm-debug.log* +yarn-debug.log* +yarn-error.log* \ No newline at end of file diff --git a/Events-API-Board/.vscode/extensions.json b/Events-API-Board/.vscode/extensions.json new file mode 100644 index 000000000..38b0d6772 --- /dev/null +++ b/Events-API-Board/.vscode/extensions.json @@ -0,0 +1,3 @@ +{ + "recommendations": ["decentralandfoundation.decentraland-sdk7"] +} diff --git a/Events-API-Board/.vscode/launch.json b/Events-API-Board/.vscode/launch.json new file mode 100644 index 000000000..a3b7699af --- /dev/null +++ b/Events-API-Board/.vscode/launch.json @@ -0,0 +1,17 @@ +{ + // Use the Decentraland Editor extension of VSCode to debug the scene + // in chrome from VSCode + "version": "0.2.0", + "configurations": [ + { + "type": "chrome", + "request": "launch", + "name": "Debug Decentraland in Chrome", + "url": "${command:decentraland-sdk7.commands.getDebugURL}", + "webRoot": "${workspaceFolder}/bin", + "sourceMapPathOverrides": { + "dcl:///*": "${workspaceFolder}/*" + } + } + ] +} diff --git a/Events-API-Board/LICENSE b/Events-API-Board/LICENSE new file mode 100644 index 000000000..b09cd7856 --- /dev/null +++ b/Events-API-Board/LICENSE @@ -0,0 +1,201 @@ +Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] + + 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. diff --git a/Events-API-Board/README.md b/Events-API-Board/README.md new file mode 100644 index 000000000..6bea954aa --- /dev/null +++ b/Events-API-Board/README.md @@ -0,0 +1,71 @@ +# Events API Board + +Call the Decentraland Events API to display events that are currently going on. If there are several active at the same time, it will cycle through up to 5 events. + + + + +This scene shows you: +- How to call the Decentraland Events API +- How to handle async functions that require waiting for a response +- How to parse a JSON response from an API +- How to parse incoming text fields so that they adjust to line width and a maximum length +- How to use a system to shuffle through different in-world UI screens +- How to use a teleport to travel to other locations in Decentraland + +Events that are currently going on have a `live` field = _true_, so it's easy to filter through the returned events to only pick those that are live. + +The board displays the event's title, screenshot, location, and location name (if applicable). The board also works as a teleport when clicked, taking players to the currently displayed event. + +## About the events API: + +Root URL: https://events.decentraland.org/api/events/ + +Events are always ordered by their `start_at` time + +optional params: + +- limit: only show x amount of events +- offset: start showing events from x position onwards +- position: a single event on x position +- estate_id: only events that happen in a given estate +- user: only events created by a given user +- onlyUpcoming: only events that have not started yet + + +## Try it out + +**Install the CLI** + +Download and install the Decentraland CLI by running the following command: + +```bash +npm i -g decentraland +``` + +**Previewing the scene** + + 1. Download this full repository from [sdk7-goerli-plaza](https://github.com/decentraland/sdk7-goerli-plaza/tree/main), including this and several other example scenes on SDK7. + 2. Install the [Decentraland Editor](https://docs.decentraland.org/creator/development-guide/sdk7/editor/) + 3. Open a Visual Studio Code window on this scene's root folder. Not on the root folder of the whole repo, but instead on this sub-folder that belongs to the scene. + 4. Open the Decentraland Editor tab, and press **Run Scene** + +Alternatively, you can use the command line. Inside this scene root directory run: + +```bash + +npm run start + +``` + +**Scene Usage** + +If there are no live events right now, you won't see the board when running the preview. If there are several live events, they will be shuffled through every few seconds, or you can also click on the dots on the bottom of the board to switch through them manually. + + +Learn more about how to build your own scenes in our [documentation](https://docs.decentraland.org/) site. + + +## Copyright info + +This scene is protected with a standard Apache 2 licence. See the terms and conditions in the [LICENSE](/LICENSE) file. \ No newline at end of file diff --git a/Events-API-Board/assets/scene/main.composite b/Events-API-Board/assets/scene/main.composite new file mode 100644 index 000000000..ce07ea3c4 --- /dev/null +++ b/Events-API-Board/assets/scene/main.composite @@ -0,0 +1,253 @@ +{ + "version": 1, + "components": [ + { + "name": "core::Transform", + "jsonSchema": { + "type": "object", + "properties": { + "position": { + "type": "object", + "properties": { + "x": { + "type": "number" + }, + "y": { + "type": "number" + }, + "z": { + "type": "number" + } + } + }, + "scale": { + "type": "object", + "properties": { + "x": { + "type": "number" + }, + "y": { + "type": "number" + }, + "z": { + "type": "number" + } + } + }, + "rotation": { + "type": "object", + "properties": { + "x": { + "type": "number" + }, + "y": { + "type": "number" + }, + "z": { + "type": "number" + }, + "w": { + "type": "number" + } + } + }, + "parent": { + "type": "integer" + } + }, + "serializationType": "transform" + }, + "data": { + "512": { + "json": { + "position": { + "x": 8, + "y": 1, + "z": 8 + }, + "scale": { + "x": 1, + "y": 1, + "z": 1 + }, + "rotation": { + "x": 0, + "y": 0, + "z": 0, + "w": 1 + }, + "parent": 0 + } + } + } + }, + { + "name": "core::MeshRenderer", + "jsonSchema": { + "type": "object", + "properties": {}, + "serializationType": "protocol-buffer", + "protocolBuffer": "PBMeshRenderer" + }, + "data": { + "512": { + "json": { + "mesh": { + "$case": "box", + "box": { + "uvs": [] + } + } + } + } + } + }, + { + "name": "core-schema::Name", + "jsonSchema": { + "type": "object", + "properties": { + "value": { + "type": "string", + "serializationType": "utf8-string" + } + }, + "serializationType": "map" + }, + "data": { + "512": { + "json": { + "value": "Magic Cube" + } + } + } + }, + { + "name": "inspector::Scene", + "jsonSchema": { + "type": "object", + "properties": { + "layout": { + "type": "object", + "properties": { + "base": { + "type": "object", + "properties": { + "x": { + "type": "integer", + "serializationType": "int32" + }, + "y": { + "type": "integer", + "serializationType": "int32" + } + }, + "serializationType": "map" + }, + "parcels": { + "type": "array", + "items": { + "type": "object", + "properties": { + "x": { + "type": "integer", + "serializationType": "int32" + }, + "y": { + "type": "integer", + "serializationType": "int32" + } + }, + "serializationType": "map" + }, + "serializationType": "array" + } + }, + "serializationType": "map" + } + }, + "serializationType": "map" + }, + "data": { + "0": { + "json": { + "layout": { + "base": { + "x": 0, + "y": 0 + }, + "parcels": [ + { + "x": 0, + "y": 0 + } + ] + } + } + } + } + }, + { + "name": "inspector::Nodes", + "jsonSchema": { + "type": "object", + "properties": { + "value": { + "type": "array", + "items": { + "type": "object", + "properties": { + "entity": { + "type": "integer", + "serializationType": "entity" + }, + "children": { + "type": "array", + "items": { + "type": "integer", + "serializationType": "entity" + }, + "serializationType": "array" + } + }, + "serializationType": "map" + }, + "serializationType": "array" + } + }, + "serializationType": "map" + }, + "data": { + "0": { + "json": { + "value": [ + { + "entity": 0, + "children": [ + 512 + ] + }, + { + "entity": 512, + "children": [] + } + ] + } + } + } + }, + { + "name": "cube-id", + "jsonSchema": { + "type": "object", + "properties": {}, + "serializationType": "map" + }, + "data": { + "512": { + "json": {} + } + } + } + ] +} \ No newline at end of file diff --git a/Events-API-Board/images/gray.png b/Events-API-Board/images/gray.png new file mode 100644 index 000000000..8715ae65f Binary files /dev/null and b/Events-API-Board/images/gray.png differ diff --git a/Events-API-Board/images/red.png b/Events-API-Board/images/red.png new file mode 100644 index 000000000..192c5a1c0 Binary files /dev/null and b/Events-API-Board/images/red.png differ diff --git a/Events-API-Board/images/scene-thumbnail.png b/Events-API-Board/images/scene-thumbnail.png new file mode 100644 index 000000000..325f986da Binary files /dev/null and b/Events-API-Board/images/scene-thumbnail.png differ diff --git a/Events-API-Board/images/screenshot.png b/Events-API-Board/images/screenshot.png new file mode 100644 index 000000000..3e1f2438d Binary files /dev/null and b/Events-API-Board/images/screenshot.png differ diff --git a/Events-API-Board/models/events-UI.glb b/Events-API-Board/models/events-UI.glb new file mode 100644 index 000000000..c18f62eff Binary files /dev/null and b/Events-API-Board/models/events-UI.glb differ diff --git a/Events-API-Board/package.json b/Events-API-Board/package.json new file mode 100644 index 000000000..653e2bdf0 --- /dev/null +++ b/Events-API-Board/package.json @@ -0,0 +1,26 @@ +{ + "name": "events-api-board", + "description": "Call the Decentraland Events API to display events that are currently going on.", + "version": "1.0.0", + "devDependencies": { + "@dcl/js-runtime": "next", + "@dcl/sdk": "next" + }, + "engines": { + "node": ">=16.0.0", + "npm": ">=6.0.0" + }, + "prettier": { + "semi": false, + "singleQuote": true, + "printWidth": 120, + "trailingComma": "none" + }, + "scripts": { + "build": "sdk-commands build", + "deploy": "sdk-commands deploy", + "start": "sdk-commands start", + "upgrade-sdk": "npm install --save-dev @dcl/sdk@latest", + "upgrade-sdk:next": "npm install --save-dev @dcl/sdk@next" + } +} diff --git a/Events-API-Board/scene.json b/Events-API-Board/scene.json new file mode 100644 index 000000000..2c0237d8c --- /dev/null +++ b/Events-API-Board/scene.json @@ -0,0 +1,58 @@ +{ + "ecs7": true, + "runtimeVersion": "7", + "display": { + "title": "Events-API-Board", + "description": "Call the Decentraland Events API to display events that are currently going on.", + "navmapThumbnail": "images/scene-thumbnail.png", + "favicon": "favicon_asset" + }, + "owner": "", + "contact": { + "name": "SDK", + "email": "" + }, + "main": "bin/index.js", + "tags": [], + "scene": { + "parcels": [ + "76,7" + ], + "base": "76,7" + }, + "spawnPoints": [ + { + "name": "spawn1", + "default": true, + "position": { + "x": [ + 0, + 3 + ], + "y": [ + 0, + 0 + ], + "z": [ + 0, + 3 + ] + }, + "cameraTarget": { + "x": 8, + "y": 1, + "z": 8 + } + } + ], + "requiredPermissions": [ + "ALLOW_TO_TRIGGER_AVATAR_EMOTE", + "ALLOW_TO_MOVE_PLAYER_INSIDE_SCENE", + "ALLOW_MEDIA_HOSTNAMES" + ], + "allowedMediaHostnames": [ + "events-assets-099ac00.decentraland.org" + ], + "featureToggles": {}, + "name": "Events-API-Board" +} \ No newline at end of file diff --git a/Events-API-Board/src/eventApi.ts b/Events-API-Board/src/eventApi.ts new file mode 100644 index 000000000..e955b9496 --- /dev/null +++ b/Events-API-Board/src/eventApi.ts @@ -0,0 +1,22 @@ +export async function getEvents() { + const events: any[] = [] + const url = 'https://events.decentraland.org/api/events/?limit=5' + + try { + const response = await fetch(url) + const json = await response.json() + + console.log(json) + + for (const event of json.data) { + if (event.live) { + events.push(event) + } + } + + console.log(events) + return events + } catch (error) { + console.log('error getting event data ', error) + } +} diff --git a/Events-API-Board/src/eventBoards.ts b/Events-API-Board/src/eventBoards.ts new file mode 100644 index 000000000..0d15c4a2b --- /dev/null +++ b/Events-API-Board/src/eventBoards.ts @@ -0,0 +1,253 @@ +import { + Entity, + GltfContainer, + InputAction, + Material, + MaterialTransparencyMode, + MeshCollider, + MeshRenderer, + Schemas, + TextShape, + Transform, + TransformType, + engine, + pointerEventsSystem +} from '@dcl/sdk/ecs' +import { getEvents } from './eventApi' +import { Color3, Color4 } from '@dcl/sdk/math' +import { shortenText, splitTextIntoLines } from './helperFunctions' +import { teleportTo } from '~system/RestrictedActions' + +export async function createEventBoard(currentEvent: number, interval: number, transform: Partial) { + // Get event list from API + const events = await getEvents().catch((error) => console.error(error)) + + // If API returns no events return + if (!events || events.length == 0) { + console.log('No events found.') + return + } + + // Board Base + const boardBase = engine.addEntity() + Transform.create(boardBase, transform) + + // Board + const board = engine.addEntity() + GltfContainer.create(board, { + src: 'models/events-UI.glb' + }) + Transform.create(board, { + parent: boardBase, + position: { x: 0, y: 0, z: 0 } + }) + + // Image + const image = engine.addEntity() + MeshRenderer.setPlane(image) + Transform.create(image, { + parent: boardBase, + position: { x: 0, y: 0.4, z: -0.04 }, + scale: { x: 4.25, y: 2.125, z: 1 } + }) + + // Title + const title = engine.addEntity() + Transform.create(title, { + parent: boardBase, + position: { x: -1.9, y: -0.7, z: -0.04 } + }) + TextShape.create(title, { + text: 'Title', + fontSize: 3, + textAlign: 0 // top left + }) + + // coordinates + const coordinates = engine.addEntity() + Transform.create(coordinates, { + parent: boardBase, + position: { x: -1.7, y: -1.44, z: -0.04 } + }) + TextShape.create(coordinates, { + text: '00,00', + fontSize: 2, + textAlign: 0 + }) + + // click pannel + const clickPannel = engine.addEntity() + MeshRenderer.setPlane(clickPannel) + MeshCollider.setPlane(clickPannel) + Transform.create(clickPannel, { + parent: boardBase, + position: { x: 0, y: 0, z: -0.2 }, + scale: { x: 5, y: 4.5, z: 1 } + }) + Material.setPbrMaterial(clickPannel, invisibleMaterial) + + const eventBoard = { + boardBase, + board, + image, + title, + coordinates, + clickPannel, + dots: [], + currentEvent: currentEvent, + interval: interval, + timer: 0 + } + + // Attach the component with IDs to the entity to allow later access by the system + EventBoardComponent.create(boardBase, eventBoard) + + // Remove the existing system first to prevent the + // simultaneous operation of multiple switch systems. + engine.removeSystem('switchEventSystem') + engine.addSystem(switchEventSystem) + + // System + function switchEventSystem(dt: number) { + for (const [entity, component] of engine.getEntitiesWith(EventBoardComponent)) { + const board = EventBoardComponent.getMutable(entity) + board.timer -= dt + if (board.timer < 0) { + board.timer = board.interval + if (events) { + displayEvent(board, events, board.currentEvent) + board.currentEvent += 1 + if (board.currentEvent >= events.length) { + board.currentEvent = 0 + } + } + } + } + } + + return boardBase +} + +export function displayEvent(eventBoard: any, events: any[], currentEvent: number) { + if (events.length <= 0) return + const event = events[currentEvent] + + // Image + setImageTexture(eventBoard.image, event.image) + + // Coordinates + let eventCoords = event.x.toString() + ',' + event.y.toString() + if (event.scene_name) { + eventCoords = shortenText(event.scene_name, 25) + ' ' + eventCoords + } + TextShape.getMutable(eventBoard.coordinates).text = eventCoords + + // Title + TextShape.getMutable(eventBoard.title).text = splitTextIntoLines(event.name, 24, 2) + + // Click panel + pointerEventsSystem.onPointerDown( + { + entity: eventBoard.clickPannel, + opts: { + button: InputAction.IA_POINTER, + hoverText: 'Teleport to event' + } + }, + function () { + teleportTo({ + worldCoordinates: { + x: event.x.toString(), + y: event.y.toString() + } + }) + } + ) + + // Dots + if (eventBoard.dots.length <= 0 && events.length > 1) { + createDots(eventBoard, events.length) + } + + if (events.length > 1) { + for (let i = 0; i < eventBoard.dots.length; i++) { + if (i === currentEvent) { + Material.setPbrMaterial(eventBoard.dots[i], activeEventMaterial) + } else { + Material.setPbrMaterial(eventBoard.dots[i], inactiveEventMaterial) + } + } + } +} + +export function createDots(eventBoard: any, dotAmount: number) { + for (let i = 0; i < dotAmount; i++) { + const offset = (i - dotAmount / 2) * 0.1 + const dot = engine.addEntity() + MeshRenderer.setPlane(dot) + Transform.create(dot, { + parent: eventBoard.boardBase, + position: { x: offset, y: -1.9, z: -0.05 }, + scale: { x: 0.05, y: 0.05, z: 0.05 } + }) + eventBoard.dots.push(dot) + } +} + +// Materials +const invisibleMaterial = { + albedoColor: Color4.create(0, 0, 0, 0) +} + +const inactiveEventMaterial = { + texture: Material.Texture.Common({ + src: 'images/gray.png' + }), + transparencyMode: MaterialTransparencyMode.MTM_ALPHA_BLEND, + roughness: 1.0 +} + +const activeEventMaterial = { + texture: Material.Texture.Common({ + src: 'images/red.png' + }), + transparencyMode: MaterialTransparencyMode.MTM_ALPHA_BLEND, + roughness: 1.0, + specularIntensity: 0, + metallic: 0, + emissiveTexture: Material.Texture.Common({ + src: 'images/red.png' + }), + emissiveIntensity: 1, + emissiveColor: Color3.White() +} + +function setImageTexture(entity: Entity, texture: string) { + Material.setPbrMaterial(entity, { + texture: Material.Texture.Common({ + src: texture + }), + roughness: 1.0, + specularIntensity: 0, + metallic: 0, + emissiveTexture: Material.Texture.Common({ + src: texture + }), + emissiveIntensity: 1, + emissiveColor: Color3.White() + }) +} + +// Custom component +export const EventBoardComponent = engine.defineComponent('eventBoardComponent', { + boardBase: Schemas.Entity, + board: Schemas.Entity, + image: Schemas.Entity, + title: Schemas.Entity, + coordinates: Schemas.Entity, + clickPannel: Schemas.Entity, + dots: Schemas.Array(Schemas.Entity), + currentEvent: Schemas.Int, + interval: Schemas.Number, + timer: Schemas.Number +}) diff --git a/Events-API-Board/src/helperFunctions.ts b/Events-API-Board/src/helperFunctions.ts new file mode 100644 index 000000000..e7720e7e1 --- /dev/null +++ b/Events-API-Board/src/helperFunctions.ts @@ -0,0 +1,38 @@ +export function splitTextIntoLines(text: string, maxLenght: number, maxLines?: number) { + let finalText: string = '' + for (let i = 0; i < text.length; i++) { + const lines = finalText.split('\n') + + if (lines[lines.length - 1].length >= maxLenght && i !== text.length) { + if (finalText[finalText.length - 1] !== ' ') { + if (maxLines && lines.length >= maxLines) { + finalText = finalText.concat('...') + return finalText + } else { + finalText = finalText.concat('-') + } + } + finalText = finalText.concat('\n') + if (text[i] === ' ') { + continue + } + } + + finalText = finalText.concat(text[i]) + } + + return finalText +} + +export function shortenText(text: string, maxLenght: number) { + let finalText: string = '' + + if (text.length > maxLenght) { + finalText = text.substring(0, maxLenght) + finalText = finalText.concat('...') + } else { + finalText = text + } + + return finalText +} diff --git a/Events-API-Board/src/index.ts b/Events-API-Board/src/index.ts new file mode 100644 index 000000000..ee68ec6ab --- /dev/null +++ b/Events-API-Board/src/index.ts @@ -0,0 +1,19 @@ +import { Quaternion } from '@dcl/sdk/math' +import { createEventBoard } from './eventBoards' + +export async function main() { + const board_1 = createEventBoard(0, 4, { + position: { x: 9.5, y: 3, z: 9.3 }, + rotation: Quaternion.fromEulerDegrees(0, -120, 0) + }) + + const board_2 = createEventBoard(1, 4, { + position: { x: 6.5, y: 3, z: 9.3 }, + rotation: Quaternion.fromEulerDegrees(0, 120, 0) + }) + + const board_3 = createEventBoard(2, 4, { + position: { x: 8, y: 3, z: 6.7 }, + rotation: Quaternion.fromEulerDegrees(0, 0, 0) + }) +} diff --git a/Events-API-Board/tsconfig.json b/Events-API-Board/tsconfig.json new file mode 100644 index 000000000..d84fa280f --- /dev/null +++ b/Events-API-Board/tsconfig.json @@ -0,0 +1,11 @@ +{ + "compilerOptions": { + "allowJs": true, + "strict": true + }, + "include": [ + "src/**/*.ts", + "src/**/*.tsx" + ], + "extends": "@dcl/sdk/types/tsconfig.ecs7.json" +} \ No newline at end of file diff --git a/dcl-workspace.json b/dcl-workspace.json index 30a030938..8d83ef6be 100644 --- a/dcl-workspace.json +++ b/dcl-workspace.json @@ -183,6 +183,9 @@ { "path": "Gnark" }, + { + "path": "Events-API-Board" + }, { "path": "Dance-floor" }, diff --git a/package.json b/package.json index 636cef68d..75e888bbd 100644 --- a/package.json +++ b/package.json @@ -35,6 +35,7 @@ "BouncerUI", "Cube", "Dance-floor", + "Events-API-Board", "Gnark", "Hummingbirds", "Hypno-wheels",