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

📄 scrollbar.cpp

📁 3D游戏展示程序
💻 CPP
📖 第 1 页 / 共 2 页
字号:
//--------------------------------------------------
//  Desc: ScrollBar
//  Author: artsylee/2006.11.27
//--------------------------------------------------
#include "../stdafx.h"
#include <assert.h>
#include "ScrollBar.h"
#include "../Core/Common.h"
#include "../Core/Input.h"
#include "../Core/IniFile.h"
#include "../Core/Graphics.h"
#include "../Core/TextureManager.h"
#include "../Core/Interface.h"
#include "../Core/Log.h"
#include "../Core/Message.h"

GScrollBar::GScrollBar(CWindow* pParent):CWindow(pParent)
{
	m_dwGUIType = GUI_SCROLLBAR;
	m_bShowSlider = true;
	m_MinState = m_MaxState = BTN_NORMAL;
	m_hBtnMin = INVALID_HANDLE;
	m_hBtnMax = INVALID_HANDLE;
	m_hBtnSlider = INVALID_HANDLE;
	m_bPressed = false;

	m_nPosition = 0;
	m_nPageSize = 1;
	m_nStart = 0;
	m_nEnd = 1;
	m_Arrow = CLEAR;
	m_dArrowTS = 0;
	m_ButtonOffset = 0;
}

GScrollBar::~GScrollBar()
{
	if(g_pTextureManager)
	{
		g_pTextureManager->ReleaseTexture(m_hBtnMin);
		g_pTextureManager->ReleaseTexture(m_hBtnMax);
		g_pTextureManager->ReleaseTexture(m_hBtnSlider);
	}
}

bool GScrollBar::LoadFromIni(char* pfilename, char* pIndex)
{
	// common
	CIniFile ui(pfilename);
	m_dwID = ui.ReadDWORD(pIndex, "ID");
	m_ptPos = ui.ReadPoint(pIndex, "Position");
	// 滑块的区域
	m_rcSrc = ui.ReadRect(pIndex, "SrcRect");
	m_dwColor = ui.ReadDWORD(pIndex, "Color", 0xffffffff);
	m_dwAttrib = ui.ReadDWORD(pIndex, "Attrib", 10);
	// value
	m_nPosition = ui.ReadInt(pIndex, "Pos", 0);
	m_nPageSize = ui.ReadInt(pIndex, "PageSize", 1);
	m_nStart = ui.ReadInt(pIndex, "Start", 0);
	m_nEnd = ui.ReadInt(pIndex, "End", 1);
	ui.ReadString(pIndex, "File", m_szUIPath);
	
	GRect rcSrc;
	CTexture *pTex = NULL;

	// button1 min
	DWORD index = ui.ReadDWORD(pIndex, "Btn1Picture");
	if(ui.IsReadSucceed())
		m_hBtnMin = LoadAMFTexture(m_szUIPath, index);
	if(m_hBtnMin)
	{
		pTex = g_pTextureManager->GetTexture(m_hBtnMin);
		if(pTex)	rcSrc.SetRect(0, 0, pTex->GetWidth(), pTex->GetHeight());
	}
	else
	{
		WriteLog(INFO_ERROR, "Can't Get Min Button in file[%s] index[%s]", pfilename, pIndex);
		return false;
	}
	for(int i=0; i<BTN_STATENUM; i++)
	{
		m_rcMin[i].SetRectWH(0, i*(rcSrc.Height()/4), rcSrc.Width(), rcSrc.Height()/4);
	}

	// button2 max
	index = ui.ReadDWORD(pIndex, "Btn2Picture");
	if(ui.IsReadSucceed())
		m_hBtnMax = LoadAMFTexture(m_szUIPath, index);
	if(m_hBtnMax)
	{
		pTex = g_pTextureManager->GetTexture(m_hBtnMax);
		if(pTex)	rcSrc.SetRect(0, 0, pTex->GetWidth(), pTex->GetHeight());
	}
	else
	{
		WriteLog(INFO_ERROR, "Can't Get Max Button in file[%s] index[%s]", pfilename, pIndex);
		return false;
	}

	for(int i=0; i<BTN_STATENUM; i++)
	{
		m_rcMax[i].SetRectWH(0, i*(rcSrc.Height()/4), rcSrc.Width(), rcSrc.Height()/4);
	}

	// Source RECT
	/*
	if(m_rcSrc==GRect(0, 0, 0, 0))
	{
		WriteLog(INFO_ERROR, "Can't Get m_rcSrc in file[%s] index[%s]", pfilename, pIndex);
		return false;
	}
	*/
	// slider button
	index = ui.ReadDWORD(pIndex, "PinPicture");
	if(ui.IsReadSucceed())
		m_hBtnSlider = LoadAMFTexture(m_szUIPath, index);
	if(m_hBtnSlider)
	{
		pTex = g_pTextureManager->GetTexture(m_hBtnSlider);
		if(pTex)	m_rcButton.SetRect(0, 0, pTex->GetWidth(), pTex->GetHeight());
	}
	else
	{
		WriteLog(INFO_ERROR, "Can't Get Slider Button in file[%s] index[%s]", pfilename, pIndex);
		return false;
	}
	// control end
	if(m_pParent)
	{
		OffSet(m_pParent->GetRect().left, m_pParent->GetRect().top);
	}
	UpdateRect();
	UpdateBtnRect();
	if(m_dwAttrib & GUI_ENABLE)
	{
		m_MinState = BTN_NORMAL;
		m_MaxState = BTN_NORMAL;
	}
	else
	{
		m_MinState = BTN_DISABLE;
		m_MaxState = BTN_DISABLE;
	}
	return true;
}

