Files
Aerofoil/PortabilityLayer/PLPopupMenuWidget.cpp

142 lines
4.0 KiB
C++
Raw Normal View History

2020-01-05 02:33:03 -05:00
#include "PLPopupMenuWidget.h"
2020-02-23 20:21:04 -05:00
#include "MenuManager.h"
#include "PLMenus.h"
#include "PLPasStr.h"
#include "PLStandardColors.h"
2020-03-01 17:01:35 -05:00
#include "PLTimeTaggedVOSEvent.h"
2020-02-23 20:21:04 -05:00
#include "FontFamily.h"
2020-03-01 17:01:35 -05:00
#include "Vec2i.h"
2020-02-23 20:21:04 -05:00
2020-05-18 02:03:17 -04:00
static const int kLightGray = 238;
static const int kMidGray = 221;
static const int kMidDarkGray = 170;
static const int kDarkGray = 102;
2020-01-05 02:33:03 -05:00
namespace PortabilityLayer
{
PopupMenuWidget::PopupMenuWidget(const WidgetBasicState &state)
: WidgetSpec<PopupMenuWidget>(state)
{
}
2020-03-01 17:01:35 -05:00
PopupMenuWidget::~PopupMenuWidget()
{
if (m_menu)
m_menu.Dispose();
}
2020-05-18 02:03:17 -04:00
bool PopupMenuWidget::Init(const WidgetBasicState &state, const void *additionalData)
2020-01-05 02:33:03 -05:00
{
2020-03-01 17:01:35 -05:00
m_menu = ::GetMenu(state.m_resID);
2020-02-23 20:21:04 -05:00
if (!m_menu)
return false;
2020-01-05 02:33:03 -05:00
return true;
}
2020-02-23 20:21:04 -05:00
2020-03-01 17:01:35 -05:00
WidgetHandleState_t PopupMenuWidget::ProcessEvent(const TimeTaggedVOSEvent &evt)
{
if (evt.IsLMouseDownEvent())
{
const GpMouseInputEvent &mouseEvent = evt.m_vosEvent.m_event.m_mouseInputEvent;
const Vec2i globalPoint = Vec2i(mouseEvent.m_x, mouseEvent.m_y);
2020-05-20 17:20:50 -04:00
const Vec2i localPoint = globalPoint - m_window->GetPosition();
2020-03-01 17:01:35 -05:00
if (this->m_rect.Contains(Point::Create(localPoint.m_x, localPoint.m_y)))
{
int16_t part = Capture(Point::Create(localPoint.m_x, localPoint.m_y), nullptr);
if (part >= 1)
return WidgetHandleStates::kActivated;
else
return WidgetHandleStates::kIgnored;
}
}
return WidgetHandleStates::kIgnored;
}
int16_t PopupMenuWidget::Capture(const Point &pos, WidgetUpdateCallback_t callback)
{
MenuManager *mm = PortabilityLayer::MenuManager::GetInstance();
2020-05-20 17:20:50 -04:00
const Vec2i popupMenuPos = m_window->GetPosition() + Vec2i(m_rect.left, m_rect.top);
const Vec2i globalPos = Vec2i(pos.h, pos.v) + m_window->GetPosition();
2020-03-01 17:01:35 -05:00
uint16_t item = 0;
mm->PopupMenuSelect(m_menu, popupMenuPos, globalPos, m_state - 1, &item);
if (item < 1)
return -1;
this->SetState(item);
return item;
}
2020-02-23 20:21:04 -05:00
void PopupMenuWidget::DrawControl(DrawSurface *surface)
{
const Rect rect = m_rect;
const Rect innerRect = rect.Inset(2, 2);
2020-05-18 02:03:17 -04:00
surface->SetForeColor(StdColors::Black());
surface->FillRect(rect);
2020-02-23 20:21:04 -05:00
surface->SetForeColor(StdColors::White());
2020-05-18 02:03:17 -04:00
surface->FillRect(rect.Inset(1, 1));
surface->SetForeColor(RGBAColor::Create(kMidGray, kMidGray, kMidGray, 255));
surface->FillRect(rect.Inset(2, 2));
const Rect inset2Rect = rect.Inset(2, 2);
surface->SetForeColor(RGBAColor::Create(kDarkGray, kDarkGray, kDarkGray, 255));
surface->FillRect(Rect::Create(inset2Rect.bottom, inset2Rect.left, inset2Rect.bottom + 1, inset2Rect.right + 1));
surface->FillRect(Rect::Create(inset2Rect.top, inset2Rect.right, inset2Rect.bottom + 1, inset2Rect.right + 1));
2020-02-23 20:21:04 -05:00
Rect textRect = innerRect;
2020-05-18 03:51:20 -04:00
textRect.right -= 11;
2020-02-23 20:21:04 -05:00
surface->SetSystemFont(12, PortabilityLayer::FontFamilyFlag_Bold);
2020-05-18 02:03:17 -04:00
Point basePoint = Point::Create(textRect.left + 2, (textRect.top + textRect.bottom + surface->MeasureFontAscender() + 1) / 2 - 1);
2020-02-23 20:21:04 -05:00
2020-05-18 02:03:17 -04:00
surface->SetForeColor(StdColors::Black());
2020-02-23 20:21:04 -05:00
surface->DrawStringConstrained(basePoint, GetString(), true, textRect);
Point arrowMidPoint = Point::Create(textRect.right + 5, (textRect.top + textRect.bottom + 1) / 2);
2020-05-18 03:51:20 -04:00
const Rect arrowRects[4] =
2020-02-23 20:21:04 -05:00
{
2020-05-18 03:51:20 -04:00
Rect::Create(arrowMidPoint.v - 2, arrowMidPoint.h - 3, arrowMidPoint.v - 1, arrowMidPoint.h + 4),
2020-02-23 20:21:04 -05:00
Rect::Create(arrowMidPoint.v - 1, arrowMidPoint.h - 2, arrowMidPoint.v, arrowMidPoint.h + 3),
Rect::Create(arrowMidPoint.v, arrowMidPoint.h - 1, arrowMidPoint.v + 1, arrowMidPoint.h + 2),
Rect::Create(arrowMidPoint.v + 1, arrowMidPoint.h, arrowMidPoint.v + 2, arrowMidPoint.h + 1)
};
2020-05-18 03:51:20 -04:00
for (int i = 0; i < 4; i++)
2020-02-23 20:21:04 -05:00
{
const Rect constrainedRect = innerRect.Intersect(arrowRects[i]);
surface->FillRect(constrainedRect);
}
}
2020-03-01 17:01:35 -05:00
void PopupMenuWidget::OnStateChanged()
{
DrawControl(m_window->GetDrawSurface());
}
2020-02-23 20:21:04 -05:00
PLPasStr PopupMenuWidget::GetString() const
{
2020-03-01 17:01:35 -05:00
if (m_state < 1)
return PSTR("");
2020-02-23 20:21:04 -05:00
const Menu *menu = (*m_menu);
return PortabilityLayer::MenuManager::GetInstance()->GetItemText(m_menu, m_state - 1);
}
2020-03-01 17:01:35 -05:00
const THandle<Menu> &PopupMenuWidget::GetMenu() const
{
return m_menu;
}
2020-01-05 02:33:03 -05:00
}