2017-02-11 15:11:48 +00:00
|
|
|
/*
|
|
|
|
** listmenu.cpp
|
|
|
|
** A simple menu consisting of a list of items
|
|
|
|
**
|
|
|
|
**---------------------------------------------------------------------------
|
|
|
|
** Copyright 2010-2017 Christoph Oelckers
|
|
|
|
** All rights reserved.
|
|
|
|
**
|
|
|
|
** Redistribution and use in source and binary forms, with or without
|
|
|
|
** modification, are permitted provided that the following conditions
|
|
|
|
** are met:
|
|
|
|
**
|
|
|
|
** 1. Redistributions of source code must retain the above copyright
|
|
|
|
** notice, this list of conditions and the following disclaimer.
|
|
|
|
** 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
** notice, this list of conditions and the following disclaimer in the
|
|
|
|
** documentation and/or other materials provided with the distribution.
|
|
|
|
** 3. The name of the author may not be used to endorse or promote products
|
|
|
|
** derived from this software without specific prior written permission.
|
|
|
|
**
|
|
|
|
** THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
** IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
** OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
|
** IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
** INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
** NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
|
|
** THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
**---------------------------------------------------------------------------
|
|
|
|
**
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
class ListMenuItem : MenuItemBase
|
|
|
|
{
|
2020-10-04 08:15:52 +00:00
|
|
|
protected void DrawText(ListMenuDescriptor desc, Font fnt, int color, double x, double y, String text, bool ontop = false)
|
|
|
|
{
|
|
|
|
int w = desc ? desc.DisplayWidth() : ListMenuDescriptor.CleanScale;
|
|
|
|
int h = desc ? desc.DisplayHeight() : -1;
|
|
|
|
if (w == ListMenuDescriptor.CleanScale)
|
|
|
|
{
|
|
|
|
screen.DrawText(fnt, color, x, y, text, ontop? DTA_CleanTop : DTA_Clean, true);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
screen.DrawText(fnt, color, x, y, text, DTA_VirtualWidth, w, DTA_VirtualHeight, h, DTA_FullscreenScale, FSMode_ScaleToFit43);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected void DrawTexture(ListMenuDescriptor desc, TextureID tex, double x, double y, bool ontop = false)
|
|
|
|
{
|
|
|
|
int w = desc ? desc.DisplayWidth() : ListMenuDescriptor.CleanScale;
|
|
|
|
int h = desc ? desc.DisplayHeight() : -1;
|
|
|
|
if (w == ListMenuDescriptor.CleanScale)
|
|
|
|
{
|
|
|
|
screen.DrawTexture(tex, true, x, y, ontop ? DTA_CleanTop : DTA_Clean, true);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
screen.DrawTexture(tex, true, x, y, DTA_VirtualWidth, w, DTA_VirtualHeight, h, DTA_FullscreenScale, FSMode_ScaleToFit43);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void DrawSelector(double xofs, double yofs, TextureID tex, ListMenuDescriptor desc = null)
|
2017-02-11 15:11:48 +00:00
|
|
|
{
|
|
|
|
if (tex.isNull())
|
|
|
|
{
|
|
|
|
if ((Menu.MenuTime() % 8) < 6)
|
|
|
|
{
|
2020-10-04 08:15:52 +00:00
|
|
|
DrawText(desc, ConFont, OptionMenuSettings.mFontColorSelection, mXpos + xofs, mYpos + yofs + 8, "\xd");
|
2017-02-11 15:11:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-10-04 08:15:52 +00:00
|
|
|
DrawTexture(desc, tex, mXpos + xofs, mYpos + yofs);
|
2017-02-11 15:11:48 +00:00
|
|
|
}
|
|
|
|
}
|
2020-10-04 08:15:52 +00:00
|
|
|
|
|
|
|
// We cannot extend Drawer here because it is inherited from the parent class.
|
|
|
|
virtual void Draw(bool selected, ListMenuDescriptor desc)
|
|
|
|
{
|
|
|
|
Drawer(selected); // fall back to the legacy version, if not overridden
|
|
|
|
}
|
2017-02-11 15:11:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//=============================================================================
|
|
|
|
//
|
|
|
|
// static patch
|
|
|
|
//
|
|
|
|
//=============================================================================
|
|
|
|
|
|
|
|
class ListMenuItemStaticPatch : ListMenuItem
|
|
|
|
{
|
|
|
|
TextureID mTexture;
|
|
|
|
bool mCentered;
|
2019-02-19 23:43:31 +00:00
|
|
|
String mSubstitute;
|
|
|
|
Font mFont;
|
|
|
|
int mColor;
|
2017-02-11 15:11:48 +00:00
|
|
|
|
2019-02-20 17:54:29 +00:00
|
|
|
void Init(ListMenuDescriptor desc, double x, double y, TextureID patch, bool centered = false, String substitute = "")
|
2017-02-11 15:11:48 +00:00
|
|
|
{
|
|
|
|
Super.Init(x, y);
|
|
|
|
mTexture = patch;
|
|
|
|
mCentered = centered;
|
2019-02-19 23:43:31 +00:00
|
|
|
mSubstitute = substitute;
|
|
|
|
mFont = desc.mFont;
|
|
|
|
mColor = desc.mFontColor;
|
|
|
|
|
2017-02-11 15:11:48 +00:00
|
|
|
}
|
|
|
|
|
2020-10-04 08:15:52 +00:00
|
|
|
override void Draw(bool selected, ListMenuDescriptor desc)
|
2017-02-11 15:11:48 +00:00
|
|
|
{
|
|
|
|
if (!mTexture.Exists())
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-02-19 16:24:30 +00:00
|
|
|
double x = mXpos;
|
2017-02-11 15:11:48 +00:00
|
|
|
Vector2 vec = TexMan.GetScaledSize(mTexture);
|
2020-10-04 08:15:52 +00:00
|
|
|
|
|
|
|
if (mSubstitute == "" || TexMan.OkForLocalization(mTexture, mSubstitute))
|
2017-02-11 15:11:48 +00:00
|
|
|
{
|
2020-10-04 08:15:52 +00:00
|
|
|
if (mCentered) x -= vec.X / 2;
|
|
|
|
DrawTexture(desc, mTexture, x, abs(mYpos), mYpos < 0);
|
2017-02-11 15:11:48 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-10-04 08:15:52 +00:00
|
|
|
let font = generic_ui ? NewSmallFont : mFont;
|
|
|
|
if (mCentered) x -= font.StringWidth(mSubstitute) / 2;
|
|
|
|
DrawText(desc, font, mColor, x, abs(mYpos), mSubstitute, mYpos < 0);
|
2017-02-11 15:11:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-12 13:04:48 +00:00
|
|
|
class ListMenuItemStaticPatchCentered : ListMenuItemStaticPatch
|
2017-02-12 00:18:49 +00:00
|
|
|
{
|
2019-02-20 17:54:29 +00:00
|
|
|
void Init(ListMenuDescriptor desc, double x, double y, TextureID patch)
|
2017-02-12 00:18:49 +00:00
|
|
|
{
|
2019-02-20 17:54:29 +00:00
|
|
|
Super.Init(desc, x, y, patch, true);
|
2017-02-12 00:18:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-11 15:11:48 +00:00
|
|
|
//=============================================================================
|
|
|
|
//
|
|
|
|
// static text
|
|
|
|
//
|
|
|
|
//=============================================================================
|
|
|
|
|
|
|
|
class ListMenuItemStaticText : ListMenuItem
|
|
|
|
{
|
|
|
|
String mText;
|
|
|
|
Font mFont;
|
|
|
|
int mColor;
|
|
|
|
bool mCentered;
|
|
|
|
|
2017-03-13 08:30:33 +00:00
|
|
|
void Init(ListMenuDescriptor desc, double x, double y, String text, int color = -1)
|
2017-02-12 13:04:48 +00:00
|
|
|
{
|
|
|
|
Super.Init(x, y);
|
|
|
|
mText = text;
|
|
|
|
mFont = desc.mFont;
|
|
|
|
mColor = color >= 0? color : desc.mFontColor;
|
|
|
|
mCentered = false;
|
|
|
|
}
|
|
|
|
|
2017-02-19 16:24:30 +00:00
|
|
|
void InitDirect(double x, double y, String text, Font font, int color = Font.CR_UNTRANSLATED, bool centered = false)
|
2017-02-11 15:11:48 +00:00
|
|
|
{
|
|
|
|
Super.Init(x, y);
|
|
|
|
mText = text;
|
|
|
|
mFont = font;
|
|
|
|
mColor = color;
|
|
|
|
mCentered = centered;
|
|
|
|
}
|
|
|
|
|
2020-10-04 08:15:52 +00:00
|
|
|
override void Draw(bool selected, ListMenuDescriptor desc)
|
2017-02-11 15:11:48 +00:00
|
|
|
{
|
|
|
|
if (mText.Length() != 0)
|
|
|
|
{
|
2019-04-10 20:25:31 +00:00
|
|
|
let font = generic_ui? NewSmallFont : mFont;
|
|
|
|
|
2017-02-11 15:11:48 +00:00
|
|
|
String text = Stringtable.Localize(mText);
|
2020-10-04 08:15:52 +00:00
|
|
|
|
|
|
|
double x = mXpos;
|
|
|
|
if (mCentered) x -= font.StringWidth(text) / 2;
|
|
|
|
DrawText(desc, font, mColor, x, abs(mYpos), text, mYpos < 0);
|
2017-02-11 15:11:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-12 13:04:48 +00:00
|
|
|
class ListMenuItemStaticTextCentered : ListMenuItemStaticText
|
2017-02-12 00:18:49 +00:00
|
|
|
{
|
2017-02-19 16:24:30 +00:00
|
|
|
void Init(ListMenuDescriptor desc, double x, double y, String text, int color = -1)
|
2017-02-12 00:18:49 +00:00
|
|
|
{
|
2017-02-12 13:04:48 +00:00
|
|
|
Super.Init(desc, x, y, text, color);
|
|
|
|
mCentered = true;
|
2017-02-12 00:18:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-11 15:11:48 +00:00
|
|
|
//=============================================================================
|
|
|
|
//
|
|
|
|
// selectable items
|
|
|
|
//
|
|
|
|
//=============================================================================
|
|
|
|
|
|
|
|
class ListMenuItemSelectable : ListMenuItem
|
|
|
|
{
|
|
|
|
int mHotkey;
|
|
|
|
int mHeight;
|
|
|
|
int mParam;
|
|
|
|
|
2017-02-19 16:24:30 +00:00
|
|
|
protected void Init(double x, double y, int height, Name childmenu, int param = -1)
|
2017-02-11 15:11:48 +00:00
|
|
|
{
|
|
|
|
Super.Init(x, y, childmenu);
|
|
|
|
mHeight = height;
|
|
|
|
mParam = param;
|
|
|
|
mHotkey = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
override bool CheckCoordinate(int x, int y)
|
|
|
|
{
|
2020-10-07 16:29:34 +00:00
|
|
|
return mEnabled > 0 && y >= mYpos && y < mYpos + mHeight; // no x check here
|
2017-02-11 15:11:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
override bool Selectable()
|
|
|
|
{
|
2020-10-07 14:00:39 +00:00
|
|
|
return mEnabled > 0;
|
2017-02-11 15:11:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
override bool CheckHotkey(int c)
|
|
|
|
{
|
2017-05-18 18:44:15 +00:00
|
|
|
return c > 0 && c == mHotkey;
|
2017-02-11 15:11:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
override bool Activate()
|
|
|
|
{
|
|
|
|
Menu.SetMenu(mAction, mParam);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
override bool MouseEvent(int type, int x, int y)
|
|
|
|
{
|
|
|
|
if (type == Menu.MOUSE_Release)
|
|
|
|
{
|
|
|
|
let m = Menu.GetCurrentMenu();
|
|
|
|
if (m != NULL && m.MenuEvent(Menu.MKEY_Enter, true))
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
override Name, int GetAction()
|
|
|
|
{
|
|
|
|
return mAction, mParam;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//=============================================================================
|
|
|
|
//
|
|
|
|
// text item
|
|
|
|
//
|
|
|
|
//=============================================================================
|
|
|
|
|
2017-02-12 00:18:49 +00:00
|
|
|
class ListMenuItemTextItem : ListMenuItemSelectable
|
2017-02-11 15:11:48 +00:00
|
|
|
{
|
|
|
|
String mText;
|
|
|
|
Font mFont;
|
|
|
|
int mColor;
|
|
|
|
int mColorSelected;
|
|
|
|
|
2017-02-12 00:18:49 +00:00
|
|
|
void Init(ListMenuDescriptor desc, String text, String hotkey, Name child, int param = 0)
|
|
|
|
{
|
|
|
|
Super.Init(desc.mXpos, desc.mYpos, desc.mLinespacing, child, param);
|
|
|
|
mText = text;
|
|
|
|
mFont = desc.mFont;
|
|
|
|
mColor = desc.mFontColor;
|
|
|
|
mColorSelected = desc.mFontcolor2;
|
2019-04-13 07:31:36 +00:00
|
|
|
mHotkey = hotkey.GetNextCodePoint(0);
|
2017-02-12 00:18:49 +00:00
|
|
|
}
|
|
|
|
|
2017-02-19 16:24:30 +00:00
|
|
|
void InitDirect(double x, double y, int height, String hotkey, String text, Font font, int color, int color2, Name child, int param = 0)
|
2017-02-11 15:11:48 +00:00
|
|
|
{
|
|
|
|
Super.Init(x, y, height, child, param);
|
|
|
|
mText = text;
|
|
|
|
mFont = font;
|
|
|
|
mColor = color;
|
|
|
|
mColorSelected = color2;
|
2019-04-13 07:31:36 +00:00
|
|
|
int pos = 0;
|
|
|
|
mHotkey = hotkey.GetNextCodePoint(0);
|
2017-02-11 15:11:48 +00:00
|
|
|
}
|
|
|
|
|
2020-10-04 08:15:52 +00:00
|
|
|
override void Draw(bool selected, ListMenuDescriptor desc)
|
2017-02-11 15:11:48 +00:00
|
|
|
{
|
2020-10-04 08:15:52 +00:00
|
|
|
let font = generic_ui ? NewSmallFont : mFont;
|
|
|
|
DrawText(desc, font, selected ? mColorSelected : mColor, mXpos, mYpos, mText);
|
2017-02-11 15:11:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
override int GetWidth()
|
|
|
|
{
|
2019-04-10 20:25:31 +00:00
|
|
|
let font = generic_ui? NewSmallFont : mFont;
|
|
|
|
return max(1, font.StringWidth(StringTable.Localize(mText)));
|
2017-02-11 15:11:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//=============================================================================
|
|
|
|
//
|
|
|
|
// patch item
|
|
|
|
//
|
|
|
|
//=============================================================================
|
|
|
|
|
2017-02-12 00:18:49 +00:00
|
|
|
class ListMenuItemPatchItem : ListMenuItemSelectable
|
2017-02-11 15:11:48 +00:00
|
|
|
{
|
|
|
|
TextureID mTexture;
|
2019-02-20 23:35:27 +00:00
|
|
|
|
|
|
|
void Init(ListMenuDescriptor desc, TextureID patch, String hotkey, Name child, int param = 0)
|
2017-02-12 00:18:49 +00:00
|
|
|
{
|
|
|
|
Super.Init(desc.mXpos, desc.mYpos, desc.mLinespacing, child, param);
|
2019-04-13 07:31:36 +00:00
|
|
|
mHotkey = hotkey.GetNextCodePoint(0);
|
2017-02-12 00:18:49 +00:00
|
|
|
mTexture = patch;
|
|
|
|
}
|
|
|
|
|
2019-02-20 23:35:27 +00:00
|
|
|
void InitDirect(double x, double y, int height, TextureID patch, String hotkey, Name child, int param = 0)
|
2017-02-11 15:11:48 +00:00
|
|
|
{
|
|
|
|
Super.Init(x, y, height, child, param);
|
2019-04-13 07:31:36 +00:00
|
|
|
mHotkey = hotkey.GetNextCodePoint(0);
|
2017-02-11 15:11:48 +00:00
|
|
|
mTexture = patch;
|
|
|
|
}
|
|
|
|
|
2020-10-04 08:15:52 +00:00
|
|
|
override void Draw(bool selected, ListMenuDescriptor desc)
|
2017-02-11 15:11:48 +00:00
|
|
|
{
|
2020-10-04 08:15:52 +00:00
|
|
|
DrawTexture(desc, mTexture, mXpos, mYpos);
|
2017-02-11 15:11:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
override int GetWidth()
|
|
|
|
{
|
2019-02-20 23:35:27 +00:00
|
|
|
return TexMan.GetSize(mTexture);
|
2017-02-11 15:11:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-10-07 16:29:34 +00:00
|
|
|
//=============================================================================
|
|
|
|
//
|
|
|
|
// caption - draws a text using the customizer's caption hook
|
|
|
|
//
|
|
|
|
//=============================================================================
|
|
|
|
|
|
|
|
class ListMenuItemCaptionItem : ListMenuItem
|
|
|
|
{
|
|
|
|
String mText;
|
|
|
|
Font mFont;
|
|
|
|
|
|
|
|
void Init(ListMenuDescriptor desc, String text, String fnt = "BigFont")
|
|
|
|
{
|
|
|
|
Super.Init(0, 0);
|
|
|
|
mText = text;
|
|
|
|
mFont = Font.FindFont(fnt);
|
|
|
|
}
|
|
|
|
|
|
|
|
override void Draw(bool selected, ListMenuDescriptor desc)
|
|
|
|
{
|
|
|
|
let font = generic_ui || !desc.mFont ? NewSmallFont : desc.mFont;
|
|
|
|
if (font && mText.Length() > 0)
|
|
|
|
{
|
|
|
|
menuDelegate.DrawCaption(mText, font, 0, true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|