Skip to content

Commit

Permalink
Merge branch 'main' into release-0.26
Browse files Browse the repository at this point in the history
  • Loading branch information
MihailRis committed Jan 20, 2025
2 parents 8c88484 + 92f226c commit 7997e9a
Show file tree
Hide file tree
Showing 24 changed files with 416 additions and 36 deletions.
11 changes: 10 additions & 1 deletion doc/en/scripting/builtins/libworld.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,11 @@ world.is_night() -> bool
world.count_chunks() -> int

-- Returns the compressed chunk data to send.
-- If the chunk is not loaded, returns the saved data.
-- Currently includes:
-- 1. Voxel data (id and state)
-- 2. Voxel metadata (fields)
world.get_chunk_data(x: int, z: int) -> Bytearray
world.get_chunk_data(x: int, z: int) -> Bytearray or nil

-- Modifies the chunk based on the compressed data.
-- Returns true if the chunk exists.
Expand All @@ -61,4 +62,12 @@ world.set_chunk_data(
-- compressed chunk data
data: Bytearray
) -> bool

-- Saves chunk data to region.
-- Changes will be written to file only on world save.
world.save_chunk_data(
x: int, z: int,
-- compressed chunk data
data: Bytearray
)
```
19 changes: 19 additions & 0 deletions doc/en/scripting/ui.md
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,25 @@ Properties:
| ----- | ------ | ---- | ----- | ------------ |
| src | string | yes | yes | texture name |

## Canvas

Properties:

| Title | Type | Read | Write | Description |
|-------|--------| ---- |-------|-------------|
| data | Canvas | yes | no | canvas data |

Methods:

| Method | Description |
|----------------------------------------------------------|---------------------------------------------------------|
| data:at(x: int, y: int) | returns an RGBA pixel at the given coordinates |
| data:set(x: int, y: int, rgba: int) | updates an RGBA pixel at the given coordinates |
| data:set(x: int, y: int, r: int, g: int, b: int) | updates an RGBA pixel at the given coordinates |
| data:set(x: int, y: int, r: int, g: int, b: int, a: int) | updates an RGBA pixel at the given coordinates |
| data:update() | applies changes to the canvas and uploads it to the GPU |


## Inventory

Properties:
Expand Down
6 changes: 5 additions & 1 deletion doc/en/xml-ui-layouts.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,11 @@ Inner text is a button text.
- `src` - name of an image stored in textures folder. Extension is not specified. Type: string.
Example: *gui/error*

## *textbox*
## *canvas*

- _No additional attributes_

## *textbox*

Inner text - initially entered text

Expand Down
11 changes: 10 additions & 1 deletion doc/ru/scripting/builtins/libworld.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,11 @@ world.is_night() -> bool
world.count_chunks() -> int

-- Возвращает сжатые данные чанка для отправки.
-- Если чанк не загружен, возвращает сохранённые данные.
-- На данный момент включает:
-- 1. Данные вокселей (id и состояние)
-- 2. Метаданные (поля) вокселей
world.get_chunk_data(x: int, z: int) -> Bytearray
world.get_chunk_data(x: int, z: int) -> Bytearray или nil

-- Изменяет чанк на основе сжатых данных.
-- Возвращает true если чанк существует.
Expand All @@ -60,4 +61,12 @@ world.set_chunk_data(
-- сжатые данные чанка
data: Bytearray
) -> bool

-- Сохраняет данные чанка в регион.
-- Изменения будет записаны в файл только после сохранения мира.
world.save_chunk_data(
x: int, z: int,
-- сжатые данные чанка
data: Bytearray
)
```
19 changes: 19 additions & 0 deletions doc/ru/scripting/ui.md
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,25 @@ document["worlds-panel"]:clear()
| -------- | ------ | ------ | ------ | --------------------- |
| src | string | да | да | отображаемая текстура |


## Холст (canvas)

Свойства:

| Название | Тип | Чтение | Запись | Описание |
|----------|--------| ------ |--------|----------------|
| data | Canvas | да | нет | пиксели холста |

Методы:

