SDL游戏之路(十五)--英文输入法



英文输入法

前面已经创建好了模拟键盘。那么在按钮点击上添加事件。就可以实现英文输入法了。

绘制一个白色的矩形框为输入框,在上面添加一个红色闪动光标。

头文件:

#ifndef SKVIEWINPUT_H_
#define SKVIEWINPUT_H_

#include "SkView.h"
#include "SkButton.h"
#include "SkWord.h"

/**键盘按钮点击事件**/
void sk_button_keyboard_pressed(void * pData, sk_park::SkButton * pButton);
namespace sk_park {

/**键盘输入展示界面**/
class SkViewInput: public SkView {
public:
	/**定义键盘布局**/
	class Keyboard {
	public:
		SkButton m_bt_zm[200];
		Sint32 m_iSize;
		Sint32 m_iShift;
		void resize();
		void init(SkFont * pFont, void (*func_pressed)(void *, SkButton *));
		void show(SkShow* pSkShow, Sint64 iCurMTime);
		bool doEvent(SkEvent & stSkEvent, Sint64 iCurMTime, SkView * pView);
	private:
		SkFont * m_pFont;
	};
	void init();
	void doEvent(SkEvent & stSkEvent, Sint64 iCurMTime);
	void show(SkShow* pSkShow, Sint64 iCurMTime);
	void run(Sint64 iCurMTime);
	void resize();
	/**设置shift按键切换**/
	void setShift();
	/**设置中英文输入法切换**/
	void setEnglish(bool bFlag);
	/**添加一个输入**/
	void addInput(string sIn);
	/**删除一个输入**/
	void delInput();
private:
	/**记录当前是否为英文输入法**/
	bool m_bEnglish;
	/**输入框内字符窜展示的最大宽度**/
	Sint32 m_strMaxLen;
	/**当前输入框内容**/
	SkWord m_curWord;
	/**输入历史**/
	vector<string> m_vctStr;
	/**返回按键**/
	SkButton m_backButton;
	/**英文字体**/
	SkFont m_font_en;
	/**中文字体**/
	SkFont m_font_ch;
	/**模拟键盘**/
	Keyboard m_keyboard;
	/**英文输入按键**/
	SkButton m_bt_english;
	/**中文输入按键**/
	SkButton m_bt_chinese;
};
}

extern sk_park::SkViewInput g_SkViewInput;
#endif /* SKVIEWINPUT_H_ */


具体实现:

#include "pch.h"

#include "SkViewInput.h"
#include "../sk_src/SkFunction.h"
using namespace sk_park;

