Browse Source

setUpUI(), getUIMetric(), fix UILayoutV2 initialization

Odilitime 5 years ago
parent
commit
41e311aff3
  1. 154
      src/graphics/components/Component.cpp
  2. 22
      src/graphics/components/Component.h

154
src/graphics/components/Component.cpp

@ -8,9 +8,163 @@ @@ -8,9 +8,163 @@
#include "ComponentBuilder.h"
// pos, size
std::unique_ptr<std::pair<UImetricV2, UImetricV2>> getUIMetric(UImetricV2 &s1, UImetricV2 &s2, UImetricV2 &size) {
// only 2 of these can be requested
if ((s1.requested && s2.requested && size.requested) || (!s1.requested && !s2.requested && !size.requested)) {
// FIXME: better message
std::cout << "component.getUIMetric - can't resolve" << std::endl;
return nullptr;
}
std::unique_ptr<std::pair<UImetricV2, UImetricV2>> result=std::make_unique<std::pair<UImetricV2, UImetricV2>>();
if (s1.requested && s2.requested) {
// left/right
//std::cout << "left/right px " << s1.px << " & " << s2.px << std::endl;
//std::cout << "left/right % " << s1.pct << " & " << s2.pct << std::endl;
result->first = { true, s1.pct, s1.px }; // pos
result->second = { true, (100.0 - s2.pct) - s1.pct, -s2.px - s1.px }; // size
/*
if (s1.pct && s2.pct) {
// right 25% = left 75%
result->second = { true, (100 - s2.pct) - s1.pct, 0 }; // size
} else
if (!s1.pct && !s2.pct) {
result->second = { true, 100.0 - s2.pct, -s2.px };
} else if (s1.pct) {
result->second = { true, 100.0 - s2.pct, -s2.px };
} else if (s2.pct) {
// right 25% = left 75%
result->second = { true, 100.0 - s2.pct, -s2.px };
} else {
// unknown
}
*/
} else
if (s1.requested && size.requested) {
// left + width
result->first = { true, s1.pct, s1.px }; // pos
result->second = { true, size.pct, size.px };
} else
if (s2.requested && size.requested) {
// right - width
// right 25% = left 75%
// + width 50% means start at 25%
result->first = { true, (100.0 - s2.pct) - size.pct, -s2.px - size.px }; // pos
result->second = { true, size.pct, size.px };
} else {
std::cout << "component.getUIMetric - Unkown state" << std::endl;
}
return result;
}
Component::~Component() {
}
// translate properties (left/right/width && top/bottom/height) into resizeable layout
void Component::setUpUI(std::map<std::string, std::string> &properties, Window *win) {
UILayoutV2 boxSetup;
boxSetup.width.px = 32;
boxSetup.height.px = 32;
if (properties.find("width") != properties.end()) {
boxSetup.width.requested = true;
if (properties["width"][properties["width"].size() - 1] == '%') {
std::cout << "Component::setUpUI - found width %" << std::endl;
boxSetup.width.pct = std::stoi(properties["width"]);
} else {
boxSetup.width.px = std::stoi(properties["width"]);
}
}
if (properties.find("height") != properties.end()) {
boxSetup.height.requested = true;
if (properties["height"][properties["height"].size() - 1] == '%') {
std::cout << "Component::setUpUI - found height %" << std::endl;
boxSetup.height.pct = std::stoi(properties["height"]);
} else {
boxSetup.height.px = std::stoi(properties["height"]);
}
}
if (properties.find("top") != properties.end()) {
boxSetup.top.requested = true;
if (properties["top"][properties["top"].size() - 1] == '%') {
std::cout << "Component::setUpUI - found top %" << std::endl;
boxSetup.top.pct = std::stoi(properties["top"]);
} else {
boxSetup.top.px = std::stoi(properties["top"]);
}
}
if (properties.find("bottom") != properties.end()) {
boxSetup.bottom.requested = true;
if (properties["bottom"][properties["bottom"].size() - 1] == '%') {
std::cout << "Component::setUpUI - found bottom %" << std::endl;
boxSetup.bottom.pct = std::stoi(properties["bottom"]);
} else {
boxSetup.bottom.px = std::stoi(properties["bottom"]);
}
}
if (properties.find("left") != properties.end()) {
boxSetup.left.requested = true;
if (properties["left"][properties["left"].size() - 1] == '%') {
std::cout << "Component::setUpUI - found left %" << std::endl;
boxSetup.left.pct = std::stoi(properties["left"]);
} else {
boxSetup.left.px = std::stoi(properties["left"]);
}
}
if (properties.find("right") != properties.end()) {
boxSetup.right.requested = true;
if (properties["right"][properties["right"].size() - 1] == '%') {
std::cout << "Component::setUpUI - found right %" << std::endl;
boxSetup.right.pct = std::stoi(properties["right"]);
} else {
boxSetup.right.px = std::stoi(properties["right"]);
}
}
std::unique_ptr<std::pair<UImetricV2, UImetricV2>> xUI = getUIMetric(boxSetup.left, boxSetup.right, boxSetup.width);
std::unique_ptr<std::pair<UImetricV2, UImetricV2>> yUI = getUIMetric(boxSetup.top, boxSetup.bottom, boxSetup.height);
int cX = 0, cY = 0, cW = boxSetup.width.px, cH = boxSetup.height.px;
double xPct = 0, yPct = 0;
double wPct = 100, hPct = 100;
int wPx = 0, hPx = 0;
int winWidth = win->windowWidth;
int winHeight = win->windowHeight;
if (xUI) {
//std::cout << "box XUI at " << xUI->first.pct << "% +" << xUI->first.px << std::endl;
//std::cout << "box XUI for " << xUI->second.pct << "% +" << xUI->second.px << std::endl;
// QUESTION: I don't think cx should take pct into account
// I think it does because 25% into starting px is important
// no because we could just set uiControl.x correctly...
// and if we did that we could pull out win
// well we still need winHeight tho, because of the coords
cX = (xUI->first.pct * 0.01 * winWidth) + xUI->first.px;
xPct = xUI->first.pct;
cW = (xUI->second.pct * 0.01 * winWidth) + xUI->second.px;
wPct = xUI->second.pct;
wPx = xUI->second.px;
//std::cout << "Component::setUpUI X at " << cX << " for " << cW << std::endl;
//std::cout << "Component::setUpUI W at " << wPct << "% for " << wPx << std::endl;
}
if (yUI) {
cY = (yUI->first.pct * 0.01 * winHeight) + yUI->first.px;
yPct = yUI->first.pct;
cH = (yUI->second.pct * 0.01 * winHeight) + yUI->second.px;
hPct = yUI->second.pct;
hPx = yUI->second.px;
//std::cout << "Component::setUpUI Y at " << cY << " for " << cH << std::endl;
//std::cout << "Component::setUpUI H at " << hPct << "% for " << hPx << std::endl;
}
// set up proper component information for resizing
this->uiControl.x = { 0, cX }; //
this->uiControl.y = { 100, -cH - cY }; //
this->uiControl.w = { wPct, wPx }; //
this->uiControl.h = { hPct, hPx }; //
// recalculate new vertices
this->layout(); // move from uiContorl to x,y
// we don't need to set to call resize because layout already calls it (always calls wrap->resize)
//this->resize(winWidth, winHeight); // apply x,y state to vertices
}
// resize is where width/height can change within the confines of the window
void Component::resize(const int passedWindowWidth, const int passedWindowHeight) {
//std::cout << "Component::resize - doing nothing" << std::endl;

22
src/graphics/components/Component.h

@ -5,6 +5,7 @@ @@ -5,6 +5,7 @@
#include <memory>
#include <vector>
#include <string>
#include <map>
#include <GL/glew.h>
//#include "../opengl/Window.h"
@ -16,7 +17,7 @@ const unsigned int indices[6] = { @@ -16,7 +17,7 @@ const unsigned int indices[6] = {
};
struct UImetric {
double lengthPct = 0;
double lengthPct = 0; // 100% is 1.0 or 100.0 ? 100.0
int offset = 0;
};
@ -34,16 +35,18 @@ struct UImetricV2 { @@ -34,16 +35,18 @@ struct UImetricV2 {
};
// defines an area that a component covers
struct UILayoutV2 {
class UILayoutV2 {
public:
// only height or 2 of these can be set
UImetricV2 top = { false, false, 0 };
UImetricV2 bottom = { false, false, 0 };
UImetricV2 height = { false, false, 0 };
UImetricV2 top = { false, 0.0, 0 };
UImetricV2 bottom = { false, 0.0, 0 };
UImetricV2 height = { false, 0.0, 0 };
// only width or 2 of these can be set
UImetricV2 left = { false, false, 0 };
UImetricV2 right = { false, false, 0 };
UImetricV2 width = { false, false, 0 };
UImetricV2 left = { false, 0.0, 0 };
UImetricV2 right = { false, 0.0, 0 };
UImetricV2 width = { false, 0.0, 0 };
};
std::unique_ptr<std::pair<UImetricV2, UImetricV2>> getUIMetric(UImetricV2 &s1, UImetricV2 &s2, UImetricV2 &size);
// texturedComponent
// dual textComponent
@ -59,6 +62,7 @@ public: @@ -59,6 +62,7 @@ public:
// we're currently constructed with a pos
// so recalculating it is only good for resize
// the goal is to combine the resize and initial positioning
void setUpUI(std::map<std::string, std::string> &properties, Window *win);
void layout();
void wrap(); // what resize should be really called but also calls updateParentSize
void updateParentSize(); // on w/h change
@ -117,7 +121,7 @@ public: @@ -117,7 +121,7 @@ public:
// current placement on screen
float x = 0.0f;
float y = 0.0f;
// text flow (needed here because parent elements of textnode need to pass this info)
// text flow (needed here because parent elements of textnode need to pass this info onto next inline element)
int endingX = 0;
int endingY = 0;
// height/width on screen

Loading…
Cancel
Save