Merge branch 'main' into update-items

This commit is contained in:
MihailRis 2025-02-18 18:39:37 +03:00
commit 3d0d6cab93
18 changed files with 93 additions and 48 deletions

View File

@ -1,5 +1,6 @@
{ {
"steps-sound": "steps/grass", "steps-sound": "steps/grass",
"place-sound": "blocks/ground_place", "place-sound": "blocks/ground_place",
"break-sound": "blocks/ground_break" "break-sound": "blocks/ground_break",
"hit-sound": "blocks/ground_hit"
} }

View File

@ -1,5 +1,6 @@
{ {
"steps-sound": "steps/ground", "steps-sound": "steps/ground",
"place-sound": "blocks/ground_place", "place-sound": "blocks/ground_place",
"break-sound": "blocks/ground_break" "break-sound": "blocks/ground_break",
"hit-sound": "blocks/ground_hit"
} }

View File

@ -1,5 +1,6 @@
{ {
"steps-sound": "steps/stone", "steps-sound": "steps/stone",
"place-sound": "blocks/stone_place", "place-sound": "blocks/stone_place",
"break-sound": "blocks/stone_break" "break-sound": "blocks/stone_break",
"hit-sound": "blocks/stone_hit"
} }

View File

@ -1,5 +1,6 @@
{ {
"steps-sound": "steps/wood", "steps-sound": "steps/wood",
"place-sound": "blocks/wood_place", "place-sound": "blocks/wood_place",
"break-sound": "blocks/wood_break" "break-sound": "blocks/wood_break",
"hit-sound": "blocks/wood_hit"
} }

View File

@ -2,5 +2,5 @@
"texture": "dirt", "texture": "dirt",
"material": "base:ground", "material": "base:ground",
"surface-replacement": "base:grass_block", "surface-replacement": "base:grass_block",
"base:durability": 1.0 "base:durability": 1.5
} }

View File

@ -8,7 +8,7 @@
"replaceable": true, "replaceable": true,
"grounded": true, "grounded": true,
"model": "X", "model": "X",
"hitbox": [0.15, 0.0, 0.15, 0.7, 0.7, 0.7], "hitbox": [0.15, 0.0, 0.15, 0.7, 0.5, 0.7],
"base:durability": 0.0, "base:durability": 0.0,
"base:loot": [] "base:loot": []
} }

View File

