Merge branch 'main' into main

This commit is contained in:
Onran 2024-02-22 21:20:30 +09:00 committed by GitHub
commit a78b5213e3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
20 changed files with 256 additions and 43 deletions

View File

@ -1,6 +1,6 @@
{
"id": "base",
"title": "Base",
"version": "0.19",
"version": "0.20",
"description": "basic content package"
}

View File

@ -42,7 +42,7 @@ function load_script(path, nocache)
error("script '"..filename.."' not found in '"..packname.."'")
end
local script, err = loadfile(file.resolve(path))
local script, err = load(file.read(path), path)
if script == nil then
error(err)
end

View File

@ -2,10 +2,13 @@
#define SRC_CONSTANTS_H_
#include <limits>
#include <string>
#include "typedefs.h"
const int ENGINE_VERSION_MAJOR = 0;
const int ENGINE_VERSION_MINOR = 19;
const int ENGINE_VERSION_MINOR = 20;
const bool ENGINE_VERSION_INDEV = true;
#define ENGINE_VERSION_STRING "0.20"
const int BLOCK_AIR = 0;
const int ITEM_EMPTY = 0;

View File

@ -75,6 +75,9 @@ Engine::Engine(EngineSettings& settings, EnginePaths* paths)
gui = std::make_unique<gui::GUI>();
if (settings.ui.language == "auto") {
settings.ui.language = langs::locale_by_envlocale(platform::detect_locale(), paths->getResources());
}
if (ENGINE_VERSION_INDEV) {
menus::create_version_label(this);
}
setLanguage(settings.ui.language);
}

View File

