예제 #1
0
import pygameui as ui


def actionHandler(widget, action, data):
    if action == 'action1':
        win.idButton.text = 'Thank you'
        win.idButton.tooltip = 'Press ESC to quit.'
        win.idButton.action = 'action2'
    else:
        win.idButton.text = 'Oh, thank you'
        win.idButton.tooltip = 'Press ESC to quit.'
        win.idButton.action = 'action1'


ui.SkinableTheme.setSkin("../OSSkin")
app = ui.Application()

win = ui.Window(app,
                title='Hello',
                rect=pygame.Rect(0, 0, 170, 120),
                layoutManager=ui.SimpleGridLM(),
                rightButtonClose=1)
ui.Button(win,
          id='idButton',
          text='Press Me',
          layout=(1, 1, 6, 1),
          tooltip='This is tooltip.',
          action='action1')
ui.Label(
    win,
    text='Press ESC to quit.',
예제 #2
0
def runClient(options):

    # log initialization
    log.message("Starting Outer Space Client", ige.version.versionString)
    log.debug("sys.path =", sys.path)
    log.debug("os.name =", os.name)
    log.debug("sys.platform =", sys.platform)
    log.debug("os.getcwd() =", os.getcwd())
    log.debug("sys.frozen =", getattr(sys, "frozen", None))

    # create required directories
    if not os.path.exists(options.configDir):
        os.makedirs(options.configDir)
    log.debug("options.configDir =", options.configDir)

    running = 1
    first = True
    #while running:
    if not first:
        reload(osci)
    # parse configuration
    if first:
        import osci.gdata as gdata
    else:
        reload(gdata)

    gdata.config = Config(
        os.path.join(options.configDir, options.configFilename))
    gdata.config.game.server = options.server

    setDefaults(gdata, options)

    language = gdata.config.client.language
    import gettext
    log.debug('OSCI', 'Installing translation for:', language)
    if language == 'en':
        log.debug('OSCI', 'English is native - installing null translations')
        tran = gettext.NullTranslations()
    else:
        try:
            tran = gettext.translation('OSPACE',
                                       resources.get('translations'),
                                       languages=[language])
        except IOError:
            log.warning('OSCI', 'Cannot find catalog for', language)
            log.message('OSCI', 'Installing null translations')
            tran = gettext.NullTranslations()

    tran.install(unicode=1)

    #initialize pygame and prepare screen
    if (gdata.config.defaults.sound == "yes") or (gdata.config.defaults.music
                                                  == "yes"):
        pygame.mixer.pre_init(44100, -16, 2, 4096)

    os.environ['SDL_VIDEO_ALLOW_SCREENSAVER'] = '1'
    os.environ['SDL_DEBUG'] = '1'
    pygame.init()

    flags = pygame.SWSURFACE

    DEFAULT_SCRN_SIZE = (800, 600)
    gdata.scrnSize = DEFAULT_SCRN_SIZE
    if gdata.config.display.resolution == "FULLSCREEN":
        gdata.scrnSize = (0, 0)
        flags |= pygame.FULLSCREEN
    elif gdata.config.display.resolution is not None:
        width, height = gdata.config.display.resolution.split('x')
        gdata.scrnSize = (int(width), int(height))

    if gdata.config.display.depth == None:
        # guess best depth
        bestdepth = pygame.display.mode_ok(gdata.scrnSize, flags)
    else:
        bestdepth = int(gdata.config.display.depth)

    # initialize screen
    try:
        screen = pygame.display.set_mode(gdata.scrnSize, flags, bestdepth)
        # gdata.scrnSize is used everywhere to setup windows
        gdata.scrnSize = screen.get_size()
    except pygame.error:
        # for example if fullscreen is selected with resolution bigger than display
        # TODO: as of now, fullscreen has automatic resolution
        gdata.scrnSize = DEFAULT_SCRN_SIZE
        screen = pygame.display.set_mode(gdata.scrnSize, flags, bestdepth)
    gdata.screen = screen
    log.debug('OSCI', 'Driver:', pygame.display.get_driver())
    log.debug('OSCI', 'Using depth:', bestdepth)
    log.debug('OSCI', 'Display info:', pygame.display.Info())

    pygame.mouse.set_visible(1)

    pygame.display.set_caption(_('Outer Space %s') % ige.version.versionString)

    # set icon
    pygame.display.set_icon(
        pygame.image.load(resources.get('icon48.png')).convert_alpha())

    # UI stuff
    if first:
        import pygameui as ui
    else:
        reload(ui)

    setSkinTheme(gdata, ui)

    app = ui.Application(update, theme=ui.SkinableTheme)
    app.background = defineBackground()
    app.draw(gdata.screen)
    app.windowSurfaceFlags = pygame.SWSURFACE | pygame.SRCALPHA
    gdata.app = app

    pygame.event.clear()

    # resources
    import osci.res

    osci.res.initialize()

    # load resources
    import osci.dialog
    dlg = osci.dialog.ProgressDlg(gdata.app)
    osci.res.loadResources(dlg)
    dlg.hide()
    osci.res.prepareUIIcons(ui.SkinableTheme.themeIcons)

    while running:
        if first:
            import osci.client, osci.handler
            from igeclient.IClient import IClientException
        else:
            reload(osci.client)
            reload(osci.handler)
        osci.client.initialize(gdata.config.game.server, osci.handler, options)

        # create initial dialogs
        if first:
            import osci.dialog
        else:
            reload(osci.dialog)
        gdata.savePassword = gdata.config.game.lastpasswordcrypted != None

        if options.login and options.password:
            gdata.config.game.lastlogin = options.login
            gdata.config.game.lastpassword = options.password
            gdata.config.game.lastpasswordcrypted = binascii.b2a_base64(
                options.password).strip()
            gdata.config.game.autologin = '******'
            gdata.savePassword = '******'

        loginDlg = osci.dialog.LoginDlg(gdata.app)
        updateDlg = osci.dialog.UpdateDlg(gdata.app)

        # event loop
        update()

        lastSave = time.clock()
        # set counter to -1 to trigger Update dialog (see "if" below)
        counter = -1
        needsRefresh = False
        session = 1
        first = False
        while running and session:
            try:
                counter += 1
                if counter == 0:
                    # display initial dialog in the very first cycle
                    updateDlg.display(caller=loginDlg, options=options)
                # process as many events as possible before updating
                evt = pygame.event.wait()
                evts = pygame.event.get()
                evts.insert(0, evt)

                forceKeepAlive = False
                saveDB = False

                for evt in evts:
                    if evt.type == pygame.QUIT:
                        running = 0
                        break
                    if evt.type == (
                            ui.USEREVENT) and evt.action == "localExit":
                        session = False
                        break
                    if evt.type == pygame.ACTIVEEVENT:
                        if evt.gain == 1 and evt.state == 6:
                            # pygame desktop window focus event
                            needsRefresh = True
                    if evt.type == pygame.KEYUP and evt.key == pygame.K_F12:
                        if not pygame.key.get_mods() & pygame.KMOD_CTRL:
                            running = 0
                            break
                    if evt.type == pygame.KEYUP and evt.key == pygame.K_F9:
                        forceKeepAlive = True
                    evt = gdata.app.processEvent(evt)

                if gdata.app.needsUpdate() or needsRefresh:
                    needsRefresh = False
                    update()
                # keep alive connection
                osci.client.keepAlive(forceKeepAlive)

                # save DB every 4 hours in case of a computer crash
                # using "counter" to limit calls to time.clock() to approximately every 10-15 minutes
                if counter > 5000:
                    # set this to zero so we don't display Update dialog
                    counter = 0
                    if time.clock() - lastSave > 14400:
                        saveDB = True
                if saveDB:
                    osci.client.saveDB()
                    lastSave = time.clock()

            except IClientException, e:
                osci.client.reinitialize()
                gdata.app.setStatus(e.args[0])
                loginDlg.display(message=e.args[0])
            except Exception, e:
                log.warning('OSCI', 'Exception in event loop')
                if not isinstance(e, SystemExit) and not isinstance(
                        e, KeyboardInterrupt):
                    log.debug("Processing exception")
                    # handle exception
                    import traceback, StringIO
                    fh = StringIO.StringIO()
                    exctype, value, tb = sys.exc_info()
                    funcs = [entry[2] for entry in traceback.extract_tb(tb)]
                    faultID = "%06d-%03d" % (
                        hash("/".join(funcs)) % 1000000,
                        traceback.extract_tb(tb)[-1][1] % 1000,
                    )
                    del tb
                    # high level info
                    print >> fh, "Exception ID:", faultID
                    print >> fh
                    print >> fh, "%s: %s" % (exctype, value)
                    print >> fh
                    print >> fh, "--- EXCEPTION DATA ---"
                    # dump exception
                    traceback.print_exc(file=fh)
                    excDlg = osci.dialog.ExceptionDlg(gdata.app)
                    excDlg.display(faultID, fh.getvalue())
                    del excDlg  # reference to the dialog holds app's intance
                    fh.close()
                    del fh
                else:
                    break
예제 #3
0

pygame.init()

screen = pygame.display.set_mode((800, 600), SWSURFACE, 32)
pygame.mouse.set_visible(1)
pygame.display.set_caption('PYGAME.UI 0.4 Test Client')

colorBlack = screen.map_rgb((0x00, 0x00, 0x00))
pygame.display.flip()

# create UI
import pygameui as ui

ui.SkinableTheme.setSkin("../OSSkin")
app = ui.Application(update, theme=ui.SkinableTheme)
app.windowSurfaceFlags = SWSURFACE


def echoHandler(widget, action, data):
    print 'ACTION', widget, action, data


popUpMenu = ui.Menu(
    app,
    title="Test Menu",
    items=[
        ui.Item("Test 1", action="MENU TEST", data=1),
        ui.Item("Test 2", action="MENU TEST", data=2),
    ],
)