game.loadFont("default", "Assets/FreePixel.ttf", 16)
+game.loadFont("dialog", "Assets/FreePixel.ttf", 16)
player = {
Player = 0,
self.Velocity.x = self.Velocity.x - 3.0
--self.Velocity.y = self.Velocity.y - 1.0
self.Render.flipx = true;
+ game.dialogClear()
end,
MoveLeftReleased = function(self)
self.Velocity.x = self.Velocity.x + 3.0
end,
JumpKeyReleased = function(self)
game.dialog(30, 150, 400, 100)
- game.puts("default", 36, 52, "Hey. Hag?")
+ game.puts("dialog", 36, 52, "Hey. Hag?")
end
},
Position = {
script->addToGameNamespace("dialog",
bindInstance(&UISystem::createDialogBox,
systems.system<UISystem>().get()));
+ script->addToGameNamespace("dialogClear",
+ [this] { events.emit<HideDialog>(); });
script->init();
--- /dev/null
+/*
+ * Copyright (C) 2022 Clyne Sullivan <clyne@bitgloo.com>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef EVENTS_UI_HPP_
+#define EVENTS_UI_HPP_
+
+struct HideDialog
+{
+ char unused;
+};
+
+#endif // EVENTS_UI_HPP
+
#include "text.hpp"
#include "events/render.hpp"
+#include "events/ui.hpp"
#include <iostream>
shouldUpdateVBOs = false;
events.subscribe<ShowTextEvent>(*this);
+ events.subscribe<HideDialog>(*this);
if (FT_Init_FreeType(&freetype) != 0) {
// TODO handle error
updateVBOs();
for (auto& [name, font] : fontData) {
- if (font.text.size() != 0) {
+ if (font.changed) {
+ font.changed = false;
+
events.emit<NewRenderEvent>(font.vbo, font.tex, 0,
font.buffer.size());
}
put(ste.font, ste.x, ste.y, ste.text);
}
+void TextSystem::receive(const HideDialog&)
+{
+ auto fd = fontData.find("dialog");
+
+ if (fd != fontData.end()) {
+ auto& font = fd->second;
+
+ font.text.clear();
+ font.changed = true;
+ }
+}
+
void TextSystem::loadFont(const std::string& name,
const std::string& file,
int size)
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
- // // convert red-on-black to RGBA
- // auto& g = face->glyph;
- // std::vector<uint32_t> buf (g->bitmap.width * g->bitmap.rows, 0xFFFFFF);
- // for (auto j = buf.size(); j--;)
- // buf[j] |= g->bitmap.buffer[j] << 24;
-
// Load each character and add it to the texture
//
<< font.tex << ")" << std::endl;
}
-void TextSystem::put(const std::string& font,
+void TextSystem::put(const std::string& fname,
float x,
float y,
const std::string& text)
{
- if (fontData.find(font) == fontData.end())
+ auto fd = fontData.find(fname);
+ if (fd == fontData.end())
return;
- auto& vector = fontData[font].text;
+ auto& font = fd->second;
+
+ auto& vector = font.text;
- y = -(y + fontData[font].fontSize);
+ y = -(y + font.fontSize);
const auto it = std::find_if(vector.begin(), vector.end(),
[&x, &y](const Text& t) {
*it = Text(text, x, y);
} else {
// Invert y axis so positive grows south.
- fontData[font].text.emplace_back(text, x, y);
+ vector.emplace_back(text, x, y);
}
+ font.changed = true;
shouldUpdateVBOs = true;
}
#ifndef SYSTEM_TEXT_HPP_
#define SYSTEM_TEXT_HPP_
+#include "events/ui.hpp"
+
#include <entityx/entityx.h>
#include <ft2build.h>
#include <freetype/freetype.h>
float width;
float height;
+ bool changed = false;
std::array<FT_Info, 96> data;
// Stores currently shown text at given index into VBO?
entityx::TimeDelta dt) final;
void receive(const ShowTextEvent&);
+ void receive(const HideDialog&);
void put(const std::string& font, float x, float y, const std::string& text);
static const unsigned int NRE_TEX_DATA = 0xBBBBBBBB;
-void UISystem::configure(entityx::EntityManager&, entityx::EventManager&)
+void UISystem::configure(entityx::EntityManager&, entityx::EventManager &events)
{
+ events.subscribe<HideDialog>(*this);
}
void UISystem::createDialogBox(float x, float y, float w, float h)
entityx::EventManager& events,
entityx::TimeDelta)
{
- for (auto& b : m_boxes) {
- if (b.vbo == 0) {
- auto nre = generateDialogBox(b);
- events.emit<NewRenderEvent>(nre);
+ if (m_clear_boxes) {
+ m_clear_boxes = false;
+
+ for (const auto& b : m_boxes) {
+ if (b.vbo != 0) {
+ events.emit<DelRenderEvent>(b.vbo);
+ }
+ }
+
+ m_boxes.clear();
+ } else {
+ for (auto& b : m_boxes) {
+ if (b.vbo == 0) {
+ auto nre = generateDialogBox(b);
+ events.emit<NewRenderEvent>(nre);
+ }
}
}
}
+void UISystem::receive(const HideDialog&)
+{
+ m_clear_boxes = true;
+}
+
NewRenderEvent UISystem::generateDialogBox(Box& box)
{
NewRenderEvent nre;
#define SYSTEM_UI_HPP_
#include "events/render.hpp"
+#include "events/ui.hpp"
#include <entityx/entityx.h>
#include <GL/glew.h>
-#include <SDL2/SDL.h>
-#include <SDL2/SDL_opengl.h>
#include <vector>
-class UISystem : public entityx::System<UISystem>
+class UISystem : public entityx::System<UISystem>,
+ public entityx::Receiver<UISystem>
{
public:
void configure(entityx::EntityManager&, entityx::EventManager&) final;
entityx::EventManager&,
entityx::TimeDelta) final;
+ void receive(const HideDialog &hd);
+
void createDialogBox(float x, float y, float w, float h);
private:
};
std::vector<Box> m_boxes;
+ bool m_clear_boxes = false;
NewRenderEvent generateDialogBox(Box& box);
};