# -*- coding: utf8 -*-
# This file is part of CatchX.
#
# CatchX 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 3 of the License, or
# (at your option) any later version.
#
# CatchX 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 CatchX. If not, see <http://www.gnu.org/licenses/>.
import pygtk
import gtk
import os
import xmlrpclib
import time
punkte = {411 : 392, 641 : 553, 943 : 537, 853 : 655, 664 : 796, 638 : 795}
def create_window(window_name):
window = gtk.Window(gtk.WINDOW_TOPLEVEL)
window.set_title(window_name)
return window
def init_connection(server):
print("Trying to connect to " + server)
try :
s = xmlrpclib.ServerProxy('http://' + server + ':20211')
ping = s.ping()
except:
print("Error - Couldn't establish a connecting to the server.")
return False
print("Connected to " + server + ':20211')
return s
def get_gamedata(s, password, name, room):
room = s.roomreq(name, room, password)
if room == 'running':
print "Game is already running"
return "err_running"
if room == 'password':
print "Wrong password"
return "err_login"
playerid = room[0]
roomid = room[1]
print("Got assigned playerid #" + str(playerid))
players = s.get_players(playerid)
#print "Players on the server: " + str(players)
return (playerid,players,roomid)
def about_window(self):
text = """Coded by:\n-Lutoma (Lukas Martini)\n-vIiRuS (Philip Thelen)\n\nGrafics by:\n-Pixelmännchen (Robin Eberhard)\n\nSounds and Music by:\n-Waldteufel\n\n\nCatchX is licensed under the GNU\nGeneral Public License. For more\ninformations, read the "COPYING"\nfile you should have in your\nCatchX directory."""
win = create_window("About CatchX")
win.set_border_width(10)
box = gtk.VBox(False, 0)
win.add(box)
image = gtk.Image()
image.set_from_file("img/logo.png")
box.add(image)
label = gtk.Label(text)
box.add(label)
win.show_all()
def load_maingui(cliwindow, players, playerid, version, s): #This is the most complex GUI, the maingui...
#def load_maingui(start):
#cliwindow = start.cliwindow
#players = start.gdata[1]
#playerid = start.gdata[0]
cliwindow.maximize() #We need the complete space
size = cliwindow.get_size() #Now get the size of the maximized window
print "WSize = " + str(size)
uppersize = int((size[0] / 95) *100)
print uppersize
ui = '''<ui>
<menubar name="MenuBar">
<menu action="File">
<menuitem action="Join"/>
<menuitem action="Create"/>
<separator/>
<menuitem action="Quit"/>
</menu>
<menu action="Help">
<menuitem action="About"/>
</menu>
</menubar>
</ui>'''
uimanager = gtk.UIManager()
accelgroup = uimanager.get_accel_group()
cliwindow.add_accel_group(accelgroup)
actiongroup = gtk.ActionGroup('UIManagerExample')
actiongroup.add_actions([('Quit', gtk.STOCK_QUIT, '_Quit', None,
'Quit CatchX', quit_cb),
('Join', None, '_Join an existing room'),
('Create', None, '_Create a new room'),
('File', None, '_File'),
('Help', None, '_Help'),
('About', gtk.STOCK_ABOUT, '_About', None,
'About', about_window)])
actiongroup.get_action('Quit').set_property('short-label', '_Quit')
uimanager.insert_action_group(actiongroup, 0)
uimanager.add_ui_from_string(ui)
menubar = uimanager.get_widget('/MenuBar')
clibox = gtk.VBox(False, 0)
cliwindow.add(clibox)
clibox.pack_start(menubar, False)
mapbox = gtk.HBox(False, 0)
toolbox = gtk.HBox(False, 0)
leftdnbox = gtk.HBox(False, 0)
rightdnbox = gtk.VBox(False, 0)
buttonbox = create_buttonbox()
mapbox.pack_start(buttonbox, False, False, 0)
image = gtk.Image()
image.set_from_file("img/map.png")
pixelsize = image.get_pixbuf() # get the size of the picture to adjust the Layout size
pixelwidth = pixelsize.get_width()
pixelheight = pixelsize.get_height()
#Layout and Scrollbar settings
layout = gtk.Layout()
vadjust = layout.get_vadjustment()
hadjust = layout.get_hadjustment()
vadjust.step_increment = 10
hadjust.step_increment = 10
layout.set_size(pixelwidth, pixelheight)
vscrollbar = gtk.VScrollbar(vadjust)
hscrollbar = gtk.HScrollbar(hadjust)
vscrollbar.show()
hscrollbar.show()
layout.set_events(gtk.gdk.BUTTON_PRESS_MASK)
layout.connect("button_press_event", button_press)
layout.put(image, 0, 0)
#packing the layout and the scrollbars in the Table
table = gtk.Table(2, 2, 0)
table.attach(layout, 0, 1, 0, 1, gtk.EXPAND|gtk.FILL, gtk.EXPAND|gtk.FILL, 0, 0)
table.attach(vscrollbar, 1, 2, 0, 1, 0, gtk.EXPAND|gtk.FILL, 0, 0)
table.attach(hscrollbar, 0, 1, 1, 2, gtk.EXPAND|gtk.FILL, 0, 0, 0)
mapbox.pack_start(table)
xbar = gtk.ProgressBar()
xbar.set_orientation(gtk.PROGRESS_BOTTOM_TO_TOP)
xbar.set_fraction(0.35)
mapbox.pack_end(xbar, False, False, 0)
mapbox.pack_end(gtk.VSeparator(), False, False, 3)
chatframe = gtk.Frame()
rightdnbox.add(chatframe)
#label3 = gtk.Label(str)
#label3.set_text("Right Toolbox")
#chatframe.add(label3)
textview = gtk.TextView()
textbuffer = textview.get_buffer()
textview.set_editable(False)
dtext = "* Welcome to CatchX v%s" % str(version)
textbuffer.set_text(dtext)
global textbuffer, dtext
chatframe.add(textview)
downerchat = gtk.HBox(False, 0)
rightdnbox.pack_end(downerchat, False, False, 0)
chatentry = gtk.Entry()
downerchat.pack_start(chatentry, True, True, 0)
def sendmsg(widget):
msg = chatentry.get_text()
chatentry.set_text('')
s.chat(playerid, msg)
sendbutton = gtk.Button("-> Send")
sendbutton.connect("clicked", sendmsg)
sendbutton.set_sensitive(False)
def changebuttonstate(widget):
if chatentry.get_text() <> "":
sendbutton.set_sensitive(True)
else:
sendbutton.set_sensitive(False)
chatentry.connect("changed", changebuttonstate)
downerchat.pack_end(sendbutton, False, False, 0)
#The user-list
liststore = gtk.ListStore(str, str)
treeview = gtk.TreeView(liststore)
tvcolumn = gtk.TreeViewColumn('Name')
tvcolumn1 = gtk.TreeViewColumn('Money')
treeview.append_column(tvcolumn)
treeview.append_column(tvcolumn1)
#print players
for name in players:
liststore.append([name[0], "--"])
cell = gtk.CellRendererText()
cell1 = gtk.CellRendererText()
cell2 = gtk.CellRendererText()
#tvcolumn.pack_start(cellpb, False)
tvcolumn.pack_start(cell, True)
tvcolumn1.pack_start(cell1, True)
tvcolumn.set_attributes(cell, text=0)
tvcolumn1.set_attributes(cell1, text=1)
#userlist scrollbar
useradjust = treeview.get_vadjustment()
useradjust.step_increment = 10
userscrollbar = gtk.VScrollbar(useradjust)
treeview.set_search_column(0)
tvcolumn1.set_sort_column_id(0)
leftdnbox.add(treeview)
leftdnbox.pack_end(userscrollbar, False, False, 1)
vpan = gtk.VPaned()
vpan.add1(mapbox)
vpan.add2(toolbox)
clibox.pack_start(vpan)
hpan = gtk.HPaned()
hpan.add1(leftdnbox)
hpan.add2(rightdnbox)
toolbox.add(hpan)
vpan.set_position(uppersize)
hpan.set_position(200)
cliwindow.show_all()
def button_press(widget, event):
print event.x, event.y
for xwert, ywert in punkte.items():
if (-10 < (event.x - xwert) < 10) and (-10 < (event.y - ywert)):
#buttonlist = place_selected()
return True
return False
def keepalive(s,pid):
global textbuffer, dtext
time.sleep(1)
i = 0
while(True): #infinite loop
i += 1
try:
command = s.keepalive(pid)
except:
print "Lost connection to the Server!"
print "Quitting program!"
gtk.main_quit()
if command != "none":
command = command.split("§$");
print command
if command[0] == 'chatmsg':
dtext += "\n" + str(command[1]) + ": " + str(command[2])
textbuffer.set_text(dtext)
time.sleep(0.35)
#def place_selected():
# bus = gtk.Button("bus")
# ship = gtk.Button("ship")
# taxi = gtk.Button("taxi")
# tram = gtk.Button("tram")
# travel_box = gtk.VBox()
# travel_box.add(bus)
# travel_box.add(ship)
# travel_box.add(tram)
# travel_box.add(taxi)
# bus.show()
# ship.show()
# taxi.show()
# tram.show()
# travel_box.show()
#def load_mappoints():
# spots = []
# i = 0
# point = []
# f = file("mapname.mapdata", "r")
# while True:
# line = f.readline()
# if len(line) == 0:
# break
# point.append(line)
# i = i + 1
# for n in range(0, len(point)):
# point[n] = point[n].split(":")
# point[n][1] = (point[n][1].rstrip("\n"))
# print point[n]
# return point
def quit_cb(self):
print 'Quitting program'
gtk.main_quit()
def create_buttonbox():
buttonbox = gtk.VBox(False, 5)
busbutton = gtk.Button()
trambutton = gtk.Button()
taxibutton = gtk.Button()
shipbutton = gtk.Button()
traveltable = gtk.Table(2, 3, 0)
buttonbox.pack_start(traveltable, False, False, 20)
traveltable.attach(gtk.Label("travel:"), 0, 2, 0, 1)
traveltable.attach(busbutton, 0, 1, 1, 2)
traveltable.attach(trambutton, 1, 2, 1, 2)
traveltable.attach(taxibutton, 0, 1, 2, 3)
traveltable.attach(shipbutton, 1, 2, 2, 3)
busimage = gtk.Image()
busimage.set_from_file("img/bus.png")
tramimage = gtk.Image()
tramimage.set_from_file("img/tram.png")
taxiimage = gtk.Image()
taxiimage.set_from_file("img/car.png")
shipimage = gtk.Image()
shipimage.set_from_file("img/ship.png")
busbox = gtk.VBox()
trambox = gtk.VBox()
taxibox = gtk.VBox()
shipbox = gtk.VBox()
busbox.add(busimage)
trambox.add(tramimage)
taxibox.add(taxiimage)
shipbox.add(shipimage)
busbox.add(gtk.Label("Bus"))
trambox.add(gtk.Label("Tram"))
taxibox.add(gtk.Label("Taxi"))
shipbox.add(gtk.Label("Ship"))
busbutton.add(busbox)
trambutton.add(trambox)
taxibutton.add(taxibox)
shipbutton.add(shipbox)
busbutton.set_sensitive(False)
trambutton.set_sensitive(False)
taxibutton.set_sensitive(False)
shipbutton.set_sensitive(False)
bankrobb = gtk.Button()
simpletheftb = gtk.Button()
grandtheftb = gtk.Button()
burglaryb = gtk.Button()
buttonbox.pack_start(gtk.HSeparator(), False, False, 0)
buttonbox.pack_start(gtk.Label("crimes:"), False, False, 0)
bankrobb.add(gtk.Label("bank robbery"))
simpletheftb.add(gtk.Label("simple theft"))
grandtheftb.add(gtk.Label("grand theft"))
burglaryb.add(gtk.Label("burglary"))
buttonbox.pack_start(bankrobb, False, False, 0)
buttonbox.pack_start(simpletheftb, False, False, 0)
buttonbox.pack_start(grandtheftb, False, False, 0)
buttonbox.pack_start(burglaryb, False, False, 0)
currentbox = gtk.VBox()
buttonbox.pack_start(currentbox, False, False, 20)
currentbox.add(gtk.HSeparator())
currentbox.add(gtk.Label("Current player:"))
currentbox.add(gtk.Label("Lutoma"))
return buttonbox