Skip to content

Commit

Permalink
initial keyboard gui, disable cache to fix font rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
vgmoose committed Jul 22, 2018
1 parent 4c5293b commit 68015be
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 8 deletions.
8 changes: 8 additions & 0 deletions gui/AppList.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "AppList.hpp"
#include "AppCard.hpp"
#include "Keyboard.hpp"
#include <SDL2/SDL2_gfxPrimitives.h>

AppList::AppList(Get* get, Sidebar* sidebar)
Expand Down Expand Up @@ -232,4 +233,11 @@ void AppList::update()
TextElement* category = new TextElement(this->sidebar->currentCatName().c_str(), 28, &black);
category->position(20, 90);
this->elements.push_back(category);

// if it's a search, add a keyboard
if (curCategoryValue == "_search")
{
Keyboard* keyboard = new Keyboard();
this->elements.push_back(keyboard);
}
}
37 changes: 37 additions & 0 deletions gui/Keyboard.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#include "Keyboard.hpp"

Keyboard::Keyboard()
{

}

void Keyboard::render(Element* parent)
{
SDL_Rect dimens = { 880, 0, 400, 150 };

this->window = parent->window;
this->renderer = parent->renderer;

SDL_SetRenderDrawColor(parent->renderer, 0xf9, 0xf9, 0xf9, 0xFF);
SDL_RenderFillRect(parent->renderer, &dimens);

// draw a qwerty keyboard
std::string row1 = "Q W E R T Y U I O P";
std::string row2 = "A S D F G H J K L";
std::string row3 = "Z X C V B N M";

std::vector<std::string> rows = std::vector<std::string>();
rows.push_back(row1);
rows.push_back(row2);
rows.push_back(row3);
SDL_Color gray = { 0x52, 0x52, 0x52, 0xff };

for (int x=0; x<rows.size(); x++)
{
TextElement* rowText = new TextElement(rows[x].c_str(), 30, &gray, true);
rowText->position(910+x*22, 14+x*33);
this->elements.push_back(rowText);
}

super::render(this);
}
12 changes: 12 additions & 0 deletions gui/Keyboard.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#include "Element.hpp"
#include "TextElement.hpp"
#include <SDL2/SDL_image.h>
#pragma once

class Keyboard : public Element
{
public:
Keyboard();
void render(Element* parent);
// bool process(InputEvents* event);
};
18 changes: 12 additions & 6 deletions gui/TextElement.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "MainDisplay.hpp"

TextElement::TextElement(const char* text, int size, SDL_Color* color)
TextElement::TextElement(const char* text, int size, SDL_Color* color, bool monospaced)
{
this->text = new std::string(text);
this->size = size;
Expand All @@ -10,7 +10,7 @@ TextElement::TextElement(const char* text, int size, SDL_Color* color)
else
this->color = *color;

this->textSurface = this->renderText(*(this->text), size);
this->textSurface = this->renderText(*(this->text), size, monospaced);
}

void TextElement::render(Element* parent)
Expand All @@ -32,17 +32,23 @@ void TextElement::render(Element* parent)

}

SDL_Texture* TextElement::renderText(std::string& message, int size)
SDL_Texture* TextElement::renderText(std::string& message, int size, bool monospaced)
{
std::string key = message + std::to_string(size);

// try to find it in the cache first
if (ImageCache::cache.count(key))
return ImageCache::cache[key];
// TODO: fix cache?
// if (ImageCache::cache.count(key))
// return ImageCache::cache[key];

// not found, make/render it

TTF_Font *font = TTF_OpenFont("./res/opensans.ttf", size);
TTF_Font* font;

if (monospaced)
font = TTF_OpenFont("./res/mono.ttf", size);
else
font = TTF_OpenFont("./res/opensans.ttf", size);

// font couldn't load, don't render anything
if (!font)
Expand Down
4 changes: 2 additions & 2 deletions gui/TextElement.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
class TextElement : public Element
{
public:
TextElement(const char* text, int size, SDL_Color* color = 0);
TextElement(const char* text, int size, SDL_Color* color = 0, bool monospaced = false);
void render(Element* parent);
SDL_Texture* renderText(std::string& message, int size);
SDL_Texture* renderText(std::string& message, int size, bool monospaced);


int width, height;
Expand Down

0 comments on commit 68015be

Please sign in to comment.