📄 statusbar.cpp
字号:
/************************************************************************************
Copyright (c) 2000 Aaron O'Neil
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) Redistributions in binary form must reproduce the above copyright notice on
program startup. Additional credits for program modification are acceptable
but original copyright and credits must be visible at startup.
4) You may charge a reasonable copying fee for any distribution of Mud Master.
You may charge any fee you choose for support of Mud Master. You may not
charge a fee for Mud Master itself.
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.
**************************************************************************************/
#include "StdAfx.h"
#include "Extern.h"
#include "StatusBar.h"
#include "MudWindow.h"
#include "TextInput.h"
CStatusBar::CStatusBar()
{
m_nFore = 14;
m_wFore = F_YELLOW;
m_nBack = 5;
m_wBack = B_MAGENTA;
m_wAttr = m_wFore | m_wBack;
m_bShowing = FALSE;
m_ptrList.SetSize(DEFAULT_BAR_ARRAY_SIZE,DEFAULT_BAR_GROW_SIZE);
m_nCount = 0;
m_nGetIndex = 0;
}
CStatusBar::~CStatusBar()
{
RemoveAll();
}
void CStatusBar::RemoveAll()
{
BAR_ITEM *pItem;
for (int i=0;i<m_nCount;i++)
{
pItem = (BAR_ITEM *)m_ptrList.GetAt(i);
delete pItem;
}
m_ptrList.RemoveAll();
m_nCount = 0;
m_nGetIndex = 0;
}
int CStatusBar::RemoveGroup(const char *pszGroup)
{
BAR_ITEM *pItem;
int nCount = 0;
for (int i=m_nCount-1;i>-1;i--)
{
pItem = (BAR_ITEM *)m_ptrList.GetAt(i);
if (pItem->strGroup == pszGroup)
{
Remove(i);
nCount++;
}
}
return(nCount);
}
int CStatusBar::DisableGroup(const char *pszGroup)
{
int nCount = 0;
BAR_ITEM *pItem = GetFirst();
while(pItem != NULL)
{
if (pItem->strGroup == pszGroup)
{
pItem->bEnabled = FALSE;
nCount++;
}
pItem = GetNext();
}
return(nCount);
}
int CStatusBar::EnableGroup(const char *pszGroup)
{
int nCount = 0;
BAR_ITEM *pItem = GetFirst();
while(pItem != NULL)
{
if (pItem->strGroup == pszGroup)
{
pItem->bEnabled = TRUE;
nCount++;
}
pItem = GetNext();
}
return(nCount);
}
void CStatusBar::SetBarFore(int nColor)
{
m_nFore = nColor;
m_wFore = ForeColorAttribute(nColor);
m_wAttr = m_wFore | m_wBack;
if (m_bShowing)
Redraw();
}
void CStatusBar::SetBarBack(int nColor)
{
m_nBack = nColor;
m_wBack = BackColorAttribute(nColor);
m_wAttr = m_wFore | m_wBack;
if (m_bShowing)
Redraw();
}
void CStatusBar::SetBarItemFore(BAR_ITEM *pItem, int nColor)
{
pItem->nFore = nColor;
WORD wFore = ForeColorAttribute(nColor);
WORD wBack = BackColorAttribute(pItem->nBack);
pItem->wColor = wFore | wBack;
DrawItem(pItem);
}
void CStatusBar::SetBarItemBack(BAR_ITEM *pItem, int nColor)
{
pItem->nBack = nColor;
WORD wFore = ForeColorAttribute(pItem->nFore);
WORD wBack = BackColorAttribute(nColor);
pItem->wColor = wFore | wBack;
DrawItem(pItem);
}
void CStatusBar::ShiftItems(int nStart, int nEnd, int nNum)
{
if (nStart < 0 || nStart >= m_nCount || nStart > nEnd)
return;
if (nEnd < 0 || nEnd >= m_nCount)
return;
int nNewPos;
BAR_ITEM *pItem;
for (int i=nStart;i<=nEnd;i++)
{
pItem = GetAt(i);
nNewPos = pItem->nPos + nNum;
if (nNewPos > 0 && nNewPos < 81)
pItem->nPos = nNewPos;
}
int j;
BAR_ITEM *pItemI, *pItemJ;
for (i=0;i<m_nCount;i++)
for (j=0;j<m_nCount-1;j++)
{
pItemI = GetAt(i);
pItemJ = GetAt(j);
if (pItemI->nPos < pItemJ->nPos)
{
m_ptrList.SetAt(i,pItemJ);
m_ptrList.SetAt(j,pItemI);
}
}
}
void CStatusBar::ShowBar(int nPos)
{
// If already showing and they want the same position
// they are ignorant fools.
if (m_bShowing && nPos == m_nPosition)
return;
// Must be trying to move the bar's position. Need to
// hide it first.
if (m_bShowing)
HideBar();
m_nPosition = nPos;
m_nLine = _terminal.GetWindowLines();
_terminal.Scroll();
_terminal.SetWindowLines(m_nLine-1);
if (nPos == STATUS_BAR_ABOVE)
{
m_nLine--;
Redraw();
}
else
{
Redraw();
_tiUserInput.Move(0,m_nLine-1,80);
}
_terminal.AdjustY();
_tiUserInput.SetFocus();
m_bShowing = TRUE;
}
void CStatusBar::HideBar()
{
_terminal.SetWindowLines(_terminal.GetWindowLines()+1);
if (m_nPosition == STATUS_BAR_BELOW)
{
_tiUserInput.Move(0,_terminal.GetWindowLines(),80);
m_screen.FillChar(0,m_nLine-1,80,' ',F_WHITE|B_BLACK);
}
else
m_screen.FillChar(0,m_nLine,80,' ',F_WHITE|B_BLACK);
_tiUserInput.SetFocus();
m_bShowing = FALSE;
}
void CStatusBar::DrawItem(BAR_ITEM *pItem)
{
CString strText(pItem->strText);
ReplaceVariables(strText);
if (strText.GetLength() > pItem->nLen)
strText = strText.Left(pItem->nLen);
COORD c;
c.X = pItem->nPos-1;
c.Y = m_nLine;
unsigned long lWritten;
WORD wAttr = (!pItem->wColor ? m_wAttr : pItem->wColor);
FillConsoleOutputAttribute(m_screen.m_hScreen,wAttr,
pItem->nLen,c,&lWritten);
m_screen.Gotoxy(pItem->nPos-1,m_nLine);
m_screen.SetColor(wAttr);
int nLen = strText.GetLength();
if (pItem->nPos + nLen > 80)
{
// Cannot use WriteConsole for the last character or it will
// scroll the window.
WriteConsole(m_screen.m_hScreen,(const char *)strText.Left(nLen-1),
nLen-1,&lWritten,NULL);
c.X = (pItem->nPos-1) + (nLen-1);
c.Y = m_nLine;
FillConsoleOutputCharacter(m_screen.m_hScreen,
strText.GetAt(nLen-1),1,c,&lWritten);
}
else
WriteConsole(m_screen.m_hScreen,(const char *)strText,
strText.GetLength(),&lWritten,NULL);
int nBlanks = pItem->nLen - nLen;
if (nBlanks)
{
c.X = (pItem->nPos-1) + nLen;
FillConsoleOutputCharacter(m_screen.m_hScreen,' ',nBlanks,c,
&lWritten);
}
}
void CStatusBar::Redraw()
{
_tiUserInput.HideCursor();
m_screen.FillChar(0,m_nLine,80,' ',m_wAttr);
BAR_ITEM *pItem = GetFirst();
while(pItem != NULL)
{
if (pItem->bEnabled)
DrawItem(pItem);
pItem = GetNext();
}
_tiUserInput.SetFocus();
_tiUserInput.ShowCursor();
}
BOOL CStatusBar::Remove(const char *pszItem)
{
BAR_ITEM *pItem;
for (int i=0;i<m_nCount;i++)
{
pItem = (BAR_ITEM *)m_ptrList.GetAt(i);
if (pItem->strItem == pszItem)
{
m_ptrList.RemoveAt(i);
m_nCount--;
delete pItem;
return(TRUE);
}
}
return(FALSE);
}
BOOL CStatusBar::Remove(int nIndex)
{
if (nIndex < 0 || nIndex >= m_nCount)
return(FALSE);
BAR_ITEM *pItem = (BAR_ITEM *)m_ptrList.GetAt(nIndex);
if (pItem == NULL)
return(FALSE);
m_ptrList.RemoveAt(nIndex);
m_nCount--;
delete pItem;
return(TRUE);
}
int CStatusBar::AddSeparator(const char *pszItem, int nPos,
const char *pszGroup)
{
int i;
BAR_ITEM *pItem = FindItem(pszItem);
if (pItem != NULL)
{
pItem->strItem = pszItem;
pItem->strText = '
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -