Panda3D
buttonEvent.cxx
1 // Filename: buttonEvent.cxx
2 // Created by: drose (01Mar00)
3 //
4 ////////////////////////////////////////////////////////////////////
5 //
6 // PANDA 3D SOFTWARE
7 // Copyright (c) Carnegie Mellon University. All rights reserved.
8 //
9 // All use of this software is subject to the terms of the revised BSD
10 // license. You should have received a copy of this license along
11 // with this source code in a file named "LICENSE."
12 //
13 ////////////////////////////////////////////////////////////////////
14 
15 #include "buttonEvent.h"
16 #include "datagram.h"
17 #include "datagramIterator.h"
18 #include "buttonRegistry.h"
19 #include "textEncoder.h"
20 
21 ////////////////////////////////////////////////////////////////////
22 // Function: ButtonEvent::output
23 // Access: Public
24 // Description:
25 ////////////////////////////////////////////////////////////////////
26 void ButtonEvent::
27 output(ostream &out) const {
28  switch (_type) {
29  case T_down:
30  out << "button " << _button << " down";
31  break;
32 
33  case T_resume_down:
34  out << "button " << _button << " resume down";
35  break;
36 
37  case T_up:
38  out << "button " << _button << " up";
39  break;
40 
41  case T_repeat:
42  out << "button " << _button << " repeat";
43  break;
44 
45  case T_keystroke:
46  out << "keystroke " << _keycode;
47  break;
48 
49  case T_candidate:
50  out << "candidate "
51  << TextEncoder::encode_wtext(_candidate_string,
53  break;
54 
55  case T_move:
56  out << "move";
57  break;
58 
59  case T_raw_down:
60  out << "raw button " << _button << " down";
61  break;
62 
63  case T_raw_up:
64  out << "raw button " << _button << " up";
65  break;
66  }
67 }
68 
69 ////////////////////////////////////////////////////////////////////
70 // Function: ButtonEvent::write_datagram
71 // Access: Public
72 // Description: Writes the event into a datagram.
73 ////////////////////////////////////////////////////////////////////
74 void ButtonEvent::
76  dg.add_uint8(_type);
77  switch (_type) {
78  case T_down:
79  case T_resume_down:
80  case T_up:
81  case T_repeat:
82  case T_raw_down:
83  case T_raw_up:
84  // We write the button name. This is not particularly compact, but
85  // presumably we don't get thousands of button events per frame, and
86  // it is robust as the button index may change between sessions but
87  // the button name will not.
88  dg.add_string(_button.get_name());
89  break;
90 
91  case T_keystroke:
92  dg.add_int16(_keycode);
93  break;
94 
95  case T_candidate:
96  // We should probably store the wtext directly in the datagram
97  // rather than encoding it, but I don't feel like adding
98  // add_wstring() to datagram right now.
99  dg.add_string(TextEncoder::encode_wtext(_candidate_string,
101  dg.add_uint16(_highlight_start);
102  dg.add_uint16(_highlight_end);
103  dg.add_uint16(_cursor_pos);
104 
105  case T_move:
106  break;
107  }
108 }
109 
110 ////////////////////////////////////////////////////////////////////
111 // Function: ButtonEvent::read_datagram
112 // Access: Public
113 // Description: Restores the event from the datagram.
114 ////////////////////////////////////////////////////////////////////
115 void ButtonEvent::
117  _type = (Type)scan.get_uint8();
118  switch (_type) {
119  case T_down:
120  case T_resume_down:
121  case T_up:
122  case T_repeat:
123  case T_raw_down:
124  case T_raw_up:
125  _button = ButtonRegistry::ptr()->get_button(scan.get_string());
126  break;
127 
128  case T_keystroke:
129  _keycode = scan.get_int16();
130  break;
131 
132  case T_candidate:
133  _candidate_string = TextEncoder::decode_text(scan.get_string(),
135  _highlight_start = scan.get_uint16();
136  _highlight_end = scan.get_uint16();
137  _cursor_pos = scan.get_uint16();
138 
139  case T_move:
140  break;
141  }
142 }
ButtonHandle get_button(const string &name)
Finds a ButtonHandle in the registry matching the indicated name.
void add_uint8(PN_uint8 value)
Adds an unsigned 8-bit integer to the datagram.
Definition: datagram.I:138
void add_string(const string &str)
Adds a variable-length string to the datagram.
Definition: datagram.I:351
static Encoding get_default_encoding()
Specifies the default encoding to be used for all subsequently created TextEncoder objects...
Definition: textEncoder.I:97
wstring decode_text(const string &text) const
Returns the given wstring decoded to a single-byte string, via the current encoding system...
Definition: textEncoder.I:568
PN_uint8 get_uint8()
Extracts an unsigned 8-bit integer.
PN_int16 get_int16()
Extracts a signed 16-bit integer.
string get_string()
Extracts a variable-length string.
PN_uint16 get_uint16()
Extracts an unsigned 16-bit integer.
void add_int16(PN_int16 value)
Adds a signed 16-bit integer to the datagram.
Definition: datagram.I:148
void read_datagram(DatagramIterator &scan)
Restores the event from the datagram.
static ButtonRegistry * ptr()
Returns the pointer to the global ButtonRegistry object.
string encode_wtext(const wstring &wtext) const
Encodes a wide-text string into a single-char string, according to the current encoding.
Definition: textEncoder.I:557
string get_name() const
Returns the name of the button.
void add_uint16(PN_uint16 value)
Adds an unsigned 16-bit integer to the datagram.
Definition: datagram.I:181
A class to retrieve the individual data elements previously stored in a Datagram. ...
An ordered list of data elements, formatted in memory for transmission over a socket or writing to a ...
Definition: datagram.h:43
void write_datagram(Datagram &dg) const
Writes the event into a datagram.
Definition: buttonEvent.cxx:75