shithub: tinygl

Download patch

ref: 508ec7281fc86c62c845e908096aeca7aeed0431
parent: a2713e4763320b7aeeffcc57770286451938ef7b
author: David <gek@katherine>
date: Wed Mar 3 08:30:47 EST 2021

Update OpenIMGUI standard

--- a/SDL_Examples/include/openimgui.h
+++ b/SDL_Examples/include/openimgui.h
@@ -77,6 +77,7 @@
 float omg_cursorpos[2]; //Defaults to zero
 float omg_cursorpos_presuck[2]; //Defaults to zero
 int omg_cursor_has_been_sucked;
+int omg_cursor_was_inside;  //Set 
 float omg_buttonjump[2]; //Defaults to zero
 // Setting for users using 
 
@@ -98,22 +99,35 @@
 	return f;
 }
 
-static inline void omg_update_keycursor(int up, int down, int left, int right, int bstate){
+static inline void omg_update_keycursor(int _up, int _down, int _left, int _right, int bstate){
 	static int bstate_old = 0;
+	static int udlr_old[4] = {0,0,0,0};
+	omg_cursor_was_inside = 0;
+	int up = _up && ! udlr_old[0];
+	int down = _down && ! udlr_old[1];
+	int left = _left && ! udlr_old[2];
+	int right = _right && ! udlr_old[3];
+	udlr_old[0] = _up;
+	udlr_old[1] = _down;
+	udlr_old[2] = _left;
+	udlr_old[3] = _right;
 	omg_cursor_has_been_sucked = 0;
-	if(left) omg_cursorpos[0] -= omg_buttonjump[0];
-	if(right) omg_cursorpos[0] += omg_buttonjump[0];
-	if(up) omg_cursorpos[1] -= omg_buttonjump[1];
+	omg_cursorpos_presuck[0] = omg_cursorpos[0];
+	omg_cursorpos_presuck[1] = omg_cursorpos[1];
+	if(up)   omg_cursorpos[1] -= omg_buttonjump[1];
 	if(down) omg_cursorpos[1] += omg_buttonjump[1];
+	if(left) omg_cursorpos[0] -= omg_buttonjump[0];
+	if(right)omg_cursorpos[0] += omg_buttonjump[0];
+	
 	//Clamp the cursorpos
 	omg_cursorpos[0] = omg_wrapf(omg_cursorpos[0]);
 	omg_cursorpos[1] = omg_wrapf(omg_cursorpos[1]);
 	omg_cursorpos_presuck[0] = omg_cursorpos[0];
 	omg_cursorpos_presuck[1] = omg_cursorpos[1];
-
-	//omb_cb = 0; 
+	//printf("BEGIN! Cx = %f, Cy = %f\n", omg_cursorpos[0], omg_cursorpos[1]);
+	omg_cb = 0; 
 	if(bstate && !bstate_old) omg_cb = 1;
-	else omg_cb = 0;
+	else if (!bstate && bstate_old) omg_cb = 2;
 	bstate_old = bstate;
 }
 
@@ -121,6 +135,7 @@
 static inline void omg_update_mcursor(float ncx, float ncy, int bstate){
 	static int bstate_old = 0;
 	omg_cursor_has_been_sucked = 0;
+	omg_cursor_was_inside = 0;
 	omg_cursorpos[0] = ncx;
 	omg_cursorpos[1] = ncy;
 	// Clamp the cursorpos (if necessary)
@@ -149,15 +164,29 @@
 }
 static inline void omg_box_suck(float x, float y, float xdim, float ydim, int sucks, float buttonjumpx, float buttonjumpy){
 	 if(omg_cursorpos_presuck[0] != -1 && sucks){ //Do not attempt to suck if this graphical element does not suck or sucking is not enabled.
+		int btest = 0;
 		 if(!omg_cursor_has_been_sucked){
 		 	//We are free to try to suck up the cursor without a check.
 			omg_cursorpos[0] = x + xdim/2.0;
 			omg_cursorpos[1] = y + ydim/2.0;
 			omg_cursor_has_been_sucked = 1;
-		  omg_buttonjump[0] = buttonjumpx;
-		  omg_buttonjump[1] = buttonjumpy;
-		} else if (omg_sqrlinelength(x+xdim/2.0, y+ydim/2.0, omg_cursorpos_presuck[0],  omg_cursorpos_presuck[1]) < 
-		           omg_sqrlinelength(omg_cursorpos[0], omg_cursorpos[1], omg_cursorpos_presuck[0],  omg_cursorpos_presuck[1])){ 
+		  	omg_buttonjump[0] = buttonjumpx;
+		  	omg_buttonjump[1] = buttonjumpy;
+		  	if(omg_boxtest(x,y,xdim,ydim, omg_cursorpos_presuck[0], omg_cursorpos_presuck[1])) omg_cursor_was_inside = 1;
+		  	//puts("Initial grab...\n");
+		  	//printf("Cx = %f, Cy = %f\n", omg_cursorpos[0], omg_cursorpos[1]);
+		} else if (
+		(!omg_cursor_was_inside && //Cursor was not inside.
+		omg_sqrlinelength(x+xdim/2.0, y+ydim/2.0, 			omg_cursorpos_presuck[0],  omg_cursorpos_presuck[1]) < 
+		           omg_sqrlinelength(omg_cursorpos[0], omg_cursorpos[1], omg_cursorpos_presuck[0],  omg_cursorpos_presuck[1])
+		 ) || //Cursor was inside, if it's inside this one as well, pick the closest.
+		 (!omg_cursor_was_inside && omg_boxtest(x,y,xdim,ydim, omg_cursorpos_presuck[0], omg_cursorpos_presuck[1])) ||
+		  (
+		  (omg_boxtest(x,y,xdim,ydim, omg_cursorpos_presuck[0], omg_cursorpos_presuck[1])) && 
+			omg_sqrlinelength(x+xdim/2.0, y+ydim/2.0, 			omg_cursorpos_presuck[0],  omg_cursorpos_presuck[1]) < 
+		    omg_sqrlinelength(omg_cursorpos[0], omg_cursorpos[1], omg_cursorpos_presuck[0],  omg_cursorpos_presuck[1])
+		  )
+		           ){
 		           //The box is closer than the current suck position.
 			omg_cursorpos[0] = x+xdim/2.0;
 			omg_cursorpos[1] = y+ydim/2.0;
@@ -164,6 +193,10 @@
 			omg_cursor_has_been_sucked = 1;
 		  omg_buttonjump[0] = buttonjumpx;
 		  omg_buttonjump[1] = buttonjumpy;
+		  //if(boxtest(x,y,xdim,ydim)) omg_cursor_was_inside = 1;
+		  omg_cursor_was_inside = omg_boxtest(x,y,xdim,ydim, omg_cursorpos_presuck[0], omg_cursorpos_presuck[1]);
+		  //puts("Found a different button!\n");
+		  //printf("Cx = %f, Cy = %f\n", omg_cursorpos[0], omg_cursorpos[1]);
 		}
 	}
 }
--- a/SDL_Examples/menu.c
+++ b/SDL_Examples/menu.c
@@ -96,7 +96,10 @@
 		glColor3f(0.7,0.7,0.7);
 	else
 		glColor3f(1.0,0.1,0.1);
-	drawBox(omg_cursorpos[0],omg_cursorpos[1], 0.03, 0.03);
+	if(!using_cursorkeys)
+		drawBox(omg_cursorpos[0],omg_cursorpos[1], 0.03, 0.03);
+	else
+		drawBox(omg_cursorpos_presuck[0],omg_cursorpos_presuck[1], 0.03, 0.03);
 }
 
 void drawTB(const char* text, GLuint textcolor, GLfloat x, GLfloat y, GLint size, float* tw, float* th){
@@ -153,8 +156,16 @@
 		tbcoords.d[1] =  omg_cursorpos[1];
 		haveclicked = 0;
 	}
