bool GuiButton(ui_context & ui, const ui_id id, string sprite, Transform & transform, int x, int y, int width, int height, const mouse_info & mouse) { bool result = false; bool mouseover = false; int frame = 0; int x1 = x - width / 2; int x2 = x + width / 2; int y1 = y - height / 2; int y2 = y + height / 2; if (!result && (mouse.x > x1 && mouse.x < x2) && (mouse.y > y1 && mouse.y < y2)) { mouseover = true; } if (ui.active == id) { frame = 2; if (mouse.LMB == false) { if (mouseover && ui.hot == id) { result = true; } ui.active.owner = NULL; } } else if (ui.hot == id) { if (!mouseover) { ui.hot == NULL; } else if (mouse.LMB == true) { ui.active = id; } } if (mouseover && ui.active.owner == NULL) { ui.hot = id; frame = 1; } // get dimensions of sprite SpritePtr btnSprite = g_Sprite.GetSprite(sprite); const ivec4 & size = btnSprite->GetSize(); double wRatio = (double) width / (double) size.x; double hRatio = (double) height / (double) size.y; // draw the button MatrixStack * mv = transform.GetModelViewStack(); mv->PushMatrix(); mv->Translate((float)x, (float)y, 0); mv->Scale((float)wRatio, (float)hRatio, 1); btnSprite->Draw(frame, transform); mv->PopMatrix(); return result; }
void Sprite::Draw(int frame, Transform & transform, int anchor, vec4 color) { // calculate st.uv texture offsets assert(frame >= 0 && frame < frameCount); int cols = size.z; int rows = size.w; vec4 offset; // 0 1 2 3 4 // 5 6 7 8 9 // 10 11 12 13 14 // 15 16 17 18 19 // 20 21 22 23 24 int frameCol = frame % cols; int frameRow = frame / cols; // 0 - row 0, col 0 // 12 - row 2, col 2 // 24 - row 4, col 4 float tX = 1.f / cols; float tY = 1.f / rows; offset.x = tX * frameCol; offset.y = tY * frameRow; MatrixStack * mv = transform.GetModelViewStack(); mv->PushMatrix(); vec2 renderPos = GetAnchorCoords(anchor, vec2(0, 0), vec2(size.x, size.y)); renderPos += (size.xy() / 2); mv->Translate(renderPos.x, renderPos.y, 0); g_Sprite.SetupShader(transform.GetMVP(), offset, texture, color); buffer.render(GL_TRIANGLE_STRIP); mv->PopMatrix(); }