自己动手写GUI

最近用喜欢还是一款开源的2D图形库(SDL 2.0)支持硬件纹理加速而且跨平台。这个库还是挺简单的,资料很多,大概一天就能上手。 然后我就基于这套库写起了我的GUI,前面废话直接跳过,(●'◡'●)

先上两张图:

SDL 2.0 下载地址 点击打开链接

一个很不错的入门教程 点击打开链接

好了继续继续 (●'◡'●)

第一次尝试写gui 也没啥思路,那就简单模仿一下HTML的方式来吧。

HTML 的布局主要是由div(层) 

然后HTML5中给DIV标签加入 contenteditable="true" 这个属性,div 就支持文字的输入

然后我就简单的把这个功能模仿了一下下。。大佬勿喷 (●'◡'●)

目前,只写了三个类
CManage 管理类
主要负责窗口的消息处理,以及CDiv(控件)的渲染
CDiv 控件类
现在也只有这一个控件,这个类的事情也很简单,就是管理一个主纹理,就是看到呢那个矩形 
CFont 类

每个CDiv里面都有一个CFont 对象,用于显示文字,文字的插入删除,行高,间距,排序,等都在这个类里面

先看看CManage 管理类的头文件

#ifndef __CMANAGE__
#define __CMANAGE__
#include "All.h"
#include "CDiv.h"
using namespace std;
class CManage {
private:
	SDL_Window *		m_pWindow = NULL;
	SDL_Renderer*		m_pRenderer = NULL;

	std::vector<CDiv*>m_lsDiv;
private:
	std::string title;	//窗口名字
	int Width;
	int Height;
	bool flage_quit = false;

private:

	int find_nDiv(int x,int y);				//通过坐标查找元素,返回元素索引
	void TextInput(char* text);				//输入字符

	void Mousebuttondown(int index,SDL_Event e);					//鼠标按下
	void Press_Del(SDL_Event e);									//删除字符
	void Press_Left( SDL_Event e);									//按下键盘left
	void Press_Right(SDL_Event e);									//按下键盘Right
	void Press_Enter(SDL_Event e);									//回车键

public:
	bool Add_Div(CDiv* Div);
	SDL_Renderer * get_Renderer() { return m_pRenderer; };
	void InitObject();		//初始化对象
	void Rendering();		//渲染
	
public:
	void Error();
	int Init(const std::string t,int w,int h);			//初始化
	int Message();			//消息循环
	int Run();				//运行
	CManage();
	~CManage();
};

#endif // !1

其实就是实现了窗口的创建,消息处理,m_lsDiv就是被添加进来管理的对象,当获得鼠标点击消息的时候,查询有没有CDiv 在这个坐标上,如果有那么将这个CDiv 设置得到焦点。

edit 就是当某CDiv得到焦点之后,然后设置了他允许输入标位置,通过接受输入法输入的字符添加到此CDiv的CFont中。

#include "CManage.h"

bool CManage::Add_Div(CDiv* Div)
{
	if (Div == NULL)return false;
	m_lsDiv.push_back(Div);
	return true;
}

//初始化对象
void CManage::InitObject()
{
	for (unsigned int i = 0; i < m_lsDiv.size(); i++)
	{
		m_lsDiv[i]->Create();
	}
	prepareForRendering(m_pRenderer);
}
//渲染
void CManage::Rendering()
{
	for (unsigned int i = 0; i < m_lsDiv.size(); i++)
	{
		m_lsDiv[i]->display();
	}
}

void CManage::Error()
{
	std::cout << SDL_GetError()<<std::endl;
}
//初始化
int CManage::Init(const std::string t,int w, int h)
{
	title = t;
	Width = w;
	Height = h;
	if (SDL_Init(SDL_INIT_EVERYTHING)!=0) 
	{
		Error();
		return -1;
	}

	if (TTF_Init() != 0)			//初始化字体
	{
		std::cout << TTF_GetError() << endl;
		SDL_Quit();
	}


	m_pWindow = SDL_CreateWindow(title.c_str(), SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, Width, Height, SDL_WINDOW_SHOWN);
	if (m_pWindow == NULL) 
	{
		Error();
		return -2;
	}
	m_pRenderer = SDL_CreateRenderer(m_pWindow, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);

	if (m_pRenderer == NULL) 
	{
		Error();
		SDL_DestroyWindow(m_pWindow);
		SDL_Quit();
		return -3;
	}

	

	//InitObject();

	
	return 0;
}
//消息循环
int CManage::Message() 
{
	return 0;
}

