1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
|
#!/usr/bin/python
# -*- coding: iso-8859-15 -*-
import wx
import random
class MyCanvas(wx.Frame):
def __init__(self, titre):
wx.Frame.__init__(self, parent = None, id = 1, title = titre, size = (500, 500))
self.conteneur = wx.Panel(self, 1, size = self.GetClientSize())
# Initialise les diff?rents Sizer
vbox = wx.BoxSizer(wx.VERTICAL)
# Create vertical box sizer : bordure
self.bordure1 = wx.BoxSizer(wx.VERTICAL)
# Cr?ation du Widget wxStaticBox : 'Terrain de jeu' avec son Sizer
self.staticBox1 = wx.StaticBox ( self.conteneur, -1, 'Terrain de jeu' )
self.staticBoxSizer1 = wx.StaticBoxSizer ( box = self.staticBox1, orient=wx.VERTICAL )
# ?cran "Map_Terrain"
self.sizer_Map = wx.BoxSizer(orient=wx.VERTICAL)
# Cr?ation du panel "P_Map"
self.P_Map = Interface_Map(self.conteneur)
self.sizer_Map.AddSizer(self.P_Map, 1, border=0, flag=wx.EXPAND)
self.staticBoxSizer1.AddSizer(self.sizer_Map, 1, border=0, flag=wx.EXPAND)
#-----------------------------------------------------------
# Configure the box sizers
self.bordure1.Add(self.staticBoxSizer1, 1, wx.EXPAND | wx.ALL, 10)
# Attach everything
self.conteneur.SetSizerAndFit (self.bordure1)
vbox.Add(self.conteneur, 1, wx.EXPAND)
# Rendu final du sizer
self.SetSizer(vbox)
#-------------------------------------------------------------------------
class Interface_Map(wx.ScrolledWindow):
"""Cr?ation de l'interface "Map Terrain"""
#-------------------------------------------------------------------------
def __init__(self, conteneur):
wx.ScrolledWindow.__init__(self, conteneur)
# Map "Terrain de jeu" :
self.SetBackgroundColour(wx.Colour(255, 141, 14)) # Couleur 'Orange'
self.Rouge = 1
self.Vert = 2
# create a PseudoDC to record our drawing
self.pdc = wx.PseudoDC()
self.DoDrawing(self.pdc)
# Evenements
self.Bind(wx.EVT_PAINT, self.OnPaint)
self.Bind(wx.EVT_ERASE_BACKGROUND, lambda x:None)
self.Bind(wx.EVT_MOUSE_EVENTS, self.OnMouseMove)
# vars for handling mouse clicks
self.dragid = -1
self.lastpos = (0,0)
#*********************************************************
def DoDrawing(self, dc):
self.OnAddPions(self.Rouge, dc)
self.OnAddPions(self.Vert, dc)
#-----------------------------------------------------------
#---------------------------------------------------------
def OnPaint(self, event):
# Create a buffered paint DC. It will create the real
# wx.PaintDC and then blit the bitmap to it when dc is
# deleted.
dc = wx.BufferedPaintDC(self)
# use PrepateDC to set position correctly
self.PrepareDC(dc)
# we need to clear the dc BEFORE calling PrepareDC
bg = wx.Brush(self.GetBackgroundColour())
dc.SetBackground(bg)
dc.Clear()
self.pdc.DrawToDC(dc)
#---------------------------------------------------------
def OnMouseMove(self, event):
hitradius = 5
if event.LeftDown():
x,y = event.GetPosition()
l = self.pdc.FindObjects(x, y, hitradius)
for id in l:
if not self.pdc.GetIdGreyedOut(id):
self.dragid = id
self.lastpos = (event.GetX(),event.GetY())
break
elif event.RightDown():
x,y = event.GetPosition()
l = self.pdc.FindObjects(x, y, hitradius)
if l:
self.pdc.SetIdGreyedOut(l[0], not self.pdc.GetIdGreyedOut(l[0]))
r = self.pdc.GetIdBounds(l[0])
r.Inflate(4,4)
self.OffsetRect(r)
self.RefreshRect(r, False)
elif event.Dragging() or event.LeftUp():
if self.dragid != -1:
x,y = self.lastpos
dx = event.GetX() - x
dy = event.GetY() - y
r = self.pdc.GetIdBounds(self.dragid)
self.pdc.TranslateId(self.dragid, dx, dy)
r2 = self.pdc.GetIdBounds(self.dragid)
r = r.Union(r2)
r.Inflate(4,4)
self.OffsetRect(r)
self.RefreshRect(r, False)
self.lastpos = (event.GetX(),event.GetY())
if event.LeftUp():
self.dragid = -1
#---------------------------------------------------------
def OffsetRect(self, r):
xView, yView = self.GetViewStart()
xDelta, yDelta = self.GetScrollPixelsPerUnit()
r.OffsetXY(-(xView*xDelta),-(yView*yDelta))
#---------------------------------------------------------
def OnAddPions(self, couleur, dc):
radius = 25
self.pDC = dc
self.pDC.SetPen(wx.Pen('#d4d4d4'))
if couleur == self.Rouge :
# Dessine les Pions Rouges
self.pDC.SetBrush(wx.Brush('#c50024')) # Couleur 'Rouge'
else :
# Dessine les Pions verts
self.pDC.SetBrush(wx.Brush('#1ac500')) # Couleur 'Verte'
self.pDC.BeginDrawing()
for i in range(1,4):
id = wx.NewId()
self.pDC.SetId(id)
x = random.randint(50, 200)
y = random.randint(50, 200)
x1 = x - radius
y1 = y - radius
x2 = 2 * radius
y2 = 2 * radius
r = wx.Rect(x1,y1,x2,y2)
self.pDC.DrawCircle(x,y,radius)
self.pDC.SetIdBounds(id,r)
self.pDC.EndDrawing()
return self.pDC
#---------------------------------------------------------
class MonApp(wx.App):
def OnInit(self):
fen = MyCanvas("Somme de 2 PseudoDC")
fen.Show(True)
self.SetTopWindow(fen)
return True
app = MonApp()
app.MainLoop() |
Partager