因为游戏的基本操作只有按钮点击。
所有,输入法需要内嵌。而要实现输入法,首先要模拟键盘。
那么我们需要定义一个键盘的View
#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();
void setShift();
private:
SkButton m_testButton;
SkFont m_font;
Keyboard m_keyboard;
};
}
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;
}
//第一行 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) {
for (int i = 0; i < 50; i++) {
bool 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_font.init("font/adobe2.otf", 80);
m_keyboard.init(&m_font, 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_testButton.setShow(pic_ch_1.m_skSurface, pic_ch_1.m_skSurface,
pic_ch_2.m_skSurface);
m_testButton.m_area.addPoint(0, 0);
m_testButton.m_area.addPoint(0, pic_ch_1.m_iHeight);
m_testButton.m_area.addPoint(pic_ch_1.m_iWidth, pic_ch_1.m_iHeight);
m_testButton.m_area.addPoint(pic_ch_1.m_iWidth, 0);
m_testButton.setPosition(600, 100);
m_testButton.setFuncPressed(button_back_to_first);
resize();
}
void SkViewInput::doEvent(SkEvent & stSkEvent, Sint64 iCurMTime) {
bool bRet = m_testButton.handleEvent(m_testButton.m_iPosX,
m_testButton.m_iPosY, stSkEvent, NULL);
if (bRet) {
return;
}
bRet = m_keyboard.doEvent(stSkEvent, iCurMTime, 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);
/**绘制一个红色矩形**/
SkRect stSkRect;
stSkRect.x = 100;
stSkRect.y = 200;
stSkRect.w = 300;
stSkRect.h = 200;
//pSkShow->fillRec(&stSkRect, 0xff, 0x00, 0x00, 0xff);
SkRect stPos;
stPos.x = m_testButton.m_iPosX;
stPos.y = m_testButton.m_iPosY;
pSkShow->drawButton(NULL, &m_testButton, &stPos, 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 sk_button_keyboard_pressed(void * pData, SkButton * pButton) {
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) {
SkViewInput * pView = (SkViewInput *) pData;
pView->setShift();
}
}
SkViewInput g_SkViewInput;
效果: