TileSelector.cpp
上传用户:pfmy85
上传日期:2007-01-07
资源大小:22k
文件大小:4k
- // TileSelector.cpp: implementation of the CTileSelector class.
- //
- //////////////////////////////////////////////////////////////////////
- #include "stdafx.h"
- #include "Isotest.h"
- #include "TileSelector.h"
- #ifdef _DEBUG
- #undef THIS_FILE
- static char THIS_FILE[]=__FILE__;
- #define new DEBUG_NEW
- #endif
- //////////////////////////////////////////////////////////////////////
- // Construction/Destruction
- //////////////////////////////////////////////////////////////////////
- CTileSelector::CTileSelector()
- {
- m_bTop = true;
- m_bBottom = false;
- m_pArrows = NULL;
- m_pTiles = NULL;
- m_nCurPos = 0;
- // m_pMap = NULL;
- }
- CTileSelector::~CTileSelector()
- {
- if (m_pArrows != NULL)
- {
- delete m_pArrows;
- m_pArrows = NULL;
- }
- // if (m_pMap != NULL)
- // {
- // delete m_pMap;
- // m_pMap = NULL;
- // }
- }
- bool CTileSelector::Create(CDDDevice *pDevice,
- CDDSurface* pTiles, // tile source
- int nTileWidthShift,
- int nTileHeightShift,
- LPRECT lpRect, // Display rectangle
- LPCTSTR lpName, // Arrows bitmap file name
- CPackFileManager* pPackFileManager) // Packfile manager
- {
- ASSERT(pTiles!=NULL);
- m_pTiles = pTiles;
- m_pArrows = new CDDDIBSurface;
- if (!m_pArrows->Create(pDevice, lpName, pPackFileManager))
- return false;
- // the arrow bitmap file must contain 4 arrow figure
- // one column of four raws
- m_nArrowWidth = m_pArrows->GetWidth();
- m_nArrowHeight = m_pArrows->GetHeight() >> 2;
- m_nTileWidthShift = nTileWidthShift;
- m_nTileHeightShift = nTileHeightShift;
- m_nTileWidth = 1 << nTileWidthShift;
- m_nTileHeight = 1 << nTileHeightShift;
- m_nSrcSurfaceCellRows = pTiles->GetWidth() >> nTileWidthShift;
- m_nTileCnt = (pTiles->GetWidth() >> nTileWidthShift)
- * (pTiles->GetHeight() >> nTileHeightShift);
- m_nTotalHeight = m_nTileCnt << m_nTileHeightShift;
- m_rcTiles.SetRect(lpRect->left, lpRect->top+m_nArrowHeight,
- lpRect->left+m_nArrowWidth, lpRect->bottom-m_nArrowHeight);
- m_rect.SetRect(lpRect->left, lpRect->top,
- lpRect->left+m_nArrowWidth, lpRect->bottom);
- /*
- m_pMap = new CDXMap;
- if (!m_pMap->Create(pTiles , nTileWidthShift , nTileHeightShift ,
- 1, nTileCnt,
- 0, lpRect))
- return false;
- for (int i=0; i<nTileCnt; i++)
- m_pMap->SetTile(i, i);
- */
- return true;
- }
- void CTileSelector::Draw(CDDSurface* pSurface)
- {
- CRect rc;
- // m_pMap->Draw(pSurface, m_rcTiles);
- if (m_bTop)
- rc.SetRect(0, 28, 32, 56);
- else
- rc.SetRect(0, 0, 32, 28);
- m_pArrows->Draw(pSurface, m_rect.left, m_rect.top, rc);
- if (m_bBottom)
- rc.SetRect(0, 84, 32, 112);
- else
- rc.SetRect(0, 56, 32, 84);
- m_pArrows->Draw(pSurface, m_rect.left, m_rect.bottom-28, rc);
- int nTileIndex = m_nCurPos >> m_nTileHeightShift;
- int nOff = m_rcTiles.top - (m_nCurPos & (m_nTileHeight -1));
- CRect rc1;
- rc.SetRect(m_rcTiles.left, nOff, m_rcTiles.right, nOff+m_nTileHeight);
- while (nOff < m_rcTiles.bottom)
- {
- rc.SetRect(m_rcTiles.left, nOff, m_rcTiles.right, nOff+m_nTileHeight);
- //rc.top = nOff;
- //rc.bottom = nOff + m_nTileHeight;
- if (!rc1.IntersectRect(rc, m_rcTiles))
- break;
- int x1, y1;
- x1 = (nTileIndex % m_nSrcSurfaceCellRows) << m_nTileWidthShift;
- y1 = (nTileIndex / m_nSrcSurfaceCellRows) << m_nTileHeightShift;
- rc.left = x1;
- rc.right = x1+m_nTileWidth;
- rc.top = y1 + (rc1.top - rc.top);
- rc.bottom = rc.top + rc1.Height();
- m_pTiles->Draw(pSurface, rc1.left, rc1.top, &rc);
- nOff += m_nTileHeight;
- nTileIndex ++;
- }
- }
- void CTileSelector::Up()
- {
- m_bBottom = false;
- m_nCurPos -= 8;
- if (m_nCurPos <= 0)
- {
- m_nCurPos = 0;
- m_bTop = true;
- }
- else
- m_bTop = false;
- }
- void CTileSelector::Down()
- {
- m_bTop = false;
- m_nCurPos += 8;
- if (m_nCurPos >= m_nTotalHeight-m_rcTiles.Height())
- {
- m_nCurPos = m_nTotalHeight-m_rcTiles.Height();
- m_bBottom = true;
- }
- else
- m_bBottom = false;
- }
- bool CTileSelector::ProcessMouse(int x, int y, // mouse position
- CDIMouseState::MOUSE_BUTTON_STATE eMouseState) // mouse button state
- {
- POINT pt;
- pt.x = x;
- pt.y = y;
- if (!m_rect.PtInRect(pt))
- return false;
- if (eMouseState == CDIMouseState::MOUSE_BUTTON_0_DOWN)
- {
- if (y < m_rcTiles.top)
- {
- Up();
- }
- else if (y >= m_rcTiles.bottom)
- {
- Down();
- }
- else
- {
- m_nCurTile = ((y-m_rcTiles.top) + m_nCurPos)
- >> m_nTileHeightShift;
- }
- }
- return true;
- }