📄 gwidgets.cpp
字号:
if(m_dirty) Update(); pOutRect->x = 0; pOutRect->y = 0; pOutRect->w = m_image.GetWidth(); pOutRect->h = m_image.GetHeight(); return &m_image;}void GWidgetTextLabel::SetSize(int w, int h){ m_rect.w = w; m_rect.h = h; m_image.SetSize(w, h); m_dirty = true;}void GWidgetTextLabel::SetText(GString* pText){ m_text.Copy(pText); m_dirty = true; Draw(NULL);}void GWidgetTextLabel::SetText(const char* szText){ m_text.Copy(szText); m_dirty = true; Draw(NULL);}// ----------------------------------------------------------------------GWidgetVCRButton::GWidgetVCRButton(GWidgetGroup* pParent, int x, int y, int w, int h, VCR_Type eType): GWidgetAtomic(pParent, x, y, w, h){ m_image.SetSize(w * 2, h); m_eType = eType; m_pressed = false; m_dirty = true;}/*virtual*/ GWidgetVCRButton::~GWidgetVCRButton(){}/*virtual*/ void GWidgetVCRButton::Grab(int x, int y){ m_pressed = true; Draw(NULL); m_pParent->OnPushVCRButton(this);}/*virtual*/ void GWidgetVCRButton::Release(){ m_pressed = false; Draw(NULL);}void GWidgetVCRButton::DrawIcon(int nHorizOfs, int nVertOfs){ int nMinSize = m_image.GetWidth() / 2; if(nMinSize > (int)m_image.GetHeight()) nMinSize = m_image.GetHeight(); int nArrowSize = nMinSize / 3; int hh = m_image.GetHeight() / 2; int n; if(m_eType == ArrowRight) { for(n = 0; n < nArrowSize; n++) m_image.DrawLine(nHorizOfs + hh - nArrowSize / 2 + n, nVertOfs + hh - nArrowSize + n + 1, nHorizOfs + hh - nArrowSize / 2 + n, nVertOfs + hh + nArrowSize - n - 1, 0); } else if(m_eType == ArrowLeft) { for(n = 0; n < nArrowSize; n++) m_image.DrawLine(nHorizOfs + hh + nArrowSize / 2 - n, nVertOfs + hh - nArrowSize + n + 1, nHorizOfs + hh + nArrowSize / 2 - n, nVertOfs + hh + nArrowSize - n - 1, 0); } if(m_eType == ArrowDown) { for(n = 0; n < nArrowSize; n++) m_image.DrawLine(nHorizOfs + hh - nArrowSize + n + 1, nVertOfs + hh - nArrowSize / 2 + n, nHorizOfs + hh + nArrowSize - n - 1, nVertOfs + hh - nArrowSize / 2 + n, 0); } else if(m_eType == ArrowUp) { for(n = 0; n < nArrowSize; n++) m_image.DrawLine(nHorizOfs + hh - nArrowSize + n + 1, nVertOfs + hh + nArrowSize / 2 - n, nHorizOfs + hh + nArrowSize - n - 1, nVertOfs + hh + nArrowSize / 2 - n, 0); } else if(m_eType == Square) { m_image.DrawBox(nHorizOfs + hh - nArrowSize, nVertOfs + hh - nArrowSize, nHorizOfs + hh + nArrowSize, nVertOfs + hh + nArrowSize, true, 0); }}void GWidgetVCRButton::Update(){ m_dirty = false; // Draw the non-pressed image int w = m_image.GetWidth() / 2; int h = m_image.GetHeight(); m_pStyle->DrawVertCurvedOutSurface(&m_image, 0, 0, w, h); DrawIcon(0, 0); m_image.DrawBox(0, 0, w - 1, h - 1, gRGB(64, 64, 64), false); // Draw the pressed image int nHorizOfs = (int)(w * (float).05); int nVertOfs = (int)(h * (float).15); m_pStyle->DrawVertCurvedInSurface(&m_image, w, 0, w, h); DrawIcon(nHorizOfs + w, nVertOfs); m_image.DrawBox(w, 0, w + w - 1, h - 1, gRGB(255, 255, 255), false);}GImage* GWidgetVCRButton::GetImage(GRect* pOutRect){ if(m_dirty) Update(); pOutRect->x = m_pressed ? m_image.GetWidth() / 2 : 0; pOutRect->y = 0; pOutRect->w = m_image.GetWidth() / 2; pOutRect->h = m_image.GetHeight(); return &m_image;}void GWidgetVCRButton::SetSize(int w, int h){ m_rect.w = w; m_rect.h = h; m_image.SetSize(w * 2, h); m_dirty = true;}void GWidgetVCRButton::SetType(VCR_Type eType){ m_eType = eType; m_dirty = true;}// ----------------------------------------------------------------------GWidgetProgressBar::GWidgetProgressBar(GWidgetGroup* pParent, int x, int y, int w, int h): GWidgetAtomic(pParent, x, y, w, h){ m_image.SetSize(w, h); m_fProgress = 0; m_dirty = true;}/*virtual*/ GWidgetProgressBar::~GWidgetProgressBar(){}/*virtual*/ void GWidgetProgressBar::Grab(int x, int y){}/*virtual*/ void GWidgetProgressBar::Release(){}void GWidgetProgressBar::Update(){ m_dirty = false; m_image.Clear(0xff000000); int w = m_image.GetWidth(); int h = m_image.GetHeight(); if(m_fProgress > 0) { if(w >= h) m_pStyle->DrawVertCurvedOutSurface(&m_image, 1, 1, (int)(m_fProgress * (w - 2)), h - 2); else { int hgt = (int)(m_fProgress * (h - 2)); m_pStyle->DrawHorizCurvedOutSurface(&m_image, 1, h - 1 - hgt, w - 2, hgt); } } m_image.DrawBox(0, 0, w - 1, h - 1, gRGB(64, 64, 64), false);}GImage* GWidgetProgressBar::GetImage(GRect* pOutRect){ if(m_dirty) Update(); pOutRect->x = 0; pOutRect->y = 0; pOutRect->w = m_image.GetWidth(); pOutRect->h = m_image.GetHeight(); return &m_image;}void GWidgetProgressBar::SetSize(int w, int h){ m_rect.w = w; m_rect.h = h; m_image.SetSize(w, h); m_dirty = true;}void GWidgetProgressBar::SetProgress(float fProgress){ m_fProgress = fProgress; m_dirty = true; Draw(NULL);}// ----------------------------------------------------------------------GWidgetCheckBox::GWidgetCheckBox(GWidgetGroup* pParent, int x, int y, int w, int h): GWidgetAtomic(pParent, x, y, w, h){ m_image.SetSize(w * 2, h); m_checked = false; m_dirty = true;}/*virtual*/ GWidgetCheckBox::~GWidgetCheckBox(){}/*virtual*/ void GWidgetCheckBox::Grab(int x, int y){ // todo: gray the box?}/*virtual*/ void GWidgetCheckBox::Release(){ m_checked = !m_checked; Draw(NULL);}void GWidgetCheckBox::Update(){ m_dirty = false; // Draw the non-checked image int w = m_image.GetWidth() / 2; int h = m_image.GetHeight(); m_image.Clear(0xff000000); m_image.DrawBox(1, 1, w - 2, h - 2, gRGB(64, 128, 128), false); m_image.DrawBox(2, 2, w - 3, h - 3, 0xffffffff, true); // Draw the checked image m_image.DrawBox(w + 1, 1, w + w - 2, h - 2, gRGB(64, 128, 128), false); m_image.DrawBox(w + 2, 2, w + w - 3, h - 3, 0xffffffff, true); m_image.DrawLine(w + 4, 4, w + w - 5, h - 5, 0xff000000); m_image.DrawLine(w + 5, 4, w + w - 5, h - 6, 0xff000000); m_image.DrawLine(w + 4, 5, w + w - 6, h - 5, 0xff000000); m_image.DrawLine(w + w - 5, 4, w + 4, h - 5, 0xff000000); m_image.DrawLine(w + w - 6, 4, w + 4, h - 6, 0xff000000); m_image.DrawLine(w + w - 5, 5, w + 5, h - 5, 0xff000000);}GImage* GWidgetCheckBox::GetImage(GRect* pOutRect){ if(m_dirty) Update(); pOutRect->x = m_checked ? m_image.GetWidth() / 2 : 0; pOutRect->y = 0; pOutRect->w = m_image.GetWidth() / 2; pOutRect->h = m_image.GetHeight(); return &m_image;}void GWidgetCheckBox::SetSize(int w, int h){ m_rect.w = w; m_rect.h = h; m_image.SetSize(w * 2, h); m_dirty = true;}void GWidgetCheckBox::SetChecked(bool checked){ m_checked = checked; m_dirty = true;}// ----------------------------------------------------------------------GWidgetSliderTab::GWidgetSliderTab(GWidgetGroup* pParent, int x, int y, int w, int h, bool vertical, Style eStyle): GWidgetAtomic(pParent, x, y, w, h){ m_image.SetSize(w, h); m_vertical = vertical; m_eStyle = eStyle; m_dirty = true;}/*virtual*/ GWidgetSliderTab::~GWidgetSliderTab(){}/*virtual*/ void GWidgetSliderTab::Grab(int x, int y){ m_pParent->OnClickTab(this);}/*virtual*/ void GWidgetSliderTab::Release(){}/*virtual*/ void GWidgetSliderTab::OnMouseMove(int dx, int dy){ m_pParent->OnSlideTab(this, dx, dy);}void GWidgetSliderTab::Update(){ m_dirty = false; int i, j; if(m_rect.w <= 0 || m_rect.h <= 0) return; if(m_vertical) { switch(m_eStyle) { case ScrollBarTab: m_pStyle->DrawHorizCurvedOutSurface(&m_image, 0, 0, m_rect.w, m_rect.h); break; case ScrollBarArea: m_pStyle->DrawHorizCurvedInSurface(&m_image, 0, 0, m_rect.w, m_rect.h); break; case SliderNub: m_pStyle->DrawHorizCurvedOutSurface(&m_image, 0, 0, m_rect.w, m_rect.h); for(i = m_rect.h / 2; i >= 0; i--) { for(j = m_rect.h / 2 - i; j >= 0; j--) { m_image.SetPixel(m_rect.w - 1 - j, i, 0xff000000); m_image.SetPixel(m_rect.w - 1 - j, m_rect.h - 1 - i, 0xff000000); } } break; case SliderArea: m_pStyle->DrawHorizCurvedInSurface(&m_image, m_rect.w / 4, 0, m_rect.w / 2, m_rect.h); break; default: GAssert(false, "Unexpected case"); } } else { switch(m_eStyle) { case ScrollBarTab: m_pStyle->DrawVertCurvedOutSurface(&m_image, 0, 0, m_rect.w, m_rect.h); break; case ScrollBarArea: m_pStyle->DrawVertCurvedInSurface(&m_image, 0, 0, m_rect.w, m_rect.h); break; case SliderNub: m_pStyle->DrawVertCurvedOutSurface(&m_image, 0, 0, m_rect.w, m_rect.h); for(i = m_rect.w / 2; i >= 0; i--) { for(j = m_rect.w / 2 - i; j >= 0; j--) { m_image.SetPixel(i, j, 0xff000000); m_image.SetPixel(m_rect.w - 1 - i, j, 0xff000000); } } break; case SliderArea: m_pStyle->DrawVertCurvedInSurface(&m_image, 0, m_rect.h / 4, m_rect.w, m_rect.h / 2); break; default: GAssert(false, "Unexpected case"); } }}GImage* GWidgetSliderTab::GetImage(GRect* pOutRect){ if(m_dirty) Update(); pOutRect->x = 0; pOutRect->y = 0; pOutRect->w = m_rect.w; pOutRect->h = m_rect.h; return &m_image;}void GWidgetSliderTab::SetSize(int w, int h){ m_rect.w = w; m_rect.h = h; m_image.SetSize(w, h); m_dirty = true;}// ----------------------------------------------------------------------GWidgetHorizScrollBar::GWidgetHorizScrollBar(GWidgetGroup* pParent, int x, int y, int w, int h, int nViewSize, int nModelSize): GWidgetGroupWithCanvas(pParent, x, y, w, h){ m_nViewSize = nViewSize; m_nModelSize = nModelSize; m_nPos = 0; int wid = GetButtonWidth(); m_pLeftButton = new GWidgetVCRButton(this, 0, 0, wid, h, GWidgetVCRButton::ArrowLeft); m_pRightButton = new GWidgetVCRButton(this, m_rect.w - wid, 0, wid, h, GWidgetVCRButton::ArrowRight); m_pLeftTab = new GWidgetSliderTab(this, 0, 0, w, 0, false, GWidgetSliderTab::ScrollBarArea); m_pTab = new GWidgetSliderTab(this, 0, 0, w, 0, false, GWidgetSliderTab::ScrollBarTab); m_pRightTab = new GWidgetSliderTab(this, 0, 0, w, 0, false, GWidgetSliderTab::ScrollBarArea);}/*virtual*/ GWidgetHorizScrollBar::~GWidgetHorizScrollBar(){}int GWidgetHorizScrollBar::GetButtonWidth(){ if((m_rect.w >> 2) < m_rect.h) return (m_rect.w >> 2); else return m_rect.h;}void GWidgetHorizScrollBar::SetSize(int w, int h){ m_rect.w = w; m_rect.h = h; m_image.SetSize(w, h); m_dirty = true;}/*virtual*/ void GWidgetHorizScrollBar::Update(){ m_dirty = false; // Calculations int wid = m_image.GetWidth(); int hgt = m_image.GetHeight(); int nButtonSize = GetButtonWidth(); int nSlideAreaSize = wid - nButtonSize - nButtonSize; int nTabSize = nSlideAreaSize * m_nViewSize / m_nModelSize; if(nTabSize < hgt) nTabSize = hgt; if(nTabSize > nSlideAreaSize) nTabSize = nSlideAreaSize; int nTabPos = m_nPos * nSlideAreaSize / m_nModelSize; if(nTabPos > nSlideAreaSize - nTabSize) nTabPos = nSlideAreaSize - nTabSize; nTabPos += nButtonSize; // Draw the three tab areas m_pLeftTab->SetPos(nButtonSize, 0); m_pLeftTab->SetSize(nTabPos - nButtonSize, m_rect.h); m_pTab->SetPos(nTabPos, 0); m_pTab->SetSize(nTabSize, m_rect.h); m_pRightTab->SetPos(nTabPos + nTabSize, 0); m_pRightTab->SetSize(nSlideAreaSize + nButtonSize - (nTabPos + nTabSize), m_rect.h); m_pLeftTab->Draw(this); m_pTab->Draw(this); m_pRightTab->Draw(this); // Draw the buttons m_pLeftButton->Draw(this); m_pRightButton->Draw(this);}/*virtual*/ void GWidgetHorizScrollBar::OnPushVCRButton(GWidgetVCRButton* pButton){ if(pButton == m_pLeftButton) { m_nPos -= m_nViewSize / 5; if(m_nPos < 0) m_nPos = 0; } else { GAssert(pButton == m_pRightButton, "unexpected button"); m_nPos += m_nViewSize / 5; if(m_nPos > m_nModelSize - m_nViewSize) m_nPos = m_nModelSize - m_nViewSize; } m_dirty = true; if(m_pParent) m_pParent->OnHorizScroll(this);}/*virtual*/ void GWidgetHorizScrollBar::OnClickTab(GWidgetSliderTab* pTab){ if(pTab == m_pLeftTab) { m_nPos -= m_nViewSize; if(m_nPos < 0) m_nPos = 0; m_dirty = true; if(m_pParent) m_pParent->OnHorizScroll(this); } else if(pTab == m_pRightTab) { m_nPos += m_nViewSize; if(m_nPos > m_nModelSize - m_nViewSize) m_nPos = m_nModelSize - m_nViewSize; m_dirty = true; if(m_pParent) m_pParent->OnHorizScroll(this); }}/*virtual*/ void GWidgetHorizScrollBar::OnSlideTab(GWidgetSliderTab* pTab, int dx, int dy){ if(pTab != m_pTab) return; m_nPos += dx * m_nModelSize / m_nViewSize; if(m_nPos < 0) m_nPos = 0; else if(m_nPos > m_nModelSize - m_nViewSize) m_nPos = m_nModelSize - m_nViewSize; m_dirty = true; Draw(NULL); if(m_pParent) m_pParent->OnHorizScroll(this);}// ----------------------------------------------------------------------GWidgetVertScrollBar::GWidgetVertScrollBar(GWidgetGroup* pParent, int x, int y, int w, int h, int nViewSize, int nModelSize): GWidgetGroupWithCanvas(pParent, x, y, w, h){ m_nViewSize = nViewSize; m_nModelSize = nModelSize; m_nPos = 0; int hgt = GetButtonHeight(); m_pUpButton = new GWidgetVCRButton(this, 0, 0, w, hgt, GWidgetVCRButton::ArrowUp); m_pDownButton = new GWidgetVCRButton(this, 0, m_rect.h - hgt, w, hgt, GWidgetVCRButton::ArrowDown); m_pAboveTab = new GWidgetSliderTab(this, 0, 0, w, 0, true, GWidgetSliderTab::ScrollBarArea); m_pTab = new GWidgetSliderTab(this, 0, 0, w, 0, true, GWidgetSliderTab::ScrollBarTab); m_pBelowTab = new GWidgetSliderTab(this, 0, 0, w, 0, true, GWidgetSliderTab::ScrollBarArea);}/*virtual*/ GWidgetVertScrollBar::~GWidgetVertScrollBar(){}int GWidgetVertScrollBar::GetButtonHeight(){ if((m_rect.h >> 2) < m_rect.w) return (m_rect.h >> 2); else return m_rect.w;}void GWidgetVertScrollBar::SetSize(int w, int h){ m_rect.w = w; m_rect.h = h; m_image.SetSize(w, h); m_dirty = true;}/*virtual*/ void GWidgetVertScrollBar::Update(){ m_dirty = false; // Calculations if(m_nModelSize < m_nViewSize) m_nModelSize = m_nViewSize; int wid = m_image.GetWidth(); int hgt = m_image.GetHeight(); int nButtonSize = GetButtonHeight(); int nSlideAreaSize = hgt - nButtonSize - nButtonSize; int nTabSize = nSlideAreaSize * m_nViewSize / m_nModelSize; if(nTabSize < wid) nTabSize = wid; if(nTabSize > nSlideAreaSize) nTabSize = nSlideAreaSize; if(m_nPos < 0) m_nPos = 0; int nTabPos = m_nPos * nSlideAreaSize / m_nModelSize; if(nTabPos > nSlideAreaSize - nTabSize) nTabPos = nSlideAreaSize - nTabSize; nTabPos += nButtonSize; // Draw the three tab areas m_pAboveTab->SetPos(0, nButtonSize); m_pAboveTab->SetSize(m_rect.w, nTabPos - nButtonSize); m_pTab->SetPos(0, nTabPos); m_pTab->SetSize(m_rect.w, nTabSize); m_pBelowTab->SetPos(0, nTabPos + nTabSize); m_pBelowTab->SetSize(m_rect.w, nSlideAreaSize + nButtonSize - (nTabPos + nTabSize)); m_pAboveTab->Draw(this); m_pTab->Draw(this); m_pBelowTab->Draw(this); // Draw the buttons m_pUpButton->Draw(this); m_pDownButton->Draw(this);}/*virtual*/ void GWidgetVertScrollBar::OnPushVCRButton(GWidgetVCRButton* pButton){ if(pButton == m_pUpButton) { m_nPos -= m_nViewSize / 5; if(m_nPos < 0) m_nPos = 0; } else { GAssert(pButton == m_pDownButton, "unexpected button"); m_nPos += m_nViewSize / 5; if(m_nPos > m_nModelSize - m_nViewSize) m_nPos = m_nModelSize - m_nViewSize; } m_dirty = true; if(m_pParent) m_pParent->OnVertScroll(this);}/*virtual*/ void GWidgetVertScrollBar::OnClickTab(GWidgetSliderTab* pTab){ if(pTab == m_pAboveTab) { m_nPos -= m_nViewSize; if(m_nPos < 0) m_nPos = 0; m_dirty = true; if(m_pParent) m_pParent->OnVertScroll(this); } else if(pTab == m_pBelowTab)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -