def showCity(handle, xpos, ypos): """Display city information screen""" city = SDATA.data.map.regions["sardegna"].city print str(city) w = SGFX.gui.iWidth('startup') h = SGFX.gui.iHeight('startup') window = SWINDOW.CWindow(-1, -1, w, h, "City Info", True) window.modal = True # we need a label for the name lbl_name = SWIDGET.buildLabel(city.name, SPQR.FONT_VERA_LG) lbl_name.rect.x = w / 2 - 25 lbl_name.rect.y = 20 # a picture of city and surrounding area picture = pygame.Surface((100, 100)) picture.blit(SGFX.gui.image("buffer"), (0, 0)) city_image = SWIDGET.buildUniqueImage(picture) city_image.rect.x = 30 city_image.rect.y = 50 # labels for data: lbl_population = SWIDGET.buildLabel("Population: ") lbl_population.rect.x = 150 lbl_population.rect.y = 50 lbl_population_data = SWIDGET.buildLabel(str(city)) lbl_population_data.rect.x = 200 lbl_population_data.rect.y = 50 lbl_happiness = SWIDGET.buildLabel("Happiness: ") lbl_happiness.rect.x = 150 lbl_happiness.rect.y = 80 lbl_food = SWIDGET.buildLabel("Food consumption: ") lbl_food.rect.x = 150 lbl_food.rect.y = 110 btn_ok = SWIDGET.CButton(w / 2, h - 50, "OK") btn_ok.callbacks.mouse_lclk = killModalWindow btn_ok.active = True widgets = [ lbl_name, city_image, lbl_population, lbl_happiness, lbl_food, btn_ok ] for i in widgets: window.addWidget(i) SGFX.gui.addWindow(window) SGFX.gui.unitFlashAndOff() # setup dirty rect stuff SGFX.gui.addDirtyRect(window.drawWindow(), window.rect) return True
def widgetTest(handle, xpos, ypos): """Routine to bring up a window with all widgets on display. Used to test widgets, as the name implies""" # we start by building up all of the widgets lbl_widget = SWIDGET.buildLabel("Label") lbl_widget.rect.x = 10 lbl_widget.rect.y = 10 btn_widget = SWIDGET.CButton(10, 30, "Quit") btn_widget.callbacks.mouse_lclk = killModalWindow btn_widget.active = True chk_widget = SWIDGET.CCheckBox(10, 68, True) chk_widget.active = True sld_widget = SWIDGET.CSlider(10, 90, 150, 0, 150, 75) sld_widget.active = True dc_widget = SWIDGET.buildLabel("Double-Click Me!") dc_widget.rect.x = 20 dc_widget.rect.y = 230 dc_widget.active = True dc_widget.callbacks.mouse_dclick = dclickTest options = ["Romans", "Iberians", "Greeks", "Selucids"] opt_widget = SWIDGET.COptionMenu(120, 30, options) opt_widget.active = True w = SGFX.gui.iWidth("test_image") scl_widget = SWIDGET.CScrollArea(10, 114, w, 96, SGFX.gui.image("test_image")) scl_widget.active = True # make sure we have a console output for the example slider sld_widget.setUpdateFunction(displaySliderContents) # let's have a window index = SGFX.gui.addWindow( SWINDOW.CWindow(-1, -1, 260, 300, "Widget Test", True)) # add items to window SGFX.gui.windows[index].addWidget(lbl_widget) SGFX.gui.windows[index].addWidget(btn_widget) SGFX.gui.windows[index].addWidget(chk_widget) SGFX.gui.windows[index].addWidget(sld_widget) SGFX.gui.windows[index].addWidget(scl_widget) SGFX.gui.windows[index].addWidget(dc_widget) SGFX.gui.windows[index].addWidget(opt_widget) # set it modal SGFX.gui.windows[index].modal = True # there is only one key, but don't forget to add an enter one button windows SGFX.gui.keyboard.addKey(K_q, killModalWindow) SGFX.gui.keyboard.addKey(K_RETURN, killModalWindow) SGFX.gui.keyboard.setModalKeys(2) # turn off unit animations for the moment and thats it SGFX.gui.unitFlashAndOff() # add the dirty rect details SGFX.gui.addDirtyRect(SGFX.gui.windows[index].drawWindow(), SGFX.gui.windows[index].rect) return True
def welcomeScreen(handle, xpos, ypos): """Routine displays opening screen, with load/save/new/about buttons. Always returns True after doing it's work""" ## kill if there are another modalWindow before #killModalWindow(0, 0, 0) # set the sizes w = SGFX.gui.iWidth("startup") h = SGFX.gui.iHeight("startup") # build the window welcome = SWINDOW.CWindow(-1, -1, w, h, "SPQR " + SPQR.VERSION, True) # add the image that pretty much takes up the whole area: main_img = SWIDGET.buildImage("startup") welcome.addWidget(main_img) # create the 4 main buttons btn_new = SWIDGET.CButton(460, 12, "New") btn_load = SWIDGET.CButton(460, 52, "Load") btn_options = SWIDGET.CButton(460, 92, "Options") btn_about = SWIDGET.CButton(460, 132, "About") btn_quit = SWIDGET.CButton(460, 192, "Quit") # make active # add callbacks btn_new.callbacks.mouse_lclk = factionOptionScreen btn_load.callbacks.mouse_lclk = menuLoad btn_options.callbacks.mouse_lclk = menuPreferences btn_about.callbacks.mouse_lclk = menuHelpAbout btn_quit.callbacks.mouse_lclk = quitSpqr # include a link for information about pygame # we'll do this by adding an active button that is not displayed btn_pygame = SWIDGET.CButton(420, 332, "*BLANK*") # resize it and don't display! btn_pygame.rect.width = 127 btn_pygame.rect.height = 45 btn_pygame.visible = False # add a simple callback btn_pygame.callbacks.mouse_lclk = displayPygameInfo # add all that to the window for i in [btn_new, btn_load, btn_options, btn_about, btn_quit, btn_pygame]: i.active = True welcome.addWidget(i) # make modal welcome.modal = True # add the modal key events: n=new, l=load, o=options, a=about, q=quit SGFX.gui.keyboard.addKey(K_n, factionOptionScreen) SGFX.gui.keyboard.addKey(K_l, menuLoad) SGFX.gui.keyboard.addKey(K_o, menuPreferences) SGFX.gui.keyboard.addKey(K_a, menuHelpAbout) SGFX.gui.keyboard.addKey(K_q, quitSpqr) SGFX.gui.keyboard.setModalKeys(5) # turn off unit animations SGFX.gui.unitFlashAndOff() # add the window as a dirty image SGFX.gui.addDirtyRect(welcome.drawWindow(), welcome.rect) SGFX.gui.addWindow(welcome) return True
def createWidget(wlist): """ Function that creates a widget and returns it based on the passed list of properties """ # First we check the key that will always be in our widget's # and defines the widget's purpose if wlist['widget'] == "CLabel": # if this is a label make it with a text label = SWIDGET.buildLabel(wlist['text']) # and add the cords to the rect label.rect.x = wlist['x'] label.rect.y = wlist['y'] # Check if there is any widgets try: checkCallbacks(label, wlist['callbacks']) except: pass return label elif wlist['widget'] == "CCheckBox": intro = SWIDGET.CCheckBox(wlist['x'], wlist['y'], wlist['initial']) return intro elif wlist['widget'] == "COptionMenu": options = SWIDGET.COptionMenu(wlist['x'], wlist['y'], wlist['options']) options.describe = wlist['describe'] return options elif wlist['widget'] == "CSeperator": sepbar = SWIDGET.CSeperator(wlist['x'], wlist['y'], wlist['w']) return sepbar elif wlist['widget'] == "CSlider": sepbar = SWIDGET.CSlider(wlist['x'], wlist['y'], wlist['w'], wlist['start'], wlist['stop'], wlist['initial']) return sepbar elif wlist['widget'] == "CButton": # if it is a button we create it and then we must put the callbacks in button = SWIDGET.CButton(wlist['x'], wlist['y'], wlist['text']) # for every callback we check the given function from the global list checkCallbacks(button, wlist['callbacks']) return button elif wlist['widget'] == "CScrollArea": image = SGFX.gui.image(wlist['image']) sepbar = SWIDGET.CScrollArea(wlist['x'], wlist['y'], wlist['w'], wlist['h'], image) sepbar.setUpdateFunction(getattr(SEVENTS, wlist['update'])) return sepbar elif wlist['widget'] == "CButtonDetails": buttondetail = SWINDOW.CButtonDetails( wlist['text'], wlist['key'], getattr(SEVENTS, wlist["callback"])) return buttondetail
def factionOptionScreen(handle, xpos, ypos): """Screen to choose the starting faction for the player""" #kill the welcomeScreen killModalWindow(0, 0, 0) # set the sizes w = 700 h = 420 # build the window1 window_factions = SWINDOW.CWindow(-1, -1, w, h, 'Factions playable', True) # list faction playable lbl_gauls = SWIDGET.buildLabel('Gauls') lbl_gauls.rect.x = 70 lbl_gauls.rect.y = 20 logo_gauls = SWIDGET.CImage(40, 40, 110, 130, 'logo_gauls') lbl_germans = SWIDGET.buildLabel('Germans') lbl_germans.rect.x = 200 lbl_germans.rect.y = 20 logo_germans = SWIDGET.CImage(180, 40, 110, 130, 'logo_germans') lbl_egypt = SWIDGET.buildLabel('Egypt') lbl_egypt.rect.x = 350 lbl_egypt.rect.y = 20 logo_egypt = SWIDGET.CImage(320, 40, 110, 130, 'logo_egypt') lbl_greeks = SWIDGET.buildLabel('Greeks') lbl_greeks.rect.x = 70 lbl_greeks.rect.y = 180 logo_greeks = SWIDGET.CImage(40, 200, 110, 130, 'logo_greeks') lbl_carthage = SWIDGET.buildLabel('Carthage') lbl_carthage.rect.x = 200 lbl_carthage.rect.y = 180 logo_carthage = SWIDGET.CImage(180, 200, 110, 130, 'logo_carthage') lbl_rome = SWIDGET.buildLabel('Rome') lbl_rome.rect.x = 350 lbl_rome.rect.y = 180 logo_rome = SWIDGET.CImage(320, 200, 110, 130, 'logo_rome') # faction option lbl_options_faction = SWIDGET.buildLabel('Faction choose: ') lbl_options_faction.rect.x = 500 lbl_options_faction.rect.y = 20 options_faction = SWIDGET.COptionMenu( 500, 40, ['Gauls', 'Germans', 'Egypt', 'Greeks', 'Carthage', 'Rome']) options_faction.describe = "opt-faction" # AI Difficulty option lbl_ai = SWIDGET.buildLabel('AI Difficulty: ') lbl_ai.rect.x = 500 lbl_ai.rect.y = 90 options_ai = SWIDGET.COptionMenu(500, 110, ['Easy', 'Medium', 'Hard']) options_ai.describe = "opt-IA" # army option lbl_army = SWIDGET.buildLabel('Army: ') lbl_army.rect.x = 500 lbl_army.rect.y = 160 options_army = SWIDGET.COptionMenu( 500, 180, ['60 soldiers', '120 soldiers', '200 soldiers']) options_army.describe = "opt-Army" # a separator sep = SWIDGET.CSeperator(130, 350, 350) # create the 2 main buttons btn_play = SWIDGET.CButton(300, 370, "Play") btn_previous = SWIDGET.CButton(205, 370, "Previous") # add callbacks btn_play.callbacks.mouse_lclk = startGame btn_previous.callbacks.mouse_lclk = welcomeScreen #btn_previous.callbacks.mouse_lclk = killModalWindow # widgets list widgets = [ btn_play, btn_previous, sep, lbl_ai, options_ai, lbl_gauls, logo_gauls, lbl_germans, logo_germans, lbl_egypt, logo_egypt, lbl_greeks, logo_greeks, lbl_carthage, logo_carthage, lbl_rome, logo_rome, lbl_options_faction, options_faction, lbl_army, options_army ] # add all that to the window for i in widgets: i.active = True window_factions.addWidget(i) # make modal window_factions.modal = True # turn off unit animations SGFX.gui.unitFlashAndOff() # add the window as a dirty image SGFX.gui.addDirtyRect(window_factions.drawWindow(), window_factions.rect) SGFX.gui.addWindow(window_factions) return True
def menuEmpireCouncil(handle, xpos, ypos): """Temp routine, just displays a messagebox for now""" #string = "It is not possible to visit the senate at this moment in time." #SGFX.gui.messagebox(SPQR.BUTTON_OK, string, "Visit Senate") w = SGFX.gui.iWidth("startup") h = SGFX.gui.iHeight("startup") * 1.5 window = SWINDOW.CWindow(-1, -1, w, h, "Visit Council", True) window.modal = True ## image spqr spqr_img = SWIDGET.buildImage('logo_rome') spqr_img.rect.x = SGFX.gui.iWidth('logo_rome') / 2 spqr_img.rect.y = 20 ## separators from the top and the bottom sep_top = SWIDGET.CSeperator(w / 10, h / 3, w / 1.2) sep_bottom = SWIDGET.CSeperator(w / 3, h - 70, w / 3) ## statics from your empire lbl_population = SWIDGET.buildLabel('Population: ') lbl_population.rect.x = 190 lbl_population.rect.y = 30 lbl_happiness = SWIDGET.buildLabel('Happiness: ') lbl_happiness.rect.x = 190 lbl_happiness.rect.y = 60 lbl_regions = SWIDGET.buildLabel('Regions: ') lbl_regions.rect.x = 190 lbl_regions.rect.y = 90 lbl_food = SWIDGET.buildLabel('Food: ') lbl_food.rect.x = 190 lbl_food.rect.y = 120 lbl_naval_armies = SWIDGET.buildLabel('Naval Armies: ') lbl_naval_armies.rect.x = 370 lbl_naval_armies.rect.y = 30 lbl_armies = SWIDGET.buildLabel('Armies: ') lbl_armies.rect.x = 370 lbl_armies.rect.y = 60 lbl_faction_at_war = SWIDGET.buildLabel('At war with: ') lbl_faction_at_war.rect.x = 370 lbl_faction_at_war.rect.y = 90 lbl_faction_allies = SWIDGET.buildLabel('Allies: ') lbl_faction_allies.rect.x = 370 lbl_faction_allies.rect.y = 120 string = SWIDGET.buildLabel( "It is not possible to visit the council at this moment in time.") string.rect.x = 50 string.rect.y = 250 ## ok button btn_ok = SWIDGET.CButton(w / 2.2, h - 50, "OK") btn_ok.callbacks.mouse_lclk = killModalWindow btn_ok.active = True ## add widgets to window widgets = [ spqr_img, btn_ok, sep_top, sep_bottom, lbl_population, lbl_happiness, lbl_food, lbl_armies, lbl_naval_armies, lbl_faction_at_war, lbl_faction_allies, lbl_regions, string ] for i in widgets: window.addWidget(i) SGFX.gui.addWindow(window) SGFX.gui.unitFlashAndOff() # setup dirty rect stuff SGFX.gui.addDirtyRect(window.drawWindow(), window.rect) return True
def menuPreferences(handle, xpos, ypos): """Display the user preferences window. You can only really play with the music and volume settings for now""" # first off, let's make the window that we need index = SGFX.gui.addWindow( SWINDOW.CWindow(-1, -1, 328, 252, "SPQR Preferences", True)) # we'll need an image first img = SWIDGET.buildImageAlpha("img_music") img.rect.x = 20 img.rect.y = 20 ## now image for display monitor img_display = SWIDGET.buildImageAlpha('display') img_display.rect.x = 20 img_display.rect.y = 90 # a couple of labels, and there positions lbl_MusicOn = SWIDGET.buildLabel("Music On:") lbl_MusicOn.rect.x = 88 lbl_MusicOn.rect.y = 28 lbl_Volume = SWIDGET.buildLabel("Volume:") lbl_Volume.rect.x = 88 lbl_Volume.rect.y = 60 ## new label for screen settings lbl_screenResolution = SWIDGET.buildLabel('Resolution:') lbl_screenResolution.rect.x = 88 lbl_screenResolution.rect.y = 100 ## option for display monitor options_display = SWIDGET.COptionMenu( 178, 95, ['Fullscreen', '1024x768', '800x600']) options_display.describe = "opt-Resolution" options_display.active = True # a checkbox for the music option chk_Volume = SWIDGET.CCheckBox(180, 30, SSFX.sound.music_playing) chk_Volume.active = True # connect it to some code chk_Volume.addAfterClick(musicCheckbox) # a slider for the volume sld_Volume = SWIDGET.CSlider(180, 62, 140, 0, 100, SSFX.sound.getVolume()) sld_Volume.active = True # connect to some code sld_Volume.setUpdateFunction(setVolume) # a seperator sep = SWIDGET.CSeperator(10, 190, 300 - SPQR.WINSZ_SIDE) ## an apply button btn_apply = SWIDGET.CButton(120, 215, 'Apply') btn_apply.callbacks.mouse_lclk = resizeWindow btn_apply.active = True # and an ok button btn_ok = SWIDGET.CButton(210, 215, "OK") btn_ok.callbacks.mouse_lclk = killModalWindow btn_ok.active = True # add them all to our window SGFX.gui.windows[index].addWidget(img) SGFX.gui.windows[index].addWidget(lbl_MusicOn) ## add news settings SGFX.gui.windows[index].addWidget(img_display) SGFX.gui.windows[index].addWidget(lbl_screenResolution) SGFX.gui.windows[index].addWidget(options_display) SGFX.gui.windows[index].addWidget(lbl_Volume) SGFX.gui.windows[index].addWidget(chk_Volume) SGFX.gui.windows[index].addWidget(sld_Volume) SGFX.gui.windows[index].addWidget(sep) SGFX.gui.windows[index].addWidget(btn_apply) SGFX.gui.windows[index].addWidget(btn_ok) # make the window modal SGFX.gui.windows[index].modal = True # add the new key event: o = ok SGFX.gui.keyboard.addKey(K_o, killModalWindow) SGFX.gui.keyboard.setModalKeys(1) # turn off unit animations SGFX.gui.unitFlashAndOff() # setup dirty rect stuff SGFX.gui.addDirtyRect(SGFX.gui.windows[index].drawWindow(), SGFX.gui.windows[index].rect) # and thats us done return True
def buildButtonArea(self, button_list, lhs=False): """Function to add buttons to bottom of a window. The window's size is amended to take account of this. The routine adds the buttons, and also the seperator widget used. Pass a list of button_details. If the list is empty, nothing is done The buttons are taken but .pop(0), so index 0 is first to be done The final parameter asks to place the last button on the extreme left hand side if equal to True. Returns a list of index numbers to the buttons if if all ok, False otherwise (and False leaves window as it was to start with)""" # any buttons at all? If not, don't do anything if len(button_list) == 0: return True bindex = [] # Firstly, we need to work out how many buttons can fit on 1 line # get the real width of the window minus it's sides width = self.rect.w - (2 * SPQR.WINSZ_SIDE) # and the button size: bwidth = SGFX.gui.iWidth("button") # the basic button layout is as follows: # we add a sep bar immediatly below. This is always 2 pixels in height # The button holding area below this is always 2*BUTTON_HEIGHT, # with the buttons being placed in the centre. # The buttons are added from right to left, and from top to bottom # they are spaced, with from left to right, (SPACER*2),button etc... # firstly, have we enough room for 1 button even? if width < (SPQR.SPACER * 4) + bwidth: # cant do it, so return false if SPQR.DEBUG_MODE == True: print "Error: Couldn't add buttons (window too small)" return False # how many buttons can we add then? padding = SPQR.SPACER * 2 totb = (width - padding) / (bwidth + SPQR.SPACER) # hopefully we can get away with only one level of buttons: if totb >= len(button_list): # yes, all buttons go on the one line # start by extending the size of the window and rebuilding the image extend_height = (SGFX.gui.iHeight("button") * 2) + 2 self.rect.h += extend_height new_image = pygame.Surface((self.rect.w, self.rect.h)) new_image.fill(SPQR.BGUI_COL) # blit most of the old image: new_image.blit(self.image, (0, 0), (0, 0, self.rect.w, self.rect.h - (SPQR.WINSZ_BOT + extend_height))) # blit bottom to the bottom (!) new_image.blit(self.image, (0, self.rect.h - SPQR.WINSZ_BOT), (0, self.rect.h - (SPQR.WINSZ_BOT + extend_height), self.rect.w, SPQR.WINSZ_BOT)) # we now need to draw in the sides that are missing. # area to draw is actually pretty small. We can assume that there # is enough space already on the present window to draw from # it sure makes the code a heck of a lot smaller :-) new_image.blit( self.image, (0, (self.rect.h - (extend_height + SPQR.WINSZ_BOT))), (0, SPQR.WINSZ_TOP, SPQR.WINSZ_SIDE, extend_height + 1)) new_image.blit(self.image, (self.rect.w - SPQR.WINSZ_SIDE, (self.rect.h - (extend_height + SPQR.WINSZ_BOT))), (self.rect.w - SPQR.WINSZ_SIDE, SPQR.WINSZ_TOP, SPQR.WINSZ_SIDE, extend_height + 1)) # new image is now complete, copy it across self.image = new_image # now we can start to add the various parts. Easiest of all is the bar # you may question the maths here (how do we know the length is going to be # big enough?), but all we are after is a width >(2*SPACER), ok (if SPACER is # fairly small) because we already tested for button width earlier self.addWidget( SWIDGET.CSeperator( SPQR.SPACER, self.rect.h - (extend_height + SPQR.WINSZ_TOP), (self.rect.w - (2 * (SPQR.WINSZ_SIDE + SPQR.SPACER))))) # now we add the buttons xpos = width - ((2 * SPQR.SPACER) + SGFX.gui.iWidth("button")) ypos = (self.rect.h - (extend_height + SPQR.WINSZ_TOP)) ypos += (extend_height - SGFX.gui.iHeight("button") / 2) while len(button_list) > 0: # get the next button button = button_list.pop(0) # could be the last button... if len(button_list) == 1 and lhs == True: # amend xpos - real easy xpos = SPQR.SPACER # build the button bwidget = SWIDGET.CButton(xpos, ypos, button.text) bwidget.active = True # and then add it bindex.append(self.addWidget(bwidget)) # might as well add the callback now... bwidget.callbacks.mouse_lclk = button.event # and the keystuff, if needed: if button.key != None: SGFX.gui.keyboard.addKey(button.key, button.event) # reset x position xpos -= (2 * SPQR.SPACER) + SGFX.gui.iWidth("button") else: # 2 lines of buttons not implemented if SPQR.DEBUG_MODE == True: print "Error: >1 line of buttons not implemented in buildButtonArea()" return False # one last thing. Recentre the window? if self.centre == True: self.centreWindow() # everything went ok. it seems. I leave it to the calling code to # deal with making the window modal, etc... return (bindex)