/* * * Custom widget Demo * * File : DConsoleView.cpp * Purpose : Implements the custom Console View class * Author : JLV * Creation : 10/19/04 * Last Update : 11/05/04 * * (C) hexaZen 2004 * * This file is part of a demo application for Zinzala. It can be * used, distributed and modified without limitation. * * * $Id$ */ /* * $Log$ */ #include #include #include #include #include #include #include #include #include #include #include // Some sizes const tUint16 kConsoleWidth = 321; const tUint16 kConsoleHeight = 98; const tUint16 kButtonWidth = 64; const tUint16 kButtonHeight = 64; const tUint16 kLabelWidth = 25; const tUint16 kLabelHeight = 25; // Initial position of the buttons const tUint16 kPositionX = 18; const tUint16 kPositionY = 17; const tUint16 kSpacing = 10; // Buttons const tUint8 kButtonsCount = 4; const tUint8 kButtonBwd = 0; const tUint8 kButtonPlay = 1; const tUint8 kButtonPause = 2; const tUint8 kButtonFwd = 3; // Button's states const tUint8 kStatesCount = 7; const tUint8 kStateDimmed = 0; const tUint8 kStatePressed = 1; const tUint8 kStateNormal = 2; const tUint8 kStateActive = 3; const tUint8 kStateActive1 = 4; const tUint8 kStateActive2 = 5; const tUint8 kStateActive3 = 6; // Button's labels const tUint8 kLabelsCount = 5; const tUint8 kLabelBwd = 0; const tUint8 kLabelPlay = 1; const tUint8 kLabelPause = 2; const tUint8 kLabelFwd = 3; const tUint8 kLabelStop = 4; // delay (in ms) before a button is redraw after been depressed const tUint32 kDelayPressed = 150; // Button's data typedef struct cDConsoleView::tButton { uRect iBFrame; // frame of the button uRect iLFrame; // frame of the label tUint8 iState; // button state tUint8 iLabel; // label displayed on the button } tButton; /* * Function : CreateBitmapFromResourceL * Purpose : Create a bitmap from the resource * Inputs : * * const tBitmapResource &aRes, resource * tBool aTransparent, apply transparency or not * * Output : tInt16 * Side Effects : Can leave * */ cBitmap *CreateBitmapFromResourceL(const tBitmapResource &aRes,tBool aTransparent = true) { tErr lErr; cBitmap *lBitmap = NULL; lBitmap = new cBitmap(aRes.iWidth,aRes.iHeight,eSpaceColor8Bit,true,aRes.iColors); lErr = cBitmap::VerifyD(lBitmap); if(!lErr) { lBitmap->SetPalette(aRes.iPalette); lBitmap->SetBits(aRes.iBits,aRes.iLength,0); if(aTransparent) lBitmap->MakeTransparent(aRes.iIndex); return lBitmap; } else throw uException(lErr,"CreateBitmapFromResourceL()"); } /* * Function : CreateBitmapFromResourceLC * Purpose : Create a bitmap from the resource * Inputs : * * const tBitmapResource &aRes, resource * tBool aTransparent, apply transparency or not * * Output : tInt16 * Side Effects : Can leave, the bitmap is left on the cleanup stack * */ cBitmap *CreateBitmapFromResourceLC(const tBitmapResource &aRes,tBool aTransparent = true) { tErr lErr; cBitmap *lBitmap = NULL; lBitmap = new cBitmap(aRes.iWidth,aRes.iHeight,eSpaceColor8Bit,true,aRes.iColors); lErr = cBitmap::VerifyD(lBitmap); if(!lErr) { sCleanupStack::PushL(lBitmap); lBitmap->SetPalette(aRes.iPalette); lBitmap->SetBits(aRes.iBits,aRes.iLength,0); if(aTransparent) lBitmap->MakeTransparent(aRes.iIndex); return lBitmap; } else throw uException(lErr,"CreateBitmapFromResourceLC()"); } /* * Method : cDConsoleView * Purpose : Constructor * Inputs : none * Output : none * Side Effects : none * */ cDConsoleView::cDConsoleView() : cDrawView(uRect(kConsoleWidth,kConsoleHeight),"console",kFollowNone, kFlgBeatNeeded | kFlgInteractive | kFlgOffscreen) { iBackground = NULL; iBStates = NULL; iBLabelsN = NULL; iBLabelsD = NULL; iButtons = NULL; iPressedButton = kButtonsCount; iActiveButton = kButtonsCount; iActiveDir = 1; iState = eStopped; if(!iFault) { iButtons = new tButton[kButtonsCount]; if(!iButtons) iFault = kErrOutOfMemory; else { iBStates = new (cBitmap *)[kStatesCount]; if(!iBStates) iFault = kErrOutOfMemory; else { memset(iBStates,0,sizeof(cBitmap *) * kStatesCount); iBLabelsN = new (cBitmap *)[kLabelsCount]; if(!iBLabelsN) iFault = kErrOutOfMemory; else { memset(iBLabelsN,0,sizeof(cBitmap *) * kLabelsCount); iBLabelsD = new (cBitmap *)[kLabelsCount]; if(!iBLabelsD) iFault = kErrOutOfMemory; else memset(iBLabelsD,0,sizeof(cBitmap *) * kLabelsCount); } } } } } /* * Method : ~cDConsoleView * Purpose : Destructor * Inputs : none * Output : none * Side Effects : none * */ cDConsoleView::~cDConsoleView() { if(iButtons) delete [] iButtons; if(iBackground) delete iBackground; if(iBStates) { for(tUint8 i=0;iConstructL(aSkin); sCleanupStack::PopL(lView); return lView; } /* * Method : ConstructL * Purpose : 2nd phase constructor * Inputs : * * cDSkin &aSkin, Skin to use * * Output : none * Side Effects : none * */ void cDConsoleView::ConstructL(cDSkin &aSkin) { uPoint lPoint; // If the view is already faulty, we will leave right away sEnv::LeaveIfError(iFault); // Instantiate the background bitmap iBackground = CreateBitmapFromResourceL(aSkin.GetResourceL(eResBackground),false); // Instantiate the button bitmaps iBStates[kStateNormal] = CreateBitmapFromResourceL(aSkin.GetResourceL(eResNormal)); iBStates[kStateDimmed] = CreateBitmapFromResourceL(aSkin.GetResourceL(eResDimmed)); iBStates[kStatePressed] = CreateBitmapFromResourceL(aSkin.GetResourceL(eResPressed)); iBStates[kStateActive] = iBStates[kStateNormal]; // use same bitmap as the normal state iBStates[kStateActive1] = CreateBitmapFromResourceL(aSkin.GetResourceL(eResAnimate1)); iBStates[kStateActive2] = CreateBitmapFromResourceL(aSkin.GetResourceL(eResAnimate2)); iBStates[kStateActive3] = CreateBitmapFromResourceL(aSkin.GetResourceL(eResAnimate3)); // Instantiate the label bitmaps iBLabelsN[kLabelBwd] = CreateBitmapFromResourceL(aSkin.GetResourceL(eResBackwardN)); iBLabelsN[kLabelFwd] = CreateBitmapFromResourceL(aSkin.GetResourceL(eResForwardN)); iBLabelsN[kLabelStop] = CreateBitmapFromResourceL(aSkin.GetResourceL(eResStopN)); iBLabelsN[kLabelPlay] = CreateBitmapFromResourceL(aSkin.GetResourceL(eResPlayN)); iBLabelsN[kLabelPause] = CreateBitmapFromResourceL(aSkin.GetResourceL(eResPauseN)); iBLabelsD[kLabelBwd] = CreateBitmapFromResourceL(aSkin.GetResourceL(eResBackwardD)); iBLabelsD[kLabelFwd] = CreateBitmapFromResourceL(aSkin.GetResourceL(eResForwardD)); iBLabelsD[kLabelStop] = CreateBitmapFromResourceL(aSkin.GetResourceL(eResStopD)); iBLabelsD[kLabelPlay] = CreateBitmapFromResourceL(aSkin.GetResourceL(eResPlayD)); iBLabelsD[kLabelPause] = CreateBitmapFromResourceL(aSkin.GetResourceL(eResPauseD)); tUint16 lX = kPositionX; // initialise the buttons data (all are dimmed) for(tUint8 i=0;iGetTransparent(lTrans); // get pixel color index iBStates[kStateNormal]->GetPixelColor(lPoint.iX,lPoint.iY,&lIndex); // if the color is transparent, then we are outside of the button if(lIndex != lTrans) break; } } return lButton; } /* * Method : Render * Purpose : Render the console or a part of the console * Inputs : * * tUint8 aButton, button to render (kButtonsCount if the all console is to be rendered) * tBool aBackground, render the background as well * * Output : tUint8, button * Side Effects : none * */ void cDConsoleView::Render(tUint8 aButton,tBool aBackground) { StartDrawing(); if(aButton < kButtonsCount) { // Render only one button DrawBitmap(iBStates[iButtons[aButton].iState],iButtons[aButton].iBFrame.GetLeftTop()); if(iButtons[aButton].iState == kStateDimmed) DrawBitmap(iBLabelsD[iButtons[aButton].iLabel],iButtons[aButton].iLFrame.GetLeftTop()); else DrawBitmap(iBLabelsN[iButtons[aButton].iLabel],iButtons[aButton].iLFrame.GetLeftTop()); } else { // Render the background if(aBackground) DrawBitmap(iBackground); // Render all the button for(tUint8 i=0;i= kStateActive) { // set the button as pressed iPrevState = iButtons[aButton].iState; iButtons[aButton].iState = kStatePressed; // redraw the button Render(aButton); } } } /* * Method : OnScreen * Purpose : The view is put on screen * Inputs : none * Output : none * Side Effects : none * */ void cDConsoleView::OnScreen() { cDrawView::OnScreen(); // Render the whole console (for the first time) Render(kButtonsCount,true); } /* * Method : Beat * Purpose : Render the pulsating effect on the active button * Inputs : none * Output : none * Side Effects : none * */ void cDConsoleView::Beat() { // if there is an active button if(iActiveButton < kButtonsCount) { if(iButtons[iActiveButton].iState >= kStateActive) { // set the state of the button iButtons[iActiveButton].iState += iActiveDir; if(iButtons[iActiveButton].iState > kStateActive2) { iButtons[iActiveButton].iState = kStateActive2; iActiveDir = -1; } else if(iButtons[iActiveButton].iState < kStateActive) { iButtons[iActiveButton].iState = kStateActive; iActiveDir = 1; } // redraw the button Render(iActiveButton); } } } /* * Method : MouseUp * Purpose : The mouse button has been depressed * Inputs : * * uPoint aPoint, position in the view where the button was depressed * * Output : none * Side Effects : none * */ void cDConsoleView::MouseUp(uPoint aPoint) { if(FindButton(aPoint) == iPressedButton) Invoked(); else { if(iButtons[iPressedButton].iState == kStatePressed) { // revert the state of the button iButtons[iPressedButton].iState = iPrevState; // redraw the button Render(iPressedButton); } iPressedButton = kButtonsCount; } } /* * Method : MouseDown * Purpose : The mouse button has been pressed * Inputs : * * uPoint aPoint, position in the view where the button was pressed * * Output : none * Side Effects : none * */ void cDConsoleView::MouseDown(uPoint aPoint) { Pressed(FindButton(aPoint)); } /* * Method : OutBound * Purpose : The mouse exit the view and the mouse button is still pressed * Inputs : * * uPoint aPoint, position in the view where mouse exit the view * * Output : none * Side Effects : none * */ void cDConsoleView::OutBound(uPoint aPoint) { if(iPressedButton < kButtonsCount) { if(iButtons[iPressedButton].iState == kStatePressed) { // revert the state of the button iButtons[iPressedButton].iState = iPrevState; // redraw the button Render(iPressedButton); } iPressedButton = kButtonsCount; } }