new crosshair + minor refactor
This commit is contained in:
parent
1145267308
commit
ef81a024c9
BIN
res/textures/gui/crosshair.png
Normal file
BIN
res/textures/gui/crosshair.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 5.1 KiB |
@ -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_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/moon.png", "misc/moon");
|
||||||
loader.add(ASSET_TEXTURE, TEXTURES_FOLDER"/misc/sun.png", "misc/sun");
|
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);
|
addLayouts(0, "core", loader.getPaths()->getMainRoot()/fs::path("layouts"), loader);
|
||||||
for (auto& pack : content->getPacks()) {
|
for (auto& pack : content->getPacks()) {
|
||||||
|
|||||||
@ -49,9 +49,6 @@
|
|||||||
#include "../items/Inventory.h"
|
#include "../items/Inventory.h"
|
||||||
#include "../logic/scripting/scripting.h"
|
#include "../logic/scripting/scripting.h"
|
||||||
|
|
||||||
using glm::vec2;
|
|
||||||
using glm::vec3;
|
|
||||||
using glm::vec4;
|
|
||||||
using namespace gui;
|
using namespace gui;
|
||||||
|
|
||||||
static std::shared_ptr<Label> create_label(wstringsupplier supplier) {
|
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) {
|
std::shared_ptr<UINode> HudRenderer::createDebugPanel(Engine* engine) {
|
||||||
auto level = frontend->getLevel();
|
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]() {
|
panel->listenInterval(0.5f, [this]() {
|
||||||
fpsString = std::to_wstring(fpsMax)+L" / "+std::to_wstring(fpsMin);
|
fpsString = std::to_wstring(fpsMax)+L" / "+std::to_wstring(fpsMin);
|
||||||
fpsMin = fps;
|
fpsMin = fps;
|
||||||
fpsMax = 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([this](){ return L"fps: "+this->fpsString;}));
|
||||||
panel->add(create_label([](){
|
panel->add(create_label([](){
|
||||||
return L"meshes: " + std::to_wstring(Mesh::meshesCount);
|
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++){
|
for (int ax = 0; ax < 3; ax++){
|
||||||
auto sub = std::make_shared<Container>(vec2(), vec2(250, 27));
|
auto sub = std::make_shared<Container>(glm::vec2(), glm::vec2(250, 27));
|
||||||
|
|
||||||
std::wstring str = L"x: ";
|
std::wstring str = L"x: ";
|
||||||
str[0] += ax;
|
str[0] += ax;
|
||||||
auto label = std::make_shared<Label>(str);
|
auto label = std::make_shared<Label>(str);
|
||||||
label->setMargin(vec4(2, 3, 2, 3));
|
label->setMargin(glm::vec4(2, 3, 2, 3));
|
||||||
label->setSize(vec2(20, 27));
|
label->setSize(glm::vec2(20, 27));
|
||||||
sub->add(label);
|
sub->add(label);
|
||||||
sub->setColor(vec4(0.0f));
|
sub->setColor(glm::vec4(0.0f));
|
||||||
|
|
||||||
// Coord input
|
// Coord input
|
||||||
auto box = std::make_shared<TextBox>(L"");
|
auto box = std::make_shared<TextBox>(L"");
|
||||||
@ -118,7 +115,7 @@ std::shared_ptr<UINode> HudRenderer::createDebugPanel(Engine* engine) {
|
|||||||
});
|
});
|
||||||
box->textConsumer([=](std::wstring text) {
|
box->textConsumer([=](std::wstring text) {
|
||||||
try {
|
try {
|
||||||
vec3 position = level->player->hitbox->position;
|
glm::vec3 position = level->player->hitbox->position;
|
||||||
position[ax] = std::stoi(text);
|
position[ax] = std::stoi(text);
|
||||||
level->player->teleport(position);
|
level->player->teleport(position);
|
||||||
} catch (std::invalid_argument& _){
|
} catch (std::invalid_argument& _){
|
||||||
@ -128,9 +125,9 @@ std::shared_ptr<UINode> HudRenderer::createDebugPanel(Engine* engine) {
|
|||||||
Hitbox* hitbox = level->player->hitbox.get();
|
Hitbox* hitbox = level->player->hitbox.get();
|
||||||
box->setText(std::to_wstring(int(hitbox->position[ax])));
|
box->setText(std::to_wstring(int(hitbox->position[ax])));
|
||||||
});
|
});
|
||||||
box->setSize(vec2(230, 27));
|
box->setSize(glm::vec2(230, 27));
|
||||||
|
|
||||||
sub->add(box, vec2(20, 0));
|
sub->add(box, glm::vec2(20, 0));
|
||||||
panel->add(sub);
|
panel->add(sub);
|
||||||
}
|
}
|
||||||
panel->add(create_label([=](){
|
panel->add(create_label([=](){
|
||||||
@ -156,7 +153,7 @@ std::shared_ptr<UINode> HudRenderer::createDebugPanel(Engine* engine) {
|
|||||||
}
|
}
|
||||||
{
|
{
|
||||||
auto checkbox = std::make_shared<FullCheckBox>(
|
auto checkbox = std::make_shared<FullCheckBox>(
|
||||||
L"Show Chunk Borders", vec2(400, 24)
|
L"Show Chunk Borders", glm::vec2(400, 24)
|
||||||
);
|
);
|
||||||
checkbox->setSupplier([=]() {
|
checkbox->setSupplier([=]() {
|
||||||
return engine->getSettings().debug.showChunkBorders;
|
return engine->getSettings().debug.showChunkBorders;
|
||||||
@ -237,7 +234,7 @@ HudRenderer::HudRenderer(Engine* engine, LevelFrontend* frontend)
|
|||||||
|
|
||||||
contentAccess = createContentAccess();
|
contentAccess = createContentAccess();
|
||||||
contentAccessPanel = std::make_shared<Panel>(
|
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->setColor(glm::vec4());
|
||||||
contentAccessPanel->add(contentAccess);
|
contentAccessPanel->add(contentAccess);
|
||||||
@ -247,8 +244,9 @@ HudRenderer::HudRenderer(Engine* engine, LevelFrontend* frontend)
|
|||||||
darkOverlay = std::make_unique<Panel>(glm::vec2(4000.0f));
|
darkOverlay = std::make_unique<Panel>(glm::vec2(4000.0f));
|
||||||
darkOverlay->setColor(glm::vec4(0, 0, 0, 0.5f));
|
darkOverlay->setColor(glm::vec4(0, 0, 0, 0.5f));
|
||||||
darkOverlay->setZIndex(-1);
|
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->perspective = false;
|
||||||
uicamera->flipped = true;
|
uicamera->flipped = true;
|
||||||
|
|
||||||
@ -265,6 +263,7 @@ HudRenderer::HudRenderer(Engine* engine, LevelFrontend* frontend)
|
|||||||
}
|
}
|
||||||
|
|
||||||
HudRenderer::~HudRenderer() {
|
HudRenderer::~HudRenderer() {
|
||||||
|
// removing all controlled ui
|
||||||
gui->remove(grabbedItemView);
|
gui->remove(grabbedItemView);
|
||||||
if (inventoryView) {
|
if (inventoryView) {
|
||||||
gui->remove(inventoryView);
|
gui->remove(inventoryView);
|
||||||
@ -287,23 +286,20 @@ void HudRenderer::update(bool visible) {
|
|||||||
auto menu = gui->getMenu();
|
auto menu = gui->getMenu();
|
||||||
|
|
||||||
debugPanel->setVisible(player->debug && visible);
|
debugPanel->setVisible(player->debug && visible);
|
||||||
menu->setVisible(pause);
|
|
||||||
|
|
||||||
if (!visible && inventoryOpen) {
|
if (!visible && inventoryOpen) {
|
||||||
closeInventory();
|
closeInventory();
|
||||||
}
|
}
|
||||||
if (pause && menu->getCurrent().panel == nullptr) {
|
if (pause && menu->getCurrent().panel == nullptr) {
|
||||||
pause = false;
|
setPause(false);
|
||||||
}
|
}
|
||||||
if (Events::jpressed(keycode::ESCAPE) && !gui->isFocusCaught()) {
|
if (Events::jpressed(keycode::ESCAPE) && !gui->isFocusCaught()) {
|
||||||
if (pause) {
|
if (pause) {
|
||||||
pause = false;
|
setPause(false);
|
||||||
menu->reset();
|
|
||||||
} else if (inventoryOpen) {
|
} else if (inventoryOpen) {
|
||||||
closeInventory();
|
closeInventory();
|
||||||
} else {
|
} else {
|
||||||
pause = true;
|
setPause(true);
|
||||||
menu->setPage("pause");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (visible && Events::jactive(BIND_HUD_INVENTORY)) {
|
if (visible && Events::jactive(BIND_HUD_INVENTORY)) {
|
||||||
@ -319,9 +315,9 @@ void HudRenderer::update(bool visible) {
|
|||||||
Events::toggleCursor();
|
Events::toggleCursor();
|
||||||
}
|
}
|
||||||
|
|
||||||
vec2 invSize = contentAccessPanel->getSize();
|
glm::vec2 invSize = contentAccessPanel->getSize();
|
||||||
contentAccessPanel->setVisible(inventoryOpen);
|
contentAccessPanel->setVisible(inventoryOpen);
|
||||||
contentAccessPanel->setSize(vec2(invSize.x, Window::height));
|
contentAccessPanel->setSize(glm::vec2(invSize.x, Window::height));
|
||||||
hotbarView->setVisible(visible);
|
hotbarView->setVisible(visible);
|
||||||
|
|
||||||
for (int i = keycode::NUM_1; i <= keycode::NUM_9; i++) {
|
for (int i = keycode::NUM_1; i <= keycode::NUM_9; i++) {
|
||||||
@ -340,10 +336,11 @@ void HudRenderer::update(bool visible) {
|
|||||||
}
|
}
|
||||||
player->setChosenSlot(slot);
|
player->setChosenSlot(slot);
|
||||||
}
|
}
|
||||||
|
|
||||||
darkOverlay->setVisible(pause);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Show inventory on the screen and turn on inventory mode blocking movement
|
||||||
|
*/
|
||||||
void HudRenderer::openInventory() {
|
void HudRenderer::openInventory() {
|
||||||
auto level = frontend->getLevel();
|
auto level = frontend->getLevel();
|
||||||
auto player = level->player;
|
auto player = level->player;
|
||||||
@ -359,6 +356,9 @@ void HudRenderer::openInventory() {
|
|||||||
gui->add(inventoryView);
|
gui->add(inventoryView);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Hide inventory and turn off inventory mode
|
||||||
|
*/
|
||||||
void HudRenderer::closeInventory() {
|
void HudRenderer::closeInventory() {
|
||||||
scripting::on_ui_close(inventoryDocument, inventoryView->getInventory().get());
|
scripting::on_ui_close(inventoryDocument, inventoryView->getInventory().get());
|
||||||
inventoryOpen = false;
|
inventoryOpen = false;
|
||||||
@ -371,13 +371,12 @@ void HudRenderer::closeInventory() {
|
|||||||
|
|
||||||
void HudRenderer::draw(const GfxContext& ctx){
|
void HudRenderer::draw(const GfxContext& ctx){
|
||||||
auto level = frontend->getLevel();
|
auto level = frontend->getLevel();
|
||||||
|
auto player = level->player;
|
||||||
|
|
||||||
const Viewport& viewport = ctx.getViewport();
|
const Viewport& viewport = ctx.getViewport();
|
||||||
const uint width = viewport.getWidth();
|
const uint width = viewport.getWidth();
|
||||||
const uint height = viewport.getHeight();
|
const uint height = viewport.getHeight();
|
||||||
|
|
||||||
Player* player = level->player;
|
|
||||||
|
|
||||||
uicamera->setFov(height);
|
uicamera->setFov(height);
|
||||||
|
|
||||||
auto batch = ctx.getBatch2D();
|
auto batch = ctx.getBatch2D();
|
||||||
@ -391,16 +390,21 @@ void HudRenderer::draw(const GfxContext& ctx){
|
|||||||
hotbarView->setSelected(player->getChosenSlot());
|
hotbarView->setSelected(player->getChosenSlot());
|
||||||
|
|
||||||
// Crosshair
|
// Crosshair
|
||||||
batch->begin();
|
|
||||||
if (!pause && Events::_cursor_locked && !level->player->debug) {
|
if (!pause && Events::_cursor_locked && !level->player->debug) {
|
||||||
batch->lineWidth(2);
|
GfxContext chctx = ctx.sub();
|
||||||
batch->line(width/2, height/2-6, width/2, height/2+6, 0.2f, 0.2f, 0.2f, 1.0f);
|
chctx.blendMode(blendmode::inversion);
|
||||||
batch->line(width/2+6, height/2, width/2-6, height/2, 0.2f, 0.2f, 0.2f, 1.0f);
|
batch->texture(assets->getTexture("gui/crosshair"));
|
||||||
batch->line(width/2-5, height/2-5, width/2+5, height/2+5, 0.9f, 0.9f, 0.9f, 1.0f);
|
int chsize = 16;
|
||||||
batch->line(width/2+5, height/2-5, width/2-5, height/2+5, 0.9f, 0.9f, 0.9f, 1.0f);
|
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) {
|
if (level->player->debug) {
|
||||||
|
batch->texture(nullptr);
|
||||||
const int dmwidth = 256;
|
const int dmwidth = 256;
|
||||||
const float dmscale = 4000.0f;
|
const float dmscale = 4000.0f;
|
||||||
static float deltameter[dmwidth]{};
|
static float deltameter[dmwidth]{};
|
||||||
@ -417,15 +421,13 @@ void HudRenderer::draw(const GfxContext& ctx){
|
|||||||
|
|
||||||
if (inventoryOpen) {
|
if (inventoryOpen) {
|
||||||
float caWidth = contentAccess->getSize().x;
|
float caWidth = contentAccess->getSize().x;
|
||||||
|
contentAccessPanel->setCoord(glm::vec2(width-caWidth, 0));
|
||||||
|
|
||||||
glm::vec2 invSize = inventoryView->getSize();
|
glm::vec2 invSize = inventoryView->getSize();
|
||||||
|
|
||||||
float width = viewport.getWidth();
|
|
||||||
|
|
||||||
inventoryView->setCoord(glm::vec2(
|
inventoryView->setCoord(glm::vec2(
|
||||||
glm::min(width/2-invSize.x/2, width-caWidth-10-invSize.x),
|
glm::min(width/2-invSize.x/2, width-caWidth-10-invSize.x),
|
||||||
height/2-invSize.y/2
|
height/2-invSize.y/2
|
||||||
));
|
));
|
||||||
contentAccessPanel->setCoord(glm::vec2(width-caWidth, 0));
|
|
||||||
}
|
}
|
||||||
grabbedItemView->setCoord(glm::vec2(Events::cursor));
|
grabbedItemView->setCoord(glm::vec2(Events::cursor));
|
||||||
batch->render();
|
batch->render();
|
||||||
@ -438,3 +440,19 @@ bool HudRenderer::isInventoryOpen() const {
|
|||||||
bool HudRenderer::isPause() const {
|
bool HudRenderer::isPause() const {
|
||||||
return pause;
|
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);
|
||||||
|
}
|
||||||
|
|||||||
@ -64,6 +64,7 @@ public:
|
|||||||
|
|
||||||
bool isInventoryOpen() const;
|
bool isInventoryOpen() const;
|
||||||
bool isPause() const;
|
bool isPause() const;
|
||||||
|
void setPause(bool pause);
|
||||||
|
|
||||||
void openInventory();
|
void openInventory();
|
||||||
void closeInventory();
|
void closeInventory();
|
||||||
|
|||||||
@ -175,6 +175,9 @@ void Window::setBlendMode(blendmode mode) {
|
|||||||
case blendmode::addition:
|
case blendmode::addition:
|
||||||
glBlendFunc(GL_SRC_ALPHA, GL_ONE);
|
glBlendFunc(GL_SRC_ALPHA, GL_ONE);
|
||||||
break;
|
break;
|
||||||
|
case blendmode::inversion:
|
||||||
|
glBlendFunc(GL_ONE_MINUS_DST_COLOR, GL_ONE_MINUS_SRC_ALPHA);
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -15,7 +15,7 @@ struct DisplaySettings;
|
|||||||
struct GLFWmonitor;
|
struct GLFWmonitor;
|
||||||
|
|
||||||
enum class blendmode {
|
enum class blendmode {
|
||||||
normal, addition
|
normal, addition, inversion
|
||||||
};
|
};
|
||||||
|
|
||||||
class Window {
|
class Window {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user