diff options
| author | Brandon Schaefer <brandontschaefer@gmail.com> | 2014-05-07 09:39:02 -0700 |
|---|---|---|
| committer | Brandon Schaefer <brandontschaefer@gmail.com> | 2014-05-07 09:39:02 -0700 |
| commit | 4e1b9f9de5543cfb7f513bfb42b8a275bd68efd7 (patch) | |
| tree | 59a3f1ffd88e995796c2054a352268ed7e80d2c9 /unity-shared | |
| parent | a35ac423a367fc467a30adb1a3f73819a489bb1b (diff) | |
* Use a map to map textures based on scale size, so we only load scale sizes we need,
and still cache them. Good for multi monitor. (bzr r3788.4.8)
Diffstat (limited to 'unity-shared')
| -rw-r--r-- | unity-shared/UnityWindowStyle.cpp | 81 | ||||
| -rw-r--r-- | unity-shared/UnityWindowStyle.h | 44 | ||||
| -rw-r--r-- | unity-shared/UnityWindowView.cpp | 50 |
3 files changed, 91 insertions, 84 deletions
diff --git a/unity-shared/UnityWindowStyle.cpp b/unity-shared/UnityWindowStyle.cpp index 056c6edc9..9fbccbe75 100644 --- a/unity-shared/UnityWindowStyle.cpp +++ b/unity-shared/UnityWindowStyle.cpp @@ -16,13 +16,18 @@ * * Authored by: Jason Smith <jason.smith@canonical.com> * Marco Trevisan <marco.trevisan@canonical.com> + * Brandon Schaefer <brandon.schaefer@canonical.com> */ +#include <NuxCore/Logger.h> + #include "UnityWindowStyle.h" #include "config.h" -namespace unity { -namespace ui { +namespace unity +{ +namespace ui +{ namespace { const char* const SWITCHER_TOP = PKGDATADIR"/switcher_top.png"; @@ -32,30 +37,31 @@ namespace const char* const DIALOG_CLOSE = PKGDATADIR"/dialog_close.png"; const char* const DIALOG_HIGHLIGHT = PKGDATADIR"/dialog_close_highlight.png"; const char* const DIALOG_PRESS = PKGDATADIR"/dialog_close_press.png"; + + double const DEFAULT_SCALE = 1.0; } +DECLARE_LOGGER(logger, "unity.ui.unity.window.style"); + UnityWindowStyle::UnityWindowStyle() - : scale(1.0) { - ReloadIcons(); - - scale.changed.connect([this] (double new_scale) { - ReloadIcons(); - }); + LoadAllTextureInScale(DEFAULT_SCALE); } -void UnityWindowStyle::ReloadIcons() +void UnityWindowStyle::LoadAllTextureInScale(double scale) { - background_top_.Adopt(LoadTexture(SWITCHER_TOP)); - background_left_.Adopt(LoadTexture(SWITCHER_LEFT)); - background_corner_.Adopt(LoadTexture(SWITCHER_CORNER)); + auto& window_textures = unity_window_textures_[scale]; - close_icon_.Adopt(LoadTexture(DIALOG_CLOSE)); - close_icon_highlighted_.Adopt(LoadTexture(DIALOG_HIGHLIGHT)); - close_icon_pressed_.Adopt(LoadTexture(DIALOG_PRESS)); + window_textures[unsigned(WindowTextureType::BACKGROUND_TOP)] = LoadTexture(scale, SWITCHER_TOP); + window_textures[unsigned(WindowTextureType::BACKGROUND_LEFT)] = LoadTexture(scale, SWITCHER_LEFT); + window_textures[unsigned(WindowTextureType::BACKGROUND_CORNER)] = LoadTexture(scale, SWITCHER_CORNER); + + window_textures[unsigned(WindowTextureType::CLOSE_ICON)] = LoadTexture(scale, DIALOG_CLOSE); + window_textures[unsigned(WindowTextureType::CLOSE_ICON_HIGHLIGHTED)] = LoadTexture(scale, DIALOG_HIGHLIGHT); + window_textures[unsigned(WindowTextureType::CLOSE_ICON_PRESSED)] = LoadTexture(scale, DIALOG_PRESS); } -nux::BaseTexture* UnityWindowStyle::LoadTexture(const char* const texture_name) const +nux::BaseTexture* UnityWindowStyle::LoadTexture(double scale, const char* const texture_name) const { RawPixel max_size = GetDefaultMaxTextureSize(texture_name); return nux::CreateTexture2DFromFile(texture_name, max_size.CP(scale), true); @@ -92,36 +98,23 @@ int UnityWindowStyle::GetCloseButtonPadding() const return 3; } -UnityWindowStyle::BaseTexturePtr UnityWindowStyle::GetCloseIcon() const +UnityWindowStyle::BaseTexturePtr UnityWindowStyle::GetTexture(double scale, WindowTextureType const& type) { - return close_icon_; -} + auto it = unity_window_textures_.find(scale); + if (it == unity_window_textures_.end()) + { + LoadAllTextureInScale(scale); -UnityWindowStyle::BaseTexturePtr UnityWindowStyle::GetCloseIconHighligted() const -{ - return close_icon_highlighted_; -} + it = unity_window_textures_.find(scale); + if (it == unity_window_textures_.end()) + { + LOG_ERROR(logger) << "Failed to create unity window style textures, for scale size: " << scale; + return BaseTexturePtr(nullptr); + } + } -UnityWindowStyle::BaseTexturePtr UnityWindowStyle::GetCloseIconPressed() const -{ - return close_icon_pressed_; + return it->second[unsigned(type)]; } -UnityWindowStyle::BaseTexturePtr UnityWindowStyle::GetBackgroundTop() const -{ - return background_top_; -} - -UnityWindowStyle::BaseTexturePtr UnityWindowStyle::GetBackgroundLeft() const -{ - return background_left_; -} - -UnityWindowStyle::BaseTexturePtr UnityWindowStyle::GetBackgroundCorner() const -{ - return background_corner_; -} - - -} -} +} // namespace ui +} // namespace unity diff --git a/unity-shared/UnityWindowStyle.h b/unity-shared/UnityWindowStyle.h index 30efb1137..cc04f7d5e 100644 --- a/unity-shared/UnityWindowStyle.h +++ b/unity-shared/UnityWindowStyle.h @@ -16,6 +16,7 @@ * * Authored by: Jason Smith <jason.smith@canonical.com> * Marco Trevisan <marco.trevisan@canonical.com> + * Brandon Schaefer <brandon.schaefer@canonical.com> */ #ifndef UNITYWINDOWSTYLE_H @@ -26,8 +27,21 @@ #include "RawPixel.h" -namespace unity { -namespace ui { +namespace unity +{ +namespace ui +{ + +enum class WindowTextureType : unsigned +{ + BACKGROUND_TOP, + BACKGROUND_LEFT, + BACKGROUND_CORNER, + CLOSE_ICON, + CLOSE_ICON_HIGHLIGHTED, + CLOSE_ICON_PRESSED, + Size +}; class UnityWindowStyle { @@ -37,35 +51,25 @@ public: static UnityWindowStyle::Ptr const& Get(); - BaseTexturePtr GetCloseIcon() const; - BaseTexturePtr GetCloseIconHighligted() const; - BaseTexturePtr GetCloseIconPressed() const; + BaseTexturePtr GetTexture(double scale, WindowTextureType const& type); int GetCloseButtonPadding() const; - - BaseTexturePtr GetBackgroundTop() const; - BaseTexturePtr GetBackgroundLeft() const; - BaseTexturePtr GetBackgroundCorner() const; int GetBorderSize() const; int GetInternalOffset() const; - nux::Property<double> scale; - private: UnityWindowStyle(); void ReloadIcons(); - nux::BaseTexture* LoadTexture(const char* const texture_name) const; + void LoadAllTextureInScale(double scale); + nux::BaseTexture* LoadTexture(double scale, const char* const texture_name) const; RawPixel GetDefaultMaxTextureSize(const char* const texture_name) const; - BaseTexturePtr background_top_; - BaseTexturePtr background_left_; - BaseTexturePtr background_corner_; - BaseTexturePtr close_icon_; - BaseTexturePtr close_icon_highlighted_; - BaseTexturePtr close_icon_pressed_; + typedef std::array<BaseTexturePtr, size_t(WindowTextureType::Size)> UnityWindowTextures; + std::unordered_map<double, UnityWindowTextures> unity_window_textures_; + }; -} -} +} // namespace ui +} // namespace unity #endif diff --git a/unity-shared/UnityWindowView.cpp b/unity-shared/UnityWindowView.cpp index 53585152a..c2bf758e2 100644 --- a/unity-shared/UnityWindowView.cpp +++ b/unity-shared/UnityWindowView.cpp @@ -128,34 +128,39 @@ void UnityWindowView::OnClosableChanged(bool closable) return; } - auto const& texture = style()->GetCloseIcon(); - int padding = style()->GetCloseButtonPadding(); + auto const& texture = style()->GetTexture(cv_->DPIScale(), WindowTextureType::CLOSE_ICON); + int padding_raw = style()->GetCloseButtonPadding(); + int padding = cv_->CP(padding_raw); + close_button_ = new IconTexture(texture); close_button_->SetBaseXY(padding, padding); close_button_->SetParentObject(this); close_button_->mouse_enter.connect([this](int, int, unsigned long, unsigned long) { if (close_button_->IsMouseOwner()) - close_button_->SetTexture(style()->GetCloseIconPressed()); + close_button_->SetTexture(style()->GetTexture(cv_->DPIScale(), WindowTextureType::CLOSE_ICON_PRESSED)); else - close_button_->SetTexture(style()->GetCloseIconHighligted()); + close_button_->SetTexture(style()->GetTexture(cv_->DPIScale(), WindowTextureType::CLOSE_ICON_HIGHLIGHTED)); }); close_button_->mouse_leave.connect([this](int, int, unsigned long, unsigned long) { - close_button_->SetTexture(style()->GetCloseIcon()); + close_button_->SetTexture(style()->GetTexture(cv_->DPIScale(), WindowTextureType::CLOSE_ICON)); }); close_button_->mouse_down.connect([this](int, int, unsigned long, unsigned long) { - close_button_->SetTexture(style()->GetCloseIconPressed()); + close_button_->SetTexture(style()->GetTexture(cv_->DPIScale(), WindowTextureType::CLOSE_ICON_PRESSED)); }); close_button_->mouse_up.connect([this](int, int, unsigned long, unsigned long) { bool inside = close_button_->IsMouseInside(); - close_button_->SetTexture(inside ? style()->GetCloseIconHighligted() : style()->GetCloseIcon()); + if (inside) + close_button_->SetTexture(style()->GetTexture(cv_->DPIScale(), WindowTextureType::CLOSE_ICON_HIGHLIGHTED)); + else + close_button_->SetTexture(style()->GetTexture(cv_->DPIScale(), WindowTextureType::CLOSE_ICON)); }); close_button_->mouse_click.connect([this](int, int, unsigned long, unsigned long) { - close_button_->SetTexture(style()->GetCloseIcon()); + close_button_->SetTexture(style()->GetTexture(cv_->DPIScale(), WindowTextureType::CLOSE_ICON)); request_close.emit(); }); @@ -346,6 +351,9 @@ void UnityWindowView::DrawBackground(nux::GraphicsEngine& GfxContext, nux::Geome { int border_raw = style()->GetBorderSize(); int border = cv_->CP(border_raw); + float scale = cv_->DPIScale(); + + auto background_corner_textrue = style()->GetTexture(scale, WindowTextureType::BACKGROUND_CORNER)->GetDeviceTexture(); GfxContext.GetRenderStates().SetBlend(true, GL_ONE, GL_ONE_MINUS_SRC_ALPHA); @@ -359,7 +367,7 @@ void UnityWindowView::DrawBackground(nux::GraphicsEngine& GfxContext, nux::Geome texxform.u1 = border; texxform.v1 = border; GfxContext.QRP_1Tex (geo.x, geo.y, - border, border, style()->GetBackgroundCorner()->GetDeviceTexture(), texxform, nux::color::White); + border, border, background_corner_textrue, texxform, nux::color::White); // Draw TOP-RIGHT CORNER texxform.u0 = 0; @@ -369,7 +377,7 @@ void UnityWindowView::DrawBackground(nux::GraphicsEngine& GfxContext, nux::Geome texxform.flip_u_coord = true; texxform.flip_v_coord = false; GfxContext.QRP_1Tex (geo.x + geo.width - border, geo.y, - border, border, style()->GetBackgroundCorner()->GetDeviceTexture(), texxform, nux::color::White); + border, border, background_corner_textrue, texxform, nux::color::White); // Draw BOTTOM-LEFT CORNER texxform.u0 = 0; @@ -379,7 +387,7 @@ void UnityWindowView::DrawBackground(nux::GraphicsEngine& GfxContext, nux::Geome texxform.flip_u_coord = false; texxform.flip_v_coord = true; GfxContext.QRP_1Tex (geo.x, geo.y + geo.height - border, - border, border, style()->GetBackgroundCorner()->GetDeviceTexture(), texxform, nux::color::White); + border, border, background_corner_textrue, texxform, nux::color::White); // Draw BOTTOM-RIGHT CORNER texxform.u0 = 0; @@ -389,10 +397,11 @@ void UnityWindowView::DrawBackground(nux::GraphicsEngine& GfxContext, nux::Geome texxform.flip_u_coord = true; texxform.flip_v_coord = true; GfxContext.QRP_1Tex (geo.x + geo.width - border, geo.y + geo.height - border, - border, border, style()->GetBackgroundCorner()->GetDeviceTexture(), texxform, nux::color::White); + border, border, background_corner_textrue, texxform, nux::color::White); - int top_width_raw = style()->GetBackgroundTop()->GetWidth(); - int top_height_raw = style()->GetBackgroundTop()->GetHeight(); + auto background_top = style()->GetTexture(scale, WindowTextureType::BACKGROUND_TOP); + int top_width_raw = background_top->GetWidth(); + int top_height_raw = background_top->GetHeight(); int top_width = cv_->CP(top_width_raw); int top_height = cv_->CP(top_height_raw); @@ -403,7 +412,7 @@ void UnityWindowView::DrawBackground(nux::GraphicsEngine& GfxContext, nux::Geome texxform.v1 = top_height; texxform.flip_u_coord = false; texxform.flip_v_coord = false; - GfxContext.QRP_1Tex (geo.x + border, geo.y, geo.width - border - border, border, style()->GetBackgroundTop()->GetDeviceTexture(), texxform, nux::color::White); + GfxContext.QRP_1Tex (geo.x + border, geo.y, geo.width - border - border, border, background_top->GetDeviceTexture(), texxform, nux::color::White); // Draw BOTTOM BORDER texxform.u0 = 0; @@ -412,10 +421,11 @@ void UnityWindowView::DrawBackground(nux::GraphicsEngine& GfxContext, nux::Geome texxform.v1 = top_height; texxform.flip_u_coord = false; texxform.flip_v_coord = true; - GfxContext.QRP_1Tex (geo.x + border, geo.y + geo.height - border, geo.width - border - border, border, style()->GetBackgroundTop()->GetDeviceTexture(), texxform, nux::color::White); + GfxContext.QRP_1Tex (geo.x + border, geo.y + geo.height - border, geo.width - border - border, border, background_top->GetDeviceTexture(), texxform, nux::color::White); - int left_width_raw = style()->GetBackgroundLeft()->GetWidth(); - int left_height_raw = style()->GetBackgroundLeft()->GetHeight(); + auto background_left = style()->GetTexture(scale, WindowTextureType::BACKGROUND_LEFT); + int left_width_raw = background_left->GetWidth(); + int left_height_raw = background_left->GetHeight(); int left_width = cv_->CP(left_width_raw); int left_height = cv_->CP(left_height_raw); @@ -426,7 +436,7 @@ void UnityWindowView::DrawBackground(nux::GraphicsEngine& GfxContext, nux::Geome texxform.v1 = left_height; texxform.flip_u_coord = false; texxform.flip_v_coord = false; - GfxContext.QRP_1Tex (geo.x, geo.y + border, border, geo.height - border - border, style()->GetBackgroundLeft()->GetDeviceTexture(), texxform, nux::color::White); + GfxContext.QRP_1Tex (geo.x, geo.y + border, border, geo.height - border - border, background_left->GetDeviceTexture(), texxform, nux::color::White); // Draw RIGHT BORDER texxform.u0 = 0; @@ -435,7 +445,7 @@ void UnityWindowView::DrawBackground(nux::GraphicsEngine& GfxContext, nux::Geome texxform.v1 = left_height; texxform.flip_u_coord = true; texxform.flip_v_coord = false; - GfxContext.QRP_1Tex (geo.x + geo.width - border, geo.y + border, border, geo.height - border - border, style()->GetBackgroundLeft()->GetDeviceTexture(), texxform, nux::color::White); + GfxContext.QRP_1Tex (geo.x + geo.width - border, geo.y + border, border, geo.height - border - border, background_left->GetDeviceTexture(), texxform, nux::color::White); GfxContext.GetRenderStates().SetBlend(false); } |
