Skip to content

Commit

Permalink
[SUTK] Refactored TabView, created a new button graphic called TabBut…
Browse files Browse the repository at this point in the history
…tonGraphic
  • Loading branch information
ravi688 committed Jan 1, 2025
1 parent 97b68bb commit 513972a
Show file tree
Hide file tree
Showing 6 changed files with 129 additions and 53 deletions.
12 changes: 11 additions & 1 deletion sutk/include/sutk/Constants.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,17 @@ namespace SUTK

namespace Notebook
{
static constexpr Color4 TabSelectColor { 0x749bafffu };
namespace Tab
{
static constexpr Color4 SelectColor { 0x749bafffu };
static constexpr f32 CloseButtonSize { 0.5f };
static constexpr f32 LeftMargin { 0.2f };
static constexpr f32 RightMargin { 0.2f };
static constexpr f32 LabelCloseSpacing { };
static constexpr f32 LabelMaxSize { 8 }; // 8 centimeters
static constexpr f32 LabelMinSize { 3 }; // 3 centimeters
static constexpr f32 Height { 0.8f }; // 0.8 centimeters
}
}
}
}
Expand Down
10 changes: 3 additions & 7 deletions sutk/include/sutk/NotebookView.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <sutk/Label.hpp> // for SUTK::Label::set()
#include <sutk/AnimationEngine.hpp>
#include <sutk/MaskedScrollableContainer.hpp>
#include <sutk/TabButtonGraphic.hpp> // for SUTK::TabButtonGraphic

#include <common/Concepts.hpp> // for com::CompareFunction
#include <common/Event.hpp> // for com::Event
Expand Down Expand Up @@ -56,27 +57,22 @@ namespace SUTK
const std::string& getLabel() const noexcept;
};

class ImageButtonGraphic;
class DefaultButtonGraphic;
class NotebookView;

class SUTK_API TabView : public Button
{
friend class NotebookView;
private:
DefaultButtonGraphic* m_graphic;
Button* m_closeButton;
ImageButtonGraphic* m_closeButtonGraphic;
TabButtonGraphic* m_graphic;
// Preserve hover and press color of the m_graphic
// Since we modify those when the tab is selected
// we would need to restore them to their initial values
// using these preserved values.
Color4 m_hoverColor;
Color4 m_pressColor;
Color4 m_idleColor;
static UIDriver::ImageReference s_closeIcon;

Button* getCloseButton() noexcept { return m_closeButton; }
Button* getCloseButton() noexcept { return m_graphic->getCloseButton(); }
public:
TabView(UIDriver& driver, Container* parent) noexcept;
~TabView() noexcept;
Expand Down
35 changes: 35 additions & 0 deletions sutk/include/sutk/TabButtonGraphic.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#pragma once

#include <sutk/ButtonGraphic.hpp> // for SUTK::DefaultButtonGraphicNoLabel

namespace SUTK
{
class Label;
class ImageButtonGraphic;
class Button;

class SUTK_API TabButtonGraphic : public DefaultButtonGraphicNoLabel
{
private:
Label* m_label;
Button* m_closeButton;
ImageButtonGraphic* m_closeButtonGraphic;
Color4 m_hoverColor;
Color4 m_pressColor;
Color4 m_idleColor;
static UIDriver::ImageReference s_closeIcon;

public:
TabButtonGraphic(UIDriver& driver, Container* parent, GfxDriverObjectHandleType textGroup = GFX_DRIVER_OBJECT_NULL_HANDLE) noexcept;
virtual ~TabButtonGraphic() noexcept;

// Overrides
virtual Vec2Df getMinBoundSize() noexcept override;

Button* getCloseButton() noexcept { return m_closeButton; }
Label& getLabel() noexcept { return *m_label; }

void selectedState() noexcept;
void unselectedState() noexcept;
};
}
54 changes: 9 additions & 45 deletions sutk/source/NotebookView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,11 @@
#include <sutk/TextGroupContainer.hpp>
#include <sutk/ContainerUtility.hpp>
#include <sutk/ButtonGraphic.hpp> // for SUTK::ImageButtonGraphic
#include <sutk/TabButtonGraphic.hpp> // for SUTK::TabButtonGraphic
#include <sutk/Panel.hpp> // for SUTK::Panel
#include <sutk/Constants.hpp>

#define TAB_VIEW_MIN_WIDTH 3.0f
#define TAB_BAR_HEIGHT 0.8f
#define TAB_LABEL_LEFT_MARGIN 0.2f
#define TAB_LABEL_TOP_MARGIN 0.2f
#define TAB_LABEL_BOTTOM_MARGIN 0.2f
#define TAB_LABEL_CLOSE_BUTTON_SPACING 0.1f
#define CLOSE_BUTTON_RIGHT_MARGIN 0.2f
#define CLOSE_BUTTON_WIDTH 0.5f
#define CLOSE_BUTTON_HEIGHT 0.5f

#define TAB_VIEW_MIDDLE_SPREAD 0.1f

#define TAB_VIEW_ANIM_START_WIDTH 0.0f
#define DEFAULT_NEW_TAB_ANIM_DURATION 0.28f
Expand Down Expand Up @@ -71,42 +62,21 @@ namespace SUTK
onDeactivate();
}

