This commit is contained in:
A-lex-Ra 2024-02-14 10:11:42 +06:00
commit de3e3e0fe4
31 changed files with 774 additions and 345 deletions

View File

@ -1,3 +1,26 @@
<inventory color="#1F1F1FE0" size="400,0">
<inventory color="#1F1F1FE0" size="0,600">
<slots-grid rows="4" count="40" sharefunc="inventory_share_func"/>
<panel coord="10,230" padding="4,4,4,4" size="300,100">
<label>fps: 62 / 59</label>
<label>meshes: 4143</label>
<label>frustum-culling: on</label>
<label>chunks: 3206 visible: 1153</label>
<label>block: 0 0 (core:air)</label>
<label>seed: 42</label>
<container size="200,30">
<label size="100,30">x:</label>
<textbox coord="40,0">4321.40</textbox>
</container>
<container size="200,30">
<label size="100,30">y:</label>
<textbox coord="40,0">120.0</textbox>
</container>
<container size="200,30">
<label size="100,30">z:</label>
<textbox coord="40,0">-424.10</textbox>
</container>
<label>time: 19:50</label>
<trackbar min="0.0" max="1.0" step="0.005f" track-width="8" consumer="time_change"/>
<trackbar min="0.0" max="1.0" step="0.005f" value="0.5" track-width="8"/>
</panel>
</inventory>

View File

@ -1,11 +1,15 @@
function on_open(inv)
print("OPEN", inv)
function on_open(invid)
print("OPEN", invid)
end
function on_close(inv)
print("CLOSE", inv)
function on_close(invid)
print("CLOSE", invid)
end
function inventory_share_func(invid, slotid)
inventory.set(invid, slotid, 0, 0)
end
function time_change(x)
world.set_day_time(x)
end

0
res/modules/document.lua Normal file
View File

View File

@ -91,3 +91,46 @@ end
function pack.is_installed(packid)
return file.isfile(packid..":package.json")
end
vec2_mt = {}
function vec2_mt.__tostring(self)
return "vec2("..self[1]..", "..self[2]..")"
end
vec3_mt = {}
function vec3_mt.__tostring(self)
return "vec3("..self[1]..", "..self[2]..", "..self[3]..")"
end
vec4_mt = {}
function vec4_mt.__tostring(self)
return "vec4("..self[1]..", "..self[2]..", "..self[3]..", "..self[4]..")"
end
color_mt = {}
function color_mt.__tostring(self)
return "rgba("..self[1]..", "..self[2]..", "..self[3]..", "..self[4]..")"
end
-- class designed for simple UI-nodes access via properties syntax
local Element = {}
function Element.new(docname, name)
return setmetatable({docname=docname, name=name}, {
__index=function(self, k)
return gui.getattr(self.docname, self.name, k)
end,
__newindex=function(self, k, v)
gui.setattr(self.docname, self.name, k, v)
end
})
end
-- the engine automatically creates an instance for every ui document (layout)
Document = {}
function Document.new(docname)
return setmetatable({name=docname}, {
__index=function(self, k)
return Element.new(self.name, k)
end
})
end

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.1 KiB

View File