//运行

int CManage::Run()
{
	SDL_Event e;

	

	SDL_StartTextInput();
	while (!flage_quit)
	{
		while (SDL_PollEvent(&e))
		{
			int x = e.motion.x;
			int y = e.motion.y;

			int index = find_nDiv(x, y);

			if (e.type == SDL_TEXTINPUT)
			{
				//strcat(text, e.text.text);
				TextInput(e.text.text);
			}

			if (e.type == SDL_TEXTEDITING)
			{
				//TextInput(e.edit.text);
				//cout<<e.edit.text<<endl;
				//cout<<e.edit.start<<endl;
				//cout<< e.edit.length<<endl;
			}

			if (e.type == SDL_QUIT) {	//退出
				flage_quit = true;
			}
			if (e.type == SDL_MOUSEMOTION) {		//鼠标移动
				//std::cout <<"移动  " <<"index: " << index <<" id: "<< m_lsDiv[index]->get_id().c_str()
				//	<<" x: "<<x<<" y: "<< y <<std::endl;
			}
			if (e.type == SDL_MOUSEBUTTONDOWN) {	//鼠标按下
				//std::cout <<"点击  "<< "index: " << index << " id: " << m_lsDiv[index]->get_id().c_str()
				//	<< " x: " << x << " y: " << y << std::endl;
				Mousebuttondown(index,e);
				
			}
			if (e.type == SDL_MOUSEBUTTONUP) {		//鼠标抬起
				//std::cout << "抬起  " << "index: " << index << " id: " << m_lsDiv[index]->get_id().c_str()
				//	<< " x: " << x << " y: " << y << std::endl;
			}

			if (e.type == SDL_KEYDOWN)
			{
				switch (e.key.keysym.sym)
				{
				case SDLK_BACKSPACE:
					
					//cout << "按下删除键";
					Press_Del(e);
					break;
				case SDLK_LEFT:	//右移动处理
					//
					Press_Left(e);
					break;
				case SDLK_RIGHT:
					Press_Right(e);
					break;
				case SDLK_RETURN:
					Press_Enter(e);
					break;
				}
			}

		}
		SDL_RenderClear(m_pRenderer);
		//Draw the texture
		Rendering();
		
		//Update the screen
		SDL_RenderPresent(m_pRenderer);
	}
	return 0;
}

void CManage::Press_Enter(SDL_Event e)	//回车键
{
	for (unsigned int i = 0; i < m_lsDiv.size(); i++)
	{
		if (m_lsDiv[i]->get_focus() == 1)	//如果有焦点
		{
			if (m_lsDiv[i]->isInput())		//检查是否允许输入
			{
				m_lsDiv[i]->in_text(L"\n");	//转成纹理
			}
		}
	}
}

void CManage::Press_Right(SDL_Event e)
{
	for (unsigned int i = 0; i < m_lsDiv.size(); i++)
	{
		if (m_lsDiv[i]->get_focus() == 1)	//如果有焦点
		{
			if (m_lsDiv[i]->isInput())		//检查是否允许输入
			{
				///std::cout << m_lsDiv[i]->get_id().c_str();
				m_lsDiv[i]->m_CFont->set_Cursor_mov_right();
			}
		}
	}
}
void CManage::Press_Left(SDL_Event e)
{

	for (unsigned int i = 0; i < m_lsDiv.size(); i++)
	{
		if (m_lsDiv[i]->get_focus() == 1)	//如果有焦点
		{
			if (m_lsDiv[i]->isInput())		//检查是否允许输入
			{
				m_lsDiv[i]->m_CFont->set_Cursor_mov_left();
			}
		}
	}
}

void CManage::Press_Del(SDL_Event e)
{
	for (unsigned int i = 0; i < m_lsDiv.size(); i++)
	{
		if (m_lsDiv[i]->get_focus() == 1)	//如果有焦点
		{
			if (m_lsDiv[i]->isInput())		//检查是否允许输入
			{
				m_lsDiv[i]->DelText();
			}
		}
	}
	
}

