Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
DMS423_HW03-2_Xiaoyu
# DMS423_HW03-2_Xiaoyu.py
#
# Homework 03-2 for DMS 423 Fall 14
# by Xiaoyu Tai(Sean)
# UBID: 50133396
#
#
# Paint Program
# Abnormal Function
#
from pyglet.gl import *
from pyglet.window import key, mouse
import math, random
window = pyglet.window.Window(600, 600)
keys = key.KeyStateHandler()
window.push_handlers(keys)
mouse_position = [0, 0, 0]
background = pyglet.graphics.vertex_list(4, ('v2f', [0,0, 0,600, 600,600, 600,0]))
indicator = pyglet.graphics.vertex_list(4, ('v2f', [0,0, 0,50, 50,50, 50,0]))
selected_arrow = pyglet.graphics.vertex_list(3, ('v2f', [20,0, 25,12, 30,0]))
selected_color = -1
current_line = 0
start_point = [0, 0]
current_color = [0, 0, 0]
current_line_color = [0, 0, 0]
data = []
color_set = [[0,0,0], [1,0,0], [0,1,0], [0,0,1], [1,1,0], [1,0,1], [0,1,1]]
@window.event
def on_draw():
clear_background()
draw_line()
draw_all_lines()
draw_color_set()
update_indicator()
update_color()
update_color_set()
def update(dt):
pass
def clear_background():
global mouse_position
if mouse_position[2]:
x,y = float(mouse_position[0])/600, float(mouse_position[1])/600
else:
x,y = 1,1
glClear(pyglet.gl.GL_COLOR_BUFFER_BIT)
background = pyglet.graphics.vertex_list(6, ('v2f',
[mouse_position[0],mouse_position[1], 0,0, 0,600, 600,600, 600,0, 0,0]), ('c3f', [
1,1,1, 1,x,y, y,x,1, x,y,1, x,1,y, 1,x,y]))
glLoadIdentity()
glPushMatrix()
background.draw(GL_TRIANGLE_FAN)
glPopMatrix()
def update_indicator():
global current_color, indicator
# Color indicator
glPushMatrix()
glColor3f(current_color[0], current_color[1], current_color[2])
indicator.draw(GL_TRIANGLE_FAN)
glPopMatrix()
def draw_color_set():
global color_set, current_color, indicator, selected_arrow, selected_color
for i in xrange(0, len(color_set)):
x = color_set[i]
glPushMatrix()
glColor3f(x[0], x[1], x[2])
glTranslatef(244+i*51, 0, 0)
indicator.draw(GL_TRIANGLE_FAN)
glPopMatrix()
glPushMatrix()
glColor3f(1,1,1)
if selected_color != -1:
glTranslatef(244+selected_color*51, 0, 0)
selected_arrow.draw(GL_TRIANGLES)
glPopMatrix()
def update_color_set():
global color_set, selected_color, current_color
if selected_color != -1:
current_color = color_set[selected_color]
def draw_line():
global mouse_position, start_point, current_line_color
if current_line:
line = pyglet.graphics.vertex_list(2, ('v2f', [start_point[0],start_point[1], mouse_position[0],mouse_position[1]]))
glPushMatrix()
glColor3f(current_line_color[0], current_line_color[1], current_line_color[2])
line.draw(GL_LINES)
glPopMatrix()
def draw_all_lines():
global data
if len(data) >= 2:
for i in xrange(1, len(data), 2):
x = data[i-1]
y = data[i]
line = pyglet.graphics.vertex_list(2, ('v2f', [x[0],x[1], y[0],y[1]]))
glPushMatrix()
glColor3f(y[2], y[3], y[4])
line.draw(GL_LINES)
glPopMatrix()
def update_color():
global keys, current_color, selected_color, mouse_position
if keys[key.R]:
update_color_x(0)
elif keys[key.G]:
update_color_x(1)
elif keys[key.B]:
update_color_x(2)
elif keys[key.SPACE]:
current_color = [0, 0, 0]
selected_color = -1
elif keys[key._1]:
selected_color = 0
elif keys[key._2]:
selected_color = 1
elif keys[key._3]:
selected_color = 2
elif keys[key._4]:
selected_color = 3
elif keys[key._5]:
selected_color = 4
elif keys[key._6]:
selected_color = 5
elif keys[key._7]:
selected_color = 6
m = mouse_position
if 600-m[1] <= 50:
for x in xrange(0, 7):
if 600-m[0] >= (244+x*51) and 600-m[0] <= (294+x*51):
selected_color = x
def update_color_x(x):
global current_color, keys, selected_color
selected_color = -1
if current_color[x] > 1:
current_color[x] = 0
elif current_color[x] < 0:
current_color[x] = 1
else:
if keys[key.LSHIFT] or keys[key.RSHIFT]:
current_color[x] -= 0.01
else:
current_color[x] += 0.01
@window.event
def on_mouse_motion(x, y, dx, dy):
global mouse_position
mouse_position[0] = -x+600+random.gauss(0,10)
mouse_position[1] = -y+600+random.gauss(0,10)
@window.event
def on_mouse_press(x, y, button, modifiers):
global current_line, start_point, current_color, current_line_color, mouse_button
if button == mouse.LEFT:
if current_line:
current_line = 0
data.append([-x+600, -y+600, current_line_color[0], current_line_color[1], current_line_color[2]])
else:
current_line = 1
start_point = [-x+600, -y+600]
current_line_color = current_color
data.append([-x+600, -y+600, current_line_color[0], current_line_color[1], current_line_color[2]])
elif button == mouse.RIGHT:
if current_line:
current_line = 0
data.pop()
elif len(data) >= 2:
data.pop()
data.pop()
@window.event
def on_mouse_leave(x, y):
global mouse_position
mouse_position[2] = 0
@window.event
def on_mouse_enter(x, y):
global mouse_position
mouse_position[2] = 1
pyglet.clock.schedule_interval(update, 1/60.0)
pyglet.app.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment