00001 #include "gui_checkbutton.hpp" 00002 #include "gui_frame.hpp" 00003 #include "gui_manager.hpp" 00004 #include "gui_texture.hpp" 00005 #include "gui_out.hpp" 00006 00007 namespace gui 00008 { 00009 check_button::check_button(manager* pManager) : button(pManager), 00010 bChecked_(false), pCheckedTexture_(nullptr), pDisabledCheckedTexture_(nullptr) 00011 { 00012 lType_.push_back("CheckButton"); 00013 } 00014 00015 check_button::~check_button() 00016 { 00017 } 00018 00019 std::string check_button::serialize(const std::string& sTab) const 00020 { 00021 return button::serialize(sTab); 00022 } 00023 00024 void check_button::copy_from(uiobject* pObj) 00025 { 00026 button::copy_from(pObj); 00027 00028 check_button* pButton = dynamic_cast<check_button*>(pObj); 00029 00030 if (pButton) 00031 { 00032 if (pButton->get_checked_texture()) 00033 { 00034 this->create_checked_texture_(); 00035 if (this->is_virtual()) 00036 pCheckedTexture_->set_virtual(); 00037 pCheckedTexture_->set_name(pButton->get_checked_texture()->get_name()); 00038 if (!pManager_->add_uiobject(pCheckedTexture_)) 00039 { 00040 gui::out << gui::warning << "gui::" << lType_.back() << " : " 00041 "Trying to add \""+pButton->get_checked_texture()->get_name()+"\" to \""+sName_+"\",\n" 00042 "but its name was already taken : \""+pCheckedTexture_->get_name()+"\". Skipped." << std::endl; 00043 delete pCheckedTexture_; pCheckedTexture_ = nullptr; 00044 } 00045 else 00046 { 00047 if (!is_virtual()) 00048 pCheckedTexture_->create_glue(); 00049 pCheckedTexture_->set_draw_layer(pButton->get_checked_texture()->get_draw_layer()); 00050 this->add_region(pCheckedTexture_); 00051 pCheckedTexture_->copy_from(pButton->get_checked_texture()); 00052 pCheckedTexture_->notify_loaded(); 00053 } 00054 } 00055 if (pButton->get_disabled_checked_texture()) 00056 { 00057 this->create_disabled_checked_texture_(); 00058 if (this->is_virtual()) 00059 pDisabledCheckedTexture_->set_virtual(); 00060 pDisabledCheckedTexture_->set_name(pButton->get_disabled_checked_texture()->get_name()); 00061 if (!pManager_->add_uiobject(pDisabledCheckedTexture_)) 00062 { 00063 gui::out << gui::warning << "gui::" << lType_.back() << " : " 00064 "Trying to add \""+pButton->get_disabled_checked_texture()->get_name()+"\" to \""+sName_+"\",\n" 00065 "but its name was already taken : \""+pDisabledCheckedTexture_->get_name()+"\". Skipped." << std::endl; 00066 delete pDisabledTexture_; pDisabledTexture_ = nullptr; 00067 } 00068 else 00069 { 00070 if (!is_virtual()) 00071 pDisabledCheckedTexture_->create_glue(); 00072 pDisabledCheckedTexture_->set_draw_layer(pButton->get_disabled_checked_texture()->get_draw_layer()); 00073 this->add_region(pDisabledCheckedTexture_); 00074 pDisabledCheckedTexture_->copy_from(pButton->get_disabled_checked_texture()); 00075 pDisabledCheckedTexture_->notify_loaded(); 00076 } 00077 } 00078 } 00079 } 00080 00081 void check_button::check() 00082 { 00083 if (!bChecked_) 00084 { 00085 if (mState_ == STATE_DISABLED) 00086 { 00087 if (pDisabledCheckedTexture_) 00088 pDisabledCheckedTexture_->show(); 00089 else if (pCheckedTexture_) 00090 pCheckedTexture_->show(); 00091 } 00092 else 00093 { 00094 if (pCheckedTexture_) 00095 pCheckedTexture_->show(); 00096 } 00097 00098 bChecked_ = true; 00099 } 00100 } 00101 00102 void check_button::uncheck() 00103 { 00104 if (bChecked_) 00105 { 00106 if (pDisabledCheckedTexture_) 00107 pDisabledCheckedTexture_->hide(); 00108 00109 if (pCheckedTexture_) 00110 pCheckedTexture_->hide(); 00111 00112 bChecked_ = false; 00113 } 00114 } 00115 00116 void check_button::disable() 00117 { 00118 button::disable(); 00119 00120 if (is_enabled() && is_checked() && pDisabledCheckedTexture_) 00121 { 00122 if (pCheckedTexture_) 00123 pCheckedTexture_->hide(); 00124 00125 pDisabledCheckedTexture_->show(); 00126 } 00127 } 00128 00129 void check_button::enable() 00130 { 00131 button::enable(); 00132 00133 if (!is_enabled() && is_checked() && pDisabledCheckedTexture_) 00134 { 00135 if (pCheckedTexture_) 00136 pCheckedTexture_->show(); 00137 00138 pDisabledCheckedTexture_->hide(); 00139 } 00140 } 00141 00142 void check_button::release() 00143 { 00144 button::release(); 00145 00146 if (is_checked()) 00147 uncheck(); 00148 else 00149 check(); 00150 } 00151 00152 bool check_button::is_checked() 00153 { 00154 return bChecked_; 00155 } 00156 00157 texture* check_button::get_checked_texture() 00158 { 00159 return pCheckedTexture_; 00160 } 00161 00162 texture* check_button::get_disabled_checked_texture() 00163 { 00164 return pDisabledCheckedTexture_; 00165 } 00166 00167 void check_button::set_checked_texture(texture* pTexture) 00168 { 00169 pCheckedTexture_ = pTexture; 00170 } 00171 00172 void check_button::set_disabled_checked_texture(texture* pTexture) 00173 { 00174 pDisabledCheckedTexture_ = pTexture; 00175 } 00176 00177 void check_button::create_glue() 00178 { 00179 if (bVirtual_) 00180 { 00181 utils::wptr<lua::state> pLua = pManager_->get_lua(); 00182 pLua->push_number(uiID_); 00183 lGlueList_.push_back(pLua->push_new<lua_virtual_glue>()); 00184 pLua->set_global(sLuaName_); 00185 pLua->pop(); 00186 } 00187 else 00188 { 00189 utils::wptr<lua::state> pLua = pManager_->get_lua(); 00190 pLua->push_string(sLuaName_); 00191 lGlueList_.push_back(pLua->push_new<lua_check_button>()); 00192 pLua->set_global(sLuaName_); 00193 pLua->pop(); 00194 } 00195 } 00196 00197 texture* check_button::create_checked_texture_() 00198 { 00199 if (!pCheckedTexture_) 00200 { 00201 pCheckedTexture_ = new texture(pManager_); 00202 pCheckedTexture_->set_special(); 00203 pCheckedTexture_->set_parent(this); 00204 pCheckedTexture_->set_draw_layer("ARTWORK"); 00205 } 00206 00207 return pCheckedTexture_; 00208 } 00209 00210 texture* check_button::create_disabled_checked_texture_() 00211 { 00212 if (!pDisabledCheckedTexture_) 00213 { 00214 pDisabledCheckedTexture_ = new texture(pManager_); 00215 pDisabledCheckedTexture_->set_special(); 00216 pDisabledCheckedTexture_->set_parent(this); 00217 pDisabledCheckedTexture_->set_draw_layer("ARTWORK"); 00218 } 00219 00220 return pDisabledCheckedTexture_; 00221 } 00222 }