Home | History | Annotate | Download | only in app
      1 //
      2 // Copyright 2005 The Android Open Source Project
      3 //
      4 // Simulated device data.
      5 //
      6 
      7 // For compilers that support precompilation, include "wx/wx.h".
      8 #include "wx/wxprec.h"
      9 
     10 // Otherwise, include all standard headers
     11 #ifndef WX_PRECOMP
     12 # include "wx/wx.h"
     13 #endif
     14 #include "wx/image.h"   // needed for Windows build
     15 
     16 #include "LinuxKeys.h"
     17 #include "PhoneButton.h"
     18 
     19 using namespace android;
     20 
     21 
     22 /*
     23  * Create a PhoneButton without a backing image.
     24  */
     25 bool PhoneButton::Create(const char* label)
     26 {
     27     assert(!mHasImage);     // quick check for re-use
     28 
     29     mKeyCode = LookupKeyCode(label);
     30     if (mKeyCode == kKeyCodeUnknown) {
     31         fprintf(stderr, "WARNING: key code '%s' not recognized\n", label);
     32         // keep going
     33     }
     34 
     35     return true;
     36 }
     37 
     38 /*
     39  * Create a PhoneButton with an associated image.  Don't load the image yet.
     40  */
     41 bool PhoneButton::Create(const char* label, const char* imageFileName,
     42     int x, int y)
     43 {
     44     if (!Create(label))
     45         return false;
     46 
     47     if (mSelectedImage.Create(imageFileName, x, y))
     48         mHasImage = true;
     49     else
     50         fprintf(stderr, "Warning: image create (%s, %d, %d) failed\n",
     51             imageFileName, x, y);
     52 
     53     return true;
     54 }
     55 
     56 /*
     57  * Load the image, if any.
     58  */
     59 bool PhoneButton::LoadResources(void)
     60 {
     61     if (!mHasImage)
     62         return true;        // no image associated with this button
     63 
     64     bool result = mSelectedImage.LoadResources();
     65     if (result)
     66         CreateHighlightedBitmap();
     67     return result;
     68 }
     69 
     70 /*
     71  * Unload the image if we loaded one.
     72  */
     73 bool PhoneButton::UnloadResources(void)
     74 {
     75     if (!mHasImage)
     76         return true;
     77 
     78     return mSelectedImage.UnloadResources();
     79 }
     80 
     81 /* use an inline instead of macro so we don't evaluate args multiple times */
     82 static inline int MinVal(int a, int b) { return (a < b ? a : b); }
     83 
     84 /*
     85  * Create the "highlighted" bitmap from the "selected" image.
     86  */
     87 void PhoneButton::CreateHighlightedBitmap(void)
     88 {
     89     wxBitmap* src = mSelectedImage.GetBitmap();
     90     assert(src != NULL);
     91     wxImage tmpImage = src->ConvertToImage();
     92 
     93     unsigned char* pRGB = tmpImage.GetData();       // top-left RGBRGB...
     94     int x, y;
     95 
     96     /*
     97      * Modify the color used for the "highlight" image.
     98      */
     99     for (y = tmpImage.GetHeight()-1; y >= 0; --y) {
    100         for (x = tmpImage.GetWidth()-1; x >= 0; --x) {
    101             *(pRGB)   = MinVal(*(pRGB)   + *(pRGB) / 8, 255);
    102             *(pRGB+1) = MinVal(*(pRGB+1) + *(pRGB+1) / 8, 255);
    103             *(pRGB+2) = *(pRGB+2) * 5 / 8;
    104 
    105             pRGB += 3;
    106         }
    107     }
    108 
    109     mHighlightedBitmap = wxBitmap(tmpImage);
    110 }
    111 
    112 /*
    113  * Check to see if the button "collides" with the specified point.
    114  *
    115  * This is currently a simple rectangle check, but could be modified
    116  * to take image transparency into account.
    117  */
    118 bool PhoneButton::CheckCollision(int x, int y) const
    119 {
    120     if (!mHasImage)
    121         return false;
    122 
    123     return (x >= mSelectedImage.GetX() &&
    124             x < mSelectedImage.GetX() + mSelectedImage.GetWidth() &&
    125             y >= mSelectedImage.GetY() &&
    126             y < mSelectedImage.GetY() + mSelectedImage.GetHeight());
    127 }
    128 
    129 /*
    130  * Look up a key code based on a string.
    131  *
    132  * Returns kKeyCodeUnknown if the label doesn't match anything.
    133  */
    134 KeyCode PhoneButton::LookupKeyCode(const char* label) const
    135 {
    136     static const struct {
    137         const char* label;
    138         int keyCode;
    139     } codeList[] = {
    140         { "soft-left",      KEY_MENU },
    141         { "soft-right",     KEY_KBDILLUMUP },
    142         { "home",           KEY_HOME },
    143         { "back",           KEY_BACK },
    144         { "call",           KEY_F3 },
    145         { "phone-dial",     KEY_F3 },
    146         { "end-call",       KEY_F4 },
    147         { "phone-hangup",   KEY_F4 },
    148         { "0",              KEY_0 },
    149         { "1",              KEY_1 },
    150         { "2",              KEY_2 },
    151         { "3",              KEY_3 },
    152         { "4",              KEY_4 },
    153         { "5",              KEY_5 },
    154         { "6",              KEY_6 },
    155         { "7",              KEY_7 },
    156         { "8",              KEY_8 },
    157         { "9",              KEY_9 },
    158         { "star",           KEY_SWITCHVIDEOMODE },
    159         { "pound",          KEY_KBDILLUMTOGGLE },
    160         { "dpad-up",        KEY_UP },
    161         { "dpad-down",      KEY_DOWN },
    162         { "dpad-left",      KEY_LEFT },
    163         { "dpad-right",     KEY_RIGHT },
    164         { "dpad-center",    KEY_REPLY },
    165         { "volume-up",      KEY_VOLUMEUP },
    166         { "volume-down",    KEY_VOLUMEDOWN },
    167         { "power",          KEY_POWER },
    168         { "camera",         KEY_CAMERA },
    169         //{ "clear",          kKeyCodeClear },
    170     };
    171     const int numCodes = sizeof(codeList) / sizeof(codeList[0]);
    172 
    173     for (int i = 0; i < numCodes; i++) {
    174         if (strcmp(label, codeList[i].label) == 0)
    175             return (KeyCode) codeList[i].keyCode;
    176     }
    177 
    178     return kKeyCodeUnknown;
    179 };
    180 
    181