def __init__(self, evManager, text, events_attrs=[], rect=None, txtcolor=(255, 0, 0), bgcolor=None): Widget.__init__(self, evManager) # When receiving an event containing text, # replace self.text by that event's text. # events_attrs maps event classes to event text attributes. self.events_attrs = events_attrs if events_attrs: for evtClass in events_attrs: self._em.reg_cb(evtClass, self.on_textevent) # gfx self.font = Font(None, config_get_fontsize()) if rect: self.rect = rect else: self.rect = Rect((0, 0), (100, config_get_fontsize() + 4)) # default width = 100px, # 4px from 1px each of border bottom, # padding bottom, padding top, and border top self.txtcolor = txtcolor self.bgcolor = bgcolor self.text = text self.image = Surface(self.rect.size)
def __init__( self, evManager, text, rect=None, onDownClickEvent=None, onUpClickEvent=None, onMouseMoveOutEvent=None ): Widget.__init__(self, evManager) # Widget init sets dirty to true, hence the actual text rendering # will be done in ButtonWidget.update(), called by the view renderer. self._em.reg_cb(DownClickEvent, self.on_downclick) self._em.reg_cb(UpClickEvent, self.on_upclick) self._em.reg_cb(MoveMouseEvent, self.on_mousemove) # the events to be triggered when the button is clicked or mouse moved self.onDownClickEvent = onDownClickEvent self.onUpClickEvent = onUpClickEvent self.onMouseMoveOutEvent = onMouseMoveOutEvent self.text = text self.font = Font(None, config_get_fontsize()) # default font, 40 pixels high if rect: self.rect = rect self.image = Surface(self.rect.size) # container size from specified rect # if txtimg does not fit in rect, it'll get cut (good thing) else: txtimg = self.font.render(self.text, True, (0, 0, 0)) self.rect = txtimg.get_rect() self.image = Surface(self.rect.size) # container size from rendered text
def __init__(self, evManager, numlines=3, rect=(0, 0, 100, 20), txtcolor=(255, 0, 0), bgcolor=None): Widget.__init__(self, evManager) self._em.reg_cb(ChatlogUpdatedEvent, self.on_remotechat) self._em.reg_cb(MGameAdminEvt, self.on_gameadmin) self._em.reg_cb(MNameChangeFailEvt, self.on_namechangefail) self._em.reg_cb(MMyNameChangedEvent, self.on_namechangesuccess) self._em.reg_cb(MNameChangedEvt, self.on_namechangesuccess) self._em.reg_cb(MdHpsChangeEvt, self.on_updatehps) self.font = Font(None, config_get_fontsize()) self.rect = rect size = rect.size self.txtcolor = txtcolor self.bgcolor = bgcolor if bgcolor: # completely opaque bg img = Surface(size) img.fill(self.bgcolor) img = img.convert() else: # more or less transparent img = Surface(self.rect.size, SRCALPHA) # handles transparency transparency = 50 # 0 = transparent, 255 = opaque img.fill((0, 0, 0, transparency)) # black img = img.convert_alpha() self.image = img self.maxnumlines = numlines self.linewidgets = deque(maxlen=numlines) # deque of TextLabelWidgets
def __init__(self, evManager, numlines=3, rect=(0, 0, 100, 20), txtcolor=(255, 0, 0), bgcolor=None): Widget.__init__(self, evManager) # When receiving an event containing text, # add or remove that text to the widget's set of text lines to display. # addevents_attrs maps event classes to the text attr of events # that add text to display. self.addevents_attrs = {MdAddPlayerEvt: "pname", MNameChangedEvt: "newname", MMyNameChangedEvent: "newname"} for evtClass in self.addevents_attrs: self._em.reg_cb(evtClass, self.on_addtextevent) # rmevents_attrs maps event classes to the text attributes of events # that remove text to display. self.rmevents_attrs = {MPlayerLeftEvt: "pname", MNameChangedEvt: "oldname", MMyNameChangedEvent: "oldname"} for evtClass in self.rmevents_attrs: self._em.reg_cb(evtClass, self.on_rmtextevent) self.texts = [] # Each text is a player name. # gfx self.font = Font(None, config_get_fontsize()) self.rect = rect self.txtcolor = txtcolor self.bgcolor = bgcolor img = Surface(rect.size) if bgcolor: img.fill(bgcolor) else: img.set_alpha(0, RLEACCEL) # fully transparent self.image = img self.maxnumlines = numlines self.linewidgets = deque(maxlen=numlines) # deque of TextLabelWidgets
def build_gui(self): """ Add widgets to the screen. Widgets on the left need only be reblitted when they get dirty. Widgets that overlay the world screen need to be reblitted every frame. """ # start adding widgets leftgui = LayeredDirty() # only reblit when dirty=1 overlaygui = LayeredUpdates() # reblit every frame w, h = self.win_size line_h = config_get_fontsize() gui_w = self.gui_offset # -- name label at top-left of the screen rec = pygame.Rect(0, 0, gui_w - 1, line_h - 1) evt_txt_dict = {MMyNameChangedEvent: 'newname', MGreetNameEvt:'newname'} txtcol = config_get_txtlabel_txtcolor() bgcol = config_get_txtlabel_bgcolor() namebox = TextLabelWidget(self._em, '', events_attrs=evt_txt_dict, rect=rec, txtcolor=txtcol, bgcolor=bgcol) leftgui.add(namebox) # -- list of connected players, until middle of the screen rec = pygame.Rect(0, line_h, gui_w - 1, line_h - 1) txt = 'Connected players:' txtcol = config_get_txtlabel_txtcolor() bgcol = config_get_txtlabel_bgcolor() whosonlinetitle = TextLabelWidget(self._em, txt, rect=rec, txtcolor=txtcol, bgcolor=bgcol) leftgui.add(whosonlinetitle) rec = pygame.Rect(0, 2 * line_h, gui_w - 1, h / 2 - 2 * line_h - 1) numlines = int(rec.height / line_h) txtcol = config_get_chatlog_txtcolor() bgcol = None #config_get_chatlog_bgcolor() whosonlinebox = PlayerListWidget(self._em, numlines, rect=rec, txtcolor=txtcol, bgcolor=bgcol) leftgui.add(whosonlinebox) # -- chat window overlay at bottom of the world screen chat_height = h / 4 numlines = int(chat_height / line_h) if numlines > 0: # text input field rec = pygame.Rect(gui_w + 1, h - line_h, w - gui_w - 1, line_h - 1) chatbox = InputFieldWidget(self._em, rect=rec) overlaygui.add(chatbox) if numlines > 1: # text display line rec = pygame.Rect(gui_w + 1, h * 3 / 4, w - gui_w - 1, h / 4 - line_h - 1) txtcol = config_get_chatlog_txtcolor() # no bg color to disply on top of world screen chatwindow = ChatLogWidget(self._em, numlines=numlines, rect=rec, txtcolor=txtcol) overlaygui.add(chatwindow) self.left_gui_sprites = leftgui self.overlay_gui_sprites = overlaygui
def __init__(self, evManager, rect=None): Widget.__init__(self, evManager) # Widget init sets dirty to true, hence the actual rendering # will be done in InputFieldWidget.update(), called by the view renderer. self._em.reg_cb(NonprintableKeyEvent, self.on_invisiblekeypushed) self._em.reg_cb(UnicodeKeyPushedEvent, self.on_visiblekeypushed) self._em.reg_cb(DownClickEvent, self.on_downclick) self.text = "" self.font = Font(None, config_get_fontsize()) if rect: self.rect = rect else: self.rect = Rect((0, 0), (100, config_get_fontsize() + 4)) # 100px = default width, # 25px = font height, 4px = 1 px for each of border-bottom, # padding bottom, padding top, and border top. # rectangle to be drawn around the box as border border_rect = Rect((0, 0), self.rect.size) # draw and store unfocused empty box emptyboxImg = Surface(self.rect.size) self.unfocused_bgcolor = config_get_unfocusedinput_bgcolor() self.unfocused_txtcolor = config_get_unfocusedinput_txtcolor() emptyboxImg.fill(self.unfocused_bgcolor) pygame.draw.rect(emptyboxImg, self.unfocused_txtcolor, border_rect, 2) self.unfocused_emptyboxImg = emptyboxImg.convert_alpha() self.image = emptyboxImg # draw and store focused empty box emptyboxImg = Surface(self.rect.size) self.focused_bgcolor = config_get_focusedinput_bgcolor() self.focused_txtcolor = config_get_focusedinput_txtcolor() emptyboxImg.fill(self.focused_bgcolor) pygame.draw.rect(emptyboxImg, self.focused_txtcolor, border_rect, 2) self.focused_emptyboxImg = emptyboxImg.convert_alpha() self.textPos = (4, 2) # 4px padding-left and 2px padding-top