#ifdef G_OS_WIN32 #define WIN32_LEAN_AND_MEAN 1 #include #else #define FALSE 0 #define TRUE !FALSE #define max(a, b) (((a) > (b)) ? (a) : (b)) #endif #include #include #include #include #include #include "button.h" #include "font.h" struct ButtonStruct ButtonCreate(int f_iXPos, int f_iYPos, int f_iWidth, int f_iHeight, char *f_pcTitle, int f_iType, int f_iGroup, int f_iId) { struct ButtonStruct l_sButton; // copy parameters l_sButton.m_iXPos = f_iXPos; l_sButton.m_iYPos = f_iYPos; l_sButton.m_iType = f_iType; l_sButton.m_iHeight = f_iHeight; l_sButton.m_iWidth = f_iWidth; l_sButton.m_iGroup = f_iGroup; l_sButton.m_iId = f_iId; // copy title memset(&l_sButton.m_pcTitle, 0, sizeof(l_sButton.m_pcTitle)); memcpy(l_sButton.m_pcTitle, f_pcTitle, strlen(f_pcTitle)); switch (f_iType) { case BUTTON_CLICK: l_sButton.m_psImgNormal[0] = BitmapLoad("img/button_click_left.bmp"); l_sButton.m_psImgNormal[1] = BitmapLoad("img/button_click_center.bmp"); l_sButton.m_psImgNormal[2] = BitmapLoad("img/button_click_right.bmp"); l_sButton.m_psImgHover[0] = BitmapLoad("img/button_click_left_hover.bmp"); l_sButton.m_psImgHover[1] = BitmapLoad("img/button_click_center_hover.bmp"); l_sButton.m_psImgHover[2] = BitmapLoad("img/button_click_right_hover.bmp"); BitmapConvertWidth(&l_sButton.m_psImgNormal[0], f_iHeight); BitmapConvertWidth(&l_sButton.m_psImgNormal[1], f_iHeight); BitmapConvertWidth(&l_sButton.m_psImgNormal[2], f_iHeight); BitmapConvertWidth(&l_sButton.m_psImgHover[0], f_iHeight); BitmapConvertWidth(&l_sButton.m_psImgHover[1], f_iHeight); BitmapConvertWidth(&l_sButton.m_psImgHover[2], f_iHeight); // calculate (center)width of image l_sButton.m_iCenterWidth = max(f_iWidth - l_sButton.m_psImgNormal[0].m_iWidth - l_sButton.m_psImgNormal[2].m_iWidth, 0); l_sButton.m_iWidth = l_sButton.m_iCenterWidth + l_sButton.m_psImgNormal[0].m_iWidth + l_sButton.m_psImgNormal[2].m_iWidth; break; case BUTTON_RADIO: l_sButton.m_psImgNormal[0] = BitmapLoad("img/button_radio.bmp"); l_sButton.m_psImgHover[0] = BitmapLoad("img/button_radio_hover.bmp"); l_sButton.m_psImgPressed[0] = BitmapLoad("img/button_radio_pressed.bmp"); BitmapConvertWidth(&l_sButton.m_psImgNormal[0], f_iHeight); BitmapConvertWidth(&l_sButton.m_psImgHover[0], f_iHeight); BitmapConvertWidth(&l_sButton.m_psImgPressed[0], f_iHeight); l_sButton.m_iCenterWidth = 0; l_sButton.m_iHeight = f_iHeight; l_sButton.m_iWidth = f_iWidth; break; default: break; } l_sButton.m_bPressed = FALSE; l_sButton.m_bHover = FALSE; return l_sButton; } // ButtonCreate void ButtonClickRender(struct ButtonStruct *f_sButton) { char l_pcTitle[BUTTON_MAX_TITLE]; struct BitmapStruct *l_sImgButton; int l_iImageIdLeft, l_iImageIdCenter, l_iImageIdRight; int l_iLen, l_iXOffset, l_iYOffset, l_iWidth; struct ColorStruct l_sColor = {0, 0, 0}; // copy button attributes int l_iCnWdt = f_sButton->m_iCenterWidth; int l_iHeight = f_sButton->m_iHeight; int l_iXPos = f_sButton->m_iXPos; int l_iYPos = f_sButton->m_iYPos; if (f_sButton->m_bPressed) { glColor3d(0.8, 0.9, 1); } else { glColor3d(1, 1, 1); } // get the right image struct if (f_sButton->m_bHover) { l_sImgButton = f_sButton->m_psImgHover; } else { l_sImgButton = f_sButton->m_psImgNormal; } // copy buttom images l_iImageIdLeft = l_sImgButton[0].m_iImageId; l_iImageIdCenter = l_sImgButton[1].m_iImageId; l_iImageIdRight = l_sImgButton[2].m_iImageId; // copy button title memcpy(&l_pcTitle, f_sButton->m_pcTitle, strlen(f_sButton->m_pcTitle)); // render left side of button l_iWidth = l_sImgButton[0].m_iWidth; BitmapRender(l_iXPos, l_iYPos, l_iWidth + 1, l_iHeight, l_iImageIdLeft); // render right side of button l_iWidth = l_sImgButton[2].m_iWidth; BitmapRender(l_iXPos + l_iCnWdt + l_iWidth - 1, l_iYPos, l_iWidth, l_iHeight, l_iImageIdRight); // render center of button if (l_iCnWdt > 0) { BitmapRender(l_iXPos + l_iWidth - 1, l_iYPos, l_iCnWdt + 1, l_iHeight, l_iImageIdCenter); } l_iLen = (int)strlen(f_sButton->m_pcTitle); l_iXOffset = (int)(0.5 * f_sButton->m_iWidth - 0.5 * l_iLen * FONT_SPACING); l_iYOffset = (int)(0.5 * f_sButton->m_iHeight - 0.5 * FONT_HEIGHT); glPrint(f_sButton->m_iXPos + l_iXOffset, f_sButton->m_iYPos + l_iYOffset, f_sButton->m_pcTitle, l_sColor); } // ButtonClickRender void ButtonRadioRender(struct ButtonStruct *f_sButton) { struct BitmapStruct *l_sImgButton; int l_iXOffset, l_iYOffset, l_iImageRadioId, l_iImageEnabledId; // copy button attributes int l_iWidth = f_sButton->m_iWidth; int l_iHeight = f_sButton->m_iHeight; int l_iXPos = f_sButton->m_iXPos; int l_iYPos = f_sButton->m_iYPos; struct ColorStruct l_sColor = {0.2, 0.4, 0.8}; // get the right image struct if (f_sButton->m_bHover) { l_sImgButton = f_sButton->m_psImgHover; } else { l_sImgButton = f_sButton->m_psImgNormal; } l_iImageRadioId = l_sImgButton[0].m_iImageId; l_iImageEnabledId = f_sButton->m_psImgPressed[0].m_iImageId; glColor3d(1, 1, 1); // render the radio button BitmapRender(l_iXPos, l_iYPos, l_iWidth, l_iHeight, l_iImageRadioId); // render the enable-dot if button is pressed if (f_sButton->m_bPressed) { BitmapRender(l_iXPos, l_iYPos, l_iWidth, l_iHeight, l_iImageEnabledId); } l_iXOffset = (int)f_sButton->m_iWidth; l_iYOffset = (int)(0.5 * f_sButton->m_iHeight - 0.5 * FONT_HEIGHT); glPrint(f_sButton->m_iXPos + l_iXOffset, f_sButton->m_iYPos + l_iYOffset, f_sButton->m_pcTitle, l_sColor); } // ButtonRadioRender void ButtonRender(struct ButtonStruct *f_sButton) { int l_iType = f_sButton->m_iType; switch (l_iType) { case BUTTON_CLICK: ButtonClickRender(f_sButton); break; case BUTTON_RADIO: ButtonRadioRender(f_sButton); break; default: break; } } // ButtonRender void ButtonEnter(struct ButtonStruct *f_sButton) { f_sButton->m_bHover = TRUE; } // ButtonHover void ButtonExit(struct ButtonStruct *f_sButton) { f_sButton->m_bHover = FALSE; } // ButtonHover void ButtonClickPress(struct ButtonStruct *f_sButton) { if (!f_sButton->m_bPressed) { ButtonEnable(f_sButton); } } // ButtonClickPress void ButtonRadioPress(struct ButtonStruct *f_sButton) { int i = 0; if (!f_sButton->m_bPressed) { ButtonEnable(f_sButton); } } // ButtonClickPress void ButtonPress(struct ButtonStruct *f_sButton) { int l_iType = f_sButton->m_iType; switch (l_iType) { case BUTTON_CLICK: ButtonClickPress(f_sButton); break; case BUTTON_RADIO: ButtonRadioPress(f_sButton); break; default: break; } } // ButtonPress void ButtonClickRelease(struct ButtonStruct *f_sButton) { ButtonDisable(f_sButton); } // ButtonClickRelease void ButtonRelease(struct ButtonStruct *f_sButton) { int l_iType = f_sButton->m_iType; switch (l_iType) { case BUTTON_CLICK: ButtonClickRelease(f_sButton); break; case BUTTON_RADIO: default: break; } } // ButtonRelease void ButtonEnable(struct ButtonStruct *f_sButton) { f_sButton->m_bPressed = TRUE; } // ButtonEnable void ButtonDisable(struct ButtonStruct *f_sButton) { f_sButton->m_bPressed = FALSE; } // ButtonDisable