| Метод | Описание |
|----------------------------------------------------------|-----------------------------------------------------|
| data:at(x: int, y: int) | возвращает RGBA пиксель по указанным координатам |
| data:set(x: int, y: int, rgba: int) | изменяет RGBA пиксель по указанным координатам |
| data:set(x: int, y: int, r: int, g: int, b: int) | изменяет RGBA пиксель по указанным координатам |
| data:set(x: int, y: int, r: int, g: int, b: int, a: int) | изменяет RGBA пиксель по указанным координатам |
| data:update() | применяет изменения и загружает холст в видеопамять |

## Inventory (inventory)

Свойства:
Expand Down
4 changes: 4 additions & 0 deletions doc/ru/xml-ui-layouts.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,10 @@

- `src` - имя изображения в папке textures без указания расширения. Тип: строка. Например `gui/error`

## Холст - *canvas*

- _Нет дополнительных свойств_

## Текстовое поле - *textbox*

Внутренний текст - изначально введенный текст
Expand Down
4 changes: 3 additions & 1 deletion res/layouts/ingame_chat.xml.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ local animation_fps = 30

local function remove_line(line)
document[line[1]]:destruct()
time.post_runnable(function() document.root:reposition() end)
time.post_runnable(function()
if world.is_open() then document.root:reposition() end
end)
end

local function update_line(line, uptime)
Expand Down
4 changes: 2 additions & 2 deletions res/modules/bit_converter.lua
Original file line number Diff line number Diff line change
Expand Up @@ -313,8 +313,8 @@ function bit_converter.bytes_to_uint16(bytes, order)

return
bit.bor(
bit.lshift(bytes[2], 8),
bytes[1], 0)
bit.lshift(bytes[1], 8),
bytes[2], 0)
end

function bit_converter.bytes_to_int64(bytes, order)
Expand Down
2 changes: 1 addition & 1 deletion res/modules/internal/gui_util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ function gui_util.add_page_dispatcher(dispatcher)
table.insert(gui_util.local_dispatchers, dispatcher)
end

function gui_util.reset_local()
function gui_util.__reset_local()
gui_util.local_dispatchers = {}
end

