Compare commits
3 Commits
a904f0cdaf
...
882a645926
Author | SHA1 | Date |
---|---|---|
Jasper Blanckenburg | 882a645926 | |
Jasper Blanckenburg | 0b34bac5bc | |
Jasper Blanckenburg | c0b56f9f76 |
|
@ -11,16 +11,18 @@
|
|||
class AMI final : public View {
|
||||
public:
|
||||
AMI(SDL_Renderer* renderer);
|
||||
~AMI();
|
||||
|
||||
void draw() override;
|
||||
|
||||
private:
|
||||
TTF_Font* avenir;
|
||||
TTF_Font* chinat;
|
||||
|
||||
std::vector<Widget*> widgets;
|
||||
|
||||
ImageWidget ft_logo;
|
||||
TextWidget choose;
|
||||
ListWidget missions_widget;
|
||||
std::unique_ptr<ImageWidget> ft_logo;
|
||||
std::unique_ptr<TextWidget> choose;
|
||||
std::unique_ptr<ListWidget> missions_widget;
|
||||
std::vector<std::unique_ptr<Widget>> missions;
|
||||
};
|
|
@ -33,7 +33,7 @@ public:
|
|||
virtual void draw() = 0;
|
||||
|
||||
protected:
|
||||
void recalculate_rect();
|
||||
void recalculate_pos();
|
||||
|
||||
SDL_Renderer* renderer;
|
||||
SDL_Rect rect;
|
||||
|
|
Binary file not shown.
49
src/AMI.cpp
49
src/AMI.cpp
|
@ -5,14 +5,16 @@
|
|||
#include "widgets.h"
|
||||
|
||||
#include <array>
|
||||
#include <memory>
|
||||
|
||||
constexpr const char* FT_LOGO_PATH = "resources/Fasttube_Logo-white.bmp";
|
||||
constexpr const char* AVENIR_FONT_PATH = "resources/Avenir-Book.ttf";
|
||||
constexpr int AVENIR_PTS = 20;
|
||||
constexpr const char* CHINAT_FONT_PATH = "resources/CHINAT.ttf";
|
||||
constexpr int CHINAT_PTS = 24;
|
||||
constexpr int FT_LOGO_HEIGHT = 40;
|
||||
constexpr int GAP = 5;
|
||||
constexpr int CHOOSE_Y = FT_LOGO_HEIGHT + GAP;
|
||||
constexpr int MISSION_HEIGHT = 30;
|
||||
|
||||
constexpr std::array MISSIONS = {"ACCELERATION", "SKIDPAD", "AUTOCROSS",
|
||||
"TRACKDRIVE", "EBS TEST", "INSPECTION",
|
||||
|
@ -20,31 +22,38 @@ constexpr std::array MISSIONS = {"ACCELERATION", "SKIDPAD", "AUTOCROSS",
|
|||
|
||||
AMI::AMI(SDL_Renderer* renderer)
|
||||
: View{renderer}, avenir{util::load_font(AVENIR_FONT_PATH, AVENIR_PTS)},
|
||||
ft_logo{renderer, FT_LOGO_PATH}, choose{renderer, avenir,
|
||||
"Choose a mission:"},
|
||||
missions_widget{renderer, MISSION_HEIGHT, Alignment::CENTER} {
|
||||
ft_logo.set_height(FT_LOGO_HEIGHT);
|
||||
ft_logo.set_position(SCREEN_WIDTH / 2, 0);
|
||||
ft_logo.set_alignment(Alignment::CENTER);
|
||||
widgets.push_back(&ft_logo);
|
||||
chinat{util::load_font(CHINAT_FONT_PATH, CHINAT_PTS)} {
|
||||
ft_logo = std::make_unique<ImageWidget>(renderer, FT_LOGO_PATH);
|
||||
ft_logo->set_height(FT_LOGO_HEIGHT);
|
||||
ft_logo->set_position(SCREEN_WIDTH / 2, 0);
|
||||
ft_logo->set_alignment(Alignment::CENTER);
|
||||
widgets.push_back(ft_logo.get());
|
||||
|
||||
choose.set_position(SCREEN_WIDTH / 2, CHOOSE_Y);
|
||||
choose.set_alignment(Alignment::CENTER);
|
||||
widgets.push_back(&choose);
|
||||
choose = std::make_unique<TextWidget>(renderer, avenir, "Choose a mission:");
|
||||
choose->set_position(SCREEN_WIDTH / 2, CHOOSE_Y);
|
||||
choose->set_alignment(Alignment::CENTER);
|
||||
widgets.push_back(choose.get());
|
||||
|
||||
int choose_height = choose.get_height();
|
||||
for (const auto mission : MISSIONS) {
|
||||
missions.push_back(std::make_unique<TextWidget>(renderer, chinat, mission));
|
||||
}
|
||||
int choose_height = choose->get_height();
|
||||
int missions_y = CHOOSE_Y + choose_height + GAP;
|
||||
int missions_height = SCREEN_HEIGHT - CHOOSE_Y - choose_height - 1;
|
||||
missions_widget.set_position(0, missions_y);
|
||||
missions_widget.set_width(SCREEN_WIDTH, false);
|
||||
missions_widget.set_height(missions_height, false);
|
||||
for (const auto mission : MISSIONS) {
|
||||
missions.push_back(std::make_unique<TextWidget>(renderer, avenir, mission));
|
||||
}
|
||||
missions_widget = std::make_unique<ListWidget>(
|
||||
renderer, missions[0]->get_height(), Alignment::CENTER);
|
||||
missions_widget->set_position(0, missions_y);
|
||||
missions_widget->set_width(SCREEN_WIDTH, false);
|
||||
missions_widget->set_height(missions_height, false);
|
||||
for (const auto& mission : missions) {
|
||||
missions_widget.add_element(mission.get());
|
||||
missions_widget->add_element(mission.get());
|
||||
}
|
||||
widgets.push_back(&missions_widget);
|
||||
widgets.push_back(missions_widget.get());
|
||||
}
|
||||
|
||||
AMI::~AMI() {
|
||||
TTF_CloseFont(avenir);
|
||||
TTF_CloseFont(chinat);
|
||||
}
|
||||
|
||||
void AMI::draw() {
|
||||
|
|
|
@ -21,12 +21,14 @@ void Widget::set_width(int width, bool preserve_aspect_ratio) {
|
|||
rect.h = round(rect.h * scale);
|
||||
}
|
||||
rect.w = width;
|
||||
recalculate_pos();
|
||||
}
|
||||
|
||||
void Widget::set_height(int height, bool preserve_aspect_ratio) {
|
||||
if (preserve_aspect_ratio) {
|
||||
float scale = ((float)height) / rect.h;
|
||||
rect.w = round(rect.w * scale);
|
||||
recalculate_pos();
|
||||
}
|
||||
rect.h = height;
|
||||
}
|
||||
|
@ -34,12 +36,12 @@ void Widget::set_height(int height, bool preserve_aspect_ratio) {
|
|||
void Widget::set_position(int x, int y) {
|
||||
pos.x = x;
|
||||
pos.y = y;
|
||||
recalculate_rect();
|
||||
recalculate_pos();
|
||||
}
|
||||
|
||||
void Widget::set_alignment(Alignment align) {
|
||||
pos.align = align;
|
||||
recalculate_rect();
|
||||
recalculate_pos();
|
||||
}
|
||||
|
||||
int Widget::get_width() { return rect.w; }
|
||||
|
@ -48,7 +50,7 @@ int Widget::get_height() { return rect.h; }
|
|||
|
||||
const PositionInfo& Widget::get_position() { return pos; }
|
||||
|
||||
void Widget::recalculate_rect() {
|
||||
void Widget::recalculate_pos() {
|
||||
int x = pos.x;
|
||||
int y = pos.y;
|
||||
switch (pos.align) {
|
||||
|
|
Loading…
Reference in New Issue