00001 /* Crown and Cutlass 00002 * Crown and Cutlass Button Class 00003 */ 00004 00005 #include "../Callback.h" 00006 #include "../Log.h" 00007 #include "CCFont.h" 00008 #include "CCButton.h" 00009 00010 using namespace std; 00011 00012 CCButton::CCButton(const std::string& caption, 00013 const std::string& eventId, 00014 const std::string mainFontName, 00015 const std::string highlightFontName, 00016 const std::string disabledFontName, 00017 cCallback *recallFunction): gcn::Button(caption) { 00018 // Set up the button 00019 SetMainFont(mainFontName); 00020 SetHighlightFont(highlightFontName); 00021 SetDisabledFont(disabledFontName); 00022 00023 setEventId(eventId); 00024 SetRecallFunction(recallFunction); 00025 addActionListener(this); 00026 SetID(0); 00027 setBorderSize(0); 00028 00029 setEnabled(true); 00030 adjustSize(); 00031 } 00032 00033 CCButton::~CCButton() { 00034 00035 } 00036 00037 void CCButton::draw(gcn::Graphics* graphics) { 00038 graphics->setFont(getFont()); 00039 graphics->drawText(getCaption(),0,0); 00040 } 00041 00042 void CCButton::setEnabled(bool enable) { 00043 gcn::Button::setEnabled(enable); 00044 00045 if (!enable) { 00046 setFont(m_disabledFont->GetImageFont()); 00047 } else if (hasMouse()) { 00048 setFont(m_highlightFont->GetImageFont()); 00049 } else { 00050 setFont(m_mainFont->GetImageFont()); 00051 } 00052 } 00053 00054 void CCButton::mouseIn() { 00055 gcn::Button::mouseIn(); 00056 if (isEnabled()) { 00057 setFont(m_highlightFont->GetImageFont()); 00058 } 00059 } 00060 00061 void CCButton::mouseOut() { 00062 gcn::Button::mouseOut(); 00063 if (isEnabled()) { 00064 setFont(m_mainFont->GetImageFont()); 00065 } 00066 } 00067 00068 void CCButton::SetMainFont(string fontName) { 00069 m_mainFont.reset(new CCFont(fontName)); 00070 } 00071 00072 void CCButton::SetHighlightFont(string fontName) { 00073 m_highlightFont.reset(new CCFont(fontName)); 00074 } 00075 00076 void CCButton::SetDisabledFont(string fontName) { 00077 m_disabledFont.reset(new CCFont(fontName)); 00078 } 00079 00080 void CCButton::SetRecallFunction(cCallback *recallFunc) { 00081 m_recallFunction = recallFunc; 00082 } 00083 00084 void CCButton::SetID(int id) { 00085 m_id = id; 00086 } 00087 00088 int CCButton::GetID() { 00089 return m_id; 00090 } 00091 00092 void CCButton::action(const std::string &eventId) { 00093 if ((eventId.compare(getEventId()) == 0) && (m_recallFunction != NULL) && (isEnabled())) { 00094 m_recallFunction->Execute(m_id); 00095 } 00096 }
1.4.7