Esempio n. 1
0
class PopMenu(object):
    """docstring for PopMenu"""
    def __init__(self, text, textlst, pos, window, batch,
                 line_space=None, font_size=24, hcolor=(25, 25, 25),
                 scl=unity):
        super(PopMenu, self).__init__()
        self.scale = scl
        self.pos = vec2(*pos) * scl
        self.target_pos = pos
        self.window = window
        self.line_space = (line_space + font_size) * self.scale.x
        self.font_size = font_size * self.scale.x
        self.hcolor = hcolor

        #initial text
        self.idoc = pyglet.text.document.UnformattedDocument(text, )
        self.idoc.set_style(0, len(text), dict(font_size=self.font_size,
                            color=[255]*4))
        self.ilayout = pyglet.text.layout.TextLayout(self.idoc, batch=batch,
                                                     group=CustomGroup(-1))
        self.ilayout.anchor_x, self.ilayout.anchor_y = ('left', 'center')
        self.ilayout.x, self.ilayout.y = self.pos

        #other text in popmenu, is inserted later
        self.doc = Doc('\n')
        self.doc.set_paragraph_style(0, 0, dict(indent=None,
                                     line_spacing=self.line_space,
                                     font_size=self.font_size))

        self.lwidth = max(len(txt) * self.font_size*72./96 for txt in textlst)
        tempf = self.idoc.get_font(0)
        self.lheight = tempf.size+tempf.ascent-tempf.descent
        self.textlst = textlst

        self.layout = Layout(self.doc, width=self.lwidth,
                             height=self.lheight*len(textlst)+1,
                             multiline=True, batch=batch)

        self.layout.anchor_x, self.layout.anchor_y = 'left', 'top'
        self.layout.x = self.pos[0]-self.lwidth/2+self.ilayout.content_width/2
        self.layout.y = self.pos[1]

        self.even = 0
        self.active_i = []
        self.curr_line = None
        self.active_line = None

        self.active = False
        self.hlighted = False

        @self.window.event
        def on_mouse_scroll(x, y, scroll_x, scroll_y):
            self.layout.view_y = scroll_y * self.layout.content_height
        self.handler = on_mouse_scroll

    def remove_handler(self):
        self.window.remove_handler('on_mouse_scroll', self.handler)

    def insert(self, text):
        self.even += 1
        iseven = self.even % 2
        text += ' ' * 60
        self.doc.insert_text(len(self.doc.text),
                             text + '\n',
                             dict(font_size=self.font_size, color=[255] * 4,
                                  background_color=[50 * iseven] * 3 + [255]))
        ln = len(self.doc.text) - 3
        self.doc.set_paragraph_style(ln, ln,
                                     dict(indent=None, wrap=False,
                                          line_spacing=self.line_space))

    def over_button(self, x, y):
        self.mpos = (x, y)
        if not self.active:
            return (0 < x - self.pos.x < self.ilayout.content_width and
                    0 < y - self.pos.y + self.ilayout.content_height / 2
                    < self.ilayout.content_height)
        else:
            return (0 < x - self.layout.x < self.layout.width and
                    0 < self.layout.y - y < self.layout.height)

    def highlight(self):
        if not self.active:
            if not self.hlighted:
                self.idoc.set_style(0, len(self.idoc.text),
                                    dict(background_color=[25]*3+[255]))
                self.hlighted = True
        else:
            y = self.layout.y - self.mpos[1] - self.layout.view_y
            lines = self.doc.text.split('\n')
            num = int(float(y) / self.line_space - 0.18)
            try:
                if not self.active_line:
                    ind = self.doc.text.find(lines[num])
                    if (ind, num) not in self.active_i:
                        self.curr_line = (ind, num)
                        self.restore()
                        self.doc.set_paragraph_style(ind, ind, dict(
                                                     background_color=
                                                     self.hcolor + (255,)))
                        self.active_i.append((ind, num))
            except (IndexError, ValueError):
                self.restore()

    def restore(self):
        if not self.active:
            if self.hlighted:
                self.idoc.set_style(0, len(self.idoc.text),
                                    dict(background_color=[0]*3+[255]))
                self.hlighted = False
        if len(self.active_i) > 0:
            for a in self.active_i:
                ind, num = a
                if not a == self.active_line:
                    self.doc.set_paragraph_style(ind, ind, dict(
                                                 background_color=
                                                 [50 * (num % 2)] * 3 + [255]))
            self.active_i = []
            if (ind, num) == self.curr_line:
                self.curr_line = None

    def activate(self):
        if not self.active:
            self.active = True
            self.layout.begin_update()
            for txt in self.textlst:
                self.insert(txt)
            self.layout.end_update()
            return False
        else:
            if self.curr_line[0] != 0:
                lines = self.doc.text.split('\n')
                self.idoc.text = lines[self.curr_line[1]].strip()
                npos = self.pos[0]-self.lwidth/2 + self.ilayout.content_width/2
                self.layout.x = npos
                self.active = False
                self.doc.delete_text(1, len(self.doc.text))
                self.even = 0
                return self.idoc.text

    def deactivate(self):
        self.active = False
        self.doc.delete_text(1, len(self.doc.text))
        self.even = 0
