00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "clientButtonDevice.h"
00021
00022 #include "indent.h"
00023
00024 TypeHandle ClientButtonDevice::_type_handle;
00025
00026
00027
00028
00029
00030
00031 ClientButtonDevice::
00032 ClientButtonDevice(ClientBase *client, const string &device_name):
00033 ClientDevice(client, get_class_type(), device_name)
00034 {
00035 _button_events = new ButtonEventList();
00036 }
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048 void ClientButtonDevice::
00049 set_button_state(int index, bool down) {
00050 ensure_button_index(index);
00051 nassertv(index >= 0 && index < (int)_buttons.size());
00052 _buttons[index]._state = down ? S_down : S_up;
00053
00054 ButtonHandle handle = _buttons[index]._handle;
00055 if (handle != ButtonHandle::none()) {
00056 _button_events->add_event(ButtonEvent(handle, down ? ButtonEvent::T_down : ButtonEvent::T_up));
00057 }
00058 }
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068 void ClientButtonDevice::
00069 ensure_button_index(int index) {
00070 nassertv(index >= 0);
00071
00072 _buttons.reserve(index + 1);
00073 while ((int)_buttons.size() <= index) {
00074 _buttons.push_back(ButtonState());
00075 }
00076 }
00077
00078
00079
00080
00081
00082
00083 void ClientButtonDevice::
00084 output(ostream &out) const {
00085 out << get_type() << " " << get_device_name() << " (";
00086 output_buttons(out);
00087 out << ")";
00088 }
00089
00090
00091
00092
00093
00094
00095 void ClientButtonDevice::
00096 write(ostream &out, int indent_level) const {
00097 indent(out, indent_level) << get_type() << " " << get_device_name() << ":\n";
00098 write_buttons(out, indent_level + 2);
00099 }
00100
00101
00102
00103
00104
00105
00106
00107 void ClientButtonDevice::
00108 output_buttons(ostream &out) const {
00109 bool any_buttons = false;
00110 Buttons::const_iterator bi;
00111 for (bi = _buttons.begin(); bi != _buttons.end(); ++bi) {
00112 const ButtonState &state = (*bi);
00113 if (state._state != S_unknown) {
00114 if (any_buttons) {
00115 out << ", ";
00116 }
00117 any_buttons = true;
00118 out << (int)(bi - _buttons.begin()) << "=";
00119 if (state._state == S_up) {
00120 out << "up";
00121 } else {
00122 out << "down";
00123 }
00124 }
00125 }
00126
00127 if (!any_buttons) {
00128 out << "no known buttons";
00129 }
00130 }
00131
00132
00133
00134
00135
00136
00137
00138 void ClientButtonDevice::
00139 write_buttons(ostream &out, int indent_level) const {
00140 bool any_buttons = false;
00141 Buttons::const_iterator bi;
00142 for (bi = _buttons.begin(); bi != _buttons.end(); ++bi) {
00143 const ButtonState &state = (*bi);
00144 if (state._state != S_unknown) {
00145 any_buttons = true;
00146
00147 indent(out, indent_level)
00148 << (int)(bi - _buttons.begin()) << ". ";
00149
00150 if (state._handle != ButtonHandle::none()) {
00151 out << "(" << state._handle << ") ";
00152 }
00153
00154 if (state._state == S_up) {
00155 out << "up";
00156 } else {
00157 out << "down";
00158 }
00159 out << "\n";
00160 }
00161 }
00162
00163 if (!any_buttons) {
00164 indent(out, indent_level)
00165 << "(no known buttons)\n";
00166 }
00167 }