@ -36,6 +36,14 @@ std::wstring Label::getText() const {
return text;
}
void Label::setFontName(std::string name) {
this->fontName = name;
}
const std::string& Label::getFontName() const {
return fontName;
}
void Label::draw(const GfxContext* pctx, Assets* assets) {
if (supplier) {
setText(supplier());
@ -246,6 +254,25 @@ TextBox::TextBox(std::wstring placeholder, glm::vec4 padding)
setHoverColor(glm::vec4(0.05f, 0.1f, 0.2f, 0.75f));
}
void TextBox::draw(const GfxContext* pctx, Assets* assets) {
Panel::draw(pctx, assets);
font = assets->getFont(label->getFontName());
if (!isFocused())
return;
if (int((Window::time() - caretLastMove) * 2) % 2 == 0) {
auto batch = pctx->getBatch2D();
batch->texture(nullptr);
batch->color = glm::vec4(1.0f);
glm::vec2 lcoord = label->calcCoord();
int width = font->calcWidth(input.substr(0, caret));
batch->rect(lcoord.x + width, lcoord.y, 2, font->getLineHeight());
}
}
void TextBox::drawBackground(const GfxContext* pctx, Assets* assets) {
glm::vec2 coord = calcCoord();
@ -279,11 +306,22 @@ void TextBox::drawBackground(const GfxContext* pctx, Assets* assets) {
setScrollable(false);
}
void TextBox::typed(unsigned int codepoint) {
input += std::wstring({(wchar_t)codepoint});
void TextBox::paste(const std::wstring& text) {
if (caret >= input.length()) {
input += text;
} else {
auto left = input.substr(0, caret);
auto right = input.substr(caret);
input = left + text + right;
}
setCaret(caret + text.length());
validate();
}
void TextBox::typed(unsigned int codepoint) {
paste(std::wstring({(wchar_t)codepoint}));
}
bool TextBox::validate() {
if (validator) {
valid = validator(getText());
@ -308,6 +346,7 @@ void TextBox::setOnEditStart(runnable oneditstart) {
void TextBox::focus(GUI* gui) {
Panel::focus(gui);
if (onEditStart){
setCaret(input.size());
onEditStart();
}
}
@ -317,24 +356,60 @@ void TextBox::refresh() {
label->setSize(size-glm::vec2(padding.z+padding.x, padding.w+padding.y));
}
void TextBox::clicked(GUI*, int button) {
}
void TextBox::mouseMove(GUI*, int x, int y) {
if (font == nullptr)
return;
glm::vec2 lcoord = label->calcCoord();
uint offset = 0;
while (lcoord.x + font->calcWidth(input, offset) < x && offset <= input.length()) {
offset++;
}
setCaret(offset);
}
void TextBox::keyPressed(int key) {
if (key == keycode::BACKSPACE) {
if (!input.empty()){
input = input.substr(0, input.length()-1);
if (caret > 0 && input.length() > 0) {
if (caret > input.length()) {
caret = input.length();
}
input = input.substr(0, caret-1) + input.substr(caret);
setCaret(caret-1);
validate();
}
} else if (key == keycode::DELETE) {
if (caret < input.length()) {
input = input.substr(0, caret) + input.substr(caret + 1);
validate();
}
} else if (key == keycode::ENTER) {
if (validate() && consumer) {
consumer(label->getText());
}
defocus();
defocus();
} else if (key == keycode::LEFT) {
if (caret > 0) {
if (caret > input.length()) {
setCaret(input.length()-1);
} else {
setCaret(caret-1);
}
}
} else if (key == keycode::RIGHT) {
if (caret < input.length()) {
setCaret(caret+1);
caretLastMove = Window::time();
}
}
// Pasting text from clipboard
if (key == keycode::V && Events::pressed(keycode::LEFT_CONTROL)) {
const char* text = Window::getClipboardText();
if (text) {
input += util::str2wstr_utf8(text);
validate();
paste(util::str2wstr_utf8(text));
}
}
}
@ -355,6 +430,22 @@ void TextBox::setTextValidator(wstringchecker validator) {
this->validator = validator;
}
void TextBox::setFocusedColor(glm::vec4 color) {
this->focusedColor = color;
}
glm::vec4 TextBox::getFocusedColor() const {
return focusedColor;
}
void TextBox::setErrorColor(glm::vec4 color) {
this->invalidColor = color;
}
glm::vec4 TextBox::getErrorColor() const {
return invalidColor;
}
std::wstring TextBox::getText() const {
if (input.empty())
return placeholder;
@ -373,6 +464,15 @@ void TextBox::setPlaceholder(const std::wstring& placeholder) {
this->placeholder = placeholder;
}
uint TextBox::getCaret() const {
return caret;
}
void TextBox::setCaret(uint position) {
this->caret = position;
caretLastMove = Window::time();
}
// ============================== InputBindBox ================================
InputBindBox::InputBindBox(Binding& binding, glm::vec4 padding)
: Panel(glm::vec2(100,32), padding, 0),

View File

@ -15,6 +15,7 @@
class Batch2D;
class Assets;
class Font;
namespace gui {
class Label : public UINode {
@ -29,6 +30,9 @@ namespace gui {
virtual void setText(std::wstring text);
std::wstring getText() const;
virtual void setFontName(std::string name);
const std::string& getFontName() const;
virtual void draw(const GfxContext* pctx, Assets* assets) override;
virtual void textSupplier(wstringsupplier supplier);
@ -105,12 +109,19 @@ namespace gui {
wstringchecker validator = nullptr;
runnable onEditStart = nullptr;
bool valid = true;
/// @brief text input pointer, value may be greather than text length
uint caret = 0;
double caretLastMove = 0.0;
Font* font = nullptr;
void paste(const std::wstring& text);
public:
TextBox(std::wstring placeholder,
glm::vec4 padding=glm::vec4(4.0f));
virtual std::shared_ptr<UINode> getAt(glm::vec2 pos, std::shared_ptr<UINode> self) override;
virtual void draw(const GfxContext* pctx, Assets* assets) override;
virtual void drawBackground(const GfxContext* pctx, Assets* assets) override;
virtual void typed(unsigned int codepoint) override;
virtual void keyPressed(int key) override;
@ -118,18 +129,26 @@ namespace gui {
virtual void setTextConsumer(wstringconsumer consumer);
virtual void setTextValidator(wstringchecker validator);
virtual bool isFocuskeeper() const override {return true;}
virtual void setFocusedColor(glm::vec4 color);
virtual glm::vec4 getFocusedColor() const;
virtual void setErrorColor(glm::vec4 color);
virtual glm::vec4 getErrorColor() const;
/* Get TextBox content text or placeholder if empty */
virtual std::wstring getText() const;
/* Set TextBox content text */
virtual void setText(std::wstring value);
virtual std::wstring getPlaceholder() const;
virtual void setPlaceholder(const std::wstring&);
virtual uint getCaret() const;
virtual void setCaret(uint position);
virtual bool validate();
virtual void setValid(bool valid);
virtual bool isValid() const;
virtual void setOnEditStart(runnable oneditstart);
virtual void focus(GUI*) override;
virtual void refresh() override;
virtual void clicked(GUI*, int button) override;
virtual void mouseMove(GUI*, int x, int y) override;
};
class InputBindBox : public Panel {

View File

@ -228,6 +228,20 @@ static std::shared_ptr<UINode> readTextBox(UiXmlReader& reader, xml::xmlelement
);
textbox->setTextSupplier(supplier);
}
if (element->has("focused-color")) {
textbox->setFocusedColor(element->attr("focused-color").asColor());
}
if (element->has("error-color")) {
textbox->setErrorColor(element->attr("error-color").asColor());
}
if (element->has("validator")) {
auto validator = scripting::create_wstring_validator(
reader.getEnvironment().getId(),
element->attr("validator").getText(),
reader.getFilename()+".lua"
);
textbox->setTextValidator(validator);
}
return textbox;
}

View File

@ -380,18 +380,20 @@ void Hud::update(bool visible) {
}
glm::vec2 invSize = contentAccessPanel->getSize();
contentAccessPanel->setVisible(inventoryOpen);
contentAccessPanel->setVisible(inventoryView != nullptr);
contentAccessPanel->setSize(glm::vec2(invSize.x, Window::height));
contentAccess->setMinSize(glm::vec2(1, Window::height));
hotbarView->setVisible(visible);
for (int i = keycode::NUM_1; i <= keycode::NUM_9; i++) {
if (Events::jpressed(i)) {
player->setChosenSlot(i - keycode::NUM_1);
if (!gui->isFocusCaught() && !pause) {
for (int i = keycode::NUM_1; i <= keycode::NUM_9; i++) {
if (Events::jpressed(i)) {
player->setChosenSlot(i - keycode::NUM_1);
}
}
if (Events::jpressed(keycode::NUM_0)) {
player->setChosenSlot(9);
}
}
if (Events::jpressed(keycode::NUM_0)) {
player->setChosenSlot(9);
}
if (!pause && !inventoryOpen && Events::scroll) {
int slot = player->getChosenSlot();
@ -436,7 +438,7 @@ void Hud::openInventory() {
* @param blockinv block inventory.
* In case of nullptr a new virtual inventory will be created
*/
void Hud::openInventory(glm::ivec3 block, UiDocument* doc, std::shared_ptr<Inventory> blockinv) {
void Hud::openInventory(glm::ivec3 block, UiDocument* doc, std::shared_ptr<Inventory> blockinv, bool playerInventory) {
if (isInventoryOpen()) {
closeInventory();
}
@ -445,7 +447,11 @@ void Hud::openInventory(glm::ivec3 block, UiDocument* doc, std::shared_ptr<Inven
if (blockUI == nullptr) {
throw std::runtime_error("block UI root element must be 'inventory'");
}
openInventory();
if (playerInventory) {
openInventory();
} else {
inventoryOpen = true;
}
if (blockinv == nullptr) {
blockinv = level->inventories->createVirtual(blockUI->getSlotsCount());
}
@ -594,25 +600,30 @@ void Hud::draw(const GfxContext& ctx){
}
if (inventoryOpen) {
float caWidth = contentAccess->getSize().x;
float caWidth = inventoryView ? contentAccess->getSize().x : 0.0f;
contentAccessPanel->setCoord(glm::vec2(width-caWidth, 0));
glm::vec2 invSize = inventoryView->getSize();
glm::vec2 invSize = inventoryView ? inventoryView->getSize() : glm::vec2();
if (blockUI == nullptr) {
inventoryView->setCoord(glm::vec2(
glm::min(width/2-invSize.x/2, width-caWidth-10-invSize.x),
height/2-invSize.y/2
));
if (inventoryView) {
inventoryView->setCoord(glm::vec2(
glm::min(width/2-invSize.x/2, width-caWidth-10-invSize.x),
height/2-invSize.y/2
));
}
} else {
glm::vec2 blockInvSize = blockUI->getSize();
int interval = 5;
float invwidth = glm::max(invSize.x, blockInvSize.x);
int interval = invSize.y > 0.0 ? 5 : 0;
float totalHeight = invSize.y + blockInvSize.y + interval;
inventoryView->setCoord(glm::vec2(
glm::min(width/2-invSize.x/2, width-caWidth-10-invSize.x),
height/2+totalHeight/2-invSize.y
));
if (inventoryView) {
inventoryView->setCoord(glm::vec2(
glm::min(width/2-invwidth/2, width-caWidth-10-invwidth),
height/2+totalHeight/2-invSize.y
));
}
blockUI->setCoord(glm::vec2(
glm::min(width/2-invSize.x/2, width-caWidth-10-invSize.x),
glm::min(width/2-invwidth/2, width-caWidth-10-invwidth),
height/2-totalHeight/2
));
}

View File

@ -110,7 +110,7 @@ public:
void setPause(bool pause);
void openInventory();
void openInventory(glm::ivec3 block, UiDocument* doc, std::shared_ptr<Inventory> blockInv);
void openInventory(glm::ivec3 block, UiDocument* doc, std::shared_ptr<Inventory> blockInv, bool playerInventory);
void closeInventory();
void openPermanent(UiDocument* doc);

View File

@ -87,6 +87,20 @@ static std::shared_ptr<Button> create_button(
return btn;
}
void menus::create_version_label(Engine* engine) {
auto gui = engine->getGUI();
auto vlabel = std::make_shared<gui::Label>(
util::str2wstr_utf8(ENGINE_VERSION_STRING " development build ")
);
vlabel->setZIndex(1000);
vlabel->setColor(glm::vec4(1, 1, 1, 0.5f));
vlabel->setPositionFunc([=]() {
return glm::vec2(Window::width-vlabel->getSize().x, 2);
});
gui->add(vlabel);
}
static void show_content_missing(
Engine* engine,
const Content* content,

View File

@ -4,6 +4,7 @@
class Engine;
namespace menus {
void create_version_label(Engine* engine);
void create_menus(Engine* engine);
void refresh_menus(Engine* engine);
}

View File

@ -34,8 +34,8 @@ bool Font::isPrintableChar(int c) {
const int RES = 16;
int Font::calcWidth(std::wstring text) {
return text.length() * 8;
int Font::calcWidth(std::wstring text, size_t length) {
return std::min(text.length(), length) * 8;
}
void Font::draw(Batch2D* batch, std::wstring text, int x, int y) {

View File

@ -23,7 +23,7 @@ public:
int getLineHeight() const;
int getYOffset() const;
int calcWidth(std::wstring text);
int calcWidth(std::wstring text, size_t length=-1);
// int getGlyphWidth(char c);
bool isPrintableChar(int c);
void draw(Batch2D* batch, std::wstring text, int x, int y);

View File

@ -11,7 +11,19 @@
lua::luaerror::luaerror(const std::string& message) : std::runtime_error(message) {
}
void lua::LuaState::removeLibFuncs(const char* libname, const char* funcs[]) {
if (getglobal(libname)) {
for (uint i = 0; funcs[i]; i++) {
pushnil();
setfield(funcs[i], -2);
}
}
}
lua::LuaState::LuaState() {
std::cout << LUA_VERSION << std::endl;
std::cout << LUAJIT_VERSION << std::endl;
L = luaL_newstate();
if (L == nullptr) {
throw lua::luaerror("could not to initialize Lua");
@ -25,10 +37,19 @@ lua::LuaState::LuaState() {
luaopen_jit(L);
luaopen_bit(L);
std::cout << LUA_VERSION << std::endl;
std::cout << LUAJIT_VERSION << std::endl;
luaopen_os(L);
const char* removed_os[] {
"execute",
"exit",
"remove",
"rename",
"setlocale",
"tmpname",
nullptr
};
removeLibFuncs("os", removed_os);
createFuncs();
createLibs();
lua_pushvalue(L, LUA_GLOBALSINDEX);
setglobal(envName(0));
@ -94,7 +115,7 @@ void lua::LuaState::remove(const std::string& name) {
lua_setglobal(L, name.c_str());
}
void lua::LuaState::createFuncs() {
void lua::LuaState::createLibs() {
openlib("pack", packlib, 0);
openlib("world", worldlib, 0);
openlib("player", playerlib, 0);

View File

@ -21,6 +21,9 @@ namespace lua {
void logError(const std::string& text);
void initEnvironment();
void removeLibFuncs(const char* libname, const char* funcs[]);
void createLibs();
public:
LuaState();
~LuaState();
@ -55,7 +58,6 @@ namespace lua {
bool hasglobal(const std::string& name);
bool rename(const std::string& from, const std::string& to);
void remove(const std::string& name);;
void createFuncs();
int createEnvironment(int parent);
void removeEnvironment(int id);
const std::string storeAnonymous();

View File

@ -40,6 +40,7 @@ int l_hud_open_block(lua_State* L) {
lua::luaint x = lua_tointeger(L, 1);
lua::luaint y = lua_tointeger(L, 2);
lua::luaint z = lua_tointeger(L, 3);
bool playerInventory = !lua_toboolean(L, 4);
voxel* vox = scripting::level->chunks->get(x, y, z);
if (vox == nullptr) {
@ -55,7 +56,7 @@ int l_hud_open_block(lua_State* L) {
auto id = scripting::blocks->createBlockInventory(x, y, z);
scripting::hud->openInventory(
glm::ivec3(x, y, z), layout, scripting::level->inventories->get(id)
glm::ivec3(x, y, z), layout, scripting::level->inventories->get(id), playerInventory
);
lua_pushinteger(L, id);

View File

@ -66,6 +66,21 @@ wstringsupplier scripting::create_wstring_supplier(
};
}
wstringchecker scripting::create_wstring_validator(
int env,
const std::string& src,
const std::string& file
) {
return [=](const std::wstring& x){
if (processCallback(env, src, file)) {
state->pushstring(util::wstr2str_utf8(x));
if (state->callNoThrow(1))
return state->toboolean(-1);
}
return false;
};
}
boolconsumer scripting::create_bool_consumer(
int env,
const std::string& src,

View File

@ -24,6 +24,12 @@ namespace scripting {
const std::string& file="<string>"
);
wstringchecker create_wstring_validator(
int env,
const std::string& src,
const std::string& file="<string>"
);
boolconsumer create_bool_consumer(
int env,
const std::string& src,

View File

@ -27,7 +27,7 @@ Level::Level(World* world, const Content* content, EngineSettings& settings)
{
objCounter = 0;
physics = new PhysicsSolver(glm::vec3(0, -22.6f, 0));
auto inv = std::make_shared<Inventory>(world->getNextInventoryId(), DEF_PLAYER_INVENTORY_SIZE);
auto inv = std::make_shared<Inventory>(0, DEF_PLAYER_INVENTORY_SIZE);
player = spawnObject<Player>(glm::vec3(0, DEF_PLAYER_Y, 0), DEF_PLAYER_SPEED, inv);
uint matrixSize = (settings.chunks.loadDistance+

View File

@ -91,7 +91,9 @@ Level* World::create(std::string name,
const std::vector<ContentPack>& packs) {
auto world = new World(name, type, directory, seed, settings, content, packs);
auto level = new Level(world, content, settings);
auto inventory = level->player->getInventory();
inventory->setId(world->getNextInventoryId());
level->inventories->store(inventory);
return level;
}
@ -110,6 +112,7 @@ Level* World::load(fs::path directory,
auto level = new Level(world.get(), content, settings);
wfile->readPlayer(level->player);
level->inventories->store(level->player->getInventory());
world.release();
return level;
}