-
Notifications
You must be signed in to change notification settings - Fork 14
/
blocklisttask.cpp
200 lines (172 loc) · 6.23 KB
/
blocklisttask.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#include "blocklisttask.h"
#include <algorithm>
#include "blockrenderer.h"
#include "font.h"
#include "inventory.h"
#include "terrain.h"
#include "texturetools.h"
#include "worldtask.h"
BlockListTask block_list_task;
static const BLOCK_WDATA user_selectable[] = {
BLOCK_STONE,
BLOCK_COBBLESTONE,
BLOCK_DIRT,
BLOCK_GRASS,
BLOCK_SAND,
BLOCK_WOOD,
BLOCK_LEAVES,
BLOCK_PLANKS_NORMAL,
BLOCK_PLANKS_DARK,
BLOCK_PLANKS_BRIGHT,
BLOCK_WALL,
BLOCK_GLASS,
BLOCK_DOOR,
BLOCK_COAL_ORE,
BLOCK_GOLD_ORE,
BLOCK_IRON_ORE,
BLOCK_DIAMOND_ORE,
BLOCK_REDSTONE_ORE,
BLOCK_IRON,
BLOCK_GOLD,
BLOCK_DIAMOND,
BLOCK_GLOWSTONE,
BLOCK_NETHERRACK,
BLOCK_TNT,
BLOCK_SPONGE,
BLOCK_FURNACE,
BLOCK_CRAFTING_TABLE,
BLOCK_BOOKSHELF,
BLOCK_PUMPKIN,
getBLOCKWDATA(BLOCK_WATER, RANGE_WATER),
getBLOCKWDATA(BLOCK_LAVA, RANGE_LAVA),
getBLOCKWDATA(BLOCK_FLOWER, 0),
getBLOCKWDATA(BLOCK_FLOWER, 1),
getBLOCKWDATA(BLOCK_MUSHROOM, 0),
getBLOCKWDATA(BLOCK_MUSHROOM, 1),
getBLOCKWDATA(BLOCK_WHEAT, 0),
BLOCK_SPIDERWEB,
BLOCK_TORCH,
BLOCK_CAKE,
BLOCK_REDSTONE_LAMP,
BLOCK_REDSTONE_SWITCH,
BLOCK_PRESSURE_PLATE,
BLOCK_REDSTONE_WIRE,
BLOCK_REDSTONE_TORCH
};
//The values have to stay somewhere
unsigned int BlockListTask::blocklist_top;
//Black texture as background
TEXTURE *BlockListTask::blocklist_background;
constexpr int user_selectable_count = sizeof(user_selectable)/sizeof(*user_selectable);
BlockListTask::BlockListTask()
{
blocklist_top = (SCREEN_HEIGHT - blocklist_height - current_inventory.height()) / 2;
static_assert(field_width * fields_x <= SCREEN_WIDTH, "fields_x too high");
static_assert(fields_x * fields_y >= sizeof(user_selectable)/sizeof(*user_selectable), "Not enough fields");
if(blocklist_height + current_inventory.height() > SCREEN_WIDTH)
printf("fields_y too high\n");
blocklist_background = newTexture(blocklist_width, blocklist_height, 0, false);
}
BlockListTask::~BlockListTask()
{
deleteTexture(blocklist_background);
}
void BlockListTask::makeCurrent()
{
if(!background_saved)
saveBackground();
Task::makeCurrent();
}
void BlockListTask::render()
{
drawBackground();
drawTextureOverlay(*blocklist_background, 0, 0, *screen, blocklist_left, blocklist_top, blocklist_background->width, blocklist_background->height);
int block_nr = 0;
int screen_x, screen_y = blocklist_top + pad_y;
for(int y = 0; y < fields_y; y++, screen_y += field_height)
{
screen_x = blocklist_left + pad_x;
for(int x = 0; x < fields_x; x++, screen_x += field_width)
{
//BLOCK_DOOR is twice as high, so center it manually
if(getBLOCK(user_selectable[block_nr]) == BLOCK_DOOR)
global_block_renderer.drawPreview(user_selectable[block_nr], *screen, screen_x + pad_x, screen_y + pad_y_door);
else
global_block_renderer.drawPreview(user_selectable[block_nr], *screen, screen_x + pad_y, screen_y + pad_y);
block_nr++;
if(block_nr == user_selectable_count)
goto end;
}
}
end:
//Draw the selection indicator
screen_x = blocklist_left + pad_x + field_width * (current_selection % fields_x);
screen_y = blocklist_top + pad_y + field_height * (current_selection / fields_x);
drawTexture(*inv_selection_p, *screen, 0, 0, inv_selection_p->width, inv_selection_p->height, screen_x + pad_x - 11, screen_y + pad_y - 10, inv_selection_p->width, inv_selection_p->height);
current_inventory.draw(*screen);
drawStringCenter(global_block_renderer.getName(user_selectable[current_selection]), 0xFFFF, *screen, SCREEN_WIDTH / 2, SCREEN_HEIGHT - current_inventory.height() - fontHeight());
}
void BlockListTask::logic()
{
if(key_held_down)
key_held_down = keyPressed(KEY_NSPIRE_ESC) || keyPressed(KEY_NSPIRE_PERIOD) || keyPressed(KEY_NSPIRE_2) || keyPressed(KEY_NSPIRE_8) || keyPressed(KEY_NSPIRE_4) || keyPressed(KEY_NSPIRE_6) || keyPressed(KEY_NSPIRE_1) || keyPressed(KEY_NSPIRE_3) || keyPressed(KEY_NSPIRE_5) || keyPressed(KEY_NSPIRE_UP) || keyPressed(KEY_NSPIRE_DOWN) || keyPressed(KEY_NSPIRE_LEFT) || keyPressed(KEY_NSPIRE_RIGHT) || keyPressed(KEY_NSPIRE_CLICK);
else if(keyPressed(KEY_NSPIRE_ESC) || keyPressed(KEY_NSPIRE_PERIOD))
{
world_task.makeCurrent();
key_held_down = true;
}
else if(keyPressed(KEY_NSPIRE_2) || keyPressed(KEY_NSPIRE_DOWN))
{
current_selection += fields_x;
if(current_selection >= user_selectable_count)
current_selection %= fields_x;
key_held_down = true;
}
else if(keyPressed(KEY_NSPIRE_8) || keyPressed(KEY_NSPIRE_UP))
{
if(current_selection >= fields_x)
current_selection -= fields_x;
else
{
current_selection = ((user_selectable_count - 1) / fields_x) * fields_x + (current_selection % fields_x);
if(current_selection >= user_selectable_count)
current_selection -= fields_x;
}
key_held_down = true;
}
else if(keyPressed(KEY_NSPIRE_4) || keyPressed(KEY_NSPIRE_LEFT))
{
if(current_selection % fields_x == 0)
{
current_selection += fields_x - 1;
if(current_selection >= user_selectable_count)
current_selection = user_selectable_count - 1;
}
else
current_selection--;
key_held_down = true;
}
else if(keyPressed(KEY_NSPIRE_6) || keyPressed(KEY_NSPIRE_RIGHT))
{
if(current_selection % fields_x != fields_x-1 && current_selection < user_selectable_count - 1)
current_selection++;
else
current_selection -= current_selection % fields_x;
key_held_down = true;
}
else if(keyPressed(KEY_NSPIRE_1)) //Switch inventory slot
{
current_inventory.previousSlot();
key_held_down = true;
}
else if(keyPressed(KEY_NSPIRE_3))
{
current_inventory.nextSlot();
key_held_down = true;
}
else if(keyPressed(KEY_NSPIRE_5) || keyPressed(KEY_NSPIRE_CLICK))
{
current_inventory.currentSlot() = user_selectable[current_selection];
key_held_down = true;
}
}