~ubuntu-branches/ubuntu/precise/widelands/precise-backports

« back to all changes in this revision

Viewing changes to src/ui/ui_fs_menus/fullscreen_menu_netsetup.cc

  • Committer: Bazaar Package Importer
  • Author(s): Martin Quinson
  • Date: 2005-02-14 10:41:12 UTC
  • Revision ID: james.westby@ubuntu.com-20050214104112-6v08iux9fptxpva9
Tags: upstream-build9
Import upstream version build9

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2004 by the Widelands Development Team
 
3
 *
 
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.
 
8
 *
 
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.
 
13
 *
 
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.
 
17
 *
 
18
 */
 
19
 
 
20
#include "fullscreen_menu_netsetup.h"
 
21
#include "ui_button.h"
 
22
#include "ui_textarea.h"
 
23
#include "ui_editbox.h"
 
24
#include "ui_table.h"
 
25
#include "constants.h"
 
26
#include "network_lan_promotion.h"
 
27
#include "network.h"
 
28
 
 
29
 
 
30
Fullscreen_Menu_NetSetup::Fullscreen_Menu_NetSetup ()
 
31
        :Fullscreen_Menu_Base("singleplmenu.jpg") // change this
 
32
{
 
33
        discovery=new LAN_Game_Finder();
 
34
        discovery->set_callback (discovery_callback, this);
 
35
        
 
36
        // Text
 
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);
 
39
 
 
40
        // UIButtons
 
41
        UIButton* b;
 
42
 
 
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");
 
46
 
 
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");
 
50
 
 
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");
 
54
*/
 
55
        b = new UIButton(this, 60, 290, 174, 24, 0, CANCEL);
 
56
        b->clickedid.set(this, &Fullscreen_Menu_NetSetup::end_modal);
 
57
        b->set_title("Back");
 
58
        
 
59
        // Hostname
 
60
        hostname=new UIEdit_Box(this, 288, 170, 174, 24, 2, 0);
 
61
        hostname->set_text("localhost");        
 
62
        
 
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);
 
69
}
 
70
 
 
71
Fullscreen_Menu_NetSetup::~Fullscreen_Menu_NetSetup ()
 
72
{
 
73
        delete discovery;
 
74
}
 
75
 
 
76
void Fullscreen_Menu_NetSetup::think ()
 
77
{
 
78
        Fullscreen_Menu_Base::think ();
 
79
        
 
80
        discovery->run ();
 
81
}
 
82
 
 
83
bool Fullscreen_Menu_NetSetup::get_host_address (ulong& addr, ushort& port)
 
84
{
 
85
        const char* host=hostname->get_text();
 
86
        
 
87
        int i;
 
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());
 
90
            
 
91
            if (!strcmp(game->info.hostname, host)) {
 
92
                addr=game->address;
 
93
                port=game->port;
 
94
                return true;
 
95
            }
 
96
        }
 
97
        
 
98
        hostent* he=gethostbyname(host);
 
99
        if (he==0)
 
100
            return false;
 
101
        
 
102
        addr=((in_addr*) (he->h_addr_list[0]))->s_addr;
 
103
        port=htons(WIDELANDS_PORT);
 
104
        
 
105
        return true;
 
106
}
 
107
 
 
108
void Fullscreen_Menu_NetSetup::game_selected (int sel)
 
109
{
 
110
        LAN_Open_Game* game=(LAN_Open_Game*) (opengames->get_selection());
 
111
        
 
112
        hostname->set_text (game->info.hostname);
 
113
}
 
114
 
 
115
void Fullscreen_Menu_NetSetup::update_game_info (UITable_Entry* entry, const LAN_Game_Info& info)
 
116
{
 
117
        entry->set_string (0, info.hostname);
 
118
        entry->set_string (1, info.map);
 
119
        
 
120
        switch (info.state) {
 
121
            case LAN_GAME_OPEN:
 
122
                entry->set_string (2, "Open");
 
123
                break;
 
124
            case LAN_GAME_CLOSED:
 
125
                entry->set_string (2, "Closed");
 
126
                break;
 
127
            default:
 
128
                entry->set_string (2, "Unknown");
 
129
                break;
 
130
        }
 
131
}
 
132
 
 
133
void Fullscreen_Menu_NetSetup::game_opened (const LAN_Open_Game* game)
 
134
{
 
135
        update_game_info (new UITable_Entry(opengames, (void*) game), game->info);
 
136
}
 
137
 
 
138
void Fullscreen_Menu_NetSetup::game_closed (const LAN_Open_Game* game)
 
139
{
 
140
}
 
141
 
 
142
void Fullscreen_Menu_NetSetup::game_updated (const LAN_Open_Game* game)
 
143
{
 
144
        UITable_Entry* entry=opengames->find_entry(game);
 
145
        
 
146
        if (entry!=0)
 
147
            update_game_info (entry, game->info);
 
148
}
 
149
 
 
150
void Fullscreen_Menu_NetSetup::discovery_callback (int type, const LAN_Open_Game* game, void* userdata)
 
151
{
 
152
    switch (type) {
 
153
        case LAN_Game_Finder::GameOpened:
 
154
            static_cast<Fullscreen_Menu_NetSetup*>(userdata)->game_opened (game);
 
155
            break;
 
156
        case LAN_Game_Finder::GameClosed:
 
157
            static_cast<Fullscreen_Menu_NetSetup*>(userdata)->game_closed (game);
 
158
            break;
 
159
        case LAN_Game_Finder::GameUpdated:
 
160
            static_cast<Fullscreen_Menu_NetSetup*>(userdata)->game_updated (game);
 
161
            break;
 
162
        default:
 
163
            abort ();
 
164
    }
 
165
}
 
166
 
 
167
void Fullscreen_Menu_NetSetup::fill(std::list<std::string> tables)
 
168
{
 
169
        LAN_Game_Info info;
 
170
        std::list<std::string>::iterator it;
 
171
        for(it = tables.begin(); it != tables.end(); it++)
 
172
        {
 
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);
 
177
        }
 
178
}
 
179