Package horizons :: Package gui :: Package modules :: Module playerdataselection
[hide private]
[frames] | no frames]

Source Code for Module horizons.gui.modules.playerdataselection

  1  # ################################################### 
  2  # Copyright (C) 2008-2017 The Unknown Horizons Team 
  3  # team@unknown-horizons.org 
  4  # This file is part of Unknown Horizons. 
  5  # 
  6  # Unknown Horizons is free software; you can redistribute it and/or modify 
  7  # it under the terms of the GNU General Public License as published by 
  8  # the Free Software Foundation; either version 2 of the License, or 
  9  # (at your option) any later version. 
 10  # 
 11  # This program is distributed in the hope that it will be useful, 
 12  # but WITHOUT ANY WARRANTY; without even the implied warranty of 
 13  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 14  # GNU General Public License for more details. 
 15  # 
 16  # You should have received a copy of the GNU General Public License 
 17  # along with this program; if not, write to the 
 18  # Free Software Foundation, Inc., 
 19  # 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA 
 20  # ################################################### 
 21   
 22  from fife.extensions.pychan.widgets import HBox, Label 
 23   
 24  import horizons.globals 
 25  from horizons.gui.util import load_uh_widget 
 26  from horizons.util.color import Color 
 27  from horizons.util.python.callback import Callback 
 28   
 29   
30 -class PlayerDataSelection:
31 """Subwidget for selecting player name and color. 32 Used by Multiplayer and Singleplayer menu.""" 33
34 - def __init__(self, color_palette=None):
35 """ 36 @param widgets: WidgetsDict 37 """ 38 self.gui = load_uh_widget('playerdataselection.xml') 39 40 self.colors = self.gui.findChild(name='playercolor') 41 42 colorlabels = [] 43 events = {} 44 45 # need the id to save it as int in settings file. 46 for color in (Color.get_defaults() if color_palette is None else color_palette): 47 label = Label(name='{color}'.format(color=color.name), 48 text=" ", 49 max_size=(20, 20), 50 min_size=(20, 20), 51 background_color=color) 52 events['{label}/mouseClicked'.format(label=color.name)] = \ 53 Callback(self.set_color, color.id) 54 colorlabels.append(label) 55 56 # split into three rows with at max 5 entries in each row 57 # right now there are 14 different colors to choose from. 58 for i in range(0, len(colorlabels), 5): 59 hbox = HBox(name='line_{index}'.format(index=i)) 60 hbox.addChildren(colorlabels[i:i + 5]) 61 self.colors.addChild(hbox) 62 63 playertextfield = self.gui.findChild(name='playername') 64 def playertextfield_clicked(): 65 if playertextfield.text == 'Unnamed Traveler': 66 playertextfield.text = ""
67 playertextfield.capture(playertextfield_clicked, event_name='mouseClicked') 68 69 self.gui.mapEvents(events) 70 self.update_data()
71
72 - def set_color(self, color_id):
73 """Updates the background color of large label where players 74 see their currently chosen color. 75 @param color_id: int. Gets converted to util.Color object. 76 """ 77 try: 78 self.selected_color = Color.get(color_id) 79 except KeyError: 80 # For some reason, color_id can be 0 apparently: 81 # http://forum.unknown-horizons.org/viewtopic.php?t=6927 82 # Reset that setting to 1 if the problem occurs. 83 self.selected_color = Color.get(1) 84 self.gui.findChild(name='selectedcolor').background_color = self.selected_color
85
86 - def set_player_name(self, playername):
87 """Updates the player name""" 88 self.gui.distributeData({ 89 'playername': str(playername), 90 })
91
92 - def get_player_name(self):
93 """Returns the name that was entered by the user""" 94 return self.gui.collectData('playername')
95
96 - def get_player_color(self):
97 """Returns the color that the player selected as Color obj""" 98 return self.selected_color
99
100 - def get_widget(self):
101 return self.gui
102
103 - def update_data(self):
104 """Update the player's name and color from the settings""" 105 self.set_color(horizons.globals.fife.get_uh_setting("ColorID")) 106 self.set_player_name(horizons.globals.fife.get_uh_setting("Nickname"))
107
108 - def save_settings(self):
109 """Stores the current player_name and color into settings""" 110 horizons.globals.fife.set_uh_setting("Nickname", self.get_player_name()) 111 horizons.globals.fife.set_uh_setting("ColorID", self.get_player_color().id) 112 horizons.globals.fife.save_settings()
113