void CManage::TextInput(char* text)
{
	for (unsigned int i = 0; i < m_lsDiv.size(); i++)
	{
		if (m_lsDiv[i]->get_focus() == 1)	//如果有焦点
		{
			if (m_lsDiv[i]->isInput())		//检查是否允许输入
			{
				//std::cout << text;
				if (strlen(text) == 1)	//单字符
				{
					m_lsDiv[i]->in_text(UTF82WCS(text));	//转成纹理
				}
				else 
				{
					std::wstring wc = UTF82WCS(text);

					for (int j = 0; j < wc.length(); j++)
					{
						wchar_t t[2];
						memset(t, 0, sizeof(wchar_t)*2);
						t[0] = wc.at(j);

						m_lsDiv[i]->in_text(t);	//转成纹理
					}
				}

			}
		}
	}

}


void CManage::Mousebuttondown(int index,SDL_Event e)				//鼠标按下
{
	std::cout << "index:"<<index<<"   name:"<< m_lsDiv[index]->get_id().c_str()<<endl;
	for (unsigned int i = 0; i < m_lsDiv.size(); i++)
	{
		if (i == index) {				//获取焦点
			m_lsDiv[i]->set_focus();	
		}
		else
		{
			m_lsDiv[i]->lose_focus();	//失去焦点
		}
	}
}

// 查找元素索引
int CManage::find_nDiv(int x, int y)	
{
	int index = 0;
	for (unsigned int i = 0; i < m_lsDiv.size(); i++)
	{
		int _x = m_lsDiv[i]->get_x();
		int _y = m_lsDiv[i]->get_y();
		
		int _w = m_lsDiv[i]->get_w();
		int _h = m_lsDiv[i]->get_h();
		
		int _ex = _x + _w;
		int _ey = _y + _h;

		if ((x >= _x && x <= _ex) & (y >= _y && y <= _ey)) {
			index = i;
		}
	}

	return index;
}


CManage::CManage() 
{

}
CManage::~CManage() 
{

	for (unsigned int i=0;i<m_lsDiv.size();i++)
	{
		if (m_lsDiv[i] != NULL) {
			delete m_lsDiv[i];
		}
	}
	m_lsDiv.clear();
	SDL_DestroyWindow(m_pWindow);
	SDL_DestroyRenderer(m_pRenderer);

}

然后是CDiv

/*
 *  类名:CDiv 
 *  说明:模仿HTML DIV的元素属性
 *	作者:夏木
 *	Q  Q:3187227064
 *	时间:2018-2-10
 *
 */

#ifndef __CDIV__
#define __CDIV__

#include "All.h"

#include "CFont.h"
#define TTFURL "./ttf/msyh.ttc"

class CDiv
{
public:
	CFont*					m_CFont			= NULL;
private:	//事件
	int						m_flag_focus	= 0;		//焦点标志位

protected:
	SDL_Renderer*			g_pRenderer		= NULL;				
	SDL_Texture*			m_Texture		= NULL;				
	//边框上右下左纹理
	SDL_Texture*			m_border_top	= NULL;
	SDL_Texture*			m_border_right	= NULL;
	SDL_Texture*			m_border_bottom = NULL;
	SDL_Texture*			m_border_left	= NULL;

	SDL_Rect				m_Rect;
	SDL_Color				m_RGBA			= {0,0,0,255};		//div底色
	SDL_Color				m_Border_Color  = {0,0,0,255};		//边框颜色
	std::string				filename		= "";
	std::string				className		= "CDiv";



	bool					m_flag_isInput  = false;
	unsigned char			m_Alpha			= 255;

	std::string				id = "";
public:
	void 				set_id(const std::string& _id) { id = _id; };
	std::string			get_id() { return id; };



	//设置字体标签
	void				set_text(const std::wstring& str);
	void				set_text_color(unsigned char r, unsigned char g, unsigned char b, unsigned char a);
	void				set_text_color(unsigned int rgba);
	void				set_text_line(int line) { m_CFont->line = line; };		//行高
	void				set_text_spacing(int spacing) { m_CFont->spacing = spacing; };	//间距
	void				set_text_all(int size,const std::wstring& str,unsigned int rgba);

