IMGUI use data from Gamestate, fix pointInRect bug
Wrong if logic causing point-in-rect to be valid if only x or y is valid, where correct logic is both x and y need to be within the rect.
This commit is contained in:
		
							parent
							
								
									8eb9068093
								
							
						
					
					
						commit
						eb1962e05e
					
				| @ -18,7 +18,7 @@ | |||||||
|     # |     # | ||||||
|     AllowAllParametersOfDeclarationOnNextLine: true, |     AllowAllParametersOfDeclarationOnNextLine: true, | ||||||
|     AllowShortBlocksOnASingleLine: false, |     AllowShortBlocksOnASingleLine: false, | ||||||
|     AllowShortIfStatementsOnASingleLine: false, |     AllowShortIfStatementsOnASingleLine: true, | ||||||
|     AllowShortLoopsOnASingleLine: false, |     AllowShortLoopsOnASingleLine: false, | ||||||
|     # |     # | ||||||
|     BinPackArguments: true, |     BinPackArguments: true, | ||||||
| @ -42,4 +42,5 @@ | |||||||
|     # |     # | ||||||
|     Cpp11BracedListStyle: true, |     Cpp11BracedListStyle: true, | ||||||
|     Standard: Cpp11, |     Standard: Cpp11, | ||||||
|  |     # | ||||||
| } | } | ||||||
|  | |||||||
| @ -432,7 +432,7 @@ void debug_drawUi(GameState *state, f32 dt) | |||||||
| 	DEBUG_PUSH_VAR("FreeEntityIndex: %d", world->freeEntityIndex, "i32"); | 	DEBUG_PUSH_VAR("FreeEntityIndex: %d", world->freeEntityIndex, "i32"); | ||||||
| 	DEBUG_PUSH_VAR("glDrawArray Calls: %d", | 	DEBUG_PUSH_VAR("glDrawArray Calls: %d", | ||||||
| 	               GLOBAL_debug.callCount[debugcallcount_drawArrays], "i32"); | 	               GLOBAL_debug.callCount[debugcallcount_drawArrays], "i32"); | ||||||
| 	DEBUG_PUSH_VAR("Mouse Pos: %06.2f, %06.2f", state->input.mouse, "v2"); | 	DEBUG_PUSH_VAR("Mouse Pos: %06.2f, %06.2f", state->input.mouseP, "v2"); | ||||||
| 
 | 
 | ||||||
| 	i32 debug_bAllocated = state->arena.bytesAllocated; | 	i32 debug_bAllocated = state->arena.bytesAllocated; | ||||||
| 	DEBUG_PUSH_VAR("TotalMemoryAllocated: %db", debug_bAllocated, "i32"); | 	DEBUG_PUSH_VAR("TotalMemoryAllocated: %db", debug_bAllocated, "i32"); | ||||||
|  | |||||||
| @ -82,9 +82,6 @@ INTERNAL void rendererInit(GameState *state, v2 windowSize) | |||||||
| // TODO(doyle): Remove and implement own random generator!
 | // TODO(doyle): Remove and implement own random generator!
 | ||||||
| #include <time.h> | #include <time.h> | ||||||
| #include <stdlib.h> | #include <stdlib.h> | ||||||
| 
 |  | ||||||
| GLOBAL_VAR UiState uiState = {0}; |  | ||||||
| 
 |  | ||||||
