mirror of
https://github.com/MihailRis/voxelcore.git
synced 2026-10-11 22:11:51 +00:00
commit
df99701a31
9 changed files with 100 additions and 92 deletions
|
|
@ -43,20 +43,28 @@ std::shared_ptr<UINode> Container::getAt(const glm::vec2& pos) {
|
||||||
return UINode::getAt(pos);
|
return UINode::getAt(pos);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Container::click(int x, int y) {
|
||||||
|
UINode::click(x, y);
|
||||||
|
|
||||||
|
if (scrollable) {
|
||||||
|
auto pos = calcPos();
|
||||||
|
x -= pos.x;
|
||||||
|
y -= pos.y;
|
||||||
|
if (x >= size.x - scrollBarWidth) {
|
||||||
|
scrollbarTriggered = true;
|
||||||
|
prevScrollY = y;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void Container::mouseMove(int x, int y) {
|
void Container::mouseMove(int x, int y) {
|
||||||
UINode::mouseMove(x, y);
|
UINode::mouseMove(x, y);
|
||||||
if (!scrollable) {
|
if (!scrollable || !scrollbarTriggered) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
auto pos = calcPos();
|
auto pos = calcPos();
|
||||||
x -= pos.x;
|
x -= pos.x;
|
||||||
y -= pos.y;
|
y -= pos.y;
|
||||||
if (prevScrollY == -1) {
|
|
||||||
if (x >= size.x - scrollBarWidth) {
|
|
||||||
prevScrollY = y;
|
|
||||||
}
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
int diff = (actualLength-size.y);
|
int diff = (actualLength-size.y);
|
||||||
if (diff > 0) {
|
if (diff > 0) {
|
||||||
scroll -= (y - prevScrollY) / static_cast<float>(size.y) * actualLength;
|
scroll -= (y - prevScrollY) / static_cast<float>(size.y) * actualLength;
|
||||||
|
|
@ -69,7 +77,7 @@ void Container::mouseMove(int x, int y) {
|
||||||
|
|
||||||
void Container::mouseRelease(int x, int y) {
|
void Container::mouseRelease(int x, int y) {
|
||||||
UINode::mouseRelease(x, y);
|
UINode::mouseRelease(x, y);
|
||||||
prevScrollY = -1;
|
scrollbarTriggered = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Container::act(float delta) {
|
void Container::act(float delta) {
|
||||||
|
|
|
||||||
|
|
@ -9,6 +9,7 @@
|
||||||
namespace gui {
|
namespace gui {
|
||||||
class Container : public UINode, public ::util::ObjectsKeeper {
|
class Container : public UINode, public ::util::ObjectsKeeper {
|
||||||
int prevScrollY = -1;
|
int prevScrollY = -1;
|
||||||
|
bool scrollbarTriggered = false;
|
||||||
protected:
|
protected:
|
||||||
std::vector<std::shared_ptr<UINode>> nodes;
|
std::vector<std::shared_ptr<UINode>> nodes;
|
||||||
std::vector<IntervalEvent> intervalEvents;
|
std::vector<IntervalEvent> intervalEvents;
|
||||||
|
|
@ -18,13 +19,14 @@ namespace gui {
|
||||||
int actualLength = 0;
|
int actualLength = 0;
|
||||||
bool scrollable = true;
|
bool scrollable = true;
|
||||||
|
|
||||||
bool isScrolling() {
|
bool isScrolling() const {
|
||||||
return prevScrollY != -1;
|
return scrollbarTriggered;
|
||||||
}
|
}
|
||||||
public:
|
public:
|
||||||
Container(GUI& gui, glm::vec2 size);
|
Container(GUI& gui, glm::vec2 size);
|
||||||
virtual ~Container();
|
virtual ~Container();
|
||||||
|
|
||||||
|
virtual void click(int, int) override;
|
||||||
virtual void act(float delta) override;
|
virtual void act(float delta) override;
|
||||||
virtual void drawBackground(const DrawContext& pctx, const Assets& assets);
|
virtual void drawBackground(const DrawContext& pctx, const Assets& assets);
|
||||||
virtual void draw(const DrawContext& pctx, const Assets& assets) override;
|
virtual void draw(const DrawContext& pctx, const Assets& assets) override;
|
||||||
|
|
|
||||||
|
|
@ -648,7 +648,7 @@ void TextBox::refresh() {
|
||||||
label->setSize(
|
label->setSize(
|
||||||
size - glm::vec2(
|
size - glm::vec2(
|
||||||
padding.z + padding.x +
|
padding.z + padding.x +
|
||||||
LINE_NUMBERS_PANE_WIDTH * showLineNumbers + textOffset,
|
LINE_NUMBERS_PANE_WIDTH * showLineNumbers - textOffset,
|
||||||
padding.w + padding.y
|
padding.w + padding.y
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
|
|
@ -671,7 +671,9 @@ size_t TextBox::normalizeIndex(int index) {
|
||||||
/// @param y screen Y position
|
/// @param y screen Y position
|
||||||
/// @return non-normalized character index
|
/// @return non-normalized character index
|
||||||
int TextBox::calcIndexAt(int x, int y) const {
|
int TextBox::calcIndexAt(int x, int y) const {
|
||||||
if (!rawTextCache.metrics.font.has_value()) return 0;
|
if (!rawTextCache.metrics.font.has_value()) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
const auto& labelText = label->getText();
|
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);
|
||||||
|
|
@ -733,6 +735,7 @@ void TextBox::doubleClick(int x, int y) {
|
||||||
}
|
}
|
||||||
|
|
||||||
void TextBox::click(int x, int y) {
|
void TextBox::click(int x, int y) {
|
||||||
|
Container::click(x, y);
|
||||||
int index = normalizeIndex(calcIndexAt(x, y));
|
int index = normalizeIndex(calcIndexAt(x, y));
|
||||||
selectionStart = index;
|
selectionStart = index;
|
||||||
selectionEnd = index;
|
selectionEnd = index;
|
||||||
|
|
@ -755,15 +758,16 @@ void TextBox::resetMaxLocalCaret() {
|
||||||
caret - label->getTextLineOffset(label->getLineByTextIndex(caret));
|
caret - label->getTextLineOffset(label->getLineByTextIndex(caret));
|
||||||
}
|
}
|
||||||
|
|
||||||
void TextBox::stepLeft(bool shiftPressed, bool breakSelection) {
|
void TextBox::stepCaret(bool shiftPressed, bool breakSelection, bool right) {
|
||||||
uint previousCaret = this->caret;
|
uint previousCaret = this->caret;
|
||||||
size_t caret = breakSelection ? selectionStart : this->caret;
|
size_t caret =
|
||||||
if (caret > 0) {
|
breakSelection ? (right ? selectionEnd : selectionStart) : this->caret;
|
||||||
if (caret > input.length()) {
|
if (right ? (caret < input.length()) : (caret > 0)) {
|
||||||
setCaret(input.length() - 1);
|
setCaret(
|
||||||
} else {
|
right ? (caret + 1)
|
||||||
setCaret(caret - 1);
|
: ((caret > input.length()) ? (input.length() - 1)
|
||||||
}
|
: (caret - 1))
|
||||||
|
);
|
||||||
if (shiftPressed) {
|
if (shiftPressed) {
|
||||||
if (selectionStart == selectionEnd) {
|
if (selectionStart == selectionEnd) {
|
||||||
selectionOrigin = previousCaret;
|
selectionOrigin = previousCaret;
|
||||||
|
|
@ -774,28 +778,9 @@ void TextBox::stepLeft(bool shiftPressed, bool breakSelection) {
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
setCaret(caret);
|
setCaret(caret);
|
||||||
resetSelection();
|
if (!shiftPressed) {
|
||||||
}
|
|
||||||
resetMaxLocalCaret();
|
|
||||||
}
|
|
||||||
|
|
||||||
void TextBox::stepRight(bool shiftPressed, bool breakSelection) {
|
|
||||||
uint previousCaret = this->caret;
|
|
||||||
size_t caret = breakSelection ? selectionEnd : this->caret;
|
|
||||||
if (caret < input.length()) {
|
|
||||||
setCaret(caret + 1);
|
|
||||||
caretLastMove = gui.getWindow().time();
|
|
||||||
if (shiftPressed) {
|
|
||||||
if (selectionStart == selectionEnd) {
|
|
||||||
selectionOrigin = previousCaret;
|
|
||||||
}
|
|
||||||
extendSelection(this->caret);
|
|
||||||
} else {
|
|
||||||
resetSelection();
|
resetSelection();
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
setCaret(caret);
|
|
||||||
resetSelection();
|
|
||||||
}
|
}
|
||||||
resetMaxLocalCaret();
|
resetMaxLocalCaret();
|
||||||
}
|
}
|
||||||
|
|
@ -983,9 +968,9 @@ void TextBox::performEditingKeyboardEvents(Keycode key) {
|
||||||
} else if (key == Keycode::TAB) {
|
} else if (key == Keycode::TAB) {
|
||||||
onTab(shiftPressed);
|
onTab(shiftPressed);
|
||||||
} else if (key == Keycode::LEFT) {
|
} else if (key == Keycode::LEFT) {
|
||||||
stepLeft(shiftPressed, breakSelection);
|
stepCaret(shiftPressed, breakSelection, false);
|
||||||
} else if (key == Keycode::RIGHT) {
|
} else if (key == Keycode::RIGHT) {
|
||||||
stepRight(shiftPressed, breakSelection);
|
stepCaret(shiftPressed, breakSelection, true);
|
||||||
} else if (key == Keycode::UP && onUpPressed) {
|
} else if (key == Keycode::UP && onUpPressed) {
|
||||||
onUpPressed();
|
onUpPressed();
|
||||||
} else if (key == Keycode::DOWN && onDownPressed) {
|
} else if (key == Keycode::DOWN && onDownPressed) {
|
||||||
|
|
@ -1218,7 +1203,7 @@ void TextBox::setCaret(size_t position) {
|
||||||
if (font == nullptr) {
|
if (font == nullptr) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
int width = label->getSize().x;
|
int width = size.x - padding.x - padding.z - LINE_NUMBERS_PANE_WIDTH * showLineNumbers;
|
||||||
|
|
||||||
rawTextCache.prepare(font, rawTextCache.metrics, width);
|
rawTextCache.prepare(font, rawTextCache.metrics, width);
|
||||||
rawTextCache.update(input, multiline, label->isTextWrapping());
|
rawTextCache.update(input, multiline, label->isTextWrapping());
|
||||||
|
|
@ -1239,12 +1224,12 @@ void TextBox::setCaret(size_t position) {
|
||||||
}
|
}
|
||||||
int lcaret = caret - rawTextCache.getTextLineOffset(line);
|
int lcaret = caret - rawTextCache.getTextLineOffset(line);
|
||||||
int realoffset = rawTextCache.metrics.calcWidth(labelText, 0, lcaret) -
|
int realoffset = rawTextCache.metrics.calcWidth(labelText, 0, lcaret) -
|
||||||
static_cast<int>(textOffset) + 2;
|
static_cast<int>(textOffset);
|
||||||
|
|
||||||
if (realoffset - width > 0) {
|
if (realoffset - width > 0) {
|
||||||
setTextOffset(textOffset + realoffset - width);
|
setTextOffset(textOffset + realoffset - width);
|
||||||
} else if (realoffset < 0) {
|
} else if (realoffset < 0) {
|
||||||
setTextOffset(std::max(textOffset + realoffset, static_cast<size_t>(0)));
|
setTextOffset(std::max<int>(textOffset + realoffset - static_cast<int>(padding.x + padding.w), 0));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -66,8 +66,7 @@ namespace gui {
|
||||||
std::string markup;
|
std::string markup;
|
||||||
std::string syntax;
|
std::string syntax;
|
||||||
|
|
||||||
void stepLeft(bool shiftPressed, bool breakSelection);
|
void stepCaret(bool shiftPressed, bool breakSelection, bool right);
|
||||||
void stepRight(bool shiftPressed, bool breakSelection);
|
|
||||||
void stepDefaultDown(bool shiftPressed, bool breakSelection);
|
void stepDefaultDown(bool shiftPressed, bool breakSelection);
|
||||||
void stepDefaultUp(bool shiftPressed, bool breakSelection);
|
void stepDefaultUp(bool shiftPressed, bool breakSelection);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -130,11 +130,15 @@ void BlocksController::updateBlock(int x, int y, int z) {
|
||||||
}
|
}
|
||||||
|
|
||||||
void BlocksController::update(float delta, uint padding) {
|
void BlocksController::update(float delta, uint padding) {
|
||||||
if (randTickClock.update(delta)) {
|
if (int parts = randTickClock.update(delta)) {
|
||||||
randomTick(randTickClock.getPart(), randTickClock.getParts(), padding);
|
for (int i = 0; i < parts; i++) {
|
||||||
|
randomTick(randTickClock.convertPart(i), randTickClock.getParts(), padding);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if (blocksTickClock.update(delta)) {
|
if (int parts = blocksTickClock.update(delta)) {
|
||||||
onBlocksTick(blocksTickClock.getTickId(), blocksTickClock.getParts());
|
for (int i = 0; i < parts; i++) {
|
||||||
|
onBlocksTick(blocksTickClock.convertPart(i), blocksTickClock.getParts());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if (worldTickClock.update(delta)) {
|
if (worldTickClock.update(delta)) {
|
||||||
scripting::on_world_tick(worldTickClock.getTickRate());
|
scripting::on_world_tick(worldTickClock.getTickRate());
|
||||||
|
|
|
||||||
|
|
@ -104,10 +104,12 @@ void LevelController::update(float delta, bool pause) {
|
||||||
if (player->isSuspended()) {
|
if (player->isSuspended()) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if (playerTickClock.update(delta)) {
|
if (int parts = playerTickClock.update(delta)) {
|
||||||
if (player->getId() % playerTickClock.getParts() ==
|
for (int i = 0; i < parts; i++) {
|
||||||
playerTickClock.getPart()) {
|
if (player->getId() % playerTickClock.getParts() !=
|
||||||
|
playerTickClock.convertPart(i)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
const auto& position = player->getPosition();
|
const auto& position = player->getPosition();
|
||||||
if (player->chunks->get(
|
if (player->chunks->get(
|
||||||
std::floor(position.x),
|
std::floor(position.x),
|
||||||
|
|
@ -119,6 +121,7 @@ void LevelController::update(float delta, bool pause) {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -288,22 +288,24 @@ void Entities::preparePhysics(float delta) {
|
||||||
auto& hitboxes = physics.getHitboxesWriteable();
|
auto& hitboxes = physics.getHitboxesWriteable();
|
||||||
auto& solidHitboxes = physics.getSolidHitboxesWriteable();
|
auto& solidHitboxes = physics.getSolidHitboxesWriteable();
|
||||||
|
|
||||||
if (sensorsTickClock.update(delta)) {
|
if (int parts = sensorsTickClock.update(delta)) {
|
||||||
auto part = sensorsTickClock.getPart();
|
for (int i = 0; i < parts; i++) {
|
||||||
auto parts = sensorsTickClock.getParts();
|
auto part = sensorsTickClock.convertPart(i);
|
||||||
|
auto allParts = sensorsTickClock.getParts();
|
||||||
|
|
||||||
auto& sensors = physics.getSensorsWriteable();
|
auto& sensors = physics.getSensorsWriteable();
|
||||||
sensors.clear();
|
sensors.clear();
|
||||||
|
|
||||||
auto view = registry->view<EntityId, Transform, Rigidbody>();
|
auto view = registry->view<EntityId, Transform, Rigidbody>();
|
||||||
for (auto [entity, eid, transform, rigidbody] : view.each()) {
|
for (auto [entity, eid, transform, rigidbody] : view.each()) {
|
||||||
if (!rigidbody.enabled) {
|
if (!rigidbody.enabled) {
|
||||||
continue;
|
continue;
|
||||||
|
}
|
||||||
|
if ((eid.uid + part) % allParts != 0) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
updateSensors(rigidbody, transform, sensors);
|
||||||
}
|
}
|
||||||
if ((eid.uid + part) % parts != 0) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
updateSensors(rigidbody, transform, sensors);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -364,12 +366,14 @@ void Entities::updatePhysics(float delta) {
|
||||||
}
|
}
|
||||||
|
|
||||||
void Entities::update(float delta) {
|
void Entities::update(float delta) {
|
||||||
if (updateTickClock.update(delta)) {
|
if (int parts = updateTickClock.update(delta)) {
|
||||||
scripting::on_entities_update(
|
for (int i = 0; i < parts; i++) {
|
||||||
updateTickClock.getTickRate(),
|
scripting::on_entities_update(
|
||||||
updateTickClock.getParts(),
|
updateTickClock.getTickRate(),
|
||||||
updateTickClock.getPart()
|
updateTickClock.getParts(),
|
||||||
);
|
updateTickClock.convertPart(i)
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
updatePhysics(delta);
|
updatePhysics(delta);
|
||||||
scripting::on_entities_physics_update(delta);
|
scripting::on_entities_physics_update(delta);
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
#include "Clock.hpp"
|
#include "Clock.hpp"
|
||||||
|
|
||||||
#include <cmath>
|
#include <cmath>
|
||||||
|
#include <algorithm>
|
||||||
|
|
||||||
using namespace util;
|
using namespace util;
|
||||||
|
|
||||||
|
|
@ -8,30 +9,28 @@ Clock::Clock(int tickRate, int tickParts)
|
||||||
: tickRate(tickRate), tickParts(tickParts) {
|
: tickRate(tickRate), tickParts(tickParts) {
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Clock::update(float delta) {
|
int Clock::update(float delta) {
|
||||||
tickTimer += delta;
|
tickTimer += delta;
|
||||||
float delay = 1.0f / static_cast<float>(tickRate);
|
float delay = 1.0f / static_cast<float>(tickRate);
|
||||||
if (tickTimer > delay || tickPartsUndone) {
|
if (tickTimer < delay / tickParts) {
|
||||||
if (tickPartsUndone) {
|
return 0;
|
||||||
tickPartsUndone--;
|
|
||||||
} else {
|
|
||||||
tickTimer = std::fmod(tickTimer, delay);
|
|
||||||
tickPartsUndone = tickParts - 1;
|
|
||||||
}
|
|
||||||
++tickId;
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
return false;
|
int parts = tickTimer / (delay / tickParts);
|
||||||
|
if (parts) {
|
||||||
|
tickTimer -= parts * delay / tickParts;
|
||||||
|
tickTimer = std::min<float>(tickTimer, delay);
|
||||||
|
}
|
||||||
|
currentTickPart += parts;
|
||||||
|
if (currentTickPart >= tickParts) {
|
||||||
|
currentTickPart %= tickParts;
|
||||||
|
}
|
||||||
|
return parts;
|
||||||
}
|
}
|
||||||
|
|
||||||
int Clock::getParts() const {
|
int Clock::getParts() const {
|
||||||
return tickParts;
|
return tickParts;
|
||||||
}
|
}
|
||||||
|
|
||||||
int Clock::getPart() const {
|
|
||||||
return tickParts - tickPartsUndone - 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
int Clock::getTickRate() const {
|
int Clock::getTickRate() const {
|
||||||
return tickRate;
|
return tickRate;
|
||||||
}
|
}
|
||||||
|
|
@ -39,3 +38,7 @@ int Clock::getTickRate() const {
|
||||||
int Clock::getTickId() const {
|
int Clock::getTickId() const {
|
||||||
return tickId;
|
return tickId;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int Clock::convertPart(int index) const {
|
||||||
|
return (tickParts - currentTickPart) % tickParts + index;
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -7,15 +7,15 @@ namespace util {
|
||||||
|
|
||||||
float tickTimer = 0.0f;
|
float tickTimer = 0.0f;
|
||||||
int tickId = 0;
|
int tickId = 0;
|
||||||
int tickPartsUndone = 0;
|
int currentTickPart = 0;
|
||||||
public:
|
public:
|
||||||
Clock(int tickRate, int tickParts);
|
Clock(int tickRate, int tickParts);
|
||||||
|
|
||||||
bool update(float delta);
|
int update(float delta);
|
||||||
|
|
||||||
int getParts() const;
|
int getParts() const;
|
||||||
int getPart() const;
|
|
||||||
int getTickRate() const;
|
int getTickRate() const;
|
||||||
int getTickId() const;
|
int getTickId() const;
|
||||||
|
int convertPart(int index) const;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue