Merge branch 'db/rev'
[plumiferos.git] / source / tools / MakeCursor.py
blob40f6c57b5c9e64c828745f6de42e10487375ec2d
1 #!/usr/bin/python
3 # Created by Robert Wenzlaff (Det. Thorn).
4 # Oct. 30, 2003
6 from Tkinter import *
7 color=["black", "white", "darkgreen", "gray"]
9 class App:
11 def __init__(self, master):
12 frame = Frame(master, borderwidth=5)
13 frame.grid(column=0, row=0, pady=5)
15 self.state=[]
16 self.states=256
17 self.laststate=2 # 0=Black, 1=White, 2=Transp.
19 self.size=16
20 self.gridsz=20
22 for x in range(1024):
23 self.state.append(2);
25 self.screen=Canvas( frame, height=320, width=320, bg=color[2] )
26 self.screen.bind("<Button-1>", self.scrnclick1)
27 self.screen.bind("<Button-3>", self.scrnclick2)
28 self.screen.bind("<B1-Motion>", self.scrndrag)
30 for x in range(16):
31 self.screen.create_line((x*20, 0, x*20, 320), fill=color[3]);
32 self.screen.create_line((0, x*20, 320, x*20), fill=color[3]);
34 self.screen.grid(row=0, column=0, columnspan=5)
36 frame2=Frame(master, borderwidth=5)
37 frame2.grid(column=0, row=1, pady=5)
39 self.clear=Button(frame2,text="Clear",command=self.clearit)
40 self.clear.grid(row=0, column=0, pady=20)
42 self.doit=Button(frame2, text="Do It", command=self.doit)
43 self.doit.grid(row=0, column=1, pady=20)
45 #self.doitlab=Label(frame2, text="(Output to stdout)");
46 #self.doitlab.grid(row=1, column=1);
48 self.parse=Button(frame2, text="Parse it", command=self.parsetext);
49 self.parse.grid(row=0, column=2, pady=20)
51 self.large=0
52 self.dummy=IntVar()
53 self.largeb=Checkbutton(frame2, text="Large", var=self.dummy, command=self.changesize)
54 self.largeb.grid(row=0, column=3, pady=20)
56 self.prev=Canvas(frame2, height=17, width=17, bg=color[2], relief=RIDGE)
57 self.prev.grid(row=0, column=4, pady=20, padx=20)
59 # DataParsers
60 self.bmlabel=Label(frame2, text="Bitmap Data (paste hex from code)")
61 self.bmlabel.grid(row=2, column=0, columnspan=5, sticky="W")
63 self.bmentry=Text(frame2, width=80, height=9, font="Times 8");
64 self.bmentry.bind("<Leave>", self.bmtextpaste);
65 self.bmentry.grid(row=3, column=0, columnspan=5, pady=5);
67 self.msklabel=Label(frame2, text="Mask Data (paste hex from code)")
68 self.msklabel.grid(row=4, column=0, columnspan=5, sticky="W")
70 self.mskentry=Text(frame2, width=80, height=9, font="Times 8");
71 self.mskentry.bind("<Leave>", self.msktextpaste);
72 self.mskentry.grid(row=5, column=0, columnspan=5, pady=5);
74 def changesize(self):
75 self.large=~self.large
76 if self.large:
77 self.size=32
78 self.gridsz=10
79 self.states=1024
80 oldstate=self.state
81 self.state=[]
82 for n in range(1024):
83 col=(n/2)%16
84 row=int(n/64)
85 self.state.append(oldstate[16*row+col])
86 oldstate=[]
87 else:
88 self.size=16
89 self.gridsz=20
90 self.states=256
91 oldstate=self.state
92 self.state=[]
93 for n in range(1024):
94 if not((n%2) or ((n/32)%2)):
95 self.state.append(oldstate[n])
96 for n in range(256,1024):
97 self.state.append(2)
98 oldstate=[]
100 #Insert scaling here
102 self.updatescrn()
103 self.prev.config(width=self.size+1, height=self.size+1)
104 for n in range(self.states):
105 self.updateprev(n)
106 #self.prev.grid(row=0, column=4, padx=self.gridsz, pady=self.gridsz)
108 def scrnclick1(self, event):
109 self.scrnclick(event, 1)
111 def scrnclick2(self, event):
112 self.scrnclick(event, -1)
114 def scrnclick(self, event, direction):
115 if (event.x>319) or (event.y>319) or (event.x<0) or (event.y<0): return
117 n=(event.x/self.gridsz) + self.size*(event.y/self.gridsz)
119 self.state[n]+=direction
120 self.state[n]%=3
122 row=n%self.size
123 col=n/self.size
125 self.screen.create_rectangle( (self.gridsz*row+1, self.gridsz*col+1,\
126 self.gridsz*row+self.gridsz-1, self.gridsz*col+self.gridsz-1), \
127 fill=color[self.state[n]], outline="" )
129 self.laststate=self.state[n]
130 self.updateprev(n)
132 def scrndrag(self,event):
133 if (event.x>319) or (event.y>319) or (event.x<0) or (event.y<0): return
135 n=(event.x/self.gridsz) + self.size*(event.y/self.gridsz)
137 row=n%self.size
138 col=n/self.size
140 self.screen.create_rectangle( (self.gridsz*row+1, self.gridsz*col+1, \
141 self.gridsz*row+self.gridsz-1, self.gridsz*col+self.gridsz-1), \
142 fill=color[self.laststate], outline="" )
143 self.state[n]=self.laststate
145 self.updateprev(n)
147 def updateprev(self, n):
148 x=n%self.size+1
149 y=n/self.size+1
151 if self.large: pad=12
152 else: pad=20
154 self.prev.create_line(x+1,y+1,x+2,y+1,fill=color[self.state[n]])
155 self.prev.grid(row=0, column=4, padx=pad, pady=pad)
157 def updatescrn(self):
158 self.screen.create_rectangle( 0,0,320,320, fill=color[2] )
159 for x in range(self.size):
160 self.screen.create_line((x*self.gridsz, 0, x*self.gridsz, 320), fill=color[3]);
161 self.screen.create_line((0, x*self.gridsz, 320, x*self.gridsz), fill=color[3]);
162 for n in range(self.states):
163 row=n%self.size
164 col=n/self.size
165 self.screen.create_rectangle( (self.gridsz*row+1, self.gridsz*col+1, \
166 self.gridsz*row+self.gridsz-1, self.gridsz*col+self.gridsz-1), \
167 fill=color[self.state[n]], outline="" )
169 def bmtextpaste(self, event):
170 string=self.bmentry.get(1.0, END)
171 self.bmentry.delete(1.0, END)
172 string=string.replace("\t", "")
173 self.bmentry.insert(END, string)
175 def msktextpaste(self, event):
176 string=self.mskentry.get(1.0, END)
177 self.mskentry.delete(1.0, END)
178 string=string.replace("\t", "")
179 self.mskentry.insert(END, string)
181 def parsetext(self):
182 from string import atoi
184 bmstring=self.bmentry.get(1.0, END)
185 bmstring=bmstring.replace(",", " ")
186 bmstring=bmstring.split()
188 mskstring=self.mskentry.get(1.0, END)
189 mskstring=mskstring.replace(",", " ")
190 mskstring=mskstring.split()
192 if (len(bmstring)!=len(mskstring)):
193 print "Mismatched data. Bitmap and mask must be same size,"
194 return
195 elif not (len(bmstring)==32 or len(bmstring)==128):
196 print "Size Error, input must be 32 or 128 hex bytes. "
197 return
200 for n in range(self.states):
201 self.state[n]=0;
203 m=0
204 for entry in bmstring:
205 e=atoi(entry, 16);
206 for bit in range(8):
207 self.state[m]=(e&1)
208 e=e>>1;
209 m+=1
211 m=0
212 for entry in mskstring:
213 e=atoi(entry, 16);
214 for bit in range(8):
215 if not (e&1):
216 self.state[m]=2;
217 e=e>>1
218 m+=1
220 self.updatescrn()
221 for n in range(self.states): self.updateprev(n)
223 def clearit(self):
224 for n in range(self.states):
225 self.state[n]=2;
226 self.updateprev(n)
227 self.updatescrn()
228 self.bmentry.delete(0.0, END);
229 self.mskentry.delete(0.0, END);
231 def doit(self):
232 mask=[]
233 bitmap=[]
234 numbytes=self.size*self.size/8
235 for i in range(numbytes):
238 for j in range(8):
239 m<<=1
240 b<<=1
241 if (self.state[(i*8)+(7-j)]!=2):
242 m|=1
243 if (self.state[(i*8)+(7-j)]==1):
244 b|=1
245 #print (i*8)+(7-j), self.state[(i*8)+(7-j)], m
246 mask.append(m)
247 bitmap.append(b)
249 print "\n\nstatic char bitmap[]={" %vars(),
250 for i in range(numbytes):
251 b1=bitmap[i]
252 if not(i%8): print "\n\t",
253 print "0x%(b1)02x, "%vars(),
254 print "\n};"
256 print "\nstatic char mask[]={" %vars(),
257 for i in range(numbytes):
258 b1=mask[i]
259 if not(i%8): print "\n\t",
260 print "0x%(b1)02x, "%vars(),
261 print "\n};"
263 ################## Main App #######################
264 root = Tk()
266 app = App(root)
267 root.title("Cursor Maker")
269 root.mainloop()