⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 guicellarrayctrl.cc

📁 五行MMORPG引擎系统V1.0
💻 CC
📖 第 1 页 / 共 2 页
字号:
//guiCellArrayCtrl.cc
/*/////////////////////////////////////////////////////////////////

   李亦 liease@163.com 4040719
	2006-7-8
/*/////////////////////////////////////////////////////////////////

#include "gui/controls/guiCellButtonCtrl.h"
#include "gui/containers/guiCellArrayCtrl.h"
#include "rpg/ui/gameIconMan.h"
//#include "rpg/gobjects/GGoodsArray.h"
#include "rpg/RPGSetting.h"
#include "gui/core/guiCanvas.h"


#define GGOODSINFO_NUM		3

using namespace RPG;



StringTableEntry GuiCellArrayControl::ms_sCellNamesLabel = NULL;

IMPLEMENT_CONOBJECT(GuiCellArrayControl);

GuiCellArrayControl::GuiCellArrayControl()
:m_cellSize(34,34)
,m_cellAreaSize(0,0)
{
	//m_bNeedCreateCells	= true;
	m_nCellAmount	= 0;
	m_nIconBase		= 0;
	m_pIconTexture	= 0;
	m_pCellProfile	= 0;
   m_nCols = 0;
   m_nRows = 0;
   mRowSpacing = 1;
   mColSpacing = 1;
	m_bCellBorder	= true;
	//m_bCanDragDrop	= true;
	m_iconTextureHandle	= NULL;
	m_pText					= StringTable->getBlank();
	m_pEndCallback			= StringTable->getBlank();
	m_pPrefName				= StringTable->getBlank();
	m_uDragMode				= CDT_DRAGDROP;

	m_pDataSource	= NULL;
	m_sSupplyKey	= "";
	m_pToolTipCtrl	= NULL;
	m_nToolTipStyle= TTS_TOOLTIP;
	//m_pToolTipGui	= StringTable->getBlank();
	m_pHilightCell		= NULL;
	m_dwHilightTimer	= 0;
	m_dwHilightState	= CHLS_NONE;
	m_dwHilightDelay	= 1000;
	m_dwHideDelay		= 300;
	m_dwHilightMode	= CHLM_NORMAL;
	m_pDragCell			= NULL;
	m_bMouseDown		= FALSE;
	m_pCursorCaret		= NULL;
}


///////////////////////////////////////////////////////////////
//高亮Cell模式
static EnumTable::Enums gs_HilightModeEnums[] =
{
	{		GuiCellArrayControl::CHLM_NORMAL,	"normal"},
	{		GuiCellArrayControl::CHLM_CONTINUE,	"continue"}
};
static EnumTable gs_HilightModeEnumTable(GuiCellArrayControl::CHLM_AMOUNT, &gs_HilightModeEnums[0]);

static EnumTable::Enums gs_ToolTipStyleEnums[] =
{
	{		GuiCellArrayControl::TTS_NONE,	"none"},
	{		GuiCellArrayControl::TTS_TOOLTIP,	"tooltip"},
	{		GuiCellArrayControl::TTS_TEXTTIP,	"texttip"}
};
static EnumTable gs_ToolTipStyleEnumTable(GuiCellArrayControl::TTS_AMOUNT, &gs_ToolTipStyleEnums[0]);

///////////////////////////////////////////////////////////////
//拖曳模式
static EnumTable::Enums gs_DragEnums[] =
{
	{		GuiCellArrayControl::CDT_NONE,			"none"},

	{		GuiCellArrayControl::CDT_DRAG,			"drag"},
	{		GuiCellArrayControl::CDT_DRAGDROP,		"dragDrop"},
	{		GuiCellArrayControl::CDT_DRAGSELFDROP,	"dragSelfDrop"},

	{		GuiCellArrayControl::CDT_DRAGMOVE,		"move"},
	{		GuiCellArrayControl::CDT_MOVEDROP,		"moveDrop"},
	{		GuiCellArrayControl::CDT_MOVESELFDROP,	"moveSelfDrop"},

	{		GuiCellArrayControl::CDT_DRAGSELFMOVE,			"selfMove"},
	{		GuiCellArrayControl::CDT_SELFMOVEDROP,			"selfMoveDrop"},
	{		GuiCellArrayControl::CDT_SELFMOVESELFDROP,	"selfMoveSelfDrop"},

	{		GuiCellArrayControl::CDT_DROP,			"drop"},
	{		GuiCellArrayControl::CDT_SELFDROP,		"selfDrop"},




};
static EnumTable gs_cellDragModeEnums(6, &gs_DragEnums[0]);

