Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
sekta2 committed Jan 28, 2025
2 parents 67e286d + f2ad1ba commit c4fafbd
Show file tree
Hide file tree
Showing 12 changed files with 73 additions and 33 deletions.
15 changes: 13 additions & 2 deletions src/graphics/ui/GUI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "elements/UINode.hpp"
#include "elements/Label.hpp"
#include "elements/Menu.hpp"
#include "elements/Panel.hpp"

#include "assets/Assets.hpp"
#include "frontend/UiDocument.hpp"
Expand All @@ -23,8 +24,10 @@

using namespace gui;

GUI::GUI() : batch2D(std::make_unique<Batch2D>(1024)) {
container = std::make_shared<Container>(glm::vec2(1000));
GUI::GUI()
: batch2D(std::make_unique<Batch2D>(1024)),
container(std::make_shared<Container>(glm::vec2(1000))) {
container->setId("root");
uicamera = std::make_unique<Camera>(glm::vec3(), Window::height);
uicamera->perspective = false;
uicamera->flipped = true;
Expand Down Expand Up @@ -214,6 +217,14 @@ void GUI::draw(const DrawContext& pctx, const Assets& assets) {
auto& viewport = ctx.getViewport();
glm::vec2 wsize = viewport.size();

auto& page = menu->getCurrent();
if (page.panel) {
menu->setSize(page.panel->getSize());
page.panel->refresh();
if (auto panel = std::dynamic_pointer_cast<gui::Panel>(page.panel)) {
panel->cropToContent();
}
}
menu->setPos((wsize - menu->getSize()) / 2.0f);
uicamera->setFov(wsize.y);

Expand Down
8 changes: 5 additions & 3 deletions src/graphics/ui/elements/Menu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,10 @@ void Menu::setPage(Page page, bool history) {
setSize(current.panel->getSize());
}

void Menu::back() {
if (pageStack.empty())
return;
bool Menu::back() {
if (pageStack.empty()) {
return false;
}
Page page = pageStack.top();
pageStack.pop();

Expand All @@ -77,6 +78,7 @@ void Menu::back() {
}

setPage(page, false);
return true;
}

void Menu::setPageLoader(PageLoaderFunc loader) {
Expand Down
2 changes: 1 addition & 1 deletion src/graphics/ui/elements/Menu.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ namespace gui {
PageLoaderFunc getPageLoader();

/// @brief Set page to previous saved in history
void back();
bool back();

/// @brief Clear pages history
void clearHistory();
Expand Down
14 changes: 6 additions & 8 deletions src/graphics/ui/elements/Panel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,30 +70,28 @@ void Panel::remove(const std::shared_ptr<UINode> &node) {

void Panel::refresh() {
UINode::refresh();
std::stable_sort(nodes.begin(), nodes.end(), [](auto a, auto b) {
return a->getZIndex() < b->getZIndex();
});

float x = padding.x;
float y = padding.y;
glm::vec2 size = getSize();
if (orientation == Orientation::vertical) {
float maxw = size.x;
for (auto& node : nodes) {
glm::vec2 nodesize = node->getSize();
const glm::vec4 margin = node->getMargin();
y += margin.y;

float ex = x + margin.x;
node->setPos(glm::vec2(ex, y));
y += nodesize.y + margin.w + interval;


float width = size.x - padding.x - padding.z - margin.x - margin.z;
if (node->isResizing()) {
node->setSize(glm::vec2(width, nodesize.y));
node->setMaxSize({width, node->getMaxSize().y});
node->setSize(glm::vec2(width, node->getSize().y));
}
node->refresh();
maxw = fmax(maxw, ex+node->getSize().x+margin.z+padding.z);
glm::vec2 nodeSize = node->getSize();
y += nodeSize.y + margin.w + interval;
maxw = fmax(maxw, ex+nodeSize.x+margin.z+padding.z);
}
actualLength = y + padding.w;
} else {
Expand Down
12 changes: 11 additions & 1 deletion src/graphics/ui/elements/UINode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,8 @@ glm::vec2 UINode::getSize() const {

void UINode::setSize(glm::vec2 size) {
this->size = glm::vec2(
glm::max(minSize.x, size.x), glm::max(minSize.y, size.y)
glm::max(minSize.x, glm::min(maxSize.x, size.x)),
glm::max(minSize.y, glm::min(maxSize.y, size.y))
);
}

Expand All @@ -208,6 +209,15 @@ void UINode::setMinSize(glm::vec2 minSize) {
setSize(getSize());
}

glm::vec2 UINode::getMaxSize() const {
return maxSize;
}

void UINode::setMaxSize(glm::vec2 maxSize) {
this->maxSize = maxSize;
setSize(getSize());
}

void UINode::setColor(glm::vec4 color) {
this->color = color;
this->hoverColor = color;
Expand Down
4 changes: 4 additions & 0 deletions src/graphics/ui/elements/UINode.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ namespace gui {
glm::vec2 size;
/// @brief minimal element size
glm::vec2 minSize {1.0f};
/// @brief maximal element size
glm::vec2 maxSize {1e6f};
/// @brief element primary color (background-color or text-color if label)
glm::vec4 color {1.0f};
/// @brief element color when mouse is over it
Expand Down Expand Up @@ -224,6 +226,8 @@ namespace gui {
virtual void setSize(glm::vec2 size);
virtual glm::vec2 getMinSize() const;
virtual void setMinSize(glm::vec2 size);
virtual glm::vec2 getMaxSize() const;
virtual void setMaxSize(glm::vec2 size);
/// @brief Called in containers when new element added
virtual void refresh() {};
virtual void fullRefresh() {
Expand Down
18 changes: 15 additions & 3 deletions src/graphics/ui/gui_util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,17 @@ void guiutil::alert(
const std::wstring& text,
const runnable& on_hidden
) {
auto panel = std::make_shared<Panel>(glm::vec2(500, 300), glm::vec4(4.0f), 4.0f);
auto panel = std::make_shared<Panel>(
glm::vec2(
glm::min(
static_cast<size_t>(650),
glm::max(text.length() * 10, static_cast<size_t>(200))
),
300
),
glm::vec4(4.0f),
4.0f
);
panel->setColor(glm::vec4(0.0f, 0.0f, 0.0f, 0.5f));

auto menuPtr = engine.getGUI()->getMenu();
Expand All @@ -40,14 +50,15 @@ void guiutil::alert(
menu.removePage("<alert>");
if (on_hidden) {
on_hidden();
} else {
menu.back();
} else if (!menu.back()) {
menu.reset();
}
};

auto label = std::make_shared<Label>(text);
label->setMultiline(true);
label->setSize(glm::vec2(1, 24));
label->setAutoResize(true);
panel->add(label);
panel->add(std::make_shared<Button>(
langs::get(L"Ok"), glm::vec4(10.f),
Expand All @@ -56,6 +67,7 @@ void guiutil::alert(
}
));
panel->refresh();

panel->keepAlive(Events::keyCallbacks[keycode::ENTER].add([on_hidden_final](){
on_hidden_final();
return true;
Expand Down
1 change: 1 addition & 0 deletions src/logic/scripting/lua/lua_custom_types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ namespace lua {
public:
LuaBytearray(size_t capacity);
LuaBytearray(std::vector<ubyte> buffer);
LuaBytearray(const ubyte* data, size_t size);
virtual ~LuaBytearray();

const std::string& getTypeName() const override {
Expand Down
8 changes: 1 addition & 7 deletions src/logic/scripting/lua/lua_util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,7 @@ int lua::pushvalue(State* L, const dv::value& value) {
break;
case value_type::bytes: {
const auto& bytes = value.asBytes();
createtable(L, 0, bytes.size());
size_t size = bytes.size();
for (size_t i = 0; i < size;) {
pushinteger(L, bytes[i]);
i++;
rawseti(L, i);
}
newuserdata<LuaBytearray>(L, bytes.data(), bytes.size());
break;
}
}
Expand Down
3 changes: 3 additions & 0 deletions src/logic/scripting/lua/usertypes/lua_type_bytearray.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ LuaBytearray::LuaBytearray(size_t capacity) : buffer(capacity) {
LuaBytearray::LuaBytearray(std::vector<ubyte> buffer) : buffer(std::move(buffer)) {
}

LuaBytearray::LuaBytearray(const ubyte* data, size_t size) : buffer(data, data + size) {
}

LuaBytearray::~LuaBytearray() {
}

Expand Down
9 changes: 5 additions & 4 deletions src/network/Network.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,6 @@ class SocketConnection : public Connection {
~SocketConnection() {
if (state != ConnectionState::CLOSED) {
shutdown(descriptor, 2);
closesocket(descriptor);
}
if (thread) {
thread->join();
Expand Down Expand Up @@ -382,6 +381,9 @@ class SocketConnection : public Connection {
}

int send(const char* buffer, size_t length) override {
if (state == ConnectionState::CLOSED) {
return 0;
}
int len = sendsocket(descriptor, buffer, length, 0);
if (len == -1) {
int err = errno;
Expand Down Expand Up @@ -444,20 +446,19 @@ class SocketConnection : public Connection {
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;

addrinfo* addrinfo;
addrinfo* addrinfo = nullptr;
if (int res = getaddrinfo(
address.c_str(), nullptr, &hints, &addrinfo
)) {
throw std::runtime_error(gai_strerror(res));
}

sockaddr_in serverAddress = *(sockaddr_in*)addrinfo->ai_addr;
freeaddrinfo(addrinfo);
serverAddress.sin_port = htons(port);
freeaddrinfo(addrinfo);

SOCKET descriptor = socket(AF_INET, SOCK_STREAM, 0);
if (descriptor == -1) {
freeaddrinfo(addrinfo);
throw std::runtime_error("Could not create socket");
}
auto socket = std::make_shared<SocketConnection>(descriptor, std::move(serverAddress));
Expand Down
12 changes: 8 additions & 4 deletions src/util/platform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,17 +65,21 @@ int platform::get_process_id() {
#else // _WIN32

#include <unistd.h>
#include "frontend/locale.hpp"

void platform::configure_encoding() {
}

std::string platform::detect_locale() {
std::string programLocaleName = setlocale(LC_ALL, nullptr);
std::string preferredLocaleName =
const char* const programLocaleName = setlocale(LC_ALL, nullptr);
const char* const preferredLocaleName =
setlocale(LC_ALL, ""); // locale name format: ll_CC.encoding
setlocale(LC_ALL, programLocaleName.c_str());
if (programLocaleName && preferredLocaleName) {
setlocale(LC_ALL, programLocaleName);

return preferredLocaleName.substr(0, 5);
return std::string(preferredLocaleName, 5);
}
return langs::FALLBACK_DEFAULT;
}

void platform::sleep(size_t millis) {
Expand Down

0 comments on commit c4fafbd

Please sign in to comment.