Work resumed

This commit is contained in:
2023-10-05 17:19:48 -05:00
parent 5ec1659865
commit 4f33f208ea
12 changed files with 221 additions and 194 deletions

View File

@ -12,6 +12,7 @@ target_sources(${DAWN_TARGET_NAME}
UIImage.cpp
UIEmpty.cpp
UIBorder.cpp
UIMesh.cpp
)
add_subdirectory(menu)

View File

@ -41,27 +41,61 @@ void UIComponent::updateAlignment() {
parentInnerWidth = dimensional->getContentWidth();
parentInnerHeight = dimensional->getContentHeight();
UIComponent::calculateDimensions(
this->alignX,
this->alignUnitLeft,
this->alignUnitRight,
&translate.x,
&this->width,
parentInnerWidth,
this->getContentWidth(),
glm::vec2(align[0], align[2])
);
UIComponent::calculateDimensions(
this->alignY,
this->alignUnitTop,
this->alignUnitBottom,
&translate.y,
&this->height,
parentInnerHeight,
this->getContentHeight(),
glm::vec2(align[1], align[3])
// Should we be doing width first, or height first?
bool_t heightFirst = (
this->alignUnitLeft == UI_COMPONENT_ALIGN_UNIT_RATIO ||
this->alignUnitRight == UI_COMPONENT_ALIGN_UNIT_RATIO
);
if(heightFirst) {
UIComponent::calculateDimensions(
this->alignY,
this->alignUnitTop,
this->alignUnitBottom,
&translate.y,
&this->height,
parentInnerHeight,
this->getContentHeight(),
this->width,
glm::vec2(align[1], align[3])
);
UIComponent::calculateDimensions(
this->alignX,
this->alignUnitLeft,
this->alignUnitRight,
&translate.x,
&this->width,
parentInnerWidth,
this->getContentWidth(),
this->height,
glm::vec2(align[0], align[2])
);
} else {
UIComponent::calculateDimensions(
this->alignY,
this->alignUnitTop,
this->alignUnitBottom,
&translate.y,
&this->height,
parentInnerHeight,
this->getContentHeight(),
this->width,
glm::vec2(align[1], align[3])
);
UIComponent::calculateDimensions(
this->alignX,
this->alignUnitLeft,
this->alignUnitRight,
&translate.x,
&this->width,
parentInnerWidth,
this->getContentWidth(),
this->height,
glm::vec2(align[0], align[2])
);
}
translate.x += dimensional->getChildOffsetX();
translate.y += dimensional->getChildOffsetY();
@ -73,10 +107,14 @@ void UIComponent::updateAlignment() {
float_t UIComponent::calculateAlignmentValue(
float_t alignmentValue,
float_t parentSize,
float_t ratioSize,
enum UIComponentAlignUnit unit
) {
if(unit == UI_COMPONENT_ALIGN_UNIT_SCALE) return alignmentValue;
return (alignmentValue / 100.0f) * parentSize;
if(unit == UI_COMPONENT_ALIGN_UNIT_PERCENT) return (alignmentValue / 100.0f) * parentSize;
if(unit == UI_COMPONENT_ALIGN_UNIT_RATIO) return (alignmentValue / 100.0f) * ratioSize;
assertUnreachable("UIComponent::calculateAlignmentValue: Unknown alignment unit");
return -1;
}
void UIComponent::calculateDimensions(
@ -87,6 +125,7 @@ void UIComponent::calculateDimensions(
float_t *size,
float_t outerSize,
float_t innerSize,
float_t ratioSize,
glm::vec2 alignment
) {
assertNotNull(position, "UIComponent::calculateDimensions: Position cannot be null");
@ -99,11 +138,13 @@ void UIComponent::calculateDimensions(
*position = UIComponent::calculateAlignmentValue(
alignment[0],
outerSize,
ratioSize,
unitStart
);
*size = outerSize - (*position + UIComponent::calculateAlignmentValue(
alignment[1],
outerSize,
ratioSize,
unitEnd
));
break;
@ -115,28 +156,29 @@ void UIComponent::calculateDimensions(
*position = UIComponent::calculateAlignmentValue(
alignment[0],
outerSize,
ratioSize,
unitStart
);
*size = UIComponent::calculateAlignmentValue(
alignment[1],
outerSize,
ratioSize,
unitEnd
);
break;
}
case UI_COMPONENT_ALIGN_MIDDLE: {
*size = mathMax<float_t>(
innerSize,
UIComponent::calculateAlignmentValue(
alignment[1],
outerSize,
unitEnd
)
*size = UIComponent::calculateAlignmentValue(
alignment[1],
outerSize,
ratioSize,
unitEnd
);
*position = (outerSize / 2.0f) - (*size / 2.0f) + UIComponent::calculateAlignmentValue(
*position = (outerSize / 2.0f) - ((*size) / 2.0f) + UIComponent::calculateAlignmentValue(
alignment[0],
outerSize,
ratioSize,
unitStart
);
break;
@ -146,11 +188,13 @@ void UIComponent::calculateDimensions(
*size = UIComponent::calculateAlignmentValue(
alignment[0],
outerSize,
ratioSize,
unitStart
);
*position = outerSize - *size - UIComponent::calculateAlignmentValue(
alignment[1],
outerSize,
ratioSize,
unitEnd
);
break;

View File

@ -21,7 +21,8 @@ namespace Dawn {
enum UIComponentAlignUnit {
UI_COMPONENT_ALIGN_UNIT_SCALE,
UI_COMPONENT_ALIGN_UNIT_PERCENT
UI_COMPONENT_ALIGN_UNIT_PERCENT,
UI_COMPONENT_ALIGN_UNIT_RATIO
};
class UIComponent : public SceneItemComponent, public UIComponentDimensional {
@ -53,12 +54,14 @@ namespace Dawn {
*
* @param alignmentValue Alignment value.
* @param parentSize Parent size.
* @param ratioSize The dimension that the ratio is based on.
* @param unit Alignment unit.
* @return The calculated alignment value.
*/
static float_t calculateAlignmentValue(
float_t alignmentValue,
float_t parentSize,
float_t ratioSize,
enum UIComponentAlignUnit unit
);
@ -72,6 +75,7 @@ namespace Dawn {
* @param size Output size floating point.
* @param outerSize Outer size (of the parent).
* @param innerSize Inner size (of this element's content).
* @param ratioSize The size that the opposite dimension is.
* @param alignment Alignment settings.
*/
static void calculateDimensions(
@ -82,6 +86,7 @@ namespace Dawn {
float_t *size,
float_t outerSize,
float_t innerSize,
float_t ratioSize,
glm::vec2 alignment
);

View File

@ -10,7 +10,8 @@ using namespace Dawn;
UIImage::UIImage(SceneItem *item) :
texture(nullptr),
UIComponentRenderable(item)
UIComponentRenderable(item),
uvs(glm::vec4(0, 1, 1, 0))
{
}
@ -42,7 +43,6 @@ std::vector<struct ShaderPassItem> UIImage::getUIRenderPasses() {
item.w = this->transform->getWorldPosition().z;
item.renderFlags = RENDER_MANAGER_RENDER_FLAG_BLEND;
item.mesh = &mesh;
return { item };
}
@ -51,13 +51,22 @@ void UIImage::onStart() {
useEvent([&]{
QuadMesh::bufferPositions(&mesh,
glm::vec2(0, 0), glm::vec2(width, height), 0
glm::vec2(0, 0),
glm::vec2(width, height), 0
);
}, this->eventAlignmentUpdated);
QuadMesh::initQuadMesh(&mesh,
glm::vec2(0, 0), glm::vec2(0, 1),
glm::vec2(width, height), glm::vec2(1, 0),
useEffect([&]{
QuadMesh::bufferCoordinates(&mesh,
glm::vec2(this->uvs._realValue[0], this->uvs._realValue[1]),
glm::vec2(this->uvs._realValue[2], this->uvs._realValue[3]),
0
);
}, this->uvs);
QuadMesh::initQuadMesh(&mesh,
glm::vec2(0, 0), glm::vec2(this->uvs._realValue[0], this->uvs._realValue[1]),
glm::vec2(width, height), glm::vec2(this->uvs._realValue[2], this->uvs._realValue[3]),
0.0f
);
}

View File

@ -17,6 +17,8 @@ namespace Dawn {
struct Color color = COLOR_WHITE;
// @optional
StateProperty<Texture*> texture;
// @optional
StateProperty<glm::vec4> uvs;
UIImage(SceneItem *item);

View File

@ -0,0 +1,56 @@
// Copyright (c) 2023 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#include "UIMesh.hpp"
#include "game/DawnGame.hpp"
using namespace Dawn;
UIMesh::UIMesh(SceneItem *i) :
texture(nullptr),
UIComponentRenderable(i)
{
}
float_t UIMesh::getContentWidth() {
if(this->texture != nullptr) return this->texture->getWidth();
return this->width;
}
float_t UIMesh::getContentHeight() {
if(this->texture != nullptr) return this->texture->getHeight();
return this->height;
}
std::vector<struct ShaderPassItem> UIMesh::getUIRenderPasses() {
struct ShaderPassItem item;
auto shader = getGame()->renderManager.uiShader;
item.shader = shader;
item.colorValues[shader->paramColor] = this->color;
item.parameterBuffers[shader->bufferUiCanvas] = &getCanvas()->shaderBuffer;
item.matrixValues[shader->paramModel] = this->transform->getWorldTransform();
if(this->texture == nullptr) {
item.boolValues[shader->paramHasTexture] = false;
} else {
item.boolValues[shader->paramHasTexture] = true;
item.textureSlots[0] = this->texture;
item.textureValues[shader->paramTexture] = 0;
}
item.w = this->transform->getWorldPosition().z;
item.renderFlags = RENDER_MANAGER_RENDER_FLAG_BLEND;
item.mesh = &mesh;
return { item };
}
void UIMesh::onStart() {
useEffect([&]{
}, this->positions)();
useEffect([&]{
}, this->uvs)();
}

View File

@ -0,0 +1,31 @@
// Copyright (c) 2023 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#pragma once
#include "UIComponentRenderable.hpp"
#include "display/mesh/QuadMesh.hpp"
namespace Dawn {
class UIMesh : public UIComponentRenderable {
private:
Mesh mesh;
public:
// @optional
StateProperty<std::vector<glm::vec2>> positions;
// @optional
StateProperty<std::vector<glm::vec2>> uvs;
// @optional
struct Color color = COLOR_WHITE;
// @optional
StateProperty<Texture*> texture;
UIMesh(SceneItem *item);
float_t getContentWidth() override;
float_t getContentHeight() override;
std::vector<struct ShaderPassItem> getUIRenderPasses() override;
void onStart() override;
};
}