Event Handling Mouse dan Keyboard

Event Handling Mouse dan Keyboard Menggunakan PyOpenGL


       OpenGL menyediakan fungsi yang didefinisikan oleh user dan dipanggil pada saat ada event yang bersesuaian. OpenGL memonitor peralatan input, ketika ada sesuatu yang terjadi,misalkan saat mouse down,key press,dan lain-lain.
Berikut ini merupakan sebuah persegi empat yang dapat digerakan menggunakan tombol
keyboard (keyup, keydown, keyright, key left), tombol mouse kiri dan kanan untuk merubah warna persegi empat, dimana pada saat persegi empat digerakan pada posisi tertentu warna background dapat berubah-ubah.

Berikut sourcode yang digunakan :
from OpenGL.GL import *
from OpenGL.GLU import *
from OpenGL.GLUT import *

# Koordinat x dan y
pos_x = 0
pos_y = 0

# Warna Kotak
biru = 0
hijau = 0
merah = 0


def init():
glClearColor(0.0, 0.0, 0.0, 1.0)
gluOrtho2D(-500.0, 500.0, -500.0, 500.0)

def kotak():
global pos_x, pos_y
glColor3f(merah,hijau,biru)
glBegin(GL_POLYGON)
# Kiri Atas
glVertex2f(-50 + pos_x,-50 + pos_y)
# Kanan Atas
glVertex2f(50 + pos_x,-50 + pos_y)
# Kanan Bawah
glVertex2f(50 + pos_x,50 + pos_y)
# Kiri Bawah
glVertex2f(-50 + pos_x,50 + pos_y)
glEnd()


def display():
glClear(GL_COLOR_BUFFER_BIT)
glColor3f(1.,1.0,1.0)
glBegin(GL_LINES)
glVertex2f(-500.0, 0.0)
glVertex2f(500.0, 0.0)
glVertex2f(0.0, 500.0)
glVertex2f(0.0, -500.0)
glEnd()

glPushMatrix()
kotak()
glPopMatrix()


glFlush()

def input_mouse(button, state, x, y):
global merah, hijau, biru
if button == GLUT_RIGHT_BUTTON and state == GLUT_DOWN:
if hijau < 1:
merah = 0
hijau = 1
biru = 0
elif biru < 1:
merah = 0
hijau = 0
biru = 1
print("Klik Kanan ditekan ", "(", x, ",", y, ")")
elif button == GLUT_LEFT_BUTTON and state == GLUT_DOWN:
if merah < 1:
merah = 1
hijau = 0
biru = 0
else:
merah = 0
hijau = 0
biru = 0
print("Klik Kiri ", "(", x, ",", y, ")")

def input_keyboard(key,x,y):
global pos_x, pos_y

# Untuk mengubah posisi kotak
if key == GLUT_KEY_UP:
pos_y += 5
print("Tombol Atas ", "x : ", pos_x, " y : ", pos_y)
elif key == GLUT_KEY_DOWN:
pos_y -= 5
print("Tombol Bawah ", "x : ", pos_x, " y : ", pos_y)
elif key == GLUT_KEY_RIGHT:
pos_x += 5
print("Tombol Kanan ", "x : ", pos_x, " y : ", pos_y)
elif key == GLUT_KEY_LEFT:
pos_x -= 5
print("Tombol Kiri ", "x : ", pos_x, " y : ", pos_y)

# Untuk Mengubah Warna backgorund window
if pos_x < 0 and pos_y > 0:
glClearColor(1.0, 0.0, 0.0, 1.0)
if pos_x > 0 and pos_y > 0:
glClearColor(0.0, 1.0, 0.0, 1.0)
if pos_x > 0 and pos_y < 0:
glClearColor(0.0,0.0,1.0,1.0)
if pos_x < 0 and pos_y < 0:
glClearColor(0.0,0.0,0.0,1.0)


def update(value):
glutPostRedisplay()
glutTimerFunc(10,update,0)

def main():
glutInit(sys.argv)
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB)
glutInitWindowSize(500,500)
glutInitWindowPosition(100,100)
glutCreateWindow("Praktikum8")
glutDisplayFunc(display)

glutSpecialFunc(input_keyboard)
glutMouseFunc(input_mouse)

glutTimerFunc(50, update, 0)


init()
glutMainLoop()
main()

Maka output yang dihasilkan seperti berikut :





Komentar