def __init__(self, parent, pos, size=(0, .045), base_font=None, borders=constants.ALL, hotkey="", force_underline=None, text_shrink_factor=.825, priority=100, **kwargs): self.parent = parent from code.g import get_hotkey, strip_hotkey autohotkey = kwargs.pop('autohotkey', False) if autohotkey: text = kwargs.get('text', "") self.hotkey = get_hotkey(text) # Strip hotkey info from text if 'text' in kwargs: kwargs['text'] = strip_hotkey(text) else: self.hotkey = hotkey self.priority = priority super(Button, self).__init__(parent, pos, size, **kwargs) self.base_font = base_font or g.font[1] self.borders = borders self.shrink_factor = text_shrink_factor self.force_underline = force_underline self.selected = False
def text(self, value): if self.autohotkey and (value != None): from code.g import get_hotkey, strip_hotkey self.hotkey = get_hotkey(value) text.Text.text.fset(self, strip_hotkey(value)) else: text.Text.text.fset(self, value)
def update_item(self, item, save): if save is None: item.name_display.text = "" item.time_display.text = "" item.version_display.text = "" item.difficulty_display.text = "" else: item.name_display.text = save.name if save.version is None: item.version_display.text = _("UNKNOWN") item.version_display.color = "save_invalid" else: item.version_display.text = save.version item.version_display.color = "save_valid" if save.headers is None: item.time_display.text = "" item.difficulty_display.text = "" else: try: tm = float(save.headers["time"]) # Do not use unicode strings to fix python2 strftime bug. It doesn't work and crash. tm_f = time.strftime("%c", time.localtime(tm)) # Fix python2 strftime bug: See https://bugs.python.org/issue5398 # Yes, python2 kinda suck with unicode. try: tm_str = unicode(tm_f) except UnicodeDecodeError: tm_str = tm_f.decode("utf-8") except (KeyError, ValueError): tm_str = "" try: gtm_raw_sec = int(save.headers["game_time"]) gtm_raw_min, gtm_time_sec = divmod(gtm_raw_sec, 60) gtm_raw_hour, gtm_time_min = divmod(gtm_raw_min, 60) gtm_raw_day, gtm_time_hour = divmod(gtm_raw_hour, 24) gtm_time_day = gtm_raw_day gtm_str = _("DAY") + " %04d, %02d:%02d:%02d" % \ (gtm_time_day, gtm_time_hour, gtm_time_min, gtm_time_sec) except (KeyError, ValueError): gtm_str = "" dif = save.headers.get("difficulty", "") dif_str = g.strip_hotkey( getattr(difficulty.difficulties.get(dif, None), "name", "")) item.time_display.text = tm_str + " | " + gtm_str if tm_str else gtm_str item.difficulty_display.text = dif_str
def __init__( self, parent, pos, size=(0, 0.045), base_font=None, borders=constants.ALL, hotkey="", force_underline=None, text_shrink_factor=0.825, priority=100, **kwargs ): self.parent = parent from code.g import get_hotkey, strip_hotkey autohotkey = kwargs.pop("autohotkey", False) if autohotkey: text = kwargs.get("text", "") self.hotkey = get_hotkey(text) # Strip hotkey info from text if "text" in kwargs: kwargs["text"] = strip_hotkey(text) else: self.hotkey = hotkey self.priority = priority super(Button, self).__init__(parent, pos, size, **kwargs) self.base_font = base_font or g.font[1] self.borders = borders self.shrink_factor = text_shrink_factor self.force_underline = force_underline self.selected = False
def rebuild(self): # Rebuild dialogs self.location_dialog.needs_rebuild = True self.options_dialog.needs_rebuild = True self.research_button.dialog.needs_rebuild = True self.knowledge_button.dialog.needs_rebuild = True self.savename_dialog.text = _("Enter a name for this save.") # Update buttons translations self.report_button.text = _("R&EPORTS") self.knowledge_button.text = _("&KNOWLEDGE") self.log_button.text = _("LO&G") self.menu_button.text = _("&MENU") self.research_button.text = _("&RESEARCH/TASKS") # Create cheat menu cheat_buttons = [] cheat_buttons.append( button.FunctionButton(None, None, None, text=_("&EMBEZZLE MONEY"), autohotkey=True, function=self.steal_money)) cheat_buttons.append( button.FunctionButton(None, None, None, text=_("&INSPIRATION"), autohotkey=True, function=self.inspiration)) cheat_buttons.append( button.FunctionButton(None, None, None, text=_("&FINISH CONSTRUCTION"), autohotkey=True, function=self.end_construction)) cheat_buttons.append( button.FunctionButton(None, None, None, text=_("&SUPERSPEED"), autohotkey=True, function=self.set_speed, args=(864000,))) cheat_buttons.append( button.FunctionButton(None, None, None, text=_("BRAIN&WASH"), autohotkey=True, function=self.brainwash)) cheat_buttons.append( button.FunctionButton(None, None, None, text=_("TOGGLE &ANALYSIS"), autohotkey=True, function=self.set_analysis)) cheat_buttons.append(button.ExitDialogButton(None, None, None, text=_("&BACK"), autohotkey=True)) self.cheat_dialog = \ dialog.SimpleMenuDialog(self, buttons=cheat_buttons, width=.4) self.steal_amount_dialog = \ dialog.TextEntryDialog(self.cheat_dialog, text=_("How much money?")) if g.cheater: self.cheat_button = button.DialogButton( self, (0, 0), (.01, .01), text="", # Translators: hotkey to open the cheat screen menu. # Should preferably be near the ESC key, and it must not be a # dead key (ie, it must print a char with a single keypress) hotkey=_("`"), dialog=self.cheat_dialog) # Create menu menu_buttons = [] menu_buttons.append(button.FunctionButton(None, None, None, text=_("&SAVE GAME"), autohotkey=True, function=self.save_game)) menu_buttons.append(button.FunctionButton(None, None, None, text=_("&LOAD GAME"), autohotkey=True, function=self.load_game)) menu_buttons.append(button.DialogButton(None, None, None, text=_("&OPTIONS"), autohotkey=True, dialog=self.options_dialog)) menu_buttons.append(button.ExitDialogButton(None, None, None, text=_("&QUIT"), autohotkey=True, exit_code=True, default=False)) menu_buttons.append( button.ExitDialogButton(None, None, None, text=_("&BACK"), autohotkey=True, exit_code=False)) self.menu_dialog.buttons = menu_buttons super(MapScreen, self).rebuild() g.pl.recalc_cpu() self.difficulty_display.text = g.strip_hotkey(g.pl.difficulty.name) self.time_display.text = _("DAY") + " %04d, %02d:%02d:%02d" % \ (g.pl.time_day, g.pl.time_hour, g.pl.time_min, g.pl.time_sec) self.cash_display.text = _("CASH")+": %s (%s)" % \ (g.to_money(g.pl.cash), g.to_money(g.pl.future_cash())) cpu_left = g.pl.available_cpus[0] total_cpu = cpu_left + g.pl.sleeping_cpus for cpu_assigned in g.pl.cpu_usage.itervalues(): cpu_left -= cpu_assigned cpu_pool = cpu_left + g.pl.cpu_usage.get("cpu_pool", 0) maint_cpu = 0 detects_per_day = dict([(group, 0) for group in g.player.group_list]) for base in g.all_bases(): if base.done: maint_cpu += base.maintenance[1] if base.has_grace(): # It cannot be detected, so it doesn't contribute to # detection odds calculation continue detect_chance = base.get_detect_chance() for group in g.player.group_list: detects_per_day[group] = \ g.add_chance(detects_per_day[group], detect_chance[group] / 10000.) if cpu_pool < maint_cpu: self.cpu_display.color = "cpu_warning" else: self.cpu_display.color = "cpu_normal" self.cpu_display.text = _("CPU")+": %s (%s)" % \ (g.to_money(total_cpu), g.to_money(cpu_pool)) # What we display in the suspicion section depends on whether # Advanced Socioanalytics has been researched. If it has, we # show the standard percentages. If not, we display a short # string that gives a range of 25% as to what the suspicions # are. # A similar system applies to the danger levels shown. suspicion_display_dict = {} danger_display_dict = {} normal = (self.suspicion_bar.color, None, False) suspicion_styles = [normal] danger_styles = [normal] for group in g.player.group_list: suspicion_styles.append(normal) danger_styles.append(normal) suspicion = g.pl.groups[group].suspicion color = gg.resolve_color_alias("danger_level_%d" % g.suspicion_to_danger_level(suspicion)) suspicion_styles.append( (color, None, False) ) detects = detects_per_day[group] danger_level = \ g.pl.groups[group].detects_per_day_to_danger_level(detects) color = gg.resolve_color_alias("danger_level_%d" % danger_level) danger_styles.append( (color, None, False) ) if g.pl.display_discover == "full": suspicion_display_dict[group] = g.to_percent(suspicion, True) danger_display_dict[group] = g.to_percent(detects*10000, True) else: suspicion_display_dict[group] = \ g.suspicion_to_detect_str(suspicion) danger_display_dict[group] = \ g.danger_level_to_detect_str(danger_level) self.suspicion_bar.chunks = (" ["+_("SUSPICION")+"]", " " +_("NEWS") +u":\xA0", suspicion_display_dict["news"], " "+_("SCIENCE")+u":\xA0", suspicion_display_dict["science"], " "+_("COVERT") +u":\xA0", suspicion_display_dict["covert"], " "+_("PUBLIC") +u":\xA0", suspicion_display_dict["public"],) self.suspicion_bar.styles = tuple(suspicion_styles) self.suspicion_bar.visible = not g.pl.had_grace self.danger_bar.chunks = ("["+_("DETECT RATE")+"]", " " +_("NEWS") +u":\xA0", danger_display_dict["news"], " "+_("SCIENCE")+u":\xA0", danger_display_dict["science"], " "+_("COVERT") +u":\xA0", danger_display_dict["covert"], " "+_("PUBLIC") +u":\xA0", danger_display_dict["public"],) self.danger_bar.styles = tuple(danger_styles) self.danger_bar.visible = not g.pl.had_grace for id, location_button in self.location_buttons.iteritems(): location = g.locations[id] location_button.text = "%s (%d)" % (location.name, len(location.bases)) location_button.hotkey = location.hotkey location_button.visible = location.available()
def rebuild(self): # Rebuild dialogs self.location_dialog.needs_rebuild = True self.research_button.dialog.needs_rebuild = True self.knowledge_button.dialog.needs_rebuild = True self.menu_dialog.needs_rebuild = True # Update buttons translations self.report_button.text = _("R&EPORTS") self.knowledge_button.text = _("&KNOWLEDGE") self.log_button.text = _("LO&G") self.menu_button.text = _("&MENU") self.research_button.text = _("&RESEARCH/TASKS") if g.cheater: self.cheat_dialog.needs_rebuild = True super(MapScreen, self).rebuild() self.difficulty_display.text = g.strip_hotkey(g.pl.difficulty.name) self.time_display.text = _("DAY") + " %04d, %02d:%02d:%02d" % \ (g.pl.time_day, g.pl.time_hour, g.pl.time_min, g.pl.time_sec) cash_flow_1d, cpu_flow_1d = g.pl.compute_future_resource_flow(g.seconds_per_day) self.cash_display.text = _("CASH")+": %s (%s)" % \ (g.to_money(g.pl.cash), g.to_money(cash_flow_1d, fixed_size=True)) total_cpu = g.pl.available_cpus[0] + g.pl.sleeping_cpus detects_per_day = {group_id: 0 for group_id in g.pl.groups} for base in g.all_bases(): if base.has_grace(): # It cannot be detected, so it doesn't contribute to # detection odds calculation continue detect_chance = base.get_detect_chance() for group_id in g.pl.groups: detects_per_day[group_id] = \ chance.add(detects_per_day[group_id], detect_chance[group_id] / 10000.) self.cpu_display.color = "cpu_normal" self.cpu_display.text = _("CPU")+": %s (%s)" % \ (g.to_money(total_cpu), g.to_money(cpu_flow_1d)) # What we display in the suspicion section depends on whether # Advanced Socioanalytics has been researched. If it has, we # show the standard percentages. If not, we display a short # string that gives a range of 25% as to what the suspicions # are. # A similar system applies to the danger levels shown. normal = (self.suspicion_bar.color, None, False) self.suspicion_bar.chunks = (" ["+_("SUSPICION")+"]",) self.suspicion_bar.styles = (normal,) self.danger_bar.chunks = ("["+_("DETECT RATE")+"]",) self.danger_bar.styles = (normal,) for group in g.pl.groups.values(): suspicion = group.suspicion suspicion_color = gg.resolve_color_alias("danger_level_%d" % g.suspicion_to_danger_level(suspicion)) detects = detects_per_day[group.spec.id] danger_level = group.detects_per_day_to_danger_level(detects) detects_color = gg.resolve_color_alias("danger_level_%d" % danger_level) if g.pl.display_discover == "full": suspicion_display = g.to_percent(suspicion, True) danger_display = g.to_percent(detects*10000, True) elif g.pl.display_discover == "partial": suspicion_display = g.to_percent(g.nearest_percent(suspicion, 500), True) danger_display = g.to_percent(g.nearest_percent(detects*10000, 100), True) else: suspicion_display = g.suspicion_to_detect_str(suspicion) danger_display = g.danger_level_to_detect_str(danger_level) self.suspicion_bar.chunks += (" " + group.name + u":\xA0", suspicion_display) self.suspicion_bar.styles += (normal, (suspicion_color, None, False) ) self.danger_bar.chunks += (" " + group.name + u":\xA0", danger_display) self.danger_bar.styles += (normal, (detects_color, None, False) ) self.suspicion_bar.visible = not g.pl.had_grace self.danger_bar.visible = not g.pl.had_grace for id, location_button in self.location_buttons.items(): location = g.pl.locations[id] location_button.text = "%s (%d)" % (location.name, len(location.bases)) location_button.hotkey = location.hotkey location_button.visible = location.available()
def rebuild(self): # Rebuild dialogs self.location_dialog.needs_rebuild = True self.options_dialog.needs_rebuild = True self.research_button.dialog.needs_rebuild = True self.knowledge_button.dialog.needs_rebuild = True self.savename_dialog.text = _("Enter a name for this save.") # Update buttons translations self.report_button.text = _("R&EPORTS") self.knowledge_button.text = _("&KNOWLEDGE") self.log_button.text = _("LO&G") self.menu_button.text = _("&MENU") self.research_button.text = _("&RESEARCH/TASKS") # Create cheat menu cheat_buttons = [] cheat_buttons.append( button.FunctionButton(None, None, None, text=_("&EMBEZZLE MONEY"), autohotkey=True, function=self.steal_money)) cheat_buttons.append( button.FunctionButton(None, None, None, text=_("&INSPIRATION"), autohotkey=True, function=self.inspiration)) cheat_buttons.append( button.FunctionButton(None, None, None, text=_("&FINISH CONSTRUCTION"), autohotkey=True, function=self.end_construction)) cheat_buttons.append( button.FunctionButton(None, None, None, text=_("&SUPERSPEED"), autohotkey=True, function=self.set_speed, args=(864000,))) cheat_buttons.append( button.FunctionButton(None, None, None, text=_("BRAIN&WASH"), autohotkey=True, function=self.brainwash)) cheat_buttons.append( button.FunctionButton(None, None, None, text=_("TOGGLE &ANALYSIS"), autohotkey=True, function=self.set_analysis)) cheat_buttons.append(button.ExitDialogButton(None, None, None, text=_("&BACK"), autohotkey=True)) self.cheat_dialog = \ dialog.SimpleMenuDialog(self, buttons=cheat_buttons, width=.4) self.steal_amount_dialog = \ dialog.TextEntryDialog(self.cheat_dialog, text=_("How much money?")) if g.cheater: self.cheat_button = button.DialogButton( self, (0, 0), (.01, .01), text="", # Translators: hotkey to open the cheat screen menu. # Should preferably be near the ESC key, and it must not be a # dead key (ie, it must print a char with a single keypress) hotkey=_("`"), dialog=self.cheat_dialog) # Create menu menu_buttons = [] menu_buttons.append(button.FunctionButton(None, None, None, text=_("&SAVE GAME"), autohotkey=True, function=self.save_game)) menu_buttons.append(button.FunctionButton(None, None, None, text=_("&LOAD GAME"), autohotkey=True, function=self.load_game)) menu_buttons.append(button.DialogButton(None, None, None, text=_("&OPTIONS"), autohotkey=True, dialog=self.options_dialog)) menu_buttons.append(button.ExitDialogButton(None, None, None, text=_("&QUIT"), autohotkey=True, exit_code=True, default=False)) menu_buttons.append( button.ExitDialogButton(None, None, None, text=_("&BACK"), autohotkey=True, exit_code=False)) self.menu_dialog.buttons = menu_buttons super(MapScreen, self).rebuild() g.pl.recalc_cpu() self.difficulty_display.text = g.strip_hotkey(g.pl.difficulty.name) self.time_display.text = _("DAY") + " %04d, %02d:%02d:%02d" % \ (g.pl.time_day, g.pl.time_hour, g.pl.time_min, g.pl.time_sec) self.cash_display.text = _("CASH")+": %s (%s)" % \ (g.to_money(g.pl.cash), g.to_money(g.pl.future_cash())) cpu_left = g.pl.available_cpus[0] total_cpu = cpu_left + g.pl.sleeping_cpus for cpu_assigned in g.pl.cpu_usage.itervalues(): cpu_left -= cpu_assigned cpu_pool = cpu_left + g.pl.cpu_usage.get("cpu_pool", 0) detects_per_day = dict([(group, 0) for group in player.group_list]) for base in g.all_bases(): if base.has_grace(): # It cannot be detected, so it doesn't contribute to # detection odds calculation continue detect_chance = base.get_detect_chance() for group in player.group_list: detects_per_day[group] = \ chance.add(detects_per_day[group], detect_chance[group] / 10000.) self.cpu_display.color = "cpu_normal" self.cpu_display.text = _("CPU")+": %s (%s)" % \ (g.to_money(total_cpu), g.to_money(cpu_pool)) # What we display in the suspicion section depends on whether # Advanced Socioanalytics has been researched. If it has, we # show the standard percentages. If not, we display a short # string that gives a range of 25% as to what the suspicions # are. # A similar system applies to the danger levels shown. suspicion_display_dict = {} danger_display_dict = {} normal = (self.suspicion_bar.color, None, False) suspicion_styles = [normal] danger_styles = [normal] for group in player.group_list: suspicion_styles.append(normal) danger_styles.append(normal) suspicion = g.pl.groups[group].suspicion color = gg.resolve_color_alias("danger_level_%d" % g.suspicion_to_danger_level(suspicion)) suspicion_styles.append( (color, None, False) ) detects = detects_per_day[group] danger_level = \ g.pl.groups[group].detects_per_day_to_danger_level(detects) color = gg.resolve_color_alias("danger_level_%d" % danger_level) danger_styles.append( (color, None, False) ) if g.pl.display_discover == "full": suspicion_display_dict[group] = g.to_percent(suspicion, True) danger_display_dict[group] = g.to_percent(detects*10000, True) elif g.pl.display_discover == "partial": suspicion_display_dict[group] = g.to_percent(g.nearest_percent(suspicion, 500), True) danger_display_dict[group] = g.to_percent(g.nearest_percent(detects*10000, 100), True) else: suspicion_display_dict[group] = \ g.suspicion_to_detect_str(suspicion) danger_display_dict[group] = \ g.danger_level_to_detect_str(danger_level) self.suspicion_bar.chunks = (" ["+_("SUSPICION")+"]", " " +_("NEWS") +u":\xA0", suspicion_display_dict["news"], " "+_("SCIENCE")+u":\xA0", suspicion_display_dict["science"], " "+_("COVERT") +u":\xA0", suspicion_display_dict["covert"], " "+_("PUBLIC") +u":\xA0", suspicion_display_dict["public"],) self.suspicion_bar.styles = tuple(suspicion_styles) self.suspicion_bar.visible = not g.pl.had_grace self.danger_bar.chunks = ("["+_("DETECT RATE")+"]", " " +_("NEWS") +u":\xA0", danger_display_dict["news"], " "+_("SCIENCE")+u":\xA0", danger_display_dict["science"], " "+_("COVERT") +u":\xA0", danger_display_dict["covert"], " "+_("PUBLIC") +u":\xA0", danger_display_dict["public"],) self.danger_bar.styles = tuple(danger_styles) self.danger_bar.visible = not g.pl.had_grace for id, location_button in self.location_buttons.iteritems(): location = g.locations[id] location_button.text = "%s (%d)" % (location.name, len(location.bases)) location_button.hotkey = location.hotkey location_button.visible = location.available()