sources/interface.cpp

changeset 88
08ccaf26cffd
parent 86
3c02a728a681
child 89
777b2a10b835
equal deleted inserted replaced
87:53c2aecb9704 88:08ccaf26cffd
26 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 26 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 27 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */ 29 */
30 30
31 #include <curses.h>
31 #include <string.h> 32 #include <string.h>
32 #include <time.h> 33 #include <time.h>
33 #include "interface.h" 34 #include "interface.h"
34 #include "network/rconsession.h" 35 #include "network/rconsession.h"
35 #include "network/ipaddress.h" 36 #include "network/ipaddress.h"
36 #include "coloredline.h" 37 #include "coloredline.h"
38 BEGIN_ZFC_NAMESPACE
37 39
38 static const int g_pageSize = 10; 40 static const int g_pageSize = 10;
39 41
40 // ------------------------------------------------------------------------------------------------- 42 // -------------------------------------------------------------------------------------------------
41 // 43 //
125 127
126 switch (newstate) 128 switch (newstate)
127 { 129 {
128 case INPUTSTATE_ADDRESS: 130 case INPUTSTATE_ADDRESS:
129 if (CurrentAddress.host != 0) 131 if (CurrentAddress.host != 0)
130 mutable_current_input() = CurrentAddress.to_string (IP_WITH_PORT); 132 mutable_current_input() = CurrentAddress.to_string (IPAddress::WITH_PORT);
131 break; 133 break;
132 134
133 default: 135 default:
134 break; 136 break;
135 } 137 }
139 } 141 }
140 142
141 // ------------------------------------------------------------------------------------------------- 143 // -------------------------------------------------------------------------------------------------
142 // 144 //
143 Interface::Interface() : 145 Interface::Interface() :
144 Session (this) 146 Session (this),
147 InputCursor (0),
148 CursorPosition (0),
149 InputPanning (0),
150 NeedRefresh (false),
151 NeedStatusBarRender (false),
152 NeedInputRender (false),
153 NeedOutputRender (false),
154 NeedNicklistRender (false),
155 OutputScroll (0),
156 CurrentInputState (INPUTSTATE_NORMAL),
157 DisconnectConfirmFunction (NULL)
145 { 158 {
146 #ifdef XCURSES 159 #ifdef XCURSES
147 ::Xinitscr(argc, argv); 160 ::Xinitscr(argc, argv);
148 #else 161 #else
149 ::initscr(); 162 ::initscr();
217 render_titlebar(); 230 render_titlebar();
218 } 231 }
219 232
220 // ------------------------------------------------------------------------------------------------- 233 // -------------------------------------------------------------------------------------------------
221 // 234 //
222 void Interface::safe_disconnect (Function<void()> afterwards) 235 void Interface::safe_disconnect (std::function<void()> afterwards)
223 { 236 {
224 if (Session.is_active()) 237 if (Session.is_active())
225 { 238 {
226 DisconnectConfirmFunction = afterwards; 239 DisconnectConfirmFunction = afterwards;
227 set_input_state (INPUTSTATE_CONFIRM_DISCONNECTION); 240 set_input_state (INPUTSTATE_CONFIRM_DISCONNECTION);
250 // 263 //
251 int Interface::render_colorline (int y, int x0, int width, const ColoredLine& line, bool allowWrap) 264 int Interface::render_colorline (int y, int x0, int width, const ColoredLine& line, bool allowWrap)
252 { 265 {
253 int x = x0; 266 int x = x0;
254 267
255 for (int byte : line.data()) 268 for (int i = 0; i < line.length(); ++i)
256 { 269 {
270 int byte = line.data()[i];
271
257 if (x == x0 + width) 272 if (x == x0 + width)
258 { 273 {
259 if (not allowWrap) 274 if (not allowWrap)
260 return y; 275 return y;
261 276
441 int y = LINES - 2; 456 int y = LINES - 2;
442 457
443 // If we're inputting a password, replace it with asterisks 458 // If we're inputting a password, replace it with asterisks
444 if (CurrentInputState == INPUTSTATE_PASSWORD) 459 if (CurrentInputState == INPUTSTATE_PASSWORD)
445 { 460 {
446 for (char& ch : displayString) 461 for (int i = 0; i < displayString.length(); ++i)
447 ch = '*'; 462 displayString[i] = '*';
448 } 463 }
449 464
450 // Ensure the cursor is within bounds 465 // Ensure the cursor is within bounds
451 CursorPosition = clamp (CursorPosition, 0, displayString.length()); 466 CursorPosition = clamp (CursorPosition, 0, displayString.length());
452 467
504 text = "Disconnected."; 519 text = "Disconnected.";
505 break; 520 break;
506 521
507 case RCON_CONNECTING: 522 case RCON_CONNECTING:
508 case RCON_AUTHENTICATING: 523 case RCON_AUTHENTICATING:
509 text = "Connecting to " + Session.address().to_string (IP_WITH_PORT) + "..."; 524 text = "Connecting to " + Session.address().to_string (IPAddress::WITH_PORT) + "...";
510 break; 525 break;
511 526
512 case RCON_CONNECTED: 527 case RCON_CONNECTED:
513 { 528 {
514 String adminText; 529 String adminText;
521 { 536 {
522 adminText.sprintf ("%d other admin%s", Session.num_admins(), 537 adminText.sprintf ("%d other admin%s", Session.num_admins(),
523 Session.num_admins() != 1 ? "s" : ""); 538 Session.num_admins() != 1 ? "s" : "");
524 } 539 }
525 540
526 text.sprintf ("%s | %s | %s", Session.address().to_string (IP_WITH_PORT).chars(), 541 text.sprintf ("%s | %s | %s",
527 Session.level().chars(), adminText.chars()); 542 Session.address().to_string (IPAddress::WITH_PORT).chars(),
543 Session.level().chars(),
544 adminText.chars());
528 } 545 }
529 break; 546 break;
530 } 547 }
531 548
532 if (not text.is_empty()) 549 if (not text.is_empty())
948 va_end (args); 965 va_end (args);
949 } 966 }
950 967
951 // ------------------------------------------------------------------------------------------------- 968 // -------------------------------------------------------------------------------------------------
952 // 969 //
953 void Interface::print_to_console (String a) 970 void Interface::print_to_console (String message)
954 { 971 {
955 // Zandronum sometimes sends color codes as "\\c" and sometimes as "\x1C". 972 // Zandronum sometimes sends color codes as "\\c" and sometimes as "\x1C".
956 // Let's correct that on our end and hope this won't cause conflicts. 973 // Let's correct that on our end and hope this won't cause conflicts.
957 a.replace ("\\c", "\x1C"); 974 message.replace ("\\c", "\x1C");
958 975
959 for (char ch : a) 976 for (int i = 0; i < message.length(); ++i)
960 { 977 {
978 char ch = message[i];
979
961 if (ch == '\n') 980 if (ch == '\n')
962 { 981 {
963 OutputLines.last().finalize(); 982 OutputLines.last().finalize();
964 OutputLines << ColoredLine(); 983 OutputLines << ColoredLine();
965 continue; 984 continue;
1026 input.replace (0, part.length(), complete); 1045 input.replace (0, part.length(), complete);
1027 CursorPosition = complete.length(); 1046 CursorPosition = complete.length();
1028 NeedInputRender = true; 1047 NeedInputRender = true;
1029 } 1048 }
1030 } 1049 }
1050
1051 END_ZFC_NAMESPACE

mercurial