Esempio n. 2
0
class EventLoopFixture(InteractiveFixture):

    question = '\n\n(P)ass/(F)ail/(S)kip/(Q)uit?'
    key_pass = key.P
    key_fail = key.F
    key_skip = key.S
    key_quit = key.Q
    clear_color = 1, 1, 1, 1
    base_options = {
        'width': 300,
        'height': 300,
    }

    def __init__(self, request, peng):
        super(EventLoopFixture, self).__init__(request)
        self._request = request
        self.window = None
        self.text_batch = None
        self.text_document = None
        self.answer = None
        self.peng = peng
        request.addfinalizer(self.tear_down)

    def tear_down(self):
        if self.window:
            self.window.close()
            self.window = None

    def create_window(self, **kwargs):
        combined_kwargs = {}
        combined_kwargs.update(self.base_options)
        combined_kwargs.update(kwargs)
        #self.window = Window(**combined_kwargs)
        self.window = self.peng.createWindow(**combined_kwargs)
        self.window.push_handlers(self)
        return self.window

    def create_menu(self, menu="menu"):
        m = TestMenu(self, menu, self.peng.window, self.peng)
        self.window.addMenu(m)
        self.window.changeMenu(menu)
        return m

    def get_document(self):
        if self.text_document is None:
            self._create_text()
        return self.text_document

    def _create_text(self):
        assert self.window is not None
        self.text_batch = Batch()
        self.text_document = FormattedDocument()
        layout = TextLayout(self.text_document,
                            self.window.width,
                            self.window.height,
                            multiline=True,
                            wrap_lines=True,
                            batch=self.text_batch)
        layout.content_valign = 'bottom'

    def add_text(self, text):
        self.get_document()
        self.text_document.insert_text(len(self.text_document.text), text)
        self.window._legacy_invalid = True

    def ask_question(self, description=None, screenshot=True):
        """Ask a question inside the test window. By default takes a screenshot and validates
        that too."""
        if self.window is None:
            self.create_window()
        self.add_text('\n\n')
        if description:
            self.add_text(description)
        self.add_text(self.question)
        self.answer = None
        caught_exception = None
        try:
            if self.interactive:
                self.run_event_loop()
                self.handle_answer()
            else:
                self.run_event_loop(0.1)
        except Exception as ex:
            import traceback
            traceback.print_exc()
            caught_exception = ex
        finally:
            if screenshot:
                try:
                    screenshot_name = self._take_screenshot(self.window)
                    if caught_exception is None and not self.interactive:
                        self._check_screenshot(screenshot_name)
                except:
                    if not caught_exception:
                        raise
            if caught_exception:
                raise caught_exception

    def handle_answer(self):
        if self.answer is None:
            raise Exception('Did not receive valid input in question window')
        elif self.answer == self.key_fail:
            # TODO: Ask input
            pytest.fail('Tester marked test failed')
        elif self.answer == self.key_skip:
            pytest.skip('Tester marked test skipped')
        elif self.answer == self.key_quit:
            pytest.exit('Tester requested to quit')

    def ask_question_no_window(self, description=None):
        """Ask a question to verify the current test result. Uses the console or an external gui
        as no window is available."""
        super(EventLoopFixture, self).ask_question(description)

    def run_event_loop(self, duration=None):
        if duration:
            clock.schedule_once(self.interrupt_event_loop, duration)
        pyglet.app.run()

    def interrupt_event_loop(self, *args, **kwargs):
        pyglet.app.exit()

    @staticmethod
    def schedule_once(callback, dt=.1):
        clock.schedule_once(callback, dt)

    def on_draw(self):
        self.clear()
        self.draw_text()

    def clear(self):
        gl.glClearColor(*self.clear_color)
        self.window.clear()

    def draw_text(self):
        if self.text_batch is not None:
            self.text_batch.draw()

    def on_key_press(self, symbol, modifiers):
        if symbol in (self.key_pass, self.key_fail, self.key_skip,
                      self.key_quit):
            self.answer = symbol
            self.interrupt_event_loop()

        # Prevent handling of Escape to close the window
        return True
