《TD开发笔记》滚动字幕自定义控件

免责声明:文中部分信息有参考到其他网站及牛人的资料,在引用到的地方会注明其来源,如有不宜之处可联系本人进行更正或者删除!学术看法及观点仅代表个人,仅供参考。知识共享,共同学习,来源于社会,回馈社会。

目录

1 效果

2 控件实现代码

3 控件源码及DEMO


1 效果

2 控件实现代码

2.1 tdScrollingText.h

#ifndef _TDSCROLLINGTEXT_H_
#define _TDSCROLLINGTEXT_H_

#include <TWidget/TWidget.h>

/*滚动文本*/
#define TD_SCROLLING_TEXT TStringID("scrolling_text")


#endif

2.2 tdScrollingText.c

#include <math.h>
#include <tdScrollingText.h>
#include <TWidget/TWidget_dev.h>

#define DEFAULT_TIMEOUT 200

typedef struct {
	TWidget widget;
	TDisplayCell *vc;
	TwStyle *style;
	TTaskTimer *scroll_timer;
	int speed; //ms
	char *text;
	unsigned int text_len;
	int scroll_index;
	Tbool is_init;
	Tbool is_out;
} TD_SCROLLINGTEXT;

static void scroll_timer_cb(TTaskTimer *ptimer, TTimeout timeout, void *arg)
{
	TFloat r;
	TD_SCROLLINGTEXT *pct = (TD_SCROLLINGTEXT *)arg;
	TwLock();
	
	DcExpose(pct->vc, 0, 0, -1, -1, 1);

	int tw, th, tb, tn;
	Font font = pct->vc->font;
	tn = pct->text_len;
	DcGetTextSize(font, pct->text, tn, &tw, &th, &tb);

	if(pct->is_out){
		pct->scroll_index++;
		if(pct->scroll_index >= tn){
			pct->scroll_index = -(pct->vc->w/(float)(tw/tn));
		}
	}else{
		TTaskEnableTimer(pct->scroll_timer, False);
	}
	DcUpdateShow();

	TwUnlock();
	return;
}

/*根据cur_pos 画图*/
static void scrolling_text_expose(TDisplayCell *pvc, Window win, GC gc, Region clipregion)
{
	TD_SCROLLINGTEXT *pct = (TD_SCROLLINGTEXT *)(pvc->widget);

	int tw, th, tb, tn;
	Font font = pvc->font;

	char *pText = pct->text==NULL?"":pct->text;

	XSetFont(tw_display, gc, font);
	tn = pct->text_len;
	DcGetTextSize(font, pText, tn, &tw, &th, &tb);
	th = pvc->h/2.0 + tb - th/2;

	if(pct->is_init){
		XSetForeground(tw_display, gc, pvc->text_color);

		if(pct->is_out){
			XDrawString(tw_display, win, gc, pvc->x-pct->scroll_index*(tw/tn), pvc->y+th, pText, tn);
		}else{
			int deltaX = (pvc->w - tw)/2;
			XDrawString(tw_display, win, gc, pvc->x+deltaX, pvc->y+th, pText, tn);
		}
	}

	pct->is_init = True;

	return;
}

static Tbool scrolling_text_set_value(TWidget *widget, int value)
{
	TD_SCROLLINGTEXT *pct = (TD_SCROLLINGTEXT *)widget;

	if(value < 0)
		pct->speed = 0;
	else
		pct->speed = value;
	TTaskChangeTimeout(pct->scroll_timer,pct->speed);

	DcExpose(pct->vc, 0, 0, -1, -1, 1);
	return TRUE;
}

static void scrolling_text_event_handler(TwEvent *event, TWidget *widget, TDisplayCell *vc)
{
	TD_SCROLLINGTEXT *pct = (TD_SCROLLINGTEXT *)widget;
	Tint offy, offtime;

	switch (event->type)
	{
	case TW_EVENT_BUTTON_DOWN:

		break;

	case TW_EVENT_MOUSE_POSITION:

		break;

	case TW_EVENT_BUTTON_UP:

		break;
	}
	return;
}

static void scrolling_text_change_style(TWidget *widget, T_ID style)
{
	TD_SCROLLINGTEXT *pct = (TD_SCROLLINGTEXT *)widget;
	TwStyle *pstyle = _TwFindStyle(TD_SCROLLING_TEXT, widget->style_name);
	if(pct->style != pstyle) {
		pct->style = pstyle;
		_TwSetStyle(pct->vc, pstyle, TW_STATUS_NONE);
	}

	return;
}