///////////////////////////////////////////////////////////////
//文本排列模式
static EnumTable::Enums gs_alignEnums[] =
{
	{		GuiCellArrayControl::ALIGN_NONE,		"none"},
	{		GuiCellArrayControl::ALIGN_LEFT,		"left"},
	{		GuiCellArrayControl::ALIGN_RIGHT,	"right"},
	{		GuiCellArrayControl::ALIGN_TOP,		"top"},
	{		GuiCellArrayControl::ALIGN_BOTTOM,	"bottom"}
};

static EnumTable gs_cellAlignEnumTable(5, &gs_alignEnums[0]);



void GuiCellArrayControl::initPersistFields()
{
	ms_sCellNamesLabel = StringTable->insert("cellNames");

  Parent::initPersistFields();

  addField("tooltipGuiStyle", TypeEnum,			Offset(m_nToolTipStyle,		GuiCellArrayControl),1,&gs_ToolTipStyleEnumTable);
  addField("dragMode",			TypeEnum,			Offset(m_uDragMode,			GuiCellArrayControl),1,&gs_cellDragModeEnums);
  addField("cellAlign",    TypeEnum,			Offset(m_uCellAlign,		GuiCellArrayControl),1,&gs_cellAlignEnumTable);
  addField("text",			TypeString,			Offset(m_pText,		GuiCellArrayControl));
  addField("cellBlanks",	TypeStringVector,	Offset(m_arCellBlanks,		GuiCellArrayControl));
  addField("cellCursors",	TypeStringVector,	Offset(m_arCellCursors,		GuiCellArrayControl));
  addField("cellAccels",	TypeStringVector,	Offset(m_arCellAccels,		GuiCellArrayControl));
  addField("iconBase",	   TypeS32,				Offset(m_nIconBase,		GuiCellArrayControl));
  addField("rowSpacing",   TypeS32,				Offset(mRowSpacing,		GuiCellArrayControl));
  addField("colSpacing",   TypeS32,				Offset(mColSpacing,		GuiCellArrayControl));
  addField("cols",			TypeS32,				Offset(m_nCols,			GuiCellArrayControl));
  addField("cellAmount",	TypeS32,				Offset(m_nCellAmount,	GuiCellArrayControl));
  addField("cellSize",	   TypePoint2I,		Offset(m_cellSize,		GuiCellArrayControl));
  addField("cellBorder",	TypeBool,			Offset(m_bCellBorder,	GuiCellArrayControl));
  addField("cellProfile",	TypeGuiProfile,	Offset(m_pCellProfile,  GuiCellArrayControl));
  addField("iconTexture",	TypeFilename,		Offset(m_pIconTexture,	GuiCellArrayControl));
  addField("toolTipGui",	TypeGuiControlPtr,Offset(m_pToolTipCtrl,		GuiCellArrayControl));
  //addField("toolTipGui",	TypeString,			Offset(m_pToolTipGui,		GuiCellArrayControl));

  addField("hilightMode",			TypeEnum,			Offset(m_dwHilightMode,		GuiCellArrayControl),1,&gs_HilightModeEnumTable);
  addField("hilightDelay",			TypeS32,				Offset(m_dwHilightDelay,	GuiCellArrayControl));
  addField("hideDelay",				TypeS32,				Offset(m_dwHideDelay,		GuiCellArrayControl));

  //addField("isDataSupply",			TypeBool,			Offset(m_sSupplyKey,			GuiCellArrayControl));
  addField("supplyKey",				TypeString,			Offset(m_sSupplyKey,			GuiCellArrayControl));
  addField("prefName",				TypeString,			Offset(m_pPrefName,			GuiCellArrayControl));
  addField("endActionCallback",	TypeString,			Offset(m_pEndCallback,		GuiCellArrayControl));
  addField(ms_sCellNamesLabel,	TypeStringVector,	Offset(m_arCellNames,		GuiCellArrayControl));

}


