forked from Vogtinator/crafti
-
Notifications
You must be signed in to change notification settings - Fork 0
/
menutask.cpp
139 lines (114 loc) · 3.66 KB
/
menutask.cpp
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#include "menutask.h"
#include "texturetools.h"
#include "worldtask.h"
#include "helptask.h"
#include "settingstask.h"
#include "textures/menu.h"
#include "textures/selection.h"
MenuTask menu_task;
MenuTask::MenuTask()
{
menu_with_selection = newTexture(menu.width, menu.height);
}
MenuTask::~MenuTask()
{
deleteTexture(menu_with_selection);
}
void MenuTask::makeCurrent()
{
menu_open = true;
menu_width_visible = 0;
menu_selected_item = SAVE_WORLD;
if(!background_saved)
saveBackground();
Task::makeCurrent();
}
void MenuTask::render()
{
drawBackground();
copyTexture(menu, *menu_with_selection);
const int selection_y[MENU_ITEM_MAX] = { 14, 36, 63, 88, 112, 136 };
drawTexture(selection, *menu_with_selection, 0, 0, selection.width, selection.height, 23, selection_y[menu_selected_item], selection.width, selection.height);
drawTexture(*menu_with_selection, *screen, 0, 0, menu_width_visible, menu_with_selection->height, SCREEN_WIDTH - menu_width_visible, 0, menu_width_visible, menu_with_selection->height);
}
void MenuTask::logic()
{
//Slide menu
if(menu_open && static_cast<unsigned int>(menu_width_visible) < menu_with_selection->width)
menu_width_visible += 10;
else if(!menu_open && menu_width_visible > 0)
menu_width_visible -= 10;
if(menu_width_visible < 0)
menu_width_visible = 0;
else if(static_cast<unsigned int>(menu_width_visible) > menu.width)
menu_width_visible = menu.width;
//Wait for the menu to be closed, then set the current task
if(!menu_open && menu_width_visible == 0)
{
switch(menu_selected_item)
{
case HELP:
help_task.makeCurrent();
break;
case SETTINGS:
settings_task.makeCurrent();
world_task.setMessage("Settings applied.");
break;
default:
world_task.makeCurrent();
break;
}
return;
}
if(key_held_down)
key_held_down = keyPressed(KEY_NSPIRE_CLICK) || keyPressed(KEY_NSPIRE_UP) || keyPressed(KEY_NSPIRE_DOWN) || keyPressed(KEY_NSPIRE_8) || keyPressed(KEY_NSPIRE_2) || keyPressed(KEY_NSPIRE_5) || keyPressed(KEY_NSPIRE_MENU) || keyPressed(KEY_NSPIRE_ESC);
else if(keyPressed(KEY_NSPIRE_8) || keyPressed(KEY_NSPIRE_UP))
{
--menu_selected_item;
if(menu_selected_item < 0)
menu_selected_item = MENU_ITEM_MAX - 1;
key_held_down = true;
}
else if(keyPressed(KEY_NSPIRE_2) || keyPressed(KEY_NSPIRE_DOWN))
{
++menu_selected_item;
if(menu_selected_item == MENU_ITEM_MAX)
menu_selected_item = 0;
key_held_down = true;
}
else if(keyPressed(KEY_NSPIRE_5) || keyPressed(KEY_NSPIRE_CLICK))
{
switch(menu_selected_item)
{
case NEW_WORLD:
world_task.resetWorld();
break;
case LOAD_WORLD:
if(load())
world_task.setMessage("World loaded.");
else
world_task.setMessage("World failed to load.");
break;
case SAVE_WORLD:
if(save())
world_task.setMessage("World saved.");
else
world_task.setMessage("Failed to save world.");
break;
case EXIT:
running = false;
break;
case HELP:
case SETTINGS:
//Handled above, at the start of this function
break;
}
menu_open = false;
key_held_down = true;
}
else if(keyPressed(KEY_NSPIRE_MENU) || keyPressed(KEY_NSPIRE_ESC))
{
menu_open = false;
key_held_down = true;
}
}