	//文本输入相关
	void				set_Allow_input() ;								//允许输入
	void				set_focus();									//设置焦点
	void				lose_focus();									//失去焦点
	int					get_focus() { return m_flag_focus; };			//获取焦点
	bool				isInput() { return m_CFont->getIn(); };			//是否允许输入
	void				in_text(const std::wstring& str);				//输入字符
	void				DelText();										//删除一个字符

	//样式

	// 边框
	// size 边框大小
	// rgba 边框颜色
	void				set_border(int size, unsigned int rgba);			//设置边框


private:
	SDL_TimerID			time_focus_Id = NULL;
	static Uint32 		tim_focus(Uint32 interval, void *param);					//光标处理定时器
public:
	void set_Color(unsigned int rgba);
	void set_Color(unsigned char r, unsigned char g, unsigned char b, unsigned char a);
	void set_Alpha(unsigned char a) { m_Alpha = a; m_RGBA.a = a; };			//设置透明
	void set_Background(const std::string& src);
	
	void set_Width(int w) {	m_Rect.w = w; m_CFont->set_w_width(w);};
	void set_Height(int h) { m_Rect.h = h; };
	void set_X(int x) {	m_Rect.x = x; m_CFont->setPos(m_Rect.x, m_Rect.y);};
	void set_Y(int y) { m_Rect.y = y; m_CFont->setPos(m_Rect.x, m_Rect.y); };
	void set_w_h_x_y(int w, int h, int x, int y) { m_Rect.w = w; m_Rect.h = h; m_Rect.x = x; m_Rect.y = y; m_CFont->set_w_width(w); m_CFont->setPos(m_Rect.x, m_Rect.y); };

	int get_x() { return m_Rect.x; };
	int get_y() { return m_Rect.y; };
	int get_w() { return m_Rect.w; };
	int get_h() { return m_Rect.h; };

	void display();
	void Create();

	CDiv(SDL_Renderer* r);
	~CDiv();
};

#endif
#include "CDiv.h"


void CDiv::Create()
{

	if (filename == "") {
		m_Texture = SDL_CreateTexture(g_pRenderer,
			SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, m_Rect.w, m_Rect.h);

		SDL_SetTextureBlendMode(m_Texture, SDL_BLENDMODE_BLEND);
		fillTexture(g_pRenderer, m_Texture, m_RGBA.r, m_RGBA.g, m_RGBA.b, m_RGBA.a);
		
	}
	else
	{
		SDL_SetTextureBlendMode(m_Texture, SDL_BLENDMODE_BLEND);
		SDL_SetTextureAlphaMod(m_Texture, m_Alpha);
	}

	prepareForRendering(g_pRenderer);
	
}
void CDiv::display()
{
	if (m_Texture != NULL)		//渲染DIV
	{
		SDL_RenderCopy(g_pRenderer, m_Texture, NULL, &m_Rect);
	}

	m_CFont->display();			//字体渲染
}
void CDiv::in_text(const std::wstring& str)				//输入
{
	//std::cout << m_Text.c_str();

	for (int i = 0; i < str.length(); i++)
	{
		wchar_t s[2] = { 0 };
		s[0]= str.at(i);
		SDL_Texture* tex = renderText_in(UnicodeToUtf8(s), TTFURL, m_CFont->m_Text_Rgba, m_CFont->m_font_size,g_pRenderer);
		SDL_Rect rect;
		SDL_QueryTexture(tex, NULL, NULL, &rect.w, &rect.h);
		m_CFont->addNode(tex, rect, str.at(0));
	}

	
}

void CDiv::DelText()									//删除一个字符
{
	
	m_CFont->del_Node();
	return;
}

void CDiv::set_Allow_input()									//允许输入
{
	
	
	m_CFont->set_allow_Input();	//设置允许输入

	//开启一个光标控制定时器
	Uint32 delay = (33 / 10) * 100;  
	Uint32 time;
	//定时器ID  
	time_focus_Id = SDL_AddTimer(delay, tim_focus, &time);
	if (time_focus_Id == NULL)
	{
		std::cout << SDL_GetError() << std::endl;
		return;
	}
};


