File: chat.cpp

package info (click to toggle)
warmux 1%3A11.04.1%2Brepack2-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, buster, sid
  • size: 126,388 kB
  • sloc: cpp: 186,040; xml: 8,909; sh: 3,358; makefile: 1,052; ansic: 713
file content (162 lines) | stat: -rw-r--r-- 4,001 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/******************************************************************************
 *  Warmux is a convivial mass murder game.
 *  Copyright (C) 2007 Jon de Andres
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
 ******************************************************************************
 * Chat in game session.
 * nefertum - Jon de Andres
 *****************************************************************************/

#include "game/game_time.h"
#include "graphic/text.h"
#include "graphic/text_list.h"
#include "graphic/video.h"
#include "include/action_handler.h"
#include "include/app.h"
#include "network/chat.h"
#include "network/admin_commands.h"
#include "network/network.h"
#include "tool/text_handling.h"

#include <SDL_events.h>

#define HEIGHT       15
#define XPOS         25
#define YPOS        130
#define MAXLINES     10 //Fidel's advise
#define MAXSECONDS   40

Chat::~Chat()
{
  delete msg;
  delete input;
}

Chat::Chat():
  input(NULL),
  msg(NULL),
  cursor_pos(0),
  check_input(false),
  last_time(0)
{
}

void Chat::Show()
{
  uint now = GameTime::GetInstance()->ReadSec();

  if (now - last_time >= MAXSECONDS){
    chat.DeleteLine();
    last_time = now;
  }

  chat.Draw(XPOS, YPOS, HEIGHT);

  if(check_input)
    ShowInput();
}

void Chat::ShowInput()
{
  if (!check_input) {
    check_input = true;

    /* Enable key repeat when chatting :) */
    SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
  }

  if (!input) {
    input = new Text("", c_white);
    msg = new Text(_("Say: "), c_red);
  }

  /* FIXME where do those constants come from ?*/
  int ypos = GetMainWindow().GetHeight() - 100;
  msg->DrawLeftTop(Point2i(25, ypos));
  if (input->GetText() != "") {
    int x = 25 + msg->GetWidth() + 5;
    input->DrawCursor(Point2i(x, ypos), cursor_pos, input->GetWidth());
    input->DrawLeftTop(Point2i(x, ypos));
  }
}

void Chat::NewMessage(const std::string &msg, const Color& color)
{
  if (!chat.Size()){
    uint now = GameTime::GetInstance()->ReadSec();
    last_time = now;
  }

  chat.AddText(msg, color, MAXLINES);
}

void Chat::SendMessage(const std::string &msg, bool in_game)
{
  if (msg.size() == 0)
    return;

  Action* a = new Action((in_game) ? Action::ACTION_CHAT_INGAME_MESSAGE
                                   : Action::ACTION_CHAT_MENU_MESSAGE);
  a->Push(Network::GetInstance()->GetPlayer().GetId());
  a->Push(msg);
  ActionHandler::GetInstance()->NewAction(a);
}

void Chat::CloseInput()
{
  check_input = false; //Hide input widget

  input->SetText("");
  cursor_pos = 0;

  // Disable key repeat during the game!
  SDL_EnableKeyRepeat(0, 0);
}

void Chat::HandleKeyPressed(const SDL_Event& evnt)
{
  SDL_KeyboardEvent kbd_event = evnt.key;
  SDL_keysym key = kbd_event.keysym;
  std::string txt = input->GetText();

  if (TextHandle(txt, cursor_pos, key))
    input->SetText(txt);
}

void Chat::HandleKeyReleased(const SDL_Event& evnt)
{
  SDL_KeyboardEvent kbd_event = evnt.key;
  SDL_keysym key = kbd_event.keysym;
  std::string txt = input->GetText();

  switch (key.sym) {

  case SDLK_RETURN:
  case SDLK_KP_ENTER:
    if (txt[0] == '/')
      ProcessCommand(txt);
    else if (txt != "")
      SendMessage(txt);

    CloseInput();
    break;
  case SDLK_ESCAPE:
    CloseInput();
    break;
  default:
    break;
  }
}