void GuiCellArrayControl::updateGameIcon(U32 nIndex)
{
	if(nIndex >= m_nCellAmount)
		return;

	if(m_pDataSource)
	{
		m_arCellNames[nIndex] = m_pDataSource->GetIconLabel(nIndex + m_uBaseIndex);
	}
	if(m_arCellBtns[nIndex])
		m_arCellBtns[nIndex]->setGameIcon(m_arCellNames[nIndex],true);
}

void GuiCellArrayControl::updateGameIcons()
{
	U32 n;

	for(n=0; n<m_nCellAmount; n++)
	{
		updateGameIcon(n);
	}//for
}


void GuiCellArrayControl::onCellAction(GuiCellButtonCtrl* pCell,bool bRight)
{
	AssertWarn(pCell, "nCellIndex小于0,肯定调用出错了");
	if(pCell == 0)
		return;

	S32 nCellIndex = pCell-> m_nCellIndex;
	if(nCellIndex < 0)
		return;
	
	StringTableEntry	pName;
	StringTableEntry	pFuncName;
	CSTR					pRet;

	if(nCellIndex < m_arCellNames.size() &&  m_arCellNames[nCellIndex][0])
		pName	= m_arCellNames[nCellIndex];
	else
		pName	= StringTable->getBlank();

	if(pName[0] == 0)
		return;

	//数据源回调处理
	if(m_pDataSource)
	{
		GGoodsArray::GGoodsInfo	goodsInfo[GGOODSINFO_NUM];
		U32							nNum;
		
		nNum = m_pDataSource->OnGoodsApply(nCellIndex + m_uBaseIndex, goodsInfo);
		if(processGoodsInfos(goodsInfo, nNum))
			goto laEnd;
	}

	/////////////////////////////////////////////////////////////
	//m_arCallbacks型回调 
	CSTR pCallback = SimObject::getDataField("m_arCallbacks",pName );
	if(pCallback[0])
	{
		Con::evaluate(pCallback);
		goto laEnd;
	}

	/////////////////////////////////////////////////////////////
	//m_arCallbacks 索引型回调 
	pCallback = SimObject::getDataField("m_arCallbacks", avar("%d",nCellIndex));
	if(pCallback[0])
	{
		Con::evaluate(pCallback);
		goto laEnd;
	}


	StringTableEntry argv[5];

	/////////////////////////////////////////////////////////////
	//函数型回调 
	pFuncName = StringTable->insert(avar("onAction_%s",pName));
	if(Con::checkScriptMethod(this, pFuncName))
	{
		argv[0]	= pFuncName;
		argv[2]	=  Con::getIntArg(bRight?1:0);
		Con::execute(this,3, argv);
	}
	/////////////////////////////////////////////////////////////
	//公共回调 
	else
	{
		argv[0]	= "onCellAction";
		argv[2]	= Con::getIntArg(nCellIndex);
		argv[3]	= pName;
		argv[4]	=  Con::getIntArg(bRight?1:0);

		Con::execute(this,5, argv);
	}


	//
laEnd:
	if(m_pEndCallback && m_pEndCallback[0])
		Con::evaluate(m_pEndCallback);

}


void GuiCellArrayControl::ToggleVH(bool bToVerticalBar,GuiControl *pDispatch)
{
	if(bToVerticalBar)
	{
		if(m_uCellAlign == ALIGN_LEFT)
		{	
			m_nCols	= m_cellNumBak.y;
			m_uCellAlign = ALIGN_TOP;
		}
		else if(m_uCellAlign == ALIGN_RIGHT)
		{
			m_nCols	= m_cellNumBak.y;
			m_uCellAlign = ALIGN_BOTTOM;
		}
		
	}
	else
	{
		if(m_uCellAlign == ALIGN_TOP)
		{
			m_nCols	= m_cellNumBak.x;
			m_uCellAlign = ALIGN_LEFT;
		}
		else if(m_uCellAlign == ALIGN_BOTTOM)
		{
			m_nCols	= m_cellNumBak.x;
			m_uCellAlign = ALIGN_RIGHT;
		}
	}
	
	Parent::ToggleVH(bToVerticalBar,pDispatch);
	updateCells();
}



void GuiCellArrayControl::onMouseDown(const GuiEvent &event)
{
	//mouseLock();
	m_bMouseDown = TRUE;
	//mouseLock();
	hideToolTip();
	m_pCursorCaret = NULL;
	m_pCursorCaret = NULL;
}
void GuiCellArrayControl::onRightMouseDown(const GuiEvent &)
{
	toggleShowToolTip();
	toggleHilightMode();
}