void CDiv::set_focus()	//设置焦点
{
	m_flag_focus = 1;
};				
void CDiv::lose_focus() //失去焦点
{
	m_flag_focus = 0;
};


 Uint32 CDiv::tim_focus(Uint32 interval, void *param)
{
	 m_flag_isDisoplayUrsor = !m_flag_isDisoplayUrsor;		//光标显示控制
	 return interval;
}

void CDiv::set_Background(const std::string& src)
{
	filename = src;
	m_Texture = IMG_LoadTexture(g_pRenderer, src.c_str());
}

//设置颜色
void CDiv::set_Color(unsigned int rgba)
{
	m_RGBA.r = rgba >> 24 & 0xFF;
	m_RGBA.g = rgba >> 16 & 0xFF;
	m_RGBA.b = rgba >> 8 & 0xFF;
	m_RGBA.a = rgba >> 0 & 0xFF;

}
void CDiv::set_Color(unsigned char r, unsigned char g, unsigned char b, unsigned char a)
{
	m_RGBA.r = r;
	m_RGBA.g = g;
	m_RGBA.b = b;
	m_RGBA.a = a;
}

void CDiv::set_text(const std::wstring& str)
{
	in_text(str);
}

void CDiv::set_text_color(unsigned char r, unsigned char g, unsigned char b, unsigned char a)
{
	m_CFont->m_Text_Rgba.r = r;
	m_CFont->m_Text_Rgba.g = g;
	m_CFont->m_Text_Rgba.b = b;
	m_CFont->m_Text_Rgba.a = a;
}
void CDiv::set_text_color(unsigned int rgba)
{
	m_CFont->m_Text_Rgba.r = rgba >> 24 & 0xFF;
	m_CFont->m_Text_Rgba.g = rgba >> 16 & 0xFF;
	m_CFont->m_Text_Rgba.b = rgba >> 8 & 0xFF;
	m_CFont->m_Text_Rgba.a = rgba >> 0 & 0xFF;
}
void CDiv::set_text_all(int size, const std::wstring& str, unsigned int rgba)
{
	set_text_color(rgba);	//设置字符串颜色
	m_CFont->m_font_size = 12;
	set_text(str);			//显示的字符串	
}

//边框
void CDiv::set_border(int size, unsigned int rgba)			
{
	m_Border_Color.r = rgba >> 24 & 0xFF;
	m_Border_Color.g = rgba >> 16 & 0xFF;
	m_Border_Color.b = rgba >> 8 & 0xFF;
	m_Border_Color.a = rgba >> 0 & 0xFF;
	
	if (m_border_top == NULL)
	{
		m_border_top = create_rectangle(m_Rect.w, size, g_pRenderer, m_Border_Color);
	}

	if (m_border_right == NULL)
	{
		m_border_right = create_rectangle(m_Rect.w, size, g_pRenderer, m_Border_Color);
	}

	if (m_border_bottom == NULL)
	{
		m_border_bottom = create_rectangle(m_Rect.w, size, g_pRenderer, m_Border_Color);
	}

	if (m_border_left == NULL)
	{
		m_border_left = create_rectangle(m_Rect.w, size, g_pRenderer, m_Border_Color);
	}

}


CDiv::CDiv(SDL_Renderer* r)
{
	m_RGBA.r = 255;
	m_RGBA.g = 255;
	m_RGBA.b = 255;
	m_RGBA.a = 255;
	g_pRenderer = r;

	m_Rect.x = 0;
	m_Rect.y = 0;
	m_Rect.w = 100;
	m_Rect.h = 100;

	m_CFont = new CFont(g_pRenderer);
	m_CFont->setPos(m_Rect.x, m_Rect.y);
	m_CFont->m_font_size = 12;
	m_CFont->line = 0;
	m_CFont->spacing = 0;
}
CDiv::~CDiv()
{

}

CFont

#ifndef __CFONT__
#define __CFONT__
#include "All.h"


