All pastes #796235 Raw Edit

Mine

public text v1 · immutable
#796235 ·published 2007-11-24 21:55 UTC
rendered paste body
import wx

colour_codes=[(192, 192, 192), (0, 0, 0), (255, 0, 0), (0, 255, 0), (0, 0, 255), (255, 255, 0), (255, 0, 255), (0, 255, 255), (64, 64, 64), (128, 128, 128), (192, 192, 192), (255, 255, 255), (255, 255, 255), (204, 255, 255), (153, 255, 255), (102, 255, 255), (51, 255, 255), (0, 255, 255), (0, 255, 204), (51, 255, 204), (102, 255, 204), (153, 255, 204), (204, 255, 204), (255, 255, 204), (255, 204, 255), (204, 204, 255), (153, 204, 255), (102, 204, 255), (51, 204, 255), (0, 204, 255), (0, 204, 204), (51, 204, 204), (102, 204, 204), (153, 204, 204), (204, 204, 204), (255, 204, 204), (255, 153, 255), (204, 153, 255), (153, 153, 255), (102, 153, 255), (51, 153, 255), (0, 153, 255), (0, 153, 204), (51, 153, 204), (102, 153, 204), (153, 153, 204), (204, 153, 204), (255, 153, 204), (255, 102, 255), (204, 102, 255), (153, 102, 255), (102, 102, 255), (51, 102, 255), (0, 102, 255), (0, 102, 204), (51, 102, 204), (102, 102, 204), (153, 102, 204), (204, 102, 204), (255, 102, 204), (255, 51, 255), (204, 51, 255), (153, 51, 255), (102, 51, 255), (51, 51, 255), (0, 51, 255), (0, 51, 204), (51, 51, 204), (102, 51, 204), (153, 51, 204), (204, 51, 204), (255, 51, 204), (255, 0, 255), (204, 0, 255), (153, 0, 255), (102, 0, 255), (51, 0, 255), (0, 0, 255), (0, 0, 204), (51, 0, 204), (102, 0, 204), (153, 0, 204), (204, 0, 204), (255, 0, 204), (255, 0, 153), (204, 0, 153), (153, 0, 153), (102, 0, 153), (51, 0, 153), (0, 0, 153), (0, 0, 102), (51, 0, 102), (102, 0, 102), (153, 0, 102), (204, 0, 102), (255, 0, 102), (255, 51, 153), (204, 51, 153), (153, 51, 153), (102, 51, 153), (51, 51, 153), (0, 51, 153), (0, 51, 102), (51, 51, 102), (102, 51, 102), (153, 51, 102), (204, 51, 102), (255, 51, 102), (255, 102, 153), (204, 102, 153), (153, 102, 153), (102, 102, 153), (51, 102, 153), (0, 102, 153), (0, 102, 102), (51, 102, 102), (102, 102, 102), (153, 102, 102), (204, 102, 102), (255, 102, 102), (255, 153, 153), (204, 153, 153), (153, 153, 153), (102, 153, 153), (51, 153, 153), (0, 153, 153), (0, 153, 102), (51, 153, 102), (102, 153, 102), (153, 153, 102), (204, 153, 102), (255, 153, 102), (255, 204, 153), (204, 204, 153), (153, 204, 153), (102, 204, 153), (51, 204, 153), (0, 204, 153), (0, 204, 102), (51, 204, 102), (102, 204, 102), (153, 204, 102), (204, 204, 102), (255, 204, 102), (255, 255, 153), (204, 255, 153), (153, 255, 153), (102, 255, 153), (51, 255, 153), (0, 255, 153), (0, 255, 102), (51, 255, 102), (102, 255, 102), (153, 255, 102), (204, 255, 102), (255, 255, 102), (255, 255, 51), (204, 255, 51), (153, 255, 51), (102, 255, 51), (51, 255, 51), (0, 255, 51), (0, 255, 0), (51, 255, 0), (102, 255, 0), (153, 255, 0), (204, 255, 0), (255, 255, 0), (255, 204, 51), (204, 204, 51), (153, 204, 51), (102, 204, 51), (51, 204, 51), (0, 204, 51), (0, 204, 0), (51, 204, 0), (102, 204, 0), (153, 204, 0), (204, 204, 0), (255, 204, 0), (255, 153, 51), (204, 153, 51), (153, 153, 51), (102, 153, 51), (51, 153, 51), (0, 153, 51), (0, 153, 0), (51, 153, 0), (102, 153, 0), (153, 153, 0), (204, 153, 0), (255, 153, 0), (255, 102, 51), (204, 102, 51), (153, 102, 51), (102, 102, 51), (51, 102, 51), (0, 102, 51), (0, 102, 0), (51, 102, 0), (102, 102, 0), (153, 102, 0), (204, 102, 0), (255, 102, 0), (255, 51, 51), (204, 51, 51), (153, 51, 51), (102, 51, 51), (51, 51, 51), (0, 51, 51), (0, 51, 0), (51, 51, 0), (102, 51, 0), (153, 51, 0), (204, 51, 0), (255, 51, 0), (255, 0, 51), (204, 0, 51), (153, 0, 51), (102, 0, 51), (51, 0, 51), (0, 0, 51), (0, 0, 0), (51, 0, 0), (102, 0, 0), (153, 0, 0), (204, 0, 0), (255, 0, 0), (0, 0, 0), (7, 7, 7), (14, 14, 14), (21, 21, 21), (28, 28, 28), (35, 35, 35), (42, 42, 42), (49, 49, 49), (56, 56, 56), (63, 63, 63), (70, 70, 70), (77, 77, 77), (84, 84, 84), (91, 91, 91), (98, 98, 98), (105, 105, 105), (112, 112, 112), (119, 119, 119), (126, 126, 126), (133, 133, 133), (140, 140, 140), (147, 147, 147), (154, 154, 154), (161, 161, 161), (168, 168, 168), (175, 175, 175), (182, 182, 182), (189, 189, 189), (196, 196, 196), (203, 203, 203), (210, 210, 210), (217, 217, 217), (224, 224, 224), (231, 231, 231), (238, 238, 238), (245, 245, 245)]