void GuiCellArrayControl::onMouseUp(const GuiEvent &event)
{
	//mouseUnlock();
	if(m_pCursorCaret)
	{
		Canvas->clearDragCursor();
		m_pCursorCaret = NULL;
	}
	m_bMouseDown = FALSE;
	//mouseLock();
	hideToolTip();
	m_pDragCell		= NULL;
}


bool GuiCellArrayControl::onAdd()
{
	if(!Parent::onAdd())
		return false;
	loadPrefData();
	return true;
}

void GuiCellArrayControl::onRemove()
{
	savePrefData();
	Parent::onRemove();
}



bool GuiCellArrayControl::onWake()
{
	if(m_nCellAmount <= 0)
	{	
		AssertWarn(0,"总数须大过0,现自动设为1");
		m_nCellAmount	= 1;
	}

   if ( !Parent::onWake() )
      return false;


	if(m_pIconTexture && m_pIconTexture[0])
	{	
		m_iconTextureHandle = TextureHandle(m_pIconTexture,BitmapTexture);
		if(bool(m_iconTextureHandle))
			m_uDragMode = CDT_NONE;
			//m_bCanDragDrop = false;
	}
	
	//m_pPrefName
	//loadPrefData();
	if(!m_arCellBtns.size()/* || m_bNeedCreateCells*/)
		createCells();

   return true;
}


void GuiCellArrayControl::onSleep()
{
	//savePrefData();
   Parent::onSleep();
}


void GuiCellArrayControl::attachDataSource(RPG::GGoodsArray*	pDatSrc, U32 uBase, U32 uAmount)
{
	m_pDataSource	= pDatSrc;
	m_uBaseIndex	= uBase;
	m_nCellAmount	= uAmount;

	destroyCells();
	loadSourceData();
	createCells();
}	

void GuiCellArrayControl::loadSourceData()
{
	if(m_pDataSource == NULL)
		return;

	U32 n;
	RPG::GGoods*	pGGoods;

	//m_nCellAmount = m_pDataSource->size();
	m_arCellNames.setSize(m_nCellAmount);

	for(n=0; n<m_nCellAmount; n++)
	{
		pGGoods = (*m_pDataSource)[m_uBaseIndex + n];
		if(pGGoods)
		{
			m_arCellNames[n] = pGGoods->GetIconLabel();
		}
		else
		{
			m_arCellNames[n] = StringTable->getBlank();
		}
	}//for

}


void GuiCellArrayControl::updateHilightCell()
{
	if(m_dwHilightState == CHLS_NONE || m_pToolTipCtrl == NULL)
		return;

	U32 dwTimer;

	switch(m_dwHilightState)
	{

	case CHLS_LEAVE:
		dwTimer = Platform::getRealMilliseconds();
		if(dwTimer - m_dwHilightTimer > m_dwHideDelay)
		{
			m_dwHilightTimer = dwTimer;
			hideToolTip();
		}
		break;

	case CHLS_ENTER:
	case CHLS_ENTERSHOWING:
		dwTimer = Platform::getRealMilliseconds();
		if(m_dwHilightState == CHLS_ENTERSHOWING || dwTimer - m_dwHilightTimer > m_dwHilightDelay )
		{
			m_dwHilightTimer = dwTimer;
			if(!showToolTip())
				break;
			//继续执行 case CHLS_SHOWING 分支
		}
		else
			break;

	case CHLS_SHOWING:
		{
			if(m_nToolTipStyle == TTS_TOOLTIP)
			{
				Point2I pt = Canvas->getCursorPos() + Canvas->getCursorExtent();
				if(pt.x + m_pToolTipCtrl->getWidth() > Canvas->getWidth())
					pt.x -= m_pToolTipCtrl->getWidth();
				if(pt.y + m_pToolTipCtrl->getHeight() > Canvas->getHeight())
					pt.y -= m_pToolTipCtrl->getHeight();
				m_pToolTipCtrl->setPosition(pt);
			}

			if(m_dwHilightMode != CHLM_CONTINUE)
				m_dwHilightState = CHLS_SHOWED;	//继续执行 case CHLS_SHOWED
			else
				break;
		}

	case CHLS_SHOWED:
		break;

	}//switch
}