+
+	if(omg_textbox(0.01, 0,   "\nEntry 1\n", 24, 1,   0.4, 0.2, 0xFFFFFF, 0) && omg_cb == 2 )
+		puts("Entry 1");
+	if(omg_textbox(0.01, 0.2,   "\nEntry 2\n", 24, 1,   0.4, 0.2, 0xFFFFFF, 0) && omg_cb == 2)
+		puts("Entry 2");
+	if(omg_textbox(0.01, 0.4,   "\nEntry 3\n", 24, 1,   0.4, 0.2, 0xFFFFFF, 0) && omg_cb == 2)
+		puts("Entry 3");
+	
 	if(
-	omg_textbox(tbcoords.d[0], tbcoords.d[1], "Click me and I toggle color!", 16, 1, 20, 20, 0xFFFFFF, haveclicked?0xFF0000:0x00) && omg_cb == 1)
+	omg_textbox(tbcoords.d[0], tbcoords.d[1], "\nClick me and I toggle color!\n", 16, 1, 0.4, 0.3, 0xFFFFFF, haveclicked?0xFF0000:0x00) && omg_cb == 1)
 		{puts("Detected click! EVENT FIRED!\n");haveclicked = !haveclicked; }
 	drawMouse();
 }
@@ -206,10 +217,15 @@
 		}
 	break;
 	case SDL_KEYUP:
+		using_cursorkeys = 1;
 		switch(E_KEYSYM){
 			case SDLK_SPACE: case SDLK_RETURN:
 				mb = 0;
 			break;
+			case SDLK_UP:   dirbstates[0] = 0; break;
+			case SDLK_DOWN: dirbstates[1] = 0; break;
+			case SDLK_LEFT: dirbstates[2] = 0; break;
+			case SDLK_RIGHT:dirbstates[3] = 0; break;
 			default: break;
 		}
 	break;