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

📄 numberinputbox.cpp

📁 3D游戏展示程序
💻 CPP
字号:
//--------------------------------------------------
//  Desc: Number input box
//  Author: artsylee/2007.6.10
//--------------------------------------------------
#include "../stdafx.h"
#include "NumberInputBox.h"
#include "../Core/Common.h"
#include "../Core/Input.h"
#include "../Core/IniFile.h"
#include "../Core/Graphics.h"
#include "../Core/TextureManager.h"
#include "../Core/Message.h"
#include "../Core/Interface.h"

#define BACKSPACE_DELAY  330
#define BACKSPACE_REPEAT 50

CNumberInputBox::CNumberInputBox(CWindow* pParent /* = NULL */):CWindow(pParent)
{
	m_dwGUIType = GUI_COOLNUMBERINPUT;
	m_szCaption[0] = 0;
	m_hCursor = INVALID_HANDLE;
	m_CursorOffset = 0;
	m_BackState = CLEAR;
	m_dwStateTime = 0;
	m_CursorTime = 0;
	m_bShowCursor = true;
}

CNumberInputBox::~CNumberInputBox()
{
	ReleaseTexture(m_hCursor);
}

//--------------------------------------------------
// 1, 改写SetCaption
// 2, 长按退格
// 3, 激活光标
//--------------------------------------------------

bool CNumberInputBox::LoadFromIni(char* pfilename, char* pIndex)
{
	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);
	ui.ReadString(pIndex, "File", m_szUIPath);
	DWORD index = ui.ReadDWORD(pIndex, "Picture");
	if(ui.IsReadSucceed())
	{
		m_hTexture = g_pTextureManager->LoadAMFTexture(m_szUIPath, index);
		GRect zeroRc(0, 0, 0, 0);
		if(m_rcSrc==zeroRc)
		{
			CTexture * ptex = g_pTextureManager->GetTexture(m_hTexture);
			if(ptex)
			{
				m_rcSrc.SetRect(0, 0, ptex->GetWidth(), ptex->GetHeight());
			}
		}
	}
	index = ui.ReadDWORD(pIndex, "Cursor");
	if(ui.IsReadSucceed())
	{
		m_hCursor = g_pTextureManager->LoadAMFTexture(m_szUIPath, index);
	}
	if(m_pParent)
	{
		OffSet(m_pParent->GetRect().left, m_pParent->GetRect().top);
	}
	
	return true;
}

void CNumberInputBox::Render()
{
	if(m_dwAttrib & GUI_VISIBLE)
	{
		g_pGraphics->RenderSprite(m_hTexture, m_ptPos.x, m_ptPos.y, NULL, m_dwColor);
		DrawSingleLine(m_ptPos.x+4, m_ptPos.y+4, 0xffffffff, false, m_szCaption);
		if(m_dwAttrib & GUI_ACTIVE)
		{
			DWORD tm = ::timeGetTime();
			if(tm-m_CursorTime>=400)
			{
				m_bShowCursor = !m_bShowCursor;
				m_CursorTime = tm;
			}
			if(m_bShowCursor)
			{
				GRect rcPos;
				rcPos.SetRectWH(m_ptPos.x+4+m_CursorOffset, m_ptPos.y+4, 2, 12);
				g_pGraphics->RenderSprite(m_hCursor, &rcPos);
			}
		}
	}
}

