2
* Copyright (C) 2004 by the Widelands Development Team
4
* This program is free software; you can redistribute it and/or
5
* modify it under the terms of the GNU General Public License
6
* as published by the Free Software Foundation; either version 2
7
* of the License, or (at your option) any later version.
9
* This program is distributed in the hope that it will be useful,
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
* GNU General Public License for more details.
14
* You should have received a copy of the GNU General Public License
15
* along with this program; if not, write to the Free Software
16
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20
#include "fullscreen_menu_netsetup.h"
21
#include "ui_button.h"
22
#include "ui_textarea.h"
23
#include "ui_editbox.h"
25
#include "constants.h"
26
#include "network_lan_promotion.h"
30
Fullscreen_Menu_NetSetup::Fullscreen_Menu_NetSetup ()
31
:Fullscreen_Menu_Base("singleplmenu.jpg") // change this
33
discovery=new LAN_Game_Finder();
34
discovery->set_callback (discovery_callback, this);
37
UITextarea* title= new UITextarea(this, MENU_XRES/2, 140, "Begin Network Game", Align_HCenter);
38
title->set_font(UI_FONT_BIG, UI_FONT_CLR_FG);
43
b = new UIButton(this, 60, 170, 174, 24, 1, JOINGAME);
44
b->clickedid.set(this, &Fullscreen_Menu_NetSetup::end_modal);
45
b->set_title("Join a Game");
47
b = new UIButton(this, 60, 210, 174, 24, 1, HOSTGAME);
48
b->clickedid.set(this, &Fullscreen_Menu_NetSetup::end_modal);
49
b->set_title("Host a New Game");
51
/* b = new UIButton(this, 60, 250, 174, 24, 1, INTERNETGAME);
52
b->clickedid.set(this, &Fullscreen_Menu_NetSetup::end_modal);
53
b->set_title("Play in Internet");
55
b = new UIButton(this, 60, 290, 174, 24, 0, CANCEL);
56
b->clickedid.set(this, &Fullscreen_Menu_NetSetup::end_modal);
60
hostname=new UIEdit_Box(this, 288, 170, 174, 24, 2, 0);
61
hostname->set_text("localhost");
63
// List of open games in local network
64
opengames=new UITable(this, 288, 210, 320, 128);
65
opengames->add_column ("Host", UITable::STRING, 128);
66
opengames->add_column ("Map", UITable::STRING, 128);
67
opengames->add_column ("State", UITable::STRING, 64);
68
opengames->selected.set (this, &Fullscreen_Menu_NetSetup::game_selected);
71
Fullscreen_Menu_NetSetup::~Fullscreen_Menu_NetSetup ()
76
void Fullscreen_Menu_NetSetup::think ()
78
Fullscreen_Menu_Base::think ();
83
bool Fullscreen_Menu_NetSetup::get_host_address (ulong& addr, ushort& port)
85
const char* host=hostname->get_text();
88
for (i=0;i<opengames->get_nr_entries();i++) {
89
LAN_Open_Game* game=(LAN_Open_Game*) (opengames->get_entry(i)->get_user_data());
91
if (!strcmp(game->info.hostname, host)) {
98
hostent* he=gethostbyname(host);
102
addr=((in_addr*) (he->h_addr_list[0]))->s_addr;
103
port=htons(WIDELANDS_PORT);
108
void Fullscreen_Menu_NetSetup::game_selected (int sel)
110
LAN_Open_Game* game=(LAN_Open_Game*) (opengames->get_selection());
112
hostname->set_text (game->info.hostname);
115
void Fullscreen_Menu_NetSetup::update_game_info (UITable_Entry* entry, const LAN_Game_Info& info)
117
entry->set_string (0, info.hostname);
118
entry->set_string (1, info.map);
120
switch (info.state) {
122
entry->set_string (2, "Open");
124
case LAN_GAME_CLOSED:
125
entry->set_string (2, "Closed");
128
entry->set_string (2, "Unknown");
133
void Fullscreen_Menu_NetSetup::game_opened (const LAN_Open_Game* game)
135
update_game_info (new UITable_Entry(opengames, (void*) game), game->info);
138
void Fullscreen_Menu_NetSetup::game_closed (const LAN_Open_Game* game)
142
void Fullscreen_Menu_NetSetup::game_updated (const LAN_Open_Game* game)
144
UITable_Entry* entry=opengames->find_entry(game);
147
update_game_info (entry, game->info);
150
void Fullscreen_Menu_NetSetup::discovery_callback (int type, const LAN_Open_Game* game, void* userdata)
153
case LAN_Game_Finder::GameOpened:
154
static_cast<Fullscreen_Menu_NetSetup*>(userdata)->game_opened (game);
156
case LAN_Game_Finder::GameClosed:
157
static_cast<Fullscreen_Menu_NetSetup*>(userdata)->game_closed (game);
159
case LAN_Game_Finder::GameUpdated:
160
static_cast<Fullscreen_Menu_NetSetup*>(userdata)->game_updated (game);
167
void Fullscreen_Menu_NetSetup::fill(std::list<std::string> tables)
170
std::list<std::string>::iterator it;
171
for(it = tables.begin(); it != tables.end(); it++)
173
strncpy(info.hostname, "(ggz)", sizeof(info.hostname));
174
strncpy(info.map, (*it).c_str(), sizeof(info.map));
175
info.state = LAN_GAME_OPEN;
176
update_game_info (new UITable_Entry(opengames, (void*) NULL), info);