Add sample panels

This commit is contained in:
Tyler Perkins 2021-12-22 22:44:59 -05:00
parent 0ede29dace
commit c4b2cc1fce
5 changed files with 233 additions and 0 deletions

View File

@ -9,6 +9,7 @@
// PANELS
// A list of all panels to be displayed, in order
dashboard::panel::panel* PANELS[] = {
//new dashboard::panel::sample_panel(),
new dashboard::panel::weather(),
new dashboard::panel::wifi(),
};
@ -21,6 +22,7 @@ dashboard::panel::panel* OVERLAY = new dashboard::panel::def_overlay();
// IMAGE_LOCATIONS
// Locations of all static images used
const char* IMAGE_LOCATIONS[] = {
//sample panel
//"bmp_24.png",
//wifi
@ -69,6 +71,8 @@ const FONT_SIZE_STRING CONST_STRINGS[] = {
//Overlay strings
{ "Weather", { "Roboto_Mono/RobotoMono-Medium.ttf", 50 } },
{ "Wireless", { "Roboto_Mono/RobotoMono-Medium.ttf", 50 } },
//sample panel
{ "Sample Panel", { "Roboto_Mono/RobotoMono-Medium.ttf", 50} },
//Weather strings
{ "Clear Skies", { "Roboto_Mono/RobotoMono-Medium.ttf", 50 } },

View File

@ -78,6 +78,8 @@ constexpr int IMG_FLAGS = 0
#include "panel/weather.hpp"
#include "panel/def_overlay.hpp"
#include "panel/wifi.hpp"
//uncomment this to use the sample panel
//#include "panel/sample_panel.hpp"
extern dashboard::panel::panel* PANELS[];
extern size_t PANELS_LENGTH;

View File

@ -0,0 +1,148 @@
///////////////////////////////////////////////////////////////////////////////
// Tyler Perkins
// 22-12-21
// Sample panel cpp
//
#include "sample_panel.hpp"
#include "sample_panel_config.hpp"
using namespace dashboard::panel;
///////////////////////////////////////////////////////////////////////////////
// Constructors ///////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
sample_panel::sample_panel(){
std::cerr << "SAMPLE_PANEL CONSTRUCTOR\n";
_texture = nullptr;
_time_on_screen = SAMPLE_PANEL_TIME_ON_SCREEN;
_update_interval = std::chrono::milliseconds{SAMPLE_PANEL_UPDATE_INTERVAL};
_title = SAMPLE_PANEL_TITLE;
}
sample_panel::~sample_panel(){
std::cerr << "SAMPLE_PANEL DECONSTRUCTOR\n";
if(_texture != nullptr)
SDL_DestroyTexture(_texture);
}
///////////////////////////////////////////////////////////////////////////////
// Draw function //////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
void sample_panel::draw(){
//create the texture if this is the first time running draw
if(_texture == nullptr){
initTexture();
update();
update_texture();
}
//check if its time to update
if((std::chrono::high_resolution_clock::now() - _last_update)
> _update_interval){
update();
update_texture();
}
//copy the local _texture to the rendering canvas
SDL_RenderCopy(board::getRenderer(), _texture, NULL, NULL);
}
///////////////////////////////////////////////////////////////////////////////
// Helper functions ///////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////
// Update the information of the obj
// This DOES NOT update the display
void sample_panel::update(){
std::cerr << "SAMPLE_PANEL::UPDATE\n";
_last_update = std::chrono::high_resolution_clock::now();
//TODO Add your code to update the state of this panel here
//Sample code
i++;
if(SAMPLE_PANEL_DOUBLE_VAL)
i = i * 2;
}
///////////////////////////////////////
// Update the texture that is being
// displayed, based on data that was
// grabed by update()
void sample_panel::update_texture(){
std::cerr << "SAMPLE_PANEL::UPDATE_TEXTURE\n";
//set the render target to our local _texture
SDL_SetRenderTarget(board::getRenderer(), _texture);
SDL_RenderClear(board::getRenderer());
//TODO Add your code to update the texture here
// Begin Sample code //////////////
SDL_Rect tgt;
std::string i_str = std::to_string(i);
//Get the size of the string to be shown, and store it in tgt
TTF_SizeText(board::getFont({ "Roboto_Mono/RobotoMono-Medium.ttf", 50 }),
i_str.c_str(),
&tgt.w, &tgt.h);
//Set the position of the text by changing tgt.x and tgt.y
//NOTE 0,0 is the top left corner of both textures and the screen
tgt.x = (SCREEN_WIDTH / 2) - (tgt.w / 2); //center the text on the x axis
tgt.y = (SCREEN_HEIGHT / 2) - (tgt.h / 2);
//Get a texture of the string and place it on the screen
SDL_RenderCopy(board::getRenderer(),
board::getString(i_str.c_str(),
{ "Roboto_Mono/RobotoMono-Medium.ttf", 50 }),
NULL, &tgt);
//NOTE board::getString will generate a texture of that string and place it
//in a cache. After so many textures have been generated, the texture will
//be deleted (LRU cache). If you have a static string that you wish to add,
//be sure to add it to config.cpp as a static string and it will be generated
//and cached for the lifetime of the program
//copy a texture at the right hand size of the screen
tgt.x = (3*SCREEN_WIDTH / 4);
tgt.x = (SCREEN_HEIGHT / 2) - 100;
tgt.h = 100;
tgt.w = 100;
SDL_RenderCopy(board::getRenderer(),
board::getImage("bmp_24.png"),
NULL, &tgt);
//NOTE board::getImage can only get images that were declared at runtime as
//of this writing. To add these images place them in the img folder and add
//the name to config.cpp
// End Sample Code ////////////////
//reset the render texture
SDL_SetRenderTarget(board::getRenderer(), NULL);
}
///////////////////////////////////////
// Lazy load the texture object
// This is to make sure that all of SDL
// is init before we attempt to draw anything
// Most of this is fine as defaults and you probably dont want to change any of
// this
void sample_panel::initTexture(){
std::cerr << "SAMPLE PANEL INIT TEXTURE\n";
if(_texture == nullptr){
_texture = SDL_CreateTexture(board::getRenderer(),
SDL_PIXELFORMAT_RGBA8888,
SDL_TEXTUREACCESS_TARGET,
SCREEN_WIDTH, SCREEN_HEIGHT);
SDL_SetTextureBlendMode(_texture, SDL_BLENDMODE_BLEND);
}
}

View File

@ -0,0 +1,52 @@
///////////////////////////////////////////////////////////////////////////////
// Tyler Perkins
// 22-12-21
// Sample panel hpp
//
#pragma once
#include "panel.hpp"
#include <SDL.h>
#include <SDL2/SDL_image.h>
#include <SDL2/SDL_ttf.h>
#include <iostream>
#include <string>
#include <chrono>
namespace dashboard::panel {
class sample_panel : public panel {
public:
sample_panel();
~sample_panel();
//This will be called every frame your panel is selected as the active
//panel
void draw();
private:
//This is a sample value for this sample panel. You can delete this
int i;
//This is for you to update the data for your panel. You can think of
//this as the Model component of an MVC
void update();
//This is for you to update the texture for your panel. You can think
//of this as the View component of an MVC
void update_texture();
//This is for you to setup the _texture value. You should set it up
//here and not the contructor, as there is no gaurentee that SDL will
//be setup when the constructor is called
void initTexture();
//this is the last time that the data was updated. By default this is
//updated to the current time when update() is called
std::chrono::time_point<std::chrono::high_resolution_clock> _last_update;
//this is how long we should wait between updating, in milliseconds
std::chrono::milliseconds _update_interval;
};
}
#include "../board.hpp"

View File

@ -0,0 +1,27 @@
///////////////////////////////////////////////////////////////////////////////
// Tyler Perkins
// 22-12-21
// sample panel configuration
//
#pragma once
namespace dashboard::panel {
//This will be displayed at the top left of the status bar. Set to a blank
//string to not shown anything
constexpr char SAMPLE_PANEL_TITLE[] = "Sample Panel";
//Default time the slide is shown on the screen, in ms
//Default 15s
constexpr size_t SAMPLE_PANEL_TIME_ON_SCREEN = 15000;
//How long should we wait between updates? in ms
//NOTE this is a minimum and update() is not guarenteed to run at this
//exact interval
//Default 30s
constexpr size_t SAMPLE_PANEL_UPDATE_INTERVAL = 30000;
//Should we double the value each time as well?
constexpr bool SAMPLE_PANEL_DOUBLE_VAL = true;
}