Commit 3af18c64 authored by Jean-Baptiste Demonte's avatar Jean-Baptiste Demonte

set keyboard width relative to the screen (fix #190)

parent e81047fd
......@@ -48,19 +48,40 @@ bool ButtonComponent::input(InputConfig* config, Input input)
return GuiComponent::input(config, input);
}
void ButtonComponent::setText(const std::string& text, const std::string& helpText, bool upperCase)
void ButtonComponent::setText(const std::string& text, const std::string& helpText, bool upperCase, bool resize)
{
mText = upperCase ? strToUpper(text) : text;
mHelpText = helpText;
mTextCache = std::unique_ptr<TextCache>(mFont->buildTextCache(mText, 0, 0, getCurTextColor()));
float minWidth = mFont->sizeText("DELETE").x() + 12;
setSize(std::max(mTextCache->metrics.size.x() + 12, minWidth), mTextCache->metrics.size.y() + 8);
if (resize)
{
float minWidth = mFont->sizeText("DELETE").x() + 12;
setSize(std::max(mTextCache->metrics.size.x() + 12, minWidth), mTextCache->metrics.size.y() + 8);
}
updateHelpPrompts();
}
void ButtonComponent::autoSizeFont()
{
if (mFont->sizeText(mText).x() + 6 > getSize().x()) {
mFont = Font::get(FONT_SIZE_SMALL);
onSizeChanged();
}
if (mFont->sizeText(mText).x() + 6 > getSize().x()) {
mFont = Font::get(FONT_SIZE_EXTRASMALL);
onSizeChanged();
}
}
bool ButtonComponent::isTextOverlapping()
{
// + 2 to keep a small space between text and border
return mFont->sizeText(mText).x() + 2 > getSize().x();
}
void ButtonComponent::onFocusGained()
{
mFocused = true;
......
......@@ -18,7 +18,9 @@ public:
bool input(InputConfig* config, Input input) override;
void render(const Eigen::Affine3f& parentTrans) override;
void setText(const std::string& text, const std::string& helpText, bool upperCase = true);
void setText(const std::string& text, const std::string& helpText, bool upperCase = true, bool resize = true);
void autoSizeFont();
bool isTextOverlapping();
inline const std::string& getText() const { return mText; };
inline const std::function<void()>& getPressedFunc() const { return mPressedFunc; };
......
......@@ -6,7 +6,7 @@
#include "MenuThemeData.h"
#define BUTTON_GRID_VERT_PADDING 32
#define BUTTON_GRID_HORIZ_PADDING 10
#define BUTTON_GRID_HORIZ_PADDING 20
#define TITLE_HEIGHT (mTitle->getFont()->getLetterHeight() + TITLE_VERT_PADDING)
......@@ -177,61 +177,65 @@ std::vector<HelpPrompt> MenuComponent::getHelpPrompts()
std::shared_ptr<ComponentGrid> makeButtonGrid(Window* window, const std::vector< std::shared_ptr<ButtonComponent> >& buttons)
{
std::shared_ptr<ComponentGrid> buttonGrid = std::make_shared<ComponentGrid>(window, Vector2i(buttons.size(), 2));
std::shared_ptr<ComponentGrid> grid = std::make_shared<ComponentGrid>(window, Vector2i(buttons.size(), 2));
float buttonGridWidth = (float)BUTTON_GRID_HORIZ_PADDING * buttons.size(); // initialize to padding
float gridWidth = (float)BUTTON_GRID_HORIZ_PADDING * buttons.size(); // initialize to padding
for(int i = 0; i < (int)buttons.size(); i++)
{
buttonGrid->setEntry(buttons.at(i), Vector2i(i, 0), true, false);
buttonGridWidth += buttons.at(i)->getSize().x();
grid->setEntry(buttons.at(i), Vector2i(i, 0), true, false);
gridWidth += buttons.at(i)->getSize().x();
}
for(unsigned int i = 0; i < buttons.size(); i++)
{
buttonGrid->setColWidthPerc(i, (buttons.at(i)->getSize().x() + BUTTON_GRID_HORIZ_PADDING) / buttonGridWidth);
grid->setColWidthPerc(i, (buttons.at(i)->getSize().x() + BUTTON_GRID_HORIZ_PADDING) / gridWidth);
}
buttonGrid->setSize(buttonGridWidth, buttons.at(0)->getSize().y() + BUTTON_GRID_VERT_PADDING + 2);
buttonGrid->setRowHeightPerc(1, 2 / buttonGrid->getSize().y()); // spacer row to deal with dropshadow to make buttons look centered
grid->setSize(gridWidth, buttons.at(0)->getSize().y() + BUTTON_GRID_VERT_PADDING + 2);
grid->setRowHeightPerc(1, 2 / grid->getSize().y()); // spacer row to deal with dropshadow to make buttons look centered
return buttonGrid;
return grid;
}
/**
* Limitation: same number of button per line, same dimension per cell
*/
std::shared_ptr<ComponentGrid> makeMultiDimButtonGrid(Window* window, const std::vector< std::vector< std::shared_ptr<ButtonComponent> > >& buttons)
std::shared_ptr<ComponentGrid> makeMultiDimButtonGrid(Window* window, const std::vector< std::vector< std::shared_ptr<ButtonComponent> > >& buttons, float outerWidth)
{
int sizeX = (int) buttons.at(0).size();
int sizeY = (int) buttons.size();
const std::shared_ptr<ButtonComponent>& sampleButton = buttons.at(0).at(0);
std::shared_ptr<ComponentGrid> buttonGrid = std::make_shared<ComponentGrid>(window, Vector2i(sizeX, sizeY));
const int sizeX = (int) buttons.at(0).size();
const int sizeY = (int) buttons.size();
const float buttonHeight = buttons.at(0).at(0)->getSize().y();
const float gridHeight = (buttonHeight + BUTTON_GRID_VERT_PADDING + 2) * sizeY;
float maxWidth = 0;
float horizPadding = (float) BUTTON_GRID_HORIZ_PADDING;
float gridWidth, buttonWidth;
for (int y = 0; y < sizeY; y++)
{
float currentWidth = 0;
for (int x = 0; x < sizeX; x++)
{
buttonGrid->setEntry(buttons.at(y).at(x), Vector2i(x, y), true, false);
currentWidth += buttons.at(y).at(x)->getSize().x();
}
maxWidth = std::max(maxWidth, currentWidth);
}
do {
gridWidth = outerWidth - horizPadding; // to get centered because size * (button size + BUTTON_GRID_VERT_PADDING) let a half BUTTON_GRID_VERT_PADDING left / right marge
buttonWidth = (gridWidth / sizeX) - horizPadding;
horizPadding -= 2;
} while ((buttonWidth < 100) && (horizPadding > 2));
float buttonGridWidth = (float)BUTTON_GRID_HORIZ_PADDING * sizeX + maxWidth;
float buttonGridHeight =((float)BUTTON_GRID_VERT_PADDING + 2 + sampleButton->getSize().y()) * sizeY;
buttonGrid->setSize(buttonGridWidth, buttonGridHeight);
std::shared_ptr<ComponentGrid> grid = std::make_shared<ComponentGrid>(window, Vector2i(sizeX, sizeY));
grid->setSize(gridWidth, gridHeight);
for (int x = 0; x < sizeX; x++)
grid->setColWidthPerc(x, (float) 1 / sizeX);
for (int y = 0; y < sizeY; y++)
{
buttonGrid->setColWidthPerc(x, (float) 1 /sizeX);
for (int x = 0; x < sizeX; x++)
{
const std::shared_ptr<ButtonComponent>& button = buttons.at(y).at(x);
button->setSize(buttonWidth, buttonHeight);
grid->setEntry(button, Vector2i(x, y), true, false);
}
}
return buttonGrid;
return grid;
}
std::shared_ptr<ImageComponent> makeArrow(Window* window)
......
......@@ -17,7 +17,7 @@ class ButtonComponent;
class ImageComponent;
std::shared_ptr<ComponentGrid> makeButtonGrid(Window* window, const std::vector< std::shared_ptr<ButtonComponent> >& buttons);
std::shared_ptr<ComponentGrid> makeMultiDimButtonGrid(Window* window, const std::vector< std::vector< std::shared_ptr<ButtonComponent> > >& buttons);
std::shared_ptr<ComponentGrid> makeMultiDimButtonGrid(Window* window, const std::vector< std::vector< std::shared_ptr<ButtonComponent> > >& buttons, const float outerWidth);
std::shared_ptr<ImageComponent> makeArrow(Window* window);
#define TITLE_VERT_PADDING (Renderer::getScreenHeight()*0.0637f)
......
......@@ -11,6 +11,9 @@ GuiTextEditPopupKeyboard::GuiTextEditPopupKeyboard(Window* window, const std::st
{
auto menuTheme = MenuThemeData::getInstance()->getCurrentTheme();
const float gridWidth = Renderer::getScreenWidth() * 0.96f;
mBackground.setImagePath(menuTheme->menuBackground.path);
mBackground.setCenterColor(menuTheme->menuBackground.color);
mBackground.setEdgeColor(menuTheme->menuBackground.color);
......@@ -30,7 +33,7 @@ GuiTextEditPopupKeyboard::GuiTextEditPopupKeyboard(Window* window, const std::st
mGrid.setEntry(mTitle, Vector2i(0, 0), false, true);
// Text edit add
mGrid.setEntry(mText, Vector2i(0, 1), true, false, Vector2i(1, 1), GridFlags::BORDER_TOP | GridFlags::BORDER_BOTTOM);
mGrid.setEntry(mText, Vector2i(0, 1), true, false, Vector2i(1, 1));
std::vector< std::vector< std::shared_ptr<ButtonComponent> > > buttonList;
......@@ -77,7 +80,12 @@ GuiTextEditPopupKeyboard::GuiTextEditPopupKeyboard(Window* window, const std::st
}
// Add keyboard keys
mKeyboardGrid = makeMultiDimButtonGrid(mWindow, buttonList);
mKeyboardGrid = makeMultiDimButtonGrid(mWindow, buttonList, gridWidth);
mShiftButton->autoSizeFont();
if (mShiftButton->isTextOverlapping()) {
mShiftButton->setText("SH.", _("SHIFTS FOR UPPER,LOWER, AND SPECIAL"), false, false);
}
mGrid.setEntry(mKeyboardGrid, Vector2i(0, 2), true, false);
......@@ -104,7 +112,7 @@ GuiTextEditPopupKeyboard::GuiTextEditPopupKeyboard(Window* window, const std::st
float textHeight = mText->getFont()->getHeight();
if (multiLine)
textHeight *= 6;
mText->setSize(0, textHeight);
mText->setSize(gridWidth - 40, textHeight);
// If multiline, set all size back to default, else draw size for keyboard.
if (mMultiLine)
......@@ -114,9 +122,7 @@ GuiTextEditPopupKeyboard::GuiTextEditPopupKeyboard(Window* window, const std::st
}
else
{
// Set size based on ScreenHeight * .08f by the amount of keyboard rows there are.
// set width base on bButtons[0] (the SHIFT key) x 12 as SHIFT key is the widest
setSize(std::min(Renderer::getScreenWidth() * 0.99f, mShiftButton->getSize().x() * 12 + Renderer::getScreenWidth() * 0.1f), mTitle->getFont()->getHeight() + textHeight + 40 + (Renderer::getScreenHeight() * 0.085f) * 6);
setSize(gridWidth, mTitle->getFont()->getHeight() + textHeight + 40 + mKeyboardGrid->getSize().y() + mButtons->getSize().y());
setPosition((Renderer::getScreenWidth() - mSize.x()) / 2, (Renderer::getScreenHeight() - mSize.y()) / 2);
}
}
......@@ -206,7 +212,7 @@ void GuiTextEditPopupKeyboard::switchShift()
for (auto & kb: keyboardButtons)
{
const std::string& text = mShift ? kb.shiftedKey : kb.key;
kb.button->setText(text, text, false);
kb.button->setText(text, text, false, false);
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment