SDL游戏之路(十八)--文本输入框



文本输入框

为了方便游戏界面绘制,可以使用一些配置文件来初始化组件的展示。

配置文件定义如下:

按钮:

<?xml version="1.0" encoding="utf-8"?>
<root>
	<bt>
		<!-- 按钮类型: 1:文字按钮  2:图片按钮 -->
		<type>1</type>
	</bt>
	<pos>
		<x></x>
		<y></y>
	</pos>
	<type1>
		<font_size></font_size>
		<color_in></color_in>
		<color_out></color_out>
		<color_pressed></color_pressed>
		<str_in></str_in>
		<str_out></str_out>
		<str_pressed></str_pressed>
		<!-- 是否有背景色 -->
		<bg_color>1</bg_color>
		<bg_in></bg_in>
		<bg_out></bg_out>
		<bg_pressed></bg_pressed>
	</type1>
	<type2>
		<width></width>
		<height></height>
		<pic_in></pic_in>
		<pic_out></pic_out>
		<pic_pressed></pic_pressed>
	</type2>
</root>


文本输入框:

<?xml version="1.0" encoding="utf-8"?>
<root>
	<tf>
		<!-- 按钮类型: 1:普通输入框 2:密码输入框 -->
		<type>1</type>
		<font_size></font_size>
		<font_color></font_color>
		<cursor_color></cursor_color>
		<max_types></max_types>
	</tf>
	<bg>
		<w></w>
		<h></h>
		<color></color>
	</bg>
	<pos>
		<x></x>
		<y></y>
	</pos>
</root>

输入框要实现的功能就是:

点击输入框后,跳转显示输入法的界面。点击返回后,将内容写入到输入框中。

输入框,继承按钮来实现:

#ifndef SKTXTFIELD_H_
#define SKTXTFIELD_H_

#include "SkComm.h"
#include "SkConfXml.h"
#include "SkButton.h"
#include "SkWord.h"
#include "SkString.h"

namespace sk_park {

class SkTxtField: public SkButton {
public:
	SkTxtField();
	~SkTxtField();
	void initConf(SkConfXml & conf);
	void setString(const char * pStr);

	/**是否密码输入框**/
	bool m_bPass;
	/** 背景颜色**/
	SkColor m_bgColor;
	/** 背景大小**/
	Sint32 m_iWidth;
	Sint32 m_iHeight;
	/** 光标颜色**/
	SkColor m_cursorColor;
	/** 字体大小**/
	Sint32 m_iFontSize;
	/** 字体颜色**/
	SkColor m_fontColor;
	/** 最大字节数**/
	Sint32 m_iMaxBytes;
	/**当前输入框内容**/
	SkWord m_curWord;
	/**当前输入框Str**/
	SkString m_inputStr;
};

}
extern sk_park::SkTxtField g_SkTxtField;

#endif /* SKTXTFIELD_H_ */


#include "pch.h"

#include "SkTxtField.h"
#include "../sk_src/SkFunction.h"

using namespace sk_park;

SkTxtField::SkTxtField() {
	m_bPass = false;
}
SkTxtField::~SkTxtField() {

}
void SkTxtField::initConf(SkConfXml & conf) {
	int iType = conf.getKKValueInt32("tf", "type");
	if (iType == 2) {
		m_bPass = true;
	}
	m_iPosX = conf.getKKValueInt32("pos", "x");
	m_iPosY = conf.getKKValueInt32("pos", "y");

	SkString sColor;
	sColor = conf.getKKValueStr("bg", "color");
	g_SkComm.setColorFromStr(sColor, m_bgColor);

	m_iWidth = conf.getKKValueInt32("bg", "w");
	m_iHeight = conf.getKKValueInt32("bg", "h");

	sColor = conf.getKKValueStr("tf", "cursor_color");
	g_SkComm.setColorFromStr(sColor, m_cursorColor);

	sColor = conf.getKKValueStr("tf", "font_color");
	g_SkComm.setColorFromStr(sColor, m_fontColor);

	m_iFontSize = conf.getKKValueInt32("tf", "font_size");
	m_iMaxBytes = conf.getKKValueInt32("tf", "max_types");

	this->m_area.clear();
	this->m_area.addPoint(0, 0);
	this->m_area.addPoint(0, m_iHeight);
	this->m_area.addPoint(m_iWidth, m_iHeight);
	this->m_area.addPoint(m_iWidth, 0);

	this->setFuncPressed(button_input_pressed);
}
void SkTxtField::setString(const char * pStr) {
	string str = pStr;
	if ((int) str.length() > m_iMaxBytes) {
		str = string(pStr, m_iMaxBytes);
	}
	m_inputStr = str.c_str();
	SkFont font;
	font.init("font/simhei.ttf", m_iFontSize);
	m_curWord.setStr(str, font, m_fontColor.r, m_fontColor.g, m_fontColor.b,
			m_fontColor.a, m_bPass);
}

SkTxtField g_SkTxtField;


按钮点击事件:

void button_input_pressed(void * pData, SkButton * pButton) {
	SkViewInput * pView = new SkViewInput;
	pView->init("conf/view/SkViewInput.conf.xml");
	pView->setReturn(g_SkGame.getCurView(), (SkTxtField *) pButton);
	g_SkGame.setCurView(pView);
}

void button_back_to_first(void * pData, SkButton * pButton) {
	SkViewInput * pInputView = (SkViewInput *) g_SkGame.getCurView();
	pInputView->setRetStr();
	SkView * pView = (SkView *) pData;
	g_SkGame.setCurViewAndDelOldView(pView);
	//ViewFirst * pView = new ViewFirst;
	//pView->init("conf/view/ViewFirst.conf.xml");
	//g_SkGame.setCurViewAndDelOldView(pView);
}


实现如下:


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值