-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMenuMode.hpp
More file actions
97 lines (79 loc) · 3.48 KB
/
MenuMode.hpp
File metadata and controls
97 lines (79 loc) · 3.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#pragma once
/*
* MenuMode is a game mode that implements a multiple-choice system.
* Menu part implementation modified from previous year's base code
* https://github.com/15-466/15-466-f19-base6/blob/master/MenuMode.hpp
*
*/
#include "engine/Text.hpp"
#include "Sprite.hpp"
#include "Mode.hpp"
#include "engine/Transform2D.hpp"
#include "gamebase/ItemPrototype.hpp"
#include <vector>
#include <functional>
struct MenuMode : Mode {
struct Item;
int row_width;
Text text;
MenuMode(std::vector< Item > const& items, int width);
virtual ~MenuMode();
//functions called by main loop:
virtual bool handle_event(SDL_Event const&, glm::uvec2 const& window_size) override;
virtual void update(float elapsed) override;
virtual void draw(glm::uvec2 const& drawable_size) override;
virtual void on_leave() override;
virtual void on_enter() override;
//----- menu state -----
//Each menu item is an "Item":
struct Item {
Item(
std::string const& name_,
Sprite const* sprite_ = nullptr,
Sprite const* sprite_selected_ = nullptr,
ItemPrototype const* item_prototype_ = nullptr,
glm::u8vec4 const& tint_ = glm::u8vec4(0xff),
std::function< void(Item const&) > const& on_select_ = nullptr,
std::function< void(Item const&) > const& on_discard_ = nullptr
) : name(name_), sprite(sprite_), sprite_selected(sprite_selected_), item_prototype(item_prototype_), tint(tint_), selected_tint(tint_), on_select(on_select_), on_discard(on_discard_), transform(nullptr) {
}
std::string name;
Sprite const* sprite; //sprite drawn for item if not selected
Sprite const* sprite_selected; //sprite drawn for item if selected
ItemPrototype const* item_prototype;
glm::u8vec4 tint; //tint for sprite (unselected)
glm::u8vec4 selected_tint; //tint for sprite (selected)
std::function< void(Item const&) > on_select; //if set, item is selectable
std::function< void(Item const&) > on_discard;
Transform2D transform; //transform to draw item
};
std::vector< Item > items;
//call to arrange items in a centered list:
void vertical_layout_items(float gap = 0.0f);
void grid_layout_items(glm::vec2 const& top_left, float horizontal_gap, float vertical_gap, int start_idx, int end_idx);
//if set, used to highlight the current selection:
/*Sprite const* left_select = nullptr;
Sprite const* right_select = nullptr;
glm::vec2 left_select_offset = glm::vec2(0.0f);
glm::vec2 right_select_offset = glm::vec2(0.0f);
glm::u8vec4 left_select_tint = glm::u8vec4(0xff);
glm::u8vec4 right_select_tint = glm::u8vec4(0xff);
float select_bounce_amount = 0.0f;
float select_bounce_acc = 0.0f;*/
//must be set to the atlas from which all the sprites used herein are taken:
SpriteAtlas const* atlas = nullptr;
//currently selected item:
uint32_t selected = 0;
//area to display; by default, menu lays items out in the [-1,1]^2 box:
glm::vec2 view_min = glm::vec2(-1.0f, -1.0f);
glm::vec2 view_max = glm::vec2(1.0f, 1.0f);
//if not nullptr, background's functions are called as follows:
// background->handle_event() is called at the end of handle_event() [if this doesn't handle the event]
// background->update() is called at the end of update()
// background->draw() is called at the start of draw()
//IMPORTANT NOTE: this means that if background->draw() ends up deleting this (e.g., by removing
// the last shared_ptr that references it), then it will crash. Don't do that!
std::shared_ptr< Mode > background;
const std::string menu_font_file_name = "bulky-pixels.regular.ttf";
int font_size = 1300;
};