static void scrolling_text_set_caption(TWidget *widget, const char *str, int len)
{
	TD_SCROLLINGTEXT *pct = (TD_SCROLLINGTEXT *)widget;
	pct->text_len = len;
	pct->text = str;

	int tw, th, tb, tn;
	Font font = pct->vc->font;
	tn = pct->text_len;
	DcGetTextSize(font, pct->text, tn, &tw, &th, &tb);

	pct->scroll_index = 0;
	if(tw <= pct->widget.w){
		pct->is_out = False;
	}else{
		pct->is_out = True;
	}

	TTaskEnableTimer(pct->scroll_timer, TRUE);
}

const char * scrolling_text_get_caption(TWidget *widget, int *plen)
{
	TD_SCROLLINGTEXT *pct = (TD_SCROLLINGTEXT *)widget;
	*plen = pct->text=NULL?0:pct->text_len;

	return pct->text;
}

/*******************************************************************************************/

static TwMethod scrolling_text_method = {
	.event_handler = scrolling_text_event_handler,
	.set_value = scrolling_text_set_value,
	.change_style = scrolling_text_change_style,
	.set_caption = scrolling_text_set_caption,
	.get_caption = scrolling_text_get_caption
};

static int scrolling_text_obj_create(void *obj, TTable *in)
{
	TDisplayCell *parent_vc;
	TD_SCROLLINGTEXT *pct = (TD_SCROLLINGTEXT*)obj;
	TWidget *widget = (TWidget*)obj;

	TwLock();

	parent_vc = _TwInit(widget, &scrolling_text_method, in);
	if(parent_vc == NULL) {
		TwUnlock();
		return T_FAIL;
	}

	pct->vc = DcCreate(DC_INPUT_WINDOW, parent_vc, widget->cx, widget->cy, widget->w, widget->h, widget);
	pct->style = _TwFindStyle(TD_SCROLLING_TEXT, widget->style_name);
	_TwSetStyle(pct->vc, pct->style, TW_STATUS_NORMAL);

	pct->scroll_index = 0;
	pct->text_len = 0;
	pct->is_init = False;
	pct->is_out = False;
	pct->speed = DEFAULT_TIMEOUT;

	DcSetExposeFunc(pct->vc, scrolling_text_expose);
	XSimpleGrabButton(tw_display, Button1, AnyModifier, DC_WID(pct->vc), False,
		(ButtonPressMask|ButtonReleaseMask|ButtonMotionMask|MonopolizeHintMask), GrabModeTolerate);
	_TwSetFocusVC(widget, pct->vc);

	pct->scroll_timer = TTaskAddTimer(pct->speed, scroll_timer_cb, pct);
	_TwInit_Finish(widget);

	TwUnlock();
	return T_SUCCESS;
}

static void scrolling_text_obj_destroy(void *obj)
{
	TD_SCROLLINGTEXT *pct = (TD_SCROLLINGTEXT*)obj;
	TwLock();
	_TwUnInit((TWidget*)obj);
	DcDestroy(pct->vc);
	TTaskDestroyTimer(pct->scroll_timer);
	TwUnlock();
	return ;
}

static TObjectType scrolling_text_obj_info = {
	.create_func = scrolling_text_obj_create,
	.destroy_func = scrolling_text_obj_destroy,
	.obj_size = sizeof(TD_SCROLLINGTEXT),
	.interface_num = 1,
	.interfaces = {None},
};

static TwStyleNode scrolling_text_style[] = {
	{
	.status_mask = TW_STATUS_NORMAL|TW_STATUS_DISABLE|TW_STATUS_ACTIVE|TW_STATUS_SELECT,
	.flags_mask = HAS_TEXT_COLOR,
	.text_color = T_BGR(255, 255, 255),
	.text_bg_color = T_BGR(128, 128, 128),
	}
};

static void init_scrolling_text(void) __attribute__((constructor));
void init_scrolling_text(void)
{
	scrolling_text_obj_info.interfaces[0] = T_STRING_ID(widget);
	TObjectRegisterType(TD_SCROLLING_TEXT, &scrolling_text_obj_info);
	_TwRegisterStyle(TD_SCROLLING_TEXT, T_ARRAY_NUM(scrolling_text_style), scrolling_text_style);
	return;
}

 

3 控件源码及DEMO

下载地址:https://download.csdn.net/download/w1820020635/14962188

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值