// TODO int UIHSlider(int id, int x, int y, int w, int max, int *value) { // Calculate mouse cursor's relative y offset int xpos = ((256 - 16) * *value) / max; for (int i = 0; i < MAX_POINTERS; i++) { // Check for hotness if (UIRegionHit(i, x+8, y+8, 16, 255, 0)) { uistate.hotitem[i] = id; if (uistate.activeitem[i] == 0 && uistate.mousedown[i]) uistate.activeitem[i] = id; } // Update widget value if (uistate.activeitem[i] == id) { int mousepos = uistate.mousey[i] - (y + 8); if (mousepos < 0) mousepos = 0; if (mousepos > 255) mousepos = 255; int v = (mousepos * max) / 255; if (v != *value) { *value = v; return 1; } } } // Render the scrollbar ui_draw2d.Rect(x, y, 32, 256+16, 0x777777); ui_draw2d.Rect(x+8+xpos, y+8, 16, 16, 0xffffff); return 0; }
int UIButton(int id, const LayoutManager &layout, float w, float h, const char *text, int button_align) { if (h == 0.0f) h = themeAtlas->images[theme.buttonImage].h; float x, y; layout.GetPos(&w, &h, &x, &y); if (button_align & ALIGN_HCENTER) x -= w / 2; if (button_align & ALIGN_VCENTER) y -= h / 2; if (button_align & ALIGN_RIGHT) x -= w; if (button_align & ALIGN_BOTTOM) y -= h; int txOffset = 0; int clicked = 0; for (int i = 0; i < MAX_POINTERS; i++) { // Check whether the button should be hot, use a generous margin for touch ease if (UIRegionHit(i, x, y, w, h, 8)) { uistate.hotitem[i] = id; if (uistate.activeitem[i] == 0 && uistate.mousedown[i]) { uistate.activeitem[i] = id; } } if (uistate.hotitem[i] == id) { if (uistate.activeitem[i] == id) { // Button is both 'hot' and 'active' txOffset = 2; } else { // Button is merely 'hot' } } else { // button is not hot, but it may be active } // If button is hot and active, but mouse button is not // down, the user must have clicked the button. if (uistate.mousedown[i] == 0 && uistate.hotitem[i] == id && uistate.activeitem[i] == id) { clicked = 1; } } // Render button if (h == themeAtlas->images[theme.buttonImage].h) ui_draw2d.DrawImage2GridH((txOffset && theme.buttonSelected) ? theme.buttonSelected : theme.buttonImage, x, y, x + w); else ui_draw2d.DrawImage4Grid((txOffset && theme.buttonSelected) ? theme.buttonSelected : theme.buttonImage, x, y, x + w, y + h); ui_draw2d.DrawTextShadow(theme.uiFont, text, x + w/2, y + h/2 + txOffset, 0xFFFFFFFF, ALIGN_HCENTER | ALIGN_VCENTER); uistate.lastwidget = id; return clicked; }
int UICheckBox(int id, int x, int y, const char *text, int align, bool *value) { #ifdef _WIN32 const int h = 32; #else const int h = 48; #endif float tw, th; ui_draw2d.MeasureText(theme.uiFont, text, &tw, &th); int w = themeAtlas->images[theme.checkOn].w + UI_SPACE + tw; if (align & ALIGN_HCENTER) x -= w / 2; if (align & ALIGN_VCENTER) y -= h / 2; if (align & ALIGN_RIGHT) x -= w; if (align & ALIGN_BOTTOMRIGHT) y -= h; int txOffset = 0; int clicked = 0; for (int i = 0; i < MAX_POINTERS; i++) { // Check whether the button should be hot if (UIRegionHit(i, x, y, w, h, 8)) { uistate.hotitem[i] = id; if (uistate.activeitem[i] == 0 && uistate.mousedown[i]) uistate.activeitem[i] = id; } // Render button if (uistate.hotitem[i] == id) { if (uistate.activeitem[i] == id) { // Button is both 'hot' and 'active' txOffset = 2; } else { // Button is merely 'hot' } } else { // button is not hot, but it may be active } // If button is hot and active, but mouse button is not // down, the user must have clicked the button. if (uistate.mousedown[i] == 0 && uistate.hotitem[i] == id && uistate.activeitem[i] == id) { *value = !(*value); clicked = 1; } } ui_draw2d.DrawImage((*value) ? theme.checkOn : theme.checkOff, x, y+h/2, 1.0f, 0xFFFFFFFF, ALIGN_LEFT | ALIGN_VCENTER); ui_draw2d.DrawTextShadow(theme.uiFont, text, x + themeAtlas->images[theme.checkOn].w + UI_SPACE, y + txOffset + h/2, 0xFFFFFFFF, ALIGN_LEFT | ALIGN_VCENTER); uistate.lastwidget = id; return clicked; }
int UIImageButton(int id, const LayoutManager &layout, float w, int image, int button_align) { float h = 64; float x, y; layout.GetPos(&w, &h, &x, &y); if (button_align & ALIGN_HCENTER) x -= w / 2; if (button_align & ALIGN_VCENTER) y -= h / 2; if (button_align & ALIGN_RIGHT) x -= w; if (button_align & ALIGN_BOTTOMRIGHT) y -= h; int txOffset = 0; int clicked = 0; for (int i = 0; i < MAX_POINTERS; i++) { // Check whether the button should be hot, use a generous margin for touch ease if (UIRegionHit(i, x, y, w, h, 8)) { uistate.hotitem[i] = id; if (uistate.activeitem[i] == 0 && uistate.mousedown[i]) uistate.activeitem[i] = id; } if (uistate.hotitem[i] == id) { if (uistate.activeitem[i] == id) { // Button is both 'hot' and 'active' txOffset = 2; } else { // Button is merely 'hot' } } else { // button is not hot, but it may be active§ } // If button is hot and active, but mouse button is not // down, the user must have clicked the button. if (uistate.mousedown[i] == 0 && uistate.hotitem[i] == id && uistate.activeitem[i] == id) { clicked = 1; } } // Render button ui_draw2d.DrawImage2GridH(theme.buttonImage, x, y, x + w); ui_draw2d.DrawImage(image, x + w/2, y + h/2 + txOffset, 1.0f, 0xFFFFFFFF, ALIGN_HCENTER | ALIGN_VCENTER); uistate.lastwidget = id; return clicked; }
int UIList::Do(int id, int x, int y, int w, int h, UIListAdapter *adapter) { int clicked = 0; // Pointer and focus handling // UIList only cares about the first pointer for simplicity. // Probably not much need to scroll one of these while dragging something // else. // TODO: Abstract this stuff out into EmulatePointerEvents for (int i = 0; i < 1; i++) { // Check for hover bool isInside = false; if (UIRegionHit(i, x, y, w, h, 0)) { isInside = true; uistate.hotitem[i] = id; if (uistate.activeitem[i] == 0 && uistate.mousedown[i]) { // Mousedown uistate.activeitem[i] = id; pointerDown(i, uistate.mousex[i], uistate.mousey[i]); } } if (uistate.activeitem[i] == id) { // NOTE: won't work with multiple pointers if (uistate.mousex[i] != lastX || uistate.mousey[i] != lastY) { pointerMove(i, uistate.mousex[i], uistate.mousey[i], isInside); } } // If button is hot and active, but mouse button is not // down, the user must have clicked a list item (unless after the last item). if (uistate.mousedown[i] == 0 && uistate.activeitem[i] == id && selected != -1) { if (uistate.hotitem[i] == id) { clicked = 1; } pointerUp(i, uistate.mousex[i], uistate.mousey[i], isInside); } } int itemHeight = adapter->itemHeight(0); int numItems = adapter->getCount(); // Cap total inertia if (inertiaY > 20) inertiaY = 20; if (inertiaY < -20) inertiaY = -20; float mouseY = uistate.mousey[0]; if (!uistate.mousedown[0]) { // Let it slide if the pointer is not down scrollY += inertiaY; } else if (scrolling /* && mouseY > y && mouseY < y + h*/ ) { // Pointer is down so stick to it scrollY = startScrollY - (uistate.mousey[0] - startDragY); } // Inertia gradually trails off inertiaY *= 0.92f; if (scrolling && fabsf(inertiaY) < 0.001f) scrolling = false; // Cap top and bottom softly. float maxScrollY = numItems * itemHeight - h; if (maxScrollY < 0.0f) maxScrollY = 0.0f; if (scrollY > maxScrollY) { scrollY -= 0.4f * (scrollY - maxScrollY); } else if (scrollY < 0.0f) { scrollY += 0.4f * -scrollY; } lastX = uistate.mousex[0]; lastY = uistate.mousey[0]; uistate.lastwidget = id; // Drawing and item hittesting // render items for (int i = 0; i < numItems; i++) { int item_y = y + i * itemHeight - scrollY; if (item_y >= y - itemHeight && item_y <= y + h) { for (int k = 0; k < 1; k++) { // MAX_POINTERS if we add back multitouch if (uistate.mousedown[k] && uistate.mouseframesdown[k] >= holdFramesClick && adapter->itemEnabled(i) && !scrolling && selected == -1 && UIRegionHit(k, x, item_y, w, itemHeight, 0)) { selected = i; } else if (scrolling) { selected = -1; } } adapter->drawItem(i, x, item_y, w, itemHeight, i == selected); } } // Prevent scroll-clicks from registering if (selected == -1) clicked = 0; // Otherwise, no clicky. return clicked; }
int UITextureButton(UIContext *ctx, int id, const LayoutManager &layout, float w, float h, Texture *texture, int button_align, uint32_t color, int drop_shadow) // uses current UI atlas for fetching images. { float x, y; layout.GetPos(&w, &h, &x, &y); if (button_align & ALIGN_HCENTER) x -= w / 2; if (button_align & ALIGN_VCENTER) y -= h / 2; if (button_align & ALIGN_RIGHT) x -= w; if (button_align & ALIGN_BOTTOMRIGHT) y -= h; int txOffset = 0; int clicked = 0; for (int i = 0; i < MAX_POINTERS; i++) { // Check whether the button should be hot, use a generous margin for touch ease if (UIRegionHit(i, x, y, w, h, 8)) { uistate.hotitem[i] = id; if (uistate.activeitem[i] == 0 && uistate.mousedown[i]) uistate.activeitem[i] = id; } if (uistate.hotitem[i] == id) { if (uistate.activeitem[i] == id) { // Button is both 'hot' and 'active' txOffset = 2; } else { // Button is merely 'hot' } } else { // button is not hot, but it may be active } // If button is hot and active, but mouse button is not // down, the user must have clicked the button. if (uistate.mousedown[i] == 0 && uistate.hotitem[i] == id && uistate.activeitem[i] == id) { clicked = 1; } } if (texture) { float tw = texture->Width(); float th = texture->Height(); // Adjust position so we don't stretch the image vertically or horizontally. // TODO: Add a param to specify fit? The below assumes it's never too wide. float nw = h * tw / th; x += (w - nw) / 2.0f; w = nw; } // Render button int dropsize = 10; if (drop_shadow && texture) { if (txOffset) { dropsize = 3; y += txOffset * 2; } ui_draw2d.DrawImage4Grid(drop_shadow, x - dropsize, y, x+w + dropsize, y+h+dropsize*1.5, blackAlpha(0.5f), 1.0f); ui_draw2d.Flush(true); } if (texture) { texture->Bind(0); } else { ui_draw2d.DrawImage2GridH(theme.buttonImage, x, y, x + w, color); ui_draw2d.Flush(); Texture::Unbind(); } ui_draw2d.DrawTexRect(x, y, x+w, y+h, 0, 0, 1, 1, color); ui_draw2d.Flush(); ctx->RebindTexture(); uistate.lastwidget = id; return clicked; }