# 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)
window.set_border_width(5)
return window
def init_connection(server):
print("Trying to connect to " + server)
s = xmlrpclib.ServerProxy('http://' + server + ':20211')
ping = s.ping()
if ping != "pong":
print("Error - Couldn't establish a connecting to the server.")
return False
else:
print("Connected to " + server + ':20211')
return s
def get_gamedata(s, password, name):
runs = s.running()
if runs == True:
print "Game is already running"
return "err_running"
playerid = s.connect()
print("Got assigned playerid #" + str(playerid))
auth = s.auth(playerid, password, name)
if auth == False:
print "Wrong password"
return "err_login"
players = s.get_players(playerid)
#print "Players on the server: " + str(players)
return (playerid,players)
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
clibox = gtk.VBox(False, 0)
cliwindow.add(clibox)
mapbox = gtk.HBox(False, 0)
toolbox = gtk.HBox(False, 0)
leftdnbox = gtk.HBox(False, 0)
rightdnbox = gtk.VBox(False, 0)
travelbox = gtk.VBox(False, 0)
mapbox.pack_start(travelbox, False, False, 0)
planebutton = gtk.Button()
trainbutton = gtk.Button()
taxibutton = gtk.Button()
shipbutton = gtk.Button()
travelbox.pack_start(planebutton, False, False, 2)
travelbox.pack_start(trainbutton, False, False, 2)
travelbox.pack_start(taxibutton, False, False, 2)
travelbox.pack_start(shipbutton, False, False, 2)
planeimage = gtk.Image()
planeimage.set_from_file("plane.png")
trainimage = gtk.Image()
trainimage.set_from_file("train.png")
taxiimage = gtk.Image()
taxiimage.set_from_file("car.png")
shipimage = gtk.Image()
shipimage.set_from_file("ship.png")
planebox = gtk.VBox()
trainbox = gtk.VBox()
taxibox = gtk.VBox()
shipbox = gtk.VBox()
planebox.add(planeimage)
trainbox.add(trainimage)
taxibox.add(taxiimage)
shipbox.add(shipimage)
planebox.add(gtk.Label("Plane"))
trainbox.add(gtk.Label("Train"))
taxibox.add(gtk.Label("Taxi"))
shipbox.add(gtk.Label("Ship"))
planebutton.add(planebox)
trainbutton.add(trainbox)
taxibutton.add(taxibox)
shipbutton.add(shipbox)
planebutton.set_sensitive(False)
trainbutton.set_sensitive(False)
taxibutton.set_sensitive(False)
shipbutton.set_sensitive(False)
mapbox.pack_start(gtk.VSeparator(), False, False, 0)
image = gtk.Image()
image.set_from_file("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)
textbuffer.set_text("* Welcome to CatchX v%s \n" % str(version))
chatframe.add(textview)
downerchat = gtk.HBox(False, 0)
rightdnbox.pack_end(downerchat, False, False, 0)
chatentry = gtk.Entry()
downerchat.add(chatentry)
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.add(sendbutton)
#The user-list
liststore = gtk.ListStore(str, str)
treeview = gtk.TreeView(liststore)
tvcolumn = gtk.TreeViewColumn('Id')
tvcolumn1 = gtk.TreeViewColumn('Name')
treeview.append_column(tvcolumn)
treeview.append_column(tvcolumn1)
#print players
for name in players:
liststore.append([name[1], name[0]])
cell = gtk.CellRendererText()
cell1 = 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)
tvcolumn.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.add(vpan)
hpan = gtk.HPaned()
hpan.add1(leftdnbox)
hpan.add2(rightdnbox)
toolbox.add(hpan)
vpan.set_position(uppersize)
hpan.set_position(150)
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):
time.sleep(1)
i = 0
while(True): #infinite loop
i += 1
command = s.keepalive(pid)
if command != True and command != False:
if command[0] == 'chatmsg':
time.sleep(0.35)
#def place_selected():
# plane = gtk.Button("plane")
# ship = gtk.Button("ship")
# taxi = gtk.Button("taxi")
# train = gtk.Button("train")
# travel_box = gtk.VBox()
# travel_box.add(plane)
# travel_box.add(ship)
# travel_box.add(train)
# travel_box.add(taxi)
# plane.show()
# ship.show()
# taxi.show()
# train.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