加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
TalkToNpcDlg.cpp 8.89 KB
一键复制 编辑 原始数据 按行查看 历史
Shaker 提交于 2014-08-12 21:17 . *提交到Git@OSC
// TalkToNpcDlg.cpp : 实现文件
//
#include "stdafx.h"
#include "TurboMir.h"
#include "talktonpcdlg.h"
#include "mainfrm.h"
#include ".\talktonpcdlg.h"
// CTalkToNpcDlg 对话框
IMPLEMENT_DYNAMIC(CTalkToNpcDlg, CDialogBase)
CTalkToNpcDlg::CTalkToNpcDlg( CGameMir& Game, CWnd* pParent /*=NULL*/)
: CDialogBase(CTalkToNpcDlg::IDD, pParent)
, m_NpcId(0)
, m_NpcName(_T(""))
, m_LastBegin(-1)
, m_LastEnd(-1)
, m_Game(Game)
, m_bNpcTextOK(false)
, m_bItemListOK(false)
, m_bItemDetailOK(false)
{
}
CTalkToNpcDlg::~CTalkToNpcDlg()
{
}
void CTalkToNpcDlg::DoDataExchange(CDataExchange* pDX)
{
CDialogBase::DoDataExchange(pDX);
DDX_Control(pDX, IDC_RICHEDIT21, m_TalkView);
DDX_Control(pDX, IDC_ITEM_LIST, m_ItemList);
}
BEGIN_MESSAGE_MAP(CTalkToNpcDlg, CDialogBase)
ON_NOTIFY(EN_MSGFILTER, IDC_RICHEDIT21, OnEnMsgfilterRichedit21)
ON_EN_SETFOCUS(IDC_RICHEDIT21, OnEnSetfocusRichedit21)
ON_NOTIFY(HDN_BEGINTRACK, 0, OnHdnBegintrackItemList)
ON_NOTIFY(HDN_DIVIDERDBLCLICK, 0, OnHdnDividerdblclickItemList)
ON_NOTIFY(NM_DBLCLK, IDC_ITEM_LIST, OnNMDblclkItemList)
END_MESSAGE_MAP()
// CTalkToNpcDlg 消息处理程序
BOOL CTalkToNpcDlg::OnInitDialog()
{
CDialogBase::OnInitDialog();
m_Font.CreateStockObject(DEFAULT_GUI_FONT); //得到字体信息
m_ItemList.InsertColumn(0,"名称",LVCFMT_LEFT,100);
m_ItemList.InsertColumn(1,"价格",LVCFMT_LEFT,50);
m_ItemList.InsertColumn(3,"数量",LVCFMT_LEFT,50);
m_ItemList.InsertColumn(4,"详细",LVCFMT_LEFT,170);
m_ItemList.SetExtendedStyle(LVS_EX_FULLROWSELECT|m_ItemList.GetExtendedStyle());
m_ItemList.SetBkColor(RGB(0,0,0));
m_ItemList.SetTextBkColor(RGB(0,0,0));
m_ItemList.SetTextColor(crMessage);
m_TalkView.SetBackgroundColor(FALSE,RGB(0,0,0));
m_TalkView.SetFont(&m_Font);
CHARFORMAT cf;
memset(&cf, 0, sizeof(CHARFORMAT));
m_TalkView.GetDefaultCharFormat(cf);
cf.dwEffects = cf.dwEffects ^ CFE_AUTOCOLOR;
cf.crTextColor = RGB(255,255,255);
m_TalkView.SetDefaultCharFormat(cf);
m_TalkView.SetEventMask(m_TalkView.GetEventMask()|ENM_LINK|ENM_MOUSEEVENTS);
m_Game.Merchant.OnMerchantSay=boost::bind(&CTalkToNpcDlg::SetTalkText,boost::ref(*this),_1);
m_Game.Merchant.OnClose=boost::bind(&CTalkToNpcDlg::CloseTalk,boost::ref(*this));
m_Game.Merchant.OnSetItemList=boost::bind(&CTalkToNpcDlg::SetItemList,boost::ref(*this),_1);
return TRUE; // return TRUE unless you set the focus to a control
// 异常: OCX 属性页应返回 FALSE
}
void CTalkToNpcDlg::SetTalkText(LPCTSTR TalkText)
{
m_LastBegin=-1;
std::string body;
m_NpcName.clear();
body=TalkText;
// 分割NPC名字
size_t iFind=body.find('/');
if(iFind!=std::string::npos)
{
m_NpcName=body.substr(0,iFind);
body=body.substr(iFind+1);
}
if (m_NpcName.empty())
{
return;
}
SetWindowText(m_NpcName.c_str());
// 设置换行
iFind=body.find('\\');
while(iFind!=std::string::npos)
{
if (body[iFind+1]!='\\')
{
body=body.substr(0,iFind)+"\n"+body.substr(iFind+1);
}
else
{
body=body.substr(0,iFind)+body.substr(iFind+1);
}
iFind=body.find('\\');
}
CHARFORMAT2 cf;
cf.cbSize=sizeof(cf);
m_TalkView.SetWindowText("");
m_CmdList.clear();
long nLen;
const boost::regex reCmd("^(.*?)<(.+?)>(.*)$",boost::regbase::normal|boost::regbase::icase);
boost::smatch match;
while ( boost::regex_match( body, match, reCmd) ) // 循环匹配命令
{
nLen=m_TalkView.GetTextLength();
m_TalkView.GetDefaultCharFormat(cf);
m_TalkView.SetSel(nLen,nLen);
m_TalkView.SetSelectionCharFormat(cf);
m_TalkView.ReplaceSel(match.str(1).c_str());
std::string strCmd=match.str(2);
size_t iFind=strCmd.find('/');
if(iFind!=std::string::npos)
{
CMD_ITEM cmd;
cmd.cmd=strCmd.substr(iFind+1);
strCmd=strCmd.substr(0,iFind);
m_TalkView.GetDefaultCharFormat(cf);
cf.crTextColor = RGB(255,255,0);
cf.dwEffects = ( cf.dwEffects | CFE_AUTOCOLOR /*| CFE_AUTOBACKCOLOR*/ ) ^ ( CFE_AUTOCOLOR /*| CFE_AUTOBACKCOLOR*/ );
cf.dwEffects |= CFE_UNDERLINE;
cf.dwMask |= CFM_UNDERLINE | CFM_COLOR;
nLen=m_TalkView.GetTextLength();
m_TalkView.SetSel(nLen,nLen);
m_TalkView.GetSel(cmd.begin,cmd.begin);
m_TalkView.SetSelectionCharFormat(cf);
m_TalkView.ReplaceSel(strCmd.c_str());
nLen=m_TalkView.GetTextLength();
cmd.end=nLen;
nLen=m_TalkView.GetTextLength();
m_TalkView.SetSel(nLen,nLen);
m_TalkView.GetSel(cmd.end,cmd.end);
m_CmdList.push_back(cmd);
}
else
{
nLen=m_TalkView.GetTextLength();
m_TalkView.GetDefaultCharFormat(cf);
cf.crTextColor = RGB(255,0,0);
cf.dwEffects = ( cf.dwEffects | CFE_AUTOCOLOR /*| CFE_AUTOBACKCOLOR*/ ) ^ ( CFE_AUTOCOLOR /*| CFE_AUTOBACKCOLOR*/ );
//cf.dwEffects |= CFE_UNDERLINE;
cf.dwMask |= /*CFM_UNDERLINE |*/ CFM_COLOR;
m_TalkView.SetSel(nLen,nLen);
m_TalkView.SetSelectionCharFormat(cf);
m_TalkView.ReplaceSel(strCmd.c_str());
}
body=match.str(3);
}
if ( !body.empty() )
{
nLen=m_TalkView.GetTextLength();
m_TalkView.GetDefaultCharFormat(cf);
m_TalkView.SetSel(nLen,nLen);
m_TalkView.SetSelectionCharFormat(cf);
m_TalkView.ReplaceSel(body.c_str());
}
ResizeWindow(false);
}
void CTalkToNpcDlg::OnEnMsgfilterRichedit21(NMHDR *pNMHDR, LRESULT *pResult)
{
MSGFILTER *pMsgFilter = reinterpret_cast<MSGFILTER *>(pNMHDR);
if( pMsgFilter->msg==WM_LBUTTONDOWN || pMsgFilter->msg==WM_MOUSEMOVE )
{
WORD x=LOWORD(pMsgFilter->lParam);
WORD y=HIWORD(pMsgFilter->lParam);
CPoint point(x,y);
CString str;
int index=m_TalkView.CharFromPos(point);
bool flag=false;
for(std::vector<CMD_ITEM>::iterator pos=m_CmdList.begin();pos!=m_CmdList.end();pos++)
{
if(index>=pos->begin && index<pos->end)
{
if(pMsgFilter->msg==WM_LBUTTONDOWN)
{
m_Game.MerchantDialogSelect(pos->cmd.c_str());
}
else
{
flag=true;
if(pos->begin==m_LastBegin && pos->end==m_LastEnd)
{
}
else
{
if(m_LastBegin!=-1)
{
CHARFORMAT2 cf2;
cf2.cbSize=sizeof(cf2);
m_TalkView.GetDefaultCharFormat(cf2);
cf2.crTextColor = RGB(255,255,0);
cf2.dwEffects = ( cf2.dwEffects | CFE_AUTOCOLOR ) ^ CFE_AUTOCOLOR;
cf2.dwEffects |= CFE_UNDERLINE;
cf2.dwMask |= CFM_UNDERLINE | CFM_COLOR;
m_TalkView.SetSel(m_LastBegin,m_LastEnd);
m_TalkView.SetSelectionCharFormat(cf2);
m_LastBegin=-1;
m_LastEnd=-1;
}
CHARFORMAT2 cf2;
cf2.cbSize=sizeof(cf2);
m_TalkView.GetDefaultCharFormat(cf2);
cf2.crTextColor = RGB(255,0,0);
cf2.dwEffects = ( cf2.dwEffects | CFE_AUTOCOLOR ) ^ CFE_AUTOCOLOR;
cf2.dwEffects |= CFE_UNDERLINE;
cf2.dwMask |= CFM_UNDERLINE | CFM_COLOR;
m_TalkView.SetSel(pos->begin,pos->end);
m_TalkView.SetSelectionCharFormat(cf2);
m_LastBegin=pos->begin;
m_LastEnd=pos->end;
}
}
break;
}
}
if(!flag)
{
if(m_LastBegin!=-1)
{
CHARFORMAT2 cf2;
cf2.cbSize=sizeof(cf2);
m_TalkView.GetDefaultCharFormat(cf2);
cf2.crTextColor = RGB(255,255,0);
cf2.dwEffects = ( cf2.dwEffects | CFE_AUTOCOLOR ) ^ CFE_AUTOCOLOR;
cf2.dwEffects |= CFE_UNDERLINE;
cf2.dwMask |= CFM_UNDERLINE | CFM_COLOR;
m_TalkView.SetSel(m_LastBegin,m_LastEnd);
m_TalkView.SetSelectionCharFormat(cf2);
m_LastBegin=-1;
m_LastEnd=-1;
}
}
}
*pResult = 0;
}
// 关闭对话
void CTalkToNpcDlg::CloseTalk(void)
{
ShowWindow(SW_HIDE);
}
void CTalkToNpcDlg::OnEnSetfocusRichedit21()
{
m_TalkView.SendMessage(WM_KILLFOCUS,0,0);
}
void CTalkToNpcDlg::ResizeWindow(bool bShowItemList)
{
CRect clnt,win;
GetClientRect(clnt);
GetWindowRect(win);
int rx=win.Width()-clnt.Width();
int ry=win.Height()-clnt.Height();
if(bShowItemList)
{
MoveWindow(win.left,win.top,400+rx,330+ry);
}
else
{
MoveWindow(win.left,win.top,400+rx,180+ry);
}
m_TalkView.MoveWindow(0,0,400,180);
m_ItemList.MoveWindow(0,180,400,150);
ShowWindow(SW_SHOW);
}
void CTalkToNpcDlg::SetItemList(std::vector<CGameMir::sMerchantItem>&Items)
{
m_ItemList.DeleteAllItems();
int idx;
for(std::vector<CGameMir::sMerchantItem>::iterator pos=Items.begin();pos!=Items.end();pos++)
{
idx=m_ItemList.InsertItem(idx,pos->strName.c_str());
if(pos->bNeedDetial)
m_ItemList.SetItemText(idx,3,"进一步选择>>");
else
m_ItemList.SetItemText(idx,3,"");
m_ItemList.SetItemText(idx,1,boost::lexical_cast<std::string>(pos->iPrice).c_str());
m_ItemList.SetItemText(idx,2,boost::lexical_cast<std::string>(pos->iCount).c_str());
m_ItemList.SetItemData(idx,pos->ciItem.MakeIndex);
}
ResizeWindow(true);
}
void CTalkToNpcDlg::OnHdnBegintrackItemList(NMHDR* /*pNMHDR*/, LRESULT *pResult)
{
*pResult = TRUE;
}
void CTalkToNpcDlg::OnHdnDividerdblclickItemList(NMHDR*, LRESULT*)
{
const int w[]={100,50,50,170};
for(int i=0;i<4;i++)
{
m_ItemList.SetColumnWidth(i,w[i]);
}
}
void CTalkToNpcDlg::OnNMDblclkItemList(NMHDR *pNMHDR, LRESULT *pResult)
{
POSITION pos=m_ItemList.GetFirstSelectedItemPosition();
if(pos!=NULL)
{
int idx=m_ItemList.GetNextSelectedItem(pos);
CString ItemName,ItemCount;
ItemName=m_ItemList.GetItemText(idx,0);
DWORD ItemId=m_ItemList.GetItemData(idx);
m_Game.BuyItem(ItemName,ItemId);
}
*pResult = 0;
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化