Panda3D
|
00001 // Filename: buttonEvent.cxx 00002 // Created by: drose (01Mar00) 00003 // 00004 //////////////////////////////////////////////////////////////////// 00005 // 00006 // PANDA 3D SOFTWARE 00007 // Copyright (c) Carnegie Mellon University. All rights reserved. 00008 // 00009 // All use of this software is subject to the terms of the revised BSD 00010 // license. You should have received a copy of this license along 00011 // with this source code in a file named "LICENSE." 00012 // 00013 //////////////////////////////////////////////////////////////////// 00014 00015 #include "buttonEvent.h" 00016 #include "datagram.h" 00017 #include "datagramIterator.h" 00018 #include "buttonRegistry.h" 00019 #include "textEncoder.h" 00020 00021 //////////////////////////////////////////////////////////////////// 00022 // Function: ButtonEvent::output 00023 // Access: Public 00024 // Description: 00025 //////////////////////////////////////////////////////////////////// 00026 void ButtonEvent:: 00027 output(ostream &out) const { 00028 switch (_type) { 00029 case T_down: 00030 out << "button " << _button << " down"; 00031 break; 00032 00033 case T_resume_down: 00034 out << "button " << _button << " resume down"; 00035 break; 00036 00037 case T_up: 00038 out << "button " << _button << " up"; 00039 break; 00040 00041 case T_repeat: 00042 out << "button " << _button << " repeat"; 00043 break; 00044 00045 case T_keystroke: 00046 out << "keystroke " << _keycode; 00047 break; 00048 00049 case T_candidate: 00050 out << "candidate " 00051 << TextEncoder::encode_wtext(_candidate_string, 00052 TextEncoder::get_default_encoding()); 00053 break; 00054 00055 case T_move: 00056 out << "move"; 00057 break; 00058 } 00059 } 00060 00061 //////////////////////////////////////////////////////////////////// 00062 // Function: ButtonEvent::write_datagram 00063 // Access: Public 00064 // Description: Writes the event into a datagram. 00065 //////////////////////////////////////////////////////////////////// 00066 void ButtonEvent:: 00067 write_datagram(Datagram &dg) const { 00068 dg.add_uint8(_type); 00069 switch (_type) { 00070 case T_down: 00071 case T_resume_down: 00072 case T_up: 00073 case T_repeat: 00074 // We write the button name. This is not particularly compact, but 00075 // presumably we don't get thousands of button events per frame, and 00076 // it is robust as the button index may change between sessions but 00077 // the button name will not. 00078 dg.add_string(_button.get_name()); 00079 break; 00080 00081 case T_keystroke: 00082 dg.add_int16(_keycode); 00083 break; 00084 00085 case T_candidate: 00086 // We should probably store the wtext directly in the datagram 00087 // rather than encoding it, but I don't feel like adding 00088 // add_wstring() to datagram right now. 00089 dg.add_string(TextEncoder::encode_wtext(_candidate_string, 00090 TextEncoder::get_default_encoding())); 00091 dg.add_uint16(_highlight_start); 00092 dg.add_uint16(_highlight_end); 00093 dg.add_uint16(_cursor_pos); 00094 00095 case T_move: 00096 break; 00097 } 00098 } 00099 00100 //////////////////////////////////////////////////////////////////// 00101 // Function: ButtonEvent::read_datagram 00102 // Access: Public 00103 // Description: Restores the event from the datagram. 00104 //////////////////////////////////////////////////////////////////// 00105 void ButtonEvent:: 00106 read_datagram(DatagramIterator &scan) { 00107 _type = (Type)scan.get_uint8(); 00108 switch (_type) { 00109 case T_down: 00110 case T_resume_down: 00111 case T_up: 00112 case T_repeat: 00113 _button = ButtonRegistry::ptr()->get_button(scan.get_string()); 00114 break; 00115 00116 case T_keystroke: 00117 _keycode = scan.get_int16(); 00118 break; 00119 00120 case T_candidate: 00121 _candidate_string = TextEncoder::decode_text(scan.get_string(), 00122 TextEncoder::get_default_encoding()); 00123 _highlight_start = scan.get_uint16(); 00124 _highlight_end = scan.get_uint16(); 00125 _cursor_pos = scan.get_uint16(); 00126 00127 case T_move: 00128 break; 00129 } 00130 }