From 868e741ad9d47afc58d1a215201fc713c6c0f4f2 Mon Sep 17 00:00:00 2001 From: Andre Taulien Date: Sat, 17 Dec 2016 01:26:01 +0100 Subject: [PATCH] More work on image-view --- src/content/Texture.cpp | 4 ++++ src/content/Texture.h | 2 ++ src/target/REGoth.cpp | 10 +++++---- src/ui/BarView.cpp | 17 ++++++++++----- src/ui/BarView.h | 4 ++-- src/ui/ImageView.cpp | 47 +++++++++++++++++++++++++++++------------ src/ui/ImageView.h | 19 ++++++++++++++++- src/ui/View.cpp | 21 +++++++++++++++++- src/ui/View.h | 29 +++++++++++++++++++++++++ 9 files changed, 127 insertions(+), 26 deletions(-) diff --git a/src/content/Texture.cpp b/src/content/Texture.cpp index d9854b1f..ce266527 100755 --- a/src/content/Texture.cpp +++ b/src/content/Texture.cpp @@ -49,6 +49,10 @@ Handle::TextureHandle TextureAllocator::loadTextureDDS(const std::vector Texture; diff --git a/src/target/REGoth.cpp b/src/target/REGoth.cpp index 9736ba15..2fc45e32 100755 --- a/src/target/REGoth.cpp +++ b/src/target/REGoth.cpp @@ -577,10 +577,12 @@ class ExampleCubes : public /*entry::AppI*/ PLATFORM_CLASS Textures::Texture& health = alloc.getTexture(healthh); UI::BarView* bar = new UI::BarView(); - bar->setBackgroundImage(back.m_TextureHandle); - bar->setBarImage(health.m_TextureHandle); - bar->setTranslation(Math::float2(0.1f, 0.1f)); - bar->setSize(Math::float2(0.3f, 0.1f)); + bar->setAlignment(UI::A_BottomLeft); + bar->setBackgroundImage(back); + bar->setBarImage(health); + bar->setValue(1.0f); + bar->setTranslation(Math::float2(0.01f, 0.995f)); + bar->setSize(Math::float2(0.7f, 0.7f)); m_pEngine->getRootUIView().addChild(bar); }*/ diff --git a/src/ui/BarView.cpp b/src/ui/BarView.cpp index e2fd0cd0..5a622751 100644 --- a/src/ui/BarView.cpp +++ b/src/ui/BarView.cpp @@ -2,6 +2,7 @@ // Created by desktop on 16.12.16. // +#include #include "BarView.h" #include "ImageView.h" @@ -10,6 +11,9 @@ UI::BarView::BarView() m_pBackground = new UI::ImageView(); m_pBar = new UI::ImageView(); + m_pBar->setAlignment(A_Center); + m_pBackground->setAlignment(A_Center); + addChild(m_pBackground); addChild(m_pBar); } @@ -25,7 +29,10 @@ UI::BarView::~BarView() void UI::BarView::update(double dt, Engine::Input::MouseState& mstate, Render::RenderConfig& config) { - m_pBar->setSize(Math::float2(m_Value, 1.0f)); + m_pBar->setTranslation(Math::float2(0.1,0.1)); + m_pBar->setSize(Math::float2(m_Value * 0.8f, 0.8f)); + m_pBar->setAlignment(m_Alignment); + m_pBackground->setAlignment(m_Alignment); View::update(dt, mstate, config); } @@ -35,12 +42,12 @@ void UI::BarView::setValue(float v) m_Value = v; } -void UI::BarView::setBackgroundImage(bgfx::TextureHandle tx) +void UI::BarView::setBackgroundImage(const Textures::Texture& texture) { - m_pBackground->setImage(tx); + m_pBackground->setImage(texture.m_TextureHandle, texture.m_Width, texture.m_Height); } -void UI::BarView::setBarImage(bgfx::TextureHandle tx) +void UI::BarView::setBarImage(const Textures::Texture& texture) { - m_pBar->setImage(tx); + m_pBar->setImage(texture.m_TextureHandle, texture.m_Width, texture.m_Height); } diff --git a/src/ui/BarView.h b/src/ui/BarView.h index 5e08cda4..ba916614 100644 --- a/src/ui/BarView.h +++ b/src/ui/BarView.h @@ -22,8 +22,8 @@ namespace UI */ void setValue(float v); - void setBackgroundImage(bgfx::TextureHandle tx); - void setBarImage(bgfx::TextureHandle tx); + void setBackgroundImage(const Textures::Texture& texture); + void setBarImage(const Textures::Texture& texture); protected: diff --git a/src/ui/ImageView.cpp b/src/ui/ImageView.cpp index ece1507b..ebcc1ffd 100644 --- a/src/ui/ImageView.cpp +++ b/src/ui/ImageView.cpp @@ -9,7 +9,9 @@ extern bgfx::ProgramHandle imguiGetImageProgram(uint8_t _mip); UI::ImageView::ImageView() { - + m_RelativeSize = true; + m_ImageHeight = 0; + m_ImageWidth = 0; } @@ -20,22 +22,41 @@ UI::ImageView::~ImageView() void UI::ImageView::update(double dt, Engine::Input::MouseState& mstate, Render::RenderConfig& config) { - // Un-normalize transforms - Math::float2 absTranslation = getAbsoluteTranslation(); - Math::float2 absSize = getAbsoluteSize(); - int px = (int)(absTranslation.x * config.state.viewWidth + 0.5f); - int py = (int)(absTranslation.y * config.state.viewHeight + 0.5f); - int width = (int)(absSize.x * config.state.viewWidth + 0.5f); - int height = (int)(absSize.y * config.state.viewHeight+ 0.5f); - - bgfx::ProgramHandle program = ::imguiGetImageProgram(1); - drawTexture(BGFX_VIEW, px, py, width, height, - config.state.viewWidth, config.state.viewHeight, m_Image, program, config.uniforms.diffuseTexture); + if(bgfx::isValid(m_Image)) + { + // Un-normalize transforms + Math::float2 offset = getAlignOffset(m_Alignment, m_ImageWidth, m_ImageHeight); + Math::float2 absTranslation = getAbsoluteTranslation(); + Math::float2 absSize = getAbsoluteSize(); + int px = (int) (absTranslation.x * config.state.viewWidth + 0.5f); + int py = (int) (absTranslation.y * config.state.viewHeight + 0.5f); + + px += offset.x; + py += offset.y; + + int width; + int height; + + if (m_RelativeSize) + { + absSize.x *= m_ImageWidth / (float) config.state.viewWidth; + absSize.y *= m_ImageHeight / (float) config.state.viewHeight; + } + + width = (int) (absSize.x * config.state.viewWidth + 0.5f); + height = (int) (absSize.y * config.state.viewHeight + 0.5f); + + bgfx::ProgramHandle program = ::imguiGetImageProgram(1); + drawTexture(BGFX_VIEW, px, py, width, height, + config.state.viewWidth, config.state.viewHeight, m_Image, program, config.uniforms.diffuseTexture); + } View::update(dt, mstate, config); } -void UI::ImageView::setImage(bgfx::TextureHandle image) +void UI::ImageView::setImage(bgfx::TextureHandle image, int32_t width, int32_t height) { m_Image = image; + m_ImageWidth = width; + m_ImageHeight = height; } diff --git a/src/ui/ImageView.h b/src/ui/ImageView.h index 2f704b29..f00a3b15 100644 --- a/src/ui/ImageView.h +++ b/src/ui/ImageView.h @@ -19,8 +19,13 @@ namespace UI /** * Sets the text to be displayed inside the box */ - void setImage(bgfx::TextureHandle image); + void setImage(bgfx::TextureHandle image, int32_t width = 0, int32_t height = 0); + /** + * @param rel If set to true, images actual width and height will be ignored and it will be stretched to fit + * with the given m_Size-value of the View-class. + */ + void setRelativeSize(bool rel){ m_RelativeSize = rel; } protected: /** @@ -28,5 +33,17 @@ namespace UI */ bgfx::TextureHandle m_Image; + /** + * Width and height of the given image + */ + int32_t m_ImageWidth; + int32_t m_ImageHeight; + + /** + * If set to true, images actual width and height will be ignored and it will be stretched to fit + * with the given m_Size-value of the View-class. + */ + bool m_RelativeSize; + }; } diff --git a/src/ui/View.cpp b/src/ui/View.cpp index 77c982e0..e720f2df 100644 --- a/src/ui/View.cpp +++ b/src/ui/View.cpp @@ -103,6 +103,7 @@ View::View() m_Translation = Math::float2(0,0); m_Size = Math::float2(1,1); + m_Alignment = EAlign::A_TopLeft; ViewUtil::PosUvVertex::init(); @@ -153,7 +154,7 @@ void View::drawTexture(uint8_t id, int x, int y, int width, int height, int surf //imguiBeginArea("Picking Render Target:", x, y, width, height); //imguiImage(texture, 0, width, height); - bgfx::setState(BGFX_STATE_DEFAULT); + bgfx::setState(BGFX_STATE_DEFAULT & ~BGFX_STATE_DEPTH_WRITE); float ortho[16]; bx::mtxOrtho(ortho, 0.0f, (float)surfaceWidth, (float)surfaceHeight, 0.0f, 0.0f, 1000.0f); @@ -182,6 +183,7 @@ void View::drawTexture(uint8_t id, int x, int y, int width, int height, int surf Math::float2 View::getAbsoluteTranslation() { + if(!m_pParent) return m_Translation; @@ -203,3 +205,20 @@ void View::addChild(View* pView) m_Children.push_back(pView); pView->setParent(this); } + +Math::float2 View::getAlignOffset(EAlign align, float width, float height) +{ + switch(align) + { + case A_Center: return Math::float2(-width / 2.0f, -height / 2.0f); + case A_LeftCenter: return Math::float2(0, -height / 2.0f); + case A_RightCenter: return Math::float2(-width, -height / 2.0f); + case A_TopCenter: return Math::float2(-width / 2.0f, 0); + case A_BottomCenter: return Math::float2(-width / 2.0f, -height); + case A_TopLeft: return Math::float2(0, 0); + case A_TopRight: return Math::float2(-width, 0); + case A_BottomLeft: return Math::float2(0, -height); + case A_BottomRight: return Math::float2(-width, -height); + } + return Math::float2(nullptr); +} diff --git a/src/ui/View.h b/src/ui/View.h index a5d969a7..69b049e9 100644 --- a/src/ui/View.h +++ b/src/ui/View.h @@ -9,6 +9,19 @@ namespace UI // BGFX-View to be used for rendering views const int BGFX_VIEW = 254; + enum EAlign + { + A_Center, + A_LeftCenter, + A_RightCenter, + A_TopCenter, + A_BottomCenter, + A_TopLeft, + A_TopRight, + A_BottomLeft, + A_BottomRight + }; + /** * Base UI-View */ @@ -52,6 +65,12 @@ namespace UI void setTranslation(const Math::float2& translation){ m_Translation = translation; } void setSize(const Math::float2& size){ m_Size = size; } + /** + * Sets how this view should be aligned to it's translation + * @param alignment The alignment + */ + void setAlignment(EAlign alignment){ m_Alignment = alignment; } + /** * Draws a texture on screen somewhere * Note: Uses alpha-blending @@ -67,6 +86,15 @@ namespace UI Math::float2 getAbsoluteTranslation(); Math::float2 getAbsoluteSize(); + /** + * From everything being aligned to "top-left" by default, we can realize other alignments by shifting the + * top-left translation before rendering, which is what this function outputs. + * @param align Alignment to account for + * @param width Width of the area to align + * @param height Height of the area to align + * @return Offset to the top-left translation of a view + */ + static Math::float2 getAlignOffset(EAlign align, float width, float height); protected: /** @@ -90,5 +118,6 @@ namespace UI */ Math::float2 m_Translation; Math::float2 m_Size; + EAlign m_Alignment; }; } \ No newline at end of file