the game where you go into mines and start crafting! but for consoles (forked directly from smartcmd's github)
at main 358 lines 11 kB view raw
1#include "stdafx.h" 2#include "ScrolledSelectionList.h" 3#include "Button.h" 4#include "Tesselator.h" 5#include "Textures.h" 6#include "..\Minecraft.World\System.h" 7 8ScrolledSelectionList::ScrolledSelectionList(Minecraft *minecraft, int width, int height, int y0, int y1, int itemHeight) 9{ 10 this->minecraft = minecraft; 11 this->width = width; 12 this->height = height; 13 this->y0 = y0; 14 this->y1 = y1; 15 this->itemHeight = itemHeight; 16 this->x0 = 0; 17 this->x1 = width; 18 19 20 // 4J Stu - Smoe default initialisers 21 upId = 0; 22 downId = 0; 23 24 yDrag = 0.0f; 25 yDragScale = 0.0f; 26 yo = 0.0f; 27 28 lastSelection = 0; 29 lastSelectionTime = 0; 30 31 renderSelection = false; 32 _renderHeader = false; 33 headerHeight = 0; 34 //End 35} 36 37void ScrolledSelectionList::setRenderSelection(bool renderSelection) 38{ 39 this->renderSelection = renderSelection; 40} 41 42void ScrolledSelectionList::setRenderHeader(bool renderHeader, int headerHeight) 43{ 44 this->_renderHeader = renderHeader; 45 this->headerHeight = headerHeight; 46 47 if (!_renderHeader) 48 { 49 this->headerHeight = 0; 50 } 51} 52 53int ScrolledSelectionList::getMaxPosition() 54{ 55 return getNumberOfItems() * itemHeight + headerHeight; 56} 57 58void ScrolledSelectionList::renderHeader(int x, int y, Tesselator *t) 59{ 60} 61 62void ScrolledSelectionList::clickedHeader(int headerMouseX, int headerMouseY) 63{ 64} 65 66void ScrolledSelectionList::renderDecorations(int mouseX, int mouseY) 67{ 68} 69 70 int ScrolledSelectionList::getItemAtPosition(int x, int y) 71{ 72 int x0 = width / 2 - (92 + 16 + 2); 73 int x1 = width / 2 + (92 + 16 + 2); 74 75 int clickSlotPos = (y - y0 - headerHeight + (int) yo - 4); 76 int slot = clickSlotPos / itemHeight; 77 if (x >= x0 && x <= x1 && slot >= 0 && clickSlotPos >= 0 && slot < getNumberOfItems()) 78 { 79 return slot; 80 } 81 return -1; 82} 83 84void ScrolledSelectionList::init(vector<Button *> *buttons, int upButtonId, int downButtonId) 85{ 86 this->upId = upButtonId; 87 this->downId = downButtonId; 88} 89 90void ScrolledSelectionList::capYPosition() 91{ 92 int max = getMaxPosition() - (y1 - y0 - 4); 93 if (max < 0) max /= 2; 94 if (yo < 0) yo = 0; 95 if (yo > max) yo = (float)max; 96} 97 98void ScrolledSelectionList::buttonClicked(Button *button) 99{ 100 if (!button->active) return; 101 102 if (button->id == upId) 103 { 104 yo -= (itemHeight * 2) / 3; 105 yDrag = DRAG_OUTSIDE; 106 capYPosition(); 107 } else if (button->id == downId) 108 { 109 yo += (itemHeight * 2) / 3; 110 yDrag = DRAG_OUTSIDE; 111 capYPosition(); 112 } 113} 114 115void ScrolledSelectionList::render(int xm, int ym, float a) 116{ 117 // 4J Unused 118#if 0 119 renderBackground(); 120 121 int itemCount = getNumberOfItems(); 122 123 int xx0 = width / 2 + 124; 124 int xx1 = xx0 + 6; 125 126 if (Mouse::isButtonDown(0)) 127 { 128 if (yDrag == NO_DRAG) 129 { 130 bool doDrag = true; 131 if (ym >= y0 && ym <= y1) 132 { 133 int x0 = width / 2 - (92 + 16 + 2); 134 int x1 = width / 2 + (92 + 16 + 2); 135 136 int clickSlotPos = (ym - y0 - headerHeight + (int) yo - 4); 137 int slot = clickSlotPos / itemHeight; 138 if (xm >= x0 && xm <= x1 && slot >= 0 && clickSlotPos >= 0 && slot < itemCount) 139 { 140 141 bool doubleClick = slot == lastSelection && (System::currentTimeMillis() - lastSelectionTime) < 250; 142 143 selectItem(slot, doubleClick); 144 lastSelection = slot; 145 lastSelectionTime = System::currentTimeMillis(); 146 } 147 else if (xm >= x0 && xm <= x1 && clickSlotPos < 0) 148 { 149 clickedHeader(xm - x0, ym - y0 + (int) yo - 4); 150 doDrag = false; 151 } 152 if (xm >= xx0 && xm <= xx1) 153 { 154 yDragScale = -1; 155 156 int max = getMaxPosition() - (y1 - y0 - 4); 157 if (max < 1) max = 1; 158 int barHeight = (int) ((y1 - y0) * (y1 - y0) / (float) getMaxPosition()); 159 if (barHeight < 32) barHeight = 32; 160 if (barHeight > (y1 - y0 - 8)) barHeight = (y1 - y0 - 8); 161 162// int yp = yo * (y1 - y0 - barHeight) / max + y0; 163// if (yp < y0) yp = y0; 164 165 yDragScale /= (y1 - y0 - barHeight) / (float) max; 166 167 } 168 else 169 { 170 yDragScale = 1; 171 } 172 if (doDrag) 173 { 174 yDrag = (float)ym; 175 } 176 else 177 { 178 yDrag = DRAG_OUTSIDE; 179 } 180 } 181 else 182 { 183 yDrag = DRAG_OUTSIDE; 184 } 185 } 186 else if (yDrag >= 0) 187 { 188 yo -= (ym - yDrag) * yDragScale; 189 yDrag = (float)ym; 190 } 191 192 } 193 else 194 { 195 yDrag = NO_DRAG; 196 } 197 198 capYPosition(); 199 200 glDisable(GL_LIGHTING); 201 glDisable(GL_FOG); 202 Tesselator *t = Tesselator::getInstance(); 203 204 glBindTexture(GL_TEXTURE_2D, minecraft->textures->loadTexture(L"/gui/background.png")); 205 glColor4f(1.0f, 1, 1, 1); 206 float s = 32; 207 t->begin(); 208 t->color(0x202020); 209 t->vertexUV((float)(x0), (float)( y1), (float)( 0), (float)( x0 / s), (float)( (y1 + (int) yo) / s)); 210 t->vertexUV((float)(x1), (float)( y1), (float)( 0), (float)( x1 / s), (float)( (y1 + (int) yo) / s)); 211 t->vertexUV((float)(x1), (float)( y0), (float)( 0), (float)( x1 / s), (float)( (y0 + (int) yo) / s)); 212 t->vertexUV((float)(x0), (float)( y0), (float)( 0), (float)( x0 / s), (float)( (y0 + (int) yo) / s)); 213 t->end(); 214 215 int rowX = width / 2 - 92 - 16; 216 int rowBaseY = y0 + 4 - (int) yo; 217 218 if (_renderHeader) 219 { 220 renderHeader(rowX, rowBaseY, t); 221 } 222 223 for (int i = 0; i < itemCount; i++) 224 { 225 226 int y = rowBaseY + (i) * itemHeight + headerHeight; 227 int h = itemHeight - 4; 228 229 if (y > y1 || (y + h) < y0) 230 { 231 continue; 232 } 233 234 if (renderSelection && isSelectedItem(i)) 235 { 236 int x0 = width / 2 - (92 + 16 + 2); 237 int x1 = width / 2 + (92 + 16 + 2); 238 glColor4f(1, 1, 1, 1); 239 glDisable(GL_TEXTURE_2D); 240 t->begin(); 241 t->color(0x808080); 242 t->vertexUV((float)(x0), (float)( y + h + 2), (float)( 0), (float)( 0), (float)( 1)); 243 t->vertexUV((float)(x1), (float)( y + h + 2), (float)( 0), (float)( 1), (float)( 1)); 244 t->vertexUV((float)(x1), (float)( y - 2), (float)( 0), (float)( 1), (float)( 0)); 245 t->vertexUV((float)(x0), (float)( y - 2), (float)( 0), (float)( 0), (float)( 0)); 246 247 t->color(0x000000); 248 t->vertexUV((float)(x0 + 1), (float)( y + h + 1), (float)( 0), (float)( 0), (float)( 1)); 249 t->vertexUV((float)(x1 - 1), (float)( y + h + 1), (float)( 0), (float)( 1), (float)( 1)); 250 t->vertexUV((float)(x1 - 1), (float)( y - 1), (float)( 0), (float)( 1), (float)( 0)); 251 t->vertexUV((float)(x0 + 1), (float)( y - 1), (float)( 0), (float)( 0), (float)( 0)); 252 253 t->end(); 254 glEnable(GL_TEXTURE_2D); 255 } 256 257 renderItem(i, rowX, y, h, t); 258 259 } 260 261 glDisable(GL_DEPTH_TEST); 262 263 264 int d = 4; 265 266 renderHoleBackground(0, y0, 255, 255); 267 renderHoleBackground(y1, height, 255, 255); 268 269 glEnable(GL_BLEND); 270 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); 271 glDisable(GL_ALPHA_TEST); 272 glShadeModel(GL_SMOOTH); 273 274 glDisable(GL_TEXTURE_2D); 275 276 t->begin(); 277 t->color(0x000000, 0); 278 t->vertexUV((float)(x0), (float)( y0 + d), (float)( 0), (float)( 0), (float)( 1)); 279 t->vertexUV((float)(x1), (float)( y0 + d), (float)( 0), (float)( 1), (float)( 1)); 280 t->color(0x000000, 255); 281 t->vertexUV((float)(x1), (float)( y0), (float)( 0), (float)( 1), (float)( 0)); 282 t->vertexUV((float)(x0), (float)( y0), (float)( 0), (float)( 0), (float)( 0)); 283 t->end(); 284 285 t->begin(); 286 t->color(0x000000, 255); 287 t->vertexUV((float)(x0), (float)( y1), (float)( 0), (float)( 0), (float)( 1)); 288 t->vertexUV((float)(x1), (float)( y1), (float)( 0), (float)( 1), (float)( 1)); 289 t->color(0x000000, 0); 290 t->vertexUV((float)(x1), (float)( y1 - d), (float)( 0), (float)( 1), (float)( 0)); 291 t->vertexUV((float)(x0), (float)( y1 - d), (float)( 0), (float)( 0), (float)( 0)); 292 t->end(); 293 294 { 295 int max = getMaxPosition() - (y1 - y0 - 4); 296 if (max > 0) 297 { 298 int barHeight = (int) (y1 - y0) * (y1 - y0) / (getMaxPosition()); 299 if (barHeight < 32) barHeight = 32; 300 if (barHeight > (y1 - y0 - 8)) barHeight = (y1 - y0 - 8); 301 302 int yp = (int) yo * (y1 - y0 - barHeight) / max + y0; 303 if (yp < y0) yp = y0; 304 305 t->begin(); 306 t->color(0x000000, 255); 307 t->vertexUV((float)(xx0), (float)( y1), (float)( 0), (float)( 0), (float)( 1)); 308 t->vertexUV((float)(xx1), (float)( y1), (float)( 0), (float)( 1), (float)( 1)); 309 t->vertexUV((float)(xx1), (float)( y0), (float)( 0), (float)( 1), (float)( 0)); 310 t->vertexUV((float)(xx0), (float)( y0), (float)( 0), (float)( 0), (float)( 0)); 311 t->end(); 312 313 t->begin(); 314 t->color(0x808080, 255); 315 t->vertexUV((float)(xx0), (float)( yp + barHeight), (float)( 0), (float)( 0), (float)( 1)); 316 t->vertexUV((float)(xx1), (float)( yp + barHeight), (float)( 0), (float)( 1), (float)( 1)); 317 t->vertexUV((float)(xx1), (float)( yp), (float)( 0), (float)( 1), (float)( 0)); 318 t->vertexUV((float)(xx0), (float)( yp), (float)( 0), (float)( 0), (float)( 0)); 319 t->end(); 320 321 t->begin(); 322 t->color(0xc0c0c0, 255); 323 t->vertexUV((float)(xx0), (float)( yp + barHeight - 1), (float)( 0), (float)( 0), (float)( 1)); 324 t->vertexUV((float)(xx1 - 1), (float)( yp + barHeight - 1), (float)( 0), (float)( 1), (float)( 1)); 325 t->vertexUV((float)(xx1 - 1), (float)( yp), (float)( 0), (float)( 1), (float)( 0)); 326 t->vertexUV((float)(xx0), (float)( yp), (float)( 0), (float)( 0), (float)( 0)); 327 t->end(); 328 } 329 } 330 331 renderDecorations(xm, ym); 332 333 glEnable(GL_TEXTURE_2D); 334 335 glShadeModel(GL_FLAT); 336 glEnable(GL_ALPHA_TEST); 337 glDisable(GL_BLEND); 338#endif 339} 340 341void ScrolledSelectionList::renderHoleBackground(int y0, int y1, int a0, int a1) 342{ 343 // 4J Unused 344#if 0 345 Tesselator *t = Tesselator::getInstance(); 346 glBindTexture(GL_TEXTURE_2D, minecraft->textures->loadTexture(L"/gui/background.png")); 347 glColor4f(1.0f, 1, 1, 1); 348 float s = 32; 349 t->begin(); 350 t->color(0x404040, a1); 351 t->vertexUV((float)(0), (float)( y1), (float)( 0), (float)( 0), (float)( y1 / s)); 352 t->vertexUV((float)(width), (float)( y1), (float)( 0), (float)( width / s), (float)( y1 / s)); 353 t->color(0x404040, a0); 354 t->vertexUV((float)(width), (float)( y0), (float)( 0), (float)( width / s), (float)( y0 / s)); 355 t->vertexUV((float)(0), (float)( y0), (float)( 0), (float)( 0), (float)( y0 / s)); 356 t->end(); 357#endif 358}