@ -8,7 +8,7 @@
"grass_side", "grass_side",
"grass_side" "grass_side"
], ],
"base:durability": 1.3, "base:durability": 1.7,
"base:loot": [ "base:loot": [
{"item": "base:dirt.item"} {"item": "base:dirt.item"}
] ]

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -355,7 +355,7 @@ function __vc_on_hud_open()
hud._set_debug_cheats(value) hud._set_debug_cheats(value)
end) end)
input.add_callback("devtools.console", function() input.add_callback("devtools.console", function()
if hud.is_paused() then if menu.page ~= "" then
return return
end end
time.post_runnable(function() time.post_runnable(function()
@ -363,7 +363,7 @@ function __vc_on_hud_open()
end) end)
end) end)
input.add_callback("hud.chat", function() input.add_callback("hud.chat", function()
if hud.is_paused() then if menu.page ~= "" then
return return
end end
time.post_runnable(function() time.post_runnable(function()

View File

@ -21,6 +21,20 @@ void LabelCache::prepare(Font* font, size_t wrapWidth) {
} }
} }
size_t LabelCache::getTextLineOffset(size_t line) const {
line = std::min(lines.size()-1, line);
return lines.at(line).offset;
}
uint LabelCache::getLineByTextIndex(size_t index) const {
for (size_t i = 0; i < lines.size(); i++) {
if (lines[i].offset > index) {
return i-1;
}
}
return lines.size()-1;
}
void LabelCache::update(const std::wstring& text, bool multiline, bool wrap) { void LabelCache::update(const std::wstring& text, bool multiline, bool wrap) {
resetFlag = false; resetFlag = false;
lines.clear(); lines.clear();
@ -49,7 +63,7 @@ void LabelCache::update(const std::wstring& text, bool multiline, bool wrap) {
} }
Label::Label(const std::string& text, std::string fontName) Label::Label(const std::string& text, std::string fontName)
: UINode(glm::vec2(text.length() * 8, 15)), : UINode(glm::vec2(text.length() * 8, 16)),
text(util::str2wstr_utf8(text)), text(util::str2wstr_utf8(text)),
fontName(std::move(fontName)) fontName(std::move(fontName))
{ {
@ -59,7 +73,7 @@ Label::Label(const std::string& text, std::string fontName)
Label::Label(const std::wstring& text, std::string fontName) Label::Label(const std::wstring& text, std::string fontName)
: UINode(glm::vec2(text.length() * 8, 15)), : UINode(glm::vec2(text.length() * 8, 16)),
text(text), text(text),
fontName(std::move(fontName)) fontName(std::move(fontName))
{ {
@ -131,8 +145,7 @@ int Label::getTextYOffset() const {
} }
size_t Label::getTextLineOffset(size_t line) const { size_t Label::getTextLineOffset(size_t line) const {
line = std::min(cache.lines.size()-1, line); return cache.getTextLineOffset(line);
return cache.lines.at(line).offset;
} }
bool Label::isFakeLine(size_t line) const { bool Label::isFakeLine(size_t line) const {
@ -152,12 +165,7 @@ uint Label::getLineByYOffset(int offset) const {
} }
uint Label::getLineByTextIndex(size_t index) const { uint Label::getLineByTextIndex(size_t index) const {
for (size_t i = 0; i < cache.lines.size(); i++) { return cache.getLineByTextIndex(index);
if (cache.lines[i].offset > index) {
return i-1;
}
}
return cache.lines.size()-1;
} }
uint Label::getLinesNumber() const { uint Label::getLinesNumber() const {

View File

@ -20,6 +20,9 @@ namespace gui {
void prepare(Font* font, size_t wrapWidth); void prepare(Font* font, size_t wrapWidth);
void update(const std::wstring& text, bool multiline, bool wrap); void update(const std::wstring& text, bool multiline, bool wrap);
size_t getTextLineOffset(size_t line) const;
uint getLineByTextIndex(size_t index) const;
}; };
class Label : public UINode { class Label : public UINode {

View File

@ -57,6 +57,8 @@ void TextBox::draw(const DrawContext& pctx, const Assets& assets) {
if (!isFocused()) { if (!isFocused()) {
return; return;
} }
const auto& labelText = getText();
glm::vec2 pos = calcPos(); glm::vec2 pos = calcPos();
glm::vec2 size = getSize(); glm::vec2 size = getSize();
@ -70,8 +72,8 @@ void TextBox::draw(const DrawContext& pctx, const Assets& assets) {
batch->texture(nullptr); batch->texture(nullptr);
batch->setColor(glm::vec4(1.0f)); batch->setColor(glm::vec4(1.0f));
if (editable && int((Window::time() - caretLastMove) * 2) % 2 == 0) { if (editable && int((Window::time() - caretLastMove) * 2) % 2 == 0) {
uint line = label->getLineByTextIndex(caret); uint line = rawTextCache.getLineByTextIndex(caret);
uint lcaret = caret - label->getTextLineOffset(line); uint lcaret = caret - rawTextCache.getTextLineOffset(line);
int width = font->calcWidth(input, lcaret); int width = font->calcWidth(input, lcaret);
batch->rect( batch->rect(
lcoord.x + width, lcoord.x + width,
@ -89,10 +91,10 @@ void TextBox::draw(const DrawContext& pctx, const Assets& assets) {
batch->setColor(glm::vec4(0.8f, 0.9f, 1.0f, 0.25f)); batch->setColor(glm::vec4(0.8f, 0.9f, 1.0f, 0.25f));
int start = font->calcWidth( int start = font->calcWidth(
input, selectionStart - label->getTextLineOffset(startLine) labelText, selectionStart - label->getTextLineOffset(startLine)
); );
int end = font->calcWidth( int end = font->calcWidth(
input, selectionEnd - label->getTextLineOffset(endLine) labelText, selectionEnd - label->getTextLineOffset(endLine)
); );
int lineY = label->getLineYOffset(startLine); int lineY = label->getLineYOffset(startLine);
@ -192,11 +194,14 @@ void TextBox::drawBackground(const DrawContext& pctx, const Assets&) {
} }
void TextBox::refreshLabel() { void TextBox::refreshLabel() {
rawTextCache.prepare(font, static_cast<size_t>(getSize().x));
rawTextCache.update(input, multiline, false);
label->setColor(textColor * glm::vec4(input.empty() ? 0.5f : 1.0f)); label->setColor(textColor * glm::vec4(input.empty() ? 0.5f : 1.0f));
const auto& displayText = input.empty() && !hint.empty() ? hint : getText(); const auto& displayText = input.empty() && !hint.empty() ? hint : getText();
if (markup == "md") { if (markup == "md") {
auto [processedText, styles] = markdown::process(displayText, !focused); auto [processedText, styles] = markdown::process(displayText, !focused || !editable);
label->setText(std::move(processedText)); label->setText(std::move(processedText));
label->setStyles(std::move(styles)); label->setStyles(std::move(styles));
} else { } else {
@ -313,7 +318,7 @@ size_t TextBox::getLineLength(uint line) const {
size_t position = label->getTextLineOffset(line); size_t position = label->getTextLineOffset(line);
size_t lineLength = label->getTextLineOffset(line+1)-position; size_t lineLength = label->getTextLineOffset(line+1)-position;
if (lineLength == 0) { if (lineLength == 0) {
lineLength = input.length() - position + 1; lineLength = label->getText().length() - position + 1;
} }
return lineLength; return lineLength;
} }
@ -325,8 +330,8 @@ size_t TextBox::getSelectionLength() const {
/// @brief Set scroll offset /// @brief Set scroll offset
/// @param x scroll offset /// @param x scroll offset
void TextBox::setTextOffset(uint x) { void TextBox::setTextOffset(uint x) {
label->setPos(glm::vec2(textInitX - int(x), label->getPos().y));
textOffset = x; textOffset = x;
refresh();
} }
void TextBox::typed(unsigned int codepoint) { void TextBox::typed(unsigned int codepoint) {
@ -403,7 +408,9 @@ void TextBox::refresh() {
Container::refresh(); Container::refresh();
label->setSize(size-glm::vec2(padding.z+padding.x, padding.w+padding.y)); label->setSize(size-glm::vec2(padding.z+padding.x, padding.w+padding.y));
label->setPos(glm::vec2( label->setPos(glm::vec2(
padding.x + LINE_NUMBERS_PANE_WIDTH * showLineNumbers, padding.y padding.x + LINE_NUMBERS_PANE_WIDTH * showLineNumbers + textInitX -
static_cast<int>(textOffset),
padding.y
)); ));
} }
@ -421,15 +428,16 @@ size_t TextBox::normalizeIndex(int index) {
int TextBox::calcIndexAt(int x, int y) const { int TextBox::calcIndexAt(int x, int y) const {
if (font == nullptr) if (font == nullptr)
return 0; return 0;
const auto& labelText = label->getText();
glm::vec2 lcoord = label->calcPos(); glm::vec2 lcoord = label->calcPos();
uint line = label->getLineByYOffset(y-lcoord.y); uint line = label->getLineByYOffset(y-lcoord.y);
line = std::min(line, label->getLinesNumber()-1); line = std::min(line, label->getLinesNumber()-1);
size_t lineLength = getLineLength(line); size_t lineLength = getLineLength(line);
uint offset = 0; uint offset = 0;
while (lcoord.x + font->calcWidth(input, offset) < x && offset < lineLength-1) { while (lcoord.x + font->calcWidth(labelText, offset) < x && offset < lineLength-1) {
offset++; offset++;
} }
return std::min(offset+label->getTextLineOffset(line), input.length()); return std::min(offset+label->getTextLineOffset(line), labelText.length());
} }
static inline std::wstring get_alphabet(wchar_t c) { static inline std::wstring get_alphabet(wchar_t c) {
@ -443,21 +451,22 @@ static inline std::wstring get_alphabet(wchar_t c) {
} }
void TextBox::tokenSelectAt(int index) { void TextBox::tokenSelectAt(int index) {
if (input.empty()) { const auto& actualText = label->getText();
if (actualText.empty()) {
return; return;
} }
int left = index; int left = index;
int right = index; int right = index;
std::wstring alphabet = get_alphabet(input.at(index)); std::wstring alphabet = get_alphabet(actualText.at(index));
while (left >= 0) { while (left >= 0) {
if (alphabet.find(input.at(left)) == std::wstring::npos) { if (alphabet.find(actualText.at(left)) == std::wstring::npos) {
break; break;
} }
left--; left--;
} }
while (static_cast<size_t>(right) < input.length()) { while (static_cast<size_t>(right) < actualText.length()) {
if (alphabet.find(input.at(right)) == std::wstring::npos) { if (alphabet.find(actualText.at(right)) == std::wstring::npos) {
break; break;
} }
right++; right++;
@ -800,7 +809,8 @@ void TextBox::setHint(const std::wstring& text) {
} }
std::wstring TextBox::getSelection() const { std::wstring TextBox::getSelection() const {
return input.substr(selectionStart, selectionEnd-selectionStart); const auto& text = label->getText();
return text.substr(selectionStart, selectionEnd-selectionStart);
} }
size_t TextBox::getCaret() const { size_t TextBox::getCaret() const {
@ -808,13 +818,19 @@ size_t TextBox::getCaret() const {
} }
void TextBox::setCaret(size_t position) { void TextBox::setCaret(size_t position) {
this->caret = std::min(static_cast<size_t>(position), input.length()); const auto& labelText = label->getText();
caret = std::min(static_cast<size_t>(position), input.length());
if (font == nullptr) { if (font == nullptr) {
return; return;
} }
caretLastMove = Window::time();
int width = label->getSize().x; int width = label->getSize().x;
uint line = label->getLineByTextIndex(caret);
rawTextCache.prepare(font, width);
rawTextCache.update(input, multiline, label->isTextWrapping());
caretLastMove = Window::time();
uint line = rawTextCache.getLineByTextIndex(caret);
int offset = label->getLineYOffset(line) + getContentOffset().y; int offset = label->getLineYOffset(line) + getContentOffset().y;
uint lineHeight = font->getLineHeight()*label->getLineInterval(); uint lineHeight = font->getLineHeight()*label->getLineInterval();
if (scrollStep == 0) { if (scrollStep == 0) {
@ -826,8 +842,10 @@ void TextBox::setCaret(size_t position) {
offset -= getSize().y; offset -= getSize().y;
scrolled(-glm::ceil(offset / static_cast<double>(scrollStep)+0.5f)); scrolled(-glm::ceil(offset / static_cast<double>(scrollStep)+0.5f));
} }
uint lcaret = caret - label->getTextLineOffset(line); int lcaret = caret - rawTextCache.getTextLineOffset(line);
int realoffset = font->calcWidth(input, lcaret)-int(textOffset) + 2; int realoffset =
font->calcWidth(labelText, lcaret) - static_cast<int>(textOffset) + 2;
if (realoffset-width > 0) { if (realoffset-width > 0) {
setTextOffset(textOffset + realoffset-width); setTextOffset(textOffset + realoffset-width);
} else if (realoffset < 0) { } else if (realoffset < 0) {

View File

@ -6,9 +6,8 @@
class Font; class Font;
namespace gui { namespace gui {
class Label;
class TextBox : public Container { class TextBox : public Container {
LabelCache rawTextCache;
protected: protected:
glm::vec4 focusedColor {0.0f, 0.0f, 0.0f, 1.0f}; glm::vec4 focusedColor {0.0f, 0.0f, 0.0f, 1.0f};
glm::vec4 invalidColor {0.1f, 0.05f, 0.03f, 1.0f}; glm::vec4 invalidColor {0.1f, 0.05f, 0.03f, 1.0f};
@ -43,11 +42,12 @@ namespace gui {
/// @brief Actual local (line) position of the caret on vertical move /// @brief Actual local (line) position of the caret on vertical move
size_t maxLocalCaret = 0; size_t maxLocalCaret = 0;
size_t textOffset = 0; size_t textOffset = 0;
int textInitX; int textInitX = 0;
/// @brief Last time of the caret was moved (used for blink animation) /// @brief Last time of the caret was moved (used for blink animation)
double caretLastMove = 0.0; double caretLastMove = 0.0;
Font* font = nullptr; Font* font = nullptr;
// Note: selection does not include markup
size_t selectionStart = 0; size_t selectionStart = 0;
size_t selectionEnd = 0; size_t selectionEnd = 0;
size_t selectionOrigin = 0; size_t selectionOrigin = 0;

View File

@ -49,10 +49,20 @@ CameraControl::CameraControl(
offset(0.0f, 0.7f, 0.0f) { offset(0.0f, 0.7f, 0.0f) {
} }
void CameraControl::refresh() { void CameraControl::refreshPosition() {
camera->position = player.getPosition() + offset; camera->position = player.getPosition() + offset;
} }
void CameraControl::refreshRotation() {
const glm::vec3& rotation = player.getRotation();
camera->rotation = glm::mat4(1.0f);
camera->rotate(
glm::radians(rotation.y),
glm::radians(rotation.x),
glm::radians(rotation.z)
);
}
void CameraControl::updateMouse(PlayerInput& input) { void CameraControl::updateMouse(PlayerInput& input) {
glm::vec3 rotation = player.getRotation(); glm::vec3 rotation = player.getRotation();
@ -177,7 +187,7 @@ void CameraControl::update(
const auto& spCamera = player.spCamera; const auto& spCamera = player.spCamera;
const auto& tpCamera = player.tpCamera; const auto& tpCamera = player.tpCamera;
refresh(); refreshPosition();
camera->updateVectors(); camera->updateVectors();
if (player.currentCamera == spCamera) { if (player.currentCamera == spCamera) {
@ -274,6 +284,7 @@ void PlayerController::postUpdate(float delta, bool input, bool pause) {
if (!pause && input) { if (!pause && input) {
camControl.updateMouse(this->input); camControl.updateMouse(this->input);
} }
camControl.refreshRotation();
player.postUpdate(); player.postUpdate();
camControl.update(this->input, pause ? 0.0f : delta, *player.chunks); camControl.update(this->input, pause ? 0.0f : delta, *player.chunks);
if (input) { if (input) {

View File

@ -42,7 +42,8 @@ public:
CameraControl(Player& player, const CameraSettings& settings); CameraControl(Player& player, const CameraSettings& settings);
void updateMouse(PlayerInput& input); void updateMouse(PlayerInput& input);
void update(PlayerInput input, float delta, const Chunks& chunks); void update(PlayerInput input, float delta, const Chunks& chunks);
void refresh(); void refreshPosition();
void refreshRotation();
}; };
class PlayerController { class PlayerController {