DWORD CNumberInputBox::ProcessEvent()
{
	if(!(m_dwAttrib & GUI_VISIBLE) || !(m_dwAttrib & GUI_ENABLE) || m_hTexture==INVALID_HANDLE)
	{
		return INVALID_ID;
	}
	if(g_stInputInfo.MouseValue==LB_DOWN)
	{
		GRect rcPos;
		rcPos.SetRectWH(m_ptPos.x, m_ptPos.y, m_rcSrc.Width(), m_rcSrc.Height());
		if(CheckInRGN(rcPos))
		{
			m_dwAttrib |= GUI_ACTIVE;
		}
		else
		{
			m_dwAttrib &= ~GUI_ACTIVE;
		}
	}
	if(!(m_dwAttrib & GUI_ACTIVE))
	{
		return INVALID_ID;
	}
	if(g_stInputInfo.KeyValue == DIK_BACK)
	{
		BackSpace();
		m_BackState = CLICKED;
		m_dwStateTime = ::timeGetTime();
	}
	if(g_stInputInfo.KeyValue == DIK_BACK+MAXKEY)
	{
		m_BackState = CLEAR;
	}
	// Delay & Repeat
	if(m_BackState != CLEAR)
	{
		DWORD dCurrTime = ::timeGetTime();
		switch(m_BackState)
		{
		case CLICKED:
			if(dCurrTime - m_dwStateTime>BACKSPACE_DELAY)
			{
				BackSpace();
				m_BackState = HELD;
				m_dwStateTime = dCurrTime;
			}
			break;
		case HELD:
			if(dCurrTime - m_dwStateTime>BACKSPACE_REPEAT)
			{
				BackSpace();
				m_dwStateTime = dCurrTime;
			}
			break;
		}
	}
	if(g_stInputInfo.KeyValue==DIK_RETURN || g_stInputInfo.KeyValue==DIK_NUMPADENTER)
	{
		char *p = NULL;
		float fValue = (float)strtod(m_szCaption, &p);
		//发送ValueChange信息
		CMessage msg(MSG_SYS_GUI, m_dwID);
		msg.SetParameter(GUI_VALUE_CHANGE, *(DWORD*)(&fValue), this);
		PostMessage(msg);
	}
	
	if(strlen(m_szCaption)>=16)	return INVALID_ID;
	switch(g_stInputInfo.KeyValue)
	{
	case DIK_NUMPAD0:
	case DIK_0:		strcat(m_szCaption, "0");	UpdateCursor();	break;
	case DIK_NUMPAD1:
	case DIK_1:		strcat(m_szCaption, "1");	UpdateCursor();	break;
	case DIK_NUMPAD2:
	case DIK_2:		strcat(m_szCaption, "2");	UpdateCursor();	break;
	case DIK_NUMPAD3:
	case DIK_3:		strcat(m_szCaption, "3");	UpdateCursor();	break;
	case DIK_NUMPAD4:
	case DIK_4:		strcat(m_szCaption, "4");	UpdateCursor();	break;
	case DIK_NUMPAD5:
	case DIK_5:		strcat(m_szCaption, "5");	UpdateCursor();	break;
	case DIK_NUMPAD6:
	case DIK_6:		strcat(m_szCaption, "6");	UpdateCursor();	break;
	case DIK_NUMPAD7:
	case DIK_7:		strcat(m_szCaption, "7");	UpdateCursor();	break;
	case DIK_NUMPAD8:
	case DIK_8:		strcat(m_szCaption, "8");	UpdateCursor();	break;
	case DIK_NUMPAD9:
	case DIK_9:		strcat(m_szCaption, "9");	UpdateCursor();	break;
	case DIK_DECIMAL:
	case DIK_PERIOD:
		{
			for(size_t i=0; i<strlen(m_szCaption); i++)
			{
				if(m_szCaption[i]=='.')	break;
			}
			if(i>=strlen(m_szCaption))	
			{
				strcat(m_szCaption, ".");	
				UpdateCursor();
			}
		}
		break;
	case DIK_SUBTRACT:
	case DIK_MINUS:
		{
			if(m_szCaption[0]==0)	
			{
				strcat(m_szCaption, "-");
				UpdateCursor();
			}
		}
		break;
	}
	return INVALID_ID;
}

void CNumberInputBox::UpdateCursor(void)
{
	RECT rc = GetFontRect(m_szCaption);
	m_CursorOffset = rc.right-rc.left;
}

void CNumberInputBox::BackSpace(void)
{
	if(m_szCaption[0]!=0)	
	{
		m_szCaption[strlen(m_szCaption)-1] = 0;
		UpdateCursor();
	}
}

void CNumberInputBox::SetEmptyValue(void)
{
	m_szCaption[0] = 0;
	UpdateCursor();
}

void CNumberInputBox::SetValue(float value)
{
	char szTemp[256];
	sprintf(szTemp, "%f", value);
	szTemp[16] = 0;
	strcpy(m_szCaption, szTemp);
	UpdateCursor();
}

void CNumberInputBox::SetValue(int value)
{
	char szTemp[256];
	sprintf(szTemp, "%d", value);
	szTemp[16] = 0;
	strcpy(m_szCaption, szTemp);
	UpdateCursor();
}

float CNumberInputBox::GetValue(void)
{
	char *p = NULL;
	float fValue = (float)strtod(m_szCaption, &p);
	return fValue;
}

void CNumberInputBox::PostValue(void)
{
	char *p = NULL;
	float fValue = (float)strtod(m_szCaption, &p);
	//发送ValueChange信息
	CMessage msg(MSG_SYS_GUI, m_dwID);
	msg.SetParameter(GUI_VALUE_CHANGE, *(DWORD*)(&fValue), this);
	PostMessage(msg);
}

⌨️ 快捷键说明

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