DWORD GScrollBar::ProcessEvent()
{
	if(!(m_dwAttrib & GUI_VISIBLE) || !(m_dwAttrib & GUI_ENABLE))
	{
		return INVALID_ID;
	}
	static int ThumbOffset;
	bool bMouseOver = false;
	GRect rcPos;
	int oldPos = m_nPosition;
	// button min
	rcPos.SetRectWH(m_ptMin.x+m_ButtonOffset, m_ptMin.y+m_ButtonOffset, m_rcMin[BTN_NORMAL].Width()-2*m_ButtonOffset, m_rcMin[BTN_NORMAL].Height()-2*m_ButtonOffset);
	if(CheckInRGN(rcPos))
	{
		if(g_stInputInfo.MouseValue==LB_DOWN)
		{
			m_MinState = BTN_DOWN;
			if(m_nPosition > m_nStart)
			{
				--m_nPosition;
				UpdateBtnRect();
			}

			m_LastMouse = g_stInputInfo.point;
			m_Arrow = CLICKED_MIN;
			m_dArrowTS = ::timeGetTime();
		}
		if(!g_Input.GetMousePtr()->IsKeyDown(LBUTTON))
		{
			m_MinState = BTN_OVER;
		}

		bMouseOver = true;
	}
	else
	{
		m_MinState = BTN_NORMAL;
	}
	// button max
	rcPos.SetRectWH(m_ptMax.x+m_ButtonOffset, m_ptMax.y+m_ButtonOffset, m_rcMax[BTN_NORMAL].Width()-2*m_ButtonOffset, m_rcMax[BTN_NORMAL].Height()-2*m_ButtonOffset);
	if(CheckInRGN(rcPos))
	{
		if(g_stInputInfo.MouseValue==LB_DOWN)
		{
			m_MaxState = BTN_DOWN;
			if(m_nPosition + m_nPageSize < m_nEnd)
			{
				++m_nPosition;
				UpdateBtnRect();
			}

			m_LastMouse = g_stInputInfo.point;
			m_Arrow = CLICKED_MAX;
			m_dArrowTS = ::timeGetTime();
		}
		if(!g_Input.GetMousePtr()->IsKeyDown(LBUTTON))
		{
			m_MaxState = BTN_OVER;
		}
		bMouseOver = true;
	}
	else
	{
		m_MaxState = BTN_NORMAL;
	}
	// Slider
	rcPos.SetRectWH(m_DstButton.left+m_ButtonOffset, m_DstButton.right+m_ButtonOffset, m_DstButton.Width()-2*m_ButtonOffset, m_DstButton.Height()-2*m_ButtonOffset);
	if(CheckInRGN(m_DstButton) && g_stInputInfo.MouseValue==LB_DOWN)
	{
		m_bPressed = true;
		if(m_SliderType)	{	ThumbOffset = g_stInputInfo.point.y - m_DstButton.top;	}
		else				{	ThumbOffset = g_stInputInfo.point.x - m_DstButton.left;	}
		bMouseOver = true;
	}
	rcPos.SetRectWH(m_ptPos.x, m_ptPos.y, m_rcSrc.Width(), m_rcSrc.Height());
	// 控件范围内
	if(CheckInRGN(rcPos))
	{
		// 点击					   
		if(g_stInputInfo.MouseValue==LB_DOWN && !m_bPressed)
		{	
			if(m_SliderType)
			{
				if(m_DstButton.top>g_stInputInfo.point.y && m_ptPos.y<=g_stInputInfo.point.y)
				{
					Scroll(-(m_nPageSize-1));
				}
				else if(m_DstButton.bottom<=g_stInputInfo.point.y && m_ptPos.y+m_rcSrc.Height()>g_stInputInfo.point.y)
				{
					Scroll(m_nPageSize-1);
				}
			}
			else
			{
				if(m_DstButton.left>g_stInputInfo.point.x && m_ptPos.x<=g_stInputInfo.point.x)
				{
					Scroll(-(m_nPageSize-1));
				}
				else if(m_DstButton.right<=g_stInputInfo.point.x && m_ptPos.x+m_rcSrc.Width()>g_stInputInfo.point.x)
				{
					Scroll(m_nPageSize-1);
				}
			}
		}
		bMouseOver = true;
	}
	// Drag
	if(g_stInputInfo.MouseMove && m_bPressed)
	{
		int nMaxFirstItem = m_nEnd - m_nStart - m_nPageSize;
		if(m_SliderType)
		{
			m_DstButton.bottom += g_stInputInfo.point.y - ThumbOffset - m_DstButton.top; 
			m_DstButton.top = g_stInputInfo.point.y - ThumbOffset;
			int nMaxThumb = m_rcSrc.Height()-m_rcButton.Height();
			m_nPosition = m_nStart+FloatToIntAver((m_DstButton.top-m_ptPos.y)/((float)nMaxThumb/nMaxFirstItem));
		}
		else
		{
			m_DstButton.right += g_stInputInfo.point.x - ThumbOffset - m_DstButton.left; 
			m_DstButton.left = g_stInputInfo.point.x - ThumbOffset;
			int nMaxFirstItem = m_nEnd - m_nStart - m_nPageSize;
			int nMaxThumb = m_rcSrc.Width()-m_rcButton.Width();
			m_nPosition = m_nStart+FloatToIntAver((m_DstButton.left-m_ptPos.x)/((float)nMaxThumb/nMaxFirstItem));
		}
		AdjustPosition();
		UpdateBtnRect(); 
		bMouseOver = true;
	}
	if(g_stInputInfo.MouseValue==LB_UP)
	{
		m_bPressed = false;
		m_Arrow = CLEAR;
	}
	// Delay & Repeat
	if(m_Arrow != CLEAR)
	{
		DWORD dCurrTime = ::timeGetTime();
		rcPos.SetRectWH(m_ptMin.x+m_ButtonOffset, m_ptMin.y+m_ButtonOffset, m_rcMin[BTN_NORMAL].Width()-2*m_ButtonOffset, m_rcMin[BTN_NORMAL].Height()-2*m_ButtonOffset);
		GRect rcPosMax;
		rcPosMax.SetRectWH(m_ptMax.x+m_ButtonOffset, m_ptMax.y+m_ButtonOffset, m_rcMax[BTN_NORMAL].Width()-2*m_ButtonOffset, m_rcMax[BTN_NORMAL].Height()-2*m_ButtonOffset);
		if(CheckInRGN(rcPos, &m_LastMouse))
		{
			switch(m_Arrow)
			{
			case CLICKED_MIN:
				if(SCROLLBAR_ARROWCLICK_DELAY < dCurrTime - m_dArrowTS)
				{
					Scroll(-1);
					m_Arrow = HELD_MIN;
					m_dArrowTS = dCurrTime;
				}
				break;
			case HELD_MIN:
				if(SCROLLBAR_ARROWCLICK_REPEAT < dCurrTime - m_dArrowTS)
				{
					Scroll(-1);
					m_dArrowTS = dCurrTime;
				}
				break;
			}
		} 
		else if(CheckInRGN(rcPosMax, &m_LastMouse))
		{
			switch(m_Arrow)
			{
			case CLICKED_MAX:
				if(SCROLLBAR_ARROWCLICK_DELAY < dCurrTime - m_dArrowTS)
				{
					Scroll(1);
					m_Arrow = HELD_MAX;
					m_dArrowTS = dCurrTime;
				}
				break;
			case HELD_MAX:
				if(SCROLLBAR_ARROWCLICK_REPEAT < dCurrTime - m_dArrowTS)
				{
					Scroll(1);
					m_dArrowTS = dCurrTime;
				}
				break;
			}
		}
	}
	if(oldPos!=m_nPosition)
	{
		//发送ValueChange信息
		CMessage msg(MSG_SYS_GUI, m_dwID);
		msg.SetParameter(GUI_VALUE_CHANGE, m_nPosition, this);
		PostMessage(msg);
	}

	if(bMouseOver)
		return m_dwID;
	else
		return INVALID_ID;

⌨️ 快捷键说明

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