void SkViewInput::Keyboard::resize() {
	//SkButton m_bt_zm[39];
	int iSize = m_iSize - 10;
	for (int i = 0; i < 50; i++) {
		SkWord wordIn, wordOut, wordPressed;
		wordIn.setStr(m_bt_zm[i].m_sName.c_str(), *m_pFont, 255, 0, 0, 255);
		wordOut.setStr(m_bt_zm[i].m_sName.c_str(), *m_pFont, 0, 255, 0, 255);
		wordPressed.setStr(m_bt_zm[i].m_sName.c_str(), *m_pFont, 255, 255, 0,
				255);
		m_bt_zm[i].setShow(wordIn.m_skSurface, wordOut.m_skSurface,
				wordPressed.m_skSurface);
		m_bt_zm[i].m_area.clear();
		m_bt_zm[i].m_area.addPoint(0, 0);
		m_bt_zm[i].m_area.addPoint(iSize, 0);
		m_bt_zm[i].m_area.addPoint(iSize, iSize);
		m_bt_zm[i].m_area.addPoint(0, iSize);
	}
	for (int i = 100; i < 150; i++) {
		SkWord wordIn, wordOut, wordPressed;
		wordIn.setStr(m_bt_zm[i].m_sName.c_str(), *m_pFont, 255, 0, 0, 255);
		wordOut.setStr(m_bt_zm[i].m_sName.c_str(), *m_pFont, 0, 255, 0, 255);
		wordPressed.setStr(m_bt_zm[i].m_sName.c_str(), *m_pFont, 255, 255, 0,
				255);
		m_bt_zm[i].setShow(wordIn.m_skSurface, wordOut.m_skSurface,
				wordPressed.m_skSurface);
		m_bt_zm[i].m_area.clear();
		m_bt_zm[i].m_area.addPoint(0, 0);
		m_bt_zm[i].m_area.addPoint(iSize, 0);
		m_bt_zm[i].m_area.addPoint(iSize, iSize);
		m_bt_zm[i].m_area.addPoint(0, iSize);
	}
}
void SkViewInput::Keyboard::init(SkFont * pFont,
		void (*func_pressed)(void *, SkButton *)) {
	m_iShift = 0;
	m_iSize = 90;
	m_pFont = pFont;
	int iSize = m_iSize;
	int iPosX = 50;
	int iPosY = 300;
	int iLine1 = 12;
	int iLine2 = 13;
	int iLine3 = 13;
	int iLine4 = 12;
	for (int i = 0; i < iLine1; i++) {
		m_bt_zm[i].m_iPosX = iPosX + iSize * i;
		m_bt_zm[i].m_iPosY = iPosY;

		m_bt_zm[i + 100].m_iPosX = iPosX + iSize * i;
		m_bt_zm[i + 100].m_iPosY = iPosY;
	}
	for (int i = 0; i < iLine2; i++) {
		m_bt_zm[i + iLine1].m_iPosX = iPosX + iSize * i + iSize / 2;
		m_bt_zm[i + iLine1].m_iPosY = iPosY + iSize;

		m_bt_zm[i + 100 + iLine1].m_iPosX = iPosX + iSize * i + iSize / 2;
		m_bt_zm[i + 100 + iLine1].m_iPosY = iPosY + iSize;
	}
	for (int i = 0; i < iLine3; i++) {
		m_bt_zm[i + iLine1 + iLine2].m_iPosX = iPosX + iSize * i + iSize;
		m_bt_zm[i + iLine1 + iLine2].m_iPosY = iPosY + iSize * 2;

		m_bt_zm[i + 100 + iLine1 + iLine2].m_iPosX = iPosX + iSize * i + iSize;
		m_bt_zm[i + 100 + iLine1 + iLine2].m_iPosY = iPosY + iSize * 2;
	}
	for (int i = 0; i < iLine4; i++) {
		m_bt_zm[i + iLine1 + iLine2 + iLine3].m_iPosX = iPosX + iSize * i
				+ iSize / 2;
		m_bt_zm[i + iLine1 + iLine2 + iLine3].m_iPosY = iPosY + iSize * 3;

		m_bt_zm[i + 100 + iLine1 + iLine2 + iLine3].m_iPosX = iPosX + iSize * i
				+ iSize / 2;
		m_bt_zm[i + 100 + iLine1 + iLine2 + iLine3].m_iPosY = iPosY + iSize * 3;
	}
	for (int i = 0; i < 50; i++) {
		m_bt_zm[i].setFuncPressed(func_pressed);
		m_bt_zm[i + 100].setFuncPressed(func_pressed);
	}
	for (int i = 0; i < 150; i++) {
		m_bt_zm[i].m_iId = i;
		m_bt_zm[i].m_bWord = true;
	}
	//第一行 12
	m_bt_zm[0].m_sName = "1";
	m_bt_zm[1].m_sName = "2";
	m_bt_zm[2].m_sName = "3";
	m_bt_zm[3].m_sName = "4";
	m_bt_zm[4].m_sName = "5";
	m_bt_zm[5].m_sName = "6";
	m_bt_zm[6].m_sName = "7";
	m_bt_zm[7].m_sName = "8";
	m_bt_zm[8].m_sName = "9";
	m_bt_zm[9].m_sName = "0";
	m_bt_zm[10].m_sName = "-";
	m_bt_zm[11].m_sName = "=";
	//第二行 13
	m_bt_zm[12].m_sName = "q";
	m_bt_zm[13].m_sName = "w";
	m_bt_zm[14].m_sName = "e";
	m_bt_zm[15].m_sName = "r";
	m_bt_zm[16].m_sName = "t";
	m_bt_zm[17].m_sName = "y";
	m_bt_zm[18].m_sName = "u";
	m_bt_zm[19].m_sName = "i";
	m_bt_zm[20].m_sName = "o";
	m_bt_zm[21].m_sName = "p";
	m_bt_zm[22].m_sName = "[";
	m_bt_zm[23].m_sName = "]";
	m_bt_zm[24].m_sName = "\\";
	//第三行 13
	m_bt_zm[25].m_sName = "a";
	m_bt_zm[26].m_sName = "s";
	m_bt_zm[27].m_sName = "d";
	m_bt_zm[28].m_sName = "f";
	m_bt_zm[29].m_sName = "g";
	m_bt_zm[30].m_sName = "h";
	m_bt_zm[31].m_sName = "j";
	m_bt_zm[32].m_sName = "k";
	m_bt_zm[33].m_sName = "l";
	m_bt_zm[34].m_sName = ";";
	m_bt_zm[35].m_sName = "'";
	m_bt_zm[36].m_sName = "De";
	m_bt_zm[37].m_sName = "En";
	//第四行 12
	m_bt_zm[38].m_sName = "Sh";
	m_bt_zm[39].m_sName = "z";
	m_bt_zm[40].m_sName = "x";
	m_bt_zm[41].m_sName = "c";
	m_bt_zm[42].m_sName = "v";
	m_bt_zm[43].m_sName = "b";
	m_bt_zm[44].m_sName = "n";
	m_bt_zm[45].m_sName = "m";
	m_bt_zm[46].m_sName = ",";
	m_bt_zm[47].m_sName = ".";
	m_bt_zm[48].m_sName = "/";
	m_bt_zm[49].m_sName = "Sh";

	//第一行 12
	m_bt_zm[100].m_sName = "!";
	m_bt_zm[101].m_sName = "@";
	m_bt_zm[102].m_sName = "#";
	m_bt_zm[103].m_sName = "$";
	m_bt_zm[104].m_sName = "%";
	m_bt_zm[105].m_sName = "^";
	m_bt_zm[106].m_sName = "&";
	m_bt_zm[107].m_sName = "*";
	m_bt_zm[108].m_sName = "(";
	m_bt_zm[109].m_sName = ")";
	m_bt_zm[110].m_sName = "_";
	m_bt_zm[111].m_sName = "+";
	//第二行 13
	m_bt_zm[112].m_sName = "Q";
	m_bt_zm[113].m_sName = "W";
	m_bt_zm[114].m_sName = "E";
	m_bt_zm[115].m_sName = "R";
	m_bt_zm[116].m_sName = "T";
	m_bt_zm[117].m_sName = "Y";
	m_bt_zm[118].m_sName = "U";
	m_bt_zm[119].m_sName = "I";
	m_bt_zm[120].m_sName = "O";
	m_bt_zm[121].m_sName = "P";
	m_bt_zm[122].m_sName = "{";
	m_bt_zm[123].m_sName = "}";
	m_bt_zm[124].m_sName = "|";
	//第三行 13
	m_bt_zm[125].m_sName = "A";
	m_bt_zm[126].m_sName = "S";
	m_bt_zm[127].m_sName = "D";
	m_bt_zm[128].m_sName = "F";
	m_bt_zm[129].m_sName = "G";
	m_bt_zm[130].m_sName = "H";
	m_bt_zm[131].m_sName = "J";
	m_bt_zm[132].m_sName = "K";
	m_bt_zm[133].m_sName = "L";
	m_bt_zm[134].m_sName = ":";
	m_bt_zm[135].m_sName = "\"";
	m_bt_zm[136].m_sName = "De";
	m_bt_zm[137].m_sName = "En";
	//第四行 12
	m_bt_zm[138].m_sName = "Sh";
	m_bt_zm[139].m_sName = "Z";
	m_bt_zm[140].m_sName = "X";
	m_bt_zm[141].m_sName = "C";
	m_bt_zm[142].m_sName = "V";
	m_bt_zm[143].m_sName = "B";
	m_bt_zm[144].m_sName = "N";
	m_bt_zm[145].m_sName = "M";
	m_bt_zm[146].m_sName = "<";
	m_bt_zm[147].m_sName = ">";
	m_bt_zm[148].m_sName = "?";
	m_bt_zm[149].m_sName = "Sh";

}
void SkViewInput::Keyboard::show(SkShow* pSkShow, Sint64 iCurMTime) {
	SkRect stPos;
	for (int i = 0; i < 50; i++) {
		stPos.x = m_bt_zm[i + m_iShift].m_iPosX;
		stPos.y = m_bt_zm[i + m_iShift].m_iPosY;
		pSkShow->drawButton(NULL, &m_bt_zm[i + m_iShift], &stPos, iCurMTime);
	}
}
bool SkViewInput::Keyboard::doEvent(SkEvent & stSkEvent, Sint64 iCurMTime,
		SkView * pView) {
	bool bRet = false;
	for (int i = 0; i < 50; i++) {
		bRet = m_bt_zm[i + m_iShift].handleEvent(m_bt_zm[i + m_iShift].m_iPosX,
				m_bt_zm[i + m_iShift].m_iPosY, stSkEvent, pView);
		if (bRet) {
			return true;
		}
	}
	return false;
}

