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