class SavingLabelHandler: def __init__(self, overlay): self.overlay = overlay self.batch = self.overlay.batch self.counter = 0 self.dots = '' self.saving_lbl = Label('', font_name='Consolas', font_size=20, color=( 0, 0, 0, 255, ), batch=self.batch, anchor_x='center', anchor_y='center', x=self.overlay.centerX, y=self.overlay.height - 30) def __call__(self): self.saving_lbl.begin_update() if self.overlay.game.savingstate.saving: self.counter += 1 if self.counter % 5 == 0: if len(self.dots) == 3: self.dots = '' self.dots += '.' self.saving_lbl.text = 'speichert' + self.dots else: self.saving_lbl.text = '' self.saving_lbl.end_update()
class EndScreenHandler: def __init__(self, overlay): self.overlay = overlay self.batch = self.overlay.batch self.sizeX, self.sizeY, = self.overlay.width // 4, self.overlay.height // 4, self.shown = True self.text = 'YOU DIED\n\npress [space] to close' self.endscreen = self.batch.add(4, GL_QUADS, None, ('v2i/static', (0, ) * len(self.getCoords())), ('c4B/static', ( 100, 100, 100, 150, ) * 4)) self.endscreenLabel = Label('', font_name='Consolas', font_size=50, width=self.sizeX * 2, color=( 200, 0, 0, 255, ), batch=self.batch, anchor_x='center', anchor_y='center', x=self.overlay.centerX, y=self.overlay.centerY + 30, align='center', multiline=True) def __call__(self): if not self.overlay.game.player.alive and not self.shown: self.shown = True self.endscreen.vertices[:] = self.getCoords() self.endscreenLabel.begin_update() self.endscreenLabel.text = self.text self.endscreenLabel.end_update() elif self.overlay.game.player.alive and self.shown: # if self.shown: self.shown = False self.endscreen.vertices[:] = (0, ) * len(self.getCoords()) self.endscreenLabel.begin_update() self.endscreenLabel.text = '' self.endscreenLabel.end_update() def getCoords(self): return (self.overlay.centerX - self.sizeX, self.overlay.centerY - self.sizeY, self.overlay.centerX + self.sizeX, self.overlay.centerY - self.sizeY, self.overlay.centerX + self.sizeX, self.overlay.centerY + self.sizeY, self.overlay.centerX - self.sizeX, self.overlay.centerY + self.sizeY)
class InteractiveLabelV2(UIObject, ABC): font_name: str base_color: (int, int, int) placeholder_color: (int, int, int) def __init__(self, logger, parent_viewport): super().__init__(logger, parent_viewport) self.arguments = [] self.text_label = None self.placeholder_label = None self.bold = False self.anchor_x = 'center' self.anchor_y = 'center' self.align = 'left' self.multiline = False self.batch = BATCHES['ui_batch'] self.group = GROUPS['button_text'] self.text_length_limit = 25 self.on_key_press_handlers = [self.on_key_press] self.on_text_handlers = [self.on_text] @final def __len__(self): return len(self.text_label.text) if self.text_label else 0 @abstractmethod def get_x(self): pass @abstractmethod def get_y(self): pass @abstractmethod def get_font_size(self): pass @abstractmethod def get_formatted_text(self): pass @final @is_not_active def on_activate(self): super().on_activate() if not self.placeholder_label: self.placeholder_label = PygletLabel( self.get_formatted_text(), font_name=self.font_name, bold=self.bold, font_size=self.get_font_size(), color=(*self.placeholder_color, self.opacity), x=self.get_x(), y=self.get_y(), anchor_x=self.anchor_x, anchor_y=self.anchor_y, align=self.align, multiline=self.multiline, batch=self.batch, group=self.group) @final def on_update_opacity(self, new_opacity): super().on_update_opacity(new_opacity) if self.text_label: if self.opacity > 0: self.text_label.color = (*self.base_color, self.opacity) else: self.text_label.delete() self.text_label = None if self.placeholder_label: if self.opacity > 0: self.placeholder_label.color = (*self.placeholder_color, self.opacity) else: self.placeholder_label.delete() self.placeholder_label = None @final @window_size_has_changed def on_window_resize(self, width, height): super().on_window_resize(width, height) if self.text_label: self.text_label.begin_update() self.text_label.x = self.get_x() self.text_label.y = self.get_y() self.text_label.font_size = self.get_font_size() self.text_label.end_update() if self.placeholder_label: self.placeholder_label.begin_update() self.placeholder_label.x = self.get_x() self.placeholder_label.y = self.get_y() self.placeholder_label.font_size = self.get_font_size() self.placeholder_label.end_update() @final def on_text(self, text): if not self.text_label: self.placeholder_label.delete() self.placeholder_label = None self.text_label = PygletLabel(text[:self.text_length_limit], font_name=self.font_name, bold=self.bold, font_size=self.get_font_size(), color=(*self.base_color, self.opacity), x=self.get_x(), y=self.get_y(), anchor_x=self.anchor_x, anchor_y=self.anchor_y, align=self.align, multiline=self.multiline, batch=self.batch, group=self.group) else: self.text_label.text = (self.text_label.text + text)[:self.text_length_limit] @final def on_key_press(self, symbol, modifiers): if symbol == BACKSPACE and self.text_label: if len(self.text_label.text) > 0: self.text_label.text = self.text_label.text[:-1] else: self.text_label.delete() self.text_label = None self.placeholder_label = PygletLabel( self.get_formatted_text(), font_name=self.font_name, bold=self.bold, font_size=self.get_font_size(), color=(*self.placeholder_color, self.opacity), x=self.get_x(), y=self.get_y(), anchor_x=self.anchor_x, anchor_y=self.anchor_y, align=self.align, multiline=self.multiline, batch=self.batch, group=self.group) elif modifiers & MOD_CTRL and symbol == V: OpenClipboard() try: self.on_text(GetClipboardData()) except TypeError: pass CloseClipboard()
class LabelV2(UIObject, ABC): font_name: str base_color: (int, int, int) def __init__(self, logger, parent_viewport): super().__init__(logger, parent_viewport) self.arguments = [] self.resource_list_keys = [] self.text_label = None self.bold = False self.anchor_x = 'center' self.anchor_y = 'center' self.align = 'left' self.multiline = False self.batch = BATCHES['ui_batch'] self.group = GROUPS['button_text'] self.undergoing_text_update = False @abstractmethod def get_x(self): pass @abstractmethod def get_y(self): pass @abstractmethod def get_font_size(self): pass def get_width(self): return None @abstractmethod def get_formatted_text(self): pass @final @is_not_active def on_activate(self): super().on_activate() if not self.text_label: self.text_label = PygletLabel(self.get_formatted_text(), font_name=self.font_name, bold=self.bold, font_size=self.get_font_size(), color=(*self.base_color, self.opacity), x=self.get_x(), y=self.get_y(), width=self.get_width(), anchor_x=self.anchor_x, anchor_y=self.anchor_y, align=self.align, multiline=self.multiline, batch=self.batch, group=self.group) @final def on_update_opacity(self, new_opacity): super().on_update_opacity(new_opacity) if self.text_label: if self.opacity > 0: self.text_label.color = (*self.base_color, self.opacity) else: self.text_label.delete() self.text_label = None @final @window_size_has_changed def on_window_resize(self, width, height): super().on_window_resize(width, height) if self.text_label: self.text_label.begin_update() self.text_label.x = self.get_x() self.text_label.y = self.get_y() self.text_label.font_size = self.get_font_size() self.text_label.width = self.get_width() self.text_label.end_update() @final @is_active def on_position_update(self): self.text_label.begin_update() self.text_label.x = self.get_x() self.text_label.y = self.get_y() self.text_label.end_update() @final def begin_update(self): self.undergoing_text_update = True @final def end_update(self): self.undergoing_text_update = False if self.text_label: self.text_label.text = self.get_formatted_text()
class Hud(object): """docstring for Hud""" def __init__(self, batch, window): super(Hud, self).__init__() self.text_ = 'This is the warmup phase' self.hp_t = '-1' self.armor_t = '-1' self.text_active = 5 self.killmsg_active = False self.chat_active = False self.batch = batch self.scale = vec2(window.width / 1280., window.height / 720.) self.hp = Label(self.hp_t, font_name=font, font_size=36, bold=True, x=80, y=10, anchor_x='center', anchor_y='bottom', batch=self.batch) self.armor = Label(self.armor_t, font_name=font, font_size=36, x=240, y=10, anchor_x='center', anchor_y='bottom', bold=True, batch=self.batch) self.text = Label(self.text_, font_name=font, font_size=36, x=640, y=360, anchor_x='center', anchor_y='center', batch=self.batch) self.weapon = Label('melee', font_name=font, font_size=32, x=1160, y=80, anchor_x='center', anchor_y='bottom', color=(0, 255, 255, 255), batch=self.batch) self.ammo = Label('1', font_name=font, font_size=36, x=1160, y=10, anchor_x='center', anchor_y='bottom', bold=True, batch=self.batch) self.time = Label('0:00', font_name=font, font_size=36, x=640, y=680, anchor_x='center', anchor_y='center', bold=True, batch=self.batch) self.chatdoc = FormattedDocument('\n' * 11) #self.chatlog = document .FormattedDocument('\n') self.chat = ScrollableTextLayout(self.chatdoc, width=500, height=208, multiline=True, batch=self.batch) self.chat.x = 130 self.chat.y = 130 self.chat.content_valign = 'bottom' self.killdoc = FormattedDocument('\n') self.killmsg = ScrollableTextLayout(self.killdoc, width=300, height=104, multiline=True, batch=self.batch) self.killmsg.x = 20 self.killmsg.y = 600 self.scoredoc = FormattedDocument('0 : 0') self.score = ScrollableTextLayout(self.scoredoc, width=150, height=104, multiline=True, batch=self.batch) self.score.x = 1270 self.score.y = 650 self.score.anchor_x = 'right' self.score.anchor_y = 'center' self.normal_hpcol = (255, 255, 255, 255) self.low_hpcol = (255, 128, 0, 255) self.high_hpcol = (0, 204, 255, 255) self.bname = '_' self.aname = '_' self.gametime = 70 self.weaponcolors = {proto.melee: (0, 255, 255, 255), proto.explBlaster: (255, 255, 0, 255)} self.killmsg_count = 0 self.scoreboard = None self.weaponbar = WeaponBar(self.batch, self.scale) self.do_scale() def do_scale(self): for item in (self.armor, self.hp, self.text, self.chat, self.killmsg, self.time, self.ammo, self.weapon, self.score): item.x *= self.scale.x item.y *= self.scale.y def init_player(self, players): if len(players) == 0: self.set_score(0, 0) else: self.bname = players.values()[0].name self.set_score(0, 0) self.init_pers_hud() def init_pers_hud(self): if self.hp._vertex_lists: self.weaponbar.batch = self.batch self.weapon.batch = self.batch self.ammo.batch = self.batch self.hp.batch = self.batch self.armor.begin_update() self.armor.batch = self.batch self.armor.end_update() def init_spec(self): self.weaponbar.remove() self.weapon.delete() self.ammo.delete() self.hp.delete() self.armor.delete() def draw(self): self.active_batch.draw() def update(self, dt): if self.text_active: self.text_active -= dt if self.text_active <= 0: self.text_active = False self.text.delete() if self.killmsg_active: self.killmsg_active -= dt if self.killmsg_active <= 0: self.killmsg_count -= 1 start = self.killdoc.get_paragraph_start(1) end = self.killdoc.get_paragraph_end(1) self.killdoc.delete_text(start, end) if self.killmsg_count > 0: self.killmsg_active = 4 else: self.killmsg_active = False self.killmsg.delete() if self.chat_active: self.chat_active -= dt if self.chat_active <= 0: self.chat_active = False self.chat.delete() self.time.text = self.calc_time(self.gametime) def update_prop(self, armor=False, hp=False, text=False, weapon=False, ammo=False, time=False, name=False, msg=False, chat=None): if armor: if armor != self.armor.text: self.armor.text = armor if int(armor) <= 20: self.armor.color = self.low_hpcol elif int(armor) > 100: self.armor.color = self.high_hpcol else: self.armor.color = self.normal_hpcol if hp: if hp != self.hp.text: self.hp.text = hp if int(hp) <= 20: self.hp.color = self.low_hpcol elif int(hp) > 100: self.hp.color = self.high_hpcol else: self.hp.color = self.normal_hpcol if text: if not self.text_active: self.text.begin_update() self.text.text = text self.text_active = 3 self.text.batch = self.batch self.text.end_update() else: self.text.text = text self.text_active = 3 if weapon: if weapon != self.weapon.text: self.weapon.text = weapon wkey = [key for key, val in allstrings.iteritems() if val == weapon][0] self.weapon.color = weaponcolors[wkey] + [255] if ammo: ammo, weapons = ammo if ammo != self.ammo.text: self.ammo.text = ammo self.weaponbar.update(weapons) if isinstance(time, float): self.gametime = time if name: a, name = name if a: self.aname = name else: self.bname = name #self.score.text = '0:0 ' + self.bname if chat: name, color, msg = chat self.chat.begin_update() #self.chat.text = chat self.chatdoc.insert_text(len(self.chatdoc.text), name, dict(color=list(color) + [255], bold=True)) self.chatdoc.insert_text(len(self.chatdoc.text), '\t' + msg + '\n', dict(color=[255] * 4, bold=False)) start = self.chatdoc.get_paragraph_start(0) end = self.chatdoc.get_paragraph_end(0) self.chatdoc.delete_text(start, end) """self.chatlog.insert_text(len(self.chatlog.text), name, dict(color=list(color) + [255], bold=True)) self.chatlog.insert_text(len(self.chatlog.text), '\t' + msg + '\n', dict(color=[255] * 4, bold=False))""" self.chat.batch = self.batch self.chat.view_y = -self.chat.content_height self.chat.end_update() self.chat_active = 4 def set_score(self, a, b, msg=False, scoreboard=False): self.score.begin_update() self.scoredoc.delete_text(0, len(self.scoredoc.text)) self.scoredoc.insert_text(0, ''.join((str(a), ' ', self.aname, '\n', str(b), ' ', self.bname)), dict(color=[255] * 4)) apos = self.scoredoc.get_paragraph_start(1) bpos = self.scoredoc.get_paragraph_start(len(self.scoredoc.text) - 1) self.scoredoc.set_style(apos, apos+len(str(a)), dict(font_size=24, baseline=-6)) self.scoredoc.set_style(bpos, bpos+len(str(b)), dict(font_size=24, baseline=-6)) self.score.end_update() self.score.width = self.score.content_width + 40 if msg: w, killer, killed = msg if w == 11: w = 4 if w == 12: w = 5 wcol = weaponcolors['w' + str(w-1)] self.killmsg.begin_update() self.killdoc.insert_text(len(self.killdoc.text), killer, dict(color=[255] * 4)) self.killdoc.insert_text(len(self.killdoc.text), ' fragged', dict(color=wcol + [255])) self.killdoc.insert_text(len(self.killdoc.text), ' '.join(('', killed, '\n')), dict(color=[255] * 4)) self.killmsg.batch = self.batch if not self.killmsg_active: self.killmsg_active = 4 self.killmsg_count += 1 self.killmsg.end_update() if scoreboard: if not self.scoreboard: self.scoreboard = ScoreBoard((a, self.aname), (b, self.bname), self.batch, self.scale) else: if not self.scoreboard is None: self.scoreboard.delete() self.scoreboard = None def calc_time(self, gametime): mins = '{:01.0f}'.format(gametime // 60) secs = '{:02.0f}'.format(gametime % 60) return ''.join((mins, ':', secs))
class DebugLabelHandler: def __init__(self, overlay): self.overlay = overlay self.batch = self.overlay.batch self.debug_lbl = Label('', font_name='Consolas', font_size=12, color=( 0, 0, 0, 255, ), batch=self.batch, x=10, y=self.overlay.height - 10, anchor_x='left', anchor_y='top') self.level = None self.setLevel(self.overlay.game.data.engine.core.debug_lvl) def __call__(self): self.level() def setLevel(self, lvl): if lvl == 0: self.level = self.level_0 elif lvl == 1: self.level = self.level_1 elif lvl == 2: self.level = self.level_2 elif lvl == 3: self.level = self.level_3 @staticmethod def fpsColor(fps): if fps >= 60: return 0, 0, 0, 255, elif fps >= 30: return 255, 120, 0, 255, else: return 255, 0, 0, 255, @staticmethod def level_0(): return def level_1(self): self.debug_lbl.begin_update() fps = self.overlay.game.clock.get_fps() self.debug_lbl.color = self.fpsColor(fps) self.debug_lbl.text = '%02d' % (fps, ) self.debug_lbl.end_update() def level_2(self): self.debug_lbl.begin_update() fps = self.overlay.game.clock.get_fps() self.debug_lbl.color = self.fpsColor(fps) x, y, z, = self.overlay.game.player.position dy = self.overlay.game.player.dy r0, r1, = self.overlay.game.player.rotation self.debug_lbl.text = '%02d (%.2f, %.2f, %.2f // %.2f) (%.2f, %.2f) %d / %d (%.2f %s)' % ( fps, x, y, z, dy, r0, r1, len(self.overlay.game.renderer.rendered), len(self.overlay.game.world), round( len(self.overlay.game.renderer.rendered) / len(self.overlay.game.world) * 100, 2), '%') self.debug_lbl.end_update() def level_3(self): self.level_2() self.overlay.fpslist.append(self.overlay.game.clock.get_fps())
class InventorySlot: def __init__(self, handler, index, x, y, name, count=0, framesize=50): self.handler, self.index, = handler, index, self.x, self.y, = x, y, self.frame = self.createFrame(framesize) self.label = Label('{}\n{}'.format(name, count), anchor_x='center', anchor_y='center', align='center', font_name='Impact', font_size=16, color=(255, 255, 255, 255), bold=False, multiline=True, batch=self.handler.batch, width=100, height=100, x=x, y=100) def __del__(self): self.label.delete() self.frame.delete() def createFrame(self, size): return self.handler.batch.add(4, GL_QUADS, None, ('v2i/static', ( self.x - size, self.y - size, self.x + size, self.y - size, self.x + size, self.y + size, self.x - size, self.y + size, )), ('c4B/static', ( 150, 150, 150, 180, ) * 4)) def updateText(self, selected=False, count=False): self.label.begin_update() if count: self.label.text = '{}\n{}'.format( self.handler.inventory[self.index].name, self.handler.inventory[self.index].get()) if selected: self.label.font_size = 18 else: self.label.font_size = 16 self.label.bold = selected self.label.end_update() def select(self): self.updateText(True) self.setFrame(True) def unselect(self): self.updateText(False) self.setFrame(False) def setFrame(self, selected): self.frame.delete() self.frame = self.createFrame(65 if selected else 50)