Expand Down
3 changes: 3 additions & 0 deletions res/modules/internal/stdcomp.lua
Original file line number Diff line number Diff line change
Expand Up @@ -132,5 +132,8 @@ return {
end
return values
end
end,
__reset = function()
entities = {}
end
}
7 changes: 4 additions & 3 deletions res/scripts/stdlib.lua
Original file line number Diff line number Diff line change
Expand Up @@ -343,8 +343,8 @@ function __vc_on_hud_open()
end)
end)
input.add_callback("key:escape", function()
if hud.is_paused() then
hud.resume()
if menu.page ~= "" then
menu:reset()
elseif hud.is_inventory_open() then
hud.close_inventory()
else
Expand Down Expand Up @@ -375,7 +375,8 @@ end

function __vc_on_world_quit()
_rules.clear()
gui_util:reset_local()
gui_util:__reset_local()
stdcomp.__reset()
end

local __vc_coroutines = {}
Expand Down
5 changes: 3 additions & 2 deletions src/frontend/hud.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -687,9 +687,10 @@ void Hud::setPause(bool pause) {
}

const auto& menu = gui.getMenu();
if (menu->hasOpenPage()) {
if (!pause && menu->hasOpenPage()) {
menu->reset();
} else {
}
if (pause && !menu->hasOpenPage()) {
menu->setPage("pause");
}
menu->setVisible(pause);
Expand Down
4 changes: 3 additions & 1 deletion src/frontend/screens/LevelScreen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,9 @@ void LevelScreen::initializePack(ContentPackRuntime* pack) {
}

LevelScreen::~LevelScreen() {
saveWorldPreview();
if (!controller->getLevel()->getWorld()->isNameless()) {
saveWorldPreview();
}
scripting::on_frontend_close();
// unblock all bindings
Events::enableBindings();
Expand Down
19 changes: 19 additions & 0 deletions src/graphics/ui/elements/Canvas.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include "Canvas.hpp"

#include "graphics/core/Batch2D.hpp"
#include "graphics/core/DrawContext.hpp"
#include "graphics/core/Texture.hpp"

gui::Canvas::Canvas(ImageFormat inFormat, glm::uvec2 inSize) : UINode(inSize) {
ImageData data {inFormat, inSize.x, inSize.y};
mTexture = Texture::from(&data);
}

void gui::Canvas::draw(const DrawContext& pctx, const Assets& assets) {
auto pos = calcPos();
auto col = calcColor();

auto batch = pctx.getBatch2D();
batch->texture(mTexture.get());
batch->rect(pos.x, pos.y, size.x, size.y, 0, 0, 0, {}, false, true, col);
}
25 changes: 25 additions & 0 deletions src/graphics/ui/elements/Canvas.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#pragma once

#include "UINode.hpp"
#include "graphics/core/ImageData.hpp"
#include "graphics/core/Texture.hpp"

class Texture;

namespace gui {
class Canvas final : public UINode {
public:
explicit Canvas(ImageFormat inFormat, glm::uvec2 inSize);

~Canvas() override = default;

void draw(const DrawContext& pctx, const Assets& assets) override;

[[nodiscard]] std::shared_ptr<::Texture> texture() const {
return mTexture;
}
private:
std::shared_ptr<::Texture> mTexture;
std::unique_ptr<ImageData> mData;
};
}
14 changes: 14 additions & 0 deletions src/graphics/ui/gui_xml.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "elements/Image.hpp"
#include "elements/Menu.hpp"
#include "elements/Button.hpp"
#include "elements/Canvas.hpp"
#include "elements/CheckBox.hpp"
#include "elements/TextBox.hpp"
#include "elements/TrackBar.hpp"
Expand Down Expand Up @@ -455,6 +456,18 @@ static std::shared_ptr<UINode> readImage(
return image;
}

static std::shared_ptr<UINode> readCanvas(
const UiXmlReader& reader, const xml::xmlelement& element
) {
auto size = glm::uvec2{32, 32};
if (element.has("size")) {
size = element.attr("size").asVec2();
}
auto image = std::make_shared<Canvas>(ImageFormat::rgba8888, size);
_readUINode(reader, element, *image);
return image;
}

static std::shared_ptr<UINode> readTrackBar(
const UiXmlReader& reader, const xml::xmlelement& element
) {
Expand Down Expand Up @@ -634,6 +647,7 @@ static std::shared_ptr<UINode> readPageBox(UiXmlReader& reader, const xml::xmlel
UiXmlReader::UiXmlReader(const scriptenv& env) : env(env) {
contextStack.emplace("");
add("image", readImage);
add("canvas", readCanvas);
add("label", readLabel);
add("panel", readPanel);
add("button", readButton);
Expand Down
9 changes: 9 additions & 0 deletions src/logic/scripting/lua/libs/libgui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "engine/Engine.hpp"
#include "frontend/locale.hpp"
#include "graphics/ui/elements/Button.hpp"
#include "graphics/ui/elements/Canvas.hpp"
#include "graphics/ui/elements/CheckBox.hpp"
#include "graphics/ui/elements/Image.hpp"
#include "graphics/ui/elements/InventoryView.hpp"
Expand Down Expand Up @@ -323,6 +324,13 @@ static int p_get_src(UINode* node, lua::State* L) {
return 0;
}

static int p_get_data(UINode* node, lua::State* L) {
if (auto canvas = dynamic_cast<Canvas*>(node)) {
return lua::newuserdata<lua::LuaCanvas>(L, canvas->texture());
}
return 0;
}

static int p_get_add(UINode* node, lua::State* L) {
if (dynamic_cast<Container*>(node)) {
return lua::pushcfunction(L, lua::wrap<l_container_add>);
Expand Down Expand Up @@ -464,6 +472,7 @@ static int l_gui_getattr(lua::State* L) {
{"inventory", p_get_inventory},
{"focused", p_get_focused},
{"cursor", p_get_cursor},
{"data", p_get_data},
};
auto func = getters.find(attr);
if (func != getters.end()) {
Expand Down
Loading

0 comments on commit 7997e9a

Please sign in to comment.