Browse Source

resizeToTextSize(), make button width adjust to text width, size prototype change

master
Odilitime 4 years ago
parent
commit
0fc94ab538

+ 56
- 4
src/graphics/components/ButtonComponent.cpp View File

@@ -127,6 +127,57 @@ void ButtonComponent::resize(const int passedWindowWidth, const int passedWindow
}
}

void ButtonComponent::resizeToTextSize() {
if (!textLabel->win && this->win) {
textLabel->win = this->win;
}
textLabel->x = x;
if (y < 0) {
textLabel->y = y;
} else {
textLabel->y = y - windowHeight + 16;
}
//std::cout << "placed userInputText at " << static_cast<int>(x) << "," << static_cast<int>(y - windowHeight) << std::endl;
// 62 pixels width
// but first we need to know how wide the text is
const std::shared_ptr<TextRasterizer> textRasterizer=rasterizerCache->loadFont(12, false); // fontSize, bold
rasterizationRequest request;
request.text = value;
request.startX = x;
request.availableWidth = windowWidth; // have to include x, because width=62, and we need to render that
request.sourceStartX = 0;
request.sourceStartY = 0;
request.noWrap = true;
// FIXME: center this!
std::unique_ptr<sizeResponse> textInfo = textRasterizer->size(request);
if (textInfo.get() == nullptr) {
std::cout << "ButtonComponent::updateText - couldn't estimate value[" << value << "] size" << std::endl;
return;
}
//int textWidth = textInfo->first;
int estWidth = textInfo->width;
//int estHeight = std::get<1>(*textInfo.get());
textLabel->rasterStartX = 0;
textLabel->rasterStartY = 0;
//std::cout << "ButtonComponent::updateText - estWidth: " << estWidth << " width: " << static_cast<int>(width) << std::endl;
/*
if (estWidth > width) {
//std::cout << "scrolling text" << std::endl;
textLabel->rasterStartX = estWidth - width;
}
*/
this->width = estWidth + 10; // 5px padding on each side
this->resize(windowWidth, windowHeight);
textLabel->noWrap = true;
// why does changing the width mess shit up?
//std::cout << "ButtonComponent::updateText - our width: " << static_cast<int>(width) << " windowWidth: " << windowWidth << std::endl;
textLabel->resize(windowWidth, windowHeight, windowWidth); // need to make sure there's a texture
if (this->win) {
this->win->renderDirty = true;
}
}

// most of this functionality should be inside TextComponent
void ButtonComponent::updateText() {
//std::cout << "ButtonComponent::updateText - input value is now: " << value << std::endl;
@@ -157,13 +208,12 @@ void ButtonComponent::updateText() {
request.sourceStartY = 0;
request.noWrap = true;
// FIXME: center this!
std::unique_ptr<std::pair<int, int>> textInfo = textRasterizer->size(request);
std::unique_ptr<sizeResponse> textInfo = textRasterizer->size(request);
if (textInfo.get() == nullptr) {
std::cout << "ButtonComponent::updateText - couldn't estimate value[" << value << "] size" << std::endl;
return;
}
//int textWidth = textInfo->first;
int estWidth = std::get<0>(*textInfo.get());
int estWidth = textInfo->width;
//int estHeight = std::get<1>(*textInfo.get());
textLabel->rasterStartX = 0;
textLabel->rasterStartY = 0;
@@ -177,5 +227,7 @@ void ButtonComponent::updateText() {
//std::cout << "ButtonComponent::updateText - our width: " << static_cast<int>(width) << " windowWidth: " << windowWidth << std::endl;
textLabel->resize(windowWidth, windowHeight, width + x); // need to make sure there's a texture
this->win->renderDirty = true;
if (this->win) {
this->win->renderDirty = true;
}
}

+ 1
- 0
src/graphics/components/ButtonComponent.h View File

@@ -13,6 +13,7 @@ public:
ButtonComponent(const float rawX, const float rawY, const float rawWidth, const float rawHeight, const int passedWindowWidth, const int passedWindowHeight);
void render();
void resize(const int passedWindowWidth, const int passedWindowHeight);
void resizeToTextSize();
void updateText();
std::string value="";
TextComponent *textLabel = nullptr;

Loading…
Cancel
Save