def lookforkeys(event):
   global oldcells, object
   
   # look for keys used to flip/rotate object
   if event == "key x none" or event == "key y none":
      # flip floating object left-right or top-bottom
      g.putcells(object, 0, 0, 1, 0, 0, 1, "xor")  # erase object
      if len(oldcells) > 0: g.putcells(oldcells)
      obox = getminbox(object)
      if event == "key x none":
         # translate object so that bounding box doesn't change
         xshift = 2 * (obox.left + int(obox.wd/2))
         if obox.wd % 2 == 0: xshift -= 1
         object = g.transform(object, xshift, 0, -1, 0, 0, 1)
      else:
         # translate object so that bounding box doesn't change
         yshift = 2 * (obox.top + int(obox.ht/2))
         if obox.ht % 2 == 0: yshift -= 1
         object = g.transform(object, 0, yshift, 1, 0, 0, -1)
      oldcells = underneath(object)
      g.putcells(object)
      g.update()
      return
   
   if event == "key > none" or event == "key < none":
      # rotate floating object clockwise or anticlockwise
      # about the center of the object's bounding box
      obox = getminbox(object)
      midx = obox.left + int(obox.wd/2)
      midy = obox.top + int(obox.ht/2)
      newleft = midx + obox.top - midy
      newtop = midy + obox.left - midx
      rotrect = [ newleft, newtop, obox.ht, obox.wd ]
      if not rectingrid(rotrect):
         g.warn("Rotation is not allowed if object would be outside grid.")
         return
      g.putcells(object, 0, 0, 1, 0, 0, 1, "xor")  # erase object
      if len(oldcells) > 0: g.putcells(oldcells)
      if event == "key > none":
         # rotate clockwise
         object = g.transform(object, 0, 0, 0, -1, 1, 0)
      else:
         # rotate anticlockwise
         object = g.transform(object, 0, 0, 0, 1, -1, 0)
      # shift rotated object to same position as rotrect
      obox = getminbox(object)
      object = g.transform(object, newleft - obox.left, newtop - obox.top)
      oldcells = underneath(object)
      g.putcells(object)
      g.update()
      return

   if event == "key h none":
      showhelp2()
      return
   
   g.doevent(event)