@ -76,6 +76,7 @@ void AssetsLoader::addDefaults(AssetsLoader& loader, const Content* content) {
loader.add(ASSET_SHADER, SHADERS_FOLDER"/skybox_gen", "skybox_gen");
loader.add(ASSET_TEXTURE, TEXTURES_FOLDER"/misc/moon.png", "misc/moon");
loader.add(ASSET_TEXTURE, TEXTURES_FOLDER"/misc/sun.png", "misc/sun");
loader.add(ASSET_TEXTURE, TEXTURES_FOLDER"/gui/crosshair.png", "gui/crosshair");
addLayouts(0, "core", loader.getPaths()->getMainRoot()/fs::path("layouts"), loader);
for (auto& pack : content->getPacks()) {

View File

@ -8,5 +8,10 @@ using runnable = std::function<void()>;
using stringconsumer = std::function<void(const std::string&)>;
using wstringsupplier = std::function<std::wstring()>;
using wstringconsumer = std::function<void(const std::wstring&)>;
using doublesupplier = std::function<double()>;
using doubleconsumer = std::function<void(double)>;
using boolsupplier = std::function<bool()>;
using boolconsumer = std::function<void(bool)>;
using wstringchecker = std::function<bool(const std::wstring&)>;
#endif // DELEGATES_H_

View File

@ -208,6 +208,10 @@ void Engine::loadAllPacks() {
ContentPack::scan(paths, contentPacks);
}
double Engine::getDelta() const {
return delta;
}
void Engine::setScreen(std::shared_ptr<Screen> screen) {
this->screen = screen;
}

View File

@ -63,6 +63,7 @@ public:
void loadContent();
void loadWorldContent(const fs::path& folder);
void loadAllPacks();
double getDelta() const;
std::shared_ptr<Screen> getScreen();
};

View File

@ -30,6 +30,14 @@ const std::shared_ptr<gui::UINode> UiDocument::getRoot() const {
return root;
}
const std::shared_ptr<gui::UINode> UiDocument::get(const std::string& id) const {
auto found = map.find(id);
if (found == map.end()) {
return nullptr;
}
return found->second;
}
const uidocscript& UiDocument::getScript() const {
return script;
}

View File

@ -43,6 +43,7 @@ public:
const std::string& getId() const;
const uinodes_map& getMap() const;
const std::shared_ptr<gui::UINode> getRoot() const;
const std::shared_ptr<gui::UINode> get(const std::string& id) const;
const uidocscript& getScript() const;
int getEnvironment() const;
/* Collect map of all uinodes having identifiers */

View File

@ -110,6 +110,10 @@ void UINode::setCoord(glm::vec2 coord) {
this->coord = coord;
}
glm::vec2 UINode::getCoord() const {
return coord;
}
glm::vec2 UINode::getSize() const {
return size;
}

View File

@ -112,6 +112,7 @@ namespace gui {
/* Calculate screen position of the element */
virtual glm::vec2 calcCoord() const;
virtual void setCoord(glm::vec2 coord);
glm::vec2 getCoord() const;
virtual glm::vec2 getSize() const;
virtual void setSize(glm::vec2 size);
virtual void refresh() {};

View File

@ -10,24 +10,20 @@
#include "../../util/stringutil.h"
#include "GUI.h"
using glm::vec2;
using glm::vec3;
using glm::vec4;
using namespace gui;
Label::Label(std::string text, std::string fontName)
: UINode(vec2(), vec2(text.length() * 8, 15)),
: UINode(glm::vec2(), glm::vec2(text.length() * 8, 15)),
text(util::str2wstr_utf8(text)),
fontName_(fontName) {
fontName(fontName) {
setInteractive(false);
}
Label::Label(std::wstring text, std::string fontName)
: UINode(vec2(), vec2(text.length() * 8, 15)),
: UINode(glm::vec2(), glm::vec2(text.length() * 8, 15)),
text(text),
fontName_(fontName) {
fontName(fontName) {
setInteractive(false);
}
@ -46,14 +42,14 @@ void Label::draw(const GfxContext* pctx, Assets* assets) {
auto batch = pctx->getBatch2D();
batch->color = getColor();
Font* font = assets->getFont(fontName_);
vec2 size = getSize();
vec2 newsize = vec2(
Font* font = assets->getFont(fontName);
glm::vec2 size = getSize();
glm::vec2 newsize (
font->calcWidth(text),
font->getLineHeight()+font->getYOffset()
);
vec2 coord = calcCoord();
glm::vec2 coord = calcCoord();
switch (align) {
case Align::left:
break;
@ -74,13 +70,13 @@ Label* Label::textSupplier(wstringsupplier supplier) {
}
// ================================= Image ====================================
Image::Image(std::string texture, vec2 size) : UINode(vec2(), size), texture(texture) {
Image::Image(std::string texture, glm::vec2 size) : UINode(glm::vec2(), size), texture(texture) {
setInteractive(false);
}
void Image::draw(const GfxContext* pctx, Assets* assets) {
vec2 coord = calcCoord();
vec4 color = getColor();
glm::vec2 coord = calcCoord();
glm::vec4 color = getColor();
auto batch = pctx->getBatch2D();
batch->texture(assets->getTexture(texture));
batch->color = color;
@ -90,10 +86,11 @@ void Image::draw(const GfxContext* pctx, Assets* assets) {
// ================================= Button ===================================
Button::Button(std::shared_ptr<UINode> content, glm::vec4 padding)
: Panel(vec2(), padding, 0) {
vec4 margin = getMargin();
setSize(content->getSize()+vec2(padding[0]+padding[2]+margin[0]+margin[2],
padding[1]+padding[3]+margin[1]+margin[3]));
: Panel(glm::vec2(), padding, 0) {
glm::vec4 margin = getMargin();
setSize(content->getSize()+
glm::vec2(padding[0]+padding[2]+margin[0]+margin[2],
padding[1]+padding[3]+margin[1]+margin[3]));
add(content);
setScrollable(false);
setHoverColor(glm::vec4(0.05f, 0.1f, 0.15f, 0.75f));
@ -102,13 +99,13 @@ Button::Button(std::shared_ptr<UINode> content, glm::vec4 padding)
Button::Button(
std::wstring text,
vec4 padding,
glm::vec4 padding,
onaction action,
vec2 size
glm::vec2 size
) : Panel(size, padding, 0)
{
if (size.y < 0.0f) {
size = vec2(
size = glm::vec2(
glm::max(padding.x + padding.z + text.length()*8, size.x),
glm::max(padding.y + padding.w + 16, size.y)
);
@ -122,7 +119,7 @@ Button::Button(
label = std::make_shared<Label>(text);
label->setAlign(Align::center);
label->setSize(size-vec2(padding.z+padding.x, padding.w+padding.y));
label->setSize(size-glm::vec2(padding.z+padding.x, padding.w+padding.y));
label->setInteractive(false);
add(label);
setHoverColor(glm::vec4(0.05f, 0.1f, 0.15f, 0.75f));
@ -151,12 +148,12 @@ Button* Button::textSupplier(wstringsupplier supplier) {
void Button::refresh() {
Panel::refresh();
if (label) {
label->setSize(size-vec2(padding.z+padding.x, padding.w+padding.y));
label->setSize(size-glm::vec2(padding.z+padding.x, padding.w+padding.y));
}
}
void Button::drawBackground(const GfxContext* pctx, Assets* assets) {
vec2 coord = calcCoord();
glm::vec2 coord = calcCoord();
auto batch = pctx->getBatch2D();
batch->texture(nullptr);
batch->color = (isPressed() ? pressedColor : (hover ? hoverColor : color));
@ -165,7 +162,7 @@ void Button::drawBackground(const GfxContext* pctx, Assets* assets) {
void Button::mouseRelease(GUI* gui, int x, int y) {
UINode::mouseRelease(gui, x, y);
if (isInside(vec2(x, y))) {
if (isInside(glm::vec2(x, y))) {
for (auto callback : actions) {
callback(gui);
}
@ -192,13 +189,13 @@ Align Button::getTextAlign() const {
}
// ============================== RichButton ==================================
RichButton::RichButton(vec2 size) : Container(vec2(), size) {
RichButton::RichButton(glm::vec2 size) : Container(glm::vec2(), size) {
setHoverColor(glm::vec4(0.05f, 0.1f, 0.15f, 0.75f));
}
void RichButton::mouseRelease(GUI* gui, int x, int y) {
UINode::mouseRelease(gui, x, y);
if (isInside(vec2(x, y))) {
if (isInside(glm::vec2(x, y))) {
for (auto callback : actions) {
callback(gui);
}
@ -211,7 +208,7 @@ RichButton* RichButton::listenAction(onaction action) {
}
void RichButton::drawBackground(const GfxContext* pctx, Assets* assets) {
vec2 coord = calcCoord();
glm::vec2 coord = calcCoord();
auto batch = pctx->getBatch2D();
batch->texture(nullptr);
batch->color = (isPressed() ? pressedColor : (hover ? hoverColor : color));
@ -219,18 +216,18 @@ void RichButton::drawBackground(const GfxContext* pctx, Assets* assets) {
}
// ================================ TextBox ===================================
TextBox::TextBox(std::wstring placeholder, vec4 padding)
: Panel(vec2(200,32), padding, 0),
TextBox::TextBox(std::wstring placeholder, glm::vec4 padding)
: Panel(glm::vec2(200,32), padding, 0),
input(L""),
placeholder(placeholder) {
label = std::make_shared<Label>(L"");
label->setSize(size-vec2(padding.z+padding.x, padding.w+padding.y));
label->setSize(size-glm::vec2(padding.z+padding.x, padding.w+padding.y));
add(label);
setHoverColor(glm::vec4(0.05f, 0.1f, 0.2f, 0.75f));
}
void TextBox::drawBackground(const GfxContext* pctx, Assets* assets) {
vec2 coord = calcCoord();
glm::vec2 coord = calcCoord();
auto batch = pctx->getBatch2D();
batch->texture(nullptr);
@ -253,10 +250,10 @@ void TextBox::drawBackground(const GfxContext* pctx, Assets* assets) {
}
if (input.empty()) {
label->setColor(vec4(0.5f));
label->setColor(glm::vec4(0.5f));
label->setText(placeholder);
} else {
label->setColor(vec4(1.0f));
label->setColor(glm::vec4(1.0f));
label->setText(input);
}
setScrollable(false);
@ -297,7 +294,7 @@ void TextBox::focus(GUI* gui) {
void TextBox::refresh() {
Panel::refresh();
label->setSize(size-vec2(padding.z+padding.x, padding.w+padding.y));
label->setSize(size-glm::vec2(padding.z+padding.x, padding.w+padding.y));
}
void TextBox::keyPressed(int key) {
@ -322,7 +319,7 @@ void TextBox::keyPressed(int key) {
}
}
std::shared_ptr<UINode> TextBox::getAt(vec2 pos, std::shared_ptr<UINode> self) {
std::shared_ptr<UINode> TextBox::getAt(glm::vec2 pos, std::shared_ptr<UINode> self) {
return UINode::getAt(pos, self);
}
@ -349,8 +346,8 @@ void TextBox::setText(std::wstring value) {
}
// ============================== InputBindBox ================================
InputBindBox::InputBindBox(Binding& binding, vec4 padding)
: Panel(vec2(100,32), padding, 0),
InputBindBox::InputBindBox(Binding& binding, glm::vec4 padding)
: Panel(glm::vec2(100,32), padding, 0),
binding(binding) {
label = std::make_shared<Label>(L"");
add(label);
@ -358,7 +355,7 @@ InputBindBox::InputBindBox(Binding& binding, vec4 padding)
}
void InputBindBox::drawBackground(const GfxContext* pctx, Assets* assets) {
vec2 coord = calcCoord();
glm::vec2 coord = calcCoord();
auto batch = pctx->getBatch2D();
batch->texture(nullptr);
batch->color = (isFocused() ? focusedColor : (hover ? hoverColor : color));
@ -386,20 +383,20 @@ TrackBar::TrackBar(double min,
double value,
double step,
int trackWidth)
: UINode(vec2(), vec2(26)),
: UINode(glm::vec2(), glm::vec2(26)),
min(min),
max(max),
value(value),
step(step),
trackWidth(trackWidth) {
setColor(vec4(0.f, 0.f, 0.f, 0.4f));
setColor(glm::vec4(0.f, 0.f, 0.f, 0.4f));
}
void TrackBar::draw(const GfxContext* pctx, Assets* assets) {
if (supplier_) {
value = supplier_();
if (supplier) {
value = supplier();
}
vec2 coord = calcCoord();
glm::vec2 coord = calcCoord();
auto batch = pctx->getBatch2D();
batch->texture(nullptr);
batch->color = (hover ? hoverColor : color);
@ -413,16 +410,16 @@ void TrackBar::draw(const GfxContext* pctx, Assets* assets) {
batch->rect(coord.x + width * t, coord.y, actualWidth, size.y);
}
void TrackBar::supplier(doublesupplier supplier) {
this->supplier_ = supplier;
void TrackBar::setSupplier(doublesupplier supplier) {
this->supplier = supplier;
}
void TrackBar::consumer(doubleconsumer consumer) {
this->consumer_ = consumer;
void TrackBar::setConsumer(doubleconsumer consumer) {
this->consumer = consumer;
}
void TrackBar::mouseMove(GUI*, int x, int y) {
vec2 coord = calcCoord();
glm::vec2 coord = calcCoord();
value = x;
value -= coord.x;
value = (value)/size.x * (max-min+trackWidth*step);
@ -430,56 +427,56 @@ void TrackBar::mouseMove(GUI*, int x, int y) {
value = (value > max) ? max : value;
value = (value < min) ? min : value;
value = (int)(value / step) * step;
if (consumer_) {
consumer_(value);
if (consumer) {
consumer(value);
}
}
// ================================ CheckBox ==================================
CheckBox::CheckBox(bool checked) : UINode(vec2(), vec2(32.0f)), checked_(checked) {
setColor(vec4(0.0f, 0.0f, 0.0f, 0.5f));
CheckBox::CheckBox(bool checked) : UINode(glm::vec2(), glm::vec2(32.0f)), checked(checked) {
setColor(glm::vec4(0.0f, 0.0f, 0.0f, 0.5f));
}
void CheckBox::draw(const GfxContext* pctx, Assets* assets) {
if (supplier_) {
checked_ = supplier_();
if (supplier) {
checked = supplier();
}
vec2 coord = calcCoord();
glm::vec2 coord = calcCoord();
auto batch = pctx->getBatch2D();
batch->texture(nullptr);
batch->color = checked_ ? checkColor : (hover ? hoverColor : color);
batch->color = checked ? checkColor : (hover ? hoverColor : color);
batch->rect(coord.x, coord.y, size.x, size.y);
}
void CheckBox::mouseRelease(GUI*, int x, int y) {
checked_ = !checked_;
if (consumer_) {
consumer_(checked_);
checked = !checked;
if (consumer) {
consumer(checked);
}
}
void CheckBox::supplier(boolsupplier supplier) {
supplier_ = supplier;
void CheckBox::setSupplier(boolsupplier supplier) {
this->supplier = supplier;
}
void CheckBox::consumer(boolconsumer consumer) {
consumer_ = consumer;
void CheckBox::setConsumer(boolconsumer consumer) {
this->consumer = consumer;
}
CheckBox* CheckBox::checked(bool flag) {
checked_ = flag;
CheckBox* CheckBox::setChecked(bool flag) {
checked = flag;
return this;
}
FullCheckBox::FullCheckBox(std::wstring text, glm::vec2 size, bool checked)
: Panel(size),
checkbox(std::make_shared<CheckBox>(checked)){
setColor(vec4(0.0f));
setColor(glm::vec4(0.0f));
setOrientation(Orientation::horizontal);
add(checkbox);
auto label = std::make_shared<Label>(text);
label->setMargin(vec4(5.f, 5.f, 0.f, 0.f));
label->setMargin(glm::vec4(5.f, 5.f, 0.f, 0.f));
add(label);
}

View File

@ -17,18 +17,10 @@ class Batch2D;
class Assets;
namespace gui {
using doublesupplier = std::function<double()>;
using doubleconsumer = std::function<void(double)>;
using boolsupplier = std::function<bool()>;
using boolconsumer = std::function<void(bool)>;
using wstringchecker = std::function<bool(const std::wstring&)>;
class Label : public UINode {
protected:
std::wstring text;
std::string fontName_;
std::string fontName;
wstringsupplier supplier = nullptr;
public:
Label(std::string text, std::string fontName="normal");
@ -148,8 +140,8 @@ namespace gui {
protected:
glm::vec4 hoverColor {0.01f, 0.02f, 0.03f, 0.5f};
glm::vec4 trackColor {1.0f, 1.0f, 1.0f, 0.4f};
doublesupplier supplier_ = nullptr;
doubleconsumer consumer_ = nullptr;
doublesupplier supplier = nullptr;
doubleconsumer consumer = nullptr;
double min;
double max;
double value;
@ -163,8 +155,8 @@ namespace gui {
int trackWidth=1);
virtual void draw(const GfxContext* pctx, Assets* assets) override;
virtual void supplier(doublesupplier supplier);
virtual void consumer(doubleconsumer consumer);
virtual void setSupplier(doublesupplier supplier);
virtual void setConsumer(doubleconsumer consumer);
virtual void mouseMove(GUI*, int x, int y) override;
};
@ -173,9 +165,9 @@ namespace gui {
protected:
glm::vec4 hoverColor {0.05f, 0.1f, 0.2f, 0.75f};
glm::vec4 checkColor {1.0f, 1.0f, 1.0f, 0.4f};
boolsupplier supplier_ = nullptr;
boolconsumer consumer_ = nullptr;
bool checked_ = false;
boolsupplier supplier = nullptr;
boolconsumer consumer = nullptr;
bool checked = false;
public:
CheckBox(bool checked=false);
@ -183,15 +175,15 @@ namespace gui {
virtual void mouseRelease(GUI*, int x, int y) override;
virtual void supplier(boolsupplier supplier);
virtual void consumer(boolconsumer consumer);
virtual void setSupplier(boolsupplier supplier);
virtual void setConsumer(boolconsumer consumer);
virtual CheckBox* checked(bool flag);
virtual CheckBox* setChecked(bool flag);
virtual bool checked() const {
if (supplier_)
return supplier_();
return checked_;
virtual bool isChecked() const {
if (supplier)
return supplier();
return checked;
}
};
@ -201,22 +193,22 @@ namespace gui {
public:
FullCheckBox(std::wstring text, glm::vec2 size, bool checked=false);
virtual void supplier(boolsupplier supplier) {
checkbox->supplier(supplier);
virtual void setSupplier(boolsupplier supplier) {
checkbox->setSupplier(supplier);
}
virtual void consumer(boolconsumer consumer) {
checkbox->consumer(consumer);
virtual void setConsumer(boolconsumer consumer) {
checkbox->setConsumer(consumer);
}
virtual void checked(bool flag) {
checkbox->checked(flag);
virtual void setChecked(bool flag) {
checkbox->setChecked(flag);
}
virtual bool checked() const {
return checkbox->checked();
virtual bool isChecked() const {
return checkbox->isChecked();
}
};
}
#endif // FRONTEND_GUI_CONTROLS_H_
#endif // FRONTEND_GUI_CONTROLS_H_

View File

@ -124,6 +124,13 @@ static std::shared_ptr<UINode> readContainer(UiXmlReader& reader, xml::xmlelemen
return container;
}
static std::shared_ptr<UINode> readPanel(UiXmlReader& reader, xml::xmlelement element) {
float interval = element->attr("interval", "2").asFloat();
auto panel = std::make_shared<Panel>(glm::vec2(), glm::vec4(), interval);
_readPanel(reader, element, *panel);
return panel;
}
static std::shared_ptr<UINode> readButton(UiXmlReader& reader, xml::xmlelement element) {
std::wstring text = readAndProcessInnerText(element);
auto button = std::make_shared<Button>(text, glm::vec4(0.0f), nullptr);
@ -156,7 +163,7 @@ static std::shared_ptr<UINode> readTextBox(UiXmlReader& reader, xml::xmlelement
auto consumer = scripting::create_wstring_consumer(
reader.getEnvironment().getId(),
element->attr("consumer").getText(),
reader.getFilename()+"lua"
reader.getFilename()+".lua"
);
textbox->textConsumer(consumer);
}
@ -171,13 +178,34 @@ static std::shared_ptr<UINode> readImage(UiXmlReader& reader, xml::xmlelement el
return image;
}
static std::shared_ptr<UINode> readTrackBar(UiXmlReader& reader, xml::xmlelement element) {
float min = element->attr("min", "0.0").asFloat();
float max = element->attr("max", "1.0").asFloat();
float def = element->attr("value", "0.0").asFloat();
float step = element->attr("step", "1.0").asFloat();
int trackWidth = element->attr("track-width", "1.0").asInt();
auto bar = std::make_shared<TrackBar>(min, max, def, step, trackWidth);
_readUINode(element, *bar);
if (element->has("consumer")) {
auto consumer = scripting::create_number_consumer(
reader.getEnvironment().getId(),
element->attr("consumer").getText(),
reader.getFilename()+".lua"
);
bar->setConsumer(consumer);
}
return bar;
}
UiXmlReader::UiXmlReader(const scripting::Environment& env, AssetsLoader& assetsLoader)
: env(env), assetsLoader(assetsLoader)
{
add("image", readImage);
add("label", readLabel);
add("panel", readPanel);
add("button", readButton);
add("textbox", readTextBox);
add("trackbar", readTrackBar);
add("container", readContainer);
}

View File

@ -9,15 +9,12 @@
using namespace gui;
using glm::vec2;
using glm::vec4;
Container::Container(vec2 coord, vec2 size) : UINode(coord, size) {
Container::Container(glm::vec2 coord, glm::vec2 size) : UINode(coord, size) {
actualLength = size.y;
setColor(glm::vec4());
}
std::shared_ptr<UINode> Container::getAt(vec2 pos, std::shared_ptr<UINode> self) {
std::shared_ptr<UINode> Container::getAt(glm::vec2 pos, std::shared_ptr<UINode> self) {
if (!interactive) {
return nullptr;
}
@ -79,8 +76,8 @@ void Container::setScrollable(bool flag) {
}
void Container::draw(const GfxContext* pctx, Assets* assets) {
vec2 coord = calcCoord();
vec2 size = getSize();
glm::vec2 coord = calcCoord();
glm::vec2 size = getSize();
drawBackground(pctx, assets);
auto batch = pctx->getBatch2D();
@ -88,7 +85,7 @@ void Container::draw(const GfxContext* pctx, Assets* assets) {
batch->render();
{
GfxContext ctx = pctx->sub();
ctx.scissors(vec4(coord.x, coord.y, size.x, size.y));
ctx.scissors(glm::vec4(coord.x, coord.y, size.x, size.y));
for (auto node : nodes) {
if (node->isVisible())
node->draw(pctx, assets);
@ -100,7 +97,7 @@ void Container::draw(const GfxContext* pctx, Assets* assets) {
void Container::drawBackground(const GfxContext* pctx, Assets* assets) {
if (color.a <= 0.0f)
return;
vec2 coord = calcCoord();
glm::vec2 coord = calcCoord();
auto batch = pctx->getBatch2D();
batch->texture(nullptr);
@ -154,11 +151,11 @@ const std::vector<std::shared_ptr<UINode>>& Container::getNodes() const {
return nodes;
}
Panel::Panel(vec2 size, glm::vec4 padding, float interval)
: Container(vec2(), size),
Panel::Panel(glm::vec2 size, glm::vec4 padding, float interval)
: Container(glm::vec2(), size),
padding(padding),
interval(interval) {
setColor(vec4(0.0f, 0.0f, 0.0f, 0.75f));
setColor(glm::vec4(0.0f, 0.0f, 0.0f, 0.75f));
}
Panel::~Panel() {
@ -183,9 +180,9 @@ glm::vec4 Panel::getPadding() const {
void Panel::cropToContent() {
if (maxLength > 0.0f) {
setSize(vec2(getSize().x, glm::min(maxLength, actualLength)));
setSize(glm::vec2(getSize().x, glm::min(maxLength, actualLength)));
} else {
setSize(vec2(getSize().x, actualLength));
setSize(glm::vec2(getSize().x, actualLength));
}
}
@ -199,21 +196,21 @@ void Panel::refresh() {
UINode::refresh();
float x = padding.x;
float y = padding.y;
vec2 size = getSize();
glm::vec2 size = getSize();
if (orientation == Orientation::vertical) {
float maxw = size.x;
for (auto& node : nodes) {
vec2 nodesize = node->getSize();
const vec4 margin = node->getMargin();
glm::vec2 nodesize = node->getSize();
const glm::vec4 margin = node->getMargin();
y += margin.y;
float ex = x + margin.x;
node->setCoord(vec2(ex, y));
node->setCoord(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(vec2(width, nodesize.y));
node->setSize(glm::vec2(width, nodesize.y));
}
node->refresh();
maxw = fmax(maxw, ex+node->getSize().x+margin.z+padding.z);
@ -222,10 +219,10 @@ void Panel::refresh() {
} else {
float maxh = size.y;
for (auto& node : nodes) {
vec2 nodesize = node->getSize();
const vec4 margin = node->getMargin();
glm::vec2 nodesize = node->getSize();
const glm::vec4 margin = node->getMargin();
x += margin.x;
node->setCoord(vec2(x, y+margin.y));
node->setCoord(glm::vec2(x, y+margin.y));
x += nodesize.x + margin.z + interval;
node->refresh();
@ -243,7 +240,7 @@ Orientation Panel::getOrientation() const {
return orientation;
}
PagesControl::PagesControl() : Container(vec2(), vec2(1)){
PagesControl::PagesControl() : Container(glm::vec2(), glm::vec2(1)){
}
bool PagesControl::has(std::string name) {
@ -259,16 +256,16 @@ void PagesControl::setPage(std::string name, bool history) {
if (found == pages.end()) {
throw std::runtime_error("no page found");
}
if (current_.panel) {
Container::remove(current_.panel);
if (current.panel) {
Container::remove(current.panel);
}
if (history) {
pageStack.push(curname_);
pageStack.push(curname);
}
curname_ = name;
current_ = found->second;
Container::add(current_.panel);
setSize(current_.panel->getSize());
curname = name;
current = found->second;
Container::add(current.panel);
setSize(current.panel->getSize());
}
void PagesControl::back() {
@ -279,8 +276,8 @@ void PagesControl::back() {
setPage(name, false);
}
Page& PagesControl::current() {
return current_;
Page& PagesControl::getCurrent() {
return current;
}
void PagesControl::clearHistory() {
@ -289,9 +286,9 @@ void PagesControl::clearHistory() {
void PagesControl::reset() {
clearHistory();
if (current_.panel) {
curname_ = "";
Container::remove(current_.panel);
current_ = Page{nullptr};
if (current.panel) {
curname = "";
Container::remove(current.panel);
current = Page{nullptr};
}
}

View File

@ -94,8 +94,8 @@ namespace gui {
protected:
std::unordered_map<std::string, Page> pages;
std::stack<std::string> pageStack;
Page current_;
std::string curname_ = "";
Page current;
std::string curname = "";
public:
PagesControl();
@ -106,7 +106,7 @@ namespace gui {
void clearHistory();
void reset();
Page& current();
Page& getCurrent();
};
}
#endif // FRONTEND_GUI_PANELS_H_

View File

@ -49,9 +49,6 @@
#include "../items/Inventory.h"
#include "../logic/scripting/scripting.h"
using glm::vec2;
using glm::vec3;
using glm::vec4;
using namespace gui;
static std::shared_ptr<Label> create_label(wstringsupplier supplier) {
@ -63,13 +60,13 @@ static std::shared_ptr<Label> create_label(wstringsupplier supplier) {
std::shared_ptr<UINode> HudRenderer::createDebugPanel(Engine* engine) {
auto level = frontend->getLevel();
auto panel = std::make_shared<Panel>(vec2(250, 200), vec4(5.0f), 2.0f);
auto panel = std::make_shared<Panel>(glm::vec2(250, 200), glm::vec4(5.0f), 2.0f);
panel->listenInterval(0.5f, [this]() {
fpsString = std::to_wstring(fpsMax)+L" / "+std::to_wstring(fpsMin);
fpsMin = fps;
fpsMax = fps;
});
panel->setCoord(vec2(10, 10));
panel->setCoord(glm::vec2(10, 10));
panel->add(create_label([this](){ return L"fps: "+this->fpsString;}));
panel->add(create_label([](){
return L"meshes: " + std::to_wstring(Mesh::meshesCount);
@ -100,15 +97,15 @@ std::shared_ptr<UINode> HudRenderer::createDebugPanel(Engine* engine) {
}));
for (int ax = 0; ax < 3; ax++){
auto sub = std::make_shared<Panel>(vec2(10, 27), vec4(0.0f));
sub->setOrientation(Orientation::horizontal);
auto sub = std::make_shared<Container>(glm::vec2(), glm::vec2(250, 27));
std::wstring str = L"x: ";
str[0] += ax;
auto label = std::make_shared<Label>(str);
label->setMargin(vec4(2, 3, 2, 3));
label->setMargin(glm::vec4(2, 3, 2, 3));
label->setSize(glm::vec2(20, 27));
sub->add(label);
sub->setColor(vec4(0.0f));
sub->setColor(glm::vec4(0.0f));
// Coord input
auto box = std::make_shared<TextBox>(L"");
@ -118,7 +115,7 @@ std::shared_ptr<UINode> HudRenderer::createDebugPanel(Engine* engine) {
});
box->textConsumer([=](std::wstring text) {
try {
vec3 position = level->player->hitbox->position;
glm::vec3 position = level->player->hitbox->position;
position[ax] = std::stoi(text);
level->player->teleport(position);
} catch (std::invalid_argument& _){
@ -128,8 +125,9 @@ std::shared_ptr<UINode> HudRenderer::createDebugPanel(Engine* engine) {
Hitbox* hitbox = level->player->hitbox.get();
box->setText(std::to_wstring(int(hitbox->position[ax])));
});
box->setSize(glm::vec2(230, 27));
sub->add(box);
sub->add(box, glm::vec2(20, 0));
panel->add(sub);
}
panel->add(create_label([=](){
@ -143,24 +141,24 @@ std::shared_ptr<UINode> HudRenderer::createDebugPanel(Engine* engine) {
}));
{
auto bar = std::make_shared<TrackBar>(0.0f, 1.0f, 1.0f, 0.005f, 8);
bar->supplier([=]() {return level->world->daytime;});
bar->consumer([=](double val) {level->world->daytime = val;});
bar->setSupplier([=]() {return level->world->daytime;});
bar->setConsumer([=](double val) {level->world->daytime = val;});
panel->add(bar);
}
{
auto bar = std::make_shared<TrackBar>(0.0f, 1.0f, 0.0f, 0.005f, 8);
bar->supplier([=]() {return WorldRenderer::fog;});
bar->consumer([=](double val) {WorldRenderer::fog = val;});
bar->setSupplier([=]() {return WorldRenderer::fog;});
bar->setConsumer([=](double val) {WorldRenderer::fog = val;});
panel->add(bar);
}
{
auto checkbox = std::make_shared<FullCheckBox>(
L"Show Chunk Borders", vec2(400, 24)
L"Show Chunk Borders", glm::vec2(400, 24)
);
checkbox->supplier([=]() {
checkbox->setSupplier([=]() {
return engine->getSettings().debug.showChunkBorders;
});
checkbox->consumer([=](bool checked) {
checkbox->setConsumer([=](bool checked) {
engine->getSettings().debug.showChunkBorders = checked;
});
panel->add(checkbox);
@ -236,7 +234,7 @@ HudRenderer::HudRenderer(Engine* engine, LevelFrontend* frontend)
contentAccess = createContentAccess();
contentAccessPanel = std::make_shared<Panel>(
contentAccess->getSize(), vec4(0.0f), 0.0f
contentAccess->getSize(), glm::vec4(0.0f), 0.0f
);
contentAccessPanel->setColor(glm::vec4());
contentAccessPanel->add(contentAccess);
@ -246,8 +244,9 @@ HudRenderer::HudRenderer(Engine* engine, LevelFrontend* frontend)
darkOverlay = std::make_unique<Panel>(glm::vec2(4000.0f));
darkOverlay->setColor(glm::vec4(0, 0, 0, 0.5f));
darkOverlay->setZIndex(-1);
darkOverlay->setVisible(false);
uicamera = std::make_unique<Camera>(vec3(), 1);
uicamera = std::make_unique<Camera>(glm::vec3(), 1);
uicamera->perspective = false;
uicamera->flipped = true;
@ -264,6 +263,7 @@ HudRenderer::HudRenderer(Engine* engine, LevelFrontend* frontend)
}
HudRenderer::~HudRenderer() {
// removing all controlled ui
gui->remove(grabbedItemView);
if (inventoryView) {
gui->remove(inventoryView);
@ -286,23 +286,20 @@ void HudRenderer::update(bool visible) {
auto menu = gui->getMenu();
debugPanel->setVisible(player->debug && visible);
menu->setVisible(pause);
if (!visible && inventoryOpen) {
closeInventory();
}
if (pause && menu->current().panel == nullptr) {
pause = false;
if (pause && menu->getCurrent().panel == nullptr) {
setPause(false);
}
if (Events::jpressed(keycode::ESCAPE) && !gui->isFocusCaught()) {
if (pause) {
pause = false;
menu->reset();
setPause(false);
} else if (inventoryOpen) {
closeInventory();
} else {
pause = true;
menu->setPage("pause");
setPause(true);
}
}
if (visible && Events::jactive(BIND_HUD_INVENTORY)) {
@ -318,9 +315,9 @@ void HudRenderer::update(bool visible) {
Events::toggleCursor();
}
vec2 invSize = contentAccessPanel->getSize();
glm::vec2 invSize = contentAccessPanel->getSize();
contentAccessPanel->setVisible(inventoryOpen);
contentAccessPanel->setSize(vec2(invSize.x, Window::height));
contentAccessPanel->setSize(glm::vec2(invSize.x, Window::height));
hotbarView->setVisible(visible);
for (int i = keycode::NUM_1; i <= keycode::NUM_9; i++) {
@ -339,10 +336,11 @@ void HudRenderer::update(bool visible) {
}
player->setChosenSlot(slot);
}
darkOverlay->setVisible(pause);
}
/**
* Show inventory on the screen and turn on inventory mode blocking movement
*/
void HudRenderer::openInventory() {
auto level = frontend->getLevel();
auto player = level->player;
@ -358,6 +356,9 @@ void HudRenderer::openInventory() {
gui->add(inventoryView);
}
/**
* Hide inventory and turn off inventory mode
*/
void HudRenderer::closeInventory() {
scripting::on_ui_close(inventoryDocument, inventoryView->getInventory().get());
inventoryOpen = false;
@ -370,13 +371,12 @@ void HudRenderer::closeInventory() {
void HudRenderer::draw(const GfxContext& ctx){
auto level = frontend->getLevel();
auto player = level->player;
const Viewport& viewport = ctx.getViewport();
const uint width = viewport.getWidth();
const uint height = viewport.getHeight();
Player* player = level->player;
uicamera->setFov(height);
auto batch = ctx.getBatch2D();
@ -390,16 +390,21 @@ void HudRenderer::draw(const GfxContext& ctx){
hotbarView->setSelected(player->getChosenSlot());
// Crosshair
batch->begin();
if (!pause && Events::_cursor_locked && !level->player->debug) {
batch->lineWidth(2);
batch->line(width/2, height/2-6, width/2, height/2+6, 0.2f, 0.2f, 0.2f, 1.0f);
batch->line(width/2+6, height/2, width/2-6, height/2, 0.2f, 0.2f, 0.2f, 1.0f);
batch->line(width/2-5, height/2-5, width/2+5, height/2+5, 0.9f, 0.9f, 0.9f, 1.0f);
batch->line(width/2+5, height/2-5, width/2-5, height/2+5, 0.9f, 0.9f, 0.9f, 1.0f);
GfxContext chctx = ctx.sub();
chctx.blendMode(blendmode::inversion);
batch->texture(assets->getTexture("gui/crosshair"));
int chsize = 16;
batch->rect(
(width-chsize)/2, (height-chsize)/2,
chsize, chsize, 0,0, 1,1, 1,1,1,1
);
batch->render();
}
// Delta-time visualizer
if (level->player->debug) {
batch->texture(nullptr);
const int dmwidth = 256;
const float dmscale = 4000.0f;
static float deltameter[dmwidth]{};
@ -416,15 +421,13 @@ void HudRenderer::draw(const GfxContext& ctx){
if (inventoryOpen) {
float caWidth = contentAccess->getSize().x;
contentAccessPanel->setCoord(glm::vec2(width-caWidth, 0));
glm::vec2 invSize = inventoryView->getSize();
float width = viewport.getWidth();
inventoryView->setCoord(glm::vec2(
glm::min(width/2-invSize.x/2, width-caWidth-10-invSize.x),
height/2-invSize.y/2
));
contentAccessPanel->setCoord(glm::vec2(width-caWidth, 0));
}
grabbedItemView->setCoord(glm::vec2(Events::cursor));
batch->render();
@ -437,3 +440,19 @@ bool HudRenderer::isInventoryOpen() const {
bool HudRenderer::isPause() const {
return pause;
}
void HudRenderer::setPause(bool pause) {
if (this->pause == pause) {
return;
}
this->pause = pause;
auto menu = gui->getMenu();
if (pause) {
menu->setPage("pause");
} else {
menu->reset();
}
darkOverlay->setVisible(pause);
menu->setVisible(pause);
}

View File

@ -64,6 +64,7 @@ public:
bool isInventoryOpen() const;
bool isPause() const;
void setPause(bool pause);
void openInventory();
void closeInventory();

View File

@ -472,10 +472,10 @@ void create_controls_panel(Engine* engine) {
}));
auto trackbar = std::make_shared<TrackBar>(0.1, 10.0, 2.0, 0.1, 4);
trackbar->supplier([=]() {
trackbar->setSupplier([=]() {
return engine->getSettings().camera.sensitivity;
});
trackbar->consumer([=](double value) {
trackbar->setConsumer([=](double value) {
engine->getSettings().camera.sensitivity = value;
});
panel->add(trackbar);
@ -512,10 +512,10 @@ void create_settings_panel(Engine* engine) {
}));
auto trackbar = std::make_shared<TrackBar>(3, 66, 10, 1, 3);
trackbar->supplier([=]() {
trackbar->setSupplier([=]() {
return engine->getSettings().chunks.loadDistance;
});
trackbar->consumer([=](double value) {
trackbar->setConsumer([=](double value) {
engine->getSettings().chunks.loadDistance = static_cast<uint>(value);
});
panel->add(trackbar);
@ -528,10 +528,10 @@ void create_settings_panel(Engine* engine) {
}));
auto trackbar = std::make_shared<TrackBar>(1, 32, 10, 1, 1);
trackbar->supplier([=]() {
trackbar->setSupplier([=]() {
return engine->getSettings().chunks.loadSpeed;
});
trackbar->consumer([=](double value) {
trackbar->setConsumer([=](double value) {
engine->getSettings().chunks.loadSpeed = static_cast<uint>(value);
});
panel->add(trackbar);
@ -545,10 +545,10 @@ void create_settings_panel(Engine* engine) {
}));
auto trackbar = std::make_shared<TrackBar>(1.0, 6.0, 1.0, 0.1, 2);
trackbar->supplier([=]() {
trackbar->setSupplier([=]() {
return engine->getSettings().graphics.fogCurve;
});
trackbar->consumer([=](double value) {
trackbar->setConsumer([=](double value) {
engine->getSettings().graphics.fogCurve = value;
});
panel->add(trackbar);
@ -561,10 +561,10 @@ void create_settings_panel(Engine* engine) {
}));
auto trackbar = std::make_shared<TrackBar>(30.0, 120.0, 90, 1, 4);
trackbar->supplier([=]() {
trackbar->setSupplier([=]() {
return engine->getSettings().camera.fov;
});
trackbar->consumer([=](double value) {
trackbar->setConsumer([=](double value) {
engine->getSettings().camera.fov = value;
});
panel->add(trackbar);
@ -574,10 +574,10 @@ void create_settings_panel(Engine* engine) {
auto checkbox = std::make_shared<FullCheckBox>(
langs::get(L"V-Sync", L"settings"), vec2(400, 32)
);
checkbox->supplier([=]() {
checkbox->setSupplier([=]() {
return engine->getSettings().display.swapInterval != 0;
});
checkbox->consumer([=](bool checked) {
checkbox->setConsumer([=](bool checked) {
engine->getSettings().display.swapInterval = checked;
});
panel->add(checkbox);
@ -587,10 +587,10 @@ void create_settings_panel(Engine* engine) {
auto checkbox = std::make_shared<FullCheckBox>(
langs::get(L"Backlight", L"settings"), vec2(400, 32)
);
checkbox->supplier([=]() {
checkbox->setSupplier([=]() {
return engine->getSettings().graphics.backlight;
});
checkbox->consumer([=](bool checked) {
checkbox->setConsumer([=](bool checked) {
engine->getSettings().graphics.backlight = checked;
});
panel->add(checkbox);

View File

@ -1,6 +1,9 @@
#include "LuaState.h"
#include "api_lua.h"
#include <iostream>
#include "lua_util.h"
#include "api/api_lua.h"
#include "api/libgui.h"
#include "../../util/stringutil.h"
lua::luaerror::luaerror(const std::string& message) : std::runtime_error(message) {
@ -98,6 +101,7 @@ void lua::LuaState::createFuncs() {
openlib("item", itemlib, 0);
openlib("time", timelib, 0);
openlib("file", filelib, 0);
openlib("gui", guilib, 0);
addfunc("print", l_print);

View File

@ -1,39 +1,38 @@
#ifndef LOGIC_SCRIPTING_API_LUA_H_
#define LOGIC_SCRIPTING_API_LUA_H_
#include "api_lua.h"
#include "scripting.h"
#include "lua_util.h"
#include "../scripting.h"
#include "../lua_util.h"
#include <glm/glm.hpp>
#include <iostream>
#include "../../files/files.h"
#include "../../physics/Hitbox.h"
#include "../../objects/Player.h"
#include "../../world/Level.h"
#include "../../world/World.h"
#include "../../content/Content.h"
#include "../../voxels/Block.h"
#include "../../voxels/Chunks.h"
#include "../../voxels/voxel.h"
#include "../../items/ItemDef.h"
#include "../../items/ItemStack.h"
#include "../../items/Inventory.h"
#include "../../items/Inventories.h"
#include "../../lighting/Lighting.h"
#include "../../logic/BlocksController.h"
#include "../../window/Window.h"
#include "../../engine.h"
#include "../../../files/files.h"
#include "../../../physics/Hitbox.h"
#include "../../../objects/Player.h"
#include "../../../world/Level.h"
#include "../../../world/World.h"
#include "../../../content/Content.h"
#include "../../../voxels/Block.h"
#include "../../../voxels/Chunks.h"
#include "../../../voxels/voxel.h"
#include "../../../items/ItemDef.h"
#include "../../../items/ItemStack.h"
#include "../../../items/Inventory.h"
#include "../../../items/Inventories.h"
#include "../../../lighting/Lighting.h"
#include "../../../logic/BlocksController.h"
#include "../../../window/Window.h"
#include "../../../engine.h"
/* == file library == */
static int l_file_resolve(lua_State* L) {
int l_file_resolve(lua_State* L) {
std::string path = lua_tostring(L, 1);
fs::path resolved = scripting::engine->getPaths()->resolve(path);
lua_pushstring(L, resolved.u8string().c_str());
return 1;
}
static int l_file_read(lua_State* L) {
int l_file_read(lua_State* L) {
auto paths = scripting::engine->getPaths();
fs::path path = paths->resolve(lua_tostring(L, 1));
if (fs::is_regular_file(path)) {
@ -43,7 +42,7 @@ static int l_file_read(lua_State* L) {
return luaL_error(L, "file does not exists '%s'", path.u8string().c_str());
}
static int l_file_write(lua_State* L) {
int l_file_write(lua_State* L) {
auto paths = scripting::engine->getPaths();
fs::path path = paths->resolve(lua_tostring(L, 1));
const char* text = lua_tostring(L, 2);
@ -51,28 +50,28 @@ static int l_file_write(lua_State* L) {
return 1;
}
static int l_file_exists(lua_State* L) {
int l_file_exists(lua_State* L) {
auto paths = scripting::engine->getPaths();
fs::path path = paths->resolve(lua_tostring(L, 1));
lua_pushboolean(L, fs::exists(path));
return 1;
}
static int l_file_isfile(lua_State* L) {
int l_file_isfile(lua_State* L) {
auto paths = scripting::engine->getPaths();
fs::path path = paths->resolve(lua_tostring(L, 1));
lua_pushboolean(L, fs::is_regular_file(path));
return 1;
}
static int l_file_isdir(lua_State* L) {
int l_file_isdir(lua_State* L) {
auto paths = scripting::engine->getPaths();
fs::path path = paths->resolve(lua_tostring(L, 1));
lua_pushboolean(L, fs::is_directory(path));
return 1;
}
static int l_file_length(lua_State* L) {
int l_file_length(lua_State* L) {
auto paths = scripting::engine->getPaths();
fs::path path = paths->resolve(lua_tostring(L, 1));
if (fs::exists(path)){
@ -83,38 +82,26 @@ static int l_file_length(lua_State* L) {
return 1;
}
static int l_file_mkdir(lua_State* L) {
int l_file_mkdir(lua_State* L) {
auto paths = scripting::engine->getPaths();
fs::path path = paths->resolve(lua_tostring(L, 1));
lua_pushboolean(L, fs::create_directory(path));
return 1;
}
static const luaL_Reg filelib [] = {
{"resolve", l_file_resolve},
{"read", l_file_read},
{"write", l_file_write},
{"exists", l_file_exists},
{"isfile", l_file_isfile},
{"isdir", l_file_isdir},
{"length", l_file_length},
{"mkdir", l_file_mkdir},
{NULL, NULL}
};
/* == time library == */
static int l_time_uptime(lua_State* L) {
int l_time_uptime(lua_State* L) {
lua_pushnumber(L, Window::time());
return 1;
}
static const luaL_Reg timelib [] = {
{"uptime", l_time_uptime},
{NULL, NULL}
};
int l_time_delta(lua_State* L) {
lua_pushnumber(L, scripting::engine->getDelta());
return 1;
}
/* == pack library == */
static int l_pack_get_folder(lua_State* L) {
int l_pack_get_folder(lua_State* L) {
std::string packName = lua_tostring(L, 1);
if (packName == "core") {
auto folder = scripting::engine->getPaths()
@ -132,43 +119,30 @@ static int l_pack_get_folder(lua_State* L) {
return 1;
}
static const luaL_Reg packlib [] = {
{"get_folder", l_pack_get_folder},
{NULL, NULL}
};
/* == world library == */
static int l_world_get_total_time(lua_State* L) {
int l_world_get_total_time(lua_State* L) {
lua_pushnumber(L, scripting::level->world->totalTime);
return 1;
}
static int l_world_get_day_time(lua_State* L) {
int l_world_get_day_time(lua_State* L) {
lua_pushnumber(L, scripting::level->world->daytime);
return 1;
}
static int l_world_set_day_time(lua_State* L) {
int l_world_set_day_time(lua_State* L) {
double value = lua_tonumber(L, 1);
scripting::level->world->daytime = fmod(value, 1.0);
return 0;
}
static int l_world_get_seed(lua_State* L) {
int l_world_get_seed(lua_State* L) {
lua_pushinteger(L, scripting::level->world->getSeed());
return 1;
}
static const luaL_Reg worldlib [] = {
{"get_total_time", l_world_get_total_time},
{"get_day_time", l_world_get_day_time},
{"set_day_time", l_world_set_day_time},
{"get_seed", l_world_get_seed},
{NULL, NULL}
};
/* == player library ==*/
static int l_player_get_pos(lua_State* L) {
int l_player_get_pos(lua_State* L) {
int playerid = lua_tointeger(L, 1);
if (playerid != 1)
return 0;
@ -179,7 +153,7 @@ static int l_player_get_pos(lua_State* L) {
return 3;
}
static int l_player_get_rot(lua_State* L) {
int l_player_get_rot(lua_State* L) {
int playerid = lua_tointeger(L, 1);
if (playerid != 1)
return 0;
@ -189,7 +163,7 @@ static int l_player_get_rot(lua_State* L) {
return 2;
}
static int l_player_set_rot(lua_State* L) {
int l_player_set_rot(lua_State* L) {
int playerid = lua_tointeger(L, 1);
if (playerid != 1)
return 0;
@ -201,7 +175,7 @@ static int l_player_set_rot(lua_State* L) {
return 0;
}
static int l_player_set_pos(lua_State* L) {
int l_player_set_pos(lua_State* L) {
int playerid = lua_tointeger(L, 1);
if (playerid != 1)
return 0;
@ -212,7 +186,7 @@ static int l_player_set_pos(lua_State* L) {
return 0;
}
static int l_player_get_inv(lua_State* L) {
int l_player_get_inv(lua_State* L) {
int playerid = lua_tointeger(L, 1);
if (playerid != 1)
return 0;
@ -222,15 +196,6 @@ static int l_player_get_inv(lua_State* L) {
return 2;
}
static const luaL_Reg playerlib [] = {
{"get_pos", l_player_get_pos},
{"set_pos", l_player_set_pos},
{"get_rot", l_player_get_rot},
{"set_rot", l_player_set_rot},
{"get_inventory", l_player_get_inv},
{NULL, NULL}
};
static void validate_itemid(lua_State* L, itemid_t id) {
if (id >= scripting::indices->countItemDefs()) {
luaL_error(L, "invalid item id");
@ -238,7 +203,7 @@ static void validate_itemid(lua_State* L, itemid_t id) {
}
/* == inventory library == */
static int l_inventory_get(lua_State* L) {
int l_inventory_get(lua_State* L) {
lua::luaint invid = lua_tointeger(L, 1);
lua::luaint slotid = lua_tointeger(L, 2);
auto inv = scripting::level->inventories->get(invid);
@ -254,7 +219,7 @@ static int l_inventory_get(lua_State* L) {
return 2;
}
static int l_inventory_set(lua_State* L) {
int l_inventory_set(lua_State* L) {
lua::luaint invid = lua_tointeger(L, 1);
lua::luaint slotid = lua_tointeger(L, 2);
lua::luaint itemid = lua_tointeger(L, 3);
@ -273,7 +238,7 @@ static int l_inventory_set(lua_State* L) {
return 0;
}
static int l_inventory_size(lua_State* L) {
int l_inventory_size(lua_State* L) {
lua::luaint invid = lua_tointeger(L, 1);
auto inv = scripting::level->inventories->get(invid);
if (inv == nullptr) {
@ -283,7 +248,7 @@ static int l_inventory_size(lua_State* L) {
return 1;
}
static int l_inventory_add(lua_State* L) {
int l_inventory_add(lua_State* L) {
lua::luaint invid = lua_tointeger(L, 1);
lua::luaint itemid = lua_tointeger(L, 2);
lua::luaint count = lua_tointeger(L, 3);
@ -299,7 +264,7 @@ static int l_inventory_add(lua_State* L) {
return 1;
}
static int l_inventory_get_block(lua_State* L) {
int l_inventory_get_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);
@ -308,17 +273,8 @@ static int l_inventory_get_block(lua_State* L) {
return 1;
}
static const luaL_Reg inventorylib [] = {
{"get", l_inventory_get},
{"set", l_inventory_set},
{"size", l_inventory_size},
{"add", l_inventory_add},
{"get_block", l_inventory_get_block},
{NULL, NULL}
};
/* == item library == */
static int l_item_name(lua_State* L) {
int l_item_name(lua_State* L) {
auto indices = scripting::content->getIndices();
lua::luaint id = lua_tointeger(L, 1);
if (id < 0 || size_t(id) >= indices->countItemDefs()) {
@ -329,13 +285,13 @@ static int l_item_name(lua_State* L) {
return 1;
}
static int l_item_index(lua_State* L) {
int l_item_index(lua_State* L) {
auto name = lua_tostring(L, 1);
lua_pushinteger(L, scripting::content->requireItem(name).rt.id);
return 1;
}
static int l_item_stack_size(lua_State* L) {
int l_item_stack_size(lua_State* L) {
auto indices = scripting::content->getIndices();
lua::luaint id = lua_tointeger(L, 1);
if (id < 0 || size_t(id) >= indices->countItemDefs()) {
@ -346,21 +302,13 @@ static int l_item_stack_size(lua_State* L) {
return 1;
}
static int l_item_defs_count(lua_State* L) {
int l_item_defs_count(lua_State* L) {
lua_pushinteger(L, scripting::indices->countItemDefs());
return 1;
}
static const luaL_Reg itemlib [] = {
{"index", l_item_index},
{"name", l_item_name},
{"stack_size", l_item_stack_size},
{"defs_count", l_item_defs_count},
{NULL, NULL}
};
/* == blocks-related functions == */
static int l_block_name(lua_State* L) {
int l_block_name(lua_State* L) {
auto indices = scripting::content->getIndices();
lua::luaint id = lua_tointeger(L, 1);
if (id < 0 || size_t(id) >= indices->countBlockDefs()) {
@ -371,7 +319,7 @@ static int l_block_name(lua_State* L) {
return 1;
}
static int l_is_solid_at(lua_State* L) {
int l_is_solid_at(lua_State* L) {
lua::luaint x = lua_tointeger(L, 1);
lua::luaint y = lua_tointeger(L, 2);
lua::luaint z = lua_tointeger(L, 3);
@ -380,18 +328,18 @@ static int l_is_solid_at(lua_State* L) {
return 1;
}
static int l_blocks_count(lua_State* L) {
int l_blocks_count(lua_State* L) {
lua_pushinteger(L, scripting::indices->countBlockDefs());
return 1;
}
static int l_block_index(lua_State* L) {
int l_block_index(lua_State* L) {
auto name = lua_tostring(L, 1);
lua_pushinteger(L, scripting::content->requireBlock(name).rt.id);
return 1;
}
static int l_set_block(lua_State* L) {
int l_set_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);
@ -408,7 +356,7 @@ static int l_set_block(lua_State* L) {
return 0;
}
static int l_get_block(lua_State* L) {
int l_get_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);
@ -418,7 +366,7 @@ static int l_get_block(lua_State* L) {
return 1;
}
static int l_get_block_x(lua_State* L) {
int l_get_block_x(lua_State* L) {
lua::luaint x = lua_tointeger(L, 1);
lua::luaint y = lua_tointeger(L, 2);
lua::luaint z = lua_tointeger(L, 3);
@ -435,7 +383,7 @@ static int l_get_block_x(lua_State* L) {
}
}
static int l_get_block_y(lua_State* L) {
int l_get_block_y(lua_State* L) {
lua::luaint x = lua_tointeger(L, 1);
lua::luaint y = lua_tointeger(L, 2);
lua::luaint z = lua_tointeger(L, 3);
@ -452,7 +400,7 @@ static int l_get_block_y(lua_State* L) {
}
}
static int l_get_block_z(lua_State* L) {
int l_get_block_z(lua_State* L) {
lua::luaint x = lua_tointeger(L, 1);
lua::luaint y = lua_tointeger(L, 2);
lua::luaint z = lua_tointeger(L, 3);
@ -469,7 +417,7 @@ static int l_get_block_z(lua_State* L) {
}
}
static int l_get_block_states(lua_State* L) {
int l_get_block_states(lua_State* L) {
lua::luaint x = lua_tointeger(L, 1);
lua::luaint y = lua_tointeger(L, 2);
lua::luaint z = lua_tointeger(L, 3);
@ -479,7 +427,7 @@ static int l_get_block_states(lua_State* L) {
return 1;
}
static int l_get_block_user_bits(lua_State* L) {
int l_get_block_user_bits(lua_State* L) {
lua::luaint x = lua_tointeger(L, 1);
lua::luaint y = lua_tointeger(L, 2);
lua::luaint z = lua_tointeger(L, 3);
@ -497,7 +445,7 @@ static int l_get_block_user_bits(lua_State* L) {
return 1;
}
static int l_set_block_user_bits(lua_State* L) {
int l_set_block_user_bits(lua_State* L) {
lua::luaint x = lua_tointeger(L, 1);
lua::luaint y = lua_tointeger(L, 2);
lua::luaint z = lua_tointeger(L, 3);
@ -515,7 +463,7 @@ static int l_set_block_user_bits(lua_State* L) {
return 0;
}
static int l_is_replaceable_at(lua_State* L) {
int l_is_replaceable_at(lua_State* L) {
int x = lua_tointeger(L, 1);
int y = lua_tointeger(L, 2);
int z = lua_tointeger(L, 3);
@ -524,8 +472,7 @@ static int l_is_replaceable_at(lua_State* L) {
return 1;
}
// Modified version of luaB_print from lbaselib.c
static int l_print(lua_State* L) {
int l_print(lua_State* L) {
int n = lua_gettop(L); /* number of arguments */
lua_getglobal(L, "tostring");
for (int i=1; i<=n; i++) {
@ -544,5 +491,3 @@ static int l_print(lua_State* L) {
std::cout << std::endl;
return 0;
}
#endif // LOGIC_SCRIPTING_API_LUA_H_

View File

@ -0,0 +1,124 @@
#ifndef LOGIC_SCRIPTING_API_LUA_H_
#define LOGIC_SCRIPTING_API_LUA_H_
#include <lua.hpp>
/* == file library == */
extern int l_file_resolve(lua_State* L);
extern int l_file_read(lua_State* L);
extern int l_file_write(lua_State* L);
extern int l_file_exists(lua_State* L);
extern int l_file_isfile(lua_State* L);
extern int l_file_isdir(lua_State* L);
extern int l_file_length(lua_State* L);
extern int l_file_mkdir(lua_State* L);
static const luaL_Reg filelib [] = {
{"resolve", l_file_resolve},
{"read", l_file_read},
{"write", l_file_write},
{"exists", l_file_exists},
{"isfile", l_file_isfile},
{"isdir", l_file_isdir},
{"length", l_file_length},
{"mkdir", l_file_mkdir},
{NULL, NULL}
};
/* == time library == */
extern int l_time_uptime(lua_State* L);
extern int l_time_delta(lua_State* L);
static const luaL_Reg timelib [] = {
{"uptime", l_time_uptime},
{"delta", l_time_delta},
{NULL, NULL}
};
/* == pack library == */
extern int l_pack_get_folder(lua_State* L);
static const luaL_Reg packlib [] = {
{"get_folder", l_pack_get_folder},
{NULL, NULL}
};
/* == world library == */
extern int l_world_get_total_time(lua_State* L);
extern int l_world_get_day_time(lua_State* L);
extern int l_world_set_day_time(lua_State* L);
extern int l_world_get_seed(lua_State* L);
static const luaL_Reg worldlib [] = {
{"get_total_time", l_world_get_total_time},
{"get_day_time", l_world_get_day_time},
{"set_day_time", l_world_set_day_time},
{"get_seed", l_world_get_seed},
{NULL, NULL}
};
/* == player library ==*/
extern int l_player_get_pos(lua_State* L);
extern int l_player_get_rot(lua_State* L);
extern int l_player_set_rot(lua_State* L);
extern int l_player_set_pos(lua_State* L);
extern int l_player_get_inv(lua_State* L);
static const luaL_Reg playerlib [] = {
{"get_pos", l_player_get_pos},
{"set_pos", l_player_set_pos},
{"get_rot", l_player_get_rot},
{"set_rot", l_player_set_rot},
{"get_inventory", l_player_get_inv},
{NULL, NULL}
};
/* == inventory library == */
extern int l_inventory_get(lua_State* L);
extern int l_inventory_set(lua_State* L);
extern int l_inventory_size(lua_State* L);
extern int l_inventory_add(lua_State* L);
extern int l_inventory_get_block(lua_State* L);
static const luaL_Reg inventorylib [] = {
{"get", l_inventory_get},
{"set", l_inventory_set},
{"size", l_inventory_size},
{"add", l_inventory_add},
{"get_block", l_inventory_get_block},
{NULL, NULL}
};
/* == item library == */
extern int l_item_name(lua_State* L);
extern int l_item_index(lua_State* L);
extern int l_item_stack_size(lua_State* L);
extern int l_item_defs_count(lua_State* L);
static const luaL_Reg itemlib [] = {
{"index", l_item_index},
{"name", l_item_name},
{"stack_size", l_item_stack_size},
{"defs_count", l_item_defs_count},
{NULL, NULL}
};
/* == blocks-related functions == */
extern int l_block_name(lua_State* L);
extern int l_is_solid_at(lua_State* L);
extern int l_blocks_count(lua_State* L);
extern int l_block_index(lua_State* L);
extern int l_set_block(lua_State* L);
extern int l_get_block(lua_State* L);
extern int l_get_block_x(lua_State* L);
extern int l_get_block_y(lua_State* L);
extern int l_get_block_z(lua_State* L);
extern int l_get_block_states(lua_State* L);
extern int l_get_block_user_bits(lua_State* L);
extern int l_set_block_user_bits(lua_State* L);
extern int l_is_replaceable_at(lua_State* L);
// Modified version of luaB_print from lbaselib.c
extern int l_print(lua_State* L);
#endif // LOGIC_SCRIPTING_API_LUA_H_

View File

@ -0,0 +1,101 @@
#include "libgui.h"
#include <iostream>
#include "../scripting.h"
#include "../lua_util.h"
#include "../../../engine.h"
#include "../../../assets/Assets.h"
#include "../../../frontend/gui/UINode.h"
#include "../../../frontend/gui/controls.h"
#include "../../../frontend/UiDocument.h"
#include "../../../util/stringutil.h"
static gui::UINode* getDocumentNode(lua_State* L, const std::string& name, const std::string& nodeName) {
auto doc = scripting::engine->getAssets()->getLayout(name);
if (doc == nullptr) {
luaL_error(L, "document '%s' not found", name.c_str());
}
auto node = doc->get(nodeName);
if (node == nullptr) {
luaL_error(L, "document '%s' has no element with id '%s'", name.c_str(), nodeName.c_str());
}
return node.get();
}
static bool getattr(lua_State* L, gui::Button* button, const std::string& attr) {
if (button == nullptr)
return false;
if (attr == "text") {
lua_pushstring(L, util::wstr2str_utf8(button->getText()).c_str());
return true;
}
return false;
}
static bool getattr(lua_State* L, gui::Label* label, const std::string& attr) {
if (label == nullptr)
return false;
if (attr == "text") {
lua_pushstring(L, util::wstr2str_utf8(label->getText()).c_str());
return true;
}
return false;
}
static bool setattr(lua_State* L, gui::Button* button, const std::string& attr) {
if (button == nullptr)
return false;
if (attr == "text") {
button->setText(util::str2wstr_utf8(lua_tostring(L, 4)));
return true;
}
return false;
}
static bool setattr(lua_State* L, gui::Label* label, const std::string& attr) {
if (label == nullptr)
return false;
if (attr == "text") {
label->setText(util::str2wstr_utf8(lua_tostring(L, 4)));
return true;
}
return false;
}
int l_gui_getattr(lua_State* L) {
auto docname = lua_tostring(L, 1);
auto element = lua_tostring(L, 2);
const std::string attr = lua_tostring(L, 3);
auto node = getDocumentNode(L, docname, element);
if (attr == "color") {
return lua::pushcolor_arr(L, node->getColor());
} else if (attr == "coord") {
return lua::pushvec2_arr(L, node->getCoord());
} else if (attr == "size") {
return lua::pushvec2_arr(L, node->getSize());
}
if (getattr(L, dynamic_cast<gui::Button*>(node), attr))
return 1;
if (getattr(L, dynamic_cast<gui::Label*>(node), attr))
return 1;
return 0;
}
int l_gui_setattr(lua_State* L) {
auto docname = lua_tostring(L, 1);
auto element = lua_tostring(L, 2);
const std::string attr = lua_tostring(L, 3);
auto node = getDocumentNode(L, docname, element);
if (setattr(L, dynamic_cast<gui::Button*>(node), attr))
return 0;
if (setattr(L, dynamic_cast<gui::Label*>(node), attr))
return 0;
return 0;
}

View File

@ -0,0 +1,15 @@
#ifndef LOGIC_SCRIPTING_API_LIBGUI_H_
#define LOGIC_SCRIPTING_API_LIBGUI_H_
#include <lua.hpp>
extern int l_gui_getattr(lua_State* L);
extern int l_gui_setattr(lua_State* L);
static const luaL_Reg guilib [] = {
{"getattr", l_gui_getattr},
{"setattr", l_gui_setattr},
{NULL, NULL}
};
#endif // LOGIC_SCRIPTING_API_LIBGUI_H_

View File

@ -2,6 +2,8 @@
#define LOGIC_SCRIPTING_LUA_UTIL_H_
#include <lua.hpp>
#include <glm/glm.hpp>
#include "LuaState.h"
namespace lua {
inline int pushivec3(lua_State* L, luaint x, luaint y, luaint z) {
@ -10,6 +12,79 @@ namespace lua {
lua_pushinteger(L, z);
return 3;
}
inline int pushvec3(lua_State* L, glm::vec3 vec) {
lua_pushnumber(L, vec.x);
lua_pushnumber(L, vec.y);
lua_pushnumber(L, vec.z);
return 3;
}
inline int pushvec4(lua_State* L, glm::vec4 vec) {
lua_pushnumber(L, vec.x);
lua_pushnumber(L, vec.y);
lua_pushnumber(L, vec.z);
lua_pushnumber(L, vec.w);
return 4;
}
inline int pushvec2_arr(lua_State* L, glm::vec2 vec) {
lua_createtable(L, 2, 0);
lua_getglobal(L, "vec2_mt");
lua_setmetatable(L, -2);
lua_pushnumber(L, vec.x);
lua_rawseti(L, -2, 1);
lua_pushnumber(L, vec.y);
lua_rawseti(L, -2, 2);
return 1;
}
inline int pushvec3_arr(lua_State* L, glm::vec3 vec) {
lua_createtable(L, 3, 0);
lua_getglobal(L, "vec3_mt");
lua_setmetatable(L, -2);
lua_pushnumber(L, vec.x);
lua_rawseti(L, -2, 1);
lua_pushnumber(L, vec.y);
lua_rawseti(L, -2, 2);
lua_pushnumber(L, vec.z);
lua_rawseti(L, -2, 3);
return 1;
}
inline int pushvec4_arr(lua_State* L, glm::vec4 vec) {
lua_createtable(L, 4, 0);
lua_getglobal(L, "vec4_mt");
lua_setmetatable(L, -2);
lua_pushnumber(L, vec.x);
lua_rawseti(L, -2, 1);
lua_pushnumber(L, vec.y);
lua_rawseti(L, -2, 2);
lua_pushnumber(L, vec.z);
lua_rawseti(L, -2, 3);
lua_pushnumber(L, vec.w);
lua_rawseti(L, -2, 4);
return 1;
}
inline int pushcolor_arr(lua_State* L, glm::vec4 vec) {
lua_createtable(L, 4, 0);
lua_getglobal(L, "color_mt");
lua_setmetatable(L, -2);
lua_pushinteger(L, vec.x*255);
lua_rawseti(L, -2, 1);
lua_pushinteger(L, vec.y*255);
lua_rawseti(L, -2, 2);
lua_pushinteger(L, vec.z*255);
lua_rawseti(L, -2, 3);
lua_pushinteger(L, vec.w*255);
lua_rawseti(L, -2, 4);
return 1;
}
}
#endif // LOGIC_SCRIPTING_LUA_UTIL_H_

View File

@ -16,6 +16,7 @@
#include "../../engine.h"
#include "LuaState.h"
#include "../../util/stringutil.h"
#include "../../util/timeutil.h"
using namespace scripting;
@ -69,21 +70,42 @@ runnable scripting::create_runnable(
};
}
static bool processCallback(
int env,
const std::string& src,
const std::string& file
) {
try {
return state->eval(env, src, file) != 0;
} catch (lua::luaerror& err) {
std::cerr << err.what() << std::endl;
return false;
}
}
wstringconsumer scripting::create_wstring_consumer(
int env,
const std::string& src,
const std::string& file
) {
return [=](const std::wstring& x){
try {
if (state->eval(env, src, file) == 0)
return;
} catch (lua::luaerror err) {
std::cerr << err.what() << std::endl;
return;
if (processCallback(env, src, file)) {
state->pushstring(util::wstr2str_utf8(x));
state->callNoThrow(1);
}
};
}
doubleconsumer scripting::create_number_consumer(
int env,
const std::string& src,
const std::string& file
) {
return [=](double x){
if (processCallback(env, src, file)) {
state->pushnumber(x);
state->callNoThrow(1);
}
state->pushstring(util::wstr2str_utf8(x));
state->callNoThrow(1);
};
}
@ -93,17 +115,12 @@ int_array_consumer scripting::create_int_array_consumer(
const std::string& file
) {
return [=](const int arr[], size_t len){
try {
if (state->eval(env, src, file) == 0)
return;
} catch (lua::luaerror err) {
std::cerr << err.what() << std::endl;
return;
if (processCallback(env, src, file)) {
for (uint i = 0; i < len; i++) {
state->pushinteger(arr[i]);
}
state->callNoThrow(len);
}
for (uint i = 0; i < len; i++) {
state->pushinteger(arr[i]);
}
state->callNoThrow(len);
};
}
@ -129,6 +146,16 @@ std::unique_ptr<Environment> scripting::create_doc_environment(int parent, const
state->setfield("DOC_ENV");
state->pushstring(name.c_str());
state->setfield("DOC_NAME");
if (state->getglobal("Document")) {
if (state->getfield("new")) {
state->pushstring(name.c_str());
if (state->callNoThrow(1)) {
state->setfield("document", -3);
}
}
state->pop();
}
state->pop();
return std::make_unique<Environment>(id);
}

View File

@ -55,6 +55,12 @@ namespace scripting {
const std::string& file="<string>"
);
doubleconsumer create_number_consumer(
int env,
const std::string& src,
const std::string& file="<string>"
);
int_array_consumer create_int_array_consumer(
int env,
const std::string& src,

View File

@ -175,6 +175,9 @@ void Window::setBlendMode(blendmode mode) {
case blendmode::addition:
glBlendFunc(GL_SRC_ALPHA, GL_ONE);
break;
case blendmode::inversion:
glBlendFunc(GL_ONE_MINUS_DST_COLOR, GL_ONE_MINUS_SRC_ALPHA);
break;
}
}

View File

@ -15,7 +15,7 @@ struct DisplaySettings;
struct GLFWmonitor;
enum class blendmode {
normal, addition
normal, addition, inversion
};
class Window {