#include "ManagerSystem.h"
UiSystem::UiSystem(RenderSystem* renderSystem,InputSystem* inputSystem)
{
m_inputSystem = inputSystem;
InitResource(renderSystem);
}
UiSystem::~UiSystem(VOID)
{
for (MButton::iterator iter = m_button.begin(); iter != m_button.end();iter++)
{
DeleteObj(iter->second);
}
for (std::vector<Texture*>::iterator iter = m_texture.begin(); iter != m_texture.end();iter++)
{
DeleteObj(*iter);
}
}
BOOL UiSystem::InitResource(RenderSystem* renderSystem)
{
Texture* ploading = new Texture(renderSystem,"UI//Loading.bmp");
assert(ploading);
m_texture.push_back(ploading);
Sprite* spriteLoading = new Sprite(renderSystem,ploading);
spriteLoading->SetTextureInfo(0,0,0,0,800,600);
AddButton(0,spriteLoading);
Texture* uiTexture = new Texture(renderSystem,"UI//UI.tga");
assert(uiTexture);
m_texture.push_back(uiTexture);
for (UINT i=0,x=0,y=60,z=350;i<4;i++)
{
Button* uiButton = new Button(renderSystem,uiTexture);
uiButton->SetTextureInfo(0,x,y,z,138,36);
uiButton->SetRect(y,z,138,36);
AddButton(i+1,uiButton);
x += 72;
y += 100;
z += 45;
}
return TRUE;
}
VOID UiSystem::AddButton(UINT ID,Sprite* button)
{
m_button[ID] = button;
}
Sprite* UiSystem::GetButton(UINT ID)
{
return m_button[ID];
}
UINT UiSystem::GetButtonID(VOID)
{
return m_id;
}
BOOL UiSystem::Update(VOID)
{
POINT point = m_inputSystem->GetMousePos();
for (MButton::iterator iter= m_button.begin(); iter != m_button.end();iter++)
{
if(((Button*)iter->second)->TestButton(point) && m_inputSystem->IsMouseLeft())
{
switch(iter->first)
{
case FIRST:
m_id = FIRST;
break;
case SECOND:
m_id = SECOND;
break;
case THREE:
m_id = THREE;
break;
case EXIT:
exit(0);
break;
}
}
}
return TRUE;
}
BOOL UiSystem::Render(VOID)
{
POINT point = m_inputSystem->GetMousePos();
for (MButton::iterator iter= m_button.begin();iter != m_button.end();iter++)
{
POINT textureSrcPos = ((Button*)iter->second)->GetSrcPos();
UINT tempVal = textureSrcPos.y % 72;
if (((Button*)iter->second)->TestButton(point))
{
if (tempVal == 0)
{
iter->second->SetSrcPos(textureSrcPos.x,textureSrcPos.y + 36);
}
SetCursor(LoadCursor(m_inputSystem->GetInstance(),LPCSTR(IDC_CURSOR2)));
}
else
{
if (tempVal != 0)
{
iter->second->SetSrcPos(textureSrcPos.x,textureSrcPos.y - 36);
}
}
iter->second->Render(iter->second->GetTextureInfo());
}
return TRUE;
}
//**********************SceneSystem类******************
UINT SceneSystem::m_num = 0;
UINT SceneSystem::m_oldNum = 0;
SceneSystem::SceneSystem(InputSystem* inputSystem,HWND hwnd,
UINT length,UINT row,UINT col,UINT cellWidth,UINT cellHeight)
{
m_inputSystem = inputSystem;
m_length = length;
m_row = row;
m_col = col;
m_cellWidth = cellWidth;
m_cellHeight = cellHeight;
m_hwnd = hwnd;
}
SceneSystem::~SceneSystem(VOID)
{
}
std::vector<UINT> SceneSystem::Random(UINT length)
{
std::vector<UINT> tempV;
for (UINT i= 0,j= 0; i< length; i++)
{
if (i%4 == 0)
{
j++;
}
tempV.push_back(j);
}
srand((unsigned)time(NULL));
for (UINT i= 0; i< length; i++)
{
UINT x = rand()%length;
UINT y = rand()%length;
UINT temp = tempV[x];
tempV[x] = tempV[y];
tempV[y] = temp;
}
return tempV;
}
BOOL SceneSystem::InitResource(RenderSystem* renderSystem,std::string fileBackName,
std::string mainName,UINT step)
{
std::vector<UINT> tempArray = Random((m_row - 2) * (m_col-2));
UINT num = 0;
Texture* backTexture = new Texture(renderSystem,fileBackName);
assert(backTexture);
m_texture.push_back(backTexture);
m_gameBack = new Button(renderSystem,backTexture);
assert(m_gameBack);
m_gameBack->SetTextureInfo(0,0,0,0,800,600);
m_gameBack->SetFlag(1);
Texture* mainTexture = new Texture(renderSystem,mainName);
assert(mainTexture);
for (UINT j=0 ;j< m_row; j++)
{
for (UINT i=0 ;i< m_col; i++)
{
Button* tempButton = new Button(renderSystem,mainTexture,step);
tempButton->SetTextureInfo(0,0,
(LONG)GetScreenStart().x+m_cellWidth*i,(LONG)GetScreenStart().y+m_cellHeight*j,
m_cellWidth,m_cellHeight);
tempButton->SetRect((LONG)GetScreenStart().x+m_cellWidth*i,
(LONG)GetScreenStart().y+m_cellHeight*j,
m_cellWidth,m_cellHeight);
if(i == 0 || i == m_col-1 || j== 0 || j == m_row-1) //这给行.列元素两边赋为0
{
m_sceneButton.push_back(tempButton);
continue; //给边框赋为0,这里前面已经初始化为0.
}
tempButton->SetSrcPos(0,m_cellHeight*(tempArray[num]-1));
tempButton->SetFlag(tempArray[num]);
m_sceneButton.push_back(tempButton);
num++;
}
}
return TRUE;
}
UINT SceneSystem::GetArrayPos(int x, int y)
{
UINT col = (x - GetScreenStart().x) / m_cellWidth;
UINT row = (y - GetScreenStart().y) / m_cellHeight;
m_oldNum = m_num; //存储原来的位置
UINT val = row * m_col +col;
if (val < 0 || val >= m_sceneButton.size()) //判断mouse是否点到vector中的作标之外.
{ //如果在vector之外把原来的位置赋给它
return m_oldNum;
}
return m_sceneButton[val]->GetFlag() ? val : m_num;
}
BOOL SceneSystem::CheckLink(UINT first,UINT second)
{
if (m_sceneButton[first]->GetFlag() != m_sceneButton[second]->GetFlag())
{
return FALSE;
}
if (DirectLink(first,second) || oneCenterLink(first,second) || twoCenterLink(first,second))
{
m_sceneButton[first]->SetFlag(0);
m_sceneButton[second]->SetFlag(0);
m_num = 0;
return TRUE;
}
return FALSE;
}
VOID SceneSystem::DrawLine(VOID)
{
if (m_path.size() <= 0)
{
return;
}
HDC hdc = GetDC(m_hwnd);
HPEN hpen = (HPEN)SelectObject(hdc,CreatePen(PS_DOT,10,RGB(255,0,0)));
RECT rect = m_sceneButton[*(m_path.begin())]->GetRect();
int startX = (rect.right - rect.left)/2 + rect.left;
int startY = (rect.bottom - rect.top)/2 + rect.top;
MoveToEx(hdc,startX,startY,NULL);
for (std::vector<UINT>::iterator iter= m_path.begin()+1;iter != m_path.end();iter++)
{
RECT rect = m_sceneButton[*iter]->GetRect();
int NextX = (rect.right - rect.left)/2 + rect.left;
int NextY = (rect.bottom - rect.top)/2 + rect.top;
LineTo(hdc,NextX,NextY);
}
Sleep(50);
m_path.clear();
DeleteObject(hpen);
ReleaseDC(m_hwnd,hdc);
}
BOOL SceneSystem::Update(VOID)
{
POINT point = m_inputSystem->GetMousePos();
for (std::vector< Button* >::iterator iter = m_sceneButton.begin();
iter != m_sceneButton.end(); iter++)
{
if ((*iter)->GetFlag() == 0)
{
continue;
}
if ((*iter)->TestButton(point))
{
(*iter)->Animation(m_cellWidth);
}
}
if (m_inputSystem->IsMouseLeft())
{
int x = m_inputSystem->GetMouseX();
int y = m_inputSystem->GetMouseY();
UINT tempVal = GetArrayPos(x,y);
if( m_num > 0 && CheckLink(m_num,tempVal)) //调用检测函数;
{
return TRUE;
}
m_num = tempVal;
}
if (m_inputSystem->IsMouseRight())
{
m_num = 0;
}
return TRUE;
}
BOOL SceneSystem::Render(VOID)
{
m_gameBack->Render(m_gameBack->GetTextureInfo());
for (std::vector< Button* >::iterator iter = m_sceneButton.begin();
iter != m_sceneButton.end(); iter++)
{
if ((*iter)->GetFlag() == 0) //如果标记为0代表不画东西
{
continue;
}
if ((*iter)->TestButton(m_inputSystem->GetMousePos()))
{
SetCursor(LoadCursor(m_inputSystem->GetInstance(),LPCSTR(IDC_CURSOR3)));
(*iter)->Render((*iter)->GetTextureInfo(),0xff949494);
continue;
}
(*iter)->Render((*iter)->GetTextureInfo());
}
if (m_num > 0) //画鼠标右键点到button;
{
m_sceneButt
- 1
- 2
前往页