From a33b3d4ffc1defda5bdcd3348036ce48ef5b0085 Mon Sep 17 00:00:00 2001 From: Clyne Sullivan Date: Thu, 27 Apr 2017 17:40:12 -0400 Subject: modernized ui --- include/ui.hpp | 130 +++++++++++++++++++++++++-------------------------------- 1 file changed, 56 insertions(+), 74 deletions(-) (limited to 'include/ui.hpp') diff --git a/include/ui.hpp b/include/ui.hpp index ac415f5..456c88a 100644 --- a/include/ui.hpp +++ b/include/ui.hpp @@ -32,112 +32,94 @@ public: void update(entityx::EntityManager &en, entityx::EventManager &ev, entityx::TimeDelta dt) override; }; -namespace ui { +struct OptionDim { + float x; + float y; + float width; +}; - extern bool fadeEnable; - extern int fadeIntensity; +using DialogOption = std::pair; - // the pixel-coordinates of the mouse - extern vec2 mouse; +class UISystem : public entityx::System { +private: + static bool fadeEnable; + static bool fadeFast; + static int fadeIntensity; - // raw mouse values from SDL - extern vec2 premouse; + static std::string dialogText; + static std::string importantText; + static std::vector dialogOptions; + static int dialogOptionResult; - // the currently used font size for text rendering - extern unsigned int fontSize; +public: + UISystem(void) {} - // shows the debug overlay when set to true - extern bool debug; + void update(entityx::EntityManager& en, entityx::EventManager& ev, entityx::TimeDelta dt) override; - // shows tracers when set to true (alongside `debug`) - extern bool posFlag; + static void render(void); - extern unsigned char dialogOptChosen; - extern bool dialogBoxExists; - extern bool dialogImportant; - extern bool dialogPassive; + /*** + * Fade library + */ - extern unsigned int textWrapLimit; - extern int fontTransInv; + static void fadeToggle(void); + static void fadeToggleFast(void); + static void waitForCover(void); + static void waitForUncover(void); - /* - * Initializes the FreeType system. - */ - void initFonts(void); - void initSounds(void); + static inline bool isFading(void) + { return fadeIntensity != 0; } - void destroyFonts(void); + static inline bool isDialog(void) + { return !dialogText.empty() || !importantText.empty(); } - /* - * Sets the current font/font size. - */ + /** + * Text library + */ - void setFontFace(const char *ttf); - void setFontSize(unsigned int size); - void setFontColor(int r, int g, int b, int a); - void setFontZ(float z); + static void putText(const vec2& p, const std::string& s, ...); + static void putString(const vec2& p, const std::string& s, float wrap = 0.12345f); + static float putStringCentered(const vec2& p, const std::string& s, bool print = true); - /* - * Draw a centered string. - */ + static void dialogBox(const std::string& n, const std::string& s, ...); + static void dialogAddOption(const std::string& o); + static void dialogImportant(const std::string& s); - float putStringCentered(const float x,const float y,std::string s); + static void waitForDialog(void); + static void advanceDialog(void); + static int getDialogResult(void); +}; - /* - * Draws a formatted string at the given coordinates. - */ +namespace ui { + // the pixel-coordinates of the mouse + extern vec2 mouse; - float putText(const float x,const float y,const char *str,...); + // raw mouse values from SDL + extern vec2 premouse; - /** - * This function is a facility for logic events to draw text; the text - * will be prepared then drawn in the render loop. - */ - void putTextL(vec2 c,const char *str, ...); + // shows the debug overlay when set to true + extern bool debug; - /* - * Creates a dialogBox text string (format: `name`: `text`). This function simply sets up - * variables that are drawn in ui::draw(). When the dialog box exists player control is - * limited until a right click is given, closing the box. - */ + // shows tracers when set to true (alongside `debug`) + extern bool posFlag; + + void initSounds(void); - void drawBox(vec2 c1, vec2 c2); void drawNiceBox(vec2 c1, vec2 c2, float z); void drawNiceBoxColor(vec2 c1, vec2 c2, float z, Color c); - void dialogBox(std::string name, std::string opt, bool passive, std::string text, ...); - void closeBox(); - void waitForDialog(void); bool pageExists(void); void drawPage(const GLuint& tex); - void dontTypeOut(void); - /* - * Draws a larger string in the center of the screen. Drawing is done inside this function. - */ - - void importantText(const char *text,...); - void passiveImportantText(int duration,const char *text,...); + //void importantText(const char *text,...); + //void passiveImportantText(int duration,const char *text,...); /* * Draw various UI elements (dialogBox, player health) */ void draw(void); - void drawFade(void); - void fadeUpdate(void); - - /* - * Toggle the black overlay thing. - */ - - void toggleBlack(void); - void toggleBlackFast(void); - void toggleWhite(void); - void toggleWhiteFast(void); - void waitForCover(void); - void waitForUncover(void); /* * Takes a screenshot of the game -- cgit v1.2.3 From 00de7a4b0aa48c3cb42c45e0f203902ca034b94c Mon Sep 17 00:00:00 2001 From: Clyne Sullivan Date: Thu, 27 Apr 2017 21:28:33 -0400 Subject: important textls --- assets/music/town.ogg | Bin 0 -> 3159813 bytes include/components.hpp | 16 ++++++++++++++++ include/ui.hpp | 2 ++ main.cpp | 4 +++- src/components.cpp | 32 ++++++++++++++++++++++++-------- src/ui.cpp | 22 +++++++++++++++++++--- src/world.cpp | 2 ++ xml/!town.xml | 2 +- xml/entities.xml | 9 +++++++++ 9 files changed, 76 insertions(+), 13 deletions(-) create mode 100644 assets/music/town.ogg (limited to 'include/ui.hpp') diff --git a/assets/music/town.ogg b/assets/music/town.ogg new file mode 100644 index 0000000..0cbe3a7 Binary files /dev/null and b/assets/music/town.ogg differ diff --git a/include/components.hpp b/include/components.hpp index dafb859..87d4293 100644 --- a/include/components.hpp +++ b/include/components.hpp @@ -623,6 +623,22 @@ struct Hit : public Component { } }; +struct Trigger : public Component { + Trigger(const std::string& t) + : text(t) {} + Trigger(XMLElement* imp, XMLElement* def) { + fromXML(imp, def); + } + + std::string text; + + void fromXML(XMLElement* imp, XMLElement* def) final { + (void)imp; + (void)def; + text = "You got me!"; + } +}; + /** * SYSTEMS */ diff --git a/include/ui.hpp b/include/ui.hpp index 456c88a..3f0a67f 100644 --- a/include/ui.hpp +++ b/include/ui.hpp @@ -126,6 +126,8 @@ namespace ui { */ void takeScreenshot(GLubyte *pixels); + + bool handleGLEvent(SDL_Event& e); } #endif // UI_HPP_ diff --git a/main.cpp b/main.cpp index 967050c..a934b40 100644 --- a/main.cpp +++ b/main.cpp @@ -166,8 +166,10 @@ int main(int argc, char *argv[]) Render::render(fps); SDL_Event e; - while (SDL_PollEvent(&e)) + while (SDL_PollEvent(&e)) { + ui::handleGLEvent(e); eventQueue.push_back(e); + } } // on game end, get back together diff --git a/src/components.cpp b/src/components.cpp index d89e195..dcb1551 100644 --- a/src/components.cpp +++ b/src/components.cpp @@ -50,11 +50,9 @@ void MovementSystem::update(entityx::EntityManager &en, entityx::EventManager &e fl = (direction.x < 0); } - // make the entity wander - // TODO initialX and range? - if (entity.has_component()) { - auto ppos = game::engine.getSystem()->getPosition(); - if (ppos.x > position.x && ppos.x < position.x + entity.component()->width) { + auto ppos = game::engine.getSystem()->getPosition(); + if (ppos.x > position.x && ppos.x < position.x + entity.component()->width) { + if (entity.has_component()) { auto dim = entity.component(); ev.emit(vec2(position.x + dim->width, position.y + dim->height), AttackType::ShortSlash, false); @@ -64,9 +62,27 @@ void MovementSystem::update(entityx::EntityManager &en, entityx::EventManager &e toFight = entity; h = 0; }*/ - } else - direction.x = (ppos.x > position.x) ? .01 : -.01; - } else if (entity.has_component()) { + } else if (entity.has_component()) { + static bool triggering = false; + if (!triggering) { + triggering = true; + std::thread([&](entityx::Entity e) { + UISystem::fadeToggle(); + UISystem::waitForCover(); + UISystem::dialogImportant(e.component()->text); + UISystem::waitForDialog(); + UISystem::fadeToggle(); + e.destroy(); + triggering = false; + }, entity).detach(); + } + return; + } + } + + // make the entity wander + // TODO initialX and range? + if (entity.has_component()) { auto& countdown = entity.component()->countdown; if (countdown > 0) { diff --git a/src/ui.cpp b/src/ui.cpp index fc815f2..d14b0a6 100644 --- a/src/ui.cpp +++ b/src/ui.cpp @@ -618,6 +618,20 @@ namespace ui { fclose(bmp); } + + bool handleGLEvent(SDL_Event& e) { + switch (e.type) { + case SDL_MOUSEBUTTONDOWN: + if ((UISystem::isDialog() | pageTexReady) && (e.button.button & SDL_BUTTON_RIGHT)) + UISystem::advanceDialog(); + return true; + break; + default: + break; + } + + return false; + } } using namespace ui; @@ -658,8 +672,6 @@ void InputSystem::receive(const MainSDLEvent& event) case SDL_MOUSEBUTTONDOWN: ev.emit(mouse, e.button.button); - UISystem::advanceDialog(); - if (UISystem::isDialog() || pageTexReady) { if ((e.button.button & SDL_BUTTON_RIGHT)) UISystem::advanceDialog(); @@ -974,6 +986,10 @@ void UISystem::render(void) } if (!importantText.empty()) { - putStringCentered(offset, importantText); + FontSystem::setFontSize(24); + FontSystem::setFontZ(-9.0f); + putStringCentered(vec2(offset.x, 400), importantText); + FontSystem::setFontZ(-6.0f); + FontSystem::setFontSize(16); } } diff --git a/src/world.cpp b/src/world.cpp index 648507a..e251706 100644 --- a/src/world.cpp +++ b/src/world.cpp @@ -389,6 +389,8 @@ void WorldSystem::load(const std::string& file) entity.assign(wxml, abcd); else if (tname == "Animation") entity.assign(wxml, abcd); + else if (tname == "Trigger") + entity.assign(wxml, abcd); abcd = abcd->NextSiblingElement(); } diff --git a/xml/!town.xml b/xml/!town.xml index e3fb946..32f696a 100644 --- a/xml/!town.xml +++ b/xml/!town.xml @@ -2,7 +2,7 @@ -