Added align UI code.
This commit is contained in:
		| @@ -75,6 +75,7 @@ | ||||
| #include "ui/menuv2.h" | ||||
| #include "ui/rectangle.h" | ||||
| #include "ui/textmenu.h" | ||||
| #include "ui/align.h" | ||||
|  | ||||
| #include "ui/frame.h" | ||||
| #include "ui/image.h" | ||||
|   | ||||
							
								
								
									
										20
									
								
								include/dawn/ui/align.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										20
									
								
								include/dawn/ui/align.h
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,20 @@ | ||||
| // Copyright (c) 2021 Dominic Masters | ||||
| //  | ||||
| // This software is released under the MIT License. | ||||
| // https://opensource.org/licenses/MIT | ||||
|  | ||||
| #pragma once | ||||
| #include "../libs.h" | ||||
|  | ||||
| #define ALIGN_POS_CENTER flagDefine(0) | ||||
| #define ALIGN_POS_START flagDefine(1) | ||||
| #define ALIGN_POS_END flagDefine(2) | ||||
|  | ||||
| #define ALIGN_SIZE_FILL flagDefine(3) | ||||
| #define ALIGN_SIZE_ORIGINAL flagDefine(4) | ||||
| #define ALIGN_SIZE_RATIO flagDefine(5) | ||||
|  | ||||
| typedef struct { | ||||
|   float x, y; | ||||
|   float width, height; | ||||
| } align_t; | ||||
							
								
								
									
										73
									
								
								src/ui/align.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										73
									
								
								src/ui/align.c
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,73 @@ | ||||
| /** | ||||
|  * Copyright (c) 2021 Dominic Masters | ||||
|  *  | ||||
|  * This software is released under the MIT License. | ||||
|  * https://opensource.org/licenses/MIT | ||||
|  */ | ||||
|  | ||||
| #include "align.h" | ||||
|  | ||||
| void alignmentGetAxis( | ||||
|   uint8_t align, | ||||
|   float containerSize, float childSize, float childPos, | ||||
|   float *outSize, float *outPos | ||||
| ) { | ||||
|   if(align & ALIGN_SIZE_FILL) { | ||||
|     *outSize = containerSize; | ||||
|   } else if(align & ALIGN_SIZE_ORIGINAL) { | ||||
|     *outSize = childSize; | ||||
|   } | ||||
|  | ||||
|   if(align & ALIGN_POS_START) { | ||||
|     *outPos = 0; | ||||
|   } else if(align & ALIGN_POS_CENTER) { | ||||
|     *outPos = (containerSize/2.0f) - ((*outSize) / 2.0f); | ||||
|   } else if(align & ALIGN_POS_END) { | ||||
|     *outPos = containerSize - (*outSize); | ||||
|   } | ||||
| } | ||||
|  | ||||
| align_t alignmentGet( | ||||
|   uint8_t alignX, uint8_t alignY, | ||||
|   float containerWidth, float containerHeight, | ||||
|   float childWidth, float childHeight, | ||||
|   float childX, float childY | ||||
| ) { | ||||
|   align_t out; | ||||
|  | ||||
|   if(alignX & ALIGN_SIZE_RATIO) { | ||||
|     // Work out Y size and alignment first, then we base off that. | ||||
|     alignmentGetAxis( | ||||
|       alignY, containerHeight, childHeight, childY, &out.height, &out.y | ||||
|     ); | ||||
|  | ||||
|     // Now work out ratio | ||||
|     out.width = out.height * (childWidth / childHeight); | ||||
|  | ||||
|     // Now do X | ||||
|     alignmentGetAxis( | ||||
|       alignX, containerWidth, childWidth, childX, &out.width, &out.x | ||||
|     ); | ||||
|   } else { | ||||
|     alignmentGetAxis( | ||||
|       alignX, containerWidth, childWidth, childX, &out.width, &out.x | ||||
|     ); | ||||
|   } | ||||
|    | ||||
|   // Same as above but inverted for Y axis. | ||||
|   if(alignY & ALIGN_SIZE_RATIO) { | ||||
|     alignmentGetAxis( | ||||
|       alignX, containerWidth, childWidth, childX, &out.width, &out.x | ||||
|     ); | ||||
|     out.height = out.width * (childHeight / childWidth); | ||||
|     alignmentGetAxis( | ||||
|       alignY, containerHeight, childHeight, childY, &out.height, &out.y | ||||
|     ); | ||||
|   } else { | ||||
|     alignmentGetAxis( | ||||
|       alignY, containerHeight, childHeight, childY, &out.height, &out.y | ||||
|     ); | ||||
|   } | ||||
|  | ||||
|   return out; | ||||
| } | ||||
							
								
								
									
										46
									
								
								src/ui/align.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										46
									
								
								src/ui/align.h
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,46 @@ | ||||
| /** | ||||
|  * Copyright (c) 2021 Dominic Masters | ||||
|  *  | ||||
|  * This software is released under the MIT License. | ||||
|  * https://opensource.org/licenses/MIT | ||||
|  */ | ||||
|  | ||||
| #pragma once | ||||
| #include <dawn/dawn.h> | ||||
|  | ||||
| /** | ||||
|  * Return the alignment for a specific access, really this doesn't need to be | ||||
|  * used. Does not support the ALIGN_SIZE_RATIO alignment method. | ||||
|  *  | ||||
|  * @param align Alignment flags to get. | ||||
|  * @param containerSize Size of the container. | ||||
|  * @param childSize Size of the child. | ||||
|  * @param childPos Currently unused. | ||||
|  * @param outSize Pointer to a float for the size output. | ||||
|  * @param outPos Poitner to a float for the position output. | ||||
|  */ | ||||
| void alignmentGetAxis( | ||||
|   uint8_t align, | ||||
|   float containerSize, float childSize, float childPos, | ||||
|   float *outSize, float *outPos | ||||
| ); | ||||
|  | ||||
| /** | ||||
|  * Get the alignment set for a given set of options. | ||||
|  *  | ||||
|  * @param alignX Alignment options for the X axis. | ||||
|  * @param alignY Alignment options for the Y axis. | ||||
|  * @param containerWidth Width of the container. | ||||
|  * @param containerHeight Height of the container. | ||||
|  * @param childWidth Width of the child | ||||
|  * @param childHeight Height of the child. | ||||
|  * @param childX Currently unused. | ||||
|  * @param childY Currently unused.  | ||||
|  * @return The alignment calculation. | ||||
|  */ | ||||
| align_t alignmentGet( | ||||
|   uint8_t alignX, uint8_t alignY, | ||||
|   float containerWidth, float containerHeight, | ||||
|   float childWidth, float childHeight, | ||||
|   float childX, float childY | ||||
| ); | ||||
		Reference in New Issue
	
	Block a user