class CFont
{ 
private:
	SDL_Renderer*				g_pRenderer = NULL;
	std::vector<SDL_Texture*>	m_lsFont;
	std::vector<SDL_Rect>		m_lsRect;
	SDL_Texture*				m_Texture_Cursor = NULL;		//光标纹理
	
	

	SDL_Rect					m_Rect_Cursor;					//光标

	
	int							x;
	int							y;
	
	int							x_width = 100;					//最大宽度
	int							length	= 0;					//字符长度

	bool						isInput = false;				//是否允许输入

	int							NumberLine = 0;					//行数


	std::wstring str;		//
public:
	SDL_Color					m_Text_Rgba = { 250,100,100,255 };
	int							line = 5;						//默认行间
	int							m_font_size = 14;				//字体大小
	int							spacing = 2;					//间距
private:
	void row(int index);							//字体位置排序
	int Find_Pos();									//查找光标的映射位置
	void change_Pos(int index);						//修改坐标

	void set_cPos(int index);						//设置光标位置
public:
	//添加一个字体节点
	//index 插入的位置
	void addNode(SDL_Texture* texture, SDL_Rect re, wchar_t strx);


	void del_Node();					//删除光标位置后面一个节点

	void set_Cursor_pos(int x,int y);	//鼠标点击某位置			
	void set_Cursor_mov_left();			//光标向左移动一个字符
	void set_Cursor_mov_right();		//光标向右移动一个字符
	void set_Cursor_mov_up();			//光标向上移动一个字符
	void set_Cursor_mov_down();			//光标向下移动一个字符
	
	//设置起点
	void setPos(int _x, int _y) {
		x = _x; y = _y; 
		m_Rect_Cursor.x = _x; m_Rect_Cursor.y = _y;
	};

	void set_allow_Input() { isInput = true; };	//允许输入

	bool getIn() { return isInput; };

	void set_w_width(int w) { x_width = w; };	//设置最大宽度

	

	//显示
	void display();

	CFont(SDL_Renderer* rend);
	~CFont() {};
};

#endif





#include "CFont.h"



CFont::CFont(SDL_Renderer* rend) {
	g_pRenderer = rend; 	//创建光标纹理
	SDL_Color rx = { 10,10,10,255 };
	m_Texture_Cursor = create_rectangle(1, m_font_size, g_pRenderer, rx);
	m_Rect_Cursor.w = 1;
	m_Rect_Cursor.h = m_font_size;
};

void CFont::addNode(SDL_Texture* texture, SDL_Rect re, wchar_t strx)
{
	//str += strx;
	//查表,找光标位置
	int index = Find_Pos();

	//插入数据
	m_lsFont.insert(m_lsFont.begin() + index, texture);
	m_lsRect.insert(m_lsRect.begin() + index, re);
	str.insert(str.begin() + index, strx);

	//修改当前插入数据的坐标
	change_Pos(index);
	length++;

	m_Rect_Cursor.h = re.h;
}
void CFont::change_Pos(int index)			//修改坐标
{
	//如果是开始位置
	if (index == 0)
	{
		wchar_t h = L'\n';
		if (str.at(index) == L'\n')
		{
			std::cout << "x换行符x";
		}

		m_lsRect[index].x = x;
		m_lsRect[index].y = y;

		m_Rect_Cursor.x = x + m_lsRect[index].w + spacing;
		m_Rect_Cursor.y = y;

		//row(index);
		//std::cout <<"xxx"<< m_lsRect[index].x << std::endl;
		row(index-1);
		m_Rect_Cursor.x = m_lsRect[index].x + m_lsRect[index].w;
		m_Rect_Cursor.y = m_lsRect[index].y;
		return;
	}

	//其他位置
	m_lsRect[index].x = m_Rect_Cursor.x + spacing;
	m_lsRect[index].y = m_Rect_Cursor.y;

	row(index);
	//set_cPos(index+2);
	m_Rect_Cursor.x = m_lsRect[index].x + m_lsRect[index].w;
	m_Rect_Cursor.y = m_lsRect[index].y;

}
int CFont::Find_Pos()
{
	int index = 0;
	//情况1:如果光标的坐标都位0
	if (m_Rect_Cursor.x == x && m_Rect_Cursor.y == y) {
		index = 0;
	}
	else {
		//查找和光标一致的地方
		for (int i = 0; i < m_lsFont.size(); i++)
		{
			if (m_Rect_Cursor.x == (m_lsRect[i].x + m_lsRect[i].w) && m_Rect_Cursor.y == m_lsRect[i].y)
			{
				index = i + 1;
				break;
			}
		}
		if (index == 0) {
			index = m_lsFont.size();
		}
	}
	return index;
}