Example #2
0
def parsecensus(census):
    '''Get census of the form (freq, cat) and convert it to glife.pattern.'''
    freq, cat = census
    catpat = parsepat(cat)
    freqpat = parsefreq(freq)
    # Align them to the center.
    _, _, _, fonth = glife.getminbox(freqpat)
    _, _, w, h = glife.getminbox(catpat)
    return (catpat.translate((patw-w)//2, (height-h)//2)
            + freqpat.translate(patw, (height-fonth)//2))
Example #3
0
def color_text(string, extrastate):
    t = make_text(string, "mono")
    bbox = getminbox(t)
    # convert two-state pattern to multi-state and set state to extrastate
    mlist = []
    tlist = list(t)
    for i in xrange(0, len(tlist), 2):
        mlist.append(tlist[i])
        mlist.append(tlist[i+1])
        mlist.append(extrastate)
    if len(mlist) % 2 == 0: mlist.append(0)
    p = pattern(mlist)
    return p, bbox.wd, bbox.ht
def moveobject():
   global oldcells, object, object1
   
   # wait for 1st click in live cell
   while True:
      event = g.getevent()
      if event.startswith("click"):
         # event is a string like "click 10 20 left none"
         evt, xstr, ystr, butt, mods = event.split()
         result = findlivecell(int(xstr), int(ystr))
         if len(result) > 0:
            prevx = int(xstr)
            prevy = int(ystr)
            oldmouse = xstr + ' ' + ystr
            g.show("Extracting object...")
            x, y = result
            object = getobject(x, y)
            object1 = list(object)     # save in case user aborts script
            if mods == "alt":
               # don't delete object
               oldcells = list(object)
            break
         else:
            g.warn("Click on or near a live cell belonging to the desired object.")
      elif event == "key h none":
         showhelp1()
      else:
         g.doevent(event)
   
   # wait for 2nd click while moving object
   g.show("Move mouse and click again..." + helpmsg)
   gotclick = False
   while not gotclick:
      event = g.getevent()
      if event.startswith("click"):
         evt, x, y, butt, mods = event.split()
         mousepos = x+' '+y
         gotclick = True
      else:
         if len(event) > 0: lookforkeys(event)
         mousepos = g.getxy()
      if len(mousepos) > 0 and mousepos != oldmouse:
         # mouse has moved, so move object
         g.putcells(object, 0, 0, 1, 0, 0, 1, "xor")  # erase object
         if len(oldcells) > 0: g.putcells(oldcells)
         xstr, ystr = mousepos.split()
         x = int(xstr)
         y = int(ystr)

         if g.getwidth() > 0:
            # ensure object doesn't move beyond left/right edge of grid
            obox = getminbox( g.transform(object, x - prevx, y - prevy) )
            if obox.left < gridl:
               x += gridl - obox.left
            elif obox.right > gridr:
               x -= obox.right - gridr
         if g.getheight() > 0:
            # ensure object doesn't move beyond top/bottom edge of grid
            obox = getminbox( g.transform(object, x - prevx, y - prevy) )
            if obox.top < gridt:
               y += gridt - obox.top
            elif obox.bottom > gridb:
               y -= obox.bottom - gridb

         object = g.transform(object, x - prevx, y - prevy)
         oldcells = underneath(object)
         g.putcells(object)
         prevx = x
         prevy = y
         oldmouse = mousepos
         g.update()
Example #5
0
mingen = min(genlist)
maxgen = max(genlist)
xlen = int(maxgen - mingen)
ylen = int(min(xlen, (maxpop - minpop) / popscale))
# popscale = float(maxpop - minpop) / float(ylen)

genscale = 1
# genscale = float(maxgen - mingen) / float(xlen)

# draw axes with origin at 0,0
draw_line(0, 0, xlen, 0)
draw_line(0, 0, 0, -ylen)

# add annotation using mono-spaced ASCII font
t = make_text(pattname.upper(), "mono")
bbox = getminbox(t)
t.put((xlen - bbox.wd) / 2, -ylen - 10 - bbox.ht)

t = make_text(ytitle, "mono")
bbox = getminbox(t)
t.put(-10 - bbox.ht, -(ylen - bbox.wd) / 2, rccw)

t = make_text(str(minpop), "mono")
bbox = getminbox(t)
t.put(-bbox.wd - 10, -bbox.ht / 2)

t = make_text(str(maxpop), "mono")
bbox = getminbox(t)
t.put(-bbox.wd - 10, -ylen - bbox.ht / 2)

t = make_text("%s (step=%s)" % (xtitle, stepsize), "mono")
Example #6
0
if minpop == maxpop:
    # avoid division by zero
    minpop -= 1
popscale = float(maxpop - minpop) / float(ylen)

mingen = min(genlist)
maxgen = max(genlist)
genscale = float(maxgen - mingen) / float(xlen)

# draw axes with origin at 0,0
draw_line(0, 0, xlen, 0)
draw_line(0, 0, 0, -ylen)

# add annotation using mono-spaced ASCII font
t = make_text(pattname.upper(), "mono")
bbox = getminbox(t)
t.put((xlen - bbox.wd) / 2, -ylen - 10 - bbox.ht)

t = make_text("POPULATION", "mono")
bbox = getminbox(t)
t.put(-10 - bbox.ht, -(ylen - bbox.wd) / 2, rccw)

t = make_text(str(minpop), "mono")
bbox = getminbox(t)
t.put(-bbox.wd - 10, -bbox.ht / 2)

t = make_text(str(maxpop), "mono")
bbox = getminbox(t)
t.put(-bbox.wd - 10, -ylen - bbox.ht / 2)

t = make_text("GENERATION (step=%s)" % stepsize, "mono")