class PaintPage(wx.ScrolledWindow):
	def __init__(self, parent):
		wx.ScrolledWindow.__init__(self, parent, -1)
		self.SetScrollRate(20, 20)

		self.control_panel=ControlPanel(self)
		self.paint_canvas=PaintCanvas(self)

		sizer=wx.BoxSizer(wx.HORIZONTAL)
		sizer.Add(self.control_panel, 0, wx.EXPAND)
		sizer.Add(self.paint_canvas, 1, wx.EXPAND)
		self.SetSizer(sizer)


class PaintCanvas(wx.Panel):
	def __init__(self, parent):
		wx.Panel.__init__(self, parent, -1)

		self.buffer=wx.EmptyBitmap(256, 256)

		self.x=0
		self.y=0

		self.line_width=1

		self.Bind(wx.EVT_LEFT_DOWN, self.OnLeftMouseDown)
		self.Bind(wx.EVT_LEFT_UP, self.OnLeftMouseUp)
		self.Bind(wx.EVT_MOTION, self.OnMouseMotion)
		self.Bind(wx.EVT_PAINT, self.OnPaint)

	def OnLeftMouseDown(self, event):
		self.CaptureMouse()
		self.SetXY(event)
		self.OnMouseMotion(event, True)

	def OnLeftMouseUp(self, event):
		self.ReleaseMouse()
		self.SetXY(event)

	def OnMouseMotion(self, event, bypass=False):
		if event.Dragging() or bypass:
			dc=wx.BufferedDC(wx.ClientDC(self), self.buffer)
			dc.BeginDrawing()
			dc.SetPen(wx.Pen(wx.Colour(*self.GetParent().control_panel.GetColour()), self.line_width))
			dc.DrawLine(self.x, self.y, event.GetX(), event.GetY())
			dc.EndDrawing()
			self.SetXY(event)

	def OnPaint(self, event):
		dc=wx.BufferedPaintDC(self, self.buffer)

	def SetXY(self, event):
		self.x=event.GetX()
		self.y=event.GetY()


class ControlPanel(wx.Panel):
	def __init__(self, parent):
		wx.Panel.__init__(self, parent, -1, style=wx.RAISED_BORDER)

		self.selected_colour_pos=(0, 0)
		self.selected_colour=0

		self.width_slider=wx.Slider(self, -1, 1, 1, 50, size=(12 * 10, -1), style=wx.SL_HORIZONTAL|wx.SL_AUTOTICKS|wx.SL_LABELS)
		self.width_slider.SetTickFreq(10, 1)

		sizer=wx.BoxSizer(wx.VERTICAL)
		sizer.AddSpacer(12 * 10 + 2)
		sizer.AddSpacer(12 * 10 - 12)
		sizer.Add(self.width_slider, 0)
		self.SetSizer(sizer)

		self.Bind(wx.EVT_SCROLL, self.OnSetWidth, self.width_slider)
		self.buffer=wx.EmptyBitmap(12 * 10, 22 * 10)
		self.DrawPalette()

		self.Bind(wx.EVT_PAINT, self.OnPaint)
		self.Bind(wx.EVT_LEFT_DOWN, self.OnLeftDown)

	def OnLeftDown(self, event):
		x,y=event.GetX(),event.GetY()
		x=x/10*10
		y=y/10*10
		if x < 12 * 10 and y < 22 * 10:
			self.selected_colour_pos=(x, y)
			x/=10
			y/=10
			self.selected_colour=x + (y * 12)
			self.Refresh(False)

	def DrawPalette(self):
		dc=wx.BufferedDC(wx.ClientDC(self), self.buffer)
		x_mult=0
		y_mult=0
		for colour in colour_codes:
				dc.SetBrush(wx.Brush(wx.Colour(*colour)))
				dc.SetPen(wx.TRANSPARENT_PEN)
				dc.DrawRectangle(x_mult * 10, y_mult * 10, 10, 10)
				x_mult+=1
				if x_mult>=12:
					x_mult-=12
					y_mult+=1

	def GetColour(self):
		return colour_codes[self.selected_colour]

	def OnPaint(self, evt):
			o=self.buffer.ConvertToImage().ConvertToBitmap()
			dc=wx.BufferedPaintDC(self, o)
			dc.SetBrush(wx.TRANSPARENT_BRUSH)
			dc.SetPen(wx.Pen('WHITE', 2))
			dc.DrawRectangle(self.selected_colour_pos[0], self.selected_colour_pos[1], 10, 10)

	def BuildTools(self):
		tools_grid=wx.GridSizer(1, 2, 2)
		tools_grid.Add(self.CreateToolButton('Brush'), 0)
		return tools_grid

	def CreateToolButton(self, text):
		b=buttons.GenToggleButton(self, -1, text, size=(32, 32))
		b.SetBezelWidth(1)
		b.SetUseFocusIndicator(False)
		return b

	def OnSetWidth(self, event):
		self.GetParent().paint_canvas.line_width=event.GetPosition()