UIDriver::ImageReference TabView::s_closeIcon = UIDriver::InvalidImage;

TabView::TabView(UIDriver& driver, Container* parent) noexcept : Button(driver, parent, /* isCreateDefaultGraphic: */ true)
TabView::TabView(UIDriver& driver, Container* parent) noexcept : Button(driver, parent, /* isCreateDefaultGraphic: */ false)
{
m_graphic = getGraphicAs<DefaultButtonGraphic>();
m_hoverColor = m_graphic->getHoverColor();
m_pressColor = m_graphic->getPressColor();
m_idleColor = m_graphic->getIdleColor();
Label& label = m_graphic->getLabel();
label.setAlignment(HorizontalAlignment::Left, VerticalAlignment::Middle);
label.fitInParent({ TAB_LABEL_LEFT_MARGIN, TAB_LABEL_CLOSE_BUTTON_SPACING + CLOSE_BUTTON_RIGHT_MARGIN + CLOSE_BUTTON_WIDTH, TAB_LABEL_TOP_MARGIN, TAB_LABEL_BOTTOM_MARGIN });
m_graphic->getAnchorRect()->fitToParentRect();

m_closeButton = driver.createContainer<Button>(this, /* isCreateDefaultGraphic: */ false);
m_closeButton->setRect({ getSize().width - CLOSE_BUTTON_WIDTH - CLOSE_BUTTON_RIGHT_MARGIN,
(getSize().height - CLOSE_BUTTON_HEIGHT) * 0.5f,
CLOSE_BUTTON_WIDTH, CLOSE_BUTTON_HEIGHT });
m_closeButton->getAnchorRect()->moveToRightMiddleOfParent();
m_closeButtonGraphic = driver.createContainer<ImageButtonGraphic>(m_closeButton);
if(!s_closeIcon)
s_closeIcon = driver.loadImage("svg_files/close-square-svgrepo-com.svg");
m_closeButtonGraphic->setImage(s_closeIcon);
m_graphic = driver.createContainer<TabButtonGraphic>(this);
setGraphic(m_graphic);
}

TabView::~TabView() noexcept
{
auto& driver = getUIDriver();
driver.destroyContainer<ImageButtonGraphic>(m_closeButtonGraphic);
driver.destroyContainer<Button>(m_closeButton);
getUIDriver().destroyContainer<TabButtonGraphic>(m_graphic);
}

void TabView::setLabel(const std::string_view str) noexcept
{
m_graphic->getLabel().set(str);
SUTK::Vec2Df size = m_graphic->getMinBoundSize();
setSize({ size.width + TAB_LABEL_CLOSE_BUTTON_SPACING + CLOSE_BUTTON_RIGHT_MARGIN + CLOSE_BUTTON_WIDTH, getSize().height });
setSize({ m_graphic->getMinBoundSize().width, getSize().height });
}

const std::string& TabView::getLabel() noexcept
Expand All @@ -116,18 +86,12 @@ namespace SUTK

void TabView::unselectedState() noexcept
{
auto* graphic = getGraphicAs<DefaultButtonGraphic>();
graphic->setHoverColor(m_hoverColor);
graphic->setPressColor(m_pressColor);
graphic->setIdleColor(m_idleColor);
m_graphic->unselectedState();
}

void TabView::selectedState() noexcept
{
auto* graphic = getGraphicAs<DefaultButtonGraphic>();
graphic->setIdleColor(Constants::Defaults::Notebook::TabSelectColor);
graphic->setHoverColor(Constants::Defaults::Notebook::TabSelectColor);
graphic->setPressColor(Constants::Defaults::Notebook::TabSelectColor);
m_graphic->selectedState();
}

