openmohaa/code/uilib/uistatus.cpp

296 lines
5.4 KiB
C++
Raw Normal View History

2016-03-27 11:49:47 +02:00
/*
===========================================================================
Copyright (C) 2015-2024 the OpenMoHAA team
2016-03-27 11:49:47 +02:00
This file is part of OpenMoHAA source code.
OpenMoHAA source code is free software; you can redistribute it
and/or modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of the License,
or (at your option) any later version.
OpenMoHAA source code is distributed in the hope that it will be
useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with OpenMoHAA source code; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
===========================================================================
*/
#include "ui_local.h"
#include "../qcommon/localization.h"
2016-03-27 11:49:47 +02:00
CLASS_DECLARATION( UIWidget, UIWindowSizer, NULL )
{
2023-05-20 23:59:44 +02:00
{ &W_LeftMouseDown, &UIWindowSizer::MouseDown },
{ &W_LeftMouseUp, &UIWindowSizer::MouseUp },
{ &W_LeftMouseDragged, &UIWindowSizer::MouseDragged },
2016-03-27 11:49:47 +02:00
{ NULL, NULL }
};
UIWindowSizer::UIWindowSizer()
{
2023-05-20 23:59:44 +02:00
m_draggingwidget = NULL;
m_mouseState = M_NONE;
// Added in OPM
// So this can be made as the first responder
m_canactivate = true;
2016-03-27 11:49:47 +02:00
}
2023-05-20 23:59:44 +02:00
UIWindowSizer::UIWindowSizer(UIWidget* w)
: UIWindowSizer()
{
m_draggingwidget = w;
}
2016-03-27 11:49:47 +02:00
void UIWindowSizer::Draw
(
void
)
{
2023-05-20 23:59:44 +02:00
m_font->setColor(UWhite);
m_font->Print(0, 0, "o", -1, getVirtualScale());
2023-05-20 23:59:44 +02:00
m_font->setColor(UBlack);
m_font->Print(0, 0, "p", -1, getVirtualScale());
2016-03-27 11:49:47 +02:00
}
void UIWindowSizer::FrameInitialized
(
void
)
{
2023-05-20 23:59:44 +02:00
setFont("marlett");
2016-03-27 11:49:47 +02:00
}
void UIWindowSizer::MouseDown
(
Event *ev
)
{
2023-05-20 23:59:44 +02:00
m_screenDragPoint.x = ev->GetFloat(1);
m_screenDragPoint.y = ev->GetFloat(2);
m_mouseState = M_DRAGGING;
uWinMan.setFirstResponder(this);
2016-03-27 11:49:47 +02:00
}
void UIWindowSizer::MouseUp
(
Event *ev
)
{
2023-05-20 23:59:44 +02:00
if (uWinMan.getFirstResponder() == this) {
uWinMan.setFirstResponder(NULL);
}
m_mouseState = M_NONE;
2016-03-27 11:49:47 +02:00
}
void UIWindowSizer::MouseDragged
(
Event *ev
)
{
2023-05-20 23:59:44 +02:00
UIPoint2D newpoint, delta;
UISize2D newsize;
if (m_mouseState != M_DRAGGING) {
return;
}
newpoint.x = ev->GetFloat(1);
newpoint.y = ev->GetFloat(2);
delta = UIPoint2D(newpoint.x - m_screenDragPoint.x, newpoint.y - m_screenDragPoint.y);
if (delta.x || delta.y)
{
m_screenDragPoint = newpoint;
newsize = m_draggingwidget->getSize();
newsize.width += delta.x;
newsize.height += delta.y;
if (newsize.width < 32) newsize.width = 32;
if (newsize.height < 16) newsize.height = 16;
m_draggingwidget->setSize(newsize);
}
2016-03-27 11:49:47 +02:00
}
void UIWindowSizer::setDraggingWidget
(
UIWidget *w
)
{
2023-05-20 23:59:44 +02:00
m_draggingwidget = w;
2016-03-27 11:49:47 +02:00
}
UIWidget *UIWindowSizer::getDraggingWidget
(
void
)
{
2023-05-20 23:59:44 +02:00
return m_draggingwidget;
2016-03-27 11:49:47 +02:00
}
CLASS_DECLARATION( UIWidget, UIStatusBar, NULL )
{
2023-05-20 23:59:44 +02:00
{ &W_RealignWidget, &UIStatusBar::ParentSized },
{ &W_SizeChanged, &UIStatusBar::SelfSized },
2016-03-27 11:49:47 +02:00
{ NULL, NULL }
};
UIStatusBar::UIStatusBar()
{
m_sizeenabled = NULL;
2023-05-20 23:59:44 +02:00
m_created = false;
m_sizer = NULL;
m_align.alignment = WND_ALIGN_NONE;
m_iFontAlignmentHorizontal = FONT_JUSTHORZ_LEFT;
2016-03-27 11:49:47 +02:00
}
UIStatusBar::UIStatusBar
(
alignment_t align,
float height
)
: UIStatusBar() // Fixed in OPM:
// original builds didn't call this ctor,
// so statusbar text got center-justified due to
// m_iFontAlignmentHorizontal defaulting to FONT_JUSTHORZ_CENTER (0)
2016-03-27 11:49:47 +02:00
{
2023-05-20 23:59:44 +02:00
AlignBar(align, height);
2016-03-27 11:49:47 +02:00
}
void UIStatusBar::FrameInitialized
(
void
)
{
2023-05-20 23:59:44 +02:00
m_created = true;
Connect(this, W_SizeChanged, W_SizeChanged);
if (m_parent) {
m_parent->Connect(this, W_SizeChanged, W_RealignWidget);
}
SelfSized(NULL);
ParentSized(NULL);
2016-03-27 11:49:47 +02:00
}
void UIStatusBar::Draw
(
void
)
{
2023-05-20 23:59:44 +02:00
if (m_align.alignment == WND_ALIGN_BOTTOM) {
DrawBox(0.0, 0.0, m_frame.size.width, 1.0, m_border_color.dark, 1.0);
}
m_font->setColor(m_foreground_color);
m_font->PrintJustified(
getClientFrame(),
m_iFontAlignmentHorizontal,
m_iFontAlignmentVertical,
Sys_LV_CL_ConvertString(m_title.c_str()),
getVirtualScale()
2023-05-20 23:59:44 +02:00
);
if (m_sizer)
{
m_sizer->setForegroundColor(m_foreground_color);
m_sizer->setBackgroundColor(m_background_color, true);
m_sizer->setBackgroundAlpha(m_local_alpha);
}
2016-03-27 11:49:47 +02:00
}
void UIStatusBar::AlignBar
(
alignment_t align,
float height
)
{
2023-05-20 23:59:44 +02:00
m_align.alignment = align;
m_align.dist = height;
2016-03-27 11:49:47 +02:00
}
void UIStatusBar::DontAlignBar
(
void
)
{
2023-05-20 23:59:44 +02:00
m_align.alignment = WND_ALIGN_NONE;
2016-03-27 11:49:47 +02:00
}
void UIStatusBar::EnableSizeBox
(
UIWidget *which
)
{
2023-05-20 23:59:44 +02:00
m_sizeenabled = which;
if (m_created) {
SelfSized(NULL);
}
2016-03-27 11:49:47 +02:00
}
void UIStatusBar::ParentSized
(
Event *ev
)
{
2023-05-20 23:59:44 +02:00
if (m_align.alignment == WND_ALIGN_BOTTOM)
{
UISize2D parentSize = m_parent->getSize();
setFrame(UIRect2D(0.0, parentSize.height - m_align.dist, parentSize.width, m_align.dist));
}
2016-03-27 11:49:47 +02:00
}
void UIStatusBar::SelfSized
(
Event *ev
)
{
2023-05-20 23:59:44 +02:00
if (m_sizeenabled)
{
if (!m_sizer) {
m_sizer = new UIWindowSizer(m_sizeenabled);
m_sizer->InitFrame(this, 0, 0, 16.0 * getHighResScale()[0], 16.0 * getHighResScale()[1], 0);
} else if (m_sizer->getDraggingWidget() != m_sizeenabled) {
m_sizer->setDraggingWidget(m_sizeenabled);
}
2023-05-20 23:59:44 +02:00
UISize2D sizerFrame = m_sizer->getSize();
m_sizer->setFrame(UIRect2D(
UIPoint2D(m_frame.size.width - sizerFrame.width, m_frame.size.height - sizerFrame.height),
sizerFrame
));
}
else if (m_sizer)
{
delete m_sizer;
m_sizer = NULL;
}
2016-03-27 11:49:47 +02:00
}