void SkViewInput::init() {
	m_strMaxLen = 900;
	m_bEnglish = true;
	m_font_en.init("font/adobe2.otf", 50);
	m_font_ch.init("font/simhei.ttf", 35);
	m_keyboard.init(&m_font_en, sk_button_keyboard_pressed);
	m_curWord.setStr("", m_font_ch, 0x00, 0x00, 0x00, 0xff);

	m_bt_english.m_sName = "Eng";
	m_bt_english.m_iId = 1001;
	m_bt_chinese.m_sName = "中文";
	m_bt_chinese.m_iId = 1002;
	m_bt_english.m_bWord = true;
	m_bt_chinese.m_bWord = true;
	{
		SkWord wordIn, wordOut, wordPressed;
		wordIn.setStr(m_bt_english.m_sName.c_str(), m_font_ch, 255, 0, 0, 255);
		wordOut.setStr(m_bt_english.m_sName.c_str(), m_font_ch, 0, 255, 0, 255);
		wordPressed.setStr(m_bt_english.m_sName.c_str(), m_font_ch, 255, 255, 0,
				255);
		m_bt_english.setShow(wordIn.m_skSurface, wordOut.m_skSurface,
				wordPressed.m_skSurface);
		m_bt_english.m_area.clear();
		m_bt_english.m_area.addPoint(0, 0);
		m_bt_english.m_area.addPoint(wordIn.getWidth(), 0);
		m_bt_english.m_area.addPoint(wordIn.getWidth(), wordIn.getHeight());
		m_bt_english.m_area.addPoint(0, wordIn.getHeight());
	}
	{
		SkWord wordIn, wordOut, wordPressed;
		wordIn.setStr(m_bt_chinese.m_sName.c_str(), m_font_ch, 255, 0, 0, 255);
		wordOut.setStr(m_bt_chinese.m_sName.c_str(), m_font_ch, 0, 255, 0, 255);
		wordPressed.setStr(m_bt_chinese.m_sName.c_str(), m_font_ch, 255, 255, 0,
				255);
		m_bt_chinese.setShow(wordIn.m_skSurface, wordOut.m_skSurface,
				wordPressed.m_skSurface);
		m_bt_chinese.m_area.clear();
		m_bt_chinese.m_area.addPoint(0, 0);
		m_bt_chinese.m_area.addPoint(wordIn.getWidth(), 0);
		m_bt_chinese.m_area.addPoint(wordIn.getWidth(), wordIn.getHeight());
		m_bt_chinese.m_area.addPoint(0, wordIn.getHeight());
	}
	m_bt_english.setPosition(1050, 140);
	m_bt_chinese.setPosition(1150, 140);
	m_bt_english.setFuncPressed(sk_button_keyboard_pressed);
	m_bt_chinese.setFuncPressed(sk_button_keyboard_pressed);

	SkImage pic_ch_1;
	SkImage pic_ch_2;

	pic_ch_1.load("pic/ch1.png");
	pic_ch_2.load("pic/ch2.png");
	m_backButton.setShow(pic_ch_1.m_skSurface, pic_ch_1.m_skSurface,
			pic_ch_2.m_skSurface);
	m_backButton.m_area.addPoint(0, 0);
	m_backButton.m_area.addPoint(0, pic_ch_1.m_iHeight);
	m_backButton.m_area.addPoint(pic_ch_1.m_iWidth, pic_ch_1.m_iHeight);
	m_backButton.m_area.addPoint(pic_ch_1.m_iWidth, 0);
	m_backButton.setPosition(600, 20);
	m_backButton.setFuncPressed(button_back_to_first);

	resize();
}
void SkViewInput::doEvent(SkEvent & stSkEvent, Sint64 iCurMTime) {
	bool bRet = m_backButton.handleEvent(m_backButton.m_iPosX,
			m_backButton.m_iPosY, stSkEvent, NULL);
	if (bRet) {
		return;
	}
	bRet = m_keyboard.doEvent(stSkEvent, iCurMTime, this);
	if (bRet) {
		return;
	}
	bRet = m_bt_english.handleEvent(m_bt_english.m_iPosX, m_bt_english.m_iPosY,
			stSkEvent, this);
	if (bRet) {
		return;
	}
	bRet = m_bt_chinese.handleEvent(m_bt_chinese.m_iPosX, m_bt_chinese.m_iPosY,
			stSkEvent, this);
	if (bRet) {
		return;
	}
}
void SkViewInput::show(SkShow* pSkShow, Sint64 iCurMTime) {
	/**清空屏幕背景为黑色矩形**/
	pSkShow->clear(0x00, 0x00, 0x00, 0xff);
	/**游戏区域背景为蓝色矩形**/
	SkRect backRect;
	backRect.x = 0;
	backRect.y = 0;
	backRect.w = g_SP.m_iBaseWidth;
	backRect.h = g_SP.m_iBaseHeight;
	pSkShow->fillRec(&backRect, 0x00, 0x00, 0xff, 0xff);
	/**返回按钮**/
	pSkShow->drawButton(NULL, &m_backButton, NULL, iCurMTime);

	/**绘制一个白矩形-文字输入区域**/
	SkRect stSkRect;
	stSkRect.x = 50;
	stSkRect.y = 120;
	stSkRect.w = m_strMaxLen + 10;
	stSkRect.h = 80;
	pSkShow->fillRec(&stSkRect, 0xff, 0xff, 0xff, 0xff);
	/**绘制一个红色光标-每秒闪烁一次**/
	if ((iCurMTime / 500) % 2 == 0) {
		stSkRect.x = 50 + m_curWord.getWidth() + 5;
		stSkRect.y = 120 + 5;
		stSkRect.w = 4;
		stSkRect.h = 80 - 10;
		pSkShow->fillRec(&stSkRect, 0xff, 0x00, 0x00, 0xff);
	}
	/**绘制输入**/
	stSkRect.x = 50 + 2;
	stSkRect.y = 120 + 5;
	pSkShow->drawWord(NULL, &m_curWord, &stSkRect, iCurMTime);

	if (m_bEnglish) {
		stSkRect.x = m_bt_english.m_iPosX;
		stSkRect.y = m_bt_english.m_iPosY;
		stSkRect.w = m_bt_english.getWidth();
		stSkRect.h = m_bt_english.getHeight();
		pSkShow->fillRec(&stSkRect, 0xff, 0x00, 0xff, 0xff);
	} else {
		stSkRect.x = m_bt_chinese.m_iPosX;
		stSkRect.y = m_bt_chinese.m_iPosY;
		stSkRect.w = m_bt_chinese.getWidth();
		stSkRect.h = m_bt_chinese.getHeight();
		pSkShow->fillRec(&stSkRect, 0xff, 0x00, 0xff, 0xff);
	}
	pSkShow->drawButton(NULL, &m_bt_english, NULL, iCurMTime);
	pSkShow->drawButton(NULL, &m_bt_chinese, NULL, iCurMTime);

	m_keyboard.show(pSkShow, iCurMTime);
}
void SkViewInput::setShift() {
	m_keyboard.m_iShift = 100 - m_keyboard.m_iShift;
}
void SkViewInput::run(Sint64 iCurMTime) {

}
void SkViewInput::resize() {
	m_keyboard.resize();
}
void SkViewInput::setEnglish(bool bFlag) {
	m_bEnglish = bFlag;
}
void SkViewInput::addInput(string sIn) {
	if (m_vctStr.size() > 0) {
		string sOld = m_vctStr.back();
		sOld.append(sIn);
		m_vctStr.push_back(sOld);
	} else {
		m_vctStr.push_back(sIn);
	}
	m_curWord.setStr(m_vctStr.back(), m_font_ch, 0x00, 0x00, 0x00, 0xff);
	if (m_curWord.getWidth() >= m_strMaxLen) {
		delInput();
	}
}
void SkViewInput::delInput() {
	if (m_vctStr.size() > 0) {
		m_vctStr.pop_back();
	}
	if (m_vctStr.size() > 0) {
		m_curWord.setStr(m_vctStr.back(), m_font_ch, 0x00, 0x00, 0x00, 0xff);
	} else {
		m_curWord.setStr("", m_font_ch, 0x00, 0x00, 0x00, 0xff);
	}
}
void sk_button_keyboard_pressed(void * pData, SkButton * pButton) {
	SkViewInput * pView = (SkViewInput *) pData;
	g_SkComm.log("[%s][%d]key %d:%s", __FILE__, __LINE__, pButton->m_iId,
			pButton->m_sName.c_str());
	if (pButton->m_iId == 38 || pButton->m_iId == 49 || pButton->m_iId == 138
			|| pButton->m_iId == 149) {
		pView->setShift();
	} else if (pButton->m_iId == 1001) {
		pView->setEnglish(true);
	} else if (pButton->m_iId == 1002) {
		pView->setEnglish(false);
	} else if (pButton->m_iId == 36 || pButton->m_iId == 136) {
		pView->delInput();
	} else if (pButton->m_iId == 37 || pButton->m_iId == 137) {

	} else {
		pView->addInput(pButton->m_sName);
	}
}
SkViewInput g_SkViewInput;

效果:


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值