Esempio n. 3
0
class KeysFrame(object):
    """docstring for KeysFrame"""
    def __init__(self, pos, width, height, window, batch,
                 line_space=None, font_size=24, hcolor=(25, 25, 25),
                 scl=unity):
        super(KeysFrame, self).__init__()
        self.scale = scl
        self.pos = vec2(*pos) * scl
        self.target_pos = pos
        self.window = window
        self.line_space = (line_space + font_size) * self.scale.x
        self.font_size = font_size * self.scale.x
        self.width = width * self.scale.x
        self.height = height * self.scale.y
        self.hcolor = hcolor

        self.doc = Doc('\n')
        self.layout = Layout(self.doc, width=self.width, height=self.height,
                             multiline=True, batch=batch)
        self.doc.set_paragraph_style(0, 0, dict(indent=None,
                                     line_spacing=self.line_space,
                                     font_size=self.font_size))

        self.layout.anchor_x = 'left'
        self.layout.anchor_y = 'top'
        self.layout.x = self.pos[0]
        self.layout.y = self.pos[1]

        self.even = 0
        self.active_i = []
        self.curr_line = None
        self.active_line = None

        @self.window.event
        def on_mouse_scroll(x, y, scroll_x, scroll_y):
            self.layout.view_y = scroll_y * self.layout.content_height
        self.handler = on_mouse_scroll

    def remove_handler(self):
        self.window.remove_handler('on_mouse_scroll', self.handler)

    def insert(self, action, key):
        self.even += 1
        iseven = self.even % 2
        action = dict(weaponsdict.items() + inputdict.items())[action]
        text = action + '\t' + key
        #i have no clue how to calculate the width properly
        length = pyglet.text.Label(key, font_size=self.font_size)
        length = length.content_width
        stop = self.width - length
        text += ' ' * 6
        self.doc.insert_text(len(self.doc.text),
                             text + '\n',
                             dict(font_size=self.font_size, color=[255] * 4,
                                  background_color=[50 * iseven] * 3 + [255]))
        ln = len(self.doc.text) - 3
        self.doc.set_paragraph_style(ln, ln,
                                     dict(tab_stops=[stop],
                                          indent=None,
                                          line_spacing=self.line_space,
                                          wrap=False))

    def in_box(self, mpos):
        x, y = mpos
        self.mpos = mpos
        return (0 < x - self.layout.x < self.layout.width and
                0 < self.layout.y - y < self.layout.height)

    def over_button(self, x, y):
        self.mpos = (x, y)
        return (0 < x - self.layout.x < self.layout.width and
                0 < self.layout.y - y < self.layout.height)

    def highlight(self):
        y = self.layout.y - self.mpos[1] - self.layout.view_y
        lines = self.doc.text.split('\n')
        num = int(float(y) / self.line_space - 0.18)
        try:
            if not self.active_line:
                action, key = lines[num].split('\t')
                ind = self.doc.text.find(action) + 1
                if (ind, num) not in self.active_i:
                    self.curr_line = (ind, num)
                    self.restore()
                    self.doc.set_paragraph_style(ind, ind, dict(
                                                 background_color=
                                                 self.hcolor + (255,)))
                    self.active_i.append((ind, num))
        except (IndexError, ValueError):
            self.restore()

    def restore(self):
        if len(self.active_i) > 0:
            for a in self.active_i:
                ind, num = a
                if not a == self.active_line:
                    self.doc.set_paragraph_style(ind, ind, dict(
                                                 background_color=
                                                 [50 * (num % 2)] * 3 + [255]))
            self.active_i = []
            if (ind, num) == self.curr_line:
                self.curr_line = None

    def activate(self):
        if not self.curr_line:
            return
        self.active_line = self.curr_line
        ind, num = self.active_line
        self.doc.set_paragraph_style(ind, ind, dict(background_color=
                                                    (255, 128, 0) + (255,)))

    def deactivate(self):
        self.active_line = None
        self.restore()

    def get_action(self):
        ind, num = self.active_line
        action, key = self.doc.text.split('\n')[num].split('\t')
        self.layout.delete()
        return action

    def update(self):
        pass
