Skip to content

Commit

Permalink
Introducing ChatBalloon #13 [ci skip]
Browse files Browse the repository at this point in the history
  • Loading branch information
gbaudic committed Feb 13, 2019
1 parent 4f0f4ed commit 5470f1a
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 0 deletions.
54 changes: 54 additions & 0 deletions src/ui/chatballoon.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/* This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
* If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/.
*
* This Source Code Form is "Incompatible With Secondary Licenses",
* as defined by the Mozilla Public License, v. 2.0.
*/

#include <SDL2/SDL_ttf.h>
#include <SDL2/SDL2_gfxPrimitives.h>
#include "chatballoon.hpp"
using namespace std;

/**
* Constructor
* \param text message to display
* \param x x coord of the tip (bottom) of the balloon
* \param y y coord of the tip (bottom) of the balloon
*/
ChatBalloon::ChatBalloon(const string &text, const int x, const int y) : message(text),
_x(x), _y(y), nbCharsDisplayed(1), creationTime(SDL_GetTicks()) {
if(message.size() == 0) {
message = " ";
}
}

ChatBalloon::~ChatBalloon() {

}

void ChatBalloon::draw(SDL_Renderer *renderer) {
if(nbCharsDisplayed < message.size()) {
Uint32 delta = SDL_GetTicks() - creationTime;
nbCharsDisplayed = 1 + delta / (1000 / CHARACTERS_PER_SECOND);
}

int textWidth = nbCharsDisplayed >= BALLOON_WIDTH ? BALLOON_WIDTH : nbCharsDisplayed;
int nbLines = 1 + nbCharsDisplayed / BALLOON_WIDTH;
if(nbCharsDisplayed % BALLOON_WIDTH == 0) {
nbLines -= 1;
}

// Draw tip
filledTrigonRGBA(renderer, _x, _y, _x + 10, _y - 10, _x - 10, _y - 10, 0xff, 0xff, 0xff, 0xff);

// Draw balloon

// Draw text, line by line

}

bool ChatBalloon::isVisible() const {
return SDL_Ticks() <= creationTime + FULL_DELAY + (message.size() - 1) * 1000 / CHARACTERS_PER_SECOND;
}
37 changes: 37 additions & 0 deletions src/ui/chatballoon.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/* This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
* If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/.
*
* This Source Code Form is "Incompatible With Secondary Licenses",
* as defined by the Mozilla Public License, v. 2.0.
*/

#include <SDL2/SDL.h>
#include <string>

#ifndef _H_CHATBALLOON_
#define _H_CHATBALLOON_

class ChatBalloon final {
public:
const int BALLOON_WIDTH = 20; //! in characters
const int CHARACTERS_PER_SECOND = 10; //! used when deploying the balloon
const Uint32 FULL_DELAY = 5000; //! time to leave the full balloon visible

ChatBalloon(const std::string &text, const int x, const int y);
~ChatBalloon();

void draw(SDL_Renderer *renderer);
bool isVisible() const;

private:
std::string message;
int _x;
int _y;
int nbCharsDisplayed;
Uint32 creationTime;

const int PADDING = 5; //! in pixels
};

#endif // _H_CHATBALLOON_

0 comments on commit 5470f1a

Please sign in to comment.