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
{
2017-03-28 19:39:03 +00:00
virtual void DrawSelector(double xofs, double yofs, TextureID tex)
2017-02-11 15:11:48 +00:00
{
if (tex.isNull())
{
if ((Menu.MenuTime() % 8) < 6)
{
screen.DrawText(ConFont, OptionMenuSettings.mFontColorSelection,
(mXpos + xofs - 160) * CleanXfac + screen.GetWidth() / 2,
(mYpos + yofs - 100) * CleanYfac + screen.GetHeight() / 2,
"\xd",
DTA_CellX, 8 * CleanXfac,
DTA_CellY, 8 * CleanYfac
);
}
}
else
{
2017-02-18 17:35:44 +00:00
screen.DrawTexture (tex, true, mXpos + xofs, mYpos + yofs, DTA_Clean, true);
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
}
override void Drawer(bool selected)
{
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);
if (mYpos >= 0)
{
2019-02-19 23:43:31 +00:00
if (mSubstitute == "" || TexMan.OkForLocalization(mTexture))
{
if (mCentered) x -= vec.X / 2;
screen.DrawTexture (mTexture, true, x, mYpos, DTA_Clean, true);
}
else
{
if (mCentered) x -= mFont.StringWidth(mSubstitute)/2;
screen.DrawText(mFont, mColor, x, mYpos, mSubstitute, DTA_Clean, true);
}
2017-02-11 15:11:48 +00:00
}
else
{
2017-02-19 16:24:30 +00:00
x = (mXpos - 160) * CleanXfac + (Screen.GetWidth()>>1);
2019-02-19 23:43:31 +00:00
if (mSubstitute == "" || TexMan.OkForLocalization(mTexture))
{
if (mCentered) x -= (vec.X * CleanXfac)/2;
screen.DrawTexture (mTexture, true, x, -mYpos*CleanYfac, DTA_CleanNoMove, true);
}
else
{
if (mCentered) x -= (mFont.StringWidth(mSubstitute) * CleanXfac)/2;
screen.DrawText(mFont, mColor, x, mYpos, mSubstitute, DTA_CleanNoMove, true);
}
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;
}
override void Drawer(bool selected)
{
if (mText.Length() != 0)
{
String text = Stringtable.Localize(mText);
if (mYpos >= 0)
{
2017-02-19 16:24:30 +00:00
double x = mXpos;
2017-02-11 15:11:48 +00:00
if (mCentered) x -= mFont.StringWidth(text)/2;
screen.DrawText(mFont, mColor, x, mYpos, text, DTA_Clean, true);
}
else
{
2017-02-19 16:24:30 +00:00
double x = (mXpos - 160) * CleanXfac + (Screen.GetWidth() >> 1);
2017-02-11 15:11:48 +00:00
if (mCentered) x -= (mFont.StringWidth(text) * CleanXfac)/2;
screen.DrawText (mFont, mColor, x, -mYpos*CleanYfac, text, DTA_CleanNoMove, true);
}
}
}
}
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)
{
return mEnabled && y >= mYpos && y < mYpos + mHeight; // no x check here
}
override bool Selectable()
{
return mEnabled;
}
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;
2017-02-12 13:04:48 +00:00
mHotkey = hotkey.CharCodeAt(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;
2017-02-12 13:04:48 +00:00
mHotkey = hotkey.CharCodeAt(0);
2017-02-11 15:11:48 +00:00
}
override void Drawer(bool selected)
{
screen.DrawText(mFont, selected ? mColorSelected : mColor, mXpos, mYpos, mText, DTA_Clean, true);
}
override int GetWidth()
{
2017-03-13 22:02:59 +00:00
return max(1, mFont.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-19 00:22:12 +00:00
String mSubstitute;
Font mFont;
int mColor;
2017-02-11 15:11:48 +00:00
2019-02-19 00:22:12 +00:00
void Init(ListMenuDescriptor desc, TextureID patch, String hotkey, Name child, int param = 0, String subst = "")
2017-02-12 00:18:49 +00:00
{
Super.Init(desc.mXpos, desc.mYpos, desc.mLinespacing, child, param);
2017-02-12 13:04:48 +00:00
mHotkey = hotkey.CharCodeAt(0);
2017-02-12 00:18:49 +00:00
mTexture = patch;
2019-02-19 00:22:12 +00:00
mSubstitute = subst;
mFont = desc.mFont;
mColor = desc.mFontColor;
2017-02-12 00:18:49 +00:00
}
2019-02-19 00:22:12 +00:00
void InitDirect(double x, double y, int height, TextureID patch, String hotkey, Name child, int param = 0, String subst = "", Font fnt = null, int col = Font.CR_UNTRANSLATED)
2017-02-11 15:11:48 +00:00
{
Super.Init(x, y, height, child, param);
2017-02-12 13:04:48 +00:00
mHotkey = hotkey.CharCodeAt(0);
2017-02-11 15:11:48 +00:00
mTexture = patch;
2019-02-19 00:22:12 +00:00
mSubstitute = subst;
mFont = fnt;
mColor = col;
2017-02-11 15:11:48 +00:00
}
override void Drawer(bool selected)
{
2019-02-19 00:22:12 +00:00
if (mSubstitute == "" || TexMan.OkForLocalization(mTexture))
{
screen.DrawTexture (mTexture, true, mXpos, mYpos, DTA_Clean, true);
}
else
{
screen.DrawText(mFont, mColor, mXpos, mYpos, mSubstitute, DTA_Clean, true);
}
2017-02-11 15:11:48 +00:00
}
override int GetWidth()
{
2019-02-19 00:22:12 +00:00
if (mSubstitute == "" || TexMan.OkForLocalization(mTexture))
{
return TexMan.GetSize(mTexture);
}
else
{
return max(1, mFont.StringWidth(StringTable.Localize(mSubstitute)));
}
2017-02-11 15:11:48 +00:00
}
}