MAUS YÖNETİMİ
Yaptığımız uygulamalarımızı kontrol etmenin iki yolu vardır,
1- Maus yardımı ile
2- Klavye yardımı ile
Uygulamaları oluştururken kullanmış olduğumuz yardımcı kütüphanelerin farklı
olmasına rağmen maus yönetimleri temelde birbirine benzemektedir.
Biz burada glut yada freeglut kullanarak maus kontrelleri hakkında oldukça faydalı
bir bilgi edinmiş olacağız.
Ben burada internetten bulmuş olduğum bir dersi sizlerle paylaşarak anlatmaya çalışacağım.
Kodu aynen veriyorum. Kod yanlarında açıklamalar yapmaya çalıştım.
/// Arkadaşlar bu alıntı ders bize maus yönetimi
/// hakkında çok güzel bir anlatım sunuyor.
/// Bilgiyi paylaşmanın güzelliğini ve kıymetini
/// insan daha iyi anlıyor.
/// By Grant James'e teşekkür ediyor ve mutlu bir yaşam diliyoruz.
/********************************************
* Zeus CMD - GLUT Tutorial 03 : Mouse Input *
* By Grant James (ZEUS) *
* http://www.zeuscmd.com *
********************************************/
/// kod başlangıç
#include <iostream>
#include <gl/glut.h>
using namespace std;
bool lbuttonDown = false;
bool rbuttonDown = false;
bool init()
{
return true;
}
void display()
{
/// bos bir dünya sahnesi
}
/// 1. maus yönetimi
void mouse(int button, int state, int x, int y)// butonlar, durum ve xy konum
{
if (button == GLUT_RIGHT_BUTTON)// sag buton
{
if (state == GLUT_DOWN)// buton basılı
cout << " Sag dugme basildi"<< endl;
else// buton yukarda
cout << "Sag dugme birakildi "
<< " (" << x << "," << y<< ")" << endl;
}
else if (button == GLUT_LEFT_BUTTON)// sol buton
{
if (state == GLUT_DOWN)// buton basılı
lbuttonDown = true;// basılı iken motion fonksiyununu aktif yap. Bir..
else
lbuttonDown = false;//... aç kapa anahtar
}
if (state == GLUT_UP) lbuttonDown = false; cout << "sol tus pasif "<< endl;
}
/// 2. maus yönetimi
void motion(int x, int y)// haraket yakalama. Bir üstteki
// void mouse(int button, int state, int x, int y) fonksiyonunun
//hareketlerini yakalayarak kullanıcıya geri döndürür.
// Hangi buton sağ sol gibi ve bunlar basılımı değilmi gibi
//vede xy konumlarını döndürmek gibi
{
if (lbuttonDown)// anahtar aktif
cout << "sol tus surukleniyor "
<< "(" << x << "," << y << ")" << endl;
}
/// 3. maus yönetimi
void motionPassive(int x, int y)// maus pasif durumda
{
cout << "fare geziyor "
<< "(" << x << "," << y << ")" << endl;
}
/// 4. maus yönetimi
void entry(int state)// maus oku ekranımızın sınırları içindemi yoksa değilmi dönüşünü verir
{
if (state == GLUT_ENTERED)
cout << "Fare giris yapti" << endl;//oluşan ekranımıza tıkladığımızda bu yazıyı görürüz
else
cout << "Fare ayrildi" << endl;// ekran dışında bir yere tıkladığımızda ise bu yazıyı görürüz
}
int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitWindowPosition(200, 200);
glutInitWindowSize(200, 200);
glutCreateWindow("03 - Mouse Input");
glutDisplayFunc(display);
glutMouseFunc(mouse);
glutMotionFunc(motion);
glutPassiveMotionFunc(motionPassive);
glutEntryFunc(entry);
if (!init())return 1;
glutMainLoop();
return 0;
}/// kod sonu
*********************************************************************************
Maus ve Buton ikilisi: By Grant James' den aldığımız bilgilerle birkaç buton denemesi yapalım.
3-Örnek Buton 1
///----Buton 1 kodu --------------------
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>
#include <stdlib.h>
#include <iostream>
#include <iomanip>
using namespace std;
void init();
void display();
int alan=6;/// 100*6 600x lik pencere
int winx=300; /// 600x lik pencerenin ortası
float korx, wx;
void buton(){
glPushMatrix();
glColor3f(1.0, 0.0, 0.0);
glTranslatef(korx,0,0);
glScalef(1,15,1);/// burada y değerinin 15 olması pencere yüksekliğinin küçük olmasıdır,
/// bu şekilde küpümüz normal gözükecektir.
glutSolidCube(0.1); /// her türlü nesne olabilir biz burada küp seçtik.
glPopMatrix();
}
void init(){
glClearColor(1.0,1.0,1.0,1.0);
gluOrtho2D(0.0, 600.0, 0.0, 600.0);
}
void display(){
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
buton();
glutSwapBuffers();
}
void motion(int nx, int ny)
{
wx =(winx- nx)*(-0.02);
korx=wx/alan; /// pencere koordinatlarını kartezyen koor. çevirme
cout<<"Penx:"<<nx<<" korx:"<< setprecision(2)<<korx<<endl;
if(korx<=-0.94)korx=-0.94;
if(korx>=0.94)korx=0.94;
glutPostRedisplay();
}
void klavye (unsigned char k, int x, int y)
{
if(k==27) exit(0);
}
int main(int argc, char* argv[]){
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);
glutInitWindowSize(600,50);
glutInitWindowPosition(300,300);
glutCreateWindow("OpenGL Program");
init();
glutDisplayFunc(display);
glutMotionFunc(motion);
glutKeyboardFunc( klavye);
glutMainLoop();
return 0;
}
///------------------Buton 1 kod sonu-------------------------------------------------
glutSolidCube(0.1); /// her türlü nesne olabilir biz burada küp seçtik.
glPopMatrix();
}
void init(){
glClearColor(1.0,1.0,1.0,1.0);
gluOrtho2D(0.0, 600.0, 0.0, 600.0);
}
void display(){
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
buton();
glutSwapBuffers();
}
void motion(int nx, int ny)
{
wx =(winx- nx)*(-0.02);
korx=wx/alan; /// pencere koordinatlarını kartezyen koor. çevirme
cout<<"Penx:"<<nx<<" korx:"<< setprecision(2)<<korx<<endl;
if(korx<=-0.94)korx=-0.94;
if(korx>=0.94)korx=0.94;
glutPostRedisplay();
}
void klavye (unsigned char k, int x, int y)
{
if(k==27) exit(0);
}
int main(int argc, char* argv[]){
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);
glutInitWindowSize(600,50);
glutInitWindowPosition(300,300);
glutCreateWindow("OpenGL Program");
init();
glutDisplayFunc(display);
glutMotionFunc(motion);
glutKeyboardFunc( klavye);
glutMainLoop();
return 0;
}
///------------------Buton 1 kod sonu-------------------------------------------------
*********************************************************************************
4- Örnek Buton 2:
///------------------------------------
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>
#include <stdlib.h>
#include <iostream>
#include <iomanip> ///
using namespace std;
void init();
void display();
int alan=6;/// 100*6 600x lik pencere
int winx=300; /// 600x lik pencerenin ortası
float bred=0.4;
float korx, wx;
void buton(){
glPushMatrix();
glColor3f(bred, 0.0, 0.0);
glTranslatef(korx,0,0);
glScalef(1,15,1);
glutSolidCube(0.1); /// her türlü nesne olabilir (glpoint)
glPopMatrix();
}
void lines(){
glColor3f(bred, 0.0, 0.0);
glLineWidth(10.0);
glPopMatrix();
glBegin(GL_LINES);
glVertex2f(-1,0);
glVertex2f(korx,0); glColor3f(0.3,0.3,0.3);
glEnd();
glPopMatrix();
glutPostRedisplay();
}
void init(){
glClearColor(1.0,1.0,1.0,1.0);
gluOrtho2D(0.0, 600.0, 0.0, 600.0);
}
void display(){
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
lines();
buton();
glutSwapBuffers();
}
void mausKullan(int button, int state, int x, int y)
{
if (button == GLUT_LEFT_BUTTON) {
if (state == GLUT_DOWN) { // left mouse button pressed
bred=1;
}else bred=0.5;
}}
void motion(int nx, int ny)
{
wx =(winx- nx)*(-0.02);
korx=wx/alan; /// pencere koordinatlarını kartezyen koor. çevirme
cout<<"Penx:"<<nx<<" korx:"<< setprecision(2)<<korx<<endl; ///
if(korx<=-0.94)korx=-0.94;
if(korx>=0.94)korx=0.94;
glutPostRedisplay();
}
void ess (unsigned char k, int x, int y)
{
if(k==27) exit(0);
}
int main(int argc, char* argv[]){
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);
glutInitWindowSize(600,50);/// y attırılabilir
glutInitWindowPosition(300,300);
glutCreateWindow("OpenGL Program");
init();
glutDisplayFunc(display);
glutMouseFunc(mausKullan);
glutMotionFunc(motion);
glutKeyboardFunc(ess);
glutMainLoop();
return 0;
}
4- Örnek Buton 2:
///------------------------------------
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>
#include <stdlib.h>
#include <iostream>
#include <iomanip> ///
using namespace std;
void init();
void display();
int alan=6;/// 100*6 600x lik pencere
int winx=300; /// 600x lik pencerenin ortası
float bred=0.4;
float korx, wx;
void buton(){
glPushMatrix();
glColor3f(bred, 0.0, 0.0);
glTranslatef(korx,0,0);
glScalef(1,15,1);
glutSolidCube(0.1); /// her türlü nesne olabilir (glpoint)
glPopMatrix();
}
void lines(){
glColor3f(bred, 0.0, 0.0);
glLineWidth(10.0);
glPopMatrix();
glBegin(GL_LINES);
glVertex2f(-1,0);
glVertex2f(korx,0); glColor3f(0.3,0.3,0.3);
glEnd();
glPopMatrix();
glutPostRedisplay();
}
void init(){
glClearColor(1.0,1.0,1.0,1.0);
gluOrtho2D(0.0, 600.0, 0.0, 600.0);
}
void display(){
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
lines();
buton();
glutSwapBuffers();
}
void mausKullan(int button, int state, int x, int y)
{
if (button == GLUT_LEFT_BUTTON) {
if (state == GLUT_DOWN) { // left mouse button pressed
bred=1;
}else bred=0.5;
}}
void motion(int nx, int ny)
{
wx =(winx- nx)*(-0.02);
korx=wx/alan; /// pencere koordinatlarını kartezyen koor. çevirme
cout<<"Penx:"<<nx<<" korx:"<< setprecision(2)<<korx<<endl; ///
if(korx<=-0.94)korx=-0.94;
if(korx>=0.94)korx=0.94;
glutPostRedisplay();
}
void ess (unsigned char k, int x, int y)
{
if(k==27) exit(0);
}
int main(int argc, char* argv[]){
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);
glutInitWindowSize(600,50);/// y attırılabilir
glutInitWindowPosition(300,300);
glutCreateWindow("OpenGL Program");
init();
glutDisplayFunc(display);
glutMouseFunc(mausKullan);
glutMotionFunc(motion);
glutKeyboardFunc(ess);
glutMainLoop();
return 0;
}
///------------------Buton 2 kod sonu-------------------------------------------------
********************************************************************************
5- Örnek Buton 3:
///------------------------------------------
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
#include <windows.h>
#include <iomanip> /// setpraction (x)
using namespace std;
static char bas[20];
void init();
void display();
int alam=4;/// x lik pencere oranı
int winx=4;/// konum maus hizalama
int winx2=4;
float bred=0.3;
float korx, wxm;
short mk,mk2;
float bred2=0.3;
float korx2, wxm2;
void rakam();
void harfYazak(char *z)
{
unsigned int i;
for (i = 0; i < strlen (z); i++)
glutBitmapCharacter (GLUT_BITMAP_HELVETICA_18, z[i]);
}
void rakam(){
glColor3f(bred,0,0);
sprintf (bas," %1.0f ",korx*100);
glRasterPos3f (0.20, 0.42,0);
harfYazak (bas);
glColor3f(bred2,0,0);
sprintf (bas," %1.0f ",korx2*100);
glRasterPos3f (0.20, -0.55,0);
harfYazak (bas);
}
void buton(){
glPushMatrix();
glColor3f(bred, 0.0, 0.0);
glTranslatef(-0.98,0.5,0);
glPointSize(10.0);
glBegin(GL_POINTS);
glVertex2f(korx,0.0);
glEnd();
glPopMatrix();
}
void lines(){
glColor3f(bred, 0.0, 0.0);
glPushMatrix();
glTranslatef(-0.98,0.0,0);
glLineWidth(3.0);
glBegin(GL_LINES);
glVertex2f(0,0.5);
glVertex2f(korx,0.5); glColor3f(0.3,0.3,0.3);
glEnd();
glPopMatrix();
glutPostRedisplay();
}
///-----------------------------
void buton2(){
glPushMatrix();
glColor3f(bred2, 0.0, 0.0);
glTranslatef(-0.98,-0.5,0);
glPointSize(10.0);
glBegin(GL_POINTS);
glVertex2f(korx2,0.0);
glEnd();
glPopMatrix();
}
void lines2(){
glColor3f(bred2, 0.0, 0.0);
glPushMatrix();
glTranslatef(-0.98,0.0,0);
glLineWidth(3.0);
glBegin(GL_LINES);
glVertex2f(0,-0.5);
glVertex2f(korx2,-0.5); glColor3f(0.3,0.3,0.3);
glEnd();
glPopMatrix();
glutPostRedisplay();
}
void fon(){
glPushMatrix();
glTranslatef(0,0.0,0);
glColor3f(0.5,0.5,0.5);
glBegin(GL_QUADS); /// arka fon
glVertex2f(-1.0,-1.0);
glVertex2f( 1.0,-1.0);
glVertex2f( 1.0, 1.0);
glVertex2f(-1.0, 1.0);
glEnd();
glColor3f(0.8,0.8,0.8);
glBegin(GL_QUADS); /// buton 1
glVertex2f(-0.98, 0.95);
glVertex2f( 0.98, 0.95);
glVertex2f( 0.98, 0.05);
glVertex2f(-0.98, 0.05);
glEnd();
glBegin(GL_QUADS); /// buton 2
glVertex2f(-0.98,- 0.95);
glVertex2f( 0.98, -0.95);
glVertex2f( 0.98, -0.05);
glVertex2f(-0.98, -0.05);
glEnd();
glPopMatrix();
glPushMatrix(); /// orta line
glLineWidth(1.0);
glColor3f(0.65,0.65,0.65);
glBegin(GL_LINES);
glVertex2f(-0.98,0.0);
glVertex2f( 0.98,0.0);
glEnd();
glPopMatrix();
}
///------------------------------
void init(){
glClearColor(1.0,1.0,1.0,1.0);
gluOrtho2D(0.0, 600.0, 0.0, 600.0);
}
void display(){
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
fon();
lines();
buton();
lines2();
buton2();
rakam();
glutSwapBuffers();
}
void mausKullan(int button, int state, int x, int y)
{
if (button == GLUT_LEFT_BUTTON) {
if (state == GLUT_DOWN) { // left mouse button pressed
if(y<=74&&x<=200) {bred=1;}
if(y>=76&&x<=200) {bred2=1; }
} else {bred=0.3;bred2=0.3; }
glutPostRedisplay();
}}
void pasive_mouseTut(int x,int y){
if(y<=74&&x<=210) bred=0.65; else bred=0.3;
if(y>=76&&x<=210){ bred2=0.65;} else bred2=0.3;
/// glutPostRedisplay();
}
void mouseHIZ(int nx, int ny)
{
if(ny<=74)mk=1;
if(mk==1) {
if(mk==1)mk=0;else mk=1;
wxm =(winx- nx)*(-0.02);
korx=wxm/alam;
if(korx<=0.0)korx=0.0;
if(korx>=1.0)korx=1.0;}
if(ny>=76)mk2=1;
if(mk2==1){
if(mk2==1)mk2=0;else mk2=1;
wxm2 =(winx2- nx)*(-0.02);
korx2=wxm2/alam;
if(korx2<=0.0)korx2=0.0;
if(korx2>=1.0)korx2=1.0; }
///cout<<"Penx:"<<nx<<" korx:"<<korx<<" k2:"<<korx2<<endl;
glutPostRedisplay();
}
void ess (unsigned char k, int x, int y)
{
if(k==27) exit(0);
}
int main(int argc, char* argv[]){
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);
glutInitWindowSize(400,150);/// y attırılabilir
glutInitWindowPosition(300,300);
glutCreateWindow("OpenGL Program");
init();
glutDisplayFunc(display);
glutMouseFunc(mausKullan);
glutPassiveMotionFunc(pasive_mouseTut);
glutMotionFunc(mouseHIZ);
glutKeyboardFunc(ess);
glutMainLoop();
return 0;
}
5- Örnek Buton 3:
///------------------------------------------
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
#include <windows.h>
#include <iomanip> /// setpraction (x)
using namespace std;
static char bas[20];
void init();
void display();
int alam=4;/// x lik pencere oranı
int winx=4;/// konum maus hizalama
int winx2=4;
float bred=0.3;
float korx, wxm;
short mk,mk2;
float bred2=0.3;
float korx2, wxm2;
void rakam();
void harfYazak(char *z)
{
unsigned int i;
for (i = 0; i < strlen (z); i++)
glutBitmapCharacter (GLUT_BITMAP_HELVETICA_18, z[i]);
}
void rakam(){
glColor3f(bred,0,0);
sprintf (bas," %1.0f ",korx*100);
glRasterPos3f (0.20, 0.42,0);
harfYazak (bas);
glColor3f(bred2,0,0);
sprintf (bas," %1.0f ",korx2*100);
glRasterPos3f (0.20, -0.55,0);
harfYazak (bas);
}
void buton(){
glPushMatrix();
glColor3f(bred, 0.0, 0.0);
glTranslatef(-0.98,0.5,0);
glPointSize(10.0);
glBegin(GL_POINTS);
glVertex2f(korx,0.0);
glEnd();
glPopMatrix();
}
void lines(){
glColor3f(bred, 0.0, 0.0);
glPushMatrix();
glTranslatef(-0.98,0.0,0);
glLineWidth(3.0);
glBegin(GL_LINES);
glVertex2f(0,0.5);
glVertex2f(korx,0.5); glColor3f(0.3,0.3,0.3);
glEnd();
glPopMatrix();
glutPostRedisplay();
}
///-----------------------------
void buton2(){
glPushMatrix();
glColor3f(bred2, 0.0, 0.0);
glTranslatef(-0.98,-0.5,0);
glPointSize(10.0);
glBegin(GL_POINTS);
glVertex2f(korx2,0.0);
glEnd();
glPopMatrix();
}
void lines2(){
glColor3f(bred2, 0.0, 0.0);
glPushMatrix();
glTranslatef(-0.98,0.0,0);
glLineWidth(3.0);
glBegin(GL_LINES);
glVertex2f(0,-0.5);
glVertex2f(korx2,-0.5); glColor3f(0.3,0.3,0.3);
glEnd();
glPopMatrix();
glutPostRedisplay();
}
void fon(){
glPushMatrix();
glTranslatef(0,0.0,0);
glColor3f(0.5,0.5,0.5);
glBegin(GL_QUADS); /// arka fon
glVertex2f(-1.0,-1.0);
glVertex2f( 1.0,-1.0);
glVertex2f( 1.0, 1.0);
glVertex2f(-1.0, 1.0);
glEnd();
glColor3f(0.8,0.8,0.8);
glBegin(GL_QUADS); /// buton 1
glVertex2f(-0.98, 0.95);
glVertex2f( 0.98, 0.95);
glVertex2f( 0.98, 0.05);
glVertex2f(-0.98, 0.05);
glEnd();
glBegin(GL_QUADS); /// buton 2
glVertex2f(-0.98,- 0.95);
glVertex2f( 0.98, -0.95);
glVertex2f( 0.98, -0.05);
glVertex2f(-0.98, -0.05);
glEnd();
glPopMatrix();
glPushMatrix(); /// orta line
glLineWidth(1.0);
glColor3f(0.65,0.65,0.65);
glBegin(GL_LINES);
glVertex2f(-0.98,0.0);
glVertex2f( 0.98,0.0);
glEnd();
glPopMatrix();
}
///------------------------------
void init(){
glClearColor(1.0,1.0,1.0,1.0);
gluOrtho2D(0.0, 600.0, 0.0, 600.0);
}
void display(){
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
fon();
lines();
buton();
lines2();
buton2();
rakam();
glutSwapBuffers();
}
void mausKullan(int button, int state, int x, int y)
{
if (button == GLUT_LEFT_BUTTON) {
if (state == GLUT_DOWN) { // left mouse button pressed
if(y<=74&&x<=200) {bred=1;}
if(y>=76&&x<=200) {bred2=1; }
} else {bred=0.3;bred2=0.3; }
glutPostRedisplay();
}}
void pasive_mouseTut(int x,int y){
if(y<=74&&x<=210) bred=0.65; else bred=0.3;
if(y>=76&&x<=210){ bred2=0.65;} else bred2=0.3;
/// glutPostRedisplay();
}
void mouseHIZ(int nx, int ny)
{
if(ny<=74)mk=1;
if(mk==1) {
if(mk==1)mk=0;else mk=1;
wxm =(winx- nx)*(-0.02);
korx=wxm/alam;
if(korx<=0.0)korx=0.0;
if(korx>=1.0)korx=1.0;}
if(ny>=76)mk2=1;
if(mk2==1){
if(mk2==1)mk2=0;else mk2=1;
wxm2 =(winx2- nx)*(-0.02);
korx2=wxm2/alam;
if(korx2<=0.0)korx2=0.0;
if(korx2>=1.0)korx2=1.0; }
///cout<<"Penx:"<<nx<<" korx:"<<korx<<" k2:"<<korx2<<endl;
glutPostRedisplay();
}
void ess (unsigned char k, int x, int y)
{
if(k==27) exit(0);
}
int main(int argc, char* argv[]){
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);
glutInitWindowSize(400,150);/// y attırılabilir
glutInitWindowPosition(300,300);
glutCreateWindow("OpenGL Program");
init();
glutDisplayFunc(display);
glutMouseFunc(mausKullan);
glutPassiveMotionFunc(pasive_mouseTut);
glutMotionFunc(mouseHIZ);
glutKeyboardFunc(ess);
glutMainLoop();
return 0;
}
///------------------------------------------------------------------------------------------------
Hiç yorum yok:
Yorum Gönder