Added image UI element.
This commit is contained in:
		
							
								
								
									
										34
									
								
								src/ui/image.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										34
									
								
								src/ui/image.c
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,34 @@
 | 
			
		||||
/**
 | 
			
		||||
 * Copyright (c) 2021 Dominic Masters
 | 
			
		||||
 * 
 | 
			
		||||
 * This software is released under the MIT License.
 | 
			
		||||
 * https://opensource.org/licenses/MIT
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
#include "image.h"
 | 
			
		||||
 | 
			
		||||
void imageInit(image_t *image, texture_t *texture) {
 | 
			
		||||
  image->x = 0;
 | 
			
		||||
  image->y = 0;
 | 
			
		||||
  image->quad.verticeCount = -1;
 | 
			
		||||
  imageSetTexture(image, texture);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void imageSetTexture(image_t *image, texture_t *texture) {
 | 
			
		||||
  if(image->quad.verticeCount != -1) {
 | 
			
		||||
    primitiveDispose(&image->quad);
 | 
			
		||||
    image->quad.verticeCount = -1;
 | 
			
		||||
  }
 | 
			
		||||
  
 | 
			
		||||
  image->texture = texture;
 | 
			
		||||
  quadInit(&image->quad, 0, 0,0,0,0, texture->width,texture->height,1,1);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void imageRender(image_t *image, shader_t *shader) {
 | 
			
		||||
  shaderUsePosition(shader, image->x, image->y, 0, 0, 0, 0);
 | 
			
		||||
  primitiveDraw(&image->quad, 0, -1);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void imageDispose(image_t *image) {
 | 
			
		||||
  if(image->quad.verticeCount != -1) primitiveDispose(&image->quad);
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										43
									
								
								src/ui/image.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										43
									
								
								src/ui/image.h
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,43 @@
 | 
			
		||||
/**
 | 
			
		||||
 * Copyright (c) 2021 Dominic Masters
 | 
			
		||||
 * 
 | 
			
		||||
 * This software is released under the MIT License.
 | 
			
		||||
 * https://opensource.org/licenses/MIT
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
#pragma once
 | 
			
		||||
#include <dawn/dawn.h>
 | 
			
		||||
#include "../display/primitive.h"
 | 
			
		||||
#include "../display/primitives/quad.h"
 | 
			
		||||
#include "../display/shader.h"
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Initialize an image.
 | 
			
		||||
 * 
 | 
			
		||||
 * @param image Image to initialize.
 | 
			
		||||
 * @param texture Texture to use for initialization.
 | 
			
		||||
 */
 | 
			
		||||
void imageInit(image_t *image, texture_t *texture);
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Set the texture for an image. This will also initialize the underlying quad.
 | 
			
		||||
 * 
 | 
			
		||||
 * @param image Image to set the texture for.
 | 
			
		||||
 * @param texture Texture to use.
 | 
			
		||||
 */
 | 
			
		||||
void imageSetTexture(image_t *image, texture_t *texture);
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Render an image
 | 
			
		||||
 * 
 | 
			
		||||
 * @param image Image to render.
 | 
			
		||||
 * @param shader Shader to use while rendering.
 | 
			
		||||
 */
 | 
			
		||||
void imageRender(image_t *image, shader_t *shader);
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Cleanup a previously initialized image.
 | 
			
		||||
 * 
 | 
			
		||||
 * @param image Image to dispose.
 | 
			
		||||
 */
 | 
			
		||||
void imageDispose(image_t *image);
 | 
			
		||||
@@ -5,4 +5,83 @@
 | 
			
		||||
 * https://opensource.org/licenses/MIT
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
#include "menulist.h"
 | 
			
		||||
#include "menulist.h"
 | 
			
		||||
 | 
			
		||||
