EditCtrl.cpp
上传用户:maxiaolivb
上传日期:2022-06-07
资源大小:915k
文件大小:4k
- #include ".editctrl.h"
- EditCtrl::EditCtrl(int nId, float fPosX, float fPosY, float fWidth, float fHeight, DWORD dwTextColor, DWORD dwFrameColor)
- {
- id = nId;
- bStatic = false;
- bVisible = true;
- bEnabled = true;
- rect.Set(fPosX, fPosY, fPosX + fWidth, fPosY + fHeight);
- m_nCurPos = 0;
- m_fTime = 0;
- m_bFocus = false;
- m_bBlink = false;
- m_bComposition = false;
- m_dwTextColor = dwTextColor;
- m_dwFrameColor = dwFrameColor;
- strcpy(m_szText,"");
- ClearCharArray();
- }
- EditCtrl::~EditCtrl(void)
- {
- ClearCharArray();
- }
- // 清除字符池
- void EditCtrl::ClearCharArray()
- {
- size_t size = m_pCharArray.size();
- for (size_t i=0;i<size;i++)
- {
- if (m_pCharArray[i])
- delete m_pCharArray[i];
- }
- m_pCharArray.clear();
- }
- void EditCtrl::Render()
- {
- hge->Gfx_RenderLine(rect.x1-1, rect.y1-1, rect.x2+1, rect.y1-1, m_dwFrameColor);
- hge->Gfx_RenderLine(rect.x2+1, rect.y1-1, rect.x2+1, rect.y2+1, m_dwFrameColor);
- hge->Gfx_RenderLine(rect.x2+1, rect.y2+1, rect.x1-1, rect.y2+1, m_dwFrameColor);
- hge->Gfx_RenderLine(rect.x1-1, rect.y2+1, rect.x1-1, rect.y1-1, m_dwFrameColor);
- DWORD dwLastColor = g_pFontCN->GetColor();
- g_pFontCN->SetColor(m_dwTextColor);
- g_pFontCN->Render(rect.x1, rect.y1 + 2, m_szText);
- g_pFontCN->SetColor(dwLastColor);
- if (m_bFocus && m_bBlink)
- RedrawCursor();
- }
- void EditCtrl::Update(float dt)
- {
- if (m_bFocus)
- {
- m_fTime += dt;
- if (m_fTime > 0.3)
- {
- m_fTime = 0;
- m_bBlink = !m_bBlink;
- }
- }
- }
- void EditCtrl::Focus(bool bFocused)
- {
- m_bFocus = bFocused;
- }
- bool EditCtrl::KeyClick(int key, int chr)
- {
- if ( m_bFocus && !IsComposition())
- {
- int size = (int)m_pCharArray.size();
- switch (key)
- {
- case HGEK_ENTER:
- case HGEK_TAB:
- break;
- case HGEK_HOME:
- m_nCurPos = 0;
- break;
- case HGEK_END:
- m_nCurPos = size;
- break;
- case HGEK_LEFT:
- if (m_nCurPos > 0)
- m_nCurPos--;
- break;
- case HGEK_RIGHT:
- if (m_nCurPos < size)
- m_nCurPos++;
- break;
- case HGEK_DELETE:
- DelChar(m_nCurPos);
- break;
- case HGEK_BACKSPACE:
- if (m_nCurPos == 0)
- break;
- DelChar(--m_nCurPos);
- break;
- default:
- {
- if (chr == 0 || IsComposition())
- break;
- AddChar(chr,m_nCurPos++);
- UpdataString();
- }
- break;
- };
- m_bBlink = true;
- m_fTime = 0;
- }
- return false;
- }
- // 开始组字状态
- void EditCtrl::StartComposition()
- {
- m_bComposition = true;
- }
- // 结束组字状态
- void EditCtrl::EndComposition()
- {
- m_bComposition = false;
- UpdataString();
- };
- void EditCtrl::OnImeCharMsg(const char *szImeChar)
- {
- if (m_bFocus)
- AddCharEx(szImeChar,m_nCurPos++);
- }
- // 添加一个新的IME字符
- bool EditCtrl::AddCharEx(const char *szNewChar,int nPos)
- {
- st_Char *_pChar = new st_Char;
- if (!_pChar)
- return false;
- if (strlen(szNewChar)>2 || strlen(szNewChar)==0)
- return false;
- _pChar->bIME = (strlen(szNewChar)==1)?false:true;
- strcpy(_pChar->szChar,szNewChar);
- if (nPos < 0) //追加
- {
- m_pCharArray.push_back(_pChar);
- }
- else //根据指定位置插入
- {
- size_t size = m_pCharArray.size();
- if (nPos > (int)size) //末尾
- AddCharEx(szNewChar);
- else
- {
- vector <st_Char *>::iterator it;
- it = &m_pCharArray[nPos];
- m_pCharArray.insert(it,_pChar);
- }
- }
- return true;
- }
- // 添加一个普通的字符,将普通字符转为IME字符
- bool EditCtrl::AddChar(const char szNewChar,int nPos)
- {
- char szBuf[3];
- szBuf[0] = szNewChar;
- szBuf[1] = ' ';
- return AddCharEx(szBuf,nPos);
- }
- // 删除一个指定位置的字符
- bool EditCtrl::DelChar(int nPos)
- {
- size_t size = m_pCharArray.size();
- if (nPos < (int)size && nPos >= 0)
- {
- if (m_pCharArray[nPos])
- {
- m_pCharArray.erase(&m_pCharArray[nPos]);
- UpdataString();
- return true;
- }
- }
- return false;
- }
- // 重绘光标
- void EditCtrl::RedrawCursor()
- {
- if (IsComposition())
- return;
-
- size_t size = m_pCharArray.size();
- if (size >= 0)
- {
- int nCurPos = 0;
- for (int i = 0;i < m_nCurPos;i++)
- {
- st_Char *_pChar = m_pCharArray[i];
- nCurPos += (_pChar->bIME?12:ASCII_WIDTH);
- }
- hge->Gfx_RenderLine(rect.x1 + nCurPos, rect.y1 + 1, rect.x1 + nCurPos, rect.y2 - 1, m_dwTextColor);
- }
- }
- // 刷新字符串数据
- void EditCtrl::UpdataString()
- {
- size_t size = m_pCharArray.size();
- strcpy(m_szText,"");
- if (size >= 0)
- {
- for (size_t i = 0;i < size;i++)
- {
- st_Char *_pChar = m_pCharArray[i];
- if (_pChar)
- strcat(m_szText,_pChar->szChar);
- }
- }
- }