LayoutManager

This commit is contained in:
Henrik Rydgard 2012-04-12 19:45:21 +02:00
parent 7772433257
commit 8656ec051c
2 changed files with 63 additions and 8 deletions

View file

@ -79,8 +79,12 @@ void UIText(int font, int x, int y, const char *text, uint32_t color, float scal
ui_draw2d.SetFontScale(1.0f, 1.0f);
}
int UIButton(int id, int x, int y, int w, const char *text, int button_align) {
const int h = 64;
int UIButton(int id, const LayoutManager &layout, float w, const char *text, int button_align) {
float h = themeAtlas->images[themeButtonImage].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;
@ -122,8 +126,11 @@ int UIButton(int id, int x, int y, int w, const char *text, int button_align) {
return clicked;
}
int UIImageButton(int id, int x, int y, int w, int image, int button_align) {
const int h = 64;
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;

56
ui/ui.h
View file

@ -13,8 +13,10 @@
// multiple parts of a single screen of UI over multiple files unless you use IMGUI_SRC_ID.
#ifdef IMGUI_SRC_ID
#define GEN_ID ((IMGUI_SRC_ID) + (__LINE__))
#define GEN_ID_LOOP(i) ((IMGUI_SRC_ID) + (__LINE__) + (i) * 1000)
#else
#define GEN_ID (__LINE__)
#define GEN_ID_LOOP(i) ((__LINE__) + (i) * 1000)
#endif
#include "gfx_es2/draw_buffer.h"
@ -22,6 +24,54 @@
#include <string>
#include <vector>
class LayoutManager {
public:
virtual void GetPos(float *w, float *h, float *x, float *y) const = 0;
};
class Pos : public LayoutManager {
public:
Pos(float x, float y) : x_(x), y_(y) {}
virtual void GetPos(float *w, float *h, float *x, float *y) const {
*x = x_;
*y = y_;
}
private:
float x_;
float y_;
};
class HLinear : public LayoutManager {
public:
HLinear(float x, float y, float spacing = 2.0f) : x_(x), y_(y), spacing_(spacing) {}
virtual void GetPos(float *w, float *h, float *x, float *y) const {
*x = x_;
*y = y_;
x_ += *w + spacing_;
}
private:
mutable float x_;
float y_;
float spacing_;
};
class VLinear : public LayoutManager {
public:
VLinear(float x, float y, float spacing = 2.0f) : x_(x), y_(y), spacing_(spacing) {}
virtual void GetPos(float *w, float *h, float *x, float *y) const {
*x = x_;
*y = y_;
y_ += *h + spacing_;
}
private:
float x_;
mutable float y_;
float spacing_;
};
// Mouse out of habit, applies just as well to touch events.
// UI does not yet support multitouch.
struct UIState {
@ -112,14 +162,12 @@ void UIBegin();
void UIUpdateMouse(float x, float y, int buttons);
// Returns 1 if clicked
int UIButton(int id, int x, int y, int w, const char *text, int button_align);
int UIButton(int id, const LayoutManager &layout, float w, const char *text, int button_align);
int UIImageButton(int id, const LayoutManager &layout, float w, int image_id, int button_align); // uses current UI atlas for fetching images.
// Returns 1 if clicked, puts the value in *value (where it also gets the current state).
int UICheckBox(int id, int x, int y, const char *text, int align, bool *value);
// Just like a button, but with an image, doh.
int UIImageButton(int id, int x, int y, int w, int image_id, int button_align); // uses current UI atlas for fetching images.
// Vertical slider. Not yet working.
int UIVSlider(int id, int x, int y, int h, int max, int *value);