00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include "pgButton.h"
00020 #include "pgMouseWatcherParameter.h"
00021
00022 #include "throw_event.h"
00023 #include "mouseButton.h"
00024 #include "mouseWatcherParameter.h"
00025 #include "colorAttrib.h"
00026 #include "transformState.h"
00027
00028 TypeHandle PGButton::_type_handle;
00029
00030
00031
00032
00033
00034
00035 PGButton::
00036 PGButton(const string &name) : PGItem(name)
00037 {
00038 _button_down = false;
00039 _click_buttons.insert(MouseButton::one());
00040
00041 set_active(true);
00042 }
00043
00044
00045
00046
00047
00048
00049 PGButton::
00050 ~PGButton() {
00051 }
00052
00053
00054
00055
00056
00057
00058 PGButton::
00059 PGButton(const PGButton ©) :
00060 PGItem(copy)
00061 {
00062 _button_down = false;
00063 }
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073 PandaNode *PGButton::
00074 make_copy() const {
00075 return new PGButton(*this);
00076 }
00077
00078
00079
00080
00081
00082
00083
00084 void PGButton::
00085 enter(const MouseWatcherParameter ¶m) {
00086 if (get_active()) {
00087 set_state(_button_down ? S_depressed : S_rollover);
00088 }
00089 PGItem::enter(param);
00090 }
00091
00092
00093
00094
00095
00096
00097
00098 void PGButton::
00099 exit(const MouseWatcherParameter ¶m) {
00100 if (get_active()) {
00101 set_state(S_ready);
00102 }
00103 PGItem::exit(param);
00104 }
00105
00106
00107
00108
00109
00110
00111
00112
00113 void PGButton::
00114 press(const MouseWatcherParameter ¶m, bool background) {
00115 if (has_click_button(param.get_button())) {
00116 if (get_active()) {
00117 _button_down = true;
00118 set_state(S_depressed);
00119 }
00120 }
00121 PGItem::press(param, background);
00122 }
00123
00124
00125
00126
00127
00128
00129
00130
00131 void PGButton::
00132 release(const MouseWatcherParameter ¶m, bool background) {
00133 if (has_click_button(param.get_button())) {
00134 _button_down = false;
00135 if (get_active()) {
00136 if (param.is_outside()) {
00137 set_state(S_ready);
00138 } else {
00139 set_state(S_rollover);
00140 click(param);
00141 }
00142 }
00143 }
00144 PGItem::release(param, background);
00145 }
00146
00147
00148
00149
00150
00151
00152
00153 void PGButton::
00154 click(const MouseWatcherParameter ¶m) {
00155 PGMouseWatcherParameter *ep = new PGMouseWatcherParameter(param);
00156 string event = get_click_event(param.get_button());
00157 play_sound(event);
00158 throw_event(event, EventParameter(ep));
00159 }
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170 void PGButton::
00171 setup(const string &label) {
00172 clear_state_def(S_ready);
00173 clear_state_def(S_depressed);
00174 clear_state_def(S_rollover);
00175 clear_state_def(S_inactive);
00176
00177 TextNode *text_node = get_text_node();
00178 text_node->set_text(label);
00179 PT(PandaNode) geom = text_node->generate();
00180
00181 LVecBase4f frame = text_node->get_card_actual();
00182 set_frame(frame[0] - 0.4f, frame[1] + 0.4f, frame[2] - 0.15f, frame[3] + 0.15f);
00183
00184 PT(PandaNode) ready = new PandaNode("ready");
00185 PT(PandaNode) depressed = new PandaNode("depressed");
00186 PT(PandaNode) rollover = new PandaNode("rollover");
00187 PT(PandaNode) inactive = new PandaNode("inactive");
00188
00189 get_state_def(S_ready).attach_new_node(ready);
00190 get_state_def(S_depressed).attach_new_node(depressed);
00191 get_state_def(S_rollover).attach_new_node(rollover);
00192 get_state_def(S_inactive).attach_new_node(inactive);
00193
00194 ready->add_child(geom);
00195 depressed->add_child(geom);
00196 rollover->add_child(geom);
00197 inactive->add_child(geom);
00198
00199 PGFrameStyle style;
00200 style.set_color(0.8f, 0.8f, 0.8f, 1.0f);
00201 style.set_width(0.1f, 0.1f);
00202
00203 style.set_type(PGFrameStyle::T_bevel_out);
00204 set_frame_style(S_ready, style);
00205
00206 style.set_color(0.9f, 0.9f, 0.9f, 1.0f);
00207 set_frame_style(S_rollover, style);
00208
00209 inactive->set_attrib(ColorAttrib::make_flat(Colorf(0.8f, 0.8f, 0.8f, 1.0f)));
00210 style.set_color(0.6f, 0.6f, 0.6f, 1.0f);
00211 set_frame_style(S_inactive, style);
00212
00213 style.set_type(PGFrameStyle::T_bevel_in);
00214 style.set_color(0.8f, 0.8f, 0.8f, 1.0f);
00215 set_frame_style(S_depressed, style);
00216 depressed->set_transform(TransformState::make_pos(LVector3f(0.05f, 0.0f, -0.05f)));
00217 }
00218
00219
00220
00221
00222
00223
00224
00225 void PGButton::
00226 setup(const NodePath &ready, const NodePath &depressed,
00227 const NodePath &rollover, const NodePath &inactive) {
00228 clear_state_def(S_ready);
00229 clear_state_def(S_depressed);
00230 clear_state_def(S_rollover);
00231 clear_state_def(S_inactive);
00232
00233 instance_to_state_def(S_ready, ready);
00234 instance_to_state_def(S_depressed, depressed);
00235 instance_to_state_def(S_rollover, rollover);
00236 instance_to_state_def(S_inactive, inactive);
00237 }
00238
00239
00240
00241
00242
00243
00244
00245
00246 void PGButton::
00247 set_active(bool active) {
00248 if (active != get_active()) {
00249 PGItem::set_active(active);
00250 set_state(active ? S_ready : S_inactive);
00251 }
00252 }
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262 bool PGButton::
00263 add_click_button(const ButtonHandle &button) {
00264 return _click_buttons.insert(button).second;
00265 }
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276 bool PGButton::
00277 remove_click_button(const ButtonHandle &button) {
00278 return (_click_buttons.erase(button) != 0);
00279 }
00280
00281
00282
00283
00284
00285
00286
00287
00288 bool PGButton::
00289 has_click_button(const ButtonHandle &button) {
00290 return (_click_buttons.count(button) != 0);
00291 }