Esempio n. 4
0
class MessageQueue(Widget):
    MSG_DELETE_FREQ = 0.5  # in seconds

    def __init__(self, padding=0):
        super(MessageQueue, self).__init__()

        self.font = gui.config.get('control_panel', 'MSG_QUEUE_FONT')
        self.titleLabelText = gui.config.get('control_panel_strings',
                                             'MSG_QUEUE_TITLE')

        bgColorStr = gui.config.get('control_panel', 'MSG_QUEUE_BG_COLOR')
        self.bgColor = list(map(int, tuple(bgColorStr.split(','))))

        self.titleLabel = None
        self.textLayout = None

        self.dt = 0
        self.halfSecs = 0
        self.bgRect = None
        self.border = None
        self.padding = padding

        self.currentPos = 0
        self.numMsgs = 0

        self.doc = FormattedDocument()
        self.msgs = list()

    def reset(self):
        self.doc.text = ""
        self.msgs = list()

    def delete(self):
        if self.textLayout is not None:
            self.textLayout.delete()
        if self.titleLabel is not None:
            self.titleLabel.delete()
            self.titleLabel = None
        if self.bgRect is not None:
            self.bgRect.delete()
            self.bgRect = None
        if self.border is not None:
            self.border.delete()
            self.border = None

    def size(self, frame):
        super(MessageQueue, self).size(frame)

        self.width = frame.width or 200
        self.height = 200

        if self.textLayout is None:
            self.textLayout = TextLayout(self.doc,
                                         width=self.width,
                                         batch=self.parentFrame.batch,
                                         group=fgGroup,
                                         multiline=True)
            self.parentFrame.setNeedsLayout()
        else:
            self.textLayout.width = self.width

        if self.titleLabel is None:
            self.titleLabel = Label(self.titleLabelText,
                                    x=self.x + 10,
                                    y=self.y + 10,
                                    batch=self.parentFrame.batch,
                                    group=fgGroup,
                                    color=(0, 0, 0, 255),
                                    font_name=self.font,
                                    font_size=14)
        else:
            self.titleLabel.x = self.x + 10
            self.titleLabel.y = self.y + 10

    def layout(self, x, y):
        super(MessageQueue, self).layout(x, y)
        self.textLayout.x = self.x + self.padding
        self.textLayout.y = self.y - self.padding
        self.textLayout.width = self.width
        self.textLayout.height = self.height
        self.titleLabel.x = self.x + 10
        self.titleLabel.y = self.y + 10

        self.createBackground()

    def createBackground(self):
        if self.bgRect is not None:
            self.bgRect.delete()
            self.bgRect = None
        if self.border is not None:
            self.border.delete()
            self.border = None

        self.bgRect = createRect(self.x, self.y, self.width, self.height,
                                 self.bgColor, self.parentFrame.batch, mgGroup)

        self.border = createRect(self.x, self.y + self.height, self.width, 2,
                                 (0, 0, 0, 255), self.parentFrame.batch,
                                 highlightGroup)

    def deleteMsg(self, item):
        self.msgs.remove(item)
        self.doc.delete_text(item.index, item.index + len(item))
        for msg in self.msgs:
            if msg.num > item.num:
                msg.num -= 1
                msg.index -= len(item)

    def addMessage(self, message):
        if len(self.msgs) and message == self.msgs[0].string:
            return

        message += '\n'
        item = Message(message, self.halfSecs)

        self.doc.set_style(0, len(self.doc.text), {
            'font_name': self.font,
            'bold': False
        })
        for msg in self.msgs:
            msg.num += 1
            msg.index += len(item)

        self.msgs.insert(0, item)
        self.doc.insert_text(0, message, {
            'font_name': self.font,
            'bold': True
        })

        if self.textLayout is not None:
            while (self.textLayout.content_height >=
                   self.height - self.titleLabel.content_height + 14):
                item = self.msgs[len(self.msgs) - 1]
                self.deleteMsg(item)

    def update(self, dt):
        self.dt += dt
        if self.dt >= self.MSG_DELETE_FREQ:
            # expired messages are collected on 0.5 sec freq
            self.halfSecs += 1
            self.dt %= self.MSG_DELETE_FREQ
            toRemove = []
            for item in self.msgs:
                if item.isExpired(self.halfSecs):
                    toRemove.append(item)
            if len(toRemove):
                for item in toRemove:
                    self.deleteMsg(item)
