Pastebin launched a little side project called HostCabi.net, check it out ;-)Don't like ads? PRO users don't see any ads ;-)
Guest

main.py

By: jstokes75 on Feb 26th, 2013  |  syntax: Python  |  size: 8.77 KB  |  hits: 19  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #! /usr/bin/env  python
  2. from __future__ import division
  3. import os,shlex,subprocess,math
  4. import math
  5. from math import pi , tan, radians, sqrt
  6. import pyglet
  7. from pyglet.gl import *
  8. from pyglet import window
  9. from pyglet.window import key, mouse, Window
  10. from pyglet import app,clock
  11. from pyglet.image.codecs import *
  12.  
  13. import applications
  14. from my_camera import camera
  15. from objects import *
  16.  
  17.  
  18.  
  19. #----------------------End Classes----------------------------
  20.    
  21. #_________________Globals_________________________________
  22. def convert_color_alpha(color):
  23.     r, g, b, a= color
  24.     return float(r/255), float(g/255), float(b/255), float(a/255)
  25.    
  26. def convert_color(color):
  27.     r, g, b= color
  28.     return float(r/255), float(g/255), float(b/255)
  29.    
  30. window = window.Window(width=800, height=600, style=pyglet.window.Window.WINDOW_STYLE_BORDERLESS,visible=False)
  31. screen_width = window.screen.width
  32. screen_height = window.screen.height
  33. window.set_location(int(screen_width/2 - window.width/2),int( screen_height/2 - window.height/2))
  34. window.set_caption("BigSky")
  35. fps_display = pyglet.clock.ClockDisplay()
  36.  
  37. batch = pyglet.graphics.Batch()
  38. batch2 = pyglet.graphics.Batch()
  39.  
  40. text_group = pyglet.graphics.OrderedGroup(1)
  41.  
  42.  
  43.  
  44. #self, name, execute, cat_type, x, y, z,  w, h,
  45. boxs = []
  46. x = 0
  47. y = window.height
  48. z = 0
  49. box_color = convert_color_alpha((165,165,255, 255))
  50. box_color2 = convert_color_alpha((255,0,0, 255))
  51.  
  52. h_box_color = convert_color_alpha((255, 255, 255, 255))
  53. text_color = (0, 255, 255, 255)
  54.  
  55. window_color = convert_color_alpha((24, 24, 128, 255))
  56. glClearColor(*window_color)
  57.  
  58. camera = camera(window.width/2, window.height /2, window.width, window.height)
  59. camera.update()
  60.  
  61. drag = False
  62. drag_lenght = 0
  63. drag_base = window.width*.04
  64.  
  65. # ---- Build Objects --------
  66.  
  67. anchor_lower_x = 1
  68. box_height = 80
  69. box_width = 200
  70. spacer = 10
  71. offset = 20
  72. top_bottom_offset = 100 +box_height
  73. left_right_offset = 20
  74. x = 0
  75. y= window.height
  76. ml = applications.application_list()
  77. lables = []
  78. section_location_x = []
  79. section_list = []
  80. s_iter = 0
  81. max_x = 0
  82.  
  83. def calc_objects():
  84.     global x, y,  ml, boxs,  lables, section_location_x, box_height, box_width, spacer
  85.     global left_right_offset, top_bottom_offset, batch ,  batch2,  anchor_lower_x , s_iter
  86.     global section_list, max_x ,  text_color
  87.     del boxs[:]
  88.     del lables[:]
  89.     favs = ['Google Chrome', "Firefox"]
  90.     batch = pyglet.graphics.Batch()
  91.     batch2 = pyglet.graphics.Batch()
  92.     z = 8
  93.     x=0
  94.     y = window.height
  95.     for i in ml.known_cats:
  96.         title = i #titles
  97.         thelist = ml.list[i] # The List for this cat
  98.         number_of_items = float(len(thelist)) #number of items in cat
  99.         title_y = top_bottom_offset/2 #setup title for cat
  100.         title_x = x
  101.         col_num = 0
  102.          # Add space to the sections.
  103.         if len(thelist) > 0:
  104.             col_num = 1
  105.             title_x = x
  106.             for level2 in thelist:
  107.                 if y - top_bottom_offset <= box_height:
  108.                     x += (box_width + spacer)
  109.                     y = window.height
  110.                     col_num += 1
  111.                 if level2.name in favs:
  112.                     box_c = box_color2
  113.                 else:
  114.                     box_c = box_color
  115.                    
  116.                 boxs.append(l_items(level2.name, level2.execute, level2.icon,  title, x + left_right_offset, y - top_bottom_offset , z ,  box_width ,  box_height , box_c, text_color,  batch))
  117.                 y -= (box_height + spacer)
  118.         y = window.height
  119.        
  120.         width_of_section =int(col_num * (box_width+spacer)) # this over all witdh of cat
  121.         if col_num > 0: #if there is somethinng there draw title of cat
  122.             lables.append(top_titles(title_x, title_y,  width_of_section ,window.width, window.height,  title,  batch2))
  123.             x += (box_width + spacer + left_right_offset)
  124.             section_location_x.append(lables[-1].get_center_x())
  125.             section_list.append(title)
  126.         if number_of_items > 0:
  127.             x += spacer*2
  128.            
  129.     max_x = x + spacer + left_right_offset
  130.     camera.max_world_x = max_x
  131.  
  132.  
  133. # -----draw and stuff-------
  134.  
  135. def move(n):
  136.     camera.pan(n)
  137.    
  138. def goto_full_screen():
  139.     window.set_fullscreen(fullscreen = True)
  140.    
  141. def get_next_section():
  142.     global s_iter
  143.     if s_iter + 1 < len(section_location_x):
  144.         s_iter += 1
  145.    
  146.     print s_iter, section_location_x[s_iter] , section_list[s_iter]
  147.     return section_location_x[s_iter]      
  148. def get_prev_section():
  149.     global s_iter
  150.     if s_iter > 1:
  151.         s_iter -= 1
  152.    
  153.     print s_iter, section_location_x[s_iter] , section_list[s_iter]
  154.     return section_location_x[s_iter]
  155.    
  156. @window.event
  157. def on_resize(width, height):
  158.     calc_objects()
  159.     window.set_location(int(screen_width/2 - window.width/2),int( screen_height/2 - window.height/2))
  160.     glViewport(0, 0, width, height)
  161.     glMatrixMode(GL_PROJECTION)
  162.     glLoadIdentity()
  163.     gluPerspective(45, 1.0 * width / height, 0.1, 100.0)
  164.     camera.w = width
  165.     camera.h = height
  166.     camera.x = window.width/2
  167.     camera.y = window.height /2
  168.     camera.target.x = camera.x
  169.     camera.target.y = camera.y
  170.     camera.headsup(window.width, window.height)
  171.  
  172. @window.event
  173. def on_draw():
  174.     glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)                            
  175.     glEnable(GL_TEXTURE_2D)
  176.     glEnable (GL_BLEND)                                                            
  177.     glEnable (GL_LINE_SMOOTH)
  178.     glClear(GL_COLOR_BUFFER_BIT)
  179.     glLoadIdentity()
  180.     camera.update()
  181.     camera.project(window.width, window.height)
  182.     batch.draw()
  183.     batch2.draw()
  184.     camera.headsup(window.width, window.height)
  185.     fps_display.draw()
  186.  
  187. clock.schedule(lambda _: None)
  188.  
  189.  
  190. key_handlers = {
  191.     key.ESCAPE: lambda: window.close(),
  192.     key.PAGEUP: lambda: camera.pan_lr(window.width),
  193.     key.PAGEDOWN: lambda: camera.pan_lr(-window.width),
  194.     key.LEFT: lambda:camera.pan_lr(-box_width+spacer),
  195.     key.RIGHT: lambda: camera.pan_lr(box_width+spacer),
  196.     key.DOWN: lambda: camera.goto_x(get_next_section()),
  197.     key.UP: lambda: camera.goto_x(get_prev_section()),
  198.     key.END: lambda: camera.goto_x(max_x),
  199.     key.HOME: lambda:camera.goto_x(0),
  200.     key.F: lambda: goto_full_screen(),
  201.     }
  202.    
  203. def launch_ap(application):
  204.     print("starting {0}".format(application))
  205.     a = shlex.split(application)
  206.     subprocess.Popen(a)
  207.     exit_app()
  208.    
  209. def exit_app():
  210.     window.set_fullscreen(fullscreen=False)
  211.     window.minimize()
  212.     pyglet.app.exit()
  213.  
  214.  
  215.  
  216. def check_for_box( x ,  y , nx ,  ny ):
  217.     for i in boxs:
  218.         if nx > i.x and nx < (i.x + i.w) and ny > i.y and ny < (i.y + i.h):
  219.             print ("starting {0}".format(i.execute))
  220.             launch_ap(i.execute)
  221.  
  222. @window.event
  223. def on_deactivate():
  224.         exit_app()
  225.        
  226. @window.event
  227. def on_key_press(symbol, modifiers):
  228.     handler = key_handlers.get(symbol, lambda: None)
  229.     handler()
  230.    
  231.  
  232. @window.event
  233. def on_mouse_motion(x, y, dx, dy):
  234.  
  235.     nx, ny = camera.calc_pos(x, y)
  236.     #print x ,  y , nx ,  ny ,  camera.scale
  237.     for i in boxs:
  238.         if nx > i.x and nx < (i.x + i.w) and ny > i.y and ny < (i.y + i.h):
  239.             i.box_color = h_box_color
  240.             i.highted = True
  241.             i.z = -5
  242.             i.update()
  243.         elif i.highted == True :
  244.             i.highted = False
  245.             i.box_color = box_color
  246.             i.z = 8
  247.             i.update()
  248.             print i.icon
  249.            
  250. @window.event
  251. def on_mouse_release(x, y, button, modifiers):
  252.     global drag, drag_lenght, drag_base
  253.     print drag_lenght,  drag
  254.     if button & mouse.LEFT:
  255.         if drag == True and drag_lenght >= drag_base:
  256.             camera.pan_lr(-window.width)
  257.        
  258.         elif drag == True and drag_lenght <= -drag_base:
  259.             camera.pan_lr(+window.width)
  260.            
  261.         elif drag == True and (drag_lenght < drag_base and  drag_lenght > -drag_base):
  262.             nx, ny = camera.calc_pos(x, y)
  263.             cb = check_for_box(x, y,  nx, ny )
  264.        
  265.     drag = False
  266.     drag_lenght = 0
  267.  
  268. @window.event
  269. def on_mouse_drag(x, y, dx, dy, buttons, modifiers):
  270.     global drag_lenght , drag
  271.     print x, dx
  272.     if buttons & mouse.MIDDLE:  
  273.         camera.pan(dx, 0)
  274.    
  275.     if buttons & mouse.LEFT :
  276.         #camera.pan(dx, 0)
  277.         drag = True
  278.         drag_lenght += dx
  279.        
  280. @window.event
  281. def on_mouse_press(x, y, button,  modifiers):
  282.     global drag
  283.     if button == mouse.LEFT and drag == False:
  284.         drag = True
  285.        
  286. @window.event
  287. def on_mouse_scroll(x, y, scroll_x, scroll_y):
  288.     if scroll_y > 0:
  289.         camera.goto_x(get_next_section())
  290.     if scroll_y < 0:
  291.         camera.goto_x(get_prev_section())
  292.        
  293. window.set_visible(visible=True)
  294. pyglet.app.run()