[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [pygame] wx Menu



Great!  I've got some success melding SDL and wx.  I am now needing to add menus.  Here's the main code.

import wx
import thread
import os, sys
global pygame
from pygame.locals import *
from FileFunctions import *
##from Colors import *
##from Distort import *
from Window import *
MaxZoom = 33.0
Windows = []
SelectedWindow = 0
class SDLThread:
    def __init__(self,screen):
        self.m_bKeepGoing = self.m_bRunning = False
        self.screen = screen
        self.color = (255,0,0)
        self.rect = (10,10,100,100)
    def Start(self):
        self.m_bKeepGoing = self.m_bRunning = True
        thread.start_new_thread(self.Run, ())
    def Stop(self):
        self.m_bKeepGoing = False
    def IsRunning(self):
        return self.m_bRunning
    def Run(self):
        while self.m_bKeepGoing:
            GetInput()
            Draw()
        self.m_bRunning = False;
class SDLPanel(wx.Panel):
    def __init__(self,parent,ID,tplSize):
        global pygame
        wx.Panel.__init__(self, parent, ID, size=tplSize)
        self.Fit()
        os.environ['SDL_WINDOWID'] = str(self.GetHandle())
        os.environ['SDL_VIDEODRIVER'] = 'windib'
        import pygame
        pygame.init()
        icon = pygame.Surface((1,1));icon.set_alpha(0);pygame.display.set_icon(icon)
        global Surface
        Surface = pygame.display.set_mode(tplSize)
        WindowInit()
        self.thread = SDLThread(Surface)
        self.thread.Start()
    def __del__(self):
        self.thread.Stop()
class MyFrame(wx.Frame):
    def __init__(self, parent, ID, strTitle, tplSize):
        wx.Frame.__init__(self, parent, ID, strTitle, size=tplSize)
        self.pnlSDL = SDLPanel(self, -1, tplSize)
        #self.Fit()
def GetInput():
    global SelectedWindow
    keystate = pygame.key.get_pressed()
    mrel = pygame.mouse.get_rel()
    mpos = pygame.mouse.get_pos()
    mpress = pygame.mouse.get_pressed()
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit(); sys.exit()
        elif event.type == MOUSEBUTTONDOWN:
            if event.button == 4: #Scroll In
                window = Windows[SelectedWindow]
                if window.Zoom < MaxZoom:
                    window.Zoom += 1
                    window.zoom()
            elif event.button == 5: #Scroll Out
                window = Windows[SelectedWindow]
                if window.Zoom > -63:
                    window.Zoom -= 1
                    window.zoom()
    if keystate[K_LCTRL] or keystate[K_RCTRL]:
        if keystate[K_o] or keystate[K_l]:
            path = Open()
            if path != None:
                Image = pygame.image.load(path).convert_alpha()
                Windows.append(Window(Image))
                SelectedWindow = 0
        if keystate[K_s] and CurrentSurface != None:
            path = Save()
            if path != None:
                pygame.image.save(DrawSurfaces[CurrentSurface-1],path)
    if mpress[1]:
        window = Windows[SelectedWindow]
        window.Zoom += 0.1*mrel[1]
        if window.Zoom > MaxZoom: window.Zoom = MaxZoom
        if window.Zoom < -63    : window.Zoom = -63
        window.zoom()
    for window in Windows:
        #Buttons
        NotSelected = True
        if mpos[1] >= 10+window.DrawPosition[1]:
            if mpos[1] <= 25+window.DrawPosition[1]:
                if mpos[0] >= (window.WindowSize[0]-18)+window.DrawPosition[0]:
                    if mpos[0] <= (window.WindowSize[0]+10)+window.DrawPosition[0]:
                        if mpress[0]:Windows.remove(Windows[SelectedWindow]);continue
                        window.select("X2")
                        NotSelected = False
        if NotSelected:window.select(None)
        #Move Window
        if mpress[0]:
            if mpos[0] >= 0+window.DrawPosition[0]:
                if mpos[0] <= window.WindowSize[0]+window.DrawPosition[0]:
                    if mpos[1] >= 0+window.DrawPosition[1]:
                        if mpos[1] <= 30+window.DrawPosition[1]:
                            window.DrawPosition[0] += mrel[0]
                            window.DrawPosition[1] += mrel[1]
def Draw():
    Surface.fill((171,171,171))
    for window in Windows:
        window.draw(Surface)
    pygame.display.flip()
if __name__ == '__main__':
    app = wx.PySimpleApp()
    Title = "The Programmer's Paint - v.6.0.0 - Ian Mallett - 2008"
    frame = MyFrame(None, wx.ID_ANY, Title, (800,600))
    frame.Show()
    app.MainLoop()

How do I add menus to this?  Thanks again!
Ian