| void worldTraveller_gameInit(GameState *state, v2 windowSize) | void worldTraveller_gameInit(GameState *state, v2 windowSize) | ||||||
| { | { | ||||||
| 	AssetManager *assetManager = &state->assetManager; | 	AssetManager *assetManager = &state->assetManager; | ||||||
| @ -345,9 +342,6 @@ void worldTraveller_gameInit(GameState *state, v2 windowSize) | |||||||
| #endif | #endif | ||||||
| 
 | 
 | ||||||
| 	srand(CAST(u32)(time(NULL))); | 	srand(CAST(u32)(time(NULL))); | ||||||
| 
 |  | ||||||
| 	uiState.mouseP      = &state->input.mouse; |  | ||||||
| 	uiState.mouseIsDown = &state->input.mouseLeft; |  | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| INTERNAL inline v4 getEntityScreenRect(Entity entity) | INTERNAL inline v4 getEntityScreenRect(Entity entity) | ||||||
| @ -884,28 +878,35 @@ INTERNAL void sortWorldEntityList(World *world, i32 numDeadEntities) | |||||||
| 
 | 
 | ||||||
| INTERNAL b32 pointInRect(Rect rect, v2 point) | INTERNAL b32 pointInRect(Rect rect, v2 point) | ||||||
| { | { | ||||||
| 	if (point.x < rect.pos.x || point.x > rect.pos.x + rect.size.w || | 	b32 outsideOfRectX = FALSE; | ||||||
| 	    point.y > rect.pos.y + rect.size.h || point.y < rect.pos.y) | 	if (point.x < rect.pos.x || point.x > (rect.pos.x + rect.size.w)) | ||||||
| 		return FALSE; | 		outsideOfRectX = TRUE; | ||||||
| 	return TRUE; | 
 | ||||||
|  | 	b32 outsideOfRectY = FALSE; | ||||||
|  | 	if (point.y < rect.pos.y || point.y > (rect.pos.y + rect.size.h)) | ||||||
|  | 		outsideOfRectY = TRUE; | ||||||
|  | 
 | ||||||
|  | 	if (outsideOfRectX || outsideOfRectY) return FALSE; | ||||||
|  | 	else return TRUE; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| i32 button(AssetManager *assetManager, Renderer *renderer, i32 id, Rect rect, | INTERNAL i32 button(UiState *uiState, AssetManager *assetManager, | ||||||
|            v2 mouseP) |                     Renderer *renderer, KeyInput input, i32 id, Rect rect) | ||||||
| { | { | ||||||
| 	if (pointInRect(rect, mouseP)) | 	if (pointInRect(rect, input.mouseP)) | ||||||
| 	{ | 	{ | ||||||
| 		uiState.hotItem = id; | 		DEBUG_PUSH_STRING("POINT IN RECT"); | ||||||
| 		if (uiState.activeItem == 0 && uiState.mouseIsDown) | 		uiState->hotItem = id; | ||||||
| 			uiState.activeItem = id; | 		if (uiState->activeItem == 0 && input.mouseLeft) | ||||||
|  | 			uiState->activeItem = id; | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	RenderTex renderTex = renderer_createNullRenderTex(assetManager); | 	RenderTex renderTex = renderer_createNullRenderTex(assetManager); | ||||||
| 	renderer_staticRect(renderer, v2_add(V2(8, 8), rect.pos), rect.size, | 	renderer_staticRect(renderer, v2_add(V2(8, 8), rect.pos), rect.size, | ||||||
| 	                    V2(0, 0), 0, renderTex, V4(0, 0, 0, 1)); | 	                    V2(0, 0), 0, renderTex, V4(0, 0, 0, 1)); | ||||||
| 	if (uiState.hotItem == id) | 	if (uiState->hotItem == id) | ||||||
| 	{ | 	{ | ||||||
| 		if (uiState.activeItem == id) | 		if (uiState->activeItem == id) | ||||||
| 		{ | 		{ | ||||||
| 			renderer_staticRect(renderer, v2_add(V2(2, 2), rect.pos), rect.size, | 			renderer_staticRect(renderer, v2_add(V2(2, 2), rect.pos), rect.size, | ||||||
| 			                    V2(0, 0), 0, renderTex, V4(1, 1, 1, 1)); | 			                    V2(0, 0), 0, renderTex, V4(1, 1, 1, 1)); | ||||||
| @ -922,6 +923,13 @@ i32 button(AssetManager *assetManager, Renderer *renderer, i32 id, Rect rect, | |||||||
| 		                    V2(0, 0), 0, renderTex, V4(0.5f, 0.5f, 0.5f, 1)); | 		                    V2(0, 0), 0, renderTex, V4(0.5f, 0.5f, 0.5f, 1)); | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
|  | 	if (!input.mouseLeft && | ||||||
|  | 	    uiState->hotItem == id && | ||||||
|  | 	    uiState->activeItem == id) | ||||||
|  | 	{ | ||||||
|  | 		return 1; | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
| 	return 0; | 	return 0; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| @ -1171,21 +1179,25 @@ void worldTraveller_gameUpdateAndRender(GameState *state, f32 dt) | |||||||
| 		hero->stats->busyDuration     = 0; | 		hero->stats->busyDuration     = 0; | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
|  | 	// INIT IMGUI
 | ||||||
|  | 	state->uiState.hotItem = 0; | ||||||
|  | 
 | ||||||
| 	/* Draw ui */ | 	/* Draw ui */ | ||||||
| 	RenderTex nullRenderTex = renderer_createNullRenderTex(assetManager); | 	Rect buttonRectA = {V2(300, 500), V2(100, 50)}; | ||||||
| 	renderer_staticRect(renderer, state->input.mouse, hero->hitboxSize, | 	button(&state->uiState, assetManager, renderer, state->input, 1, | ||||||
| 	                    V2(0, 0), 0, nullRenderTex, V4(0.5f, 0, 0, 0.5f)); | 	       buttonRectA); | ||||||
| 
 | 
 | ||||||
| #if 0 | 	Rect buttonRectB = {V2(500, 500), V2(100, 50)}; | ||||||
| 	RenderTex renderTex = renderer_createNullRenderTex(assetManager); | 	button(&state->uiState, assetManager, renderer, state->input, 2, | ||||||
| 	v2 buttonP = V2(500, 500); | 	       buttonRectB); | ||||||
| 	v2 buttonSize = V2(100, 100); |  | ||||||
| 	renderer_staticRect(renderer, v2_add(V2(8, 8), buttonP), buttonSize, |  | ||||||
| 	                    V2(0, 0), 0, renderTex, V4(0, 0, 0, 1)); |  | ||||||
| #endif |  | ||||||
| 
 | 
 | ||||||
| 	Rect buttonRect = {V2(500, 500), V2(100, 100)}; | 	Rect buttonRectC = {V2(700, 500), V2(100, 50)}; | ||||||
| 	button(assetManager, renderer, 1, buttonRect, *uiState.mouseP); | 	button(&state->uiState, assetManager, renderer, state->input, 3, | ||||||
|  | 	       buttonRectC); | ||||||
|  | 
 | ||||||
|  | 	// RESET IMGUI
 | ||||||
|  | 	if (!state->input.mouseLeft)             state->uiState.activeItem = 0; | ||||||
|  | 	else if (state->uiState.activeItem == 0) state->uiState.activeItem = -1; | ||||||
| 
 | 
 | ||||||
| 	/* Draw hero avatar */ | 	/* Draw hero avatar */ | ||||||
| 	TexAtlas *heroAtlas  = asset_getTextureAtlas(assetManager, texlist_hero); | 	TexAtlas *heroAtlas  = asset_getTextureAtlas(assetManager, texlist_hero); | ||||||
|  | |||||||
| @ -53,7 +53,7 @@ INTERNAL void mouseCallback(GLFWwindow *window, double xPos, double yPos) | |||||||
| 	// NOTE(doyle): x(0), y(0) of mouse starts from the top left of window
 | 	// NOTE(doyle): x(0), y(0) of mouse starts from the top left of window
 | ||||||
| 	v2 windowSize      = game->renderer.size; | 	v2 windowSize      = game->renderer.size; | ||||||
| 	f32 flipYPos       = windowSize.h - CAST(f32) yPos; | 	f32 flipYPos       = windowSize.h - CAST(f32) yPos; | ||||||
| 	game->input.mouse = V2(CAST(f32) xPos, flipYPos); | 	game->input.mouseP = V2(CAST(f32) xPos, flipYPos); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| INTERNAL void mouseButtonCallback(GLFWwindow *window, int button, int action, | INTERNAL void mouseButtonCallback(GLFWwindow *window, int button, int action, | ||||||
|  | |||||||
| @ -20,7 +20,7 @@ enum KeyCodes | |||||||
| 
 | 
 | ||||||
| typedef struct KeyInput | typedef struct KeyInput | ||||||
| { | { | ||||||
| 	v2 mouse; | 	v2 mouseP; | ||||||
| 	union | 	union | ||||||
| 	{ | 	{ | ||||||
| 		b32 keys[keycode_count - 1]; | 		b32 keys[keycode_count - 1]; | ||||||
|  | |||||||
| @ -15,8 +15,6 @@ | |||||||
| 
 | 
 | ||||||
| typedef struct UiState | typedef struct UiState | ||||||
| { | { | ||||||
| 	v2 *mouseP; |  | ||||||
| 	b32 *mouseIsDown; |  | ||||||
| 	i32 hotItem; | 	i32 hotItem; | ||||||
| 	i32 activeItem; | 	i32 activeItem; | ||||||
| } UiState; | } UiState; | ||||||
| @ -55,6 +53,7 @@ typedef struct GameState | |||||||
| 	AssetManager assetManager; | 	AssetManager assetManager; | ||||||
| 	AudioManager audioManager; | 	AudioManager audioManager; | ||||||
| 	MemoryArena arena; | 	MemoryArena arena; | ||||||
|  | 	UiState uiState; | ||||||
| } GameState; | } GameState; | ||||||
| 
 | 
 | ||||||
| void worldTraveller_gameInit(GameState *state, v2 windowSize); | void worldTraveller_gameInit(GameState *state, v2 windowSize); | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user