void GuiCellArrayControl::toggleHilightMode()
{
	m_dwHilightMode++;
	if(m_dwHilightMode >= CHLM_AMOUNT)
		m_dwHilightMode = CHLM_NORMAL;
	updateHilightCell();
}


bool GuiCellArrayControl::toggleShowToolTip()
{
	if(!isHilightShow())
	{
		m_dwHilightState = CHLS_SHOWING;
		showToolTip();
	}
	else
	{
		m_dwHilightState = CHLS_NONE;
		hideToolTip();
	}
	return isHilightShow();
}


void GuiCellArrayControl::hideToolTip()
{
	if(m_pToolTipCtrl)
		Con::executef(m_pToolTipCtrl,2, "HideGoodsInfo");
	m_dwHilightState = CHLS_NONE;
}



bool GuiCellArrayControl::showToolTip()
{
	if(!m_pHilightCell)
	{
		m_dwHilightState = CHLS_NONE;
		return false;
	}

	char* szID;

	if(m_pDataSource)
	{
		szID = Con::getArgBuffer(32);
		szID[0] = 0;
		RPG::GGoods*  pGGoods = (*m_pDataSource)[m_pHilightCell->m_nCellIndex];
		GGoods::ToggleGoods(pGGoods);
		if(pGGoods)
			dSprintf(szID, 32, "%d", pGGoods->getId());
	}
	else
	{
		szID = (char*)m_arCellNames[m_pHilightCell->m_nCellIndex];
	}
	Con::executef(m_pToolTipCtrl,4, "ShowGoodsInfo", Con::getIntArg(m_pHilightCell->m_nCellIndex), szID);
	m_dwHilightState = CHLS_SHOWING;
	return true;
}


void GuiCellArrayControl::readyDragCell(GuiCellButtonCtrl* pCell)
{
	m_pDragCell = pCell;
}

void GuiCellArrayControl::beginDragCell(GuiCellButtonCtrl* pCell)
{
	if(!m_pDragCell || !isCellDraging(pCell))
		return;
	if(!isMouseDown())
		return;

	m_pCursorCaret = Canvas->createDragCursor(pCell);

}


bool GuiCellArrayControl::showToolTip(GuiCellButtonCtrl* pCell)
{
	if(m_pToolTipCtrl == NULL)
	{
		return false;
	}

	m_pHilightCell		= pCell;
	m_dwHilightTimer	= Platform::getRealMilliseconds();
	if(pCell == NULL)
	{
		if(m_dwHilightMode == CHLM_CONTINUE)
		{
			m_dwHilightState	= CHLS_LEAVE;
		}
		else
		{
			hideToolTip();
		}
	}
	else if(m_arCellNames[pCell->m_nCellIndex] && m_arCellNames[pCell->m_nCellIndex][0])
	{
		if(m_dwHilightMode == CHLM_CONTINUE)
		{
			if(m_dwHilightState	== CHLS_LEAVE)
				m_dwHilightState	= CHLS_ENTERSHOWING;
			else if(m_dwHilightState	!= CHLS_SHOWING)
				m_dwHilightState	= CHLS_ENTER;
		}
		else
		{
			m_dwHilightState	= CHLS_ENTER;
		}
	}
	return true;
}



void GuiCellArrayControl::loadPrefData()
{
	if(m_pPrefName == NULL || m_pPrefName[0] == 0)
		return;

	char	szPref[256];
	CSTR	pData;

	dSprintf(szPref, 256, "$pref::GUICellArray::%s",  
		(m_pPrefName[0] == '.')? getName() : m_pPrefName );
	pData = Con::getVariable(szPref);
	if(pData && pData[0])
	{
		setDataField(ms_sCellNamesLabel,NULL,pData);
	}

}


void GuiCellArrayControl::savePrefData()
{
	if(m_pPrefName == NULL || m_pPrefName[0] == 0)
		return;

	char	szPref[256];
	CSTR	pData;

	dSprintf(szPref, 256, "$pref::GUICellArray::%s",  
		(m_pPrefName[0] == '.')? getName() : m_pPrefName );
	pData = getDataField(ms_sCellNamesLabel,NULL);
	Con::setVariable(szPref,pData);
}


void GuiCellArrayControl::inspectPreApply()
{
	Parent::inspectPreApply();
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -