LCUI_API LCUI_FontBMP* FontLIB_AddFontBMP( wchar_t char_code, int font_id, int pixel_size, LCUI_FontBMP *fontbmp ) { LCUI_FontBMP *bmp_cache; LCUI_RBTree *tree_font, *tree_bmp; if( !fontlib.is_inited ) { return NULL; } /* 获取字符的字体信息集 */ tree_font = (LCUI_RBTree*) RBTree_GetData( &fontlib.bitmap_cache, char_code ); if( !tree_font ) { tree_font = (LCUI_RBTree*)malloc( sizeof(LCUI_RBTree) ); if( !tree_font ) { return NULL; } RBTree_Init( tree_font ); RBTree_SetDataNeedFree( tree_font, TRUE ); RBTree_OnDestroy( tree_font, FontLIB_DestroyTreeNode ); RBTree_Insert( &fontlib.bitmap_cache, char_code, tree_font ); } /* 当字体ID不大于0时,使用内置字体 */ if( font_id <= 0 ) { font_id = fontlib.incore_font->id; } /* 获取相应字体样式标识号的字体位图库 */ tree_bmp = (LCUI_RBTree*)RBTree_GetData( tree_font, font_id ); if( !tree_bmp ) { tree_bmp = (LCUI_RBTree*)malloc( sizeof(LCUI_RBTree) ); if( !tree_bmp ) { return NULL; } RBTree_Init( tree_bmp ); RBTree_OnDestroy( tree_bmp, FontLIB_DestroyFontBitmap ); RBTree_SetDataNeedFree( tree_bmp, TRUE ); RBTree_Insert( tree_font, font_id, tree_bmp ); } /* 在字体位图库中获取指定像素大小的字体位图 */ bmp_cache = (LCUI_FontBMP*)RBTree_GetData( tree_bmp, pixel_size ); if( !bmp_cache ) { bmp_cache = (LCUI_FontBMP*)malloc( sizeof(LCUI_FontBMP) ); if( !bmp_cache ) { return NULL; } RBTree_Insert( tree_bmp, pixel_size, bmp_cache ); } /* 拷贝数据至该空间内 */ memcpy( bmp_cache, fontbmp, sizeof(LCUI_FontBMP) ); return bmp_cache; }
/** 添加已被按下的按键 */ void LCUIKeyboard_HitKey(int key_code) { clock_t ct; KeyStateNode node; LCUIMutex_Lock(&self.mutex); ct = clock() * 1000 / CLOCKS_PER_SEC; node = RBTree_GetData(&self.state_tree, key_code); if (!node) { node = NEW(KeyStateNodeRec, 1); node->interval_time = -1; node->hit_time = ct; node->state = LCUI_KSTATE_PRESSED; RBTree_Insert(&self.state_tree, key_code, node); LCUIMutex_Unlock(&self.mutex); return; } if (node->state == LCUI_KSTATE_RELEASE) { node->state = LCUI_KSTATE_PRESSED; /* 记录与上次此键被按下时的时间间隔 */ node->interval_time = ct - node->hit_time; /* 记录本次此键被按下时的时间 */ node->hit_time = ct; } LCUIMutex_Unlock(&self.mutex); }
/** 获取指定字体ID的字体信息 */ static LCUI_FontInfo* FontLIB_GetFont( int font_id ) { if( !fontlib.is_inited ) { return NULL; } return (LCUI_FontInfo*)RBTree_GetData( &fontlib.info_cache, font_id ); }
/** 标记指定键值的按键已释放 */ void LCUIKeyboard_ReleaseKey(int key_code) { KeyStateNode node; LCUIMutex_Lock(&self.mutex); node = RBTree_GetData(&self.state_tree, key_code); if (node) { node->state = LCUI_KSTATE_RELEASE; } LCUIMutex_Unlock(&self.mutex); }
/** 检测指定键值的按键是否处于按下状态 */ LCUI_BOOL LCUIKeyboard_IsHit(int key_code) { KeyStateNode node; LCUIMutex_Lock(&self.mutex); node = RBTree_GetData(&self.state_tree, key_code); LCUIMutex_Unlock(&self.mutex); if (node && node->state == LCUI_KSTATE_PRESSED) { return TRUE; } return FALSE; }
LCUI_API LCUI_FontBMP* FontLIB_GetFontBMP( wchar_t char_code, int font_id, int pixel_size ) { LCUI_RBTree *tree; if( !fontlib.is_inited ) { return NULL; } tree = (LCUI_RBTree*)RBTree_GetData( &fontlib.bitmap_cache, char_code ); if( !tree ) { return NULL; } if( font_id <= 0 ) { font_id = fontlib.incore_font->id; } tree = (LCUI_RBTree*)RBTree_GetData( tree, font_id ); if( !tree ) { return NULL; } return (LCUI_FontBMP*)RBTree_GetData( tree, pixel_size ); }
/** * 检测指定键值的按键是否按了两次 * @param key_code 要检测的按键的键值 * @param interval_time 该按键倒数第二次按下时的时间与当前时间的最大间隔 */ LCUI_BOOL LCUIKeyboard_IsDoubleHit(int key_code, int interval_time) { clock_t ct; KeyStateNode node; /* 计算当前时间(单位:毫秒) */ ct = clock() * 1000 / CLOCKS_PER_SEC; LCUIMutex_Lock(&self.mutex); node = RBTree_GetData(&self.state_tree, key_code); LCUIMutex_Unlock(&self.mutex); if (!node) { return FALSE; } /* 间隔时间为-1,说明该键是新记录的 */ if (node->interval_time == -1) { return FALSE; } ct -= node->hit_time - node->interval_time; /* 判断按键被按下两次时是否在距当前 interval_time 毫秒的时间内发生 */ if (ct <= interval_time) { return TRUE; } return FALSE; }