Creating custom controls

Started by Ryex, October 16, 2011, 03:06:43 am

Previous topic - Next topic

Ryex

there are a multitude of custom controls that will need to be created. for example the bar graph boxes in the actors panels

these custom controls are actually pretty easy to make. I'll keep this topic as a running tutorial on how to make them as I work on the editor so that your guys will know what to do latter.

The first step to making a control is to start off with a base. wx.Panel makes a good base as it has all th basics built in but provides us with a clean slate on which to draw

class MyCtrl(wx.Panel):

    def __init__(parent, id, pos, size, style):
        super(MyCtrl, self).__init__(parent, id, pos, size, style)

        self.Bind(wx.EVT_SIZE, self.processSizeEvent)
        self.Bind(wx.EVT_PAINT, self.processPaintEvent)

    def processSizeEvent(self, event):
        '''Process the resize event.'''
        self.Refresh()

    def processPaintEvent(self, event):
        '''Process the drawing event.'''
        dc = wx.PaintDC(self)
        self.OnDraw(dc)
        event.Skip()

    def OnDraw(self, dc):
        #set the background color
        dc.SetBackground(wx.Brush("WHITE"))
        #clear the dc to the background color
        dc.Clear()
        #draw here

    def Destroy(self):
        '''cleanup method, make sure to clean up before the panel is destroyed'''
        #clean up code should go above the super method
        super(wx.Panel, self).Destroy()



this code gets us started, it gives us a empty panel with which to work. Note that I added several methods

the Destroy method is already defined but we are redefining it and calling the method from the super class so we can inject some clean up code if we need to.
the processSizeEvent and processPaintEvent methods are bound to their respective wxEvents so that we know when the ctrl has been re-sized or if we need to refresh the screen
the processPaintEvent created a dc or Devise Context for the ctrl on which we can draw. once we have this dc we can treat it like the screen in RGSS and draw what ever we want to it. when the DC object is deposed (either because it's name space expired and it is left reference-less and thus deleted or because we manually deleted it) the window will update with the new content
there are many different types of DC's. look at the wxPython docs for more information for now. I'll give specific instruction on which to use when latter
also look at the docs for information on the methods that the dc classes have for drawing. you will find mote of them under the documentation for the wx.DC class.
I no longer keep up with posts in the forum very well. If you have a question or comment, about my work, or in general I welcome PM's. if you make a post in one of my threads and I don't reply with in a day or two feel free to PM me and point it out to me.<br /><br />DropBox, the best free file syncing service there is.<br />