Exemplo n.º 1
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;
}
Exemplo n.º 2
0
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;
}