Esempio n. 5
0
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))
Esempio n. 6
0
class WeaponBar(object):
    """docstring for WeaponBar"""
    def __init__(self, batch, scale):
        super(WeaponBar, self).__init__()
        self.batch = batch
        self.weapons = {}
        self.ammos = []
        self.tris = []
        self.scale = scale

    def __len__(self):
        return len(self.weapons)

    def init_bar(self, weapons):
        try:
            if self.ammolayout._document:
                self.ammolayout.delete()
                for tri in self.tris:
                    tri.remove()
        except AttributeError:
            pass
        ammotext = '\t'.join(str(w.ammo) for key, w in weapons.iteritems()
                             if key != 'w0')
        self.ammos = ammotext.split('\t')
        self.ammodoc = FormattedDocument(ammotext)
        self.ammodoc.set_style(0, len(ammotext), dict(color=[255]*4,
                               tab_stops=[120*(i+1) for i in range(6)],
                               font_size=24, align='center', wrap=False))
        self.ammolayout = IncrementalTextLayout(self.ammodoc, width=600,
                                                height=50, batch=self.batch,
                                                multiline=True)
        self.ammolayout.x = (1280 / 2) * self.scale.x
        self.ammolayout.y = 20 * self.scale.y
        self.ammolayout.anchor_x = 'center'
        self.ammolayout.anchor_y = 'bottom'
        w = self.ammolayout.content_width
        colorlist = [weaponcolors[key] for key in weapons if key != 'w0']
        self.tris = [Triangle(640*self.scale.x-w/2-52+120*i, 35*self.scale.y,
                     50, 50, col, self.batch, 0, 0)
                     for i, col in enumerate(colorlist)]

    def update(self, weapons):
        if len(weapons) != len(self.weapons):
            self.init_bar(weapons)
            self.weapons = weapons.copy()
        else:
            ammotext = '\t'.join(str(w.ammo) for key, w in weapons.iteritems()
                                 if key != 'w0')
            ammos = ammotext.split('\t')
            self.ammolayout.begin_update()
            for i, a in enumerate(ammos):
                if a != self.ammos[i]:
                    ln = sum(len(self.ammos[j]) for j in range(i)) + i
                    self.ammodoc.delete_text(ln, ln + len(self.ammos[i]))
                    self.ammodoc.insert_text(ln, a)
            self.ammolayout.end_update()

            self.ammos = ammos

    def remove(self):
        try:
            self.weapons = {}
            self.ammolayout.delete()
            for tri in self.tris:
                tri.remove()
        except AttributeError:
            pass
