first commit
This commit is contained in:
340
src/ui/UINineSlice.cpp
Normal file
340
src/ui/UINineSlice.cpp
Normal file
@ -0,0 +1,340 @@
|
||||
/*
|
||||
* UINineSlice.cpp
|
||||
*
|
||||
* Created on: May 31, 2020
|
||||
* Author: ayoungblood
|
||||
*/
|
||||
|
||||
#include "../ui/UINineSlice.h"
|
||||
#include "../game/Game.hpp"
|
||||
#include "../ecs/ECS.h"
|
||||
#include "../ecs/Components.h"
|
||||
#include "string.h"
|
||||
#include <iostream>
|
||||
#include <stdio.h>
|
||||
|
||||
extern Manager manager;
|
||||
|
||||
UINineSlice::UINineSlice(std::string texID)
|
||||
{
|
||||
textureID = texID;
|
||||
}
|
||||
|
||||
UINineSlice::~UINineSlice()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void UINineSlice::MakeSlices(std::string texture, int srcW, int srcH, int x0, int x1, int y0, int y1, SDL_Rect finalRect,int scale)
|
||||
{
|
||||
// Note about the variables: The source texture is what everything is drawn from, with srcW and srcH defining the size of that texture and x0 x1 being the vertical slices position across the x axis, and the y0, y1 being the horizontal slices. This is also in the project readme with an ASCII art diagram. finalRect refers to the Rectangle that describes the area of the screen in which we want the 9-sliced source to appear in it's final in-game rendering.
|
||||
|
||||
SDL_Rect srcRect;
|
||||
SDL_Rect destRect;
|
||||
int cols;
|
||||
int rows;
|
||||
int colsRemainder;
|
||||
int rowsRemainder;
|
||||
|
||||
cols = ((finalRect.w-x0-(srcW-x1))/(x1-x0));
|
||||
rows = (finalRect.h-y0-(srcH-y1))/(y1-y0);
|
||||
// Calculate a remainders
|
||||
colsRemainder = ((finalRect.w-x0-(srcW-x1))%(x1-x0));
|
||||
rowsRemainder = ((finalRect.h-y0-(srcH-y1))%(y1-y0));
|
||||
|
||||
// printf("\n\nColumns: %d\nRows: %d\nColumn Remainder: %d\nRow Remainder: %d\n\n",cols,rows,colsRemainder,rowsRemainder);
|
||||
|
||||
finalRect.x = finalRect.x*scale;
|
||||
finalRect.y = finalRect.y*scale;
|
||||
finalRect.w = finalRect.w*scale;
|
||||
finalRect.h = finalRect.h*scale;
|
||||
|
||||
// x0 = x0*scale;
|
||||
// x1 = x1*scale;
|
||||
// y0 = y0*scale;
|
||||
// y1 = y1*scale;
|
||||
|
||||
// printf("finalRect x:%d, y:%d, w:%d, h:%d \n",finalRect.x,finalRect.y,finalRect.w,finalRect.h);
|
||||
|
||||
// destRect.x = destRect.x*scale;
|
||||
// destRect.y = destRect.y*scale;
|
||||
// destRect.w = destRect.w*scale;
|
||||
// destRect.h = destRect.h*scale;
|
||||
// colsRemainder = colsRemainder*scale;
|
||||
// rowsRemainder = rowsRemainder*scale;
|
||||
|
||||
for (int i=0; i<9; i++)
|
||||
{
|
||||
switch(i)
|
||||
{
|
||||
case 0:
|
||||
srcRect.x = 0;
|
||||
srcRect.y = 0;
|
||||
srcRect.w = x0;
|
||||
srcRect.h = y0;
|
||||
// printf("tile zero.w or x0: %d\n",x0);
|
||||
destRect.w = srcRect.w;
|
||||
destRect.h = srcRect.h;
|
||||
break;
|
||||
case 1:
|
||||
srcRect.x = x0;
|
||||
srcRect.y = 0;
|
||||
srcRect.w = x1-x0;
|
||||
srcRect.h = y0;
|
||||
destRect.w = srcRect.w;
|
||||
destRect.h = srcRect.h;
|
||||
break;
|
||||
case 2:
|
||||
srcRect.x = x1;
|
||||
srcRect.y = 0;
|
||||
srcRect.w = srcW-x1;
|
||||
srcRect.h = y0;
|
||||
destRect.w = srcRect.w;
|
||||
destRect.h = srcRect.h;
|
||||
// printf("srcRect.x: %d",srcRect.x);
|
||||
// printf("UI9Slice #2 srcRect x:%d y:%d w:%d h:%d \n",srcRect.x,srcRect.y,srcRect.w,srcRect.h);
|
||||
break;
|
||||
case 3:
|
||||
srcRect.x = 0;
|
||||
srcRect.y = y0;
|
||||
srcRect.w = x0;
|
||||
srcRect.h = y1-y0;
|
||||
destRect.w = srcRect.w;
|
||||
destRect.h = srcRect.h;
|
||||
break;
|
||||
case 4:
|
||||
srcRect.x = x0;
|
||||
srcRect.y = y0;
|
||||
srcRect.w = x1-x0;
|
||||
srcRect.h = y1-y0;
|
||||
destRect.w = srcRect.w;
|
||||
destRect.h = srcRect.h;
|
||||
break;
|
||||
case 5:
|
||||
srcRect.x = x1;
|
||||
srcRect.y = y0;
|
||||
srcRect.w = srcW-x1;
|
||||
srcRect.h = y1-y0;
|
||||
destRect.w = srcRect.w;
|
||||
destRect.h = srcRect.h;
|
||||
break;
|
||||
case 6:
|
||||
srcRect.x = 0;
|
||||
srcRect.y = y1;
|
||||
srcRect.w = x0;
|
||||
srcRect.h = srcH-y1;
|
||||
destRect.w = srcRect.w;
|
||||
destRect.h = srcRect.h;
|
||||
break;
|
||||
case 7:
|
||||
srcRect.x = x0;
|
||||
srcRect.y = y1;
|
||||
srcRect.w = x1-x0;
|
||||
srcRect.h = srcH-y1;
|
||||
destRect.w = srcRect.w;
|
||||
destRect.h = srcRect.h;
|
||||
break;
|
||||
case 8:
|
||||
srcRect.x = x1;
|
||||
srcRect.y = y1;
|
||||
srcRect.w = srcW-x1;
|
||||
srcRect.h = srcH-y1;
|
||||
destRect.w = srcRect.w;
|
||||
destRect.h = srcRect.h;
|
||||
break;
|
||||
default:
|
||||
srcRect.x = 0;
|
||||
srcRect.y = 0;
|
||||
srcRect.w = 0;
|
||||
srcRect.h = 0;
|
||||
destRect.w = 0;
|
||||
destRect.h = 0;
|
||||
}
|
||||
// These will be the same for each SDL_Rect except if there's a scaling int, which still needs to be implemented
|
||||
// destRect.w = srcRect.w*scale;
|
||||
// destRect.h = srcRect.h*scale;
|
||||
|
||||
// x0 = x0*scale;
|
||||
// x1 = x1*scale;
|
||||
// y0 = y0*scale;
|
||||
// y1 = y1*scale;
|
||||
|
||||
// Calculate where and how many tiles to place
|
||||
// We only need one instance of each of these in each corner or slices 0,2,6,8
|
||||
if (i==0||i==2||i==6||i==8)
|
||||
{
|
||||
if (i==0)
|
||||
{
|
||||
destRect.x = finalRect.x;
|
||||
destRect.y = finalRect.y;
|
||||
}
|
||||
if (i==2)
|
||||
{
|
||||
destRect.x = finalRect.x+(finalRect.w-srcRect.w*scale);
|
||||
destRect.y = finalRect.y;
|
||||
}
|
||||
if (i==6)
|
||||
{
|
||||
destRect.x = finalRect.x;
|
||||
destRect.y = finalRect.y+(finalRect.h-srcRect.h*scale);
|
||||
}
|
||||
if (i==8)
|
||||
{
|
||||
destRect.x = finalRect.x+(finalRect.w-srcRect.w*scale);
|
||||
destRect.y = finalRect.y+(finalRect.h-srcRect.h*scale);
|
||||
}
|
||||
AddSlice(srcRect,destRect,scale);
|
||||
// printf("Corner Slice\n");
|
||||
// printf("srcRect x:%d, y:%d, w:%d, h:%d \n",srcRect.x,srcRect.y,srcRect.w,srcRect.h);
|
||||
// printf("destRect x:%d, y:%d, w:%d, h:%d \n\n",destRect.x,destRect.y,destRect.w,destRect.h);
|
||||
}
|
||||
// Slices 1,7 need to be repeated in a row
|
||||
if (i==1||i==7)
|
||||
{
|
||||
if (i==1)
|
||||
{
|
||||
for (int c=0;c<cols;c++)
|
||||
{
|
||||
destRect.x = finalRect.x+(x0*scale+c*(x1*scale-x0*scale));
|
||||
destRect.y = finalRect.y;
|
||||
AddSlice(srcRect,destRect,scale);
|
||||
// printf("Top Slice (1) \n");
|
||||
// printf("srcRect x:%d, y:%d, w:%d, h:%d \n",srcRect.x,srcRect.y,srcRect.w,srcRect.h);
|
||||
// printf("destRect x:%d, y:%d, w:%d, h:%d \n\n",destRect.x,destRect.y,destRect.w,destRect.h);
|
||||
}
|
||||
if (colsRemainder>0){
|
||||
destRect.x = finalRect.x+(x0*scale+cols*(x1*scale-x0*scale));
|
||||
destRect.y = finalRect.y;
|
||||
srcRect.w = colsRemainder;
|
||||
destRect.w = colsRemainder;
|
||||
AddSlice(srcRect,destRect,scale);
|
||||
// printf("Top Slice (1) \n");
|
||||
// printf("srcRect x:%d, y:%d, w:%d, h:%d \n",srcRect.x,srcRect.y,srcRect.w,srcRect.h);
|
||||
// printf("destRect x:%d, y:%d, w:%d, h:%d \n\n",destRect.x,destRect.y,destRect.w,destRect.h);
|
||||
}
|
||||
}
|
||||
if (i==7)
|
||||
{
|
||||
for (int c=0;c<cols;c++)
|
||||
{
|
||||
destRect.x = finalRect.x+(x0*scale+c*(x1*scale-x0*scale));
|
||||
destRect.y = finalRect.y+(finalRect.h-(srcH-y1)*scale);
|
||||
AddSlice(srcRect,destRect,scale);
|
||||
// printf("destRect x:%d, y:%d, w:%d, h:%d \n",destRect.x,destRect.y,destRect.w,destRect.h);
|
||||
}
|
||||
if (colsRemainder>0){
|
||||
destRect.x = finalRect.x+(x0*scale+cols*(x1*scale-x0*scale));
|
||||
destRect.y = finalRect.y+(finalRect.h-(srcH-y1)*scale);
|
||||
srcRect.w = colsRemainder;
|
||||
destRect.w = colsRemainder;
|
||||
AddSlice(srcRect,destRect,scale);
|
||||
// printf("destRect x:%d, y:%d, w:%d, h:%d \n",destRect.x,destRect.y,destRect.w,destRect.h);
|
||||
}
|
||||
}
|
||||
}
|
||||
// Slice 3,5 need to be repeated in a column
|
||||
if (i==3||i==5)
|
||||
{
|
||||
if (i==3)
|
||||
{
|
||||
for (int r=0;r<rows;r++)
|
||||
{
|
||||
destRect.x = finalRect.x;
|
||||
destRect.y = finalRect.y+(y0*scale+r*(y1-y0)*scale);
|
||||
AddSlice(srcRect,destRect,scale);
|
||||
// printf("destRect x:%d, y:%d, w:%d, h:%d \n",destRect.x,destRect.y,destRect.w,destRect.h);
|
||||
}
|
||||
if (rowsRemainder>0){
|
||||
destRect.x = finalRect.x;
|
||||
destRect.y = finalRect.y+(y0*scale+rows*(y1-y0)*scale);
|
||||
srcRect.h = rowsRemainder;
|
||||
destRect.h = rowsRemainder;
|
||||
AddSlice(srcRect,destRect,scale);
|
||||
// printf("destRect x:%d, y:%d, w:%d, h:%d \n",destRect.x,destRect.y,destRect.w,destRect.h);
|
||||
}
|
||||
}
|
||||
if (i==5)
|
||||
{
|
||||
for (int r=0;r<rows;r++)
|
||||
{
|
||||
destRect.x = finalRect.x+(finalRect.w-(srcW-x1)*scale);
|
||||
destRect.y = finalRect.y+(y0*scale+r*(y1-y0)*scale);
|
||||
AddSlice(srcRect,destRect,scale);
|
||||
// printf("destRect x:%d, y:%d, w:%d, h:%d \n",destRect.x,destRect.y,destRect.w,destRect.h);
|
||||
}
|
||||
if (rowsRemainder>0){
|
||||
destRect.x = finalRect.x+(finalRect.w-(srcW-x1)*scale);
|
||||
destRect.y = finalRect.y+(y0*scale+rows*(y1-y0)*scale);
|
||||
srcRect.h = rowsRemainder;
|
||||
destRect.h = rowsRemainder;
|
||||
AddSlice(srcRect,destRect,scale);
|
||||
// printf("destRect x:%d, y:%d, w:%d, h:%d \n",destRect.x,destRect.y,destRect.w,destRect.h);
|
||||
}
|
||||
}
|
||||
}
|
||||
// Slice 4 will need to be repeated in columns and rows
|
||||
if (i==4)
|
||||
{
|
||||
int rowY;
|
||||
for (int r=0;r<rows;r++)
|
||||
{
|
||||
rowY = r*(y1-y0);
|
||||
for (int c=0;c<cols;c++)
|
||||
{
|
||||
destRect.x = finalRect.x+(x0*scale+c*(x1-x0)*scale);
|
||||
destRect.y = finalRect.y+(y0*scale+rowY*scale);
|
||||
AddSlice(srcRect,destRect,scale);
|
||||
// printf("destRect x:%d, y:%d, w:%d, h:%d \n",destRect.x,destRect.y,destRect.w,destRect.h);
|
||||
}
|
||||
if (colsRemainder>0)
|
||||
{
|
||||
destRect.x = finalRect.x+(x0*scale+cols*(x1-x0)*scale);
|
||||
destRect.y = finalRect.y+(y0*scale+rowY*scale);
|
||||
srcRect.w = colsRemainder;
|
||||
destRect.w = colsRemainder;
|
||||
AddSlice(srcRect,destRect,scale);
|
||||
// printf("destRect x:%d, y:%d, w:%d, h:%d \n",destRect.x,destRect.y,destRect.w,destRect.h);
|
||||
}
|
||||
}
|
||||
if (rowsRemainder>0)
|
||||
{
|
||||
for (int c=0;c<cols;c++)
|
||||
{
|
||||
destRect.x = finalRect.x+(x0*scale+c*(x1-x0)*scale);
|
||||
destRect.y = finalRect.y+(y0*scale+rows*(y1-y0)*scale);
|
||||
srcRect.h = rowsRemainder;
|
||||
destRect.h = rowsRemainder;
|
||||
srcRect.w = (srcW-x0-(srcW-x1));
|
||||
destRect.w = (srcW-x0-(srcW-x1));
|
||||
AddSlice(srcRect,destRect,scale);
|
||||
// printf("destRect x:%d, y:%d, w:%d, h:%d \n",destRect.x,destRect.y,destRect.w,destRect.h);
|
||||
}
|
||||
}
|
||||
if(rowsRemainder>0&&colsRemainder>0)
|
||||
{
|
||||
destRect.x = finalRect.x+(x0*scale+cols*(x1-x0)*scale);
|
||||
destRect.y = finalRect.y+(y0*scale+rows*(y1-y0)*scale);
|
||||
srcRect.w = colsRemainder;
|
||||
srcRect.h = rowsRemainder;
|
||||
destRect.w = colsRemainder;
|
||||
destRect.h = rowsRemainder;
|
||||
AddSlice(srcRect,destRect,scale);
|
||||
// printf("destRect x:%d, y:%d, w:%d, h:%d \n",destRect.x,destRect.y,destRect.w,destRect.h);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// Need to take in variables: (srcRect, desiredRect)
|
||||
void UINineSlice::AddSlice(SDL_Rect srcRect, SDL_Rect destRect, int scale)
|
||||
{
|
||||
// printf("adding a 9slice element");
|
||||
auto& slice(manager.addEntity());
|
||||
SDL_Rect scaledRect = SDL_Rect();
|
||||
scaledRect.x = destRect.x;
|
||||
scaledRect.y = destRect.y;
|
||||
scaledRect.w = destRect.w*scale;
|
||||
scaledRect.h = destRect.h*scale;
|
||||
slice.addComponent<TransformComponent>(scaledRect.x, scaledRect.y, scaledRect.w, scaledRect.h, 1);
|
||||
slice.addComponent<SpriteComponent>("textBox",SpriteComponent::spriteUIL0,srcRect,scaledRect);
|
||||
slice.addGroup(Game::groupUI_Layer0);
|
||||
}
|
28
src/ui/UINineSlice.h
Normal file
28
src/ui/UINineSlice.h
Normal file
@ -0,0 +1,28 @@
|
||||
/*
|
||||
* UINineSlice.h
|
||||
*
|
||||
* Created on: May 31, 2020
|
||||
* Author: ayoungblood
|
||||
*/
|
||||
|
||||
#ifndef SRC_UININESLICE_H_
|
||||
#define SRC_UININESLICE_H_
|
||||
|
||||
#include "SDL2/SDL.h"
|
||||
#include "string"
|
||||
|
||||
class UINineSlice
|
||||
{
|
||||
public:
|
||||
std::string textureID;
|
||||
SDL_Rect destRect;
|
||||
UINineSlice(std::string texID);
|
||||
~UINineSlice();
|
||||
void MakeSlices(std::string texture, int srcW, int srcH, int x0, int x1, int y0, int y1, SDL_Rect destRect, int scale);
|
||||
void AddSlice(SDL_Rect srcRect, SDL_Rect destRect, int scale);
|
||||
private:
|
||||
|
||||
};
|
||||
|
||||
|
||||
#endif /* SRC_UININESLICE_H_ */
|
67
src/ui/UIText.cpp
Normal file
67
src/ui/UIText.cpp
Normal file
@ -0,0 +1,67 @@
|
||||
/*
|
||||
* UIText.cpp
|
||||
*
|
||||
* Created on: May 21, 2020
|
||||
* Author: ayoungblood
|
||||
*/
|
||||
|
||||
#include "UIText.h"
|
||||
#include "../game/Game.hpp"
|
||||
#include "../ecs/ECS.h"
|
||||
#include "../ecs/Components.h"
|
||||
#include "string.h"
|
||||
#include <iostream>
|
||||
#include <stdio.h>
|
||||
|
||||
extern Manager manager;
|
||||
|
||||
UIText::UIText(const char* text, std::string texId, int x, int y, int letterW, int letterH, int lScale)
|
||||
{
|
||||
inputText = text;
|
||||
textureID = texId;
|
||||
posX = x;
|
||||
posY = y;
|
||||
letterWidth = letterW;
|
||||
letterHeight = letterH;
|
||||
scale = lScale;
|
||||
}
|
||||
|
||||
UIText::~UIText()
|
||||
{
|
||||
}
|
||||
|
||||
void UIText::ParseString(const char* inputText, int x, int y, int scale)
|
||||
{
|
||||
//Parse input text into an array of char
|
||||
int posX = x;
|
||||
int posY = y;
|
||||
int i = 0;
|
||||
// printf(inputText);
|
||||
char current = inputText[i];
|
||||
do
|
||||
{
|
||||
++i;
|
||||
if (strcmp(¤t,"\n"))
|
||||
{
|
||||
posX += letterWidth;
|
||||
}
|
||||
else
|
||||
{
|
||||
// printf("new line detected");
|
||||
posX = x;
|
||||
posY += letterHeight;
|
||||
}
|
||||
UIText::AddLetter(posX, posY, current);
|
||||
current = inputText[i];
|
||||
} while ((strcmp(¤t,"\0"))!=0);
|
||||
}
|
||||
|
||||
|
||||
void UIText::AddLetter(int xpos, int ypos, char crnt)
|
||||
{
|
||||
auto& letter(manager.addEntity());
|
||||
letter.addComponent<TransformComponent>(xpos*scale, ypos*scale, letterWidth, letterHeight, 1);
|
||||
// printf("Scale: %d\n",scale);
|
||||
letter.addComponent<SpriteComponent>("font", SpriteComponent::spriteText, crnt, letterWidth, letterHeight, scale);
|
||||
letter.addGroup(Game::groupUI_Layer1);
|
||||
}
|
32
src/ui/UIText.h
Normal file
32
src/ui/UIText.h
Normal file
@ -0,0 +1,32 @@
|
||||
/*
|
||||
* UIText.h
|
||||
*
|
||||
* Created on: May 21, 2020
|
||||
* Author: ayoungblood
|
||||
*/
|
||||
|
||||
#ifndef SRC_UITEXT_H_
|
||||
#define SRC_UITEXT_H_
|
||||
|
||||
#include "SDL2/SDL.h"
|
||||
#include <iostream>
|
||||
|
||||
class UIText
|
||||
{
|
||||
public:
|
||||
const char* inputText;
|
||||
int letterHeight;
|
||||
int letterWidth;
|
||||
int posX;
|
||||
int posY;
|
||||
std::string textureID;
|
||||
UIText(const char* inputText, std::string texID, int x, int y, int letterW, int letterH, int lScale);
|
||||
~UIText();
|
||||
|
||||
// void SetCharClips(SDL_Texture* fontTex, int x, int y, int letterW, int letterH);
|
||||
void AddLetter(int xpos, int ypos, char crnt);
|
||||
void ParseString(const char* inputText, int x, int y, int scale);
|
||||
int scale;
|
||||
};
|
||||
|
||||
#endif /* SRC_UITEXT_H_ */
|
Reference in New Issue
Block a user