pykiss

Python Kisekae set viewer
git clone https://code.literati.org/pykiss.git
Log | Files | Refs | LICENSE

wxkiss.py (4472B)


      1 #!/usr/bin/python
      2 # Copyright (C) 2003 Sean R. Lynch <seanl@chaosring.org>
      3 #
      4 # This program is free software; you can redistribute it and/or modify
      5 # it under the terms of the GNU General Public License as published by
      6 # the Free Software Foundation; either version 2 of the License, or
      7 # (at your option) any later version.
      8 #
      9 # This program is distributed in the hope that it will be useful,
     10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
     11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     12 # GNU General Public License for more details.
     13 #
     14 # You should have received a copy of the GNU General Public License
     15 # along with this program; if not, write to the Free Software
     16 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
     17 
     18 import os
     19 from   wxPython.wx import *
     20 from wxPython.xrc import *
     21 from PIL import Image
     22 import kiss
     23 
     24 ID_EXIT = 110
     25 
     26 class App(wxApp):
     27     def OnInit(self):
     28 	self.res = wxXmlResource('wxkiss.xrc')
     29 	self.InitFrame()
     30 	self.InitMenu()
     31         self.InitKiss()
     32 	return True
     33     
     34     def InitFrame(self):
     35 	self.frame = self.res.LoadFrame(None, "MainFrame")
     36         self.frame.Show(1)
     37     
     38     def InitMenu(self):
     39 	self.menuBar = self.res.LoadMenuBar("MenuBar")
     40 	EVT_MENU(self.frame, XRCID("OpenItem"), self.OnOpen)
     41 	EVT_MENU(self.frame, XRCID("ExitItem"), self.OnExit)
     42         EVT_CLOSE(self.frame, self.OnExit)
     43 	self.frame.SetMenuBar(self.menuBar)
     44 
     45     def InitKiss(self): 
     46         self.kissWindow = KissWindow(self.frame)
     47 	
     48     def OnExit(self, event):
     49 	sys.exit()
     50 	
     51     def OnOpen(self, event):
     52 	dialog = wxFileDialog(None, wildcard="*.cnf", style=wxOPEN)
     53 	result = dialog.ShowModal()
     54 	if result == wxID_OK:
     55 	    cnf = os.path.join(dialog.GetDirectory(), dialog.GetFilename())
     56             self.kissWindow.set_kiss(cnf)
     57 
     58 class Cel(kiss.Cel):
     59     def convert(self):
     60         import tempfile
     61 
     62         print 'Converting', self.name
     63         print self.bpp, self.width, self.height, len(self.data)
     64         if self.bpp == 4:
     65             im = Image.fromstring("F", (self.width, self.height), self.data,
     66                                   "bit", 4, 8, 1).convert("RGBA")
     67         elif self.bpp == 8:
     68             im = Image.fromstring("L", (self.width, self.height), self.data).convert("RGBA")
     69 	elif self.bpp == 32: 
     70             im = Image.fromstring("RGBA", (self.width, self.height), self.data)
     71             r, g, b, a = im.split()
     72             im = Image.merge("RGBA", (b, g, r, a))
     73         else: raise ValueError, "Unimplemented bpp %d" % self.bpp
     74             
     75         filename = tempfile.mktemp('.png')
     76         im.save(filename)
     77         self.bitmap = wxBitmap(filename, wxBITMAP_TYPE_PNG)
     78         #os.unlink(filename)
     79 	
     80 	
     81 class KissWindow(wxScrolledWindow):
     82     def __init__(self, *args):
     83         wxScrolledWindow.__init__(self, *args)
     84         EVT_TIMER(self, -1, self.OnTimer)
     85 
     86     def OnPaint(self, event):
     87         dc = wxPaintDC(self)
     88         upd = wxRegionIterator(self.GetUpdateRegion())
     89         self.PrepareDC(dc)
     90         dc1 = wxMemoryDC()
     91         dc1.SelectObject(self.bitmap)
     92         while upd.HaveRects():
     93             x = upd.GetX()
     94             y = upd.GetY()
     95             dc.Blit(x, y, upd.GetW(), upd.GetH(), dc1, x, y)
     96             upd.Next()
     97 
     98     def OnTimer(self, event):
     99         self.kiss.pulse()
    100         
    101 ##    def OnDraw(self, dc):
    102 ##        if not hasattr(self, 'kiss'): return True
    103 ##        self.dc = dc
    104 ##        set = 0
    105 ##        self.kiss.draw(self, set)
    106 ##        return True
    107 
    108     def set_kiss(self, cnf):
    109 	self.kiss = kiss.KiSS(cnf, canvas=self, cel_class=Cel)
    110         self.bitmap = wxEmptyBitmap(self.kiss.width, self.kiss.height)
    111         #self.dc = wxMemoryDC()
    112         #self.dc.SelectObject(self.bitmap)
    113         self.SetScrollbars(20, 20, self.kiss.width/20, self.kiss.height/20)
    114         self.SetBackgroundColour(wxColour(0, 0, 0))
    115         self.kiss.run()
    116         self.timer = wxTimer(self)
    117         self.timer.Start(10)
    118         self.redraw()
    119         EVT_PAINT(self, self.OnPaint)
    120 
    121     # Functions called by KiSS class
    122     def draw(self, cel, (x, y)):
    123         print cel.width, cel.height
    124         dc = wxMemoryDC()
    125         dc.SelectObject(self.bitmap)
    126         dc.DrawBitmap(cel.bitmap, x, y, True)
    127         self.Refresh(eraseBackground=False,
    128                      rect=wxRect(x, y, cel.width, cel.height))
    129         
    130 
    131     def redraw(self):
    132         self.kiss.draw(self, 0)
    133         
    134     	    
    135 if __name__ == '__main__':
    136     wxInitAllImageHandlers()
    137     app = App()
    138     app.MainLoop()
    139 
    140 
    141 
    142 
    143