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