Created
October 1, 2014 13:43
-
-
Save xtai/2ea2073c26a6a8afcf9a to your computer and use it in GitHub Desktop.
DMS423_HW03-1_Xiaoyu
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# DMS423_HW03-1_Xiaoyu.py | |
# | |
# Homework 03-1 for DMS 423 Fall 14 | |
# by Xiaoyu Tai(Sean) | |
# UBID: 50133396 | |
# | |
# | |
# Paint Program | |
# Normal Function | |
# | |
from pyglet.gl import * | |
from pyglet.window import key, mouse | |
import math | |
window = pyglet.window.Window(600, 600) | |
keys = key.KeyStateHandler() | |
window.push_handlers(keys) | |
mouse_position = [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 background | |
glClear(pyglet.gl.GL_COLOR_BUFFER_BIT) | |
glLoadIdentity() | |
glPushMatrix() | |
glColor3f(1, 1, 1) | |
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 | |
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 | |
if mouse_position[1] <= 50: | |
for x in xrange(0, 7): | |
if mouse_position[0] >= (244+x*51) and mouse_position[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 | |
mouse_position[1] = y | |
@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, y, current_line_color[0], current_line_color[1], current_line_color[2]]) | |
else: | |
current_line = 1 | |
start_point = [x, y] | |
current_line_color = current_color | |
data.append([x, y, current_line_color[0], current_line_color[1], current_line_color[2]]) | |
print current_color, current_line_color | |
elif button == mouse.RIGHT: | |
if current_line: | |
current_line = 0 | |
data.pop() | |
elif len(data) >= 2: | |
data.pop() | |
data.pop() | |
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