void CFont::row(int index)						//字体位置排序
{
	m_lsRect[0].x = x;
	m_lsRect[0].y = y + line;

	int i = 1;
	int n = 0;
	for (i; i < m_lsRect.size(); i++)
	{
		//设置字符x坐标
		//当前字符x坐标等于上一个字符的坐标加宽度加间距
		m_lsRect[i].x = m_lsRect[i - 1].x + m_lsRect[i-1].w + spacing;

		//设置字符y坐标
		//当前字符坐标加宽度加间距超过iedt宽度则换行
		if ((m_lsRect[i].x + m_lsRect[i].w + spacing) >= x_width + x) {
			m_lsRect[i].x = x;
			n++;
			m_lsRect[i].y = (y  + (m_lsRect[i].h + line) * n) ;
		}
		else
		{
			m_lsRect[i].y = m_lsRect[i - 1].y;
		}

	}

	//设置光标位置到当前
	set_cPos(index);

}
void CFont::display()
{
	for (int i = 0; i < m_lsFont.size(); i++)
	{
		SDL_RenderCopy(g_pRenderer,m_lsFont[i] , NULL, &m_lsRect[i]);
	}

	

	if (isInput==true) //渲染光标
	{
		if (m_flag_isDisoplayUrsor) {
			SDL_RenderCopy(g_pRenderer, m_Texture_Cursor, NULL, &m_Rect_Cursor);
		}
		//std::cout << "m_Rect_Cursor:" << m_Rect_Cursor.x << "  " << m_Rect_Cursor.y << std::endl;

	}


}

void CFont::del_Node()					//删除光标位置后面一个节点
{
	//查表,找光标位置
	int index = Find_Pos();
	//插入数据

	if (index > 1)
	{
		SDL_DestroyTexture(m_lsFont[index - 1]);		//清除纹理
		m_lsFont.erase(m_lsFont.begin() + index - 1);
		m_lsRect.erase(m_lsRect.begin() + index - 1);
		str.erase(str.begin() + index - 1);
		length--;
		row(index - 2);
	}
	if (index == 1) {
	//	std::cout << index << std::endl;
		SDL_DestroyTexture(m_lsFont[index - 1]);		//清除纹理
		m_lsFont.erase(m_lsFont.begin() + index - 1);
		m_lsRect.erase(m_lsRect.begin() + index - 1);
		str.erase(str.begin() + index - 1);
		length--;
		//row(index);
		m_Rect_Cursor.x = x;
		m_Rect_Cursor.y = y;
	}
	//std::cout << length << std::endl;
}
void set_Cursor_pos(int x, int y);	//鼠标点击某位置			
void CFont::set_Cursor_mov_left()			//光标向左移动一个字符
{
	//std::cout << "光标移动";
	int index = Find_Pos()-2;
	std::cout << index << std::endl;
	set_cPos(index);
}
void CFont::set_Cursor_mov_right()		//光标向右移动一个字符
{
	int index = Find_Pos();
	std::cout << index << std::endl;
	set_cPos(index);
	
}
void set_Cursor_mov_up();			//光标向上移动一个字符
void set_Cursor_mov_down();			//光标向下移动一个字符

void CFont::set_cPos(int index)					//设置光标位置
{
	if (index < 0)
	{
		m_Rect_Cursor.x = x;
		m_Rect_Cursor.x = y + line;
		return;
	}
	if (index >= length)
	{
		m_Rect_Cursor.x = m_lsRect[length-1].x + m_lsRect[length-1].w;
		m_Rect_Cursor.y = m_lsRect[length-1].y;
		return;
	}
	

	m_Rect_Cursor.x = m_lsRect[index].x + m_lsRect[index].w;
	m_Rect_Cursor.y = m_lsRect[index].y;
}

完整项目工程下载

  • 4
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

夏mu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值