comparison orpg/map/_fog.py @ 0:4385a7d0efd1 grumpy-goblin

Deleted and repushed it with the 'grumpy-goblin' branch. I forgot a y
author sirebral
date Tue, 14 Jul 2009 16:41:58 -0500
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:4385a7d0efd1
1 from math import sqrt
2
3 import wx
4
5 import orpg.dirpath
6 from orpg.orpgCore import *
7
8 from _object import MapObject
9
10 class FogLayer(MapObject):
11 def __init__(self, canvas):
12 MapObject.__init__(self, canvas=canvas)
13
14 def Draw(self, dc):
15 path = dc.CreatePath()
16 r, g, b = self.RGBHex.rgb_tuple(self.canvas.fogColor)
17 if self.canvas.toolWnd.gmToolBar.IsShown():
18 brush = wx.Brush(wx.Color(r, g, b, 128))
19 else:
20 brush = wx.Brush(wx.Color(r, g, b, 255))
21 dc.SetBrush(brush)
22
23 self.region = wx.Region(0, 0, self.canvas.size[0]+2, self.canvas.size[1]+2)
24
25 points = []
26 lp = 's'
27 for point in self.canvas.fogRegion:
28 if point == 's' or point == 'h':
29 if lp == 's' and len(points) > 0:
30 self.region.XorRegion(wx.RegionFromPoints(points))
31 self.region.SubtractRegion(wx.RegionFromPoints(points))
32 elif len(points) > 0:
33 self.region.UnionRegion(wx.RegionFromPoints(points))
34 lp = point
35 points = []
36 else:
37 points.append((point.x, point.y))
38
39 if len(points) > 0:
40 if lp == 's':
41 self.region.XorRegion(wx.RegionFromPoints(points))
42 self.region.SubtractRegion(wx.RegionFromPoints(points))
43 else:
44 self.region.UnionRegion(wx.RegionFromPoints(points))
45
46 dc.ClipRegion(self.region)
47
48 dc.DrawRectangle(0, 0, self.canvas.size[0]+2, self.canvas.size[1]+2)
49
50 dc.SetBrush(wx.NullBrush)
51
52 def OnLeftDown(self, pos):
53 self.start = pos
54 self.lastPoint = pos
55 if self.canvas.toolWnd.currentFog == 'Show':
56 self.canvas.fogRegion.append('s')
57 else:
58 self.canvas.fogRegion.append('h')
59 self.canvas.fogRegion.append(pos)
60
61 def OnMotion(self, pos):
62 cdc = wx.ClientDC(self.canvas)
63 self.canvas.PrepareDC(cdc)
64
65 dc = wx.GraphicsContext.Create(cdc)
66 dc.Scale(self.canvas.zoomScale, self.canvas.zoomScale)
67
68 dc.SetPen(wx.WHITE_PEN)
69
70 path = dc.CreatePath()
71
72 xd = (self.lastPoint.x-pos.x)*(self.lastPoint.x-pos.x)
73 yd = (self.lastPoint.y-pos.y)*(self.lastPoint.y-pos.y)
74 distance = sqrt(xd+yd)
75
76 if distance > 5:
77 path.MoveToPoint(self.lastPoint.x, self.lastPoint.y)
78 path.AddLineToPoint(pos.x, pos.y)
79
80 self.canvas.fogRegion.append(pos)
81 self.lastPoint = pos
82
83 path.CloseSubpath()
84 dc.StrokePath(path)
85
86 dc.SetPen(wx.NullPen)
87
88 def OnLeftUp(self, pos):
89 self.canvas.fogRegion.append(pos)
90 self.canvas.fogRegion.append(self.start)
91 self.canvas.UpdateMap()
92