当前位置:网站首页>Minesweeping II of souI instance

Minesweeping II of souI instance

2022-04-23 18:52:00 Brick Porter

List of articles

 


Preface

Follow the above minesweeping game , A displayed minefield interface has been established above . This chapter realizes the right-click mark .


 

One 、 stay XX Find a picture or yourself on PS And define it as skin

1.1 copy Picture to uires\image My name here is flag.png

1.2 uires Add below

<IMG>
    <file name="flag" path="image\flag.png"/>
</IMG>

1.3 skin.xml Define a skin   

<imglist name="skin_flag" src="img:flag" states="2"/>

1.4 Give... In the defined template img Specify this skin

<?xml version="1.0" encoding="utf-8"?>
<template>  
  <mine>
    <window skin="mine_bkskin" size="26,26" columnWeight="1" rowWeight="1">
      <img name="flag" pos="|,|" skin="skin_flag" show="0" offset="-0.5,-0.5" size="-1,-1"/>
      <text name="minecount" pos="|,|" show="0" offset="-0.5,-0.5" size="-1,-1" text="1"/>
    </window>
  </mine>
</template>

Two 、 Add processing functions

#pragma once
#include <core/SWnd.h>
#include <vector>
#include <layout/SGridLayout.h>
#include <sstream>

/// <summary>
///  Mark the grid status 
/// </summary>
enum class MINE_STATE {
	normal,// routine 
	flag,// Mark 
	open// open 
};

#define ICON_MAX_INDEX 1
/// <summary>
///  A grid represents a minefield 
/// </summary>
class CMine
{
	MINE_STATE m_mineState = MINE_STATE::normal;
private:
	SWindow* _pWnd = nullptr;
	int _iiconIndex = 0;
	size_t _uminecount = 0;
public:
	CMine()
	{

	}
	SWindow* getWnd()
	{
		return _pWnd;
	}
	void reset()
	{
		m_mineState = MINE_STATE::normal;
		_iiconIndex = 0;
		_pWnd->FindChildByName(L"flag")->SetVisible(FALSE);
		_pWnd->FindChildByName(L"minecount")->SetVisible(FALSE);
		_pWnd->EnableWindow(TRUE);

	}
	//EventLButtonUp	
	bool OnLButtonUp(EventLButtonUp* e)
	{
		//SWindow* _pWnd = sobj_cast<SWindow>(e->sender);
		if (canLClick())
		{
			m_mineState = MINE_STATE::open;
		}
		return true;
	}
	// EventCtxMenu
	bool OnRButtonDown(EventCtxMenu* e)
	{
		//SWindow* _pWnd = sobj_cast<SWindow>(e->sender);
		if (canRClick())
		{
			if (m_mineState == MINE_STATE::flag)
				if (++_iiconIndex > ICON_MAX_INDEX)
				{
					_iiconIndex = 0;
					m_mineState = MINE_STATE::normal;
					_pWnd->FindChildByName(L"flag")->SetVisible(FALSE, TRUE);
				}
				else
				{
					_pWnd->FindChildByName2<SImageWnd>(L"flag")->SetIcon(_iiconIndex);
				}
			else
			{
				m_mineState = MINE_STATE::flag;
				_pWnd->FindChildByName2<SImageWnd>(L"flag")->SetIcon(_iiconIndex);
				_pWnd->FindChildByName(L"flag")->SetVisible(TRUE,TRUE);
			}
		}
		return true;
	}

	void attackWnd(SWindow* pWnd)
	{
		SASSERT(pWnd);
		SASSERT(!_pWnd);
		_pWnd = pWnd;

		_pWnd->GetEventSet()->subscribeEvent(&CMine::OnLButtonUp, this);
		_pWnd->GetEventSet()->subscribeEvent(&CMine::OnRButtonDown, this);
	}
	~CMine()
	{

	}
	// To obtain state 
	MINE_STATE getState()const {
		return m_mineState;
	}
	// Whether to click on 
	bool isOpen()const {
		return m_mineState == MINE_STATE::open;
	}
	// Can I left click 
	bool canLClick()const {
		return m_mineState == MINE_STATE::normal;
	}
	// Can I right-click 
	bool canRClick()const {
		return m_mineState != MINE_STATE::open;
	}
	// Get icon drawing status 
	int getCurIndex()const {
		return _iiconIndex;
	}
	// Get icon drawing status 
	int getNextIndex() {
		++_iiconIndex;
		SASSERT(_iiconIndex >= 0);
		if (_iiconIndex >= ICON_MAX_INDEX)
			_iiconIndex = 0;
		return _iiconIndex;
	}
public:
#define MAX_COL 16
#define MAX_ROW 30

#define MIN_COL 9
#define MIN_ROW 9
	/// <summary>
	///  Create a minefield 
	/// </summary>
	/// <param name="parent"> Minefield window template </param>
	/// <param name="strName"> Minefield window template </param>
	/// <param name="mines"> Minefield data </param>
	/// <param name="row"> That's ok </param>
	/// <param name="col"> Column </param>
	/// <returns></returns>
	static std::vector<std::vector<CMine>>& createMine(
		SWindow* parent,
		const SStringT& strName,
		std::vector<std::vector<CMine>>& mines,
		const size_t col,
		const size_t row)
	{
		SASSERT(parent);

		if (!parent)
			return mines;

		{
			SGridLayout* param = sobj_cast<SGridLayout>(parent->GetLayout());
			SASSERT(param);
			std::wstringstream os;
			os << col;
			param->SetAttribute(L"columnCount", os.str().c_str());
		}

		for (auto& ite1 : mines)
		{
			for (auto& ite2 : ite1)
			{
				SWindow* _pWnd = ite2.getWnd();
				if (_pWnd)
				{
					SWindow* _Parent = _pWnd->GetParent();
					if (_Parent)
					{
						_Parent->DestroyChild(_pWnd);
					}
				}

			}
		}

		mines.clear();
		mines.resize(row);
		for (int r = 0; r < row; r++)
		{
			mines[r].resize(col);
		}

		SStringW strXml = GETTEMPLATEPOOLMR->GetTemplateString(strName);
		SASSERT(!strXml.IsEmpty());
		if (!strXml.IsEmpty())
		{
			pugi::xml_document xmlDoc;
			if (xmlDoc.load_buffer_inplace(strXml.GetBuffer(strXml.GetLength()), strXml.GetLength() * sizeof(WCHAR), 116, pugi::encoding_utf16))
			{
				pugi::xml_node xmlTemp = xmlDoc.first_child();
				SASSERT(xmlTemp);

				for (int r = 0; r < row; r++)
					for (int c = 0; c < col; c++)
					{
						SWindow* pChild = SApplication::getSingleton().CreateWindowByName(xmlTemp.name());
						if (pChild)
						{
							parent->InsertChild(pChild);
							pChild->InitFromXml(xmlTemp);
							mines[r][c].attackWnd(pChild);
						}
					}
			}
		}
		return mines;
	}

};

 

summary

Tips : That's what we're going to talk about today .

版权声明
本文为[Brick Porter]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204210603257720.html