metaforce/specter/lib/Menu.cpp

327 lines
9.6 KiB
C++
Raw Normal View History

2016-03-04 23:03:47 +00:00
#include "specter/Menu.hpp"
#include "specter/RootView.hpp"
#include "specter/ViewResources.hpp"
2016-01-13 01:31:50 +00:00
2016-01-15 23:32:14 +00:00
#define ROW_HEIGHT 18
#define ITEM_MARGIN 1
2016-03-04 23:03:47 +00:00
namespace specter
2016-01-13 01:31:50 +00:00
{
Menu::Menu(ViewResources& res, View& parentView, IMenuNode* rootNode)
: View(res, parentView)
{
2016-03-30 19:15:32 +00:00
commitResources(res, [&](boo::IGraphicsDataFactory::Context& ctx) -> bool
{
buildResources(ctx, res);
2017-01-29 03:57:48 +00:00
m_vertsBinding.init(ctx, res, 8, m_viewVertBlockBuf);
2016-03-30 19:15:32 +00:00
return true;
});
2016-01-13 01:31:50 +00:00
m_headText.reset(new TextView(res, *this, res.m_mainFont));
m_scroll.m_view.reset(new ScrollView(res, *this, ScrollView::Style::ThinIndicator));
m_content.reset(new ContentView(res, *this));
m_scroll.m_view->setContentView(m_content.get());
reset(rootNode);
}
void Menu::reset(IMenuNode* rootNode)
{
m_rootNode = rootNode;
m_thisNode = rootNode;
ViewResources& res = rootView().viewRes();
2016-01-15 23:32:14 +00:00
for (int i=0 ; i<8 ; ++i)
2016-01-13 01:31:50 +00:00
m_verts[i].m_color = res.themeData().tooltipBackground();
m_vertsBinding.load<decltype(m_verts)>(m_verts);
2016-01-13 01:31:50 +00:00
m_subMenu.reset();
const std::string* headText = rootNode->text();
2016-01-18 23:32:16 +00:00
m_headText->typesetGlyphs(headText?*headText:"", rootView().themeData().uiAltText());
2016-01-13 01:31:50 +00:00
float pf = rootView().viewRes().pixelFactor();
2016-01-15 23:32:14 +00:00
int itemAdv = (ROW_HEIGHT + ITEM_MARGIN*2) * pf;
2016-01-13 01:31:50 +00:00
m_cWidth = m_headText->nominalWidth() + 10*pf;
2016-01-15 23:32:14 +00:00
m_cHeight = headText ? itemAdv : 0;
m_cTop = m_cHeight;
2016-01-13 01:31:50 +00:00
size_t subCount = rootNode->subNodeCount();
m_items.clear();
if (subCount)
{
m_items.reserve(subCount);
for (size_t i=0 ; i<subCount ; ++i)
{
IMenuNode* node = rootNode->subNode(i);
const std::string* nodeText = node->text();
m_items.emplace_back();
ViewChild<std::unique_ptr<ItemView>>& item = m_items.back();
if (nodeText)
{
2016-01-16 04:18:24 +00:00
item.m_view.reset(new ItemView(res, *this, *nodeText, i, node));
2016-01-13 01:31:50 +00:00
m_cWidth = std::max(m_cWidth, int(item.m_view->m_textView->nominalWidth() + 10*pf));
}
2016-01-15 23:32:14 +00:00
m_cHeight += itemAdv;
2016-01-13 01:31:50 +00:00
}
}
}
Menu::Menu(ViewResources& res, View& parentView, IMenuNode* rootNode, IMenuNode* thisNode)
: View(res, parentView), m_rootNode(rootNode), m_thisNode(thisNode)
{
2016-03-30 19:15:32 +00:00
commitResources(res, [&](boo::IGraphicsDataFactory::Context& ctx) -> bool
{
buildResources(ctx, res);
2017-01-29 03:57:48 +00:00
m_vertsBinding.init(ctx, res, 8, m_viewVertBlockBuf);
2016-03-30 19:15:32 +00:00
return true;
});
2016-01-13 01:31:50 +00:00
m_headText.reset(new TextView(res, *this, res.m_mainFont));
m_scroll.m_view.reset(new ScrollView(res, *this, ScrollView::Style::ThinIndicator));
m_content.reset(new ContentView(res, *this));
m_scroll.m_view->setContentView(m_content.get());
}
2016-01-15 23:32:14 +00:00
Menu::ContentView::ContentView(ViewResources& res, Menu& menu)
2016-01-13 01:31:50 +00:00
: View(res, menu), m_menu(menu)
2016-01-15 23:32:14 +00:00
{
2016-03-30 19:15:32 +00:00
commitResources(res, [&](boo::IGraphicsDataFactory::Context& ctx) -> bool
{
buildResources(ctx, res);
2017-01-29 03:57:48 +00:00
m_hlVertsBinding.init(ctx, res, 4, m_viewVertBlockBuf);
2016-03-30 19:15:32 +00:00
return true;
});
2016-01-15 23:32:14 +00:00
m_hlVerts[0].m_color = res.themeData().button1Hover();
m_hlVerts[1].m_color = res.themeData().button2Hover();
m_hlVerts[2].m_color = res.themeData().button1Hover();
m_hlVerts[3].m_color = res.themeData().button2Hover();
}
2017-11-13 06:14:52 +00:00
Menu::ItemView::ItemView(ViewResources& res, Menu& menu, std::string_view text, size_t idx, IMenuNode* node)
2016-01-16 03:57:11 +00:00
: View(res, menu), m_menu(menu), m_idx(idx), m_node(node)
2016-01-13 01:31:50 +00:00
{
2016-03-30 19:15:32 +00:00
commitResources(res, [&](boo::IGraphicsDataFactory::Context& ctx) -> bool
{
buildResources(ctx, res);
return true;
});
2016-03-04 23:03:47 +00:00
m_textView.reset(new specter::TextView(res, *this, res.m_mainFont));
2016-01-13 01:31:50 +00:00
m_textView->typesetGlyphs(text, res.themeData().uiText());
}
2016-01-16 03:57:11 +00:00
void Menu::setVerts(int width, int height, float pf)
2016-01-13 01:31:50 +00:00
{
2016-01-16 03:57:11 +00:00
m_verts[0].m_pos.assign(0, height-m_cTop-pf, 0);
2016-01-15 23:32:14 +00:00
m_verts[1].m_pos.assign(0, 0, 0);
2016-01-16 03:57:11 +00:00
m_verts[2].m_pos.assign(width, height-m_cTop-pf, 0);
2016-01-15 23:32:14 +00:00
m_verts[3].m_pos.assign(width, 0, 0);
2016-01-13 01:31:50 +00:00
2016-01-15 23:32:14 +00:00
m_verts[4].m_pos.assign(0, height, 0);
2016-01-16 03:57:11 +00:00
m_verts[5].m_pos.assign(0, height-m_cTop+pf, 0);
2016-01-15 23:32:14 +00:00
m_verts[6].m_pos.assign(width, height, 0);
2016-01-16 03:57:11 +00:00
m_verts[7].m_pos.assign(width, height-m_cTop+pf, 0);
2016-01-15 23:32:14 +00:00
m_vertsBinding.load<decltype(m_verts)>(m_verts);
2016-01-13 01:31:50 +00:00
}
2016-01-15 23:32:14 +00:00
void Menu::ContentView::setHighlightedItem(size_t idx)
2016-01-13 01:31:50 +00:00
{
2016-01-15 23:32:14 +00:00
if (idx == -1)
{
m_highlightedItem = -1;
return;
}
ViewChild<std::unique_ptr<ItemView>>& vc = m_menu.m_items[idx];
2016-01-13 01:31:50 +00:00
2016-01-15 23:32:14 +00:00
if (!vc.m_view)
{
m_highlightedItem = -1;
return;
}
m_highlightedItem = idx;
const boo::SWindowRect& bgRect = subRect();
const boo::SWindowRect& itemRect = vc.m_view->subRect();
int y = itemRect.location[1] - bgRect.location[1];
m_hlVerts[0].m_pos.assign(0, y+itemRect.size[1], 0);
m_hlVerts[1].m_pos.assign(0, y, 0);
m_hlVerts[2].m_pos.assign(itemRect.size[0], y+itemRect.size[1], 0);
m_hlVerts[3].m_pos.assign(itemRect.size[0], y, 0);
m_hlVertsBinding.load<decltype(m_hlVerts)>(m_hlVerts);
2016-01-15 23:32:14 +00:00
}
void Menu::mouseDown(const boo::SWindowCoord& coord, boo::EMouseButton button, boo::EModifierKey mod)
{
m_scroll.mouseDown(coord, button, mod);
}
void Menu::ContentView::mouseDown(const boo::SWindowCoord& coord, boo::EMouseButton button, boo::EModifierKey mod)
{
for (ViewChild<std::unique_ptr<ItemView>>& v : m_menu.m_items)
v.mouseDown(coord, button, mod);
2016-01-13 01:31:50 +00:00
}
void Menu::ItemView::mouseDown(const boo::SWindowCoord& coord, boo::EMouseButton button, boo::EModifierKey mod)
{
}
void Menu::mouseUp(const boo::SWindowCoord& coord, boo::EMouseButton button, boo::EModifierKey mod)
{
2016-01-15 23:32:14 +00:00
m_scroll.mouseUp(coord, button, mod);
2016-01-16 04:18:24 +00:00
if (m_deferredActivation)
{
IMenuNode* node = m_deferredActivation;
m_deferredActivation = nullptr;
node->activated(coord);
}
2016-01-13 01:31:50 +00:00
}
void Menu::ContentView::mouseUp(const boo::SWindowCoord& coord, boo::EMouseButton button, boo::EModifierKey mod)
{
2016-01-15 23:32:14 +00:00
for (ViewChild<std::unique_ptr<ItemView>>& v : m_menu.m_items)
v.mouseUp(coord, button, mod);
2016-01-13 01:31:50 +00:00
}
void Menu::ItemView::mouseUp(const boo::SWindowCoord& coord, boo::EMouseButton button, boo::EModifierKey mod)
{
2016-01-16 03:57:11 +00:00
if (m_menu.m_content->m_highlightedItem == m_idx)
2016-01-16 04:18:24 +00:00
m_menu.m_deferredActivation = m_node;
2016-01-13 01:31:50 +00:00
}
void Menu::mouseMove(const boo::SWindowCoord& coord)
{
2016-01-15 23:32:14 +00:00
m_scroll.mouseMove(coord);
2016-01-13 01:31:50 +00:00
}
void Menu::ContentView::mouseMove(const boo::SWindowCoord& coord)
{
2016-01-15 23:32:14 +00:00
for (ViewChild<std::unique_ptr<ItemView>>& v : m_menu.m_items)
v.mouseMove(coord);
2016-01-13 01:31:50 +00:00
}
void Menu::ItemView::mouseEnter(const boo::SWindowCoord& coord)
{
2016-01-15 23:32:14 +00:00
m_menu.m_content->setHighlightedItem(m_idx);
2016-01-13 01:31:50 +00:00
}
void Menu::mouseLeave(const boo::SWindowCoord& coord)
{
2016-01-15 23:32:14 +00:00
m_scroll.mouseLeave(coord);
2016-01-13 01:31:50 +00:00
}
void Menu::ContentView::mouseLeave(const boo::SWindowCoord& coord)
{
2016-01-15 23:32:14 +00:00
for (ViewChild<std::unique_ptr<ItemView>>& v : m_menu.m_items)
v.mouseLeave(coord);
2016-01-13 01:31:50 +00:00
}
void Menu::ItemView::mouseLeave(const boo::SWindowCoord& coord)
{
2016-01-15 23:32:14 +00:00
m_menu.m_content->unsetHighlightedItem(m_idx);
2016-01-13 01:31:50 +00:00
}
void Menu::scroll(const boo::SWindowCoord& coord, const boo::SScrollDelta& scroll)
{
2016-01-15 23:32:14 +00:00
m_scroll.scroll(coord, scroll);
}
2016-01-13 01:31:50 +00:00
2016-01-15 23:32:14 +00:00
void Menu::think()
{
m_scroll.m_view->think();
2016-01-13 01:31:50 +00:00
}
void Menu::resized(const boo::SWindowRect& root, const boo::SWindowRect& sub)
{
2016-01-15 23:32:14 +00:00
float pf = rootView().viewRes().pixelFactor();
2016-01-13 01:31:50 +00:00
boo::SWindowRect rect = sub;
2016-01-15 23:32:14 +00:00
rect.size[0] = m_cWidth;
if (rect.location[1] - m_cHeight < 0)
{
rect.location[1] += ROW_HEIGHT*pf;
rect.size[1] = std::min(root.size[1] - rect.location[1], m_cHeight);
}
else
{
rect.location[1] -= m_cHeight;
rect.size[1] = m_cHeight;
}
2016-01-13 01:31:50 +00:00
View::resized(root, rect);
2016-01-15 23:32:14 +00:00
m_scroll.m_view->resized(root, rect);
2016-01-16 03:57:11 +00:00
setVerts(rect.size[0], rect.size[1], pf);
2016-01-15 23:32:14 +00:00
rect.location[0] += 5*pf;
rect.location[1] += rect.size[1] - (ROW_HEIGHT + ITEM_MARGIN - 5)*pf;
rect.size[0] = m_headText->nominalWidth();
rect.size[1] = m_headText->nominalHeight();
m_headText->resized(root, rect);
2016-01-13 01:31:50 +00:00
}
2016-01-15 23:32:14 +00:00
void Menu::ContentView::resized(const boo::SWindowRect& root, const boo::SWindowRect& sub,
const boo::SWindowRect& scissor)
2016-01-13 01:31:50 +00:00
{
2016-01-15 23:32:14 +00:00
View::resized(root, sub);
float pf = rootView().viewRes().pixelFactor();
m_scissorRect = scissor;
boo::SWindowRect itemRect = sub;
itemRect.size[0] = m_menu.m_cWidth;
itemRect.size[1] = ROW_HEIGHT*pf;
itemRect.location[1] += sub.size[1] - m_menu.m_cTop + ITEM_MARGIN*pf;
int itemAdv = (ROW_HEIGHT + ITEM_MARGIN*2) * pf;
for (ViewChild<std::unique_ptr<ItemView>>& c : m_menu.m_items)
{
itemRect.location[1] -= itemAdv;
if (c.m_view)
c.m_view->resized(root, itemRect);
}
}
void Menu::ItemView::resized(const boo::SWindowRect& root, const boo::SWindowRect& sub)
{
View::resized(root, sub);
float pf = rootView().viewRes().pixelFactor();
boo::SWindowRect textRect = sub;
textRect.location[0] += 5*pf;
textRect.location[1] += 5*pf;
m_textView->resized(root, textRect);
2016-01-13 01:31:50 +00:00
}
void Menu::draw(boo::IGraphicsCommandQueue* gfxQ)
{
View::draw(gfxQ);
2016-01-15 23:32:14 +00:00
gfxQ->setShaderDataBinding(m_vertsBinding);
gfxQ->draw(0, 4);
m_scroll.m_view->draw(gfxQ);
gfxQ->setShaderDataBinding(m_vertsBinding);
gfxQ->draw(4, 4);
m_headText->draw(gfxQ);
2016-01-13 01:31:50 +00:00
}
void Menu::ContentView::draw(boo::IGraphicsCommandQueue* gfxQ)
{
View::draw(gfxQ);
2016-01-15 23:32:14 +00:00
gfxQ->setScissor(m_scissorRect);
if (m_highlightedItem != -1)
{
gfxQ->setShaderDataBinding(m_hlVertsBinding);
gfxQ->draw(0, 4);
}
for (ViewChild<std::unique_ptr<ItemView>>& c : m_menu.m_items)
if (c.m_view)
c.m_view->draw(gfxQ);
gfxQ->setScissor(rootView().subRect());
}
void Menu::ItemView::draw(boo::IGraphicsCommandQueue* gfxQ)
{
View::draw(gfxQ);
m_textView->draw(gfxQ);
2016-01-13 01:31:50 +00:00
}
}