void menuListInit(menulist_t *ml, texture_t *texture) {
 | 
			
		||||
  uint8_t i;
 | 
			
		||||
 | 
			
		||||
  ml->x = 0;
 | 
			
		||||
  ml->y = 0;
 | 
			
		||||
  ml->count = 0;
 | 
			
		||||
 | 
			
		||||
  frameInit(&ml->frame);
 | 
			
		||||
  ml->frame.texture = texture;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
uint8_t menuListAdd(menulist_t *ml, font_t *font, char *text) {
 | 
			
		||||
  uint8_t i;
 | 
			
		||||
  label_t *label;
 | 
			
		||||
  menuitem_t *item;
 | 
			
		||||
 | 
			
		||||
  // Get the variables.
 | 
			
		||||
  i = ml->count;
 | 
			
		||||
  label = ml->labels + i;
 | 
			
		||||
  ml->items[i] = text;
 | 
			
		||||
 | 
			
		||||
  // Initialize the label.
 | 
			
		||||
  labelInit(label);
 | 
			
		||||
  labelSetText(label, font, text);
 | 
			
		||||
 | 
			
		||||
  // Reize the frame
 | 
			
		||||
  frameSetInnerSize(
 | 
			
		||||
    &ml->frame,
 | 
			
		||||
    100,
 | 
			
		||||
    FONT_LINE_HEIGHT * fontGetScale(label->fontSize) * (i+1)
 | 
			
		||||
  );
 | 
			
		||||
 | 
			
		||||
  // Add Menu Item
 | 
			
		||||
  item = menuAdd(&ml->menu);
 | 
			
		||||
  item->width = 1;
 | 
			
		||||
  item->height = 1;
 | 
			
		||||
  item->x = 0;
 | 
			
		||||
  item->y = i;
 | 
			
		||||
 | 
			
		||||
  ml->count++;
 | 
			
		||||
  return i;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void menuListUpdate(menulist_t *ml, engine_t *engine) {
 | 
			
		||||
  menuUpdate(&ml->menu, engine);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void menuListRender(menulist_t *ml, shader_t *shader) {
 | 
			
		||||
  uint8_t i;
 | 
			
		||||
  label_t *label;
 | 
			
		||||
 | 
			
		||||
  // Render the frame
 | 
			
		||||
  ml->frame.x = ml->x;
 | 
			
		||||
  ml->frame.y = ml->y;
 | 
			
		||||
  frameRender(&ml->frame, shader);
 | 
			
		||||
 | 
			
		||||
  // Render selection box.
 | 
			
		||||
 | 
			
		||||
  // Render each labels
 | 
			
		||||
  for(i = 0; i < ml->count; i++) {
 | 
			
		||||
    label = ml->labels + i;
 | 
			
		||||
    label->x = FRAME_BORDER_SIZE;
 | 
			
		||||
    if(ml->menu.selected == i) label->x += 4;
 | 
			
		||||
    label->y = FRAME_BORDER_SIZE + (
 | 
			
		||||
      i * FONT_LINE_HEIGHT * fontGetScale(label->fontSize)
 | 
			
		||||
    );
 | 
			
		||||
    labelRender(label, shader);
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void menuListDispose(menulist_t *ml) {
 | 
			
		||||
  uint8_t i;
 | 
			
		||||
  for(i = 0; i < ml->count; i++) {
 | 
			
		||||
    labelDispose(ml->labels + i);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  frameDispose(&ml->frame);
 | 
			
		||||
}
 | 
			
		||||
@@ -7,3 +7,16 @@
 | 
			
		||||
 | 
			
		||||
#pragma once
 | 
			
		||||
#include <dawn/dawn.h>
 | 
			
		||||
#include "label.h"
 | 
			
		||||
#include "menu.h"
 | 
			
		||||
#include "frame.h"
 | 
			
		||||
 | 
			
		||||
void menuListInit(menulist_t *ml, texture_t *texture);
 | 
			
		||||
 | 
			
		||||
uint8_t menuListAdd(menulist_t *ml, font_t *font, char *text);
 | 
			
		||||
 | 
			
		||||
void menuListUpdate(menulist_t *ml, engine_t *engine);
 | 
			
		||||
 | 
			
		||||
void menuListRender(menulist_t *ml, shader_t *shader);
 | 
			
		||||
 | 
			
		||||
void menuListDispose(menulist_t *ml);
 | 
			
		||||
		Reference in New Issue
	
	Block a user