Esempio n. 7
0
class MessageQueue(Widget):
    MSG_DELETE_FREQ = 0.5  # in seconds

    def __init__(self, padding=0):
        super(MessageQueue, self).__init__()

        self.font = gui.config.get('control_panel', 'MSG_QUEUE_FONT')
        self.titleLabelText = gui.config.get('control_panel_strings', 'MSG_QUEUE_TITLE')

        bgColorStr = gui.config.get('control_panel', 'MSG_QUEUE_BG_COLOR')
        self.bgColor = list(map(int, tuple(bgColorStr.split(','))))

        self.titleLabel = None
        self.textLayout = None

        self.dt = 0
        self.halfSecs = 0
        self.bgRect = None
        self.border = None
        self.padding = padding

        self.currentPos = 0
        self.numMsgs = 0

        self.doc = FormattedDocument()
        self.msgs = list()

    def reset(self):
        self.doc.text = ""
        self.msgs = list()

    def delete(self):
        if self.textLayout is not None:
            self.textLayout.delete()
        if self.titleLabel is not None:
            self.titleLabel.delete()
            self.titleLabel = None
        if self.bgRect is not None:
            self.bgRect.delete()
            self.bgRect = None
        if self.border is not None:
            self.border.delete()
            self.border = None

    def size(self, frame):
        super(MessageQueue, self).size(frame)

        self.width = frame.width or 200
        self.height = 200

        if self.textLayout is None:
            self.textLayout = TextLayout(self.doc,
                                         width=self.width,
                                         batch=self.parentFrame.batch,
                                         group=fgGroup,
                                         multiline=True)
            self.parentFrame.setNeedsLayout()
        else:
            self.textLayout.width = self.width

        if self.titleLabel is None:
            self.titleLabel = Label(self.titleLabelText,
                                    x=self.x + 10, y=self.y + 10,
                                    batch=self.parentFrame.batch,
                                    group=fgGroup,
                                    color=(0, 0, 0, 255),
                                    font_name=self.font,
                                    font_size=14)
        else:
            self.titleLabel.x = self.x + 10
            self.titleLabel.y = self.y + 10

    def layout(self, x, y):
        super(MessageQueue, self).layout(x, y)
        self.textLayout.x = self.x + self.padding
        self.textLayout.y = self.y - self.padding
        self.textLayout.width = self.width
        self.textLayout.height = self.height
        self.titleLabel.x = self.x + 10
        self.titleLabel.y = self.y + 10

        self.createBackground()

    def createBackground(self):
        if self.bgRect is not None:
            self.bgRect.delete()
            self.bgRect = None
        if self.border is not None:
            self.border.delete()
            self.border = None

        self.bgRect = createRect(self.x, self.y,
                                 self.width, self.height,
                                 self.bgColor,
                                 self.parentFrame.batch,
                                 mgGroup)

        self.border = createRect(self.x, self.y + self.height,
                                 self.width, 2,
                                 (0, 0, 0, 255),
                                 self.parentFrame.batch,
                                 highlightGroup)

    def deleteMsg(self, item):
        self.msgs.remove(item)
        self.doc.delete_text(item.index, item.index + len(item))
        for msg in self.msgs:
            if msg.num > item.num:
                msg.num -= 1
                msg.index -= len(item)

    def addMessage(self, message):
        if len(self.msgs) and message == self.msgs[0].string:
            return

        message += '\n'
        item = Message(message, self.halfSecs)

        self.doc.set_style(0, len(self.doc.text), {'font_name': self.font,
                                                   'bold': False})
        for msg in self.msgs:
            msg.num += 1
            msg.index += len(item)

        self.msgs.insert(0, item)
        self.doc.insert_text(0, message, {'font_name': self.font,
                                          'bold': True})

        if self.textLayout is not None:
            while (self.textLayout.content_height >=
                    self.height - self.titleLabel.content_height + 14):
                item = self.msgs[len(self.msgs) - 1]
                self.deleteMsg(item)

    def update(self, dt):
        self.dt += dt
        if self.dt >= self.MSG_DELETE_FREQ:
            # expired messages are collected on 0.5 sec freq
            self.halfSecs += 1
            self.dt %= self.MSG_DELETE_FREQ
            toRemove = []
            for item in self.msgs:
                if item.isExpired(self.halfSecs):
                    toRemove.append(item)
            if len(toRemove):
                for item in toRemove:
                    self.deleteMsg(item)