Advertisement
Guest User

Untitled

a guest
May 27th, 2017
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 8.87 KB | None | 0 0
  1. #!/usr/bin/python
  2.  
  3. import cluttergtk   # must be the first to be imported
  4. import clutter
  5. import pygtk
  6. pygtk.require("2.0")
  7. import gtk
  8. import gobject
  9. import glib
  10. import time
  11. import cairo
  12. import pango
  13. import pangocairo
  14. import rsvg
  15. import sys
  16. import math
  17. from math import sqrt
  18.  
  19. import random
  20.  
  21.  
  22. class DiceRollView(clutter.Group):
  23.     """
  24.     Does dice-rolling animation and display of
  25.     successes.
  26.     """
  27.     DICE_ROLLED_HEIGHT = 50
  28.     DICE_UNROLLED_HEIGHT = 50
  29.     DICE_SIZE = 40
  30.     def __init__(self, stage):
  31.         clutter.Group.__init__(self)
  32.  
  33.         self.set_reactive(True)     #needed to recieve clicks!
  34.         self.stage = stage
  35.         self.stage.add(self)
  36.  
  37.         self.be_behind = []     #a list of actors that animations should stay behind; this is used for 'real_roll' values
  38.         self.rollable_area = (0,0,1,1)      #bounding box that designates where popup numbers may appear
  39.         self.completed_rolls = 0
  40.  
  41.         self.set_size(500,500)
  42.         self.w,self.h = self.get_size()
  43.        
  44.         self.background = self._create_background()
  45.         self.add(self.background)
  46.  
  47.         self.connect("motion-event", self.on_mouse_move)
  48.         self.connect("button-press-event", self.on_mouse_press)
  49.    
  50.     def on_mouse_press(self, actor, event):
  51.         self.animate_popout_real_roll(self._make_number( "1234567890" ))
  52.     def on_mouse_move(self, actor, event):
  53.         self.animate_popout(self._make_number( 1230 ))
  54.         pass
  55.  
  56.     def _get_next_completed_roll_pos_and_size(self):
  57.         """
  58.         returns a the coordinates of the next empty spot
  59.         in the completed rolls queue and the size that the die
  60.         should be.
  61.         """
  62.         return (self.completed_rolls*self.DICE_SIZE, 0), self.DICE_SIZE
  63.     def add_completed_roll(self, actor):
  64.         """
  65.         move actor to the completed rolls queue
  66.         """
  67.         def do_shrink(*x):
  68.             #anim2 = actor.animate(clutter.LINEAR, 400, 'scale-x', scale_factor, 'scale-y', scale_factor)
  69.             anim2 = actor.animate(clutter.LINEAR, 400, 'scale-x', 1)
  70.             anim2.connect_after('completed', do_last)
  71.         def do_last(*x):
  72.             actor.raise_top()
  73.             print actor.get_opacity(), actor.get_color(), actor.get_animation()
  74.             print actor
  75.             anim2 = actor.animate(clutter.LINEAR, 1000, 'scale-x', .5)
  76.             anim2.connect_after("completed", do_last_last)
  77.         def do_last_last(*x):
  78.             actor.animate(clutter.LINEAR, 1000, 'scale-x', 1)
  79.             pass
  80.  
  81.         pos,target_size = self._get_next_completed_roll_pos_and_size()
  82.         scale_factor = float(target_size)/max(actor.get_size())
  83.         print scale_factor
  84.         anim1 = actor.animate(clutter.LINEAR, 400, 'scale-x', 1, 'x', pos[0], 'y', pos[1] )
  85.         anim1.connect_after('completed', do_shrink)
  86.         self.completed_rolls += 1
  87.  
  88.     def animate_popout_real_roll(self, actor):
  89.         """
  90.         animates actor to 'pop out' of the screen
  91.         with a pretty effect and then moves
  92.         the actor into the roll queue
  93.         """
  94.         def do_glow(*x):
  95.             actor_clone.set_position( self.w/2, self.h/2 )
  96.             actor_clone.set_opacity(0)
  97.             actor_clone.show()
  98.             anim2 = actor_clone.animate(clutter.EASE_OUT_QUAD, 400, "opacity", 200,
  99.                 "scale-x", 2, "scale-y", 2,
  100.                 "fixed::scale-center-x", actor_clone.get_width()/2, "fixed::scale-center-y", actor_clone.get_height()/2,
  101.                 "x", end_pos[0], "y", end_pos[1])
  102.             anim2.connect_after("completed", do_fade_out)
  103.         def do_fade_out(*x):
  104.             anim3 = actor_clone.animate(clutter.EASE_OUT_QUAD, 400, "opacity", 0)
  105.             anim4 = actor.animate(clutter.EASE_OUT_QUAD, 400,
  106.                 "color", clutter.Color(255,0,0,255))
  107.             anim4.connect_after("completed", do_finish_actor)
  108.             anim3.connect_after("completed", do_finish_clone)
  109.         def do_finish_actor(*x):
  110.             self.add_completed_roll(actor)
  111.         def do_finish_clone(*x):
  112.             self.remove(actor_clone)
  113.        
  114.         self.be_behind.append(actor)
  115.  
  116.         actor.set_position( self.w/2, self.h/2 )
  117.         actor.set_scale(.1,.1)
  118.  
  119.         end_pos = (random.randrange(self.rollable_area[0],self.rollable_area[2]-actor.get_width()), random.randrange(self.rollable_area[1],self.rollable_area[3]-actor.get_height()))
  120.         actor_clone = clutter.Clone(actor)
  121.         actor_clone.hide()
  122.         actor_clone.set_position(*end_pos)
  123.         self.add(actor_clone)
  124.         self.add(actor)
  125.         anim1 = actor.animate(clutter.EASE_OUT_CUBIC, 250,
  126.             "x", end_pos[0], "y", end_pos[1],
  127.             "scale-x", 1, "scale-y", 1)
  128.             #"color", clutter.Color(255,0,0,255))
  129.         anim1.connect_after("completed", do_fade_out)
  130.         #do_glow()
  131.  
  132.     def animate_popout(self, actor):
  133.         """
  134.         animates actor to 'pop out' of the screen
  135.         with a pretty effect
  136.         """
  137.         def do_glow(*x):
  138.             actor_clone.set_position( self.w/2, self.h/2 )
  139.             actor_clone.set_opacity(0)
  140.             actor_clone.show()
  141.             anim2 = actor_clone.animate(clutter.EASE_OUT_QUAD, 400, "opacity", 200,
  142.                 "scale-x", 2, "scale-y", 2,
  143.                 "fixed::scale-center-x", actor_clone.get_width()/2, "fixed::scale-center-y", actor_clone.get_height()/2,
  144.                 "x", end_pos[0], "y", end_pos[1])
  145.             anim2.connect_after("completed", do_fade_out)
  146.         def do_fade_out(*x):
  147.             anim3 = actor_clone.animate(clutter.EASE_OUT_QUAD, 400, "opacity", 0)
  148.             anim4 = actor.animate(clutter.EASE_OUT_QUAD, 400, "opacity", 0)
  149.             anim4.connect_after("completed", do_finish_actor)
  150.             anim3.connect_after("completed", do_finish_clone)
  151.         def do_finish_actor(*x):
  152.             self.remove(actor)
  153.         def do_finish_clone(*x):
  154.             self.remove(actor_clone)
  155.  
  156.         actor.set_position( self.w/2, self.h/2 )
  157.         actor.set_scale(.1,.1)
  158.  
  159.         end_pos = (random.randrange(self.rollable_area[0],self.rollable_area[2]-actor.get_width()), random.randrange(self.rollable_area[1],self.rollable_area[3]-actor.get_height()))
  160.         actor_clone = clutter.Clone(actor)
  161.         actor_clone.hide()
  162.         actor_clone.set_position(*end_pos)
  163.         self.add(actor_clone)
  164.         self.add(actor)
  165.         for above in self.be_behind:
  166.             above.raise_top()
  167.  
  168.         anim1 = actor.animate(clutter.EASE_OUT_CUBIC, 250,
  169.             "x", end_pos[0], "y", end_pos[1],
  170.             "scale-x", 1, "scale-y", 1)
  171.         do_glow()
  172.  
  173.     def _make_number(self, num):
  174.         """
  175.         makes a new actor that displays the number num
  176.         """
  177.         num = str(num)
  178.  
  179.         ret = clutter.Text()
  180.         ret.set_color( clutter.color_from_string("white") )
  181.         ret.set_text( num )
  182.         ret.set_font_name( "Serif Bold 50" )
  183.  
  184.         return ret
  185.  
  186.     def _create_background(self):
  187.         PADDING = 20
  188.         RADIUS = 20
  189.         new_w,new_h = int(self.stage.get_width()),int(self.stage.get_height())
  190.        
  191.         back =  clutter.CairoTexture(new_w,new_h)
  192.         cr = back.cairo_create()
  193.         cr.set_source_rgb(.4,.4,.4)
  194.         cr.paint()
  195.        
  196.         #figure out where we should draw this box
  197.         draw_area = (PADDING, self.DICE_ROLLED_HEIGHT+PADDING, self.w-2*PADDING, self.h-self.DICE_ROLLED_HEIGHT-self.DICE_UNROLLED_HEIGHT -2*PADDING)
  198.         rollable_inset = (sqrt(2)-1)*RADIUS
  199.         self.rollable_area = [int(i) for i in (draw_area[0] + rollable_inset, draw_area[1] + rollable_inset, draw_area[0]+draw_area[2]-rollable_inset, draw_area[1]+draw_area[3]-rollable_inset)]
  200.         self.draw_box_with_rounded_corners(cr, draw_area[0], draw_area[1], draw_area[2], draw_area[3], RADIUS, (0.95*.1, 0.75*.1, 0.5*.1, 1))
  201.         del cr
  202.        
  203.         return back
  204.     def draw_box_with_rounded_corners(self, ctx, x, y, w, h, rad=10, col_fill=(0.95, 0.75, 0.5, 0.3), col_border=(0.9, 0.5, 0.1, .5)):
  205.         ctx.move_to(x+rad,y)
  206.         ctx.rel_line_to( w - 2*rad,0)
  207.         ctx.rel_curve_to( 0, 0, rad, 0, rad, rad)
  208.         ctx.rel_line_to( 0,h - 2*rad)
  209.         ctx.rel_curve_to( 0, 0, 0, rad, -rad, rad)
  210.         ctx.rel_line_to( -(w - 2*rad),0)
  211.         ctx.rel_curve_to( 0, 0, -rad, 0, -rad, -rad)
  212.         ctx.rel_line_to( 0,-(h - 2*rad))
  213.         ctx.rel_curve_to( 0, 0, 0, -rad, rad, -rad)
  214.         ctx.close_path()
  215.  
  216.         ctx.set_source_rgba(*col_fill)
  217.         ctx.fill_preserve()
  218.         ctx.set_source_rgba(*col_border)
  219.         ctx.stroke()
  220.  
  221.     def stage_resized(self):
  222.         """
  223.         resize ourselves and redraw the background, etc.
  224.         """
  225.         self.set_size( *self.stage.get_size() )
  226.         self.w,self.h = self.get_size()
  227.  
  228.         new_back = self._create_background()
  229.         self.add(new_back)
  230.         new_back.lower_bottom()
  231.         self.remove(self.background)
  232.         self.background = new_back
  233.  
  234.  
  235.  
  236. class MainWindow(object):
  237.     def __init__(self):
  238.         builder = gtk.Builder()
  239.         builder.add_from_file("charactersettings.ui")
  240.  
  241.         builder.get_object("main_window").connect("destroy-event", gtk.main_quit)
  242.         builder.get_object("main_window").connect("delete-event", gtk.main_quit)
  243.        
  244.         #get us a clutter stage and put it in the vbox
  245.         self.vbox_clutter = builder.get_object("vbox_clutter")
  246.         embed = cluttergtk.Embed()
  247.         self.vbox_clutter.pack_start(embed, True, True, 0)
  248.         embed.set_size_request(640,492)
  249.         #realizing the embed allows us to get the stage
  250.         embed.realize()
  251.        
  252.         stage = embed.get_stage()
  253.         stage.set_color(clutter.color_from_string('DarkBlue'))
  254.  
  255.         #we need depth sorting so we can flip cards over and see their backside
  256.         #clutter.cogl.set_depth_test_enabled(True)
  257.         #clutter.cogl.set_backface_culling_enabled(True)
  258.         #clutter.cogl.perspective(10,1,-1,1000)
  259.         self.inven = DiceRollView(stage)
  260.        
  261.    
  262.         stage.connect('notify::allocation', self.on_stage_allocate)
  263.         embed.show()
  264.  
  265.     def on_stage_allocate (self, actor, pspec):
  266.         self.inven.stage_resized()
  267.         pass
  268.  
  269.     def start(self):
  270.         gtk.main()
  271.  
  272.  
  273.  
  274.  
  275.  
  276.  
  277. if __name__ == "__main__":
  278.     app = MainWindow()
  279.     app.start()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement