EditColorDlg.cpp
资源名称:gloop.zip [点击查看]
上传用户:shxiangxiu
上传日期:2007-01-03
资源大小:1101k
文件大小:13k
源码类别:
OpenGL
开发平台:
Visual C++
- /////////////////////////////////////////////////////////////////////////////
- // EditColorDlg.cpp : implementation file
- //
- // glOOP (OpenGL Object Oriented Programming library)
- // Copyright (c) Craig Fahrnbach 1997, 1998
- //
- // OpenGL is a registered trademark of Silicon Graphics
- //
- //
- // This program is provided for educational and personal use only and
- // is provided without guarantee or warrantee expressed or implied.
- //
- // Commercial use is strickly prohibited without written permission
- // from ImageWare Development.
- //
- // This program is -not- in the public domain.
- //
- /////////////////////////////////////////////////////////////////////////////
- #include "stdafx.h"
- #include "glOOP.h"
- #include "MyColorPaletteWnd.h"
- #include "EditColorDlg.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- /////////////////////////////////////////////////////////////////////////////
- // CEditColorDlg dialog
- CEditColorDlg::CEditColorDlg(C3dColorList* pList, C3dColor* pColor, CWnd* pParent /*=NULL*/)
- : CDialog(CEditColorDlg::IDD, pParent)
- {
- m_pList = pList;
- if(pColor)
- {
- m_Color.SetColor4fv(pColor);
- m_ColorRef.SetColor4fv(pColor);
- m_Color.m_szName = pColor->m_szName;
- }
- else
- {
- m_Color.SetColor4f(1.0f, 1.0f, 1.0f, 1.0f); // Default to White
- m_ColorRef.SetColor4f(1.0f, 1.0f, 1.0f, 1.0f);
- m_Color.m_szName = "White";
- }
- // initialize our class variables
- m_iSliderMaxRange = 255;
- //{{AFX_DATA_INIT(CEditColorDlg)
- m_fBlu = 0.0f;
- m_fGrn = 0.0f;
- m_fRed = 0.0f;
- m_fAlpha = 0.0f;
- //}}AFX_DATA_INIT
- }
- BOOL CEditColorDlg::Create(LPCTSTR lpszClassName, LPCTSTR lpszWindowName, DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID, CCreateContext* pContext)
- {
- // TODO: Add your specialized code here and/or call the base class
- return CDialog::Create(IDD, pParentWnd);
- }
- void CEditColorDlg::DoDataExchange(CDataExchange* pDX)
- {
- CDialog::DoDataExchange(pDX);
- //{{AFX_DATA_MAP(CEditColorDlg)
- DDX_Control(pDX, IDC_SLIDER_GRADIENT, m_wndGradientSlider);
- DDX_Control(pDX, IDC_COLOR_COMBO, m_wndColorComboBox);
- DDX_Text(pDX, IDC_BLU, m_fBlu);
- DDV_MinMaxFloat(pDX, m_fBlu, 0.f, 1.f);
- DDX_Text(pDX, IDC_GRN, m_fGrn);
- DDV_MinMaxFloat(pDX, m_fGrn, 0.f, 1.f);
- DDX_Text(pDX, IDC_RED, m_fRed);
- DDV_MinMaxFloat(pDX, m_fRed, 0.f, 1.f);
- DDX_Text(pDX, IDC_ALPHA, m_fAlpha);
- DDV_MinMaxFloat(pDX, m_fAlpha, 0.f, 1.f);
- //}}AFX_DATA_MAP
- }
- BEGIN_MESSAGE_MAP(CEditColorDlg, CDialog)
- //{{AFX_MSG_MAP(CEditColorDlg)
- ON_WM_VSCROLL()
- ON_CBN_DROPDOWN(IDC_COLOR_COMBO, OnDropdownColorCombo)
- ON_CBN_SELCHANGE(IDC_COLOR_COMBO, OnSelchangeColorCombo)
- ON_WM_CREATE()
- ON_WM_PAINT()
- ON_EN_KILLFOCUS(IDC_RED, OnKillfocusRed)
- ON_EN_KILLFOCUS(IDC_GRN, OnKillfocusGrn)
- ON_EN_KILLFOCUS(IDC_BLU, OnKillfocusBlu)
- ON_EN_KILLFOCUS(IDC_ALPHA, OnKillfocusAlpha)
- ON_WM_DESTROY()
- //}}AFX_MSG_MAP
- // USER defined messages
- ON_MESSAGE( WM_CPW_LBUTTONDOWN, OnColorPaletteLButtonDown)
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // CEditColorDlg message handlers
- int CEditColorDlg::OnCreate(LPCREATESTRUCT lpCreateStruct)
- {
- if (CDialog::OnCreate(lpCreateStruct) == -1)
- return -1;
- return 0;
- }
- BOOL CEditColorDlg::OnInitDialog()
- {
- RECT rect; // Used to get window coordinates
- // Let the base class do the initializing..
- CDialog::OnInitDialog();
- // Get the Color Palette Window rect size
- GetDlgItem(IDC_COLOR_CUBE_WND)->GetWindowRect(&rect);
- ScreenToClient(&rect);
- // Create the Color Palette Cube window
- if (!m_wndColorCube.Create(NULL, // dwExtStyle
- NULL, // Class Name
- "Palette Cube", // Window name
- WS_CHILD | WS_VISIBLE | // dwStyle
- WS_DLGFRAME |
- WS_HSCROLL | WS_VSCROLL,
- rect, // rect
- this, // CWnd* pParentWnd
- IDC_COLOR_CUBE_WND, // UINT nID
- 0)) { // pContext
- return -1;
- }
- // Get the 'Selected Color' window rect size
- GetDlgItem(IDC_SELECTED_COLOR_WND)->GetWindowRect(&rect);
- ScreenToClient(&rect);
- // Create the 'Selected Color' window
- if (!m_wndSelColor.Create(NULL, // lpszClassName
- NULL, // lpszWindowName
- WS_CHILD | WS_VISIBLE | // dwStyle
- WS_DLGFRAME |
- WS_CLIPSIBLINGS,
- rect, // rect
- this, // CWnd* pParentWnd
- IDC_SELECTED_COLOR_WND, // UINT nID
- 0)) { // pContext
- return -1;
- }
- // Get the Gradient color Window rect size
- GetDlgItem(IDC_GRADIENT_WND)->GetWindowRect(&rect);
- ScreenToClient(&rect);
- // Create the Gradient color window
- if (!m_wndGradient.Create(NULL, // lpszClassName
- NULL, // lpszWindowName
- WS_CHILD | WS_VISIBLE | // dwStyle
- WS_DLGFRAME |
- WS_CLIPSIBLINGS,
- rect, // rect
- this, // CWnd* pParentWnd
- IDC_GRADIENT_WND, // UINT nID
- 0)) { // pContext
- return -1;
- }
- // Set the range of our gradient slider
- CSliderCtrl* pSlider = (CSliderCtrl*)GetDlgItem(IDC_SLIDER_GRADIENT);
- pSlider->SetRange(0, m_iSliderMaxRange, TRUE);
- UpDateRGBValues();
- return TRUE; // return TRUE unless you set the focus to a control
- // EXCEPTION: OCX Property Pages should return FALSE
- }
- void CEditColorDlg::UpDateRGBValues()
- {
- // Set our local values to the colors' values
- m_fRed = m_Color.m_fColor[0];
- m_fGrn = m_Color.m_fColor[1];
- m_fBlu = m_Color.m_fColor[2];
- m_fAlpha = m_Color.m_fColor[3];
- // Dialog box is being initialized (FALSE)
- // or data is being retrieved (TRUE).
- UpdateData(FALSE);
- }
- void CEditColorDlg::OnDestroy()
- {
- CDialog::OnDestroy();
- // Destroy the color cube window
- m_wndColorCube.DestroyWindow();
- // Destroy the gradient color window
- m_wndGradient.DestroyWindow();
- // Destroy the selected color window
- m_wndSelColor.DestroyWindow();
- }
- void CEditColorDlg::OnPaint()
- {
- CPaintDC dc(this); // device context for painting
- // Paint our 'Selected Color' window
- PaintSelColorWnd();
- // Position our gradient slider control; (this will inturn
- // paint our gradient color window)
- PosnGradientSlider();
- // Do not call CDialog::OnPaint() for painting messages
- }
- void CEditColorDlg::OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
- {
- // Note: MFC maps vertically oriented CSliderCtrl to WM_VSCROLL
- // and horizontally oriented CSliderCtrl to WM_HSCROLL
- // Get the position of our color slider and calculate the
- // difference in position to our initial position
- int posn = m_wndGradientSlider.GetPos();
- int delta = posn - m_iInitColorSliderPos;
- if(delta > 0)
- {
- m_Color.m_fColor[0] = m_ColorRef.m_fColor[0]+(m_fRedPlus*delta);
- m_Color.m_fColor[1] = m_ColorRef.m_fColor[1]+(m_fGrnPlus*delta);
- m_Color.m_fColor[2] = m_ColorRef.m_fColor[2]+(m_fBluPlus*delta);
- }
- else
- {
- m_Color.m_fColor[0] = m_ColorRef.m_fColor[0]+(m_fRedMinus*delta);
- m_Color.m_fColor[1] = m_ColorRef.m_fColor[1]+(m_fGrnMinus*delta);
- m_Color.m_fColor[2] = m_ColorRef.m_fColor[2]+(m_fBluMinus*delta);
- }
- // Limit check our color values
- if( m_Color.m_fColor[0] > 1.0f) // red
- m_Color.m_fColor[0] = 1.0f;
- if( m_Color.m_fColor[0] < 0.0f)
- m_Color.m_fColor[0] = 0.0f;
- if( m_Color.m_fColor[1] > 1.0f) // green
- m_Color.m_fColor[1] = 1.0f;
- if( m_Color.m_fColor[1] < 0.0f)
- m_Color.m_fColor[1] = 0.0f;
- if( m_Color.m_fColor[2] > 1.0f) // blue
- m_Color.m_fColor[2] = 1.0f;
- if( m_Color.m_fColor[2] < 0.0f)
- m_Color.m_fColor[2] = 0.0f;
- PaintSelColorWnd();
- UpDateRGBValues();
- CDialog::OnVScroll(nSBCode, nPos, pScrollBar);
- }
- void CEditColorDlg::OnDropdownColorCombo()
- {
- // Fill the color ComboBox listbox if we have a valid
- // C3dColorList class pointer
- if(!m_pList)
- return;
- m_pList->LoadComboBox(&m_wndColorComboBox, &m_Color);
- }
- void CEditColorDlg::OnSelchangeColorCombo()
- {
- int index = m_wndColorComboBox.GetCurSel();
- if(index == CB_ERR)
- return;
- // Cast the user selected list box items' lParam to
- // a C3dColor pointer..
- C3dColor* pColor = (C3dColor*)m_wndColorComboBox.GetItemData(index);
- if(pColor)
- {
- // Set the world's background color and name, then
- // and paint the color button window
- m_Color.SetColor4fv(pColor);
- m_Color.m_szName = pColor->m_szName;
- PaintSelColorWnd();
- // Update our RGB values
- UpDateRGBValues();
- // Position gradient slider control
- PosnGradientSlider();
- }
- }
- void CEditColorDlg::PosnGradientSlider()
- {
- int iStepsPlus;
- int iStepsMinus;
- // Save our colors initial values. We will use this to
- // calculate our new colors as the user repositions the
- // gradient slider
- m_ColorRef.m_fColor[0] = m_Color.m_fColor[0];
- m_ColorRef.m_fColor[1] = m_Color.m_fColor[1];
- m_ColorRef.m_fColor[2] = m_Color.m_fColor[2];
- // Bias our initial slider position based on the rgb color
- // values of our selected color
- m_iInitColorSliderPos = (int)(((m_Color.m_fColor[0]+m_Color.m_fColor[1]+m_Color.m_fColor[2])/3)*255);
- // Set the slider to our starting position
- m_wndGradientSlider.SetPos(m_iInitColorSliderPos);
- // Determine the number of incremental steps
- iStepsPlus = m_iSliderMaxRange-m_iInitColorSliderPos;
- iStepsMinus = m_iInitColorSliderPos;
- // Define our color index values
- if(iStepsPlus)
- {
- m_fRedPlus = (1-m_Color.m_fColor[0])/iStepsPlus;
- m_fGrnPlus = (1-m_Color.m_fColor[1])/iStepsPlus;
- m_fBluPlus = (1-m_Color.m_fColor[2])/iStepsPlus;
- }
- else
- {
- m_fRedPlus = 0.0f;
- m_fGrnPlus = 0.0f;
- m_fBluPlus = 0.0f;
- }
- if(iStepsMinus)
- {
- m_fRedMinus = m_Color.m_fColor[0]/iStepsMinus;
- m_fGrnMinus = m_Color.m_fColor[1]/iStepsMinus;
- m_fBluMinus = m_Color.m_fColor[2]/iStepsMinus;
- }
- else
- {
- m_fRedMinus = 0.0f;
- m_fGrnMinus = 0.0f;
- m_fBluMinus = 0.0f;
- }
- PaintGradientWnd();
- }
- void CEditColorDlg::PaintGradientWnd()
- {
- CRect rect; // window rect
- CDC* pDC; // pointer to a device context
- int i, x, cx;
- // Get the size of the gradient window
- m_wndGradient.GetWindowRect(&rect);
- // Get a pointer to the windows device context
- pDC = m_wndGradient.GetDC();
- // Calcualte a few constants for later use...
- float fWindowHeight = (float)(rect.bottom-rect.top);
- float fGradientHeight = fWindowHeight/m_iSliderMaxRange;
- cx = (int)fGradientHeight;
- if(cx<1)
- cx=1;
- // Display gradient fill for colors below the CSliderCtrl
- // position.
- for(i=0; i<m_iInitColorSliderPos; i++)
- {
- float fColorScale = (float)i/m_iInitColorSliderPos;
- float gradientX = (fWindowHeight/m_iSliderMaxRange)*i;
- x = (int)gradientX;
- pDC->FillSolidRect(0, x,
- (rect.right-rect.left), cx,
- COLORREF RGB(m_Color.m_fColor[0]*fColorScale*255,
- m_Color.m_fColor[1]*fColorScale*255,
- m_Color.m_fColor[2]*fColorScale*255));
- }
- // Display gradient fill for colors above the CSliderCtrl
- // position.
- for(i=m_iInitColorSliderPos; i<=m_iSliderMaxRange; i++)
- {
- float gradientX = (fWindowHeight/m_iSliderMaxRange)*i;
- x = (int)gradientX;
- float red = m_Color.m_fColor[0]+(m_fRedPlus*(i-m_iInitColorSliderPos));
- float grn = m_Color.m_fColor[1]+(m_fGrnPlus*(i-m_iInitColorSliderPos));
- float blu = m_Color.m_fColor[2]+(m_fBluPlus*(i-m_iInitColorSliderPos));
- pDC->FillSolidRect(0, x,
- (rect.right-rect.left), cx,
- COLORREF RGB(red*255, grn*255, blu*255));
- }
- ReleaseDC(pDC);
- }
- void CEditColorDlg::PaintSelColorWnd()
- {
- CRect rect; // window rect
- CDC* pDC; // pointer to a device context
- // Get the size of the gradient window
- m_wndSelColor.GetWindowRect(&rect);
- // Get a pointer to the windows device context
- pDC = m_wndSelColor.GetDC();
- if(pDC)
- pDC->FillSolidRect(0,
- 0,
- rect.right-rect.left,
- rect.bottom-rect.top,
- COLORREF RGB(m_Color.m_fColor[0]*255,
- m_Color.m_fColor[1]*255,
- m_Color.m_fColor[2]*255));
- ReleaseDC(pDC);
- }
- long CEditColorDlg::OnColorPaletteLButtonDown(WPARAM wParam, LPARAM lParam)
- {
- m_Color.m_fColor[0] = m_wndColorCube.m_fRed;
- m_Color.m_fColor[1] = m_wndColorCube.m_fGrn;
- m_Color.m_fColor[2] = m_wndColorCube.m_fBlu;
- PaintSelColorWnd();
- UpDateRGBValues();
- PosnGradientSlider();
- return 0L;
- }
- void CEditColorDlg::OnKillfocusRed()
- {
- // Dialog box is being initialized (FALSE)
- // or data is being retrieved (TRUE).
- UpdateData(TRUE);
- m_Color.m_fColor[0] = m_fRed;
- m_Color.m_fColor[1] = m_fGrn;
- m_Color.m_fColor[2] = m_fBlu;
- m_Color.m_fColor[3] = m_fAlpha;
- PaintSelColorWnd();
- PosnGradientSlider();
- }
- void CEditColorDlg::OnKillfocusGrn()
- {
- // Dialog box is being initialized (FALSE)
- // or data is being retrieved (TRUE).
- UpdateData(TRUE);
- m_Color.m_fColor[0] = m_fRed;
- m_Color.m_fColor[1] = m_fGrn;
- m_Color.m_fColor[2] = m_fBlu;
- m_Color.m_fColor[3] = m_fAlpha;
- PaintSelColorWnd();
- PosnGradientSlider();
- }
- void CEditColorDlg::OnKillfocusBlu()
- {
- // Dialog box is being initialized (FALSE)
- // or data is being retrieved (TRUE).
- UpdateData(TRUE);
- m_Color.m_fColor[0] = m_fRed;
- m_Color.m_fColor[1] = m_fGrn;
- m_Color.m_fColor[2] = m_fBlu;
- m_Color.m_fColor[3] = m_fAlpha;
- PaintSelColorWnd();
- PosnGradientSlider();
- }
- void CEditColorDlg::OnKillfocusAlpha()
- {
- // Dialog box is being initialized (FALSE)
- // or data is being retrieved (TRUE).
- UpdateData(TRUE);
- m_Color.m_fColor[0] = m_fRed;
- m_Color.m_fColor[1] = m_fGrn;
- m_Color.m_fColor[2] = m_fBlu;
- m_Color.m_fColor[3] = m_fAlpha;
- }