NotebookView::NotebookView(UIDriver& driver, Container* parent, com::Bool isLayoutIgnore, Layer layer) noexcept : VBoxContainer(driver, parent, /* isLockLayout: */ true, isLayoutIgnore, layer), GlobalMouseMoveHandlerObject(driver),
Expand All @@ -140,7 +104,7 @@ namespace SUTK
// NOTE: the 'getDepth() + 10000' is needed to keep the tabs always on top of the pages (pages can be also be scrolled up and overlap with the tabs!)
m_textGroupContainer = driver.createContainer<TextGroupContainer>(this, com::False, getDepth() + 10000);
LayoutAttributes attr = m_textGroupContainer->getLayoutAttributes();
attr.minSize.height = TAB_BAR_HEIGHT;
attr.minSize.height = Constants::Defaults::Notebook::Tab::Height;
attr.prefSize = attr.minSize;
m_textGroupContainer->setLayoutAttributes(attr);
m_tabBarBGPanel = driver.createContainer<Panel>(m_textGroupContainer);
Expand Down
67 changes: 67 additions & 0 deletions sutk/source/TabButtonGraphic.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#include <sutk/TabButtonGraphic.hpp>
#include <sutk/Constants.hpp>
#include <sutk/Label.hpp>
#include <sutk/Button.hpp>

namespace SUTK
{
UIDriver::ImageReference TabButtonGraphic::s_closeIcon = UIDriver::InvalidImage;

TabButtonGraphic::TabButtonGraphic(UIDriver& driver, Container* parent, GfxDriverObjectHandleType textGroup) noexcept : DefaultButtonGraphicNoLabel(driver, parent)
{
m_label = driver.createContainer<Label>(this, textGroup);
m_label->setAlignment(HorizontalAlignment::Left, VerticalAlignment::Middle);
m_label->set("New Button");
m_label->setColor(Color4::black());
m_label->alwaysFitInParent({ Constants::Defaults::Notebook::Tab::LeftMargin,
Constants::Defaults::Notebook::Tab::LabelCloseSpacing
+ Constants::Defaults::Notebook::Tab::CloseButtonSize
+ Constants::Defaults::Notebook::Tab::RightMargin, 0, 0 });

m_closeButton = driver.createContainer<Button>(this, /* isCreateDefaultGraphic: */ false);
m_closeButton->setRect({ getSize().width - Constants::Defaults::Notebook::Tab::CloseButtonSize - Constants::Defaults::Notebook::Tab::RightMargin,
(getSize().height - Constants::Defaults::Notebook::Tab::CloseButtonSize) * 0.5f,
Constants::Defaults::Notebook::Tab::CloseButtonSize,
Constants::Defaults::Notebook::Tab::CloseButtonSize });
m_closeButton->getAnchorRect()->moveToRightMiddleOfParent();
m_closeButtonGraphic = driver.createContainer<ImageButtonGraphic>(m_closeButton);
if(!s_closeIcon)
s_closeIcon = driver.loadImage("svg_files/close-square-svgrepo-com.svg");
m_closeButtonGraphic->setImage(s_closeIcon);

m_hoverColor = getHoverColor();
m_pressColor = getPressColor();
m_idleColor = getIdleColor();
}

TabButtonGraphic::~TabButtonGraphic() noexcept
{
auto& driver = getUIDriver();
driver.destroyContainer<ImageButtonGraphic>(m_closeButtonGraphic);
driver.destroyContainer<Button>(m_closeButton);
driver.destroyContainer<Label>(m_label);
}

Vec2Df TabButtonGraphic::getMinBoundSize() noexcept
{
return { m_label->getHorizontalBound()
+ Constants::Defaults::Notebook::Tab::CloseButtonSize
+ Constants::Defaults::Notebook::Tab::LabelCloseSpacing
+ Constants::Defaults::Notebook::Tab::LeftMargin
+ Constants::Defaults::Notebook::Tab::RightMargin, getSize().height };
}

void TabButtonGraphic::unselectedState() noexcept
{
setHoverColor(m_hoverColor);
setPressColor(m_pressColor);
setIdleColor(m_idleColor);
}

void TabButtonGraphic::selectedState() noexcept
{
setIdleColor(Constants::Defaults::Notebook::Tab::SelectColor);
setHoverColor(Constants::Defaults::Notebook::Tab::SelectColor);
setPressColor(Constants::Defaults::Notebook::Tab::SelectColor);
}
}
4 changes: 4 additions & 0 deletions sutk/source/tests/NotebookTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ namespace SUTK
}
else if(keyCode == KeyCode::D)
this->m_notebookView->removePage(this->m_notebookView->getCurrentPage());
else if(keyCode == KeyCode::R)
this->m_notebookView->getRootPage()->getNext()->setLabel("Hello World, This is New Year");
else if(keyCode == KeyCode::T)
this->m_notebookView->getRootPage()->getNext()->setLabel("I really wanna solve world's greatest challenges!");
this->m_notebookView->dump();
}
});
Expand Down

0 comments on commit 513972a

Please sign in to comment.