Vega strike Python Modules doc  0.5.1
Documentation of the " Modules " folder of Vega strike
 All Data Structures Namespaces Files Functions Variables
dialog_box.py
Go to the documentation of this file.
1 import VS
2 import GUI
3 import custom
4 import Base
5 import debug
6 
7 text_height=0.1
8 
9 def parse_dialog_box(args):
10  i=0
11  elementsToCreate=[]
12  currentList=elementsToCreate
13  while i<len(args):
14  type=args[i]
15  i+=1
16  if type=='list':
17  id=str(args[i])
18  jlen=int(args[i+1])
19  i+=2
20  listitems=args[i:i+jlen]
21  i+=jlen
22  currentList.append(DialogBox.List(id,listitems))
23  elif type=='space' or type=='width' or type=='height':
24  if type=='height':
25  wid=0.
26  else:
27  wid=float(args[i])
28  i+=1
29  if type=='width':
30  hei=0.
31  else:
32  hei=float(args[i])
33  i+=1
34  currentList.append(DialogBox.Space(wid,hei))
35  elif type=='button' or type=='buttonspr':
36  id=str(args[i])
37  i+=1
38  if type=='buttonspr':
39  sprite=str(args[i])
40  width=float(args[i+1])
41  height=float(args[i+2])
42  i+=3
43  name=id
44  else:
45  name=id #args[i]
46  #i+=1
47  width=0.
48  height=text_height
49  sprite=None
50  currentList.append(DialogBox.Button(id,name,sprite,width,height))
51  elif type=='text' or type=="textwidth":
52  text=str(args[i])
53  i+=1
54  wid=0.
55  if type=="textwidth":
56  wid=float(args[i])
57  i+=1
58  currentList.append(DialogBox.Text(text,wid))
59  elif type=='textinput':
60  id=str(args[i])
61  initialvalue=str(args[i+1])
62  i+=2
63  currentList.append(DialogBox.TextInput(id,initialvalue))
64  elif type=='endrow':
65  currentList=elementsToCreate
66  elif type=='row':
67  currentList=[]
68  elementsToCreate.append(DialogBox.Row(currentList))
69  else:
70  debug.debug("Unknown dialog item type "+str(type))
71  return elementsToCreate
72 
73 if not VS.isserver():
74  font_size = 1.0
75  text_height = Base.GetTextHeight('|',tuple([font_size]*3))*1.05
76  list_height = 0.0
77  screen_color = GUI.GUIColor(200/255.0, 220/255.0 ,210/255.0) # first I tried rgb(56 60 24) and rgb(40 44 20); both were too light
78  input_color = GUI.GUIColor(25/255.0, 25/255.0 ,75/255.0)
79  screen_bgcolor = GUI.GUIColor(0.9,0.9,1.0,0.7)
80  screen_bgcolor_nc = GUI.GUIColor(0.1,0.0,0.4)
81 
82 def makeRect(x,y,wid,hei):
83  return GUI.GUIRect(x,y,wid,hei,'normalized_biased_scaled')
84 
85 class DialogBox:
86  class Item:
87  next_id=0
88  def __init__(self,id=None):
89  self.gui_element=None
90  numid=DialogBox.Item.next_id=DialogBox.Item.next_id+1
91  numid=str(numid)
92  id = id or ''
93  self.idname=id+"_"+numid
94  id = id or numid
95  self.id=id
96  def height(self):
97  return 0.
98  def width(self):
99  return 0.
100  def getValues(self,values):
101  pass
102  def create(self,owner,room,x,y,wid,hei):
103  return None
104  def draw(self):
105  #print 'Drawing element',self.idname
106  if self.gui_element:
107  self.gui_element.show()
108  self.gui_element.draw()
109  def undraw(self):
110  if self.gui_element:
111  self.gui_element.hide()
112  self.gui_element.undraw()
113  class Space(Item):
114  def __init__(self,width,height):
115  self.hei=height
116  self.wid=width
117  DialogBox.Item.__init__(self)
118  def height(self):
119  return self.hei
120  def width(self):
121  return self.wid
122  class List(Item):
123  def __init__(self,id,items):
124  self.items=items
125  self.numlines=len(items)
126  self.scroll=False
127  if self.numlines>10:
128  self.scroll=True
129  self.numlines=10
130  DialogBox.Item.__init__(self,id)
131  def height(self):
132  return (self.numlines+1) * (text_height+0.005) #fudge factor
133  def getValues(self,values):
134  values.append(self.id)
135  sel = self.picker.selection
136  if sel is None:
137  values.append('')
138  else:
139  values.append(self.picker.items[sel].data)
140  def scrollUp(self,button,params):
141  self.picker.scroll('up')
142  def create(self,owner,room,x,y,wid,hei):
143  screen_loc = makeRect(x,y-.5*text_height,wid-0.05,hei-text_height)
145 
146  self.picker=GUI.GUISimpleListPicker(room,'XXXPicker',self.idname+'p', screen_loc,
147  textcolor =input_color , textbgcolor =screen_bgcolor,
148  selectedbgcolor=screen_bgcolor_nc, selectedcolor=screen_color, owner=owner,
149  textfontsize =font_size)
150  self.picker.items=[ GUI.GUISimpleListPicker.listitem(el,el) for el in self.items ]
151  self.gui_element.children.append(self.picker)
152 
153  if self.scroll:
154  screen_loc = makeRect(x+wid-0.05, y, 0.05, text_height)
155  scroll_up=GUI.GUIButton(room,'Scroll Up',self.idname+'u',{'*':None},screen_loc,
156  clickHandler=lambda b,p:self.picker.viewMove(-3),textfontsize=font_size)
157  scroll_up.setCaption(' /\\ ')
158  self.gui_element.children.append(scroll_up)
159 
160  screen_loc = makeRect(x+wid-0.05, y-hei+1.5*text_height, 0.05, text_height)
161  scroll_down=GUI.GUIButton(room,'Scroll Down',self.idname+'d',{'*':None},screen_loc,
162  clickHandler=lambda b,p:self.picker.viewMove(3), textfontsize=font_size)
163  scroll_down.setCaption(' \\/ ')
164  self.gui_element.children.append(scroll_down)
165 
166  return self.gui_element
167  def draw(self):
168  DialogBox.Item.draw(self)
169  class Text(Item):
170  def __init__(self,text,wid=0.):
171  self.text=text
172  print text
173  self.wid=wid
174  DialogBox.Item.__init__(self)
175  def height(self):
176  return text_height*(1+self.text.count("\n"))
177  def width(self):
178  return self.wid
179  def create(self,owner,room,x,y,wid,hei):
180  screen_loc=makeRect(x,y,wid,hei)
181  self.gui_element=GUI.GUIStaticText(room,self.idname,self.text, screen_loc,
182  color=screen_color, fontsize=font_size,
183  bgcolor=GUI.GUIColor.clear())
184  return self.gui_element
185 
186  class Button(Item):
187  def __init__(self,id,text,sprite,width,height):
188  self.text=text
189  self.sprite=sprite
190  self.wid=width
191  self.hei=height
192  if not self.hei:
193  self.hei=text_height
194  DialogBox.Item.__init__(self,id)
195  def height(self):
196  return self.hei+text_height
197  def width(self):
198  return self.wid
199  def handleButton(self,button,params):
200  button.owner.handleButton(self.id)
201  def create(self,owner,room,x,y,wid,hei):
202  screen_loc=makeRect(x+text_height/4.,y-text_height/2.,wid-text_height/2.,self.hei)
203  text=self.text or ''
204  self.gui_element=GUI.GUIButton(room,''+self.text,self.idname,{'*':self.sprite},screen_loc,
205  clickHandler=self.handleButton, owner=owner,
206  textfontsize=font_size,textbgcolor=GUI.GUIColor(0.3,0.,0.,.5))
207  if self.text:
208  self.gui_element.setCaption(' '+self.text)
209  return self.gui_element
210 
211  class TextInput(Item):
212  def __init__(self,id,text):
213  self.text=text
214  DialogBox.Item.__init__(self,id)
215  def height(self):
216  return text_height
217  def getValues(self,values):
218  values.append(self.id)
219  values.append(self.gui_element.getText())
220  def handleButton(self,textarea):
221  textarea.owner.close(not textarea.canceled)
222  def create(self,owner,room,x,y,wid,hei):
223  screen_loc=makeRect(x,y,wid,hei)
224  self.gui_element=GUI.GUILineEdit(self.handleButton,room,self.idname,self.text, screen_loc,
225  color=input_color,fontsize=font_size,bgcolor=screen_bgcolor,focusbutton=True,owner=owner)
226  return self.gui_element
227 
228 
229  class Row(Item):
230  def __init__(self,items):
231  self.items=items
232  DialogBox.Item.__init__(self)
233  def width(self):
234  totalwid=0.
235  for el in self.items:
236  wid=el.width()
237  #if wid==0.:
238  # return 0.
239  totalwid+=wid
240  return totalwid
241  def getValues(self,values):
242  for el in self.items:
243  el.getValues(values)
244  def height(self):
245  return reduce(max,map(lambda x:x.height(),self.items))
246  def create(self,owner,room,x,y,wid,hei):
247  screen_loc=makeRect(x,y,wid,hei)
249  if self.items:
250  totalwid=0.
251  num_variable=0
252  for it in self.items:
253  w=it.width()
254  if w==0:
255  num_variable+=1
256  totalwid+=w
257  childs=[]
258  avgwid = (wid-totalwid) / num_variable
259  for it in self.items:
260  w=it.width()
261  if w==0:
262  w=avgwid
263  it.create(owner,room,x,y,w,hei)
264  x+=w
265  if it.gui_element:
266  childs.append(it.gui_element)
267  self.gui_element.children = childs
268  return self.gui_element
269  def draw(self):
270  for it in self.items:
271  it.draw()
272  return DialogBox.Item.draw(self)
273  def undraw(self):
274  for it in self.items:
275  it.undraw()
276  return DialogBox.Item.undraw(self)
277 
278  def __init__(self,elements,callback):
279  self.elements=elements
280  self.bg=None
281  self.bglink=None
282  self.roomid=0
283  self.callback=callback
284  DialogBox.Item.next_id+=1
285  self.idname='dialog_bg_'+str(DialogBox.Item.next_id)
286  self.id=None
287 
288  def handleButton(self,id):
289  values=[id]
290  for el in self.elements:
291  el.getValues(values)
292  self.callback(self,values)
293 
294  def calculateWidth(self):
295  totalheight=0.
296  totalwidth=0.
297  for el in self.elements:
298  totalheight+=el.height()
299  wid=el.width()
300  if wid>totalwidth:
301  totalwidth=wid
302  self.totalheight=totalheight
303  self.totalwidth=totalwidth
304 
305  def create(self,roomid):
306  if VS.isserver():
307  return
308  self.calculateWidth()
309  x=self.totalwidth/-2.
310  y=self.totalheight/2.
311  room=GUI.GUIRoom(roomid)
312  self.roomid=roomid
313  if not self.bg:
314  rect = makeRect(x-0.01,y+0.01,self.totalwidth+0.02,self.totalheight+0.02)
315  size=rect.getHotRect()
316  self.bgsize=size
317  #print str(rect)
318  #print (x,y,self.totalwidth,self.totalheight)
319  self.bg=GUI.GUIStaticText(room,self.idname,'',rect,
320  color=GUI.GUIColor(0.6,0.6,0.6,1.0), bgcolor=GUI.GUIColor(0.2, 0.2, 0.2, 0.8))
321  self.bglink=GUI.GUIMouseOver(room,'Dialog box',self.idname,rect)
322  for el in self.elements:
323  hei=el.height()
324  #print "*** Creating "+repr(el)+" at y "+str(y)+", height "+str(hei)
325  el.create(self,room,x,y,self.totalwidth,hei)
326  y-=hei
327 
328  def close(self,success=False):
329  if success:
330  self.handleButton('OK')
331  else:
332  self.handleButton('Cancel')
333  def keyDown(self,key):
334  debug.debug("dialog box got key: %i" % key)
335  if key == 13 or key == 10: #should be some kind of return
336  self.close(True)
337  elif key == 27: #escape is always 27, isn't it?
338  self.close(False)
339 
340  def undraw(self):
341  if not VS.isserver():
342  GUI.GUIRootSingleton.keyTarget=self.lastKeyTarget
343  #debug.warn("Targetting keys to "+str(self.lastKeyTarget))
344  self.bg.hide()
345  self.bg.undraw()
346  self.bglink.hide()
347  self.bglink.undraw()
348  map(lambda x:x.undraw(), self.elements)
349 
350  def draw(self):
351  if not VS.isserver():
352  self.lastKeyTarget=GUI.GUIRootSingleton.keyTarget
353  GUI.GUIRootSingleton.keyTarget=self
354  #debug.warn("Targetting keys to "+str(self))
355  self.bg.show()
356  self.bg.draw()
357  self.bglink.show()
358  self.bglink.draw()
359  map(lambda x:x.draw(), self.elements)
360 
361 
362 def fromValues(data):
363  action = data[0]
364  i=1
365  inputs={}
366  while i<len(data):
367  inputs[data[i]] = data[i+1]
368  i+=2
369  return action, inputs
370 
371 _gui_has_initted=False
372 
373 def dialog(args, callback, room=None):
374  global _gui_has_initted
375 
376  db = DialogBox(parse_dialog_box(args),callback)
377  if VS.isserver():
378  db.id = custom.run("dialog_box",args,lambda data:callback(db,data))
379  return db.id
380  if not room:
381  room=Base.GetCurRoom()
382  if not _gui_has_initted:
383  GUI.GUIInit(320,200)
384  _gui_has_initted=True
385  db.create(room)
386  db.draw()
387  GUI.GUIRootSingleton.broadcastMessage('draw',None)
388 
389 def custom_run_dialog(local, cmd, args, id):
390  if VS.isserver():
391  return
392 
393  def myCallback(db,data):
394  def serverCallback(data):
395  if data[0]=='close':
396  db.undraw()
397 
398  custom.respond(data,serverCallback,id)
399  dialog(args, myCallback, Base.GetCurRoom())
400 
401 
402 def button_row(width, *buttons):
403  items = ["height",0.05,"row"]
404  if len(buttons)==1:
405  items += ["width",(width-0.25)/2.,
406  "button",buttons[0],
407  "width",(width-0.25)/2.]
408  elif len(buttons)==2:
409  items += ["button",buttons[0],"width",(width-0.5),"button",buttons[1]]
410  elif len(buttons)==3:
411  items += ["button",buttons[0],"width",(width-0.75)/2.,
412  "button",buttons[1],"width",(width-0.75)/2.,
413  "button",buttons[2]]
414  else:
415  for b in buttons:
416  items += ["button",b]
417  items += ["endrow"]
418  return items
419 
420 def alert(message, callback=None, width=1.0, buttonText='OK'):
421  def dbcallback(db,data):
422  db.undraw()
423  custom.respond(["close"],None,db.id)
424  if callback:
425  return callback()
426 
427  dialog(["width",width, "text",message] + button_row(width, buttonText), dbcallback)
428 
429 def confirm(message, callback, width=1.0, buttons=('Cancel','OK')):
430  def dbcallback(db,data):
431  db.undraw()
432  custom.respond(["close"],None,db.id)
433  arg = False
434  if data[0]!=buttons[0] and data[0]!="Cancel":
435  arg = data[0]
436  if callback:
437  return callback(arg)
438 
439  dialog(["width",width, "text",message] + button_row(width, *buttons), dbcallback)
440 
441 
442 custom.add("dialog_box",custom_run_dialog)
443 
444