-
Notifications
You must be signed in to change notification settings - Fork 4
/
photoshop.py
200 lines (167 loc) · 7.89 KB
/
photoshop.py
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
import gtk
import Image
from error import ErrorMessage
import StringIO
import logging
def Image_to_GdkPixbuf (image):
file = StringIO.StringIO ()
if image.mode != "RGBA":
image = image.convert("RGBA")
image.save (file, 'png')
contents = file.getvalue()
file.close ()
loader = gtk.gdk.PixbufLoader ('png')
loader.write (contents, len (contents))
pixbuf = loader.get_pixbuf()
loader.close ()
return pixbuf
class Photoshop(gtk.ScrolledWindow):
"""
Contained by : KSEStatusView
"""
def __init__(self, status):
gtk.ScrolledWindow.__init__(self)
self.logger = logging.getLogger("KRFEditor")
self.status = status
self.table = gtk.Table()
self.add_with_viewport(self.table)
eventBox = gtk.EventBox()
self.drawingArea = gtk.DrawingArea()
self.drawingArea.connect("expose-event", self._exposeEventCb)
self.drawingArea.set_flags(gtk.CAN_FOCUS)
eventBox.add(self.drawingArea)
self.table.attach(eventBox, 1, 2, 1, 2, gtk.FILL, gtk.FILL)
self.hruler = gtk.HRuler()
self.vruler = gtk.VRuler()
self.table.attach(self.hruler, 1, 2, 0, 1, gtk.EXPAND | gtk.FILL, gtk.FILL)
self.table.attach(self.vruler, 0, 1, 1, 2, gtk.FILL, gtk.EXPAND | gtk.FILL)
self.drawingArea.connect_object("motion-notify-event", self._motionNotifyCb, self.hruler)
self.drawingArea.connect_object("motion-notify-event", self._motionNotifyCb, self.vruler)
self.eventBox = eventBox
self.pixbuf = None
self.gc = None
self.highlightedSprites = None
self.highlightedAnimations = None
self.allHighlighted = False
self.zoomRatio = 1.0
self.modShift = False
self.modCtrl = False
self.currentSelection = None
self.pixmap = None
self.show_all()
def displayImage(self, drawable):
self.drawingArea.set_size_request(drawable.size[0], drawable.size[1])
pixbuf = Image_to_GdkPixbuf(drawable)
self.pixbuf = pixbuf
self.originalPixbuf = self.pixbuf
self.drawable = drawable
self._drawImage()
def highlight(self, sprite, selected):
if sprite == None:
return
if selected and not sprite.isAnim:
color = self.gc.get_colormap().alloc('blue')
elif selected:
color = self.gc.get_colormap().alloc('red')
else:
color = self.gc.get_colormap().alloc('LimeGreen')
self.gc.set_foreground(color)
if not sprite.isAnim:
self.drawingArea.window.draw_rectangle(self.gc, False, int(sprite.texturex * self.zoomRatio),
int(sprite.texturey * self.zoomRatio),
int(sprite.texturew * self.zoomRatio),
int(sprite.textureh * self.zoomRatio))
elif sprite != None:
self.drawingArea.window.draw_rectangle(self.gc, False, int(sprite.texturex * self.zoomRatio),
int(sprite.texturey * self.zoomRatio),
int(sprite.texturew * sprite.tilelen * self.zoomRatio),
int(sprite.textureh * self.zoomRatio))
color = self.gc.get_colormap().alloc('black')
self.gc.set_foreground(color)
def highlightAll(self, atlas):
self._drawImage()
self.highlightedSprites = atlas.sprites
self.highlightedAnimations = atlas.animations
self.allHighlighted = True
for elem in atlas.sprites:
self.highlight(elem, False)
for elem in atlas.animations:
self.highlight(elem, False)
if self.currentSelection:
for elem in self.currentSelection:
self.highlight(elem, True)
def zoomIn(self):
self.zoomRatio += 0.5
self.pixbuf = self.originalPixbuf.scale_simple(int(self.originalPixbuf.props.width * self.zoomRatio),
int(self.originalPixbuf.props.height * self.zoomRatio),
gtk.gdk.INTERP_HYPER)
self._drawImage()
def zoomOut(self):
self._cleanDrawingArea()
self.zoomRatio *= 0.5
self.pixbuf = self.originalPixbuf.scale_simple(int(self.originalPixbuf.props.width * self.zoomRatio),
int(self.originalPixbuf.props.height * self.zoomRatio),
gtk.gdk.INTERP_HYPER)
self._drawImage()
def zoom100(self):
self._cleanDrawingArea()
self.zoomRatio = 1.0
self.pixbuf = self.originalPixbuf
self._drawImage()
#INTERNAL
def _cleanDrawingArea(self):
color = self.gc.get_colormap().alloc('white')
self.gc.set_foreground(color)
self.drawingArea.window.draw_rectangle(self.gc, True, 0, 0, self.drawingArea.get_allocation().width, self.drawingArea.get_allocation().height)
color = self.gc.get_colormap().alloc('black')
self.gc.set_foreground(color)
def _drawImage(self):
imgw = self.pixbuf.get_width()
imgh = self.pixbuf.get_height()
self.pixmap = gtk.gdk.Pixmap(self.window, imgw, imgh)
self.ctx = self.drawingArea.window.cairo_create()
self.ct = gtk.gdk.CairoContext(self.ctx)
self.pixmap.draw_rectangle(self.gc, True, 0, 0, imgw, imgh)
self.drawingArea.set_size_request(int(self.zoomRatio * self.drawable.size[0]), int(self.zoomRatio * self.drawable.size[1]))
self.pixmap.draw_pixbuf(self.gc, self.pixbuf, 0, 0, 0, 0)
self.ct.set_source_pixmap(self.pixmap,0,0)
self.ctx.paint()
self.ctx.stroke()
xsize = self.drawable.size[0] if self.drawable.size[0] > self.drawingArea.get_allocation().width else self.drawingArea.get_allocation().width
ysize = self.drawable.size[1] if self.drawable.size[1] > self.drawingArea.get_allocation().height else self.drawingArea.get_allocation().height
self.hruler.set_range(0.0, float(xsize / self.zoomRatio), 0.0, float(self.drawable.size[0] / self.zoomRatio))
self.vruler.set_range(0.0, float(ysize / self.zoomRatio), 0.0, float(self.drawable.size[1] / self.zoomRatio))
def _exposeEventCb(self, widget, event):
#First, draw the atlas itself
if self.gc == None:
self.gc = self.get_style().fg_gc[gtk.STATE_NORMAL]
self.color = self.gc.get_colormap().alloc('LimeGreen')
self.gc.set_fill(gtk.gdk.SOLID)
self.gc.set_line_attributes(3, 0, gtk.gdk.CAP_NOT_LAST, gtk.gdk.JOIN_ROUND)
if self.pixbuf == None:
return
self._drawImage()
#Then, redraw highlighted sprites
if self.allHighlighted:
self.highlightAll(self.status.currentAtlas)
elif self.currentSelection:
for elem in self.currentSelection:
self.highlight(elem, True)
def _motionNotifyCb(self, ruler, event):
coords = event.get_coords()
self.status.posLabel.set_text(str(coords[0] / self.zoomRatio) + "," + str(coords[1] / self.zoomRatio))
ruler.emit("motion-notify-event", event)
def _atlasSelectionChangedCb(self, selection):
if not self.allHighlighted:
self._drawImage()
elif self.currentSelection is not None:
for elem in self.currentSelection:
self.highlight(elem, False)
self.currentSelection = selection.selected
for elem in self.currentSelection:
self.highlight(elem, True)
def projectCoords(self, coords):
realCoords = [0, 0]
realCoords[0] = coords[0] / self.zoomRatio
realCoords[1] = coords[1] / self.zoomRatio
return realCoords