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