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 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649
|
/***************************************************************************
messagedlg.cpp - description
-------------------
begin : Mon M� 24 2003
copyright : (C) 2003 by Martin Bickel
email : bickel@asc-hq.org
***************************************************************************/
/***************************************************************************
* *
* 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. *
* *
***************************************************************************/
#include "messagedlg.h"
#include "gamemap.h"
#include "paradialog.h"
#include "gameeventsystem.h"
#include "dialog.h"
#include "dlg_box.h"
#include "spfst.h"
#include "widgets/textrenderer.h"
#include "messages.h"
#include "pgwidget.h"
#include "widgets/playerselector.h"
#include "pgrichedit.h"
#include "pgmultilineedit.h"
#include "pgtooltiphelp.h"
#include "dialogs/fieldmarker.h"
#include "dialogs/selectionwindow.h"
#include "spfst-legacy.h"
class NewMessage : public ASC_PG_Dialog {
GameMap* gamemap;
Message* message;
PG_MultiLineEdit* editor;
PlayerSelector* to;
PlayerSelector* cc;
bool reminder;
bool ok()
{
if ( !message ) {
message = new Message ( editor->GetText(), gamemap, 0, 1 << actmap->actplayer );
gamemap->unsentmessage.push_back ( message );
} else {
message->text = editor->GetText();
}
if ( reminder ) {
message->cc = 0;
message->to = 1 << actmap->actplayer;
} else {
message->cc = cc->getSelectedPlayers();
message->to = to->getSelectedPlayers();
}
message->reminder = reminder;
QuitModal();
return true;
}
bool cancel()
{
QuitModal();
return true;
}
bool insertCursorCoordinates()
{
MapCoordinate pos = gamemap->getCursor();
if ( pos.valid() ) {
editor->InsertText( pos.toString() );
return true;
} else
return false;
}
bool insertCoordinates()
{
SelectFromMap::CoordinateList coordinates;
SelectFromMap sfm( coordinates, gamemap );
sfm.Show();
sfm.RunModal();
ASCString text = "#coord(";
for ( SelectFromMap::CoordinateList::iterator i = coordinates.begin(); i != coordinates.end(); ++i ) {
if ( i != coordinates.begin() )
text += ";";
text += ASCString::toString( i->x ) + "/" + ASCString::toString( i->y );
}
text += ")#";
editor->InsertText( text );
return true;
}
bool eventKeyDown(const SDL_KeyboardEvent* key)
{
int mod = SDL_GetModState() & ~(KMOD_NUM | KMOD_CAPS | KMOD_MODE);
if ( (mod & KMOD_CTRL) &&( key->keysym.sym == SDLK_r ))
return insertCursorCoordinates();
return ASC_PG_Dialog::eventKeyDown(key);
}
public:
NewMessage ( GameMap* gamemap, Message* msg = NULL, bool reminder = false );
void Show(bool fade = false ) {
editor->EditBegin();
ASC_PG_Dialog::Show(fade);
}
};
NewMessage :: NewMessage ( GameMap* gamemap, Message* msg, bool reminder ) : ASC_PG_Dialog( NULL, PG_Rect( -1, -1, 600, 500 ), "new message" )
{
this->gamemap = gamemap;
message = msg;
if ( message )
reminder = message->reminder;
this->reminder = reminder;
int startY = 30;
if ( !reminder ) {
new PG_Label ( this, PG_Rect( 20, 30, 30, 20 ), "TO:");
to = new PlayerSelector ( this, PG_Rect( 50, 30, 150, 150 ), gamemap, true, 1 << gamemap->actplayer );
if ( msg )
to->setSelection( msg->to );
new PG_Label ( this, PG_Rect( 210, 30, 30, 20 ), "CC:" );
cc = new PlayerSelector ( this, PG_Rect( 240, 30, 150, 150 ), gamemap, true, 1 << gamemap->actplayer );
if ( msg )
cc->setSelection( msg->cc );
startY += 170;
}
editor = new PG_MultiLineEdit( this, PG_Rect(20, startY, Width() - 140, Height() - startY - 10 ));
if ( message )
editor->SetText( message->text );
editor->SetInputFocus();
AddStandardButton("OK")->sigClick.connect( sigc::hide( sigc::mem_fun( *this, &NewMessage::ok )));
AddStandardButton("Cancel")->sigClick.connect( sigc::hide( sigc::mem_fun( *this, &NewMessage::cancel )));
AddStandardButton("");
AddStandardButton("Coordinates")->sigClick.connect( sigc::hide( sigc::mem_fun( *this, &NewMessage::insertCoordinates )));
PG_Button* coord = AddStandardButton("Cursor Coord");
coord->sigClick.connect( sigc::hide( sigc::mem_fun( *this, &NewMessage::insertCursorCoordinates )));
new PG_ToolTipHelp ( coord, "ctrl-r");
}
class IngameMessageViewer : public ASC_PG_Dialog {
TextRenderer* textViewer;
const Message* message;
PG_Label* from;
PG_Label* to;
PG_Label* cc;
bool keepMessage;
bool ok()
{
keepMessage = false;
Hide();
QuitModal();
return true;
};
bool keep()
{
keepMessage = true;
Hide();
QuitModal();
return true;
};
PG_Label* addHeaderLine( int y, const ASCString& name )
{
PG_Rect f( 10, y, Width() - 20, 25 );
new PG_Label( this, PG_Rect( f.x, f.y, 50, f.h ), name );
Emboss* emb = new Emboss( this, PG_Rect( f.x + 50, f.y, f.w - 50, f.h ), true );
return new PG_Label( emb, PG_Rect( 2, 2, emb->w - 4, emb->h - 4) );
}
public:
IngameMessageViewer ( const ASCString& title, const Message& msg, PG_Rect rect = PG_Rect( 50, 50, 500, 400 ), bool autoHeader = true ) : ASC_PG_Dialog ( NULL, rect, title ), message(NULL), from(NULL), to(NULL), cc(NULL)
{
keepMessage = false;
int footerHeight;
/* if ( !buttonText.empty() ) { */
if ( msg.reminder ) {
PG_Button* b = new PG_Button( this, PG_Rect( Width() - 70, Height() - 40, 60, 30), "Done" );
b->sigClick.connect( sigc::hide( sigc::mem_fun( *this, &IngameMessageViewer::ok)) );
PG_Button* b2 = new PG_Button( this, PG_Rect( Width() - 140, Height() - 40, 60, 30), "Keep" );
b2->sigClick.connect( sigc::hide( sigc::mem_fun( *this, &IngameMessageViewer::keep)) );
} else {
PG_Button* b = new PG_Button( this, PG_Rect( Width() - 110, Height() - 40, 100, 30), "OK" );
b->sigClick.connect( sigc::hide( sigc::mem_fun( *this, &IngameMessageViewer::ok)) );
}
footerHeight = 50;
/* } else
footerHeight = 10; */
int y = 40;
if ( !msg.getFromText( actmap ).empty() ) {
from = addHeaderLine( y, "From:");
y += 30;
if ( !autoHeader || (msg.to && !(msg.from & 512)) ) {
to = addHeaderLine( y, "To:");
y += 30;
}
if ( !autoHeader || msg.cc ) {
cc = addHeaderLine( y, "CC:");
y += 30;
}
}
PG_Rect r ( 10, y, Width() - 20, Height() - (y + footerHeight ));
new Emboss( this, r, true );
#ifdef RICHEDIT
PG_RichEdit* re = new PG_RichEdit( this, PG_Rect(r.x + 2, r.y+2, r.w-4, r.h-4));
ASCString text = msg.text;
while ( text.find ( "#crt#" ) != ASCString::npos )
text.replace ( text.find ("#crt#"), 5, " \n");
re->SetText( text );
re->SetTransparency(255);
textViewer = re;
#else
TextRenderer* tr = new TextRenderer( this, PG_Rect(r.x + 2, r.y+2, r.w-4, r.h-4));
tr->SetText( msg.text );
textViewer = tr;
#endif
SetMessage( msg );
};
void SetMessage( const Message& msg )
{
if ( &msg != message) {
PG_Application::BulkModeActivator bulk;
textViewer->SetText( msg.text );
if ( from )
from->SetText( msg.getFromText( actmap ));
if ( cc )
cc->SetText( msg.getCcText( actmap ));
if ( to )
to->SetText( msg.getToText( actmap ));
bulk.disable();
Show();
message = &msg;
} else
Show();
};
void Hide (bool fade=false)
{
ASC_PG_Dialog::Hide(fade);
getPGApplication().queueWidgetForDeletion( this );
}
bool eventKeyDown (const SDL_KeyboardEvent *key)
{
if ( key->keysym.sym == SDLK_ESCAPE ) {
quitModalLoop(10);
return true;
}
if ( key->keysym.sym == SDLK_RETURN || key->keysym.sym == SDLK_KP_ENTER ) {
quitModalLoop(11);
return true;
}
if ( key->keysym.sym == SDLK_SPACE ) {
quitModalLoop(12);
return true;
}
int mod = SDL_GetModState() & ~(KMOD_NUM | KMOD_CAPS | KMOD_MODE);
if ( mod & KMOD_CTRL )
if ( key->keysym.sym == 's' )
textViewer->saveText( mod & KMOD_SHIFT );
return ASC_PG_Dialog::eventKeyDown( key );
};
bool getKeepMessage()
{
return keepMessage;
}
~IngameMessageViewer()
{
displayLogMessage ( 9, "~IngameMessageViewer\n" );
}
};
void newmessage()
{
if ( Player::getHumanPlayerNum( actmap ) < 2 ) {
infoMessage( "nobody is listening to our transmissions");
return;
}
// PG_LineEdit::SetBlinkingTime(-1);
NewMessage nm ( actmap );
nm.Show();
nm.RunModal();
}
void newreminder()
{
NewMessage nm ( actmap, NULL, true );
nm.Show();
nm.RunModal();
}
class MessageLineWidget: public SelectionWidget
{
Message* message;
ASCString msgtime;
GameMap* map;
public:
MessageLineWidget( PG_Widget* parent, const PG_Point& pos, int width, Message* msg, GameMap* gamemap );
ASCString getName() const
{
if ( message->from > 0 && message->from <= 255 )
return map->getPlayer(getFirstBit(message->from)).getName();
else
if ( message->from & ( 1 << 9 ))
return "system";
return "";
};
Message* getMessage() const { return message; };
protected:
void display( SDL_Surface * surface, const PG_Rect & src, const PG_Rect & dst )
{
}
;
};
MessageLineWidget::MessageLineWidget( PG_Widget* parent, const PG_Point& pos, int width, Message* msg, GameMap* gamemap ) : SelectionWidget( parent, PG_Rect( pos.x, pos.y, width, 20 )), message( msg ), map(gamemap)
{
#ifndef ctime_r
msgtime = ctime( &msg->time);
#else
char c[100];
ctime_r( &msg->time, c );
msgtime = c;
#endif
int col1 = width * 3 / 10;
int col2 = col1 + width * 4 / 10;
PG_Label* lbl1 = new PG_Label( this, PG_Rect( 0, 0, col1 - 10, Height() ), getName() );
lbl1->SetFontSize( lbl1->GetFontSize() -2 );
PG_Label* lbl2 = new PG_Label( this, PG_Rect( col1, 0, col2-col1-10, Height() ), msgtime );
lbl2->SetFontSize( lbl2->GetFontSize() -2 );
int x = 0;
for ( int i = 0; i< gamemap->getPlayerCount(); ++i )
if ( msg->to & (1 << i)) {
new ColoredBar( gamemap->getPlayer(i).getColor(), this, PG_Rect( col2 + x, 0, 15 , 15 ));
x += 18;
}
SetTransparency( 255 );
};
class MessageListItemFactory: public SelectionItemFactory {
protected:
const MessagePntrContainer& messageContainer;
MessagePntrContainer::const_iterator it;
GameMap* gamemap;
public:
MessageListItemFactory( const MessagePntrContainer& messages, GameMap* g );
void restart();
SelectionWidget* spawnNextItem( PG_Widget* parent, const PG_Point& pos );
void itemMarked( const SelectionWidget* widget );
void itemSelected( const SelectionWidget* widget, bool mouse );
sigc::signal<void, Message* > messageSelected;
};
MessageListItemFactory::MessageListItemFactory( const MessagePntrContainer& messages, GameMap* map ) : messageContainer ( messages ), gamemap(map)
{
restart();
};
void MessageListItemFactory::restart()
{
it = messageContainer.begin();
};
SelectionWidget* MessageListItemFactory::spawnNextItem( PG_Widget* parent, const PG_Point& pos )
{
if ( it != messageContainer.end() )
return new MessageLineWidget( parent, pos, parent->Width() - 15, *(it++), gamemap );
else
return NULL;
};
void MessageListItemFactory::itemMarked( const SelectionWidget* widget )
{
if ( !widget )
return;
const MessageLineWidget* mlw = dynamic_cast< const MessageLineWidget*>(widget);
assert( mlw );
messageSelected( mlw->getMessage() );
}
void MessageListItemFactory::itemSelected( const SelectionWidget* widget, bool mouse )
{
if ( !widget )
return;
const MessageLineWidget* fw = dynamic_cast<const MessageLineWidget*>(widget);
assert( fw );
messageSelected( fw->getMessage() );
}
class MessageSelectionWindow : public ASC_PG_Dialog {
private:
bool edit;
bool viewerDeleted( const PG_MessageObject* obj )
{
if ( viewer == obj )
viewer = NULL;
return true;
};
bool ProcessEvent ( const SDL_Event * event, bool bModal = false )
{
if ( ASC_PG_Dialog::ProcessEvent( event, bModal ) )
return true;
if ( viewer )
if ( viewer->ProcessEvent( event, bModal ))
return true;
return false;
};
bool eventKeyDown(const SDL_KeyboardEvent* key)
{
int mod = SDL_GetModState() & ~(KMOD_NUM | KMOD_CAPS | KMOD_MODE);
if ( mod & KMOD_CTRL )
if ( key->keysym.sym == 's' )
if ( viewer )
return viewer->eventKeyDown( key );
return ASC_PG_Dialog::eventKeyDown(key);
}
protected:
void messageSelected( Message* msg );
IngameMessageViewer* viewer;
public:
MessageSelectionWindow( PG_Widget *parent, const PG_Rect &r, const MessagePntrContainer& messages, GameMap* g, bool editable );
};
void MessageSelectionWindow::messageSelected( Message* msg )
{
if ( edit ) {
NewMessage nm ( actmap, msg );
nm.Show();
nm.RunModal();
} else {
if ( !viewer ) {
PG_Rect r ( my_xpos + Width(), my_ypos, min( PG_Application::GetScreenWidth()/2, 500), Height() );
viewer = new IngameMessageViewer( "Message", *msg, r );
viewer->Show();
viewer->sigDelete.connect( sigc::mem_fun( *this, &MessageSelectionWindow::viewerDeleted ));
} else
viewer->SetMessage( *msg );
}
};
MessageSelectionWindow::MessageSelectionWindow( PG_Widget *parent, const PG_Rect &r, const MessagePntrContainer& messages, GameMap* gamemap, bool editable )
: ASC_PG_Dialog( parent, r, "Messages" ), edit( editable), viewer(NULL)
{
MessageListItemFactory* factory = new MessageListItemFactory( messages, gamemap );
factory->messageSelected.connect ( sigc::mem_fun( *this, &MessageSelectionWindow::messageSelected ));
ItemSelectorWidget* isw = new ItemSelectorWidget( this, PG_Rect(10, GetTitlebarHeight(), r.Width() - 10, r.Height() - GetTitlebarHeight()), factory );
isw->sigQuitModal.connect( sigc::mem_fun( *this, &ItemSelectorWindow::QuitModal));
};
void viewmessages ( const char* title, const MessagePntrContainer& msg, bool editable ) // mode : 0 verschickte ; 1 empfangene
{
int ww = min( PG_Application::GetScreenWidth()/2, 500 );
PG_Rect r ( max(PG_Application::GetScreenWidth()/2,0) - ww, -1, ww, 500 );
MessageSelectionWindow msw ( NULL, r, msg, actmap, editable );
msw.Show();
msw.RunModal();
}
bool viewmessage ( const Message& message )
{
//! maybe we are running in text mode
if ( PG_Application::GetScreen()==NULL )
return false;
assert( !legacyEventSystemActive() );
IngameMessageViewer igm( "incoming message...", message );
igm.Show();
igm.RunModal();
return igm.getKeepMessage();
}
void checkJournal( Player& player )
{
GameMap* actmap = player.getParentMap();
if ( actmap->lastjournalchange.abstime )
if ( (actmap->lastjournalchange.turn() == actmap->time.turn() ) ||
(actmap->lastjournalchange.turn() == actmap->time.turn()-1 && actmap->lastjournalchange.move() > actmap->actplayer ) )
viewjournal( false );
}
void viewjournal ( bool showEmptyDlg )
{
if ( !actmap->gameJournal.empty() ) {
tviewanytext vat;
vat.init ( "journal", actmap->gameJournal.c_str() );
vat.run();
vat.done();
} else
if ( showEmptyDlg )
infoMessage("no entries to journal yet");
}
void editjournal ( void )
{
MultilineEdit ej ( actmap->newJournal, "Journal" );
ej.init ();
ej.run ();
if ( ej.changed() )
actmap->lastjournalchange.set ( actmap->time.turn(), actmap->actplayer );
ej.done ();
}
void viewunreadmessages ( Player& player )
{
/** this is hack to prevent several message windows popping up at once.
The problem is that the idle handler of the first message viewer may trigger or evaluate further events,
which may themself view new messages
*/
static bool isRunning = false;
if ( isRunning )
return;
VariableLocker l( isRunning );
if( (player.stat == Player::human || player.stat == Player::supervisor) && player.getParentMap()->getPlayerView() == player.getPosition() ) {
MessagePntrContainer::iterator mi = player.unreadmessage.begin();
while ( mi != player.unreadmessage.end() ) {
Message* msg = *mi;
bool keep = viewmessage ( *msg );
if ( keep )
++mi;
else {
player.oldmessage.push_back ( *mi );
mi = player.unreadmessage.erase ( mi );
}
}
}
}
|